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