forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.c
2746 lines (2137 loc) · 99.1 KB
/
cache.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: GPL-3.0-or-later
#include "cache.h"
/* STATES AND TRANSITIONS
*
* entry | entry
* v v
* HOT -> DIRTY --> CLEAN --> EVICT
* v | v
* flush | evict
* v | v
* save | free
* callback | callback
*
*/
typedef int32_t REFCOUNT;
#define REFCOUNT_DELETING (-100)
// to use ARAL uncomment the following line:
#define PGC_WITH_ARAL 1
typedef enum __attribute__ ((__packed__)) {
// mutually exclusive flags
PGC_PAGE_CLEAN = (1 << 0), // none of the following
PGC_PAGE_DIRTY = (1 << 1), // contains unsaved data
PGC_PAGE_HOT = (1 << 2), // currently being collected
// flags related to various actions on each page
PGC_PAGE_IS_BEING_DELETED = (1 << 3),
PGC_PAGE_IS_BEING_MIGRATED_TO_V2 = (1 << 4),
PGC_PAGE_HAS_NO_DATA_IGNORE_ACCESSES = (1 << 5),
PGC_PAGE_HAS_BEEN_ACCESSED = (1 << 6),
} PGC_PAGE_FLAGS;
#define page_flag_check(page, flag) (__atomic_load_n(&((page)->flags), __ATOMIC_ACQUIRE) & (flag))
#define page_flag_set(page, flag) __atomic_or_fetch(&((page)->flags), flag, __ATOMIC_RELEASE)
#define page_flag_clear(page, flag) __atomic_and_fetch(&((page)->flags), ~(flag), __ATOMIC_RELEASE)
#define page_get_status_flags(page) page_flag_check(page, PGC_PAGE_HOT | PGC_PAGE_DIRTY | PGC_PAGE_CLEAN)
#define is_page_hot(page) (page_get_status_flags(page) == PGC_PAGE_HOT)
#define is_page_dirty(page) (page_get_status_flags(page) == PGC_PAGE_DIRTY)
#define is_page_clean(page) (page_get_status_flags(page) == PGC_PAGE_CLEAN)
struct pgc_page {
// indexing data
Word_t section;
Word_t metric_id;
time_t start_time_s;
time_t end_time_s;
uint32_t update_every_s;
uint32_t assumed_size;
REFCOUNT refcount;
uint16_t accesses; // counts the number of accesses on this page
PGC_PAGE_FLAGS flags;
SPINLOCK transition_spinlock; // when the page changes between HOT, DIRTY, CLEAN, we have to get this lock
struct {
struct pgc_page *next;
struct pgc_page *prev;
} link;
void *data;
uint8_t custom_data[];
// IMPORTANT!
// THIS STRUCTURE NEEDS TO BE INITIALIZED BY HAND!
};
struct pgc_linked_list {
SPINLOCK spinlock;
union {
PGC_PAGE *base;
Pvoid_t sections_judy;
};
PGC_PAGE_FLAGS flags;
size_t version;
size_t last_version_checked;
bool linked_list_in_sections_judy; // when true, we use 'sections_judy', otherwise we use 'base'
struct pgc_queue_statistics *stats;
};
struct pgc {
struct {
char name[PGC_NAME_MAX + 1];
size_t partitions;
size_t clean_size;
size_t max_dirty_pages_per_call;
size_t max_pages_per_inline_eviction;
size_t max_skip_pages_per_inline_eviction;
size_t max_flushes_inline;
size_t max_workers_evict_inline;
size_t additional_bytes_per_page;
free_clean_page_callback pgc_free_clean_cb;
save_dirty_page_callback pgc_save_dirty_cb;
save_dirty_init_callback pgc_save_init_cb;
PGC_OPTIONS options;
size_t severe_pressure_per1000;
size_t aggressive_evict_per1000;
size_t healthy_size_per1000;
size_t evict_low_threshold_per1000;
dynamic_target_cache_size_callback dynamic_target_size_cb;
} config;
#ifdef PGC_WITH_ARAL
ARAL **aral;
#endif
PGC_CACHE_LINE_PADDING(0);
struct pgc_index {
RW_SPINLOCK rw_spinlock;
Pvoid_t sections_judy;
PGC_CACHE_LINE_PADDING(0);
} *index;
PGC_CACHE_LINE_PADDING(1);
struct {
SPINLOCK spinlock;
size_t per1000;
} usage;
PGC_CACHE_LINE_PADDING(2);
struct pgc_linked_list clean; // LRU is applied here to free memory from the cache
PGC_CACHE_LINE_PADDING(3);
struct pgc_linked_list dirty; // in the dirty list, pages are ordered the way they were marked dirty
PGC_CACHE_LINE_PADDING(4);
struct pgc_linked_list hot; // in the hot list, pages are order the way they were marked hot
PGC_CACHE_LINE_PADDING(5);
struct pgc_statistics stats; // statistics
#ifdef NETDATA_PGC_POINTER_CHECK
PGC_CACHE_LINE_PADDING(6);
netdata_mutex_t global_pointer_registry_mutex;
Pvoid_t global_pointer_registry;
#endif
};
// ----------------------------------------------------------------------------
// validate each pointer is indexed once - internal checks only
static inline void pointer_index_init(PGC *cache __maybe_unused) {
#ifdef NETDATA_PGC_POINTER_CHECK
netdata_mutex_init(&cache->global_pointer_registry_mutex);
#else
;
#endif
}
static inline void pointer_destroy_index(PGC *cache __maybe_unused) {
#ifdef NETDATA_PGC_POINTER_CHECK
netdata_mutex_lock(&cache->global_pointer_registry_mutex);
JudyHSFreeArray(&cache->global_pointer_registry, PJE0);
netdata_mutex_unlock(&cache->global_pointer_registry_mutex);
#else
;
#endif
}
static inline void pointer_add(PGC *cache __maybe_unused, PGC_PAGE *page __maybe_unused) {
#ifdef NETDATA_PGC_POINTER_CHECK
netdata_mutex_lock(&cache->global_pointer_registry_mutex);
Pvoid_t *PValue = JudyHSIns(&cache->global_pointer_registry, &page, sizeof(void *), PJE0);
if(*PValue != NULL)
fatal("pointer already exists in registry");
*PValue = page;
netdata_mutex_unlock(&cache->global_pointer_registry_mutex);
#else
;
#endif
}
static inline void pointer_check(PGC *cache __maybe_unused, PGC_PAGE *page __maybe_unused) {
#ifdef NETDATA_PGC_POINTER_CHECK
netdata_mutex_lock(&cache->global_pointer_registry_mutex);
Pvoid_t *PValue = JudyHSGet(cache->global_pointer_registry, &page, sizeof(void *));
if(PValue == NULL)
fatal("pointer is not found in registry");
netdata_mutex_unlock(&cache->global_pointer_registry_mutex);
#else
;
#endif
}
static inline void pointer_del(PGC *cache __maybe_unused, PGC_PAGE *page __maybe_unused) {
#ifdef NETDATA_PGC_POINTER_CHECK
netdata_mutex_lock(&cache->global_pointer_registry_mutex);
int ret = JudyHSDel(&cache->global_pointer_registry, &page, sizeof(void *), PJE0);
if(!ret)
fatal("pointer to be deleted does not exist in registry");
netdata_mutex_unlock(&cache->global_pointer_registry_mutex);
#else
;
#endif
}
// ----------------------------------------------------------------------------
// locking
static inline size_t pgc_indexing_partition(PGC *cache, Word_t metric_id) {
static __thread Word_t last_metric_id = 0;
static __thread size_t last_partition = 0;
if(metric_id == last_metric_id || cache->config.partitions == 1)
return last_partition;
last_metric_id = metric_id;
last_partition = indexing_partition(metric_id, cache->config.partitions);
return last_partition;
}
static inline void pgc_index_read_lock(PGC *cache, size_t partition) {
rw_spinlock_read_lock(&cache->index[partition].rw_spinlock);
}
static inline void pgc_index_read_unlock(PGC *cache, size_t partition) {
rw_spinlock_read_unlock(&cache->index[partition].rw_spinlock);
}
static inline void pgc_index_write_lock(PGC *cache, size_t partition) {
rw_spinlock_write_lock(&cache->index[partition].rw_spinlock);
}
static inline void pgc_index_write_unlock(PGC *cache, size_t partition) {
rw_spinlock_write_unlock(&cache->index[partition].rw_spinlock);
}
static inline bool pgc_ll_trylock(PGC *cache __maybe_unused, struct pgc_linked_list *ll) {
return spinlock_trylock(&ll->spinlock);
}
static inline void pgc_ll_lock(PGC *cache __maybe_unused, struct pgc_linked_list *ll) {
spinlock_lock(&ll->spinlock);
}
static inline void pgc_ll_unlock(PGC *cache __maybe_unused, struct pgc_linked_list *ll) {
spinlock_unlock(&ll->spinlock);
}
static inline bool page_transition_trylock(PGC *cache __maybe_unused, PGC_PAGE *page) {
return spinlock_trylock(&page->transition_spinlock);
}
static inline void page_transition_lock(PGC *cache __maybe_unused, PGC_PAGE *page) {
spinlock_lock(&page->transition_spinlock);
}
static inline void page_transition_unlock(PGC *cache __maybe_unused, PGC_PAGE *page) {
spinlock_unlock(&page->transition_spinlock);
}
// ----------------------------------------------------------------------------
// evictions control
static inline size_t cache_usage_per1000(PGC *cache, size_t *size_to_evict) {
if(size_to_evict)
spinlock_lock(&cache->usage.spinlock);
else if(!spinlock_trylock(&cache->usage.spinlock))
return __atomic_load_n(&cache->usage.per1000, __ATOMIC_RELAXED);
size_t current_cache_size;
size_t wanted_cache_size;
size_t per1000;
size_t dirty = __atomic_load_n(&cache->dirty.stats->size, __ATOMIC_RELAXED);
size_t hot = __atomic_load_n(&cache->hot.stats->size, __ATOMIC_RELAXED);
if(cache->config.options & PGC_OPTIONS_AUTOSCALE) {
size_t dirty_max = __atomic_load_n(&cache->dirty.stats->max_size, __ATOMIC_RELAXED);
size_t hot_max = __atomic_load_n(&cache->hot.stats->max_size, __ATOMIC_RELAXED);
// our promise to users
size_t max_size1 = MAX(hot_max, hot) * 2;
// protection against slow flushing
size_t max_size2 = hot_max + ((dirty_max < hot_max / 2) ? hot_max / 2 : dirty_max * 2);
// the final wanted cache size
wanted_cache_size = MIN(max_size1, max_size2);
if(cache->config.dynamic_target_size_cb) {
size_t wanted_cache_size_cb = cache->config.dynamic_target_size_cb();
if(wanted_cache_size_cb > wanted_cache_size)
wanted_cache_size = wanted_cache_size_cb;
}
if (wanted_cache_size < hot + dirty + cache->config.clean_size)
wanted_cache_size = hot + dirty + cache->config.clean_size;
}
else
wanted_cache_size = hot + dirty + cache->config.clean_size;
// protection again huge queries
// if huge queries are running, or huge amounts need to be saved
// allow the cache to grow more (hot pages in main cache are also referenced)
size_t referenced_size = __atomic_load_n(&cache->stats.referenced_size, __ATOMIC_RELAXED);
if(unlikely(wanted_cache_size < referenced_size * 2 / 3))
wanted_cache_size = referenced_size * 2 / 3;
current_cache_size = __atomic_load_n(&cache->stats.size, __ATOMIC_RELAXED); // + pgc_aral_overhead();
per1000 = (size_t)((unsigned long long)current_cache_size * 1000ULL / (unsigned long long)wanted_cache_size);
__atomic_store_n(&cache->usage.per1000, per1000, __ATOMIC_RELAXED);
__atomic_store_n(&cache->stats.wanted_cache_size, wanted_cache_size, __ATOMIC_RELAXED);
__atomic_store_n(&cache->stats.current_cache_size, current_cache_size, __ATOMIC_RELAXED);
spinlock_unlock(&cache->usage.spinlock);
if(size_to_evict) {
size_t target = (size_t)((unsigned long long)wanted_cache_size * (unsigned long long)cache->config.evict_low_threshold_per1000 / 1000ULL);
if(current_cache_size > target)
*size_to_evict = current_cache_size - target;
else
*size_to_evict = 0;
}
if(per1000 >= cache->config.severe_pressure_per1000)
__atomic_add_fetch(&cache->stats.events_cache_under_severe_pressure, 1, __ATOMIC_RELAXED);
else if(per1000 >= cache->config.aggressive_evict_per1000)
__atomic_add_fetch(&cache->stats.events_cache_needs_space_aggressively, 1, __ATOMIC_RELAXED);
return per1000;
}
static inline bool cache_pressure(PGC *cache, size_t limit) {
return (cache_usage_per1000(cache, NULL) >= limit);
}
#define cache_under_severe_pressure(cache) cache_pressure(cache, (cache)->config.severe_pressure_per1000)
#define cache_needs_space_aggressively(cache) cache_pressure(cache, (cache)->config.aggressive_evict_per1000)
#define cache_above_healthy_limit(cache) cache_pressure(cache, (cache)->config.healthy_size_per1000)
typedef bool (*evict_filter)(PGC_PAGE *page, void *data);
static bool evict_pages_with_filter(PGC *cache, size_t max_skip, size_t max_evict, bool wait, bool all_of_them, evict_filter filter, void *data);
#define evict_pages(cache, max_skip, max_evict, wait, all_of_them) evict_pages_with_filter(cache, max_skip, max_evict, wait, all_of_them, NULL, NULL)
static inline void evict_on_clean_page_added(PGC *cache __maybe_unused) {
if((cache->config.options & PGC_OPTIONS_EVICT_PAGES_INLINE) || cache_needs_space_aggressively(cache)) {
evict_pages(cache,
cache->config.max_skip_pages_per_inline_eviction,
cache->config.max_pages_per_inline_eviction,
false, false);
}
}
static inline void evict_on_page_release_when_permitted(PGC *cache __maybe_unused) {
if ((cache->config.options & PGC_OPTIONS_EVICT_PAGES_INLINE) || cache_under_severe_pressure(cache)) {
evict_pages(cache,
cache->config.max_skip_pages_per_inline_eviction,
cache->config.max_pages_per_inline_eviction,
false, false);
}
}
// ----------------------------------------------------------------------------
// flushing control
static bool flush_pages(PGC *cache, size_t max_flushes, Word_t section, bool wait, bool all_of_them);
static inline bool flushing_critical(PGC *cache) {
if(unlikely(__atomic_load_n(&cache->dirty.stats->size, __ATOMIC_RELAXED) > __atomic_load_n(&cache->hot.stats->max_size, __ATOMIC_RELAXED))) {
__atomic_add_fetch(&cache->stats.events_flush_critical, 1, __ATOMIC_RELAXED);
return true;
}
return false;
}
// ----------------------------------------------------------------------------
// helpers
static inline size_t page_assumed_size(PGC *cache, size_t size) {
return size + (sizeof(PGC_PAGE) + cache->config.additional_bytes_per_page + sizeof(Word_t) * 3);
}
static inline size_t page_size_from_assumed_size(PGC *cache, size_t assumed_size) {
return assumed_size - (sizeof(PGC_PAGE) + cache->config.additional_bytes_per_page + sizeof(Word_t) * 3);
}
// ----------------------------------------------------------------------------
// Linked list management
static inline void atomic_set_max(size_t *max, size_t desired) {
size_t expected;
expected = __atomic_load_n(max, __ATOMIC_RELAXED);
do {
if(expected >= desired)
return;
} while(!__atomic_compare_exchange_n(max, &expected, desired,
false, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
}
struct section_pages {
SPINLOCK migration_to_v2_spinlock;
size_t entries;
size_t size;
PGC_PAGE *base;
};
static ARAL *pgc_section_pages_aral = NULL;
static void pgc_section_pages_static_aral_init(void) {
static SPINLOCK spinlock = NETDATA_SPINLOCK_INITIALIZER;
if(unlikely(!pgc_section_pages_aral)) {
spinlock_lock(&spinlock);
// we have to check again
if(!pgc_section_pages_aral)
pgc_section_pages_aral = aral_create(
"pgc_section",
sizeof(struct section_pages),
0,
65536, NULL,
NULL, NULL, false, false);
spinlock_unlock(&spinlock);
}
}
static inline void pgc_stats_ll_judy_change(PGC *cache, struct pgc_linked_list *ll, size_t mem_before_judyl, size_t mem_after_judyl) {
if(mem_after_judyl > mem_before_judyl) {
__atomic_add_fetch(&ll->stats->size, mem_after_judyl - mem_before_judyl, __ATOMIC_RELAXED);
__atomic_add_fetch(&cache->stats.size, mem_after_judyl - mem_before_judyl, __ATOMIC_RELAXED);
}
else if(mem_after_judyl < mem_before_judyl) {
__atomic_sub_fetch(&ll->stats->size, mem_before_judyl - mem_after_judyl, __ATOMIC_RELAXED);
__atomic_sub_fetch(&cache->stats.size, mem_before_judyl - mem_after_judyl, __ATOMIC_RELAXED);
}
}
static inline void pgc_stats_index_judy_change(PGC *cache, size_t mem_before_judyl, size_t mem_after_judyl) {
if(mem_after_judyl > mem_before_judyl) {
__atomic_add_fetch(&cache->stats.size, mem_after_judyl - mem_before_judyl, __ATOMIC_RELAXED);
}
else if(mem_after_judyl < mem_before_judyl) {
__atomic_sub_fetch(&cache->stats.size, mem_before_judyl - mem_after_judyl, __ATOMIC_RELAXED);
}
}
static void pgc_ll_add(PGC *cache __maybe_unused, struct pgc_linked_list *ll, PGC_PAGE *page, bool having_lock) {
if(!having_lock)
pgc_ll_lock(cache, ll);
internal_fatal(page_get_status_flags(page) != 0,
"DBENGINE CACHE: invalid page flags, the page has %d, but it is should be %d",
page_get_status_flags(page),
0);
if(ll->linked_list_in_sections_judy) {
size_t mem_before_judyl, mem_after_judyl;
mem_before_judyl = JudyLMemUsed(ll->sections_judy);
Pvoid_t *section_pages_pptr = JudyLIns(&ll->sections_judy, page->section, PJE0);
mem_after_judyl = JudyLMemUsed(ll->sections_judy);
struct section_pages *sp = *section_pages_pptr;
if(!sp) {
// sp = callocz(1, sizeof(struct section_pages));
sp = aral_mallocz(pgc_section_pages_aral);
memset(sp, 0, sizeof(struct section_pages));
*section_pages_pptr = sp;
mem_after_judyl += sizeof(struct section_pages);
}
pgc_stats_ll_judy_change(cache, ll, mem_before_judyl, mem_after_judyl);
sp->entries++;
sp->size += page->assumed_size;
DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(sp->base, page, link.prev, link.next);
if((sp->entries % cache->config.max_dirty_pages_per_call) == 0)
ll->version++;
}
else {
// CLEAN pages end up here.
// - New pages created as CLEAN, always have 1 access.
// - DIRTY pages made CLEAN, depending on their accesses may be appended (accesses > 0) or prepended (accesses = 0).
if(page->accesses || page_flag_check(page, PGC_PAGE_HAS_BEEN_ACCESSED | PGC_PAGE_HAS_NO_DATA_IGNORE_ACCESSES) == PGC_PAGE_HAS_BEEN_ACCESSED) {
DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(ll->base, page, link.prev, link.next);
page_flag_clear(page, PGC_PAGE_HAS_BEEN_ACCESSED);
}
else
DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(ll->base, page, link.prev, link.next);
ll->version++;
}
page_flag_set(page, ll->flags);
if(!having_lock)
pgc_ll_unlock(cache, ll);
size_t entries = __atomic_add_fetch(&ll->stats->entries, 1, __ATOMIC_RELAXED);
size_t size = __atomic_add_fetch(&ll->stats->size, page->assumed_size, __ATOMIC_RELAXED);
__atomic_add_fetch(&ll->stats->added_entries, 1, __ATOMIC_RELAXED);
__atomic_add_fetch(&ll->stats->added_size, page->assumed_size, __ATOMIC_RELAXED);
atomic_set_max(&ll->stats->max_entries, entries);
atomic_set_max(&ll->stats->max_size, size);
}
static void pgc_ll_del(PGC *cache __maybe_unused, struct pgc_linked_list *ll, PGC_PAGE *page, bool having_lock) {
__atomic_sub_fetch(&ll->stats->entries, 1, __ATOMIC_RELAXED);
__atomic_sub_fetch(&ll->stats->size, page->assumed_size, __ATOMIC_RELAXED);
__atomic_add_fetch(&ll->stats->removed_entries, 1, __ATOMIC_RELAXED);
__atomic_add_fetch(&ll->stats->removed_size, page->assumed_size, __ATOMIC_RELAXED);
if(!having_lock)
pgc_ll_lock(cache, ll);
internal_fatal(page_get_status_flags(page) != ll->flags,
"DBENGINE CACHE: invalid page flags, the page has %d, but it is should be %d",
page_get_status_flags(page),
ll->flags);
page_flag_clear(page, ll->flags);
if(ll->linked_list_in_sections_judy) {
Pvoid_t *section_pages_pptr = JudyLGet(ll->sections_judy, page->section, PJE0);
internal_fatal(!section_pages_pptr, "DBENGINE CACHE: page should be in Judy LL, but it is not");
struct section_pages *sp = *section_pages_pptr;
sp->entries--;
sp->size -= page->assumed_size;
DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(sp->base, page, link.prev, link.next);
if(!sp->base) {
size_t mem_before_judyl, mem_after_judyl;
mem_before_judyl = JudyLMemUsed(ll->sections_judy);
int rc = JudyLDel(&ll->sections_judy, page->section, PJE0);
mem_after_judyl = JudyLMemUsed(ll->sections_judy);
if(!rc)
fatal("DBENGINE CACHE: cannot delete section from Judy LL");
// freez(sp);
aral_freez(pgc_section_pages_aral, sp);
mem_after_judyl -= sizeof(struct section_pages);
pgc_stats_ll_judy_change(cache, ll, mem_before_judyl, mem_after_judyl);
}
}
else {
DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(ll->base, page, link.prev, link.next);
ll->version++;
}
if(!having_lock)
pgc_ll_unlock(cache, ll);
}
static inline void page_has_been_accessed(PGC *cache, PGC_PAGE *page) {
PGC_PAGE_FLAGS flags = page_flag_check(page, PGC_PAGE_CLEAN | PGC_PAGE_HAS_NO_DATA_IGNORE_ACCESSES);
if (!(flags & PGC_PAGE_HAS_NO_DATA_IGNORE_ACCESSES)) {
__atomic_add_fetch(&page->accesses, 1, __ATOMIC_RELAXED);
if (flags & PGC_PAGE_CLEAN) {
if(pgc_ll_trylock(cache, &cache->clean)) {
DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(cache->clean.base, page, link.prev, link.next);
DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(cache->clean.base, page, link.prev, link.next);
pgc_ll_unlock(cache, &cache->clean);
page_flag_clear(page, PGC_PAGE_HAS_BEEN_ACCESSED);
}
else
page_flag_set(page, PGC_PAGE_HAS_BEEN_ACCESSED);
}
}
}
// ----------------------------------------------------------------------------
// state transitions
static inline void page_set_clean(PGC *cache, PGC_PAGE *page, bool having_transition_lock, bool having_clean_lock) {
if(!having_transition_lock)
page_transition_lock(cache, page);
PGC_PAGE_FLAGS flags = page_get_status_flags(page);
if(flags & PGC_PAGE_CLEAN) {
if(!having_transition_lock)
page_transition_unlock(cache, page);
return;
}
if(flags & PGC_PAGE_HOT)
pgc_ll_del(cache, &cache->hot, page, false);
if(flags & PGC_PAGE_DIRTY)
pgc_ll_del(cache, &cache->dirty, page, false);
// first add to linked list, the set the flag (required for move_page_last())
pgc_ll_add(cache, &cache->clean, page, having_clean_lock);
if(!having_transition_lock)
page_transition_unlock(cache, page);
}
static inline void page_set_dirty(PGC *cache, PGC_PAGE *page, bool having_hot_lock) {
if(!having_hot_lock)
// to avoid deadlocks, we have to get the hot lock before the page transition
// since this is what all_hot_to_dirty() does
pgc_ll_lock(cache, &cache->hot);
page_transition_lock(cache, page);
PGC_PAGE_FLAGS flags = page_get_status_flags(page);
if(flags & PGC_PAGE_DIRTY) {
page_transition_unlock(cache, page);
if(!having_hot_lock)
// we don't need the hot lock anymore
pgc_ll_unlock(cache, &cache->hot);
return;
}
__atomic_add_fetch(&cache->stats.hot2dirty_entries, 1, __ATOMIC_RELAXED);
__atomic_add_fetch(&cache->stats.hot2dirty_size, page->assumed_size, __ATOMIC_RELAXED);
if(likely(flags & PGC_PAGE_HOT))
pgc_ll_del(cache, &cache->hot, page, true);
if(!having_hot_lock)
// we don't need the hot lock anymore
pgc_ll_unlock(cache, &cache->hot);
if(unlikely(flags & PGC_PAGE_CLEAN))
pgc_ll_del(cache, &cache->clean, page, false);
// first add to linked list, the set the flag (required for move_page_last())
pgc_ll_add(cache, &cache->dirty, page, false);
__atomic_sub_fetch(&cache->stats.hot2dirty_entries, 1, __ATOMIC_RELAXED);
__atomic_sub_fetch(&cache->stats.hot2dirty_size, page->assumed_size, __ATOMIC_RELAXED);
page_transition_unlock(cache, page);
}
static inline void page_set_hot(PGC *cache, PGC_PAGE *page) {
page_transition_lock(cache, page);
PGC_PAGE_FLAGS flags = page_get_status_flags(page);
if(flags & PGC_PAGE_HOT) {
page_transition_unlock(cache, page);
return;
}
if(flags & PGC_PAGE_DIRTY)
pgc_ll_del(cache, &cache->dirty, page, false);
if(flags & PGC_PAGE_CLEAN)
pgc_ll_del(cache, &cache->clean, page, false);
// first add to linked list, the set the flag (required for move_page_last())
pgc_ll_add(cache, &cache->hot, page, false);
page_transition_unlock(cache, page);
}
// ----------------------------------------------------------------------------
// Referencing
static inline size_t PGC_REFERENCED_PAGES(PGC *cache) {
return __atomic_load_n(&cache->stats.referenced_entries, __ATOMIC_RELAXED);
}
static inline void PGC_REFERENCED_PAGES_PLUS1(PGC *cache, PGC_PAGE *page) {
__atomic_add_fetch(&cache->stats.referenced_entries, 1, __ATOMIC_RELAXED);
__atomic_add_fetch(&cache->stats.referenced_size, page->assumed_size, __ATOMIC_RELAXED);
}
static inline void PGC_REFERENCED_PAGES_MINUS1(PGC *cache, size_t assumed_size) {
__atomic_sub_fetch(&cache->stats.referenced_entries, 1, __ATOMIC_RELAXED);
__atomic_sub_fetch(&cache->stats.referenced_size, assumed_size, __ATOMIC_RELAXED);
}
// If the page is not already acquired,
// YOU HAVE TO HAVE THE QUEUE (hot, dirty, clean) THE PAGE IS IN, L O C K E D !
// If you don't have it locked, NOTHING PREVENTS THIS PAGE FOR VANISHING WHILE THIS IS CALLED!
static inline bool page_acquire(PGC *cache, PGC_PAGE *page) {
__atomic_add_fetch(&cache->stats.acquires, 1, __ATOMIC_RELAXED);
REFCOUNT expected, desired;
expected = __atomic_load_n(&page->refcount, __ATOMIC_RELAXED);
size_t spins = 0;
do {
spins++;
if(unlikely(expected < 0))
return false;
desired = expected + 1;
} while(!__atomic_compare_exchange_n(&page->refcount, &expected, desired, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED));
if(unlikely(spins > 1))
__atomic_add_fetch(&cache->stats.acquire_spins, spins - 1, __ATOMIC_RELAXED);
if(desired == 1)
PGC_REFERENCED_PAGES_PLUS1(cache, page);
return true;
}
static inline void page_release(PGC *cache, PGC_PAGE *page, bool evict_if_necessary) {
__atomic_add_fetch(&cache->stats.releases, 1, __ATOMIC_RELAXED);
size_t assumed_size = page->assumed_size; // take the size before we release it
REFCOUNT expected, desired;
expected = __atomic_load_n(&page->refcount, __ATOMIC_RELAXED);
size_t spins = 0;
do {
spins++;
internal_fatal(expected <= 0,
"DBENGINE CACHE: trying to release a page with reference counter %d", expected);
desired = expected - 1;
} while(!__atomic_compare_exchange_n(&page->refcount, &expected, desired, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED));
if(unlikely(spins > 1))
__atomic_add_fetch(&cache->stats.release_spins, spins - 1, __ATOMIC_RELAXED);
if(desired == 0) {
PGC_REFERENCED_PAGES_MINUS1(cache, assumed_size);
if(evict_if_necessary)
evict_on_page_release_when_permitted(cache);
}
}
static inline bool non_acquired_page_get_for_deletion___while_having_clean_locked(PGC *cache __maybe_unused, PGC_PAGE *page) {
__atomic_add_fetch(&cache->stats.acquires_for_deletion, 1, __ATOMIC_RELAXED);
internal_fatal(!is_page_clean(page),
"DBENGINE CACHE: only clean pages can be deleted");
REFCOUNT expected, desired;
expected = __atomic_load_n(&page->refcount, __ATOMIC_RELAXED);
size_t spins = 0;
bool delete_it;
do {
spins++;
if (expected == 0) {
desired = REFCOUNT_DELETING;
delete_it = true;
}
else {
delete_it = false;
break;
}
} while(!__atomic_compare_exchange_n(&page->refcount, &expected, desired, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED));
if(delete_it) {
// we can delete this page
internal_fatal(page_flag_check(page, PGC_PAGE_IS_BEING_DELETED),
"DBENGINE CACHE: page is already being deleted");
page_flag_set(page, PGC_PAGE_IS_BEING_DELETED);
}
if(unlikely(spins > 1))
__atomic_add_fetch(&cache->stats.delete_spins, spins - 1, __ATOMIC_RELAXED);
return delete_it;
}
static inline bool acquired_page_get_for_deletion_or_release_it(PGC *cache __maybe_unused, PGC_PAGE *page) {
__atomic_add_fetch(&cache->stats.acquires_for_deletion, 1, __ATOMIC_RELAXED);
size_t assumed_size = page->assumed_size; // take the size before we release it
REFCOUNT expected, desired;
expected = __atomic_load_n(&page->refcount, __ATOMIC_RELAXED);
size_t spins = 0;
bool delete_it;
do {
spins++;
internal_fatal(expected < 1,
"DBENGINE CACHE: page to be deleted should be acquired by the caller.");
if (expected == 1) {
// we are the only one having this page referenced
desired = REFCOUNT_DELETING;
delete_it = true;
}
else {
// this page cannot be deleted
desired = expected - 1;
delete_it = false;
}
} while(!__atomic_compare_exchange_n(&page->refcount, &expected, desired, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED));
if(delete_it) {
PGC_REFERENCED_PAGES_MINUS1(cache, assumed_size);
// we can delete this page
internal_fatal(page_flag_check(page, PGC_PAGE_IS_BEING_DELETED),
"DBENGINE CACHE: page is already being deleted");
page_flag_set(page, PGC_PAGE_IS_BEING_DELETED);
}
if(unlikely(spins > 1))
__atomic_add_fetch(&cache->stats.delete_spins, spins - 1, __ATOMIC_RELAXED);
return delete_it;
}
// ----------------------------------------------------------------------------
// Indexing
static inline void free_this_page(PGC *cache, PGC_PAGE *page, size_t partition __maybe_unused) {
// call the callback to free the user supplied memory
cache->config.pgc_free_clean_cb(cache, (PGC_ENTRY){
.section = page->section,
.metric_id = page->metric_id,
.start_time_s = page->start_time_s,
.end_time_s = __atomic_load_n(&page->end_time_s, __ATOMIC_RELAXED),
.update_every_s = page->update_every_s,
.size = page_size_from_assumed_size(cache, page->assumed_size),
.hot = (is_page_hot(page)) ? true : false,
.data = page->data,
.custom_data = (cache->config.additional_bytes_per_page) ? page->custom_data : NULL,
});
// update statistics
__atomic_add_fetch(&cache->stats.removed_entries, 1, __ATOMIC_RELAXED);
__atomic_add_fetch(&cache->stats.removed_size, page->assumed_size, __ATOMIC_RELAXED);
__atomic_sub_fetch(&cache->stats.entries, 1, __ATOMIC_RELAXED);
__atomic_sub_fetch(&cache->stats.size, page->assumed_size, __ATOMIC_RELAXED);
// free our memory
#ifdef PGC_WITH_ARAL
aral_freez(cache->aral[partition], page);
#else
freez(page);
#endif
}
static void remove_this_page_from_index_unsafe(PGC *cache, PGC_PAGE *page, size_t partition) {
// remove it from the Judy arrays
pointer_check(cache, page);
internal_fatal(page_flag_check(page, PGC_PAGE_HOT | PGC_PAGE_DIRTY | PGC_PAGE_CLEAN),
"DBENGINE CACHE: page to be removed from the cache is still in the linked-list");
internal_fatal(!page_flag_check(page, PGC_PAGE_IS_BEING_DELETED),
"DBENGINE CACHE: page to be removed from the index, is not marked for deletion");
internal_fatal(partition != pgc_indexing_partition(cache, page->metric_id),
"DBENGINE CACHE: attempted to remove this page from the wrong partition of the cache");
Pvoid_t *metrics_judy_pptr = JudyLGet(cache->index[partition].sections_judy, page->section, PJE0);
if(unlikely(!metrics_judy_pptr))
fatal("DBENGINE CACHE: section '%lu' should exist, but it does not.", page->section);
Pvoid_t *pages_judy_pptr = JudyLGet(*metrics_judy_pptr, page->metric_id, PJE0);
if(unlikely(!pages_judy_pptr))
fatal("DBENGINE CACHE: metric '%lu' in section '%lu' should exist, but it does not.",
page->metric_id, page->section);
Pvoid_t *page_ptr = JudyLGet(*pages_judy_pptr, page->start_time_s, PJE0);
if(unlikely(!page_ptr))
fatal("DBENGINE CACHE: page with start time '%ld' of metric '%lu' in section '%lu' should exist, but it does not.",
page->start_time_s, page->metric_id, page->section);
PGC_PAGE *found_page = *page_ptr;
if(unlikely(found_page != page))
fatal("DBENGINE CACHE: page with start time '%ld' of metric '%lu' in section '%lu' should exist, but the index returned a different address.",
page->start_time_s, page->metric_id, page->section);
size_t mem_before_judyl = 0, mem_after_judyl = 0;
mem_before_judyl += JudyLMemUsed(*pages_judy_pptr);
if(unlikely(!JudyLDel(pages_judy_pptr, page->start_time_s, PJE0)))
fatal("DBENGINE CACHE: page with start time '%ld' of metric '%lu' in section '%lu' exists, but cannot be deleted.",
page->start_time_s, page->metric_id, page->section);
mem_after_judyl += JudyLMemUsed(*pages_judy_pptr);
mem_before_judyl += JudyLMemUsed(*metrics_judy_pptr);
if(!*pages_judy_pptr && !JudyLDel(metrics_judy_pptr, page->metric_id, PJE0))
fatal("DBENGINE CACHE: metric '%lu' in section '%lu' exists and is empty, but cannot be deleted.",
page->metric_id, page->section);
mem_after_judyl += JudyLMemUsed(*metrics_judy_pptr);
mem_before_judyl += JudyLMemUsed(cache->index[partition].sections_judy);
if(!*metrics_judy_pptr && !JudyLDel(&cache->index[partition].sections_judy, page->section, PJE0))
fatal("DBENGINE CACHE: section '%lu' exists and is empty, but cannot be deleted.", page->section);
mem_after_judyl += JudyLMemUsed(cache->index[partition].sections_judy);
pgc_stats_index_judy_change(cache, mem_before_judyl, mem_after_judyl);
pointer_del(cache, page);
}
static inline void remove_and_free_page_not_in_any_queue_and_acquired_for_deletion(PGC *cache, PGC_PAGE *page) {
size_t partition = pgc_indexing_partition(cache, page->metric_id);
pgc_index_write_lock(cache, partition);
remove_this_page_from_index_unsafe(cache, page, partition);
pgc_index_write_unlock(cache, partition);
free_this_page(cache, page, partition);
}
static inline bool make_acquired_page_clean_and_evict_or_page_release(PGC *cache, PGC_PAGE *page) {
pointer_check(cache, page);
page_transition_lock(cache, page);
pgc_ll_lock(cache, &cache->clean);
// make it clean - it does not have any accesses, so it will be prepended
page_set_clean(cache, page, true, true);
if(!acquired_page_get_for_deletion_or_release_it(cache, page)) {
pgc_ll_unlock(cache, &cache->clean);
page_transition_unlock(cache, page);
return false;
}
// remove it from the linked list
pgc_ll_del(cache, &cache->clean, page, true);
pgc_ll_unlock(cache, &cache->clean);
page_transition_unlock(cache, page);
remove_and_free_page_not_in_any_queue_and_acquired_for_deletion(cache, page);
return true;
}
// returns true, when there is more work to do
static bool evict_pages_with_filter(PGC *cache, size_t max_skip, size_t max_evict, bool wait, bool all_of_them, evict_filter filter, void *data) {
size_t per1000 = cache_usage_per1000(cache, NULL);
if(!all_of_them && per1000 < cache->config.healthy_size_per1000)
// don't bother - not enough to do anything
return false;
size_t workers_running = __atomic_add_fetch(&cache->stats.workers_evict, 1, __ATOMIC_RELAXED);
if(!wait && !all_of_them && workers_running > cache->config.max_workers_evict_inline && per1000 < cache->config.severe_pressure_per1000) {
__atomic_sub_fetch(&cache->stats.workers_evict, 1, __ATOMIC_RELAXED);
return false;
}
internal_fatal(cache->clean.linked_list_in_sections_judy,
"wrong clean pages configuration - clean pages need to have a linked list, not a judy array");
if(unlikely(!max_skip))
max_skip = SIZE_MAX;
else if(unlikely(max_skip < 2))
max_skip = 2;
if(unlikely(!max_evict))
max_evict = SIZE_MAX;
else if(unlikely(max_evict < 2))
max_evict = 2;
size_t total_pages_evicted = 0;
size_t total_pages_skipped = 0;