| 1 |
/* |
| 2 |
* This file is part of the rsyslog runtime library. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* -or- |
| 10 |
* see COPYING.ASL20 in the source distribution |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
#include "config.h" |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <sys/types.h> |
| 23 |
#include <assert.h> |
| 24 |
|
| 25 |
#include "unicode-helper.h" |
| 26 |
#include "rsyslog.h" |
| 27 |
#include "rsconf.h" |
| 28 |
#include "errmsg.h" |
| 29 |
#include "perctile_stats.h" |
| 30 |
#include "hashtable_itr.h" |
| 31 |
#include "perctile_ringbuf.h" |
| 32 |
#include "datetime.h" |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <pthread.h> |
| 36 |
#include <math.h> |
| 37 |
|
| 38 |
/* Use this macro to enable debug for this module */ |
| 39 |
#ifdef PERCTILE_STATS_DEBUG |
| 40 |
#define _DEBUG 1 |
| 41 |
#else |
| 42 |
#define _DEBUG 0 |
| 43 |
#endif |
| 44 |
#define PERCTILE_STATS_LOG(...) \ |
| 45 |
do { \ |
| 46 |
if (_DEBUG) fprintf(stderr, __VA_ARGS__); \ |
| 47 |
} while (0) |
| 48 |
|
| 49 |
/* definitions for objects we access */ |
| 50 |
DEFobjStaticHelpers; |
| 51 |
DEFobjCurrIf(statsobj) DEFobjCurrIf(datetime) |
| 52 |
|
| 53 |
#define PERCTILE_CONF_PARAM_NAME "name" |
| 54 |
#define PERCTILE_CONF_PARAM_PERCENTILES "percentiles" |
| 55 |
#define PERCTILE_CONF_PARAM_WINDOW_SIZE "windowsize" |
| 56 |
#define PERCTILE_CONF_PARAM_DELIM "delimiter" |
| 57 |
|
| 58 |
#define PERCTILE_MAX_BUCKET_NS_METRIC_LENGTH 128 |
| 59 |
#define PERCTILE_METRIC_NAME_SEPARATOR '.' |
| 60 |
|
| 61 |
static struct cnfparamdescr modpdescr[] = { |
| 62 |
{PERCTILE_CONF_PARAM_NAME, eCmdHdlrString, CNFPARAM_REQUIRED}, |
| 63 |
{PERCTILE_CONF_PARAM_DELIM, eCmdHdlrString, 0}, |
| 64 |
{PERCTILE_CONF_PARAM_PERCENTILES, eCmdHdlrArray, 0}, |
| 65 |
{PERCTILE_CONF_PARAM_WINDOW_SIZE, eCmdHdlrPositiveInt, 0}, |
| 66 |
}; |
| 67 |
|
| 68 |
static struct cnfparamblk modpblk = {CNFPARAMBLK_VERSION, sizeof(modpdescr) / sizeof(struct cnfparamdescr), modpdescr}; |
| 69 |
|
| 70 |
rsRetVal perctileClassInit(void) { |
| 71 |
DEFiRet; |
| 72 |
CHKiRet(objGetObjInterface(&obj)); |
| 73 |
CHKiRet(objUse(statsobj, CORE_COMPONENT)); |
| 74 |
CHKiRet(objUse(datetime, CORE_COMPONENT)); |
| 75 |
finalize_it: |
| 76 |
RETiRet; |
| 77 |
} |
| 78 |
|
| 79 |
static uint64_t min(uint64_t a, uint64_t b) { |
| 80 |
return a < b ? a : b; |
| 81 |
} |
| 82 |
|
| 83 |
static uint64_t max(uint64_t a, uint64_t b) { |
| 84 |
return a > b ? a : b; |
| 85 |
} |
| 86 |
|
| 87 |
static void perctileStatDestruct(perctile_bucket_t *b, perctile_stat_t *pstat) { |
| 88 |
if (pstat) { |
| 89 |
if (pstat->rb_observed_stats) { |
| 90 |
ringbuf_del(pstat->rb_observed_stats); |
| 91 |
} |
| 92 |
|
| 93 |
if (pstat->ctrs) { |
| 94 |
for (size_t i = 0; i < pstat->perctile_ctrs_count; ++i) { |
| 95 |
perctile_ctr_t *ctr = &pstat->ctrs[i]; |
| 96 |
if (ctr->ref_ctr_percentile_stat) { |
| 97 |
statsobj.DestructCounter(b->statsobj, ctr->ref_ctr_percentile_stat); |
| 98 |
} |
| 99 |
} |
| 100 |
free(pstat->ctrs); |
| 101 |
} |
| 102 |
|
| 103 |
if (pstat->refCtrWindowCount) { |
| 104 |
statsobj.DestructCounter(b->statsobj, pstat->refCtrWindowCount); |
| 105 |
} |
| 106 |
if (pstat->refCtrWindowMin) { |
| 107 |
statsobj.DestructCounter(b->statsobj, pstat->refCtrWindowMin); |
| 108 |
} |
| 109 |
if (pstat->refCtrWindowMax) { |
| 110 |
statsobj.DestructCounter(b->statsobj, pstat->refCtrWindowMax); |
| 111 |
} |
| 112 |
if (pstat->refCtrWindowSum) { |
| 113 |
statsobj.DestructCounter(b->statsobj, pstat->refCtrWindowSum); |
| 114 |
} |
| 115 |
pthread_rwlock_destroy(&pstat->stats_lock); |
| 116 |
free(pstat); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static void perctileBucketDestruct(perctile_bucket_t *bkt) { |
| 121 |
PERCTILE_STATS_LOG("destructing perctile bucket\n"); |
| 122 |
if (bkt) { |
| 123 |
pthread_rwlock_wrlock(&bkt->lock); |
| 124 |
// Delete all items in hashtable |
| 125 |
size_t count = hashtable_count(bkt->htable); |
| 126 |
if (count) { |
| 127 |
int ret = 0; |
| 128 |
struct hashtable_itr *itr = hashtable_iterator(bkt->htable); |
| 129 |
dbgprintf("%s() - All container instances, count=%zu...\n", __FUNCTION__, count); |
| 130 |
do { |
| 131 |
perctile_stat_t *pstat = hashtable_iterator_value(itr); |
| 132 |
perctileStatDestruct(bkt, pstat); |
| 133 |
ret = hashtable_iterator_advance(itr); |
| 134 |
} while (ret); |
| 135 |
free(itr); |
| 136 |
dbgprintf("End of container instances.\n"); |
| 137 |
} |
| 138 |
hashtable_destroy(bkt->htable, 0); |
| 139 |
statsobj.Destruct(&bkt->statsobj); |
| 140 |
pthread_rwlock_unlock(&bkt->lock); |
| 141 |
pthread_rwlock_destroy(&bkt->lock); |
| 142 |
free(bkt->perctile_values); |
| 143 |
free(bkt->delim); |
| 144 |
free(bkt->name); |
| 145 |
free(bkt); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void perctileBucketsDestruct(void) { |
| 150 |
perctile_buckets_t *bkts = &runConf->perctile_buckets; |
| 151 |
|
| 152 |
if (bkts->initialized) { |
| 153 |
perctile_bucket_t *head = bkts->listBuckets; |
| 154 |
if (head) { |
| 155 |
pthread_rwlock_wrlock(&bkts->lock); |
| 156 |
perctile_bucket_t *pnode = head, *pnext = NULL; |
| 157 |
while (pnode) { |
| 158 |
pnext = pnode->next; |
| 159 |
perctileBucketDestruct(pnode); |
| 160 |
pnode = pnext; |
| 161 |
} |
| 162 |
pthread_rwlock_unlock(&bkts->lock); |
| 163 |
} |
| 164 |
statsobj.Destruct(&bkts->global_stats); |
| 165 |
// destroy any global stats we keep specifically for this. |
| 166 |
pthread_rwlock_destroy(&bkts->lock); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static perctile_bucket_t *findBucket(perctile_bucket_t *head, const uchar *name) { |
| 171 |
perctile_bucket_t *pbkt_found = NULL; |
| 172 |
// walk the linked list until the name is found |
| 173 |
pthread_rwlock_rdlock(&head->lock); |
| 174 |
for (perctile_bucket_t *pnode = head; pnode != NULL; pnode = pnode->next) { |
| 175 |
if (ustrcmp(name, pnode->name) == 0) { |
| 176 |
// found. |
| 177 |
pbkt_found = pnode; |
| 178 |
} |
| 179 |
} |
| 180 |
pthread_rwlock_unlock(&head->lock); |
| 181 |
return pbkt_found; |
| 182 |
} |
| 183 |
|
| 184 |
#ifdef PERCTILE_STATS_DEBUG |
| 185 |
static void print_perctiles(perctile_bucket_t *bkt) { |
| 186 |
if (hashtable_count(bkt->htable)) { |
| 187 |
struct hashtable_itr *itr = hashtable_iterator(bkt->htable); |
| 188 |
do { |
| 189 |
uchar *key = hashtable_iterator_key(itr); |
| 190 |
perctile_stat_t *perc_stat = hashtable_iterator_value(itr); |
| 191 |
PERCTILE_STATS_LOG("print_perctile() - key: %s, perctile stat name: %s ", key, perc_stat->name); |
| 192 |
} while (hashtable_iterator_advance(itr)); |
| 193 |
PERCTILE_STATS_LOG("\n"); |
| 194 |
} |
| 195 |
} |
| 196 |
#endif |
| 197 |
|
| 198 |
// Assumes a fully created pstat and bkt, also initiliazes some values in pstat. |
| 199 |
static rsRetVal initAndAddPerctileMetrics(perctile_stat_t *pstat, perctile_bucket_t *bkt, uchar *key) { |
| 200 |
char stat_name[128]; |
| 201 |
int bytes = 0; |
| 202 |
int stat_name_len = sizeof(stat_name); |
| 203 |
DEFiRet; |
| 204 |
|
| 205 |
bytes = snprintf((char *)pstat->name, sizeof(pstat->name), "%s", key); |
| 206 |
if (bytes < 0 || bytes >= (int)sizeof(pstat->name)) { |
| 207 |
LogError(0, iRet, "statname '%s' truncated - too long for buffer size: %d\n", key, bytes); |
| 208 |
ABORT_FINALIZE(RS_RET_CONF_PARAM_INVLD); |
| 209 |
} |
| 210 |
|
| 211 |
int offset = snprintf(stat_name, stat_name_len, "%s%s", (char *)pstat->name, (char *)bkt->delim); |
| 212 |
if (offset < 0 || offset >= stat_name_len) { |
| 213 |
LogError(0, iRet, "statname '%s' delim '%s' truncated - too long for buffer size: %d\n", (char *)pstat->name, |
| 214 |
(char *)bkt->delim, offset); |
| 215 |
ABORT_FINALIZE(RS_RET_CONF_PARAM_INVLD); |
| 216 |
} |
| 217 |
|
| 218 |
int remaining_size = stat_name_len - offset - 1; |
| 219 |
// initialize the counters array |
| 220 |
for (size_t i = 0; i < pstat->perctile_ctrs_count; ++i) { |
| 221 |
perctile_ctr_t *ctr = &pstat->ctrs[i]; |
| 222 |
|
| 223 |
// bucket contains the supported percentile values. |
| 224 |
ctr->percentile = bkt->perctile_values[i]; |
| 225 |
bytes = snprintf(stat_name + offset, remaining_size, "p%d", bkt->perctile_values[i]); |
| 226 |
if (bytes < 0 || bytes >= remaining_size) { |
| 227 |
LogError(0, iRet, "statname '%s' truncated - too long for buffer size: %d\n", stat_name, stat_name_len); |
| 228 |
ABORT_FINALIZE(RS_RET_CONF_PARAM_INVLD); |
| 229 |
} |
| 230 |
CHKiRet(statsobj.AddManagedCounter(bkt->statsobj, (uchar *)stat_name, ctrType_IntCtr, CTR_FLAG_NONE, |
| 231 |
&ctr->ctr_perctile_stat, &ctr->ref_ctr_percentile_stat, 1)); |
| 232 |
} |
| 233 |
|
| 234 |
bytes = snprintf(stat_name + offset, remaining_size, "window_min"); |
| 235 |
if (bytes < 0 || bytes >= remaining_size) { |
| 236 |
LogError(0, iRet, "statname '%s' truncated - too long for buffer size: %d\n", stat_name, stat_name_len); |
| 237 |
ABORT_FINALIZE(RS_RET_CONF_PARAM_INVLD); |
| 238 |
} |
| 239 |
CHKiRet(statsobj.AddManagedCounter(bkt->statsobj, (uchar *)stat_name, ctrType_IntCtr, CTR_FLAG_NONE, |
| 240 |
&pstat->ctrWindowMin, &pstat->refCtrWindowMin, 1)); |
| 241 |
|
| 242 |
bytes = snprintf(stat_name + offset, remaining_size, "window_max"); |
| 243 |
if (bytes < 0 || bytes >= remaining_size) { |
| 244 |
LogError(0, iRet, "statname '%s' truncated - too long for buffer size: %d\n", stat_name, stat_name_len); |
| 245 |
ABORT_FINALIZE(RS_RET_CONF_PARAM_INVLD); |
| 246 |
} |
| 247 |
CHKiRet(statsobj.AddManagedCounter(bkt->statsobj, (uchar *)stat_name, ctrType_IntCtr, CTR_FLAG_NONE, |
| 248 |
&pstat->ctrWindowMax, &pstat->refCtrWindowMax, 1)); |
| 249 |
|
| 250 |
bytes = snprintf(stat_name + offset, remaining_size, "window_sum"); |
| 251 |
if (bytes < 0 || bytes >= remaining_size) { |
| 252 |
LogError(0, iRet, "statname '%s' truncated - too long for buffer size: %d\n", stat_name, stat_name_len); |
| 253 |
ABORT_FINALIZE(RS_RET_CONF_PARAM_INVLD); |
| 254 |
} |
| 255 |
CHKiRet(statsobj.AddManagedCounter(bkt->statsobj, (uchar *)stat_name, ctrType_IntCtr, CTR_FLAG_NONE, |
| 256 |
&pstat->ctrWindowSum, &pstat->refCtrWindowSum, 1)); |
| 257 |
|
| 258 |
bytes = snprintf(stat_name + offset, remaining_size, "window_count"); |
| 259 |
if (bytes < 0 || bytes >= remaining_size) { |
| 260 |
LogError(0, iRet, "statname '%s' truncated - too long for buffer size: %d\n", stat_name, stat_name_len); |
| 261 |
ABORT_FINALIZE(RS_RET_CONF_PARAM_INVLD); |
| 262 |
} |
| 263 |
CHKiRet(statsobj.AddManagedCounter(bkt->statsobj, (uchar *)stat_name, ctrType_IntCtr, CTR_FLAG_NONE, |
| 264 |
&pstat->ctrWindowCount, &pstat->refCtrWindowCount, 1)); |
| 265 |
|
| 266 |
finalize_it: |
| 267 |
if (iRet != RS_RET_OK) { |
| 268 |
LogError(0, iRet, "Could not initialize percentile stats."); |
| 269 |
} |
| 270 |
RETiRet; |
| 271 |
} |
| 272 |
|
| 273 |
static rsRetVal perctile_observe(perctile_bucket_t *bkt, uchar *key, int64_t value) { |
| 274 |
uint8_t lock_initialized = 0; |
| 275 |
uchar *hash_key = NULL; |
| 276 |
DEFiRet; |
| 277 |
time_t now; |
| 278 |
datetime.GetTime(&now); |
| 279 |
|
| 280 |
pthread_rwlock_wrlock(&bkt->lock); |
| 281 |
lock_initialized = 1; |
| 282 |
perctile_stat_t *pstat = (perctile_stat_t *)hashtable_search(bkt->htable, key); |
| 283 |
if (!pstat) { |
| 284 |
PERCTILE_STATS_LOG("perctile_observe(): key '%s' not found - creating new pstat", key); |
| 285 |
// create the pstat if not found |
| 286 |
CHKmalloc(pstat = calloc(1, sizeof(perctile_stat_t))); |
| 287 |
pstat->ctrs = calloc(bkt->perctile_values_count, sizeof(perctile_ctr_t)); |
| 288 |
if (!pstat->ctrs) { |
| 289 |
free(pstat); |
| 290 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 291 |
} |
| 292 |
pstat->perctile_ctrs_count = bkt->perctile_values_count; |
| 293 |
pstat->rb_observed_stats = ringbuf_new(bkt->window_size); |
| 294 |
if (!pstat->rb_observed_stats) { |
| 295 |
free(pstat->ctrs); |
| 296 |
free(pstat); |
| 297 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 298 |
} |
| 299 |
pstat->bReported = 0; |
| 300 |
pthread_rwlock_init(&pstat->stats_lock, NULL); |
| 301 |
|
| 302 |
// init all stat counters here |
| 303 |
pthread_rwlock_wrlock(&pstat->stats_lock); |
| 304 |
pstat->ctrWindowCount = pstat->ctrWindowMax = pstat->ctrWindowSum = 0; |
| 305 |
pstat->ctrWindowMin = value; |
| 306 |
pthread_rwlock_unlock(&pstat->stats_lock); |
| 307 |
|
| 308 |
iRet = initAndAddPerctileMetrics(pstat, bkt, key); |
| 309 |
if (iRet != RS_RET_OK) { |
| 310 |
perctileStatDestruct(bkt, pstat); |
| 311 |
ABORT_FINALIZE(iRet); |
| 312 |
} |
| 313 |
|
| 314 |
CHKmalloc(hash_key = ustrdup(key)); |
| 315 |
if (!hashtable_insert(bkt->htable, hash_key, pstat)) { |
| 316 |
perctileStatDestruct(bkt, pstat); |
| 317 |
free(hash_key); |
| 318 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 319 |
} |
| 320 |
PERCTILE_STATS_LOG("perctile_observe - new pstat created - name: %s\n", pstat->name); |
| 321 |
STATSCOUNTER_INC(bkt->ctrNewKeyAdd, bkt->mutCtrNewKeyAdd); |
| 322 |
} |
| 323 |
|
| 324 |
// add this value into the ringbuffer |
| 325 |
assert(pstat->rb_observed_stats); |
| 326 |
if (ringbuf_append_with_overwrite(pstat->rb_observed_stats, value) != 0) { |
| 327 |
// ringbuffer is operating in overwrite mode, so should never see this. |
| 328 |
ABORT_FINALIZE(RS_RET_ERR); |
| 329 |
} |
| 330 |
|
| 331 |
// update perctile specific stats |
| 332 |
pthread_rwlock_wrlock(&pstat->stats_lock); |
| 333 |
{ |
| 334 |
if (pstat->bReported) { |
| 335 |
// reset window values |
| 336 |
pstat->ctrWindowCount = pstat->ctrWindowSum = 0; |
| 337 |
pstat->ctrWindowMin = pstat->ctrWindowMax = value; |
| 338 |
pstat->bReported = 0; |
| 339 |
} |
| 340 |
++(pstat->ctrWindowCount); |
| 341 |
pstat->ctrWindowSum += value; |
| 342 |
pstat->ctrWindowMin = min(pstat->ctrWindowMin, value); |
| 343 |
pstat->ctrWindowMax = max(pstat->ctrWindowMax, value); |
| 344 |
} |
| 345 |
pthread_rwlock_unlock(&pstat->stats_lock); |
| 346 |
|
| 347 |
#ifdef PERCTILE_STATS_DEBUG |
| 348 |
PERCTILE_STATS_LOG("perctile_observe - appended value: %lld to ringbuffer\n", value); |
| 349 |
PERCTILE_STATS_LOG("ringbuffer contents... \n"); |
| 350 |
for (size_t i = 0; i < pstat->rb_observed_stats->size; ++i) { |
| 351 |
PERCTILE_STATS_LOG("%lld ", pstat->rb_observed_stats->cb.buf[i]); |
| 352 |
} |
| 353 |
PERCTILE_STATS_LOG("\n"); |
| 354 |
print_perctiles(bkt); |
| 355 |
#endif |
| 356 |
finalize_it: |
| 357 |
if (lock_initialized) { |
| 358 |
pthread_rwlock_unlock(&bkt->lock); |
| 359 |
} |
| 360 |
if (iRet != RS_RET_OK) { |
| 361 |
// clean up if there was an error |
| 362 |
if (iRet == RS_RET_OUT_OF_MEMORY) { |
| 363 |
STATSCOUNTER_INC(bkt->ctrOpsOverflow, bkt->mutCtrOpsOverflow); |
| 364 |
} |
| 365 |
} |
| 366 |
RETiRet; |
| 367 |
} |
| 368 |
|
| 369 |
static int cmp(const void *p1, const void *p2) { |
| 370 |
return (*(ITEM *)p1) - (*(ITEM *)p2); |
| 371 |
} |
| 372 |
|
| 373 |
static rsRetVal report_perctile_stats(perctile_bucket_t *pbkt) { |
| 374 |
ITEM *buf = NULL; |
| 375 |
struct hashtable_itr *itr = NULL; |
| 376 |
DEFiRet; |
| 377 |
|
| 378 |
pthread_rwlock_rdlock(&pbkt->lock); |
| 379 |
if (hashtable_count(pbkt->htable)) { |
| 380 |
itr = hashtable_iterator(pbkt->htable); |
| 381 |
CHKmalloc(buf = malloc(pbkt->window_size * sizeof(ITEM))); |
| 382 |
do { |
| 383 |
memset(buf, 0, pbkt->window_size * sizeof(ITEM)); |
| 384 |
perctile_stat_t *perc_stat = hashtable_iterator_value(itr); |
| 385 |
// ringbuffer read |
| 386 |
size_t count = ringbuf_read_to_end(perc_stat->rb_observed_stats, buf, pbkt->window_size); |
| 387 |
if (!count) { |
| 388 |
continue; |
| 389 |
} |
| 390 |
PERCTILE_STATS_LOG("read %zu values\n", count); |
| 391 |
// calculate the p95 based on the |
| 392 |
#ifdef PERCTILE_STATS_DEBUG |
| 393 |
PERCTILE_STATS_LOG("ringbuffer contents... \n"); |
| 394 |
for (size_t i = 0; i < perc_stat->rb_observed_stats->size; ++i) { |
| 395 |
PERCTILE_STATS_LOG("%lld ", perc_stat->rb_observed_stats->cb.buf[i]); |
| 396 |
} |
| 397 |
PERCTILE_STATS_LOG("\n"); |
| 398 |
|
| 399 |
PERCTILE_STATS_LOG("buffer contents... \n"); |
| 400 |
for (size_t i = 0; i < perc_stat->rb_observed_stats->size; ++i) { |
| 401 |
PERCTILE_STATS_LOG("%lld ", buf[i]); |
| 402 |
} |
| 403 |
PERCTILE_STATS_LOG("\n"); |
| 404 |
#endif |
| 405 |
qsort(buf, count, sizeof(ITEM), cmp); |
| 406 |
#ifdef PERCTILE_STATS_DEBUG |
| 407 |
PERCTILE_STATS_LOG("buffer contents after sort... \n"); |
| 408 |
for (size_t i = 0; i < perc_stat->rb_observed_stats->size; ++i) { |
| 409 |
PERCTILE_STATS_LOG("%lld ", buf[i]); |
| 410 |
} |
| 411 |
PERCTILE_STATS_LOG("\n"); |
| 412 |
#endif |
| 413 |
PERCTILE_STATS_LOG("report_perctile_stats() - perctile stat has %zu counters.", |
| 414 |
perc_stat->perctile_ctrs_count); |
| 415 |
for (size_t i = 0; i < perc_stat->perctile_ctrs_count; ++i) { |
| 416 |
perctile_ctr_t *pctr = &perc_stat->ctrs[i]; |
| 417 |
// get percentile - this can be cached. |
| 418 |
int index = max(0, ((pctr->percentile / 100.0) * count) - 1); |
| 419 |
// look into if we need to lock this. |
| 420 |
pctr->ctr_perctile_stat = buf[index]; |
| 421 |
PERCTILE_STATS_LOG("report_perctile_stats() - index: %d, perctile stat [%s, %d, %llu]", index, |
| 422 |
perc_stat->name, pctr->percentile, pctr->ctr_perctile_stat); |
| 423 |
} |
| 424 |
perc_stat->bReported = 1; |
| 425 |
} while (hashtable_iterator_advance(itr)); |
| 426 |
} |
| 427 |
|
| 428 |
finalize_it: |
| 429 |
pthread_rwlock_unlock(&pbkt->lock); |
| 430 |
free(itr); |
| 431 |
free(buf); |
| 432 |
RETiRet; |
| 433 |
} |
| 434 |
|
| 435 |
static void perctile_readCallback(statsobj_t __attribute__((unused)) * ignore, void __attribute__((unused)) * b) { |
| 436 |
perctile_buckets_t *bkts = &runConf->perctile_buckets; |
| 437 |
|
| 438 |
pthread_rwlock_rdlock(&bkts->lock); |
| 439 |
for (perctile_bucket_t *pbkt = bkts->listBuckets; pbkt != NULL; pbkt = pbkt->next) { |
| 440 |
report_perctile_stats(pbkt); |
| 441 |
} |
| 442 |
pthread_rwlock_unlock(&bkts->lock); |
| 443 |
} |
| 444 |
|
| 445 |
static rsRetVal perctileInitNewBucketStats(perctile_bucket_t *b) { |
| 446 |
DEFiRet; |
| 447 |
|
| 448 |
CHKiRet(statsobj.Construct(&b->statsobj)); |
| 449 |
CHKiRet(statsobj.SetOrigin(b->statsobj, UCHAR_CONSTANT("percentile.bucket"))); |
| 450 |
CHKiRet(statsobj.SetName(b->statsobj, b->name)); |
| 451 |
CHKiRet(statsobj.SetReportingNamespace(b->statsobj, UCHAR_CONSTANT("values"))); |
| 452 |
statsobj.SetReadNotifier(b->statsobj, perctile_readCallback, b); |
| 453 |
CHKiRet(statsobj.ConstructFinalize(b->statsobj)); |
| 454 |
|
| 455 |
finalize_it: |
| 456 |
RETiRet; |
| 457 |
} |
| 458 |
|
| 459 |
static rsRetVal perctileAddBucketMetrics(perctile_buckets_t *bkts, perctile_bucket_t *b, const uchar *name) { |
| 460 |
uchar *metric_name_buff, *metric_suffix; |
| 461 |
const uchar *suffix_litteral; |
| 462 |
int name_len; |
| 463 |
DEFiRet; |
| 464 |
|
| 465 |
name_len = ustrlen(name); |
| 466 |
CHKmalloc(metric_name_buff = malloc((name_len + PERCTILE_MAX_BUCKET_NS_METRIC_LENGTH + 1) * sizeof(uchar))); |
| 467 |
|
| 468 |
strcpy((char *)metric_name_buff, (char *)name); |
| 469 |
metric_suffix = metric_name_buff + name_len; |
| 470 |
*metric_suffix = PERCTILE_METRIC_NAME_SEPARATOR; |
| 471 |
metric_suffix++; |
| 472 |
|
| 473 |
suffix_litteral = UCHAR_CONSTANT("new_metric_add"); |
| 474 |
ustrncpy(metric_suffix, suffix_litteral, PERCTILE_MAX_BUCKET_NS_METRIC_LENGTH); |
| 475 |
STATSCOUNTER_INIT(b->ctrNewKeyAdd, b->mutCtrNewKeyAdd); |
| 476 |
CHKiRet(statsobj.AddManagedCounter(bkts->global_stats, metric_name_buff, ctrType_IntCtr, CTR_FLAG_RESETTABLE, |
| 477 |
&(b->ctrNewKeyAdd), &b->pNewKeyAddCtr, 1)); |
| 478 |
|
| 479 |
suffix_litteral = UCHAR_CONSTANT("ops_overflow"); |
| 480 |
ustrncpy(metric_suffix, suffix_litteral, PERCTILE_MAX_BUCKET_NS_METRIC_LENGTH); |
| 481 |
STATSCOUNTER_INIT(b->ctrOpsOverflow, b->mutCtrOpsOverflow); |
| 482 |
CHKiRet(statsobj.AddManagedCounter(bkts->global_stats, metric_name_buff, ctrType_IntCtr, CTR_FLAG_RESETTABLE, |
| 483 |
&(b->ctrOpsOverflow), &b->pOpsOverflowCtr, 1)); |
| 484 |
|
| 485 |
finalize_it: |
| 486 |
free(metric_name_buff); |
| 487 |
if (iRet != RS_RET_OK) { |
| 488 |
if (b->pOpsOverflowCtr != NULL) { |
| 489 |
statsobj.DestructCounter(bkts->global_stats, b->pOpsOverflowCtr); |
| 490 |
} |
| 491 |
if (b->pNewKeyAddCtr != NULL) { |
| 492 |
statsobj.DestructCounter(bkts->global_stats, b->pNewKeyAddCtr); |
| 493 |
} |
| 494 |
} |
| 495 |
RETiRet; |
| 496 |
} |
| 497 |
|
| 498 |
/* Create new perctile bucket, and add it to our list of perctile buckets. |
| 499 |
*/ |
| 500 |
static rsRetVal perctile_newBucket( |
| 501 |
const uchar *name, const uchar *delim, uint8_t *perctiles, uint32_t perctilesCount, uint32_t windowSize) { |
| 502 |
perctile_buckets_t *bkts; |
| 503 |
perctile_bucket_t *b = NULL; |
| 504 |
pthread_rwlockattr_t bucket_lock_attr; |
| 505 |
DEFiRet; |
| 506 |
|
| 507 |
bkts = &loadConf->perctile_buckets; |
| 508 |
|
| 509 |
if (bkts->initialized) { |
| 510 |
CHKmalloc(b = calloc(1, sizeof(perctile_bucket_t))); |
| 511 |
|
| 512 |
// initialize |
| 513 |
pthread_rwlockattr_init(&bucket_lock_attr); |
| 514 |
pthread_rwlock_init(&b->lock, &bucket_lock_attr); |
| 515 |
CHKmalloc(b->htable = create_hashtable(7, hash_from_string, key_equals_string, NULL)); |
| 516 |
CHKmalloc(b->name = ustrdup(name)); |
| 517 |
if (delim) { |
| 518 |
CHKmalloc(b->delim = ustrdup(delim)); |
| 519 |
} else { |
| 520 |
CHKmalloc(b->delim = ustrdup(".")); |
| 521 |
} |
| 522 |
|
| 523 |
CHKmalloc(b->perctile_values = calloc(perctilesCount, sizeof(uint8_t))); |
| 524 |
b->perctile_values_count = perctilesCount; |
| 525 |
memcpy(b->perctile_values, perctiles, perctilesCount * sizeof(uint8_t)); |
| 526 |
b->window_size = windowSize; |
| 527 |
b->next = NULL; |
| 528 |
PERCTILE_STATS_LOG( |
| 529 |
"perctile_newBucket: create new bucket for %s," |
| 530 |
"with windowsize: %d, values_count: %zu\n", |
| 531 |
b->name, b->window_size, b->perctile_values_count); |
| 532 |
|
| 533 |
// add bucket to list of buckets |
| 534 |
if (!bkts->listBuckets) { |
| 535 |
// none yet |
| 536 |
bkts->listBuckets = b; |
| 537 |
PERCTILE_STATS_LOG("perctile_newBucket: Adding new bucket to empty list \n"); |
| 538 |
} else { |
| 539 |
b->next = bkts->listBuckets; |
| 540 |
bkts->listBuckets = b; |
| 541 |
PERCTILE_STATS_LOG("perctile_newBucket: prepended new bucket list \n"); |
| 542 |
} |
| 543 |
|
| 544 |
// create the statsobj for this bucket |
| 545 |
CHKiRet(perctileInitNewBucketStats(b)); |
| 546 |
CHKiRet(perctileAddBucketMetrics(bkts, b, name)); |
| 547 |
} else { |
| 548 |
LogError(0, RS_RET_INTERNAL_ERROR, |
| 549 |
"perctile: bucket creation failed, as " |
| 550 |
"global-initialization of buckets was unsuccessful"); |
| 551 |
ABORT_FINALIZE(RS_RET_INTERNAL_ERROR); |
| 552 |
} |
| 553 |
finalize_it: |
| 554 |
if (iRet != RS_RET_OK) { |
| 555 |
if (b != NULL) { |
| 556 |
perctileBucketDestruct(b); |
| 557 |
} |
| 558 |
} |
| 559 |
RETiRet; |
| 560 |
} |
| 561 |
|
| 562 |
// Public functions |
| 563 |
rsRetVal perctile_processCnf(struct cnfobj *o) { |
| 564 |
struct cnfparamvals *pvals; |
| 565 |
uchar *name = NULL; |
| 566 |
uchar *delim = NULL; |
| 567 |
uint8_t *perctiles = NULL; |
| 568 |
uint32_t perctilesCount = 0; |
| 569 |
uint64_t windowSize = 0; |
| 570 |
DEFiRet; |
| 571 |
|
| 572 |
pvals = nvlstGetParams(o->nvlst, &modpblk, NULL); |
| 573 |
if (pvals == NULL) { |
| 574 |
ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS); |
| 575 |
} |
| 576 |
|
| 577 |
for (short i = 0; i < modpblk.nParams; ++i) { |
| 578 |
if (!pvals[i].bUsed) continue; |
| 579 |
if (!strcmp(modpblk.descr[i].name, PERCTILE_CONF_PARAM_NAME)) { |
| 580 |
CHKmalloc(name = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL)); |
| 581 |
} else if (!strcmp(modpblk.descr[i].name, PERCTILE_CONF_PARAM_DELIM)) { |
| 582 |
CHKmalloc(delim = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL)); |
| 583 |
} else if (!strcmp(modpblk.descr[i].name, PERCTILE_CONF_PARAM_PERCENTILES)) { |
| 584 |
/* Only the first instance of this parameter will be accepted */ |
| 585 |
if (!perctiles) { |
| 586 |
perctilesCount = pvals[i].val.d.ar->nmemb; |
| 587 |
if (perctilesCount) { |
| 588 |
CHKmalloc(perctiles = calloc(perctilesCount, sizeof(uint8_t))); |
| 589 |
for (int j = 0; j < pvals[i].val.d.ar->nmemb; ++j) { |
| 590 |
char *cstr = es_str2cstr(pvals[i].val.d.ar->arr[j], NULL); |
| 591 |
perctiles[j] = atoi(cstr); |
| 592 |
free(cstr); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
} else if (!strcmp(modpblk.descr[i].name, PERCTILE_CONF_PARAM_WINDOW_SIZE)) { |
| 597 |
windowSize = pvals[i].val.d.n; |
| 598 |
} else { |
| 599 |
dbgprintf( |
| 600 |
"perctile: program error, non-handled " |
| 601 |
"param '%s'\n", |
| 602 |
modpblk.descr[i].name); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
if (name != NULL && perctiles != NULL) { |
| 607 |
CHKiRet(perctile_newBucket(name, delim, perctiles, perctilesCount, windowSize)); |
| 608 |
} |
| 609 |
|
| 610 |
finalize_it: |
| 611 |
free(name); |
| 612 |
free(delim); |
| 613 |
free(perctiles); |
| 614 |
cnfparamvalsDestruct(pvals, &modpblk); |
| 615 |
RETiRet; |
| 616 |
} |
| 617 |
|
| 618 |
rsRetVal perctile_initCnf(perctile_buckets_t *bkts) { |
| 619 |
DEFiRet; |
| 620 |
|
| 621 |
bkts->initialized = 0; |
| 622 |
bkts->listBuckets = NULL; |
| 623 |
CHKiRet(statsobj.Construct(&bkts->global_stats)); |
| 624 |
CHKiRet(statsobj.SetOrigin(bkts->global_stats, UCHAR_CONSTANT("percentile"))); |
| 625 |
CHKiRet(statsobj.SetName(bkts->global_stats, UCHAR_CONSTANT("global"))); |
| 626 |
CHKiRet(statsobj.SetReportingNamespace(bkts->global_stats, UCHAR_CONSTANT("values"))); |
| 627 |
CHKiRet(statsobj.ConstructFinalize(bkts->global_stats)); |
| 628 |
pthread_rwlock_init(&bkts->lock, NULL); |
| 629 |
bkts->initialized = 1; |
| 630 |
|
| 631 |
finalize_it: |
| 632 |
if (iRet != RS_RET_OK) { |
| 633 |
statsobj.Destruct(&bkts->global_stats); |
| 634 |
} |
| 635 |
RETiRet; |
| 636 |
} |
| 637 |
|
| 638 |
perctile_bucket_t *perctile_findBucket(const uchar *name) { |
| 639 |
perctile_bucket_t *b = NULL; |
| 640 |
|
| 641 |
perctile_buckets_t *bkts = &loadConf->perctile_buckets; |
| 642 |
if (bkts->initialized) { |
| 643 |
pthread_rwlock_rdlock(&bkts->lock); |
| 644 |
if (bkts->listBuckets) { |
| 645 |
b = findBucket(bkts->listBuckets, name); |
| 646 |
} |
| 647 |
pthread_rwlock_unlock(&bkts->lock); |
| 648 |
} else { |
| 649 |
LogError(0, RS_RET_INTERNAL_ERROR, |
| 650 |
"perctile: bucket lookup failed, as global-initialization " |
| 651 |
"of buckets was unsuccessful"); |
| 652 |
} |
| 653 |
return b; |
| 654 |
} |
| 655 |
|
| 656 |
rsRetVal perctile_obs(perctile_bucket_t *perctile_bkt, uchar *key, int64_t value) { |
| 657 |
DEFiRet; |
| 658 |
if (!perctile_bkt) { |
| 659 |
LogError(0, RS_RET_INTERNAL_ERROR, "perctile() - perctile bkt not available"); |
| 660 |
FINALIZE; |
| 661 |
} |
| 662 |
PERCTILE_STATS_LOG("perctile_obs() - bucket name: %s, key: %s, val: %" PRId64 "\n", perctile_bkt->name, key, value); |
| 663 |
|
| 664 |
CHKiRet(perctile_observe(perctile_bkt, key, value)); |
| 665 |
|
| 666 |
finalize_it: |
| 667 |
if (iRet != RS_RET_OK) { |
| 668 |
LogError(0, RS_RET_INTERNAL_ERROR, "perctile_obs(): name: %s, key: %s, val: %" PRId64 "\n", perctile_bkt->name, |
| 669 |
key, value); |
| 670 |
} |
| 671 |
RETiRet; |
| 672 |
} |