Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit 765c67b

Browse files
authored
Merge pull request #5695 from dborquez/fix_memory_footprint_inside_container_average
metrics: Fix non valid results on memory usage inside container
2 parents 9a84e62 + 9cefdb3 commit 765c67b

File tree

2 files changed

+35
-51
lines changed

2 files changed

+35
-51
lines changed

.ci/run_metrics_PR_ci.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ source "${SCRIPT_DIR}/../metrics/lib/common.bash"
3434

3535
KATA_HYPERVISOR="${KATA_HYPERVISOR:-qemu}"
3636

37-
# Density paramter to set the number of iterations of the test
38-
DENSITY_FORMAT_RESULTS="${DENSITY_FORMAT_RESULTS:-json}"
39-
40-
# Density parameters to select between 'csv' and 'json' results format
37+
# Num of repetitions for the 'memory inside container' metrics check.
4138
DENSITY_TEST_REPETITIONS="${DENSITY_TEST_REPETITIONS:-1}"
4239

4340
# metrics selector among: density, boot, blogbench, all
@@ -93,7 +90,7 @@ run() {
9390
fi
9491
# Run the density test inside the container
9592
if [ "${TEST_SELECTOR}" = "all" ] || [ "${TEST_SELECTOR}" = "${TEST_DENSITY}" ]; then
96-
bash density/memory_usage_inside_container.sh ${DENSITY_FORMAT_RESULTS} ${DENSITY_TEST_REPETITIONS}
93+
bash density/memory_usage_inside_container.sh ${DENSITY_TEST_REPETITIONS}
9794
fi
9895

9996
# Run the time tests

metrics/density/memory_usage_inside_container.sh

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,27 @@ CMD="sleep 10; cat /proc/meminfo"
2222
# which makes later direct comparison easier.
2323
MEMSIZE=${MEMSIZE:-$((2048*1024))}
2424

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
2528
memtotalAvg=0
2629
units_memtotal=""
2730
memfreeAvg=0
2831
units_memfree=""
2932
memavailableAvg=0
3033
units_memavailable=""
31-
total_iters=0
32-
header=0
3334

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
3440

3541
parse_results() {
3642
local raw_results="${1}"
3743

38-
# variables that receive cummulative memory values for json case, or init to zero for csv case
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.
3946
local memtotal_acu="${2:-0}"
4047
local memfree_acu="${3:-0}"
4148
local memavailable_acu="${4:-0}"
@@ -49,21 +56,26 @@ parse_results() {
4956
local memavailable=$(echo "$raw_results" | awk '/MemAvailable/ {print $2}')
5057
units_memavailable=$(echo "$raw_results" | awk '/MemAvailable/ {print $3}')
5158

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+
5267
memtotalAvg=$((memtotal+memtotal_acu))
5368
memfreeAvg=$((memfree+memfree_acu))
5469
memavailableAvg=$((memavailable+memavailable_acu))
55-
56-
let "total_iters=total_iters+1"
57-
58-
echo "Iteration# $total_iters memtotal: $memtotal memfree: $memfree memavailable: $memavailable"
70+
valid_result=1
71+
info "Iteration# $count_iters memtotal: $memtotal memfree: $memfree memavailable: $memavailable"
5972
}
6073

61-
6274
store_results_json() {
6375
metrics_json_start_array
64-
memtotalAvg=$(( memtotalAvg / total_iters ))
65-
memfreeAvg=$(( memfreeAvg / total_iters ))
66-
memavailableAvg=$(( memavailableAvg / total_iters ))
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)
6779

6880
local json="$(cat << EOF
6981
{
@@ -84,7 +96,7 @@ store_results_json() {
8496
"Units" : "${units_memavailable}"
8597
},
8698
"repetitions": {
87-
"Result" : ${total_iters}
99+
"Result" : ${count_iters}
88100
}
89101
}
90102
EOF
@@ -94,55 +106,30 @@ EOF
94106
metrics_json_save
95107
}
96108

97-
store_results_csv() {
98-
local filename=${1}
99-
100-
if [ $header -eq 0 ]; then
101-
echo "memtotal,units_memtotal,memfree,units_memfree,memavailable,units_memavailable" > $filename
102-
header=1
103-
fi
104-
echo "$memtotalAvg,$units_memtotal,$memfreeAvg,$units_memfree,$memavailableAvg,$units_memavailable" >> $filename
105-
}
106-
107109
function main() {
108110
# switch to select output format
109-
local output_format=${1:-json}
110-
local iterations=${2:-1}
111-
local -r csv_filename="mem-usage-inside-container-$iterations-iters-$(date +"%Y_%m_%d_%H-%M").csv"
112-
echo "Output format: $output_format"
113-
echo "Iterations: $iterations"
111+
local num_iterations=${1:-1}
112+
info "Iterations: $num_iterations"
114113

115114
# Check tools/commands dependencies
116115
cmds=("awk" "ctr")
117-
118116
init_env
119117
check_cmds "${cmds[@]}"
120118
check_images "${IMAGE}"
121-
122119
metrics_json_init
123120

124-
for (( i=0; i<$iterations; i++ )) do
121+
while [ $count_iters -lt $num_iterations ]; do
125122
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}"
126124

127-
if [ ${output_format} = "json" ]; then
128-
parse_results "${output}" "${memtotalAvg}" "${memfreeAvg}" "${memavailableAvg}"
129-
130-
# Record results per iteration
131-
elif [ ${output_format} = "csv" ]; then
132-
parse_results "${output}"
133-
store_results_csv ${csv_filename}
134-
fi
135-
sleep 0.5
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))
136128
done
137-
138-
if [ $output_format = "json" ]; then
139-
store_results_json
140-
fi
141-
129+
store_results_json
142130
clean_env_ctr
143131
}
144132

145133
# Parameters
146-
# @1: output format [json/csv]
147-
# @2: iterations {integer}
134+
# @1: num_iterations {integer}
148135
main "$@"

0 commit comments

Comments
 (0)