github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/metrics/density/memory_usage_inside_container.sh (about)

     1  #!/bin/bash
     2  # Copyright (c) 2017-2021 Intel Corporation
     3  #
     4  # SPDX-License-Identifier: Apache-2.0
     5  #
     6  #  Description of the test:
     7  #  This test launches a busybox container and inside
     8  #  memory free, memory available and total memory
     9  #  is measured by using /proc/meminfo.
    10  
    11  set -e
    12  
    13  # General env
    14  SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
    15  source "${SCRIPT_PATH}/../lib/common.bash"
    16  
    17  TEST_NAME="memory footprint inside container"
    18  VERSIONS_FILE="${SCRIPT_PATH}/../../versions.yaml"
    19  IMAGE='quay.io/prometheus/busybox:latest'
    20  CMD="sleep 10; cat /proc/meminfo"
    21  # We specify here in 'k', as that then matches the results we get from the meminfo,
    22  # which makes later direct comparison easier.
    23  MEMSIZE=${MEMSIZE:-$((2048*1024))}
    24  
    25  # this variable determines the number of attempts when a test
    26  # result is considered not valid (a zero value or a negative value)
    27  MAX_FAILED_ATTEMPTS=3
    28  memtotalAvg=0
    29  units_memtotal=""
    30  memfreeAvg=0
    31  units_memfree=""
    32  memavailableAvg=0
    33  units_memavailable=""
    34  
    35  # count_iters: is the index of the current iteration
    36  count_iters=0
    37  
    38  # valid_result: if value stored is '1' the result is valid, '0' otherwise
    39  valid_result=0
    40  
    41  parse_results() {
    42  	local raw_results="${1}"
    43  
    44  	# Variables used for sum cummulative values in the case of two or more reps.
    45  	# and used to compute average results for 'json' output format.
    46  	local memtotal_acu="${2:-0}"
    47  	local memfree_acu="${3:-0}"
    48  	local memavailable_acu="${4:-0}"
    49  
    50  	local memtotal=$(echo "$raw_results" | awk '/MemTotal/ {print $2}')
    51  	units_memtotal=$(echo "$raw_results" | awk '/MemTotal/ {print $3}')
    52  
    53  	local memfree=$(echo "$raw_results" | awk '/MemFree/ {print $2}')
    54  	units_memfree=$(echo "$raw_results" | awk '/MemFree/ {print $3}')
    55  
    56  	local memavailable=$(echo "$raw_results" | awk '/MemAvailable/ {print $2}')
    57  	units_memavailable=$(echo "$raw_results" | awk '/MemAvailable/ {print $3}')
    58  
    59  	# check results: if any result is zero or negative, it is considered as invalid, and the test will be repeated.
    60  	if ((  $(echo "$memtotal <= 0" | bc -l) )) || ((  $(echo "$memfree <= 0" | bc -l) )) || ((  $(echo "$memavailable <= 0" | bc -l) )); then
    61  		MAX_FAILED_ATTEMPTS=$((MAX_FAILED_ATTEMPTS-1))
    62  		valid_result=0
    63  		info "Skipping invalid result:  memtotal: $memtotal  memfree: $memfree  memavailable: $memavailable"
    64  		return 0
    65  	fi
    66  
    67  	memtotalAvg=$((memtotal+memtotal_acu))
    68  	memfreeAvg=$((memfree+memfree_acu))
    69  	memavailableAvg=$((memavailable+memavailable_acu))
    70  	valid_result=1
    71  	info "Iteration# $count_iters  memtotal: $memtotal  memfree: $memfree  memavailable: $memavailable"
    72  }
    73  
    74  store_results_json() {
    75  	metrics_json_start_array
    76  	memtotalAvg=$(echo "scale=4; $memtotalAvg / $count_iters" | bc)
    77  	memfreeAvg=$(echo "scale=4; $memfreeAvg / $count_iters" | bc)
    78  	memavailableAvg=$(echo "scale=4; $memavailableAvg / $count_iters" | bc)
    79  
    80  	local json="$(cat << EOF
    81  	{
    82  		"memrequest": {
    83  			"Result" : ${MEMSIZE},
    84  			"Units"  : "Kb"
    85  		},
    86  		"memtotal": {
    87  			"Result" : ${memtotalAvg},
    88  			"Units"  : "${units_memtotal}"
    89  		},
    90  		"memfree": {
    91  			"Result" : ${memfreeAvg},
    92  			"Units"  : "${units_memfree}"
    93  		},
    94  		"memavailable": {
    95  			"Result" : ${memavailableAvg},
    96  			"Units"  : "${units_memavailable}"
    97  		},
    98  		"repetitions": {
    99  			"Result" : ${count_iters}
   100  		}
   101  	}
   102  EOF
   103  )"
   104  	metrics_json_add_array_element "$json"
   105  	metrics_json_end_array "Results"
   106  	metrics_json_save
   107  }
   108  
   109  function main() {
   110  	# switch to select output format
   111  	local num_iterations=${1:-1}
   112  	info "Iterations: $num_iterations"
   113  
   114  	# Check tools/commands dependencies
   115  	cmds=("awk" "ctr")
   116  	init_env
   117  	check_cmds "${cmds[@]}"
   118  	check_images "${IMAGE}"
   119  	metrics_json_init
   120  
   121  	while [  $count_iters -lt $num_iterations ]; do
   122  		local output=$(sudo -E "${CTR_EXE}" run --memory-limit $((MEMSIZE*1024)) --rm --runtime=$CTR_RUNTIME $IMAGE busybox sh -c "$CMD" 2>&1)
   123  		parse_results "${output}" "${memtotalAvg}" "${memfreeAvg}" "${memavailableAvg}"
   124  
   125  		# quit if number of attempts exceeds the allowed value.
   126  		[ ${MAX_FAILED_ATTEMPTS} -eq 0 ] && die "Max number of attempts exceeded."
   127  		[ ${valid_result} -eq 1 ] && count_iters=$((count_iters+1))
   128  	done
   129  	store_results_json
   130  	clean_env_ctr
   131  }
   132  
   133  # Parameters
   134  # @1: num_iterations {integer}
   135  main "$@"