-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathkvt.c
3622 lines (2948 loc) · 95.8 KB
/
kvt.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
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
/* SPDX-License-Identifier: Apache-2.0 OR MIT
*
* SPDX-FileCopyrightText: Copyright 2015 Micron Technology, Inc.
*/
/* Stress + data integrity test tool for HSE KVDB.
*
* kvt creates simple database and continuously transforms it such that
* the integrity of the database can be verified at any time. It performs
* this work in three distinct phases: load, test, and check.
*
* In the load phase all tables are first created and then loaded with keys
* and values which either come from a given list of files or are generated
* by kvt. Each thread in the test phase continuously selects a random
* record, verifies its integrity, and then transforms it to a new stat.
* The check phase simply reads all the records, verifies that they each
* are correct, and afterward verifies that the database as a whole is
* in the expected state.
*
* How to run:
*
* Load: There are several way to load the database.
*
* 1) Load keys from a file or stdin. In this mode the given file names
* are used as the keys to the inodes table, and the first 1MB of file
* data is used as the key's value in its data table:
*
* $ find /var/tmp/ | kvt -f- -v mp0
*
* Specify the -l option to exclude files that don't have at least vlenmin
* bytes, and to limit the value size to at most vlenmax bytes:
*
* 1a) -l 1,100 Exclude zero-length files and use at most 100 bytes
* of the file data for the value
*
* 1b) -l 1048576 Use at most 1MB of the file data for the value
* including zero-length files (this is the default)
*
*
* 2) Load self-generated keys. In this mode kvt will generate keys from
* a default snprintf format or one given on the command line via the -k
* option. The -i option specifies the max number of keys to generate
* (in this case 128 million), and the value will be randomly chosen
* binary data with a random length between 0 and 128 bytes:
*
* $ kvt -i128m -kfoo%lx -v mp0
*
* Specify the -l option to specify each key's value length. For example:
*
* 2a) -l 127 All values will be exactly 127 bytes long (this is the default)
*
* 2b) -l 7,32 Value lengths will be random chosen between 7 and 2 bytes
*
* Specify the "runlen" property to use printable ASCII for the value data:
*
* 2c) -o runlen=7 Data will be random printable ASCII where each
* each run of 7 bytes are identical.
*
* 2d) -o runlen=1 Data will be random printable ASCII data
*
* 2e) -o runlen=0 Data will be random binary data (this is the default)
*
*
* Test
*
* 3) Run the test for 1 hour, performing a full data integrity check
* after the test phase finishes. The default probability that a key
* will be updated is .50, but you can change that with the "updateprob"
* property:
*
* $ kvt -t1h -cv -o updateprob=.20 mp0
*
*
* 4) Run a fully transactional test for 15 minutes, performing a full
* data integrity check after the test finishes:
*
* $ kvt -T15m -cv mp0
*
*
* 5) Like (4), but kill (with SIGKILL) the test at a random time between
* 200 and 400 seconds (i.e., it may or may not kill itself). We run this
* in a loop in order to test kvdb's log replay. We provide an additional
* -c option to perform a full data integrity check both before and after
* the test:
*
* $ while : ; do kvt -T300 -ccv -K9,200,400 mp0; done
*
*
* Check
*
* 6) Verify that all records exist in the tables where we expect to find
* them, and that there are no data integity errors:
*
* $ kvt -cv mp0
*
*
* 7) Like (6), but only check that the rids table and inodes keys are
* intact (does not verify key values hence it cannot verify the data
* integrity of the full database):
*
*
* Misc
*
* You can run all phases in a single command:
*
* $ kvt -i128m -T6h -cv mp0
*
* The default number of jobs (i.e. threads) used in all phases is determined
* the kvt's initial cpu set. However, you can specify the max number of
* jobs to be used in all phases via the -j option, and you may individually
* specify the max number of jobs for both the init and test phases by
* appending it to the option. For example, to use 192 load threads and
* 333 test threads:
*
* $ kvt -i128m,192 -T1h,333 -cv mp0
*
*/
#include <assert.h>
#include <ctype.h>
#include <curses.h>
#include <errno.h>
#include <getopt.h>
#include <math.h>
#include <pthread.h>
#include <signal.h>
#include <stdalign.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <term.h>
#include <unistd.h>
#include <xoroshiro.h>
#include <xxhash.h>
#include <bsd/string.h>
#include <sys/file.h>
#include <sys/param.h>
#include <sys/poll.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <hse/hse.h>
#include <hse/util/arch.h>
#include <hse/tools/parm_groups.h>
/* clang-format off */
#define NELEM(_arr) (sizeof(_arr) / sizeof((_arr)[0]))
#define SCALEPOW2 (1ul << 32)
/* Base table names. Note that both the inodes and data table
* have their index appended to the base name.
*/
#define KVS_RIDS_NAME "rids"
#define KVS_TOMBS_NAME "tombs"
#define KVS_INODES_NAME "inodes"
#define KVS_DATA_NAME "data"
enum kvs_type {
kvs_type_unknown,
kvs_type_rids,
kvs_type_tombs,
kvs_type_inodes,
kvs_type_data
};
/* inodes table record flags
*/
#define KF_ENTOMBED (0x0001)
#ifndef thread_local
#define thread_local _Thread_local
#endif
#ifndef __aligned
#define __aligned(_sz) __attribute__((__aligned__(_sz)))
#endif
#define __read_mostly __attribute__((__section__(".read_mostly")))
#define __unused __attribute__((__unused__))
#ifndef timespecsub
/* From FreeBSD */
#define timespecsub(tsp, usp, vsp) \
do { \
(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
if ((vsp)->tv_nsec < 0) { \
(vsp)->tv_sec--; \
(vsp)->tv_nsec += 1000000000L; \
} \
} while (0)
#endif
struct suftab {
const char *list; /* list of suffix characters */
double mult[]; /* list of multipliers */
};
/* kibibytes, mebibytes, ..., including the dd suffixes b and w.
*/
struct suftab suftab_iec = {
"kmgtpezybw",
{ 0x1p10, 0x1p20, 0x1p30, 0x1p40, 0x1p50, 0x1p60, 0x1p70, 0x1p80, 512, sizeof(int) }
};
/* kilo, mega, giga, ...
*/
struct suftab suftab_si = {
"kmgtpezy",
{ 1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24 }
};
/* seconds, minutes, hours, days, weeks, years, centuries.
*/
struct suftab suftab_time_t = {
"smhdwyc",
{ 1, 60, 3600, 86400, 86400 * 7, 84600 * 365, 86400 * 365 * 100ul, }
};
/* clang-format on */
typedef struct timespec tsi_t;
struct stats {
u_long gets;
u_long puts;
u_long dels;
u_long commits;
u_long aborts;
u_long entombs;
u_long delays;
u_long vgetlen;
u_long vputlen;
u_long latmin;
u_long latmax;
u_long lattot;
u_long iters;
u_long usecs;
u_long nerrs;
} __aligned(64);
struct tdargs {
struct stats stats;
uint64_t hash;
size_t databufsz;
char *databuf;
pthread_t tid;
uint64_t seed;
u_int job;
bool full;
bool dump;
};
struct work {
struct work *next;
u_long rid;
u_long span;
size_t fnlen;
const char *keyfmt;
char fn[HSE_KVS_KEY_LEN_MAX + 1];
};
struct workq {
u_long rid __aligned(64 * 2); /* updated atomically */
bool running __aligned(64);
u_int lwm;
u_int hwm;
char *randbuf;
size_t randbufsz;
pthread_mutex_t mtx __aligned(64);
struct work *head;
struct work **tail;
struct work *free;
u_int cnt;
u_long p_waits __aligned(64);
u_long p_wakeups;
pthread_cond_t p_cv; /* producer */
u_long c_waits __aligned(64);
u_long c_wakeups;
pthread_cond_t c_cv; /* consumer */
};
const u_char
u64tostrtab[] __aligned(64) = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
uint8_t strtou64tab[256] __read_mostly;
struct hse_kvdb *kvdb __read_mostly;
sig_atomic_t sigint __read_mostly;
sig_atomic_t sigusr1 __read_mostly;
sig_atomic_t sigalrm __read_mostly;
size_t ridlockc __read_mostly;
u_int *ridlockv __read_mostly;
u_long ridmax __read_mostly;
u_long kvs_datac __read_mostly;
u_long kvs_inodesc __read_mostly;
struct hse_kvs *kvs_rids __read_mostly;
struct hse_kvs *kvs_tombs __read_mostly;
struct hse_kvs **kvs_inodesv __read_mostly;
struct hse_kvs **kvs_datav __read_mostly;
u_long txncdlyprob __read_mostly;
u_long txnfreeprob __read_mostly;
u_long updateprob __read_mostly;
u_long tombprob __read_mostly;
pthread_barrier_t kvt_test_barrier;
uint64_t hash0;
struct workq workq;
u_long killsecs = ULONG_MAX;
u_long testsecs;
int killsig;
u_int ridkeybase = NELEM(u64tostrtab) - 1;
size_t vlenmax = 127;
size_t vlenmin;
size_t vrunlen;
size_t ridpfxlen;
bool vcomp;
u_int ijobsmax, tjobsmax, cjobsmax;
cpu_set_t cpuset;
// ends in _ because it will conflict with tsc_freq defined in hse/src/util/src/timer.c
u_long tsc_freq_;
char *progname, *mpname;
char *tgs_clrtoeol;
int verbosity;
u_long mark;
bool initchkdups;
bool testtxn;
bool headers;
bool dryrun;
bool human;
bool force;
bool sync_enabled = false;
ulong sync_timeout_ms = 0;
struct parm_groups *pg;
struct svec hse_gparms = { 0 };
struct svec db_oparms = { 0 };
struct svec rids_oparms = { 0 }, rids_cparms = { 0 };
struct svec inodes_oparms = { 0 }, inodes_cparms = { 0 };
struct svec tombs_oparms = { 0 }, tombs_cparms = { 0 };
struct svec data_oparms = { 0 }, data_cparms = { 0 };
struct svec empty_parms = { 0 };
char perfc_buf[32];
int perfc = -1;
static int
kvt_create(const char *mpname, const char *keyfile, const char *keyfmt, u_long keymax, bool *dump);
static int
kvt_open(size_t kvs_listc, char **kvs_listv);
static int
kvt_init(const char *keyfile, const char *keyfmt, u_long keymax, bool dump);
static void *
kvt_init_main(void *arg);
static int
kvt_check(int level, bool dump);
static void *
kvt_check_main(void *arg);
static int
kvt_test(void);
static void *
kvt_test_main(void *arg);
static int
kvt_test_impl(struct tdargs *args, unsigned int flags, struct hse_kvdb_txn *txn, u_long rid);
static thread_local uint64_t xrand64_state[2];
static void
xrand64_init(uint64_t seed)
{
if (seed == 0) {
while (!(seed >> 56))
seed = (seed << 8) | (get_cycles() & 0xfful);
}
xoroshiro128plus_init(xrand64_state, seed);
}
static uint64_t
xrand64(void)
{
return xoroshiro128plus(xrand64_state);
}
/* Start a time stamp interval...
*/
void
tsi_start(tsi_t *tsip)
{
clock_gettime(CLOCK_MONOTONIC, tsip);
}
/* Return the time interval in usecs since the given time stamp...
*/
u_long
tsi_delta(tsi_t *startp)
{
tsi_t now;
clock_gettime(CLOCK_MONOTONIC, &now);
timespecsub(&now, startp, &now);
return now.tv_sec * 1000000 + now.tv_nsec / 1000;
}
static thread_local char dmsg[256], emsg[256];
static thread_local int dmsglen, emsglen;
static thread_local ssize_t dcc, ecc;
static thread_local u_int job = UINT_MAX;
int
dputc(int c)
{
dmsg[dmsglen++] = c;
return c;
}
__attribute__((format(printf, 2, 3))) void
dprint(int lvl, const char *fmt, ...)
{
size_t dmsgsz = sizeof(dmsg) - 8;
va_list ap;
int n;
if (lvl > verbosity)
return;
dmsglen = 0;
if (tgs_clrtoeol)
dmsg[dmsglen++] = '\r';
if (job < UINT_MAX) {
n = snprintf(dmsg, dmsgsz, "%4u ", job);
dmsglen += (n > 0) ? MIN(n, dmsgsz) : 0;
}
va_start(ap, fmt);
n = vsnprintf(dmsg + dmsglen, dmsgsz - dmsglen, fmt, ap);
dmsglen += (n > 0) ? MIN(n, dmsgsz - dmsglen) : 0;
va_end(ap);
if (tgs_clrtoeol)
tputs(tgs_clrtoeol, 1, dputc);
dmsg[dmsglen++] = '\n';
dcc = write(1, dmsg, dmsglen);
}
int
eputc(int c)
{
emsg[emsglen++] = c;
return c;
}
__attribute__((format(printf, 2, 3))) void
eprint(hse_err_t err, const char *fmt, ...)
{
size_t emsgsz = sizeof(emsg) - 8;
va_list ap;
int n;
emsglen = snprintf(emsg, emsgsz, "%s: ", progname);
if (job < UINT_MAX) {
n = snprintf(emsg + emsglen, emsgsz - emsglen, "%4u ", job);
emsglen += (n > 0) ? MIN(n, emsgsz - emsglen) : 0;
}
va_start(ap, fmt);
n = vsnprintf(emsg + emsglen, emsgsz - emsglen, fmt, ap);
emsglen += (n > 0) ? MIN(n, emsgsz - emsglen) : 0;
va_end(ap);
if (err) {
emsglen += strlen(strcat(emsg + emsglen, ": "));
hse_strerror(err, emsg + emsglen, emsgsz - emsglen);
emsglen += strlen(emsg + emsglen);
}
emsglen = MIN(emsglen, emsgsz);
if (tgs_clrtoeol)
tputs(tgs_clrtoeol, 1, eputc);
emsg[emsglen++] = '\n';
ecc = write(2, emsg, emsglen);
}
__attribute__((format(printf, 1, 2))) void
status(const char *fmt, ...)
{
size_t dmsgsz = sizeof(dmsg) - 8;
va_list ap;
int n;
if (verbosity < 1)
return;
dmsglen = 0;
if (tgs_clrtoeol)
dmsg[dmsglen++] = '\r';
va_start(ap, fmt);
n = vsnprintf(dmsg + dmsglen, dmsgsz - dmsglen, fmt, ap);
dmsglen += (n > 0) ? MIN(n, dmsgsz - dmsglen) : 0;
va_end(ap);
if (tgs_clrtoeol) {
tputs(tgs_clrtoeol, 1, dputc);
dmsg[dmsglen++] = '\r';
} else {
dmsg[dmsglen++] = '\n';
}
dcc = write(1, dmsg, dmsglen);
}
void
humanize(u_long *nump, char **sufp)
{
if (*nump >= 10000000ul) {
*nump /= 1000000;
*sufp = "m";
} else if (*nump > 10000ul) {
*nump /= 1000;
*sufp = "k";
}
}
__attribute__((format(printf, 1, 2))) void
syntax(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(emsg, sizeof(emsg), fmt, ap);
va_end(ap);
fprintf(stderr, "%s: %s, use -h for help\n", progname, emsg);
}
uint64_t
strtou64(const void *str, char **endp, u_int base)
{
const uint8_t *p8 = str;
uint64_t acc = 0, val;
while (isspace(*p8))
++p8;
while (*p8) {
val = strtou64tab[*p8];
if (val >= base)
break;
acc *= base;
acc += val;
++p8;
}
if (endp)
*endp = (char *)p8;
return acc;
}
int
u64tostr(void *buf, size_t bufsz, uint64_t num, u_int base)
{
uint64_t val = num;
char *right = buf;
char *left;
int len;
assert(buf && base >= 2 && base < NELEM(u64tostrtab));
do {
uint64_t tmp = val;
val /= base;
*right++ = u64tostrtab[tmp - val * base];
} while (val > 0);
len = right - (char *)buf;
assert(len < bufsz);
*right-- = '\000';
left = buf;
while (left < right) {
char tmp = *right;
*right-- = *left;
*left++ = tmp;
}
assert(base > 36 || num == strtoul(buf, NULL, base));
return len;
}
void
strtou64_init(void)
{
int i;
for (i = 0; i < NELEM(strtou64tab); ++i)
strtou64tab[i] = 255;
for (i = 0; i < NELEM(u64tostrtab); ++i)
strtou64tab[u64tostrtab[i]] = i;
}
u_long
cvt_strtoul(const char *str, char **endp, const struct suftab *suftab)
{
char *pc, *end;
u_long val;
errno = 0;
val = strtoul(str, &end, 0);
if (!errno && end != str && *end && suftab) {
pc = strchr(suftab->list, tolower(*end));
if (pc) {
val *= *(suftab->mult + (pc - suftab->list));
++end;
}
}
if (endp)
*endp = end;
return val;
}
void
sigint_isr(int sig)
{
++sigint;
}
void
sigusr1_isr(int sig)
{
++sigusr1;
}
void
sigalrm_isr(int sig)
{
if (killsig && killsecs <= testsecs && !sigint)
kill(getpid(), killsig);
++sigalrm;
}
/* Reliable signal()...
*/
int
rsignal(int signo, __sighandler_t func)
{
struct sigaction nact;
bzero(&nact, sizeof(nact));
nact.sa_handler = func;
sigemptyset(&nact.sa_mask);
if (SIGALRM == signo || SIGINT == signo) {
#ifdef SA_INTERRUPT
nact.sa_flags |= SA_INTERRUPT;
#endif
} else {
#ifdef SA_RESTART
nact.sa_flags |= SA_RESTART;
#endif
}
return sigaction(signo, &nact, (struct sigaction *)0);
}
int
parm_vec_init(void)
{
const char *txn = testtxn ? "transactions.enabled=true" : "transactions.enabled=false";
const char *cmp = vcomp ? "value.compression.default=on" : "value.compression.default=off";
char rids_pfx[64] = { 0 };
int rc = 0;
if (ridpfxlen)
snprintf(rids_pfx, sizeof(rids_pfx), "prefix.length=%zu", ridpfxlen);
svec_init(&hse_gparms);
svec_init(&db_oparms);
svec_init(&rids_oparms);
svec_init(&data_oparms);
svec_init(&tombs_oparms);
svec_init(&inodes_oparms);
svec_init(&rids_cparms);
svec_init(&data_cparms);
svec_init(&tombs_cparms);
svec_init(&inodes_cparms);
svec_init(&empty_parms);
rc = rc ?: svec_append_pg(&hse_gparms, pg, PG_HSE_GLOBAL, perfc_buf, NULL);
/* kvdb open params */
rc = rc ?: svec_append_pg(&db_oparms, pg, PG_KVDB_OPEN, perfc_buf, NULL);
/* kvs open params: txn setting applies to all, only data and tombs get compression
*/
rc = rc ?: svec_append_pg(&rids_oparms, pg, PG_KVS_OPEN, txn, perfc_buf, NULL);
rc = rc ?: svec_append_pg(&data_oparms, pg, PG_KVS_OPEN, txn, cmp, perfc_buf, NULL);
rc = rc ?: svec_append_pg(&tombs_oparms, pg, PG_KVS_OPEN, txn, cmp, perfc_buf, NULL);
rc = rc ?: svec_append_pg(&inodes_oparms, pg, PG_KVS_OPEN, txn, perfc_buf, NULL);
/* kvs create params
*/
rc = rc ?: svec_append_pg(&rids_cparms, pg, PG_KVS_CREATE, rids_pfx, NULL);
rc = rc ?: svec_append_pg(&data_cparms, pg, PG_KVS_CREATE, NULL);
rc = rc ?: svec_append_pg(&tombs_cparms, pg, PG_KVS_CREATE, NULL);
rc = rc ?: svec_append_pg(&inodes_cparms, pg, PG_KVS_CREATE, NULL);
return rc;
}
void
parm_vec_fini(void)
{
svec_reset(&hse_gparms);
svec_reset(&db_oparms);
svec_reset(&rids_oparms);
svec_reset(&rids_cparms);
svec_reset(&data_oparms);
svec_reset(&data_cparms);
svec_reset(&tombs_oparms);
svec_reset(&tombs_cparms);
svec_reset(&inodes_oparms);
svec_reset(&inodes_cparms);
}
enum kvs_type
decode_kvs_name(const char *name, int *instance)
{
*instance = 0;
if (!strcmp(name, KVS_RIDS_NAME))
return kvs_type_rids;
if (!strcmp(name, KVS_TOMBS_NAME))
return kvs_type_tombs;
if (1 == sscanf(name, KVS_INODES_NAME "%d", instance))
return kvs_type_tombs;
if (1 == sscanf(name, KVS_DATA_NAME "%d", instance))
return kvs_type_data;
return kvs_type_unknown;
}
struct svec *
kvs_cparms_get(const char *kvs_name)
{
int instance;
switch (decode_kvs_name(kvs_name, &instance)) {
case kvs_type_rids:
return &rids_cparms;
case kvs_type_data:
return &data_cparms;
case kvs_type_tombs:
return &tombs_cparms;
case kvs_type_inodes:
return &data_cparms;
default:
break;
}
return &empty_parms;
}
struct svec *
kvs_oparms_get(const char *kvs_name)
{
int instance;
switch (decode_kvs_name(kvs_name, &instance)) {
case kvs_type_rids:
return &rids_oparms;
case kvs_type_data:
return &data_oparms;
case kvs_type_tombs:
return &tombs_oparms;
case kvs_type_inodes:
return &data_oparms;
default:
break;
}
return &empty_parms;
}
int
ridlock_init(size_t nlocks)
{
size_t sz;
ridlockc = roundup(nlocks, 1024);
sz = roundup(sizeof(*ridlockv) * ridlockc, 4096);
ridlockv = aligned_alloc(4096, sz);
if (!ridlockv) {
eprint(errno, "ridlockv %zu %zu %zu", sz, ridlockc, nlocks);
return EX_OSERR;
}
memset(ridlockv, 0, sz);
return 0;
}
void
ridlock_fini(void)
{
free(ridlockv);
}
bool
ridlock_trylock(u_long rid)
{
uint exp;
uint *bkt;
if (!ridlockv)
return true;
exp = 0;
bkt = ridlockv + (rid % ridlockc);
return __atomic_compare_exchange_n(bkt, &exp, 1, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
}
void
ridlock_unlock(u_long rid)
{
uint exp;
uint *bkt;
if (!ridlockv)
return;
exp = 1;
bkt = ridlockv + (rid % ridlockc);
__atomic_compare_exchange_n(bkt, &exp, 0, false, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
}
int
rid2key(void *buf, size_t bufsz, u_long rid, u_int base)
{
if (base < 2) {
uint8_t *p = buf;
p[4] = rid & 0xfflu;
p[3] = (rid >> 8) & 0xfflu;
p[2] = (rid >> 16) & 0xfflu;
p[1] = (rid >> 24) & 0xfflu;
p[0] = (rid >> 32) & 0xfflu;
return 5;
}
return u64tostr(buf, bufsz, rid, base);
}
static inline u_int
rid2inodes_idx(u_long rid)
{
return rid % kvs_inodesc;
}
struct hse_kvs *
rid2inodes_kvs(u_long rid)
{
return kvs_inodesv[rid2inodes_idx(rid)];
}
static inline u_int
rid2data_idx(u_long rid)
{
return (rid & 0xfflu) % kvs_datac;
}
struct hse_kvs *
rid2data_kvs(u_long rid)
{
return kvs_datav[rid2data_idx(rid)];
}
u_long
prob_decode(const char *value, char **endp)
{
double d;
errno = 0;
d = strtod(value, endp);
if (errno)
return d;
if (d >= 1)
return ULONG_MAX;
return (d < 0) ? 0 : (d * (double)ULONG_MAX);
}
/* Scan the list for name/value pairs separated by the given separator.
* Decode each name/value pair and store the result accordingly.
*
* Returns an error code from errno.h on failure.
* Returns 0 on success.
*/
int
prop_decode(const char *list, const char *sep, const char *valid)
{
char *nvlist, *nvlist_base;
char *name, *value;
int rc;
if (!list)
return EINVAL;
nvlist = strdup(list);
if (!nvlist)
return ENOMEM;
nvlist_base = nvlist;
value = NULL;
rc = 0;
while (nvlist) {
char *end = NULL;
while (isspace(*nvlist))
++nvlist;
value = strsep(&nvlist, sep);
name = strsep(&value, "=");
if (verbosity > 2)
printf("%s: scanned name=%-16s value=%s\n", __func__, name, value);
if (!name || !*name)
continue;
if (!value || !*value) {
syntax("property '%s' has no value", name);
rc = EINVAL;
break;
}
if (valid && !strstr(valid, name)) {
syntax("invalid property '%s'", name);