about summary refs log tree commit diff
path: root/benchtests/bench-pthread-lock-base.c
blob: fac8a12b52dbe3895054390d3b1dae6117f03cfb (plain) (blame)
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
/* Measure lock functions for different threads and critical sections.
   Copyright (C) 2022 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 TIMEOUT (20 * 60)

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

static bench_lock_t lock;
static bench_lock_attr_t attr;
static pthread_barrier_t barrier;

#define START_ITERS 1000

#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)
{
  char buf1[512], buf2[512];
  int f = fibonacci (4);
  memcpy (buf1, buf2, f);
}

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

#pragma GCC pop_options

#define UNIT_WORK_CRT do_filler_shared ()
#define UNIT_WORK_NON_CRT do_filler ()

static inline void
critical_section (int length)
{
  for (int i = length; i >= 0; i--)
    UNIT_WORK_CRT;
}

static inline void
non_critical_section (int length)
{
  for (int i = length; i >= 0; i--)
    UNIT_WORK_NON_CRT;
}

typedef struct Worker_Params
{
  long iters;
  int crt_len;
  int non_crt_len;
  timing_t duration;
} Worker_Params;

static void *
worker (void *v)
{
  timing_t start, stop;
  Worker_Params *p = (Worker_Params *) v;
  long iters = p->iters;
  int crt_len = p->crt_len;
  int non_crt_len = p->non_crt_len;

  pthread_barrier_wait (&barrier);
  TIMING_NOW (start);
  while (iters--)
    {
      LOCK (&lock);
      critical_section (crt_len);
      UNLOCK (&lock);
      non_critical_section (non_crt_len);
    }
  TIMING_NOW (stop);

  TIMING_DIFF (p->duration, start, stop);
  return NULL;
}

static double
do_one_test (int num_threads, int crt_len, int non_crt_len, long iters)
{
  int i;
  timing_t mean;
  Worker_Params *p, params[num_threads];
  pthread_t threads[num_threads];

  LOCK_INIT (&lock, &attr);
  pthread_barrier_init (&barrier, NULL, num_threads);

  for (i = 0; i < num_threads; i++)
    {
      p = &params[i];
      p->iters = iters;
      p->crt_len = crt_len;
      p->non_crt_len = non_crt_len;
      pthread_create (&threads[i], NULL, worker, (void *) p);
    }
  for (i = 0; i < num_threads; i++)
    pthread_join (threads[i], NULL);

  LOCK_DESTROY (&lock);
  pthread_barrier_destroy (&barrier);

  mean = 0;
  for (i = 0; i < num_threads; i++)
    mean += params[i].duration;
  mean /= num_threads;
  return mean;
}

#define RUN_COUNT 10
#define MIN_TEST_SEC 0.01

static void
do_bench_one (const char *name, int num_threads, int crt_len, int non_crt_len,
	      json_ctx_t *js)
{
  timing_t cur;
  struct timeval ts, te;
  double tsd, ted, td;
  long iters, iters_limit, total_iters;
  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 = do_one_test (num_threads, crt_len, non_crt_len, iters);
      gettimeofday (&te, NULL);
      /* Make sure the test to run at least MIN_TEST_SEC.  */
      tsd = ts.tv_sec + ts.tv_usec / 1000000.0;
      ted = te.tv_sec + te.tv_usec / 1000000.0;
      td = ted - tsd;
      if (td >= MIN_TEST_SEC || iters >= iters_limit)
	break;

      iters *= 10;
    }

  curs[0] = cur;
  for (i = 1; i < RUN_COUNT + 2; i++)
    curs[i] = do_one_test (num_threads, crt_len, non_crt_len, iters);

  /* Sort the results so we can discard the fastest and slowest
     times as outliers.  */
  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;
	}

  /* Calculate mean and standard deviation.  */
  mean = 0.0;
  total_iters = iters * num_threads;
  for (i = 1; i < RUN_COUNT + 1; i++)
    mean += (double) curs[i] / (double) total_iters;
  mean /= RUN_COUNT;

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

  char buf[256];
  snprintf (buf, sizeof buf, "%s,non_crt_len=%d,crt_len=%d,threads=%d", name,
	    non_crt_len, crt_len, num_threads);

  json_attr_object_begin (js, buf);

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

  json_attr_object_end (js);
}

#define TH_CONF_MAX 10

int
do_bench (void)
{
  int rv = 0;
  json_ctx_t json_ctx;
  int i, j, k;
  int th_num, th_conf, nprocs;
  int threads[TH_CONF_MAX];
  int crt_lens[] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
  int non_crt_lens[] = { 1, 32, 128 };
  char name[128];

  json_init (&json_ctx, 2, stdout);
  json_attr_object_begin (&json_ctx, TEST_NAME);

  /* The thread config begins from 1, and increases by 2x until nprocs.
     We also wants to test over-saturation case (1.25*nprocs).  */
  nprocs = get_nprocs ();
  th_num = 1;
  for (th_conf = 0; th_conf < (TH_CONF_MAX - 2) && th_num < nprocs; th_conf++)
    {
      threads[th_conf] = th_num;
      th_num <<= 1;
    }
  threads[th_conf++] = nprocs;
  threads[th_conf++] = nprocs + nprocs / 4;

  LOCK_ATTR_INIT (&attr);
  snprintf (name, sizeof name, "type=adaptive");

  for (k = 0; k < (sizeof (non_crt_lens) / sizeof (int)); k++)
    {
      int non_crt_len = non_crt_lens[k];
      for (j = 0; j < (sizeof (crt_lens) / sizeof (int)); j++)
	{
	  int crt_len = crt_lens[j];
	  for (i = 0; i < th_conf; i++)
	    {
	      th_num = threads[i];
	      do_bench_one (name, th_num, crt_len, non_crt_len, &json_ctx);
	    }
	}
    }

  json_attr_object_end (&json_ctx);

  return rv;
}

#define TEST_FUNCTION do_bench ()

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