-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbcdf.c
More file actions
318 lines (285 loc) · 8.58 KB
/
dbcdf.c
File metadata and controls
318 lines (285 loc) · 8.58 KB
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
/*
* Copyright (c) 2016--2021 Wu, Xingbo <wuxb45@gmail.com>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#define _GNU_SOURCE
#include "lib.h"
#include "kv.h"
struct priv {
void * ref;
u32 klen;
union {
u32 vlen;
u32 nscan;
};
struct kv * tmp;
struct kv * out;
};
#define VCTRSZ ((10000))
static bool
kvmap_analyze(void * const passdata[2], const u64 dt, const struct vctr * const va, struct damp * const d, char * const out)
{
(void)dt;
(void)d;
const struct kvmap_api * const api = passdata[0];
if (api->async) {
api->fprint(passdata[1], stdout);
strcpy(out, "\n");
return true;
}
u64 sum = 0;
for (u64 i = 0; i < VCTRSZ; i++)
sum += vctr_get(va, i);
const u64 tot = sum;
const double totd = (double)tot;
sum = 0;
u64 last = 0;
printf("time_us count delta cdf\n0 0 0 0.000\n");
for (u64 i = 1; i < VCTRSZ; i++) {
const u64 tmp = vctr_get(va, i);
if (tmp) {
if ((i-1) != last)
printf("%lu %lu %lu %.3lf\n", i-1, sum, 0lu, (double)sum * 100.0 / totd);
sum += tmp;
printf("%lu %lu %lu %.3lf\n", i, sum, tmp, (double)sum * 100.0 / totd);
last = i;
}
}
sprintf(out, "total %lu\n", tot);
return true;
}
static void
latency_add(struct vctr * const vctr, const u64 dt)
{
debug_assert(dt);
const u64 us = (dt + 999) / 1000;
if (us < VCTRSZ) {
vctr_add1_atomic(vctr, us);
} else {
vctr_add1_atomic(vctr, VCTRSZ-1);
printf("%s micro-second %lu\n", __func__, us);
}
}
// (parallel) load; nr <= nr_kvs
static void
kvmap_batch_put_par(const struct forker_worker_info * const info,
const struct priv * const priv, const u64 nr)
{
const struct kvmap_api * const api = info->passdata[0];
void * const ref = priv->ref;
if (info->end_type != FORKER_END_COUNT)
return;
struct kv * const tmp = priv->tmp;
const u64 nr1 = nr / info->conc;
const u64 id0 = nr1 * info->worker_id;
const u64 end = (info->worker_id == (info->conc - 1)) ? nr : (id0 + nr1);
for (u64 i = id0; i < end; i++) {
kv_refill_hex64_klen(tmp, i, priv->klen, NULL, 0);
tmp->vlen = priv->vlen;
const u64 t0 = time_nsec();
(void)kvmap_kv_put(api, ref, tmp);
const u64 dt = time_diff_nsec(t0);
latency_add(info->vctr, dt);
}
}
static void
kvmap_batch_put(const struct forker_worker_info * const info,
const struct priv * const priv, const u64 nr)
{
const struct kvmap_api * const api = info->passdata[0];
void * const ref = priv->ref;
struct rgen * const gen = info->gen;
rgen_next_func next = info->rgen_next_write;
struct kv * const tmp = priv->tmp;
for (u64 i = 0; i < nr; i++) {
kv_refill_hex64_klen(tmp, next(gen), priv->klen, NULL, 0);
tmp->vlen = priv->vlen;
const u64 t0 = time_nsec();
(void)kvmap_kv_put(api, ref, tmp);
const u64 dt = time_diff_nsec(t0);
latency_add(info->vctr, dt);
}
}
static void
kvmap_batch_del(const struct forker_worker_info * const info,
const struct priv * const priv, const u64 nr)
{
const struct kvmap_api * const api = info->passdata[0];
void * const ref = priv->ref;
struct rgen * const gen = info->gen;
rgen_next_func next = info->rgen_next_write;
struct kv * const tmp = priv->tmp;
for (u64 i = 0; i < nr; i++) {
kv_refill_hex64_klen(tmp, next(gen), priv->klen, NULL, 0);
const u64 t0 = time_nsec();
(void)kvmap_kv_del(api, ref, tmp);
const u64 dt = time_diff_nsec(t0);
latency_add(info->vctr, dt);
}
}
static void
kvmap_batch_get(const struct forker_worker_info * const info,
const struct priv * const priv, const u64 nr)
{
const struct kvmap_api * const api = info->passdata[0];
void * const ref = priv->ref;
struct rgen * const gen = info->gen;
rgen_next_func next = info->rgen_next;
struct kv * const tmp = priv->tmp;
for (u64 i = 0; i < nr; i++) {
kv_refill_hex64_klen(tmp, next(gen), priv->klen, NULL, 0);
const u64 t0 = time_nsec();
(void)kvmap_kv_get(api, ref, tmp, priv->out);
const u64 dt = time_diff_nsec(t0);
latency_add(info->vctr, dt);
}
}
static void
kvmap_batch_pro(const struct forker_worker_info * const info,
const struct priv * const priv, const u64 nr)
{
const struct kvmap_api * const api = info->passdata[0];
void * const ref = priv->ref;
struct rgen * const gen = info->gen;
rgen_next_func next = info->rgen_next;
struct kv * const tmp = priv->tmp;
for (u64 i = 0; i < nr; i++) {
kv_refill_hex64_klen(tmp, next(gen), priv->klen, NULL, 0);
const u64 t0 = time_nsec();
(void)kvmap_kv_probe(api, ref, tmp);
const u64 dt = time_diff_nsec(t0);
latency_add(info->vctr, dt);
}
}
static void
kvmap_batch_seek_next(const struct forker_worker_info * const info,
const struct priv * const priv, const u64 nr)
{
const struct kvmap_api * const api = info->passdata[0];
void * const ref = priv->ref;
void * const iter = api->iter_create(ref);
const u32 nscan = priv->nscan;
debug_assert(iter);
struct rgen * const gen = info->gen;
rgen_next_func next = info->rgen_next;
for (u64 i = 0; i < nr; i++) {
kv_refill_hex64_klen(priv->tmp, next(gen), priv->klen, NULL, 0);
const u64 t0 = time_nsec();
kvmap_kv_iter_seek(api, iter, priv->tmp);
for (u32 j = 0; j < nscan; j++)
api->iter_next(iter, priv->out);
const u64 dt = time_diff_nsec(t0);
latency_add(info->vctr, dt);
}
api->iter_destroy(iter);
}
static void
kvmap_batch_seek_skip(const struct forker_worker_info * const info,
const struct priv * const priv, const u64 nr)
{
const struct kvmap_api * const api = info->passdata[0];
void * const ref = priv->ref;
void * const iter = api->iter_create(ref);
const u32 nscan = priv->nscan;
debug_assert(iter);
struct rgen * const gen = info->gen;
rgen_next_func next = info->rgen_next;
for (u64 i = 0; i < nr; i++) {
kv_refill_hex64_klen(priv->tmp, next(gen), priv->klen, NULL, 0);
const u64 t0 = time_nsec();
kvmap_kv_iter_seek(api, iter, priv->tmp);
api->iter_skip(iter, nscan);
const u64 dt = time_diff_nsec(t0);
latency_add(info->vctr, dt);
}
api->iter_destroy(iter);
}
static void *
kvmap_worker(void * const ptr)
{
struct forker_worker_info * const info = (typeof(info))ptr;
srandom_u64(info->seed);
const char op = info->argv[0][0];
typeof(kvmap_batch_pro) * batch_func = NULL;
switch (op) {
case 'p': batch_func = kvmap_batch_pro; break;
case 'g': batch_func = kvmap_batch_get; break;
case 's': batch_func = kvmap_batch_put; break;
case 'S': batch_func = kvmap_batch_put_par; break;
case 'd': batch_func = kvmap_batch_del; break;
case 'n': batch_func = kvmap_batch_seek_next; break;
case 'k': batch_func = kvmap_batch_seek_skip; break;
default: debug_die(); break;
}
struct priv p;
p.klen = a2u32(info->argv[1]);
p.vlen = a2u32(info->argv[2]); // vlen/nscan
const struct kvmap_api * const api = info->passdata[0];
p.ref = kvmap_ref(api, info->passdata[1]);
const u64 outlen = sizeof(struct kv) + p.klen + p.vlen + 4096;
p.tmp = yalloc(outlen);
debug_assert(p.tmp);
memset(p.tmp, 0, outlen);
p.out = yalloc(outlen);
debug_assert(p.out);
if (info->end_type == FORKER_END_TIME) {
do {
batch_func(info, &p, 1lu << 14); // batch size
} while (time_nsec() < info->end_magic);
} else if (info->end_type == FORKER_END_COUNT) {
batch_func(info, &p, info->end_magic);
}
kvmap_unref(api, p.ref);
free(p.out);
free(p.tmp);
return NULL;
}
#define NARGS ((3))
static void
dbtest_help_message(void)
{
fprintf(stderr, "%s Usage: {api ... {rgen ... {pass ...}}}\n", __func__);
kvmap_api_helper_message();
forker_passes_message();
fprintf(stderr, "%s dbtest wargs[%d]: <sSdgpnk> <klen> <vlen/nscan>\n", __func__, NARGS);
fprintf(stderr, "%s s:set S:load d:del g:get p:probe n:seeknext k:seekskip\n", __func__);
}
static int
test_kvmap(const int argc, char ** const argv)
{
const struct kvmap_api * api = NULL;
void * map = NULL;
const int n1 = kvmap_api_helper(argc, argv, NULL, &api, &map);
if (n1 < 0)
return n1;
char *pref[64] = {};
memcpy(pref, argv, sizeof(pref[0]) * (size_t)n1);
struct pass_info pi = {};
pi.passdata[0] = (void *)api;
pi.passdata[1] = map;
pi.vctr_size = VCTRSZ;
pi.wf = kvmap_worker;
pi.af = kvmap_analyze;
const int n2 = forker_passes(argc - n1, argv + n1, pref, &pi, NARGS);
if (api->fprint)
api->fprint(map, stderr);
api->destroy(map);
if (n2 < 0) {
return n2;
} else {
return n1 + n2;
}
}
int
main(int argc, char ** argv)
{
if (argc < 3) {
dbtest_help_message();
exit(0);
}
const bool r = forker_main(argc - 1, argv + 1, test_kvmap);
if (r == false)
dbtest_help_message();
return 0;
}