forked from bennbollay/tmstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmstat_test.c
3387 lines (3175 loc) · 107 KB
/
tmstat_test.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
/*
* Copyright (c) 2013, F5 Networks, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. 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.
*
* 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.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of F5 Networks, Inc.
*
* Statistics subsystem unit tests.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <err.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include "tmstat.h"
#define SIZE 1024
#define SMALL_SIZE 128
#define array_count(a) (sizeof(a) / sizeof((a)[0]))
#define TMCTL_MIN(a, b) \
({ typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a < _b ? _a : _b; })
#define TMCTL_MAX(a, b) \
({ typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a > _b ? _a : _b; })
#define rdtsc() ({ \
register long long __X; \
asm volatile("rdtsc" : "=A" (__X)); \
__X; })
extern THREAD char *tmstat_path;
/*
* Usage text (please sort options list alphabetically).
*/
static const char usage_str[] =
"usage: %1$s [OPTIONS] <--merge-test=PARAMS | --test=TEST>...\n"
"\n"
"Statistics subsystem unit tests.\n"
"\n"
"Supported options:\n"
" -b, --base=PATH Set segment directory base path.\n"
" -d, --directory=DIR Subscribe to specific directory.\n"
" -f, --file=PATH Inspect specific segment file.\n"
" -h, --help Display this text.\n"
" -M, --max-errors=N Display at most N error reports from tests.\n"
" -T, --merge-test=PARAMS Perform a merge test specifying parameters.\n"
" -s, --seed=S Use PRNG seed S.\n"
" -t, --test=TEST Run subsystem test:\n"
" single Single-segment allocation test.\n"
" multi Multi-segment allocation test.\n"
" merge Multi-segment merge test.\n"
" rndmrg Merge test of random intensity.\n"
" mrgperf Merge performance test.\n"
" parse-print Test parsing and printing functions.\n"
" eval Create segment for eval test.\n"
" reread Test auto re-read of directory.\n"
" bugs Exercise bug fixes.\n"
" cmod (Sort of) concurrent read/write test.\n"
" rollup Test rollup queries.\n"
" insn Test by-n row creation.\n"
" -v, --verbose Be verbose.\n"
"\n"
"For --merge-test, the argument should be like this example:\n"
"\n"
" SxT: [K+V U R P%%] ...\n"
"\n"
"The first two numbers' meanings are:\n"
"\n"
" S: Number of segments.\n"
" T: Number of tables in each segment.\n"
"\n"
"The numbers in the brackets apply to one table. There must be as many\n"
"bracketed groups of numbers as there are tables. The meanings for the\n"
"five bracketed numbers are:\n"
"\n"
" K: Number of key columns.\n"
" V: Number of value columns (columns with a merge rule).\n"
" U: Number of unique keys that may enter the table.\n"
" R: 1/0: Insert keys in random order?\n"
" P: Percent chance of inserting each key in a segment.\n"
"\n"
"For example, to test with two segments and two tables where table 1 has\n"
"one key column, three value columns, 100 keys, randomized insertion,\n"
"and 100%% probability of insertion; and table 2 has two key columns,\n"
"six value columns, 200 keys, ordered insertion, and 50%% probability\n"
"of insertion, you would say the following.\n"
"\n"
" %1$s --merge-test='2x2: [1+3 100 1 100%%] [2+6 200 0 50%%]'\n"
;
/*
* Options (please sort options alphabetically).
*/
enum {
OPT_BASE,
OPT_DIR,
OPT_FILE,
OPT_HELP,
OPT_MAX_ERRORS,
OPT_MERGE_TEST,
OPT_SEED,
OPT_TEST,
OPT_VERBOSE,
/* This entry is always last. */
OPT_COUNT,
};
static struct option long_options[] = {
[OPT_BASE] = { "base", required_argument, NULL, 'b' },
[OPT_DIR] = { "dir", required_argument, NULL, 'd' },
[OPT_FILE] = { "file", required_argument, NULL, 'f' },
[OPT_HELP] = { "help", no_argument, NULL, 'h' },
[OPT_MAX_ERRORS] = { "max-errors", required_argument, NULL, 'M' },
[OPT_MERGE_TEST] = { "merge-test", required_argument, NULL, 'T' },
[OPT_SEED] = { "seed", required_argument, NULL, 's' },
[OPT_TEST] = { "test", required_argument, NULL, 't' },
[OPT_VERBOSE] = { "verbose", no_argument, NULL, 'v' },
[OPT_COUNT] = { 0 },
};
static char options[] = "ab:d:e:f:hiM:m:T:t:vw:";
static unsigned seed;
/*
* User preferences.
*/
static unsigned max_errors = 100; // Max number of errors to show.
static bool verbose = false; // Be verbose.
/*
* Used by tests to specify the tables they want to the make_stuff
* function.
*/
struct table_spec {
const char *name;
const struct TMCOL *cols;
unsigned ncols;
unsigned rowsz;
};
/*
* Format value into a text representation.
*/
static char *
format_column(TMCOL col, void *p)
{
int64_t d = 0;
uint64_t u = 0;
char *s = NULL;
switch (col->type) {
case TMSTAT_T_HIDDEN:
/* Pretend that hidden fields don't exist. */
s = strdup("-");
break;
case TMSTAT_T_SIGNED:
switch (col->size) {
case 1: d = *(int8_t *)p; break;
case 2: d = *(int16_t *)p; break;
case 4: d = *(int32_t *)p; break;
case 8: d = *(int64_t *)p; break;
}
asprintf(&s, "%lld", d);
break;
case TMSTAT_T_UNSIGNED:
switch (col->size) {
case 1: u = *(uint8_t *)p; break;
case 2: u = *(uint16_t *)p; break;
case 4: u = *(uint32_t *)p; break;
case 8: u = *(uint64_t *)p; break;
}
asprintf(&s, "%llu", u);
break;
case TMSTAT_T_TEXT:
s = malloc(col->size + 1);
memcpy(s, p, col->size);
s[col->size] = '\0';
break;
case TMSTAT_T_BIN:
s = malloc((col->size * 10) + 1);
for (unsigned i = 0; i < col->size; i++) {
s[(i * 10) + 0] = (((uint8_t *)p)[i] & (1 << 7)) ? '1' : '0';
s[(i * 10) + 1] = (((uint8_t *)p)[i] & (1 << 6)) ? '1' : '0';
s[(i * 10) + 2] = (((uint8_t *)p)[i] & (1 << 5)) ? '1' : '0';
s[(i * 10) + 3] = (((uint8_t *)p)[i] & (1 << 4)) ? '1' : '0';
s[(i * 10) + 4] = ':';
s[(i * 10) + 5] = (((uint8_t *)p)[i] & (1 << 3)) ? '1' : '0';
s[(i * 10) + 6] = (((uint8_t *)p)[i] & (1 << 2)) ? '1' : '0';
s[(i * 10) + 7] = (((uint8_t *)p)[i] & (1 << 1)) ? '1' : '0';
s[(i * 10) + 8] = (((uint8_t *)p)[i] & (1 << 0)) ? '1' : '0';
s[(i * 10) + 9] = ' ';
}
s[(col->size * 10) - 1] = '\0';
break;
case TMSTAT_T_DEC: {
unsigned j = 0;
s = malloc((col->size * 4) + 1);
for (unsigned i = 0; i < col->size; i++) {
j += sprintf(&s[j], "%d.", ((uint8_t *)p)[i]);
}
s[j - 1] = '\0';
break;
}
case TMSTAT_T_HEX: {
static char digit[16] = "0123456789abcdef";
s = malloc(col->size * 3);
for (unsigned i = 0; i < col->size; i++) {
s[(i * 3) + 0] = digit[(((uint8_t *)p)[i] / 16) % 16];
s[(i * 3) + 1] = digit[ ((uint8_t *)p)[i] % 16];
s[(i * 3) + 2] = ':';
}
s[(col->size * 3) - 1] = '\0';
break;
}
default:
/* Unknown format. */
s = strdup("?");
break;
}
return s;
}
static const char *
tmctl_rule_name(enum tmstat_rule r)
{
switch (r) {
case TMSTAT_R_KEY: return "key";
case TMSTAT_R_OR: return "or";
case TMSTAT_R_SUM: return "sum";
case TMSTAT_R_MIN: return "min";
case TMSTAT_R_MAX: return "max";
}
return "???";
}
static const char *
tmctl_type_name(enum tmstat_type r)
{
switch (r) {
case TMSTAT_T_HIDDEN: return "hidden";
case TMSTAT_T_SIGNED: return "signed";
case TMSTAT_T_UNSIGNED: return "unsigned";
case TMSTAT_T_TEXT: return "text";
case TMSTAT_T_BIN: return "bin";
case TMSTAT_T_DEC: return "dec";
case TMSTAT_T_HEX: return "hex";
}
return "???";
}
static char *
format_columns(TMCOL col, unsigned col_count, void *base)
{
unsigned col_idx;
char *buffer;
buffer = malloc(2);
if (buffer == NULL) return NULL;
buffer[0] = '[';
buffer[1] = '\0';
for (unsigned i = 0; i < 2 * col_count; i++) {
char *s;
unsigned len;
col_idx = i % col_count;
if ((col[col_idx].rule == TMSTAT_R_KEY) != (i < col_count)) {
continue;
}
s = format_column(col + col_idx,
(char*)base + col[col_idx].offset);
len = strlen(buffer) + strlen(col[col_idx].name) + strlen(s) + 100;
char *tmp = malloc(len);
if (tmp == NULL) {
free(s);
free(buffer);
return NULL;
}
snprintf(tmp, len, "%s%s%s(%d:%s:%s:%d)=%s",
buffer,
strlen(buffer) > 1 ? " " : "",
col[col_idx].name,
col_idx,
tmctl_rule_name(col[col_idx].rule),
tmctl_type_name(col[col_idx].type),
col[col_idx].size,
s);
free(s);
free(buffer);
buffer = tmp;
}
strcat(buffer, "]");
return buffer;
}
/* For use in GDB. */
char *prow(TMROW);
char *
prow(TMROW row)
{
void *p;
TMCOL col;
unsigned col_count;
tmstat_row_field(row, NULL, &p);
tmstat_row_description(row, &col, &col_count);
return format_columns(col, col_count, p);
}
/*
* Perform multi-segment merge performance test.
* The point of this test to is to compare merge and search performance across
* versions. All key types with different comparison algorithms will be used
* and tables of various sizes will be compared.
*/
static int
test_merge_perf(void)
{
struct row {
uint32_t k1;
uint64_t k2;
int32_t k3;
int64_t k4;
char k5[32];
int16_t dat;
} *p;
static struct TMCOL cols[] = {
TMCOL_UINT(struct row, k1),
TMCOL_UINT(struct row, k2),
TMCOL_INT(struct row, k3),
TMCOL_INT(struct row, k4),
TMCOL_TEXT(struct row, k5),
TMCOL_INT(struct row, dat, .rule=TMSTAT_R_SUM),
};
char filename[PATH_MAX];
char label[32];
TMSTAT stat[SMALL_SIZE], sub_stat, merge_stat;
TMTABLE table;
TMROW *row[SMALL_SIZE], *sub_row;
unsigned rows;
signed ret;
unsigned long long start, end, ctot = 0, mtot = 0, qtot = 0;
unsigned REPS, TREPS = 120;
FILE* pdat = fopen("merge_perf.txt", "w");
fprintf(pdat, "size, create, merge, query\n");
printf("Merge performance test\n");
unsigned ROWS, run;
for (ROWS = 4, run = 1; ROWS < (1<<15);
ROWS = ROWS << 1, ++run)
{
REPS = TREPS/run;
printf(" %d: rows: %d (%d reps)", run, ROWS, REPS); fflush(stdout);
for (unsigned reps = 0; reps < REPS; ++reps) {
printf("."); fflush(stdout);
// creation time
start = rdtsc();
for (unsigned i = 0; i < SMALL_SIZE; i++) {
row[i] = malloc(ROWS*sizeof(TMROW));
assert(row[i] != NULL);
snprintf(label, sizeof(label), "ptest.%d", i);
ret = tmstat_create(&stat[i], label);
assert(ret == 0);
ret = tmstat_table_register(stat[i], &table, "ptest",
cols, 6, sizeof(struct row));
assert(ret == 0);
ret = tmstat_publish(stat[i], "ptest");
assert(ret == 0);
for (unsigned j = 0; j < ROWS; j++) {
ret = tmstat_row_create(stat[i], table, &row[i][j]);
assert(ret == 0);
tmstat_row_field(row[i][j], NULL, &p);
p->k4 = p->k3 = p->k2 = p->k1 = (j*5)%ROWS;
snprintf(p->k5, 32, "key.%d", (j*5)%ROWS);
}
}
end = rdtsc();
ctot += end - start;
ret = tmstat_subscribe(&sub_stat, "ptest");
assert(ret == 0);
ret = tmstat_query(
sub_stat, "ptest", 0, NULL, NULL, &sub_row, &rows);
assert(ret == 0);
assert(rows == ROWS);
for (unsigned i = 0; i < rows; i++) {
tmstat_row_field(sub_row[i], NULL, &p);
tmstat_row_drop(sub_row[i]);
}
free(sub_row);
// merge time
start = rdtsc();
tmstat_merge(sub_stat, "ptest_merged", TMSTAT_MERGE_ALL);
tmstat_destroy(sub_stat);
for (unsigned i = 0; i < SMALL_SIZE; i++) {
for (unsigned j = 0; j < ROWS; j++) {
tmstat_row_drop(row[i][j]);
}
tmstat_destroy(stat[i]);
}
end = rdtsc();
mtot += end - start;
snprintf(filename, sizeof(filename), "%s/private/ptest_merged",
tmstat_path);
assert(tmstat_read(&merge_stat, filename) == 0);
assert(tmstat_is_table_sorted(merge_stat, "ptest") == true);
char* cols[5] = {"k1","k2","k3","k4","k5"};
// query time
start = rdtsc();
for (unsigned i = 0; i < ROWS; ++i)
{
uint32_t k1v = i;
uint64_t k2v = i;
int32_t k3v = i;
int64_t k4v = i;
snprintf(label, sizeof(label), "key.%d", i);
void* vals[5] = { (void*)&k1v, (void*)&k2v, (void*)&k3v,
(void*)&k4v,(void*)label };
// query_fast will fail if an 'index' is not available
ret = tmstat_query(merge_stat, "ptest", 5, cols,
vals, &sub_row, &rows);
assert(ret == 0);
assert(rows == 1);
tmstat_row_drop(*sub_row);
}
end = rdtsc();
qtot += end - start;
tmstat_destroy(merge_stat);
}
printf("\n");
printf(" create cycles: %3.1f\n", (float)ctot/REPS);
printf(" merge cycles: %3.1f\n", (float)mtot/REPS);
printf(" query cycles: %3.1f\n", (float)qtot/REPS);
printf("done \n");
fprintf(pdat, "%d, %3.1f, %3.1f, %3.1f\n", ROWS,
(float)ctot/REPS, (float)mtot/REPS, (float)qtot/REPS);
}
fclose(pdat);
return EXIT_SUCCESS;
}
/*
* Perform multi-segment merge test.
*
* TO DO: Deprecate this function. If this covers anything that some
* invocation of merge_test cannot, merge_test should be updated.
*/
static int
test_merge(void)
{
struct row {
uint32_t key, sum, junk, min, max, or, more_junk;
} *p;
static struct TMCOL cols[] = {
/*
* Note that these are intentionally not in the same order as
* the fields in struct row and that struct row includes extra
* junk that we want to ignore.
*/
TMCOL_BIN(struct row, or, .rule = TMSTAT_R_OR),
TMCOL_UINT(struct row, min, .rule = TMSTAT_R_MIN),
TMCOL_UINT(struct row, key),
TMCOL_UINT(struct row, sum, .rule = TMSTAT_R_SUM),
TMCOL_UINT(struct row, max, .rule = TMSTAT_R_MAX),
};
unsigned alloc_count = 0, free_count = 0;
unsigned search_count = 0, match_count = 0;
char label[32];
TMSTAT stat[SMALL_SIZE], sub_stat, merge_stat;
TMTABLE table;
TMROW *row, *sub_row;
unsigned rows;
signed ret;
char filename[PATH_MAX];
row = malloc(sizeof(TMROW) * SMALL_SIZE * SIZE);
assert(row != NULL);
printf("Merge test: %d segments, %d tables, %d rows, %d bytes.\n",
SMALL_SIZE,
SMALL_SIZE,
SMALL_SIZE * SIZE,
SMALL_SIZE * SIZE * sizeof(struct row));
printf(" 1:"); fflush(stdout);
/* Create. */
printf(" create;"); fflush(stdout);
for (unsigned i = 0; i < SMALL_SIZE; i++) {
snprintf(label, sizeof(label), "test.%d", i);
ret = tmstat_create(&stat[i], label);
assert(ret == 0);
ret = tmstat_table_register(stat[i], &table, "test",
cols, 5, sizeof(struct row));
assert(ret == 0);
ret = tmstat_publish(stat[i], "test");
assert(ret == 0);
for (unsigned j = 0; j < SIZE; j++) {
ret = tmstat_row_create(stat[i], table, &row[i * SIZE + j]);
assert(ret == 0);
alloc_count++;
tmstat_row_field(row[i * SIZE + j], NULL, &p);
p->key = (j*5)%SIZE;
p->sum = 1;
p->min = (i << 8) + 1;
p->max = (i << 8) + 1;
p->or = 1 << (i % 32);
}
}
/* Subscribe. */
printf(" subscribe;"); fflush(stdout);
ret = tmstat_subscribe(&sub_stat, "test");
assert(ret == 0);
/* Query. */
printf(" query;"); fflush(stdout);
ret = tmstat_query(sub_stat, "test", 0, NULL, NULL, &sub_row, &rows);
assert(ret == 0);
assert(rows == SIZE);
search_count++;
match_count += rows;
for (unsigned i = 0; i < rows; i++) {
tmstat_row_field(sub_row[i], NULL, &p);
assert(p->sum == SMALL_SIZE);
assert(p->min == 1);
assert(p->max == ((SMALL_SIZE - 1) << 8) + 1);
assert(p->or == 0xffffffff);
tmstat_row_drop(sub_row[i]);
free_count++;
}
free(sub_row);
/* Merge out to new segment */
printf(" output merged segment;");
tmstat_merge(sub_stat, "test_merged", TMSTAT_MERGE_ALL);
/* Destroy. */
printf(" destroy;\n"); fflush(stdout);
tmstat_destroy(sub_stat);
for (unsigned i = 0; i < SMALL_SIZE; i++) {
for (unsigned j = 0; j < SIZE; j++) {
tmstat_row_drop(row[i * SIZE + j]);
free_count++;
}
tmstat_destroy(stat[i]);
}
printf(" sorted query test on merged segment.\n");
snprintf(filename, sizeof(filename), "%s/private/test_merged", tmstat_path);
assert(tmstat_read(&merge_stat, filename) == 0);
assert(tmstat_is_table_sorted(merge_stat, "test") == true);
char* col = "key";
for (unsigned i = 0; i < SIZE; ++i)
{
uint32_t kval = i;
void* val = (void*)&kval;
int ret;
ret = tmstat_query(merge_stat, "test", 1, &col, &val, &sub_row, &rows);
assert(ret == 0);
assert(rows == 1);
tmstat_row_drop(*sub_row);
free(sub_row);
}
tmstat_destroy(merge_stat);
printf("Done: %d allocs, %d frees, %d queries, %d matches.\n",
alloc_count, free_count, search_count, match_count);
free(row);
return EXIT_SUCCESS;
}
/**
* Merge two column values in the way we would expect the library to
* do so.
*/
static void
tmctl_col_merge(TMCOL col, void *r_ptr, void *a_ptr)
{
switch (col->rule) {
case TMSTAT_R_OR:
switch (col->size) {
case 1:
*(uint8_t *)a_ptr |= *(uint8_t *)r_ptr;
break;
case 2:
*(uint16_t *)a_ptr |= *(uint16_t *)r_ptr;
break;
case 4:
*(uint32_t *)a_ptr |= *(uint32_t *)r_ptr;
break;
case 8:
*(uint64_t *)a_ptr |= *(uint64_t *)r_ptr;
break;
default:
assert(0);
break;
}
break;
case TMSTAT_R_SUM:
switch (col->size) {
case 1:
*(uint8_t *)a_ptr += *(uint8_t *)r_ptr;
break;
case 2:
*(uint16_t *)a_ptr += *(uint16_t *)r_ptr;
break;
case 4:
*(uint32_t *)a_ptr += *(uint32_t *)r_ptr;
break;
case 8:
*(uint64_t *)a_ptr += *(uint64_t *)r_ptr;
break;
default:
assert(0);
break;
}
break;
case TMSTAT_R_MIN:
switch (col->type) {
case TMSTAT_T_SIGNED:
switch (col->size) {
case 1:
*(int8_t *)a_ptr =
TMCTL_MIN(*(int8_t *)a_ptr, *(int8_t *)r_ptr);
break;
case 2:
*(int16_t *)a_ptr =
TMCTL_MIN(*(int16_t *)a_ptr, *(int16_t *)r_ptr);
break;
case 4:
*(int32_t *)a_ptr =
TMCTL_MIN(*(int32_t *)a_ptr, *(int32_t *)r_ptr);
break;
case 8:
*(int64_t *)a_ptr =
TMCTL_MIN(*(int64_t *)a_ptr, *(int64_t *)r_ptr);
break;
default:
assert(0);
break;
}
break;
case TMSTAT_T_UNSIGNED:
switch (col->size) {
case 1:
*(uint8_t *)a_ptr =
TMCTL_MIN(*(uint8_t *)a_ptr, *(uint8_t *)r_ptr);
break;
case 2:
*(uint16_t *)a_ptr =
TMCTL_MIN(*(uint16_t *)a_ptr, *(uint16_t *)r_ptr);
break;
case 4:
*(uint32_t *)a_ptr =
TMCTL_MIN(*(uint32_t *)a_ptr, *(uint32_t *)r_ptr);
break;
case 8:
*(uint64_t *)a_ptr =
TMCTL_MIN(*(uint64_t *)a_ptr, *(uint64_t *)r_ptr);
break;
default:
assert(0);
break;
}
break;
default:
assert(0);
break;
}
break;
case TMSTAT_R_MAX:
switch (col->type) {
case TMSTAT_T_SIGNED:
switch (col->size) {
case 1:
*(int8_t *)a_ptr =
TMCTL_MAX(*(int8_t *)a_ptr, *(int8_t *)r_ptr);
break;
case 2:
*(int16_t *)a_ptr =
TMCTL_MAX(*(int16_t *)a_ptr, *(int16_t *)r_ptr);
break;
case 4:
*(int32_t *)a_ptr =
TMCTL_MAX(*(int32_t *)a_ptr, *(int32_t *)r_ptr);
break;
case 8:
*(int64_t *)a_ptr =
TMCTL_MAX(*(int64_t *)a_ptr, *(int64_t *)r_ptr);
break;
default:
assert(0);
break;
}
break;
case TMSTAT_T_UNSIGNED:
switch (col->size) {
case 1:
*(uint8_t *)a_ptr =
TMCTL_MAX(*(uint8_t *)a_ptr, *(uint8_t *)r_ptr);
break;
case 2:
*(uint16_t *)a_ptr =
TMCTL_MAX(*(uint16_t *)a_ptr, *(uint16_t *)r_ptr);
break;
case 4:
*(uint32_t *)a_ptr =
TMCTL_MAX(*(uint32_t *)a_ptr, *(uint32_t *)r_ptr);
break;
case 8:
*(uint64_t *)a_ptr =
TMCTL_MAX(*(uint64_t *)a_ptr, *(uint64_t *)r_ptr);
break;
default:
assert(0);
break;
}
break;
default:
assert(0);
break;
}
break;
default:
assert(0);
break;
}
}
/**
* Compare either key or value columns of two rows.
*
* @param[in] col Column definitions
* @param[in] n_col Number of columns
* @param[in] r1 Base pointer of lhs row
* @param[in] r2 Base pointer of rhs row
* @param[in] key 1: compare key columns, 0: compare value columns
*/
static int
tmctl_row_cmp(TMCOL col, unsigned n_col, char *r1, char *r2, int key)
{
int m;
key = !!key; /* boolify */
for (unsigned i = 0; i < n_col; i++) {
if ((col[i].rule == TMSTAT_R_KEY) == key) {
m = memcmp(&r1[col[i].offset], &r2[col[i].offset], col[i].size);
if (m != 0) {
return m;
}
}
}
return 0;
}
static void
show_error(unsigned *n_err, unsigned err_max, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(*n_err) += 1;
if (*n_err <= err_max) {
vprintf(fmt, ap);
}
if (*n_err == err_max) {
printf("Further error reports will be suppressed.\n");
}
va_end(ap);
}
static void
verbosef(const char *fmt, ...)
{
if (verbose) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
}
/**
* Merge test: lots of segments/rows/tables/columns.
* All parameters are per-table except n_seg and n_tbl.
*
* @param[in] n_seg Number of segments to use
* @param[in] n_tbl Number of tables to use
* @param[in] n_key Number of key columns to use
* @param[in] n_val Number of value columns to use
* @param[in] n_uniq Number of distinct keys to use
* @param[in] rnd 1 says add rows always in same order; 0 otherwise
* @param[in] p_ins % Probability of inserting a row on any iteration
* @return EXIT_SUCCESS
*/
static int
test_merge_new(unsigned n_seg,
unsigned n_tbl,
const unsigned *n_key,
const unsigned *n_val,
const unsigned *n_uniq,
const unsigned *rnd,
const unsigned *p_ins)
{
/* col[i][j]: jth column of ith table. Not segment-specific. */
TMCOL *col;
/* rowsz[i]: Calculated row size for ith table. */
unsigned *rowsz;
/* stat[i]: ith stat segment. */
TMSTAT *stat;
/* tbl[i][j]: jth table of ith segment. */
TMTABLE **tbl;
/* row[i][j]: jth row accumulator for ith table. */
void ***row;
/* Subscription to the entire stats dir with all segments. */
TMSTAT sub_stat;
/* Count of errors encountered in comparing query results to our data. */
unsigned error_count = 0;
int r, ret;
/*
* Chatty chat chat.
*/
printf("Merge test. %d segments, %d tables per segment.\n"
"Seed = %d.\n"
"Table | # key col | # val col | # keys | rnd? | p(ins)\n"
"------+-----------+-----------+---------+------+-------\n",
n_seg, n_tbl, seed);
for (unsigned i = 0; i < n_tbl; i++) {
printf("%5d |%10d |%10d |%8d | %c |%6d%%\n",
i, n_key[i], n_val[i], n_uniq[i],
rnd[i] ? 'Y' : 'N', p_ins[i]);
}
/*
* Dream up table definitions.
*/
col = (TMCOL *)calloc(n_tbl, sizeof(TMCOL *));
assert(col);
rowsz = (unsigned*)calloc(n_tbl, sizeof(int));
for (unsigned i = 0; i < n_tbl; i++) {
unsigned key_done = 0;
unsigned val_done = 0;
const unsigned n_col = n_key[i] + n_val[i];
col[i] = (TMCOL)calloc(n_col, sizeof(struct TMCOL));
assert(col[i]);
for (unsigned j = 0; j < n_col; j++) {
/* Add empty space with low probability */
r = random() % 100;
for (unsigned c = 95; c < r; c++) rowsz[i] += 1 + (random() % 8);
/* Name */
col[i][j].name = (char*)malloc(32);
assert(col[i][j].name);
snprintf(col[i][j].name, 32, "col_%d_%d", i, j);
/* Offset */
col[i][j].offset = rowsz[i];
/* Decide at random whether to make a key or value column,
* unless we must make one or the other because we have
* enough of one kind already. */
r = random() % n_col;
if ((r < n_key[i] && key_done < n_key[i]) || val_done == n_val[i]) {
/* Key column */
col[i][j].rule = TMSTAT_R_KEY;
/* Key type */
r = random() % 6;
switch (r) {
case 0:
col[i][j].type = TMSTAT_T_TEXT;
break;
case 1:
col[i][j].type = TMSTAT_T_BIN;
break;
case 2:
col[i][j].type = TMSTAT_T_DEC;
break;
case 3:
col[i][j].type = TMSTAT_T_HEX;
break;
case 4:
col[i][j].type = TMSTAT_T_SIGNED;
break;
case 5:
col[i][j].type = TMSTAT_T_UNSIGNED;
break;
}
/* Key size: loop to ensure that first key column is
* big enough to store an integer or string
* representation thereof. */
while (1) {
if (col[i][j].type == TMSTAT_T_SIGNED ||
col[i][j].type == TMSTAT_T_UNSIGNED) {
r = 1 << (random() % 4);
} else {
r = 1 + (random() % ((2 * TMSTAT_PATHED_NAMELEN) - 1));
}
if (key_done != 0 ||
(col[i][j].type == TMSTAT_T_TEXT && r > 20) ||
(col[i][j].type != TMSTAT_T_TEXT && r >= sizeof(int))) {
break;
}
}
col[i][j].size = r;
rowsz[i] += col[i][j].size;
key_done += 1;
} else {
/* Value column */
r = random() % 9;
switch (r) {
case 0:
case 1:
case 2:
case 3:
/* SUM fields are our bread and butter, so they're
* most common. */
col[i][j].rule = TMSTAT_R_SUM;
break;
case 4:
case 5:
col[i][j].rule = TMSTAT_R_MIN;
break;
case 6:
case 7:
col[i][j].rule = TMSTAT_R_MAX;
break;
case 8:
/* OR fields are likely to saturate and are thus
* not very interesting, so they're less probable. */
col[i][j].rule = TMSTAT_R_OR;
break;
}
/* Value type */
r = random() % 2;
switch (r) {
case 0:
col[i][j].type = TMSTAT_T_SIGNED;
break;