rrdr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. calculated_number *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, CALCULATED_NUMBER_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 static void rrdr_lock_rrdset(RRDR *r) {
  50. if(unlikely(!r)) {
  51. error("NULL value given!");
  52. return;
  53. }
  54. rrdset_rdlock(r->st);
  55. r->has_st_lock = 1;
  56. }
  57. inline static void rrdr_unlock_rrdset(RRDR *r) {
  58. if(unlikely(!r)) {
  59. error("NULL value given!");
  60. return;
  61. }
  62. if(likely(r->has_st_lock)) {
  63. r->has_st_lock = 0;
  64. rrdset_unlock(r->st);
  65. }
  66. }
  67. inline void rrdr_free(ONEWAYALLOC *owa, RRDR *r)
  68. {
  69. if(unlikely(!r)) {
  70. error("NULL value given!");
  71. return;
  72. }
  73. rrdr_unlock_rrdset(r);
  74. onewayalloc_freez(owa, r->t);
  75. onewayalloc_freez(owa, r->v);
  76. onewayalloc_freez(owa, r->o);
  77. onewayalloc_freez(owa, r->od);
  78. onewayalloc_freez(owa, r);
  79. }
  80. RRDR *rrdr_create(ONEWAYALLOC *owa, struct rrdset *st, long n, struct context_param *context_param_list)
  81. {
  82. if (unlikely(!st)) {
  83. error("NULL value given!");
  84. return NULL;
  85. }
  86. RRDR *r = onewayalloc_callocz(owa, 1, sizeof(RRDR));
  87. r->st = st;
  88. if (!context_param_list || !(context_param_list->flags & CONTEXT_FLAGS_ARCHIVE)) {
  89. rrdr_lock_rrdset(r);
  90. r->st_needs_lock = 1;
  91. }
  92. RRDDIM *temp_rd = context_param_list ? context_param_list->rd : NULL;
  93. RRDDIM *rd;
  94. if (temp_rd) {
  95. RRDDIM *t = temp_rd;
  96. while (t) {
  97. r->d++;
  98. t = t->next;
  99. }
  100. } else
  101. rrddim_foreach_read(rd, st) r->d++;
  102. r->n = n;
  103. r->t = onewayalloc_callocz(owa, (size_t)n, sizeof(time_t));
  104. r->v = onewayalloc_mallocz(owa, n * r->d * sizeof(calculated_number));
  105. r->o = onewayalloc_mallocz(owa, n * r->d * sizeof(RRDR_VALUE_FLAGS));
  106. r->od = onewayalloc_mallocz(owa, r->d * sizeof(RRDR_DIMENSION_FLAGS));
  107. // set the hidden flag on hidden dimensions
  108. int c;
  109. for (c = 0, rd = temp_rd ? temp_rd : st->dimensions; rd; c++, rd = rd->next) {
  110. if (unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)))
  111. r->od[c] = RRDR_DIMENSION_HIDDEN;
  112. else
  113. r->od[c] = RRDR_DIMENSION_DEFAULT;
  114. }
  115. r->group = 1;
  116. r->update_every = 1;
  117. return r;
  118. }