aboutsummaryrefslogtreecommitdiff
path: root/benchtests/bench-pthread-locks.c
blob: 5274246a4bd28f09e9b1b5bbb1d300566abec20f (plain)
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
/* Measure various lock acquisition times for empty critical sections.
   Copyright (C) 2020-2021 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#define TEST_MAIN
#define TEST_NAME "pthread-locks"

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdatomic.h>
#include <sys/time.h>
#include <math.h>
#include "bench-timing.h"
#include "json-lib.h"

/* The point of this benchmark is to measure the overhead of an empty
   critical section or a small critical section.  This is never going
   to be indicative of real application performance.  Instead we are
   trying to benchmark the effects of the compiler and the runtime
   coupled with a particular set of hardware atomic operations.
   The numbers from this benchmark should be taken with a massive gain
   of salt and viewed through the eyes of expert reviewers.  */

static pthread_mutex_t m;
static pthread_rwlock_t rw;
static pthread_cond_t cv;
static pthread_cond_t consumer_c, producer_c;
static int cv_done;
static pthread_spinlock_t sp;
static sem_t sem;

typedef timing_t (*test_t)(long, int);

#define START_ITERS 1000

#define FILLER_GOES_HERE \
  if (filler) \
    do_filler ();

/* Everyone loves a good fibonacci series.  This isn't quite one of
   them because we need larger values in fewer steps, in a way that
   won't be optimized away.  We're looking to approximately double the
   total time each test iteration takes, so as to not swamp the useful
   timings.  */

#pragma GCC push_options
#pragma GCC optimize(1)

static int __attribute__((noinline))
fibonacci (int i)
{
  asm("");
  if (i > 2)
    return fibonacci (i-1) + fibonacci (i-2);
  return 10+i;
}

static void
do_filler (void)
{
  static char buf1[512], buf2[512];
  int f = fibonacci (5);
  memcpy (buf1, buf2, f);
}

#pragma GCC pop_options

static timing_t
test_mutex (long iters, int filler)
{
  timing_t start, stop, cur;

  pthread_mutex_init (&m, NULL);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      pthread_mutex_lock (&m);
      FILLER_GOES_HERE;
      pthread_mutex_unlock (&m);
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  return cur;
}

static timing_t
test_mutex_trylock (long iters, int filler)
{
  timing_t start, stop, cur;

  pthread_mutex_init (&m, NULL);
  pthread_mutex_lock (&m);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      pthread_mutex_trylock (&m);
      FILLER_GOES_HERE;
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  pthread_mutex_unlock (&m);
  return cur;
}

static timing_t
test_rwlock_read (long iters, int filler)
{
  timing_t start, stop, cur;

  pthread_rwlock_init (&rw, NULL);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      pthread_rwlock_rdlock (&rw);
      FILLER_GOES_HERE;
      pthread_rwlock_unlock (&rw);
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  return cur;
}

static timing_t
test_rwlock_tryread (long iters, int filler)
{
  timing_t start, stop, cur;

  pthread_rwlock_init (&rw, NULL);
  pthread_rwlock_wrlock (&rw);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      pthread_rwlock_tryrdlock (&rw);
      FILLER_GOES_HERE;
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  pthread_rwlock_unlock (&rw);
  return cur;
}

static timing_t
test_rwlock_write (long iters, int filler)
{
  timing_t start, stop, cur;

  pthread_rwlock_init (&rw, NULL);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      pthread_rwlock_wrlock (&rw);
      FILLER_GOES_HERE;
      pthread_rwlock_unlock (&rw);
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  return cur;
}

static timing_t
test_rwlock_trywrite (long iters, int filler)
{
  timing_t start, stop, cur;

  pthread_rwlock_init (&rw, NULL);
  pthread_rwlock_rdlock (&rw);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      pthread_rwlock_trywrlock (&rw);
      FILLER_GOES_HERE;
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  pthread_rwlock_unlock (&rw);
  return cur;
}

static timing_t
test_spin_lock (long iters, int filler)
{
  timing_t start, stop, cur;

  pthread_spin_init (&sp, PTHREAD_PROCESS_PRIVATE);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      pthread_spin_lock (&sp);
      FILLER_GOES_HERE;
      pthread_spin_unlock (&sp);
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  return cur;
}

