-
Notifications
You must be signed in to change notification settings - Fork 7
/
perf_meas.c
292 lines (232 loc) · 6.54 KB
/
perf_meas.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
/**
* Priority queue test harness.
*
*
* Copyright (c) 2013-2018, Jonatan Linden
*
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <pthread.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include <limits.h>
#include "gc/gc.h"
#include "common.h"
#include "prioq.h"
/* check your cpu core numbering before pinning */
#define PIN
#define DEFAULT_SECS 10
#define DEFAULT_NTHREADS 1
#define DEFAULT_OFFSET 32
#define DEFAULT_SIZE 1<<15
#define EXPS 100000000
#define THREAD_ARGS_FOREACH(_iter) \
for (int i = 0; i < nthreads && (_iter = &ts[i]); i++)
/* preload array with exponentially distanced integers for the
* DES workload */
unsigned long *exps;
int exps_pos = 0;
void gen_exps(unsigned long *arr, unsigned short rng[3], int len, int intensity);
/* the workloads */
void work_exp (pq_t *pq);
void work_uni (pq_t *pq);
void *run (void *_args);
void (* work)(pq_t *pq);
thread_args_t *ts;
pq_t *pq;
volatile int wait_barrier = 0;
volatile int loop = 0;
static void
usage(FILE *out, const char *argv0)
{
fprintf(out, "Usage: %s [OPTION]...\n"
"\n"
"Options:\n", argv0);
fprintf(out, "\t-h\t\tDisplay usage.\n");
fprintf(out, "\t-t SECS\t\tRun for SECS seconds. "
"Default: %i\n",
DEFAULT_SECS);
fprintf(out, "\t-o OFFSET\tUse an offset of OFFSET nodes. Sensible "
"\n\t\t\tvalues could be 16 for 8 threads, 128 for 32 threads. "
"\n\t\t\tDefault: %i\n",
DEFAULT_OFFSET);
fprintf(out, "\t-n NUM\t\tUse NUM threads. "
"Default: %i\n",
DEFAULT_NTHREADS);
fprintf(out, "\t-s SIZE\t\tInitialize queue with SIZE elements. "
"Default: %i\n",
DEFAULT_SIZE);
}
static inline unsigned long
next_geometric (unsigned short seed[3], unsigned int p)
{
/* inverse transform sampling */
/* cf. https://en.wikipedia.org/wiki/Geometric_distribution */
return floor(log(erand48(seed))/log(1 - p));
/* uniformly distributed bits => geom. dist. level, p = 0.5 */
//return __builtin_ctz(nrand48(seed) & (1LU << max) - 1) + 1;
}
int
main (int argc, char **argv)
{
int opt;
unsigned short rng[3];
struct timespec time;
struct timespec start, end;
thread_args_t *t;
unsigned long elem;
extern char *optarg;
extern int optind, optopt;
int nthreads = DEFAULT_NTHREADS;
int offset = DEFAULT_OFFSET;
int secs = DEFAULT_SECS;
int exp = 0;
int init_size = DEFAULT_SIZE;
int concise = 0;
work = work_uni;
while ((opt = getopt(argc, argv, "t:n:o:s:hex")) >= 0) {
switch (opt) {
case 'n': nthreads = atoi(optarg); break;
case 't': secs = atoi(optarg); break;
case 'o': offset = atoi(optarg); break;
case 's': init_size = atoi(optarg); break;
case 'x': concise = 1; break;
case 'e': exp = 1; work = work_exp; break;
case 'h': usage(stdout, argv[0]); exit(EXIT_SUCCESS); break;
}
}
#ifndef PIN
printf("Running without threads pinned to cores.\n");
#endif
E_NULL(ts = malloc(nthreads*sizeof(thread_args_t)));
memset(ts, 0, nthreads*sizeof(thread_args_t));
// finally available in macos 10.12 as well!
clock_gettime(CLOCK_REALTIME, &time);
/* initialize seed */
rng[0] = time.tv_nsec;
rng[1] = time.tv_nsec >> 16;
rng[2] = time.tv_nsec >> 32;
/* initialize garbage collection */
_init_gc_subsystem();
pq = pq_init(offset);
// if DES workload, pre-sample values/event times
if (exp) {
E_NULL(exps = (unsigned long *)malloc(sizeof(unsigned long) * EXPS));
gen_exps(exps, rng, EXPS, 1000);
}
/* pre-fill priority queue with elements */
for (int i = 0; i < init_size; i++) {
if (exp) {
elem = exps[exps_pos++];
insert(pq, elem, (void *)elem);
} else {
elem = nrand48(rng);
insert(pq, elem, (void *)elem);
}
}
/* initialize threads */
THREAD_ARGS_FOREACH(t) {
t->id = i;
rng_init(t->rng);
E_en(pthread_create(&t->thread, NULL, run, t));
}
/* RUN BENCHMARK */
/* wait for all threads to call in */
while (wait_barrier != nthreads) ;
IRMB();
gettime(&start);
loop = 1;
IWMB();
/* Process might sleep longer than specified,
* but this will be accounted for. */
usleep( 1000000 * secs );
loop = 0; /* halt all threads */
IWMB();
gettime(&end);
/* END RUN BENCHMARK */
THREAD_ARGS_FOREACH(t) {
pthread_join(t->thread, NULL);
}
/* PRINT PERF. MEASURES */
int sum = 0, min = INT_MAX, max =0;
THREAD_ARGS_FOREACH(t) {
sum += t->measure;
min = min(min, t->measure);
max = max(max, t->measure);
}
struct timespec elapsed = timediff(start, end);
double dt = elapsed.tv_sec + (double)elapsed.tv_nsec / 1000000000.0;
if (!concise) {
printf("Total time:\t%1.8f s\n", dt);
printf("Ops:\t\t%d\n", sum);
printf("Ops/s:\t\t%.0f\n", (double) sum / dt);
printf("Min ops/t:\t%d\n", min);
printf("Max ops/t:\t%d\n", max);
} else {
printf("%li\n", lround((double) sum / dt));
}
/* CLEANUP */
pq_destroy(pq);
free (ts);
_destroy_gc_subsystem();
}
__thread thread_args_t *args;
/* uniform workload */
void
work_uni (pq_t *pq)
{
unsigned long elem;
if (erand48(args->rng) < 0.5) {
elem = (unsigned long)1 + nrand48(args->rng);
insert(pq, elem, (void *)elem);
} else
deletemin(pq);
}
/* DES workload */
void
work_exp (pq_t *pq)
{
int pos;
unsigned long elem;
deletemin(pq);
pos = __sync_fetch_and_add(&exps_pos, 1);
elem = exps[pos];
insert(pq, elem, (void *)elem);
}
void *
run (void *_args)
{
args = (thread_args_t *)_args;
int cnt = 0;
#if defined(PIN) && defined(__linux__)
/* Straight allocation on 32 core machine.
* Check with your OS + machine. */
pin (gettid(), args->id/8 + 4*(args->id % 8));
#endif
// call in to main thread
__sync_fetch_and_add(&wait_barrier, 1);
// wait until signaled by main thread
while (!loop);
/* start benchmark execution */
do {
work(pq);
cnt++;
} while (loop);
/* end of measured execution */
args->measure = cnt;
return NULL;
}
/* generate array of exponentially distributed variables */
void
gen_exps(unsigned long *arr, unsigned short rng[3], int len, int intensity)
{
int i = 0;
arr[0] = 2;
while (++i < len)
arr[i] = arr[i-1] +
next_geometric(rng, intensity);
}