diff --git a/tests/common.bash b/tests/common.bash index 99bf43656d..cffec13045 100644 --- a/tests/common.bash +++ b/tests/common.bash @@ -137,6 +137,10 @@ function extract_kata_env() { local hypervisor_path local virtiofsd_path local initrd_path + local kata_env + local req_memory_amount + local req_num_vcpus + case "${KATA_HYPERVISOR}" in dragonball) cmd=kata-ctl @@ -159,12 +163,28 @@ function extract_kata_env() { hypervisor_path=".Hypervisor.Path" virtio_fs_daemon_path=".Hypervisor.VirtioFSDaemon" initrd_path=".Initrd.Path" + shared_fs=".Hypervisor.SharedFS" + req_memory_amount=".Host.Memory.Total" + req_num_vcpus="" ;; esac - RUNTIME_CONFIG_PATH=$(sudo ${cmd} env --json | jq -r ${config_path}) - RUNTIME_VERSION=$(sudo ${cmd} env --json | jq -r ${runtime_version} | grep ${runtime_version_semver} | cut -d'"' -f4) - RUNTIME_COMMIT=$(sudo ${cmd} env --json | jq -r ${runtime_version} | grep ${runtime_version_commit} | cut -d'"' -f4) - RUNTIME_PATH=$(sudo ${cmd} env --json | jq -r ${runtime_path}) + kata_env="$(sudo ${cmd} env --json)" + + RUNTIME_CONFIG_PATH="$(echo "${kata_env}" | jq -r ${config_path})" + RUNTIME_VERSION="$(echo "${kata_env}" | jq -r ${runtime_version} | grep ${runtime_version_semver} | cut -d'"' -f4)" + RUNTIME_COMMIT="$(echo "${kata_env}" | jq -r ${runtime_version} | grep ${runtime_version_commit} | cut -d'"' -f4)" + RUNTIME_PATH="$(echo "${kata_env}" | jq -r ${runtime_path})" + SHARED_FS="$(echo "${kata_env}" | jq -r ${shared_fs})" + + # get_kata_memory_and_vcpus() function measures the memory and the number of vcpus + # from a kata container, and saves these values ​​in the variables: + # 'MEASURED_CONTAINER_NUM_VCPUS' and 'MEASURED_CONTAINER_TOTAL_MEM' + get_kata_memory_and_vcpus + + # get the requested memory and num of vcpus from the kata config file. + config_content="$(cat ${RUNTIME_CONFIG_PATH} | grep -vE "^#")" + REQ_MEMORY="$(echo "${config_content}" | grep -i default_memory | cut -d "=" -f2 | awk '{print $1}')" + REQ_NUM_VCPUS="$(echo "${config_content}" | grep -i default_vcpus | cut -d "=" -f2 | awk '{print $1}')" # Shimv2 path is being affected by https://github.com/kata-containers/kata-containers/issues/1151 SHIM_PATH=$(command -v containerd-shim-kata-v2) diff --git a/tests/metrics/lib/common.bash b/tests/metrics/lib/common.bash index 65fbe688b8..05a0fcef08 100755 --- a/tests/metrics/lib/common.bash +++ b/tests/metrics/lib/common.bash @@ -45,6 +45,11 @@ quay.io/libpod" readonly DEFAULT_KATA_CONFIG_DIR="/opt/kata/share/defaults/kata-containers" readonly DEFAULT_KATA_CONFIG_FNAME="configuration.toml" +# Global variables used to retrieve two values: the count of Vcpus and the +# total memory available inside a container. +MEASURED_CONTAINER_NUM_VCPUS="" +MEASURED_CONTAINER_TOTAL_MEM="" + # This function checks existence of commands. # They can be received standalone or as an array, e.g. # @@ -478,7 +483,7 @@ function clean_cache() { } # This function receives as a single parameter the path to a valid kata configuration file -# that will be set as the configuration used to start new Kata containers. +# that will be set as the configuration used to start a new kata container. function set_kata_config_file() { NEW_KATA_CONFIG=${1} @@ -513,3 +518,31 @@ function get_current_kata_config_file() { function check_if_root() { [ "$EUID" -ne 0 ] && die "Please run as root or use sudo." } + +# This function launches a kata container using a Busybox image, +# then collects the current number of vcpus and the free memory from the container. +# Finalliy fullfills the global variables 'MEASURED_CONTAINER_NUM_VCPUS' and 'MEASURED_CONTAINER_TOTAL_MEM' +function get_kata_memory_and_vcpus() { + local busybox_img="quay.io/prometheus/busybox:latest" + local container_name="kata-busybox_${RANDOM}" + local PAYLOAD_ARGS="tail -f /dev/null" + local remove_img=1 + + IMG_EXIST="$(sudo ctr i list | grep -c $busybox_img)" || true + + # Pull image if it does not exist. + [ "${IMG_EXIST}" -eq 0 ] && ${CTR_EXE} i pull "${busybox_img}" + + sudo -E ${CTR_EXE} run -d --runtime "${CTR_RUNTIME}" "${busybox_img}" "${container_name}" sh -c "${PAYLOAD_ARGS}" + + MEASURED_CONTAINER_NUM_VCPUS="$(sudo -E ${CTR_EXE} t exec --exec-id ${RANDOM} ${container_name} sh -c "nproc")" + MEASURED_CONTAINER_TOTAL_MEM="$(sudo -E ${CTR_EXE} t exec --exec-id ${RANDOM} ${container_name} sh -c "free -h" | grep -i "Mem:" | awk '{print $2}')" + sudo ${CTR_EXE} t kill -a -s SIGKILL "${container_name}" + + # Delete the busubox image only if it was previously extracted. + # Otherwise do not remove. + [ ${IMG_EXIST} -eq 0 ] && ${CTR_EXE} i rm "${busybox_img}" + + sleep 1 + sudo ${CTR_EXE} c rm "${container_name}" +} diff --git a/tests/metrics/lib/json.bash b/tests/metrics/lib/json.bash index 1a30820950..e2705cfba7 100755 --- a/tests/metrics/lib/json.bash +++ b/tests/metrics/lib/json.bash @@ -60,7 +60,12 @@ EOF "HypervisorVersion": "${HYPERVISOR_VERSION}", "Shim": "${SHIM_PATH}", "ShimVersion": "${SHIM_VERSION}", - "machinename": "$(uname -n)" + "machinename": "$(uname -n)", + "SharedFs": "${SHARED_FS}", + "ReqMemKB": "${REQ_MEMORY}", + "ReqNumVcpus": "${REQ_NUM_VCPUS}", + "MeasuredTotalMem": "${MEASURED_CONTAINER_TOTAL_MEM}", + "MeasuredNumVcpus": "${MEASURED_CONTAINER_NUM_VCPUS}" } EOF )"