-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththr_bench.c
179 lines (147 loc) · 2.92 KB
/
thr_bench.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "bench.h"
struct list {
struct list *next;
long val;
};
#define MAX_THREADS 2
static char *mem;
static struct list *lists[MAX_THREADS];
void nonatominc_inc(struct list *l, size_t n)
{
for (size_t i=0; i < n; i++) {
l->val += 1;
l = l->next;
}
}
void atomic_add(struct list *l, size_t n)
{
for (size_t i=0; i < n; i++) {
__atomic_fetch_add(&l->val, 1, __ATOMIC_RELAXED);
l = l->next;
}
}
void atomic_rw(struct list *l, size_t n, bool modify)
{
for (size_t i=0; i < n; i++) {
if (modify) {
l->val += 1;
} else {
__atomic_load_n(&l->val, __ATOMIC_RELAXED);
}
l = l->next;
}
}
void benchmark_v(struct thrarg *thr)
{
struct list *l = lists[thr->params.id];
size_t n = thr->params.iters;
nonatominc_inc(l, n);
}
void benchmark_a(struct thrarg *thr)
{
size_t i;
long r = 0;
struct list *l = lists[thr->params.id];
size_t n = thr->params.iters;
for (i=0; i < n; i++) {
l->val += 1;
l = l->next;
}
USE(r);
}
void benchmark_s(struct thrarg *thr)
{
struct list *l = lists[thr->params.id];
size_t n = thr->params.iters;
atomic_add(l, n);
}
void benchmark_w(struct thrarg *thr)
{
unsigned id = thr->params.id;
struct list *l = lists[id];
size_t n = thr->params.iters;
atomic_rw(l, n, (bool)id);
}
void benchmark_r(struct thrarg *thr)
{
size_t i;
long r = 0;
unsigned id = thr->params.id;
struct list *l = lists[id];
size_t n = thr->params.iters;
for (i=0; i < n; i++) {
r = l->val;
l = l->next;
}
USE(r);
}
void init(struct thrarg *arg)
{
(void)*arg;
}
int main(int argc, char **argv)
{
unsigned i;
unsigned nthreads = MAX_THREADS;
unsigned pad;
benchmark_t benchmark;
if (argc < 3)
return 1;
if ( sscanf(argv[1], "%u", &pad) < 1)
return 1;
if (pad < sizeof(struct list) && pad != 0)
return 1;
switch(argv[2][0]) {
case 'v':
benchmark = benchmark_v;
break;
case 's':
benchmark = benchmark_s;
break;
case 'r':
benchmark = benchmark_r;
break;
case 'w':
benchmark = benchmark_w;
break;
case 'a':
benchmark = benchmark_a;
break;
default:
return 1;
}
if (posix_memalign((void **)&mem, 128, nthreads*pad)) {
perror("posix_memalign");
return 1;
}
memset(mem, 0, nthreads*pad);
for (i=0; i < nthreads; i++) {
struct list *l = (struct list *)&mem[i*pad];
l->next = l;
lists[i] = l;
}
const char *bp = getenv("BENCH_PRINT");
bool print_samples = (bp && strcmp(bp,"y") == 0) ? true : false;
struct thrarg thrarg = { .params = {
.threads = nthreads,
.benchmark = benchmark,
.init = init,
.print_samples = print_samples,
.max_samples = 100,
.min_time = 10*1000*1000,
.max_error = 10,
}};
int err = benchmark_auto(&thrarg);
if (err < 0) {
fprintf(stderr, "Bench error %s\n", strerror(err));
return 1;
}
printf("%u %.2f %.2f %.2f\n", pad, thrarg.result.avg, thrarg.result.err, thrarg.result.u);
return 0;
}