File: sockperf.c

package info (click to toggle)
glibc 2.19-13
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 202,472 kB
  • ctags: 140,344
  • sloc: ansic: 969,274; asm: 241,208; sh: 10,047; makefile: 8,467; cpp: 3,595; perl: 2,077; pascal: 1,839; awk: 1,704; yacc: 317; sed: 73
file content (594 lines) | stat: -rw-r--r-- 11,888 bytes parent folder | download | duplicates (29)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#define _GNU_SOURCE
#include <argp.h>
#include <complex.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <gd.h>
#include <inttypes.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/un.h>


#define size_x 320
#define size_y 240


#define PATH "/tmp/s.sockperf"


struct thread_param
{
  unsigned int from;
  unsigned int to;
  unsigned int nserv;
};

struct coord
{
  unsigned int x;
  unsigned int y;
  complex double z;
};


/* We use 64bit values for the times.  */
typedef unsigned long long int hp_timing_t;


static unsigned int nclients = 2;
static unsigned int nservers = 2;

static bool timing;
static int points;


static complex double top_left = -0.7 + 0.2i;
static complex double bottom_right = -0.5 - 0.0i;


static int colors[256];
static gdImagePtr image;
static pthread_mutex_t image_lock;

static int sock;


static void *
client (void *arg)
{
  struct thread_param *param = arg;
  unsigned int cnt;
  unsigned int nserv = param->nserv;
  int clisock[nserv];
  struct pollfd servpoll[nserv];
  struct sockaddr_un servaddr;
  socklen_t servlen;
  struct coord c;

  bool new_coord (void)
    {
      if (cnt >= param->to)
	return false;

      unsigned int row = cnt / size_x;
      unsigned int col = cnt % size_x;

      c.x = col;
      c.y = row;
      c.z = (top_left
	     + ((col
		 * (creal (bottom_right) - creal (top_left))) / size_x)
	     + (_Complex_I * (row * (cimag (bottom_right) - cimag (top_left)))
		/ size_y));

      ++cnt;

      return true;
    }


  for (cnt = 0; cnt < nserv; ++cnt)
    {
      servpoll[cnt].fd = socket (AF_UNIX, SOCK_STREAM, 0);
      if (clisock < 0)
	{
	  puts ("cannot create socket in client");
	  return NULL;
	}

      memset (&servaddr, '\0', sizeof (servaddr));
      servaddr.sun_family = AF_UNIX;
      strncpy (servaddr.sun_path, PATH, sizeof (servaddr.sun_path));
      servlen = offsetof (struct sockaddr_un, sun_path) + strlen (PATH) + 1;


      int err;
      while (1)
	{
	  err = TEMP_FAILURE_RETRY (connect (servpoll[cnt].fd, &servaddr,
					     servlen));
	  if (err != -1 || errno != ECONNREFUSED)
	    break;

	  pthread_yield ();
	}

      if (err == -1)
	{
	  printf ("cannot connect: %m (%d)\n", errno);
	  exit (1);
	}

      servpoll[cnt].events = POLLOUT;
      servpoll[cnt].revents = 0;
    }

  cnt = param->from;

  new_coord ();
  bool z_valid = true;

  while (1)
    {
      int i;
      int n = poll (servpoll, nserv, -1);
      if (n == -1)
	{
	  puts ("poll returned error");
	  break;
	}

      bool cont = false;
      for (i = 0; i < nserv && n > 0; ++i)
	if (servpoll[i].revents != 0)
	  {
	    if (servpoll[i].revents == POLLIN)
	      {
		unsigned int vals[3];
		if (TEMP_FAILURE_RETRY (read (servpoll[i].fd, &vals,
					      sizeof (vals)))
		    != sizeof (vals))
		  {
		    puts ("read error in client");
		    return NULL;
		  }

		pthread_mutex_lock (&image_lock);

		gdImageSetPixel (image, vals[0], vals[1], vals[2]);
		++points;

		pthread_mutex_unlock (&image_lock);

		servpoll[i].events = POLLOUT;
	      }
	    else
	      {
		if (servpoll[i].revents != POLLOUT)
		  printf ("revents: %hd != POLLOUT ???\n",
			  servpoll[i].revents);

		if (z_valid)
		  {
		    if (TEMP_FAILURE_RETRY (write (servpoll[i].fd, &c,
						   sizeof (c))) != sizeof (c))
		      {
			puts ("write error in client");
			return NULL;
		      }
		    cont = true;
		    servpoll[i].events = POLLIN;

		    z_valid = new_coord ();
		    if (! z_valid)
		      /* No more to do.  Clear the event fields.  */
		      for (i = 0; i < nserv; ++i)
			if (servpoll[i].events == POLLOUT)
			  servpoll[i].events = servpoll[i].revents = 0;
		  }
		else
		  servpoll[i].events = servpoll[i].revents = 0;
	      }

	    --n;
	  }
	else if (servpoll[i].events != 0)
	  cont = true;

      if (! cont && ! z_valid)
	break;
    }

  c.x = 0xffffffff;
  c.y = 0xffffffff;
  for (cnt = 0; cnt < nserv; ++cnt)
    {
      TEMP_FAILURE_RETRY (write (servpoll[cnt].fd, &c, sizeof (c)));
      close (servpoll[cnt].fd);
    }

  return NULL;
}


