pagecache.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_PAGECACHE_H
  3. #define NETDATA_PAGECACHE_H
  4. #include "rrdengine.h"
  5. /* Forward declarations */
  6. struct rrdengine_instance;
  7. struct extent_info;
  8. struct rrdeng_page_descr;
  9. #define INVALID_TIME (0)
  10. /* Page flags */
  11. #define RRD_PAGE_DIRTY (1LU << 0)
  12. #define RRD_PAGE_LOCKED (1LU << 1)
  13. #define RRD_PAGE_READ_PENDING (1LU << 2)
  14. #define RRD_PAGE_WRITE_PENDING (1LU << 3)
  15. #define RRD_PAGE_POPULATED (1LU << 4)
  16. struct page_cache_descr {
  17. struct rrdeng_page_descr *descr; /* parent descriptor */
  18. void *page;
  19. unsigned long flags;
  20. struct page_cache_descr *prev; /* LRU */
  21. struct page_cache_descr *next; /* LRU */
  22. unsigned refcnt;
  23. uv_mutex_t mutex; /* always take it after the page cache lock or after the commit lock */
  24. uv_cond_t cond;
  25. unsigned waiters;
  26. };
  27. /* Page cache descriptor flags, state = 0 means no descriptor */
  28. #define PG_CACHE_DESCR_ALLOCATED (1LU << 0)
  29. #define PG_CACHE_DESCR_DESTROY (1LU << 1)
  30. #define PG_CACHE_DESCR_LOCKED (1LU << 2)
  31. #define PG_CACHE_DESCR_SHIFT (3)
  32. #define PG_CACHE_DESCR_USERS_MASK (((unsigned long)-1) << PG_CACHE_DESCR_SHIFT)
  33. #define PG_CACHE_DESCR_FLAGS_MASK (((unsigned long)-1) >> (BITS_PER_ULONG - PG_CACHE_DESCR_SHIFT))
  34. /*
  35. * Page cache descriptor state bits (works for both 32-bit and 64-bit architectures):
  36. *
  37. * 63 ... 31 ... 3 | 2 | 1 | 0|
  38. * -----------------------------+------------+------------+-----------|
  39. * number of descriptor users | DESTROY | LOCKED | ALLOCATED |
  40. */
  41. struct rrdeng_page_descr {
  42. uuid_t *id; /* never changes */
  43. struct extent_info *extent;
  44. /* points to ephemeral page cache descriptor if the page resides in the cache */
  45. struct page_cache_descr *pg_cache_descr;
  46. /* Compare-And-Swap target for page cache descriptor allocation algorithm */
  47. volatile unsigned long pg_cache_descr_state;
  48. /* page information */
  49. usec_t start_time;
  50. usec_t end_time;
  51. uint32_t page_length;
  52. };
  53. #define PAGE_INFO_SCRATCH_SZ (8)
  54. struct rrdeng_page_info {
  55. uint8_t scratch[PAGE_INFO_SCRATCH_SZ]; /* scratch area to be used by page-cache users */
  56. usec_t start_time;
  57. usec_t end_time;
  58. uint32_t page_length;
  59. };
  60. /* returns 1 for success, 0 for failure */
  61. typedef int pg_cache_page_info_filter_t(struct rrdeng_page_descr *);
  62. #define PAGE_CACHE_MAX_PRELOAD_PAGES (256)
  63. /* maps time ranges to pages */
  64. struct pg_cache_page_index {
  65. uuid_t id;
  66. /*
  67. * care: JudyL_array indices are converted from useconds to seconds to fit in one word in 32-bit architectures
  68. * TODO: examine if we want to support better granularity than seconds
  69. */
  70. Pvoid_t JudyL_array;
  71. uv_rwlock_t lock;
  72. /*
  73. * Only one effective writer, data deletion workqueue.
  74. * It's also written during the DB loading phase.
  75. */
  76. usec_t oldest_time;
  77. /*
  78. * Only one effective writer, data collection thread.
  79. * It's also written by the data deletion workqueue when data collection is disabled for this metric.
  80. */
  81. usec_t latest_time;
  82. struct pg_cache_page_index *prev;
  83. };
  84. /* maps UUIDs to page indices */
  85. struct pg_cache_metrics_index {
  86. uv_rwlock_t lock;
  87. Pvoid_t JudyHS_array;
  88. struct pg_cache_page_index *last_page_index;
  89. };
  90. /* gathers dirty pages to be written on disk */
  91. struct pg_cache_committed_page_index {
  92. uv_rwlock_t lock;
  93. Pvoid_t JudyL_array;
  94. /*
  95. * Dirty page correlation ID is a hint. Dirty pages that are correlated should have
  96. * a small correlation ID difference. Dirty pages in memory should never have the
  97. * same ID at the same time for correctness.
  98. */
  99. Word_t latest_corr_id;
  100. unsigned nr_committed_pages;
  101. };
  102. /*
  103. * Gathers populated pages to be evicted.
  104. * Relies on page cache descriptors being there as it uses their memory.
  105. */
  106. struct pg_cache_replaceQ {
  107. uv_rwlock_t lock; /* LRU lock */
  108. struct page_cache_descr *head; /* LRU */
  109. struct page_cache_descr *tail; /* MRU */
  110. };
  111. struct page_cache { /* TODO: add statistics */
  112. uv_rwlock_t pg_cache_rwlock; /* page cache lock */
  113. struct pg_cache_metrics_index metrics_index;
  114. struct pg_cache_committed_page_index committed_page_index;
  115. struct pg_cache_replaceQ replaceQ;
  116. unsigned page_descriptors;
  117. unsigned populated_pages;
  118. };
  119. extern void pg_cache_wake_up_waiters_unsafe(struct rrdeng_page_descr *descr);
  120. extern void pg_cache_wake_up_waiters(struct rrdengine_instance *ctx, struct rrdeng_page_descr *descr);
  121. extern void pg_cache_wait_event_unsafe(struct rrdeng_page_descr *descr);
  122. extern unsigned long pg_cache_wait_event(struct rrdengine_instance *ctx, struct rrdeng_page_descr *descr);
  123. extern void pg_cache_replaceQ_insert(struct rrdengine_instance *ctx,
  124. struct rrdeng_page_descr *descr);
  125. extern void pg_cache_replaceQ_delete(struct rrdengine_instance *ctx,
  126. struct rrdeng_page_descr *descr);
  127. extern void pg_cache_replaceQ_set_hot(struct rrdengine_instance *ctx,
  128. struct rrdeng_page_descr *descr);
  129. extern struct rrdeng_page_descr *pg_cache_create_descr(void);
  130. extern int pg_cache_try_get_unsafe(struct rrdeng_page_descr *descr, int exclusive_access);
  131. extern void pg_cache_put_unsafe(struct rrdeng_page_descr *descr);
  132. extern void pg_cache_put(struct rrdengine_instance *ctx, struct rrdeng_page_descr *descr);
  133. extern void pg_cache_insert(struct rrdengine_instance *ctx, struct pg_cache_page_index *index,
  134. struct rrdeng_page_descr *descr);
  135. extern void pg_cache_punch_hole(struct rrdengine_instance *ctx, struct rrdeng_page_descr *descr, uint8_t remove_dirty,
  136. uint8_t is_exclusive_holder);
  137. extern usec_t pg_cache_oldest_time_in_range(struct rrdengine_instance *ctx, uuid_t *id,
  138. usec_t start_time, usec_t end_time);
  139. extern void pg_cache_get_filtered_info_prev(struct rrdengine_instance *ctx, struct pg_cache_page_index *page_index,
  140. usec_t point_in_time, pg_cache_page_info_filter_t *filter,
  141. struct rrdeng_page_info *page_info);
  142. extern unsigned
  143. pg_cache_preload(struct rrdengine_instance *ctx, uuid_t *id, usec_t start_time, usec_t end_time,
  144. struct rrdeng_page_info **page_info_arrayp, struct pg_cache_page_index **ret_page_indexp);
  145. extern struct rrdeng_page_descr *
  146. pg_cache_lookup(struct rrdengine_instance *ctx, struct pg_cache_page_index *index, uuid_t *id,
  147. usec_t point_in_time);
  148. extern struct rrdeng_page_descr *
  149. pg_cache_lookup_next(struct rrdengine_instance *ctx, struct pg_cache_page_index *index, uuid_t *id,
  150. usec_t start_time, usec_t end_time);
  151. extern struct pg_cache_page_index *create_page_index(uuid_t *id);
  152. extern void init_page_cache(struct rrdengine_instance *ctx);
  153. extern void free_page_cache(struct rrdengine_instance *ctx);
  154. extern void pg_cache_add_new_metric_time(struct pg_cache_page_index *page_index, struct rrdeng_page_descr *descr);
  155. extern void pg_cache_update_metric_times(struct pg_cache_page_index *page_index);
  156. extern unsigned long pg_cache_hard_limit(struct rrdengine_instance *ctx);
  157. extern unsigned long pg_cache_soft_limit(struct rrdengine_instance *ctx);
  158. extern unsigned long pg_cache_committed_hard_limit(struct rrdengine_instance *ctx);
  159. static inline void
  160. pg_cache_atomic_get_pg_info(struct rrdeng_page_descr *descr, usec_t *end_timep, uint32_t *page_lengthp)
  161. {
  162. usec_t end_time, old_end_time;
  163. uint32_t page_length;
  164. if (NULL == descr->extent) {
  165. /* this page is currently being modified, get consistent info locklessly */
  166. do {
  167. end_time = descr->end_time;
  168. __sync_synchronize();
  169. old_end_time = end_time;
  170. page_length = descr->page_length;
  171. __sync_synchronize();
  172. end_time = descr->end_time;
  173. __sync_synchronize();
  174. } while ((end_time != old_end_time || (end_time & 1) != 0));
  175. *end_timep = end_time;
  176. *page_lengthp = page_length;
  177. } else {
  178. *end_timep = descr->end_time;
  179. *page_lengthp = descr->page_length;
  180. }
  181. }
  182. /* The caller must hold a reference to the page and must have already set the new data */
  183. static inline void pg_cache_atomic_set_pg_info(struct rrdeng_page_descr *descr, usec_t end_time, uint32_t page_length)
  184. {
  185. assert(!(end_time & 1));
  186. __sync_synchronize();
  187. descr->end_time |= 1; /* mark start of uncertainty period by adding 1 microsecond */
  188. __sync_synchronize();
  189. descr->page_length = page_length;
  190. __sync_synchronize();
  191. descr->end_time = end_time; /* mark end of uncertainty period */
  192. }
  193. #endif /* NETDATA_PAGECACHE_H */