metric.c 32 KB

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