percentile.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_API_QUERIES_PERCENTILE_H
  3. #define NETDATA_API_QUERIES_PERCENTILE_H
  4. #include "../query.h"
  5. #include "../rrdr.h"
  6. struct tg_percentile {
  7. size_t series_size;
  8. size_t next_pos;
  9. NETDATA_DOUBLE percent;
  10. NETDATA_DOUBLE *series;
  11. };
  12. static inline void tg_percentile_create_internal(RRDR *r, const char *options, NETDATA_DOUBLE def) {
  13. long entries = r->view.group;
  14. if(entries < 10) entries = 10;
  15. struct tg_percentile *g = (struct tg_percentile *)onewayalloc_callocz(r->internal.owa, 1, sizeof(struct tg_percentile));
  16. g->series = onewayalloc_mallocz(r->internal.owa, entries * sizeof(NETDATA_DOUBLE));
  17. g->series_size = (size_t)entries;
  18. g->percent = def;
  19. if(options && *options) {
  20. g->percent = str2ndd(options, NULL);
  21. if(!netdata_double_isnumber(g->percent)) g->percent = 0.0;
  22. if(g->percent < 0.0) g->percent = 0.0;
  23. if(g->percent > 100.0) g->percent = 100.0;
  24. }
  25. g->percent = g->percent / 100.0;
  26. r->time_grouping.data = g;
  27. }
  28. static inline void tg_percentile_create_25(RRDR *r, const char *options) {
  29. tg_percentile_create_internal(r, options, 25.0);
  30. }
  31. static inline void tg_percentile_create_50(RRDR *r, const char *options) {
  32. tg_percentile_create_internal(r, options, 50.0);
  33. }
  34. static inline void tg_percentile_create_75(RRDR *r, const char *options) {
  35. tg_percentile_create_internal(r, options, 75.0);
  36. }
  37. static inline void tg_percentile_create_80(RRDR *r, const char *options) {
  38. tg_percentile_create_internal(r, options, 80.0);
  39. }
  40. static inline void tg_percentile_create_90(RRDR *r, const char *options) {
  41. tg_percentile_create_internal(r, options, 90.0);
  42. }
  43. static inline void tg_percentile_create_95(RRDR *r, const char *options) {
  44. tg_percentile_create_internal(r, options, 95.0);
  45. }
  46. static inline void tg_percentile_create_97(RRDR *r, const char *options) {
  47. tg_percentile_create_internal(r, options, 97.0);
  48. }
  49. static inline void tg_percentile_create_98(RRDR *r, const char *options) {
  50. tg_percentile_create_internal(r, options, 98.0);
  51. }
  52. static inline void tg_percentile_create_99(RRDR *r, const char *options) {
  53. tg_percentile_create_internal(r, options, 99.0);
  54. }
  55. // resets when switches dimensions
  56. // so, clear everything to restart
  57. static inline void tg_percentile_reset(RRDR *r) {
  58. struct tg_percentile *g = (struct tg_percentile *)r->time_grouping.data;
  59. g->next_pos = 0;
  60. }
  61. static inline void tg_percentile_free(RRDR *r) {
  62. struct tg_percentile *g = (struct tg_percentile *)r->time_grouping.data;
  63. if(g) onewayalloc_freez(r->internal.owa, g->series);
  64. onewayalloc_freez(r->internal.owa, r->time_grouping.data);
  65. r->time_grouping.data = NULL;
  66. }
  67. static inline void tg_percentile_add(RRDR *r, NETDATA_DOUBLE value) {
  68. struct tg_percentile *g = (struct tg_percentile *)r->time_grouping.data;
  69. if(unlikely(g->next_pos >= g->series_size)) {
  70. g->series = onewayalloc_doublesize( r->internal.owa, g->series, g->series_size * sizeof(NETDATA_DOUBLE));
  71. g->series_size *= 2;
  72. }
  73. g->series[g->next_pos++] = value;
  74. }
  75. static inline NETDATA_DOUBLE tg_percentile_flush(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr) {
  76. struct tg_percentile *g = (struct tg_percentile *)r->time_grouping.data;
  77. NETDATA_DOUBLE value;
  78. size_t available_slots = g->next_pos;
  79. if(unlikely(!available_slots)) {
  80. value = 0.0;
  81. *rrdr_value_options_ptr |= RRDR_VALUE_EMPTY;
  82. }
  83. else if(available_slots == 1) {
  84. value = g->series[0];
  85. }
  86. else {
  87. sort_series(g->series, available_slots);
  88. NETDATA_DOUBLE min = g->series[0];
  89. NETDATA_DOUBLE max = g->series[available_slots - 1];
  90. if (min != max) {
  91. size_t slots_to_use = (size_t)((NETDATA_DOUBLE)available_slots * g->percent);
  92. if(!slots_to_use) slots_to_use = 1;
  93. NETDATA_DOUBLE percent_to_use = (NETDATA_DOUBLE)slots_to_use / (NETDATA_DOUBLE)available_slots;
  94. NETDATA_DOUBLE percent_delta = g->percent - percent_to_use;
  95. NETDATA_DOUBLE percent_interpolation_slot = 0.0;
  96. NETDATA_DOUBLE percent_last_slot = 0.0;
  97. if(percent_delta > 0.0) {
  98. NETDATA_DOUBLE percent_to_use_plus_1_slot = (NETDATA_DOUBLE)(slots_to_use + 1) / (NETDATA_DOUBLE)available_slots;
  99. NETDATA_DOUBLE percent_1slot = percent_to_use_plus_1_slot - percent_to_use;
  100. percent_interpolation_slot = percent_delta / percent_1slot;
  101. percent_last_slot = 1 - percent_interpolation_slot;
  102. }
  103. int start_slot, stop_slot, step, last_slot, interpolation_slot;
  104. if(min >= 0.0 && max >= 0.0) {
  105. start_slot = 0;
  106. stop_slot = start_slot + (int)slots_to_use;
  107. last_slot = stop_slot - 1;
  108. interpolation_slot = stop_slot;
  109. step = 1;
  110. }
  111. else {
  112. start_slot = (int)available_slots - 1;
  113. stop_slot = start_slot - (int)slots_to_use;
  114. last_slot = stop_slot + 1;
  115. interpolation_slot = stop_slot;
  116. step = -1;
  117. }
  118. value = 0.0;
  119. for(int slot = start_slot; slot != stop_slot ; slot += step)
  120. value += g->series[slot];
  121. size_t counted = slots_to_use;
  122. if(percent_interpolation_slot > 0.0 && interpolation_slot >= 0 && interpolation_slot < (int)available_slots) {
  123. value += g->series[interpolation_slot] * percent_interpolation_slot;
  124. value += g->series[last_slot] * percent_last_slot;
  125. counted++;
  126. }
  127. value = value / (NETDATA_DOUBLE)counted;
  128. }
  129. else
  130. value = min;
  131. }
  132. if(unlikely(!netdata_double_isnumber(value))) {
  133. value = 0.0;
  134. *rrdr_value_options_ptr |= RRDR_VALUE_EMPTY;
  135. }
  136. //log_series_to_stderr(g->series, g->next_pos, value, "percentile");
  137. g->next_pos = 0;
  138. return value;
  139. }
  140. #endif //NETDATA_API_QUERIES_PERCENTILE_H