metrics: Add num_vcpus and free_mem to metrics results template.

This PR retrieves the free memory and the vcpus count from
a kata container and includes them to the json results file of
any metric.

Additionally this PR parses the requested vcpus quantity and the
requested amount memory from kata configuration file and includes
this pair of values into the json results file of any metric.

Finally, the file system defined in the kata configuration file
is included in the results template.

Fixes: #9972

Signed-off-by: David Esparza <david.esparza.borquez@intel.com>
This commit is contained in:
David Esparza 2024-07-08 14:02:58 -06:00
parent a554541495
commit 04df85a44f
No known key found for this signature in database
GPG Key ID: EABE0B1A98CC3B7A
3 changed files with 64 additions and 6 deletions

View File

@ -137,6 +137,10 @@ function extract_kata_env() {
local hypervisor_path local hypervisor_path
local virtiofsd_path local virtiofsd_path
local initrd_path local initrd_path
local kata_env
local req_memory_amount
local req_num_vcpus
case "${KATA_HYPERVISOR}" in case "${KATA_HYPERVISOR}" in
dragonball) dragonball)
cmd=kata-ctl cmd=kata-ctl
@ -159,12 +163,28 @@ function extract_kata_env() {
hypervisor_path=".Hypervisor.Path" hypervisor_path=".Hypervisor.Path"
virtio_fs_daemon_path=".Hypervisor.VirtioFSDaemon" virtio_fs_daemon_path=".Hypervisor.VirtioFSDaemon"
initrd_path=".Initrd.Path" initrd_path=".Initrd.Path"
shared_fs=".Hypervisor.SharedFS"
req_memory_amount=".Host.Memory.Total"
req_num_vcpus=""
;; ;;
esac esac
RUNTIME_CONFIG_PATH=$(sudo ${cmd} env --json | jq -r ${config_path}) kata_env="$(sudo ${cmd} env --json)"
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_CONFIG_PATH="$(echo "${kata_env}" | jq -r ${config_path})"
RUNTIME_PATH=$(sudo ${cmd} env --json | jq -r ${runtime_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 # Shimv2 path is being affected by https://github.com/kata-containers/kata-containers/issues/1151
SHIM_PATH=$(command -v containerd-shim-kata-v2) SHIM_PATH=$(command -v containerd-shim-kata-v2)

View File

@ -45,6 +45,11 @@ quay.io/libpod"
readonly DEFAULT_KATA_CONFIG_DIR="/opt/kata/share/defaults/kata-containers" readonly DEFAULT_KATA_CONFIG_DIR="/opt/kata/share/defaults/kata-containers"
readonly DEFAULT_KATA_CONFIG_FNAME="configuration.toml" 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. # This function checks existence of commands.
# They can be received standalone or as an array, e.g. # 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 # 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() { function set_kata_config_file() {
NEW_KATA_CONFIG=${1} NEW_KATA_CONFIG=${1}
@ -513,3 +518,31 @@ function get_current_kata_config_file() {
function check_if_root() { function check_if_root() {
[ "$EUID" -ne 0 ] && die "Please run as root or use sudo." [ "$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}"
}

View File

@ -60,7 +60,12 @@ EOF
"HypervisorVersion": "${HYPERVISOR_VERSION}", "HypervisorVersion": "${HYPERVISOR_VERSION}",
"Shim": "${SHIM_PATH}", "Shim": "${SHIM_PATH}",
"ShimVersion": "${SHIM_VERSION}", "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 EOF
)" )"