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
516
517
518
519
520
521
522
523
524
525
/*
 * Copyright 2016-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.
 */

/*
 * array.c -- example of arrays usage
 */

#include <ex_common.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/stat.h>
#include <libpmemobj.h>

#define TOID_ARRAY(x) TOID(x)
#define COUNT_OF(x) (sizeof(x) / sizeof(x[0]))
#define MAX_BUFFLEN 30
#define MAX_TYPE_NUM 8

POBJ_LAYOUT_BEGIN(array);
POBJ_LAYOUT_TOID(array, struct array_elm);
POBJ_LAYOUT_TOID(array, int);
POBJ_LAYOUT_TOID(array, PMEMoid);
POBJ_LAYOUT_TOID(array, TOID(struct array_elm));
POBJ_LAYOUT_TOID(array, struct array_info);
POBJ_LAYOUT_END(array);

static PMEMobjpool *pop;
enum array_types {
	UNKNOWN_ARRAY_TYPE,
	INT_ARRAY_TYPE,
	PMEMOID_ARRAY_TYPE,
	TOID_ARRAY_TYPE,
	MAX_ARRAY_TYPE
};

struct array_elm {
	int id;
};

struct array_info {
	char name[MAX_BUFFLEN];
	size_t size;
	enum array_types type;
	PMEMoid array;
};

/*
 * print_usage -- print general usage
 */
static void
print_usage(void)
{
	printf("usage: ./array <file-name> "
		"<alloc|realloc|free|print>"
		" <array-name> [<size> [<TOID|PMEMoid|int>]]\n");
}

/*
 * get type -- parse argument given as type of array
 */
static enum array_types
get_type(const char *type_name)
{
	const char *names[MAX_ARRAY_TYPE] = {"", "int", "PMEMoid", "TOID"};
	enum array_types type;
	for (type = (enum array_types)(MAX_ARRAY_TYPE - 1);
			type > UNKNOWN_ARRAY_TYPE;
			type = (enum array_types)(type - 1)) {
		if (strcmp(names[type], type_name) == 0)
			break;
	}
	if (type == UNKNOWN_ARRAY_TYPE)
		fprintf(stderr, "unknown type: %s\n", type_name);
	return type;
}

/*
 * find_aray -- return info about array with proper name
 */
static TOID(struct array_info)
find_array(const char *name)
{
	TOID(struct array_info) info;
	POBJ_FOREACH_TYPE(pop, info) {
		if (strncmp(D_RO(info)->name, name, MAX_BUFFLEN) == 0)
			return info;
	}
	return TOID_NULL(struct array_info);
}

/*
 * elm_constructor -- constructor of array_elm type object
 */
static int
elm_constructor(PMEMobjpool *pop, void *ptr, void *arg)
{
	struct array_elm *obj = (struct array_elm *)ptr;
	int *id = (int *)arg;
	obj->id = *id;
	pmemobj_persist(pop, obj, sizeof(*obj));

	return 0;
}

/*
 * print_int -- print array of int type
 */
static void
print_int(struct array_info *info)
{
	TOID(int) array;
	TOID_ASSIGN(array, info->array);
	for (size_t i = 0; i < info->size; i++)
		printf("%d ", D_RO(array)[i]);
}

/*
 * print_pmemoid -- print array of PMEMoid type
 */
static void
print_pmemoid(struct array_info *info)
{
	TOID(PMEMoid) array;
	TOID(struct array_elm) elm;
	TOID_ASSIGN(array, info->array);
	for (size_t i = 0; i < info->size; i++) {
		TOID_ASSIGN(elm, D_RW(array)[i]);
		printf("%d ", D_RO(elm)->id);
	}
}

/*
 * print_toid -- print array of TOID(struct array_elm) type
 */
static void
print_toid(struct array_info *info)
{
	TOID_ARRAY(TOID(struct array_elm)) array;
	TOID_ASSIGN(array, info->array);
	for (size_t i = 0; i < info->size; i++)
		printf("%d ", D_RO(D_RO(array)[i])->id);
}

typedef void (*fn_print)(struct array_info *info);
static fn_print print_array[] = {NULL, print_int, print_pmemoid, print_toid};

/*
 * free_int -- de-allocate array of int type
 */
static void
free_int(struct array_info *info)
{
	TOID(int) array;
	TOID_ASSIGN(array, info->array);
	/*
	 * When there is persistent array of simple type allocated,
	 * there is enough to de-allocate persistent pointer
	 */
	POBJ_FREE(&array);
}

/*
 * free_pmemoid -- de-allocate array of PMEMoid type
 */
static void
free_pmemoid(struct array_info *info)
{
	TOID(PMEMoid) array;
	TOID_ASSIGN(array, info->array);
	/*
	 * When there is persistent array of persistent pointer type allocated,
	 * there is necessary to de-allocate each element, if they were
	 * allocated earlier
	 */
	for (size_t i = 0; i < info->size; i++)
		pmemobj_free(&D_RW(array)[i]);
	POBJ_FREE(&array);
}

