aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/controllers/cpuctl_fj/run_cpuctl_test_fj.sh
blob: 5cb6bb566b142af91be6481b324c43fd67a02f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#! /bin/sh

################################################################################
##                                                                            ##
## Copyright (c) 2009 FUJITSU LIMITED                                         ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
## Author: Miao Xie <miaox@cn.fujitsu.com>                                    ##
## Restructure for LTP: Shi Weihua <shiwh@cn.fujitsu.com>                     ##
##                                                                            ##
################################################################################

cd $LTPROOT/testcases/bin

export TCID="cpuctl_test_fj"
export TST_TOTAL=22
export TST_COUNT=1

CPUCTL="/dev/cpuctl"
CPUCTL_TMP="/tmp/cpuctl_tmp"
SLEEP_SEC=5

# Create $CPUCTL &  mount the cgroup file system with cpu controller
# clean any group created earlier (if any)
setup()
{
	if [ -e $CPUCTL ]
	then
		tst_resm TWARN "WARN:$CPUCTL already exist..overwriting"
		cleanup || {
			tst_resm TFAIL "Err: Can't cleanup... Exiting"
			exit -1;
		}
	fi

	mkdir -p "$CPUCTL" || return 1

	mount -t cgroup -o cpu cpuctl "$CPUCTL" || {
		tst_resm TFAIL "failed to mount cpu subsystem... Exiting"
		cleanup;
		return 1;
	}
}

# Write the cleanup function
cleanup()
{
	mount | grep "$CPUCTL" > /dev/null 2>&1 || {
		rm -rf "$CPUCTL" > /dev/null 2>&1
		return 0
	}

	find $CPUCTL -type d | sort | sed -n '2,$p' | tac | while read -r tmpdir
	do
		while read tmppid
		do
			echo $tmppid > $CPUCTL/tasks
		done  < $tmpdir/tasks
		rmdir $tmpdir || return 1
	done

	umount $CPUCTL || return 1
	rmdir $CPUCTL > /dev/null 2>&1
}

creat_process()
{
	cat /dev/zero > /dev/null 2>/dev/null &
	taskset -p 1 $! >/dev/null 2>&1
	echo $!
}

get_cpu_usage()
{
	# XXX (garrcoop): expr(1) can't do float point comparisons
	# apparently...
	#
	# gcooper@optimus ~ $ expr 0 \< 42 \& 42 \< 100
	# 1
	# gcooper@optimus ~ $ expr 0 \< 42.0 \& 42.0 \< 100
	# 0
	# gcooper@optimus ~ $ expr 0.0 \< 42.0 \& 42.0 \< 100.0
	# 0
	# ... so we have to lop off the fractional piece.
	# ps -p $1 pcpu | awk -F. '{ print $1 }'
	top -bn1 -p $1 | sed -n "8p" | awk '{ print $9 }' | awk -F. '{ print $1 }'
}

kill_all_pid()
{
	while read pid_to_be_killed
	do
		kill -9 $pid_to_be_killed
	done
}

TESTUSER="`whoami`"
if [ "$TESTUSER" != "root" ]
then
	tst_brkm TBROK ignored "Test must be run as root"
	exit 0
fi

# only root directory
case1 ()
{
	[ -f "$CPUCTL/cpu.shares" ] || {
		tst_resm TFAIL "Err: No cpu.shares."
		return 1
	}

	shares=`cat $CPUCTL/cpu.shares`
	if [ $shares -ne 1024 ]
	then
		tst_resm TFAIL "Err: Init value is not 1024"
		return 1;
	fi

	ps -eo pid,rtprio > /tmp/pids_file1 &
	pspid=$!
	wait $pspid
	cat /tmp/pids_file1 | grep '-' | tr -d '-' | tr -d ' ' | \
	grep -v "$pspid" > /tmp/pids_file2

	while read pid
	do
		task=`cat $CPUCTL/tasks | grep "\b$pid\b"`
		if [ -z $task ]
		then
			tst_resm TFAIL  "Err: Some normal tasks aren't in the root group"
			return 1
		fi
	done < /tmp/pids_file2
}

# create a child directory
case2 ()
{
	mkdir $CPUCTL/tmp
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	[ -d "$CPUCTL/tmp" ] || return 1

	[ -f "$CPUCTL/tmp/cpu.shares" ] || return 1

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 1024 ]
	then
		return 1;
	fi

	task=`cat $CPUCTL/tmp/tasks`
	if [ "$task" != "" ]
	then
		return 1
	fi
}

