1 /******************************************************************************
2 *
3 * Copyright © International Business Machines Corp., 2005, 2008
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * NAME
20 * pthread_cond_latency.c
21 *
22 * DESCRIPTION
23 * measure pthread_cond_t latencies
24 *
25 * USAGE:
26 * Use run_auto.sh script in current directory to build and run test.
27 *
28 * AUTHOR
29 * Paul E. McKenney <paulmck@us.ibm.com>
30 *
31 * HISTORY
32 *
33 *
34 *****************************************************************************/
35
36 #include <stdio.h>
37 #include <pthread.h>
38 #include <sys/time.h>
39 #include <sched.h>
40 #include <sys/poll.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <librttest.h>
45
46 pthread_mutex_t child_mutex = PTHREAD_MUTEX_INITIALIZER;
47 volatile int child_waiting = 0;
48 double endtime;
49
usage(void)50 void usage(void)
51 {
52 rt_help();
53 printf("testpi-1 specific options:\n");
54 }
55
parse_args(int c,char * v)56 int parse_args(int c, char *v)
57 {
58
59 int handled = 1;
60 switch (c) {
61 case 'h':
62 usage();
63 exit(0);
64 default:
65 handled = 0;
66 break;
67 }
68 return handled;
69 }
70
71 /*
72 * Return time as a floating-point number rather than struct timeval.
73 */
74
d_gettimeofday(void)75 double d_gettimeofday(void)
76 {
77 int retval;
78 struct timeval tv;
79
80 retval = gettimeofday(&tv, NULL);
81 if (retval != 0) {
82 perror("gettimeofday");
83 exit(-1);
84 }
85 return (tv.tv_sec + ((double)tv.tv_usec) / 1000000.);
86 }
87
childfunc(void * arg)88 void *childfunc(void *arg)
89 {
90 pthread_cond_t *cp = (pthread_cond_t *) arg;
91
92 while (child_waiting == 0) {
93 pthread_mutex_lock(&child_mutex);
94 child_waiting = 1;
95 if (pthread_cond_wait(cp, &child_mutex) != 0) {
96 perror("pthread_cond_wait");
97 exit(-1);
98 }
99 endtime = d_gettimeofday();
100 child_waiting = 2;
101 pthread_mutex_unlock(&child_mutex);
102 while (child_waiting == 2) {
103 poll(NULL, 0, 10);
104 }
105 }
106 pthread_exit(NULL);
107 }
108
test_signal(int broadcast_flag,int iter)109 void test_signal(int broadcast_flag, int iter)
110 {
111 pthread_attr_t attr;
112 pthread_t childid;
113 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
114 int i;
115 int prio;
116 struct sched_param schparm;
117 double starttime;
118
119 prio = sched_get_priority_max(SCHED_FIFO);
120 if (prio == -1) {
121 perror("sched_get_priority_max");
122 exit(-1);
123 }
124 schparm.sched_priority = prio;
125 if (sched_setscheduler(getpid(), SCHED_FIFO, &schparm) != 0) {
126 perror("sched_setscheduler");
127 exit(-1);
128 }
129
130 if (pthread_attr_init(&attr) != 0) {
131 perror("pthread_attr_init");
132 exit(-1);
133 }
134 if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0) {
135 perror("pthread_attr_setschedpolicy");
136 exit(-1);
137 }
138 if (pthread_attr_setschedparam(&attr, &schparm) != 0) {
139 perror("pthread_attr_setschedparam");
140 exit(-1);
141 }
142 if (pthread_create(&childid, &attr, childfunc, (void *)&cond) != 0) {
143 perror("pthread_create");
144 exit(-1);
145 }
146 for (i = 0; i < iter; i++) {
147 pthread_mutex_lock(&child_mutex);
148 child_waiting = 0;
149 while (child_waiting == 0) {
150 pthread_mutex_unlock(&child_mutex);
151 sched_yield();
152 pthread_mutex_lock(&child_mutex);
153 }
154 pthread_mutex_unlock(&child_mutex);
155 if (broadcast_flag) {
156 starttime = d_gettimeofday();
157 if (pthread_cond_broadcast(&cond) != 0) {
158 perror("pthread_cond_broadcast");
159 exit(-1);
160 }
161 } else {
162 starttime = d_gettimeofday();
163 if (pthread_cond_signal(&cond) != 0) {
164 perror("pthread_cond_signal");
165 exit(-1);
166 }
167 }
168 for (;;) {
169 pthread_mutex_lock(&child_mutex);
170 if (child_waiting == 2) {
171 break;
172 }
173 pthread_mutex_unlock(&child_mutex);
174 poll(NULL, 0, 10);
175 }
176 printf("%s() latency: %d microseconds\n",
177 (broadcast_flag
178 ? "pthread_cond_broadcast"
179 : "pthread_cond_signal"),
180 (int)((endtime - starttime) * 1000000.));
181 pthread_mutex_unlock(&child_mutex);
182 }
183 pthread_mutex_lock(&child_mutex);
184 child_waiting = 3;
185 pthread_mutex_unlock(&child_mutex);
186 if (pthread_join(childid, NULL) != 0) {
187 perror("pthread_join");
188 exit(-1);
189 }
190 }
191
main(int argc,char * argv[])192 int main(int argc, char *argv[])
193 {
194 struct sched_param sp;
195 long iter;
196 setup();
197
198 rt_init("h", parse_args, argc, argv);
199
200 sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
201 if (sp.sched_priority == -1) {
202 perror("sched_get_priority_max");
203 exit(-1);
204 }
205 if (sched_setscheduler(0, SCHED_FIFO, &sp) != 0) {
206 perror("sched_setscheduler");
207 exit(-1);
208 }
209
210 if (argc == 1) {
211 fprintf(stderr, "Usage: %s iterations [unicast]\n", argv[0]);
212 exit(-1);
213 }
214 iter = strtol(argv[1], NULL, 0);
215 test_signal(argc == 2, iter);
216
217 return 0;
218 }
219