nvml-sys 0.0.6

A low-level FFI wrapper around the Persistent Memory Development Kit, PMDK (formerly NVML) and its libraries, including libpmem, libpmemobj and others. Currently tracks master after version 1.3.1.
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 * Copyright 2015-2017, Intel Corporation
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *
 *      * 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.
 *
 *      * Neither the name of the copyright holder nor the names of its
 *        contributors may be used to endorse or promote products derived
 *        from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "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 COPYRIGHT
 * OWNER OR CONTRIBUTORS 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.
 */

/*
 * obj_pmalloc.cpp -- pmalloc benchmarks definition
 */

#include <cassert>
#include <cerrno>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>

#include "benchmark.hpp"
#include "libpmemobj.h"
#include "os.h"
#include "valgrind_internal.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "memops.h"
#include "pmalloc.h"
#include "redo.h"
#ifdef __cplusplus
}
#endif
/*
 * The factor used for PMEM pool size calculation, accounts for metadata,
 * fragmentation and etc.
 */
#define FACTOR 1.2f

/* The minimum allocation size that pmalloc can perform */
#define ALLOC_MIN_SIZE 64

/* OOB and allocation header size */
#define OOB_HEADER_SIZE 64

/*
 * prog_args - command line parsed arguments
 */
struct prog_args {
	size_t minsize;       /* minimum size for random allocation size */
	bool use_random_size; /* if set, use random size allocations */
	unsigned seed;	/* PRNG seed */
};

POBJ_LAYOUT_BEGIN(pmalloc_layout);
POBJ_LAYOUT_ROOT(pmalloc_layout, struct my_root);
POBJ_LAYOUT_TOID(pmalloc_layout, uint64_t);
POBJ_LAYOUT_END(pmalloc_layout);

/*
 * my_root - root object
 */
struct my_root {
	TOID(uint64_t) offs; /* vector of the allocated object offsets */
};

/*
 * obj_bench - variables used in benchmark, passed within functions
 */
struct obj_bench {
	PMEMobjpool *pop;	  /* persistent pool handle */
	struct prog_args *pa;      /* prog_args structure */
	size_t *sizes;		   /* sizes for allocations */
	TOID(struct my_root) root; /* root object's OID */
	uint64_t *offs;		   /* pointer to the vector of offsets */
};

/*
 * obj_init -- common part of the benchmark initialization for pmalloc and
 * pfree. It allocates the PMEM memory pool and the necessary offset vector.
 */
