-
Notifications
You must be signed in to change notification settings - Fork 0
/
sembench.c
611 lines (543 loc) · 12.1 KB
/
sembench.c
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
* copyright Oracle 2007. Licensed under GPLv2
* To compile: gcc -Wall -o sembench sembench.c -lpthread
*
* usage: sembench -t thread count -w wakenum -r runtime -o op
* op can be: 0 (ipc sem) 1 (nanosleep) 2 (futexes)
*
* example:
* sembench -t 1024 -w 512 -r 60 -o 2
* runs 1024 threads, waking up 512 at a time, running for 60 seconds using
* futex locking.
*
*/
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 199309
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <errno.h>
#include <math.h>
#include <linux/futex.h>
#define VERSION "0.3"
#define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \
syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3)
/**
* futex_wait() - block on uaddr with optional timeout
* @timeout: relative timeout
*/
static inline int
futex_wait(u_int32_t *uaddr, u_int32_t val, struct timespec *timeout, int opflags)
{
return futex(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags);
}
/**
* futex_wake() - wake one or more tasks blocked on uaddr
* @nr_wake: wake up to this many tasks
*/
static inline int
futex_wake(u_int32_t *uaddr, int nr_wake, int opflags)
{
return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags);
}
static void smp_mb(void)
{
__sync_synchronize();
}
static int all_done = 0;
static int timeout_test = 0;
#define SEMS_PERID 250
struct sem_operations;
struct lockinfo {
unsigned long id;
unsigned long index;
int data;
pthread_t tid;
struct lockinfo *next;
struct sem_operations *ops;
unsigned long ready;
};
struct sem_wakeup_info {
int wakeup_count;
struct sembuf sb[SEMS_PERID];
};
struct sem_operations {
void (*wait)(struct lockinfo *l);
int (*wake)(struct sem_wakeup_info *wi, int num_semids, int num);
void (*setup)(struct sem_wakeup_info **wi, int num_semids);
void (*cleanup)(int num_semids);
char *name;
};
int *semid_lookup = NULL;
struct stats
{
double n, mean, M2;
};
static void update_stats(struct stats *stats, long long val)
{
double delta;
stats->n++;
delta = val - stats->mean;
stats->mean += delta / stats->n;
stats->M2 += delta*(val - stats->mean);
}
static double avg_stats(struct stats *stats)
{
return stats->mean;
}
/*
* http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
*
* (\Sum n_i^2) - ((\Sum n_i)^2)/n
* s^2 = -------------------------------
* n - 1
*
* http://en.wikipedia.org/wiki/Stddev
*
* The std dev of the mean is related to the std dev by:
*
* s
* s_mean = -------
* sqrt(n)
*
*/
static double stddev_stats(struct stats *stats)
{
double variance = stats->M2 / (stats->n - 1);
double variance_mean = variance / stats->n;
return sqrt(variance_mean);
}
pthread_mutex_t worklist_mutex = PTHREAD_MUTEX_INITIALIZER;
static unsigned long total_burns = 0;
static unsigned long min_burns = ~0UL;
static unsigned long max_burns = 0;
static struct stats burn_stats;
/* currently running threads */
static int thread_count = 0;
struct lockinfo *worklist = NULL;
static int workers_started = 0;
/* total threads started */
static int num_threads = 2048;
static void worklist_add(struct lockinfo *l)
{
smp_mb();
l->ready = 1;
}
static struct lockinfo *worklist_rm(void)
{
static int last_index = 0;
int i;
struct lockinfo *l;
for (i = 0; i < num_threads; i++) {
int test = (last_index + i) % num_threads;
l = worklist + test;
smp_mb();
if (l->ready) {
l->ready = 0;
last_index = test;
return l;
}
}
return NULL;
}
/* ipc semaphore post& wait */
void wait_ipc_sem(struct lockinfo *l)
{
struct sembuf sb;
int ret;
struct timespec *tvp = NULL;
struct timespec tv = { 0, 1 };
sb.sem_num = l->index;
sb.sem_flg = 0;
sb.sem_op = -1;
l->data = 1;
if (timeout_test && (l->id % 5) == 0)
tvp = &tv;
worklist_add(l);
ret = semtimedop(semid_lookup[l->id], &sb, 1, tvp);
while(l->data != 0 && tvp) {
struct timespec tv2 = { 0, 500 };
nanosleep(&tv2, NULL);
}
if (l->data != 0) {
if (tvp)
return;
fprintf(stderr, "wakeup without data update\n");
exit(1);
}
if (ret) {
if (errno == EAGAIN && tvp)
return;
perror("semtimed op");
exit(1);
}
}
int ipc_wake_some(struct sem_wakeup_info *wi, int num_semids, int num)
{
int i;
int ret;
struct lockinfo *l;
int found = 0;
for (i = 0; i < num_semids; i++) {
wi[i].wakeup_count = 0;
}
while(num > 0) {
struct sembuf *sb;
l = worklist_rm();
if (!l)
break;
if (l->data != 1)
fprintf(stderr, "warning, lockinfo data was %d\n",
l->data);
l->data = 0;
sb = wi[l->id].sb + wi[l->id].wakeup_count;
sb->sem_num = l->index;
sb->sem_op = 1;
sb->sem_flg = IPC_NOWAIT;
wi[l->id].wakeup_count++;
found++;
num--;
}
if (!found)
return 0;
for (i = 0; i < num_semids; i++) {
int wakeup_total;
int cur;
int offset = 0;
if (!wi[i].wakeup_count)
continue;
wakeup_total = wi[i].wakeup_count;
while(wakeup_total > 0) {
cur = wakeup_total > 64 ? 64 : wakeup_total;
ret = semtimedop(semid_lookup[i], wi[i].sb + offset,
cur, NULL);
if (ret) {
perror("semtimedop");
exit(1);
}
offset += cur;
wakeup_total -= cur;
}
}
return found;
}
void setup_ipc_sems(struct sem_wakeup_info **wi, int num_semids)
{
int i;
*wi = malloc(sizeof(**wi) * num_semids);
semid_lookup = malloc(num_semids * sizeof(int));
for(i = 0; i < num_semids; i++) {
semid_lookup[i] = semget(IPC_PRIVATE, SEMS_PERID,
IPC_CREAT | 0777);
if (semid_lookup[i] < 0) {
perror("semget");
exit(1);
}
}
sleep(10);
}
void cleanup_ipc_sems(int num)
{
int i;
for (i = 0; i < num; i++) {
semctl(semid_lookup[i], 0, IPC_RMID);
}
}
struct sem_operations ipc_sem_ops = {
.wait = wait_ipc_sem,
.wake = ipc_wake_some,
.setup = setup_ipc_sems,
.cleanup = cleanup_ipc_sems,
.name = "ipc sem operations",
};
/* futex post & wait */
void wait_futex_sem(struct lockinfo *l)
{
int ret;
l->data = 1;
worklist_add(l);
while(l->data == 1) {
ret = futex_wait(&l->data, 1, NULL, 0);
if (ret && ret == EWOULDBLOCK) {
perror("futex wait");
exit(1);
}
}
}
int futex_wake_some(struct sem_wakeup_info *wi, int num_semids, int num)
{
int i;
int ret;
struct lockinfo *l;
int found = 0;
for (i = 0; i < num; i++) {
l = worklist_rm();
if (!l)
break;
if (l->data != 1)
fprintf(stderr, "warning, lockinfo data was %d\n",
l->data);
l->data = 0;
ret = futex_wake(&l->data, 1, 0);
if (ret < 0) {
perror("futex wake");
exit(1);
}
found++;
}
return found;
}
void setup_futex_sems(struct sem_wakeup_info **wi, int num_semids)
{
return;
}
void cleanup_futex_sems(int num)
{
return;
}
struct sem_operations futex_sem_ops = {
.wait = wait_futex_sem,
.wake = futex_wake_some,
.setup = setup_futex_sems,
.cleanup = cleanup_futex_sems,
.name = "futex sem operations",
};
/* nanosleep sems here */
void wait_nanosleep_sem(struct lockinfo *l)
{
int ret;
struct timespec tv = { 0, 1000000 };
int count = 0;
l->data = 1;
worklist_add(l);
while(l->data) {
ret = nanosleep(&tv, NULL);
if (ret) {
perror("nanosleep");
exit(1);
}
count++;
}
}
int nanosleep_wake_some(struct sem_wakeup_info *wi, int num_semids, int num)
{
int i;
struct lockinfo *l;
for (i = 0; i < num; i++) {
l = worklist_rm();
if (!l)
break;
if (l->data != 1)
fprintf(stderr, "warning, lockinfo data was %d\n",
l->data);
l->data = 0;
}
return i;
}
void setup_nanosleep_sems(struct sem_wakeup_info **wi, int num_semids)
{
return;
}
void cleanup_nanosleep_sems(int num)
{
return;
}
struct sem_operations nanosleep_sem_ops = {
.wait = wait_nanosleep_sem,
.wake = nanosleep_wake_some,
.setup = setup_nanosleep_sems,
.cleanup = cleanup_nanosleep_sems,
.name = "nano sleep sem operations",
};
void *worker(void *arg)
{
struct lockinfo *l = (struct lockinfo *)arg;
int burn_count = 0;
pthread_t tid = pthread_self();
size_t pagesize = getpagesize();
char *buf = malloc(pagesize);
if (!buf) {
perror("malloc");
exit(1);
}
l->tid = tid;
workers_started = 1;
smp_mb();
while(!all_done) {
l->ops->wait(l);
if (all_done)
break;
burn_count++;
}
pthread_mutex_lock(&worklist_mutex);
total_burns += burn_count;
if (burn_count < min_burns)
min_burns = burn_count;
if (burn_count > max_burns)
max_burns = burn_count;
update_stats(&burn_stats, burn_count);
thread_count--;
pthread_mutex_unlock(&worklist_mutex);
return (void *)0;
}
void print_usage(void)
{
printf("usage: sembench [-t threads] [-w wake incr] [-r runtime]");
printf(" [-o num] (0=ipc, 1=nanosleep, 2=futex)\n");
exit(1);
}
#define NUM_OPERATIONS 3
struct sem_operations *allops[NUM_OPERATIONS] = { &ipc_sem_ops,
&nanosleep_sem_ops,
&futex_sem_ops};
int main(int ac, char **av) {
int ret;
int i;
int semid = 0;
int sem_num = 0;
int burn_count = 0;
struct sem_wakeup_info *wi = NULL;
struct timeval start;
struct timeval now;
int num_semids = 0;
int wake_num = 256;
int run_secs = 30;
int pagesize = getpagesize();
char *buf = malloc(pagesize);
struct sem_operations *ops = allops[0];
cpu_set_t cpu_mask;
cpu_set_t target_mask;
int target_cpu = 0;
int max_cpu = -1;
if (!buf) {
perror("malloc");
exit(1);
}
for (i = 1; i < ac; i++) {
if (strcmp(av[i], "-t") == 0) {
if (i == ac -1)
print_usage();
num_threads = atoi(av[i+1]);
i++;
} else if (strcmp(av[i], "-w") == 0) {
if (i == ac -1)
print_usage();
wake_num = atoi(av[i+1]);
i++;
} else if (strcmp(av[i], "-r") == 0) {
if (i == ac -1)
print_usage();
run_secs = atoi(av[i+1]);
i++;
} else if (strcmp(av[i], "-o") == 0) {
int index;
if (i == ac -1)
print_usage();
index = atoi(av[i+1]);
if (index >= NUM_OPERATIONS) {
fprintf(stderr, "invalid operations %d\n",
index);
exit(1);
}
ops = allops[index];
i++;
} else if (strcmp(av[i], "-T") == 0) {
timeout_test = 1;
} else if (strcmp(av[i], "-h") == 0) {
print_usage();
}
}
num_semids = (num_threads + SEMS_PERID - 1) / SEMS_PERID;
ops->setup(&wi, num_semids);
ret = sched_getaffinity(0, sizeof(cpu_set_t), &cpu_mask);
if (ret) {
perror("sched_getaffinity");
exit(1);
}
for (i = 0; i < CPU_SETSIZE; i++)
if (CPU_ISSET(i, &cpu_mask))
max_cpu = i;
if (max_cpu == -1) {
fprintf(stderr, "sched_getaffinity returned empty mask\n");
exit(1);
}
CPU_ZERO(&target_mask);
worklist = malloc(sizeof(*worklist) * num_threads);
memset(worklist, 0, sizeof(*worklist) * num_threads);
for (i = 0; i < num_threads; i++) {
struct lockinfo *l;
pthread_t tid;
thread_count++;
l = worklist + i;
if (!l) {
perror("malloc");
exit(1);
}
l->id = semid;
l->index = sem_num++;
l->ops = ops;
if (sem_num >= SEMS_PERID) {
semid++;
sem_num = 0;
}
ret = pthread_create(&tid, NULL, worker, (void *)l);
if (ret) {
perror("pthread_create");
exit(1);
}
while (!CPU_ISSET(target_cpu, &cpu_mask)) {
target_cpu++;
if (target_cpu > max_cpu)
target_cpu = 0;
}
CPU_SET(target_cpu, &target_mask);
ret = pthread_setaffinity_np(tid, sizeof(cpu_set_t),
&target_mask);
CPU_CLR(target_cpu, &target_mask);
target_cpu++;
ret = pthread_detach(tid);
if (ret) {
perror("pthread_detach");
exit(1);
}
}
while(!workers_started) {
smp_mb();
usleep(200);
}
gettimeofday(&start, NULL);
while(1) {
ops->wake(wi, num_semids, wake_num);
burn_count++;
gettimeofday(&now, NULL);
if (now.tv_sec - start.tv_sec >= run_secs)
break;
}
all_done = 1;
while(thread_count > 0) {
ops->wake(wi, num_semids, wake_num);
usleep(200);
}
printf("%d threads, waking %d at a time\n", num_threads, wake_num);
printf("using %s\n", ops->name);
printf("main thread burns: %d\n", burn_count);
printf("worker burn count total %lu min %lu max %lu avg %.3f +- %.3f%%\n",
total_burns, min_burns, max_burns, avg_stats(&burn_stats),
100 * stddev_stats(&burn_stats) / avg_stats(&burn_stats));
printf("run time %d seconds %lu worker burns per second\n",
(int)(now.tv_sec - start.tv_sec),
total_burns / (now.tv_sec - start.tv_sec));
ops->cleanup(num_semids);
return 0;
}