# create a grand-directory
case3 ()
{
	mkdir $CPUCTL/tmp
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	mkdir $CPUCTL/tmp/tmp1
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	[ -d "$CPUCTL/tmp/tmp1" ] || return 1

	[ -f "$CPUCTL/tmp/tmp1/cpu.shares" ] || return 1

	shares=`cat $CPUCTL/tmp/tmp1/cpu.shares`
	if [ $shares -ne 1024 ]
	then
		return 1;
	fi

	task=`cat $CPUCTL/tmp/tmp1/tasks`
	if [ "$task" != "" ]
	then
		return 1
	fi
}

# attach general process
case4 ()
{
	mkdir $CPUCTL/tmp
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	echo 1 > $CPUCTL/tmp/tasks
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	tasks=`cat $CPUCTL/tmp/tasks`
	if [ $tasks -ne 1 ]
	then
		return 1;
	fi
}

# attach realtime process
case5 ()
{
	mkdir $CPUCTL/tmp
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	./cpuctl_fj_simple_echo 3 $CPUCTL/tmp/tasks
	if [ $? -ne 22 ]	# define EINVAL 22  /* Invalid argument */
	then
		return 1;
	fi

	tasks=`cat $CPUCTL/tmp/tasks`
	if [ "$tasks" != "" ]
	then
		return 1;
	fi
}

# modify the shares of the root group
case6 ()
{
	./cpuctl_fj_simple_echo 2048 $CPUCTL/cpu.shares
	if [ $? -ne 22 ]	# define EINVAL 22  /* Invalid argument */
	then
		return 1;
	fi

	shares=`cat $CPUCTL/cpu.shares`
	if [ $shares -ne 1024 ]
	then
		return 1;
	fi
}

