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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
/*
 * 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.
 */

/*
 * pmemobj_atomic_lists.cpp -- benchmark for pmemobj atomic list API
 */

#include "benchmark.hpp"
#include "libpmemobj.h"
#include "queue.h"
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>

#define FACTOR 8
#define LAYOUT_NAME "benchmark"

struct obj_bench;
struct obj_worker;
struct element;

TOID_DECLARE(struct item, 0);
TOID_DECLARE(struct list, 1);

typedef size_t (*fn_type_num_t)(size_t worker_idx, size_t op_idx);

typedef struct element (*fn_position_t)(struct obj_worker *obj_worker,
					size_t op_idx);

typedef int (*fn_init_t)(struct worker_info *worker, size_t n_elm,
			 size_t list_len);
/*
 * args -- stores command line parsed arguments.
 */
struct obj_list_args {
	char *type_num;    /* type_number mode - one, per-thread, rand */
	char *position;    /* position - head, tail, middle, rand */
	unsigned list_len; /* initial list length */
	bool queue;	/* use circle queue from <sys/queue.h> */
	bool range;	/* use random allocation size */
	unsigned min_size; /* minimum random allocation size */
	unsigned seed;     /* seed value */
};

/*
 * obj_bench -- stores variables used in benchmark, passed within functions.
 */
static struct obj_bench {

	/* handle to persistent pool */
	PMEMobjpool *pop;

	/* pointer to benchmark specific arguments */
	struct obj_list_args *args;

	/* array to store random type_number values */
	size_t *random_types;

	/*
	 * fn_rpositions array stores random functions returning proper element
	 * from list, if position where operation is performed is random.
	 * Possible function which can be in array are:
	 *	- position_head,
	 *	- position_tail,
	 *	- position_middle.
	 */
	size_t *alloc_sizes; /* array to store random sizes of each object */
	size_t max_len;      /* maximum list length */
	size_t min_len;      /* initial list length */
	int type_mode;       /* type_number mode */
	int position_mode;   /* list destination mode */

	/*
	 * fn_type_num gets proper function assigned, depending on the
	 * value of the type_mode argument, which returns proper type number for
	 * each persistent object. Possible functions are:
	 *	- type_mode_one,
	 *	- type_mode_per_thread,
	 *	- type_mode_rand.
	 */
	fn_type_num_t fn_type_num;

	/*
	 * fn_position gets proper function assigned, depending  on the value
	 * of the position argument, which returns handle to proper element on
	 * the list. Possible functions are:
	 *	- position_head,
	 *	- position_tail,
	 *	- position_middle,
	 *	- position_rand.
	 */
	fn_position_t fn_position;

	/*
	 * fn_init gets proper function assigned, depending on the file_io
	 * flag, which allocates objects and initializes proper list. Possible
	 * functions are:
	 *	- obj_init_list,
	 *	- queue_init_list.
	 */
	fn_init_t fn_init;
} obj_bench;

/*
 * item -- structure used to connect elements in lists.
 */
struct item {
	POBJ_LIST_ENTRY(struct item) field;
	CIRCLEQ_ENTRY(item) fieldq;
};

/*
 * element -- struct contains one item from list with proper type.
 */
struct element {
	struct item *itemq;
	TOID(struct item) itemp;
	bool before;
};

/*
 * obj_worker -- stores variables used by one thread, concerning one list.
 */
struct obj_worker {

	/* head of the pmemobj list */
	POBJ_LIST_HEAD(plist, struct item) head;

	/* head of the circular queue */
	CIRCLEQ_HEAD(qlist, item) headq;
	TOID(struct item) * oids;    /* persistent pmemobj list elements */
	struct item **items;	 /* volatile elements */
	size_t n_elm;		     /* number of elements in array */
	fn_position_t *fn_positions; /* element access functions */
	struct element elm;	  /* pointer to current element */
				     /*
				      * list_move is a pointer to structure storing variables used by
				      * second list (used only for obj_move benchmark).
				      */
	struct obj_worker *list_move;
};

/*
 * position_mode -- list destination type
 */
enum position_mode {
	/* object inserted/removed/moved to/from head of list */
	POSITION_MODE_HEAD,

	/* object inserted/removed/moved to/from tail of list */
	POSITION_MODE_TAIL,

	/*
	 * object inserted/removed/moved to/from second element of the list
	 * or to/from head if list length equal to one
	 */
	POSITION_MODE_MIDDLE,

	/* object inserted/removed/moved to/from head, tail or middle */
	POSITION_MODE_RAND,
	POSITION_MODE_UNKNOWN,
};

/*
 * type_mode -- type number type
 */
