metric.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "metric.h"
  3. #include "cache.h"
  4. #include "rrddiskprotocol.h"
  5. typedef int32_t REFCOUNT;
  6. #define REFCOUNT_DELETING (-100)
  7. struct metric {
  8. uuid_t uuid; // never changes
  9. Word_t section; // never changes
  10. time_t first_time_s; // the timestamp of the oldest point in the database
  11. time_t latest_time_s_clean; // the timestamp of the newest point in the database
  12. time_t latest_time_s_hot; // the timestamp of the latest point that has been collected (not yet stored)
  13. uint32_t latest_update_every_s; // the latest data collection frequency
  14. pid_t writer;
  15. uint8_t partition;
  16. REFCOUNT refcount;
  17. // THIS IS allocated with malloc()
  18. // YOU HAVE TO INITIALIZE IT YOURSELF !
  19. };
  20. #define set_metric_field_with_condition(field, value, condition) ({ \
  21. typeof(field) _current = __atomic_load_n(&(field), __ATOMIC_RELAXED); \
  22. typeof(field) _wanted = value; \
  23. bool did_it = true; \
  24. \
  25. do { \
  26. if((condition) && (_current != _wanted)) { \
  27. ; \
  28. } \
  29. else { \
  30. did_it = false; \
  31. break; \
  32. } \
  33. } while(!__atomic_compare_exchange_n(&(field), &_current, _wanted, \
  34. false, __ATOMIC_RELAXED, __ATOMIC_RELAXED)); \
  35. \
  36. did_it; \
  37. })
  38. static struct aral_statistics mrg_aral_statistics;
  39. struct mrg {
  40. size_t partitions;
  41. struct mrg_partition {
  42. ARAL *aral; // not protected by our spinlock - it has its own
  43. RW_SPINLOCK rw_spinlock;
  44. Pvoid_t uuid_judy; // JudyHS: each UUID has a JudyL of sections (tiers)
  45. struct mrg_statistics stats;
  46. } index[];
  47. };
  48. static inline void MRG_STATS_DUPLICATE_ADD(MRG *mrg, size_t partition) {
  49. mrg->index[partition].stats.additions_duplicate++;
  50. }
  51. static inline void MRG_STATS_ADDED_METRIC(MRG *mrg, size_t partition) {
  52. mrg->index[partition].stats.entries++;
  53. mrg->index[partition].stats.additions++;
  54. mrg->index[partition].stats.size += sizeof(METRIC);
  55. }
  56. static inline void MRG_STATS_DELETED_METRIC(MRG *mrg, size_t partition) {
  57. mrg->index[partition].stats.entries--;
  58. mrg->index[partition].stats.size -= sizeof(METRIC);
  59. mrg->index[partition].stats.deletions++;
  60. }
  61. static inline void MRG_STATS_SEARCH_HIT(MRG *mrg, size_t partition) {
  62. __atomic_add_fetch(&mrg->index[partition].stats.search_hits, 1, __ATOMIC_RELAXED);
  63. }
  64. static inline void MRG_STATS_SEARCH_MISS(MRG *mrg, size_t partition) {
  65. __atomic_add_fetch(&mrg->index[partition].stats.search_misses, 1, __ATOMIC_RELAXED);
  66. }
  67. static inline void MRG_STATS_DELETE_MISS(MRG *mrg, size_t partition) {
  68. mrg->index[partition].stats.delete_misses++;
  69. }
  70. #define mrg_index_read_lock(mrg, partition) rw_spinlock_read_lock(&(mrg)->index[partition].rw_spinlock)
  71. #define mrg_index_read_unlock(mrg, partition) rw_spinlock_read_unlock(&(mrg)->index[partition].rw_spinlock)
  72. #define mrg_index_write_lock(mrg, partition) rw_spinlock_write_lock(&(mrg)->index[partition].rw_spinlock)
  73. #define mrg_index_write_unlock(mrg, partition) rw_spinlock_write_unlock(&(mrg)->index[partition].rw_spinlock)
  74. static inline void mrg_stats_size_judyl_change(MRG *mrg, size_t mem_before_judyl, size_t mem_after_judyl, size_t partition) {
  75. if(mem_after_judyl > mem_before_judyl)
  76. __atomic_add_fetch(&mrg->index[partition].stats.size, mem_after_judyl - mem_before_judyl, __ATOMIC_RELAXED);
  77. else if(mem_after_judyl < mem_before_judyl)
  78. __atomic_sub_fetch(&mrg->index[partition].stats.size, mem_before_judyl - mem_after_judyl, __ATOMIC_RELAXED);
  79. }
  80. static inline void mrg_stats_size_judyhs_added_uuid(MRG *mrg, size_t partition) {
  81. __atomic_add_fetch(&mrg->index[partition].stats.size, JUDYHS_INDEX_SIZE_ESTIMATE(sizeof(uuid_t)), __ATOMIC_RELAXED);
  82. }
  83. static inline void mrg_stats_size_judyhs_removed_uuid(MRG *mrg, size_t partition) {
  84. __atomic_sub_fetch(&mrg->index[partition].stats.size, JUDYHS_INDEX_SIZE_ESTIMATE(sizeof(uuid_t)), __ATOMIC_RELAXED);
  85. }
  86. static inline size_t uuid_partition(MRG *mrg __maybe_unused, uuid_t *uuid) {
  87. uint8_t *u = (uint8_t *)uuid;
  88. size_t n;
  89. memcpy(&n, &u[UUID_SZ - sizeof(size_t)], sizeof(size_t));
  90. return n % mrg->partitions;
  91. }
  92. static inline time_t mrg_metric_get_first_time_s_smart(MRG *mrg __maybe_unused, METRIC *metric) {
  93. time_t first_time_s = __atomic_load_n(&metric->first_time_s, __ATOMIC_RELAXED);
  94. if(first_time_s <= 0) {
  95. first_time_s = __atomic_load_n(&metric->latest_time_s_clean, __ATOMIC_RELAXED);
  96. if(first_time_s <= 0)
  97. first_time_s = __atomic_load_n(&metric->latest_time_s_hot, __ATOMIC_RELAXED);
  98. if(first_time_s <= 0)
  99. first_time_s = 0;
  100. else
  101. __atomic_store_n(&metric->first_time_s, first_time_s, __ATOMIC_RELAXED);
  102. }
  103. return first_time_s;
  104. }
  105. static inline REFCOUNT metric_acquire(MRG *mrg __maybe_unused, METRIC *metric) {
  106. size_t partition = metric->partition;
  107. REFCOUNT expected = __atomic_load_n(&metric->refcount, __ATOMIC_RELAXED);
  108. REFCOUNT refcount;
  109. do {
  110. if(expected < 0)
  111. fatal("METRIC: refcount is %d (negative) during acquire", metric->refcount);
  112. refcount = expected + 1;
  113. } while(!__atomic_compare_exchange_n(&metric->refcount, &expected, refcount, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  114. if(refcount == 1)
  115. __atomic_add_fetch(&mrg->index[partition].stats.entries_referenced, 1, __ATOMIC_RELAXED);
  116. __atomic_add_fetch(&mrg->index[partition].stats.current_references, 1, __ATOMIC_RELAXED);
  117. return refcount;
  118. }
  119. static inline bool metric_release_and_can_be_deleted(MRG *mrg __maybe_unused, METRIC *metric) {
  120. size_t partition = metric->partition;
  121. REFCOUNT expected = __atomic_load_n(&metric->refcount, __ATOMIC_RELAXED);
  122. REFCOUNT refcount;
  123. do {
  124. if(expected <= 0)
  125. fatal("METRIC: refcount is %d (zero or negative) during release", metric->refcount);
  126. refcount = expected - 1;
  127. } while(!__atomic_compare_exchange_n(&metric->refcount, &expected, refcount, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  128. if(unlikely(!refcount))
  129. __atomic_sub_fetch(&mrg->index[partition].stats.entries_referenced, 1, __ATOMIC_RELAXED);
  130. __atomic_sub_fetch(&mrg->index[partition].stats.current_references, 1, __ATOMIC_RELAXED);
  131. time_t first, last;
  132. mrg_metric_get_retention(mrg, metric, &first, &last, NULL);
  133. return (!first || !last || first > last);
  134. }
  135. static inline METRIC *metric_add_and_acquire(MRG *mrg, MRG_ENTRY *entry, bool *ret) {
  136. size_t partition = uuid_partition(mrg, entry->uuid);
  137. METRIC *allocation = aral_mallocz(mrg->index[partition].aral);
  138. mrg_index_write_lock(mrg, partition);
  139. size_t mem_before_judyl, mem_after_judyl;
  140. Pvoid_t *sections_judy_pptr = JudyHSIns(&mrg->index[partition].uuid_judy, entry->uuid, sizeof(uuid_t), PJE0);
  141. if(unlikely(!sections_judy_pptr || sections_judy_pptr == PJERR))
  142. fatal("DBENGINE METRIC: corrupted UUIDs JudyHS array");
  143. if(unlikely(!*sections_judy_pptr))
  144. mrg_stats_size_judyhs_added_uuid(mrg, partition);
  145. mem_before_judyl = JudyLMemUsed(*sections_judy_pptr);
  146. Pvoid_t *PValue = JudyLIns(sections_judy_pptr, entry->section, PJE0);
  147. mem_after_judyl = JudyLMemUsed(*sections_judy_pptr);
  148. mrg_stats_size_judyl_change(mrg, mem_before_judyl, mem_after_judyl, partition);
  149. if(unlikely(!PValue || PValue == PJERR))
  150. fatal("DBENGINE METRIC: corrupted section JudyL array");
  151. if(unlikely(*PValue != NULL)) {
  152. METRIC *metric = *PValue;
  153. metric_acquire(mrg, metric);
  154. MRG_STATS_DUPLICATE_ADD(mrg, partition);
  155. mrg_index_write_unlock(mrg, partition);
  156. if(ret)
  157. *ret = false;
  158. aral_freez(mrg->index[partition].aral, allocation);
  159. return metric;
  160. }
  161. METRIC *metric = allocation;
  162. uuid_copy(metric->uuid, *entry->uuid);
  163. metric->section = entry->section;
  164. metric->first_time_s = MAX(0, entry->first_time_s);
  165. metric->latest_time_s_clean = MAX(0, entry->last_time_s);
  166. metric->latest_time_s_hot = 0;
  167. metric->latest_update_every_s = entry->latest_update_every_s;
  168. metric->writer = 0;
  169. metric->refcount = 0;
  170. metric->partition = partition;
  171. metric_acquire(mrg, metric);
  172. *PValue = metric;
  173. MRG_STATS_ADDED_METRIC(mrg, partition);
  174. mrg_index_write_unlock(mrg, partition);
  175. if(ret)
  176. *ret = true;
  177. return metric;
  178. }
  179. static inline METRIC *metric_get_and_acquire(MRG *mrg, uuid_t *uuid, Word_t section) {
  180. size_t partition = uuid_partition(mrg, uuid);
  181. mrg_index_read_lock(mrg, partition);
  182. Pvoid_t *sections_judy_pptr = JudyHSGet(mrg->index[partition].uuid_judy, uuid, sizeof(uuid_t));
  183. if(unlikely(!sections_judy_pptr)) {
  184. mrg_index_read_unlock(mrg, partition);
  185. MRG_STATS_SEARCH_MISS(mrg, partition);
  186. return NULL;
  187. }
  188. Pvoid_t *PValue = JudyLGet(*sections_judy_pptr, section, PJE0);
  189. if(unlikely(!PValue)) {
  190. mrg_index_read_unlock(mrg, partition);
  191. MRG_STATS_SEARCH_MISS(mrg, partition);
  192. return NULL;
  193. }
  194. METRIC *metric = *PValue;
  195. metric_acquire(mrg, metric);
  196. mrg_index_read_unlock(mrg, partition);
  197. MRG_STATS_SEARCH_HIT(mrg, partition);
  198. return metric;
  199. }
  200. static inline bool acquired_metric_del(MRG *mrg, METRIC *metric) {
  201. size_t partition = metric->partition;
  202. size_t mem_before_judyl, mem_after_judyl;
  203. mrg_index_write_lock(mrg, partition);
  204. if(!metric_release_and_can_be_deleted(mrg, metric)) {
  205. mrg->index[partition].stats.delete_having_retention_or_referenced++;
  206. mrg_index_write_unlock(mrg, partition);
  207. return false;
  208. }
  209. Pvoid_t *sections_judy_pptr = JudyHSGet(mrg->index[partition].uuid_judy, &metric->uuid, sizeof(uuid_t));
  210. if(unlikely(!sections_judy_pptr || !*sections_judy_pptr)) {
  211. MRG_STATS_DELETE_MISS(mrg, partition);
  212. mrg_index_write_unlock(mrg, partition);
  213. return false;
  214. }
  215. mem_before_judyl = JudyLMemUsed(*sections_judy_pptr);
  216. int rc = JudyLDel(sections_judy_pptr, metric->section, PJE0);
  217. mem_after_judyl = JudyLMemUsed(*sections_judy_pptr);
  218. mrg_stats_size_judyl_change(mrg, mem_before_judyl, mem_after_judyl, partition);
  219. if(unlikely(!rc)) {
  220. MRG_STATS_DELETE_MISS(mrg, partition);
  221. mrg_index_write_unlock(mrg, partition);
  222. return false;
  223. }
  224. if(!*sections_judy_pptr) {
  225. rc = JudyHSDel(&mrg->index[partition].uuid_judy, &metric->uuid, sizeof(uuid_t), PJE0);
  226. if(unlikely(!rc))
  227. fatal("DBENGINE METRIC: cannot delete UUID from JudyHS");
  228. mrg_stats_size_judyhs_removed_uuid(mrg, partition);
  229. }
  230. MRG_STATS_DELETED_METRIC(mrg, partition);
  231. mrg_index_write_unlock(mrg, partition);
  232. aral_freez(mrg->index[partition].aral, metric);
  233. return true;
  234. }
  235. // ----------------------------------------------------------------------------
  236. // public API
  237. inline MRG *mrg_create(ssize_t partitions) {
  238. if(partitions < 1)
  239. partitions = get_netdata_cpus();
  240. MRG *mrg = callocz(1, sizeof(MRG) + sizeof(struct mrg_partition) * partitions);
  241. mrg->partitions = partitions;
  242. for(size_t i = 0; i < mrg->partitions ; i++) {
  243. rw_spinlock_init(&mrg->index[i].rw_spinlock);
  244. char buf[ARAL_MAX_NAME + 1];
  245. snprintfz(buf, ARAL_MAX_NAME, "mrg[%zu]", i);
  246. mrg->index[i].aral = aral_create(buf, sizeof(METRIC), 0, 16384, &mrg_aral_statistics, NULL, NULL, false, false);
  247. }
  248. return mrg;
  249. }
  250. inline size_t mrg_aral_structures(void) {
  251. return aral_structures_from_stats(&mrg_aral_statistics);
  252. }
  253. inline size_t mrg_aral_overhead(void) {
  254. return aral_overhead_from_stats(&mrg_aral_statistics);
  255. }
  256. inline void mrg_destroy(MRG *mrg __maybe_unused) {
  257. // no destruction possible
  258. // we can't traverse the metrics list
  259. // to delete entries, the caller needs to keep pointers to them
  260. // and delete them one by one
  261. ;
  262. }
  263. inline METRIC *mrg_metric_add_and_acquire(MRG *mrg, MRG_ENTRY entry, bool *ret) {
  264. // internal_fatal(entry.latest_time_s > max_acceptable_collected_time(),
  265. // "DBENGINE METRIC: metric latest time is in the future");
  266. return metric_add_and_acquire(mrg, &entry, ret);
  267. }
  268. inline METRIC *mrg_metric_get_and_acquire(MRG *mrg, uuid_t *uuid, Word_t section) {
  269. return metric_get_and_acquire(mrg, uuid, section);
  270. }
  271. inline bool mrg_metric_release_and_delete(MRG *mrg, METRIC *metric) {
  272. return acquired_metric_del(mrg, metric);
  273. }
  274. inline METRIC *mrg_metric_dup(MRG *mrg, METRIC *metric) {
  275. metric_acquire(mrg, metric);
  276. return metric;
  277. }
  278. inline bool mrg_metric_release(MRG *mrg, METRIC *metric) {
  279. return metric_release_and_can_be_deleted(mrg, metric);
  280. }
  281. inline Word_t mrg_metric_id(MRG *mrg __maybe_unused, METRIC *metric) {
  282. return (Word_t)metric;
  283. }
  284. inline uuid_t *mrg_metric_uuid(MRG *mrg __maybe_unused, METRIC *metric) {
  285. return &metric->uuid;
  286. }
  287. inline Word_t mrg_metric_section(MRG *mrg __maybe_unused, METRIC *metric) {
  288. return metric->section;
  289. }
  290. inline bool mrg_metric_set_first_time_s(MRG *mrg __maybe_unused, METRIC *metric, time_t first_time_s) {
  291. internal_fatal(first_time_s < 0, "DBENGINE METRIC: timestamp is negative");
  292. if(unlikely(first_time_s < 0))
  293. return false;
  294. __atomic_store_n(&metric->first_time_s, first_time_s, __ATOMIC_RELAXED);
  295. return true;
  296. }
  297. inline void mrg_metric_expand_retention(MRG *mrg __maybe_unused, METRIC *metric, time_t first_time_s, time_t last_time_s, uint32_t update_every_s) {
  298. internal_fatal(first_time_s < 0 || last_time_s < 0,
  299. "DBENGINE METRIC: timestamp is negative");
  300. internal_fatal(first_time_s > max_acceptable_collected_time(),
  301. "DBENGINE METRIC: metric first time is in the future");
  302. internal_fatal(last_time_s > max_acceptable_collected_time(),
  303. "DBENGINE METRIC: metric last time is in the future");
  304. if(first_time_s > 0)
  305. set_metric_field_with_condition(metric->first_time_s, first_time_s, _current <= 0 || _wanted < _current);
  306. if(last_time_s > 0) {
  307. if(set_metric_field_with_condition(metric->latest_time_s_clean, last_time_s, _current <= 0 || _wanted > _current) &&
  308. update_every_s > 0)
  309. // set the latest update every too
  310. set_metric_field_with_condition(metric->latest_update_every_s, update_every_s, true);
  311. }
  312. else if(update_every_s > 0)
  313. // set it only if it is invalid
  314. set_metric_field_with_condition(metric->latest_update_every_s, update_every_s, _current <= 0);
  315. }
  316. inline bool mrg_metric_set_first_time_s_if_bigger(MRG *mrg __maybe_unused, METRIC *metric, time_t first_time_s) {
  317. internal_fatal(first_time_s < 0, "DBENGINE METRIC: timestamp is negative");
  318. return set_metric_field_with_condition(metric->first_time_s, first_time_s, _wanted > _current);
  319. }
  320. inline time_t mrg_metric_get_first_time_s(MRG *mrg __maybe_unused, METRIC *metric) {
  321. return mrg_metric_get_first_time_s_smart(mrg, metric);
  322. }
  323. inline void mrg_metric_get_retention(MRG *mrg __maybe_unused, METRIC *metric, time_t *first_time_s, time_t *last_time_s, uint32_t *update_every_s) {
  324. time_t clean = __atomic_load_n(&metric->latest_time_s_clean, __ATOMIC_RELAXED);
  325. time_t hot = __atomic_load_n(&metric->latest_time_s_hot, __ATOMIC_RELAXED);
  326. *last_time_s = MAX(clean, hot);
  327. *first_time_s = mrg_metric_get_first_time_s_smart(mrg, metric);
  328. if (update_every_s)
  329. *update_every_s = __atomic_load_n(&metric->latest_update_every_s, __ATOMIC_RELAXED);
  330. }
  331. inline bool mrg_metric_set_clean_latest_time_s(MRG *mrg __maybe_unused, METRIC *metric, time_t latest_time_s) {
  332. internal_fatal(latest_time_s < 0, "DBENGINE METRIC: timestamp is negative");
  333. // internal_fatal(latest_time_s > max_acceptable_collected_time(),
  334. // "DBENGINE METRIC: metric latest time is in the future");
  335. // internal_fatal(metric->latest_time_s_clean > latest_time_s,
  336. // "DBENGINE METRIC: metric new clean latest time is older than the previous one");
  337. if(latest_time_s > 0) {
  338. if(set_metric_field_with_condition(metric->latest_time_s_clean, latest_time_s, true)) {
  339. set_metric_field_with_condition(metric->first_time_s, latest_time_s, _current <= 0 || _wanted < _current);
  340. return true;
  341. }
  342. }
  343. return false;
  344. }
  345. // returns true when metric still has retention
  346. inline bool mrg_metric_zero_disk_retention(MRG *mrg __maybe_unused, METRIC *metric) {
  347. Word_t section = mrg_metric_section(mrg, metric);
  348. bool do_again = false;
  349. size_t countdown = 5;
  350. do {
  351. time_t min_first_time_s = LONG_MAX;
  352. time_t max_end_time_s = 0;
  353. PGC_PAGE *page;
  354. PGC_SEARCH method = PGC_SEARCH_FIRST;
  355. time_t page_first_time_s = 0;
  356. time_t page_end_time_s = 0;
  357. while ((page = pgc_page_get_and_acquire(main_cache, section, (Word_t)metric, page_first_time_s, method))) {
  358. method = PGC_SEARCH_NEXT;
  359. bool is_hot = pgc_is_page_hot(page);
  360. bool is_dirty = pgc_is_page_dirty(page);
  361. page_first_time_s = pgc_page_start_time_s(page);
  362. page_end_time_s = pgc_page_end_time_s(page);
  363. if ((is_hot || is_dirty) && page_first_time_s > 0 && page_first_time_s < min_first_time_s)
  364. min_first_time_s = page_first_time_s;
  365. if (is_dirty && page_end_time_s > max_end_time_s)
  366. max_end_time_s = page_end_time_s;
  367. pgc_page_release(main_cache, page);
  368. }
  369. if (min_first_time_s == LONG_MAX)
  370. min_first_time_s = 0;
  371. if (--countdown && !min_first_time_s && __atomic_load_n(&metric->latest_time_s_hot, __ATOMIC_RELAXED))
  372. do_again = true;
  373. else {
  374. internal_error(!countdown, "METRIC: giving up on updating the retention of metric without disk retention");
  375. do_again = false;
  376. set_metric_field_with_condition(metric->first_time_s, min_first_time_s, true);
  377. set_metric_field_with_condition(metric->latest_time_s_clean, max_end_time_s, true);
  378. }
  379. } while(do_again);
  380. time_t first, last;
  381. mrg_metric_get_retention(mrg, metric, &first, &last, NULL);
  382. return (first && last && first < last);
  383. }
  384. inline bool mrg_metric_set_hot_latest_time_s(MRG *mrg __maybe_unused, METRIC *metric, time_t latest_time_s) {
  385. internal_fatal(latest_time_s < 0, "DBENGINE METRIC: timestamp is negative");
  386. // internal_fatal(latest_time_s > max_acceptable_collected_time(),
  387. // "DBENGINE METRIC: metric latest time is in the future");
  388. if(likely(latest_time_s > 0)) {
  389. __atomic_store_n(&metric->latest_time_s_hot, latest_time_s, __ATOMIC_RELAXED);
  390. return true;
  391. }
  392. return false;
  393. }
  394. inline time_t mrg_metric_get_latest_time_s(MRG *mrg __maybe_unused, METRIC *metric) {
  395. time_t clean = __atomic_load_n(&metric->latest_time_s_clean, __ATOMIC_RELAXED);
  396. time_t hot = __atomic_load_n(&metric->latest_time_s_hot, __ATOMIC_RELAXED);
  397. return MAX(clean, hot);
  398. }
  399. inline bool mrg_metric_set_update_every(MRG *mrg __maybe_unused, METRIC *metric, uint32_t update_every_s) {
  400. if(update_every_s > 0)
  401. return set_metric_field_with_condition(metric->latest_update_every_s, update_every_s, true);
  402. return false;
  403. }
  404. inline bool mrg_metric_set_update_every_s_if_zero(MRG *mrg __maybe_unused, METRIC *metric, uint32_t update_every_s) {
  405. if(update_every_s > 0)
  406. return set_metric_field_with_condition(metric->latest_update_every_s, update_every_s, _current <= 0);
  407. return false;
  408. }
  409. inline uint32_t mrg_metric_get_update_every_s(MRG *mrg __maybe_unused, METRIC *metric) {
  410. return __atomic_load_n(&metric->latest_update_every_s, __ATOMIC_RELAXED);
  411. }
  412. inline bool mrg_metric_set_writer(MRG *mrg, METRIC *metric) {
  413. pid_t expected = __atomic_load_n(&metric->writer, __ATOMIC_RELAXED);
  414. pid_t wanted = gettid();
  415. bool done = true;
  416. do {
  417. if(expected != 0) {
  418. done = false;
  419. break;
  420. }
  421. } while(!__atomic_compare_exchange_n(&metric->writer, &expected, wanted, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  422. if(done)
  423. __atomic_add_fetch(&mrg->index[metric->partition].stats.writers, 1, __ATOMIC_RELAXED);
  424. else
  425. __atomic_add_fetch(&mrg->index[metric->partition].stats.writers_conflicts, 1, __ATOMIC_RELAXED);
  426. return done;
  427. }
  428. inline bool mrg_metric_clear_writer(MRG *mrg, METRIC *metric) {
  429. // this function can be called from a different thread than the one than the writer
  430. pid_t expected = __atomic_load_n(&metric->writer, __ATOMIC_RELAXED);
  431. pid_t wanted = 0;
  432. bool done = true;
  433. do {
  434. if(!expected) {
  435. done = false;
  436. break;
  437. }
  438. } while(!__atomic_compare_exchange_n(&metric->writer, &expected, wanted, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  439. if(done)
  440. __atomic_sub_fetch(&mrg->index[metric->partition].stats.writers, 1, __ATOMIC_RELAXED);
  441. return done;
  442. }
  443. inline void mrg_update_metric_retention_and_granularity_by_uuid(
  444. MRG *mrg, Word_t section, uuid_t *uuid,
  445. time_t first_time_s, time_t last_time_s,
  446. uint32_t update_every_s, time_t now_s)
  447. {
  448. if(unlikely(last_time_s > now_s)) {
  449. nd_log_limit_static_global_var(erl, 1, 0);
  450. nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING,
  451. "DBENGINE JV2: wrong last time on-disk (%ld - %ld, now %ld), "
  452. "fixing last time to now",
  453. first_time_s, last_time_s, now_s);
  454. last_time_s = now_s;
  455. }
  456. if (unlikely(first_time_s > last_time_s)) {
  457. nd_log_limit_static_global_var(erl, 1, 0);
  458. nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING,
  459. "DBENGINE JV2: wrong first time on-disk (%ld - %ld, now %ld), "
  460. "fixing first time to last time",
  461. first_time_s, last_time_s, now_s);
  462. first_time_s = last_time_s;
  463. }
  464. if (unlikely(first_time_s == 0 || last_time_s == 0)) {
  465. nd_log_limit_static_global_var(erl, 1, 0);
  466. nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING,
  467. "DBENGINE JV2: zero on-disk timestamps (%ld - %ld, now %ld), "
  468. "using them as-is",
  469. first_time_s, last_time_s, now_s);
  470. }
  471. bool added = false;
  472. METRIC *metric = mrg_metric_get_and_acquire(mrg, uuid, section);
  473. if (!metric) {
  474. MRG_ENTRY entry = {
  475. .uuid = uuid,
  476. .section = section,
  477. .first_time_s = first_time_s,
  478. .last_time_s = last_time_s,
  479. .latest_update_every_s = update_every_s
  480. };
  481. metric = mrg_metric_add_and_acquire(mrg, entry, &added);
  482. }
  483. if (likely(!added))
  484. mrg_metric_expand_retention(mrg, metric, first_time_s, last_time_s, update_every_s);
  485. mrg_metric_release(mrg, metric);
  486. }
  487. inline void mrg_get_statistics(MRG *mrg, struct mrg_statistics *s) {
  488. memset(s, 0, sizeof(struct mrg_statistics));
  489. for(size_t i = 0; i < mrg->partitions ;i++) {
  490. s->entries += __atomic_load_n(&mrg->index[i].stats.entries, __ATOMIC_RELAXED);
  491. s->entries_referenced += __atomic_load_n(&mrg->index[i].stats.entries_referenced, __ATOMIC_RELAXED);
  492. s->size += __atomic_load_n(&mrg->index[i].stats.size, __ATOMIC_RELAXED);
  493. s->current_references += __atomic_load_n(&mrg->index[i].stats.current_references, __ATOMIC_RELAXED);
  494. s->additions += __atomic_load_n(&mrg->index[i].stats.additions, __ATOMIC_RELAXED);
  495. s->additions_duplicate += __atomic_load_n(&mrg->index[i].stats.additions_duplicate, __ATOMIC_RELAXED);
  496. s->deletions += __atomic_load_n(&mrg->index[i].stats.deletions, __ATOMIC_RELAXED);
  497. s->delete_having_retention_or_referenced += __atomic_load_n(&mrg->index[i].stats.delete_having_retention_or_referenced, __ATOMIC_RELAXED);
  498. s->delete_misses += __atomic_load_n(&mrg->index[i].stats.delete_misses, __ATOMIC_RELAXED);
  499. s->search_hits += __atomic_load_n(&mrg->index[i].stats.search_hits, __ATOMIC_RELAXED);
  500. s->search_misses += __atomic_load_n(&mrg->index[i].stats.search_misses, __ATOMIC_RELAXED);
  501. s->writers += __atomic_load_n(&mrg->index[i].stats.writers, __ATOMIC_RELAXED);
  502. s->writers_conflicts += __atomic_load_n(&mrg->index[i].stats.writers_conflicts, __ATOMIC_RELAXED);
  503. }
  504. s->size += sizeof(MRG) + sizeof(struct mrg_partition) * mrg->partitions;
  505. }
  506. // ----------------------------------------------------------------------------
  507. // unit test
  508. struct mrg_stress_entry {
  509. uuid_t uuid;
  510. time_t after;
  511. time_t before;
  512. };
  513. struct mrg_stress {
  514. MRG *mrg;
  515. bool stop;
  516. size_t entries;
  517. struct mrg_stress_entry *array;
  518. size_t updates;
  519. };
  520. static void *mrg_stress(void *ptr) {
  521. struct mrg_stress *t = ptr;
  522. MRG *mrg = t->mrg;
  523. ssize_t start = 0;
  524. ssize_t end = (ssize_t)t->entries;
  525. ssize_t step = 1;
  526. if(gettid() % 2) {
  527. start = (ssize_t)t->entries - 1;
  528. end = -1;
  529. step = -1;
  530. }
  531. while(!__atomic_load_n(&t->stop, __ATOMIC_RELAXED)) {
  532. for (ssize_t i = start; i != end; i += step) {
  533. struct mrg_stress_entry *e = &t->array[i];
  534. time_t after = __atomic_sub_fetch(&e->after, 1, __ATOMIC_RELAXED);
  535. time_t before = __atomic_add_fetch(&e->before, 1, __ATOMIC_RELAXED);
  536. mrg_update_metric_retention_and_granularity_by_uuid(
  537. mrg, 0x01,
  538. &e->uuid,
  539. after,
  540. before,
  541. 1,
  542. before);
  543. __atomic_add_fetch(&t->updates, 1, __ATOMIC_RELAXED);
  544. }
  545. }
  546. return ptr;
  547. }
  548. int mrg_unittest(void) {
  549. MRG *mrg = mrg_create(0);
  550. METRIC *m1_t0, *m2_t0, *m3_t0, *m4_t0;
  551. METRIC *m1_t1, *m2_t1, *m3_t1, *m4_t1;
  552. bool ret;
  553. uuid_t test_uuid;
  554. uuid_generate(test_uuid);
  555. MRG_ENTRY entry = {
  556. .uuid = &test_uuid,
  557. .section = 0,
  558. .first_time_s = 2,
  559. .last_time_s = 3,
  560. .latest_update_every_s = 4,
  561. };
  562. m1_t0 = mrg_metric_add_and_acquire(mrg, entry, &ret);
  563. if(!ret)
  564. fatal("DBENGINE METRIC: failed to add metric");
  565. // add the same metric again
  566. m2_t0 = mrg_metric_add_and_acquire(mrg, entry, &ret);
  567. if(m2_t0 != m1_t0)
  568. fatal("DBENGINE METRIC: adding the same metric twice, does not return the same pointer");
  569. if(ret)
  570. fatal("DBENGINE METRIC: managed to add the same metric twice");
  571. m3_t0 = mrg_metric_get_and_acquire(mrg, entry.uuid, entry.section);
  572. if(m3_t0 != m1_t0)
  573. fatal("DBENGINE METRIC: cannot find the metric added");
  574. // add the same metric again
  575. m4_t0 = mrg_metric_add_and_acquire(mrg, entry, &ret);
  576. if(m4_t0 != m1_t0)
  577. fatal("DBENGINE METRIC: adding the same metric twice, does not return the same pointer");
  578. if(ret)
  579. fatal("DBENGINE METRIC: managed to add the same metric twice");
  580. // add the same metric in another section
  581. entry.section = 1;
  582. m1_t1 = mrg_metric_add_and_acquire(mrg, entry, &ret);
  583. if(!ret)
  584. fatal("DBENGINE METRIC: failed to add metric in section %zu", (size_t)entry.section);
  585. // add the same metric again
  586. m2_t1 = mrg_metric_add_and_acquire(mrg, entry, &ret);
  587. if(m2_t1 != m1_t1)
  588. fatal("DBENGINE METRIC: adding the same metric twice (section %zu), does not return the same pointer", (size_t)entry.section);
  589. if(ret)
  590. fatal("DBENGINE METRIC: managed to add the same metric twice in (section 0)");
  591. m3_t1 = mrg_metric_get_and_acquire(mrg, entry.uuid, entry.section);
  592. if(m3_t1 != m1_t1)
  593. fatal("DBENGINE METRIC: cannot find the metric added (section %zu)", (size_t)entry.section);
  594. // delete the first metric
  595. mrg_metric_release(mrg, m2_t0);
  596. mrg_metric_release(mrg, m3_t0);
  597. mrg_metric_release(mrg, m4_t0);
  598. mrg_metric_set_first_time_s(mrg, m1_t0, 0);
  599. mrg_metric_set_clean_latest_time_s(mrg, m1_t0, 0);
  600. mrg_metric_set_hot_latest_time_s(mrg, m1_t0, 0);
  601. if(!mrg_metric_release_and_delete(mrg, m1_t0))
  602. fatal("DBENGINE METRIC: cannot delete the first metric");
  603. m4_t1 = mrg_metric_get_and_acquire(mrg, entry.uuid, entry.section);
  604. if(m4_t1 != m1_t1)
  605. fatal("DBENGINE METRIC: cannot find the metric added (section %zu), after deleting the first one", (size_t)entry.section);
  606. // delete the second metric
  607. mrg_metric_release(mrg, m2_t1);
  608. mrg_metric_release(mrg, m3_t1);
  609. mrg_metric_release(mrg, m4_t1);
  610. mrg_metric_set_first_time_s(mrg, m1_t1, 0);
  611. mrg_metric_set_clean_latest_time_s(mrg, m1_t1, 0);
  612. mrg_metric_set_hot_latest_time_s(mrg, m1_t1, 0);
  613. if(!mrg_metric_release_and_delete(mrg, m1_t1))
  614. fatal("DBENGINE METRIC: cannot delete the second metric");
  615. struct mrg_statistics s;
  616. mrg_get_statistics(mrg, &s);
  617. if(s.entries != 0)
  618. fatal("DBENGINE METRIC: invalid entries counter");
  619. size_t entries = 1000000;
  620. size_t threads = mrg->partitions / 3 + 1;
  621. size_t tiers = 3;
  622. size_t run_for_secs = 5;
  623. netdata_log_info("preparing stress test of %zu entries...", entries);
  624. struct mrg_stress t = {
  625. .mrg = mrg,
  626. .entries = entries,
  627. .array = callocz(entries, sizeof(struct mrg_stress_entry)),
  628. };
  629. time_t now = max_acceptable_collected_time();
  630. for(size_t i = 0; i < entries ;i++) {
  631. uuid_generate_random(t.array[i].uuid);
  632. t.array[i].after = now / 3;
  633. t.array[i].before = now / 2;
  634. }
  635. netdata_log_info("stress test is populating MRG with 3 tiers...");
  636. for(size_t i = 0; i < entries ;i++) {
  637. struct mrg_stress_entry *e = &t.array[i];
  638. for(size_t tier = 1; tier <= tiers ;tier++) {
  639. mrg_update_metric_retention_and_granularity_by_uuid(
  640. mrg, tier,
  641. &e->uuid,
  642. e->after,
  643. e->before,
  644. 1,
  645. e->before);
  646. }
  647. }
  648. netdata_log_info("stress test ready to run...");
  649. usec_t started_ut = now_monotonic_usec();
  650. pthread_t th[threads];
  651. for(size_t i = 0; i < threads ; i++) {
  652. char buf[15 + 1];
  653. snprintfz(buf, sizeof(buf) - 1, "TH[%zu]", i);
  654. netdata_thread_create(&th[i], buf,
  655. NETDATA_THREAD_OPTION_JOINABLE | NETDATA_THREAD_OPTION_DONT_LOG,
  656. mrg_stress, &t);
  657. }
  658. sleep_usec(run_for_secs * USEC_PER_SEC);
  659. __atomic_store_n(&t.stop, true, __ATOMIC_RELAXED);
  660. for(size_t i = 0; i < threads ; i++)
  661. netdata_thread_cancel(th[i]);
  662. for(size_t i = 0; i < threads ; i++)
  663. netdata_thread_join(th[i], NULL);
  664. usec_t ended_ut = now_monotonic_usec();
  665. struct mrg_statistics stats;
  666. mrg_get_statistics(mrg, &stats);
  667. netdata_log_info("DBENGINE METRIC: did %zu additions, %zu duplicate additions, "
  668. "%zu deletions, %zu wrong deletions, "
  669. "%zu successful searches, %zu wrong searches, "
  670. "in %"PRIu64" usecs",
  671. stats.additions, stats.additions_duplicate,
  672. stats.deletions, stats.delete_misses,
  673. stats.search_hits, stats.search_misses,
  674. ended_ut - started_ut);
  675. netdata_log_info("DBENGINE METRIC: updates performance: %0.2fk/sec total, %0.2fk/sec/thread",
  676. (double)t.updates / (double)((ended_ut - started_ut) / USEC_PER_SEC) / 1000.0,
  677. (double)t.updates / (double)((ended_ut - started_ut) / USEC_PER_SEC) / 1000.0 / threads);
  678. mrg_destroy(mrg);
  679. netdata_log_info("DBENGINE METRIC: all tests passed!");
  680. return 0;
  681. }