forked from wuxb45/wormhole
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstresstest.c
354 lines (331 loc) · 9.89 KB
/
stresstest.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
/*
* Copyright (c) 2016-2020 Wu, Xingbo <[email protected]>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#define _GNU_SOURCE
#include "lib.h"
#include "kv.h"
#include "wh.h"
#include "ctypes.h"
struct stress_info {
u64 nkeys;
u32 nloader;
u32 nunldr;
u32 nth;
u32 cpt;
bool has_iter;
au64 seqno;
struct kv ** keys;
const struct kvmap_api * api;
void * map;
au64 tot;
au64 wfail;
u64 endtime;
};
static void *
stress_load_worker(void * ptr)
{
struct stress_info * const si = (typeof(si))ptr;
srandom_u64(time_nsec() * time_nsec() / time_nsec());
void * const ref = kvmap_ref(si->api, si->map);
const u64 seq = atomic_fetch_add(&si->seqno, 1);
const u64 n0 = si->nkeys / si->nloader * seq;
const u64 nz = (seq == (si->nloader - 1)) ? si->nkeys : (si->nkeys / si->nloader * (seq + 1));
//printf("load worker %lu %lu\n", n0, nz-1);
char * buf = malloc(128);
debug_assert(buf);
u64 * buf64 = (typeof(buf64))buf;
for (u64 i = n0; i < nz; i++) {
const u32 klen = (u32)(random_u64() & 0x3flu) + 8;
const u32 klen8 = (klen + 7) >> 3;
/*
buf64[0] = bswap_64(i); // little endian
for (u64 j = 1; j < klen8; j++)
buf64[j] = random_u64();
*/
const u64 rkey = random_u64();
for (u32 j = 0; j < klen8; j++)
buf64[j] = (rkey >> j) & 0x0101010101010101lu;
si->keys[i] = kv_create(buf, klen, buf, 8);
if (si->keys[i] == NULL)
exit(0);
kvmap_kv_put(si->api, ref, si->keys[i]);
}
free(buf);
kvmap_unref(si->api, ref);
return NULL;
}
static void *
stress_unload_worker(void * ptr)
{
struct stress_info * const si = (typeof(si))ptr;
const u64 seq = atomic_fetch_add(&si->seqno, 1);
const u64 n0 = si->nkeys / si->nunldr * seq;
const u64 nz = (seq == (si->nunldr - 1)) ? si->nkeys : (si->nkeys / si->nunldr * (seq + 1));
void * const ref = kvmap_ref(si->api, si->map);
for (u64 i = n0; i < nz; i++) {
kvmap_kv_del(si->api, ref, si->keys[i]);
free(si->keys[i]);
}
kvmap_unref(si->api, ref);
return NULL;
}
static void
stress_inp_plus1(struct kv * const kv0, void * const priv)
{
(void)priv;
if (kv0) { // can be NULL
u64 * ptr = kv_vptr(kv0);
++(*ptr);
}
}
static struct kv *
stress_merge_plus1(struct kv * const kv0, void * const priv)
{
(void)priv;
if (kv0) { // can be NULL
u64 * ptr = kv_vptr(kv0);
++(*ptr);
return kv0;
} else {
u64 * ptr = kv_vptr((struct kv *)priv);
*ptr = 0;
return priv;
}
}
static void
stress_func(struct stress_info * const si)
{
srandom_u64(time_nsec() * time_nsec() / time_nsec());
const struct kvmap_api * const api = si->api;
void * ref = kvmap_ref(api, si->map);
struct kv * next = si->keys[random_u64() % si->nkeys];
u64 rnext = random_u64() % si->nkeys;
struct kv * const tmp = malloc(128);
struct kref tmpkref;
struct kvref tmpkvref;
debug_assert(tmp);
void * iter = NULL;
if (api->iter_park) {
iter = api->iter_create(ref);
api->iter_park(iter);
}
u64 wfail1 = 0;
u64 nops = 0;
#define BATCHSIZE ((4096))
do {
for (u64 i = 0; i < BATCHSIZE; i++) {
// reading kv keys leads to unnecessary cache misses
// use prefetch to minimize overhead on workload generation
struct kv * const key = next;
next = si->keys[rnext];
cpu_prefetch0(next);
cpu_prefetch0(((u8 *)next) + 64);
rnext = random_u64() % si->nkeys;
cpu_prefetch0(&(si->keys[rnext]));
// do probe
// customize your benchmark: do a mix of wh operations with switch-cases
const u64 r = random_u64() % 16;
switch (r) {
case 0:
kvmap_kv_probe(api, ref, key);
break;
case 1:
kvmap_kv_get(api, ref, key, tmp);
break;
case 2:
if (si->has_iter) {
if (api->iter_park == NULL)
iter = api->iter_create(ref);
debug_assert(iter);
kvmap_kv_iter_seek(api, iter, key);
api->iter_next(iter, tmp);
api->iter_peek(iter, tmp);
api->iter_skip(iter, 2);
// this is unsafe; only reader's lock is acquired
if (api->iter_inp)
api->iter_inp(iter, stress_inp_plus1, NULL);
// kref
if (api->iter_kref)
api->iter_kref(iter, &tmpkref);
// kvref
if (api->iter_kvref)
api->iter_kvref(iter, &tmpkvref);
// done
if (api->iter_park)
api->iter_park(iter);
else
api->iter_destroy(iter);
}
break;
case 3:
if (api->refpark) {
api->park(ref);
api->resume(ref);
}
break;
case 4:
if (api->iter_park)
api->iter_destroy(iter);
(void)kvmap_unref(api, ref);
ref = kvmap_ref(api, si->map);
if (api->iter_park)
iter = api->iter_create(ref);
break;
case 5:
if (api->merge) {
kv_dup2_key(key, tmp);
tmp->vlen = 8;
kvmap_kv_merge(api, ref, key, stress_merge_plus1, tmp);
}
break;
case 6:
if ((random_u64() & 0x7fffu) == 0x22 && api->delr)
(void)kvmap_kv_delr(api, ref, si->keys[rnext], (rnext + 10) < si->nkeys ? si->keys[rnext + 10] : NULL);
else
kvmap_kv_probe(api, ref, key);
break;
case 7: case 8: case 9:
(void)kvmap_kv_del(api, ref, key);
break;
case 10: case 11:
if (api->inpw)
kvmap_kv_inpw(api, ref, key, stress_inp_plus1, NULL);
break;
case 12: case 13: case 14: case 15:
if (!kvmap_kv_put(api, ref, key))
wfail1++;
break;
default:
break;
}
}
nops += BATCHSIZE;
} while (time_nsec() < si->endtime);
si->wfail += wfail1;
if (api->iter_park)
api->iter_destroy(iter);
kvmap_unref(api, ref);
free(tmp);
si->tot += nops;
}
static void
stress_co_worker(void)
{
struct stress_info * const si = (typeof(si))co_priv();
debug_assert(si);
stress_func(si);
}
static void *
stress_thread_worker(void * ptr)
{
struct stress_info * const si = (typeof(si))ptr;
if (si->cpt) {
u64 hostrsp = 0;
struct corr * crs[32];
do { // to work smoothly with ALLOCFAIL
crs[0] = corr_create(16*PGSZ, stress_co_worker, si, &hostrsp);
} while (crs[0] == NULL);
for (u32 j = 1; j < si->cpt; j++) {
do { // to work smoothly with ALLOCFAIL
crs[j] = corr_link(16*PGSZ, stress_co_worker, si, crs[j-1]);
} while (crs[j] == NULL);
}
corr_enter(crs[0]);
for (u32 j = 0; j < si->cpt; j++)
corr_destroy(crs[j]);
} else {
stress_func(si);
}
return NULL;
}
int
main(int argc, char ** argv)
{
struct stress_info si = {.nkeys = 10000, .nloader = 1, .nunldr = 1, .nth = 1, .cpt = 0};
argc--;
argv++;
int n = -1;
if ((n = kvmap_api_helper(argc, argv, NULL, &si.api, &si.map)) < 0) {
fprintf(stderr, "usage: api ... [<#keys>=10000 [<#load-threads>=1 [<#unload-threads>=1 [<#threads>=1 [<#co-per-thread>=0 (disabled) [<rounds>=1 [<epochs>=1]]]]]]]\n");
kvmap_api_helper_message();
exit(0);
}
argc -= n;
argv += n;
const bool has_point = si.api->get && si.api->probe && si.api->del && si.api->put;
if (!has_point) {
fprintf(stderr, "api not supported\n");
exit(0);
}
if (!si.api->inpw)
fprintf(stderr, "api->inpw function not found: ignored\n");
if (!si.api->merge)
fprintf(stderr, "api->merge function not found: ignored\n");
if (!si.api->delr)
fprintf(stderr, "api->delr function not found: ignored\n");
si.has_iter = si.api->iter_create && si.api->iter_seek && si.api->iter_peek &&
si.api->iter_skip && si.api->iter_next && si.api->iter_destroy;
if (!si.has_iter)
fprintf(stderr, "iter functions not complete: ignored\n");
// generate keys
if (argc >= 1)
si.nkeys = a2u64(argv[0]);
si.keys = malloc(sizeof(struct kv *) * si.nkeys);
debug_assert(si.keys);
if (argc >= 2)
si.nloader = a2u32(argv[1]);
if (argc >= 3)
si.nunldr = a2u32(argv[2]);
if (argc >= 4)
si.nth = a2u32(argv[3]);
if (argc >= 5)
si.cpt = a2u32(argv[4]);
if (si.cpt > 32)
si.cpt = 32;
#if !defined(CORR)
if (si.cpt > 1)
fprintf(stderr, TERMCLR(35) "CORR not enabled. Compile with -DCORR to enable it.\n" TERMCLR(0));
#endif // CORR
const u64 nr = (argc >= 6) ? a2u64(argv[5]) : 1; // default 1
const u64 ne = (argc >= 7) ? a2u64(argv[6]) : 1; // default 1
printf("stresstest: nkeys %lu ldr %u uldr %u th %u cpt %u r %lu e %lu\n",
si.nkeys, si.nloader, si.nunldr, si.nth, si.cpt, nr, ne);
for (u64 e = 0; e < ne; e++) {
si.seqno = 0;
const u64 dtl = thread_fork_join(si.nloader, (void *)stress_load_worker, false, &si);
printf("load th %u mops %.2lf\n", si.nloader, ((double)si.nkeys) * 1e3 / ((double)dtl));
if (si.api->fprint)
si.api->fprint(si.map, stdout);
debug_perf_switch();
for (u64 r = 0; r < nr; r++) {
si.tot = 0;
si.wfail = 0;
si.endtime = time_nsec() + 2000000000lu;
const u64 dt = thread_fork_join(si.nth, (void *)stress_thread_worker, false, &si);
const double mops = ((double)si.tot) * 1e3 / ((double)dt);
char ts[64];
time_stamp(ts, 64);
const long rss = process_get_rss();
printf("%s e %lu r %lu th %u cpt %u tot %lu mops %.2lf rss %ldkB wfail %lu\n",
ts, e, r, si.nth, si.cpt, si.tot, mops, rss, si.wfail);
debug_perf_switch();
}
si.seqno = 0;
if (si.nunldr == 0) { // use clean
const u64 t0 = time_nsec();
si.api->clean(si.map);
const u64 dtu = time_diff_nsec(t0);
for (u64 i = 0; i < si.nkeys; i++)
free(si.keys[i]);
printf("clean mops %.2lf\n", ((double)si.nkeys) *1e3 / ((double)dtu));
} else {
const u64 dtu = thread_fork_join(si.nunldr, (void *)stress_unload_worker, false, &si);
printf("unload th %u mops %.2lf\n", si.nunldr, ((double)si.nkeys) *1e3 / ((double)dtu));
}
}
free(si.keys);
si.api->destroy(si.map);
return 0;
}