xref: /aosp_15_r20/external/ltp/testcases/realtime/func/pi-tests/testpi-4.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright © International Business Machines  Corp., 2005, 2008
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * NAME
20*49cdfc7eSAndroid Build Coastguard Worker  *      testpi-4.c
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
23*49cdfc7eSAndroid Build Coastguard Worker  *     This testcase verifies that the SCHED_OTHER thread can preempt
24*49cdfc7eSAndroid Build Coastguard Worker  *     the SCHED_RR thread via priority inheritance.
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * USAGE:
27*49cdfc7eSAndroid Build Coastguard Worker  *      Use run_auto.sh script in current directory to build and run test.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * AUTHOR
30*49cdfc7eSAndroid Build Coastguard Worker  *
31*49cdfc7eSAndroid Build Coastguard Worker  *
32*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
33*49cdfc7eSAndroid Build Coastguard Worker  *      2010-06-29 Thread synchronization changes by using
34*49cdfc7eSAndroid Build Coastguard Worker  *		   conditional variables by Gowrishankar.
35*49cdfc7eSAndroid Build Coastguard Worker  *		   by Gowrishankar <[email protected]>
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  *****************************************************************************/
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <librttest.h>
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_t barrier;
51*49cdfc7eSAndroid Build Coastguard Worker 
usage(void)52*49cdfc7eSAndroid Build Coastguard Worker void usage(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	rt_help();
55*49cdfc7eSAndroid Build Coastguard Worker 	printf("testpi-4 specific options:\n");
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
parse_args(int c,char * v)58*49cdfc7eSAndroid Build Coastguard Worker int parse_args(int c, char *v)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker 	int handled = 1;
61*49cdfc7eSAndroid Build Coastguard Worker 	switch (c) {
62*49cdfc7eSAndroid Build Coastguard Worker 	case 'h':
63*49cdfc7eSAndroid Build Coastguard Worker 		usage();
64*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
65*49cdfc7eSAndroid Build Coastguard Worker 	default:
66*49cdfc7eSAndroid Build Coastguard Worker 		handled = 0;
67*49cdfc7eSAndroid Build Coastguard Worker 		break;
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker 	return handled;
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker 
gettid(void)72*49cdfc7eSAndroid Build Coastguard Worker int gettid(void)
73*49cdfc7eSAndroid Build Coastguard Worker {
74*49cdfc7eSAndroid Build Coastguard Worker 	return syscall(__NR_gettid);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker typedef void *(*entrypoint_t) (void *);
78*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *glob_mutex;
79*49cdfc7eSAndroid Build Coastguard Worker static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
80*49cdfc7eSAndroid Build Coastguard Worker static pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;
81*49cdfc7eSAndroid Build Coastguard Worker 
func_nonrt(void * arg)82*49cdfc7eSAndroid Build Coastguard Worker void *func_nonrt(void *arg)
83*49cdfc7eSAndroid Build Coastguard Worker {
84*49cdfc7eSAndroid Build Coastguard Worker 	struct thread *pthr = (struct thread *)arg;
85*49cdfc7eSAndroid Build Coastguard Worker 	int i, tid = gettid();
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	printf("Thread %d started running with priority %d\n", tid,
88*49cdfc7eSAndroid Build Coastguard Worker 	       pthr->priority);
89*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_lock(glob_mutex);
90*49cdfc7eSAndroid Build Coastguard Worker 	printf("Thread %d at start pthread pol %d pri %d - Got global lock\n",
91*49cdfc7eSAndroid Build Coastguard Worker 	       tid, pthr->policy, pthr->priority);
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	/* Wait for other RT threads to start up */
94*49cdfc7eSAndroid Build Coastguard Worker 	pthread_barrier_wait(&barrier);
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	/* Wait for the high priority noise thread to start and signal us */
97*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_lock(&cond_mutex);
98*49cdfc7eSAndroid Build Coastguard Worker 	pthread_cond_wait(&cond_var, &cond_mutex);
99*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_unlock(&cond_mutex);
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 10000; i++) {
102*49cdfc7eSAndroid Build Coastguard Worker 		if (i % 100 == 0) {
103*49cdfc7eSAndroid Build Coastguard Worker 			printf("Thread %d loop %d pthread pol %d pri %d\n",
104*49cdfc7eSAndroid Build Coastguard Worker 			       tid, i, pthr->policy, pthr->priority);
105*49cdfc7eSAndroid Build Coastguard Worker 			fflush(NULL);
106*49cdfc7eSAndroid Build Coastguard Worker 		}
107*49cdfc7eSAndroid Build Coastguard Worker 		busy_work_ms(1);
108*49cdfc7eSAndroid Build Coastguard Worker 	}
109*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_unlock(glob_mutex);
110*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
111*49cdfc7eSAndroid Build Coastguard Worker }
112*49cdfc7eSAndroid Build Coastguard Worker 
func_rt(void * arg)113*49cdfc7eSAndroid Build Coastguard Worker void *func_rt(void *arg)
114*49cdfc7eSAndroid Build Coastguard Worker {
115*49cdfc7eSAndroid Build Coastguard Worker 	struct thread *pthr = (struct thread *)arg;
116*49cdfc7eSAndroid Build Coastguard Worker 	int i, tid = gettid();
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	printf("Thread %d started running with prio %d\n", tid, pthr->priority);
119*49cdfc7eSAndroid Build Coastguard Worker 	pthread_barrier_wait(&barrier);
120*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_lock(glob_mutex);
121*49cdfc7eSAndroid Build Coastguard Worker 	printf("Thread %d at start pthread pol %d pri %d - Got global lock\n",
122*49cdfc7eSAndroid Build Coastguard Worker 	       tid, pthr->policy, pthr->priority);
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	/* we just use the mutex as something to slow things down,
125*49cdfc7eSAndroid Build Coastguard Worker 	 * say who we are and then do nothing for a while.  The aim
126*49cdfc7eSAndroid Build Coastguard Worker 	 * of this is to show that high priority threads make more
127*49cdfc7eSAndroid Build Coastguard Worker 	 * progress than lower priority threads..
128*49cdfc7eSAndroid Build Coastguard Worker 	 */
129*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 1000; i++) {
130*49cdfc7eSAndroid Build Coastguard Worker 		if (i % 100 == 0) {
131*49cdfc7eSAndroid Build Coastguard Worker 			printf("Thread %d loop %d pthread pol %d pri %d\n",
132*49cdfc7eSAndroid Build Coastguard Worker 			       tid, i, pthr->policy, pthr->priority);
133*49cdfc7eSAndroid Build Coastguard Worker 			fflush(NULL);
134*49cdfc7eSAndroid Build Coastguard Worker 		}
135*49cdfc7eSAndroid Build Coastguard Worker 		busy_work_ms(1);
136*49cdfc7eSAndroid Build Coastguard Worker 	}
137*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_unlock(glob_mutex);
138*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker 
func_noise(void * arg)141*49cdfc7eSAndroid Build Coastguard Worker void *func_noise(void *arg)
142*49cdfc7eSAndroid Build Coastguard Worker {
143*49cdfc7eSAndroid Build Coastguard Worker 	struct thread *pthr = (struct thread *)arg;
144*49cdfc7eSAndroid Build Coastguard Worker 	int i, tid = gettid();
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 	printf("Noise Thread started running with prio %d\n", pthr->priority);
147*49cdfc7eSAndroid Build Coastguard Worker 	pthread_barrier_wait(&barrier);
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	/* Give the other threads time to wait on the condition variable. */
150*49cdfc7eSAndroid Build Coastguard Worker 	usleep(1000);
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker 	/* Noise thread begins the test */
153*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_lock(&cond_mutex);
154*49cdfc7eSAndroid Build Coastguard Worker 	pthread_cond_broadcast(&cond_var);
155*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_unlock(&cond_mutex);
156*49cdfc7eSAndroid Build Coastguard Worker 
157*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 10000; i++) {
158*49cdfc7eSAndroid Build Coastguard Worker 		if (i % 100 == 0) {
159*49cdfc7eSAndroid Build Coastguard Worker 			printf("Noise Thread %d loop %d pthread pol %d "
160*49cdfc7eSAndroid Build Coastguard Worker 			       "pri %d\n", tid, i, pthr->policy,
161*49cdfc7eSAndroid Build Coastguard Worker 			       pthr->priority);
162*49cdfc7eSAndroid Build Coastguard Worker 			fflush(NULL);
163*49cdfc7eSAndroid Build Coastguard Worker 		}
164*49cdfc7eSAndroid Build Coastguard Worker 		busy_work_ms(1);
165*49cdfc7eSAndroid Build Coastguard Worker 	}
166*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
167*49cdfc7eSAndroid Build Coastguard Worker }
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker /*
170*49cdfc7eSAndroid Build Coastguard Worker  * Test pthread creation at different thread priorities.
171*49cdfc7eSAndroid Build Coastguard Worker  */
main(int argc,char * argv[])172*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
173*49cdfc7eSAndroid Build Coastguard Worker {
174*49cdfc7eSAndroid Build Coastguard Worker 	int i, retc, nopi = 0;
175*49cdfc7eSAndroid Build Coastguard Worker 	cpu_set_t mask;
176*49cdfc7eSAndroid Build Coastguard Worker 	CPU_ZERO(&mask);
177*49cdfc7eSAndroid Build Coastguard Worker 	CPU_SET(0, &mask);
178*49cdfc7eSAndroid Build Coastguard Worker 	setup();
179*49cdfc7eSAndroid Build Coastguard Worker 
180*49cdfc7eSAndroid Build Coastguard Worker 	rt_init("h", parse_args, argc, argv);
181*49cdfc7eSAndroid Build Coastguard Worker 
182*49cdfc7eSAndroid Build Coastguard Worker 	retc = pthread_barrier_init(&barrier, NULL, 5);
183*49cdfc7eSAndroid Build Coastguard Worker 	if (retc) {
184*49cdfc7eSAndroid Build Coastguard Worker 		printf("pthread_barrier_init failed: %s\n", strerror(retc));
185*49cdfc7eSAndroid Build Coastguard Worker 		exit(retc);
186*49cdfc7eSAndroid Build Coastguard Worker 	}
187*49cdfc7eSAndroid Build Coastguard Worker 
188*49cdfc7eSAndroid Build Coastguard Worker 	retc = sched_setaffinity(0, sizeof(mask), &mask);
189*49cdfc7eSAndroid Build Coastguard Worker 	if (retc < 0) {
190*49cdfc7eSAndroid Build Coastguard Worker 		printf("Main Thread: Can't set affinity: %d %s\n", retc,
191*49cdfc7eSAndroid Build Coastguard Worker 		       strerror(retc));
192*49cdfc7eSAndroid Build Coastguard Worker 		exit(-1);
193*49cdfc7eSAndroid Build Coastguard Worker 	}
194*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < argc; i++) {
195*49cdfc7eSAndroid Build Coastguard Worker 		if (strcmp(argv[i], "nopi") == 0)
196*49cdfc7eSAndroid Build Coastguard Worker 			nopi = 1;
197*49cdfc7eSAndroid Build Coastguard Worker 	}
198*49cdfc7eSAndroid Build Coastguard Worker 
199*49cdfc7eSAndroid Build Coastguard Worker 	printf("Start %s\n", argv[0]);
200*49cdfc7eSAndroid Build Coastguard Worker 
201*49cdfc7eSAndroid Build Coastguard Worker 	glob_mutex = malloc(sizeof(pthread_mutex_t));
202*49cdfc7eSAndroid Build Coastguard Worker 	if (glob_mutex == NULL) {
203*49cdfc7eSAndroid Build Coastguard Worker 		printf("Malloc failed\n");
204*49cdfc7eSAndroid Build Coastguard Worker 		exit(errno);
205*49cdfc7eSAndroid Build Coastguard Worker 	}
206*49cdfc7eSAndroid Build Coastguard Worker 
207*49cdfc7eSAndroid Build Coastguard Worker 	if (!nopi)
208*49cdfc7eSAndroid Build Coastguard Worker 		init_pi_mutex(glob_mutex);
209*49cdfc7eSAndroid Build Coastguard Worker 
210*49cdfc7eSAndroid Build Coastguard Worker 	create_other_thread(func_nonrt, NULL);
211*49cdfc7eSAndroid Build Coastguard Worker 	create_rr_thread(func_rt, NULL, 20);
212*49cdfc7eSAndroid Build Coastguard Worker 	create_rr_thread(func_rt, NULL, 30);
213*49cdfc7eSAndroid Build Coastguard Worker 	create_rr_thread(func_rt, NULL, 40);
214*49cdfc7eSAndroid Build Coastguard Worker 	create_rr_thread(func_noise, NULL, 40);
215*49cdfc7eSAndroid Build Coastguard Worker 
216*49cdfc7eSAndroid Build Coastguard Worker 	printf("Joining threads\n");
217*49cdfc7eSAndroid Build Coastguard Worker 	join_threads();
218*49cdfc7eSAndroid Build Coastguard Worker 	printf("Done\n");
219*49cdfc7eSAndroid Build Coastguard Worker 
220*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_destroy(glob_mutex);
221*49cdfc7eSAndroid Build Coastguard Worker 	pthread_mutex_destroy(&cond_mutex);
222*49cdfc7eSAndroid Build Coastguard Worker 	pthread_cond_destroy(&cond_var);
223*49cdfc7eSAndroid Build Coastguard Worker 
224*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
225*49cdfc7eSAndroid Build Coastguard Worker }
226