Merge pull request #9973 from kata-containers/add_memory_and_vcpus_info_to_results

Add memory and vcpus info to metrics results
This commit is contained in:
David Esparza 2024-07-09 18:05:07 -06:00 committed by GitHub
commit 09f523c815
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 77 additions and 14 deletions

View File

@ -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)

View File

@ -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.
#
@ -128,8 +133,9 @@ function build_dockerfile_image()
|| die "Failed to docker build image $image"
}
# This function removes the ctr image, builds a new one using a dockerfile
# and imports the image from docker to ctr
# This function deletes any existing ctr image passed as a parameter,
# then creates a new image using a dockerfile, and finally exports
# a new version of the docker image to ctr.
function check_ctr_images()
{
local ctr_image="$1"
@ -437,11 +443,11 @@ function check_containers_are_running() {
fi
}
# This function receives an identifier used to name a performance Kata configuration file.
# The current kata configuration is copied to a new kata config file with the difference
# that the parameters 'default_vcpus' and 'default_memory' will be updated with the values
# of the max vcpus and the available memory in the system.
# Finally it makes Kata point to the new configuration file.
# This function generates a new kata configuration file based on the current configuration file,
# with the update of two parameters: 'default_vcpus' and 'default_memory'.
# These parameters are updated so that they point to the maximum number of vcpus available
# on the system and to use all the available memory on the system.
# Finally, a link to the new configuration file is created for kata to use in creating containers.
set_kata_configuration_performance() {
WORKLOAD_CONFIG_FILE="${1}"
@ -476,8 +482,8 @@ function clean_cache() {
sudo sync; echo 1 > /proc/sys/vm/drop_caches
}
# This function receives as single parameter, the name of a valid Kata configuration file
# which will be established as the default Kata configuration to start new Kata containers.
# 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 a new kata container.
function set_kata_config_file() {
NEW_KATA_CONFIG=${1}
@ -495,6 +501,8 @@ function set_kata_config_file() {
popd > /dev/null
}
# This function returns the path to the symbolic link pointed to by the kata
# configuration file: configuration.toml.
function get_current_kata_config_file() {
declare -n current_config_file=$1
@ -505,6 +513,36 @@ function get_current_kata_config_file() {
current_config_file="${KATA_CONFIG_FNAME}"
}
# This function checks if the current session is runnin as root,
# if that is not the case, the function exits with an error message.
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}"
}

View File

@ -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
)"