/*
 * free_toid -- de-allocate array of TOID(struct array_elm) type
 */
static void
free_toid(struct array_info *info)
{
	TOID_ARRAY(TOID(struct array_elm)) array;
	TOID_ASSIGN(array, info->array);
	/*
	 * When there is persistent array of persistent pointer type allocated,
	 * there is necessary to de-allocate each element, if they were
	 * allocated earlier
	 */
	for (size_t i = 0; i < info->size; i++)
		POBJ_FREE(&D_RW(array)[i]);
	POBJ_FREE(&array);
}

typedef void (*fn_free)(struct array_info *info);
static fn_free free_array[] = {NULL, free_int, free_pmemoid, free_toid};

/*
 * realloc_int -- reallocate array of int type
 */
static PMEMoid
realloc_int(PMEMoid *info, size_t prev_size, size_t size)
{
	TOID(int) array;
	TOID_ASSIGN(array, *info);

	POBJ_REALLOC(pop, &array, int, size * sizeof(int));
	for (size_t i = prev_size; i < size; i++)
			D_RW(array)[i] = (int)i;
	return array.oid;
}

/*
 * realloc_pmemoid -- reallocate array of PMEMoid type
 */
static PMEMoid
realloc_pmemoid(PMEMoid *info, size_t prev_size, size_t size)
{
	TOID(PMEMoid) array;
	TOID_ASSIGN(array, *info);
	pmemobj_zrealloc(pop, &array.oid, sizeof(PMEMoid) * size,
							TOID_TYPE_NUM(PMEMoid));

	for (size_t i = prev_size; i < size; i++) {
		if (pmemobj_alloc(pop, &D_RW(array)[i],
			sizeof(struct array_elm), TOID_TYPE_NUM(PMEMoid),
							elm_constructor, &i)) {
			fprintf(stderr, "pmemobj_alloc\n");
			assert(0);
		}
	}
	return array.oid;
}

/*
 * realloc_toid -- reallocate array of TOID(struct array_elm) type
 */
static PMEMoid
realloc_toid(PMEMoid *info, size_t prev_size, size_t size)
{
	TOID_ARRAY(TOID(struct array_elm)) array;
	TOID_ASSIGN(array, *info);
	pmemobj_zrealloc(pop, &array.oid,
			sizeof(TOID(struct array_elm)) * size,
			TOID_TYPE_NUM_OF(array));
	for (size_t i = prev_size; i < size; i++) {
		POBJ_NEW(pop, &D_RW(array)[i], struct array_elm,
						elm_constructor, &i);
		if (TOID_IS_NULL(D_RW(array)[i])) {
			fprintf(stderr, "POBJ_ALLOC\n");
			assert(0);
		}
	}
	return array.oid;
}

typedef PMEMoid (*fn_realloc)(PMEMoid *info, size_t prev_size, size_t size);
static fn_realloc realloc_array[] =
	{NULL, realloc_int, realloc_pmemoid, realloc_toid};

/*
 * alloc_int -- allocate array of int type
 */
static PMEMoid
alloc_int(size_t size)
{
	TOID(int) array;
	/*
	 * To allocate persistent array of simple type is enough to allocate
	 * pointer with size equal to number of elements multiplied by size of
	 * user-defined structure.
	 */
	POBJ_ALLOC(pop, &array, int, sizeof(int) * size,
						NULL, NULL);
	if (TOID_IS_NULL(array)) {
		fprintf(stderr, "POBJ_ALLOC\n");
		return OID_NULL;
	}

	for (size_t i = 0; i < size; i++)
		D_RW(array)[i] = (int)i;
	pmemobj_persist(pop, D_RW(array), size * sizeof(*D_RW(array)));
	return array.oid;
}

/*
 * alloc_pmemoid -- allocate array of PMEMoid type
 */
static PMEMoid
alloc_pmemoid(size_t size)
{
	TOID(PMEMoid) array;
	/*
	 * To allocate persistent array of PMEMoid type is necessary to allocate
	 * pointer with size equal to number of elements multiplied by size of
	 * PMEMoid and to allocate each of elements separately.
	 */
	POBJ_ALLOC(pop, &array, PMEMoid, sizeof(PMEMoid) * size,
					NULL, NULL);
	if (TOID_IS_NULL(array)) {
		fprintf(stderr, "POBJ_ALLOC\n");
		return OID_NULL;
	}

	for (size_t i = 0; i < size; i++) {
		if (pmemobj_alloc(pop, &D_RW(array)[i],
			sizeof(struct array_elm),
			TOID_TYPE_NUM(PMEMoid), elm_constructor, &i)) {
			fprintf(stderr, "pmemobj_alloc\n");
		}
	}

	return array.oid;
}

/*
 * alloc_toid -- allocate array of TOID(struct array_elm) type
 */
