2 * Copyright (c) 2014-2018 Netflix, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Author: Lawrence Stewart <lstewart@netflix.com>
31 #include <sys/param.h>
33 #include <sys/ctype.h>
34 #include <sys/errno.h>
36 #include <sys/limits.h>
37 #include <sys/malloc.h>
38 #include <sys/qmath.h>
40 #if defined(DIAGNOSTIC)
43 #include <sys/stats.h> /* Must come after qmath.h and arb.h */
44 #include <sys/stddef.h>
45 #include <sys/stdint.h>
49 #include <sys/kernel.h>
51 #include <sys/rwlock.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
62 struct voistatdata_voistate {
63 /* Previous VOI value for diff calculation. */
64 struct voistatdata_numeric prev;
67 #define VS_VSDVALID 0x0001 /* Stat's voistatdata updated at least once. */
69 int8_t stype; /* Type of stat e.g. VS_STYPE_SUM. */
70 enum vsd_dtype dtype : 8; /* Data type of this stat's data. */
71 uint16_t data_off; /* Blob offset for this stat's data. */
72 uint16_t dsz; /* Size of stat's data. */
74 uint16_t errs : VS_EBITS;/* Non-wrapping error count. */
75 uint16_t flags : 16 - VS_EBITS;
77 /* The voistat error count is capped to avoid wrapping. */
78 #define VS_INCERRS(vs) do { \
79 if ((vs)->errs < (1U << VS_EBITS) - 1) \
85 * - Global or entity specific (global would imply use of counter(9)?)
86 * - Whether to reset stats on read or not
87 * - Signal an overflow?
88 * - Compressed voistat array
90 #define VOI_REQSTATE 0x0001 /* VOI requires VS_STYPE_VOISTATE. */
92 int16_t id; /* VOI id. */
93 enum vsd_dtype dtype : 8; /* Data type of the VOI itself. */
94 int8_t voistatmaxid; /* Largest allocated voistat index. */
95 uint16_t stats_off; /* Blob offset for this VOIs stats. */
100 * Memory for the entire blob is allocated as a slab and then offsets are
101 * maintained to carve up the slab into sections holding different data types.
104 * - Compressed voi array (trade off memory usage vs search time)
105 * - Units of offsets (default bytes, flag for e.g. vm_page/KiB/Mib)
113 /* Fields from here down are opaque to consumers. */
114 uint32_t tplhash; /* Base template hash ID. */
115 uint16_t stats_off; /* voistat array blob offset. */
116 uint16_t statsdata_off; /* voistatdata array blob offset. */
117 sbintime_t created; /* Blob creation time. */
118 sbintime_t lastrst; /* Time of last reset. */
119 struct voi vois[]; /* Array indexed by [voi_id]. */
120 } __aligned(sizeof(void *));
121 _Static_assert(offsetof(struct statsblobv1, cursz) +
122 SIZEOF_MEMBER(struct statsblobv1, cursz) ==
123 offsetof(struct statsblob, opaque),
124 "statsblobv1 ABI mismatch");
126 struct statsblobv1_tpl {
128 struct statsblobv1 *sb;
131 /* Context passed to iterator callbacks. */
133 void *usrctx; /* Caller supplied context. */
134 uint32_t flags; /* Flags for current iteration. */
135 int16_t vslot; /* struct voi slot index. */
136 int8_t vsslot; /* struct voistat slot index. */
139 struct sb_tostrcb_ctx {
141 struct statsblob_tpl *tpl;
146 struct sb_visitcb_ctx {
147 stats_blob_visitcb_t cb;
151 /* Stats blob iterator callback. */
152 typedef int (*stats_v1_blob_itercb_t)(struct statsblobv1 *sb, struct voi *v,
153 struct voistat *vs, struct sb_iter_ctx *ctx);
156 static struct rwlock tpllistlock;
157 RW_SYSINIT(stats_tpl_list, &tpllistlock, "Stat template list lock");
158 #define TPL_LIST_RLOCK() rw_rlock(&tpllistlock)
159 #define TPL_LIST_RUNLOCK() rw_runlock(&tpllistlock)
160 #define TPL_LIST_WLOCK() rw_wlock(&tpllistlock)
161 #define TPL_LIST_WUNLOCK() rw_wunlock(&tpllistlock)
162 #define TPL_LIST_LOCK_ASSERT() rw_assert(&tpllistlock, RA_LOCKED)
163 #define TPL_LIST_RLOCK_ASSERT() rw_assert(&tpllistlock, RA_RLOCKED)
164 #define TPL_LIST_WLOCK_ASSERT() rw_assert(&tpllistlock, RA_WLOCKED)
165 MALLOC_DEFINE(M_STATS, "stats(9) related memory", "stats(9) related memory");
166 #define stats_free(ptr) free((ptr), M_STATS)
167 #else /* ! _KERNEL */
168 static void stats_constructor(void);
169 static void stats_destructor(void);
170 static pthread_rwlock_t tpllistlock;
171 #define TPL_LIST_UNLOCK() pthread_rwlock_unlock(&tpllistlock)
172 #define TPL_LIST_RLOCK() pthread_rwlock_rdlock(&tpllistlock)
173 #define TPL_LIST_RUNLOCK() TPL_LIST_UNLOCK()
174 #define TPL_LIST_WLOCK() pthread_rwlock_wrlock(&tpllistlock)
175 #define TPL_LIST_WUNLOCK() TPL_LIST_UNLOCK()
176 #define TPL_LIST_LOCK_ASSERT() do { } while (0)
177 #define TPL_LIST_RLOCK_ASSERT() do { } while (0)
178 #define TPL_LIST_WLOCK_ASSERT() do { } while (0)
180 #define KASSERT(cond, msg) do {} while (0)
181 #define stats_abort() do {} while (0)
183 #define KASSERT(cond, msg) do { \
188 #define stats_abort() abort()
190 #define stats_free(ptr) free(ptr)
191 #define panic(fmt, ...) do { \
192 fprintf(stderr, (fmt), ##__VA_ARGS__); \
197 #define SB_V1_MAXSZ 65535
199 /* Obtain a blob offset pointer. */
200 #define BLOB_OFFSET(sb, off) ((void *)(((uint8_t *)(sb)) + (off)))
203 * Number of VOIs in the blob's vois[] array. By virtue of struct voi being a
204 * power of 2 size, we can shift instead of divide. The shift amount must be
205 * updated if sizeof(struct voi) ever changes, which the assert should catch.
207 #define NVOIS(sb) ((int32_t)((((struct statsblobv1 *)(sb))->stats_off - \
208 sizeof(struct statsblobv1)) >> 3))
209 _Static_assert(sizeof(struct voi) == 8, "statsblobv1 voi ABI mismatch");
211 /* Try restrict names to alphanumeric and underscore to simplify JSON compat. */
212 const char *vs_stype2name[VS_NUM_STYPES] = {
213 [VS_STYPE_VOISTATE] = "VOISTATE",
214 [VS_STYPE_SUM] = "SUM",
215 [VS_STYPE_MAX] = "MAX",
216 [VS_STYPE_MIN] = "MIN",
217 [VS_STYPE_HIST] = "HIST",
218 [VS_STYPE_TDGST] = "TDGST",
221 const char *vs_stype2desc[VS_NUM_STYPES] = {
222 [VS_STYPE_VOISTATE] = "VOI related state data (not a real stat)",
223 [VS_STYPE_SUM] = "Simple arithmetic accumulator",
224 [VS_STYPE_MAX] = "Maximum observed VOI value",
225 [VS_STYPE_MIN] = "Minimum observed VOI value",
226 [VS_STYPE_HIST] = "Histogram of observed VOI values",
227 [VS_STYPE_TDGST] = "t-digest of observed VOI values",
230 const char *vsd_dtype2name[VSD_NUM_DTYPES] = {
231 [VSD_DTYPE_VOISTATE] = "VOISTATE",
232 [VSD_DTYPE_INT_S32] = "INT_S32",
233 [VSD_DTYPE_INT_U32] = "INT_U32",
234 [VSD_DTYPE_INT_S64] = "INT_S64",
235 [VSD_DTYPE_INT_U64] = "INT_U64",
236 [VSD_DTYPE_INT_SLONG] = "INT_SLONG",
237 [VSD_DTYPE_INT_ULONG] = "INT_ULONG",
238 [VSD_DTYPE_Q_S32] = "Q_S32",
239 [VSD_DTYPE_Q_U32] = "Q_U32",
240 [VSD_DTYPE_Q_S64] = "Q_S64",
241 [VSD_DTYPE_Q_U64] = "Q_U64",
242 [VSD_DTYPE_CRHIST32] = "CRHIST32",
243 [VSD_DTYPE_DRHIST32] = "DRHIST32",
244 [VSD_DTYPE_DVHIST32] = "DVHIST32",
245 [VSD_DTYPE_CRHIST64] = "CRHIST64",
246 [VSD_DTYPE_DRHIST64] = "DRHIST64",
247 [VSD_DTYPE_DVHIST64] = "DVHIST64",
248 [VSD_DTYPE_TDGSTCLUST32] = "TDGSTCLUST32",
249 [VSD_DTYPE_TDGSTCLUST64] = "TDGSTCLUST64",
252 const size_t vsd_dtype2size[VSD_NUM_DTYPES] = {
253 [VSD_DTYPE_VOISTATE] = sizeof(struct voistatdata_voistate),
254 [VSD_DTYPE_INT_S32] = sizeof(struct voistatdata_int32),
255 [VSD_DTYPE_INT_U32] = sizeof(struct voistatdata_int32),
256 [VSD_DTYPE_INT_S64] = sizeof(struct voistatdata_int64),
257 [VSD_DTYPE_INT_U64] = sizeof(struct voistatdata_int64),
258 [VSD_DTYPE_INT_SLONG] = sizeof(struct voistatdata_intlong),
259 [VSD_DTYPE_INT_ULONG] = sizeof(struct voistatdata_intlong),
260 [VSD_DTYPE_Q_S32] = sizeof(struct voistatdata_q32),
261 [VSD_DTYPE_Q_U32] = sizeof(struct voistatdata_q32),
262 [VSD_DTYPE_Q_S64] = sizeof(struct voistatdata_q64),
263 [VSD_DTYPE_Q_U64] = sizeof(struct voistatdata_q64),
264 [VSD_DTYPE_CRHIST32] = sizeof(struct voistatdata_crhist32),
265 [VSD_DTYPE_DRHIST32] = sizeof(struct voistatdata_drhist32),
266 [VSD_DTYPE_DVHIST32] = sizeof(struct voistatdata_dvhist32),
267 [VSD_DTYPE_CRHIST64] = sizeof(struct voistatdata_crhist64),
268 [VSD_DTYPE_DRHIST64] = sizeof(struct voistatdata_drhist64),
269 [VSD_DTYPE_DVHIST64] = sizeof(struct voistatdata_dvhist64),
270 [VSD_DTYPE_TDGSTCLUST32] = sizeof(struct voistatdata_tdgstclust32),
271 [VSD_DTYPE_TDGSTCLUST64] = sizeof(struct voistatdata_tdgstclust64),
274 static const bool vsd_compoundtype[VSD_NUM_DTYPES] = {
275 [VSD_DTYPE_VOISTATE] = true,
276 [VSD_DTYPE_INT_S32] = false,
277 [VSD_DTYPE_INT_U32] = false,
278 [VSD_DTYPE_INT_S64] = false,
279 [VSD_DTYPE_INT_U64] = false,
280 [VSD_DTYPE_INT_SLONG] = false,
281 [VSD_DTYPE_INT_ULONG] = false,
282 [VSD_DTYPE_Q_S32] = false,
283 [VSD_DTYPE_Q_U32] = false,
284 [VSD_DTYPE_Q_S64] = false,
285 [VSD_DTYPE_Q_U64] = false,
286 [VSD_DTYPE_CRHIST32] = true,
287 [VSD_DTYPE_DRHIST32] = true,
288 [VSD_DTYPE_DVHIST32] = true,
289 [VSD_DTYPE_CRHIST64] = true,
290 [VSD_DTYPE_DRHIST64] = true,
291 [VSD_DTYPE_DVHIST64] = true,
292 [VSD_DTYPE_TDGSTCLUST32] = true,
293 [VSD_DTYPE_TDGSTCLUST64] = true,
296 const struct voistatdata_numeric numeric_limits[2][VSD_DTYPE_Q_U64 + 1] = {
298 [VSD_DTYPE_VOISTATE] = {0},
299 [VSD_DTYPE_INT_S32] = {.int32 = {.s32 = INT32_MIN}},
300 [VSD_DTYPE_INT_U32] = {.int32 = {.u32 = 0}},
301 [VSD_DTYPE_INT_S64] = {.int64 = {.s64 = INT64_MIN}},
302 [VSD_DTYPE_INT_U64] = {.int64 = {.u64 = 0}},
303 [VSD_DTYPE_INT_SLONG] = {.intlong = {.slong = LONG_MIN}},
304 [VSD_DTYPE_INT_ULONG] = {.intlong = {.ulong = 0}},
305 [VSD_DTYPE_Q_S32] = {.q32 = {.sq32 = Q_IFMINVAL(INT32_MIN)}},
306 [VSD_DTYPE_Q_U32] = {.q32 = {.uq32 = 0}},
307 [VSD_DTYPE_Q_S64] = {.q64 = {.sq64 = Q_IFMINVAL(INT64_MIN)}},
308 [VSD_DTYPE_Q_U64] = {.q64 = {.uq64 = 0}},
311 [VSD_DTYPE_VOISTATE] = {0},
312 [VSD_DTYPE_INT_S32] = {.int32 = {.s32 = INT32_MAX}},
313 [VSD_DTYPE_INT_U32] = {.int32 = {.u32 = UINT32_MAX}},
314 [VSD_DTYPE_INT_S64] = {.int64 = {.s64 = INT64_MAX}},
315 [VSD_DTYPE_INT_U64] = {.int64 = {.u64 = UINT64_MAX}},
316 [VSD_DTYPE_INT_SLONG] = {.intlong = {.slong = LONG_MAX}},
317 [VSD_DTYPE_INT_ULONG] = {.intlong = {.ulong = ULONG_MAX}},
318 [VSD_DTYPE_Q_S32] = {.q32 = {.sq32 = Q_IFMAXVAL(INT32_MAX)}},
319 [VSD_DTYPE_Q_U32] = {.q32 = {.uq32 = Q_IFMAXVAL(UINT32_MAX)}},
320 [VSD_DTYPE_Q_S64] = {.q64 = {.sq64 = Q_IFMAXVAL(INT64_MAX)}},
321 [VSD_DTYPE_Q_U64] = {.q64 = {.uq64 = Q_IFMAXVAL(UINT64_MAX)}},
325 /* tpllistlock protects tpllist and ntpl */
326 static uint32_t ntpl;
327 static struct statsblob_tpl **tpllist;
329 static inline void * stats_realloc(void *ptr, size_t oldsz, size_t newsz,
331 //static void stats_v1_blob_finalise(struct statsblobv1 *sb);
332 static int stats_v1_blob_init_locked(struct statsblobv1 *sb, uint32_t tpl_id,
334 static int stats_v1_blob_expand(struct statsblobv1 **sbpp, int newvoibytes,
335 int newvoistatbytes, int newvoistatdatabytes);
336 static void stats_v1_blob_iter(struct statsblobv1 *sb,
337 stats_v1_blob_itercb_t icb, void *usrctx, uint32_t flags);
338 static inline int stats_v1_vsd_tdgst_add(enum vsd_dtype vs_dtype,
339 struct voistatdata_tdgst *tdgst, s64q_t x, uint64_t weight, int attempt);
342 ctd32cmp(const struct voistatdata_tdgstctd32 *c1, const struct voistatdata_tdgstctd32 *c2)
345 KASSERT(Q_PRECEQ(c1->mu, c2->mu),
346 ("%s: Q_RELPREC(c1->mu,c2->mu)=%d", __func__,
347 Q_RELPREC(c1->mu, c2->mu)));
349 return (Q_QLTQ(c1->mu, c2->mu) ? -1 : 1);
351 ARB_GENERATE_STATIC(ctdth32, voistatdata_tdgstctd32, ctdlnk, ctd32cmp);
354 ctd64cmp(const struct voistatdata_tdgstctd64 *c1, const struct voistatdata_tdgstctd64 *c2)
357 KASSERT(Q_PRECEQ(c1->mu, c2->mu),
358 ("%s: Q_RELPREC(c1->mu,c2->mu)=%d", __func__,
359 Q_RELPREC(c1->mu, c2->mu)));
361 return (Q_QLTQ(c1->mu, c2->mu) ? -1 : 1);
363 ARB_GENERATE_STATIC(ctdth64, voistatdata_tdgstctd64, ctdlnk, ctd64cmp);
366 RB_GENERATE_STATIC(rbctdth32, voistatdata_tdgstctd32, rblnk, ctd32cmp);
367 RB_GENERATE_STATIC(rbctdth64, voistatdata_tdgstctd64, rblnk, ctd64cmp);
370 static inline sbintime_t
371 stats_sbinuptime(void)
377 #else /* ! _KERNEL */
380 clock_gettime(CLOCK_MONOTONIC_FAST, &tp);
388 stats_realloc(void *ptr, size_t oldsz, size_t newsz, int flags)
392 /* Default to M_NOWAIT if neither M_NOWAIT or M_WAITOK are set. */
393 if (!(flags & (M_WAITOK | M_NOWAIT)))
395 ptr = realloc(ptr, newsz, M_STATS, flags);
396 #else /* ! _KERNEL */
397 ptr = realloc(ptr, newsz);
398 if ((flags & M_ZERO) && ptr != NULL) {
400 memset(ptr, '\0', newsz);
401 else if (newsz > oldsz)
402 memset(BLOB_OFFSET(ptr, oldsz), '\0', newsz - oldsz);
410 stats_strdup(const char *s,
417 if (!(flags & (M_WAITOK | M_NOWAIT)))
421 if ((copy = malloc(len, M_STATS, flags)) != NULL)
433 stats_tpl_update_hash(struct statsblob_tpl *tpl)
436 TPL_LIST_WLOCK_ASSERT();
437 tpl->mb->tplhash = hash32_str(tpl->mb->tplname, 0);
438 for (int voi_id = 0; voi_id < NVOIS(tpl->sb); voi_id++) {
439 if (tpl->mb->voi_meta[voi_id].name != NULL)
440 tpl->mb->tplhash = hash32_str(
441 tpl->mb->voi_meta[voi_id].name, tpl->mb->tplhash);
443 tpl->mb->tplhash = hash32_buf(tpl->sb, tpl->sb->cursz,
447 static inline uint64_t
448 stats_pow_u64(uint64_t base, uint64_t exp)
463 stats_vss_hist_bkt_hlpr(struct vss_hist_hlpr_info *info, uint32_t curbkt,
464 struct voistatdata_numeric *bkt_lb, struct voistatdata_numeric *bkt_ub)
469 switch (info->scheme) {
471 step = info->lin.stepinc;
474 step = stats_pow_u64(info->exp.stepbase,
475 info->exp.stepexp + curbkt);
479 uint64_t curstepexp = 1;
481 switch (info->voi_dtype) {
482 case VSD_DTYPE_INT_S32:
483 while ((int32_t)stats_pow_u64(info->linexp.stepbase,
484 curstepexp) <= bkt_lb->int32.s32)
487 case VSD_DTYPE_INT_U32:
488 while ((uint32_t)stats_pow_u64(info->linexp.stepbase,
489 curstepexp) <= bkt_lb->int32.u32)
492 case VSD_DTYPE_INT_S64:
493 while ((int64_t)stats_pow_u64(info->linexp.stepbase,
494 curstepexp) <= bkt_lb->int64.s64)
497 case VSD_DTYPE_INT_U64:
498 while ((uint64_t)stats_pow_u64(info->linexp.stepbase,
499 curstepexp) <= bkt_lb->int64.u64)
502 case VSD_DTYPE_INT_SLONG:
503 while ((long)stats_pow_u64(info->linexp.stepbase,
504 curstepexp) <= bkt_lb->intlong.slong)
507 case VSD_DTYPE_INT_ULONG:
508 while ((unsigned long)stats_pow_u64(info->linexp.stepbase,
509 curstepexp) <= bkt_lb->intlong.ulong)
512 case VSD_DTYPE_Q_S32:
513 while ((s32q_t)stats_pow_u64(info->linexp.stepbase,
514 curstepexp) <= Q_GIVAL(bkt_lb->q32.sq32))
516 case VSD_DTYPE_Q_U32:
517 while ((u32q_t)stats_pow_u64(info->linexp.stepbase,
518 curstepexp) <= Q_GIVAL(bkt_lb->q32.uq32))
520 case VSD_DTYPE_Q_S64:
521 while ((s64q_t)stats_pow_u64(info->linexp.stepbase,
522 curstepexp) <= Q_GIVAL(bkt_lb->q64.sq64))
525 case VSD_DTYPE_Q_U64:
526 while ((u64q_t)stats_pow_u64(info->linexp.stepbase,
527 curstepexp) <= Q_GIVAL(bkt_lb->q64.uq64))
534 step = stats_pow_u64(info->linexp.stepbase, curstepexp) /
535 info->linexp.linstepdiv;
544 if (info->scheme == BKT_USR) {
545 *bkt_lb = info->usr.bkts[curbkt].lb;
546 *bkt_ub = info->usr.bkts[curbkt].ub;
547 } else if (step != 0) {
548 switch (info->voi_dtype) {
549 case VSD_DTYPE_INT_S32:
550 bkt_ub->int32.s32 += (int32_t)step;
552 case VSD_DTYPE_INT_U32:
553 bkt_ub->int32.u32 += (uint32_t)step;
555 case VSD_DTYPE_INT_S64:
556 bkt_ub->int64.s64 += (int64_t)step;
558 case VSD_DTYPE_INT_U64:
559 bkt_ub->int64.u64 += (uint64_t)step;
561 case VSD_DTYPE_INT_SLONG:
562 bkt_ub->intlong.slong += (long)step;
564 case VSD_DTYPE_INT_ULONG:
565 bkt_ub->intlong.ulong += (unsigned long)step;
567 case VSD_DTYPE_Q_S32:
568 error = Q_QADDI(&bkt_ub->q32.sq32, step);
570 case VSD_DTYPE_Q_U32:
571 error = Q_QADDI(&bkt_ub->q32.uq32, step);
573 case VSD_DTYPE_Q_S64:
574 error = Q_QADDI(&bkt_ub->q64.sq64, step);
576 case VSD_DTYPE_Q_U64:
577 error = Q_QADDI(&bkt_ub->q64.uq64, step);
582 } else { /* info->scheme != BKT_USR && step == 0 */
590 stats_vss_hist_nbkts_hlpr(struct vss_hist_hlpr_info *info)
592 struct voistatdata_numeric bkt_lb, bkt_ub;
596 if (info->scheme == BKT_USR) {
597 /* XXXLAS: Setting info->{lb,ub} from macro is tricky. */
598 info->lb = info->usr.bkts[0].lb;
599 info->ub = info->usr.bkts[info->usr.nbkts - 1].lb;
608 if (stats_vss_hist_bkt_hlpr(info, nbkts++, &bkt_lb, &bkt_ub))
611 if (info->scheme == BKT_USR)
612 done = (nbkts == info->usr.nbkts);
614 switch (info->voi_dtype) {
615 case VSD_DTYPE_INT_S32:
616 done = (bkt_ub.int32.s32 > info->ub.int32.s32);
618 case VSD_DTYPE_INT_U32:
619 done = (bkt_ub.int32.u32 > info->ub.int32.u32);
621 case VSD_DTYPE_INT_S64:
622 done = (bkt_ub.int64.s64 > info->ub.int64.s64);
624 case VSD_DTYPE_INT_U64:
625 done = (bkt_ub.int64.u64 > info->ub.int64.u64);
627 case VSD_DTYPE_INT_SLONG:
628 done = (bkt_ub.intlong.slong >
629 info->ub.intlong.slong);
631 case VSD_DTYPE_INT_ULONG:
632 done = (bkt_ub.intlong.ulong >
633 info->ub.intlong.ulong);
635 case VSD_DTYPE_Q_S32:
636 done = Q_QGTQ(bkt_ub.q32.sq32,
639 case VSD_DTYPE_Q_U32:
640 done = Q_QGTQ(bkt_ub.q32.uq32,
643 case VSD_DTYPE_Q_S64:
644 done = Q_QGTQ(bkt_ub.q64.sq64,
647 case VSD_DTYPE_Q_U64:
648 done = Q_QGTQ(bkt_ub.q64.uq64,
657 if (info->flags & VSD_HIST_LBOUND_INF)
659 if (info->flags & VSD_HIST_UBOUND_INF)
666 stats_vss_hist_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
667 struct vss_hist_hlpr_info *info)
669 struct voistatdata_hist *hist;
670 struct voistatdata_numeric bkt_lb, bkt_ub, *lbinfbktlb, *lbinfbktub,
671 *ubinfbktlb, *ubinfbktub;
672 uint32_t bkt, nbkts, nloop;
674 if (vss == NULL || info == NULL || (info->flags &
675 (VSD_HIST_LBOUND_INF|VSD_HIST_UBOUND_INF) && (info->hist_dtype ==
676 VSD_DTYPE_DVHIST32 || info->hist_dtype == VSD_DTYPE_DVHIST64)))
679 info->voi_dtype = voi_dtype;
681 if ((nbkts = stats_vss_hist_nbkts_hlpr(info)) == 0)
684 switch (info->hist_dtype) {
685 case VSD_DTYPE_CRHIST32:
686 vss->vsdsz = HIST_NBKTS2VSDSZ(crhist32, nbkts);
688 case VSD_DTYPE_DRHIST32:
689 vss->vsdsz = HIST_NBKTS2VSDSZ(drhist32, nbkts);
691 case VSD_DTYPE_DVHIST32:
692 vss->vsdsz = HIST_NBKTS2VSDSZ(dvhist32, nbkts);
694 case VSD_DTYPE_CRHIST64:
695 vss->vsdsz = HIST_NBKTS2VSDSZ(crhist64, nbkts);
697 case VSD_DTYPE_DRHIST64:
698 vss->vsdsz = HIST_NBKTS2VSDSZ(drhist64, nbkts);
700 case VSD_DTYPE_DVHIST64:
701 vss->vsdsz = HIST_NBKTS2VSDSZ(dvhist64, nbkts);
707 vss->iv = stats_realloc(NULL, 0, vss->vsdsz, M_ZERO);
711 hist = (struct voistatdata_hist *)vss->iv;
714 for (bkt = (info->flags & VSD_HIST_LBOUND_INF), nloop = 0;
718 if (stats_vss_hist_bkt_hlpr(info, nloop, &bkt_lb, &bkt_ub))
721 switch (info->hist_dtype) {
722 case VSD_DTYPE_CRHIST32:
723 VSD(crhist32, hist)->bkts[bkt].lb = bkt_lb;
725 case VSD_DTYPE_DRHIST32:
726 VSD(drhist32, hist)->bkts[bkt].lb = bkt_lb;
727 VSD(drhist32, hist)->bkts[bkt].ub = bkt_ub;
729 case VSD_DTYPE_DVHIST32:
730 VSD(dvhist32, hist)->bkts[bkt].val = bkt_lb;
732 case VSD_DTYPE_CRHIST64:
733 VSD(crhist64, hist)->bkts[bkt].lb = bkt_lb;
735 case VSD_DTYPE_DRHIST64:
736 VSD(drhist64, hist)->bkts[bkt].lb = bkt_lb;
737 VSD(drhist64, hist)->bkts[bkt].ub = bkt_ub;
739 case VSD_DTYPE_DVHIST64:
740 VSD(dvhist64, hist)->bkts[bkt].val = bkt_lb;
747 lbinfbktlb = lbinfbktub = ubinfbktlb = ubinfbktub = NULL;
749 switch (info->hist_dtype) {
750 case VSD_DTYPE_CRHIST32:
751 lbinfbktlb = &VSD(crhist32, hist)->bkts[0].lb;
752 ubinfbktlb = &VSD(crhist32, hist)->bkts[nbkts - 1].lb;
754 case VSD_DTYPE_DRHIST32:
755 lbinfbktlb = &VSD(drhist32, hist)->bkts[0].lb;
756 lbinfbktub = &VSD(drhist32, hist)->bkts[0].ub;
757 ubinfbktlb = &VSD(drhist32, hist)->bkts[nbkts - 1].lb;
758 ubinfbktub = &VSD(drhist32, hist)->bkts[nbkts - 1].ub;
760 case VSD_DTYPE_CRHIST64:
761 lbinfbktlb = &VSD(crhist64, hist)->bkts[0].lb;
762 ubinfbktlb = &VSD(crhist64, hist)->bkts[nbkts - 1].lb;
764 case VSD_DTYPE_DRHIST64:
765 lbinfbktlb = &VSD(drhist64, hist)->bkts[0].lb;
766 lbinfbktub = &VSD(drhist64, hist)->bkts[0].ub;
767 ubinfbktlb = &VSD(drhist64, hist)->bkts[nbkts - 1].lb;
768 ubinfbktub = &VSD(drhist64, hist)->bkts[nbkts - 1].ub;
770 case VSD_DTYPE_DVHIST32:
771 case VSD_DTYPE_DVHIST64:
777 if ((info->flags & VSD_HIST_LBOUND_INF) && lbinfbktlb) {
778 *lbinfbktlb = numeric_limits[LIM_MIN][info->voi_dtype];
780 * Assignment from numeric_limit array for Q types assigns max
781 * possible integral/fractional value for underlying data type,
782 * but we must set control bits for this specific histogram per
783 * the user's choice of fractional bits, which we extract from
786 if (info->voi_dtype == VSD_DTYPE_Q_S32 ||
787 info->voi_dtype == VSD_DTYPE_Q_U32) {
788 /* Signedness doesn't matter for setting control bits. */
789 Q_SCVAL(lbinfbktlb->q32.sq32,
790 Q_GCVAL(info->lb.q32.sq32));
791 } else if (info->voi_dtype == VSD_DTYPE_Q_S64 ||
792 info->voi_dtype == VSD_DTYPE_Q_U64) {
793 /* Signedness doesn't matter for setting control bits. */
794 Q_SCVAL(lbinfbktlb->q64.sq64,
795 Q_GCVAL(info->lb.q64.sq64));
798 *lbinfbktub = info->lb;
800 if ((info->flags & VSD_HIST_UBOUND_INF) && ubinfbktlb) {
801 *ubinfbktlb = bkt_lb;
803 *ubinfbktub = numeric_limits[LIM_MAX][info->voi_dtype];
804 if (info->voi_dtype == VSD_DTYPE_Q_S32 ||
805 info->voi_dtype == VSD_DTYPE_Q_U32) {
806 Q_SCVAL(ubinfbktub->q32.sq32,
807 Q_GCVAL(info->lb.q32.sq32));
808 } else if (info->voi_dtype == VSD_DTYPE_Q_S64 ||
809 info->voi_dtype == VSD_DTYPE_Q_U64) {
810 Q_SCVAL(ubinfbktub->q64.sq64,
811 Q_GCVAL(info->lb.q64.sq64));
820 stats_vss_tdgst_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
821 struct vss_tdgst_hlpr_info *info)
823 struct voistatdata_tdgst *tdgst;
824 struct ctdth32 *ctd32tree;
825 struct ctdth64 *ctd64tree;
826 struct voistatdata_tdgstctd32 *ctd32;
827 struct voistatdata_tdgstctd64 *ctd64;
829 info->voi_dtype = voi_dtype;
831 switch (info->tdgst_dtype) {
832 case VSD_DTYPE_TDGSTCLUST32:
833 vss->vsdsz = TDGST_NCTRS2VSDSZ(tdgstclust32, info->nctds);
835 case VSD_DTYPE_TDGSTCLUST64:
836 vss->vsdsz = TDGST_NCTRS2VSDSZ(tdgstclust64, info->nctds);
842 vss->iv = stats_realloc(NULL, 0, vss->vsdsz, M_ZERO);
846 tdgst = (struct voistatdata_tdgst *)vss->iv;
848 switch (info->tdgst_dtype) {
849 case VSD_DTYPE_TDGSTCLUST32:
850 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
851 ARB_INIT(ctd32, ctdlnk, ctd32tree, info->nctds) {
852 Q_INI(&ctd32->mu, 0, 0, info->prec);
855 case VSD_DTYPE_TDGSTCLUST64:
856 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
857 ARB_INIT(ctd64, ctdlnk, ctd64tree, info->nctds) {
858 Q_INI(&ctd64->mu, 0, 0, info->prec);
869 stats_vss_numeric_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
870 struct vss_numeric_hlpr_info *info)
872 struct voistatdata_numeric iv;
874 switch (vss->stype) {
876 iv = stats_ctor_vsd_numeric(0);
879 iv = numeric_limits[LIM_MAX][voi_dtype];
882 iv = numeric_limits[LIM_MIN][voi_dtype];
888 vss->iv = stats_realloc(NULL, 0, vsd_dtype2size[voi_dtype], 0);
892 vss->vs_dtype = voi_dtype;
893 vss->vsdsz = vsd_dtype2size[voi_dtype];
895 case VSD_DTYPE_INT_S32:
896 *((int32_t *)vss->iv) = iv.int32.s32;
898 case VSD_DTYPE_INT_U32:
899 *((uint32_t *)vss->iv) = iv.int32.u32;
901 case VSD_DTYPE_INT_S64:
902 *((int64_t *)vss->iv) = iv.int64.s64;
904 case VSD_DTYPE_INT_U64:
905 *((uint64_t *)vss->iv) = iv.int64.u64;
907 case VSD_DTYPE_INT_SLONG:
908 *((long *)vss->iv) = iv.intlong.slong;
910 case VSD_DTYPE_INT_ULONG:
911 *((unsigned long *)vss->iv) = iv.intlong.ulong;
913 case VSD_DTYPE_Q_S32:
914 *((s32q_t *)vss->iv) = Q_SCVAL(iv.q32.sq32,
915 Q_CTRLINI(info->prec));
917 case VSD_DTYPE_Q_U32:
918 *((u32q_t *)vss->iv) = Q_SCVAL(iv.q32.uq32,
919 Q_CTRLINI(info->prec));
921 case VSD_DTYPE_Q_S64:
922 *((s64q_t *)vss->iv) = Q_SCVAL(iv.q64.sq64,
923 Q_CTRLINI(info->prec));
925 case VSD_DTYPE_Q_U64:
926 *((u64q_t *)vss->iv) = Q_SCVAL(iv.q64.uq64,
927 Q_CTRLINI(info->prec));
937 stats_vss_hlpr_init(enum vsd_dtype voi_dtype, uint32_t nvss,
938 struct voistatspec *vss)
942 for (i = nvss - 1; i >= 0; i--) {
943 if (vss[i].hlpr && (ret = vss[i].hlpr(voi_dtype, &vss[i],
944 vss[i].hlprinfo)) != 0)
952 stats_vss_hlpr_cleanup(uint32_t nvss, struct voistatspec *vss)
956 for (i = nvss - 1; i >= 0; i--) {
958 stats_free((void *)vss[i].iv);
965 stats_tpl_fetch(int tpl_id, struct statsblob_tpl **tpl)
972 if (tpl_id < 0 || tpl_id >= (int)ntpl) {
975 *tpl = tpllist[tpl_id];
976 /* XXXLAS: Acquire refcount on tpl. */
984 stats_tpl_fetch_allocid(const char *name, uint32_t hash)
991 for (i = ntpl - 1; i >= 0; i--) {
993 if (strlen(name) == strlen(tpllist[i]->mb->tplname) &&
994 strncmp(name, tpllist[i]->mb->tplname,
995 TPL_MAX_NAME_LEN) == 0 && (!hash || hash ==
996 tpllist[i]->mb->tplhash)) {
1000 } else if (hash == tpllist[i]->mb->tplhash) {
1011 stats_tpl_id2name(uint32_t tpl_id, char *buf, size_t len)
1018 if (tpl_id < ntpl) {
1019 if (buf != NULL && len > strlen(tpllist[tpl_id]->mb->tplname))
1020 strlcpy(buf, tpllist[tpl_id]->mb->tplname, len);
1031 stats_tpl_sample_rollthedice(struct stats_tpl_sample_rate *rates, int nrates,
1032 void *seed_bytes, size_t seed_len)
1034 uint32_t cum_pct, rnd_pct;
1040 * Choose a pseudorandom or seeded number in range [0,100] and use
1041 * it to make a sampling decision and template selection where required.
1042 * If no seed is supplied, a PRNG is used to generate a pseudorandom
1043 * number so that every selection is independent. If a seed is supplied,
1044 * the caller desires random selection across different seeds, but
1045 * deterministic selection given the same seed. This is achieved by
1046 * hashing the seed and using the hash as the random number source.
1048 * XXXLAS: Characterise hash function output distribution.
1050 if (seed_bytes == NULL)
1051 rnd_pct = random() / (INT32_MAX / 100);
1053 rnd_pct = hash32_buf(seed_bytes, seed_len, 0) /
1054 (UINT32_MAX / 100U);
1057 * We map the randomly selected percentage on to the interval [0,100]
1058 * consisting of the cumulatively summed template sampling percentages.
1059 * The difference between the cumulative sum of all template sampling
1060 * percentages and 100 is treated as a NULL assignment i.e. no stats
1061 * template will be assigned, and -1 returned instead.
1063 for (i = 0; i < nrates; i++) {
1064 cum_pct += rates[i].tpl_sample_pct;
1066 KASSERT(cum_pct <= 100, ("%s cum_pct %u > 100", __func__,
1068 if (rnd_pct > cum_pct || rates[i].tpl_sample_pct == 0)
1071 return (rates[i].tpl_slot_id);
1078 stats_v1_blob_clone(struct statsblobv1 **dst, size_t dstmaxsz,
1079 struct statsblobv1 *src, uint32_t flags)
1081 int error, tmperror;
1083 error = tmperror = 0;
1085 if (src == NULL || dst == NULL ||
1086 src->cursz < sizeof(struct statsblob) ||
1087 ((flags & SB_CLONE_ALLOCDST) &&
1088 (flags & (SB_CLONE_USRDSTNOFAULT | SB_CLONE_USRDST)))) {
1090 } else if (flags & SB_CLONE_ALLOCDST) {
1091 *dst = stats_realloc(NULL, 0, src->cursz, 0);
1093 (*dst)->maxsz = dstmaxsz = src->cursz;
1096 } else if (*dst == NULL || dstmaxsz < sizeof(struct statsblob)) {
1101 size_t postcurszlen;
1104 * Clone src into dst except for the maxsz field. If dst is too
1105 * small to hold all of src, only copy src's header and return
1109 if (flags & SB_CLONE_USRDSTNOFAULT)
1110 error = copyout_nofault(src, *dst,
1111 offsetof(struct statsblob, maxsz));
1112 else if (flags & SB_CLONE_USRDST)
1113 error = copyout(src, *dst,
1114 offsetof(struct statsblob, maxsz));
1117 memcpy(*dst, src, offsetof(struct statsblob, maxsz));
1124 if (dstmaxsz >= src->cursz) {
1125 postcurszlen = src->cursz -
1126 offsetof(struct statsblob, cursz);
1129 postcurszlen = sizeof(struct statsblob) -
1130 offsetof(struct statsblob, cursz);
1133 if (flags & SB_CLONE_USRDSTNOFAULT)
1134 tmperror = copyout_nofault(&(src->cursz), &((*dst)->cursz),
1136 else if (flags & SB_CLONE_USRDST)
1137 tmperror = copyout(&(src->cursz), &((*dst)->cursz),
1141 memcpy(&((*dst)->cursz), &(src->cursz), postcurszlen);
1143 error = error ? error : tmperror;
1153 stats_v1_tpl_alloc(const char *name, uint32_t flags __unused)
1155 struct statsblobv1_tpl *tpl, **newtpllist;
1156 struct statsblobv1 *tpl_sb;
1157 struct metablob *tpl_mb;
1160 if (name != NULL && strlen(name) > TPL_MAX_NAME_LEN)
1163 if (name != NULL && stats_tpl_fetch_allocid(name, 0) >= 0)
1166 tpl = stats_realloc(NULL, 0, sizeof(struct statsblobv1_tpl), M_ZERO);
1167 tpl_mb = stats_realloc(NULL, 0, sizeof(struct metablob), M_ZERO);
1168 tpl_sb = stats_realloc(NULL, 0, sizeof(struct statsblobv1), M_ZERO);
1170 if (tpl_mb != NULL && name != NULL)
1171 tpl_mb->tplname = stats_strdup(name, 0);
1173 if (tpl == NULL || tpl_sb == NULL || tpl_mb == NULL ||
1174 tpl_mb->tplname == NULL) {
1177 if (tpl_mb != NULL) {
1178 stats_free(tpl_mb->tplname);
1187 tpl_sb->abi = STATS_ABI_V1;
1189 #if BYTE_ORDER == LITTLE_ENDIAN
1191 #elif BYTE_ORDER == BIG_ENDIAN
1196 tpl_sb->cursz = tpl_sb->maxsz = sizeof(struct statsblobv1);
1197 tpl_sb->stats_off = tpl_sb->statsdata_off = sizeof(struct statsblobv1);
1200 newtpllist = stats_realloc(tpllist, ntpl * sizeof(void *),
1201 (ntpl + 1) * sizeof(void *), 0);
1202 if (newtpllist != NULL) {
1204 tpllist = (struct statsblob_tpl **)newtpllist;
1205 tpllist[tpl_id] = (struct statsblob_tpl *)tpl;
1206 stats_tpl_update_hash(tpllist[tpl_id]);
1210 if (tpl_mb != NULL) {
1211 stats_free(tpl_mb->tplname);
1222 stats_v1_tpl_add_voistats(uint32_t tpl_id, int32_t voi_id, const char *voi_name,
1223 enum vsd_dtype voi_dtype, uint32_t nvss, struct voistatspec *vss,
1227 struct voistat *tmpstat;
1228 struct statsblobv1 *tpl_sb;
1229 struct metablob *tpl_mb;
1230 int error, i, newstatdataidx, newvoibytes, newvoistatbytes,
1231 newvoistatdatabytes, newvoistatmaxid;
1234 if (voi_id < 0 || voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES ||
1235 nvss == 0 || vss == NULL)
1238 error = nbytes = newvoibytes = newvoistatbytes =
1239 newvoistatdatabytes = 0;
1240 newvoistatmaxid = -1;
1242 /* Calculate the number of bytes required for the new voistats. */
1243 for (i = nvss - 1; i >= 0; i--) {
1244 if (vss[i].stype == 0 || vss[i].stype >= VS_NUM_STYPES ||
1245 vss[i].vs_dtype == 0 || vss[i].vs_dtype >= VSD_NUM_DTYPES ||
1246 vss[i].iv == NULL || vss[i].vsdsz == 0)
1248 if ((int)vss[i].stype > newvoistatmaxid)
1249 newvoistatmaxid = vss[i].stype;
1250 newvoistatdatabytes += vss[i].vsdsz;
1253 if (flags & SB_VOI_RELUPDATE) {
1254 /* XXXLAS: VOI state bytes may need to vary based on stat types. */
1255 newvoistatdatabytes += sizeof(struct voistatdata_voistate);
1257 nbytes += newvoistatdatabytes;
1260 if (tpl_id < ntpl) {
1261 tpl_sb = (struct statsblobv1 *)tpllist[tpl_id]->sb;
1262 tpl_mb = tpllist[tpl_id]->mb;
1264 if (voi_id >= NVOIS(tpl_sb) || tpl_sb->vois[voi_id].id == -1) {
1265 /* Adding a new VOI and associated stats. */
1266 if (voi_id >= NVOIS(tpl_sb)) {
1267 /* We need to grow the tpl_sb->vois array. */
1268 newvoibytes = (voi_id - (NVOIS(tpl_sb) - 1)) *
1270 nbytes += newvoibytes;
1273 (newvoistatmaxid + 1) * sizeof(struct voistat);
1275 /* Adding stats to an existing VOI. */
1276 if (newvoistatmaxid >
1277 tpl_sb->vois[voi_id].voistatmaxid) {
1278 newvoistatbytes = (newvoistatmaxid -
1279 tpl_sb->vois[voi_id].voistatmaxid) *
1280 sizeof(struct voistat);
1282 /* XXXLAS: KPI does not yet support expanding VOIs. */
1285 nbytes += newvoistatbytes;
1287 if (!error && newvoibytes > 0) {
1288 struct voi_meta *voi_meta = tpl_mb->voi_meta;
1290 voi_meta = stats_realloc(voi_meta, voi_meta == NULL ?
1291 0 : NVOIS(tpl_sb) * sizeof(struct voi_meta),
1292 (1 + voi_id) * sizeof(struct voi_meta),
1295 if (voi_meta == NULL)
1298 tpl_mb->voi_meta = voi_meta;
1302 /* NB: Resizing can change where tpl_sb points. */
1303 error = stats_v1_blob_expand(&tpl_sb, newvoibytes,
1304 newvoistatbytes, newvoistatdatabytes);
1308 tpl_mb->voi_meta[voi_id].name = stats_strdup(voi_name,
1310 if (tpl_mb->voi_meta[voi_id].name == NULL)
1315 /* Update the template list with the resized pointer. */
1316 tpllist[tpl_id]->sb = (struct statsblob *)tpl_sb;
1318 /* Update the template. */
1319 voi = &tpl_sb->vois[voi_id];
1322 /* VOI is new and needs to be initialised. */
1324 voi->dtype = voi_dtype;
1325 voi->stats_off = tpl_sb->stats_off;
1326 if (flags & SB_VOI_RELUPDATE)
1327 voi->flags |= VOI_REQSTATE;
1330 * XXXLAS: When this else block is written, the
1331 * "KPI does not yet support expanding VOIs"
1332 * error earlier in this function can be
1333 * removed. What is required here is to shuffle
1334 * the voistat array such that the new stats for
1335 * the voi are contiguous, which will displace
1336 * stats for other vois that reside after the
1337 * voi being updated. The other vois then need
1338 * to have their stats_off adjusted post
1343 voi->voistatmaxid = newvoistatmaxid;
1346 if (voi->flags & VOI_REQSTATE) {
1347 /* Initialise the voistate stat in slot 0. */
1348 tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off);
1349 tmpstat->stype = VS_STYPE_VOISTATE;
1351 tmpstat->dtype = VSD_DTYPE_VOISTATE;
1352 newstatdataidx = tmpstat->dsz =
1353 sizeof(struct voistatdata_numeric);
1354 tmpstat->data_off = tpl_sb->statsdata_off;
1357 for (i = 0; (uint32_t)i < nvss; i++) {
1358 tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off +
1359 (vss[i].stype * sizeof(struct voistat)));
1360 KASSERT(tmpstat->stype < 0, ("voistat %p "
1361 "already initialised", tmpstat));
1362 tmpstat->stype = vss[i].stype;
1363 tmpstat->flags = vss[i].flags;
1364 tmpstat->dtype = vss[i].vs_dtype;
1365 tmpstat->dsz = vss[i].vsdsz;
1366 tmpstat->data_off = tpl_sb->statsdata_off +
1368 memcpy(BLOB_OFFSET(tpl_sb, tmpstat->data_off),
1369 vss[i].iv, vss[i].vsdsz);
1370 newstatdataidx += vss[i].vsdsz;
1373 /* Update the template version hash. */
1374 stats_tpl_update_hash(tpllist[tpl_id]);
1375 /* XXXLAS: Confirm tpl name/hash pair remains unique. */
1384 struct statsblobv1 *
1385 stats_v1_blob_alloc(uint32_t tpl_id, uint32_t flags __unused)
1387 struct statsblobv1 *sb;
1393 if (tpl_id < ntpl) {
1394 sb = stats_realloc(NULL, 0, tpllist[tpl_id]->sb->maxsz, 0);
1396 sb->maxsz = tpllist[tpl_id]->sb->maxsz;
1397 error = stats_v1_blob_init_locked(sb, tpl_id, 0);
1412 stats_v1_blob_destroy(struct statsblobv1 *sb)
1419 stats_v1_voistat_fetch_dptr(struct statsblobv1 *sb, int32_t voi_id,
1420 enum voi_stype stype, enum vsd_dtype *retdtype, struct voistatdata **retvsd,
1426 if (retvsd == NULL || sb == NULL || sb->abi != STATS_ABI_V1 ||
1427 voi_id >= NVOIS(sb))
1430 v = &sb->vois[voi_id];
1431 if ((__typeof(v->voistatmaxid))stype > v->voistatmaxid)
1434 vs = BLOB_OFFSET(sb, v->stats_off + (stype * sizeof(struct voistat)));
1435 *retvsd = BLOB_OFFSET(sb, vs->data_off);
1436 if (retdtype != NULL)
1437 *retdtype = vs->dtype;
1438 if (retvsdsz != NULL)
1439 *retvsdsz = vs->dsz;
1445 stats_v1_blob_init(struct statsblobv1 *sb, uint32_t tpl_id, uint32_t flags)
1452 if (sb == NULL || tpl_id >= ntpl) {
1455 error = stats_v1_blob_init_locked(sb, tpl_id, flags);
1463 stats_v1_blob_init_locked(struct statsblobv1 *sb, uint32_t tpl_id,
1464 uint32_t flags __unused)
1468 TPL_LIST_RLOCK_ASSERT();
1469 error = (sb->maxsz >= tpllist[tpl_id]->sb->cursz) ? 0 : EOVERFLOW;
1471 ("sb %d instead of %d bytes", sb->maxsz, tpllist[tpl_id]->sb->cursz));
1474 memcpy(sb, tpllist[tpl_id]->sb, tpllist[tpl_id]->sb->cursz);
1475 sb->created = sb->lastrst = stats_sbinuptime();
1476 sb->tplhash = tpllist[tpl_id]->mb->tplhash;
1483 stats_v1_blob_expand(struct statsblobv1 **sbpp, int newvoibytes,
1484 int newvoistatbytes, int newvoistatdatabytes)
1486 struct statsblobv1 *sb;
1488 struct voistat *tmpvoistat, *voistat_array;
1489 int error, i, idxnewvois, idxnewvoistats, nbytes, nvoistats;
1491 KASSERT(newvoibytes % sizeof(struct voi) == 0,
1492 ("Bad newvoibytes %d", newvoibytes));
1493 KASSERT(newvoistatbytes % sizeof(struct voistat) == 0,
1494 ("Bad newvoistatbytes %d", newvoistatbytes));
1496 error = ((newvoibytes % sizeof(struct voi) == 0) &&
1497 (newvoistatbytes % sizeof(struct voistat) == 0)) ? 0 : EINVAL;
1499 nbytes = newvoibytes + newvoistatbytes + newvoistatdatabytes;
1502 * XXXLAS: Required until we gain support for flags which alter the
1503 * units of size/offset fields in key structs.
1505 if (!error && ((((int)sb->cursz) + nbytes) > SB_V1_MAXSZ))
1508 if (!error && (sb->cursz + nbytes > sb->maxsz)) {
1509 /* Need to expand our blob. */
1510 sb = stats_realloc(sb, sb->maxsz, sb->cursz + nbytes, M_ZERO);
1512 sb->maxsz = sb->cursz + nbytes;
1520 * Shuffle memory within the expanded blob working from the end
1521 * backwards, leaving gaps for the new voistat and voistatdata
1522 * structs at the beginning of their respective blob regions,
1523 * and for the new voi structs at the end of their blob region.
1525 memmove(BLOB_OFFSET(sb, sb->statsdata_off + nbytes),
1526 BLOB_OFFSET(sb, sb->statsdata_off),
1527 sb->cursz - sb->statsdata_off);
1528 memmove(BLOB_OFFSET(sb, sb->stats_off + newvoibytes +
1529 newvoistatbytes), BLOB_OFFSET(sb, sb->stats_off),
1530 sb->statsdata_off - sb->stats_off);
1532 /* First index of new voi/voistat structs to be initialised. */
1533 idxnewvois = NVOIS(sb);
1534 idxnewvoistats = (newvoistatbytes / sizeof(struct voistat)) - 1;
1536 /* Update housekeeping variables and offsets. */
1537 sb->cursz += nbytes;
1538 sb->stats_off += newvoibytes;
1539 sb->statsdata_off += newvoibytes + newvoistatbytes;
1541 /* XXXLAS: Zeroing not strictly needed but aids debugging. */
1542 memset(&sb->vois[idxnewvois], '\0', newvoibytes);
1543 memset(BLOB_OFFSET(sb, sb->stats_off), '\0',
1545 memset(BLOB_OFFSET(sb, sb->statsdata_off), '\0',
1546 newvoistatdatabytes);
1548 /* Initialise new voi array members and update offsets. */
1549 for (i = 0; i < NVOIS(sb); i++) {
1550 tmpvoi = &sb->vois[i];
1551 if (i >= idxnewvois) {
1552 tmpvoi->id = tmpvoi->voistatmaxid = -1;
1553 } else if (tmpvoi->id > -1) {
1554 tmpvoi->stats_off += newvoibytes +
1559 /* Initialise new voistat array members and update offsets. */
1560 nvoistats = (sb->statsdata_off - sb->stats_off) /
1561 sizeof(struct voistat);
1562 voistat_array = BLOB_OFFSET(sb, sb->stats_off);
1563 for (i = 0; i < nvoistats; i++) {
1564 tmpvoistat = &voistat_array[i];
1565 if (i <= idxnewvoistats) {
1566 tmpvoistat->stype = -1;
1567 } else if (tmpvoistat->stype > -1) {
1568 tmpvoistat->data_off += nbytes;
1577 stats_v1_blob_finalise(struct statsblobv1 *sb __unused)
1580 /* XXXLAS: Fill this in. */
1584 stats_v1_blob_iter(struct statsblobv1 *sb, stats_v1_blob_itercb_t icb,
1585 void *usrctx, uint32_t flags)
1589 struct sb_iter_ctx ctx;
1592 ctx.usrctx = usrctx;
1593 ctx.flags = SB_IT_FIRST_CB;
1596 for (i = 0; i < NVOIS(sb); i++) {
1600 ctx.flags |= SB_IT_FIRST_VOISTAT;
1603 ctx.flags |= SB_IT_FIRST_VOI;
1604 else if (i == (NVOIS(sb) - 1))
1605 ctx.flags |= SB_IT_LAST_VOI | SB_IT_LAST_CB;
1607 if (v->id < 0 && (flags & SB_IT_NULLVOI)) {
1608 if (icb(sb, v, NULL, &ctx))
1611 ctx.flags &= ~SB_IT_FIRST_CB;
1614 /* If NULL voi, v->voistatmaxid == -1 */
1615 for (j = 0; j <= v->voistatmaxid; j++) {
1616 vs = &((struct voistat *)BLOB_OFFSET(sb,
1618 if (vs->stype < 0 &&
1619 !(flags & SB_IT_NULLVOISTAT))
1622 if (j == v->voistatmaxid) {
1623 ctx.flags |= SB_IT_LAST_VOISTAT;
1624 if (i == (NVOIS(sb) - 1))
1628 ctx.flags &= ~SB_IT_LAST_CB;
1631 if (icb(sb, v, vs, &ctx))
1634 ctx.flags &= ~(SB_IT_FIRST_CB | SB_IT_FIRST_VOISTAT |
1635 SB_IT_LAST_VOISTAT);
1637 ctx.flags &= ~(SB_IT_FIRST_VOI | SB_IT_LAST_VOI);
1642 stats_voistatdata_tdgst_tostr(enum vsd_dtype voi_dtype __unused,
1643 const struct voistatdata_tdgst *tdgst, enum vsd_dtype tdgst_dtype,
1644 size_t tdgst_dsz __unused, enum sb_str_fmt fmt, struct sbuf *buf, int objdump)
1646 const struct ctdth32 *ctd32tree;
1647 const struct ctdth64 *ctd64tree;
1648 const struct voistatdata_tdgstctd32 *ctd32;
1649 const struct voistatdata_tdgstctd64 *ctd64;
1651 uint64_t smplcnt, compcnt;
1652 int is32bit, qmaxstrlen;
1653 uint16_t maxctds, curctds;
1655 switch (tdgst_dtype) {
1656 case VSD_DTYPE_TDGSTCLUST32:
1657 smplcnt = CONSTVSD(tdgstclust32, tdgst)->smplcnt;
1658 compcnt = CONSTVSD(tdgstclust32, tdgst)->compcnt;
1659 maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree);
1660 curctds = ARB_CURNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree);
1661 ctd32tree = &CONSTVSD(tdgstclust32, tdgst)->ctdtree;
1662 ctd32 = (objdump ? ARB_CNODE(ctd32tree, 0) :
1663 ARB_CMIN(ctdth32, ctd32tree));
1664 qmaxstrlen = (ctd32 == NULL) ? 1 : Q_MAXSTRLEN(ctd32->mu, 10);
1669 case VSD_DTYPE_TDGSTCLUST64:
1670 smplcnt = CONSTVSD(tdgstclust64, tdgst)->smplcnt;
1671 compcnt = CONSTVSD(tdgstclust64, tdgst)->compcnt;
1672 maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree);
1673 curctds = ARB_CURNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree);
1674 ctd64tree = &CONSTVSD(tdgstclust64, tdgst)->ctdtree;
1675 ctd64 = (objdump ? ARB_CNODE(ctd64tree, 0) :
1676 ARB_CMIN(ctdth64, ctd64tree));
1677 qmaxstrlen = (ctd64 == NULL) ? 1 : Q_MAXSTRLEN(ctd64->mu, 10);
1687 case SB_STRFMT_FREEFORM:
1688 fmtstr = "smplcnt=%ju, compcnt=%ju, maxctds=%hu, nctds=%hu";
1690 case SB_STRFMT_JSON:
1693 "\"smplcnt\":%ju,\"compcnt\":%ju,\"maxctds\":%hu,"
1694 "\"nctds\":%hu,\"ctds\":[";
1697 sbuf_printf(buf, fmtstr, (uintmax_t)smplcnt, (uintmax_t)compcnt,
1700 while ((is32bit ? NULL != ctd32 : NULL != ctd64)) {
1701 char qstr[qmaxstrlen];
1704 case SB_STRFMT_FREEFORM:
1705 fmtstr = "\n\t\t\t\t";
1707 case SB_STRFMT_JSON:
1712 sbuf_cat(buf, fmtstr);
1716 case SB_STRFMT_FREEFORM:
1717 fmtstr = "ctd[%hu].";
1719 case SB_STRFMT_JSON:
1721 fmtstr = "\"ctd\":%hu,";
1724 sbuf_printf(buf, fmtstr, is32bit ?
1725 ARB_SELFIDX(ctd32tree, ctd32) :
1726 ARB_SELFIDX(ctd64tree, ctd64));
1730 case SB_STRFMT_FREEFORM:
1733 case SB_STRFMT_JSON:
1738 sbuf_cat(buf, fmtstr);
1739 Q_TOSTR((is32bit ? ctd32->mu : ctd64->mu), -1, 10, qstr,
1741 sbuf_cat(buf, qstr);
1744 case SB_STRFMT_FREEFORM:
1745 fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}";
1747 case SB_STRFMT_JSON:
1749 fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}";
1752 sbuf_printf(buf, fmtstr,
1753 is32bit ? ctd32->cnt : (uintmax_t)ctd64->cnt);
1756 ctd32 = (objdump ? ARB_CNODE(ctd32tree,
1757 ARB_SELFIDX(ctd32tree, ctd32) + 1) :
1758 ARB_CNEXT(ctdth32, ctd32tree, ctd32));
1760 ctd64 = (objdump ? ARB_CNODE(ctd64tree,
1761 ARB_SELFIDX(ctd64tree, ctd64) + 1) :
1762 ARB_CNEXT(ctdth64, ctd64tree, ctd64));
1764 if (fmt == SB_STRFMT_JSON &&
1765 (is32bit ? NULL != ctd32 : NULL != ctd64))
1766 sbuf_putc(buf, ',');
1768 if (fmt == SB_STRFMT_JSON)
1773 stats_voistatdata_hist_tostr(enum vsd_dtype voi_dtype,
1774 const struct voistatdata_hist *hist, enum vsd_dtype hist_dtype,
1775 size_t hist_dsz, enum sb_str_fmt fmt, struct sbuf *buf, int objdump)
1777 const struct voistatdata_numeric *bkt_lb, *bkt_ub;
1782 switch (hist_dtype) {
1783 case VSD_DTYPE_CRHIST32:
1784 nbkts = HIST_VSDSZ2NBKTS(crhist32, hist_dsz);
1787 case VSD_DTYPE_DRHIST32:
1788 nbkts = HIST_VSDSZ2NBKTS(drhist32, hist_dsz);
1791 case VSD_DTYPE_DVHIST32:
1792 nbkts = HIST_VSDSZ2NBKTS(dvhist32, hist_dsz);
1795 case VSD_DTYPE_CRHIST64:
1796 nbkts = HIST_VSDSZ2NBKTS(crhist64, hist_dsz);
1799 case VSD_DTYPE_DRHIST64:
1800 nbkts = HIST_VSDSZ2NBKTS(drhist64, hist_dsz);
1803 case VSD_DTYPE_DVHIST64:
1804 nbkts = HIST_VSDSZ2NBKTS(dvhist64, hist_dsz);
1812 case SB_STRFMT_FREEFORM:
1813 fmtstr = "nbkts=%hu, ";
1815 case SB_STRFMT_JSON:
1817 fmtstr = "\"nbkts\":%hu,";
1820 sbuf_printf(buf, fmtstr, nbkts);
1823 case SB_STRFMT_FREEFORM:
1824 fmtstr = (is32bit ? "oob=%u" : "oob=%ju");
1826 case SB_STRFMT_JSON:
1828 fmtstr = (is32bit ? "\"oob\":%u,\"bkts\":[" :
1829 "\"oob\":%ju,\"bkts\":[");
1832 sbuf_printf(buf, fmtstr, is32bit ? VSD_CONSTHIST_FIELDVAL(hist,
1833 hist_dtype, oob) : (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist,
1836 for (i = 0; i < nbkts; i++) {
1837 switch (hist_dtype) {
1838 case VSD_DTYPE_CRHIST32:
1839 case VSD_DTYPE_CRHIST64:
1840 bkt_lb = VSD_CONSTCRHIST_FIELDPTR(hist, hist_dtype,
1843 bkt_ub = VSD_CONSTCRHIST_FIELDPTR(hist,
1844 hist_dtype, bkts[i + 1].lb);
1846 bkt_ub = &numeric_limits[LIM_MAX][voi_dtype];
1848 case VSD_DTYPE_DRHIST32:
1849 case VSD_DTYPE_DRHIST64:
1850 bkt_lb = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype,
1852 bkt_ub = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype,
1855 case VSD_DTYPE_DVHIST32:
1856 case VSD_DTYPE_DVHIST64:
1857 bkt_lb = bkt_ub = VSD_CONSTDVHIST_FIELDPTR(hist,
1858 hist_dtype, bkts[i].val);
1865 case SB_STRFMT_FREEFORM:
1866 fmtstr = "\n\t\t\t\t";
1868 case SB_STRFMT_JSON:
1873 sbuf_cat(buf, fmtstr);
1877 case SB_STRFMT_FREEFORM:
1878 fmtstr = "bkt[%hu].";
1880 case SB_STRFMT_JSON:
1882 fmtstr = "\"bkt\":%hu,";
1885 sbuf_printf(buf, fmtstr, i);
1889 case SB_STRFMT_FREEFORM:
1892 case SB_STRFMT_JSON:
1897 sbuf_cat(buf, fmtstr);
1898 stats_voistatdata_tostr((const struct voistatdata *)bkt_lb,
1899 voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric),
1903 case SB_STRFMT_FREEFORM:
1906 case SB_STRFMT_JSON:
1908 fmtstr = ",\"ub\":";
1911 sbuf_cat(buf, fmtstr);
1912 stats_voistatdata_tostr((const struct voistatdata *)bkt_ub,
1913 voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric),
1917 case SB_STRFMT_FREEFORM:
1918 fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}";
1920 case SB_STRFMT_JSON:
1922 fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}";
1925 sbuf_printf(buf, fmtstr, is32bit ?
1926 VSD_CONSTHIST_FIELDVAL(hist, hist_dtype, bkts[i].cnt) :
1927 (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist, hist_dtype,
1930 if (fmt == SB_STRFMT_JSON && i < nbkts - 1)
1931 sbuf_putc(buf, ',');
1933 if (fmt == SB_STRFMT_JSON)
1938 stats_voistatdata_tostr(const struct voistatdata *vsd, enum vsd_dtype voi_dtype,
1939 enum vsd_dtype vsd_dtype, size_t vsd_sz, enum sb_str_fmt fmt,
1940 struct sbuf *buf, int objdump)
1944 if (vsd == NULL || buf == NULL || voi_dtype >= VSD_NUM_DTYPES ||
1945 vsd_dtype >= VSD_NUM_DTYPES || fmt >= SB_STRFMT_NUM_FMTS)
1948 switch (vsd_dtype) {
1949 case VSD_DTYPE_VOISTATE:
1951 case SB_STRFMT_FREEFORM:
1954 case SB_STRFMT_JSON:
1956 fmtstr = "\"prev\":";
1959 sbuf_cat(buf, fmtstr);
1961 * Render prev by passing it as *vsd and voi_dtype as vsd_dtype.
1963 stats_voistatdata_tostr(
1964 (const struct voistatdata *)&CONSTVSD(voistate, vsd)->prev,
1965 voi_dtype, voi_dtype, vsd_sz, fmt, buf, objdump);
1967 case VSD_DTYPE_INT_S32:
1968 sbuf_printf(buf, "%d", vsd->int32.s32);
1970 case VSD_DTYPE_INT_U32:
1971 sbuf_printf(buf, "%u", vsd->int32.u32);
1973 case VSD_DTYPE_INT_S64:
1974 sbuf_printf(buf, "%jd", (intmax_t)vsd->int64.s64);
1976 case VSD_DTYPE_INT_U64:
1977 sbuf_printf(buf, "%ju", (uintmax_t)vsd->int64.u64);
1979 case VSD_DTYPE_INT_SLONG:
1980 sbuf_printf(buf, "%ld", vsd->intlong.slong);
1982 case VSD_DTYPE_INT_ULONG:
1983 sbuf_printf(buf, "%lu", vsd->intlong.ulong);
1985 case VSD_DTYPE_Q_S32:
1987 char qstr[Q_MAXSTRLEN(vsd->q32.sq32, 10)];
1988 Q_TOSTR((s32q_t)vsd->q32.sq32, -1, 10, qstr, sizeof(qstr));
1989 sbuf_cat(buf, qstr);
1992 case VSD_DTYPE_Q_U32:
1994 char qstr[Q_MAXSTRLEN(vsd->q32.uq32, 10)];
1995 Q_TOSTR((u32q_t)vsd->q32.uq32, -1, 10, qstr, sizeof(qstr));
1996 sbuf_cat(buf, qstr);
1999 case VSD_DTYPE_Q_S64:
2001 char qstr[Q_MAXSTRLEN(vsd->q64.sq64, 10)];
2002 Q_TOSTR((s64q_t)vsd->q64.sq64, -1, 10, qstr, sizeof(qstr));
2003 sbuf_cat(buf, qstr);
2006 case VSD_DTYPE_Q_U64:
2008 char qstr[Q_MAXSTRLEN(vsd->q64.uq64, 10)];
2009 Q_TOSTR((u64q_t)vsd->q64.uq64, -1, 10, qstr, sizeof(qstr));
2010 sbuf_cat(buf, qstr);
2013 case VSD_DTYPE_CRHIST32:
2014 case VSD_DTYPE_DRHIST32:
2015 case VSD_DTYPE_DVHIST32:
2016 case VSD_DTYPE_CRHIST64:
2017 case VSD_DTYPE_DRHIST64:
2018 case VSD_DTYPE_DVHIST64:
2019 stats_voistatdata_hist_tostr(voi_dtype, CONSTVSD(hist, vsd),
2020 vsd_dtype, vsd_sz, fmt, buf, objdump);
2022 case VSD_DTYPE_TDGSTCLUST32:
2023 case VSD_DTYPE_TDGSTCLUST64:
2024 stats_voistatdata_tdgst_tostr(voi_dtype,
2025 CONSTVSD(tdgst, vsd), vsd_dtype, vsd_sz, fmt, buf,
2032 return (sbuf_error(buf));
2036 stats_v1_itercb_tostr_freeform(struct statsblobv1 *sb, struct voi *v,
2037 struct voistat *vs, struct sb_iter_ctx *ctx)
2039 struct sb_tostrcb_ctx *sctx;
2040 struct metablob *tpl_mb;
2047 tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL;
2048 dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0);
2050 if (ctx->flags & SB_IT_FIRST_CB) {
2051 sbuf_printf(buf, "struct statsblobv1@%p", sb);
2053 sbuf_printf(buf, ", abi=%hhu, endian=%hhu, maxsz=%hu, "
2054 "cursz=%hu, created=%jd, lastrst=%jd, flags=0x%04hx, "
2055 "stats_off=%hu, statsdata_off=%hu",
2056 sb->abi, sb->endian, sb->maxsz, sb->cursz,
2057 sb->created, sb->lastrst, sb->flags, sb->stats_off,
2060 sbuf_printf(buf, ", tplhash=%u", sb->tplhash);
2063 if (ctx->flags & SB_IT_FIRST_VOISTAT) {
2064 sbuf_printf(buf, "\n\tvois[%hd]: id=%hd", ctx->vslot, v->id);
2067 sbuf_printf(buf, ", name=\"%s\"", (tpl_mb == NULL) ? "" :
2068 tpl_mb->voi_meta[v->id].name);
2070 sbuf_printf(buf, ", flags=0x%04hx, dtype=%s, "
2071 "voistatmaxid=%hhd, stats_off=%hu", v->flags,
2072 vsd_dtype2name[v->dtype], v->voistatmaxid, v->stats_off);
2075 if (!dump && vs->stype <= 0)
2078 sbuf_printf(buf, "\n\t\tvois[%hd]stat[%hhd]: stype=", v->id, ctx->vsslot);
2079 if (vs->stype < 0) {
2080 sbuf_printf(buf, "%hhd", vs->stype);
2083 sbuf_printf(buf, "%s, errs=%hu", vs_stype2name[vs->stype],
2085 vsd = BLOB_OFFSET(sb, vs->data_off);
2087 sbuf_printf(buf, ", flags=0x%04x, dtype=%s, dsz=%hu, "
2088 "data_off=%hu", vs->flags, vsd_dtype2name[vs->dtype],
2089 vs->dsz, vs->data_off);
2091 sbuf_cat(buf, "\n\t\t\tvoistatdata: ");
2092 stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz,
2093 sctx->fmt, buf, dump);
2097 stats_v1_itercb_tostr_json(struct statsblobv1 *sb, struct voi *v, struct voistat *vs,
2098 struct sb_iter_ctx *ctx)
2100 struct sb_tostrcb_ctx *sctx;
2101 struct metablob *tpl_mb;
2109 tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL;
2110 dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0);
2112 if (ctx->flags & SB_IT_FIRST_CB) {
2113 sbuf_putc(buf, '{');
2115 sbuf_printf(buf, "\"abi\":%hhu,\"endian\":%hhu,"
2116 "\"maxsz\":%hu,\"cursz\":%hu,\"created\":%jd,"
2117 "\"lastrst\":%jd,\"flags\":%hu,\"stats_off\":%hu,"
2118 "\"statsdata_off\":%hu,", sb->abi,
2119 sb->endian, sb->maxsz, sb->cursz, sb->created,
2120 sb->lastrst, sb->flags, sb->stats_off,
2125 fmtstr = "\"tplname\":%s,\"tplhash\":%u,\"vois\":{";
2127 fmtstr = "\"tplname\":\"%s\",\"tplhash\":%u,\"vois\":{";
2129 sbuf_printf(buf, fmtstr, tpl_mb ? tpl_mb->tplname : "null",
2133 if (ctx->flags & SB_IT_FIRST_VOISTAT) {
2135 sbuf_printf(buf, "\"[%d]\":{\"id\":%d", ctx->vslot,
2138 sbuf_cat(buf, "},");
2143 fmtstr = ",\"name\":%s,\"flags\":%hu,"
2144 "\"dtype\":\"%s\",\"voistatmaxid\":%hhd,"
2145 "\"stats_off\":%hu,";
2147 fmtstr = ",\"name\":\"%s\",\"flags\":%hu,"
2148 "\"dtype\":\"%s\",\"voistatmaxid\":%hhd,"
2149 "\"stats_off\":%hu,";
2151 sbuf_printf(buf, fmtstr, tpl_mb ?
2152 tpl_mb->voi_meta[v->id].name : "null", v->flags,
2153 vsd_dtype2name[v->dtype], v->voistatmaxid,
2156 if (tpl_mb == NULL) {
2157 sbuf_printf(buf, "\"[%hd]\":{", v->id);
2159 sbuf_printf(buf, "\"%s\":{",
2160 tpl_mb->voi_meta[v->id].name);
2163 sbuf_cat(buf, "\"stats\":{");
2166 vsd = BLOB_OFFSET(sb, vs->data_off);
2168 sbuf_printf(buf, "\"[%hhd]\":", ctx->vsslot);
2169 if (vs->stype < 0) {
2170 sbuf_cat(buf, "{\"stype\":-1},");
2173 sbuf_printf(buf, "{\"stype\":\"%s\",\"errs\":%hu,\"flags\":%hu,"
2174 "\"dtype\":\"%s\",\"data_off\":%hu,\"voistatdata\":{",
2175 vs_stype2name[vs->stype], vs->errs, vs->flags,
2176 vsd_dtype2name[vs->dtype], vs->data_off);
2177 } else if (vs->stype > 0) {
2179 sbuf_printf(buf, "\"[%hhd]\":", vs->stype);
2181 sbuf_printf(buf, "\"%s\":", vs_stype2name[vs->stype]);
2185 if ((vs->flags & VS_VSDVALID) || dump) {
2187 sbuf_printf(buf, "{\"errs\":%hu,", vs->errs);
2188 /* Simple non-compound VSD types need a key. */
2189 if (!vsd_compoundtype[vs->dtype])
2190 sbuf_cat(buf, "\"val\":");
2191 stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz,
2192 sctx->fmt, buf, dump);
2193 sbuf_cat(buf, dump ? "}}" : "}");
2195 sbuf_cat(buf, dump ? "null}" : "null");
2197 if (ctx->flags & SB_IT_LAST_VOISTAT)
2198 sbuf_cat(buf, "}}");
2200 if (ctx->flags & SB_IT_LAST_CB)
2201 sbuf_cat(buf, "}}");
2203 sbuf_putc(buf, ',');
2207 stats_v1_itercb_tostr(struct statsblobv1 *sb, struct voi *v, struct voistat *vs,
2208 struct sb_iter_ctx *ctx)
2210 struct sb_tostrcb_ctx *sctx;
2214 switch (sctx->fmt) {
2215 case SB_STRFMT_FREEFORM:
2216 stats_v1_itercb_tostr_freeform(sb, v, vs, ctx);
2218 case SB_STRFMT_JSON:
2219 stats_v1_itercb_tostr_json(sb, v, vs, ctx);
2225 return (sbuf_error(sctx->buf));
2229 stats_v1_blob_tostr(struct statsblobv1 *sb, struct sbuf *buf,
2230 enum sb_str_fmt fmt, uint32_t flags)
2232 struct sb_tostrcb_ctx sctx;
2235 if (sb == NULL || sb->abi != STATS_ABI_V1 || buf == NULL ||
2236 fmt >= SB_STRFMT_NUM_FMTS)
2243 if (flags & SB_TOSTR_META) {
2244 if (stats_tpl_fetch(stats_tpl_fetch_allocid(NULL, sb->tplhash),
2251 if (flags & SB_TOSTR_OBJDUMP)
2252 iflags |= (SB_IT_NULLVOI | SB_IT_NULLVOISTAT);
2253 stats_v1_blob_iter(sb, stats_v1_itercb_tostr, &sctx, iflags);
2255 return (sbuf_error(buf));
2259 stats_v1_itercb_visit(struct statsblobv1 *sb, struct voi *v,
2260 struct voistat *vs, struct sb_iter_ctx *ctx)
2262 struct sb_visitcb_ctx *vctx;
2263 struct sb_visit sbv;
2267 sbv.tplhash = sb->tplhash;
2269 sbv.voi_dtype = v->dtype;
2270 sbv.vs_stype = vs->stype;
2271 sbv.vs_dtype = vs->dtype;
2272 sbv.vs_dsz = vs->dsz;
2273 sbv.vs_data = BLOB_OFFSET(sb, vs->data_off);
2274 sbv.vs_errs = vs->errs;
2275 sbv.flags = ctx->flags & (SB_IT_FIRST_CB | SB_IT_LAST_CB |
2276 SB_IT_FIRST_VOI | SB_IT_LAST_VOI | SB_IT_FIRST_VOISTAT |
2277 SB_IT_LAST_VOISTAT);
2279 return (vctx->cb(&sbv, vctx->usrctx));
2283 stats_v1_blob_visit(struct statsblobv1 *sb, stats_blob_visitcb_t func,
2286 struct sb_visitcb_ctx vctx;
2288 if (sb == NULL || sb->abi != STATS_ABI_V1 || func == NULL)
2292 vctx.usrctx = usrctx;
2294 stats_v1_blob_iter(sb, stats_v1_itercb_visit, &vctx, 0);
2300 stats_v1_icb_reset_voistat(struct statsblobv1 *sb, struct voi *v __unused,
2301 struct voistat *vs, struct sb_iter_ctx *ctx __unused)
2305 if (vs->stype == VS_STYPE_VOISTATE)
2308 vsd = BLOB_OFFSET(sb, vs->data_off);
2310 /* Perform the stat type's default reset action. */
2311 switch (vs->stype) {
2313 switch (vs->dtype) {
2314 case VSD_DTYPE_Q_S32:
2315 Q_SIFVAL(VSD(q32, vsd)->sq32, 0);
2317 case VSD_DTYPE_Q_U32:
2318 Q_SIFVAL(VSD(q32, vsd)->uq32, 0);
2320 case VSD_DTYPE_Q_S64:
2321 Q_SIFVAL(VSD(q64, vsd)->sq64, 0);
2323 case VSD_DTYPE_Q_U64:
2324 Q_SIFVAL(VSD(q64, vsd)->uq64, 0);
2327 bzero(vsd, vs->dsz);
2332 switch (vs->dtype) {
2333 case VSD_DTYPE_Q_S32:
2334 Q_SIFVAL(VSD(q32, vsd)->sq32,
2335 Q_IFMINVAL(VSD(q32, vsd)->sq32));
2337 case VSD_DTYPE_Q_U32:
2338 Q_SIFVAL(VSD(q32, vsd)->uq32,
2339 Q_IFMINVAL(VSD(q32, vsd)->uq32));
2341 case VSD_DTYPE_Q_S64:
2342 Q_SIFVAL(VSD(q64, vsd)->sq64,
2343 Q_IFMINVAL(VSD(q64, vsd)->sq64));
2345 case VSD_DTYPE_Q_U64:
2346 Q_SIFVAL(VSD(q64, vsd)->uq64,
2347 Q_IFMINVAL(VSD(q64, vsd)->uq64));
2350 memcpy(vsd, &numeric_limits[LIM_MIN][vs->dtype],
2356 switch (vs->dtype) {
2357 case VSD_DTYPE_Q_S32:
2358 Q_SIFVAL(VSD(q32, vsd)->sq32,
2359 Q_IFMAXVAL(VSD(q32, vsd)->sq32));
2361 case VSD_DTYPE_Q_U32:
2362 Q_SIFVAL(VSD(q32, vsd)->uq32,
2363 Q_IFMAXVAL(VSD(q32, vsd)->uq32));
2365 case VSD_DTYPE_Q_S64:
2366 Q_SIFVAL(VSD(q64, vsd)->sq64,
2367 Q_IFMAXVAL(VSD(q64, vsd)->sq64));
2369 case VSD_DTYPE_Q_U64:
2370 Q_SIFVAL(VSD(q64, vsd)->uq64,
2371 Q_IFMAXVAL(VSD(q64, vsd)->uq64));
2374 memcpy(vsd, &numeric_limits[LIM_MAX][vs->dtype],
2381 /* Reset bucket counts. */
2382 struct voistatdata_hist *hist;
2386 hist = VSD(hist, vsd);
2387 switch (vs->dtype) {
2388 case VSD_DTYPE_CRHIST32:
2389 nbkts = HIST_VSDSZ2NBKTS(crhist32, vs->dsz);
2392 case VSD_DTYPE_DRHIST32:
2393 nbkts = HIST_VSDSZ2NBKTS(drhist32, vs->dsz);
2396 case VSD_DTYPE_DVHIST32:
2397 nbkts = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz);
2400 case VSD_DTYPE_CRHIST64:
2401 nbkts = HIST_VSDSZ2NBKTS(crhist64, vs->dsz);
2404 case VSD_DTYPE_DRHIST64:
2405 nbkts = HIST_VSDSZ2NBKTS(drhist64, vs->dsz);
2408 case VSD_DTYPE_DVHIST64:
2409 nbkts = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz);
2416 bzero(VSD_HIST_FIELDPTR(hist, vs->dtype, oob),
2417 is32bit ? sizeof(uint32_t) : sizeof(uint64_t));
2418 for (i = nbkts - 1; i >= 0; i--) {
2419 bzero(VSD_HIST_FIELDPTR(hist, vs->dtype,
2420 bkts[i].cnt), is32bit ? sizeof(uint32_t) :
2425 case VS_STYPE_TDGST:
2427 /* Reset sample count centroids array/tree. */
2428 struct voistatdata_tdgst *tdgst;
2429 struct ctdth32 *ctd32tree;
2430 struct ctdth64 *ctd64tree;
2431 struct voistatdata_tdgstctd32 *ctd32;
2432 struct voistatdata_tdgstctd64 *ctd64;
2434 tdgst = VSD(tdgst, vsd);
2435 switch (vs->dtype) {
2436 case VSD_DTYPE_TDGSTCLUST32:
2437 VSD(tdgstclust32, tdgst)->smplcnt = 0;
2438 VSD(tdgstclust32, tdgst)->compcnt = 0;
2439 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
2440 ARB_INIT(ctd32, ctdlnk, ctd32tree,
2441 ARB_MAXNODES(ctd32tree)) {
2443 Q_SIFVAL(ctd32->mu, 0);
2446 RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree);
2449 case VSD_DTYPE_TDGSTCLUST64:
2450 VSD(tdgstclust64, tdgst)->smplcnt = 0;
2451 VSD(tdgstclust64, tdgst)->compcnt = 0;
2452 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
2453 ARB_INIT(ctd64, ctdlnk, ctd64tree,
2454 ARB_MAXNODES(ctd64tree)) {
2456 Q_SIFVAL(ctd64->mu, 0);
2459 RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree);
2468 KASSERT(0, ("Unknown VOI stat type %d", vs->stype));
2473 vs->flags &= ~VS_VSDVALID;
2479 stats_v1_blob_snapshot(struct statsblobv1 **dst, size_t dstmaxsz,
2480 struct statsblobv1 *src, uint32_t flags)
2484 if (src != NULL && src->abi == STATS_ABI_V1) {
2485 error = stats_v1_blob_clone(dst, dstmaxsz, src, flags);
2487 if (flags & SB_CLONE_RSTSRC) {
2488 stats_v1_blob_iter(src,
2489 stats_v1_icb_reset_voistat, NULL, 0);
2490 src->lastrst = stats_sbinuptime();
2492 stats_v1_blob_finalise(*dst);
2501 stats_v1_voi_update_max(enum vsd_dtype voi_dtype __unused,
2502 struct voistatdata *voival, struct voistat *vs, void *vsd)
2506 KASSERT(vs->dtype < VSD_NUM_DTYPES,
2507 ("Unknown VSD dtype %d", vs->dtype));
2511 switch (vs->dtype) {
2512 case VSD_DTYPE_INT_S32:
2513 if (VSD(int32, vsd)->s32 < voival->int32.s32) {
2514 VSD(int32, vsd)->s32 = voival->int32.s32;
2515 vs->flags |= VS_VSDVALID;
2518 case VSD_DTYPE_INT_U32:
2519 if (VSD(int32, vsd)->u32 < voival->int32.u32) {
2520 VSD(int32, vsd)->u32 = voival->int32.u32;
2521 vs->flags |= VS_VSDVALID;
2524 case VSD_DTYPE_INT_S64:
2525 if (VSD(int64, vsd)->s64 < voival->int64.s64) {
2526 VSD(int64, vsd)->s64 = voival->int64.s64;
2527 vs->flags |= VS_VSDVALID;
2530 case VSD_DTYPE_INT_U64:
2531 if (VSD(int64, vsd)->u64 < voival->int64.u64) {
2532 VSD(int64, vsd)->u64 = voival->int64.u64;
2533 vs->flags |= VS_VSDVALID;
2536 case VSD_DTYPE_INT_SLONG:
2537 if (VSD(intlong, vsd)->slong < voival->intlong.slong) {
2538 VSD(intlong, vsd)->slong = voival->intlong.slong;
2539 vs->flags |= VS_VSDVALID;
2542 case VSD_DTYPE_INT_ULONG:
2543 if (VSD(intlong, vsd)->ulong < voival->intlong.ulong) {
2544 VSD(intlong, vsd)->ulong = voival->intlong.ulong;
2545 vs->flags |= VS_VSDVALID;
2548 case VSD_DTYPE_Q_S32:
2549 if (Q_QLTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) &&
2550 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32,
2551 voival->q32.sq32)))) {
2552 vs->flags |= VS_VSDVALID;
2555 case VSD_DTYPE_Q_U32:
2556 if (Q_QLTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) &&
2557 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32,
2558 voival->q32.uq32)))) {
2559 vs->flags |= VS_VSDVALID;
2562 case VSD_DTYPE_Q_S64:
2563 if (Q_QLTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) &&
2564 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64,
2565 voival->q64.sq64)))) {
2566 vs->flags |= VS_VSDVALID;
2569 case VSD_DTYPE_Q_U64:
2570 if (Q_QLTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) &&
2571 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64,
2572 voival->q64.uq64)))) {
2573 vs->flags |= VS_VSDVALID;
2585 stats_v1_voi_update_min(enum vsd_dtype voi_dtype __unused,
2586 struct voistatdata *voival, struct voistat *vs, void *vsd)
2590 KASSERT(vs->dtype < VSD_NUM_DTYPES,
2591 ("Unknown VSD dtype %d", vs->dtype));
2595 switch (vs->dtype) {
2596 case VSD_DTYPE_INT_S32:
2597 if (VSD(int32, vsd)->s32 > voival->int32.s32) {
2598 VSD(int32, vsd)->s32 = voival->int32.s32;
2599 vs->flags |= VS_VSDVALID;
2602 case VSD_DTYPE_INT_U32:
2603 if (VSD(int32, vsd)->u32 > voival->int32.u32) {
2604 VSD(int32, vsd)->u32 = voival->int32.u32;
2605 vs->flags |= VS_VSDVALID;
2608 case VSD_DTYPE_INT_S64:
2609 if (VSD(int64, vsd)->s64 > voival->int64.s64) {
2610 VSD(int64, vsd)->s64 = voival->int64.s64;
2611 vs->flags |= VS_VSDVALID;
2614 case VSD_DTYPE_INT_U64:
2615 if (VSD(int64, vsd)->u64 > voival->int64.u64) {
2616 VSD(int64, vsd)->u64 = voival->int64.u64;
2617 vs->flags |= VS_VSDVALID;
2620 case VSD_DTYPE_INT_SLONG:
2621 if (VSD(intlong, vsd)->slong > voival->intlong.slong) {
2622 VSD(intlong, vsd)->slong = voival->intlong.slong;
2623 vs->flags |= VS_VSDVALID;
2626 case VSD_DTYPE_INT_ULONG:
2627 if (VSD(intlong, vsd)->ulong > voival->intlong.ulong) {
2628 VSD(intlong, vsd)->ulong = voival->intlong.ulong;
2629 vs->flags |= VS_VSDVALID;
2632 case VSD_DTYPE_Q_S32:
2633 if (Q_QGTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) &&
2634 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32,
2635 voival->q32.sq32)))) {
2636 vs->flags |= VS_VSDVALID;
2639 case VSD_DTYPE_Q_U32:
2640 if (Q_QGTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) &&
2641 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32,
2642 voival->q32.uq32)))) {
2643 vs->flags |= VS_VSDVALID;
2646 case VSD_DTYPE_Q_S64:
2647 if (Q_QGTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) &&
2648 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64,
2649 voival->q64.sq64)))) {
2650 vs->flags |= VS_VSDVALID;
2653 case VSD_DTYPE_Q_U64:
2654 if (Q_QGTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) &&
2655 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64,
2656 voival->q64.uq64)))) {
2657 vs->flags |= VS_VSDVALID;
2669 stats_v1_voi_update_sum(enum vsd_dtype voi_dtype __unused,
2670 struct voistatdata *voival, struct voistat *vs, void *vsd)
2674 KASSERT(vs->dtype < VSD_NUM_DTYPES,
2675 ("Unknown VSD dtype %d", vs->dtype));
2679 switch (vs->dtype) {
2680 case VSD_DTYPE_INT_S32:
2681 VSD(int32, vsd)->s32 += voival->int32.s32;
2683 case VSD_DTYPE_INT_U32:
2684 VSD(int32, vsd)->u32 += voival->int32.u32;
2686 case VSD_DTYPE_INT_S64:
2687 VSD(int64, vsd)->s64 += voival->int64.s64;
2689 case VSD_DTYPE_INT_U64:
2690 VSD(int64, vsd)->u64 += voival->int64.u64;
2692 case VSD_DTYPE_INT_SLONG:
2693 VSD(intlong, vsd)->slong += voival->intlong.slong;
2695 case VSD_DTYPE_INT_ULONG:
2696 VSD(intlong, vsd)->ulong += voival->intlong.ulong;
2698 case VSD_DTYPE_Q_S32:
2699 error = Q_QADDQ(&VSD(q32, vsd)->sq32, voival->q32.sq32);
2701 case VSD_DTYPE_Q_U32:
2702 error = Q_QADDQ(&VSD(q32, vsd)->uq32, voival->q32.uq32);
2704 case VSD_DTYPE_Q_S64:
2705 error = Q_QADDQ(&VSD(q64, vsd)->sq64, voival->q64.sq64);
2707 case VSD_DTYPE_Q_U64:
2708 error = Q_QADDQ(&VSD(q64, vsd)->uq64, voival->q64.uq64);
2716 vs->flags |= VS_VSDVALID;
2722 stats_v1_voi_update_hist(enum vsd_dtype voi_dtype, struct voistatdata *voival,
2723 struct voistat *vs, struct voistatdata_hist *hist)
2725 struct voistatdata_numeric *bkt_lb, *bkt_ub;
2726 uint64_t *oob64, *cnt64;
2727 uint32_t *oob32, *cnt32;
2728 int error, i, found, is32bit, has_ub, eq_only;
2732 switch (vs->dtype) {
2733 case VSD_DTYPE_CRHIST32:
2734 i = HIST_VSDSZ2NBKTS(crhist32, vs->dsz);
2736 has_ub = eq_only = 0;
2737 oob32 = &VSD(crhist32, hist)->oob;
2739 case VSD_DTYPE_DRHIST32:
2740 i = HIST_VSDSZ2NBKTS(drhist32, vs->dsz);
2741 is32bit = has_ub = 1;
2743 oob32 = &VSD(drhist32, hist)->oob;
2745 case VSD_DTYPE_DVHIST32:
2746 i = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz);
2747 is32bit = eq_only = 1;
2749 oob32 = &VSD(dvhist32, hist)->oob;
2751 case VSD_DTYPE_CRHIST64:
2752 i = HIST_VSDSZ2NBKTS(crhist64, vs->dsz);
2753 is32bit = has_ub = eq_only = 0;
2754 oob64 = &VSD(crhist64, hist)->oob;
2756 case VSD_DTYPE_DRHIST64:
2757 i = HIST_VSDSZ2NBKTS(drhist64, vs->dsz);
2758 is32bit = eq_only = 0;
2760 oob64 = &VSD(drhist64, hist)->oob;
2762 case VSD_DTYPE_DVHIST64:
2763 i = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz);
2764 is32bit = has_ub = 0;
2766 oob64 = &VSD(dvhist64, hist)->oob;
2771 i--; /* Adjust for 0-based array index. */
2773 /* XXXLAS: Should probably use a better bucket search algorithm. ARB? */
2774 for (found = 0; i >= 0 && !found; i--) {
2775 switch (vs->dtype) {
2776 case VSD_DTYPE_CRHIST32:
2777 bkt_lb = &VSD(crhist32, hist)->bkts[i].lb;
2778 cnt32 = &VSD(crhist32, hist)->bkts[i].cnt;
2780 case VSD_DTYPE_DRHIST32:
2781 bkt_lb = &VSD(drhist32, hist)->bkts[i].lb;
2782 bkt_ub = &VSD(drhist32, hist)->bkts[i].ub;
2783 cnt32 = &VSD(drhist32, hist)->bkts[i].cnt;
2785 case VSD_DTYPE_DVHIST32:
2786 bkt_lb = &VSD(dvhist32, hist)->bkts[i].val;
2787 cnt32 = &VSD(dvhist32, hist)->bkts[i].cnt;
2789 case VSD_DTYPE_CRHIST64:
2790 bkt_lb = &VSD(crhist64, hist)->bkts[i].lb;
2791 cnt64 = &VSD(crhist64, hist)->bkts[i].cnt;
2793 case VSD_DTYPE_DRHIST64:
2794 bkt_lb = &VSD(drhist64, hist)->bkts[i].lb;
2795 bkt_ub = &VSD(drhist64, hist)->bkts[i].ub;
2796 cnt64 = &VSD(drhist64, hist)->bkts[i].cnt;
2798 case VSD_DTYPE_DVHIST64:
2799 bkt_lb = &VSD(dvhist64, hist)->bkts[i].val;
2800 cnt64 = &VSD(dvhist64, hist)->bkts[i].cnt;
2806 switch (voi_dtype) {
2807 case VSD_DTYPE_INT_S32:
2808 if (voival->int32.s32 >= bkt_lb->int32.s32) {
2809 if ((eq_only && voival->int32.s32 ==
2810 bkt_lb->int32.s32) ||
2811 (!eq_only && (!has_ub ||
2812 voival->int32.s32 < bkt_ub->int32.s32)))
2816 case VSD_DTYPE_INT_U32:
2817 if (voival->int32.u32 >= bkt_lb->int32.u32) {
2818 if ((eq_only && voival->int32.u32 ==
2819 bkt_lb->int32.u32) ||
2820 (!eq_only && (!has_ub ||
2821 voival->int32.u32 < bkt_ub->int32.u32)))
2825 case VSD_DTYPE_INT_S64:
2826 if (voival->int64.s64 >= bkt_lb->int64.s64)
2827 if ((eq_only && voival->int64.s64 ==
2828 bkt_lb->int64.s64) ||
2829 (!eq_only && (!has_ub ||
2830 voival->int64.s64 < bkt_ub->int64.s64)))
2833 case VSD_DTYPE_INT_U64:
2834 if (voival->int64.u64 >= bkt_lb->int64.u64)
2835 if ((eq_only && voival->int64.u64 ==
2836 bkt_lb->int64.u64) ||
2837 (!eq_only && (!has_ub ||
2838 voival->int64.u64 < bkt_ub->int64.u64)))
2841 case VSD_DTYPE_INT_SLONG:
2842 if (voival->intlong.slong >= bkt_lb->intlong.slong)
2843 if ((eq_only && voival->intlong.slong ==
2844 bkt_lb->intlong.slong) ||
2845 (!eq_only && (!has_ub ||
2846 voival->intlong.slong <
2847 bkt_ub->intlong.slong)))
2850 case VSD_DTYPE_INT_ULONG:
2851 if (voival->intlong.ulong >= bkt_lb->intlong.ulong)
2852 if ((eq_only && voival->intlong.ulong ==
2853 bkt_lb->intlong.ulong) ||
2854 (!eq_only && (!has_ub ||
2855 voival->intlong.ulong <
2856 bkt_ub->intlong.ulong)))
2859 case VSD_DTYPE_Q_S32:
2860 if (Q_QGEQ(voival->q32.sq32, bkt_lb->q32.sq32))
2861 if ((eq_only && Q_QEQ(voival->q32.sq32,
2862 bkt_lb->q32.sq32)) ||
2863 (!eq_only && (!has_ub ||
2864 Q_QLTQ(voival->q32.sq32,
2865 bkt_ub->q32.sq32))))
2868 case VSD_DTYPE_Q_U32:
2869 if (Q_QGEQ(voival->q32.uq32, bkt_lb->q32.uq32))
2870 if ((eq_only && Q_QEQ(voival->q32.uq32,
2871 bkt_lb->q32.uq32)) ||
2872 (!eq_only && (!has_ub ||
2873 Q_QLTQ(voival->q32.uq32,
2874 bkt_ub->q32.uq32))))
2877 case VSD_DTYPE_Q_S64:
2878 if (Q_QGEQ(voival->q64.sq64, bkt_lb->q64.sq64))
2879 if ((eq_only && Q_QEQ(voival->q64.sq64,
2880 bkt_lb->q64.sq64)) ||
2881 (!eq_only && (!has_ub ||
2882 Q_QLTQ(voival->q64.sq64,
2883 bkt_ub->q64.sq64))))
2886 case VSD_DTYPE_Q_U64:
2887 if (Q_QGEQ(voival->q64.uq64, bkt_lb->q64.uq64))
2888 if ((eq_only && Q_QEQ(voival->q64.uq64,
2889 bkt_lb->q64.uq64)) ||
2890 (!eq_only && (!has_ub ||
2891 Q_QLTQ(voival->q64.uq64,
2892 bkt_ub->q64.uq64))))
2912 vs->flags |= VS_VSDVALID;
2917 stats_v1_vsd_tdgst_compress(enum vsd_dtype vs_dtype,
2918 struct voistatdata_tdgst *tdgst, int attempt)
2920 struct ctdth32 *ctd32tree;
2921 struct ctdth64 *ctd64tree;
2922 struct voistatdata_tdgstctd32 *ctd32;
2923 struct voistatdata_tdgstctd64 *ctd64;
2924 uint64_t ebits, idxmask;
2925 uint32_t bitsperidx, nebits;
2926 int error, idx, is32bit, maxctds, remctds, tmperr;
2931 case VSD_DTYPE_TDGSTCLUST32:
2932 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
2933 if (!ARB_FULL(ctd32tree))
2935 VSD(tdgstclust32, tdgst)->compcnt++;
2936 maxctds = remctds = ARB_MAXNODES(ctd32tree);
2937 ARB_RESET_TREE(ctd32tree, ctdth32, maxctds);
2938 VSD(tdgstclust32, tdgst)->smplcnt = 0;
2943 RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree);
2946 case VSD_DTYPE_TDGSTCLUST64:
2947 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
2948 if (!ARB_FULL(ctd64tree))
2950 VSD(tdgstclust64, tdgst)->compcnt++;
2951 maxctds = remctds = ARB_MAXNODES(ctd64tree);
2952 ARB_RESET_TREE(ctd64tree, ctdth64, maxctds);
2953 VSD(tdgstclust64, tdgst)->smplcnt = 0;
2958 RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree);
2966 * Rebuild the t-digest ARB by pseudorandomly selecting centroids and
2967 * re-inserting the mu/cnt of each as a value and corresponding weight.
2971 * XXXCEM: random(9) is currently rand(3), not random(3). rand(3)
2972 * RAND_MAX happens to be approximately 31 bits (range [0,
2973 * 0x7ffffffd]), so the math kinda works out. When/if this portion of
2974 * the code is compiled in userspace, it gets the random(3) behavior,
2975 * which has expected range [0, 0x7fffffff].
2977 #define bitsperrand 31
2980 bitsperidx = fls(maxctds);
2981 KASSERT(bitsperidx <= sizeof(ebits) << 3,
2982 ("%s: bitsperidx=%d, ebits=%d",
2983 __func__, bitsperidx, (int)(sizeof(ebits) << 3)));
2984 idxmask = (UINT64_C(1) << bitsperidx) - 1;
2986 /* Initialise the free list with randomised centroid indices. */
2987 for (; remctds > 0; remctds--) {
2988 while (nebits < bitsperidx) {
2989 ebits |= ((uint64_t)random()) << nebits;
2990 nebits += bitsperrand;
2991 if (nebits > (sizeof(ebits) << 3))
2992 nebits = sizeof(ebits) << 3;
2994 idx = ebits & idxmask;
2995 nebits -= bitsperidx;
2996 ebits >>= bitsperidx;
2999 * Select the next centroid to put on the ARB free list. We
3000 * start with the centroid at our randomly selected array index,
3001 * and work our way forwards until finding one (the latter
3002 * aspect reduces re-insertion randomness, but is good enough).
3009 ctd32 = ARB_NODE(ctd32tree, idx);
3011 ctd64 = ARB_NODE(ctd64tree, idx);
3012 } while ((is32bit ? ARB_ISFREE(ctd32, ctdlnk) :
3013 ARB_ISFREE(ctd64, ctdlnk)) && ++idx);
3015 /* Put the centroid on the ARB free list. */
3017 ARB_RETURNFREE(ctd32tree, ctd32, ctdlnk);
3019 ARB_RETURNFREE(ctd64tree, ctd64, ctdlnk);
3023 * The free list now contains the randomised indices of every centroid.
3024 * Walk the free list from start to end, re-inserting each centroid's
3025 * mu/cnt. The tdgst_add() call may or may not consume the free centroid
3026 * we re-insert values from during each loop iteration, so we must latch
3027 * the index of the next free list centroid before the re-insertion
3028 * call. The previous loop above should have left the centroid pointer
3029 * pointing to the element at the head of the free list.
3032 ARB_FREEIDX(ctd32tree) == ARB_SELFIDX(ctd32tree, ctd32) :
3033 ARB_FREEIDX(ctd64tree) == ARB_SELFIDX(ctd64tree, ctd64)),
3034 ("%s: t-digest ARB@%p free list bug", __func__,
3035 (is32bit ? (void *)ctd32tree : (void *)ctd64tree)));
3037 while ((is32bit ? ctd32 != NULL : ctd64 != NULL)) {
3042 idx = ARB_NEXTFREEIDX(ctd32, ctdlnk);
3043 /* Cloning a s32q_t into a s64q_t should never fail. */
3044 tmperr = Q_QCLONEQ(&x, ctd32->mu);
3045 tmperr = tmperr ? tmperr : stats_v1_vsd_tdgst_add(
3046 vs_dtype, tdgst, x, ctd32->cnt, attempt);
3047 ctd32 = ARB_NODE(ctd32tree, idx);
3048 KASSERT(ctd32 == NULL || ARB_ISFREE(ctd32, ctdlnk),
3049 ("%s: t-digest ARB@%p free list bug", __func__,
3052 idx = ARB_NEXTFREEIDX(ctd64, ctdlnk);
3053 tmperr = stats_v1_vsd_tdgst_add(vs_dtype, tdgst,
3054 ctd64->mu, ctd64->cnt, attempt);
3055 ctd64 = ARB_NODE(ctd64tree, idx);
3056 KASSERT(ctd64 == NULL || ARB_ISFREE(ctd64, ctdlnk),
3057 ("%s: t-digest ARB@%p free list bug", __func__,
3061 * This process should not produce errors, bugs notwithstanding.
3062 * Just in case, latch any errors and attempt all re-insertions.
3064 error = tmperr ? tmperr : error;
3068 KASSERT(remctds == 0, ("%s: t-digest ARB@%p free list bug", __func__,
3069 (is32bit ? (void *)ctd32tree : (void *)ctd64tree)));
3075 stats_v1_vsd_tdgst_add(enum vsd_dtype vs_dtype, struct voistatdata_tdgst *tdgst,
3076 s64q_t x, uint64_t weight, int attempt)
3079 char qstr[Q_MAXSTRLEN(x, 10)];
3081 struct ctdth32 *ctd32tree;
3082 struct ctdth64 *ctd64tree;
3083 void *closest, *cur, *lb, *ub;
3084 struct voistatdata_tdgstctd32 *ctd32;
3085 struct voistatdata_tdgstctd64 *ctd64;
3086 uint64_t cnt, smplcnt, sum, tmpsum;
3087 s64q_t k, minz, q, z;
3088 int error, is32bit, n;
3091 minz = Q_INI(&z, 0, 0, Q_NFBITS(x));
3094 case VSD_DTYPE_TDGSTCLUST32:
3095 if ((UINT32_MAX - weight) < VSD(tdgstclust32, tdgst)->smplcnt)
3097 smplcnt = VSD(tdgstclust32, tdgst)->smplcnt;
3098 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
3103 case VSD_DTYPE_TDGSTCLUST64:
3104 if ((UINT64_MAX - weight) < VSD(tdgstclust64, tdgst)->smplcnt)
3106 smplcnt = VSD(tdgstclust64, tdgst)->smplcnt;
3107 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
3121 * Inspired by Ted Dunning's AVLTreeDigest.java
3124 #if defined(DIAGNOSTIC)
3125 KASSERT(attempt < 5,
3126 ("%s: Too many attempts", __func__));
3131 Q_SIFVAL(minz, Q_IFMAXVAL(minz));
3132 closest = ub = NULL;
3136 lb = cur = (void *)(ctd32 = ARB_MIN(ctdth32, ctd32tree));
3138 lb = cur = (void *)(ctd64 = ARB_MIN(ctdth64, ctd64tree));
3140 if (lb == NULL) /* Empty tree. */
3141 lb = (is32bit ? (void *)ARB_ROOT(ctd32tree) :
3142 (void *)ARB_ROOT(ctd64tree));
3145 * Find the set of centroids with minimum distance to x and
3146 * compute the sum of counts for all centroids with mean less
3147 * than the first centroid in the set.
3151 (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) :
3152 (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) {
3155 KASSERT(Q_PRECEQ(ctd32->mu, x),
3156 ("%s: Q_RELPREC(mu,x)=%d", __func__,
3157 Q_RELPREC(ctd32->mu, x)));
3158 /* Ok to assign as both have same precision. */
3162 KASSERT(Q_PRECEQ(ctd64->mu, x),
3163 ("%s: Q_RELPREC(mu,x)=%d", __func__,
3164 Q_RELPREC(ctd64->mu, x)));
3165 /* Ok to assign as both have same precision. */
3169 error = Q_QSUBQ(&z, x);
3170 #if defined(DIAGNOSTIC)
3171 KASSERT(!error, ("%s: unexpected error %d", __func__,
3178 if (Q_QLTQ(z, minz)) {
3183 } else if (Q_QGTQ(z, minz)) {
3190 (void *)(ctd32 = (struct voistatdata_tdgstctd32 *)lb) :
3191 (void *)(ctd64 = (struct voistatdata_tdgstctd64 *)lb));
3193 for (n = 0; cur != ub; cur = (is32bit ?
3194 (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) :
3195 (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) {
3203 error = Q_QFRACI(&q, 1, 2);
3205 /* [ sum + ((cnt - 1) / 2) ] / (smplcnt - 1) */
3206 error = Q_QFRACI(&q, (sum << 1) + cnt - 1,
3207 (smplcnt - 1) << 1);
3209 /* k = q x 4 x samplcnt x attempt */
3210 error |= Q_QMULI(&k, 4 * smplcnt * attempt);
3211 /* k = k x (1 - q) */
3212 error |= Q_QSUBI(&q, 1);
3214 error |= Q_QMULQ(&k, q);
3215 #if defined(DIAGNOSTIC)
3216 #if !defined(_KERNEL)
3217 double q_dbl, k_dbl, q2d, k2d;
3220 q_dbl = smplcnt == 1 ? 0.5 :
3221 (sum + ((cnt - 1) / 2.0)) / (double)(smplcnt - 1);
3222 k_dbl = 4 * smplcnt * q_dbl * (1.0 - q_dbl) * attempt;
3224 * If the difference between q and q_dbl is greater than
3225 * the fractional precision of q, something is off.
3226 * NB: q is holding the value of 1 - q
3228 q_dbl = 1.0 - q_dbl;
3229 KASSERT((q_dbl > q2d ? q_dbl - q2d : q2d - q_dbl) <
3230 (1.05 * ((double)1 / (double)(1ULL << Q_NFBITS(q)))),
3231 ("Q-type q bad precision"));
3232 KASSERT((k_dbl > k2d ? k_dbl - k2d : k2d - k_dbl) <
3233 1.0 + (0.01 * smplcnt),
3234 ("Q-type k bad precision"));
3235 #endif /* !_KERNEL */
3236 KASSERT(!error, ("%s: unexpected error %d", __func__,
3238 #endif /* DIAGNOSTIC */
3241 if ((is32bit && ((ctd32->cnt + weight) <=
3242 (uint64_t)Q_GIVAL(k))) ||
3243 (!is32bit && ((ctd64->cnt + weight) <=
3244 (uint64_t)Q_GIVAL(k)))) {
3246 /* random() produces 31 bits. */
3247 if (random() < (INT32_MAX / n))
3252 } while (closest == NULL &&
3253 (is32bit ? ARB_FULL(ctd32tree) : ARB_FULL(ctd64tree)) &&
3254 (error = stats_v1_vsd_tdgst_compress(vs_dtype, tdgst,
3260 if (closest != NULL) {
3261 /* Merge with an existing centroid. */
3263 ctd32 = (struct voistatdata_tdgstctd32 *)closest;
3264 error = Q_QSUBQ(&x, ctd32->mu);
3266 * The following calculation "x / (cnt + weight)"
3267 * computes the amount by which to adjust the centroid's
3268 * mu value in order to merge in the VOI sample.
3270 * It can underflow (Q_QDIVI() returns ERANGE) when the
3271 * user centroids' fractional precision (which is
3272 * inherited by 'x') is too low to represent the result.
3274 * A sophisticated approach to dealing with this issue
3275 * would minimise accumulation of error by tracking
3276 * underflow per centroid and making an adjustment when
3277 * a LSB's worth of underflow has accumulated.
3279 * A simpler approach is to let the result underflow
3280 * i.e. merge the VOI sample into the centroid without
3281 * adjusting the centroid's mu, and rely on the user to
3282 * specify their t-digest with sufficient centroid
3283 * fractional precision such that the accumulation of
3284 * error from multiple underflows is of no material
3285 * consequence to the centroid's final value of mu.
3287 * For the moment, the latter approach is employed by
3288 * simply ignoring ERANGE here.
3290 * XXXLAS: Per-centroid underflow tracking is likely too
3291 * onerous, but it probably makes sense to accumulate a
3292 * single underflow error variable across all centroids
3293 * and report it as part of the digest to provide
3294 * additional visibility into the digest's fidelity.
3296 error = error ? error :
3297 Q_QDIVI(&x, ctd32->cnt + weight);
3298 if ((error && error != ERANGE)
3299 || (error = Q_QADDQ(&ctd32->mu, x))) {
3301 KASSERT(!error, ("%s: unexpected error %d",
3306 ctd32->cnt += weight;
3307 error = ARB_REINSERT(ctdth32, ctd32tree, ctd32) ==
3308 NULL ? 0 : EALREADY;
3310 RB_REINSERT(rbctdth32,
3311 &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32);
3314 ctd64 = (struct voistatdata_tdgstctd64 *)closest;
3315 error = Q_QSUBQ(&x, ctd64->mu);
3316 error = error ? error :
3317 Q_QDIVI(&x, ctd64->cnt + weight);
3318 /* Refer to is32bit ERANGE discussion above. */
3319 if ((error && error != ERANGE)
3320 || (error = Q_QADDQ(&ctd64->mu, x))) {
3321 KASSERT(!error, ("%s: unexpected error %d",
3325 ctd64->cnt += weight;
3326 error = ARB_REINSERT(ctdth64, ctd64tree, ctd64) ==
3327 NULL ? 0 : EALREADY;
3329 RB_REINSERT(rbctdth64,
3330 &VSD(tdgstclust64, tdgst)->rbctdtree, ctd64);
3335 * Add a new centroid. If digest compression is working
3336 * correctly, there should always be at least one free.
3339 ctd32 = ARB_GETFREE(ctd32tree, ctdlnk);
3341 KASSERT(ctd32 != NULL,
3342 ("%s: t-digest@%p has no free centroids",
3347 if ((error = Q_QCPYVALQ(&ctd32->mu, x)))
3349 ctd32->cnt = weight;
3350 error = ARB_INSERT(ctdth32, ctd32tree, ctd32) == NULL ?
3353 RB_INSERT(rbctdth32,
3354 &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32);
3357 ctd64 = ARB_GETFREE(ctd64tree, ctdlnk);
3359 KASSERT(ctd64 != NULL,
3360 ("%s: t-digest@%p has no free centroids",
3363 if (ctd64 == NULL) /* Should not happen. */
3365 /* Direct assignment ok as both have same type/prec. */
3367 ctd64->cnt = weight;
3368 error = ARB_INSERT(ctdth64, ctd64tree, ctd64) == NULL ?
3371 RB_INSERT(rbctdth64, &VSD(tdgstclust64,
3372 tdgst)->rbctdtree, ctd64);
3378 VSD(tdgstclust32, tdgst)->smplcnt += weight;
3380 VSD(tdgstclust64, tdgst)->smplcnt += weight;
3383 struct rbctdth64 *rbctdtree =
3384 &VSD(tdgstclust64, tdgst)->rbctdtree;
3385 struct voistatdata_tdgstctd64 *rbctd64;
3387 ARB_FOREACH(ctd64, ctdth64, ctd64tree) {
3388 rbctd64 = (i == 0 ? RB_MIN(rbctdth64, rbctdtree) :
3389 RB_NEXT(rbctdth64, rbctdtree, rbctd64));
3391 if (i >= ARB_CURNODES(ctd64tree)
3393 || ARB_MIN(ctdth64, ctd64tree) !=
3394 RB_MIN(rbctdth64, rbctdtree)
3395 || ARB_MAX(ctdth64, ctd64tree) !=
3396 RB_MAX(rbctdth64, rbctdtree)
3397 || ARB_LEFTIDX(ctd64, ctdlnk) !=
3398 ARB_SELFIDX(ctd64tree, RB_LEFT(rbctd64, rblnk))
3399 || ARB_RIGHTIDX(ctd64, ctdlnk) !=
3400 ARB_SELFIDX(ctd64tree, RB_RIGHT(rbctd64, rblnk))
3401 || ARB_PARENTIDX(ctd64, ctdlnk) !=
3402 ARB_SELFIDX(ctd64tree,
3403 RB_PARENT(rbctd64, rblnk))) {
3404 Q_TOSTR(ctd64->mu, -1, 10, qstr, sizeof(qstr));
3405 printf("ARB ctd=%3d p=%3d l=%3d r=%3d c=%2d "
3407 (int)ARB_SELFIDX(ctd64tree, ctd64),
3408 ARB_PARENTIDX(ctd64, ctdlnk),
3409 ARB_LEFTIDX(ctd64, ctdlnk),
3410 ARB_RIGHTIDX(ctd64, ctdlnk),
3411 ARB_COLOR(ctd64, ctdlnk),
3414 Q_TOSTR(rbctd64->mu, -1, 10, qstr,
3416 struct voistatdata_tdgstctd64 *parent;
3417 parent = RB_PARENT(rbctd64, rblnk);
3419 parent == NULL ? 0 :
3420 RB_LEFT(parent, rblnk) == rbctd64 ?
3421 (_RB_BITSUP(parent, rblnk) & _RB_L) != 0 :
3422 (_RB_BITSUP(parent, rblnk) & _RB_R) != 0;
3423 printf(" RB ctd=%3d p=%3d l=%3d r=%3d c=%2d "
3425 (int)ARB_SELFIDX(ctd64tree, rbctd64),
3426 (int)ARB_SELFIDX(ctd64tree,
3427 RB_PARENT(rbctd64, rblnk)),
3428 (int)ARB_SELFIDX(ctd64tree,
3429 RB_LEFT(rbctd64, rblnk)),
3430 (int)ARB_SELFIDX(ctd64tree,
3431 RB_RIGHT(rbctd64, rblnk)),
3435 panic("RB@%p and ARB@%p trees differ\n",
3436 rbctdtree, ctd64tree);
3440 #endif /* DIAGNOSTIC */
3447 stats_v1_voi_update_tdgst(enum vsd_dtype voi_dtype, struct voistatdata *voival,
3448 struct voistat *vs, struct voistatdata_tdgst *tdgst)
3455 switch (vs->dtype) {
3456 case VSD_DTYPE_TDGSTCLUST32:
3457 /* Use same precision as the user's centroids. */
3458 Q_INI(&x, 0, 0, Q_NFBITS(
3459 ARB_CNODE(&VSD(tdgstclust32, tdgst)->ctdtree, 0)->mu));
3461 case VSD_DTYPE_TDGSTCLUST64:
3462 /* Use same precision as the user's centroids. */
3463 Q_INI(&x, 0, 0, Q_NFBITS(
3464 ARB_CNODE(&VSD(tdgstclust64, tdgst)->ctdtree, 0)->mu));
3467 KASSERT(vs->dtype == VSD_DTYPE_TDGSTCLUST32 ||
3468 vs->dtype == VSD_DTYPE_TDGSTCLUST64,
3469 ("%s: vs->dtype(%d) != VSD_DTYPE_TDGSTCLUST<32|64>",
3470 __func__, vs->dtype));
3475 * XXXLAS: Should have both a signed and unsigned 'x' variable to avoid
3476 * returning EOVERFLOW if the voival would have fit in a u64q_t.
3478 switch (voi_dtype) {
3479 case VSD_DTYPE_INT_S32:
3480 error = Q_QCPYVALI(&x, voival->int32.s32);
3482 case VSD_DTYPE_INT_U32:
3483 error = Q_QCPYVALI(&x, voival->int32.u32);
3485 case VSD_DTYPE_INT_S64:
3486 error = Q_QCPYVALI(&x, voival->int64.s64);
3488 case VSD_DTYPE_INT_U64:
3489 error = Q_QCPYVALI(&x, voival->int64.u64);
3491 case VSD_DTYPE_INT_SLONG:
3492 error = Q_QCPYVALI(&x, voival->intlong.slong);
3494 case VSD_DTYPE_INT_ULONG:
3495 error = Q_QCPYVALI(&x, voival->intlong.ulong);
3497 case VSD_DTYPE_Q_S32:
3498 error = Q_QCPYVALQ(&x, voival->q32.sq32);
3500 case VSD_DTYPE_Q_U32:
3501 error = Q_QCPYVALQ(&x, voival->q32.uq32);
3503 case VSD_DTYPE_Q_S64:
3504 error = Q_QCPYVALQ(&x, voival->q64.sq64);
3506 case VSD_DTYPE_Q_U64:
3507 error = Q_QCPYVALQ(&x, voival->q64.uq64);
3515 (error = stats_v1_vsd_tdgst_add(vs->dtype, tdgst, x, 1, 1)))
3518 vs->flags |= VS_VSDVALID;
3523 stats_v1_voi_update(struct statsblobv1 *sb, int32_t voi_id,
3524 enum vsd_dtype voi_dtype, struct voistatdata *voival, uint32_t flags)
3528 void *statevsd, *vsd;
3529 int error, i, tmperr;
3533 if (sb == NULL || sb->abi != STATS_ABI_V1 || voi_id >= NVOIS(sb) ||
3534 voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES || voival == NULL)
3536 v = &sb->vois[voi_id];
3537 if (voi_dtype != v->dtype || v->id < 0 ||
3538 ((flags & SB_VOI_RELUPDATE) && !(v->flags & VOI_REQSTATE)))
3541 vs = BLOB_OFFSET(sb, v->stats_off);
3542 if (v->flags & VOI_REQSTATE)
3543 statevsd = BLOB_OFFSET(sb, vs->data_off);
3547 if (flags & SB_VOI_RELUPDATE) {
3548 switch (voi_dtype) {
3549 case VSD_DTYPE_INT_S32:
3550 voival->int32.s32 +=
3551 VSD(voistate, statevsd)->prev.int32.s32;
3553 case VSD_DTYPE_INT_U32:
3554 voival->int32.u32 +=
3555 VSD(voistate, statevsd)->prev.int32.u32;
3557 case VSD_DTYPE_INT_S64:
3558 voival->int64.s64 +=
3559 VSD(voistate, statevsd)->prev.int64.s64;
3561 case VSD_DTYPE_INT_U64:
3562 voival->int64.u64 +=
3563 VSD(voistate, statevsd)->prev.int64.u64;
3565 case VSD_DTYPE_INT_SLONG:
3566 voival->intlong.slong +=
3567 VSD(voistate, statevsd)->prev.intlong.slong;
3569 case VSD_DTYPE_INT_ULONG:
3570 voival->intlong.ulong +=
3571 VSD(voistate, statevsd)->prev.intlong.ulong;
3573 case VSD_DTYPE_Q_S32:
3574 error = Q_QADDQ(&voival->q32.sq32,
3575 VSD(voistate, statevsd)->prev.q32.sq32);
3577 case VSD_DTYPE_Q_U32:
3578 error = Q_QADDQ(&voival->q32.uq32,
3579 VSD(voistate, statevsd)->prev.q32.uq32);
3581 case VSD_DTYPE_Q_S64:
3582 error = Q_QADDQ(&voival->q64.sq64,
3583 VSD(voistate, statevsd)->prev.q64.sq64);
3585 case VSD_DTYPE_Q_U64:
3586 error = Q_QADDQ(&voival->q64.uq64,
3587 VSD(voistate, statevsd)->prev.q64.uq64);
3590 KASSERT(0, ("Unknown VOI data type %d", voi_dtype));
3598 for (i = v->voistatmaxid; i > 0; i--) {
3599 vs = &((struct voistat *)BLOB_OFFSET(sb, v->stats_off))[i];
3603 vsd = BLOB_OFFSET(sb, vs->data_off);
3605 switch (vs->stype) {
3607 tmperr = stats_v1_voi_update_max(voi_dtype, voival,
3611 tmperr = stats_v1_voi_update_min(voi_dtype, voival,
3615 tmperr = stats_v1_voi_update_sum(voi_dtype, voival,
3619 tmperr = stats_v1_voi_update_hist(voi_dtype, voival,
3622 case VS_STYPE_TDGST:
3623 tmperr = stats_v1_voi_update_tdgst(voi_dtype, voival,
3627 KASSERT(0, ("Unknown VOI stat type %d", vs->stype));
3638 switch (voi_dtype) {
3639 case VSD_DTYPE_INT_S32:
3640 VSD(voistate, statevsd)->prev.int32.s32 =
3643 case VSD_DTYPE_INT_U32:
3644 VSD(voistate, statevsd)->prev.int32.u32 =
3647 case VSD_DTYPE_INT_S64:
3648 VSD(voistate, statevsd)->prev.int64.s64 =
3651 case VSD_DTYPE_INT_U64:
3652 VSD(voistate, statevsd)->prev.int64.u64 =
3655 case VSD_DTYPE_INT_SLONG:
3656 VSD(voistate, statevsd)->prev.intlong.slong =
3657 voival->intlong.slong;
3659 case VSD_DTYPE_INT_ULONG:
3660 VSD(voistate, statevsd)->prev.intlong.ulong =
3661 voival->intlong.ulong;
3663 case VSD_DTYPE_Q_S32:
3665 &VSD(voistate, statevsd)->prev.q32.sq32,
3668 case VSD_DTYPE_Q_U32:
3670 &VSD(voistate, statevsd)->prev.q32.uq32,
3673 case VSD_DTYPE_Q_S64:
3675 &VSD(voistate, statevsd)->prev.q64.sq64,
3678 case VSD_DTYPE_Q_U64:
3680 &VSD(voistate, statevsd)->prev.q64.uq64,
3684 KASSERT(0, ("Unknown VOI data type %d", voi_dtype));
3695 stats_init(void *arg)
3699 SYSINIT(stats, SI_SUB_KDTRACE, SI_ORDER_FIRST, stats_init, NULL);
3702 * Sysctl handler to display the list of available stats templates.
3705 stats_tpl_list_available(SYSCTL_HANDLER_ARGS)
3712 /* We can tolerate ntpl being stale, so do not take the lock. */
3713 s = sbuf_new(NULL, NULL, /* +1 per tpl for , */
3714 ntpl * (STATS_TPL_MAX_STR_SPEC_LEN + 1), SBUF_FIXEDLEN);
3719 for (i = 0; i < ntpl; i++) {
3720 err = sbuf_printf(s, "%s\"%s\":%u", i ? "," : "",
3721 tpllist[i]->mb->tplname, tpllist[i]->mb->tplhash);
3723 /* Sbuf overflow condition. */
3732 err = sysctl_handle_string(oidp, sbuf_data(s), 0, req);
3740 * Called by subsystem-specific sysctls to report and/or parse the list of
3741 * templates being sampled and their sampling rates. A stats_tpl_sr_cb_t
3742 * conformant function pointer must be passed in as arg1, which is used to
3743 * interact with the subsystem's stats template sample rates list. If arg2 > 0,
3744 * a zero-initialised allocation of arg2-sized contextual memory is
3745 * heap-allocated and passed in to all subsystem callbacks made during the
3746 * operation of stats_tpl_sample_rates().
3748 * XXXLAS: Assumes templates are never removed, which is currently true but may
3749 * need to be reworked in future if dynamic template management becomes a
3750 * requirement e.g. to support kernel module based templates.
3753 stats_tpl_sample_rates(SYSCTL_HANDLER_ARGS)
3755 char kvpair_fmt[16], tplspec_fmt[16];
3756 char tpl_spec[STATS_TPL_MAX_STR_SPEC_LEN];
3757 char tpl_name[TPL_MAX_NAME_LEN + 2]; /* +2 for "" */
3758 stats_tpl_sr_cb_t subsys_cb;
3760 char *buf, *new_rates_usr_str, *tpl_name_p;
3761 struct stats_tpl_sample_rate *rates;
3763 uint32_t cum_pct, pct, tpl_hash;
3764 int err, i, off, len, newlen, nrates;
3769 subsys_cb = (stats_tpl_sr_cb_t)arg1;
3770 KASSERT(subsys_cb != NULL, ("%s: subsys_cb == arg1 == NULL", __func__));
3772 subsys_ctx = malloc(arg2, M_TEMP, M_WAITOK | M_ZERO);
3776 /* Grab current count of subsystem rates. */
3777 err = subsys_cb(TPL_SR_UNLOCKED_GET, NULL, &nrates, subsys_ctx);
3781 /* +1 to ensure we can append '\0' post copyin, +5 per rate for =nnn, */
3782 len = max(req->newlen + 1, nrates * (STATS_TPL_MAX_STR_SPEC_LEN + 5));
3784 if (req->oldptr != NULL || req->newptr != NULL)
3785 buf = malloc(len, M_TEMP, M_WAITOK);
3787 if (req->oldptr != NULL) {
3789 /* No rates, so return an empty string via oldptr. */
3790 err = SYSCTL_OUT(req, "", 1);
3796 s = sbuf_new(&_s, buf, len, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
3798 /* Grab locked count of, and ptr to, subsystem rates. */
3799 err = subsys_cb(TPL_SR_RLOCKED_GET, &rates, &nrates,
3804 for (i = 0; i < nrates && !err; i++) {
3805 err = sbuf_printf(s, "%s\"%s\":%u=%u", i ? "," : "",
3806 tpllist[rates[i].tpl_slot_id]->mb->tplname,
3807 tpllist[rates[i].tpl_slot_id]->mb->tplhash,
3808 rates[i].tpl_sample_pct);
3811 /* Tell subsystem that we're done with its rates list. */
3812 err = subsys_cb(TPL_SR_RUNLOCK, &rates, &nrates, subsys_ctx);
3816 err = sbuf_finish(s);
3818 goto done; /* We lost a race for buf to be too small. */
3820 /* Return the rendered string data via oldptr. */
3821 err = SYSCTL_OUT(req, sbuf_data(s), sbuf_len(s));
3823 /* Return the upper bound size for buffer sizing requests. */
3824 err = SYSCTL_OUT(req, NULL, len);
3828 if (err || req->newptr == NULL)
3831 newlen = req->newlen - req->newidx;
3832 err = SYSCTL_IN(req, buf, newlen);
3837 * Initialise format strings at run time.
3839 * Write the max template spec string length into the
3840 * template_spec=percent key-value pair parsing format string as:
3841 * " %<width>[^=]=%u %n"
3843 * Write the max template name string length into the tplname:tplhash
3844 * parsing format string as:
3847 * Subtract 1 for \0 appended by sscanf().
3849 sprintf(kvpair_fmt, " %%%zu[^=]=%%u %%n", sizeof(tpl_spec) - 1);
3850 sprintf(tplspec_fmt, "%%%zu[^:]:%%u", sizeof(tpl_name) - 1);
3853 * Parse each CSV key-value pair specifying a template and its sample
3854 * percentage. Whitespace either side of a key-value pair is ignored.
3855 * Templates can be specified by name, hash, or name and hash per the
3856 * following formats (chars in [] are optional):
3857 * ["]<tplname>["]=<percent>
3859 * ["]<tplname>["]:hash=<percent>
3861 cum_pct = nrates = 0;
3863 buf[newlen] = '\0'; /* buf is at least newlen+1 in size. */
3864 new_rates_usr_str = buf;
3865 while (isspace(*new_rates_usr_str))
3866 new_rates_usr_str++; /* Skip leading whitespace. */
3867 while (*new_rates_usr_str != '\0') {
3868 tpl_name_p = tpl_name;
3874 * Parse key-value pair which must perform 2 conversions, then
3875 * parse the template spec to extract either name, hash, or name
3876 * and hash depending on the three possible spec formats. The
3877 * tplspec_fmt format specifier parses name or name and hash
3878 * template specs, while the ":%u" format specifier parses
3879 * hash-only template specs. If parsing is successfull, ensure
3880 * the cumulative sampling percentage does not exceed 100.
3883 if (2 != sscanf(new_rates_usr_str, kvpair_fmt, tpl_spec, &pct,
3886 if ((1 > sscanf(tpl_spec, tplspec_fmt, tpl_name, &tpl_hash)) &&
3887 (1 != sscanf(tpl_spec, ":%u", &tpl_hash)))
3889 if ((cum_pct += pct) > 100)
3893 /* Strip surrounding "" from template name if present. */
3894 len = strlen(tpl_name);
3896 if (tpl_name[len - 1] == '"')
3897 tpl_name[--len] = '\0';
3898 if (tpl_name[0] == '"') {
3904 rates = stats_realloc(rates, 0, /* oldsz is unused in kernel. */
3905 (nrates + 1) * sizeof(*rates), M_WAITOK);
3906 rates[nrates].tpl_slot_id =
3907 stats_tpl_fetch_allocid(len ? tpl_name_p : NULL, tpl_hash);
3908 if (rates[nrates].tpl_slot_id < 0) {
3909 err = -rates[nrates].tpl_slot_id;
3912 rates[nrates].tpl_sample_pct = pct;
3914 new_rates_usr_str += off;
3915 if (*new_rates_usr_str != ',')
3916 break; /* End-of-input or malformed. */
3917 new_rates_usr_str++; /* Move past comma to next pair. */
3921 if ((new_rates_usr_str - buf) < newlen) {
3922 /* Entire input has not been consumed. */
3926 * Give subsystem the new rates. They'll return the
3927 * appropriate rates pointer for us to garbage collect.
3929 err = subsys_cb(TPL_SR_PUT, &rates, &nrates,
3937 free(subsys_ctx, M_TEMP);
3941 SYSCTL_NODE(_kern, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
3944 SYSCTL_PROC(_kern_stats, OID_AUTO, templates,
3945 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3946 stats_tpl_list_available, "A",
3947 "list the name/hash of all available stats(9) templates");
3949 #else /* ! _KERNEL */
3951 static void __attribute__ ((constructor))
3952 stats_constructor(void)
3955 pthread_rwlock_init(&tpllistlock, NULL);
3958 static void __attribute__ ((destructor))
3959 stats_destructor(void)
3962 pthread_rwlock_destroy(&tpllistlock);
3965 #endif /* _KERNEL */