static int
obj_init(struct benchmark *bench, struct benchmark_args *args)
{
	assert(bench != NULL);
	assert(args != NULL);
	assert(args->opts != NULL);

	if (((struct prog_args *)(args->opts))->minsize >= args->dsize) {
		fprintf(stderr, "Wrong params - allocation size\n");
		return -1;
	}

	struct obj_bench *ob =
		(struct obj_bench *)malloc(sizeof(struct obj_bench));
	if (ob == NULL) {
		perror("malloc");
		return -1;
	}
	pmembench_set_priv(bench, ob);

	ob->pa = (struct prog_args *)args->opts;

	size_t n_ops_total = args->n_ops_per_thread * args->n_threads;
	assert(n_ops_total != 0);

	/* Create pmemobj pool. */
	size_t alloc_size = args->dsize;
	if (alloc_size < ALLOC_MIN_SIZE)
		alloc_size = ALLOC_MIN_SIZE;

	/* For data objects */
	size_t poolsize = PMEMOBJ_MIN_POOL +
		(n_ops_total * (alloc_size + OOB_HEADER_SIZE))
		/* for offsets */
		+ n_ops_total * sizeof(uint64_t);

	/* multiply by FACTOR for metadata, fragmentation, etc. */
	poolsize = (size_t)(poolsize * FACTOR);

	if (args->is_poolset) {
		if (args->fsize < poolsize) {
			fprintf(stderr, "insufficient size of poolset\n");
			goto free_ob;
		}
		poolsize = 0;
	} else {
		if (poolsize < PMEMOBJ_MIN_POOL)
			poolsize = PMEMOBJ_MIN_POOL;
	}

	ob->pop = pmemobj_create(args->fname, POBJ_LAYOUT_NAME(pmalloc_layout),
				 poolsize, args->fmode);
	if (ob->pop == NULL) {
		fprintf(stderr, "%s\n", pmemobj_errormsg());
		goto free_ob;
	}

	ob->root = POBJ_ROOT(ob->pop, struct my_root);
	if (TOID_IS_NULL(ob->root)) {
		fprintf(stderr, "POBJ_ROOT: %s\n", pmemobj_errormsg());
		goto free_pop;
	}

	POBJ_ZALLOC(ob->pop, &D_RW(ob->root)->offs, uint64_t,
		    n_ops_total * sizeof(PMEMoid));
	if (TOID_IS_NULL(D_RW(ob->root)->offs)) {
		fprintf(stderr, "POBJ_ZALLOC off_vect: %s\n",
			pmemobj_errormsg());
		goto free_pop;
	}

	ob->offs = D_RW(D_RW(ob->root)->offs);

	ob->sizes = (size_t *)malloc(n_ops_total * sizeof(size_t));
	if (ob->sizes == NULL) {
		fprintf(stderr, "malloc rand size vect err\n");
		goto free_pop;
	}

	if (ob->pa->use_random_size) {
		size_t width = args->dsize - ob->pa->minsize;
		for (size_t i = 0; i < n_ops_total; i++) {
			uint32_t hr = (uint32_t)os_rand_r(&ob->pa->seed);
			uint32_t lr = (uint32_t)os_rand_r(&ob->pa->seed);
			uint64_t r64 = (uint64_t)hr << 32 | lr;
			ob->sizes[i] = r64 % width + ob->pa->minsize;
		}
	} else {
		for (size_t i = 0; i < n_ops_total; i++)
			ob->sizes[i] = args->dsize;
	}

	return 0;

free_pop:
	pmemobj_close(ob->pop);

free_ob:
	free(ob);
	return -1;
}

/*
 * obj_exit -- common part for the exit function for pmalloc and pfree
 * benchmarks. It frees the allocated offset vector and the memory pool.
 */
static int
obj_exit(struct benchmark *bench, struct benchmark_args *args)
{
	struct obj_bench *ob = (struct obj_bench *)pmembench_get_priv(bench);

	free(ob->sizes);

	POBJ_FREE(&D_RW(ob->root)->offs);
	pmemobj_close(ob->pop);

	return 0;
}

/*
 * pmalloc_init -- initialization for the pmalloc benchmark. Performs only the
 * common initialization.
 */
static int
pmalloc_init(struct benchmark *bench, struct benchmark_args *args)
{
	return obj_init(bench, args);
}

/*
 * pmalloc_op -- actual benchmark operation. Performs the pmalloc allocations.
 */
static int
pmalloc_op(struct benchmark *bench, struct operation_info *info)
{
	struct obj_bench *ob = (struct obj_bench *)pmembench_get_priv(bench);

	uint64_t i = info->index +
		info->worker->index * info->args->n_ops_per_thread;

	int ret = pmalloc(ob->pop, &ob->offs[i], ob->sizes[i], 0, 0);
	if (ret) {
		fprintf(stderr, "pmalloc ret: %d\n", ret);
		return ret;
	}

	return 0;
}

struct pmix_worker {
	size_t nobjects;
	size_t shuffle_start;
	unsigned seed;
};

/*
 * pmix_worker_init -- initialization of the worker structure
 */
static int
pmix_worker_init(struct benchmark *bench, struct benchmark_args *args,
		 struct worker_info *worker)
{
	struct obj_bench *ob = (struct obj_bench *)pmembench_get_priv(bench);
	struct pmix_worker *w = (struct pmix_worker *)calloc(1, sizeof(*w));
	if (w == NULL)
		return -1;

	w->seed = ob->pa->seed;

	worker->priv = w;

	return 0;
}

/*
 * pmix_worker_fini -- destruction of the worker structure
 */
static void
pmix_worker_fini(struct benchmark *bench, struct benchmark_args *args,
		 struct worker_info *worker)
{
	struct pmix_worker *w = (struct pmix_worker *)worker->priv;
	free(w);
}

