forked from rigtorp/ipc-bench
-
Notifications
You must be signed in to change notification settings - Fork 32
/
test.c
327 lines (283 loc) · 9.08 KB
/
test.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
/*
* Copyright (c) 2011 Anil Madhavapeddy <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/stat.h>
#include <assert.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/time.h>
#include <err.h>
#include <inttypes.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/uio.h>
#include <errno.h>
#include "test.h"
#include "xutil.h"
static void
wait_for_children_to_finish(void)
{
int status, rv;
/* Make sure the child really does go away when it's no longer
needed. */
while (1) {
rv = waitpid(-1, &status, 0);
assert(rv != 0);
if (rv < 0) {
if (errno == ECHILD)
break;
err(1, "waitpid()");
}
if (WIFSIGNALED(status))
errx(1, "child killed by signal %d", WTERMSIG(status));
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0)
errx(1, "child exited with status %d", WEXITSTATUS(status));
} else {
errx(1, "unexpected status %x from waitpid", status);
}
}
}
static void stosmemset(void* buf, int byte, size_t count) {
#ifdef USE_INLINE_ASM
int clobber;
assert(count % 8 == 0);
asm volatile ("rep stosq\n"
: "=c" (clobber)
: "a" ((unsigned long)(byte & 0xff) * 0x0101010101010101ul),
"D" (buf),
"0" (count / 8)
: "memory");
#else
errx(1, "stosmemset: not implemented");
#endif
}
static int repmemcmp(void* buf, int byte, size_t count) {
#ifdef USE_INLINE_ASM
unsigned long clobber;
void* clobber2;
char result;
assert(count % 8 == 0);
asm ("repe scasq\n"
"setne %%al\n"
: "=a" (result),
"=c" (clobber),
"=D" (clobber2)
: "a" ((unsigned long)(byte & 0xff) * 0x0101010101010101ul),
"c" (count / 8),
"D" (buf)
);
return result;
#else
errx(1, "repmemcmp: not implemented");
#endif
}
void parent_main(test_t* test, test_data* td, int is_latency_test) {
char* private_buffer = xmalloc(td->size);
struct timeval start;
struct timeval stop;
unsigned long *iter_cycles;
unsigned long delta;
unsigned long t = 0;
struct iovec private_vec = { .iov_base = private_buffer, .iov_len = td->size };
if(test->init_parent)
test->init_parent(td);
/* calm compiler */
iter_cycles = NULL;
if (td->per_iter_timings) {
iter_cycles = calloc(sizeof(iter_cycles[0]), td->count);
if (!iter_cycles)
err(1, "calloc");
}
gettimeofday(&start, NULL);
for (int i = 0; i < td->count; i++) {
if(td->per_iter_timings)
t = rdtsc();
struct iovec* write_bufs;
int n_write_bufs;
struct iovec* produce_bufs;
int n_produce_bufs;
if(!is_latency_test) {
write_bufs = test->get_write_buffer(td, td->size, &n_write_bufs);
if(td->write_in_place) {
produce_bufs = write_bufs;
n_produce_bufs = n_write_bufs;
}
else {
produce_bufs = &private_vec;
n_produce_bufs = 1;
}
for(int j = 0; j < n_produce_bufs; j++) {
if(td->produce_method == PRODUCE_GLIBC_MEMSET)
memset(produce_bufs[j].iov_base, (char)i, produce_bufs[j].iov_len);
else if(td->produce_method == PRODUCE_STOS_MEMSET)
stosmemset(produce_bufs[j].iov_base, (char)i, produce_bufs[j].iov_len);
else if(td->produce_method == PRODUCE_LOOP) {
for(int k = 0; k < produce_bufs[j].iov_len; k++)
((char*)produce_bufs[j].iov_base)[k] = (char)i;
}
else {
assert(0 && "Bad produce method!");
}
}
if(!td->write_in_place) {
int offset = 0;
for(int j = 0; j < n_produce_bufs; j++) {
memcpy(((char*)write_bufs[j].iov_base) + offset, private_buffer + offset, write_bufs[j].iov_len);
offset += write_bufs[j].iov_len;
}
}
test->release_write_buffer(td, write_bufs, n_write_bufs);
}
else {
test->parent_ping(td);
}
if(td->per_iter_timings)
iter_cycles[i] = rdtsc() - t;
}
if(test->finish_parent)
test->finish_parent(td);
gettimeofday(&stop, NULL);
delta = ((stop.tv_sec - start.tv_sec) * (int64_t) 1000000 +
stop.tv_usec - start.tv_usec);
if (is_latency_test)
logmsg(td,
"headline",
"%s %d %" PRIu64 " %fs\n", td->name, td->size, td->count,
delta / (td->count * 1e6));
else
logmsg(td,
"headline",
"%s %d %d %d %d %d %d %d %d %" PRId64 " %" PRId64 " Mbps\n", td->name, td->first_core, td->second_core,
td->numa_node,
td->size,
td->produce_method, td->write_in_place, td->read_in_place, td->do_verify, td->count,
((((td->count * (int64_t)1e6) / delta) * td->size * 8) / (int64_t) 1e6));
if (td->per_iter_timings)
dump_tsc_counters(td, iter_cycles, td->count);
}
void child_main(test_t* test, test_data* td, int is_latency_test) {
char* private_buffer = xmalloc(td->size);
struct iovec private_vec = { .iov_base = private_buffer, .iov_len = td->size };
if(test->init_child)
test->init_child(td);
for(int i = 0; i < td->count; i++) {
struct iovec* check_bufs;
int n_check_bufs;
struct iovec* read_bufs;
int n_read_bufs;
if(!is_latency_test) {
read_bufs = test->get_read_buffer(td, td->size, &n_read_bufs);
if(td->read_in_place) {
check_bufs = read_bufs;
n_check_bufs = n_read_bufs;
}
else {
check_bufs = &private_vec;
n_check_bufs = 1;
for(int j = 0, offset = 0; j < n_read_bufs; offset += read_bufs[j].iov_len, j++) {
memcpy(private_buffer + offset, read_bufs[j].iov_base, read_bufs[j].iov_len);
}
}
if(td->do_verify) {
for(int j = 0; j < n_check_bufs; j++) {
if(repmemcmp(check_bufs[j].iov_base, i, check_bufs[j].iov_len))
err(1, "bad data");
}
}
test->release_read_buffer(td, read_bufs, n_read_bufs);
}
else {
test->child_ping(td);
}
}
if(test->finish_child)
test->finish_child(td);
}
/* Execute a test with as many parallel iterations as requested */
void
run_test(int argc, char *argv[], test_t *test)
{
bool per_iter_timings;
int first_cpu, second_cpu;
size_t count;
int size, parallel;
char *output_dir;
int write_in_place, read_in_place, produce_method, do_verify;
int numa_node;
parse_args(argc, argv, &per_iter_timings, &size, &count, &first_cpu, &second_cpu, ¶llel, &output_dir,
&write_in_place, &read_in_place, &produce_method, &do_verify, &numa_node);
if((!test->is_latency_test) && (!(produce_method >= 1 && produce_method <= 3))) {
fprintf(stderr, "Produce method (option -m) must be specified and between 1 and 3\n");
exit(1);
}
if (mkdir(output_dir, 0755) < 0 && errno != EEXIST)
err(1, "creating directory %s", output_dir);
while (parallel > 0) {
pid_t pid1 = fork ();
if (!pid1) { /* child1 */
/* Initialise a test run */
test_data *td = xmalloc(sizeof(test_data));
memset(td, 0, sizeof(test_data));
td->num = parallel;
td->size = size;
td->count = count;
td->write_in_place = write_in_place;
td->read_in_place = read_in_place;
td->produce_method = produce_method;
td->do_verify = do_verify;
td->first_core = first_cpu;
td->second_core = second_cpu;
td->per_iter_timings = per_iter_timings;
// td->mode = mode;
td->numa_node = numa_node;
/* Test-specific init */
test->init_test(td);
pid_t pid2 = fork ();
if (!pid2) { /* child2 */
setaffinity(first_cpu);
child_main(test, td, test->is_latency_test);
exit (0);
} else { /* parent2 */
td->output_dir = output_dir; /* Do this here because the child
isn't supposed to log
anything. */
td->name = test->name;
setaffinity(second_cpu);
parent_main(test, td, test->is_latency_test);
wait_for_children_to_finish();
exit (0);
}
} else { /* parent */
/* continue to fork */
parallel--;
}
}
wait_for_children_to_finish();
}