enum type_mode {
	TYPE_MODE_ONE, /* one type number for all of objects */

	/* one type number for objects allocated by the same thread */
	TYPE_MODE_PER_THREAD,
	TYPE_MODE_RAND, /* random type number for each object */
	TYPE_MODE_UNKNOWN,
};

/*
 * position_head -- returns head of the persistent list or volatile queue.
 */
static struct element
position_head(struct obj_worker *obj_worker, size_t op_idx)
{
	struct element head = {0, OID_NULL, false};
	head.before = true;
	if (!obj_bench.args->queue)
		head.itemp = POBJ_LIST_FIRST(&obj_worker->head);
	else
		head.itemq = CIRCLEQ_FIRST(&obj_worker->headq);
	return head;
}

/*
 * position_tail -- returns tail of the persistent list or volatile queue.
 */
static struct element
position_tail(struct obj_worker *obj_worker, size_t op_idx)
{
	struct element tail = {0, OID_NULL, false};
	tail.before = false;
	if (!obj_bench.args->queue)
		tail.itemp = POBJ_LIST_LAST(&obj_worker->head, field);
	else
		tail.itemq = CIRCLEQ_LAST(&obj_worker->headq);
	return tail;
}

/*
 * position_middle -- returns second or first element from the persistent list
 * or volatile queue.
 */
static struct element
position_middle(struct obj_worker *obj_worker, size_t op_idx)
{
	struct element elm = position_head(obj_worker, op_idx);
	elm.before = true;
	if (!obj_bench.args->queue)
		elm.itemp = POBJ_LIST_NEXT(elm.itemp, field);
	else
		elm.itemq = CIRCLEQ_NEXT(elm.itemq, fieldq);
	return elm;
}

/*
 * position_rand -- returns first, second or last element from the persistent
 * list or volatile queue based on r_positions array.
 */
static struct element
position_rand(struct obj_worker *obj_worker, size_t op_idx)
{
	struct element elm;
	elm = obj_worker->fn_positions[op_idx](obj_worker, op_idx);
	elm.before = true;
	return elm;
}

/*
 * type_mode_one -- always returns 0, as in the mode TYPE_MODE_ONE
 * all of the persistent objects have the same type_number value.
 */
static size_t
type_mode_one(size_t worker_idx, size_t op_idx)
{
	return 0;
}

/*
 * type_mode_per_thread -- always returns the index of the worker,
 * as in the TYPE_MODE_PER_THREAD the value of the persistent object
 * type_number is specific to the thread.
 */
static size_t
type_mode_per_thread(size_t worker_idx, size_t op_idx)
{
	return worker_idx;
}

/*
 * type_mode_rand -- returns the value from the random_types array assigned
 * for the specific operation in a specific thread.
 */
static size_t
type_mode_rand(size_t worker_idx, size_t op_idx)
{
	return obj_bench.random_types[op_idx];
}

const char *type_num_names[] = {"one", "per-thread", "rand"};
const char *position_names[] = {"head", "tail", "middle", "rand"};
static fn_type_num_t type_num_modes[] = {type_mode_one, type_mode_per_thread,
					 type_mode_rand};
static fn_position_t positions[] = {position_head, position_tail,
				    position_middle, position_rand};

/* function pointers randomly picked when using rand mode */
static fn_position_t rand_positions[] = {position_head, position_tail,
					 position_middle};

/*
 * get_item -- common part of initial operation of the all benchmarks.
 * It gets pointer to element on the list where object will
 * be inserted/removed/moved to/from.
 */
static void
get_item(struct benchmark *bench, struct operation_info *info)
{
	struct obj_worker *obj_worker = (struct obj_worker *)info->worker->priv;
	obj_worker->elm = obj_bench.fn_position(obj_worker, info->index);
}

/*
 * get_move_item -- special part of initial operation of the obj_move
 * benchmarks. It gets pointer to element on the list where object will be
 * inserted/removed/moved to/from.
 */
static int
get_move_item(struct benchmark *bench, struct operation_info *info)
{
	struct obj_worker *obj_worker = (struct obj_worker *)info->worker->priv;
	obj_worker->list_move->elm =
		obj_bench.fn_position(obj_worker->list_move, info->index);

	get_item(bench, info);

	return 0;
}

/*
 * parse_args -- parse command line string argument
 */
static int
parse_args(char *arg, int max, const char **names)
{
	int i = 0;
	for (; i < max && strcmp(names[i], arg) != 0; i++)
		;
	if (i == max)
		fprintf(stderr, "Invalid argument\n");
	return i;
}

/*
 * obj_init_list -- special part of worker initialization, performed only if
 * queue flag set false. Allocates proper number of items, and inserts proper
 * part of them to the pmemobj list.
 */
