pagecache.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. Word_t page_count;
  72. unsigned short writers;
  73. uv_rwlock_t lock;
  74. /*
  75. * Only one effective writer, data deletion workqueue.
  76. * It's also written during the DB loading phase.
  77. */
  78. usec_t oldest_time;
  79. /*
  80. * Only one effective writer, data collection thread.
  81. * It's also written by the data deletion workqueue when data collection is disabled for this metric.
  82. */
  83. usec_t latest_time;
  84. struct pg_cache_page_index *prev;
  85. };
  86. /* maps UUIDs to page indices */
  87. struct pg_cache_metrics_index {
  88. uv_rwlock_t lock;
  89. Pvoid_t JudyHS_array;
  90. struct pg_cache_page_index *last_page_index;
  91. };
  92. /* gathers dirty pages to be written on disk */
  93. struct pg_cache_committed_page_index {
  94. uv_rwlock_t lock;
  95. Pvoid_t JudyL_array;
  96. /*
  97. * Dirty page correlation ID is a hint. Dirty pages that are correlated should have
  98. * a small correlation ID difference. Dirty pages in memory should never have the
  99. * same ID at the same time for correctness.
  100. */
  101. Word_t latest_corr_id;
  102. unsigned nr_committed_pages;
  103. };
  104. /*
  105. * Gathers populated pages to be evicted.
  106. * Relies on page cache descriptors being there as it uses their memory.
  107. */
  108. struct pg_cache_replaceQ {
  109. uv_rwlock_t lock; /* LRU lock */
  110. struct page_cache_descr *head; /* LRU */
  111. struct page_cache_descr *tail; /* MRU */
  112. };
  113. struct page_cache { /* TODO: add statistics */
  114. uv_rwlock_t pg_cache_rwlock; /* page cache lock */
  115. struct pg_cache_metrics_index metrics_index;
  116. struct pg_cache_committed_page_index committed_page_index;
  117. struct pg_cache_replaceQ replaceQ;
  118. unsigned page_descriptors;
  119. unsigned populated_pages;
  120. };
  121. extern void pg_cache_wake_up_waiters_unsafe(struct rrdeng_page_descr *descr);
  122. extern void pg_cache_wake_up_waiters(struct rrdengine_instance *ctx, struct rrdeng_page_descr *descr);
  123. extern void pg_cache_wait_event_unsafe(struct rrdeng_page_descr *descr);
  124. extern unsigned long pg_cache_wait_event(struct rrdengine_instance *ctx, struct rrdeng_page_descr *descr);
  125. extern void pg_cache_replaceQ_insert(struct rrdengine_instance *ctx,
  126. struct rrdeng_page_descr *descr);
  127. extern void pg_cache_replaceQ_delete(struct rrdengine_instance *ctx,
  128. struct rrdeng_page_descr *descr);
  129. extern void pg_cache_replaceQ_set_hot(struct rrdengine_instance *ctx,
  130. struct rrdeng_page_descr *descr);
  131. extern struct rrdeng_page_descr *pg_cache_create_descr(void);
  132. extern int pg_cache_try_get_unsafe(struct rrdeng_page_descr *descr, int exclusive_access);
  133. extern void pg_cache_put_unsafe(struct rrdeng_page_descr *descr);
  134. extern void pg_cache_put(struct rrdengine_instance *ctx, struct rrdeng_page_descr *descr);
  135. extern void pg_cache_insert(struct rrdengine_instance *ctx, struct pg_cache_page_index *index,
  136. struct rrdeng_page_descr *descr);
  137. extern uint8_t pg_cache_punch_hole(struct rrdengine_instance *ctx, struct rrdeng_page_descr *descr,
  138. uint8_t remove_dirty, uint8_t is_exclusive_holder, uuid_t *metric_id);
  139. extern usec_t pg_cache_oldest_time_in_range(struct rrdengine_instance *ctx, uuid_t *id,
  140. usec_t start_time, usec_t end_time);
  141. extern void pg_cache_get_filtered_info_prev(struct rrdengine_instance *ctx, struct pg_cache_page_index *page_index,
  142. usec_t point_in_time, pg_cache_page_info_filter_t *filter,
  143. struct rrdeng_page_info *page_info);
  144. extern struct rrdeng_page_descr *pg_cache_lookup_unpopulated_and_lock(struct rrdengine_instance *ctx, uuid_t *id,
  145. usec_t start_time);
  146. extern unsigned
  147. pg_cache_preload(struct rrdengine_instance *ctx, uuid_t *id, usec_t start_time, usec_t end_time,
  148. struct rrdeng_page_info **page_info_arrayp, struct pg_cache_page_index **ret_page_indexp);
  149. extern struct rrdeng_page_descr *
  150. pg_cache_lookup(struct rrdengine_instance *ctx, struct pg_cache_page_index *index, uuid_t *id,
  151. usec_t point_in_time);
  152. extern struct rrdeng_page_descr *
  153. pg_cache_lookup_next(struct rrdengine_instance *ctx, struct pg_cache_page_index *index, uuid_t *id,
  154. usec_t start_time, usec_t end_time);
  155. extern struct pg_cache_page_index *create_page_index(uuid_t *id);
  156. extern void init_page_cache(struct rrdengine_instance *ctx);
  157. extern void free_page_cache(struct rrdengine_instance *ctx);
  158. extern void pg_cache_add_new_metric_time(struct pg_cache_page_index *page_index, struct rrdeng_page_descr *descr);
  159. extern void pg_cache_update_metric_times(struct pg_cache_page_index *page_index);
  160. extern unsigned long pg_cache_hard_limit(struct rrdengine_instance *ctx);
  161. extern unsigned long pg_cache_soft_limit(struct rrdengine_instance *ctx);
  162. extern unsigned long pg_cache_committed_hard_limit(struct rrdengine_instance *ctx);
  163. static inline void
  164. pg_cache_atomic_get_pg_info(struct rrdeng_page_descr *descr, usec_t *end_timep, uint32_t *page_lengthp)
  165. {
  166. usec_t end_time, old_end_time;
  167. uint32_t page_length;
  168. if (NULL == descr->extent) {
  169. /* this page is currently being modified, get consistent info locklessly */
  170. do {
  171. end_time = descr->end_time;
  172. __sync_synchronize();
  173. old_end_time = end_time;
  174. page_length = descr->page_length;
  175. __sync_synchronize();
  176. end_time = descr->end_time;
  177. __sync_synchronize();
  178. } while ((end_time != old_end_time || (end_time & 1) != 0));
  179. *end_timep = end_time;
  180. *page_lengthp = page_length;
  181. } else {
  182. *end_timep = descr->end_time;
  183. *page_lengthp = descr->page_length;
  184. }
  185. }
  186. /* The caller must hold a reference to the page and must have already set the new data */
  187. static inline void pg_cache_atomic_set_pg_info(struct rrdeng_page_descr *descr, usec_t end_time, uint32_t page_length)
  188. {
  189. fatal_assert(!(end_time & 1));
  190. __sync_synchronize();
  191. descr->end_time |= 1; /* mark start of uncertainty period by adding 1 microsecond */
  192. __sync_synchronize();
  193. descr->page_length = page_length;
  194. __sync_synchronize();
  195. descr->end_time = end_time; /* mark end of uncertainty period */
  196. }
  197. #endif /* NETDATA_PAGECACHE_H */