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-2.c
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
23*49cdfc7eSAndroid Build Coastguard Worker * This testcase verifies if the low priority SCHED_RR thread can preempt
24*49cdfc7eSAndroid Build Coastguard Worker * the high priority SCHED_RR thread multiple times via priority
25*49cdfc7eSAndroid Build Coastguard Worker * inheritance.
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * USAGE:
28*49cdfc7eSAndroid Build Coastguard Worker * Use run_auto.sh script in current directory to build and run test.
29*49cdfc7eSAndroid Build Coastguard Worker *
30*49cdfc7eSAndroid Build Coastguard Worker * AUTHOR
31*49cdfc7eSAndroid Build Coastguard Worker *
32*49cdfc7eSAndroid Build Coastguard Worker *
33*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
34*49cdfc7eSAndroid Build Coastguard Worker * 2010-04-22 Code cleanup and thread synchronization changes by using
35*49cdfc7eSAndroid Build Coastguard Worker * conditional variables,
36*49cdfc7eSAndroid Build Coastguard Worker * by Gowrishankar([email protected]).
37*49cdfc7eSAndroid Build Coastguard Worker *
38*49cdfc7eSAndroid Build Coastguard Worker *****************************************************************************/
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sched.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-2 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
61*49cdfc7eSAndroid Build Coastguard Worker int handled = 1;
62*49cdfc7eSAndroid Build Coastguard Worker switch (c) {
63*49cdfc7eSAndroid Build Coastguard Worker case 'h':
64*49cdfc7eSAndroid Build Coastguard Worker usage();
65*49cdfc7eSAndroid Build Coastguard Worker exit(0);
66*49cdfc7eSAndroid Build Coastguard Worker default:
67*49cdfc7eSAndroid Build Coastguard Worker handled = 0;
68*49cdfc7eSAndroid Build Coastguard Worker break;
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker return handled;
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
gettid(void)73*49cdfc7eSAndroid Build Coastguard Worker int gettid(void)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker return syscall(__NR_gettid);
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker typedef void *(*entrypoint_t) (void *);
79*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t glob_mutex;
80*49cdfc7eSAndroid Build Coastguard Worker static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
81*49cdfc7eSAndroid Build Coastguard Worker static pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;
82*49cdfc7eSAndroid Build Coastguard Worker
func_lowrt(void * arg)83*49cdfc7eSAndroid Build Coastguard Worker void *func_lowrt(void *arg)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker struct thread *pthr = (struct thread *)arg;
86*49cdfc7eSAndroid Build Coastguard Worker int i, tid = gettid();
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d started running with priority %d\n", tid,
89*49cdfc7eSAndroid Build Coastguard Worker pthr->priority);
90*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&glob_mutex);
91*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d at start pthread pol %d pri %d - Got global lock\n",
92*49cdfc7eSAndroid Build Coastguard Worker tid, pthr->policy, pthr->priority);
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 %d started running with prio %d\n", tid,
147*49cdfc7eSAndroid Build Coastguard Worker pthr->priority);
148*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_wait(&barrier);
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker /* Let others wait at conditional variable */
151*49cdfc7eSAndroid Build Coastguard Worker usleep(1000);
152*49cdfc7eSAndroid Build Coastguard Worker
153*49cdfc7eSAndroid Build Coastguard Worker /* Noise thread begins the test */
154*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&cond_mutex);
155*49cdfc7eSAndroid Build Coastguard Worker pthread_cond_broadcast(&cond_var);
156*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&cond_mutex);
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 10000; i++) {
159*49cdfc7eSAndroid Build Coastguard Worker if (i % 100 == 0) {
160*49cdfc7eSAndroid Build Coastguard Worker printf("Noise Thread %d loop %d pthread pol %d "
161*49cdfc7eSAndroid Build Coastguard Worker "pri %d\n", tid, i, pthr->policy,
162*49cdfc7eSAndroid Build Coastguard Worker pthr->priority);
163*49cdfc7eSAndroid Build Coastguard Worker fflush(NULL);
164*49cdfc7eSAndroid Build Coastguard Worker }
165*49cdfc7eSAndroid Build Coastguard Worker busy_work_ms(1);
166*49cdfc7eSAndroid Build Coastguard Worker }
167*49cdfc7eSAndroid Build Coastguard Worker return NULL;
168*49cdfc7eSAndroid Build Coastguard Worker }
169*49cdfc7eSAndroid Build Coastguard Worker
170*49cdfc7eSAndroid Build Coastguard Worker /*
171*49cdfc7eSAndroid Build Coastguard Worker * Test pthread creation at different thread priorities.
172*49cdfc7eSAndroid Build Coastguard Worker */
main(int argc,char * argv[])173*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
174*49cdfc7eSAndroid Build Coastguard Worker {
175*49cdfc7eSAndroid Build Coastguard Worker int i, retc, nopi = 0;
176*49cdfc7eSAndroid Build Coastguard Worker cpu_set_t mask;
177*49cdfc7eSAndroid Build Coastguard Worker CPU_ZERO(&mask);
178*49cdfc7eSAndroid Build Coastguard Worker CPU_SET(0, &mask);
179*49cdfc7eSAndroid Build Coastguard Worker setup();
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
195*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < argc; i++) {
196*49cdfc7eSAndroid Build Coastguard Worker if (strcmp(argv[i], "nopi") == 0)
197*49cdfc7eSAndroid Build Coastguard Worker nopi = 1;
198*49cdfc7eSAndroid Build Coastguard Worker }
199*49cdfc7eSAndroid Build Coastguard Worker
200*49cdfc7eSAndroid Build Coastguard Worker printf("Start %s\n", argv[0]);
201*49cdfc7eSAndroid Build Coastguard Worker
202*49cdfc7eSAndroid Build Coastguard Worker if (!nopi)
203*49cdfc7eSAndroid Build Coastguard Worker init_pi_mutex(&glob_mutex);
204*49cdfc7eSAndroid Build Coastguard Worker
205*49cdfc7eSAndroid Build Coastguard Worker create_rr_thread(func_lowrt, NULL, 10);
206*49cdfc7eSAndroid Build Coastguard Worker create_rr_thread(func_rt, NULL, 20);
207*49cdfc7eSAndroid Build Coastguard Worker create_fifo_thread(func_rt, NULL, 30);
208*49cdfc7eSAndroid Build Coastguard Worker create_fifo_thread(func_rt, NULL, 40);
209*49cdfc7eSAndroid Build Coastguard Worker create_rr_thread(func_noise, NULL, 40);
210*49cdfc7eSAndroid Build Coastguard Worker
211*49cdfc7eSAndroid Build Coastguard Worker printf("Joining threads\n");
212*49cdfc7eSAndroid Build Coastguard Worker join_threads();
213*49cdfc7eSAndroid Build Coastguard Worker printf("Done\n");
214*49cdfc7eSAndroid Build Coastguard Worker printf("Criteria: Low Priority Thread and High Priority Thread "
215*49cdfc7eSAndroid Build Coastguard Worker "should prempt each other multiple times\n");
216*49cdfc7eSAndroid Build Coastguard Worker
217*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_destroy(&glob_mutex);
218*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_destroy(&cond_mutex);
219*49cdfc7eSAndroid Build Coastguard Worker pthread_cond_destroy(&cond_var);
220*49cdfc7eSAndroid Build Coastguard Worker
221*49cdfc7eSAndroid Build Coastguard Worker return 0;
222*49cdfc7eSAndroid Build Coastguard Worker }
223