-
Notifications
You must be signed in to change notification settings - Fork 509
/
obj_pmalloc_mt.c
477 lines (403 loc) · 10.9 KB
/
obj_pmalloc_mt.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2015-2023, Intel Corporation */
/*
* obj_pmalloc_mt.c -- multithreaded test of allocator
*/
#include <stdint.h>
#include "file.h"
#include "obj.h"
#include "pmalloc.h"
#include "sys_util.h"
#include "unittest.h"
#define MAX_THREADS 32
#define MAX_OPS_PER_THREAD 1000
#define ALLOC_SIZE 104
#define REALLOC_SIZE (ALLOC_SIZE * 3)
#define MIX_RERUNS 2
#define CHUNKSIZE (1 << 18)
#define CHUNKS_PER_THREAD 3
static unsigned Threads;
static unsigned Ops_per_thread;
static unsigned Tx_per_thread;
struct action {
struct pobj_action pact;
os_mutex_t lock;
os_cond_t cond;
};
struct root {
uint64_t offs[MAX_THREADS][MAX_OPS_PER_THREAD];
struct action actions[MAX_THREADS][MAX_OPS_PER_THREAD];
};
struct worker_args {
PMEMobjpool *pop;
struct root *r;
unsigned idx;
};
static void *
alloc_worker(void *arg)
{
struct worker_args *a = arg;
for (unsigned i = 0; i < Ops_per_thread; ++i) {
pmalloc(a->pop, &a->r->offs[a->idx][i], ALLOC_SIZE, 0, 0);
UT_ASSERTne(a->r->offs[a->idx][i], 0);
}
return NULL;
}
static void *
realloc_worker(void *arg)
{
struct worker_args *a = arg;
int ret;
for (unsigned i = 0; i < Ops_per_thread; ++i) {
ret = prealloc(a->pop, &a->r->offs[a->idx][i], REALLOC_SIZE,
0, 0);
UT_ASSERTne(a->r->offs[a->idx][i], 0);
UT_ASSERTeq(ret, 0);
}
return NULL;
}
static void *
free_worker(void *arg)
{
struct worker_args *a = arg;
for (unsigned i = 0; i < Ops_per_thread; ++i) {
pfree(a->pop, &a->r->offs[a->idx][i]);
UT_ASSERTeq(a->r->offs[a->idx][i], 0);
}
return NULL;
}
static void *
mix_worker(void *arg)
{
struct worker_args *a = arg;
/*
* The mix scenario is ran twice to increase the chances of run
* contention.
*/
for (unsigned j = 0; j < MIX_RERUNS; ++j) {
for (unsigned i = 0; i < Ops_per_thread; ++i) {
pmalloc(a->pop, &a->r->offs[a->idx][i],
ALLOC_SIZE, 0, 0);
UT_ASSERTne(a->r->offs[a->idx][i], 0);
}
for (unsigned i = 0; i < Ops_per_thread; ++i) {
pfree(a->pop, &a->r->offs[a->idx][i]);
UT_ASSERTeq(a->r->offs[a->idx][i], 0);
}
}
return NULL;
}
static void *
tx_worker(void *arg)
{
struct worker_args *a = arg;
PMEMoid oid;
/*
* Allocate objects until exhaustion, once that happens the transaction
* will automatically abort and all of the objects will be freed.
*/
TX_BEGIN(a->pop) {
for (unsigned n = 0; ; ++n) { /* this is NOT an infinite loop */
oid = pmemobj_tx_alloc(ALLOC_SIZE, a->idx);
UT_ASSERT(!OID_IS_NULL(oid));
if (Ops_per_thread != MAX_OPS_PER_THREAD &&
n == Ops_per_thread) {
pmemobj_tx_abort(0);
}
}
} TX_END
return NULL;
}
static void *
tx3_worker(void *arg)
{
struct worker_args *a = arg;
PMEMoid oid;
/*
* Allocate N objects, abort, repeat M times. Should reveal issues in
* transaction abort handling.
*/
for (unsigned n = 0; n < Tx_per_thread; ++n) {
TX_BEGIN(a->pop) {
for (unsigned i = 0; i < Ops_per_thread; ++i) {
oid = pmemobj_tx_alloc(ALLOC_SIZE, a->idx);
UT_ASSERT(!OID_IS_NULL(oid));
}
pmemobj_tx_abort(EINVAL);
} TX_END
}
return NULL;
}
static void *
alloc_free_worker(void *arg)
{
struct worker_args *a = arg;
PMEMoid oid;
for (unsigned i = 0; i < Ops_per_thread; ++i) {
int err = pmemobj_alloc(a->pop, &oid, ALLOC_SIZE,
0, NULL, NULL);
UT_ASSERTeq(err, 0);
pmemobj_free(&oid);
}
return NULL;
}
#define OPS_PER_TX 10
#define STEP 8
#define TEST_LANES 4
static void *
tx2_worker(void *arg)
{
struct worker_args *a = arg;
for (unsigned n = 0; n < Tx_per_thread; ++n) {
PMEMoid oids[OPS_PER_TX];
TX_BEGIN(a->pop) {
for (int i = 0; i < OPS_PER_TX; ++i) {
oids[i] = pmemobj_tx_alloc(ALLOC_SIZE, a->idx);
for (unsigned j = 0; j < ALLOC_SIZE;
j += STEP) {
pmemobj_tx_add_range(oids[i], j, STEP);
}
}
} TX_END
TX_BEGIN(a->pop) {
for (int i = 0; i < OPS_PER_TX; ++i)
pmemobj_tx_free(oids[i]);
} TX_ONABORT {
UT_ASSERT(0);
} TX_END
}
return NULL;
}
static void *
action_cancel_worker(void *arg)
{
struct worker_args *a = arg;
PMEMoid oid;
for (unsigned i = 0; i < Ops_per_thread; ++i) {
unsigned arr_id = a->idx / 2;
struct action *act = &a->r->actions[arr_id][i];
if (a->idx % 2 == 0) {
os_mutex_lock(&act->lock);
oid = pmemobj_reserve(a->pop,
&act->pact, ALLOC_SIZE, 0);
UT_ASSERT(!OID_IS_NULL(oid));
os_cond_signal(&act->cond);
os_mutex_unlock(&act->lock);
} else {
os_mutex_lock(&act->lock);
while (act->pact.heap.offset == 0)
os_cond_wait(&act->cond, &act->lock);
pmemobj_cancel(a->pop, &act->pact, 1);
os_mutex_unlock(&act->lock);
}
}
return NULL;
}
static void *
action_publish_worker(void *arg)
{
struct worker_args *a = arg;
PMEMoid oid;
for (unsigned i = 0; i < Ops_per_thread; ++i) {
unsigned arr_id = a->idx / 2;
struct action *act = &a->r->actions[arr_id][i];
if (a->idx % 2 == 0) {
os_mutex_lock(&act->lock);
oid = pmemobj_reserve(a->pop,
&act->pact, ALLOC_SIZE, 0);
UT_ASSERT(!OID_IS_NULL(oid));
os_cond_signal(&act->cond);
os_mutex_unlock(&act->lock);
} else {
os_mutex_lock(&act->lock);
while (act->pact.heap.offset == 0)
os_cond_wait(&act->cond, &act->lock);
pmemobj_publish(a->pop, &act->pact, 1);
os_mutex_unlock(&act->lock);
}
}
return NULL;
}
static void *
action_mix_worker(void *arg)
{
struct worker_args *a = arg;
PMEMoid oid;
for (unsigned i = 0; i < Ops_per_thread; ++i) {
unsigned arr_id = a->idx / 2;
unsigned publish = i % 2;
struct action *act = &a->r->actions[arr_id][i];
if (a->idx % 2 == 0) {
os_mutex_lock(&act->lock);
oid = pmemobj_reserve(a->pop,
&act->pact, ALLOC_SIZE, 0);
UT_ASSERT(!OID_IS_NULL(oid));
os_cond_signal(&act->cond);
os_mutex_unlock(&act->lock);
} else {
os_mutex_lock(&act->lock);
while (act->pact.heap.offset == 0)
os_cond_wait(&act->cond, &act->lock);
if (publish)
pmemobj_publish(a->pop, &act->pact, 1);
else
pmemobj_cancel(a->pop, &act->pact, 1);
os_mutex_unlock(&act->lock);
}
pmemobj_persist(a->pop, act, sizeof(*act));
}
return NULL;
}
static void
actions_clear(PMEMobjpool *pop, struct root *r)
{
for (unsigned i = 0; i < Threads; ++i) {
for (unsigned j = 0; j < Ops_per_thread; ++j) {
struct action *a = &r->actions[i][j];
util_mutex_destroy(&a->lock);
util_mutex_init(&a->lock);
util_cond_destroy(&a->cond);
util_cond_init(&a->cond);
memset(&a->pact, 0, sizeof(a->pact));
pmemobj_persist(pop, a, sizeof(*a));
}
}
}
static void
run_worker(void *(worker_func)(void *arg), struct worker_args args[])
{
os_thread_t t[MAX_THREADS];
for (unsigned i = 0; i < Threads; ++i)
THREAD_CREATE(&t[i], NULL, worker_func, &args[i]);
for (unsigned i = 0; i < Threads; ++i)
THREAD_JOIN(&t[i], NULL);
}
int
main(int argc, char *argv[])
{
START(argc, argv, "obj_pmalloc_mt");
if (argc < 5)
UT_FATAL(
"usage: %s <threads> <ops/t> <tx/t> <file> [enable stats]",
argv[0]);
PMEMobjpool *pop;
unsigned enable_stats = 0;
size_t allocPre, alloc, allocPost;
Threads = ATOU(argv[1]);
if (Threads > MAX_THREADS)
UT_FATAL("Threads %d > %d", Threads, MAX_THREADS);
Ops_per_thread = ATOU(argv[2]);
if (Ops_per_thread > MAX_OPS_PER_THREAD)
UT_FATAL("Ops per thread %d > %d", Threads, MAX_THREADS);
Tx_per_thread = ATOU(argv[3]);
int exists = util_file_exists(argv[4]);
if (exists < 0)
UT_FATAL("!util_file_exists");
if (!exists) {
pop = pmemobj_create(argv[4], "TEST", (PMEMOBJ_MIN_POOL) +
(MAX_THREADS * CHUNKSIZE * CHUNKS_PER_THREAD),
0666);
if (pop == NULL)
UT_FATAL("!pmemobj_create");
} else {
pop = pmemobj_open(argv[4], "TEST");
if (pop == NULL)
UT_FATAL("!pmemobj_open");
}
if (argc > 5)
enable_stats = ATOU(argv[5]);
if (enable_stats) {
int ret = pmemobj_ctl_set(pop, "stats.enabled", &enable_stats);
UT_ASSERTeq(ret, 0);
}
PMEMoid oid = pmemobj_root(pop, sizeof(struct root));
struct root *r = pmemobj_direct(oid);
UT_ASSERTne(r, NULL);
int ret = pmemobj_ctl_get(pop, "stats.heap.curr_allocated", &allocPre);
UT_ASSERTeq(ret, 0);
struct worker_args args[MAX_THREADS];
for (unsigned i = 0; i < Threads; ++i) {
args[i].pop = pop;
args[i].r = r;
args[i].idx = i;
for (unsigned j = 0; j < Ops_per_thread; ++j) {
struct action *a = &r->actions[i][j];
util_mutex_init(&a->lock);
util_cond_init(&a->cond);
}
}
alloc = allocPre;
if (enable_stats)
alloc += Ops_per_thread * Threads * ((ALLOC_SIZE / 128) + 1)
* 128;
run_worker(alloc_worker, args);
ret = pmemobj_ctl_get(pop, "stats.heap.curr_allocated", &allocPost);
UT_ASSERTeq(alloc, allocPost);
if (enable_stats) {
alloc -= Ops_per_thread * Threads * ((ALLOC_SIZE / 128) + 1)
* 128;
alloc += Ops_per_thread * Threads * ((REALLOC_SIZE / 128) + 1)
* 128;
}
run_worker(realloc_worker, args);
ret = pmemobj_ctl_get(pop, "stats.heap.curr_allocated", &allocPost);
UT_ASSERTeq(alloc, allocPost);
alloc = allocPre;
run_worker(free_worker, args);
ret = pmemobj_ctl_get(pop, "stats.heap.curr_allocated", &allocPost);
UT_ASSERTeq(alloc, allocPost);
run_worker(mix_worker, args);
ret = pmemobj_ctl_get(pop, "stats.heap.curr_allocated", &allocPost);
UT_ASSERTeq(alloc, allocPost);
run_worker(alloc_free_worker, args);
ret = pmemobj_ctl_get(pop, "stats.heap.curr_allocated", &allocPost);
UT_ASSERTeq(alloc, allocPost);
run_worker(action_cancel_worker, args);
actions_clear(pop, r);
ret = pmemobj_ctl_get(pop, "stats.heap.curr_allocated", &allocPost);
UT_ASSERTeq(alloc, allocPost);
if (enable_stats && Threads > 1)
alloc += Ops_per_thread / 2 * Threads
* ((ALLOC_SIZE / 128) + 1) * 128;
run_worker(action_publish_worker, args);
actions_clear(pop, r);
ret = pmemobj_ctl_get(pop, "stats.heap.curr_allocated", &allocPost);
UT_ASSERTeq(alloc, allocPost);
if (enable_stats && Threads > 1)
alloc += Ops_per_thread / 4 * Threads
* ((ALLOC_SIZE / 128) + 1) * 128;
run_worker(action_mix_worker, args);
ret = pmemobj_ctl_get(pop, "stats.heap.curr_allocated", &allocPost);
UT_ASSERTeq(alloc, allocPost);
if (enable_stats) {
enable_stats = 0;
ret = pmemobj_ctl_set(pop, "stats.enabled", &enable_stats);
UT_ASSERTeq(ret, 0);
}
/*
* Reduce the number of lanes to a value smaller than the number of
* threads. This will ensure that at least some of the state of the lane
* will be shared between threads. Doing this might reveal bugs related
* to runtime race detection instrumentation.
*/
unsigned old_nlanes = pop->lanes_desc.runtime_nlanes;
pop->lanes_desc.runtime_nlanes = TEST_LANES;
run_worker(tx2_worker, args);
pop->lanes_desc.runtime_nlanes = old_nlanes;
/*
* This workload might create many allocation classes due to pvector,
* keep it last.
*/
if (Threads == MAX_THREADS) /* don't run for short tests */
run_worker(tx_worker, args);
run_worker(tx3_worker, args);
pmemobj_close(pop);
DONE(NULL);
}
#ifdef _MSC_VER
/*
* Since libpmemobj is linked statically, we need to invoke its ctor/dtor.
*/
MSVC_CONSTR(libpmemobj_init)
MSVC_DESTR(libpmemobj_fini)
#endif