forked from rigtorp/ipc-bench
-
Notifications
You must be signed in to change notification settings - Fork 32
/
xutil.c
332 lines (302 loc) · 8.27 KB
/
xutil.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
/*
Copyright (c) 2011 Anil Madhavapeddy <[email protected]>
Copyright (c) 2011 Steven Smith <[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include <sys/mman.h>
#include <sys/time.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <sched.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <inttypes.h>
#include "atomicio.h"
#include "xutil.h"
#include "test.h"
void *
xmalloc(size_t size)
{
void *buf;
buf = malloc(size);
if (buf == NULL)
err(1, "xmalloc");
return buf;
}
void
xread(int fd, void *buf, size_t count)
{
ssize_t r;
r = atomicio(read, fd, buf, count);
if (r != count)
err(1, "xread");
}
void
xwrite(int fd, const void *buf, size_t count)
{
ssize_t r;
r = atomicio(vwrite, fd, (void *)buf, count);
if (r != count)
err(1, "xwrite");
}
static double
get_tsc_freq(void)
{
double estimates[5];
int nr_estimates;
struct timeval start;
struct timeval now;
unsigned long start_tsc;
unsigned long end_tsc;
double low_estimate, high_estimate;
double total;
int i;
while (1) {
for (nr_estimates = 0; nr_estimates < 5; nr_estimates++) {
gettimeofday(&start, NULL);
start_tsc = rdtsc();
do {
gettimeofday(&now, NULL);
now.tv_sec -= start.tv_sec;
now.tv_usec -= start.tv_usec;
if (now.tv_usec < 0) {
now.tv_sec--;
now.tv_usec += 1e6;
}
} while (now.tv_usec < 1e4);
end_tsc = rdtsc();
estimates[nr_estimates] = (end_tsc - start_tsc) / (now.tv_usec * 1e-6);
}
/* Check for anomalies */
low_estimate = estimates[0];
high_estimate = estimates[0];
for (i = 1; i < nr_estimates; i++) {
if (estimates[i] < low_estimate)
low_estimate = estimates[i];
if (estimates[i] > high_estimate)
high_estimate = estimates[i];
}
/* Accept if range of estimates is less than 1% */
if (high_estimate < low_estimate * 1.01)
break;
/* Otherwise try again */
}
/* Use an arithmetic mean. Should arguably be using harmonic mean
here, but since the thinks we're averaging vary by less than 1%
it shouldn't make much difference. */
total = 0;
for (i = 0; i < nr_estimates; i++)
total += estimates[i];
return total / nr_estimates;
}
static FILE *
open_logfile(test_data *td, const char *file)
{
char *path;
FILE *res;
assert(td->output_dir != NULL);
if (asprintf(&path,
"%s/%02d-%s-%s.log",
td->output_dir,
td->num,
td->name,
file) < 0)
err(1, "asprintf()");
res = fopen(path, "a");
if (!res)
err(1, "fopen(%s)", path);
free(path);
return res;
}
void
dump_tsc_counters(test_data *td, unsigned long *counts, int nr_samples)
{
FILE *f = open_logfile(td, "tsc");
double clock_freq = get_tsc_freq();
int i;
double *times = (double *)counts;
times = calloc(sizeof(double), nr_samples);
for (i = 0; i < nr_samples; i++)
times[i] = counts[i] / clock_freq;
free(counts);
summarise_samples(f, times, nr_samples);
fclose(f);
#ifdef DUMP_RAW_TSCS
f = open_logfile(td, "raw_tsc");
for (i = 0; i < nr_samples; i++)
fprintf(f, "%e\n", times[i]);
fclose(f);
#endif
free(times);
}
static void
help(char *argv[])
{
fprintf(stderr, "Usage:\n%s [-h] [-a <cpuid>] [-b <cpuid>] [-p <num] [-t] [-s <bytes>] [-c <num>] [-o <directory>] [-n <node>]\n", argv[0]);
fprintf(stderr, "-h: show this help message\n");
fprintf(stderr, "-a: CPU id that the first process should have affinity with\n");
fprintf(stderr, "-b: CPU id that the second process should have affinity with\n");
fprintf(stderr, "-p: number of parallel tests to run\n");
fprintf(stderr, "-t: use high-res TSC to get more accurate results\n");
fprintf(stderr, "-s: Size of each packet\n");
fprintf(stderr, "-c: Number of iterations\n");
fprintf(stderr, "-o: Where to put the various output files\n");
fprintf(stderr, "-n: NUMA node for shared arena, if any\n");
exit(1);
}
void
parse_args(int argc, char *argv[], bool *per_iter_timings, int *size, size_t *count, int *first_cpu, int *second_cpu,
int *parallel, char **output_dir, int *write_in_place, int *read_in_place, int *produce_method, int *do_verify,
int *numa_node)
{
int opt;
*per_iter_timings = false;
*first_cpu = 0;
*second_cpu = 0;
*parallel = 1;
*size = 1024;
*count = 100;
*output_dir = "results";
*numa_node = -1;
*produce_method = 0;
*read_in_place = 0;
*write_in_place = 0;
*do_verify = 0;
while((opt = getopt(argc, argv, "h?tp:a:b:s:c:o:wrvm:n:")) != -1) {
switch(opt) {
case 't':
*per_iter_timings = true;
break;
case 'p':
*parallel = atoi(optarg);
break;
case 'a':
*first_cpu = atoi(optarg);
break;
case 'b':
*second_cpu = atoi(optarg);
break;
case 's':
*size = atoi(optarg);
break;
case 'c':
*count = atoi(optarg);
break;
case 'o':
*output_dir = optarg;
break;
case 'm':
*produce_method = atoi(optarg);
break;
case 'r':
*read_in_place = 1;
break;
case 'w':
*write_in_place = 1;
break;
case 'v':
*do_verify = 1;
break;
case 'n':
*numa_node = atoi(optarg);
break;
case '?':
case 'h':
help(argv);
default:
fprintf(stderr, "unknown command-line option '%c'", opt);
help(argv);
}
}
fprintf(stderr, "size %d count %" PRIu64 " first_cpu %d second_cpu %d parallel %d tsc %d produce-method %d %s %s numa_node %d output_dir %s\n",
*size, *count, *first_cpu, *second_cpu, *parallel, *per_iter_timings, *produce_method, *read_in_place ? "read-in-place" : "copy-read", *write_in_place ? "write-in-place" : "copy-write",
*numa_node,
*output_dir);
}
void
setaffinity(int cpunum)
{
#ifdef Linux
cpu_set_t *mask;
size_t size;
int i;
int nrcpus = 160;
pid_t pid;
mask = CPU_ALLOC(nrcpus);
size = CPU_ALLOC_SIZE(nrcpus);
CPU_ZERO_S(size, mask);
CPU_SET_S(cpunum, size, mask);
pid = getpid();
i = sched_setaffinity(pid, size, mask);
if (i == -1)
err(1, "sched_setaffinity");
CPU_FREE(mask);
#else
fprintf(stderr, "setaffinity: skipping\n");
#endif
}
#ifdef Linux
#include <numa.h>
#endif
void *
establish_shm_segment(int nr_pages, int numa_node)
{
#ifdef Linux
int fd;
void *addr;
fd = shm_open("/memflag_lat", O_RDWR|O_CREAT|O_EXCL, 0600);
if (fd < 0)
err(1, "shm_open(\"/memflag_lat\")");
shm_unlink("/memflag_lat");
if (ftruncate(fd, PAGE_SIZE * nr_pages) < 0)
err(1, "ftruncate() shared memory segment");
addr = mmap(NULL, PAGE_SIZE * nr_pages, PROT_READ|PROT_WRITE, MAP_SHARED,
fd, 0);
if (addr == MAP_FAILED)
err(1, "mapping shared memory segment");
if(numa_node != -1)
numa_tonode_memory(addr, PAGE_SIZE * nr_pages, numa_node);
close(fd);
return addr;
#else
errx(1, "establish_shm_segment not implemented\n");
#endif
}
void
logmsg(test_data *td, const char *file, const char *fmt, ...)
{
FILE *f = open_logfile(td, file);
va_list args;
char *res;
va_start(args, fmt);
if (vasprintf(&res, fmt, args) < 0)
err(1, "vasprintf(%s)", fmt);
va_end(args);
if (fputs(res, f) == EOF)
err(1, "fputs(%s) to logfile:%s", res, file);
if (fclose(f) == EOF)
err(1, "fclose(logfile:%s)", file);
}