static int
obj_init_list(struct worker_info *worker, size_t n_oids, size_t list_len)
{
	size_t i;
	struct obj_worker *obj_worker = (struct obj_worker *)worker->priv;
	obj_worker->oids =
		(TOID(struct item) *)calloc(n_oids, sizeof(TOID(struct item)));
	if (obj_worker->oids == NULL) {
		perror("calloc");
		return -1;
	}
	for (i = 0; i < n_oids; i++) {
		size_t type_num = obj_bench.fn_type_num(worker->index, i);
		size_t size = obj_bench.alloc_sizes[i];
		PMEMoid *tmp = (PMEMoid *)&obj_worker->oids[i];
		if (pmemobj_alloc(obj_bench.pop, tmp, size, type_num, NULL,
				  NULL) != 0)
			goto err_oids;
	}
	for (i = 0; i < list_len; i++)
		POBJ_LIST_INSERT_TAIL(obj_bench.pop, &obj_worker->head,
				      obj_worker->oids[i], field);
	return 0;
err_oids:
	for (; i > 0; i--)
		POBJ_FREE(&obj_worker->oids[i - 1]);
	free(obj_worker->oids);
	return -1;
}

/*
 * queue_init_list -- special part of worker initialization, performed only if
 * queue flag set. Initiates circle queue, allocates proper number of items and
 * inserts proper part of them to the queue.
 */
static int
queue_init_list(struct worker_info *worker, size_t n_items, size_t list_len)
{
	size_t i;
	struct obj_worker *obj_worker = (struct obj_worker *)worker->priv;
	CIRCLEQ_INIT(&obj_worker->headq);
	obj_worker->items =
		(struct item **)malloc(n_items * sizeof(struct item *));
	if (obj_worker->items == NULL) {
		perror("malloc");
		return -1;
	}

	for (i = 0; i < n_items; i++) {
		size_t size = obj_bench.alloc_sizes[i];
		obj_worker->items[i] = (struct item *)calloc(1, size);
		if (obj_worker->items[i] == NULL) {
			perror("calloc");
			goto err;
		}
	}

	for (i = 0; i < list_len; i++)
		CIRCLEQ_INSERT_TAIL(&obj_worker->headq, obj_worker->items[i],
				    fieldq);

	return 0;
err:
	for (; i > 0; i--)
		free(obj_worker->items[i - 1]);
	free(obj_worker->items);
	return -1;
}

/*
 * queue_free_worker_list -- special part for the worker de-initialization when
 * queue flag is true. Releases items directly from atomic list.
 */
static void
queue_free_worker_list(struct obj_worker *obj_worker)
{
	while (!CIRCLEQ_EMPTY(&obj_worker->headq)) {
		struct item *tmp = CIRCLEQ_LAST(&obj_worker->headq);
		CIRCLEQ_REMOVE(&obj_worker->headq, tmp, fieldq);
		free(tmp);
	}
	free(obj_worker->items);
}

/*
 * obj_free_worker_list -- special part for the worker de-initialization when
 * queue flag is false. Releases items directly from atomic list.
 */
static void
obj_free_worker_list(struct obj_worker *obj_worker)
{
	while (!POBJ_LIST_EMPTY(&obj_worker->head)) {
		TOID(struct item) tmp = POBJ_LIST_FIRST(&obj_worker->head);
		POBJ_LIST_REMOVE_FREE(obj_bench.pop, &obj_worker->head, tmp,
				      field);
	}
	free(obj_worker->oids);
}

/*
 * obj_free_worker_items -- special part for the worker de-initialization when
 * queue flag is false. Releases items used for create pmemobj list.
 */
static void
obj_free_worker_items(struct obj_worker *obj_worker)
{
	for (size_t i = 0; i < obj_worker->n_elm; i++)
		POBJ_FREE(&obj_worker->oids[i]);
	free(obj_worker->oids);
}

/*
 * queue_free_worker_items -- special part for the worker de-initialization
 * when queue flag set. Releases used for create circle queue.
 */
static void
queue_free_worker_items(struct obj_worker *obj_worker)
{
	for (size_t i = 0; i < obj_worker->n_elm; i++)
		free(obj_worker->items[i]);
	free(obj_worker->items);
}

/*
 * random_positions -- allocates array and calculates random values for
 * defining positions where each operation will be performed. Used only
 * in POSITION_MODE_RAND
 */
