metric.c 32 KB

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