rrdr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrdr.h"
  3. /*
  4. static void rrdr_dump(RRDR *r)
  5. {
  6. long c, i;
  7. RRDDIM *d;
  8. fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
  9. for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
  10. fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
  11. , d->id
  12. , d->name
  13. , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
  14. , (r->od[c] & RRDR_RESET)?"RESET ":""
  15. , (r->od[c] & RRDR_DIMENSION_HIDDEN)?"HIDDEN ":""
  16. , (r->od[c] & RRDR_DIMENSION_NONZERO)?"NONZERO ":""
  17. );
  18. }
  19. if(r->rows <= 0) {
  20. fprintf(stderr, "RRDR does not have any values in it.\n");
  21. return;
  22. }
  23. fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
  24. // for each line in the array
  25. for(i = 0; i < r->rows ;i++) {
  26. NETDATA_DOUBLE *cn = &r->v[ i * r->d ];
  27. RRDR_DIMENSION_FLAGS *co = &r->o[ i * r->d ];
  28. // print the id and the timestamp of the line
  29. fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
  30. // for each dimension
  31. for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
  32. if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
  33. if(unlikely(!(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
  34. if(co[c] & RRDR_EMPTY)
  35. fprintf(stderr, "null ");
  36. else
  37. fprintf(stderr, NETDATA_DOUBLE_FORMAT " %s%s%s%s "
  38. , cn[c]
  39. , (co[c] & RRDR_EMPTY)?"E":" "
  40. , (co[c] & RRDR_RESET)?"R":" "
  41. , (co[c] & RRDR_DIMENSION_HIDDEN)?"H":" "
  42. , (co[c] & RRDR_DIMENSION_NONZERO)?"N":" "
  43. );
  44. }
  45. fprintf(stderr, "\n");
  46. }
  47. }
  48. */
  49. inline void rrdr_free(ONEWAYALLOC *owa, RRDR *r) {
  50. if(unlikely(!r)) return;
  51. if(likely(r->st_locked_by_rrdr_create))
  52. rrdset_unlock(r->st);
  53. onewayalloc_freez(owa, r->t);
  54. onewayalloc_freez(owa, r->v);
  55. onewayalloc_freez(owa, r->o);
  56. onewayalloc_freez(owa, r->od);
  57. onewayalloc_freez(owa, r->ar);
  58. onewayalloc_freez(owa, r);
  59. }
  60. RRDR *rrdr_create_for_x_dimensions(ONEWAYALLOC *owa, int dimensions, long points) {
  61. RRDR *r = onewayalloc_callocz(owa, 1, sizeof(RRDR));
  62. r->internal.owa = owa;
  63. r->d = dimensions;
  64. r->n = points;
  65. r->t = onewayalloc_callocz(owa, points, sizeof(time_t));
  66. r->v = onewayalloc_mallocz(owa, points * dimensions * sizeof(NETDATA_DOUBLE));
  67. r->o = onewayalloc_mallocz(owa, points * dimensions * sizeof(RRDR_VALUE_FLAGS));
  68. r->ar = onewayalloc_mallocz(owa, points * dimensions * sizeof(uint8_t));
  69. r->od = onewayalloc_mallocz(owa, dimensions * sizeof(RRDR_DIMENSION_FLAGS));
  70. r->group = 1;
  71. r->update_every = 1;
  72. return r;
  73. }
  74. RRDR *rrdr_create(ONEWAYALLOC *owa, struct rrdset *st, long n, struct context_param *context_param_list) {
  75. if (unlikely(!st)) return NULL;
  76. bool st_locked_by_rrdr_create = false;
  77. if (!context_param_list || !(context_param_list->flags & CONTEXT_FLAGS_ARCHIVE)) {
  78. rrdset_rdlock(st);
  79. st_locked_by_rrdr_create = true;
  80. }
  81. // count the number of dimensions
  82. int dimensions = 0;
  83. RRDDIM *temp_rd = context_param_list ? context_param_list->rd : NULL;
  84. RRDDIM *rd;
  85. if (temp_rd) {
  86. RRDDIM *t = temp_rd;
  87. while (t) {
  88. dimensions++;
  89. t = t->next;
  90. }
  91. } else
  92. rrddim_foreach_read(rd, st) dimensions++;
  93. // create the rrdr
  94. RRDR *r = rrdr_create_for_x_dimensions(owa, dimensions, n);
  95. r->st = st;
  96. r->st_locked_by_rrdr_create = st_locked_by_rrdr_create;
  97. // set the hidden flag on hidden dimensions
  98. int c;
  99. for (c = 0, rd = temp_rd ? temp_rd : st->dimensions; rd; c++, rd = rd->next) {
  100. if (unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)))
  101. r->od[c] = RRDR_DIMENSION_HIDDEN;
  102. else
  103. r->od[c] = RRDR_DIMENSION_DEFAULT;
  104. }
  105. return r;
  106. }