metrics: Add function keyword to to helper metrics functions

Use the 'function' keyword to prevent bash aliases from colliding
with other function's name.

Signed-off-by: David Esparza <david.esparza.borquez@intel.com>
This commit is contained in:
David Esparza 2023-07-05 20:59:21 -06:00
parent 1ca17c2f70
commit 4e396e7285
No known key found for this signature in database
GPG Key ID: EABE0B1A98CC3B7A

View File

@ -47,14 +47,14 @@ quay.io/libpod"
#
# cmds=(“cmd1” “cmd2”)
# check_cmds "${cmds[@]}"
check_cmds()
function check_cmds()
{
local cmd req_cmds=( "$@" )
for cmd in "${req_cmds[@]}"; do
if ! command -v "$cmd" > /dev/null 2>&1; then
die "command $cmd not available"
fi
echo "command: $cmd: yes"
info "command: $cmd: yes"
done
}
@ -68,19 +68,20 @@ check_cmds()
#
# images=(“img1” “img2”)
# check_imgs "${images[@]}"
check_images()
function check_images()
{
local img req_images=( "$@" )
for img in "${req_images[@]}"; do
echo "ctr pull'ing: $img"
info "ctr pull'ing: $img"
if ! sudo "${CTR_EXE}" image pull "$img"; then
die "Failed to pull image $img"
fi
echo "ctr pull'd: $img"
info "ctr pull'd: $img"
done
}
generate_build_dockerfile() {
function generate_build_dockerfile()
{
local dockerfile="$1"
local image="$2"
local map_key="$3"
@ -99,14 +100,14 @@ generate_build_dockerfile() {
# This function performs a build on the image names
# passed in, to ensure that we have the latest changes from
# the dockerfiles
build_dockerfile_image()
function build_dockerfile_image()
{
local image="$1"
local dockerfile_path="$2"
local dockerfile_dir=${2%/*}
if [ -f "$dockerfile_path" ]; then
echo "docker building $image"
info "docker building $image"
if ! sudo "${DOCKER_EXE}" build --build-arg http_proxy="${http_proxy}" --build-arg https_proxy="${https_proxy}" --label "$image" --tag "${image}" -f "$dockerfile_path" "$dockerfile_dir"; then
die "Failed to docker build image $image"
fi
@ -119,7 +120,7 @@ build_dockerfile_image()
# This function removes the ctr image, builds a new one using a dockerfile
# and imports the image from docker to ctr
check_ctr_images()
function check_ctr_images()
{
local ctr_image="$1"
local dockerfile_path="$2"
@ -138,7 +139,7 @@ check_ctr_images()
# A one time (per uber test cycle) init that tries to get the
# system to a 'known state' as much as possible
metrics_onetime_init()
function metrics_onetime_init()
{
# The onetime init must be called once, and only once
if [ ! -z "$onetime_init_done" ]; then
@ -155,14 +156,14 @@ metrics_onetime_init()
# Print a banner to the logs noting clearly which test
# we are about to run
test_banner()
function test_banner()
{
echo -e "\n===== starting test [$1] ====="
info -e "\n===== starting test [$1] ====="
}
# Initialization/verification environment. This function makes
# minimal steps for metrics/tests execution.
init_env()
function init_env()
{
test_banner "${TEST_NAME}"
@ -183,7 +184,8 @@ init_env()
# This function checks if there are containers or
# shim/proxy/hypervisor processes up, if found, they are
# killed to start test with clean environment.
kill_processes_before_start() {
function kill_processes_before_start()
{
DOCKER_PROCS=$(sudo "${DOCKER_EXE}" ps -q)
[[ -n "${DOCKER_PROCS}" ]] && clean_env
@ -195,26 +197,29 @@ kill_processes_before_start() {
# Generate a random name - generally used when creating containers, but can
# be used for any other appropriate purpose
random_name() {
function random_name()
{
mktemp -u kata-XXXXXX
}
show_system_ctr_state() {
echo "Showing system state:"
echo " --Check containers--"
function show_system_ctr_state()
{
info "Showing system state:"
info " --Check containers--"
sudo "${CTR_EXE}" c list
echo " --Check tasks--"
info " --Check tasks--"
sudo "${CTR_EXE}" task list
local processes="containerd-shim-kata-v2"
for p in ${processes}; do
echo " --pgrep ${p}--"
info " --pgrep ${p}--"
pgrep -a ${p}
done
}
common_init(){
function common_init()
{
if [ "$CTR_RUNTIME" == "io.containerd.kata.v2" ] || [ "$RUNTIME" == "containerd-shim-kata-v2" ]; then
extract_kata_env
else
@ -225,17 +230,18 @@ common_init(){
fi
}
# Save the current KSM settings so we can restore them later
save_ksm_settings(){
echo "saving KSM settings"
function save_ksm_settings()
{
info "saving KSM settings"
ksm_stored_run=$(cat ${KSM_ENABLE_FILE})
ksm_stored_pages=$(cat ${KSM_ENABLE_FILE})
ksm_stored_sleep=$(cat ${KSM_ENABLE_FILE})
}
set_ksm_aggressive(){
echo "setting KSM to aggressive mode"
function set_ksm_aggressive()
{
info "setting KSM to aggressive mode"
# Flip the run off/on to ensure a restart/rescan
sudo bash -c "echo 0 > ${KSM_ENABLE_FILE}"
sudo bash -c "echo ${KSM_AGGRESIVE_PAGES} > ${KSM_PAGES_FILE}"
@ -245,7 +251,7 @@ set_ksm_aggressive(){
if [ "${KATA_HYPERVISOR}" == "qemu" ]; then
# Disable virtio-fs and save whether it was enabled previously
set_virtio_out=$(sudo -E PATH="$PATH" "${LIB_DIR}/../../.ci/set_kata_config.sh" shared_fs virtio-9p)
echo "${set_virtio_out}"
info "${set_virtio_out}"
grep -q "already" <<< "${set_virtio_out}" || was_virtio_fs=true;
fi
}
@ -256,8 +262,9 @@ restore_virtio_fs(){
info "Not restoring virtio-fs since it wasn't enabled previously"
}
restore_ksm_settings(){
echo "restoring KSM settings"
function restore_ksm_settings()
{
info "restoring KSM settings"
# First turn off the run to ensure if we are then re-enabling
# that any changes take effect
sudo bash -c "echo 0 > ${KSM_ENABLE_FILE}"
@ -267,15 +274,17 @@ restore_ksm_settings(){
[ "${KATA_HYPERVISOR}" == "qemu" ] && restore_virtio_fs
}
disable_ksm(){
echo "disabling KSM"
function disable_ksm()
{
info "disabling KSM"
sudo bash -c "echo 0 > ${KSM_ENABLE_FILE}"
[ "${KATA_HYPERVISOR}" == "qemu" ] && restore_virtio_fs
}
# See if KSM is enabled.
# If so, amend the test name to reflect that
check_for_ksm(){
function check_for_ksm()
{
if [ ! -f ${KSM_ENABLE_FILE} ]; then
return
fi
@ -294,7 +303,8 @@ check_for_ksm(){
# a full scan has managed to do few new merges)
#
# arg1 - timeout in seconds
wait_ksm_settle(){
function wait_ksm_settle()
{
[[ "$RUNTIME" == "runc" ]] || [[ "$CTR_RUNTIME" == "io.containerd.runc.v2" ]] && return
local t pcnt
local oldscan=-1 newscan
@ -305,7 +315,7 @@ wait_ksm_settle(){
# Wait some time for KSM to kick in to avoid early dismissal
for ((t=0; t<5; t++)); do
pages=$(cat "${KSM_PAGES_SHARED}")
[[ "$pages" -ne 0 ]] && echo "Discovered KSM activity" && break
[[ "$pages" -ne 0 ]] && info "Discovered KSM activity" && break
sleep 1
done
@ -315,13 +325,13 @@ wait_ksm_settle(){
newscan=$(cat /sys/kernel/mm/ksm/full_scans)
newpages=$(cat "${KSM_PAGES_SHARED}")
[[ "$newpages" -eq 0 ]] && echo "No need to wait for KSM to settle" && return
[[ "$newpages" -eq 0 ]] && info "No need to wait for KSM to settle" && return
if (( newscan != oldscan )); then
echo -e "\nnew full_scan ($oldscan to $newscan)"
info -e "\nnew full_scan ($oldscan to $newscan)"
# Do we have a previous scan to compare with
echo "check pages $oldpages to $newpages"
info "check pages $oldpages to $newpages"
if (( oldpages != -1 )); then
# avoid divide by zero problems
@ -330,14 +340,14 @@ wait_ksm_settle(){
# abs()
pcnt=$(( $pcnt * -1 ))
echo "$oldpages to $newpages is ${pcnt}%"
info "$oldpages to $newpages is ${pcnt}%"
if (( $pcnt <= 5 )); then
echo "KSM stabilised at ${t}s"
info "KSM stabilised at ${t}s"
return
fi
else
echo "$oldpages KSM pages... waiting"
info "$oldpages KSM pages... waiting"
fi
fi
oldscan=$newscan
@ -347,7 +357,7 @@ wait_ksm_settle(){
fi
sleep 1
done
echo "Timed out after ${1}s waiting for KSM to settle"
info "Timed out after ${1}s waiting for KSM to settle"
}
common_init