xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/nice/nice05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright(c) 2022 Huawei Technologies Co., Ltd
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Li Mengfei <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  *         Zhao Gongyi <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * 1. Create a high nice thread and a low nice thread, the main
12*49cdfc7eSAndroid Build Coastguard Worker  *    thread wake them at the same time
13*49cdfc7eSAndroid Build Coastguard Worker  * 2. Both threads run on the same CPU
14*49cdfc7eSAndroid Build Coastguard Worker  * 3. Verify that the low nice thread executes more time than
15*49cdfc7eSAndroid Build Coastguard Worker  *    the high nice thread
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
19*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_pthread.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_clocks.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static pthread_barrier_t barrier;
29*49cdfc7eSAndroid Build Coastguard Worker 
set_nice(int nice_inc)30*49cdfc7eSAndroid Build Coastguard Worker static void set_nice(int nice_inc)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	int orig_nice;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	orig_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	TEST(nice(nice_inc));
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != (orig_nice + nice_inc)) {
39*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "nice(%d) returned %li, expected %i",
40*49cdfc7eSAndroid Build Coastguard Worker 			nice_inc, TST_RET, orig_nice + nice_inc);
41*49cdfc7eSAndroid Build Coastguard Worker 	}
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR)
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "nice(%d) failed", nice_inc);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
do_something(void)47*49cdfc7eSAndroid Build Coastguard Worker static void do_something(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	volatile int number = 0;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
52*49cdfc7eSAndroid Build Coastguard Worker 		number++;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 		TEST(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL));
55*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET != 0) {
56*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK | TRERRNO,
57*49cdfc7eSAndroid Build Coastguard Worker 				"pthread_setcancelstate() failed");
58*49cdfc7eSAndroid Build Coastguard Worker 		}
59*49cdfc7eSAndroid Build Coastguard Worker 		pthread_testcancel();
60*49cdfc7eSAndroid Build Coastguard Worker 	}
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker 
thread_fn(void * arg)63*49cdfc7eSAndroid Build Coastguard Worker static void *thread_fn(void *arg)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	set_nice((intptr_t)arg);
66*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_BARRIER_WAIT(&barrier);
67*49cdfc7eSAndroid Build Coastguard Worker 	do_something();
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)72*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
73*49cdfc7eSAndroid Build Coastguard Worker {
74*49cdfc7eSAndroid Build Coastguard Worker 	size_t size;
75*49cdfc7eSAndroid Build Coastguard Worker 	size_t i;
76*49cdfc7eSAndroid Build Coastguard Worker 	int nrcpus = 1024;
77*49cdfc7eSAndroid Build Coastguard Worker 	cpu_set_t *set;
78*49cdfc7eSAndroid Build Coastguard Worker 	int some_cpu;
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	set = CPU_ALLOC(nrcpus);
81*49cdfc7eSAndroid Build Coastguard Worker 	if (!set)
82*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "CPU_ALLOC()");
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	size = CPU_ALLOC_SIZE(nrcpus);
85*49cdfc7eSAndroid Build Coastguard Worker 	CPU_ZERO_S(size, set);
86*49cdfc7eSAndroid Build Coastguard Worker 	if (sched_getaffinity(0, size, set) < 0)
87*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "sched_getaffinity()");
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < size * 8; i++)
90*49cdfc7eSAndroid Build Coastguard Worker 		if (CPU_ISSET_S(i, size, set))
91*49cdfc7eSAndroid Build Coastguard Worker 			some_cpu = i;
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	CPU_ZERO_S(size, set);
94*49cdfc7eSAndroid Build Coastguard Worker 	CPU_SET_S(some_cpu, size, set);
95*49cdfc7eSAndroid Build Coastguard Worker 	if (sched_setaffinity(0, size, set) < 0)
96*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "sched_setaffinity()");
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	CPU_FREE(set);
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker 
verify_nice(void)101*49cdfc7eSAndroid Build Coastguard Worker static void verify_nice(void)
102*49cdfc7eSAndroid Build Coastguard Worker {
103*49cdfc7eSAndroid Build Coastguard Worker 	intptr_t nice_inc_high = -1;
104*49cdfc7eSAndroid Build Coastguard Worker 	intptr_t nice_inc_low = -2;
105*49cdfc7eSAndroid Build Coastguard Worker 	clockid_t nice_low_clockid, nice_high_clockid;
106*49cdfc7eSAndroid Build Coastguard Worker 	struct timespec nice_high_ts, nice_low_ts;
107*49cdfc7eSAndroid Build Coastguard Worker 	long long delta;
108*49cdfc7eSAndroid Build Coastguard Worker 	pthread_t thread[2];
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_BARRIER_INIT(&barrier, NULL, 3);
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_CREATE(&thread[0], NULL, thread_fn,
113*49cdfc7eSAndroid Build Coastguard Worker 			(void *)nice_inc_high);
114*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_CREATE(&thread[1], NULL, thread_fn,
115*49cdfc7eSAndroid Build Coastguard Worker 			(void *)nice_inc_low);
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_BARRIER_WAIT(&barrier);
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	sleep(tst_remaining_runtime());
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 	TEST(pthread_getcpuclockid(thread[1], &nice_low_clockid));
122*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0)
123*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TRERRNO, "clock_getcpuclockid() failed");
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	TEST(pthread_getcpuclockid(thread[0], &nice_high_clockid));
126*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0)
127*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TRERRNO, "clock_getcpuclockid() failed");
128*49cdfc7eSAndroid Build Coastguard Worker 
129*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOCK_GETTIME(nice_low_clockid, &nice_low_ts);
130*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOCK_GETTIME(nice_high_clockid, &nice_high_ts);
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Nice low thread CPU time: %ld.%09ld s",
133*49cdfc7eSAndroid Build Coastguard Worker 			nice_low_ts.tv_sec, nice_low_ts.tv_nsec);
134*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Nice high thread CPU time: %ld.%09ld s",
135*49cdfc7eSAndroid Build Coastguard Worker 			nice_high_ts.tv_sec, nice_high_ts.tv_nsec);
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker 	delta = tst_timespec_diff_ns(nice_low_ts, nice_high_ts);
138*49cdfc7eSAndroid Build Coastguard Worker 	if (delta < 0) {
139*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "executes less cycles than "
140*49cdfc7eSAndroid Build Coastguard Worker 				"the high nice thread, delta: %lld ns", delta);
141*49cdfc7eSAndroid Build Coastguard Worker 	} else {
142*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "executes more cycles than "
143*49cdfc7eSAndroid Build Coastguard Worker 				"the high nice thread, delta: %lld ns", delta);
144*49cdfc7eSAndroid Build Coastguard Worker 	}
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_CANCEL(thread[0]);
147*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_CANCEL(thread[1]);
148*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_BARRIER_DESTROY(&barrier);
149*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_JOIN(thread[0], NULL);
150*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_JOIN(thread[1], NULL);
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
154*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
155*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_nice,
156*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
157*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = 3,
158*49cdfc7eSAndroid Build Coastguard Worker };
159