static timing_t
test_spin_trylock (long iters, int filler)
{
  timing_t start, stop, cur;

  pthread_spin_init (&sp, PTHREAD_PROCESS_PRIVATE);
  pthread_spin_lock (&sp);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      pthread_spin_trylock (&sp);
      FILLER_GOES_HERE;
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  pthread_spin_unlock (&sp);
  return cur;
}

static timing_t
test_sem_wait (long iters, int filler)
{
  timing_t start, stop, cur;

  sem_init (&sem, 0, 1);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      sem_post (&sem);
      FILLER_GOES_HERE;
      sem_wait (&sem);
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  return cur;
}

static timing_t
test_sem_trywait (long iters, int filler)
{
  timing_t start, stop, cur;

  sem_init (&sem, 0, 0);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      sem_trywait (&sem);
      FILLER_GOES_HERE;
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  return cur;
}

static void *
test_condvar_helper (void *v)
{
  /* This is wasteful, but the alternative is to add the overhead of a
     mutex lock/unlock to the overall iteration (both threads) and we
     don't want that.  Ideally, this thread would run on an
     independent processing core anyway.  The ONLY goal here is to
     minimize the time the other thread spends waiting for us.  */
  while (__atomic_load_n (&cv_done, __ATOMIC_RELAXED) == 0)
    pthread_cond_signal (&cv);

  return NULL;
}

static timing_t
test_condvar (long iters, int filler)
{
  timing_t start, stop, cur;
  pthread_t helper_id;

  pthread_mutex_init (&m, NULL);
  pthread_cond_init (&cv, NULL);
  pthread_mutex_lock (&m);

  __atomic_store_n (&cv_done, 0, __ATOMIC_RELAXED);
  pthread_create (&helper_id, NULL, test_condvar_helper, &iters);

  TIMING_NOW (start);
  for (long j = iters; j >= 0; --j)
    {
      pthread_cond_wait (&cv, &m);
      FILLER_GOES_HERE;
    }
  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);

  pthread_mutex_unlock (&m);
  __atomic_store_n (&cv_done, 1, __ATOMIC_RELAXED);

  pthread_join (helper_id, NULL);
  return cur;
}

/* How many items are "queued" in our pretend queue.  */
static int queued = 0;

typedef struct Producer_Params {
  long iters;
  int filler;
} Producer_Params;

/* We only benchmark the consumer thread, but both threads are doing
   essentially the same thing, and never run in parallel due to the
   locks.  Thus, even if they run on separate processing cores, we
   count the time for both threads.  */
static void *
test_producer_thread (void *v)
{
  Producer_Params *p = (Producer_Params *) v;
  long iters = p->iters;
  int filler = p->filler;
  long j;

  for (j = iters; j >= 0; --j)
    {
      /* Aquire lock on the queue.  */
      pthread_mutex_lock (&m);
      /* if something's already there, wait.  */
      while (queued > 0)
	pthread_cond_wait (&consumer_c, &m);

      /* Put something on the queue */
      FILLER_GOES_HERE;
      ++ queued;
      pthread_cond_signal (&producer_c);

      /* Give the other thread a chance to run.  */
      pthread_mutex_unlock (&m);
    }

  return NULL;
}

static timing_t
test_consumer_producer (long iters, int filler)
{
  timing_t start, stop, cur;
  pthread_t helper_id;
  Producer_Params p;

  p.iters = iters;
  p.filler = filler;

  pthread_mutex_init (&m, NULL);
  pthread_cond_init (&cv, NULL);

  pthread_create (&helper_id, NULL, test_producer_thread, &p);

  TIMING_NOW (start);

  for (long j = iters; j >= 0; --j)
    {
      /* Aquire lock on the queue.  */
      pthread_mutex_lock (&m);
      /* Wait for something to be on the queue.  */
      while (queued == 0)
	pthread_cond_wait (&producer_c, &m);

      /* Take if off. */
      FILLER_GOES_HERE;
      -- queued;
      pthread_cond_signal (&consumer_c);

      /* Give the other thread a chance to run.  */
      pthread_mutex_unlock (&m);
    }

  TIMING_NOW (stop);
  TIMING_DIFF (cur, start, stop);


  pthread_join (helper_id, NULL);
  return cur;
}

/* Number of runs we use for computing mean and standard deviation.
   We actually do two additional runs and discard the outliers.  */
#define RUN_COUNT 10