/*
 * shuffle_objects -- randomly shuffle elements on a list
 *
 * Ideally, we wouldn't count the time this function takes, but for all
 * practial purposes this is fast enough and isn't visible on the results.
 * Just make sure the amount of objects to shuffle is not large.
 */
static void
shuffle_objects(uint64_t *objects, size_t start, size_t nobjects,
		unsigned *seed)
{
	uint64_t tmp;
	size_t dest;
	for (size_t n = start; n < nobjects; ++n) {
		dest = RRAND_R(seed, nobjects - 1, 0);
		tmp = objects[n];
		objects[n] = objects[dest];
		objects[dest] = tmp;
	}
}

#define FREE_PCT 10
#define FREE_OPS 10

/*
 * pmix_op -- mixed workload benchmark
 */
static int
pmix_op(struct benchmark *bench, struct operation_info *info)
{
	struct obj_bench *ob = (struct obj_bench *)pmembench_get_priv(bench);
	struct pmix_worker *w = (struct pmix_worker *)info->worker->priv;

	uint64_t idx = info->worker->index * info->args->n_ops_per_thread;

	uint64_t *objects = &ob->offs[idx];

	if (w->nobjects > FREE_OPS && FREE_PCT > RRAND_R(&w->seed, 100, 0)) {
		shuffle_objects(objects, w->shuffle_start, w->nobjects,
				&w->seed);

		for (int i = 0; i < FREE_OPS; ++i) {
			uint64_t off = objects[--w->nobjects];
			pfree(ob->pop, &off);
		}
		w->shuffle_start = w->nobjects;
	} else {
		int ret = pmalloc(ob->pop, &objects[w->nobjects++],
				  ob->sizes[idx + info->index], 0, 0);
		if (ret) {
			fprintf(stderr, "pmalloc ret: %d\n", ret);
			return ret;
		}
	}

	return 0;
}

/*
 * pmalloc_exit -- the end of the pmalloc benchmark. Frees the memory allocated
 * during pmalloc_op and performs the common exit operations.
 */
static int
pmalloc_exit(struct benchmark *bench, struct benchmark_args *args)
{
	struct obj_bench *ob = (struct obj_bench *)pmembench_get_priv(bench);

	for (size_t i = 0; i < args->n_ops_per_thread * args->n_threads; i++) {
		if (ob->offs[i])
			pfree(ob->pop, &ob->offs[i]);
	}

	return obj_exit(bench, args);
}

/*
 * pfree_init -- initialization for the pfree benchmark. Performs the common
 * initialization and allocates the memory to be freed during pfree_op.
 */
static int
pfree_init(struct benchmark *bench, struct benchmark_args *args)
{
	int ret = obj_init(bench, args);
	if (ret)
		return ret;

	struct obj_bench *ob = (struct obj_bench *)pmembench_get_priv(bench);

	for (size_t i = 0; i < args->n_ops_per_thread * args->n_threads; i++) {
		ret = pmalloc(ob->pop, &ob->offs[i], ob->sizes[i], 0, 0);
		if (ret) {
			fprintf(stderr, "pmalloc at idx %" PRIu64 " ret: %s\n",
				i, pmemobj_errormsg());
			/* free the allocated memory */
			while (i != 0) {
				pfree(ob->pop, &ob->offs[i - 1]);
				i--;
			}
			obj_exit(bench, args);
			return ret;
		}
	}

	return 0;
}

/*
 * pmalloc_op -- actual benchmark operation. Performs the pfree operation.
 */
static int
pfree_op(struct benchmark *bench, struct operation_info *info)
{
	struct obj_bench *ob = (struct obj_bench *)pmembench_get_priv(bench);

	uint64_t i = info->index +
		info->worker->index * info->args->n_ops_per_thread;

	pfree(ob->pop, &ob->offs[i]);

	return 0;
}

/* command line options definition */
static struct benchmark_clo pmalloc_clo[3];
/*
 * Stores information about pmalloc benchmark.
 */
static struct benchmark_info pmalloc_info;
/*
 * Stores information about pfree benchmark.
 */
static struct benchmark_info pfree_info;
/*
 * Stores information about pmix benchmark.
 */
static struct benchmark_info pmix_info;

