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. // memcpy(metric->uuid, entry->uuid, sizeof(uuid_t));
  151. uuid_copy(metric->uuid, entry->uuid);
  152. metric->section = entry->section;
  153. metric->first_time_s = MAX(0, entry->first_time_s);
  154. metric->latest_time_s_clean = MAX(0, entry->last_time_s);
  155. metric->latest_time_s_hot = 0;
  156. metric->latest_update_every_s = entry->latest_update_every_s;
  157. metric->writer = 0;
  158. metric->refcount = 0;
  159. metric->flags = 0;
  160. metric->partition = partition;
  161. spinlock_init(&metric->spinlock);
  162. metric_acquire(mrg, metric, true); // no spinlock use required here
  163. *PValue = metric;
  164. MRG_STATS_ADDED_METRIC(mrg, partition);
  165. mrg_index_write_unlock(mrg, partition);
  166. if(ret)
  167. *ret = true;
  168. return metric;
  169. }
  170. static inline METRIC *metric_get_and_acquire(MRG *mrg, uuid_t *uuid, Word_t section) {
  171. size_t partition = uuid_partition(mrg, uuid);
  172. mrg_index_read_lock(mrg, partition);
  173. Pvoid_t *sections_judy_pptr = JudyHSGet(mrg->index[partition].uuid_judy, uuid, sizeof(uuid_t));
  174. if(unlikely(!sections_judy_pptr)) {
  175. mrg_index_read_unlock(mrg, partition);
  176. MRG_STATS_SEARCH_MISS(mrg, partition);
  177. return NULL;
  178. }
  179. Pvoid_t *PValue = JudyLGet(*sections_judy_pptr, section, PJE0);
  180. if(unlikely(!PValue)) {
  181. mrg_index_read_unlock(mrg, partition);
  182. MRG_STATS_SEARCH_MISS(mrg, partition);
  183. return NULL;
  184. }
  185. METRIC *metric = *PValue;
  186. metric_acquire(mrg, metric, false);
  187. mrg_index_read_unlock(mrg, partition);
  188. MRG_STATS_SEARCH_HIT(mrg, partition);
  189. return metric;
  190. }
  191. static inline bool acquired_metric_del(MRG *mrg, METRIC *metric) {
  192. size_t partition = metric->partition;
  193. size_t mem_before_judyl, mem_after_judyl;
  194. mrg_index_write_lock(mrg, partition);
  195. if(!metric_release_and_can_be_deleted(mrg, metric)) {
  196. mrg->index[partition].stats.delete_having_retention_or_referenced++;
  197. mrg_index_write_unlock(mrg, partition);
  198. return false;
  199. }
  200. Pvoid_t *sections_judy_pptr = JudyHSGet(mrg->index[partition].uuid_judy, &metric->uuid, sizeof(uuid_t));
  201. if(unlikely(!sections_judy_pptr || !*sections_judy_pptr)) {
  202. MRG_STATS_DELETE_MISS(mrg, partition);
  203. mrg_index_write_unlock(mrg, partition);
  204. return false;
  205. }
  206. mem_before_judyl = JudyLMemUsed(*sections_judy_pptr);
  207. int rc = JudyLDel(sections_judy_pptr, metric->section, PJE0);
  208. mem_after_judyl = JudyLMemUsed(*sections_judy_pptr);
  209. mrg_stats_size_judyl_change(mrg, mem_before_judyl, mem_after_judyl, partition);
  210. if(unlikely(!rc)) {
  211. MRG_STATS_DELETE_MISS(mrg, partition);
  212. mrg_index_write_unlock(mrg, partition);
  213. return false;
  214. }
  215. if(!*sections_judy_pptr) {
  216. rc = JudyHSDel(&mrg->index[partition].uuid_judy, &metric->uuid, sizeof(uuid_t), PJE0);
  217. if(unlikely(!rc))
  218. fatal("DBENGINE METRIC: cannot delete UUID from JudyHS");
  219. mrg_stats_size_judyhs_removed_uuid(mrg, partition);
  220. }
  221. MRG_STATS_DELETED_METRIC(mrg, partition);
  222. mrg_index_write_unlock(mrg, partition);
  223. aral_freez(mrg->index[partition].aral, metric);
  224. return true;
  225. }
  226. // ----------------------------------------------------------------------------
  227. // public API
  228. inline MRG *mrg_create(ssize_t partitions) {
  229. if(partitions < 1)
  230. partitions = get_netdata_cpus();
  231. MRG *mrg = callocz(1, sizeof(MRG) + sizeof(struct mrg_partition) * partitions);
  232. mrg->partitions = partitions;
  233. for(size_t i = 0; i < mrg->partitions ; i++) {
  234. rw_spinlock_init(&mrg->index[i].rw_spinlock);
  235. char buf[ARAL_MAX_NAME + 1];
  236. snprintfz(buf, ARAL_MAX_NAME, "mrg[%zu]", i);
  237. mrg->index[i].aral = aral_create(buf, sizeof(METRIC), 0, 16384, &mrg_aral_statistics, NULL, NULL, false, false);
  238. }
  239. return mrg;
  240. }
  241. inline size_t mrg_aral_structures(void) {
  242. return aral_structures_from_stats(&mrg_aral_statistics);
  243. }
  244. inline size_t mrg_aral_overhead(void) {
  245. return aral_overhead_from_stats(&mrg_aral_statistics);
  246. }
  247. inline void mrg_destroy(MRG *mrg __maybe_unused) {
  248. // no destruction possible
  249. // we can't traverse the metrics list
  250. // to delete entries, the caller needs to keep pointers to them
  251. // and delete them one by one
  252. ;
  253. }
  254. inline METRIC *mrg_metric_add_and_acquire(MRG *mrg, MRG_ENTRY entry, bool *ret) {
  255. // internal_fatal(entry.latest_time_s > max_acceptable_collected_time(),
  256. // "DBENGINE METRIC: metric latest time is in the future");
  257. return metric_add_and_acquire(mrg, &entry, ret);
  258. }
  259. inline METRIC *mrg_metric_get_and_acquire(MRG *mrg, uuid_t *uuid, Word_t section) {
  260. return metric_get_and_acquire(mrg, uuid, section);
  261. }
  262. inline bool mrg_metric_release_and_delete(MRG *mrg, METRIC *metric) {
  263. return acquired_metric_del(mrg, metric);
  264. }
  265. inline METRIC *mrg_metric_dup(MRG *mrg, METRIC *metric) {
  266. metric_acquire(mrg, metric, false);
  267. return metric;
  268. }
  269. inline bool mrg_metric_release(MRG *mrg, METRIC *metric) {
  270. return metric_release_and_can_be_deleted(mrg, metric);
  271. }
  272. inline Word_t mrg_metric_id(MRG *mrg __maybe_unused, METRIC *metric) {
  273. return (Word_t)metric;
  274. }
  275. inline uuid_t *mrg_metric_uuid(MRG *mrg __maybe_unused, METRIC *metric) {
  276. return &metric->uuid;
  277. }
  278. inline Word_t mrg_metric_section(MRG *mrg __maybe_unused, METRIC *metric) {
  279. return metric->section;
  280. }
  281. inline bool mrg_metric_set_first_time_s(MRG *mrg __maybe_unused, METRIC *metric, time_t first_time_s) {
  282. internal_fatal(first_time_s < 0, "DBENGINE METRIC: timestamp is negative");
  283. if(unlikely(first_time_s < 0))
  284. return false;
  285. metric_lock(metric);
  286. metric->first_time_s = first_time_s;
  287. metric_has_retention_unsafe(mrg, metric);
  288. metric_unlock(metric);
  289. return true;
  290. }
  291. 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) {
  292. internal_fatal(first_time_s < 0 || last_time_s < 0 || update_every_s < 0,
  293. "DBENGINE METRIC: timestamp is negative");
  294. internal_fatal(first_time_s > max_acceptable_collected_time(),
  295. "DBENGINE METRIC: metric first time is in the future");
  296. internal_fatal(last_time_s > max_acceptable_collected_time(),
  297. "DBENGINE METRIC: metric last time is in the future");
  298. if(unlikely(first_time_s < 0))
  299. first_time_s = 0;
  300. if(unlikely(last_time_s < 0))
  301. last_time_s = 0;
  302. if(unlikely(update_every_s < 0))
  303. update_every_s = 0;
  304. if(unlikely(!first_time_s && !last_time_s && !update_every_s))
  305. return;
  306. metric_lock(metric);
  307. if(unlikely(first_time_s && (!metric->first_time_s || first_time_s < metric->first_time_s)))
  308. metric->first_time_s = first_time_s;
  309. if(likely(last_time_s && (!metric->latest_time_s_clean || last_time_s > metric->latest_time_s_clean))) {
  310. metric->latest_time_s_clean = last_time_s;
  311. if(likely(update_every_s))
  312. metric->latest_update_every_s = (uint32_t) update_every_s;
  313. }
  314. else if(unlikely(!metric->latest_update_every_s && update_every_s))
  315. metric->latest_update_every_s = (uint32_t) update_every_s;
  316. metric_has_retention_unsafe(mrg, metric);
  317. metric_unlock(metric);
  318. }
  319. inline bool mrg_metric_set_first_time_s_if_bigger(MRG *mrg __maybe_unused, METRIC *metric, time_t first_time_s) {
  320. internal_fatal(first_time_s < 0, "DBENGINE METRIC: timestamp is negative");
  321. bool ret = false;
  322. metric_lock(metric);
  323. if(first_time_s > metric->first_time_s) {
  324. metric->first_time_s = first_time_s;
  325. ret = true;
  326. }
  327. metric_has_retention_unsafe(mrg, metric);
  328. metric_unlock(metric);
  329. return ret;
  330. }
  331. inline time_t mrg_metric_get_first_time_s(MRG *mrg __maybe_unused, METRIC *metric) {
  332. time_t first_time_s;
  333. metric_lock(metric);
  334. if(unlikely(!metric->first_time_s)) {
  335. if(metric->latest_time_s_clean)
  336. metric->first_time_s = metric->latest_time_s_clean;
  337. else if(metric->latest_time_s_hot)
  338. metric->first_time_s = metric->latest_time_s_hot;
  339. }
  340. first_time_s = metric->first_time_s;
  341. metric_unlock(metric);
  342. return first_time_s;
  343. }
  344. 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) {
  345. metric_lock(metric);
  346. if(unlikely(!metric->first_time_s)) {
  347. if(metric->latest_time_s_clean)
  348. metric->first_time_s = metric->latest_time_s_clean;
  349. else if(metric->latest_time_s_hot)
  350. metric->first_time_s = metric->latest_time_s_hot;
  351. }
  352. *first_time_s = metric->first_time_s;
  353. *last_time_s = MAX(metric->latest_time_s_clean, metric->latest_time_s_hot);
  354. *update_every_s = metric->latest_update_every_s;
  355. metric_unlock(metric);
  356. }
  357. inline bool mrg_metric_set_clean_latest_time_s(MRG *mrg __maybe_unused, METRIC *metric, time_t latest_time_s) {
  358. internal_fatal(latest_time_s < 0, "DBENGINE METRIC: timestamp is negative");
  359. if(unlikely(latest_time_s < 0))
  360. return false;
  361. metric_lock(metric);
  362. // internal_fatal(latest_time_s > max_acceptable_collected_time(),
  363. // "DBENGINE METRIC: metric latest time is in the future");
  364. // internal_fatal(metric->latest_time_s_clean > latest_time_s,
  365. // "DBENGINE METRIC: metric new clean latest time is older than the previous one");
  366. metric->latest_time_s_clean = latest_time_s;
  367. if(unlikely(!metric->first_time_s))
  368. metric->first_time_s = latest_time_s;
  369. metric_has_retention_unsafe(mrg, metric);
  370. metric_unlock(metric);
  371. return true;
  372. }
  373. // returns true when metric still has retention
  374. inline bool mrg_metric_zero_disk_retention(MRG *mrg __maybe_unused, METRIC *metric) {
  375. Word_t section = mrg_metric_section(mrg, metric);
  376. bool do_again = false;
  377. size_t countdown = 5;
  378. bool ret = true;
  379. do {
  380. time_t min_first_time_s = LONG_MAX;
  381. time_t max_end_time_s = 0;
  382. PGC_PAGE *page;
  383. PGC_SEARCH method = PGC_SEARCH_FIRST;
  384. time_t page_first_time_s = 0;
  385. time_t page_end_time_s = 0;
  386. while ((page = pgc_page_get_and_acquire(main_cache, section, (Word_t)metric, page_first_time_s, method))) {
  387. method = PGC_SEARCH_NEXT;
  388. bool is_hot = pgc_is_page_hot(page);
  389. bool is_dirty = pgc_is_page_dirty(page);
  390. page_first_time_s = pgc_page_start_time_s(page);
  391. page_end_time_s = pgc_page_end_time_s(page);
  392. if ((is_hot || is_dirty) && page_first_time_s > 0 && page_first_time_s < min_first_time_s)
  393. min_first_time_s = page_first_time_s;
  394. if (is_dirty && page_end_time_s > max_end_time_s)
  395. max_end_time_s = page_end_time_s;
  396. pgc_page_release(main_cache, page);
  397. }
  398. if (min_first_time_s == LONG_MAX)
  399. min_first_time_s = 0;
  400. metric_lock(metric);
  401. if (--countdown && !min_first_time_s && metric->latest_time_s_hot)
  402. do_again = true;
  403. else {
  404. internal_error(!countdown, "METRIC: giving up on updating the retention of metric without disk retention");
  405. do_again = false;
  406. metric->first_time_s = min_first_time_s;
  407. metric->latest_time_s_clean = max_end_time_s;
  408. ret = metric_has_retention_unsafe(mrg, metric);
  409. }
  410. metric_unlock(metric);
  411. } while(do_again);
  412. return ret;
  413. }
  414. inline bool mrg_metric_set_hot_latest_time_s(MRG *mrg __maybe_unused, METRIC *metric, time_t latest_time_s) {
  415. internal_fatal(latest_time_s < 0, "DBENGINE METRIC: timestamp is negative");
  416. // internal_fatal(latest_time_s > max_acceptable_collected_time(),
  417. // "DBENGINE METRIC: metric latest time is in the future");
  418. if(unlikely(latest_time_s < 0))
  419. return false;
  420. metric_lock(metric);
  421. metric->latest_time_s_hot = latest_time_s;
  422. if(unlikely(!metric->first_time_s))
  423. metric->first_time_s = latest_time_s;
  424. metric_has_retention_unsafe(mrg, metric);
  425. metric_unlock(metric);
  426. return true;
  427. }
  428. inline time_t mrg_metric_get_latest_time_s(MRG *mrg __maybe_unused, METRIC *metric) {
  429. time_t max;
  430. metric_lock(metric);
  431. max = MAX(metric->latest_time_s_clean, metric->latest_time_s_hot);
  432. metric_unlock(metric);
  433. return max;
  434. }
  435. inline bool mrg_metric_set_update_every(MRG *mrg __maybe_unused, METRIC *metric, time_t update_every_s) {
  436. internal_fatal(update_every_s < 0, "DBENGINE METRIC: timestamp is negative");
  437. if(update_every_s <= 0)
  438. return false;
  439. metric_lock(metric);
  440. metric->latest_update_every_s = (uint32_t) update_every_s;
  441. metric_unlock(metric);
  442. return true;
  443. }
  444. inline bool mrg_metric_set_update_every_s_if_zero(MRG *mrg __maybe_unused, METRIC *metric, time_t update_every_s) {
  445. internal_fatal(update_every_s < 0, "DBENGINE METRIC: timestamp is negative");
  446. if(update_every_s <= 0)
  447. return false;
  448. metric_lock(metric);
  449. if(!metric->latest_update_every_s)
  450. metric->latest_update_every_s = (uint32_t) update_every_s;
  451. metric_unlock(metric);
  452. return true;
  453. }
  454. inline time_t mrg_metric_get_update_every_s(MRG *mrg __maybe_unused, METRIC *metric) {
  455. time_t update_every_s;
  456. metric_lock(metric);
  457. update_every_s = metric->latest_update_every_s;
  458. metric_unlock(metric);
  459. return update_every_s;
  460. }
  461. inline bool mrg_metric_set_writer(MRG *mrg, METRIC *metric) {
  462. bool done = false;
  463. metric_lock(metric);
  464. if(!metric->writer) {
  465. metric->writer = gettid();
  466. __atomic_add_fetch(&mrg->index[metric->partition].stats.writers, 1, __ATOMIC_RELAXED);
  467. done = true;
  468. }
  469. else
  470. __atomic_add_fetch(&mrg->index[metric->partition].stats.writers_conflicts, 1, __ATOMIC_RELAXED);
  471. metric_unlock(metric);
  472. return done;
  473. }
  474. inline bool mrg_metric_clear_writer(MRG *mrg, METRIC *metric) {
  475. bool done = false;
  476. metric_lock(metric);
  477. if(metric->writer) {
  478. metric->writer = 0;
  479. __atomic_sub_fetch(&mrg->index[metric->partition].stats.writers, 1, __ATOMIC_RELAXED);
  480. done = true;
  481. }
  482. metric_unlock(metric);
  483. return done;
  484. }
  485. inline void mrg_update_metric_retention_and_granularity_by_uuid(
  486. MRG *mrg, Word_t section, uuid_t *uuid,
  487. time_t first_time_s, time_t last_time_s,
  488. time_t update_every_s, time_t now_s)
  489. {
  490. if(unlikely(last_time_s > now_s)) {
  491. error_limit_static_global_var(erl, 1, 0);
  492. error_limit(&erl, "DBENGINE JV2: wrong last time on-disk (%ld - %ld, now %ld), "
  493. "fixing last time to now",
  494. first_time_s, last_time_s, now_s);
  495. last_time_s = now_s;
  496. }
  497. if (unlikely(first_time_s > last_time_s)) {
  498. error_limit_static_global_var(erl, 1, 0);
  499. error_limit(&erl, "DBENGINE JV2: wrong first time on-disk (%ld - %ld, now %ld), "
  500. "fixing first time to last time",
  501. first_time_s, last_time_s, now_s);
  502. first_time_s = last_time_s;
  503. }
  504. if (unlikely(first_time_s == 0 || last_time_s == 0)) {
  505. error_limit_static_global_var(erl, 1, 0);
  506. error_limit(&erl, "DBENGINE JV2: zero on-disk timestamps (%ld - %ld, now %ld), "
  507. "using them as-is",
  508. first_time_s, last_time_s, now_s);
  509. }
  510. bool added = false;
  511. METRIC *metric = mrg_metric_get_and_acquire(mrg, uuid, section);
  512. if (!metric) {
  513. MRG_ENTRY entry = {
  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. // memcpy(entry.uuid, *uuid, sizeof(uuid_t));
  520. uuid_copy(entry.uuid, *uuid);
  521. metric = mrg_metric_add_and_acquire(mrg, entry, &added);
  522. }
  523. if (likely(!added))
  524. mrg_metric_expand_retention(mrg, metric, first_time_s, last_time_s, update_every_s);
  525. mrg_metric_release(mrg, metric);
  526. }
  527. inline void mrg_get_statistics(MRG *mrg, struct mrg_statistics *s) {
  528. memset(s, 0, sizeof(struct mrg_statistics));
  529. for(size_t i = 0; i < mrg->partitions ;i++) {
  530. s->entries += __atomic_load_n(&mrg->index[i].stats.entries, __ATOMIC_RELAXED);
  531. s->entries_referenced += __atomic_load_n(&mrg->index[i].stats.entries_referenced, __ATOMIC_RELAXED);
  532. s->entries_with_retention += __atomic_load_n(&mrg->index[i].stats.entries_with_retention, __ATOMIC_RELAXED);
  533. s->size += __atomic_load_n(&mrg->index[i].stats.size, __ATOMIC_RELAXED);
  534. s->current_references += __atomic_load_n(&mrg->index[i].stats.current_references, __ATOMIC_RELAXED);
  535. s->additions += __atomic_load_n(&mrg->index[i].stats.additions, __ATOMIC_RELAXED);
  536. s->additions_duplicate += __atomic_load_n(&mrg->index[i].stats.additions_duplicate, __ATOMIC_RELAXED);
  537. s->deletions += __atomic_load_n(&mrg->index[i].stats.deletions, __ATOMIC_RELAXED);
  538. s->delete_having_retention_or_referenced += __atomic_load_n(&mrg->index[i].stats.delete_having_retention_or_referenced, __ATOMIC_RELAXED);
  539. s->delete_misses += __atomic_load_n(&mrg->index[i].stats.delete_misses, __ATOMIC_RELAXED);
  540. s->search_hits += __atomic_load_n(&mrg->index[i].stats.search_hits, __ATOMIC_RELAXED);
  541. s->search_misses += __atomic_load_n(&mrg->index[i].stats.search_misses, __ATOMIC_RELAXED);
  542. s->writers += __atomic_load_n(&mrg->index[i].stats.writers, __ATOMIC_RELAXED);
  543. s->writers_conflicts += __atomic_load_n(&mrg->index[i].stats.writers_conflicts, __ATOMIC_RELAXED);
  544. }
  545. s->size += sizeof(MRG) + sizeof(struct mrg_partition) * mrg->partitions;
  546. }
  547. // ----------------------------------------------------------------------------
  548. // unit test
  549. struct mrg_stress_entry {
  550. uuid_t uuid;
  551. time_t after;
  552. time_t before;
  553. };
  554. struct mrg_stress {
  555. MRG *mrg;
  556. bool stop;
  557. size_t entries;
  558. struct mrg_stress_entry *array;
  559. size_t updates;
  560. };
  561. static void *mrg_stress(void *ptr) {
  562. struct mrg_stress *t = ptr;
  563. MRG *mrg = t->mrg;
  564. ssize_t start = 0;
  565. ssize_t end = (ssize_t)t->entries;
  566. ssize_t step = 1;
  567. if(gettid() % 2) {
  568. start = (ssize_t)t->entries - 1;
  569. end = -1;
  570. step = -1;
  571. }
  572. while(!__atomic_load_n(&t->stop, __ATOMIC_RELAXED)) {
  573. for (ssize_t i = start; i != end; i += step) {
  574. struct mrg_stress_entry *e = &t->array[i];
  575. time_t after = __atomic_sub_fetch(&e->after, 1, __ATOMIC_RELAXED);
  576. time_t before = __atomic_add_fetch(&e->before, 1, __ATOMIC_RELAXED);
  577. mrg_update_metric_retention_and_granularity_by_uuid(
  578. mrg, 0x01,
  579. &e->uuid,
  580. after,
  581. before,
  582. 1,
  583. before);
  584. __atomic_add_fetch(&t->updates, 1, __ATOMIC_RELAXED);
  585. }
  586. }
  587. return ptr;
  588. }
  589. int mrg_unittest(void) {
  590. MRG *mrg = mrg_create(0);
  591. METRIC *m1_t0, *m2_t0, *m3_t0, *m4_t0;
  592. METRIC *m1_t1, *m2_t1, *m3_t1, *m4_t1;
  593. bool ret;
  594. MRG_ENTRY entry = {
  595. .section = 0,
  596. .first_time_s = 2,
  597. .last_time_s = 3,
  598. .latest_update_every_s = 4,
  599. };
  600. uuid_generate(entry.uuid);
  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 %llu 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. }