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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "page.h"
#include "libnetdata/libnetdata.h"
typedef enum __attribute__((packed)) {
PAGE_OPTION_ALL_VALUES_EMPTY = (1 << 0),
} PAGE_OPTIONS;
typedef enum __attribute__((packed)) {
PGD_STATE_CREATED_FROM_COLLECTOR = (1 << 0),
PGD_STATE_CREATED_FROM_DISK = (1 << 1),
PGD_STATE_SCHEDULED_FOR_FLUSHING = (1 << 2),
PGD_STATE_FLUSHED_TO_DISK = (1 << 3),
} PGD_STATES;
typedef struct {
uint8_t *data;
uint32_t size;
} page_raw_t;
typedef struct {
size_t num_buffers;
gorilla_writer_t *writer;
int aral_index;
} page_gorilla_t;
struct pgd {
// the page type
uint8_t type;
// options related to the page
PAGE_OPTIONS options;
PGD_STATES states;
// the uses number of slots in the page
uint32_t used;
// the total number of slots available in the page
uint32_t slots;
union {
page_raw_t raw;
page_gorilla_t gorilla;
};
};
// ----------------------------------------------------------------------------
// memory management
struct {
ARAL *aral_pgd;
ARAL *aral_data[RRD_STORAGE_TIERS];
ARAL *aral_gorilla_buffer[4];
ARAL *aral_gorilla_writer[4];
} pgd_alloc_globals = {};
static ARAL *pgd_aral_data_lookup(size_t size)
{
for (size_t tier = 0; tier < storage_tiers; tier++)
if (size == tier_page_size[tier])
return pgd_alloc_globals.aral_data[tier];
return NULL;
}
void pgd_init_arals(void)
{
// pgd aral
{
char buf[20 + 1];
snprintfz(buf, sizeof(buf) - 1, "pgd");
// FIXME: add stats
pgd_alloc_globals.aral_pgd = aral_create(
buf,
sizeof(struct pgd),
64,
512 * (sizeof(struct pgd)),
pgc_aral_statistics(),
NULL, NULL, false, false);
}
// tier page aral
{
for (size_t i = storage_tiers; i > 0 ;i--)
{
size_t tier = storage_tiers - i;
char buf[20 + 1];
snprintfz(buf, sizeof(buf) - 1, "tier%zu-pages", tier);
pgd_alloc_globals.aral_data[tier] = aral_create(
buf,
tier_page_size[tier],
64,
512 * (tier_page_size[tier]),
pgc_aral_statistics(),
NULL, NULL, false, false);
}
}
// gorilla buffers aral
for (size_t i = 0; i != 4; i++) {
char buf[20 + 1];
snprintfz(buf, sizeof(buf) - 1, "gbuffer-%zu", i);
// FIXME: add stats
pgd_alloc_globals.aral_gorilla_buffer[i] = aral_create(
buf,
GORILLA_BUFFER_SIZE,
64,
512 * GORILLA_BUFFER_SIZE,
pgc_aral_statistics(),
NULL, NULL, false, false);
}
// gorilla writers aral
for (size_t i = 0; i != 4; i++) {
char buf[20 + 1];
snprintfz(buf, sizeof(buf) - 1, "gwriter-%zu", i);
// FIXME: add stats
pgd_alloc_globals.aral_gorilla_writer[i] = aral_create(
buf,
sizeof(gorilla_writer_t),
64,
512 * sizeof(gorilla_writer_t),
pgc_aral_statistics(),
NULL, NULL, false, false);
}
}
static void *pgd_data_aral_alloc(size_t size)
{
ARAL *ar = pgd_aral_data_lookup(size);
if (!ar)
return mallocz(size);
else
return aral_mallocz(ar);
}
static void pgd_data_aral_free(void *page, size_t size)
{
ARAL *ar = pgd_aral_data_lookup(size);
if (!ar)
freez(page);
else
aral_freez(ar, page);
}
// ----------------------------------------------------------------------------
// management api
PGD *pgd_create(uint8_t type, uint32_t slots)
{
PGD *pg = aral_mallocz(pgd_alloc_globals.aral_pgd);
pg->type = type;
pg->used = 0;
pg->slots = slots;
pg->options = PAGE_OPTION_ALL_VALUES_EMPTY;
pg->states = PGD_STATE_CREATED_FROM_COLLECTOR;
switch (type) {
case PAGE_METRICS:
case PAGE_TIER: {
uint32_t size = slots * page_type_size[type];
internal_fatal(!size || slots == 1,
"DBENGINE: invalid number of slots (%u) or page type (%u)", slots, type);
pg->raw.size = size;
pg->raw.data = pgd_data_aral_alloc(size);
break;
}
case PAGE_GORILLA_METRICS: {
internal_fatal(slots == 1,
"DBENGINE: invalid number of slots (%u) or page type (%u)", slots, type);
pg->slots = 8 * GORILLA_BUFFER_SLOTS;
// allocate new gorilla writer
pg->gorilla.aral_index = gettid() % 4;
pg->gorilla.writer = aral_mallocz(pgd_alloc_globals.aral_gorilla_writer[pg->gorilla.aral_index]);
// allocate new gorilla buffer
gorilla_buffer_t *gbuf = aral_mallocz(pgd_alloc_globals.aral_gorilla_buffer[pg->gorilla.aral_index]);
memset(gbuf, 0, GORILLA_BUFFER_SIZE);
global_statistics_gorilla_buffer_add_hot();
*pg->gorilla.writer = gorilla_writer_init(gbuf, GORILLA_BUFFER_SLOTS);
pg->gorilla.num_buffers = 1;
break;
}
default:
fatal("Unknown page type: %uc", type);
}
return pg;
}
PGD *pgd_create_from_disk_data(uint8_t type, void *base, uint32_t size)
{
if (!size)
return PGD_EMPTY;
if (size < page_type_size[type])
return PGD_EMPTY;
PGD *pg = aral_mallocz(pgd_alloc_globals.aral_pgd);
pg->type = type;
pg->states = PGD_STATE_CREATED_FROM_DISK;
pg->options = ~PAGE_OPTION_ALL_VALUES_EMPTY;
switch (type)
{
case PAGE_METRICS:
case PAGE_TIER:
pg->raw.size = size;
pg->used = size / page_type_size[type];
pg->slots = pg->used;
pg->raw.data = pgd_data_aral_alloc(size);
memcpy(pg->raw.data, base, size);
break;
case PAGE_GORILLA_METRICS:
internal_fatal(size == 0, "Asked to create page with 0 data!!!");
internal_fatal(size % sizeof(uint32_t), "Unaligned gorilla buffer size");
internal_fatal(size % GORILLA_BUFFER_SIZE, "Expected size to be a multiple of %zu-bytes", GORILLA_BUFFER_SIZE);
pg->raw.data = mallocz(size);
pg->raw.size = size;
// TODO: rm this
memset(pg->raw.data, 0, size);
memcpy(pg->raw.data, base, size);
uint32_t total_entries = gorilla_buffer_patch((void *) pg->raw.data);
pg->used = total_entries;
pg->slots = pg->used;
break;
default:
fatal("Unknown page type: %uc", type);
}
return pg;
}
void pgd_free(PGD *pg)
{
if (!pg)
return;
if (pg == PGD_EMPTY)
return;
switch (pg->type)
{
case PAGE_METRICS:
case PAGE_TIER:
pgd_data_aral_free(pg->raw.data, pg->raw.size);
break;
case PAGE_GORILLA_METRICS: {
if (pg->states & PGD_STATE_CREATED_FROM_DISK)
{
internal_fatal(pg->raw.data == NULL, "Tried to free gorilla PGD loaded from disk with NULL data");
freez(pg->raw.data);
pg->raw.data = NULL;
}
else if ((pg->states & PGD_STATE_CREATED_FROM_COLLECTOR) ||
(pg->states & PGD_STATE_SCHEDULED_FOR_FLUSHING) ||
(pg->states & PGD_STATE_FLUSHED_TO_DISK))
{
internal_fatal(pg->gorilla.writer == NULL,
"PGD does not have an active gorilla writer");
internal_fatal(pg->gorilla.num_buffers == 0,
"PGD does not have any gorilla buffers allocated");
while (true) {
gorilla_buffer_t *gbuf = gorilla_writer_drop_head_buffer(pg->gorilla.writer);
if (!gbuf)
break;
aral_freez(pgd_alloc_globals.aral_gorilla_buffer[pg->gorilla.aral_index], gbuf);
pg->gorilla.num_buffers -= 1;
}
internal_fatal(pg->gorilla.num_buffers != 0,
"Could not free all gorilla writer buffers");
aral_freez(pgd_alloc_globals.aral_gorilla_writer[pg->gorilla.aral_index], pg->gorilla.writer);
pg->gorilla.writer = NULL;
} else {
fatal("pgd_free() called on gorilla page with unsupported state");
// TODO: should we support any other states?
// if (!(pg->states & PGD_STATE_FLUSHED_TO_DISK))
// fatal("pgd_free() is not supported yet for pages flushed to disk");
}
break;
}
default:
fatal("Unknown page type: %uc", pg->type);
}
aral_freez(pgd_alloc_globals.aral_pgd, pg);
}
// ----------------------------------------------------------------------------
// utility functions
uint32_t pgd_type(PGD *pg)
{
return pg->type;
}
bool pgd_is_empty(PGD *pg)
{
if (!pg)
return true;
if (pg == PGD_EMPTY)
return true;
if (pg->used == 0)
return true;
if (pg->options & PAGE_OPTION_ALL_VALUES_EMPTY)
return true;
return false;
}
uint32_t pgd_slots_used(PGD *pg)
{
if (!pg)
return 0;
if (pg == PGD_EMPTY)
return 0;
return pg->used;
}
uint32_t pgd_memory_footprint(PGD *pg)
{
if (!pg)
return 0;
if (pg == PGD_EMPTY)
return 0;
size_t footprint = 0;
switch (pg->type) {
case PAGE_METRICS:
case PAGE_TIER:
footprint = sizeof(PGD) + pg->raw.size;
break;
case PAGE_GORILLA_METRICS: {
if (pg->states & PGD_STATE_CREATED_FROM_DISK)
footprint = sizeof(PGD) + pg->raw.size;
else
footprint = sizeof(PGD) + sizeof(gorilla_writer_t) + (pg->gorilla.num_buffers * GORILLA_BUFFER_SIZE);
break;
}
default:
fatal("Unknown page type: %uc", pg->type);
}
return footprint;
}
uint32_t pgd_disk_footprint(PGD *pg)
{
if (!pgd_slots_used(pg))
return 0;
size_t size = 0;
switch (pg->type) {
case PAGE_METRICS:
case PAGE_TIER: {
uint32_t used_size = pg->used * page_type_size[pg->type];
internal_fatal(used_size > pg->raw.size, "Wrong disk footprint page size");
size = used_size;
break;
}
case PAGE_GORILLA_METRICS: {
if (pg->states & PGD_STATE_CREATED_FROM_COLLECTOR ||
pg->states & PGD_STATE_SCHEDULED_FOR_FLUSHING ||
pg->states & PGD_STATE_FLUSHED_TO_DISK)
{
internal_fatal(!pg->gorilla.writer,
"pgd_disk_footprint() not implemented for NULL gorilla writers");
internal_fatal(pg->gorilla.num_buffers == 0,
"Gorilla writer does not have any buffers");
size = pg->gorilla.num_buffers * GORILLA_BUFFER_SIZE;
if (pg->states & PGD_STATE_CREATED_FROM_COLLECTOR) {
global_statistics_tier0_disk_compressed_bytes(gorilla_writer_nbytes(pg->gorilla.writer));
global_statistics_tier0_disk_uncompressed_bytes(gorilla_writer_entries(pg->gorilla.writer) * sizeof(storage_number));
}
} else if (pg->states & PGD_STATE_CREATED_FROM_DISK) {
size = pg->raw.size;
} else {
fatal("Asked disk footprint on unknown page state");
}
break;
}
default:
fatal("Unknown page type: %uc", pg->type);
}
internal_fatal(pg->states & PGD_STATE_CREATED_FROM_DISK,
"Disk footprint asked for page created from disk.");
pg->states = PGD_STATE_SCHEDULED_FOR_FLUSHING;
return size;
}
void pgd_copy_to_extent(PGD *pg, uint8_t *dst, uint32_t dst_size)
{
internal_fatal(pgd_disk_footprint(pg) != dst_size, "Wrong disk footprint size requested (need %u, available %u)",
pgd_disk_footprint(pg), dst_size);
switch (pg->type) {
case PAGE_METRICS:
case PAGE_TIER:
memcpy(dst, pg->raw.data, dst_size);
break;
case PAGE_GORILLA_METRICS: {
if ((pg->states & PGD_STATE_SCHEDULED_FOR_FLUSHING) == 0)
fatal("Copying to extent is supported only for PGDs that are scheduled for flushing.");
internal_fatal(!pg->gorilla.writer,
"pgd_copy_to_extent() not implemented for NULL gorilla writers");
internal_fatal(pg->gorilla.num_buffers == 0,
"pgd_copy_to_extent() gorilla writer does not have any buffers");
bool ok = gorilla_writer_serialize(pg->gorilla.writer, dst, dst_size);
UNUSED(ok);
internal_fatal(!ok,
"pgd_copy_to_extent() tried to serialize pg=%p, gw=%p (with dst_size=%u bytes, num_buffers=%zu)",
pg, pg->gorilla.writer, dst_size, pg->gorilla.num_buffers);
break;
}
default:
fatal("Unknown page type: %uc", pg->type);
}
pg->states = PGD_STATE_FLUSHED_TO_DISK;
}
// ----------------------------------------------------------------------------
// data collection
void pgd_append_point(PGD *pg,
usec_t point_in_time_ut __maybe_unused,
NETDATA_DOUBLE n,
NETDATA_DOUBLE min_value,
NETDATA_DOUBLE max_value,
uint16_t count,
uint16_t anomaly_count,
SN_FLAGS flags,
uint32_t expected_slot)
{
if (unlikely(pg->used >= pg->slots))
fatal("DBENGINE: attempted to write beyond page size (page type %u, slots %u, used %u)",
pg->type, pg->slots, pg->used /* FIXME:, pg->size */);
if (unlikely(pg->used != expected_slot))
fatal("DBENGINE: page is not aligned to expected slot (used %u, expected %u)",
pg->used, expected_slot);
if (!(pg->states & PGD_STATE_CREATED_FROM_COLLECTOR))
fatal("DBENGINE: collection on page not created from a collector");
if (pg->states & PGD_STATE_SCHEDULED_FOR_FLUSHING)
fatal("Data collection on page already scheduled for flushing");
switch (pg->type) {
case PAGE_METRICS: {
storage_number *tier0_metric_data = (storage_number *)pg->raw.data;
storage_number t = pack_storage_number(n, flags);
tier0_metric_data[pg->used++] = t;
if ((pg->options & PAGE_OPTION_ALL_VALUES_EMPTY) && does_storage_number_exist(t))
pg->options &= ~PAGE_OPTION_ALL_VALUES_EMPTY;
break;
}
case PAGE_TIER: {
storage_number_tier1_t *tier12_metric_data = (storage_number_tier1_t *)pg->raw.data;
storage_number_tier1_t t;
t.sum_value = (float) n;
t.min_value = (float) min_value;
t.max_value = (float) max_value;
t.anomaly_count = anomaly_count;
t.count = count;
tier12_metric_data[pg->used++] = t;
if ((pg->options & PAGE_OPTION_ALL_VALUES_EMPTY) && fpclassify(n) != FP_NAN)
pg->options &= ~PAGE_OPTION_ALL_VALUES_EMPTY;
break;
}
case PAGE_GORILLA_METRICS: {
pg->used++;
storage_number t = pack_storage_number(n, flags);
if ((pg->options & PAGE_OPTION_ALL_VALUES_EMPTY) && does_storage_number_exist(t))
pg->options &= ~PAGE_OPTION_ALL_VALUES_EMPTY;
bool ok = gorilla_writer_write(pg->gorilla.writer, t);
if (!ok) {
gorilla_buffer_t *new_buffer = aral_mallocz(pgd_alloc_globals.aral_gorilla_buffer[pg->gorilla.aral_index]);
memset(new_buffer, 0, GORILLA_BUFFER_SIZE);
gorilla_writer_add_buffer(pg->gorilla.writer, new_buffer, GORILLA_BUFFER_SLOTS);
pg->gorilla.num_buffers += 1;
global_statistics_gorilla_buffer_add_hot();
ok = gorilla_writer_write(pg->gorilla.writer, t);
internal_fatal(ok == false, "Failed to writer value in newly allocated gorilla buffer.");
}
break;
}
default:
fatal("DBENGINE: unknown page type id %d", pg->type);
break;
}
}
// ----------------------------------------------------------------------------
// querying with cursor
static void pgdc_seek(PGDC *pgdc, uint32_t position)
{
PGD *pg = pgdc->pgd;
switch (pg->type) {
case PAGE_METRICS:
case PAGE_TIER:
pgdc->slots = pgdc->pgd->used;
break;
case PAGE_GORILLA_METRICS: {
if (pg->states & PGD_STATE_CREATED_FROM_DISK) {
pgdc->slots = pgdc->pgd->slots;
pgdc->gr = gorilla_reader_init((void *) pg->raw.data);
} else {
if (!(pg->states & PGD_STATE_CREATED_FROM_COLLECTOR) &&
!(pg->states & PGD_STATE_SCHEDULED_FOR_FLUSHING) &&
!(pg->states & PGD_STATE_FLUSHED_TO_DISK))
fatal("pgdc_seek() currently is not supported for pages created from disk.");
if (!pg->gorilla.writer)
fatal("Seeking from a page without an active gorilla writer is not supported (yet).");
pgdc->slots = gorilla_writer_entries(pg->gorilla.writer);
pgdc->gr = gorilla_writer_get_reader(pg->gorilla.writer);
}
if (position > pgdc->slots)
position = pgdc->slots;
for (uint32_t i = 0; i != position; i++) {
uint32_t value;
bool ok = gorilla_reader_read(&pgdc->gr, &value);
if (!ok) {
// this is fine, the reader will return empty points
break;
}
}
break;
}
default:
fatal("DBENGINE: unknown page type id %d", pg->type);
break;
}
}
void pgdc_reset(PGDC *pgdc, PGD *pgd, uint32_t position)
{
// pgd might be null and position equal to UINT32_MAX
pgdc->pgd = pgd;
pgdc->position = position;
if (!pgd)
return;
if (pgd == PGD_EMPTY)
return;
if (position == UINT32_MAX)
return;
pgdc_seek(pgdc, position);
}
bool pgdc_get_next_point(PGDC *pgdc, uint32_t expected_position, STORAGE_POINT *sp)
{
if (!pgdc->pgd || pgdc->pgd == PGD_EMPTY || pgdc->position >= pgdc->slots)
{
storage_point_empty(*sp, sp->start_time_s, sp->end_time_s);
return false;
}
internal_fatal(pgdc->position != expected_position, "Wrong expected cursor position");
switch (pgdc->pgd->type)
{
case PAGE_METRICS: {
storage_number *array = (storage_number *) pgdc->pgd->raw.data;
storage_number n = array[pgdc->position++];
sp->min = sp->max = sp->sum = unpack_storage_number(n);
sp->flags = (SN_FLAGS)(n & SN_USER_FLAGS);
sp->count = 1;
sp->anomaly_count = is_storage_number_anomalous(n) ? 1 : 0;
return true;
}
case PAGE_TIER: {
storage_number_tier1_t *array = (storage_number_tier1_t *) pgdc->pgd->raw.data;
storage_number_tier1_t n = array[pgdc->position++];
sp->flags = n.anomaly_count ? SN_FLAG_NONE : SN_FLAG_NOT_ANOMALOUS;
sp->count = n.count;
sp->anomaly_count = n.anomaly_count;
sp->min = n.min_value;
sp->max = n.max_value;
sp->sum = n.sum_value;
return true;
}
case PAGE_GORILLA_METRICS: {
pgdc->position++;
uint32_t n = 666666666;
bool ok = gorilla_reader_read(&pgdc->gr, &n);
if (ok) {
sp->min = sp->max = sp->sum = unpack_storage_number(n);
sp->flags = (SN_FLAGS)(n & SN_USER_FLAGS);
sp->count = 1;
sp->anomaly_count = is_storage_number_anomalous(n) ? 1 : 0;
} else {
storage_point_empty(*sp, sp->start_time_s, sp->end_time_s);
}
return ok;
}
default: {
static bool logged = false;
if (!logged)
{
netdata_log_error("DBENGINE: unknown page type %d found. Cannot decode it. Ignoring its metrics.", pgd_type(pgdc->pgd));
logged = true;
}
storage_point_empty(*sp, sp->start_time_s, sp->end_time_s);
return false;
}
}
}
|