CONSTRUCTOR(obj_pmalloc_costructor)
void
obj_pmalloc_costructor(void)
{
	pmalloc_clo[0].opt_short = 'r';
	pmalloc_clo[0].opt_long = "random";
	pmalloc_clo[0].descr = "Use random size allocations - "
			       "from min-size to data-size";
	pmalloc_clo[0].off =
		clo_field_offset(struct prog_args, use_random_size);
	pmalloc_clo[0].type = CLO_TYPE_FLAG;

	pmalloc_clo[1].opt_short = 'm';
	pmalloc_clo[1].opt_long = "min-size";
	pmalloc_clo[1].descr = "Minimum size of allocation for "
			       "random mode";
	pmalloc_clo[1].type = CLO_TYPE_UINT;
	pmalloc_clo[1].off = clo_field_offset(struct prog_args, minsize);
	pmalloc_clo[1].def = "1";
	pmalloc_clo[1].type_uint.size =
		clo_field_size(struct prog_args, minsize);
	pmalloc_clo[1].type_uint.base = CLO_INT_BASE_DEC;
	pmalloc_clo[1].type_uint.min = 1;
	pmalloc_clo[1].type_uint.max = UINT64_MAX;

	pmalloc_clo[2].opt_short = 'S';
	pmalloc_clo[2].opt_long = "seed";
	pmalloc_clo[2].descr = "Random mode seed value";
	pmalloc_clo[2].off = clo_field_offset(struct prog_args, seed);
	pmalloc_clo[2].def = "1";
	pmalloc_clo[2].type = CLO_TYPE_UINT;
	pmalloc_clo[2].type_uint.size = clo_field_size(struct prog_args, seed);
	pmalloc_clo[2].type_uint.base = CLO_INT_BASE_DEC;
	pmalloc_clo[2].type_uint.min = 1;
	pmalloc_clo[2].type_uint.max = UINT_MAX;

	pmalloc_info.name = "pmalloc",
	pmalloc_info.brief = "Benchmark for internal pmalloc() "
			     "operation";
	pmalloc_info.init = pmalloc_init;
	pmalloc_info.exit = pmalloc_exit;
	pmalloc_info.multithread = true;
	pmalloc_info.multiops = true;
	pmalloc_info.operation = pmalloc_op;
	pmalloc_info.measure_time = true;
	pmalloc_info.clos = pmalloc_clo;
	pmalloc_info.nclos = ARRAY_SIZE(pmalloc_clo);
	pmalloc_info.opts_size = sizeof(struct prog_args);
	pmalloc_info.rm_file = true;
	pmalloc_info.allow_poolset = true;
	REGISTER_BENCHMARK(pmalloc_info);

	pfree_info.name = "pfree";
	pfree_info.brief = "Benchmark for internal pfree() "
			   "operation";
	pfree_info.init = pfree_init;

	pfree_info.exit = pmalloc_exit; /* same as for pmalloc */
	pfree_info.multithread = true;
	pfree_info.multiops = true;
	pfree_info.operation = pfree_op;
	pfree_info.measure_time = true;
	pfree_info.clos = pmalloc_clo;
	pfree_info.nclos = ARRAY_SIZE(pmalloc_clo);
	pfree_info.opts_size = sizeof(struct prog_args);
	pfree_info.rm_file = true;
	pfree_info.allow_poolset = true;
	REGISTER_BENCHMARK(pfree_info);

	pmix_info.name = "pmix";
	pmix_info.brief = "Benchmark for mixed alloc/free workload";
	pmix_info.init = pmalloc_init;

	pmix_info.exit = pmalloc_exit; /* same as for pmalloc */
	pmix_info.multithread = true;
	pmix_info.multiops = true;
	pmix_info.operation = pmix_op;
	pmix_info.init_worker = pmix_worker_init;
	pmix_info.free_worker = pmix_worker_fini;
	pmix_info.measure_time = true;
	pmix_info.clos = pmalloc_clo;
	pmix_info.nclos = ARRAY_SIZE(pmalloc_clo);
	pmix_info.opts_size = sizeof(struct prog_args);
	pmix_info.rm_file = true;
	pmix_info.allow_poolset = true;
	REGISTER_BENCHMARK(pmix_info);
};