]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/sockperf.c
Consolidate non cancellable nanosleep call
[thirdparty/glibc.git] / nptl / sockperf.c
1 #define _GNU_SOURCE
2 #include <argp.h>
3 #include <complex.h>
4 #include <errno.h>
5 #include <error.h>
6 #include <fcntl.h>
7 #include <gd.h>
8 #include <inttypes.h>
9 #include <pthread.h>
10 #include <signal.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 #include <unistd.h>
18 #include <sys/param.h>
19 #include <sys/poll.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22
23
24 #define size_x 320
25 #define size_y 240
26
27
28 #define PATH "/tmp/s.sockperf"
29
30
31 struct thread_param
32 {
33 unsigned int from;
34 unsigned int to;
35 unsigned int nserv;
36 };
37
38 struct coord
39 {
40 unsigned int x;
41 unsigned int y;
42 complex double z;
43 };
44
45
46 /* We use 64bit values for the times. */
47 typedef unsigned long long int hp_timing_t;
48
49
50 static unsigned int nclients = 2;
51 static unsigned int nservers = 2;
52
53 static bool timing;
54 static int points;
55
56
57 static complex double top_left = -0.7 + 0.2i;
58 static complex double bottom_right = -0.5 - 0.0i;
59
60
61 static int colors[256];
62 static gdImagePtr image;
63 static pthread_mutex_t image_lock;
64
65 static int sock;
66
67
68 static void *
69 client (void *arg)
70 {
71 struct thread_param *param = arg;
72 unsigned int cnt;
73 unsigned int nserv = param->nserv;
74 struct pollfd servpoll[nserv];
75 struct sockaddr_un servaddr;
76 socklen_t servlen;
77 struct coord c;
78
79 bool new_coord (void)
80 {
81 if (cnt >= param->to)
82 return false;
83
84 unsigned int row = cnt / size_x;
85 unsigned int col = cnt % size_x;
86
87 c.x = col;
88 c.y = row;
89 c.z = (top_left
90 + ((col
91 * (creal (bottom_right) - creal (top_left))) / size_x)
92 + (_Complex_I * (row * (cimag (bottom_right) - cimag (top_left)))
93 / size_y));
94
95 ++cnt;
96
97 return true;
98 }
99
100
101 for (cnt = 0; cnt < nserv; ++cnt)
102 {
103 servpoll[cnt].fd = socket (AF_UNIX, SOCK_STREAM, 0);
104 if (servpoll[cnt].fd < 0)
105 {
106 puts ("cannot create socket in client");
107 return NULL;
108 }
109
110 memset (&servaddr, '\0', sizeof (servaddr));
111 servaddr.sun_family = AF_UNIX;
112 strncpy (servaddr.sun_path, PATH, sizeof (servaddr.sun_path));
113 servlen = offsetof (struct sockaddr_un, sun_path) + strlen (PATH) + 1;
114
115
116 int err;
117 while (1)
118 {
119 err = TEMP_FAILURE_RETRY (connect (servpoll[cnt].fd, &servaddr,
120 servlen));
121 if (err != -1 || errno != ECONNREFUSED)
122 break;
123
124 pthread_yield ();
125 }
126
127 if (err == -1)
128 {
129 printf ("cannot connect: %m (%d)\n", errno);
130 exit (1);
131 }
132
133 servpoll[cnt].events = POLLOUT;
134 servpoll[cnt].revents = 0;
135 }
136
137 cnt = param->from;
138
139 new_coord ();
140 bool z_valid = true;
141
142 while (1)
143 {
144 int i;
145 int n = poll (servpoll, nserv, -1);
146 if (n == -1)
147 {
148 puts ("poll returned error");
149 break;
150 }
151
152 bool cont = false;
153 for (i = 0; i < nserv && n > 0; ++i)
154 if (servpoll[i].revents != 0)
155 {
156 if (servpoll[i].revents == POLLIN)
157 {
158 unsigned int vals[3];
159 if (TEMP_FAILURE_RETRY (read (servpoll[i].fd, &vals,
160 sizeof (vals)))
161 != sizeof (vals))
162 {
163 puts ("read error in client");
164 return NULL;
165 }
166
167 pthread_mutex_lock (&image_lock);
168
169 gdImageSetPixel (image, vals[0], vals[1], vals[2]);
170 ++points;
171
172 pthread_mutex_unlock (&image_lock);
173
174 servpoll[i].events = POLLOUT;
175 }
176 else
177 {
178 if (servpoll[i].revents != POLLOUT)
179 printf ("revents: %hd != POLLOUT ???\n",
180 servpoll[i].revents);
181
182 if (z_valid)
183 {
184 if (TEMP_FAILURE_RETRY (write (servpoll[i].fd, &c,
185 sizeof (c))) != sizeof (c))
186 {
187 puts ("write error in client");
188 return NULL;
189 }
190 cont = true;
191 servpoll[i].events = POLLIN;
192
193 z_valid = new_coord ();
194 if (! z_valid)
195 /* No more to do. Clear the event fields. */
196 for (i = 0; i < nserv; ++i)
197 if (servpoll[i].events == POLLOUT)
198 servpoll[i].events = servpoll[i].revents = 0;
199 }
200 else
201 servpoll[i].events = servpoll[i].revents = 0;
202 }
203
204 --n;
205 }
206 else if (servpoll[i].events != 0)
207 cont = true;
208
209 if (! cont && ! z_valid)
210 break;
211 }
212
213 c.x = 0xffffffff;
214 c.y = 0xffffffff;
215 for (cnt = 0; cnt < nserv; ++cnt)
216 {
217 TEMP_FAILURE_RETRY (write (servpoll[cnt].fd, &c, sizeof (c)));
218 close (servpoll[cnt].fd);
219 }
220
221 return NULL;
222 }
223
224
225 static void *
226 server (void *arg)
227 {
228 struct sockaddr_un cliaddr;
229 socklen_t clilen;
230 int clisock = TEMP_FAILURE_RETRY (accept (sock, &cliaddr, &clilen));
231
232 if (clisock == -1)
233 {
234 puts ("accept failed");
235 return NULL;
236 }
237
238 while (1)
239 {
240 struct coord c;
241
242 if (TEMP_FAILURE_RETRY (read (clisock, &c, sizeof (c))) != sizeof (c))
243 {
244 printf ("server read failed: %m (%d)\n", errno);
245 break;
246 }
247
248 if (c.x == 0xffffffff && c.y == 0xffffffff)
249 break;
250
251 unsigned int rnds = 0;
252 complex double z = c.z;
253 while (cabs (z) < 4.0)
254 {
255 z = z * z - 1;
256 if (++rnds == 255)
257 break;
258 }
259
260 unsigned int vals[3] = { c.x, c.y, rnds };
261 if (TEMP_FAILURE_RETRY (write (clisock, vals, sizeof (vals)))
262 != sizeof (vals))
263 {
264 puts ("server write error");
265 return NULL;
266 }
267 }
268
269 close (clisock);
270
271 return NULL;
272 }
273
274
275 static const char *outfilename = "test.png";
276
277
278 static const struct argp_option options[] =
279 {
280 { "clients", 'c', "NUMBER", 0, "Number of client threads" },
281 { "servers", 's', "NUMBER", 0, "Number of server threads per client" },
282 { "timing", 'T', NULL, 0,
283 "Measure time from startup to the last thread finishing" },
284 { NULL, 0, NULL, 0, NULL }
285 };
286
287 /* Prototype for option handler. */
288 static error_t parse_opt (int key, char *arg, struct argp_state *state);
289
290 /* Data structure to communicate with argp functions. */
291 static struct argp argp =
292 {
293 options, parse_opt
294 };
295
296
297 int
298 main (int argc, char *argv[])
299 {
300 int cnt;
301 FILE *outfile;
302 struct sockaddr_un servaddr;
303 socklen_t servlen;
304 int remaining;
305
306 /* Parse and process arguments. */
307 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
308
309
310 pthread_t servth[nservers * nclients];
311 pthread_t clntth[nclients];
312 struct thread_param clntparam[nclients];
313
314
315 image = gdImageCreate (size_x, size_y);
316 if (image == NULL)
317 {
318 puts ("gdImageCreate failed");
319 return 1;
320 }
321
322 for (cnt = 0; cnt < 255; ++cnt)
323 colors[cnt] = gdImageColorAllocate (image, 256 - cnt, 256 - cnt,
324 256 - cnt);
325 /* Black. */
326 colors[cnt] = gdImageColorAllocate (image, 0, 0, 0);
327
328
329 sock = socket (AF_UNIX, SOCK_STREAM, 0);
330 if (sock < 0)
331 error (EXIT_FAILURE, errno, "cannot create socket");
332
333 memset (&servaddr, '\0', sizeof (servaddr));
334 servaddr.sun_family = AF_UNIX;
335 strncpy (servaddr.sun_path, PATH, sizeof (servaddr.sun_path));
336 servlen = offsetof (struct sockaddr_un, sun_path) + strlen (PATH) + 1;
337
338 if (bind (sock, &servaddr, servlen) == -1)
339 error (EXIT_FAILURE, errno, "bind failed");
340
341 listen (sock, SOMAXCONN);
342
343 pthread_mutex_init (&image_lock, NULL);
344
345
346 struct sigaction sa;
347 sa.sa_handler = SIG_IGN;
348 sigemptyset (&sa.sa_mask);
349 sa.sa_flags = 0;
350
351 clockid_t cl;
352 struct timespec start_time;
353 if (timing)
354 {
355 if (clock_getcpuclockid (0, &cl) != 0
356 || clock_gettime (cl, &start_time) != 0)
357 timing = false;
358 }
359
360 /* Start the servers. */
361 for (cnt = 0; cnt < nservers * nclients; ++cnt)
362 {
363 if (pthread_create (&servth[cnt], NULL, server, NULL) != 0)
364 {
365 puts ("pthread_create for server failed");
366 exit (1);
367 }
368 }
369
370 for (cnt = 0; cnt < nclients; ++cnt)
371 {
372 clntparam[cnt].from = cnt * (size_x * size_y) / nclients;
373 clntparam[cnt].to = MIN ((cnt + 1) * (size_x * size_y) / nclients,
374 size_x * size_y);
375 clntparam[cnt].nserv = nservers;
376
377 if (pthread_create (&clntth[cnt], NULL, client, &clntparam[cnt]) != 0)
378 {
379 puts ("pthread_create for client failed");
380 exit (1);
381 }
382 }
383
384
385 /* Wait for the clients. */
386 for (cnt = 0; cnt < nclients; ++cnt)
387 if (pthread_join (clntth[cnt], NULL) != 0)
388 {
389 puts ("client pthread_join failed");
390 exit (1);
391 }
392
393 /* Wait for the servers. */
394 for (cnt = 0; cnt < nclients * nservers; ++cnt)
395 if (pthread_join (servth[cnt], NULL) != 0)
396 {
397 puts ("server pthread_join failed");
398 exit (1);
399 }
400
401
402 if (timing)
403 {
404 struct timespec end_time;
405
406 if (clock_gettime (cl, &end_time) == 0)
407 {
408 end_time.tv_sec -= start_time.tv_sec;
409 end_time.tv_nsec -= start_time.tv_nsec;
410 if (end_time.tv_nsec < 0)
411 {
412 end_time.tv_nsec += 1000000000;
413 --end_time.tv_sec;
414 }
415
416 printf ("\nRuntime: %lu.%09lu seconds\n%d points computed\n",
417 (unsigned long int) end_time.tv_sec,
418 (unsigned long int) end_time.tv_nsec,
419 points);
420 }
421 }
422
423
424 outfile = fopen (outfilename, "w");
425 if (outfile == NULL)
426 error (EXIT_FAILURE, errno, "cannot open output file '%s'", outfilename);
427
428 gdImagePng (image, outfile);
429
430 fclose (outfile);
431
432 unlink (PATH);
433
434 return 0;
435 }
436
437
438 /* Handle program arguments. */
439 static error_t
440 parse_opt (int key, char *arg, struct argp_state *state)
441 {
442 switch (key)
443 {
444 case 'c':
445 nclients = strtoul (arg, NULL, 0);
446 break;
447
448 case 's':
449 nservers = strtoul (arg, NULL, 0);
450 break;
451
452 case 'T':
453 timing = true;
454 break;
455
456 default:
457 return ARGP_ERR_UNKNOWN;
458 }
459
460 return 0;
461 }
462
463
464 static hp_timing_t
465 get_clockfreq (void)
466 {
467 /* We read the information from the /proc filesystem. It contains at
468 least one line like
469 cpu MHz : 497.840237
470 or also
471 cpu MHz : 497.841
472 We search for this line and convert the number in an integer. */
473 static hp_timing_t result;
474 int fd;
475
476 /* If this function was called before, we know the result. */
477 if (result != 0)
478 return result;
479
480 fd = open ("/proc/cpuinfo", O_RDONLY);
481 if (__glibc_likely (fd != -1))
482 {
483 /* XXX AFAIK the /proc filesystem can generate "files" only up
484 to a size of 4096 bytes. */
485 char buf[4096];
486 ssize_t n;
487
488 n = read (fd, buf, sizeof buf);
489 if (__builtin_expect (n, 1) > 0)
490 {
491 char *mhz = memmem (buf, n, "cpu MHz", 7);
492
493 if (__glibc_likely (mhz != NULL))
494 {
495 char *endp = buf + n;
496 int seen_decpoint = 0;
497 int ndigits = 0;
498
499 /* Search for the beginning of the string. */
500 while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
501 ++mhz;
502
503 while (mhz < endp && *mhz != '\n')
504 {
505 if (*mhz >= '0' && *mhz <= '9')
506 {
507 result *= 10;
508 result += *mhz - '0';
509 if (seen_decpoint)
510 ++ndigits;
511 }
512 else if (*mhz == '.')
513 seen_decpoint = 1;
514
515 ++mhz;
516 }
517
518 /* Compensate for missing digits at the end. */
519 while (ndigits++ < 6)
520 result *= 10;
521 }
522 }
523
524 close (fd);
525 }
526
527 return result;
528 }
529
530
531 int
532 clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
533 {
534 /* We don't allow any process ID but our own. */
535 if (pid != 0 && pid != getpid ())
536 return EPERM;
537
538 #ifdef CLOCK_PROCESS_CPUTIME_ID
539 /* Store the number. */
540 *clock_id = CLOCK_PROCESS_CPUTIME_ID;
541
542 return 0;
543 #else
544 /* We don't have a timer for that. */
545 return ENOENT;
546 #endif
547 }
548
549
550 #define HP_TIMING_NOW(Var) __asm__ __volatile__ ("rdtsc" : "=A" (Var))
551
552 /* Get current value of CLOCK and store it in TP. */
553 int
554 clock_gettime (clockid_t clock_id, struct timespec *tp)
555 {
556 int retval = -1;
557
558 switch (clock_id)
559 {
560 case CLOCK_PROCESS_CPUTIME_ID:
561 {
562
563 static hp_timing_t freq;
564 hp_timing_t tsc;
565
566 /* Get the current counter. */
567 HP_TIMING_NOW (tsc);
568
569 if (freq == 0)
570 {
571 freq = get_clockfreq ();
572 if (freq == 0)
573 return EINVAL;
574 }
575
576 /* Compute the seconds. */
577 tp->tv_sec = tsc / freq;
578
579 /* And the nanoseconds. This computation should be stable until
580 we get machines with about 16GHz frequency. */
581 tp->tv_nsec = ((tsc % freq) * UINT64_C (1000000000)) / freq;
582
583 retval = 0;
584 }
585 break;
586
587 default:
588 errno = EINVAL;
589 break;
590 }
591
592 return retval;
593 }