static PMEMoid
alloc_toid(size_t size)
{
	TOID_ARRAY(TOID(struct array_elm)) array;
	/*
	 * To allocate persistent array of TOID with user-defined structure type
	 * is necessary to allocate pointer with size equal to number of
	 * elements multiplied by size of TOID of proper type and to allocate
	 * each of elements separately.
	 */
	POBJ_ALLOC(pop, &array, TOID(struct array_elm),
			sizeof(TOID(struct array_elm)) * size, NULL, NULL);

	if (TOID_IS_NULL(array)) {
		fprintf(stderr, "POBJ_ALLOC\n");
		return OID_NULL;
	}

	for (size_t i = 0; i < size; i++) {
		POBJ_NEW(pop, &D_RW(array)[i], struct array_elm,
						elm_constructor, &i);
		if (TOID_IS_NULL(D_RW(array)[i])) {
			fprintf(stderr, "POBJ_ALLOC\n");
			assert(0);
		}
	}
	return array.oid;
}

typedef PMEMoid (*fn_alloc)(size_t size);
static fn_alloc alloc_array[] = {NULL, alloc_int, alloc_pmemoid, alloc_toid};

/*
 * do_print -- print values stored by proper array
 */
static void
do_print(int argc, char *argv[])
{
	if (argc != 1) {
		printf("usage: ./array <file-name> print <array-name>\n");
		return;
	}

	TOID(struct array_info) array_info = find_array(argv[0]);
	if (TOID_IS_NULL(array_info)) {
		printf("%s doesn't exist\n", argv[0]);
		return;
	}
	printf("%s:\n", argv[0]);
	print_array[D_RO(array_info)->type](D_RW(array_info));
	printf("\n");
}

/*
 * do_free -- de-allocate proper array and proper TOID of array_info type
 */
static void
do_free(int argc, char *argv[])
{
	if (argc != 1) {
		printf("usage: ./array <file-name> free <array-name>\n");
		return;
	}
	TOID(struct array_info) array_info = find_array(argv[0]);
	if (TOID_IS_NULL(array_info)) {
		printf("%s doesn't exist\n", argv[0]);
		return;
	}
	free_array[D_RO(array_info)->type](D_RW(array_info));
	POBJ_FREE(&array_info);
}

/*
 * do_realloc -- reallocate proper array to given size and update information
 * in array_info structure
 */
static void
do_realloc(int argc, char *argv[])
{
	if (argc != 2) {
		printf("usage: ./array <file-name> realloc"
						" <array-name> <size>\n");
		return;
	}
	size_t size = atoi(argv[1]);
	TOID(struct array_info) array_info = find_array(argv[0]);
	if (TOID_IS_NULL(array_info)) {
		printf("%s doesn't exist\n", argv[0]);
		return;
	}
	struct array_info *info = D_RW(array_info);
	info->array = realloc_array[info->type](&info->array, info->size, size);
	if (OID_IS_NULL(info->array)) {
		if (size != 0)
			printf("POBJ_REALLOC\n");
	}
	info->size = size;
	pmemobj_persist(pop, info, sizeof(*info));
}

/*
 * do_alloc -- allocate persistent array and TOID of array_info type
 * and set it with information about new array
 */
static void
do_alloc(int argc, char *argv[])
{
	if (argc != 3) {
		printf("usage: ./array <file-name> alloc <array-name>"
							"<size> <type>\n");
		return;
	}
	enum array_types type = get_type(argv[2]);
	if (type == UNKNOWN_ARRAY_TYPE)
		return;
	size_t size = atoi(argv[1]);
	TOID(struct array_info) array_info = find_array(argv[0]);
	if (!TOID_IS_NULL(array_info))
		POBJ_FREE(&array_info);
	POBJ_ZNEW(pop, &array_info, struct array_info);
	struct array_info *info = D_RW(array_info);
	strncpy(info->name, argv[0], MAX_BUFFLEN);
	info->name[MAX_BUFFLEN - 1] = '\0';
	info->size = size;
	info->type = type;
	info->array = alloc_array[type](size);
	if (OID_IS_NULL(info->array))
		assert(0);
	pmemobj_persist(pop, info, sizeof(*info));
}

typedef void (*fn_op)(int argc, char *argv[]);
static fn_op operations[] = {do_alloc, do_realloc, do_free, do_print};

int
main(int argc, char *argv[])
{
	if (argc < 3) {
		print_usage();
		return 1;
	}

	const char *path = argv[1];

	pop = NULL;

	if (file_exists(path) != 0) {
		if ((pop = pmemobj_create(path, POBJ_LAYOUT_NAME(array),
			PMEMOBJ_MIN_POOL, CREATE_MODE_RW)) == NULL) {
			printf("failed to create pool\n");
			return 1;
		}
	} else {
		if ((pop = pmemobj_open(path, POBJ_LAYOUT_NAME(array)))
								== NULL) {
			printf("failed to open pool\n");
			return 1;
		}
	}
	const char *option = argv[2];
	argv += 3;
	argc -= 3;
	const char *names[] = {"alloc", "realloc", "free", "print"};
	int i = 0;
	for (; i < COUNT_OF(names) && strcmp(option, names[i]) != 0; i++);

	if (i != COUNT_OF(names))
		operations[i](argc, argv);
	else
		print_usage();
	pmemobj_close(pop);
	return 0;
}