pagecache.h 9.0 KB

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