static fn_position_t *
random_positions(void)
{
	fn_position_t *positions = (fn_position_t *)calloc(
		obj_bench.max_len, sizeof(fn_position_t));
	if (positions == NULL) {
		perror("calloc");
		return NULL;
	}

	if (obj_bench.args->seed != 0)
		srand(obj_bench.args->seed);

	size_t rmax = ARRAY_SIZE(rand_positions);
	for (size_t i = 0; i < obj_bench.max_len; i++) {
		size_t id = RRAND(rmax, 0);
		positions[i] = rand_positions[id];
	}

	return positions;
}

/*
 * rand_values -- allocates array and if range mode calculates random
 * values as allocation sizes for each object otherwise populates whole array
 * with max value. Used only when range flag set.
 */
static size_t *
random_values(size_t min, size_t max, size_t n_ops, size_t min_range)
{
	size_t *randoms = (size_t *)calloc(n_ops, sizeof(size_t));
	if (randoms == NULL) {
		perror("calloc");
		return NULL;
	}
	for (size_t i = 0; i < n_ops; i++)
		randoms[i] = max;
	if (min > min_range) {
		if (min > max) {
			fprintf(stderr, "Invalid size\n");
			free(randoms);
			return NULL;
		}
		for (size_t i = 0; i < n_ops; i++)
			randoms[i] = RRAND(max, min);
	}
	return randoms;
}
/*
 * queue_insert_op -- main operations of the obj_insert benchmark when queue
 * flag set to true.
 */
static int
queue_insert_op(struct operation_info *info)
{
	struct obj_worker *obj_worker = (struct obj_worker *)info->worker->priv;
	CIRCLEQ_INSERT_AFTER(&obj_worker->headq, obj_worker->elm.itemq,
			     obj_worker->items[info->index + obj_bench.min_len],
			     fieldq);
	return 0;
}

/*
 * obj_insert_op -- main operations of the obj_insert benchmark when queue flag
 * set to false.
 */
static int
obj_insert_op(struct operation_info *info)
{
	struct obj_worker *obj_worker = (struct obj_worker *)info->worker->priv;
	POBJ_LIST_INSERT_AFTER(
		obj_bench.pop, &obj_worker->head, obj_worker->elm.itemp,
		obj_worker->oids[info->index + obj_bench.min_len], field);
	return 0;
}

/*
 * queue_remove_op -- main operations of the obj_remove benchmark when queue
 * flag set to true.
 */
static int
queue_remove_op(struct operation_info *info)
{
	struct obj_worker *obj_worker = (struct obj_worker *)info->worker->priv;
	CIRCLEQ_REMOVE(&obj_worker->headq, obj_worker->elm.itemq, fieldq);
	return 0;
}

/*
 * obj_remove_op -- main operations of the obj_remove benchmark when queue flag
 * set to false.
 */
static int
obj_remove_op(struct operation_info *info)
{
	struct obj_worker *obj_worker = (struct obj_worker *)info->worker->priv;
	POBJ_LIST_REMOVE(obj_bench.pop, &obj_worker->head,
			 obj_worker->elm.itemp, field);
	return 0;
}

/*
 * insert_op -- main operations of the obj_insert benchmark.
 */
static int
insert_op(struct benchmark *bench, struct operation_info *info)
{
	get_item(bench, info);

	return obj_bench.args->queue ? queue_insert_op(info)
				     : obj_insert_op(info);
}

/*
 * obj_insert_new_op -- main operations of the obj_insert_new benchmark.
 */
static int
obj_insert_new_op(struct benchmark *bench, struct operation_info *info)
{
	get_item(bench, info);

	struct obj_worker *obj_worker = (struct obj_worker *)info->worker->priv;
	PMEMoid tmp;
	size_t size = obj_bench.alloc_sizes[info->index];
	size_t type_num =
		obj_bench.fn_type_num(info->worker->index, info->index);
	tmp = pmemobj_list_insert_new(
		obj_bench.pop, offsetof(struct item, field), &obj_worker->head,
		obj_worker->elm.itemp.oid, obj_worker->elm.before, size,
		type_num, NULL, NULL);

	if (OID_IS_NULL(tmp)) {
		perror("pmemobj_list_insert_new");
		return -1;
	}

	return 0;
}

/*
 * remove_op -- main operations of the obj_remove benchmark.
 */
static int
remove_op(struct benchmark *bench, struct operation_info *info)
{
	get_item(bench, info);

	return obj_bench.args->queue ? queue_remove_op(info)
				     : obj_remove_op(info);
}

/*
 * obj_remove_free_op -- main operation of the obj_remove_free benchmark.
 */
static int
obj_remove_free_op(struct benchmark *bench, struct operation_info *info)
{
	get_item(bench, info);

	struct obj_worker *obj_worker = (struct obj_worker *)info->worker->priv;
	POBJ_LIST_REMOVE_FREE(obj_bench.pop, &obj_worker->head,
			      obj_worker->elm.itemp, field);
	return 0;
}

