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-1.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_OTHER thread can preempt
24*49cdfc7eSAndroid Build Coastguard Worker * the high priority 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-04-22 Code cleanup and thread synchronization changes by using
34*49cdfc7eSAndroid Build Coastguard Worker * conditional variables,
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 <pthread.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <librttest.h>
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_t barrier;
50*49cdfc7eSAndroid Build Coastguard Worker
usage(void)51*49cdfc7eSAndroid Build Coastguard Worker void usage(void)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker rt_help();
54*49cdfc7eSAndroid Build Coastguard Worker printf("testpi-1 specific options:\n");
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
parse_args(int c,char * v)57*49cdfc7eSAndroid Build Coastguard Worker int parse_args(int c, char *v)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker int handled = 1;
60*49cdfc7eSAndroid Build Coastguard Worker switch (c) {
61*49cdfc7eSAndroid Build Coastguard Worker case 'h':
62*49cdfc7eSAndroid Build Coastguard Worker usage();
63*49cdfc7eSAndroid Build Coastguard Worker exit(0);
64*49cdfc7eSAndroid Build Coastguard Worker default:
65*49cdfc7eSAndroid Build Coastguard Worker handled = 0;
66*49cdfc7eSAndroid Build Coastguard Worker break;
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker return handled;
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker
gettid(void)71*49cdfc7eSAndroid Build Coastguard Worker int gettid(void)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker return syscall(__NR_gettid);
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker typedef void *(*entrypoint_t) (void *);
77*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t glob_mutex;
78*49cdfc7eSAndroid Build Coastguard Worker static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
79*49cdfc7eSAndroid Build Coastguard Worker static pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;
80*49cdfc7eSAndroid Build Coastguard Worker
func_nonrt(void * arg)81*49cdfc7eSAndroid Build Coastguard Worker void *func_nonrt(void *arg)
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker struct thread *pthr = (struct thread *)arg;
84*49cdfc7eSAndroid Build Coastguard Worker int i, tid = gettid();
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d started running with priority %d\n", tid,
87*49cdfc7eSAndroid Build Coastguard Worker pthr->priority);
88*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&glob_mutex);
89*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d at start pthread pol %d pri %d - Got global lock\n",
90*49cdfc7eSAndroid Build Coastguard Worker tid, pthr->policy, pthr->priority);
91*49cdfc7eSAndroid Build Coastguard Worker /* Wait for other RT threads to start up */
92*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_wait(&barrier);
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker /* Wait for the high priority noise thread to start and signal us */
95*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&cond_mutex);
96*49cdfc7eSAndroid Build Coastguard Worker pthread_cond_wait(&cond_var, &cond_mutex);
97*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&cond_mutex);
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 10000; i++) {
100*49cdfc7eSAndroid Build Coastguard Worker if (i % 100 == 0) {
101*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d loop %d pthread pol %d pri %d\n",
102*49cdfc7eSAndroid Build Coastguard Worker tid, i, pthr->policy, pthr->priority);
103*49cdfc7eSAndroid Build Coastguard Worker fflush(NULL);
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker busy_work_ms(1);
106*49cdfc7eSAndroid Build Coastguard Worker }
107*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&glob_mutex);
108*49cdfc7eSAndroid Build Coastguard Worker return NULL;
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker
func_rt(void * arg)111*49cdfc7eSAndroid Build Coastguard Worker void *func_rt(void *arg)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker struct thread *pthr = (struct thread *)arg;
114*49cdfc7eSAndroid Build Coastguard Worker int i, tid = gettid();
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d started running with prio %d\n", tid, pthr->priority);
117*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_wait(&barrier);
118*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&glob_mutex);
119*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d at start pthread pol %d pri %d - Got global lock\n",
120*49cdfc7eSAndroid Build Coastguard Worker tid, pthr->policy, pthr->priority);
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker /* We just use the mutex as something to slow things down,
123*49cdfc7eSAndroid Build Coastguard Worker * say who we are and then do nothing for a while. The aim
124*49cdfc7eSAndroid Build Coastguard Worker * of this is to show that high priority threads make more
125*49cdfc7eSAndroid Build Coastguard Worker * progress than lower priority threads..
126*49cdfc7eSAndroid Build Coastguard Worker */
127*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 1000; i++) {
128*49cdfc7eSAndroid Build Coastguard Worker if (i % 100 == 0) {
129*49cdfc7eSAndroid Build Coastguard Worker printf("Thread %d loop %d pthread pol %d pri %d\n",
130*49cdfc7eSAndroid Build Coastguard Worker tid, i, pthr->policy, pthr->priority);
131*49cdfc7eSAndroid Build Coastguard Worker fflush(NULL);
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker busy_work_ms(1);
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&glob_mutex);
136*49cdfc7eSAndroid Build Coastguard Worker return NULL;
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker
func_noise(void * arg)139*49cdfc7eSAndroid Build Coastguard Worker void *func_noise(void *arg)
140*49cdfc7eSAndroid Build Coastguard Worker {
141*49cdfc7eSAndroid Build Coastguard Worker struct thread *pthr = (struct thread *)arg;
142*49cdfc7eSAndroid Build Coastguard Worker int i, tid = gettid();
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker printf("Noise Thread %d started running with prio %d\n", tid,
145*49cdfc7eSAndroid Build Coastguard Worker pthr->priority);
146*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_wait(&barrier);
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker /* Let others wait at conditional variable */
149*49cdfc7eSAndroid Build Coastguard Worker usleep(1000);
150*49cdfc7eSAndroid Build Coastguard Worker
151*49cdfc7eSAndroid Build Coastguard Worker /* Noise thread begins the test */
152*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_lock(&cond_mutex);
153*49cdfc7eSAndroid Build Coastguard Worker pthread_cond_broadcast(&cond_var);
154*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_unlock(&cond_mutex);
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 10000; i++) {
157*49cdfc7eSAndroid Build Coastguard Worker if (i % 100 == 0) {
158*49cdfc7eSAndroid Build Coastguard Worker printf("Noise Thread %d loop %d pthread pol %d "
159*49cdfc7eSAndroid Build Coastguard Worker "pri %d\n", tid, i, pthr->policy,
160*49cdfc7eSAndroid Build Coastguard Worker pthr->priority);
161*49cdfc7eSAndroid Build Coastguard Worker fflush(NULL);
162*49cdfc7eSAndroid Build Coastguard Worker }
163*49cdfc7eSAndroid Build Coastguard Worker busy_work_ms(1);
164*49cdfc7eSAndroid Build Coastguard Worker }
165*49cdfc7eSAndroid Build Coastguard Worker return NULL;
166*49cdfc7eSAndroid Build Coastguard Worker }
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker /*
169*49cdfc7eSAndroid Build Coastguard Worker * Test pthread creation at different thread priorities.
170*49cdfc7eSAndroid Build Coastguard Worker */
main(int argc,char * argv[])171*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
172*49cdfc7eSAndroid Build Coastguard Worker {
173*49cdfc7eSAndroid Build Coastguard Worker int i, retc, nopi = 0;
174*49cdfc7eSAndroid Build Coastguard Worker cpu_set_t mask;
175*49cdfc7eSAndroid Build Coastguard Worker CPU_ZERO(&mask);
176*49cdfc7eSAndroid Build Coastguard Worker CPU_SET(0, &mask);
177*49cdfc7eSAndroid Build Coastguard Worker setup();
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard Worker rt_init("h", parse_args, argc, argv);
180*49cdfc7eSAndroid Build Coastguard Worker
181*49cdfc7eSAndroid Build Coastguard Worker retc = pthread_barrier_init(&barrier, NULL, 5);
182*49cdfc7eSAndroid Build Coastguard Worker if (retc) {
183*49cdfc7eSAndroid Build Coastguard Worker printf("pthread_barrier_init failed: %s\n", strerror(retc));
184*49cdfc7eSAndroid Build Coastguard Worker exit(retc);
185*49cdfc7eSAndroid Build Coastguard Worker }
186*49cdfc7eSAndroid Build Coastguard Worker
187*49cdfc7eSAndroid Build Coastguard Worker retc = sched_setaffinity(0, sizeof(mask), &mask);
188*49cdfc7eSAndroid Build Coastguard Worker if (retc < 0) {
189*49cdfc7eSAndroid Build Coastguard Worker printf("Main Thread: Can't set affinity: %d %s\n", retc,
190*49cdfc7eSAndroid Build Coastguard Worker strerror(retc));
191*49cdfc7eSAndroid Build Coastguard Worker exit(-1);
192*49cdfc7eSAndroid Build Coastguard Worker }
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 if (!nopi)
202*49cdfc7eSAndroid Build Coastguard Worker init_pi_mutex(&glob_mutex);
203*49cdfc7eSAndroid Build Coastguard Worker
204*49cdfc7eSAndroid Build Coastguard Worker create_other_thread(func_nonrt, NULL);
205*49cdfc7eSAndroid Build Coastguard Worker create_rr_thread(func_rt, NULL, 20);
206*49cdfc7eSAndroid Build Coastguard Worker create_rr_thread(func_rt, NULL, 30);
207*49cdfc7eSAndroid Build Coastguard Worker create_rr_thread(func_rt, NULL, 40);
208*49cdfc7eSAndroid Build Coastguard Worker create_rr_thread(func_noise, NULL, 40);
209*49cdfc7eSAndroid Build Coastguard Worker
210*49cdfc7eSAndroid Build Coastguard Worker printf("Joining threads\n");
211*49cdfc7eSAndroid Build Coastguard Worker join_threads();
212*49cdfc7eSAndroid Build Coastguard Worker printf("Done\n");
213*49cdfc7eSAndroid Build Coastguard Worker printf("Criteria:Low Priority Thread should Preempt Higher Priority "
214*49cdfc7eSAndroid Build Coastguard Worker "Noise Thread\n");
215*49cdfc7eSAndroid Build Coastguard Worker
216*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_destroy(&glob_mutex);
217*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_destroy(&cond_mutex);
218*49cdfc7eSAndroid Build Coastguard Worker pthread_cond_destroy(&cond_var);
219*49cdfc7eSAndroid Build Coastguard Worker
220*49cdfc7eSAndroid Build Coastguard Worker return 0;
221*49cdfc7eSAndroid Build Coastguard Worker }
222