2323if [[ $cgroup2 ]]; then
2424 CGROUP_ROOT=$( mount -t cgroup2 | head -1 | awk ' {print $3}' )
2525 if [[ -z " $CGROUP_ROOT " ]]; then
26- CGROUP_ROOT=/dev/cgroup/memory
26+ CGROUP_ROOT=$( mktemp -d )
2727 mount -t cgroup2 none $CGROUP_ROOT
2828 do_umount=1
2929 fi
3636 do_umount=1
3737 fi
3838fi
39- MNT=' /mnt/huge/ '
39+ MNT=' /mnt/huge'
4040
4141function get_machine_hugepage_size() {
4242 hpz=$( grep -i hugepagesize /proc/meminfo)
@@ -56,10 +56,45 @@ function cleanup() {
5656 rmdir " $CGROUP_ROOT " /a/b 2> /dev/null
5757 rmdir " $CGROUP_ROOT " /a 2> /dev/null
5858 rmdir " $CGROUP_ROOT " /test1 2> /dev/null
59- echo 0 > /proc/sys/vm/nr_hugepages
59+ echo $nr_hugepgs > /proc/sys/vm/nr_hugepages
6060 set -e
6161}
6262
63+ function assert_with_retry() {
64+ local actual_path=" $1 "
65+ local expected=" $2 "
66+ local tolerance=$(( 7 * 1024 * 1024 ))
67+ local timeout=20
68+ local interval=1
69+ local start_time
70+ local now
71+ local elapsed
72+ local actual
73+
74+ start_time=$( date +%s)
75+
76+ while true ; do
77+ actual=" $( cat " $actual_path " ) "
78+
79+ if [[ $actual -ge $(( $expected - $tolerance )) ]] &&
80+ [[ $actual -le $(( $expected + $tolerance )) ]]; then
81+ return 0
82+ fi
83+
84+ now=$( date +%s)
85+ elapsed=$(( now - start_time))
86+
87+ if [[ $elapsed -ge $timeout ]]; then
88+ echo " actual = $(( ${actual%% * } / 1024 / 1024 )) MB"
89+ echo " expected = $(( ${expected%% * } / 1024 / 1024 )) MB"
90+ cleanup
91+ exit 1
92+ fi
93+
94+ sleep $interval
95+ done
96+ }
97+
6398function assert_state() {
6499 local expected_a=" $1 "
65100 local expected_a_hugetlb=" $2 "
@@ -70,58 +105,13 @@ function assert_state() {
70105 expected_b=" $3 "
71106 expected_b_hugetlb=" $4 "
72107 fi
73- local tolerance=$(( 5 * 1024 * 1024 ))
74-
75- local actual_a
76- actual_a=" $( cat " $CGROUP_ROOT " /a/memory.$usage_file ) "
77- if [[ $actual_a -lt $(( $expected_a - $tolerance )) ]] ||
78- [[ $actual_a -gt $(( $expected_a + $tolerance )) ]]; then
79- echo actual a = $(( ${actual_a%% * } / 1024 / 1024 )) MB
80- echo expected a = $(( ${expected_a%% * } / 1024 / 1024 )) MB
81- echo fail
82-
83- cleanup
84- exit 1
85- fi
86108
87- local actual_a_hugetlb
88- actual_a_hugetlb=" $( cat " $CGROUP_ROOT " /a/hugetlb.${MB} MB.$usage_file ) "
89- if [[ $actual_a_hugetlb -lt $(( $expected_a_hugetlb - $tolerance )) ]] ||
90- [[ $actual_a_hugetlb -gt $(( $expected_a_hugetlb + $tolerance )) ]]; then
91- echo actual a hugetlb = $(( ${actual_a_hugetlb%% * } / 1024 / 1024 )) MB
92- echo expected a hugetlb = $(( ${expected_a_hugetlb%% * } / 1024 / 1024 )) MB
93- echo fail
109+ assert_with_retry " $CGROUP_ROOT /a/memory.$usage_file " " $expected_a "
110+ assert_with_retry " $CGROUP_ROOT /a/hugetlb.${MB} MB.$usage_file " " $expected_a_hugetlb "
94111
95- cleanup
96- exit 1
97- fi
98-
99- if [[ -z " $expected_b " || -z " $expected_b_hugetlb " ]]; then
100- return
101- fi
102-
103- local actual_b
104- actual_b=" $( cat " $CGROUP_ROOT " /a/b/memory.$usage_file ) "
105- if [[ $actual_b -lt $(( $expected_b - $tolerance )) ]] ||
106- [[ $actual_b -gt $(( $expected_b + $tolerance )) ]]; then
107- echo actual b = $(( ${actual_b%% * } / 1024 / 1024 )) MB
108- echo expected b = $(( ${expected_b%% * } / 1024 / 1024 )) MB
109- echo fail
110-
111- cleanup
112- exit 1
113- fi
114-
115- local actual_b_hugetlb
116- actual_b_hugetlb=" $( cat " $CGROUP_ROOT " /a/b/hugetlb.${MB} MB.$usage_file ) "
117- if [[ $actual_b_hugetlb -lt $(( $expected_b_hugetlb - $tolerance )) ]] ||
118- [[ $actual_b_hugetlb -gt $(( $expected_b_hugetlb + $tolerance )) ]]; then
119- echo actual b hugetlb = $(( ${actual_b_hugetlb%% * } / 1024 / 1024 )) MB
120- echo expected b hugetlb = $(( ${expected_b_hugetlb%% * } / 1024 / 1024 )) MB
121- echo fail
122-
123- cleanup
124- exit 1
112+ if [[ -n " $expected_b " && -n " $expected_b_hugetlb " ]]; then
113+ assert_with_retry " $CGROUP_ROOT /a/b/memory.$usage_file " " $expected_b "
114+ assert_with_retry " $CGROUP_ROOT /a/b/hugetlb.${MB} MB.$usage_file " " $expected_b_hugetlb "
125115 fi
126116}
127117
@@ -174,7 +164,6 @@ size=$((${MB} * 1024 * 1024 * 25)) # 50MB = 25 * 2MB hugepages.
174164
175165cleanup
176166
177- echo
178167echo
179168echo Test charge, rmdir, uncharge
180169setup
@@ -195,7 +184,6 @@ cleanup
195184
196185echo done
197186echo
198- echo
199187if [[ ! $cgroup2 ]]; then
200188 echo " Test parent and child hugetlb usage"
201189 setup
@@ -212,7 +200,6 @@ if [[ ! $cgroup2 ]]; then
212200 assert_state 0 $(( $size * 2 )) 0 $size
213201
214202 rmdir " $CGROUP_ROOT " /a/b
215- sleep 5
216203 echo Assert memory reparent correctly.
217204 assert_state 0 $(( $size * 2 ))
218205
@@ -224,7 +211,6 @@ if [[ ! $cgroup2 ]]; then
224211 cleanup
225212fi
226213
227- echo
228214echo
229215echo " Test child only hugetlb usage"
230216echo setup
0 commit comments