/*
 * obj_move_op -- main operation of the obj_move benchmark.
 */
static int
obj_move_op(struct benchmark *bench, struct operation_info *info)
{
	if (get_move_item(bench, info))
		return -1;

	struct obj_worker *obj_worker = (struct obj_worker *)info->worker->priv;
	POBJ_LIST_MOVE_ELEMENT_BEFORE(obj_bench.pop, &obj_worker->head,
				      &obj_worker->list_move->head,
				      obj_worker->list_move->elm.itemp,
				      obj_worker->elm.itemp, field, field);
	return 0;
}

/*
 * free_worker -- free common worker state
 */
static void
free_worker(struct obj_worker *obj_worker)
{
	if (obj_bench.position_mode == POSITION_MODE_RAND)
		free(obj_worker->fn_positions);
	free(obj_worker);
}

/*
 * free_worker_list -- worker de-initialization function for: obj_insert_new,
 * obj_remove_free, obj_move. Requires releasing objects directly from list.
 */
static void
free_worker_list(struct benchmark *bench, struct benchmark_args *args,
		 struct worker_info *worker)
{
	struct obj_worker *obj_worker = (struct obj_worker *)worker->priv;
	obj_bench.args->queue ? queue_free_worker_list(obj_worker)
			      : obj_free_worker_list(obj_worker);
	free_worker(obj_worker);
}

/*
 * obj_free_worker_items -- worker de-initialization function of obj_insert and
 * obj_remove benchmarks, where deallocation can't be performed directly on the
 * list and where is possibility of using queue flag.
 */
static void
free_worker_items(struct benchmark *bench, struct benchmark_args *args,
		  struct worker_info *worker)
{
	struct obj_worker *obj_worker = (struct obj_worker *)worker->priv;
	struct obj_list_args *obj_args = (struct obj_list_args *)args->opts;
	obj_args->queue ? queue_free_worker_items(obj_worker)
			: obj_free_worker_items(obj_worker);
	free_worker(obj_worker);
}

/*
 * obj_move_free_worker -- special part for the worker de-initialization
 * function of obj_move benchmarks.
 */
static void
obj_move_free_worker(struct benchmark *bench, struct benchmark_args *args,
		     struct worker_info *worker)
{
	struct obj_worker *obj_worker = (struct obj_worker *)worker->priv;
	while (!POBJ_LIST_EMPTY(&obj_worker->list_move->head))
		POBJ_LIST_REMOVE_FREE(
			obj_bench.pop, &obj_worker->list_move->head,
			POBJ_LIST_LAST(&obj_worker->list_move->head, field),
			field);

	if (obj_bench.position_mode == POSITION_MODE_RAND)
		free(obj_worker->list_move->fn_positions);
	free(obj_worker->list_move);
	free_worker_list(bench, args, worker);
}

/*
 * obj_init_worker -- common part for the worker initialization for:
 * obj_insert, obj_insert_new, obj_remove obj_remove_free and obj_move.
 */
static int
obj_init_worker(struct worker_info *worker, size_t n_elm, size_t list_len)
{
	struct obj_worker *obj_worker =
		(struct obj_worker *)calloc(1, sizeof(struct obj_worker));
	if (obj_worker == NULL) {
		perror("calloc");
		return -1;
	}

	worker->priv = obj_worker;
	obj_worker->n_elm = obj_bench.max_len;
	obj_worker->list_move = NULL;
	if (obj_bench.position_mode == POSITION_MODE_RAND) {
		obj_worker->fn_positions = random_positions();
		if (obj_worker->fn_positions == NULL)
			goto err;
	}
	if (obj_bench.fn_init(worker, n_elm, list_len) != 0)
		goto err_positions;

	return 0;
err_positions:
	free(obj_worker->fn_positions);
err:
	free(obj_worker);
	return -1;
}

/*
 * obj_insert_init_worker -- worker initialization functions of the obj_insert
 * benchmark.
 */
static int
obj_insert_init_worker(struct benchmark *bench, struct benchmark_args *args,
		       struct worker_info *worker)
{
	return obj_init_worker(worker, obj_bench.max_len, obj_bench.min_len);
}

/*
 * obj_insert_new_init_worker -- worker initialization functions of the
 * obj_insert_new benchmark.
 */
static int
obj_insert_new_init_worker(struct benchmark *bench, struct benchmark_args *args,
			   struct worker_info *worker)
{
	return obj_init_worker(worker, obj_bench.min_len, obj_bench.min_len);
}