# echo negative into shares
case7 ()
{
	mkdir $CPUCTL/tmp

	./cpuctl_fj_simple_echo -1 $CPUCTL/tmp/cpu.shares
	if [ $? -ne 22 ]	# define EINVAL 22  /* Invalid argument */
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 1024 ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

# echo 0 into shares
case8 ()
{
	mkdir $CPUCTL/tmp

	echo 0 > $CPUCTL/tmp/cpu.shares
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 2 ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

# echo 1 into shares
case9 ()
{
	mkdir $CPUCTL/tmp

	echo 1 > $CPUCTL/tmp/cpu.shares
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 2 ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

# echo 2 into shares
case10 ()
{
	mkdir $CPUCTL/tmp

	echo 2 > $CPUCTL/tmp/cpu.shares
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 2 ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

# echo 3 into shares
case11 ()
{
	mkdir $CPUCTL/tmp

	echo 3 > $CPUCTL/tmp/cpu.shares
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 3 ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

# echo 2048 into shares
case12 ()
{
	mkdir $CPUCTL/tmp

	echo 2048 > $CPUCTL/tmp/cpu.shares
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 2048 ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

max_shares=$((1 << 18))

# echo MAX_SHARES into shares
case13 ()
{
	mkdir $CPUCTL/tmp

	echo $max_shares > $CPUCTL/tmp/cpu.shares
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ "$shares" != "$max_shares" ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

# echo MAX_SHARES+1 into shares
case14 ()
{
	mkdir $CPUCTL/tmp

	echo $(($max_shares+1)) > $CPUCTL/tmp/cpu.shares
	if [ $? -ne 0 ]
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ "$shares" != "$max_shares" ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

# echo float number into shares
case15 ()
{
	mkdir $CPUCTL/tmp

	./cpuctl_fj_simple_echo 2048.23 $CPUCTL/tmp/cpu.shares
	if [ $? -ne 22 ]	# define EINVAL 22  /* Invalid argument */
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 1024 ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

# echo a string into shares. This string begins with some number, and ends with
# charactor.
case16 ()
{
	mkdir $CPUCTL/tmp

	./cpuctl_fj_simple_echo 2048ABC $CPUCTL/tmp/cpu.shares
	if [ $? -ne 22 ]	# define EINVAL 22  /* Invalid argument */
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 1024 ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

# echo a string into shares. This string begins with charactors.
case17 ()
{
	mkdir $CPUCTL/tmp

	./cpuctl_fj_simple_echo ABC $CPUCTL/tmp/cpu.shares
	if [ $? -ne 22 ]	 # define EINVAL 22  /* Invalid argument */
	then
		return 1;
	fi

	shares=`cat $CPUCTL/tmp/cpu.shares`
	if [ $shares -ne 1024 ]
	then
		return 1;
	fi

	./cpuctl_fj_cpu-hog &
	pid=$!

	echo $pid > $CPUCTL/tmp/tasks

	/bin/kill -s SIGUSR1 $pid
	sleep $SLEEP_SEC
	/bin/kill -s SIGUSR1 $pid
	wait $pid
}

case18()
{
	mkdir "$CPUCTL/1"
	echo 0x8000000000000000 > "$CPUCTL/1/cpu.shares"
	pid=$(creat_process)
	echo $pid > "$CPUCTL/1/tasks"
	kill -9 $pid
	return 0
}

case19()
{
	mkdir "$CPUCTL/1"
	pid=$(creat_process)
	pid_other=$(creat_process)

	echo 2000000000 > "$CPUCTL/1/cpu.shares"
	echo $pid > "$CPUCTL/1/tasks"
	cpu_usage=$(get_cpu_usage $pid)
	echo "pid $pid cpu_usage $cpu_usage"

	kill -9 $pid $pid_other
	expr 96 \< "$cpu_usage" \& "$cpu_usage" \< 103 > /dev/null 2>&1 || return 1
	return 0
}

case20()
{
	mkdir "$CPUCTL/1"
	pid=$(creat_process)
	pid_other=$(creat_process)

	echo 20000000000 > "$CPUCTL/1/cpu.shares"
	echo $pid > "$CPUCTL/1/tasks"
	cpu_usage=$(get_cpu_usage $pid)
	echo "pid $pid cpu_usage $cpu_usage"

	kill -9 $pid $pid_other
	expr 96 \< "$cpu_usage" \& "$cpu_usage" \< 103 > /dev/null 2>&1 || return 1
	return 0
}

case21()
{
	mkdir "$CPUCTL/1" "$CPUCTL/1/2"
	pid=$(creat_process)
	echo $pid > "$CPUCTL/1/tasks"

	while true
	do
		creat_process > "$CPUCTL/1/2/tasks"
		sleep 1
	done &
	loop_pid=$!

	sleep 10
	ret=0

	top_times=0
	while [ "$top_times" -lt 10 -a "$ret" = 0 ]
	do
		cpu_usage=$(get_cpu_usage $pid)
		echo "pid $pid cpu_usage $cpu_usage"
		expr 44 \< "$cpu_usage" \& "$cpu_usage" \< 56 > /dev/null 2>&1
		ret=$?
		: $(( top_times+=1 ))
	done

	kill -9 $pid $loop_pid > /dev/null 2>&1
	wait $pid $loop_pid >/dev/null 2>&1
	sleep 2
	kill_all_pid < "$CPUCTL/1/2/tasks"  >/dev/null 2>&1
	sleep 2
	return $ret
}

case22()
{
	mkdir "$CPUCTL/1" "$CPUCTL/1/2"
	pid=$(creat_process)
	while true
	do
		echo $pid > "$CPUCTL/1/tasks"
		echo $pid > "$CPUCTL/1/2/tasks"
		sleep 1
	done &
	loop_pid=$!

	sleep 10
	ret=0

	top_times=0
	while [ "$top_times" -lt 10 -a "$ret" = 0 ]
	do
		cpu_usage=$(get_cpu_usage $pid)
		echo "pid $pid cpu_usage $cpu_usage"
		expr 94 \< "$cpu_usage" \& "$cpu_usage" \< 106 > /dev/null 2>&1
		ret=$?
		: $(( top_times+=1 ))
	done

	kill -s KILL $pid $loop_pid > /dev/null 2>&1
	wait $pid $loop_pid >/dev/null 2>&1
	return $ret
}

rm -rf "$CPUCTL_TMP" >/dev/null 2>&1 || exit 1
mkdir -p "$CPUCTL_TMP" || exit 1

# test
do_test ()
{
	for i in $(seq 1 $TST_TOTAL)
	do
		setup || {
			tst_resm TFAIL "case$i    FAIL"
			continue
		}

		case$i || {
			tst_resm TFAIL "case$i    FAIL"
			cleanup
			continue
		}

		cleanup || {
			tst_resm TFAIL "case$i    FAIL"
		}

		tst_resm TPASS "case$i    PASS"
	done
}

do_test

rm -rf "$CPUCTL_TMP" >/dev/null 2>&1