static void *
server (void *arg)
{
  struct sockaddr_un cliaddr;
  socklen_t clilen;
  int clisock = TEMP_FAILURE_RETRY (accept (sock, &cliaddr, &clilen));

  if (clisock == -1)
    {
      puts ("accept failed");
      return NULL;
    }

  while (1)
    {
      struct coord c;

      if (TEMP_FAILURE_RETRY (read (clisock, &c, sizeof (c))) != sizeof (c))
	{
	  printf ("server read failed: %m (%d)\n", errno);
	  break;
	}

      if (c.x == 0xffffffff && c.y == 0xffffffff)
	break;

      unsigned int rnds = 0;
      complex double z = c.z;
      while (cabs (z) < 4.0)
	{
	  z = z * z - 1;
	  if (++rnds == 255)
	    break;
	}

      unsigned int vals[3] = { c.x, c.y, rnds };
      if (TEMP_FAILURE_RETRY (write (clisock, vals, sizeof (vals)))
	  != sizeof (vals))
	{
	  puts ("server write error");
	  return NULL;
	}
    }

  close (clisock);

  return NULL;
}


static const char *outfilename = "test.png";


static const struct argp_option options[] =
  {
    { "clients", 'c', "NUMBER", 0, "Number of client threads" },
    { "servers", 's', "NUMBER", 0, "Number of server threads per client" },
    { "timing", 'T', NULL, 0,
      "Measure time from startup to the last thread finishing" },
    { NULL, 0, NULL, 0, NULL }
  };

/* Prototype for option handler.  */
static error_t parse_opt (int key, char *arg, struct argp_state *state);

/* Data structure to communicate with argp functions.  */
static struct argp argp =
{
  options, parse_opt
};


int
main (int argc, char *argv[])
{
  int cnt;
  FILE *outfile;
  struct sockaddr_un servaddr;
  socklen_t servlen;
  int remaining;

  /* Parse and process arguments.  */
  argp_parse (&argp, argc, argv, 0, &remaining, NULL);


  pthread_t servth[nservers * nclients];
  pthread_t clntth[nclients];
  struct thread_param clntparam[nclients];


  image = gdImageCreate (size_x, size_y);
  if (image == NULL)
    {
      puts ("gdImageCreate failed");
      return 1;
    }

  for (cnt = 0; cnt < 255; ++cnt)
    colors[cnt] = gdImageColorAllocate (image, 256 - cnt, 256 - cnt,
					256 - cnt);
  /* Black.  */
  colors[cnt] = gdImageColorAllocate (image, 0, 0, 0);


  sock = socket (AF_UNIX, SOCK_STREAM, 0);
  if (sock < 0)
    error (EXIT_FAILURE, errno, "cannot create socket");

  memset (&servaddr, '\0', sizeof (servaddr));
  servaddr.sun_family = AF_UNIX;
  strncpy (servaddr.sun_path, PATH, sizeof (servaddr.sun_path));
  servlen = offsetof (struct sockaddr_un, sun_path) + strlen (PATH) + 1;

  if (bind (sock, &servaddr, servlen) == -1)
    error (EXIT_FAILURE, errno, "bind failed");

  listen (sock, SOMAXCONN);

  pthread_mutex_init (&image_lock, NULL);


  struct sigaction sa;
  sa.sa_handler = SIG_IGN;
  sigemptyset (&sa.sa_mask);
  sa.sa_flags = 0;

  clockid_t cl;
  struct timespec start_time;
  if (timing)
    {
      if (clock_getcpuclockid (0, &cl) != 0
	  || clock_gettime (cl, &start_time) != 0)
	timing = false;
    }

  /* Start the servers.  */
  for (cnt = 0; cnt < nservers * nclients; ++cnt)
    {
      if (pthread_create (&servth[cnt], NULL, server, NULL) != 0)
	{
	  puts ("pthread_create for server failed");
	  exit (1);
	}
    }

  for (cnt = 0; cnt < nclients; ++cnt)
    {
      clntparam[cnt].from = cnt * (size_x * size_y) / nclients;
      clntparam[cnt].to = MIN ((cnt + 1) * (size_x * size_y) / nclients,
			       size_x * size_y);
      clntparam[cnt].nserv = nservers;

      if (pthread_create (&clntth[cnt], NULL, client, &clntparam[cnt]) != 0)
	{
	  puts ("pthread_create for client failed");
	  exit (1);
	}
    }


  /* Wait for the clients.  */
  for (cnt = 0; cnt < nclients; ++cnt)
    if (pthread_join (clntth[cnt], NULL) != 0)
      {
	puts ("client pthread_join failed");
	exit (1);
      }

  /* Wait for the servers.  */
  for (cnt = 0; cnt < nclients * nservers; ++cnt)
    if (pthread_join (servth[cnt], NULL) != 0)
      {
	puts ("server pthread_join failed");
	exit (1);
      }


  if (timing)
    {
      struct timespec end_time;

      if (clock_gettime (cl, &end_time) == 0)
	{
	  end_time.tv_sec -= start_time.tv_sec;
	  end_time.tv_nsec -= start_time.tv_nsec;
	  if (end_time.tv_nsec < 0)
	    {
	      end_time.tv_nsec += 1000000000;
	      --end_time.tv_sec;
	    }

	  printf ("\nRuntime: %lu.%09lu seconds\n%d points computed\n",
		  (unsigned long int) end_time.tv_sec,
		  (unsigned long int) end_time.tv_nsec,
		  points);
	}
    }


  outfile = fopen (outfilename, "w");
  if (outfile == NULL)
    error (EXIT_FAILURE, errno, "cannot open output file '%s'", outfilename);

  gdImagePng (image, outfile);

  fclose (outfile);

  unlink (PATH);

  return 0;
}


