1*49cdfc7eSAndroid Build Coastguard Worker /******************************************************************************
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright © International Business Machines Corp., 2006, 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 * prio-wake.c
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
23*49cdfc7eSAndroid Build Coastguard Worker * Test priority ordered wakeup with pthread_cond_*
24*49cdfc7eSAndroid Build Coastguard Worker * * Steps:
25*49cdfc7eSAndroid Build Coastguard Worker * - Creates a number of worker threads with increasing FIFO priorities
26*49cdfc7eSAndroid Build Coastguard Worker * (by default, num worker threads = num cpus)
27*49cdfc7eSAndroid Build Coastguard Worker * - Create a master thread
28*49cdfc7eSAndroid Build Coastguard Worker * - The time the worker thread starts running is noted. Each of the
29*49cdfc7eSAndroid Build Coastguard Worker * worker threads then waits on the same _condvar_. The time it
30*49cdfc7eSAndroid Build Coastguard Worker * wakes up also noted.
31*49cdfc7eSAndroid Build Coastguard Worker * - Once all the threads finish execution, the start and wakeup times
32*49cdfc7eSAndroid Build Coastguard Worker * of all the threads is displayed.
33*49cdfc7eSAndroid Build Coastguard Worker * - The output must indicate that the thread wakeup happened in a
34*49cdfc7eSAndroid Build Coastguard Worker * priority order.
35*49cdfc7eSAndroid Build Coastguard Worker *
36*49cdfc7eSAndroid Build Coastguard Worker * USAGE:
37*49cdfc7eSAndroid Build Coastguard Worker *
38*49cdfc7eSAndroid Build Coastguard Worker *
39*49cdfc7eSAndroid Build Coastguard Worker * AUTHOR
40*49cdfc7eSAndroid Build Coastguard Worker * Darren Hart <[email protected]>
41*49cdfc7eSAndroid Build Coastguard Worker *
42*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
43*49cdfc7eSAndroid Build Coastguard Worker * 2006-Apr-26: Initial version by Darren Hart
44*49cdfc7eSAndroid Build Coastguard Worker * 2006-May-25: Updated to use new librt.h features
45*49cdfc7eSAndroid Build Coastguard Worker *
46*49cdfc7eSAndroid Build Coastguard Worker *****************************************************************************/
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
54*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
55*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
56*49cdfc7eSAndroid Build Coastguard Worker #include <librttest.h>
57*49cdfc7eSAndroid Build Coastguard Worker #include <libstats.h>
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker volatile int running_threads = 0;
60*49cdfc7eSAndroid Build Coastguard Worker static int rt_threads = 0;
61*49cdfc7eSAndroid Build Coastguard Worker static int locked_broadcast = 1;
62*49cdfc7eSAndroid Build Coastguard Worker static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
63*49cdfc7eSAndroid Build Coastguard Worker static pthread_mutex_t mutex;
64*49cdfc7eSAndroid Build Coastguard Worker static volatile nsec_t beginrun;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker static int ret = 0;
67*49cdfc7eSAndroid Build Coastguard Worker
usage(void)68*49cdfc7eSAndroid Build Coastguard Worker void usage(void)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker rt_help();
71*49cdfc7eSAndroid Build Coastguard Worker printf("prio-wake specific options:\n");
72*49cdfc7eSAndroid Build Coastguard Worker printf(" -n# #: number of worker threads\n");
73*49cdfc7eSAndroid Build Coastguard Worker printf(" -l# 1:lock the mutex before broadcast, 0:don't\n");
74*49cdfc7eSAndroid Build Coastguard Worker printf(" defaults to 1\n");
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
parse_args(int c,char * v)77*49cdfc7eSAndroid Build Coastguard Worker int parse_args(int c, char *v)
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker int handled = 1;
81*49cdfc7eSAndroid Build Coastguard Worker switch (c) {
82*49cdfc7eSAndroid Build Coastguard Worker case 'h':
83*49cdfc7eSAndroid Build Coastguard Worker usage();
84*49cdfc7eSAndroid Build Coastguard Worker exit(0);
85*49cdfc7eSAndroid Build Coastguard Worker case 'n':
86*49cdfc7eSAndroid Build Coastguard Worker rt_threads = atoi(v);
87*49cdfc7eSAndroid Build Coastguard Worker break;
88*49cdfc7eSAndroid Build Coastguard Worker case 'l':
89*49cdfc7eSAndroid Build Coastguard Worker locked_broadcast = atoi(v);
90*49cdfc7eSAndroid Build Coastguard Worker break;
91*49cdfc7eSAndroid Build Coastguard Worker default:
92*49cdfc7eSAndroid Build Coastguard Worker handled = 0;
93*49cdfc7eSAndroid Build Coastguard Worker break;
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker return handled;
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker struct array {
99*49cdfc7eSAndroid Build Coastguard Worker int *arr;
100*49cdfc7eSAndroid Build Coastguard Worker int counter;
101*49cdfc7eSAndroid Build Coastguard Worker };
102*49cdfc7eSAndroid Build Coastguard Worker struct array wakeup = { NULL, 0 };
103*49cdfc7eSAndroid Build Coastguard Worker
master_thread(void * arg)104*49cdfc7eSAndroid Build Coastguard Worker void *master_thread(void *arg)
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker int rc;
107*49cdfc7eSAndroid Build Coastguard Worker nsec_t start;
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker /* make sure children are started */
110*49cdfc7eSAndroid Build Coastguard Worker while (running_threads < rt_threads)
111*49cdfc7eSAndroid Build Coastguard Worker usleep(1000);
112*49cdfc7eSAndroid Build Coastguard Worker /* give the worker threads a chance to get to sleep in the kernel
113*49cdfc7eSAndroid Build Coastguard Worker * in the unlocked broadcast case. */
114*49cdfc7eSAndroid Build Coastguard Worker usleep(1000);
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker start = rt_gettime() - beginrun;
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard Worker printf("%08lld us: Master thread about to wake the workers\n",
119*49cdfc7eSAndroid Build Coastguard Worker start / NS_PER_US);
120*49cdfc7eSAndroid Build Coastguard Worker /* start the children threads */
121*49cdfc7eSAndroid Build Coastguard Worker if (locked_broadcast)
122*49cdfc7eSAndroid Build Coastguard Worker rc = pthread_mutex_lock(&mutex);
123*49cdfc7eSAndroid Build Coastguard Worker rc = pthread_cond_broadcast(&cond);
124*49cdfc7eSAndroid Build Coastguard Worker if (locked_broadcast)
125*49cdfc7eSAndroid Build Coastguard Worker rc = pthread_mutex_unlock(&mutex);
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Worker return NULL;
128*49cdfc7eSAndroid Build Coastguard Worker }
129*49cdfc7eSAndroid Build Coastguard Worker
worker_thread(void * arg)130*49cdfc7eSAndroid Build Coastguard Worker void *worker_thread(void *arg)
131*49cdfc7eSAndroid Build Coastguard Worker {
132*49cdfc7eSAndroid Build Coastguard Worker struct sched_param sched_param;
133*49cdfc7eSAndroid Build Coastguard Worker int policy;
134*49cdfc7eSAndroid Build Coastguard Worker int rc;
135*49cdfc7eSAndroid Build Coastguard Worker int mypri;
136*49cdfc7eSAndroid Build Coastguard Worker int j;
137*49cdfc7eSAndroid Build Coastguard Worker nsec_t start, wake;
138*49cdfc7eSAndroid Build Coastguard Worker j = (intptr_t) arg;
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker if (pthread_getschedparam(pthread_self(), &policy, &sched_param) != 0) {
141*49cdfc7eSAndroid Build Coastguard Worker printf
142*49cdfc7eSAndroid Build Coastguard Worker ("ERR: Couldn't get pthread info. Priority value wrong\n");
143*49cdfc7eSAndroid Build Coastguard Worker mypri = -1;
144*49cdfc7eSAndroid Build Coastguard Worker } else {
145*49cdfc7eSAndroid Build Coastguard Worker mypri = sched_param.sched_priority;
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker start = rt_gettime() - beginrun;
149*49cdfc7eSAndroid Build Coastguard Worker debug(0, "%08lld us: RealtimeThread-%03d pri %03d started\n",
150*49cdfc7eSAndroid Build Coastguard Worker start / NS_PER_US, j, mypri);
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard Worker rc = pthread_mutex_lock(&mutex);
153*49cdfc7eSAndroid Build Coastguard Worker running_threads++;
154*49cdfc7eSAndroid Build Coastguard Worker rc = pthread_cond_wait(&cond, &mutex);
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker wake = rt_gettime() - beginrun;
157*49cdfc7eSAndroid Build Coastguard Worker running_threads--;
158*49cdfc7eSAndroid Build Coastguard Worker wakeup.arr[wakeup.counter++] = mypri;
159*49cdfc7eSAndroid Build Coastguard Worker debug(0, "%08lld us: RealtimeThread-%03d pri %03d awake\n",
160*49cdfc7eSAndroid Build Coastguard Worker wake / NS_PER_US, j, mypri);
161*49cdfc7eSAndroid Build Coastguard Worker
162*49cdfc7eSAndroid Build Coastguard Worker rc = pthread_mutex_unlock(&mutex);
163*49cdfc7eSAndroid Build Coastguard Worker
164*49cdfc7eSAndroid Build Coastguard Worker return NULL;
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char * argv[])167*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
168*49cdfc7eSAndroid Build Coastguard Worker {
169*49cdfc7eSAndroid Build Coastguard Worker int threads_per_prio;
170*49cdfc7eSAndroid Build Coastguard Worker int numcpus;
171*49cdfc7eSAndroid Build Coastguard Worker int numprios;
172*49cdfc7eSAndroid Build Coastguard Worker int prio;
173*49cdfc7eSAndroid Build Coastguard Worker int i;
174*49cdfc7eSAndroid Build Coastguard Worker setup();
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker rt_init("hn:l:", parse_args, argc, argv);
177*49cdfc7eSAndroid Build Coastguard Worker
178*49cdfc7eSAndroid Build Coastguard Worker if (rt_threads == 0) {
179*49cdfc7eSAndroid Build Coastguard Worker numcpus = sysconf(_SC_NPROCESSORS_ONLN);
180*49cdfc7eSAndroid Build Coastguard Worker rt_threads = numcpus;
181*49cdfc7eSAndroid Build Coastguard Worker }
182*49cdfc7eSAndroid Build Coastguard Worker wakeup.arr = malloc(rt_threads * sizeof(int));
183*49cdfc7eSAndroid Build Coastguard Worker wakeup.counter = 0;
184*49cdfc7eSAndroid Build Coastguard Worker printf("\n-----------------------\n");
185*49cdfc7eSAndroid Build Coastguard Worker printf("Priority Ordered Wakeup\n");
186*49cdfc7eSAndroid Build Coastguard Worker printf("-----------------------\n");
187*49cdfc7eSAndroid Build Coastguard Worker printf("Worker Threads: %d\n", rt_threads);
188*49cdfc7eSAndroid Build Coastguard Worker printf("Calling pthread_cond_broadcast() with mutex: %s\n\n",
189*49cdfc7eSAndroid Build Coastguard Worker locked_broadcast ? "LOCKED" : "UNLOCKED");
190*49cdfc7eSAndroid Build Coastguard Worker
191*49cdfc7eSAndroid Build Coastguard Worker beginrun = rt_gettime();
192*49cdfc7eSAndroid Build Coastguard Worker
193*49cdfc7eSAndroid Build Coastguard Worker init_pi_mutex(&mutex);
194*49cdfc7eSAndroid Build Coastguard Worker
195*49cdfc7eSAndroid Build Coastguard Worker /* calculate the number of threads per priority */
196*49cdfc7eSAndroid Build Coastguard Worker /* we get num numprios -1 for the workers, leaving one for the master */
197*49cdfc7eSAndroid Build Coastguard Worker numprios = sched_get_priority_max(SCHED_FIFO) -
198*49cdfc7eSAndroid Build Coastguard Worker sched_get_priority_min(SCHED_FIFO);
199*49cdfc7eSAndroid Build Coastguard Worker
200*49cdfc7eSAndroid Build Coastguard Worker threads_per_prio = rt_threads / numprios;
201*49cdfc7eSAndroid Build Coastguard Worker if (rt_threads % numprios)
202*49cdfc7eSAndroid Build Coastguard Worker threads_per_prio++;
203*49cdfc7eSAndroid Build Coastguard Worker
204*49cdfc7eSAndroid Build Coastguard Worker /* start the worker threads */
205*49cdfc7eSAndroid Build Coastguard Worker prio = sched_get_priority_min(SCHED_FIFO);
206*49cdfc7eSAndroid Build Coastguard Worker for (i = rt_threads; i > 0; i--) {
207*49cdfc7eSAndroid Build Coastguard Worker if ((i != rt_threads && (i % threads_per_prio) == 0))
208*49cdfc7eSAndroid Build Coastguard Worker prio++;
209*49cdfc7eSAndroid Build Coastguard Worker create_fifo_thread(worker_thread, (void *)(intptr_t) i, prio);
210*49cdfc7eSAndroid Build Coastguard Worker }
211*49cdfc7eSAndroid Build Coastguard Worker
212*49cdfc7eSAndroid Build Coastguard Worker /* start the master thread */
213*49cdfc7eSAndroid Build Coastguard Worker create_fifo_thread(master_thread, (void *)(intptr_t) i, ++prio);
214*49cdfc7eSAndroid Build Coastguard Worker
215*49cdfc7eSAndroid Build Coastguard Worker /* wait for threads to complete */
216*49cdfc7eSAndroid Build Coastguard Worker join_threads();
217*49cdfc7eSAndroid Build Coastguard Worker
218*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_destroy(&mutex);
219*49cdfc7eSAndroid Build Coastguard Worker
220*49cdfc7eSAndroid Build Coastguard Worker printf("\nCriteria: Threads should be woken up in priority order\n");
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < (wakeup.counter - 1); i++) {
223*49cdfc7eSAndroid Build Coastguard Worker if (wakeup.arr[i] < wakeup.arr[i + 1]) {
224*49cdfc7eSAndroid Build Coastguard Worker printf("FAIL: Thread %d woken before %d\n",
225*49cdfc7eSAndroid Build Coastguard Worker wakeup.arr[i], wakeup.arr[i + 1]);
226*49cdfc7eSAndroid Build Coastguard Worker ret++;
227*49cdfc7eSAndroid Build Coastguard Worker }
228*49cdfc7eSAndroid Build Coastguard Worker }
229*49cdfc7eSAndroid Build Coastguard Worker printf("Result: %s\n", ret ? "FAIL" : "PASS");
230*49cdfc7eSAndroid Build Coastguard Worker return ret;
231*49cdfc7eSAndroid Build Coastguard Worker }
232