/*
 * obj_remove_init_worker -- worker initialization functions of the obj_remove
 * and obj_remove_free benchmarks.
 */
static int
obj_remove_init_worker(struct benchmark *bench, struct benchmark_args *args,
		       struct worker_info *worker)
{
	return obj_init_worker(worker, obj_bench.max_len, obj_bench.max_len);
}

/*
 * obj_move_init_worker -- worker initialization functions of the obj_move
 * benchmark.
 */
static int
obj_move_init_worker(struct benchmark *bench, struct benchmark_args *args,
		     struct worker_info *worker)
{
	if (obj_init_worker(worker, obj_bench.max_len, obj_bench.max_len) != 0)
		return -1;

	struct obj_worker *obj_worker = (struct obj_worker *)worker->priv;
	obj_worker->list_move =
		(struct obj_worker *)calloc(1, sizeof(struct obj_worker));
	if (obj_worker->list_move == NULL) {
		perror("calloc");
		goto free;
	}
	size_t i;
	if (obj_bench.position_mode == POSITION_MODE_RAND) {
		obj_worker->list_move->fn_positions = random_positions();
		if (obj_worker->list_move->fn_positions == NULL)
			goto free_list_move;
	}
	for (i = 0; i < obj_bench.min_len; i++) {
		size_t size = obj_bench.alloc_sizes[i];
		POBJ_LIST_INSERT_NEW_TAIL(obj_bench.pop,
					  &obj_worker->list_move->head, field,
					  size, NULL, NULL);
		if (TOID_IS_NULL(POBJ_LIST_LAST(&obj_worker->list_move->head,
						field))) {
			perror("pmemobj_list_insert_new");
			goto free_all;
		}
	}
	return 0;
free_all:
	for (; i > 0; i--) {
		POBJ_LIST_REMOVE_FREE(
			obj_bench.pop, &obj_worker->list_move->head,
			POBJ_LIST_LAST(&obj_worker->list_move->head, field),
			field);
	}
	free(obj_worker->list_move->fn_positions);
free_list_move:
	free(obj_worker->list_move);
free:
	free_worker_list(bench, args, worker);
	return -1;
}

/*
 * obj_init - common part of the benchmark initialization for: obj_insert,
 * obj_insert_new, obj_remove, obj_remove_free and obj_move used in their init
 * functions. Parses command line arguments, sets variables and
 * creates persistent pool.
 */
static int
obj_init(struct benchmark *bench, struct benchmark_args *args)
{
	assert(bench != NULL);
	assert(args != NULL);
	assert(args->opts != NULL);

	obj_bench.args = (struct obj_list_args *)args->opts;
	obj_bench.min_len = obj_bench.args->list_len + 1;
	obj_bench.max_len = args->n_ops_per_thread + obj_bench.min_len;

	obj_bench.fn_init =
		obj_bench.args->queue ? queue_init_list : obj_init_list;
	/* Decide if use random or state allocation sizes */
	size_t obj_size = args->dsize < sizeof(struct item)
		? sizeof(struct item)
		: args->dsize;
	size_t min_size = obj_bench.args->min_size < sizeof(struct item)
		? sizeof(struct item)
		: obj_bench.args->min_size;
	obj_bench.alloc_sizes = random_values(
		min_size, obj_size, obj_bench.max_len, sizeof(struct item));
	if (obj_bench.alloc_sizes == NULL)
		goto free_random_types;

	/* Decide where operations will be performed */
	obj_bench.position_mode =
		parse_args(obj_bench.args->position, POSITION_MODE_UNKNOWN,
			   position_names);
	if (obj_bench.position_mode == POSITION_MODE_UNKNOWN)
		goto free_all;

	obj_bench.fn_position = positions[obj_bench.position_mode];
	if (!obj_bench.args->queue) {
		/* Decide what type number will be used */
		obj_bench.type_mode =
			parse_args(obj_bench.args->type_num, TYPE_MODE_UNKNOWN,
				   type_num_names);
		if (obj_bench.type_mode == TYPE_MODE_UNKNOWN)
			return -1;

		obj_bench.fn_type_num = type_num_modes[obj_bench.type_mode];
		if (obj_bench.type_mode == TYPE_MODE_RAND) {
			obj_bench.random_types = random_values(
				1, UINT32_MAX, obj_bench.max_len, 0);
			if (obj_bench.random_types == NULL)
				return -1;
		}
		/*
		 * Multiplication by FACTOR prevents from out of memory error
		 * as the actual size of the allocated persistent objects
		 * is always larger than requested.
		 */
		size_t psize =
			(args->n_ops_per_thread + obj_bench.min_len + 1) *
			obj_size * args->n_threads * FACTOR;
		if (args->is_poolset) {
			if (args->fsize < psize) {
				fprintf(stderr, "insufficient size "
						"of poolset\n");
				goto free_all;
			}

			psize = 0;
		} else {
			if (psize < PMEMOBJ_MIN_POOL)
				psize = PMEMOBJ_MIN_POOL;
		}

		/* Create pmemobj pool. */
		if ((obj_bench.pop = pmemobj_create(args->fname, LAYOUT_NAME,
						    psize, args->fmode)) ==
		    NULL) {
			perror(pmemobj_errormsg());
			goto free_all;
		}
	}
	return 0;
free_all:
	free(obj_bench.alloc_sizes);
free_random_types:
	if (obj_bench.type_mode == TYPE_MODE_RAND)
		free(obj_bench.random_types);
	return -1;
}