/* Handle program arguments.  */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    case 'c':
      nclients = strtoul (arg, NULL, 0);
      break;

    case 's':
      nservers = strtoul (arg, NULL, 0);
      break;

    case 'T':
      timing = true;
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }

  return 0;
}


static hp_timing_t
get_clockfreq (void)
{
  /* We read the information from the /proc filesystem.  It contains at
     least one line like
	cpu MHz         : 497.840237
     or also
	cpu MHz         : 497.841
     We search for this line and convert the number in an integer.  */
  static hp_timing_t result;
  int fd;

  /* If this function was called before, we know the result.  */
  if (result != 0)
    return result;

  fd = open ("/proc/cpuinfo", O_RDONLY);
  if (__builtin_expect (fd != -1, 1))
    {
      /* XXX AFAIK the /proc filesystem can generate "files" only up
         to a size of 4096 bytes.  */
      char buf[4096];
      ssize_t n;

      n = read (fd, buf, sizeof buf);
      if (__builtin_expect (n, 1) > 0)
	{
	  char *mhz = memmem (buf, n, "cpu MHz", 7);

	  if (__builtin_expect (mhz != NULL, 1))
	    {
	      char *endp = buf + n;
	      int seen_decpoint = 0;
	      int ndigits = 0;

	      /* Search for the beginning of the string.  */
	      while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
		++mhz;

	      while (mhz < endp && *mhz != '\n')
		{
		  if (*mhz >= '0' && *mhz <= '9')
		    {
		      result *= 10;
		      result += *mhz - '0';
		      if (seen_decpoint)
			++ndigits;
		    }
		  else if (*mhz == '.')
		    seen_decpoint = 1;

		  ++mhz;
		}

	      /* Compensate for missing digits at the end.  */
	      while (ndigits++ < 6)
		result *= 10;
	    }
	}

      close (fd);
    }

  return result;
}


int
clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
{
  /* We don't allow any process ID but our own.  */
  if (pid != 0 && pid != getpid ())
    return EPERM;

#ifdef CLOCK_PROCESS_CPUTIME_ID
  /* Store the number.  */
  *clock_id = CLOCK_PROCESS_CPUTIME_ID;

  return 0;
#else
  /* We don't have a timer for that.  */
  return ENOENT;
#endif
}


#define HP_TIMING_NOW(Var)	__asm__ __volatile__ ("rdtsc" : "=A" (Var))

/* Get current value of CLOCK and store it in TP.  */
int
clock_gettime (clockid_t clock_id, struct timespec *tp)
{
  int retval = -1;

  switch (clock_id)
    {
    case CLOCK_PROCESS_CPUTIME_ID:
      {

	static hp_timing_t freq;
	hp_timing_t tsc;

	/* Get the current counter.  */
	HP_TIMING_NOW (tsc);

	if (freq == 0)
	  {
	    freq = get_clockfreq ();
	    if (freq == 0)
	      return EINVAL;
	  }

	/* Compute the seconds.  */
	tp->tv_sec = tsc / freq;

	/* And the nanoseconds.  This computation should be stable until
	   we get machines with about 16GHz frequency.  */
	tp->tv_nsec = ((tsc % freq) * UINT64_C (1000000000)) / freq;

	retval = 0;
      }
    break;

    default:
      errno = EINVAL;
      break;
    }

  return retval;
}