static int
do_bench_2 (const char *name, test_t func, int filler, json_ctx_t *js)
{
  timing_t cur;
  struct timeval ts, te;
  double tsd, ted, td;
  long iters, iters_limit;
  timing_t curs[RUN_COUNT + 2];
  int i, j;
  double mean, stdev;

  iters = START_ITERS;
  iters_limit = LONG_MAX / 100;

  while (1) {
    gettimeofday (&ts, NULL);
    cur = func(iters, filler);
    gettimeofday (&te, NULL);

    /* We want a test to take at least 0.01 seconds, and try
       increasingly larger iteration counts until it does.  This
       allows for approximately constant-time tests regardless of
       hardware speed, without the overhead of checking the time
       inside the test loop itself.  We stop at a million iterations
       as that should be precise enough.  Once we determine a suitable
       iteration count, we run the test multiple times to calculate
       mean and standard deviation.  */

    /* Note that this also primes the CPU cache and triggers faster
       MHz, we hope.  */
    tsd = ts.tv_sec + ts.tv_usec / 1000000.0;
    ted = te.tv_sec + te.tv_usec / 1000000.0;
    td = ted - tsd;
    if (td >= 0.01
	|| iters >= iters_limit
	|| iters >= 1000000)
      break;

    iters *= 10;
  }

  curs[0] = cur;
  for (i = 1; i < RUN_COUNT + 2; i ++)
    curs[i] = func(iters, filler);

  /* We sort the results so we can discard the fastest and slowest
     times as outliers.  In theory we should keep the fastest time,
     but IMHO this is more fair.  A simple bubble sort suffices.  */

  for (i = 0; i < RUN_COUNT + 1; i ++)
    for (j = i + 1; j < RUN_COUNT + 2; j ++)
      if (curs[i] > curs[j])
	{
	  timing_t temp = curs[i];
	  curs[i] = curs[j];
	  curs[j] = temp;
	}

  /* Now calculate mean and standard deviation, skipping the outliers.  */
  mean = 0.0;
  for (i = 1; i<RUN_COUNT + 1; i ++)
    mean += (double) curs[i] / (double) iters;
  mean /= RUN_COUNT;

  stdev = 0.0;
  for (i = 1; i < RUN_COUNT + 1; i ++)
    {
      double s = (double) curs[i] / (double) iters - mean;
      stdev += s * s;
    }
  stdev = sqrt (stdev / (RUN_COUNT - 1));

  char buf[128];
  snprintf (buf, sizeof buf, "%s-%s", name, filler ? "filler" : "empty");

  json_attr_object_begin (js, buf);

  json_attr_double (js, "duration", (double) cur);
  json_attr_double (js, "iterations", (double) iters);
  json_attr_double (js, "wall-sec", (double) td);
  json_attr_double (js, "mean", mean);
  json_attr_double (js, "stdev", stdev);
  json_attr_double (js, "min-outlier", (double) curs[0] / (double) iters);
  json_attr_double (js, "min", (double) curs[1] / (double) iters);
  json_attr_double (js, "max", (double) curs[RUN_COUNT] / (double) iters);
  json_attr_double (js, "max-outlier", (double) curs[RUN_COUNT + 1] / (double) iters);

  json_attr_object_end (js);

  return 0;
}

static int
do_bench_1 (const char *name, test_t func, json_ctx_t *js)
{
  int rv = 0;

  rv += do_bench_2 (name, func, 0, js);
  rv += do_bench_2 (name, func, 1, js);

  return rv;
}

int
do_bench (void)
{
  int rv = 0;
  json_ctx_t json_ctx;

  json_init (&json_ctx, 2, stdout);
  json_attr_object_begin (&json_ctx, "pthread_locks");

#define BENCH(n) rv += do_bench_1 (#n, test_##n, &json_ctx)

  BENCH (mutex);
  BENCH (mutex_trylock);
  BENCH (rwlock_read);
  BENCH (rwlock_tryread);
  BENCH (rwlock_write);
  BENCH (rwlock_trywrite);
  BENCH (spin_lock);
  BENCH (spin_trylock);
  BENCH (sem_wait);
  BENCH (sem_trywait);
  BENCH (condvar);
  BENCH (consumer_producer);

  json_attr_object_end (&json_ctx);

  return rv;
}


#define TEST_FUNCTION do_bench ()

#include "../test-skeleton.c"