/*
 * obj_exit -- common part for the exit function for: obj_insert,
 * obj_insert_new, obj_remove, obj_remove_free and obj_move used in their exit
 * functions.
 */
static int
obj_exit(struct benchmark *bench, struct benchmark_args *args)
{
	if (!obj_bench.args->queue) {
		pmemobj_close(obj_bench.pop);
		if (obj_bench.type_mode == TYPE_MODE_RAND)
			free(obj_bench.random_types);
	}
	free(obj_bench.alloc_sizes);
	return 0;
}

/* obj_list_clo -- array defining common command line arguments. */
static struct benchmark_clo obj_list_clo[6];

static struct benchmark_info obj_insert;
static struct benchmark_info obj_remove;
static struct benchmark_info obj_insert_new;
static struct benchmark_info obj_remove_free;
static struct benchmark_info obj_move;

CONSTRUCTOR(pmem_atomic_list_costructor)
void
pmem_atomic_list_costructor(void)
{
	obj_list_clo[0].opt_short = 'T';
	obj_list_clo[0].opt_long = "type-number";
	obj_list_clo[0].descr = "Type number mode - one, per-thread, "
				"rand";
	obj_list_clo[0].def = "one";
	obj_list_clo[0].off = clo_field_offset(struct obj_list_args, type_num);
	obj_list_clo[0].type = CLO_TYPE_STR;

	obj_list_clo[1].opt_short = 'P';
	obj_list_clo[1].opt_long = "position";
	obj_list_clo[1].descr = "Place where operation will be "
				"performed - head, tail, rand, middle";
	obj_list_clo[1].def = "middle";
	obj_list_clo[1].off = clo_field_offset(struct obj_list_args, position);
	obj_list_clo[1].type = CLO_TYPE_STR;

	obj_list_clo[2].opt_short = 'l';
	obj_list_clo[2].opt_long = "list-len";
	obj_list_clo[2].type = CLO_TYPE_UINT;
	obj_list_clo[2].descr = "Initial list len";
	obj_list_clo[2].off = clo_field_offset(struct obj_list_args, list_len);
	obj_list_clo[2].def = "1";
	obj_list_clo[2].type_uint.size =
		clo_field_size(struct obj_list_args, list_len);
	obj_list_clo[2].type_uint.base = CLO_INT_BASE_DEC | CLO_INT_BASE_HEX;
	obj_list_clo[2].type_uint.min = 1;
	obj_list_clo[2].type_uint.max = ULONG_MAX;

	obj_list_clo[3].opt_short = 'm';
	obj_list_clo[3].opt_long = "min-size";
	obj_list_clo[3].type = CLO_TYPE_UINT;
	obj_list_clo[3].descr = "Min allocation size";
	obj_list_clo[3].off = clo_field_offset(struct obj_list_args, min_size);
	obj_list_clo[3].def = "0";
	obj_list_clo[3].type_uint.size =
		clo_field_size(struct obj_list_args, min_size);
	obj_list_clo[3].type_uint.base = CLO_INT_BASE_DEC;
	obj_list_clo[3].type_uint.min = 0;
	obj_list_clo[3].type_uint.max = UINT_MAX;

	obj_list_clo[4].opt_short = 's';
	obj_list_clo[4].type_uint.max = INT_MAX;
	obj_list_clo[4].opt_long = "seed";
	obj_list_clo[4].type = CLO_TYPE_UINT;
	obj_list_clo[4].descr = "Seed value";
	obj_list_clo[4].off = clo_field_offset(struct obj_list_args, seed);
	obj_list_clo[4].def = "0";
	obj_list_clo[4].type_uint.size =
		clo_field_size(struct obj_list_args, seed);
	obj_list_clo[4].type_uint.base = CLO_INT_BASE_DEC;
	obj_list_clo[4].type_uint.min = 0;

	/*
	 * nclos field in benchmark_info structures is decremented to make
	 * queue option available only for obj_isert, obj_remove
	 */
	obj_list_clo[5].opt_short = 'q';
	obj_list_clo[5].opt_long = "queue";
	obj_list_clo[5].descr = "Use circleq from queue.h instead "
				"pmemobj";
	obj_list_clo[5].type = CLO_TYPE_FLAG;
	obj_list_clo[5].off = clo_field_offset(struct obj_list_args, queue);

	obj_insert.name = "obj_insert";
	obj_insert.brief = "pmemobj_list_insert() benchmark";
	obj_insert.init = obj_init;
	obj_insert.exit = obj_exit;
	obj_insert.multithread = true;
	obj_insert.multiops = true;
	obj_insert.init_worker = obj_insert_init_worker;
	obj_insert.free_worker = free_worker_items;
	obj_insert.operation = insert_op;
	obj_insert.measure_time = true;
	obj_insert.clos = obj_list_clo;
	obj_insert.nclos = ARRAY_SIZE(obj_list_clo);
	obj_insert.opts_size = sizeof(struct obj_list_args);
	obj_insert.rm_file = true;
	obj_insert.allow_poolset = true;

	REGISTER_BENCHMARK(obj_insert);

	obj_remove.name = "obj_remove";
	obj_remove.brief = "pmemobj_list_remove() benchmark "
			   "without freeing element";
	obj_remove.init = obj_init;
	obj_remove.exit = obj_exit;
	obj_remove.multithread = true;
	obj_remove.multiops = true;
	obj_remove.init_worker = obj_remove_init_worker;
	obj_remove.free_worker = free_worker_items;
	obj_remove.operation = remove_op;
	obj_remove.measure_time = true;
	obj_remove.clos = obj_list_clo;
	obj_remove.nclos = ARRAY_SIZE(obj_list_clo);
	obj_remove.opts_size = sizeof(struct obj_list_args);
	obj_remove.rm_file = true;
	obj_remove.allow_poolset = true;

	REGISTER_BENCHMARK(obj_remove);

	obj_insert_new.name = "obj_insert_new";
	obj_insert_new.brief = "pmemobj_list_insert_new() benchmark";
	obj_insert_new.init = obj_init;
	obj_insert_new.exit = obj_exit;
	obj_insert_new.multithread = true;
	obj_insert_new.multiops = true;
	obj_insert_new.init_worker = obj_insert_new_init_worker;
	obj_insert_new.free_worker = free_worker_list;
	obj_insert_new.operation = obj_insert_new_op;
	obj_insert_new.measure_time = true;
	obj_insert_new.clos = obj_list_clo;
	obj_insert_new.nclos = ARRAY_SIZE(obj_list_clo) - 1;
	obj_insert_new.opts_size = sizeof(struct obj_list_args);
	obj_insert_new.rm_file = true;
	obj_insert_new.allow_poolset = true;
	REGISTER_BENCHMARK(obj_insert_new);

	obj_remove_free.name = "obj_remove_free";
	obj_remove_free.brief = "pmemobj_list_remove() benchmark "
				"with freeing element";
	obj_remove_free.init = obj_init;
	obj_remove_free.exit = obj_exit;
	obj_remove_free.multithread = true;
	obj_remove_free.multiops = true;
	obj_remove_free.init_worker = obj_remove_init_worker;
	obj_remove_free.free_worker = free_worker_list;
	obj_remove_free.operation = obj_remove_free_op;
	obj_remove_free.measure_time = true;
	obj_remove_free.clos = obj_list_clo;
	obj_remove_free.nclos = ARRAY_SIZE(obj_list_clo) - 1;
	obj_remove_free.opts_size = sizeof(struct obj_list_args);
	obj_remove_free.rm_file = true;
	obj_remove_free.allow_poolset = true;
	REGISTER_BENCHMARK(obj_remove_free);

	obj_move.name = "obj_move";
	obj_move.brief = "pmemobj_list_move() benchmark";
	obj_move.init = obj_init;
	obj_move.exit = obj_exit;
	obj_move.multithread = true;
	obj_move.multiops = true;
	obj_move.init_worker = obj_move_init_worker;
	obj_move.free_worker = obj_move_free_worker;
	obj_move.operation = obj_move_op;
	obj_move.measure_time = true;
	obj_move.clos = obj_list_clo;
	obj_move.nclos = ARRAY_SIZE(obj_list_clo) - 1;
	obj_move.opts_size = sizeof(struct obj_list_args);
	obj_move.rm_file = true;
	obj_move.allow_poolset = true;
	REGISTER_BENCHMARK(obj_move);
}