2 # SPDX-License-Identifier: GPL-2.0
6 if [[ $(id -u) -ne 0 ]]; then
7 echo "This test must be run as root. Skipping..."
11 usage_file=usage_in_bytes
13 if [[ "$1" == "-cgroup-v2" ]]; then
18 CGROUP_ROOT='/dev/cgroup/memory'
21 if [[ ! -e $CGROUP_ROOT ]]; then
23 if [[ $cgroup2 ]]; then
24 mount -t cgroup2 none $CGROUP_ROOT
26 echo "+hugetlb +memory" >$CGROUP_ROOT/cgroup.subtree_control
28 mount -t cgroup memory,hugetlb $CGROUP_ROOT
32 function get_machine_hugepage_size() {
33 hpz=$(grep -i hugepagesize /proc/meminfo)
39 MB=$(get_machine_hugepage_size)
44 rm -rf "$MNT"/* 2>/dev/null
45 umount "$MNT" 2>/dev/null
46 rmdir "$MNT" 2>/dev/null
47 rmdir "$CGROUP_ROOT"/a/b 2>/dev/null
48 rmdir "$CGROUP_ROOT"/a 2>/dev/null
49 rmdir "$CGROUP_ROOT"/test1 2>/dev/null
50 echo 0 >/proc/sys/vm/nr_hugepages
54 function assert_state() {
56 local expected_a_hugetlb="$2"
58 local expected_b_hugetlb=""
60 if [ ! -z ${3:-} ] && [ ! -z ${4:-} ]; then
62 expected_b_hugetlb="$4"
64 local tolerance=$((5 * 1024 * 1024))
67 actual_a="$(cat "$CGROUP_ROOT"/a/memory.$usage_file)"
68 if [[ $actual_a -lt $(($expected_a - $tolerance)) ]] ||
69 [[ $actual_a -gt $(($expected_a + $tolerance)) ]]; then
70 echo actual a = $((${actual_a%% *} / 1024 / 1024)) MB
71 echo expected a = $((${expected_a%% *} / 1024 / 1024)) MB
78 local actual_a_hugetlb
79 actual_a_hugetlb="$(cat "$CGROUP_ROOT"/a/hugetlb.${MB}MB.$usage_file)"
80 if [[ $actual_a_hugetlb -lt $(($expected_a_hugetlb - $tolerance)) ]] ||
81 [[ $actual_a_hugetlb -gt $(($expected_a_hugetlb + $tolerance)) ]]; then
82 echo actual a hugetlb = $((${actual_a_hugetlb%% *} / 1024 / 1024)) MB
83 echo expected a hugetlb = $((${expected_a_hugetlb%% *} / 1024 / 1024)) MB
90 if [[ -z "$expected_b" || -z "$expected_b_hugetlb" ]]; then
95 actual_b="$(cat "$CGROUP_ROOT"/a/b/memory.$usage_file)"
96 if [[ $actual_b -lt $(($expected_b - $tolerance)) ]] ||
97 [[ $actual_b -gt $(($expected_b + $tolerance)) ]]; then
98 echo actual b = $((${actual_b%% *} / 1024 / 1024)) MB
99 echo expected b = $((${expected_b%% *} / 1024 / 1024)) MB
106 local actual_b_hugetlb
107 actual_b_hugetlb="$(cat "$CGROUP_ROOT"/a/b/hugetlb.${MB}MB.$usage_file)"
108 if [[ $actual_b_hugetlb -lt $(($expected_b_hugetlb - $tolerance)) ]] ||
109 [[ $actual_b_hugetlb -gt $(($expected_b_hugetlb + $tolerance)) ]]; then
110 echo actual b hugetlb = $((${actual_b_hugetlb%% *} / 1024 / 1024)) MB
111 echo expected b hugetlb = $((${expected_b_hugetlb%% *} / 1024 / 1024)) MB
120 echo 100 >/proc/sys/vm/nr_hugepages
121 mkdir "$CGROUP_ROOT"/a
123 if [[ $cgroup2 ]]; then
124 echo "+hugetlb +memory" >$CGROUP_ROOT/a/cgroup.subtree_control
126 echo 0 >$CGROUP_ROOT/a/cpuset.mems
127 echo 0 >$CGROUP_ROOT/a/cpuset.cpus
130 mkdir "$CGROUP_ROOT"/a/b
132 if [[ ! $cgroup2 ]]; then
133 echo 0 >$CGROUP_ROOT/a/b/cpuset.mems
134 echo 0 >$CGROUP_ROOT/a/b/cpuset.cpus
138 mount -t hugetlbfs none "$MNT"
146 if [[ $cgroup2 ]]; then
147 echo $$ >$CGROUP_ROOT/$cgroup/cgroup.procs
149 echo 0 >$CGROUP_ROOT/$cgroup/cpuset.mems
150 echo 0 >$CGROUP_ROOT/$cgroup/cpuset.cpus
151 echo $$ >"$CGROUP_ROOT/$cgroup/tasks"
153 ./write_to_hugetlbfs -p "$path" -s "$size" -m 0 -o
154 if [[ $cgroup2 ]]; then
155 echo $$ >$CGROUP_ROOT/cgroup.procs
157 echo $$ >"$CGROUP_ROOT/tasks"
164 size=$((${MB} * 1024 * 1024 * 25)) # 50MB = 25 * 2MB hugepages.
170 echo Test charge, rmdir, uncharge
173 mkdir $CGROUP_ROOT/test1
176 write_hugetlbfs test1 "$MNT"/test $size
179 rmdir $CGROUP_ROOT/test1
180 mkdir $CGROUP_ROOT/test1
190 if [[ ! $cgroup2 ]]; then
191 echo "Test parent and child hugetlb usage"
195 write_hugetlbfs a "$MNT"/test $size
197 echo Assert memory charged correctly for parent use.
198 assert_state 0 $size 0 0
200 write_hugetlbfs a/b "$MNT"/test2 $size
202 echo Assert memory charged correctly for child use.
203 assert_state 0 $(($size * 2)) 0 $size
205 rmdir "$CGROUP_ROOT"/a/b
207 echo Assert memory reparent correctly.
208 assert_state 0 $(($size * 2))
212 echo Assert memory uncharged correctly.
220 echo "Test child only hugetlb usage"
225 write_hugetlbfs a/b "$MNT"/test2 $size
227 echo Assert memory charged correctly for child only use.
228 assert_state 0 $(($size)) 0 $size
230 rmdir "$CGROUP_ROOT"/a/b
231 echo Assert memory reparent correctly.
236 echo Assert memory uncharged correctly.