Remove trailing newline from date_fmt in Serbian locales [BZ #19581]
[glibc.git] / nptl / sockperf.c
blob2d4b872740d0f1ab55f74593798ebd6a3cf60a84
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>
24 #define size_x 320
25 #define size_y 240
28 #define PATH "/tmp/s.sockperf"
31 struct thread_param
33 unsigned int from;
34 unsigned int to;
35 unsigned int nserv;
38 struct coord
40 unsigned int x;
41 unsigned int y;
42 complex double z;
46 /* We use 64bit values for the times. */
47 typedef unsigned long long int hp_timing_t;
50 static unsigned int nclients = 2;
51 static unsigned int nservers = 2;
53 static bool timing;
54 static int points;
57 static complex double top_left = -0.7 + 0.2i;
58 static complex double bottom_right = -0.5 - 0.0i;
61 static int colors[256];
62 static gdImagePtr image;
63 static pthread_mutex_t image_lock;
65 static int sock;
68 static void *
69 client (void *arg)
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;
79 bool new_coord (void)
81 if (cnt >= param->to)
82 return false;
84 unsigned int row = cnt / size_x;
85 unsigned int col = cnt % size_x;
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));
95 ++cnt;
97 return true;
101 for (cnt = 0; cnt < nserv; ++cnt)
103 servpoll[cnt].fd = socket (AF_UNIX, SOCK_STREAM, 0);
104 if (servpoll[cnt].fd < 0)
106 puts ("cannot create socket in client");
107 return NULL;
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;
116 int err;
117 while (1)
119 err = TEMP_FAILURE_RETRY (connect (servpoll[cnt].fd, &servaddr,
120 servlen));
121 if (err != -1 || errno != ECONNREFUSED)
122 break;
124 pthread_yield ();
127 if (err == -1)
129 printf ("cannot connect: %m (%d)\n", errno);
130 exit (1);
133 servpoll[cnt].events = POLLOUT;
134 servpoll[cnt].revents = 0;
137 cnt = param->from;
139 new_coord ();
140 bool z_valid = true;
142 while (1)
144 int i;
145 int n = poll (servpoll, nserv, -1);
146 if (n == -1)
148 puts ("poll returned error");
149 break;
152 bool cont = false;
153 for (i = 0; i < nserv && n > 0; ++i)
154 if (servpoll[i].revents != 0)
156 if (servpoll[i].revents == POLLIN)
158 unsigned int vals[3];
159 if (TEMP_FAILURE_RETRY (read (servpoll[i].fd, &vals,
160 sizeof (vals)))
161 != sizeof (vals))
163 puts ("read error in client");
164 return NULL;
167 pthread_mutex_lock (&image_lock);
169 gdImageSetPixel (image, vals[0], vals[1], vals[2]);
170 ++points;
172 pthread_mutex_unlock (&image_lock);
174 servpoll[i].events = POLLOUT;
176 else
178 if (servpoll[i].revents != POLLOUT)
179 printf ("revents: %hd != POLLOUT ???\n",
180 servpoll[i].revents);
182 if (z_valid)
184 if (TEMP_FAILURE_RETRY (write (servpoll[i].fd, &c,
185 sizeof (c))) != sizeof (c))
187 puts ("write error in client");
188 return NULL;
190 cont = true;
191 servpoll[i].events = POLLIN;
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;
200 else
201 servpoll[i].events = servpoll[i].revents = 0;
204 --n;
206 else if (servpoll[i].events != 0)
207 cont = true;
209 if (! cont && ! z_valid)
210 break;
213 c.x = 0xffffffff;
214 c.y = 0xffffffff;
215 for (cnt = 0; cnt < nserv; ++cnt)
217 TEMP_FAILURE_RETRY (write (servpoll[cnt].fd, &c, sizeof (c)));
218 close (servpoll[cnt].fd);
221 return NULL;
225 static void *
226 server (void *arg)
228 struct sockaddr_un cliaddr;
229 socklen_t clilen;
230 int clisock = TEMP_FAILURE_RETRY (accept (sock, &cliaddr, &clilen));
232 if (clisock == -1)
234 puts ("accept failed");
235 return NULL;
238 while (1)
240 struct coord c;
242 if (TEMP_FAILURE_RETRY (read (clisock, &c, sizeof (c))) != sizeof (c))
244 printf ("server read failed: %m (%d)\n", errno);
245 break;
248 if (c.x == 0xffffffff && c.y == 0xffffffff)
249 break;
251 unsigned int rnds = 0;
252 complex double z = c.z;
253 while (cabs (z) < 4.0)
255 z = z * z - 1;
256 if (++rnds == 255)
257 break;
260 unsigned int vals[3] = { c.x, c.y, rnds };
261 if (TEMP_FAILURE_RETRY (write (clisock, vals, sizeof (vals)))
262 != sizeof (vals))
264 puts ("server write error");
265 return NULL;
269 close (clisock);
271 return NULL;
275 static const char *outfilename = "test.png";
278 static const struct argp_option options[] =
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 }
287 /* Prototype for option handler. */
288 static error_t parse_opt (int key, char *arg, struct argp_state *state);
290 /* Data structure to communicate with argp functions. */
291 static struct argp argp =
293 options, parse_opt
298 main (int argc, char *argv[])
300 int cnt;
301 FILE *outfile;
302 struct sockaddr_un servaddr;
303 socklen_t servlen;
304 int remaining;
306 /* Parse and process arguments. */
307 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
310 pthread_t servth[nservers * nclients];
311 pthread_t clntth[nclients];
312 struct thread_param clntparam[nclients];
315 image = gdImageCreate (size_x, size_y);
316 if (image == NULL)
318 puts ("gdImageCreate failed");
319 return 1;
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);
329 sock = socket (AF_UNIX, SOCK_STREAM, 0);
330 if (sock < 0)
331 error (EXIT_FAILURE, errno, "cannot create socket");
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;
338 if (bind (sock, &servaddr, servlen) == -1)
339 error (EXIT_FAILURE, errno, "bind failed");
341 listen (sock, SOMAXCONN);
343 pthread_mutex_init (&image_lock, NULL);
346 struct sigaction sa;
347 sa.sa_handler = SIG_IGN;
348 sigemptyset (&sa.sa_mask);
349 sa.sa_flags = 0;
351 clockid_t cl;
352 struct timespec start_time;
353 if (timing)
355 if (clock_getcpuclockid (0, &cl) != 0
356 || clock_gettime (cl, &start_time) != 0)
357 timing = false;
360 /* Start the servers. */
361 for (cnt = 0; cnt < nservers * nclients; ++cnt)
363 if (pthread_create (&servth[cnt], NULL, server, NULL) != 0)
365 puts ("pthread_create for server failed");
366 exit (1);
370 for (cnt = 0; cnt < nclients; ++cnt)
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;
377 if (pthread_create (&clntth[cnt], NULL, client, &clntparam[cnt]) != 0)
379 puts ("pthread_create for client failed");
380 exit (1);
385 /* Wait for the clients. */
386 for (cnt = 0; cnt < nclients; ++cnt)
387 if (pthread_join (clntth[cnt], NULL) != 0)
389 puts ("client pthread_join failed");
390 exit (1);
393 /* Wait for the servers. */
394 for (cnt = 0; cnt < nclients * nservers; ++cnt)
395 if (pthread_join (servth[cnt], NULL) != 0)
397 puts ("server pthread_join failed");
398 exit (1);
402 if (timing)
404 struct timespec end_time;
406 if (clock_gettime (cl, &end_time) == 0)
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)
412 end_time.tv_nsec += 1000000000;
413 --end_time.tv_sec;
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);
424 outfile = fopen (outfilename, "w");
425 if (outfile == NULL)
426 error (EXIT_FAILURE, errno, "cannot open output file '%s'", outfilename);
428 gdImagePng (image, outfile);
430 fclose (outfile);
432 unlink (PATH);
434 return 0;
438 /* Handle program arguments. */
439 static error_t
440 parse_opt (int key, char *arg, struct argp_state *state)
442 switch (key)
444 case 'c':
445 nclients = strtoul (arg, NULL, 0);
446 break;
448 case 's':
449 nservers = strtoul (arg, NULL, 0);
450 break;
452 case 'T':
453 timing = true;
454 break;
456 default:
457 return ARGP_ERR_UNKNOWN;
460 return 0;
464 static hp_timing_t
465 get_clockfreq (void)
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;
476 /* If this function was called before, we know the result. */
477 if (result != 0)
478 return result;
480 fd = open ("/proc/cpuinfo", O_RDONLY);
481 if (__glibc_likely (fd != -1))
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;
488 n = read (fd, buf, sizeof buf);
489 if (__builtin_expect (n, 1) > 0)
491 char *mhz = memmem (buf, n, "cpu MHz", 7);
493 if (__glibc_likely (mhz != NULL))
495 char *endp = buf + n;
496 int seen_decpoint = 0;
497 int ndigits = 0;
499 /* Search for the beginning of the string. */
500 while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
501 ++mhz;
503 while (mhz < endp && *mhz != '\n')
505 if (*mhz >= '0' && *mhz <= '9')
507 result *= 10;
508 result += *mhz - '0';
509 if (seen_decpoint)
510 ++ndigits;
512 else if (*mhz == '.')
513 seen_decpoint = 1;
515 ++mhz;
518 /* Compensate for missing digits at the end. */
519 while (ndigits++ < 6)
520 result *= 10;
524 close (fd);
527 return result;
532 clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
534 /* We don't allow any process ID but our own. */
535 if (pid != 0 && pid != getpid ())
536 return EPERM;
538 #ifdef CLOCK_PROCESS_CPUTIME_ID
539 /* Store the number. */
540 *clock_id = CLOCK_PROCESS_CPUTIME_ID;
542 return 0;
543 #else
544 /* We don't have a timer for that. */
545 return ENOENT;
546 #endif
550 #define HP_TIMING_NOW(Var) __asm__ __volatile__ ("rdtsc" : "=A" (Var))
552 /* Get current value of CLOCK and store it in TP. */
554 clock_gettime (clockid_t clock_id, struct timespec *tp)
556 int retval = -1;
558 switch (clock_id)
560 case CLOCK_PROCESS_CPUTIME_ID:
563 static hp_timing_t freq;
564 hp_timing_t tsc;
566 /* Get the current counter. */
567 HP_TIMING_NOW (tsc);
569 if (freq == 0)
571 freq = get_clockfreq ();
572 if (freq == 0)
573 return EINVAL;
576 /* Compute the seconds. */
577 tp->tv_sec = tsc / freq;
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;
583 retval = 0;
585 break;
587 default:
588 errno = EINVAL;
589 break;
592 return retval;