1#!/bin/sh
2
3################################################################################
4#                                                                              #
5# Copyright (c) 2009 FUJITSU LIMITED                                           #
6#                                                                              #
7# This program is free software;  you can redistribute it and#or modify        #
8# it under the terms of the GNU General Public License as published by         #
9# the Free Software Foundation; either version 2 of the License, or            #
10# (at your option) any later version.                                          #
11#                                                                              #
12# This program is distributed in the hope that it will be useful, but          #
13# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY   #
14# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License     #
15# for more details.                                                            #
16#                                                                              #
17# You should have received a copy of the GNU General Public License            #
18# along with this program;  if not, write to the Free Software                 #
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA      #
20#                                                                              #
21# Author: Miao Xie <[email protected]>                                      #
22#                                                                              #
23################################################################################
24
25export TCID="cpuset_base_ops"
26export TST_TOTAL=97
27export TST_COUNT=1
28
29. cpuset_funcs.sh
30
31check
32
33nr_cpus=$NR_CPUS
34nr_mems=$N_NODES
35
36cpus_all="$(seq -s, 0 $((nr_cpus-1)))"
37mems_all="$(seq -s, 0 $((nr_mems-1)))"
38
39exit_status=0
40
41cfile_name=
42
43# base_op_write_and_test <write_file_name> <write_string> <expect_string>
44base_op_write_and_test()
45{
46	local write_file="$1"
47	local write_string="$2"
48	local expect_string="$3"
49	local write_result=
50	local ret=0
51
52	mkdir -p "$(dirname $write_file)" || {
53		tst_brkm TFAIL "Failed to mkdir -p $(basename $write_file)"
54		return 1
55	}
56	[ "$write_string" = NULL ] && write_string=" "
57
58	/bin/echo "$write_string" > "$write_file" 2> $CPUSET_TMP/stderr
59	ret=$?
60	write_result="$(cat "$write_file")"
61
62	case "$expect_string" in
63	EMPTY)
64		test -z "$write_result" -a $ret = 0
65		ret=$?
66		;;
67	WRITE_ERROR)
68		ret=$((!$ret))
69		;;
70	*)
71		test "$expect_string" = "$write_result" -a $ret = 0
72		ret=$?
73		;;
74	esac
75
76	if [ $ret -eq 0 ]; then
77		tst_resm TPASS "$cfile_name: Get the expected string"
78	else
79		tst_resm TFAIL "$cfile_name: Test result - $write_result Expected string - \"$expect_string\""
80	fi
81	return $ret
82}
83
84base_op_test()
85{
86	setup
87	if [ $? -ne 0 ]; then
88		exit_status=1
89	else
90		base_op_write_and_test "$@"
91		if [ $? -ne 0 ]; then
92			exit_status=1
93		fi
94
95		cleanup
96		if [ $? -ne 0 ]; then
97			exit_status=1
98		fi
99	fi
100	TST_COUNT=$(($TST_COUNT + 1))
101}
102
103test_cpus()
104{
105	cfile_name="cpuset.cpus"
106	while read cpus result
107	do
108		base_op_test "$CPUSET/1/cpuset.cpus" "$cpus" "$result"
109	done <<- EOF
110		NULL					EMPTY
111		0					0
112		$nr_cpus				WRITE_ERROR
113		$cpus_all				0-$((nr_cpus-1))
114		${cpus_all}$nr_cpus			WRITE_ERROR
115		0,0					0
116		0-0					0
117		0-$((nr_cpus-1))			0-$((nr_cpus-1))
118		-1					WRITE_ERROR
119		0-$nr_cpus				WRITE_ERROR
120		0--$((nr_cpus-1))			WRITE_ERROR
121		0AAA					WRITE_ERROR
122		AAA					WRITE_ERROR
123	EOF
124	# while read cpus result
125
126	if [ $nr_cpus -ge 3 ]; then
127		base_op_test "$CPUSET/1/cpuset.cpus" "0,1-$((nr_cpus-2)),$((nr_cpus-1))" "0-$((nr_cpus-1))"
128		base_op_test "$CPUSET/1/cpuset.cpus" "0,1-$((nr_cpus-2))," "0-$((nr_cpus-2))"
129	fi
130
131	if tst_kvcmp -ge "4.3"; then
132		base_op_test "$CPUSET/1/cpuset.cpus" "0-" "WRITE_ERROR"
133	fi
134}
135
136test_mems()
137{
138	cfile_name="cpuset.mems"
139	while read mems result
140	do
141		base_op_test "$CPUSET/1/cpuset.mems" "$mems" "$result"
142	done <<- EOF
143		NULL					EMPTY
144		0					0
145		$nr_mems				WRITE_ERROR
146		$mems_all				0-$((nr_mems-1))
147		${mems_all}$nr_mems			WRITE_ERROR
148		0,0					0
149		0-0					0
150		0-$((nr_mems-1))			0-$((nr_mems-1))
151		-1					WRITE_ERROR
152		0-$nr_mems				WRITE_ERROR
153		0--$((nr_mems-1))			WRITE_ERROR
154		0AAA					WRITE_ERROR
155		AAA					WRITE_ERROR
156	EOF
157	# while read mems result
158
159	if [ $nr_mems -ge 3 ]; then
160		base_op_test "$CPUSET/1/cpuset.mems" "0,1-$((nr_mems-2)),$((nr_mems-1))" "0-$((nr_mems-1))"
161		base_op_test "$CPUSET/1/cpuset.mems" "0,1-$((nr_mems-2))," "0-$((nr_mems-2))"
162	fi
163
164	if tst_kvcmp -ge "4.3"; then
165		base_op_test "$CPUSET/1/cpuset.mems" "0-" "WRITE_ERROR"
166	fi
167}
168
169test_flags()
170{
171	for filename in cpu_exclusive mem_exclusive mem_hardwall \
172			memory_migrate memory_spread_page memory_spread_slab \
173			sched_load_balance memory_pressure_enabled
174	do
175		cfile_name="cpuset.$filename"
176		while read flags result
177		do
178			base_op_test "$CPUSET/cpuset.$filename" "$flags" "$result"
179		done <<- EOF
180			0	0
181			1	1
182			-1	WRITE_ERROR
183			A	WRITE_ERROR
184			2	1
185		EOF
186		# while read flags, result
187	done # for filename in flagfiles
188}
189
190# attach_task_test <cpus> <mems> <expect>
191attach_task_test()
192{
193	local cpus=$1
194	local mems=$2
195	local expect=$3
196
197	local pid=
198	local ret=
199
200	setup
201	if [ $? -ne 0 ]; then
202		exit_status=1
203		cleanup
204		TST_COUNT=$(($TST_COUNT + 1))
205		return
206	fi
207
208	# create sub cpuset
209	mkdir "$CPUSET/sub_cpuset" > /dev/null
210	if [ $? -ne 0 ]; then
211		exit_status=1
212		cleanup
213		TST_COUNT=$(($TST_COUNT + 1))
214		return
215	fi
216
217	if [ "$cpus" != "NULL" ]; then
218		echo $cpus > "$CPUSET/sub_cpuset/cpuset.cpus"
219	fi
220	if [ "$mems" != "NULL" ]; then
221		echo $mems > "$CPUSET/sub_cpuset/cpuset.mems"
222	fi
223
224	cat /dev/zero > /dev/null &
225	pid=$!
226
227	# attach task into the cpuset group
228	echo $pid > "$CPUSET/sub_cpuset/tasks" 2> /dev/null
229	if [ $? -eq $expect ]; then
230		tst_resm TPASS "Attaching Task Test successed!!"
231	else
232		tst_resm TFAIL "Attaching Task Test failed!! cpus - \"$cpus\", mems - \"$mems\", Expect - \"$expect\", Fact - \"$ret\". (0 - Attach Success, 1 - Attach Fail)"
233		exit_status=1
234	fi
235
236	/bin/kill $pid > /dev/null 2>&1
237	cleanup
238	if [ $? -ne 0 ]; then
239		exit_status=1
240	fi
241	TST_COUNT=$(($TST_COUNT + 1))
242}
243
244
245test_attach_task()
246{
247	cfile_name="tasks"
248	while read cpus mems expect
249	do
250		attach_task_test "$cpus" "$mems" "$expect"
251	done <<- EOF
252		0	NULL	1
253		0	0	0
254		NULL	0	1
255	EOF
256	# while read cpus mems expect
257}
258
259test_readonly_cfiles()
260{
261	for filename in cpus mems memory_pressure
262	do
263		cfile_name="cpuset.$filename(READONLY)"
264		base_op_test "$CPUSET/cpuset.$filename" "0" "WRITE_ERROR"
265	done # for filename in readonly cfiles
266}
267
268# Case 1-3
269test_readonly_cfiles
270
271# Case 4-19
272test_cpus
273
274# Case 20-35
275test_mems
276
277# Case 36-83
278test_flags
279
280# Case 84-86
281test_attach_task
282
283exit $exit_status
284