1 /*- 2 * Copyright (c) 2014-2018 Netflix, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * Author: Lawrence Stewart <lstewart@netflix.com> 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/arb.h> 38 #include <sys/ctype.h> 39 #include <sys/errno.h> 40 #include <sys/hash.h> 41 #include <sys/limits.h> 42 #include <sys/malloc.h> 43 #include <sys/qmath.h> 44 #include <sys/sbuf.h> 45 #if defined(DIAGNOSTIC) 46 #include <sys/tree.h> 47 #endif 48 #include <sys/stats.h> /* Must come after qmath.h and arb.h */ 49 #include <sys/stddef.h> 50 #include <sys/stdint.h> 51 #include <sys/time.h> 52 53 #ifdef _KERNEL 54 #include <sys/kernel.h> 55 #include <sys/lock.h> 56 #include <sys/rwlock.h> 57 #include <sys/sysctl.h> 58 #include <sys/systm.h> 59 #else /* ! _KERNEL */ 60 #include <pthread.h> 61 #include <stdbool.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #endif /* _KERNEL */ 66 67 struct voistatdata_voistate { 68 /* Previous VOI value for diff calculation. */ 69 struct voistatdata_numeric prev; 70 }; 71 72 #define VS_VSDVALID 0x0001 /* Stat's voistatdata updated at least once. */ 73 struct voistat { 74 int8_t stype; /* Type of stat e.g. VS_STYPE_SUM. */ 75 enum vsd_dtype dtype : 8; /* Data type of this stat's data. */ 76 uint16_t data_off; /* Blob offset for this stat's data. */ 77 uint16_t dsz; /* Size of stat's data. */ 78 #define VS_EBITS 8 79 uint16_t errs : VS_EBITS;/* Non-wrapping error count. */ 80 uint16_t flags : 16 - VS_EBITS; 81 }; 82 /* The voistat error count is capped to avoid wrapping. */ 83 #define VS_INCERRS(vs) do { \ 84 if ((vs)->errs < (1U << VS_EBITS) - 1) \ 85 (vs)->errs++; \ 86 } while (0) 87 88 /* 89 * Ideas for flags: 90 * - Global or entity specific (global would imply use of counter(9)?) 91 * - Whether to reset stats on read or not 92 * - Signal an overflow? 93 * - Compressed voistat array 94 */ 95 #define VOI_REQSTATE 0x0001 /* VOI requires VS_STYPE_VOISTATE. */ 96 struct voi { 97 int16_t id; /* VOI id. */ 98 enum vsd_dtype dtype : 8; /* Data type of the VOI itself. */ 99 int8_t voistatmaxid; /* Largest allocated voistat index. */ 100 uint16_t stats_off; /* Blob offset for this VOIs stats. */ 101 uint16_t flags; 102 }; 103 104 /* 105 * Memory for the entire blob is allocated as a slab and then offsets are 106 * maintained to carve up the slab into sections holding different data types. 107 * 108 * Ideas for flags: 109 * - Compressed voi array (trade off memory usage vs search time) 110 * - Units of offsets (default bytes, flag for e.g. vm_page/KiB/Mib) 111 */ 112 struct statsblobv1 { 113 uint8_t abi; 114 uint8_t endian; 115 uint16_t flags; 116 uint16_t maxsz; 117 uint16_t cursz; 118 /* Fields from here down are opaque to consumers. */ 119 uint32_t tplhash; /* Base template hash ID. */ 120 uint16_t stats_off; /* voistat array blob offset. */ 121 uint16_t statsdata_off; /* voistatdata array blob offset. */ 122 sbintime_t created; /* Blob creation time. */ 123 sbintime_t lastrst; /* Time of last reset. */ 124 struct voi vois[]; /* Array indexed by [voi_id]. */ 125 } __aligned(sizeof(void *)); 126 _Static_assert(offsetof(struct statsblobv1, cursz) + 127 SIZEOF_MEMBER(struct statsblobv1, cursz) == 128 offsetof(struct statsblob, opaque), 129 "statsblobv1 ABI mismatch"); 130 131 struct statsblobv1_tpl { 132 struct metablob *mb; 133 struct statsblobv1 *sb; 134 }; 135 136 /* Context passed to iterator callbacks. */ 137 struct sb_iter_ctx { 138 void *usrctx; /* Caller supplied context. */ 139 uint32_t flags; /* Flags for current iteration. */ 140 int16_t vslot; /* struct voi slot index. */ 141 int8_t vsslot; /* struct voistat slot index. */ 142 }; 143 144 struct sb_tostrcb_ctx { 145 struct sbuf *buf; 146 struct statsblob_tpl *tpl; 147 enum sb_str_fmt fmt; 148 uint32_t flags; 149 }; 150 151 struct sb_visitcb_ctx { 152 stats_blob_visitcb_t cb; 153 void *usrctx; 154 }; 155 156 /* Stats blob iterator callback. */ 157 typedef int (*stats_v1_blob_itercb_t)(struct statsblobv1 *sb, struct voi *v, 158 struct voistat *vs, struct sb_iter_ctx *ctx); 159 160 #ifdef _KERNEL 161 static struct rwlock tpllistlock; 162 RW_SYSINIT(stats_tpl_list, &tpllistlock, "Stat template list lock"); 163 #define TPL_LIST_RLOCK() rw_rlock(&tpllistlock) 164 #define TPL_LIST_RUNLOCK() rw_runlock(&tpllistlock) 165 #define TPL_LIST_WLOCK() rw_wlock(&tpllistlock) 166 #define TPL_LIST_WUNLOCK() rw_wunlock(&tpllistlock) 167 #define TPL_LIST_LOCK_ASSERT() rw_assert(&tpllistlock, RA_LOCKED) 168 #define TPL_LIST_RLOCK_ASSERT() rw_assert(&tpllistlock, RA_RLOCKED) 169 #define TPL_LIST_WLOCK_ASSERT() rw_assert(&tpllistlock, RA_WLOCKED) 170 MALLOC_DEFINE(M_STATS, "stats(9) related memory", "stats(9) related memory"); 171 #define stats_free(ptr) free((ptr), M_STATS) 172 #else /* ! _KERNEL */ 173 static void stats_constructor(void); 174 static void stats_destructor(void); 175 static pthread_rwlock_t tpllistlock; 176 #define TPL_LIST_UNLOCK() pthread_rwlock_unlock(&tpllistlock) 177 #define TPL_LIST_RLOCK() pthread_rwlock_rdlock(&tpllistlock) 178 #define TPL_LIST_RUNLOCK() TPL_LIST_UNLOCK() 179 #define TPL_LIST_WLOCK() pthread_rwlock_wrlock(&tpllistlock) 180 #define TPL_LIST_WUNLOCK() TPL_LIST_UNLOCK() 181 #define TPL_LIST_LOCK_ASSERT() do { } while (0) 182 #define TPL_LIST_RLOCK_ASSERT() do { } while (0) 183 #define TPL_LIST_WLOCK_ASSERT() do { } while (0) 184 #ifdef NDEBUG 185 #define KASSERT(cond, msg) do {} while (0) 186 #define stats_abort() do {} while (0) 187 #else /* ! NDEBUG */ 188 #define KASSERT(cond, msg) do { \ 189 if (!(cond)) { \ 190 panic msg; \ 191 } \ 192 } while (0) 193 #define stats_abort() abort() 194 #endif /* NDEBUG */ 195 #define stats_free(ptr) free(ptr) 196 #define panic(fmt, ...) do { \ 197 fprintf(stderr, (fmt), ##__VA_ARGS__); \ 198 stats_abort(); \ 199 } while (0) 200 #endif /* _KERNEL */ 201 202 #define SB_V1_MAXSZ 65535 203 204 /* Obtain a blob offset pointer. */ 205 #define BLOB_OFFSET(sb, off) ((void *)(((uint8_t *)(sb)) + (off))) 206 207 /* 208 * Number of VOIs in the blob's vois[] array. By virtue of struct voi being a 209 * power of 2 size, we can shift instead of divide. The shift amount must be 210 * updated if sizeof(struct voi) ever changes, which the assert should catch. 211 */ 212 #define NVOIS(sb) ((int32_t)((((struct statsblobv1 *)(sb))->stats_off - \ 213 sizeof(struct statsblobv1)) >> 3)) 214 _Static_assert(sizeof(struct voi) == 8, "statsblobv1 voi ABI mismatch"); 215 216 /* Try restrict names to alphanumeric and underscore to simplify JSON compat. */ 217 const char *vs_stype2name[VS_NUM_STYPES] = { 218 [VS_STYPE_VOISTATE] = "VOISTATE", 219 [VS_STYPE_SUM] = "SUM", 220 [VS_STYPE_MAX] = "MAX", 221 [VS_STYPE_MIN] = "MIN", 222 [VS_STYPE_HIST] = "HIST", 223 [VS_STYPE_TDGST] = "TDGST", 224 }; 225 226 const char *vs_stype2desc[VS_NUM_STYPES] = { 227 [VS_STYPE_VOISTATE] = "VOI related state data (not a real stat)", 228 [VS_STYPE_SUM] = "Simple arithmetic accumulator", 229 [VS_STYPE_MAX] = "Maximum observed VOI value", 230 [VS_STYPE_MIN] = "Minimum observed VOI value", 231 [VS_STYPE_HIST] = "Histogram of observed VOI values", 232 [VS_STYPE_TDGST] = "t-digest of observed VOI values", 233 }; 234 235 const char *vsd_dtype2name[VSD_NUM_DTYPES] = { 236 [VSD_DTYPE_VOISTATE] = "VOISTATE", 237 [VSD_DTYPE_INT_S32] = "INT_S32", 238 [VSD_DTYPE_INT_U32] = "INT_U32", 239 [VSD_DTYPE_INT_S64] = "INT_S64", 240 [VSD_DTYPE_INT_U64] = "INT_U64", 241 [VSD_DTYPE_INT_SLONG] = "INT_SLONG", 242 [VSD_DTYPE_INT_ULONG] = "INT_ULONG", 243 [VSD_DTYPE_Q_S32] = "Q_S32", 244 [VSD_DTYPE_Q_U32] = "Q_U32", 245 [VSD_DTYPE_Q_S64] = "Q_S64", 246 [VSD_DTYPE_Q_U64] = "Q_U64", 247 [VSD_DTYPE_CRHIST32] = "CRHIST32", 248 [VSD_DTYPE_DRHIST32] = "DRHIST32", 249 [VSD_DTYPE_DVHIST32] = "DVHIST32", 250 [VSD_DTYPE_CRHIST64] = "CRHIST64", 251 [VSD_DTYPE_DRHIST64] = "DRHIST64", 252 [VSD_DTYPE_DVHIST64] = "DVHIST64", 253 [VSD_DTYPE_TDGSTCLUST32] = "TDGSTCLUST32", 254 [VSD_DTYPE_TDGSTCLUST64] = "TDGSTCLUST64", 255 }; 256 257 const size_t vsd_dtype2size[VSD_NUM_DTYPES] = { 258 [VSD_DTYPE_VOISTATE] = sizeof(struct voistatdata_voistate), 259 [VSD_DTYPE_INT_S32] = sizeof(struct voistatdata_int32), 260 [VSD_DTYPE_INT_U32] = sizeof(struct voistatdata_int32), 261 [VSD_DTYPE_INT_S64] = sizeof(struct voistatdata_int64), 262 [VSD_DTYPE_INT_U64] = sizeof(struct voistatdata_int64), 263 [VSD_DTYPE_INT_SLONG] = sizeof(struct voistatdata_intlong), 264 [VSD_DTYPE_INT_ULONG] = sizeof(struct voistatdata_intlong), 265 [VSD_DTYPE_Q_S32] = sizeof(struct voistatdata_q32), 266 [VSD_DTYPE_Q_U32] = sizeof(struct voistatdata_q32), 267 [VSD_DTYPE_Q_S64] = sizeof(struct voistatdata_q64), 268 [VSD_DTYPE_Q_U64] = sizeof(struct voistatdata_q64), 269 [VSD_DTYPE_CRHIST32] = sizeof(struct voistatdata_crhist32), 270 [VSD_DTYPE_DRHIST32] = sizeof(struct voistatdata_drhist32), 271 [VSD_DTYPE_DVHIST32] = sizeof(struct voistatdata_dvhist32), 272 [VSD_DTYPE_CRHIST64] = sizeof(struct voistatdata_crhist64), 273 [VSD_DTYPE_DRHIST64] = sizeof(struct voistatdata_drhist64), 274 [VSD_DTYPE_DVHIST64] = sizeof(struct voistatdata_dvhist64), 275 [VSD_DTYPE_TDGSTCLUST32] = sizeof(struct voistatdata_tdgstclust32), 276 [VSD_DTYPE_TDGSTCLUST64] = sizeof(struct voistatdata_tdgstclust64), 277 }; 278 279 static const bool vsd_compoundtype[VSD_NUM_DTYPES] = { 280 [VSD_DTYPE_VOISTATE] = true, 281 [VSD_DTYPE_INT_S32] = false, 282 [VSD_DTYPE_INT_U32] = false, 283 [VSD_DTYPE_INT_S64] = false, 284 [VSD_DTYPE_INT_U64] = false, 285 [VSD_DTYPE_INT_SLONG] = false, 286 [VSD_DTYPE_INT_ULONG] = false, 287 [VSD_DTYPE_Q_S32] = false, 288 [VSD_DTYPE_Q_U32] = false, 289 [VSD_DTYPE_Q_S64] = false, 290 [VSD_DTYPE_Q_U64] = false, 291 [VSD_DTYPE_CRHIST32] = true, 292 [VSD_DTYPE_DRHIST32] = true, 293 [VSD_DTYPE_DVHIST32] = true, 294 [VSD_DTYPE_CRHIST64] = true, 295 [VSD_DTYPE_DRHIST64] = true, 296 [VSD_DTYPE_DVHIST64] = true, 297 [VSD_DTYPE_TDGSTCLUST32] = true, 298 [VSD_DTYPE_TDGSTCLUST64] = true, 299 }; 300 301 const struct voistatdata_numeric numeric_limits[2][VSD_DTYPE_Q_U64 + 1] = { 302 [LIM_MIN] = { 303 [VSD_DTYPE_VOISTATE] = {0}, 304 [VSD_DTYPE_INT_S32] = {.int32 = {.s32 = INT32_MIN}}, 305 [VSD_DTYPE_INT_U32] = {.int32 = {.u32 = 0}}, 306 [VSD_DTYPE_INT_S64] = {.int64 = {.s64 = INT64_MIN}}, 307 [VSD_DTYPE_INT_U64] = {.int64 = {.u64 = 0}}, 308 [VSD_DTYPE_INT_SLONG] = {.intlong = {.slong = LONG_MIN}}, 309 [VSD_DTYPE_INT_ULONG] = {.intlong = {.ulong = 0}}, 310 [VSD_DTYPE_Q_S32] = {.q32 = {.sq32 = Q_IFMINVAL(INT32_MIN)}}, 311 [VSD_DTYPE_Q_U32] = {.q32 = {.uq32 = 0}}, 312 [VSD_DTYPE_Q_S64] = {.q64 = {.sq64 = Q_IFMINVAL(INT64_MIN)}}, 313 [VSD_DTYPE_Q_U64] = {.q64 = {.uq64 = 0}}, 314 }, 315 [LIM_MAX] = { 316 [VSD_DTYPE_VOISTATE] = {0}, 317 [VSD_DTYPE_INT_S32] = {.int32 = {.s32 = INT32_MAX}}, 318 [VSD_DTYPE_INT_U32] = {.int32 = {.u32 = UINT32_MAX}}, 319 [VSD_DTYPE_INT_S64] = {.int64 = {.s64 = INT64_MAX}}, 320 [VSD_DTYPE_INT_U64] = {.int64 = {.u64 = UINT64_MAX}}, 321 [VSD_DTYPE_INT_SLONG] = {.intlong = {.slong = LONG_MAX}}, 322 [VSD_DTYPE_INT_ULONG] = {.intlong = {.ulong = ULONG_MAX}}, 323 [VSD_DTYPE_Q_S32] = {.q32 = {.sq32 = Q_IFMAXVAL(INT32_MAX)}}, 324 [VSD_DTYPE_Q_U32] = {.q32 = {.uq32 = Q_IFMAXVAL(UINT32_MAX)}}, 325 [VSD_DTYPE_Q_S64] = {.q64 = {.sq64 = Q_IFMAXVAL(INT64_MAX)}}, 326 [VSD_DTYPE_Q_U64] = {.q64 = {.uq64 = Q_IFMAXVAL(UINT64_MAX)}}, 327 } 328 }; 329 330 /* tpllistlock protects tpllist and ntpl */ 331 static uint32_t ntpl; 332 static struct statsblob_tpl **tpllist; 333 334 static inline void * stats_realloc(void *ptr, size_t oldsz, size_t newsz, 335 int flags); 336 //static void stats_v1_blob_finalise(struct statsblobv1 *sb); 337 static int stats_v1_blob_init_locked(struct statsblobv1 *sb, uint32_t tpl_id, 338 uint32_t flags); 339 static int stats_v1_blob_expand(struct statsblobv1 **sbpp, int newvoibytes, 340 int newvoistatbytes, int newvoistatdatabytes); 341 static void stats_v1_blob_iter(struct statsblobv1 *sb, 342 stats_v1_blob_itercb_t icb, void *usrctx, uint32_t flags); 343 static inline int stats_v1_vsd_tdgst_add(enum vsd_dtype vs_dtype, 344 struct voistatdata_tdgst *tdgst, s64q_t x, uint64_t weight, int attempt); 345 346 static inline int 347 ctd32cmp(const struct voistatdata_tdgstctd32 *c1, const struct voistatdata_tdgstctd32 *c2) 348 { 349 350 KASSERT(Q_PRECEQ(c1->mu, c2->mu), 351 ("%s: Q_RELPREC(c1->mu,c2->mu)=%d", __func__, 352 Q_RELPREC(c1->mu, c2->mu))); 353 354 return (Q_QLTQ(c1->mu, c2->mu) ? -1 : 1); 355 } 356 ARB_GENERATE_STATIC(ctdth32, voistatdata_tdgstctd32, ctdlnk, ctd32cmp); 357 358 static inline int 359 ctd64cmp(const struct voistatdata_tdgstctd64 *c1, const struct voistatdata_tdgstctd64 *c2) 360 { 361 362 KASSERT(Q_PRECEQ(c1->mu, c2->mu), 363 ("%s: Q_RELPREC(c1->mu,c2->mu)=%d", __func__, 364 Q_RELPREC(c1->mu, c2->mu))); 365 366 return (Q_QLTQ(c1->mu, c2->mu) ? -1 : 1); 367 } 368 ARB_GENERATE_STATIC(ctdth64, voistatdata_tdgstctd64, ctdlnk, ctd64cmp); 369 370 #ifdef DIAGNOSTIC 371 RB_GENERATE_STATIC(rbctdth32, voistatdata_tdgstctd32, rblnk, ctd32cmp); 372 RB_GENERATE_STATIC(rbctdth64, voistatdata_tdgstctd64, rblnk, ctd64cmp); 373 #endif 374 375 static inline sbintime_t 376 stats_sbinuptime(void) 377 { 378 sbintime_t sbt; 379 #ifdef _KERNEL 380 381 sbt = sbinuptime(); 382 #else /* ! _KERNEL */ 383 struct timespec tp; 384 385 clock_gettime(CLOCK_MONOTONIC_FAST, &tp); 386 sbt = tstosbt(tp); 387 #endif /* _KERNEL */ 388 389 return (sbt); 390 } 391 392 static inline void * 393 stats_realloc(void *ptr, size_t oldsz, size_t newsz, int flags) 394 { 395 396 #ifdef _KERNEL 397 /* Default to M_NOWAIT if neither M_NOWAIT or M_WAITOK are set. */ 398 if (!(flags & (M_WAITOK | M_NOWAIT))) 399 flags |= M_NOWAIT; 400 ptr = realloc(ptr, newsz, M_STATS, flags); 401 #else /* ! _KERNEL */ 402 ptr = realloc(ptr, newsz); 403 if ((flags & M_ZERO) && ptr != NULL) { 404 if (oldsz == 0) 405 memset(ptr, '\0', newsz); 406 else if (newsz > oldsz) 407 memset(BLOB_OFFSET(ptr, oldsz), '\0', newsz - oldsz); 408 } 409 #endif /* _KERNEL */ 410 411 return (ptr); 412 } 413 414 static inline char * 415 stats_strdup(const char *s, 416 #ifdef _KERNEL 417 int flags) 418 { 419 char *copy; 420 size_t len; 421 422 if (!(flags & (M_WAITOK | M_NOWAIT))) 423 flags |= M_NOWAIT; 424 425 len = strlen(s) + 1; 426 if ((copy = malloc(len, M_STATS, flags)) != NULL) 427 bcopy(s, copy, len); 428 429 return (copy); 430 #else 431 int flags __unused) 432 { 433 return (strdup(s)); 434 #endif 435 } 436 437 static inline void 438 stats_tpl_update_hash(struct statsblob_tpl *tpl) 439 { 440 441 TPL_LIST_WLOCK_ASSERT(); 442 tpl->mb->tplhash = hash32_str(tpl->mb->tplname, 0); 443 for (int voi_id = 0; voi_id < NVOIS(tpl->sb); voi_id++) { 444 if (tpl->mb->voi_meta[voi_id].name != NULL) 445 tpl->mb->tplhash = hash32_str( 446 tpl->mb->voi_meta[voi_id].name, tpl->mb->tplhash); 447 } 448 tpl->mb->tplhash = hash32_buf(tpl->sb, tpl->sb->cursz, 449 tpl->mb->tplhash); 450 } 451 452 static inline uint64_t 453 stats_pow_u64(uint64_t base, uint64_t exp) 454 { 455 uint64_t result = 1; 456 457 while (exp) { 458 if (exp & 1) 459 result *= base; 460 exp >>= 1; 461 base *= base; 462 } 463 464 return (result); 465 } 466 467 static inline int 468 stats_vss_hist_bkt_hlpr(struct vss_hist_hlpr_info *info, uint32_t curbkt, 469 struct voistatdata_numeric *bkt_lb, struct voistatdata_numeric *bkt_ub) 470 { 471 uint64_t step = 0; 472 int error = 0; 473 474 switch (info->scheme) { 475 case BKT_LIN: 476 step = info->lin.stepinc; 477 break; 478 case BKT_EXP: 479 step = stats_pow_u64(info->exp.stepbase, 480 info->exp.stepexp + curbkt); 481 break; 482 case BKT_LINEXP: 483 { 484 uint64_t curstepexp = 1; 485 486 switch (info->voi_dtype) { 487 case VSD_DTYPE_INT_S32: 488 while ((int32_t)stats_pow_u64(info->linexp.stepbase, 489 curstepexp) <= bkt_lb->int32.s32) 490 curstepexp++; 491 break; 492 case VSD_DTYPE_INT_U32: 493 while ((uint32_t)stats_pow_u64(info->linexp.stepbase, 494 curstepexp) <= bkt_lb->int32.u32) 495 curstepexp++; 496 break; 497 case VSD_DTYPE_INT_S64: 498 while ((int64_t)stats_pow_u64(info->linexp.stepbase, 499 curstepexp) <= bkt_lb->int64.s64) 500 curstepexp++; 501 break; 502 case VSD_DTYPE_INT_U64: 503 while ((uint64_t)stats_pow_u64(info->linexp.stepbase, 504 curstepexp) <= bkt_lb->int64.u64) 505 curstepexp++; 506 break; 507 case VSD_DTYPE_INT_SLONG: 508 while ((long)stats_pow_u64(info->linexp.stepbase, 509 curstepexp) <= bkt_lb->intlong.slong) 510 curstepexp++; 511 break; 512 case VSD_DTYPE_INT_ULONG: 513 while ((unsigned long)stats_pow_u64(info->linexp.stepbase, 514 curstepexp) <= bkt_lb->intlong.ulong) 515 curstepexp++; 516 break; 517 case VSD_DTYPE_Q_S32: 518 while ((s32q_t)stats_pow_u64(info->linexp.stepbase, 519 curstepexp) <= Q_GIVAL(bkt_lb->q32.sq32)) 520 break; 521 case VSD_DTYPE_Q_U32: 522 while ((u32q_t)stats_pow_u64(info->linexp.stepbase, 523 curstepexp) <= Q_GIVAL(bkt_lb->q32.uq32)) 524 break; 525 case VSD_DTYPE_Q_S64: 526 while ((s64q_t)stats_pow_u64(info->linexp.stepbase, 527 curstepexp) <= Q_GIVAL(bkt_lb->q64.sq64)) 528 curstepexp++; 529 break; 530 case VSD_DTYPE_Q_U64: 531 while ((u64q_t)stats_pow_u64(info->linexp.stepbase, 532 curstepexp) <= Q_GIVAL(bkt_lb->q64.uq64)) 533 curstepexp++; 534 break; 535 default: 536 break; 537 } 538 539 step = stats_pow_u64(info->linexp.stepbase, curstepexp) / 540 info->linexp.linstepdiv; 541 if (step == 0) 542 step = 1; 543 break; 544 } 545 default: 546 break; 547 } 548 549 if (info->scheme == BKT_USR) { 550 *bkt_lb = info->usr.bkts[curbkt].lb; 551 *bkt_ub = info->usr.bkts[curbkt].ub; 552 } else if (step != 0) { 553 switch (info->voi_dtype) { 554 case VSD_DTYPE_INT_S32: 555 bkt_ub->int32.s32 += (int32_t)step; 556 break; 557 case VSD_DTYPE_INT_U32: 558 bkt_ub->int32.u32 += (uint32_t)step; 559 break; 560 case VSD_DTYPE_INT_S64: 561 bkt_ub->int64.s64 += (int64_t)step; 562 break; 563 case VSD_DTYPE_INT_U64: 564 bkt_ub->int64.u64 += (uint64_t)step; 565 break; 566 case VSD_DTYPE_INT_SLONG: 567 bkt_ub->intlong.slong += (long)step; 568 break; 569 case VSD_DTYPE_INT_ULONG: 570 bkt_ub->intlong.ulong += (unsigned long)step; 571 break; 572 case VSD_DTYPE_Q_S32: 573 error = Q_QADDI(&bkt_ub->q32.sq32, step); 574 break; 575 case VSD_DTYPE_Q_U32: 576 error = Q_QADDI(&bkt_ub->q32.uq32, step); 577 break; 578 case VSD_DTYPE_Q_S64: 579 error = Q_QADDI(&bkt_ub->q64.sq64, step); 580 break; 581 case VSD_DTYPE_Q_U64: 582 error = Q_QADDI(&bkt_ub->q64.uq64, step); 583 break; 584 default: 585 break; 586 } 587 } else { /* info->scheme != BKT_USR && step == 0 */ 588 return (EINVAL); 589 } 590 591 return (error); 592 } 593 594 static uint32_t 595 stats_vss_hist_nbkts_hlpr(struct vss_hist_hlpr_info *info) 596 { 597 struct voistatdata_numeric bkt_lb, bkt_ub; 598 uint32_t nbkts; 599 int done; 600 601 if (info->scheme == BKT_USR) { 602 /* XXXLAS: Setting info->{lb,ub} from macro is tricky. */ 603 info->lb = info->usr.bkts[0].lb; 604 info->ub = info->usr.bkts[info->usr.nbkts - 1].lb; 605 } 606 607 nbkts = 0; 608 done = 0; 609 bkt_ub = info->lb; 610 611 do { 612 bkt_lb = bkt_ub; 613 if (stats_vss_hist_bkt_hlpr(info, nbkts++, &bkt_lb, &bkt_ub)) 614 return (0); 615 616 if (info->scheme == BKT_USR) 617 done = (nbkts == info->usr.nbkts); 618 else { 619 switch (info->voi_dtype) { 620 case VSD_DTYPE_INT_S32: 621 done = (bkt_ub.int32.s32 > info->ub.int32.s32); 622 break; 623 case VSD_DTYPE_INT_U32: 624 done = (bkt_ub.int32.u32 > info->ub.int32.u32); 625 break; 626 case VSD_DTYPE_INT_S64: 627 done = (bkt_ub.int64.s64 > info->ub.int64.s64); 628 break; 629 case VSD_DTYPE_INT_U64: 630 done = (bkt_ub.int64.u64 > info->ub.int64.u64); 631 break; 632 case VSD_DTYPE_INT_SLONG: 633 done = (bkt_ub.intlong.slong > 634 info->ub.intlong.slong); 635 break; 636 case VSD_DTYPE_INT_ULONG: 637 done = (bkt_ub.intlong.ulong > 638 info->ub.intlong.ulong); 639 break; 640 case VSD_DTYPE_Q_S32: 641 done = Q_QGTQ(bkt_ub.q32.sq32, 642 info->ub.q32.sq32); 643 break; 644 case VSD_DTYPE_Q_U32: 645 done = Q_QGTQ(bkt_ub.q32.uq32, 646 info->ub.q32.uq32); 647 break; 648 case VSD_DTYPE_Q_S64: 649 done = Q_QGTQ(bkt_ub.q64.sq64, 650 info->ub.q64.sq64); 651 break; 652 case VSD_DTYPE_Q_U64: 653 done = Q_QGTQ(bkt_ub.q64.uq64, 654 info->ub.q64.uq64); 655 break; 656 default: 657 return (0); 658 } 659 } 660 } while (!done); 661 662 if (info->flags & VSD_HIST_LBOUND_INF) 663 nbkts++; 664 if (info->flags & VSD_HIST_UBOUND_INF) 665 nbkts++; 666 667 return (nbkts); 668 } 669 670 int 671 stats_vss_hist_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss, 672 struct vss_hist_hlpr_info *info) 673 { 674 struct voistatdata_hist *hist; 675 struct voistatdata_numeric bkt_lb, bkt_ub, *lbinfbktlb, *lbinfbktub, 676 *ubinfbktlb, *ubinfbktub; 677 uint32_t bkt, nbkts, nloop; 678 679 if (vss == NULL || info == NULL || (info->flags & 680 (VSD_HIST_LBOUND_INF|VSD_HIST_UBOUND_INF) && (info->hist_dtype == 681 VSD_DTYPE_DVHIST32 || info->hist_dtype == VSD_DTYPE_DVHIST64))) 682 return (EINVAL); 683 684 info->voi_dtype = voi_dtype; 685 686 if ((nbkts = stats_vss_hist_nbkts_hlpr(info)) == 0) 687 return (EINVAL); 688 689 switch (info->hist_dtype) { 690 case VSD_DTYPE_CRHIST32: 691 vss->vsdsz = HIST_NBKTS2VSDSZ(crhist32, nbkts); 692 break; 693 case VSD_DTYPE_DRHIST32: 694 vss->vsdsz = HIST_NBKTS2VSDSZ(drhist32, nbkts); 695 break; 696 case VSD_DTYPE_DVHIST32: 697 vss->vsdsz = HIST_NBKTS2VSDSZ(dvhist32, nbkts); 698 break; 699 case VSD_DTYPE_CRHIST64: 700 vss->vsdsz = HIST_NBKTS2VSDSZ(crhist64, nbkts); 701 break; 702 case VSD_DTYPE_DRHIST64: 703 vss->vsdsz = HIST_NBKTS2VSDSZ(drhist64, nbkts); 704 break; 705 case VSD_DTYPE_DVHIST64: 706 vss->vsdsz = HIST_NBKTS2VSDSZ(dvhist64, nbkts); 707 break; 708 default: 709 return (EINVAL); 710 } 711 712 vss->iv = stats_realloc(NULL, 0, vss->vsdsz, M_ZERO); 713 if (vss->iv == NULL) 714 return (ENOMEM); 715 716 hist = (struct voistatdata_hist *)vss->iv; 717 bkt_ub = info->lb; 718 719 for (bkt = (info->flags & VSD_HIST_LBOUND_INF), nloop = 0; 720 bkt < nbkts; 721 bkt++, nloop++) { 722 bkt_lb = bkt_ub; 723 if (stats_vss_hist_bkt_hlpr(info, nloop, &bkt_lb, &bkt_ub)) 724 return (EINVAL); 725 726 switch (info->hist_dtype) { 727 case VSD_DTYPE_CRHIST32: 728 VSD(crhist32, hist)->bkts[bkt].lb = bkt_lb; 729 break; 730 case VSD_DTYPE_DRHIST32: 731 VSD(drhist32, hist)->bkts[bkt].lb = bkt_lb; 732 VSD(drhist32, hist)->bkts[bkt].ub = bkt_ub; 733 break; 734 case VSD_DTYPE_DVHIST32: 735 VSD(dvhist32, hist)->bkts[bkt].val = bkt_lb; 736 break; 737 case VSD_DTYPE_CRHIST64: 738 VSD(crhist64, hist)->bkts[bkt].lb = bkt_lb; 739 break; 740 case VSD_DTYPE_DRHIST64: 741 VSD(drhist64, hist)->bkts[bkt].lb = bkt_lb; 742 VSD(drhist64, hist)->bkts[bkt].ub = bkt_ub; 743 break; 744 case VSD_DTYPE_DVHIST64: 745 VSD(dvhist64, hist)->bkts[bkt].val = bkt_lb; 746 break; 747 default: 748 return (EINVAL); 749 } 750 } 751 752 lbinfbktlb = lbinfbktub = ubinfbktlb = ubinfbktub = NULL; 753 754 switch (info->hist_dtype) { 755 case VSD_DTYPE_CRHIST32: 756 lbinfbktlb = &VSD(crhist32, hist)->bkts[0].lb; 757 ubinfbktlb = &VSD(crhist32, hist)->bkts[nbkts - 1].lb; 758 break; 759 case VSD_DTYPE_DRHIST32: 760 lbinfbktlb = &VSD(drhist32, hist)->bkts[0].lb; 761 lbinfbktub = &VSD(drhist32, hist)->bkts[0].ub; 762 ubinfbktlb = &VSD(drhist32, hist)->bkts[nbkts - 1].lb; 763 ubinfbktub = &VSD(drhist32, hist)->bkts[nbkts - 1].ub; 764 break; 765 case VSD_DTYPE_CRHIST64: 766 lbinfbktlb = &VSD(crhist64, hist)->bkts[0].lb; 767 ubinfbktlb = &VSD(crhist64, hist)->bkts[nbkts - 1].lb; 768 break; 769 case VSD_DTYPE_DRHIST64: 770 lbinfbktlb = &VSD(drhist64, hist)->bkts[0].lb; 771 lbinfbktub = &VSD(drhist64, hist)->bkts[0].ub; 772 ubinfbktlb = &VSD(drhist64, hist)->bkts[nbkts - 1].lb; 773 ubinfbktub = &VSD(drhist64, hist)->bkts[nbkts - 1].ub; 774 break; 775 case VSD_DTYPE_DVHIST32: 776 case VSD_DTYPE_DVHIST64: 777 break; 778 default: 779 return (EINVAL); 780 } 781 782 if ((info->flags & VSD_HIST_LBOUND_INF) && lbinfbktlb) { 783 *lbinfbktlb = numeric_limits[LIM_MIN][info->voi_dtype]; 784 /* 785 * Assignment from numeric_limit array for Q types assigns max 786 * possible integral/fractional value for underlying data type, 787 * but we must set control bits for this specific histogram per 788 * the user's choice of fractional bits, which we extract from 789 * info->lb. 790 */ 791 if (info->voi_dtype == VSD_DTYPE_Q_S32 || 792 info->voi_dtype == VSD_DTYPE_Q_U32) { 793 /* Signedness doesn't matter for setting control bits. */ 794 Q_SCVAL(lbinfbktlb->q32.sq32, 795 Q_GCVAL(info->lb.q32.sq32)); 796 } else if (info->voi_dtype == VSD_DTYPE_Q_S64 || 797 info->voi_dtype == VSD_DTYPE_Q_U64) { 798 /* Signedness doesn't matter for setting control bits. */ 799 Q_SCVAL(lbinfbktlb->q64.sq64, 800 Q_GCVAL(info->lb.q64.sq64)); 801 } 802 if (lbinfbktub) 803 *lbinfbktub = info->lb; 804 } 805 if ((info->flags & VSD_HIST_UBOUND_INF) && ubinfbktlb) { 806 *ubinfbktlb = bkt_lb; 807 if (ubinfbktub) { 808 *ubinfbktub = numeric_limits[LIM_MAX][info->voi_dtype]; 809 if (info->voi_dtype == VSD_DTYPE_Q_S32 || 810 info->voi_dtype == VSD_DTYPE_Q_U32) { 811 Q_SCVAL(ubinfbktub->q32.sq32, 812 Q_GCVAL(info->lb.q32.sq32)); 813 } else if (info->voi_dtype == VSD_DTYPE_Q_S64 || 814 info->voi_dtype == VSD_DTYPE_Q_U64) { 815 Q_SCVAL(ubinfbktub->q64.sq64, 816 Q_GCVAL(info->lb.q64.sq64)); 817 } 818 } 819 } 820 821 return (0); 822 } 823 824 int 825 stats_vss_tdgst_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss, 826 struct vss_tdgst_hlpr_info *info) 827 { 828 struct voistatdata_tdgst *tdgst; 829 struct ctdth32 *ctd32tree; 830 struct ctdth64 *ctd64tree; 831 struct voistatdata_tdgstctd32 *ctd32; 832 struct voistatdata_tdgstctd64 *ctd64; 833 834 info->voi_dtype = voi_dtype; 835 836 switch (info->tdgst_dtype) { 837 case VSD_DTYPE_TDGSTCLUST32: 838 vss->vsdsz = TDGST_NCTRS2VSDSZ(tdgstclust32, info->nctds); 839 break; 840 case VSD_DTYPE_TDGSTCLUST64: 841 vss->vsdsz = TDGST_NCTRS2VSDSZ(tdgstclust64, info->nctds); 842 break; 843 default: 844 return (EINVAL); 845 } 846 847 vss->iv = stats_realloc(NULL, 0, vss->vsdsz, M_ZERO); 848 if (vss->iv == NULL) 849 return (ENOMEM); 850 851 tdgst = (struct voistatdata_tdgst *)vss->iv; 852 853 switch (info->tdgst_dtype) { 854 case VSD_DTYPE_TDGSTCLUST32: 855 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree; 856 ARB_INIT(ctd32, ctdlnk, ctd32tree, info->nctds) { 857 Q_INI(&ctd32->mu, 0, 0, info->prec); 858 } 859 break; 860 case VSD_DTYPE_TDGSTCLUST64: 861 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree; 862 ARB_INIT(ctd64, ctdlnk, ctd64tree, info->nctds) { 863 Q_INI(&ctd64->mu, 0, 0, info->prec); 864 } 865 break; 866 default: 867 return (EINVAL); 868 } 869 870 return (0); 871 } 872 873 int 874 stats_vss_numeric_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss, 875 struct vss_numeric_hlpr_info *info) 876 { 877 struct voistatdata_numeric iv; 878 879 switch (vss->stype) { 880 case VS_STYPE_SUM: 881 iv = stats_ctor_vsd_numeric(0); 882 break; 883 case VS_STYPE_MIN: 884 iv = numeric_limits[LIM_MAX][voi_dtype]; 885 break; 886 case VS_STYPE_MAX: 887 iv = numeric_limits[LIM_MIN][voi_dtype]; 888 break; 889 default: 890 return (EINVAL); 891 } 892 893 vss->iv = stats_realloc(NULL, 0, vsd_dtype2size[voi_dtype], 0); 894 if (vss->iv == NULL) 895 return (ENOMEM); 896 897 vss->vs_dtype = voi_dtype; 898 vss->vsdsz = vsd_dtype2size[voi_dtype]; 899 switch (voi_dtype) { 900 case VSD_DTYPE_INT_S32: 901 *((int32_t *)vss->iv) = iv.int32.s32; 902 break; 903 case VSD_DTYPE_INT_U32: 904 *((uint32_t *)vss->iv) = iv.int32.u32; 905 break; 906 case VSD_DTYPE_INT_S64: 907 *((int64_t *)vss->iv) = iv.int64.s64; 908 break; 909 case VSD_DTYPE_INT_U64: 910 *((uint64_t *)vss->iv) = iv.int64.u64; 911 break; 912 case VSD_DTYPE_INT_SLONG: 913 *((long *)vss->iv) = iv.intlong.slong; 914 break; 915 case VSD_DTYPE_INT_ULONG: 916 *((unsigned long *)vss->iv) = iv.intlong.ulong; 917 break; 918 case VSD_DTYPE_Q_S32: 919 *((s32q_t *)vss->iv) = Q_SCVAL(iv.q32.sq32, 920 Q_CTRLINI(info->prec)); 921 break; 922 case VSD_DTYPE_Q_U32: 923 *((u32q_t *)vss->iv) = Q_SCVAL(iv.q32.uq32, 924 Q_CTRLINI(info->prec)); 925 break; 926 case VSD_DTYPE_Q_S64: 927 *((s64q_t *)vss->iv) = Q_SCVAL(iv.q64.sq64, 928 Q_CTRLINI(info->prec)); 929 break; 930 case VSD_DTYPE_Q_U64: 931 *((u64q_t *)vss->iv) = Q_SCVAL(iv.q64.uq64, 932 Q_CTRLINI(info->prec)); 933 break; 934 default: 935 break; 936 } 937 938 return (0); 939 } 940 941 int 942 stats_vss_hlpr_init(enum vsd_dtype voi_dtype, uint32_t nvss, 943 struct voistatspec *vss) 944 { 945 int i, ret; 946 947 for (i = nvss - 1; i >= 0; i--) { 948 if (vss[i].hlpr && (ret = vss[i].hlpr(voi_dtype, &vss[i], 949 vss[i].hlprinfo)) != 0) 950 return (ret); 951 } 952 953 return (0); 954 } 955 956 void 957 stats_vss_hlpr_cleanup(uint32_t nvss, struct voistatspec *vss) 958 { 959 int i; 960 961 for (i = nvss - 1; i >= 0; i--) { 962 if (vss[i].hlpr) { 963 stats_free((void *)vss[i].iv); 964 vss[i].iv = NULL; 965 } 966 } 967 } 968 969 int 970 stats_tpl_fetch(int tpl_id, struct statsblob_tpl **tpl) 971 { 972 int error; 973 974 error = 0; 975 976 TPL_LIST_WLOCK(); 977 if (tpl_id < 0 || tpl_id >= (int)ntpl) { 978 error = ENOENT; 979 } else { 980 *tpl = tpllist[tpl_id]; 981 /* XXXLAS: Acquire refcount on tpl. */ 982 } 983 TPL_LIST_WUNLOCK(); 984 985 return (error); 986 } 987 988 int 989 stats_tpl_fetch_allocid(const char *name, uint32_t hash) 990 { 991 int i, tpl_id; 992 993 tpl_id = -ESRCH; 994 995 TPL_LIST_RLOCK(); 996 for (i = ntpl - 1; i >= 0; i--) { 997 if (name != NULL) { 998 if (strlen(name) == strlen(tpllist[i]->mb->tplname) && 999 strncmp(name, tpllist[i]->mb->tplname, 1000 TPL_MAX_NAME_LEN) == 0 && (!hash || hash == 1001 tpllist[i]->mb->tplhash)) { 1002 tpl_id = i; 1003 break; 1004 } 1005 } else if (hash == tpllist[i]->mb->tplhash) { 1006 tpl_id = i; 1007 break; 1008 } 1009 } 1010 TPL_LIST_RUNLOCK(); 1011 1012 return (tpl_id); 1013 } 1014 1015 int 1016 stats_tpl_id2name(uint32_t tpl_id, char *buf, size_t len) 1017 { 1018 int error; 1019 1020 error = 0; 1021 1022 TPL_LIST_RLOCK(); 1023 if (tpl_id < ntpl) { 1024 if (buf != NULL && len > strlen(tpllist[tpl_id]->mb->tplname)) 1025 strlcpy(buf, tpllist[tpl_id]->mb->tplname, len); 1026 else 1027 error = EOVERFLOW; 1028 } else 1029 error = ENOENT; 1030 TPL_LIST_RUNLOCK(); 1031 1032 return (error); 1033 } 1034 1035 int 1036 stats_tpl_sample_rollthedice(struct stats_tpl_sample_rate *rates, int nrates, 1037 void *seed_bytes, size_t seed_len) 1038 { 1039 uint32_t cum_pct, rnd_pct; 1040 int i; 1041 1042 cum_pct = 0; 1043 1044 /* 1045 * Choose a pseudorandom or seeded number in range [0,100] and use 1046 * it to make a sampling decision and template selection where required. 1047 * If no seed is supplied, a PRNG is used to generate a pseudorandom 1048 * number so that every selection is independent. If a seed is supplied, 1049 * the caller desires random selection across different seeds, but 1050 * deterministic selection given the same seed. This is achieved by 1051 * hashing the seed and using the hash as the random number source. 1052 * 1053 * XXXLAS: Characterise hash function output distribution. 1054 */ 1055 if (seed_bytes == NULL) 1056 rnd_pct = random() / (INT32_MAX / 100); 1057 else 1058 rnd_pct = hash32_buf(seed_bytes, seed_len, 0) / 1059 (UINT32_MAX / 100U); 1060 1061 /* 1062 * We map the randomly selected percentage on to the interval [0,100] 1063 * consisting of the cumulatively summed template sampling percentages. 1064 * The difference between the cumulative sum of all template sampling 1065 * percentages and 100 is treated as a NULL assignment i.e. no stats 1066 * template will be assigned, and -1 returned instead. 1067 */ 1068 for (i = 0; i < nrates; i++) { 1069 cum_pct += rates[i].tpl_sample_pct; 1070 1071 KASSERT(cum_pct <= 100, ("%s cum_pct %u > 100", __func__, 1072 cum_pct)); 1073 if (rnd_pct > cum_pct || rates[i].tpl_sample_pct == 0) 1074 continue; 1075 1076 return (rates[i].tpl_slot_id); 1077 } 1078 1079 return (-1); 1080 } 1081 1082 int 1083 stats_v1_blob_clone(struct statsblobv1 **dst, size_t dstmaxsz, 1084 struct statsblobv1 *src, uint32_t flags) 1085 { 1086 int error; 1087 1088 error = 0; 1089 1090 if (src == NULL || dst == NULL || 1091 src->cursz < sizeof(struct statsblob) || 1092 ((flags & SB_CLONE_ALLOCDST) && 1093 (flags & (SB_CLONE_USRDSTNOFAULT | SB_CLONE_USRDST)))) { 1094 error = EINVAL; 1095 } else if (flags & SB_CLONE_ALLOCDST) { 1096 *dst = stats_realloc(NULL, 0, src->cursz, 0); 1097 if (*dst) 1098 (*dst)->maxsz = dstmaxsz = src->cursz; 1099 else 1100 error = ENOMEM; 1101 } else if (*dst == NULL || dstmaxsz < sizeof(struct statsblob)) { 1102 error = EINVAL; 1103 } 1104 1105 if (!error) { 1106 size_t postcurszlen; 1107 1108 /* 1109 * Clone src into dst except for the maxsz field. If dst is too 1110 * small to hold all of src, only copy src's header and return 1111 * EOVERFLOW. 1112 */ 1113 #ifdef _KERNEL 1114 if (flags & SB_CLONE_USRDSTNOFAULT) 1115 copyout_nofault(src, *dst, 1116 offsetof(struct statsblob, maxsz)); 1117 else if (flags & SB_CLONE_USRDST) 1118 copyout(src, *dst, offsetof(struct statsblob, maxsz)); 1119 else 1120 #endif 1121 memcpy(*dst, src, offsetof(struct statsblob, maxsz)); 1122 1123 if (dstmaxsz >= src->cursz) { 1124 postcurszlen = src->cursz - 1125 offsetof(struct statsblob, cursz); 1126 } else { 1127 error = EOVERFLOW; 1128 postcurszlen = sizeof(struct statsblob) - 1129 offsetof(struct statsblob, cursz); 1130 } 1131 #ifdef _KERNEL 1132 if (flags & SB_CLONE_USRDSTNOFAULT) 1133 copyout_nofault(&(src->cursz), &((*dst)->cursz), 1134 postcurszlen); 1135 else if (flags & SB_CLONE_USRDST) 1136 copyout(&(src->cursz), &((*dst)->cursz), postcurszlen); 1137 else 1138 #endif 1139 memcpy(&((*dst)->cursz), &(src->cursz), postcurszlen); 1140 } 1141 1142 return (error); 1143 } 1144 1145 int 1146 stats_v1_tpl_alloc(const char *name, uint32_t flags __unused) 1147 { 1148 struct statsblobv1_tpl *tpl, **newtpllist; 1149 struct statsblobv1 *tpl_sb; 1150 struct metablob *tpl_mb; 1151 int tpl_id; 1152 1153 if (name != NULL && strlen(name) > TPL_MAX_NAME_LEN) 1154 return (-EINVAL); 1155 1156 if (name != NULL && stats_tpl_fetch_allocid(name, 0) >= 0) 1157 return (-EEXIST); 1158 1159 tpl = stats_realloc(NULL, 0, sizeof(struct statsblobv1_tpl), M_ZERO); 1160 tpl_mb = stats_realloc(NULL, 0, sizeof(struct metablob), M_ZERO); 1161 tpl_sb = stats_realloc(NULL, 0, sizeof(struct statsblobv1), M_ZERO); 1162 1163 if (tpl_mb != NULL && name != NULL) 1164 tpl_mb->tplname = stats_strdup(name, 0); 1165 1166 if (tpl == NULL || tpl_sb == NULL || tpl_mb == NULL || 1167 tpl_mb->tplname == NULL) { 1168 stats_free(tpl); 1169 stats_free(tpl_sb); 1170 if (tpl_mb != NULL) { 1171 stats_free(tpl_mb->tplname); 1172 stats_free(tpl_mb); 1173 } 1174 return (-ENOMEM); 1175 } 1176 1177 tpl->mb = tpl_mb; 1178 tpl->sb = tpl_sb; 1179 1180 tpl_sb->abi = STATS_ABI_V1; 1181 tpl_sb->endian = 1182 #if BYTE_ORDER == LITTLE_ENDIAN 1183 SB_LE; 1184 #elif BYTE_ORDER == BIG_ENDIAN 1185 SB_BE; 1186 #else 1187 SB_UE; 1188 #endif 1189 tpl_sb->cursz = tpl_sb->maxsz = sizeof(struct statsblobv1); 1190 tpl_sb->stats_off = tpl_sb->statsdata_off = sizeof(struct statsblobv1); 1191 1192 TPL_LIST_WLOCK(); 1193 newtpllist = stats_realloc(tpllist, ntpl * sizeof(void *), 1194 (ntpl + 1) * sizeof(void *), 0); 1195 if (newtpllist != NULL) { 1196 tpl_id = ntpl++; 1197 tpllist = (struct statsblob_tpl **)newtpllist; 1198 tpllist[tpl_id] = (struct statsblob_tpl *)tpl; 1199 stats_tpl_update_hash(tpllist[tpl_id]); 1200 } else { 1201 stats_free(tpl); 1202 stats_free(tpl_sb); 1203 if (tpl_mb != NULL) { 1204 stats_free(tpl_mb->tplname); 1205 stats_free(tpl_mb); 1206 } 1207 tpl_id = -ENOMEM; 1208 } 1209 TPL_LIST_WUNLOCK(); 1210 1211 return (tpl_id); 1212 } 1213 1214 int 1215 stats_v1_tpl_add_voistats(uint32_t tpl_id, int32_t voi_id, const char *voi_name, 1216 enum vsd_dtype voi_dtype, uint32_t nvss, struct voistatspec *vss, 1217 uint32_t flags) 1218 { 1219 struct voi *voi; 1220 struct voistat *tmpstat; 1221 struct statsblobv1 *tpl_sb; 1222 struct metablob *tpl_mb; 1223 int error, i, newstatdataidx, newvoibytes, newvoistatbytes, 1224 newvoistatdatabytes, newvoistatmaxid; 1225 uint32_t nbytes; 1226 1227 if (voi_id < 0 || voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES || 1228 nvss == 0 || vss == NULL) 1229 return (EINVAL); 1230 1231 error = nbytes = newvoibytes = newvoistatbytes = 1232 newvoistatdatabytes = 0; 1233 newvoistatmaxid = -1; 1234 1235 /* Calculate the number of bytes required for the new voistats. */ 1236 for (i = nvss - 1; i >= 0; i--) { 1237 if (vss[i].stype == 0 || vss[i].stype >= VS_NUM_STYPES || 1238 vss[i].vs_dtype == 0 || vss[i].vs_dtype >= VSD_NUM_DTYPES || 1239 vss[i].iv == NULL || vss[i].vsdsz == 0) 1240 return (EINVAL); 1241 if ((int)vss[i].stype > newvoistatmaxid) 1242 newvoistatmaxid = vss[i].stype; 1243 newvoistatdatabytes += vss[i].vsdsz; 1244 } 1245 1246 if (flags & SB_VOI_RELUPDATE) { 1247 /* XXXLAS: VOI state bytes may need to vary based on stat types. */ 1248 newvoistatdatabytes += sizeof(struct voistatdata_voistate); 1249 } 1250 nbytes += newvoistatdatabytes; 1251 1252 TPL_LIST_WLOCK(); 1253 if (tpl_id < ntpl) { 1254 tpl_sb = (struct statsblobv1 *)tpllist[tpl_id]->sb; 1255 tpl_mb = tpllist[tpl_id]->mb; 1256 1257 if (voi_id >= NVOIS(tpl_sb) || tpl_sb->vois[voi_id].id == -1) { 1258 /* Adding a new VOI and associated stats. */ 1259 if (voi_id >= NVOIS(tpl_sb)) { 1260 /* We need to grow the tpl_sb->vois array. */ 1261 newvoibytes = (voi_id - (NVOIS(tpl_sb) - 1)) * 1262 sizeof(struct voi); 1263 nbytes += newvoibytes; 1264 } 1265 newvoistatbytes = 1266 (newvoistatmaxid + 1) * sizeof(struct voistat); 1267 } else { 1268 /* Adding stats to an existing VOI. */ 1269 if (newvoistatmaxid > 1270 tpl_sb->vois[voi_id].voistatmaxid) { 1271 newvoistatbytes = (newvoistatmaxid - 1272 tpl_sb->vois[voi_id].voistatmaxid) * 1273 sizeof(struct voistat); 1274 } 1275 /* XXXLAS: KPI does not yet support expanding VOIs. */ 1276 error = EOPNOTSUPP; 1277 } 1278 nbytes += newvoistatbytes; 1279 1280 if (!error && newvoibytes > 0) { 1281 struct voi_meta *voi_meta = tpl_mb->voi_meta; 1282 1283 voi_meta = stats_realloc(voi_meta, voi_meta == NULL ? 1284 0 : NVOIS(tpl_sb) * sizeof(struct voi_meta), 1285 (1 + voi_id) * sizeof(struct voi_meta), 1286 M_ZERO); 1287 1288 if (voi_meta == NULL) 1289 error = ENOMEM; 1290 else 1291 tpl_mb->voi_meta = voi_meta; 1292 } 1293 1294 if (!error) { 1295 /* NB: Resizing can change where tpl_sb points. */ 1296 error = stats_v1_blob_expand(&tpl_sb, newvoibytes, 1297 newvoistatbytes, newvoistatdatabytes); 1298 } 1299 1300 if (!error) { 1301 tpl_mb->voi_meta[voi_id].name = stats_strdup(voi_name, 1302 0); 1303 if (tpl_mb->voi_meta[voi_id].name == NULL) 1304 error = ENOMEM; 1305 } 1306 1307 if (!error) { 1308 /* Update the template list with the resized pointer. */ 1309 tpllist[tpl_id]->sb = (struct statsblob *)tpl_sb; 1310 1311 /* Update the template. */ 1312 voi = &tpl_sb->vois[voi_id]; 1313 1314 if (voi->id < 0) { 1315 /* VOI is new and needs to be initialised. */ 1316 voi->id = voi_id; 1317 voi->dtype = voi_dtype; 1318 voi->stats_off = tpl_sb->stats_off; 1319 if (flags & SB_VOI_RELUPDATE) 1320 voi->flags |= VOI_REQSTATE; 1321 } else { 1322 /* 1323 * XXXLAS: When this else block is written, the 1324 * "KPI does not yet support expanding VOIs" 1325 * error earlier in this function can be 1326 * removed. What is required here is to shuffle 1327 * the voistat array such that the new stats for 1328 * the voi are contiguous, which will displace 1329 * stats for other vois that reside after the 1330 * voi being updated. The other vois then need 1331 * to have their stats_off adjusted post 1332 * shuffle. 1333 */ 1334 } 1335 1336 voi->voistatmaxid = newvoistatmaxid; 1337 newstatdataidx = 0; 1338 1339 if (voi->flags & VOI_REQSTATE) { 1340 /* Initialise the voistate stat in slot 0. */ 1341 tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off); 1342 tmpstat->stype = VS_STYPE_VOISTATE; 1343 tmpstat->flags = 0; 1344 tmpstat->dtype = VSD_DTYPE_VOISTATE; 1345 newstatdataidx = tmpstat->dsz = 1346 sizeof(struct voistatdata_numeric); 1347 tmpstat->data_off = tpl_sb->statsdata_off; 1348 } 1349 1350 for (i = 0; (uint32_t)i < nvss; i++) { 1351 tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off + 1352 (vss[i].stype * sizeof(struct voistat))); 1353 KASSERT(tmpstat->stype < 0, ("voistat %p " 1354 "already initialised", tmpstat)); 1355 tmpstat->stype = vss[i].stype; 1356 tmpstat->flags = vss[i].flags; 1357 tmpstat->dtype = vss[i].vs_dtype; 1358 tmpstat->dsz = vss[i].vsdsz; 1359 tmpstat->data_off = tpl_sb->statsdata_off + 1360 newstatdataidx; 1361 memcpy(BLOB_OFFSET(tpl_sb, tmpstat->data_off), 1362 vss[i].iv, vss[i].vsdsz); 1363 newstatdataidx += vss[i].vsdsz; 1364 } 1365 1366 /* Update the template version hash. */ 1367 stats_tpl_update_hash(tpllist[tpl_id]); 1368 /* XXXLAS: Confirm tpl name/hash pair remains unique. */ 1369 } 1370 } else 1371 error = EINVAL; 1372 TPL_LIST_WUNLOCK(); 1373 1374 return (error); 1375 } 1376 1377 struct statsblobv1 * 1378 stats_v1_blob_alloc(uint32_t tpl_id, uint32_t flags __unused) 1379 { 1380 struct statsblobv1 *sb; 1381 int error; 1382 1383 sb = NULL; 1384 1385 TPL_LIST_RLOCK(); 1386 if (tpl_id < ntpl) { 1387 sb = stats_realloc(NULL, 0, tpllist[tpl_id]->sb->maxsz, 0); 1388 if (sb != NULL) { 1389 sb->maxsz = tpllist[tpl_id]->sb->maxsz; 1390 error = stats_v1_blob_init_locked(sb, tpl_id, 0); 1391 } else 1392 error = ENOMEM; 1393 1394 if (error) { 1395 stats_free(sb); 1396 sb = NULL; 1397 } 1398 } 1399 TPL_LIST_RUNLOCK(); 1400 1401 return (sb); 1402 } 1403 1404 void 1405 stats_v1_blob_destroy(struct statsblobv1 *sb) 1406 { 1407 1408 stats_free(sb); 1409 } 1410 1411 int 1412 stats_v1_voistat_fetch_dptr(struct statsblobv1 *sb, int32_t voi_id, 1413 enum voi_stype stype, enum vsd_dtype *retdtype, struct voistatdata **retvsd, 1414 size_t *retvsdsz) 1415 { 1416 struct voi *v; 1417 struct voistat *vs; 1418 1419 if (retvsd == NULL || sb == NULL || sb->abi != STATS_ABI_V1 || 1420 voi_id >= NVOIS(sb)) 1421 return (EINVAL); 1422 1423 v = &sb->vois[voi_id]; 1424 if ((__typeof(v->voistatmaxid))stype > v->voistatmaxid) 1425 return (EINVAL); 1426 1427 vs = BLOB_OFFSET(sb, v->stats_off + (stype * sizeof(struct voistat))); 1428 *retvsd = BLOB_OFFSET(sb, vs->data_off); 1429 if (retdtype != NULL) 1430 *retdtype = vs->dtype; 1431 if (retvsdsz != NULL) 1432 *retvsdsz = vs->dsz; 1433 1434 return (0); 1435 } 1436 1437 int 1438 stats_v1_blob_init(struct statsblobv1 *sb, uint32_t tpl_id, uint32_t flags) 1439 { 1440 int error; 1441 1442 error = 0; 1443 1444 TPL_LIST_RLOCK(); 1445 if (sb == NULL || tpl_id >= ntpl) { 1446 error = EINVAL; 1447 } else { 1448 error = stats_v1_blob_init_locked(sb, tpl_id, flags); 1449 } 1450 TPL_LIST_RUNLOCK(); 1451 1452 return (error); 1453 } 1454 1455 static inline int 1456 stats_v1_blob_init_locked(struct statsblobv1 *sb, uint32_t tpl_id, 1457 uint32_t flags __unused) 1458 { 1459 int error; 1460 1461 TPL_LIST_RLOCK_ASSERT(); 1462 error = (sb->maxsz >= tpllist[tpl_id]->sb->cursz) ? 0 : EOVERFLOW; 1463 KASSERT(!error, 1464 ("sb %d instead of %d bytes", sb->maxsz, tpllist[tpl_id]->sb->cursz)); 1465 1466 if (!error) { 1467 memcpy(sb, tpllist[tpl_id]->sb, tpllist[tpl_id]->sb->cursz); 1468 sb->created = sb->lastrst = stats_sbinuptime(); 1469 sb->tplhash = tpllist[tpl_id]->mb->tplhash; 1470 } 1471 1472 return (error); 1473 } 1474 1475 static int 1476 stats_v1_blob_expand(struct statsblobv1 **sbpp, int newvoibytes, 1477 int newvoistatbytes, int newvoistatdatabytes) 1478 { 1479 struct statsblobv1 *sb; 1480 struct voi *tmpvoi; 1481 struct voistat *tmpvoistat, *voistat_array; 1482 int error, i, idxnewvois, idxnewvoistats, nbytes, nvoistats; 1483 1484 KASSERT(newvoibytes % sizeof(struct voi) == 0, 1485 ("Bad newvoibytes %d", newvoibytes)); 1486 KASSERT(newvoistatbytes % sizeof(struct voistat) == 0, 1487 ("Bad newvoistatbytes %d", newvoistatbytes)); 1488 1489 error = ((newvoibytes % sizeof(struct voi) == 0) && 1490 (newvoistatbytes % sizeof(struct voistat) == 0)) ? 0 : EINVAL; 1491 sb = *sbpp; 1492 nbytes = newvoibytes + newvoistatbytes + newvoistatdatabytes; 1493 1494 /* 1495 * XXXLAS: Required until we gain support for flags which alter the 1496 * units of size/offset fields in key structs. 1497 */ 1498 if (!error && ((((int)sb->cursz) + nbytes) > SB_V1_MAXSZ)) 1499 error = EFBIG; 1500 1501 if (!error && (sb->cursz + nbytes > sb->maxsz)) { 1502 /* Need to expand our blob. */ 1503 sb = stats_realloc(sb, sb->maxsz, sb->cursz + nbytes, M_ZERO); 1504 if (sb != NULL) { 1505 sb->maxsz = sb->cursz + nbytes; 1506 *sbpp = sb; 1507 } else 1508 error = ENOMEM; 1509 } 1510 1511 if (!error) { 1512 /* 1513 * Shuffle memory within the expanded blob working from the end 1514 * backwards, leaving gaps for the new voistat and voistatdata 1515 * structs at the beginning of their respective blob regions, 1516 * and for the new voi structs at the end of their blob region. 1517 */ 1518 memmove(BLOB_OFFSET(sb, sb->statsdata_off + nbytes), 1519 BLOB_OFFSET(sb, sb->statsdata_off), 1520 sb->cursz - sb->statsdata_off); 1521 memmove(BLOB_OFFSET(sb, sb->stats_off + newvoibytes + 1522 newvoistatbytes), BLOB_OFFSET(sb, sb->stats_off), 1523 sb->statsdata_off - sb->stats_off); 1524 1525 /* First index of new voi/voistat structs to be initialised. */ 1526 idxnewvois = NVOIS(sb); 1527 idxnewvoistats = (newvoistatbytes / sizeof(struct voistat)) - 1; 1528 1529 /* Update housekeeping variables and offsets. */ 1530 sb->cursz += nbytes; 1531 sb->stats_off += newvoibytes; 1532 sb->statsdata_off += newvoibytes + newvoistatbytes; 1533 1534 /* XXXLAS: Zeroing not strictly needed but aids debugging. */ 1535 memset(&sb->vois[idxnewvois], '\0', newvoibytes); 1536 memset(BLOB_OFFSET(sb, sb->stats_off), '\0', 1537 newvoistatbytes); 1538 memset(BLOB_OFFSET(sb, sb->statsdata_off), '\0', 1539 newvoistatdatabytes); 1540 1541 /* Initialise new voi array members and update offsets. */ 1542 for (i = 0; i < NVOIS(sb); i++) { 1543 tmpvoi = &sb->vois[i]; 1544 if (i >= idxnewvois) { 1545 tmpvoi->id = tmpvoi->voistatmaxid = -1; 1546 } else if (tmpvoi->id > -1) { 1547 tmpvoi->stats_off += newvoibytes + 1548 newvoistatbytes; 1549 } 1550 } 1551 1552 /* Initialise new voistat array members and update offsets. */ 1553 nvoistats = (sb->statsdata_off - sb->stats_off) / 1554 sizeof(struct voistat); 1555 voistat_array = BLOB_OFFSET(sb, sb->stats_off); 1556 for (i = 0; i < nvoistats; i++) { 1557 tmpvoistat = &voistat_array[i]; 1558 if (i <= idxnewvoistats) { 1559 tmpvoistat->stype = -1; 1560 } else if (tmpvoistat->stype > -1) { 1561 tmpvoistat->data_off += nbytes; 1562 } 1563 } 1564 } 1565 1566 return (error); 1567 } 1568 1569 static void 1570 stats_v1_blob_finalise(struct statsblobv1 *sb __unused) 1571 { 1572 1573 /* XXXLAS: Fill this in. */ 1574 } 1575 1576 static void 1577 stats_v1_blob_iter(struct statsblobv1 *sb, stats_v1_blob_itercb_t icb, 1578 void *usrctx, uint32_t flags) 1579 { 1580 struct voi *v; 1581 struct voistat *vs; 1582 struct sb_iter_ctx ctx; 1583 int i, j, firstvoi; 1584 1585 ctx.usrctx = usrctx; 1586 ctx.flags = SB_IT_FIRST_CB; 1587 firstvoi = 1; 1588 1589 for (i = 0; i < NVOIS(sb); i++) { 1590 v = &sb->vois[i]; 1591 ctx.vslot = i; 1592 ctx.vsslot = -1; 1593 ctx.flags |= SB_IT_FIRST_VOISTAT; 1594 1595 if (firstvoi) 1596 ctx.flags |= SB_IT_FIRST_VOI; 1597 else if (i == (NVOIS(sb) - 1)) 1598 ctx.flags |= SB_IT_LAST_VOI | SB_IT_LAST_CB; 1599 1600 if (v->id < 0 && (flags & SB_IT_NULLVOI)) { 1601 if (icb(sb, v, NULL, &ctx)) 1602 return; 1603 firstvoi = 0; 1604 ctx.flags &= ~SB_IT_FIRST_CB; 1605 } 1606 1607 /* If NULL voi, v->voistatmaxid == -1 */ 1608 for (j = 0; j <= v->voistatmaxid; j++) { 1609 vs = &((struct voistat *)BLOB_OFFSET(sb, 1610 v->stats_off))[j]; 1611 if (vs->stype < 0 && 1612 !(flags & SB_IT_NULLVOISTAT)) 1613 continue; 1614 1615 if (j == v->voistatmaxid) { 1616 ctx.flags |= SB_IT_LAST_VOISTAT; 1617 if (i == (NVOIS(sb) - 1)) 1618 ctx.flags |= 1619 SB_IT_LAST_CB; 1620 } else 1621 ctx.flags &= ~SB_IT_LAST_CB; 1622 1623 ctx.vsslot = j; 1624 if (icb(sb, v, vs, &ctx)) 1625 return; 1626 1627 ctx.flags &= ~(SB_IT_FIRST_CB | SB_IT_FIRST_VOISTAT | 1628 SB_IT_LAST_VOISTAT); 1629 } 1630 ctx.flags &= ~(SB_IT_FIRST_VOI | SB_IT_LAST_VOI); 1631 } 1632 } 1633 1634 static inline void 1635 stats_voistatdata_tdgst_tostr(enum vsd_dtype voi_dtype __unused, 1636 const struct voistatdata_tdgst *tdgst, enum vsd_dtype tdgst_dtype, 1637 size_t tdgst_dsz __unused, enum sb_str_fmt fmt, struct sbuf *buf, int objdump) 1638 { 1639 const struct ctdth32 *ctd32tree; 1640 const struct ctdth64 *ctd64tree; 1641 const struct voistatdata_tdgstctd32 *ctd32; 1642 const struct voistatdata_tdgstctd64 *ctd64; 1643 const char *fmtstr; 1644 uint64_t smplcnt, compcnt; 1645 int is32bit, qmaxstrlen; 1646 uint16_t maxctds, curctds; 1647 1648 switch (tdgst_dtype) { 1649 case VSD_DTYPE_TDGSTCLUST32: 1650 smplcnt = CONSTVSD(tdgstclust32, tdgst)->smplcnt; 1651 compcnt = CONSTVSD(tdgstclust32, tdgst)->compcnt; 1652 maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree); 1653 curctds = ARB_CURNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree); 1654 ctd32tree = &CONSTVSD(tdgstclust32, tdgst)->ctdtree; 1655 ctd32 = (objdump ? ARB_CNODE(ctd32tree, 0) : 1656 ARB_CMIN(ctdth32, ctd32tree)); 1657 qmaxstrlen = (ctd32 == NULL) ? 1 : Q_MAXSTRLEN(ctd32->mu, 10); 1658 is32bit = 1; 1659 ctd64tree = NULL; 1660 ctd64 = NULL; 1661 break; 1662 case VSD_DTYPE_TDGSTCLUST64: 1663 smplcnt = CONSTVSD(tdgstclust64, tdgst)->smplcnt; 1664 compcnt = CONSTVSD(tdgstclust64, tdgst)->compcnt; 1665 maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree); 1666 curctds = ARB_CURNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree); 1667 ctd64tree = &CONSTVSD(tdgstclust64, tdgst)->ctdtree; 1668 ctd64 = (objdump ? ARB_CNODE(ctd64tree, 0) : 1669 ARB_CMIN(ctdth64, ctd64tree)); 1670 qmaxstrlen = (ctd64 == NULL) ? 1 : Q_MAXSTRLEN(ctd64->mu, 10); 1671 is32bit = 0; 1672 ctd32tree = NULL; 1673 ctd32 = NULL; 1674 break; 1675 default: 1676 return; 1677 } 1678 1679 switch (fmt) { 1680 case SB_STRFMT_FREEFORM: 1681 fmtstr = "smplcnt=%ju, compcnt=%ju, maxctds=%hu, nctds=%hu"; 1682 break; 1683 case SB_STRFMT_JSON: 1684 default: 1685 fmtstr = 1686 "\"smplcnt\":%ju,\"compcnt\":%ju,\"maxctds\":%hu," 1687 "\"nctds\":%hu,\"ctds\":["; 1688 break; 1689 } 1690 sbuf_printf(buf, fmtstr, (uintmax_t)smplcnt, (uintmax_t)compcnt, 1691 maxctds, curctds); 1692 1693 while ((is32bit ? NULL != ctd32 : NULL != ctd64)) { 1694 char qstr[qmaxstrlen]; 1695 1696 switch (fmt) { 1697 case SB_STRFMT_FREEFORM: 1698 fmtstr = "\n\t\t\t\t"; 1699 break; 1700 case SB_STRFMT_JSON: 1701 default: 1702 fmtstr = "{"; 1703 break; 1704 } 1705 sbuf_cat(buf, fmtstr); 1706 1707 if (objdump) { 1708 switch (fmt) { 1709 case SB_STRFMT_FREEFORM: 1710 fmtstr = "ctd[%hu]."; 1711 break; 1712 case SB_STRFMT_JSON: 1713 default: 1714 fmtstr = "\"ctd\":%hu,"; 1715 break; 1716 } 1717 sbuf_printf(buf, fmtstr, is32bit ? 1718 ARB_SELFIDX(ctd32tree, ctd32) : 1719 ARB_SELFIDX(ctd64tree, ctd64)); 1720 } 1721 1722 switch (fmt) { 1723 case SB_STRFMT_FREEFORM: 1724 fmtstr = "{mu="; 1725 break; 1726 case SB_STRFMT_JSON: 1727 default: 1728 fmtstr = "\"mu\":"; 1729 break; 1730 } 1731 sbuf_cat(buf, fmtstr); 1732 Q_TOSTR((is32bit ? ctd32->mu : ctd64->mu), -1, 10, qstr, 1733 sizeof(qstr)); 1734 sbuf_cat(buf, qstr); 1735 1736 1737 switch (fmt) { 1738 case SB_STRFMT_FREEFORM: 1739 fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}"; 1740 break; 1741 case SB_STRFMT_JSON: 1742 default: 1743 fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}"; 1744 break; 1745 } 1746 sbuf_printf(buf, fmtstr, 1747 is32bit ? ctd32->cnt : (uintmax_t)ctd64->cnt); 1748 1749 if (is32bit) 1750 ctd32 = (objdump ? ARB_CNODE(ctd32tree, 1751 ARB_SELFIDX(ctd32tree, ctd32) + 1) : 1752 ARB_CNEXT(ctdth32, ctd32tree, ctd32)); 1753 else 1754 ctd64 = (objdump ? ARB_CNODE(ctd64tree, 1755 ARB_SELFIDX(ctd64tree, ctd64) + 1) : 1756 ARB_CNEXT(ctdth64, ctd64tree, ctd64)); 1757 1758 if (fmt == SB_STRFMT_JSON && 1759 (is32bit ? NULL != ctd32 : NULL != ctd64)) 1760 sbuf_putc(buf, ','); 1761 } 1762 if (fmt == SB_STRFMT_JSON) 1763 sbuf_cat(buf, "]"); 1764 } 1765 1766 static inline void 1767 stats_voistatdata_hist_tostr(enum vsd_dtype voi_dtype, 1768 const struct voistatdata_hist *hist, enum vsd_dtype hist_dtype, 1769 size_t hist_dsz, enum sb_str_fmt fmt, struct sbuf *buf, int objdump) 1770 { 1771 const struct voistatdata_numeric *bkt_lb, *bkt_ub; 1772 const char *fmtstr; 1773 int is32bit; 1774 uint16_t i, nbkts; 1775 1776 switch (hist_dtype) { 1777 case VSD_DTYPE_CRHIST32: 1778 nbkts = HIST_VSDSZ2NBKTS(crhist32, hist_dsz); 1779 is32bit = 1; 1780 break; 1781 case VSD_DTYPE_DRHIST32: 1782 nbkts = HIST_VSDSZ2NBKTS(drhist32, hist_dsz); 1783 is32bit = 1; 1784 break; 1785 case VSD_DTYPE_DVHIST32: 1786 nbkts = HIST_VSDSZ2NBKTS(dvhist32, hist_dsz); 1787 is32bit = 1; 1788 break; 1789 case VSD_DTYPE_CRHIST64: 1790 nbkts = HIST_VSDSZ2NBKTS(crhist64, hist_dsz); 1791 is32bit = 0; 1792 break; 1793 case VSD_DTYPE_DRHIST64: 1794 nbkts = HIST_VSDSZ2NBKTS(drhist64, hist_dsz); 1795 is32bit = 0; 1796 break; 1797 case VSD_DTYPE_DVHIST64: 1798 nbkts = HIST_VSDSZ2NBKTS(dvhist64, hist_dsz); 1799 is32bit = 0; 1800 break; 1801 default: 1802 return; 1803 } 1804 1805 switch (fmt) { 1806 case SB_STRFMT_FREEFORM: 1807 fmtstr = "nbkts=%hu, "; 1808 break; 1809 case SB_STRFMT_JSON: 1810 default: 1811 fmtstr = "\"nbkts\":%hu,"; 1812 break; 1813 } 1814 sbuf_printf(buf, fmtstr, nbkts); 1815 1816 switch (fmt) { 1817 case SB_STRFMT_FREEFORM: 1818 fmtstr = (is32bit ? "oob=%u" : "oob=%ju"); 1819 break; 1820 case SB_STRFMT_JSON: 1821 default: 1822 fmtstr = (is32bit ? "\"oob\":%u,\"bkts\":[" : 1823 "\"oob\":%ju,\"bkts\":["); 1824 break; 1825 } 1826 sbuf_printf(buf, fmtstr, is32bit ? VSD_CONSTHIST_FIELDVAL(hist, 1827 hist_dtype, oob) : (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist, 1828 hist_dtype, oob)); 1829 1830 for (i = 0; i < nbkts; i++) { 1831 switch (hist_dtype) { 1832 case VSD_DTYPE_CRHIST32: 1833 case VSD_DTYPE_CRHIST64: 1834 bkt_lb = VSD_CONSTCRHIST_FIELDPTR(hist, hist_dtype, 1835 bkts[i].lb); 1836 if (i < nbkts - 1) 1837 bkt_ub = VSD_CONSTCRHIST_FIELDPTR(hist, 1838 hist_dtype, bkts[i + 1].lb); 1839 else 1840 bkt_ub = &numeric_limits[LIM_MAX][voi_dtype]; 1841 break; 1842 case VSD_DTYPE_DRHIST32: 1843 case VSD_DTYPE_DRHIST64: 1844 bkt_lb = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype, 1845 bkts[i].lb); 1846 bkt_ub = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype, 1847 bkts[i].ub); 1848 break; 1849 case VSD_DTYPE_DVHIST32: 1850 case VSD_DTYPE_DVHIST64: 1851 bkt_lb = bkt_ub = VSD_CONSTDVHIST_FIELDPTR(hist, 1852 hist_dtype, bkts[i].val); 1853 break; 1854 default: 1855 break; 1856 } 1857 1858 switch (fmt) { 1859 case SB_STRFMT_FREEFORM: 1860 fmtstr = "\n\t\t\t\t"; 1861 break; 1862 case SB_STRFMT_JSON: 1863 default: 1864 fmtstr = "{"; 1865 break; 1866 } 1867 sbuf_cat(buf, fmtstr); 1868 1869 if (objdump) { 1870 switch (fmt) { 1871 case SB_STRFMT_FREEFORM: 1872 fmtstr = "bkt[%hu]."; 1873 break; 1874 case SB_STRFMT_JSON: 1875 default: 1876 fmtstr = "\"bkt\":%hu,"; 1877 break; 1878 } 1879 sbuf_printf(buf, fmtstr, i); 1880 } 1881 1882 switch (fmt) { 1883 case SB_STRFMT_FREEFORM: 1884 fmtstr = "{lb="; 1885 break; 1886 case SB_STRFMT_JSON: 1887 default: 1888 fmtstr = "\"lb\":"; 1889 break; 1890 } 1891 sbuf_cat(buf, fmtstr); 1892 stats_voistatdata_tostr((const struct voistatdata *)bkt_lb, 1893 voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric), 1894 fmt, buf, objdump); 1895 1896 switch (fmt) { 1897 case SB_STRFMT_FREEFORM: 1898 fmtstr = ",ub="; 1899 break; 1900 case SB_STRFMT_JSON: 1901 default: 1902 fmtstr = ",\"ub\":"; 1903 break; 1904 } 1905 sbuf_cat(buf, fmtstr); 1906 stats_voistatdata_tostr((const struct voistatdata *)bkt_ub, 1907 voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric), 1908 fmt, buf, objdump); 1909 1910 switch (fmt) { 1911 case SB_STRFMT_FREEFORM: 1912 fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}"; 1913 break; 1914 case SB_STRFMT_JSON: 1915 default: 1916 fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}"; 1917 break; 1918 } 1919 sbuf_printf(buf, fmtstr, is32bit ? 1920 VSD_CONSTHIST_FIELDVAL(hist, hist_dtype, bkts[i].cnt) : 1921 (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist, hist_dtype, 1922 bkts[i].cnt)); 1923 1924 if (fmt == SB_STRFMT_JSON && i < nbkts - 1) 1925 sbuf_putc(buf, ','); 1926 } 1927 if (fmt == SB_STRFMT_JSON) 1928 sbuf_cat(buf, "]"); 1929 } 1930 1931 int 1932 stats_voistatdata_tostr(const struct voistatdata *vsd, enum vsd_dtype voi_dtype, 1933 enum vsd_dtype vsd_dtype, size_t vsd_sz, enum sb_str_fmt fmt, 1934 struct sbuf *buf, int objdump) 1935 { 1936 const char *fmtstr; 1937 1938 if (vsd == NULL || buf == NULL || voi_dtype >= VSD_NUM_DTYPES || 1939 vsd_dtype >= VSD_NUM_DTYPES || fmt >= SB_STRFMT_NUM_FMTS) 1940 return (EINVAL); 1941 1942 switch (vsd_dtype) { 1943 case VSD_DTYPE_VOISTATE: 1944 switch (fmt) { 1945 case SB_STRFMT_FREEFORM: 1946 fmtstr = "prev="; 1947 break; 1948 case SB_STRFMT_JSON: 1949 default: 1950 fmtstr = "\"prev\":"; 1951 break; 1952 } 1953 sbuf_cat(buf, fmtstr); 1954 /* 1955 * Render prev by passing it as *vsd and voi_dtype as vsd_dtype. 1956 */ 1957 stats_voistatdata_tostr( 1958 (const struct voistatdata *)&CONSTVSD(voistate, vsd)->prev, 1959 voi_dtype, voi_dtype, vsd_sz, fmt, buf, objdump); 1960 break; 1961 case VSD_DTYPE_INT_S32: 1962 sbuf_printf(buf, "%d", vsd->int32.s32); 1963 break; 1964 case VSD_DTYPE_INT_U32: 1965 sbuf_printf(buf, "%u", vsd->int32.u32); 1966 break; 1967 case VSD_DTYPE_INT_S64: 1968 sbuf_printf(buf, "%jd", (intmax_t)vsd->int64.s64); 1969 break; 1970 case VSD_DTYPE_INT_U64: 1971 sbuf_printf(buf, "%ju", (uintmax_t)vsd->int64.u64); 1972 break; 1973 case VSD_DTYPE_INT_SLONG: 1974 sbuf_printf(buf, "%ld", vsd->intlong.slong); 1975 break; 1976 case VSD_DTYPE_INT_ULONG: 1977 sbuf_printf(buf, "%lu", vsd->intlong.ulong); 1978 break; 1979 case VSD_DTYPE_Q_S32: 1980 { 1981 char qstr[Q_MAXSTRLEN(vsd->q32.sq32, 10)]; 1982 Q_TOSTR((s32q_t)vsd->q32.sq32, -1, 10, qstr, sizeof(qstr)); 1983 sbuf_cat(buf, qstr); 1984 } 1985 break; 1986 case VSD_DTYPE_Q_U32: 1987 { 1988 char qstr[Q_MAXSTRLEN(vsd->q32.uq32, 10)]; 1989 Q_TOSTR((u32q_t)vsd->q32.uq32, -1, 10, qstr, sizeof(qstr)); 1990 sbuf_cat(buf, qstr); 1991 } 1992 break; 1993 case VSD_DTYPE_Q_S64: 1994 { 1995 char qstr[Q_MAXSTRLEN(vsd->q64.sq64, 10)]; 1996 Q_TOSTR((s64q_t)vsd->q64.sq64, -1, 10, qstr, sizeof(qstr)); 1997 sbuf_cat(buf, qstr); 1998 } 1999 break; 2000 case VSD_DTYPE_Q_U64: 2001 { 2002 char qstr[Q_MAXSTRLEN(vsd->q64.uq64, 10)]; 2003 Q_TOSTR((u64q_t)vsd->q64.uq64, -1, 10, qstr, sizeof(qstr)); 2004 sbuf_cat(buf, qstr); 2005 } 2006 break; 2007 case VSD_DTYPE_CRHIST32: 2008 case VSD_DTYPE_DRHIST32: 2009 case VSD_DTYPE_DVHIST32: 2010 case VSD_DTYPE_CRHIST64: 2011 case VSD_DTYPE_DRHIST64: 2012 case VSD_DTYPE_DVHIST64: 2013 stats_voistatdata_hist_tostr(voi_dtype, CONSTVSD(hist, vsd), 2014 vsd_dtype, vsd_sz, fmt, buf, objdump); 2015 break; 2016 case VSD_DTYPE_TDGSTCLUST32: 2017 case VSD_DTYPE_TDGSTCLUST64: 2018 stats_voistatdata_tdgst_tostr(voi_dtype, 2019 CONSTVSD(tdgst, vsd), vsd_dtype, vsd_sz, fmt, buf, 2020 objdump); 2021 break; 2022 default: 2023 break; 2024 } 2025 2026 return (sbuf_error(buf)); 2027 } 2028 2029 static void 2030 stats_v1_itercb_tostr_freeform(struct statsblobv1 *sb, struct voi *v, 2031 struct voistat *vs, struct sb_iter_ctx *ctx) 2032 { 2033 struct sb_tostrcb_ctx *sctx; 2034 struct metablob *tpl_mb; 2035 struct sbuf *buf; 2036 void *vsd; 2037 uint8_t dump; 2038 2039 sctx = ctx->usrctx; 2040 buf = sctx->buf; 2041 tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL; 2042 dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0); 2043 2044 if (ctx->flags & SB_IT_FIRST_CB) { 2045 sbuf_printf(buf, "struct statsblobv1@%p", sb); 2046 if (dump) { 2047 sbuf_printf(buf, ", abi=%hhu, endian=%hhu, maxsz=%hu, " 2048 "cursz=%hu, created=%jd, lastrst=%jd, flags=0x%04hx, " 2049 "stats_off=%hu, statsdata_off=%hu", 2050 sb->abi, sb->endian, sb->maxsz, sb->cursz, 2051 sb->created, sb->lastrst, sb->flags, sb->stats_off, 2052 sb->statsdata_off); 2053 } 2054 sbuf_printf(buf, ", tplhash=%u", sb->tplhash); 2055 } 2056 2057 if (ctx->flags & SB_IT_FIRST_VOISTAT) { 2058 sbuf_printf(buf, "\n\tvois[%hd]: id=%hd", ctx->vslot, v->id); 2059 if (v->id < 0) 2060 return; 2061 sbuf_printf(buf, ", name=\"%s\"", (tpl_mb == NULL) ? "" : 2062 tpl_mb->voi_meta[v->id].name); 2063 if (dump) 2064 sbuf_printf(buf, ", flags=0x%04hx, dtype=%s, " 2065 "voistatmaxid=%hhd, stats_off=%hu", v->flags, 2066 vsd_dtype2name[v->dtype], v->voistatmaxid, v->stats_off); 2067 } 2068 2069 if (!dump && vs->stype <= 0) 2070 return; 2071 2072 sbuf_printf(buf, "\n\t\tvois[%hd]stat[%hhd]: stype=", v->id, ctx->vsslot); 2073 if (vs->stype < 0) { 2074 sbuf_printf(buf, "%hhd", vs->stype); 2075 return; 2076 } else 2077 sbuf_printf(buf, "%s, errs=%hu", vs_stype2name[vs->stype], 2078 vs->errs); 2079 vsd = BLOB_OFFSET(sb, vs->data_off); 2080 if (dump) 2081 sbuf_printf(buf, ", flags=0x%04x, dtype=%s, dsz=%hu, " 2082 "data_off=%hu", vs->flags, vsd_dtype2name[vs->dtype], 2083 vs->dsz, vs->data_off); 2084 2085 sbuf_printf(buf, "\n\t\t\tvoistatdata: "); 2086 stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz, 2087 sctx->fmt, buf, dump); 2088 } 2089 2090 static void 2091 stats_v1_itercb_tostr_json(struct statsblobv1 *sb, struct voi *v, struct voistat *vs, 2092 struct sb_iter_ctx *ctx) 2093 { 2094 struct sb_tostrcb_ctx *sctx; 2095 struct metablob *tpl_mb; 2096 struct sbuf *buf; 2097 const char *fmtstr; 2098 void *vsd; 2099 uint8_t dump; 2100 2101 sctx = ctx->usrctx; 2102 buf = sctx->buf; 2103 tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL; 2104 dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0); 2105 2106 if (ctx->flags & SB_IT_FIRST_CB) { 2107 sbuf_putc(buf, '{'); 2108 if (dump) { 2109 sbuf_printf(buf, "\"abi\":%hhu,\"endian\":%hhu," 2110 "\"maxsz\":%hu,\"cursz\":%hu,\"created\":%jd," 2111 "\"lastrst\":%jd,\"flags\":%hu,\"stats_off\":%hu," 2112 "\"statsdata_off\":%hu,", sb->abi, 2113 sb->endian, sb->maxsz, sb->cursz, sb->created, 2114 sb->lastrst, sb->flags, sb->stats_off, 2115 sb->statsdata_off); 2116 } 2117 2118 if (tpl_mb == NULL) 2119 fmtstr = "\"tplname\":%s,\"tplhash\":%u,\"vois\":{"; 2120 else 2121 fmtstr = "\"tplname\":\"%s\",\"tplhash\":%u,\"vois\":{"; 2122 2123 sbuf_printf(buf, fmtstr, tpl_mb ? tpl_mb->tplname : "null", 2124 sb->tplhash); 2125 } 2126 2127 if (ctx->flags & SB_IT_FIRST_VOISTAT) { 2128 if (dump) { 2129 sbuf_printf(buf, "\"[%d]\":{\"id\":%d", ctx->vslot, 2130 v->id); 2131 if (v->id < 0) { 2132 sbuf_printf(buf, "},"); 2133 return; 2134 } 2135 2136 if (tpl_mb == NULL) 2137 fmtstr = ",\"name\":%s,\"flags\":%hu," 2138 "\"dtype\":\"%s\",\"voistatmaxid\":%hhd," 2139 "\"stats_off\":%hu,"; 2140 else 2141 fmtstr = ",\"name\":\"%s\",\"flags\":%hu," 2142 "\"dtype\":\"%s\",\"voistatmaxid\":%hhd," 2143 "\"stats_off\":%hu,"; 2144 2145 sbuf_printf(buf, fmtstr, tpl_mb ? 2146 tpl_mb->voi_meta[v->id].name : "null", v->flags, 2147 vsd_dtype2name[v->dtype], v->voistatmaxid, 2148 v->stats_off); 2149 } else { 2150 if (tpl_mb == NULL) { 2151 sbuf_printf(buf, "\"[%hd]\":{", v->id); 2152 } else { 2153 sbuf_printf(buf, "\"%s\":{", 2154 tpl_mb->voi_meta[v->id].name); 2155 } 2156 } 2157 sbuf_cat(buf, "\"stats\":{"); 2158 } 2159 2160 vsd = BLOB_OFFSET(sb, vs->data_off); 2161 if (dump) { 2162 sbuf_printf(buf, "\"[%hhd]\":", ctx->vsslot); 2163 if (vs->stype < 0) { 2164 sbuf_printf(buf, "{\"stype\":-1},"); 2165 return; 2166 } 2167 sbuf_printf(buf, "{\"stype\":\"%s\",\"errs\":%hu,\"flags\":%hu," 2168 "\"dtype\":\"%s\",\"data_off\":%hu,\"voistatdata\":{", 2169 vs_stype2name[vs->stype], vs->errs, vs->flags, 2170 vsd_dtype2name[vs->dtype], vs->data_off); 2171 } else if (vs->stype > 0) { 2172 if (tpl_mb == NULL) 2173 sbuf_printf(buf, "\"[%hhd]\":", vs->stype); 2174 else 2175 sbuf_printf(buf, "\"%s\":", vs_stype2name[vs->stype]); 2176 } else 2177 return; 2178 2179 if ((vs->flags & VS_VSDVALID) || dump) { 2180 if (!dump) 2181 sbuf_printf(buf, "{\"errs\":%hu,", vs->errs); 2182 /* Simple non-compound VSD types need a key. */ 2183 if (!vsd_compoundtype[vs->dtype]) 2184 sbuf_cat(buf, "\"val\":"); 2185 stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz, 2186 sctx->fmt, buf, dump); 2187 sbuf_cat(buf, dump ? "}}" : "}"); 2188 } else 2189 sbuf_cat(buf, dump ? "null}" : "null"); 2190 2191 if (ctx->flags & SB_IT_LAST_VOISTAT) 2192 sbuf_cat(buf, "}}"); 2193 2194 if (ctx->flags & SB_IT_LAST_CB) 2195 sbuf_cat(buf, "}}"); 2196 else 2197 sbuf_putc(buf, ','); 2198 } 2199 2200 static int 2201 stats_v1_itercb_tostr(struct statsblobv1 *sb, struct voi *v, struct voistat *vs, 2202 struct sb_iter_ctx *ctx) 2203 { 2204 struct sb_tostrcb_ctx *sctx; 2205 2206 sctx = ctx->usrctx; 2207 2208 switch (sctx->fmt) { 2209 case SB_STRFMT_FREEFORM: 2210 stats_v1_itercb_tostr_freeform(sb, v, vs, ctx); 2211 break; 2212 case SB_STRFMT_JSON: 2213 stats_v1_itercb_tostr_json(sb, v, vs, ctx); 2214 break; 2215 default: 2216 break; 2217 } 2218 2219 return (sbuf_error(sctx->buf)); 2220 } 2221 2222 int 2223 stats_v1_blob_tostr(struct statsblobv1 *sb, struct sbuf *buf, 2224 enum sb_str_fmt fmt, uint32_t flags) 2225 { 2226 struct sb_tostrcb_ctx sctx; 2227 uint32_t iflags; 2228 2229 if (sb == NULL || sb->abi != STATS_ABI_V1 || buf == NULL || 2230 fmt >= SB_STRFMT_NUM_FMTS) 2231 return (EINVAL); 2232 2233 sctx.buf = buf; 2234 sctx.fmt = fmt; 2235 sctx.flags = flags; 2236 2237 if (flags & SB_TOSTR_META) { 2238 if (stats_tpl_fetch(stats_tpl_fetch_allocid(NULL, sb->tplhash), 2239 &sctx.tpl)) 2240 return (EINVAL); 2241 } else 2242 sctx.tpl = NULL; 2243 2244 iflags = 0; 2245 if (flags & SB_TOSTR_OBJDUMP) 2246 iflags |= (SB_IT_NULLVOI | SB_IT_NULLVOISTAT); 2247 stats_v1_blob_iter(sb, stats_v1_itercb_tostr, &sctx, iflags); 2248 2249 return (sbuf_error(buf)); 2250 } 2251 2252 static int 2253 stats_v1_itercb_visit(struct statsblobv1 *sb, struct voi *v, 2254 struct voistat *vs, struct sb_iter_ctx *ctx) 2255 { 2256 struct sb_visitcb_ctx *vctx; 2257 struct sb_visit sbv; 2258 2259 vctx = ctx->usrctx; 2260 2261 sbv.tplhash = sb->tplhash; 2262 sbv.voi_id = v->id; 2263 sbv.voi_dtype = v->dtype; 2264 sbv.vs_stype = vs->stype; 2265 sbv.vs_dtype = vs->dtype; 2266 sbv.vs_dsz = vs->dsz; 2267 sbv.vs_data = BLOB_OFFSET(sb, vs->data_off); 2268 sbv.vs_errs = vs->errs; 2269 sbv.flags = ctx->flags & (SB_IT_FIRST_CB | SB_IT_LAST_CB | 2270 SB_IT_FIRST_VOI | SB_IT_LAST_VOI | SB_IT_FIRST_VOISTAT | 2271 SB_IT_LAST_VOISTAT); 2272 2273 return (vctx->cb(&sbv, vctx->usrctx)); 2274 } 2275 2276 int 2277 stats_v1_blob_visit(struct statsblobv1 *sb, stats_blob_visitcb_t func, 2278 void *usrctx) 2279 { 2280 struct sb_visitcb_ctx vctx; 2281 2282 if (sb == NULL || sb->abi != STATS_ABI_V1 || func == NULL) 2283 return (EINVAL); 2284 2285 vctx.cb = func; 2286 vctx.usrctx = usrctx; 2287 2288 stats_v1_blob_iter(sb, stats_v1_itercb_visit, &vctx, 0); 2289 2290 return (0); 2291 } 2292 2293 static int 2294 stats_v1_icb_reset_voistat(struct statsblobv1 *sb, struct voi *v __unused, 2295 struct voistat *vs, struct sb_iter_ctx *ctx __unused) 2296 { 2297 void *vsd; 2298 2299 if (vs->stype == VS_STYPE_VOISTATE) 2300 return (0); 2301 2302 vsd = BLOB_OFFSET(sb, vs->data_off); 2303 2304 /* Perform the stat type's default reset action. */ 2305 switch (vs->stype) { 2306 case VS_STYPE_SUM: 2307 switch (vs->dtype) { 2308 case VSD_DTYPE_Q_S32: 2309 Q_SIFVAL(VSD(q32, vsd)->sq32, 0); 2310 break; 2311 case VSD_DTYPE_Q_U32: 2312 Q_SIFVAL(VSD(q32, vsd)->uq32, 0); 2313 break; 2314 case VSD_DTYPE_Q_S64: 2315 Q_SIFVAL(VSD(q64, vsd)->sq64, 0); 2316 break; 2317 case VSD_DTYPE_Q_U64: 2318 Q_SIFVAL(VSD(q64, vsd)->uq64, 0); 2319 break; 2320 default: 2321 bzero(vsd, vs->dsz); 2322 break; 2323 } 2324 break; 2325 case VS_STYPE_MAX: 2326 switch (vs->dtype) { 2327 case VSD_DTYPE_Q_S32: 2328 Q_SIFVAL(VSD(q32, vsd)->sq32, 2329 Q_IFMINVAL(VSD(q32, vsd)->sq32)); 2330 break; 2331 case VSD_DTYPE_Q_U32: 2332 Q_SIFVAL(VSD(q32, vsd)->uq32, 2333 Q_IFMINVAL(VSD(q32, vsd)->uq32)); 2334 break; 2335 case VSD_DTYPE_Q_S64: 2336 Q_SIFVAL(VSD(q64, vsd)->sq64, 2337 Q_IFMINVAL(VSD(q64, vsd)->sq64)); 2338 break; 2339 case VSD_DTYPE_Q_U64: 2340 Q_SIFVAL(VSD(q64, vsd)->uq64, 2341 Q_IFMINVAL(VSD(q64, vsd)->uq64)); 2342 break; 2343 default: 2344 memcpy(vsd, &numeric_limits[LIM_MIN][vs->dtype], 2345 vs->dsz); 2346 break; 2347 } 2348 break; 2349 case VS_STYPE_MIN: 2350 switch (vs->dtype) { 2351 case VSD_DTYPE_Q_S32: 2352 Q_SIFVAL(VSD(q32, vsd)->sq32, 2353 Q_IFMAXVAL(VSD(q32, vsd)->sq32)); 2354 break; 2355 case VSD_DTYPE_Q_U32: 2356 Q_SIFVAL(VSD(q32, vsd)->uq32, 2357 Q_IFMAXVAL(VSD(q32, vsd)->uq32)); 2358 break; 2359 case VSD_DTYPE_Q_S64: 2360 Q_SIFVAL(VSD(q64, vsd)->sq64, 2361 Q_IFMAXVAL(VSD(q64, vsd)->sq64)); 2362 break; 2363 case VSD_DTYPE_Q_U64: 2364 Q_SIFVAL(VSD(q64, vsd)->uq64, 2365 Q_IFMAXVAL(VSD(q64, vsd)->uq64)); 2366 break; 2367 default: 2368 memcpy(vsd, &numeric_limits[LIM_MAX][vs->dtype], 2369 vs->dsz); 2370 break; 2371 } 2372 break; 2373 case VS_STYPE_HIST: 2374 { 2375 /* Reset bucket counts. */ 2376 struct voistatdata_hist *hist; 2377 int i, is32bit; 2378 uint16_t nbkts; 2379 2380 hist = VSD(hist, vsd); 2381 switch (vs->dtype) { 2382 case VSD_DTYPE_CRHIST32: 2383 nbkts = HIST_VSDSZ2NBKTS(crhist32, vs->dsz); 2384 is32bit = 1; 2385 break; 2386 case VSD_DTYPE_DRHIST32: 2387 nbkts = HIST_VSDSZ2NBKTS(drhist32, vs->dsz); 2388 is32bit = 1; 2389 break; 2390 case VSD_DTYPE_DVHIST32: 2391 nbkts = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz); 2392 is32bit = 1; 2393 break; 2394 case VSD_DTYPE_CRHIST64: 2395 nbkts = HIST_VSDSZ2NBKTS(crhist64, vs->dsz); 2396 is32bit = 0; 2397 break; 2398 case VSD_DTYPE_DRHIST64: 2399 nbkts = HIST_VSDSZ2NBKTS(drhist64, vs->dsz); 2400 is32bit = 0; 2401 break; 2402 case VSD_DTYPE_DVHIST64: 2403 nbkts = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz); 2404 is32bit = 0; 2405 break; 2406 default: 2407 return (0); 2408 } 2409 2410 bzero(VSD_HIST_FIELDPTR(hist, vs->dtype, oob), 2411 is32bit ? sizeof(uint32_t) : sizeof(uint64_t)); 2412 for (i = nbkts - 1; i >= 0; i--) { 2413 bzero(VSD_HIST_FIELDPTR(hist, vs->dtype, 2414 bkts[i].cnt), is32bit ? sizeof(uint32_t) : 2415 sizeof(uint64_t)); 2416 } 2417 break; 2418 } 2419 case VS_STYPE_TDGST: 2420 { 2421 /* Reset sample count centroids array/tree. */ 2422 struct voistatdata_tdgst *tdgst; 2423 struct ctdth32 *ctd32tree; 2424 struct ctdth64 *ctd64tree; 2425 struct voistatdata_tdgstctd32 *ctd32; 2426 struct voistatdata_tdgstctd64 *ctd64; 2427 2428 tdgst = VSD(tdgst, vsd); 2429 switch (vs->dtype) { 2430 case VSD_DTYPE_TDGSTCLUST32: 2431 VSD(tdgstclust32, tdgst)->smplcnt = 0; 2432 VSD(tdgstclust32, tdgst)->compcnt = 0; 2433 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree; 2434 ARB_INIT(ctd32, ctdlnk, ctd32tree, 2435 ARB_MAXNODES(ctd32tree)) { 2436 ctd32->cnt = 0; 2437 Q_SIFVAL(ctd32->mu, 0); 2438 } 2439 #ifdef DIAGNOSTIC 2440 RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree); 2441 #endif 2442 break; 2443 case VSD_DTYPE_TDGSTCLUST64: 2444 VSD(tdgstclust64, tdgst)->smplcnt = 0; 2445 VSD(tdgstclust64, tdgst)->compcnt = 0; 2446 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree; 2447 ARB_INIT(ctd64, ctdlnk, ctd64tree, 2448 ARB_MAXNODES(ctd64tree)) { 2449 ctd64->cnt = 0; 2450 Q_SIFVAL(ctd64->mu, 0); 2451 } 2452 #ifdef DIAGNOSTIC 2453 RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree); 2454 #endif 2455 break; 2456 default: 2457 return (0); 2458 } 2459 break; 2460 } 2461 default: 2462 KASSERT(0, ("Unknown VOI stat type %d", vs->stype)); 2463 break; 2464 } 2465 2466 vs->errs = 0; 2467 vs->flags &= ~VS_VSDVALID; 2468 2469 return (0); 2470 } 2471 2472 int 2473 stats_v1_blob_snapshot(struct statsblobv1 **dst, size_t dstmaxsz, 2474 struct statsblobv1 *src, uint32_t flags) 2475 { 2476 int error; 2477 2478 if (src != NULL && src->abi == STATS_ABI_V1) { 2479 error = stats_v1_blob_clone(dst, dstmaxsz, src, flags); 2480 if (!error) { 2481 if (flags & SB_CLONE_RSTSRC) { 2482 stats_v1_blob_iter(src, 2483 stats_v1_icb_reset_voistat, NULL, 0); 2484 src->lastrst = stats_sbinuptime(); 2485 } 2486 stats_v1_blob_finalise(*dst); 2487 } 2488 } else 2489 error = EINVAL; 2490 2491 return (error); 2492 } 2493 2494 static inline int 2495 stats_v1_voi_update_max(enum vsd_dtype voi_dtype __unused, 2496 struct voistatdata *voival, struct voistat *vs, void *vsd) 2497 { 2498 int error; 2499 2500 KASSERT(vs->dtype < VSD_NUM_DTYPES, 2501 ("Unknown VSD dtype %d", vs->dtype)); 2502 2503 error = 0; 2504 2505 switch (vs->dtype) { 2506 case VSD_DTYPE_INT_S32: 2507 if (VSD(int32, vsd)->s32 < voival->int32.s32) { 2508 VSD(int32, vsd)->s32 = voival->int32.s32; 2509 vs->flags |= VS_VSDVALID; 2510 } 2511 break; 2512 case VSD_DTYPE_INT_U32: 2513 if (VSD(int32, vsd)->u32 < voival->int32.u32) { 2514 VSD(int32, vsd)->u32 = voival->int32.u32; 2515 vs->flags |= VS_VSDVALID; 2516 } 2517 break; 2518 case VSD_DTYPE_INT_S64: 2519 if (VSD(int64, vsd)->s64 < voival->int64.s64) { 2520 VSD(int64, vsd)->s64 = voival->int64.s64; 2521 vs->flags |= VS_VSDVALID; 2522 } 2523 break; 2524 case VSD_DTYPE_INT_U64: 2525 if (VSD(int64, vsd)->u64 < voival->int64.u64) { 2526 VSD(int64, vsd)->u64 = voival->int64.u64; 2527 vs->flags |= VS_VSDVALID; 2528 } 2529 break; 2530 case VSD_DTYPE_INT_SLONG: 2531 if (VSD(intlong, vsd)->slong < voival->intlong.slong) { 2532 VSD(intlong, vsd)->slong = voival->intlong.slong; 2533 vs->flags |= VS_VSDVALID; 2534 } 2535 break; 2536 case VSD_DTYPE_INT_ULONG: 2537 if (VSD(intlong, vsd)->ulong < voival->intlong.ulong) { 2538 VSD(intlong, vsd)->ulong = voival->intlong.ulong; 2539 vs->flags |= VS_VSDVALID; 2540 } 2541 break; 2542 case VSD_DTYPE_Q_S32: 2543 if (Q_QLTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) && 2544 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32, 2545 voival->q32.sq32)))) { 2546 vs->flags |= VS_VSDVALID; 2547 } 2548 break; 2549 case VSD_DTYPE_Q_U32: 2550 if (Q_QLTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) && 2551 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32, 2552 voival->q32.uq32)))) { 2553 vs->flags |= VS_VSDVALID; 2554 } 2555 break; 2556 case VSD_DTYPE_Q_S64: 2557 if (Q_QLTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) && 2558 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64, 2559 voival->q64.sq64)))) { 2560 vs->flags |= VS_VSDVALID; 2561 } 2562 break; 2563 case VSD_DTYPE_Q_U64: 2564 if (Q_QLTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) && 2565 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64, 2566 voival->q64.uq64)))) { 2567 vs->flags |= VS_VSDVALID; 2568 } 2569 break; 2570 default: 2571 error = EINVAL; 2572 break; 2573 } 2574 2575 return (error); 2576 } 2577 2578 static inline int 2579 stats_v1_voi_update_min(enum vsd_dtype voi_dtype __unused, 2580 struct voistatdata *voival, struct voistat *vs, void *vsd) 2581 { 2582 int error; 2583 2584 KASSERT(vs->dtype < VSD_NUM_DTYPES, 2585 ("Unknown VSD dtype %d", vs->dtype)); 2586 2587 error = 0; 2588 2589 switch (vs->dtype) { 2590 case VSD_DTYPE_INT_S32: 2591 if (VSD(int32, vsd)->s32 > voival->int32.s32) { 2592 VSD(int32, vsd)->s32 = voival->int32.s32; 2593 vs->flags |= VS_VSDVALID; 2594 } 2595 break; 2596 case VSD_DTYPE_INT_U32: 2597 if (VSD(int32, vsd)->u32 > voival->int32.u32) { 2598 VSD(int32, vsd)->u32 = voival->int32.u32; 2599 vs->flags |= VS_VSDVALID; 2600 } 2601 break; 2602 case VSD_DTYPE_INT_S64: 2603 if (VSD(int64, vsd)->s64 > voival->int64.s64) { 2604 VSD(int64, vsd)->s64 = voival->int64.s64; 2605 vs->flags |= VS_VSDVALID; 2606 } 2607 break; 2608 case VSD_DTYPE_INT_U64: 2609 if (VSD(int64, vsd)->u64 > voival->int64.u64) { 2610 VSD(int64, vsd)->u64 = voival->int64.u64; 2611 vs->flags |= VS_VSDVALID; 2612 } 2613 break; 2614 case VSD_DTYPE_INT_SLONG: 2615 if (VSD(intlong, vsd)->slong > voival->intlong.slong) { 2616 VSD(intlong, vsd)->slong = voival->intlong.slong; 2617 vs->flags |= VS_VSDVALID; 2618 } 2619 break; 2620 case VSD_DTYPE_INT_ULONG: 2621 if (VSD(intlong, vsd)->ulong > voival->intlong.ulong) { 2622 VSD(intlong, vsd)->ulong = voival->intlong.ulong; 2623 vs->flags |= VS_VSDVALID; 2624 } 2625 break; 2626 case VSD_DTYPE_Q_S32: 2627 if (Q_QGTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) && 2628 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32, 2629 voival->q32.sq32)))) { 2630 vs->flags |= VS_VSDVALID; 2631 } 2632 break; 2633 case VSD_DTYPE_Q_U32: 2634 if (Q_QGTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) && 2635 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32, 2636 voival->q32.uq32)))) { 2637 vs->flags |= VS_VSDVALID; 2638 } 2639 break; 2640 case VSD_DTYPE_Q_S64: 2641 if (Q_QGTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) && 2642 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64, 2643 voival->q64.sq64)))) { 2644 vs->flags |= VS_VSDVALID; 2645 } 2646 break; 2647 case VSD_DTYPE_Q_U64: 2648 if (Q_QGTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) && 2649 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64, 2650 voival->q64.uq64)))) { 2651 vs->flags |= VS_VSDVALID; 2652 } 2653 break; 2654 default: 2655 error = EINVAL; 2656 break; 2657 } 2658 2659 return (error); 2660 } 2661 2662 static inline int 2663 stats_v1_voi_update_sum(enum vsd_dtype voi_dtype __unused, 2664 struct voistatdata *voival, struct voistat *vs, void *vsd) 2665 { 2666 int error; 2667 2668 KASSERT(vs->dtype < VSD_NUM_DTYPES, 2669 ("Unknown VSD dtype %d", vs->dtype)); 2670 2671 error = 0; 2672 2673 switch (vs->dtype) { 2674 case VSD_DTYPE_INT_S32: 2675 VSD(int32, vsd)->s32 += voival->int32.s32; 2676 break; 2677 case VSD_DTYPE_INT_U32: 2678 VSD(int32, vsd)->u32 += voival->int32.u32; 2679 break; 2680 case VSD_DTYPE_INT_S64: 2681 VSD(int64, vsd)->s64 += voival->int64.s64; 2682 break; 2683 case VSD_DTYPE_INT_U64: 2684 VSD(int64, vsd)->u64 += voival->int64.u64; 2685 break; 2686 case VSD_DTYPE_INT_SLONG: 2687 VSD(intlong, vsd)->slong += voival->intlong.slong; 2688 break; 2689 case VSD_DTYPE_INT_ULONG: 2690 VSD(intlong, vsd)->ulong += voival->intlong.ulong; 2691 break; 2692 case VSD_DTYPE_Q_S32: 2693 error = Q_QADDQ(&VSD(q32, vsd)->sq32, voival->q32.sq32); 2694 break; 2695 case VSD_DTYPE_Q_U32: 2696 error = Q_QADDQ(&VSD(q32, vsd)->uq32, voival->q32.uq32); 2697 break; 2698 case VSD_DTYPE_Q_S64: 2699 error = Q_QADDQ(&VSD(q64, vsd)->sq64, voival->q64.sq64); 2700 break; 2701 case VSD_DTYPE_Q_U64: 2702 error = Q_QADDQ(&VSD(q64, vsd)->uq64, voival->q64.uq64); 2703 break; 2704 default: 2705 error = EINVAL; 2706 break; 2707 } 2708 2709 if (!error) 2710 vs->flags |= VS_VSDVALID; 2711 2712 return (error); 2713 } 2714 2715 static inline int 2716 stats_v1_voi_update_hist(enum vsd_dtype voi_dtype, struct voistatdata *voival, 2717 struct voistat *vs, struct voistatdata_hist *hist) 2718 { 2719 struct voistatdata_numeric *bkt_lb, *bkt_ub; 2720 uint64_t *oob64, *cnt64; 2721 uint32_t *oob32, *cnt32; 2722 int error, i, found, is32bit, has_ub, eq_only; 2723 2724 error = 0; 2725 2726 switch (vs->dtype) { 2727 case VSD_DTYPE_CRHIST32: 2728 i = HIST_VSDSZ2NBKTS(crhist32, vs->dsz); 2729 is32bit = 1; 2730 has_ub = eq_only = 0; 2731 oob32 = &VSD(crhist32, hist)->oob; 2732 break; 2733 case VSD_DTYPE_DRHIST32: 2734 i = HIST_VSDSZ2NBKTS(drhist32, vs->dsz); 2735 is32bit = has_ub = 1; 2736 eq_only = 0; 2737 oob32 = &VSD(drhist32, hist)->oob; 2738 break; 2739 case VSD_DTYPE_DVHIST32: 2740 i = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz); 2741 is32bit = eq_only = 1; 2742 has_ub = 0; 2743 oob32 = &VSD(dvhist32, hist)->oob; 2744 break; 2745 case VSD_DTYPE_CRHIST64: 2746 i = HIST_VSDSZ2NBKTS(crhist64, vs->dsz); 2747 is32bit = has_ub = eq_only = 0; 2748 oob64 = &VSD(crhist64, hist)->oob; 2749 break; 2750 case VSD_DTYPE_DRHIST64: 2751 i = HIST_VSDSZ2NBKTS(drhist64, vs->dsz); 2752 is32bit = eq_only = 0; 2753 has_ub = 1; 2754 oob64 = &VSD(drhist64, hist)->oob; 2755 break; 2756 case VSD_DTYPE_DVHIST64: 2757 i = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz); 2758 is32bit = has_ub = 0; 2759 eq_only = 1; 2760 oob64 = &VSD(dvhist64, hist)->oob; 2761 break; 2762 default: 2763 return (EINVAL); 2764 } 2765 i--; /* Adjust for 0-based array index. */ 2766 2767 /* XXXLAS: Should probably use a better bucket search algorithm. ARB? */ 2768 for (found = 0; i >= 0 && !found; i--) { 2769 switch (vs->dtype) { 2770 case VSD_DTYPE_CRHIST32: 2771 bkt_lb = &VSD(crhist32, hist)->bkts[i].lb; 2772 cnt32 = &VSD(crhist32, hist)->bkts[i].cnt; 2773 break; 2774 case VSD_DTYPE_DRHIST32: 2775 bkt_lb = &VSD(drhist32, hist)->bkts[i].lb; 2776 bkt_ub = &VSD(drhist32, hist)->bkts[i].ub; 2777 cnt32 = &VSD(drhist32, hist)->bkts[i].cnt; 2778 break; 2779 case VSD_DTYPE_DVHIST32: 2780 bkt_lb = &VSD(dvhist32, hist)->bkts[i].val; 2781 cnt32 = &VSD(dvhist32, hist)->bkts[i].cnt; 2782 break; 2783 case VSD_DTYPE_CRHIST64: 2784 bkt_lb = &VSD(crhist64, hist)->bkts[i].lb; 2785 cnt64 = &VSD(crhist64, hist)->bkts[i].cnt; 2786 break; 2787 case VSD_DTYPE_DRHIST64: 2788 bkt_lb = &VSD(drhist64, hist)->bkts[i].lb; 2789 bkt_ub = &VSD(drhist64, hist)->bkts[i].ub; 2790 cnt64 = &VSD(drhist64, hist)->bkts[i].cnt; 2791 break; 2792 case VSD_DTYPE_DVHIST64: 2793 bkt_lb = &VSD(dvhist64, hist)->bkts[i].val; 2794 cnt64 = &VSD(dvhist64, hist)->bkts[i].cnt; 2795 break; 2796 default: 2797 return (EINVAL); 2798 } 2799 2800 switch (voi_dtype) { 2801 case VSD_DTYPE_INT_S32: 2802 if (voival->int32.s32 >= bkt_lb->int32.s32) { 2803 if ((eq_only && voival->int32.s32 == 2804 bkt_lb->int32.s32) || 2805 (!eq_only && (!has_ub || 2806 voival->int32.s32 < bkt_ub->int32.s32))) 2807 found = 1; 2808 } 2809 break; 2810 case VSD_DTYPE_INT_U32: 2811 if (voival->int32.u32 >= bkt_lb->int32.u32) { 2812 if ((eq_only && voival->int32.u32 == 2813 bkt_lb->int32.u32) || 2814 (!eq_only && (!has_ub || 2815 voival->int32.u32 < bkt_ub->int32.u32))) 2816 found = 1; 2817 } 2818 break; 2819 case VSD_DTYPE_INT_S64: 2820 if (voival->int64.s64 >= bkt_lb->int64.s64) 2821 if ((eq_only && voival->int64.s64 == 2822 bkt_lb->int64.s64) || 2823 (!eq_only && (!has_ub || 2824 voival->int64.s64 < bkt_ub->int64.s64))) 2825 found = 1; 2826 break; 2827 case VSD_DTYPE_INT_U64: 2828 if (voival->int64.u64 >= bkt_lb->int64.u64) 2829 if ((eq_only && voival->int64.u64 == 2830 bkt_lb->int64.u64) || 2831 (!eq_only && (!has_ub || 2832 voival->int64.u64 < bkt_ub->int64.u64))) 2833 found = 1; 2834 break; 2835 case VSD_DTYPE_INT_SLONG: 2836 if (voival->intlong.slong >= bkt_lb->intlong.slong) 2837 if ((eq_only && voival->intlong.slong == 2838 bkt_lb->intlong.slong) || 2839 (!eq_only && (!has_ub || 2840 voival->intlong.slong < 2841 bkt_ub->intlong.slong))) 2842 found = 1; 2843 break; 2844 case VSD_DTYPE_INT_ULONG: 2845 if (voival->intlong.ulong >= bkt_lb->intlong.ulong) 2846 if ((eq_only && voival->intlong.ulong == 2847 bkt_lb->intlong.ulong) || 2848 (!eq_only && (!has_ub || 2849 voival->intlong.ulong < 2850 bkt_ub->intlong.ulong))) 2851 found = 1; 2852 break; 2853 case VSD_DTYPE_Q_S32: 2854 if (Q_QGEQ(voival->q32.sq32, bkt_lb->q32.sq32)) 2855 if ((eq_only && Q_QEQ(voival->q32.sq32, 2856 bkt_lb->q32.sq32)) || 2857 (!eq_only && (!has_ub || 2858 Q_QLTQ(voival->q32.sq32, 2859 bkt_ub->q32.sq32)))) 2860 found = 1; 2861 break; 2862 case VSD_DTYPE_Q_U32: 2863 if (Q_QGEQ(voival->q32.uq32, bkt_lb->q32.uq32)) 2864 if ((eq_only && Q_QEQ(voival->q32.uq32, 2865 bkt_lb->q32.uq32)) || 2866 (!eq_only && (!has_ub || 2867 Q_QLTQ(voival->q32.uq32, 2868 bkt_ub->q32.uq32)))) 2869 found = 1; 2870 break; 2871 case VSD_DTYPE_Q_S64: 2872 if (Q_QGEQ(voival->q64.sq64, bkt_lb->q64.sq64)) 2873 if ((eq_only && Q_QEQ(voival->q64.sq64, 2874 bkt_lb->q64.sq64)) || 2875 (!eq_only && (!has_ub || 2876 Q_QLTQ(voival->q64.sq64, 2877 bkt_ub->q64.sq64)))) 2878 found = 1; 2879 break; 2880 case VSD_DTYPE_Q_U64: 2881 if (Q_QGEQ(voival->q64.uq64, bkt_lb->q64.uq64)) 2882 if ((eq_only && Q_QEQ(voival->q64.uq64, 2883 bkt_lb->q64.uq64)) || 2884 (!eq_only && (!has_ub || 2885 Q_QLTQ(voival->q64.uq64, 2886 bkt_ub->q64.uq64)))) 2887 found = 1; 2888 break; 2889 default: 2890 break; 2891 } 2892 } 2893 2894 if (found) { 2895 if (is32bit) 2896 *cnt32 += 1; 2897 else 2898 *cnt64 += 1; 2899 } else { 2900 if (is32bit) 2901 *oob32 += 1; 2902 else 2903 *oob64 += 1; 2904 } 2905 2906 vs->flags |= VS_VSDVALID; 2907 return (error); 2908 } 2909 2910 static inline int 2911 stats_v1_vsd_tdgst_compress(enum vsd_dtype vs_dtype, 2912 struct voistatdata_tdgst *tdgst, int attempt) 2913 { 2914 struct ctdth32 *ctd32tree; 2915 struct ctdth64 *ctd64tree; 2916 struct voistatdata_tdgstctd32 *ctd32; 2917 struct voistatdata_tdgstctd64 *ctd64; 2918 uint64_t ebits, idxmask; 2919 uint32_t bitsperidx, nebits; 2920 int error, idx, is32bit, maxctds, remctds, tmperr; 2921 2922 error = 0; 2923 2924 switch (vs_dtype) { 2925 case VSD_DTYPE_TDGSTCLUST32: 2926 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree; 2927 if (!ARB_FULL(ctd32tree)) 2928 return (0); 2929 VSD(tdgstclust32, tdgst)->compcnt++; 2930 maxctds = remctds = ARB_MAXNODES(ctd32tree); 2931 ARB_RESET_TREE(ctd32tree, ctdth32, maxctds); 2932 VSD(tdgstclust32, tdgst)->smplcnt = 0; 2933 is32bit = 1; 2934 ctd64tree = NULL; 2935 ctd64 = NULL; 2936 #ifdef DIAGNOSTIC 2937 RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree); 2938 #endif 2939 break; 2940 case VSD_DTYPE_TDGSTCLUST64: 2941 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree; 2942 if (!ARB_FULL(ctd64tree)) 2943 return (0); 2944 VSD(tdgstclust64, tdgst)->compcnt++; 2945 maxctds = remctds = ARB_MAXNODES(ctd64tree); 2946 ARB_RESET_TREE(ctd64tree, ctdth64, maxctds); 2947 VSD(tdgstclust64, tdgst)->smplcnt = 0; 2948 is32bit = 0; 2949 ctd32tree = NULL; 2950 ctd32 = NULL; 2951 #ifdef DIAGNOSTIC 2952 RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree); 2953 #endif 2954 break; 2955 default: 2956 return (EINVAL); 2957 } 2958 2959 /* 2960 * Rebuild the t-digest ARB by pseudorandomly selecting centroids and 2961 * re-inserting the mu/cnt of each as a value and corresponding weight. 2962 */ 2963 2964 /* 2965 * XXXCEM: random(9) is currently rand(3), not random(3). rand(3) 2966 * RAND_MAX happens to be approximately 31 bits (range [0, 2967 * 0x7ffffffd]), so the math kinda works out. When/if this portion of 2968 * the code is compiled in userspace, it gets the random(3) behavior, 2969 * which has expected range [0, 0x7fffffff]. 2970 */ 2971 #define bitsperrand 31 2972 ebits = 0; 2973 nebits = 0; 2974 bitsperidx = fls(maxctds); 2975 KASSERT(bitsperidx <= sizeof(ebits) << 3, 2976 ("%s: bitsperidx=%d, ebits=%d", 2977 __func__, bitsperidx, (int)(sizeof(ebits) << 3))); 2978 idxmask = (UINT64_C(1) << bitsperidx) - 1; 2979 2980 /* Initialise the free list with randomised centroid indices. */ 2981 for (; remctds > 0; remctds--) { 2982 while (nebits < bitsperidx) { 2983 ebits |= ((uint64_t)random()) << nebits; 2984 nebits += bitsperrand; 2985 if (nebits > (sizeof(ebits) << 3)) 2986 nebits = sizeof(ebits) << 3; 2987 } 2988 idx = ebits & idxmask; 2989 nebits -= bitsperidx; 2990 ebits >>= bitsperidx; 2991 2992 /* 2993 * Select the next centroid to put on the ARB free list. We 2994 * start with the centroid at our randomly selected array index, 2995 * and work our way forwards until finding one (the latter 2996 * aspect reduces re-insertion randomness, but is good enough). 2997 */ 2998 do { 2999 if (idx >= maxctds) 3000 idx %= maxctds; 3001 3002 if (is32bit) 3003 ctd32 = ARB_NODE(ctd32tree, idx); 3004 else 3005 ctd64 = ARB_NODE(ctd64tree, idx); 3006 } while ((is32bit ? ARB_ISFREE(ctd32, ctdlnk) : 3007 ARB_ISFREE(ctd64, ctdlnk)) && ++idx); 3008 3009 /* Put the centroid on the ARB free list. */ 3010 if (is32bit) 3011 ARB_RETURNFREE(ctd32tree, ctd32, ctdlnk); 3012 else 3013 ARB_RETURNFREE(ctd64tree, ctd64, ctdlnk); 3014 } 3015 3016 /* 3017 * The free list now contains the randomised indices of every centroid. 3018 * Walk the free list from start to end, re-inserting each centroid's 3019 * mu/cnt. The tdgst_add() call may or may not consume the free centroid 3020 * we re-insert values from during each loop iteration, so we must latch 3021 * the index of the next free list centroid before the re-insertion 3022 * call. The previous loop above should have left the centroid pointer 3023 * pointing to the element at the head of the free list. 3024 */ 3025 KASSERT((is32bit ? 3026 ARB_FREEIDX(ctd32tree) == ARB_SELFIDX(ctd32tree, ctd32) : 3027 ARB_FREEIDX(ctd64tree) == ARB_SELFIDX(ctd64tree, ctd64)), 3028 ("%s: t-digest ARB@%p free list bug", __func__, 3029 (is32bit ? (void *)ctd32tree : (void *)ctd64tree))); 3030 remctds = maxctds; 3031 while ((is32bit ? ctd32 != NULL : ctd64 != NULL)) { 3032 tmperr = 0; 3033 if (is32bit) { 3034 s64q_t x; 3035 3036 idx = ARB_NEXTFREEIDX(ctd32, ctdlnk); 3037 /* Cloning a s32q_t into a s64q_t should never fail. */ 3038 tmperr = Q_QCLONEQ(&x, ctd32->mu); 3039 tmperr = tmperr ? tmperr : stats_v1_vsd_tdgst_add( 3040 vs_dtype, tdgst, x, ctd32->cnt, attempt); 3041 ctd32 = ARB_NODE(ctd32tree, idx); 3042 KASSERT(ctd32 == NULL || ARB_ISFREE(ctd32, ctdlnk), 3043 ("%s: t-digest ARB@%p free list bug", __func__, 3044 ctd32tree)); 3045 } else { 3046 idx = ARB_NEXTFREEIDX(ctd64, ctdlnk); 3047 tmperr = stats_v1_vsd_tdgst_add(vs_dtype, tdgst, 3048 ctd64->mu, ctd64->cnt, attempt); 3049 ctd64 = ARB_NODE(ctd64tree, idx); 3050 KASSERT(ctd64 == NULL || ARB_ISFREE(ctd64, ctdlnk), 3051 ("%s: t-digest ARB@%p free list bug", __func__, 3052 ctd64tree)); 3053 } 3054 /* 3055 * This process should not produce errors, bugs notwithstanding. 3056 * Just in case, latch any errors and attempt all re-insertions. 3057 */ 3058 error = tmperr ? tmperr : error; 3059 remctds--; 3060 } 3061 3062 KASSERT(remctds == 0, ("%s: t-digest ARB@%p free list bug", __func__, 3063 (is32bit ? (void *)ctd32tree : (void *)ctd64tree))); 3064 3065 return (error); 3066 } 3067 3068 static inline int 3069 stats_v1_vsd_tdgst_add(enum vsd_dtype vs_dtype, struct voistatdata_tdgst *tdgst, 3070 s64q_t x, uint64_t weight, int attempt) 3071 { 3072 #ifdef DIAGNOSTIC 3073 char qstr[Q_MAXSTRLEN(x, 10)]; 3074 #endif 3075 struct ctdth32 *ctd32tree; 3076 struct ctdth64 *ctd64tree; 3077 void *closest, *cur, *lb, *ub; 3078 struct voistatdata_tdgstctd32 *ctd32; 3079 struct voistatdata_tdgstctd64 *ctd64; 3080 uint64_t cnt, smplcnt, sum, tmpsum; 3081 s64q_t k, minz, q, z; 3082 int error, is32bit, n; 3083 3084 error = 0; 3085 minz = Q_INI(&z, 0, 0, Q_NFBITS(x)); 3086 3087 switch (vs_dtype) { 3088 case VSD_DTYPE_TDGSTCLUST32: 3089 if ((UINT32_MAX - weight) < VSD(tdgstclust32, tdgst)->smplcnt) 3090 error = EOVERFLOW; 3091 smplcnt = VSD(tdgstclust32, tdgst)->smplcnt; 3092 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree; 3093 is32bit = 1; 3094 ctd64tree = NULL; 3095 ctd64 = NULL; 3096 break; 3097 case VSD_DTYPE_TDGSTCLUST64: 3098 if ((UINT64_MAX - weight) < VSD(tdgstclust64, tdgst)->smplcnt) 3099 error = EOVERFLOW; 3100 smplcnt = VSD(tdgstclust64, tdgst)->smplcnt; 3101 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree; 3102 is32bit = 0; 3103 ctd32tree = NULL; 3104 ctd32 = NULL; 3105 break; 3106 default: 3107 error = EINVAL; 3108 break; 3109 } 3110 3111 if (error) 3112 return (error); 3113 3114 /* 3115 * Inspired by Ted Dunning's AVLTreeDigest.java 3116 */ 3117 do { 3118 #if defined(DIAGNOSTIC) 3119 KASSERT(attempt < 5, 3120 ("%s: Too many attempts", __func__)); 3121 #endif 3122 if (attempt >= 5) 3123 return (EAGAIN); 3124 3125 Q_SIFVAL(minz, Q_IFMAXVAL(minz)); 3126 closest = ub = NULL; 3127 sum = tmpsum = 0; 3128 3129 if (is32bit) 3130 lb = cur = (void *)(ctd32 = ARB_MIN(ctdth32, ctd32tree)); 3131 else 3132 lb = cur = (void *)(ctd64 = ARB_MIN(ctdth64, ctd64tree)); 3133 3134 if (lb == NULL) /* Empty tree. */ 3135 lb = (is32bit ? (void *)ARB_ROOT(ctd32tree) : 3136 (void *)ARB_ROOT(ctd64tree)); 3137 3138 /* 3139 * Find the set of centroids with minimum distance to x and 3140 * compute the sum of counts for all centroids with mean less 3141 * than the first centroid in the set. 3142 */ 3143 for (; cur != NULL; 3144 cur = (is32bit ? 3145 (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) : 3146 (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) { 3147 if (is32bit) { 3148 cnt = ctd32->cnt; 3149 KASSERT(Q_PRECEQ(ctd32->mu, x), 3150 ("%s: Q_RELPREC(mu,x)=%d", __func__, 3151 Q_RELPREC(ctd32->mu, x))); 3152 /* Ok to assign as both have same precision. */ 3153 z = ctd32->mu; 3154 } else { 3155 cnt = ctd64->cnt; 3156 KASSERT(Q_PRECEQ(ctd64->mu, x), 3157 ("%s: Q_RELPREC(mu,x)=%d", __func__, 3158 Q_RELPREC(ctd64->mu, x))); 3159 /* Ok to assign as both have same precision. */ 3160 z = ctd64->mu; 3161 } 3162 3163 error = Q_QSUBQ(&z, x); 3164 #if defined(DIAGNOSTIC) 3165 KASSERT(!error, ("%s: unexpected error %d", __func__, 3166 error)); 3167 #endif 3168 if (error) 3169 return (error); 3170 3171 z = Q_QABS(z); 3172 if (Q_QLTQ(z, minz)) { 3173 minz = z; 3174 lb = cur; 3175 sum = tmpsum; 3176 tmpsum += cnt; 3177 } else if (Q_QGTQ(z, minz)) { 3178 ub = cur; 3179 break; 3180 } 3181 } 3182 3183 cur = (is32bit ? 3184 (void *)(ctd32 = (struct voistatdata_tdgstctd32 *)lb) : 3185 (void *)(ctd64 = (struct voistatdata_tdgstctd64 *)lb)); 3186 3187 for (n = 0; cur != ub; cur = (is32bit ? 3188 (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) : 3189 (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) { 3190 if (is32bit) 3191 cnt = ctd32->cnt; 3192 else 3193 cnt = ctd64->cnt; 3194 3195 q = Q_CTRLINI(16); 3196 if (smplcnt == 1) 3197 error = Q_QFRACI(&q, 1, 2); 3198 else 3199 /* [ sum + ((cnt - 1) / 2) ] / (smplcnt - 1) */ 3200 error = Q_QFRACI(&q, (sum << 1) + cnt - 1, 3201 (smplcnt - 1) << 1); 3202 k = q; 3203 /* k = q x 4 x samplcnt x attempt */ 3204 error |= Q_QMULI(&k, 4 * smplcnt * attempt); 3205 /* k = k x (1 - q) */ 3206 error |= Q_QSUBI(&q, 1); 3207 q = Q_QABS(q); 3208 error |= Q_QMULQ(&k, q); 3209 #if defined(DIAGNOSTIC) 3210 #if !defined(_KERNEL) 3211 double q_dbl, k_dbl, q2d, k2d; 3212 q2d = Q_Q2D(q); 3213 k2d = Q_Q2D(k); 3214 q_dbl = smplcnt == 1 ? 0.5 : 3215 (sum + ((cnt - 1) / 2.0)) / (double)(smplcnt - 1); 3216 k_dbl = 4 * smplcnt * q_dbl * (1.0 - q_dbl) * attempt; 3217 /* 3218 * If the difference between q and q_dbl is greater than 3219 * the fractional precision of q, something is off. 3220 * NB: q is holding the value of 1 - q 3221 */ 3222 q_dbl = 1.0 - q_dbl; 3223 KASSERT((q_dbl > q2d ? q_dbl - q2d : q2d - q_dbl) < 3224 (1.05 * ((double)1 / (double)(1ULL << Q_NFBITS(q)))), 3225 ("Q-type q bad precision")); 3226 KASSERT((k_dbl > k2d ? k_dbl - k2d : k2d - k_dbl) < 3227 1.0 + (0.01 * smplcnt), 3228 ("Q-type k bad precision")); 3229 #endif /* !_KERNEL */ 3230 KASSERT(!error, ("%s: unexpected error %d", __func__, 3231 error)); 3232 #endif /* DIAGNOSTIC */ 3233 if (error) 3234 return (error); 3235 if ((is32bit && ((ctd32->cnt + weight) <= 3236 (uint64_t)Q_GIVAL(k))) || 3237 (!is32bit && ((ctd64->cnt + weight) <= 3238 (uint64_t)Q_GIVAL(k)))) { 3239 n++; 3240 /* random() produces 31 bits. */ 3241 if (random() < (INT32_MAX / n)) 3242 closest = cur; 3243 } 3244 sum += cnt; 3245 } 3246 } while (closest == NULL && 3247 (is32bit ? ARB_FULL(ctd32tree) : ARB_FULL(ctd64tree)) && 3248 (error = stats_v1_vsd_tdgst_compress(vs_dtype, tdgst, 3249 attempt++)) == 0); 3250 3251 if (error) 3252 return (error); 3253 3254 if (closest != NULL) { 3255 /* Merge with an existing centroid. */ 3256 if (is32bit) { 3257 ctd32 = (struct voistatdata_tdgstctd32 *)closest; 3258 error = Q_QSUBQ(&x, ctd32->mu); 3259 error = error ? error : 3260 Q_QDIVI(&x, ctd32->cnt + weight); 3261 if (error || (error = Q_QADDQ(&ctd32->mu, x))) { 3262 #ifdef DIAGNOSTIC 3263 KASSERT(!error, ("%s: unexpected error %d", 3264 __func__, error)); 3265 #endif 3266 return (error); 3267 } 3268 ctd32->cnt += weight; 3269 error = ARB_REINSERT(ctdth32, ctd32tree, ctd32) == 3270 NULL ? 0 : EALREADY; 3271 #ifdef DIAGNOSTIC 3272 RB_REINSERT(rbctdth32, 3273 &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32); 3274 #endif 3275 } else { 3276 ctd64 = (struct voistatdata_tdgstctd64 *)closest; 3277 error = Q_QSUBQ(&x, ctd64->mu); 3278 error = error ? error : 3279 Q_QDIVI(&x, ctd64->cnt + weight); 3280 if (error || (error = Q_QADDQ(&ctd64->mu, x))) { 3281 KASSERT(!error, ("%s: unexpected error %d", 3282 __func__, error)); 3283 return (error); 3284 } 3285 ctd64->cnt += weight; 3286 error = ARB_REINSERT(ctdth64, ctd64tree, ctd64) == 3287 NULL ? 0 : EALREADY; 3288 #ifdef DIAGNOSTIC 3289 RB_REINSERT(rbctdth64, 3290 &VSD(tdgstclust64, tdgst)->rbctdtree, ctd64); 3291 #endif 3292 } 3293 } else { 3294 /* 3295 * Add a new centroid. If digest compression is working 3296 * correctly, there should always be at least one free. 3297 */ 3298 if (is32bit) { 3299 ctd32 = ARB_GETFREE(ctd32tree, ctdlnk); 3300 #ifdef DIAGNOSTIC 3301 KASSERT(ctd32 != NULL, 3302 ("%s: t-digest@%p has no free centroids", 3303 __func__, tdgst)); 3304 #endif 3305 if (ctd32 == NULL) 3306 return (EAGAIN); 3307 if ((error = Q_QCPYVALQ(&ctd32->mu, x))) 3308 return (error); 3309 ctd32->cnt = weight; 3310 error = ARB_INSERT(ctdth32, ctd32tree, ctd32) == NULL ? 3311 0 : EALREADY; 3312 #ifdef DIAGNOSTIC 3313 RB_INSERT(rbctdth32, 3314 &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32); 3315 #endif 3316 } else { 3317 ctd64 = ARB_GETFREE(ctd64tree, ctdlnk); 3318 #ifdef DIAGNOSTIC 3319 KASSERT(ctd64 != NULL, 3320 ("%s: t-digest@%p has no free centroids", 3321 __func__, tdgst)); 3322 #endif 3323 if (ctd64 == NULL) /* Should not happen. */ 3324 return (EAGAIN); 3325 /* Direct assignment ok as both have same type/prec. */ 3326 ctd64->mu = x; 3327 ctd64->cnt = weight; 3328 error = ARB_INSERT(ctdth64, ctd64tree, ctd64) == NULL ? 3329 0 : EALREADY; 3330 #ifdef DIAGNOSTIC 3331 RB_INSERT(rbctdth64, &VSD(tdgstclust64, 3332 tdgst)->rbctdtree, ctd64); 3333 #endif 3334 } 3335 } 3336 3337 if (is32bit) 3338 VSD(tdgstclust32, tdgst)->smplcnt += weight; 3339 else { 3340 VSD(tdgstclust64, tdgst)->smplcnt += weight; 3341 3342 #ifdef DIAGNOSTIC 3343 struct rbctdth64 *rbctdtree = 3344 &VSD(tdgstclust64, tdgst)->rbctdtree; 3345 struct voistatdata_tdgstctd64 *rbctd64; 3346 int i = 0; 3347 ARB_FOREACH(ctd64, ctdth64, ctd64tree) { 3348 rbctd64 = (i == 0 ? RB_MIN(rbctdth64, rbctdtree) : 3349 RB_NEXT(rbctdth64, rbctdtree, rbctd64)); 3350 3351 if (i >= ARB_CURNODES(ctd64tree) 3352 || ctd64 != rbctd64 3353 || ARB_MIN(ctdth64, ctd64tree) != 3354 RB_MIN(rbctdth64, rbctdtree) 3355 || ARB_MAX(ctdth64, ctd64tree) != 3356 RB_MAX(rbctdth64, rbctdtree) 3357 || ARB_LEFTIDX(ctd64, ctdlnk) != 3358 ARB_SELFIDX(ctd64tree, RB_LEFT(rbctd64, rblnk)) 3359 || ARB_RIGHTIDX(ctd64, ctdlnk) != 3360 ARB_SELFIDX(ctd64tree, RB_RIGHT(rbctd64, rblnk)) 3361 || ARB_PARENTIDX(ctd64, ctdlnk) != 3362 ARB_SELFIDX(ctd64tree, 3363 RB_PARENT(rbctd64, rblnk))) { 3364 Q_TOSTR(ctd64->mu, -1, 10, qstr, sizeof(qstr)); 3365 printf("ARB ctd=%3d p=%3d l=%3d r=%3d c=%2d " 3366 "mu=%s\n", 3367 (int)ARB_SELFIDX(ctd64tree, ctd64), 3368 ARB_PARENTIDX(ctd64, ctdlnk), 3369 ARB_LEFTIDX(ctd64, ctdlnk), 3370 ARB_RIGHTIDX(ctd64, ctdlnk), 3371 ARB_COLOR(ctd64, ctdlnk), 3372 qstr); 3373 3374 Q_TOSTR(rbctd64->mu, -1, 10, qstr, 3375 sizeof(qstr)); 3376 printf(" RB ctd=%3d p=%3d l=%3d r=%3d c=%2d " 3377 "mu=%s\n", 3378 (int)ARB_SELFIDX(ctd64tree, rbctd64), 3379 (int)ARB_SELFIDX(ctd64tree, 3380 RB_PARENT(rbctd64, rblnk)), 3381 (int)ARB_SELFIDX(ctd64tree, 3382 RB_LEFT(rbctd64, rblnk)), 3383 (int)ARB_SELFIDX(ctd64tree, 3384 RB_RIGHT(rbctd64, rblnk)), 3385 RB_COLOR(rbctd64, rblnk), 3386 qstr); 3387 3388 panic("RB@%p and ARB@%p trees differ\n", 3389 rbctdtree, ctd64tree); 3390 } 3391 i++; 3392 } 3393 #endif /* DIAGNOSTIC */ 3394 } 3395 3396 return (error); 3397 } 3398 3399 static inline int 3400 stats_v1_voi_update_tdgst(enum vsd_dtype voi_dtype, struct voistatdata *voival, 3401 struct voistat *vs, struct voistatdata_tdgst *tdgst) 3402 { 3403 s64q_t x; 3404 int error; 3405 3406 error = 0; 3407 3408 switch (vs->dtype) { 3409 case VSD_DTYPE_TDGSTCLUST32: 3410 /* Use same precision as the user's centroids. */ 3411 Q_INI(&x, 0, 0, Q_NFBITS( 3412 ARB_CNODE(&VSD(tdgstclust32, tdgst)->ctdtree, 0)->mu)); 3413 break; 3414 case VSD_DTYPE_TDGSTCLUST64: 3415 /* Use same precision as the user's centroids. */ 3416 Q_INI(&x, 0, 0, Q_NFBITS( 3417 ARB_CNODE(&VSD(tdgstclust64, tdgst)->ctdtree, 0)->mu)); 3418 break; 3419 default: 3420 KASSERT(vs->dtype == VSD_DTYPE_TDGSTCLUST32 || 3421 vs->dtype == VSD_DTYPE_TDGSTCLUST64, 3422 ("%s: vs->dtype(%d) != VSD_DTYPE_TDGSTCLUST<32|64>", 3423 __func__, vs->dtype)); 3424 return (EINVAL); 3425 } 3426 3427 /* 3428 * XXXLAS: Should have both a signed and unsigned 'x' variable to avoid 3429 * returning EOVERFLOW if the voival would have fit in a u64q_t. 3430 */ 3431 switch (voi_dtype) { 3432 case VSD_DTYPE_INT_S32: 3433 error = Q_QCPYVALI(&x, voival->int32.s32); 3434 break; 3435 case VSD_DTYPE_INT_U32: 3436 error = Q_QCPYVALI(&x, voival->int32.u32); 3437 break; 3438 case VSD_DTYPE_INT_S64: 3439 error = Q_QCPYVALI(&x, voival->int64.s64); 3440 break; 3441 case VSD_DTYPE_INT_U64: 3442 error = Q_QCPYVALI(&x, voival->int64.u64); 3443 break; 3444 case VSD_DTYPE_INT_SLONG: 3445 error = Q_QCPYVALI(&x, voival->intlong.slong); 3446 break; 3447 case VSD_DTYPE_INT_ULONG: 3448 error = Q_QCPYVALI(&x, voival->intlong.ulong); 3449 break; 3450 case VSD_DTYPE_Q_S32: 3451 error = Q_QCPYVALQ(&x, voival->q32.sq32); 3452 break; 3453 case VSD_DTYPE_Q_U32: 3454 error = Q_QCPYVALQ(&x, voival->q32.uq32); 3455 break; 3456 case VSD_DTYPE_Q_S64: 3457 error = Q_QCPYVALQ(&x, voival->q64.sq64); 3458 break; 3459 case VSD_DTYPE_Q_U64: 3460 error = Q_QCPYVALQ(&x, voival->q64.uq64); 3461 break; 3462 default: 3463 error = EINVAL; 3464 break; 3465 } 3466 3467 if (error || 3468 (error = stats_v1_vsd_tdgst_add(vs->dtype, tdgst, x, 1, 1))) 3469 return (error); 3470 3471 vs->flags |= VS_VSDVALID; 3472 return (0); 3473 } 3474 3475 int 3476 stats_v1_voi_update(struct statsblobv1 *sb, int32_t voi_id, 3477 enum vsd_dtype voi_dtype, struct voistatdata *voival, uint32_t flags) 3478 { 3479 struct voi *v; 3480 struct voistat *vs; 3481 void *statevsd, *vsd; 3482 int error, i, tmperr; 3483 3484 error = 0; 3485 3486 if (sb == NULL || sb->abi != STATS_ABI_V1 || voi_id >= NVOIS(sb) || 3487 voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES || voival == NULL) 3488 return (EINVAL); 3489 v = &sb->vois[voi_id]; 3490 if (voi_dtype != v->dtype || v->id < 0 || 3491 ((flags & SB_VOI_RELUPDATE) && !(v->flags & VOI_REQSTATE))) 3492 return (EINVAL); 3493 3494 vs = BLOB_OFFSET(sb, v->stats_off); 3495 if (v->flags & VOI_REQSTATE) 3496 statevsd = BLOB_OFFSET(sb, vs->data_off); 3497 else 3498 statevsd = NULL; 3499 3500 if (flags & SB_VOI_RELUPDATE) { 3501 switch (voi_dtype) { 3502 case VSD_DTYPE_INT_S32: 3503 voival->int32.s32 += 3504 VSD(voistate, statevsd)->prev.int32.s32; 3505 break; 3506 case VSD_DTYPE_INT_U32: 3507 voival->int32.u32 += 3508 VSD(voistate, statevsd)->prev.int32.u32; 3509 break; 3510 case VSD_DTYPE_INT_S64: 3511 voival->int64.s64 += 3512 VSD(voistate, statevsd)->prev.int64.s64; 3513 break; 3514 case VSD_DTYPE_INT_U64: 3515 voival->int64.u64 += 3516 VSD(voistate, statevsd)->prev.int64.u64; 3517 break; 3518 case VSD_DTYPE_INT_SLONG: 3519 voival->intlong.slong += 3520 VSD(voistate, statevsd)->prev.intlong.slong; 3521 break; 3522 case VSD_DTYPE_INT_ULONG: 3523 voival->intlong.ulong += 3524 VSD(voistate, statevsd)->prev.intlong.ulong; 3525 break; 3526 case VSD_DTYPE_Q_S32: 3527 error = Q_QADDQ(&voival->q32.sq32, 3528 VSD(voistate, statevsd)->prev.q32.sq32); 3529 break; 3530 case VSD_DTYPE_Q_U32: 3531 error = Q_QADDQ(&voival->q32.uq32, 3532 VSD(voistate, statevsd)->prev.q32.uq32); 3533 break; 3534 case VSD_DTYPE_Q_S64: 3535 error = Q_QADDQ(&voival->q64.sq64, 3536 VSD(voistate, statevsd)->prev.q64.sq64); 3537 break; 3538 case VSD_DTYPE_Q_U64: 3539 error = Q_QADDQ(&voival->q64.uq64, 3540 VSD(voistate, statevsd)->prev.q64.uq64); 3541 break; 3542 default: 3543 KASSERT(0, ("Unknown VOI data type %d", voi_dtype)); 3544 break; 3545 } 3546 } 3547 3548 if (error) 3549 return (error); 3550 3551 for (i = v->voistatmaxid; i > 0; i--) { 3552 vs = &((struct voistat *)BLOB_OFFSET(sb, v->stats_off))[i]; 3553 if (vs->stype < 0) 3554 continue; 3555 3556 vsd = BLOB_OFFSET(sb, vs->data_off); 3557 3558 switch (vs->stype) { 3559 case VS_STYPE_MAX: 3560 tmperr = stats_v1_voi_update_max(voi_dtype, voival, 3561 vs, vsd); 3562 break; 3563 case VS_STYPE_MIN: 3564 tmperr = stats_v1_voi_update_min(voi_dtype, voival, 3565 vs, vsd); 3566 break; 3567 case VS_STYPE_SUM: 3568 tmperr = stats_v1_voi_update_sum(voi_dtype, voival, 3569 vs, vsd); 3570 break; 3571 case VS_STYPE_HIST: 3572 tmperr = stats_v1_voi_update_hist(voi_dtype, voival, 3573 vs, vsd); 3574 break; 3575 case VS_STYPE_TDGST: 3576 tmperr = stats_v1_voi_update_tdgst(voi_dtype, voival, 3577 vs, vsd); 3578 break; 3579 default: 3580 KASSERT(0, ("Unknown VOI stat type %d", vs->stype)); 3581 break; 3582 } 3583 3584 if (tmperr) { 3585 error = tmperr; 3586 VS_INCERRS(vs); 3587 } 3588 } 3589 3590 if (statevsd) { 3591 switch (voi_dtype) { 3592 case VSD_DTYPE_INT_S32: 3593 VSD(voistate, statevsd)->prev.int32.s32 = 3594 voival->int32.s32; 3595 break; 3596 case VSD_DTYPE_INT_U32: 3597 VSD(voistate, statevsd)->prev.int32.u32 = 3598 voival->int32.u32; 3599 break; 3600 case VSD_DTYPE_INT_S64: 3601 VSD(voistate, statevsd)->prev.int64.s64 = 3602 voival->int64.s64; 3603 break; 3604 case VSD_DTYPE_INT_U64: 3605 VSD(voistate, statevsd)->prev.int64.u64 = 3606 voival->int64.u64; 3607 break; 3608 case VSD_DTYPE_INT_SLONG: 3609 VSD(voistate, statevsd)->prev.intlong.slong = 3610 voival->intlong.slong; 3611 break; 3612 case VSD_DTYPE_INT_ULONG: 3613 VSD(voistate, statevsd)->prev.intlong.ulong = 3614 voival->intlong.ulong; 3615 break; 3616 case VSD_DTYPE_Q_S32: 3617 error = Q_QCPYVALQ( 3618 &VSD(voistate, statevsd)->prev.q32.sq32, 3619 voival->q32.sq32); 3620 break; 3621 case VSD_DTYPE_Q_U32: 3622 error = Q_QCPYVALQ( 3623 &VSD(voistate, statevsd)->prev.q32.uq32, 3624 voival->q32.uq32); 3625 break; 3626 case VSD_DTYPE_Q_S64: 3627 error = Q_QCPYVALQ( 3628 &VSD(voistate, statevsd)->prev.q64.sq64, 3629 voival->q64.sq64); 3630 break; 3631 case VSD_DTYPE_Q_U64: 3632 error = Q_QCPYVALQ( 3633 &VSD(voistate, statevsd)->prev.q64.uq64, 3634 voival->q64.uq64); 3635 break; 3636 default: 3637 KASSERT(0, ("Unknown VOI data type %d", voi_dtype)); 3638 break; 3639 } 3640 } 3641 3642 return (error); 3643 } 3644 3645 #ifdef _KERNEL 3646 3647 static void 3648 stats_init(void *arg) 3649 { 3650 3651 } 3652 SYSINIT(stats, SI_SUB_KDTRACE, SI_ORDER_FIRST, stats_init, NULL); 3653 3654 /* 3655 * Sysctl handler to display the list of available stats templates. 3656 */ 3657 static int 3658 stats_tpl_list_available(SYSCTL_HANDLER_ARGS) 3659 { 3660 struct sbuf *s; 3661 int err, i; 3662 3663 err = 0; 3664 3665 /* We can tolerate ntpl being stale, so do not take the lock. */ 3666 s = sbuf_new(NULL, NULL, /* +1 per tpl for , */ 3667 ntpl * (STATS_TPL_MAX_STR_SPEC_LEN + 1), SBUF_FIXEDLEN); 3668 if (s == NULL) 3669 return (ENOMEM); 3670 3671 TPL_LIST_RLOCK(); 3672 for (i = 0; i < ntpl; i++) { 3673 err = sbuf_printf(s, "%s\"%s\":%u", i ? "," : "", 3674 tpllist[i]->mb->tplname, tpllist[i]->mb->tplhash); 3675 if (err) { 3676 /* Sbuf overflow condition. */ 3677 err = EOVERFLOW; 3678 break; 3679 } 3680 } 3681 TPL_LIST_RUNLOCK(); 3682 3683 if (!err) { 3684 sbuf_finish(s); 3685 err = sysctl_handle_string(oidp, sbuf_data(s), 0, req); 3686 } 3687 3688 sbuf_delete(s); 3689 return (err); 3690 } 3691 3692 /* 3693 * Called by subsystem-specific sysctls to report and/or parse the list of 3694 * templates being sampled and their sampling rates. A stats_tpl_sr_cb_t 3695 * conformant function pointer must be passed in as arg1, which is used to 3696 * interact with the subsystem's stats template sample rates list. If arg2 > 0, 3697 * a zero-initialised allocation of arg2-sized contextual memory is 3698 * heap-allocated and passed in to all subsystem callbacks made during the 3699 * operation of stats_tpl_sample_rates(). 3700 * 3701 * XXXLAS: Assumes templates are never removed, which is currently true but may 3702 * need to be reworked in future if dynamic template management becomes a 3703 * requirement e.g. to support kernel module based templates. 3704 */ 3705 int 3706 stats_tpl_sample_rates(SYSCTL_HANDLER_ARGS) 3707 { 3708 char kvpair_fmt[16], tplspec_fmt[16]; 3709 char tpl_spec[STATS_TPL_MAX_STR_SPEC_LEN]; 3710 char tpl_name[TPL_MAX_NAME_LEN + 2]; /* +2 for "" */ 3711 stats_tpl_sr_cb_t subsys_cb; 3712 void *subsys_ctx; 3713 char *buf, *new_rates_usr_str, *tpl_name_p; 3714 struct stats_tpl_sample_rate *rates; 3715 struct sbuf *s, _s; 3716 uint32_t cum_pct, pct, tpl_hash; 3717 int err, i, off, len, newlen, nrates; 3718 3719 buf = NULL; 3720 rates = NULL; 3721 err = nrates = 0; 3722 subsys_cb = (stats_tpl_sr_cb_t)arg1; 3723 KASSERT(subsys_cb != NULL, ("%s: subsys_cb == arg1 == NULL", __func__)); 3724 if (arg2 > 0) 3725 subsys_ctx = malloc(arg2, M_TEMP, M_WAITOK | M_ZERO); 3726 else 3727 subsys_ctx = NULL; 3728 3729 /* Grab current count of subsystem rates. */ 3730 err = subsys_cb(TPL_SR_UNLOCKED_GET, NULL, &nrates, subsys_ctx); 3731 if (err) 3732 goto done; 3733 3734 /* +1 to ensure we can append '\0' post copyin, +5 per rate for =nnn, */ 3735 len = max(req->newlen + 1, nrates * (STATS_TPL_MAX_STR_SPEC_LEN + 5)); 3736 3737 if (req->oldptr != NULL || req->newptr != NULL) 3738 buf = malloc(len, M_TEMP, M_WAITOK); 3739 3740 if (req->oldptr != NULL) { 3741 if (nrates == 0) { 3742 /* No rates, so return an empty string via oldptr. */ 3743 err = SYSCTL_OUT(req, "", 1); 3744 if (err) 3745 goto done; 3746 goto process_new; 3747 } 3748 3749 s = sbuf_new(&_s, buf, len, SBUF_FIXEDLEN | SBUF_INCLUDENUL); 3750 3751 /* Grab locked count of, and ptr to, subsystem rates. */ 3752 err = subsys_cb(TPL_SR_RLOCKED_GET, &rates, &nrates, 3753 subsys_ctx); 3754 if (err) 3755 goto done; 3756 TPL_LIST_RLOCK(); 3757 for (i = 0; i < nrates && !err; i++) { 3758 err = sbuf_printf(s, "%s\"%s\":%u=%u", i ? "," : "", 3759 tpllist[rates[i].tpl_slot_id]->mb->tplname, 3760 tpllist[rates[i].tpl_slot_id]->mb->tplhash, 3761 rates[i].tpl_sample_pct); 3762 } 3763 TPL_LIST_RUNLOCK(); 3764 /* Tell subsystem that we're done with its rates list. */ 3765 err = subsys_cb(TPL_SR_RUNLOCK, &rates, &nrates, subsys_ctx); 3766 if (err) 3767 goto done; 3768 3769 err = sbuf_finish(s); 3770 if (err) 3771 goto done; /* We lost a race for buf to be too small. */ 3772 3773 /* Return the rendered string data via oldptr. */ 3774 err = SYSCTL_OUT(req, sbuf_data(s), sbuf_len(s)); 3775 } else { 3776 /* Return the upper bound size for buffer sizing requests. */ 3777 err = SYSCTL_OUT(req, NULL, len); 3778 } 3779 3780 process_new: 3781 if (err || req->newptr == NULL) 3782 goto done; 3783 3784 newlen = req->newlen - req->newidx; 3785 err = SYSCTL_IN(req, buf, newlen); 3786 if (err) 3787 goto done; 3788 3789 /* 3790 * Initialise format strings at run time. 3791 * 3792 * Write the max template spec string length into the 3793 * template_spec=percent key-value pair parsing format string as: 3794 * " %<width>[^=]=%u %n" 3795 * 3796 * Write the max template name string length into the tplname:tplhash 3797 * parsing format string as: 3798 * "%<width>[^:]:%u" 3799 * 3800 * Subtract 1 for \0 appended by sscanf(). 3801 */ 3802 sprintf(kvpair_fmt, " %%%zu[^=]=%%u %%n", sizeof(tpl_spec) - 1); 3803 sprintf(tplspec_fmt, "%%%zu[^:]:%%u", sizeof(tpl_name) - 1); 3804 3805 /* 3806 * Parse each CSV key-value pair specifying a template and its sample 3807 * percentage. Whitespace either side of a key-value pair is ignored. 3808 * Templates can be specified by name, hash, or name and hash per the 3809 * following formats (chars in [] are optional): 3810 * ["]<tplname>["]=<percent> 3811 * :hash=pct 3812 * ["]<tplname>["]:hash=<percent> 3813 */ 3814 cum_pct = nrates = 0; 3815 rates = NULL; 3816 buf[newlen] = '\0'; /* buf is at least newlen+1 in size. */ 3817 new_rates_usr_str = buf; 3818 while (isspace(*new_rates_usr_str)) 3819 new_rates_usr_str++; /* Skip leading whitespace. */ 3820 while (*new_rates_usr_str != '\0') { 3821 tpl_name_p = tpl_name; 3822 tpl_name[0] = '\0'; 3823 tpl_hash = 0; 3824 off = 0; 3825 3826 /* 3827 * Parse key-value pair which must perform 2 conversions, then 3828 * parse the template spec to extract either name, hash, or name 3829 * and hash depending on the three possible spec formats. The 3830 * tplspec_fmt format specifier parses name or name and hash 3831 * template specs, while the ":%u" format specifier parses 3832 * hash-only template specs. If parsing is successfull, ensure 3833 * the cumulative sampling percentage does not exceed 100. 3834 */ 3835 err = EINVAL; 3836 if (2 != sscanf(new_rates_usr_str, kvpair_fmt, tpl_spec, &pct, 3837 &off)) 3838 break; 3839 if ((1 > sscanf(tpl_spec, tplspec_fmt, tpl_name, &tpl_hash)) && 3840 (1 != sscanf(tpl_spec, ":%u", &tpl_hash))) 3841 break; 3842 if ((cum_pct += pct) > 100) 3843 break; 3844 err = 0; 3845 3846 /* Strip surrounding "" from template name if present. */ 3847 len = strlen(tpl_name); 3848 if (len > 0) { 3849 if (tpl_name[len - 1] == '"') 3850 tpl_name[--len] = '\0'; 3851 if (tpl_name[0] == '"') { 3852 tpl_name_p++; 3853 len--; 3854 } 3855 } 3856 3857 rates = stats_realloc(rates, 0, /* oldsz is unused in kernel. */ 3858 (nrates + 1) * sizeof(*rates), M_WAITOK); 3859 rates[nrates].tpl_slot_id = 3860 stats_tpl_fetch_allocid(len ? tpl_name_p : NULL, tpl_hash); 3861 if (rates[nrates].tpl_slot_id < 0) { 3862 err = -rates[nrates].tpl_slot_id; 3863 break; 3864 } 3865 rates[nrates].tpl_sample_pct = pct; 3866 nrates++; 3867 new_rates_usr_str += off; 3868 if (*new_rates_usr_str != ',') 3869 break; /* End-of-input or malformed. */ 3870 new_rates_usr_str++; /* Move past comma to next pair. */ 3871 } 3872 3873 if (!err) { 3874 if ((new_rates_usr_str - buf) < newlen) { 3875 /* Entire input has not been consumed. */ 3876 err = EINVAL; 3877 } else { 3878 /* 3879 * Give subsystem the new rates. They'll return the 3880 * appropriate rates pointer for us to garbage collect. 3881 */ 3882 err = subsys_cb(TPL_SR_PUT, &rates, &nrates, 3883 subsys_ctx); 3884 } 3885 } 3886 stats_free(rates); 3887 3888 done: 3889 free(buf, M_TEMP); 3890 free(subsys_ctx, M_TEMP); 3891 return (err); 3892 } 3893 3894 SYSCTL_NODE(_kern, OID_AUTO, stats, CTLFLAG_RW, NULL, 3895 "stats(9) MIB"); 3896 3897 SYSCTL_PROC(_kern_stats, OID_AUTO, templates, CTLTYPE_STRING|CTLFLAG_RD, 3898 NULL, 0, stats_tpl_list_available, "A", 3899 "list the name/hash of all available stats(9) templates"); 3900 3901 #else /* ! _KERNEL */ 3902 3903 static void __attribute__ ((constructor)) 3904 stats_constructor(void) 3905 { 3906 3907 pthread_rwlock_init(&tpllistlock, NULL); 3908 } 3909 3910 static void __attribute__ ((destructor)) 3911 stats_destructor(void) 3912 { 3913 3914 pthread_rwlock_destroy(&tpllistlock); 3915 } 3916 3917 #endif /* _KERNEL */ 3918