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)
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 error = copyout_nofault(&(src->cursz), &((*dst)->cursz),
1136 else if (flags & SB_CLONE_USRDST)
1137 error = copyout(&(src->cursz), &((*dst)->cursz),
1141 memcpy(&((*dst)->cursz), &(src->cursz), postcurszlen);
1151 stats_v1_tpl_alloc(const char *name, uint32_t flags __unused)
1153 struct statsblobv1_tpl *tpl, **newtpllist;
1154 struct statsblobv1 *tpl_sb;
1155 struct metablob *tpl_mb;
1158 if (name != NULL && strlen(name) > TPL_MAX_NAME_LEN)
1161 if (name != NULL && stats_tpl_fetch_allocid(name, 0) >= 0)
1164 tpl = stats_realloc(NULL, 0, sizeof(struct statsblobv1_tpl), M_ZERO);
1165 tpl_mb = stats_realloc(NULL, 0, sizeof(struct metablob), M_ZERO);
1166 tpl_sb = stats_realloc(NULL, 0, sizeof(struct statsblobv1), M_ZERO);
1168 if (tpl_mb != NULL && name != NULL)
1169 tpl_mb->tplname = stats_strdup(name, 0);
1171 if (tpl == NULL || tpl_sb == NULL || tpl_mb == NULL ||
1172 tpl_mb->tplname == NULL) {
1175 if (tpl_mb != NULL) {
1176 stats_free(tpl_mb->tplname);
1185 tpl_sb->abi = STATS_ABI_V1;
1187 #if BYTE_ORDER == LITTLE_ENDIAN
1189 #elif BYTE_ORDER == BIG_ENDIAN
1194 tpl_sb->cursz = tpl_sb->maxsz = sizeof(struct statsblobv1);
1195 tpl_sb->stats_off = tpl_sb->statsdata_off = sizeof(struct statsblobv1);
1198 newtpllist = stats_realloc(tpllist, ntpl * sizeof(void *),
1199 (ntpl + 1) * sizeof(void *), 0);
1200 if (newtpllist != NULL) {
1202 tpllist = (struct statsblob_tpl **)newtpllist;
1203 tpllist[tpl_id] = (struct statsblob_tpl *)tpl;
1204 stats_tpl_update_hash(tpllist[tpl_id]);
1208 if (tpl_mb != NULL) {
1209 stats_free(tpl_mb->tplname);
1220 stats_v1_tpl_add_voistats(uint32_t tpl_id, int32_t voi_id, const char *voi_name,
1221 enum vsd_dtype voi_dtype, uint32_t nvss, struct voistatspec *vss,
1225 struct voistat *tmpstat;
1226 struct statsblobv1 *tpl_sb;
1227 struct metablob *tpl_mb;
1228 int error, i, newstatdataidx, newvoibytes, newvoistatbytes,
1229 newvoistatdatabytes, newvoistatmaxid;
1232 if (voi_id < 0 || voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES ||
1233 nvss == 0 || vss == NULL)
1236 error = nbytes = newvoibytes = newvoistatbytes =
1237 newvoistatdatabytes = 0;
1238 newvoistatmaxid = -1;
1240 /* Calculate the number of bytes required for the new voistats. */
1241 for (i = nvss - 1; i >= 0; i--) {
1242 if (vss[i].stype == 0 || vss[i].stype >= VS_NUM_STYPES ||
1243 vss[i].vs_dtype == 0 || vss[i].vs_dtype >= VSD_NUM_DTYPES ||
1244 vss[i].iv == NULL || vss[i].vsdsz == 0)
1246 if ((int)vss[i].stype > newvoistatmaxid)
1247 newvoistatmaxid = vss[i].stype;
1248 newvoistatdatabytes += vss[i].vsdsz;
1251 if (flags & SB_VOI_RELUPDATE) {
1252 /* XXXLAS: VOI state bytes may need to vary based on stat types. */
1253 newvoistatdatabytes += sizeof(struct voistatdata_voistate);
1255 nbytes += newvoistatdatabytes;
1258 if (tpl_id < ntpl) {
1259 tpl_sb = (struct statsblobv1 *)tpllist[tpl_id]->sb;
1260 tpl_mb = tpllist[tpl_id]->mb;
1262 if (voi_id >= NVOIS(tpl_sb) || tpl_sb->vois[voi_id].id == -1) {
1263 /* Adding a new VOI and associated stats. */
1264 if (voi_id >= NVOIS(tpl_sb)) {
1265 /* We need to grow the tpl_sb->vois array. */
1266 newvoibytes = (voi_id - (NVOIS(tpl_sb) - 1)) *
1268 nbytes += newvoibytes;
1271 (newvoistatmaxid + 1) * sizeof(struct voistat);
1273 /* Adding stats to an existing VOI. */
1274 if (newvoistatmaxid >
1275 tpl_sb->vois[voi_id].voistatmaxid) {
1276 newvoistatbytes = (newvoistatmaxid -
1277 tpl_sb->vois[voi_id].voistatmaxid) *
1278 sizeof(struct voistat);
1280 /* XXXLAS: KPI does not yet support expanding VOIs. */
1283 nbytes += newvoistatbytes;
1285 if (!error && newvoibytes > 0) {
1286 struct voi_meta *voi_meta = tpl_mb->voi_meta;
1288 voi_meta = stats_realloc(voi_meta, voi_meta == NULL ?
1289 0 : NVOIS(tpl_sb) * sizeof(struct voi_meta),
1290 (1 + voi_id) * sizeof(struct voi_meta),
1293 if (voi_meta == NULL)
1296 tpl_mb->voi_meta = voi_meta;
1300 /* NB: Resizing can change where tpl_sb points. */
1301 error = stats_v1_blob_expand(&tpl_sb, newvoibytes,
1302 newvoistatbytes, newvoistatdatabytes);
1306 tpl_mb->voi_meta[voi_id].name = stats_strdup(voi_name,
1308 if (tpl_mb->voi_meta[voi_id].name == NULL)
1313 /* Update the template list with the resized pointer. */
1314 tpllist[tpl_id]->sb = (struct statsblob *)tpl_sb;
1316 /* Update the template. */
1317 voi = &tpl_sb->vois[voi_id];
1320 /* VOI is new and needs to be initialised. */
1322 voi->dtype = voi_dtype;
1323 voi->stats_off = tpl_sb->stats_off;
1324 if (flags & SB_VOI_RELUPDATE)
1325 voi->flags |= VOI_REQSTATE;
1328 * XXXLAS: When this else block is written, the
1329 * "KPI does not yet support expanding VOIs"
1330 * error earlier in this function can be
1331 * removed. What is required here is to shuffle
1332 * the voistat array such that the new stats for
1333 * the voi are contiguous, which will displace
1334 * stats for other vois that reside after the
1335 * voi being updated. The other vois then need
1336 * to have their stats_off adjusted post
1341 voi->voistatmaxid = newvoistatmaxid;
1344 if (voi->flags & VOI_REQSTATE) {
1345 /* Initialise the voistate stat in slot 0. */
1346 tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off);
1347 tmpstat->stype = VS_STYPE_VOISTATE;
1349 tmpstat->dtype = VSD_DTYPE_VOISTATE;
1350 newstatdataidx = tmpstat->dsz =
1351 sizeof(struct voistatdata_numeric);
1352 tmpstat->data_off = tpl_sb->statsdata_off;
1355 for (i = 0; (uint32_t)i < nvss; i++) {
1356 tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off +
1357 (vss[i].stype * sizeof(struct voistat)));
1358 KASSERT(tmpstat->stype < 0, ("voistat %p "
1359 "already initialised", tmpstat));
1360 tmpstat->stype = vss[i].stype;
1361 tmpstat->flags = vss[i].flags;
1362 tmpstat->dtype = vss[i].vs_dtype;
1363 tmpstat->dsz = vss[i].vsdsz;
1364 tmpstat->data_off = tpl_sb->statsdata_off +
1366 memcpy(BLOB_OFFSET(tpl_sb, tmpstat->data_off),
1367 vss[i].iv, vss[i].vsdsz);
1368 newstatdataidx += vss[i].vsdsz;
1371 /* Update the template version hash. */
1372 stats_tpl_update_hash(tpllist[tpl_id]);
1373 /* XXXLAS: Confirm tpl name/hash pair remains unique. */
1382 struct statsblobv1 *
1383 stats_v1_blob_alloc(uint32_t tpl_id, uint32_t flags __unused)
1385 struct statsblobv1 *sb;
1391 if (tpl_id < ntpl) {
1392 sb = stats_realloc(NULL, 0, tpllist[tpl_id]->sb->maxsz, 0);
1394 sb->maxsz = tpllist[tpl_id]->sb->maxsz;
1395 error = stats_v1_blob_init_locked(sb, tpl_id, 0);
1410 stats_v1_blob_destroy(struct statsblobv1 *sb)
1417 stats_v1_voistat_fetch_dptr(struct statsblobv1 *sb, int32_t voi_id,
1418 enum voi_stype stype, enum vsd_dtype *retdtype, struct voistatdata **retvsd,
1424 if (retvsd == NULL || sb == NULL || sb->abi != STATS_ABI_V1 ||
1425 voi_id >= NVOIS(sb))
1428 v = &sb->vois[voi_id];
1429 if ((__typeof(v->voistatmaxid))stype > v->voistatmaxid)
1432 vs = BLOB_OFFSET(sb, v->stats_off + (stype * sizeof(struct voistat)));
1433 *retvsd = BLOB_OFFSET(sb, vs->data_off);
1434 if (retdtype != NULL)
1435 *retdtype = vs->dtype;
1436 if (retvsdsz != NULL)
1437 *retvsdsz = vs->dsz;
1443 stats_v1_blob_init(struct statsblobv1 *sb, uint32_t tpl_id, uint32_t flags)
1450 if (sb == NULL || tpl_id >= ntpl) {
1453 error = stats_v1_blob_init_locked(sb, tpl_id, flags);
1461 stats_v1_blob_init_locked(struct statsblobv1 *sb, uint32_t tpl_id,
1462 uint32_t flags __unused)
1466 TPL_LIST_RLOCK_ASSERT();
1467 error = (sb->maxsz >= tpllist[tpl_id]->sb->cursz) ? 0 : EOVERFLOW;
1469 ("sb %d instead of %d bytes", sb->maxsz, tpllist[tpl_id]->sb->cursz));
1472 memcpy(sb, tpllist[tpl_id]->sb, tpllist[tpl_id]->sb->cursz);
1473 sb->created = sb->lastrst = stats_sbinuptime();
1474 sb->tplhash = tpllist[tpl_id]->mb->tplhash;
1481 stats_v1_blob_expand(struct statsblobv1 **sbpp, int newvoibytes,
1482 int newvoistatbytes, int newvoistatdatabytes)
1484 struct statsblobv1 *sb;
1486 struct voistat *tmpvoistat, *voistat_array;
1487 int error, i, idxnewvois, idxnewvoistats, nbytes, nvoistats;
1489 KASSERT(newvoibytes % sizeof(struct voi) == 0,
1490 ("Bad newvoibytes %d", newvoibytes));
1491 KASSERT(newvoistatbytes % sizeof(struct voistat) == 0,
1492 ("Bad newvoistatbytes %d", newvoistatbytes));
1494 error = ((newvoibytes % sizeof(struct voi) == 0) &&
1495 (newvoistatbytes % sizeof(struct voistat) == 0)) ? 0 : EINVAL;
1497 nbytes = newvoibytes + newvoistatbytes + newvoistatdatabytes;
1500 * XXXLAS: Required until we gain support for flags which alter the
1501 * units of size/offset fields in key structs.
1503 if (!error && ((((int)sb->cursz) + nbytes) > SB_V1_MAXSZ))
1506 if (!error && (sb->cursz + nbytes > sb->maxsz)) {
1507 /* Need to expand our blob. */
1508 sb = stats_realloc(sb, sb->maxsz, sb->cursz + nbytes, M_ZERO);
1510 sb->maxsz = sb->cursz + nbytes;
1518 * Shuffle memory within the expanded blob working from the end
1519 * backwards, leaving gaps for the new voistat and voistatdata
1520 * structs at the beginning of their respective blob regions,
1521 * and for the new voi structs at the end of their blob region.
1523 memmove(BLOB_OFFSET(sb, sb->statsdata_off + nbytes),
1524 BLOB_OFFSET(sb, sb->statsdata_off),
1525 sb->cursz - sb->statsdata_off);
1526 memmove(BLOB_OFFSET(sb, sb->stats_off + newvoibytes +
1527 newvoistatbytes), BLOB_OFFSET(sb, sb->stats_off),
1528 sb->statsdata_off - sb->stats_off);
1530 /* First index of new voi/voistat structs to be initialised. */
1531 idxnewvois = NVOIS(sb);
1532 idxnewvoistats = (newvoistatbytes / sizeof(struct voistat)) - 1;
1534 /* Update housekeeping variables and offsets. */
1535 sb->cursz += nbytes;
1536 sb->stats_off += newvoibytes;
1537 sb->statsdata_off += newvoibytes + newvoistatbytes;
1539 /* XXXLAS: Zeroing not strictly needed but aids debugging. */
1540 memset(&sb->vois[idxnewvois], '\0', newvoibytes);
1541 memset(BLOB_OFFSET(sb, sb->stats_off), '\0',
1543 memset(BLOB_OFFSET(sb, sb->statsdata_off), '\0',
1544 newvoistatdatabytes);
1546 /* Initialise new voi array members and update offsets. */
1547 for (i = 0; i < NVOIS(sb); i++) {
1548 tmpvoi = &sb->vois[i];
1549 if (i >= idxnewvois) {
1550 tmpvoi->id = tmpvoi->voistatmaxid = -1;
1551 } else if (tmpvoi->id > -1) {
1552 tmpvoi->stats_off += newvoibytes +
1557 /* Initialise new voistat array members and update offsets. */
1558 nvoistats = (sb->statsdata_off - sb->stats_off) /
1559 sizeof(struct voistat);
1560 voistat_array = BLOB_OFFSET(sb, sb->stats_off);
1561 for (i = 0; i < nvoistats; i++) {
1562 tmpvoistat = &voistat_array[i];
1563 if (i <= idxnewvoistats) {
1564 tmpvoistat->stype = -1;
1565 } else if (tmpvoistat->stype > -1) {
1566 tmpvoistat->data_off += nbytes;
1575 stats_v1_blob_finalise(struct statsblobv1 *sb __unused)
1578 /* XXXLAS: Fill this in. */
1582 stats_v1_blob_iter(struct statsblobv1 *sb, stats_v1_blob_itercb_t icb,
1583 void *usrctx, uint32_t flags)
1587 struct sb_iter_ctx ctx;
1590 ctx.usrctx = usrctx;
1591 ctx.flags = SB_IT_FIRST_CB;
1594 for (i = 0; i < NVOIS(sb); i++) {
1598 ctx.flags |= SB_IT_FIRST_VOISTAT;
1601 ctx.flags |= SB_IT_FIRST_VOI;
1602 else if (i == (NVOIS(sb) - 1))
1603 ctx.flags |= SB_IT_LAST_VOI | SB_IT_LAST_CB;
1605 if (v->id < 0 && (flags & SB_IT_NULLVOI)) {
1606 if (icb(sb, v, NULL, &ctx))
1609 ctx.flags &= ~SB_IT_FIRST_CB;
1612 /* If NULL voi, v->voistatmaxid == -1 */
1613 for (j = 0; j <= v->voistatmaxid; j++) {
1614 vs = &((struct voistat *)BLOB_OFFSET(sb,
1616 if (vs->stype < 0 &&
1617 !(flags & SB_IT_NULLVOISTAT))
1620 if (j == v->voistatmaxid) {
1621 ctx.flags |= SB_IT_LAST_VOISTAT;
1622 if (i == (NVOIS(sb) - 1))
1626 ctx.flags &= ~SB_IT_LAST_CB;
1629 if (icb(sb, v, vs, &ctx))
1632 ctx.flags &= ~(SB_IT_FIRST_CB | SB_IT_FIRST_VOISTAT |
1633 SB_IT_LAST_VOISTAT);
1635 ctx.flags &= ~(SB_IT_FIRST_VOI | SB_IT_LAST_VOI);
1640 stats_voistatdata_tdgst_tostr(enum vsd_dtype voi_dtype __unused,
1641 const struct voistatdata_tdgst *tdgst, enum vsd_dtype tdgst_dtype,
1642 size_t tdgst_dsz __unused, enum sb_str_fmt fmt, struct sbuf *buf, int objdump)
1644 const struct ctdth32 *ctd32tree;
1645 const struct ctdth64 *ctd64tree;
1646 const struct voistatdata_tdgstctd32 *ctd32;
1647 const struct voistatdata_tdgstctd64 *ctd64;
1649 uint64_t smplcnt, compcnt;
1650 int is32bit, qmaxstrlen;
1651 uint16_t maxctds, curctds;
1653 switch (tdgst_dtype) {
1654 case VSD_DTYPE_TDGSTCLUST32:
1655 smplcnt = CONSTVSD(tdgstclust32, tdgst)->smplcnt;
1656 compcnt = CONSTVSD(tdgstclust32, tdgst)->compcnt;
1657 maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree);
1658 curctds = ARB_CURNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree);
1659 ctd32tree = &CONSTVSD(tdgstclust32, tdgst)->ctdtree;
1660 ctd32 = (objdump ? ARB_CNODE(ctd32tree, 0) :
1661 ARB_CMIN(ctdth32, ctd32tree));
1662 qmaxstrlen = (ctd32 == NULL) ? 1 : Q_MAXSTRLEN(ctd32->mu, 10);
1667 case VSD_DTYPE_TDGSTCLUST64:
1668 smplcnt = CONSTVSD(tdgstclust64, tdgst)->smplcnt;
1669 compcnt = CONSTVSD(tdgstclust64, tdgst)->compcnt;
1670 maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree);
1671 curctds = ARB_CURNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree);
1672 ctd64tree = &CONSTVSD(tdgstclust64, tdgst)->ctdtree;
1673 ctd64 = (objdump ? ARB_CNODE(ctd64tree, 0) :
1674 ARB_CMIN(ctdth64, ctd64tree));
1675 qmaxstrlen = (ctd64 == NULL) ? 1 : Q_MAXSTRLEN(ctd64->mu, 10);
1685 case SB_STRFMT_FREEFORM:
1686 fmtstr = "smplcnt=%ju, compcnt=%ju, maxctds=%hu, nctds=%hu";
1688 case SB_STRFMT_JSON:
1691 "\"smplcnt\":%ju,\"compcnt\":%ju,\"maxctds\":%hu,"
1692 "\"nctds\":%hu,\"ctds\":[";
1695 sbuf_printf(buf, fmtstr, (uintmax_t)smplcnt, (uintmax_t)compcnt,
1698 while ((is32bit ? NULL != ctd32 : NULL != ctd64)) {
1699 char qstr[qmaxstrlen];
1702 case SB_STRFMT_FREEFORM:
1703 fmtstr = "\n\t\t\t\t";
1705 case SB_STRFMT_JSON:
1710 sbuf_cat(buf, fmtstr);
1714 case SB_STRFMT_FREEFORM:
1715 fmtstr = "ctd[%hu].";
1717 case SB_STRFMT_JSON:
1719 fmtstr = "\"ctd\":%hu,";
1722 sbuf_printf(buf, fmtstr, is32bit ?
1723 ARB_SELFIDX(ctd32tree, ctd32) :
1724 ARB_SELFIDX(ctd64tree, ctd64));
1728 case SB_STRFMT_FREEFORM:
1731 case SB_STRFMT_JSON:
1736 sbuf_cat(buf, fmtstr);
1737 Q_TOSTR((is32bit ? ctd32->mu : ctd64->mu), -1, 10, qstr,
1739 sbuf_cat(buf, qstr);
1742 case SB_STRFMT_FREEFORM:
1743 fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}";
1745 case SB_STRFMT_JSON:
1747 fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}";
1750 sbuf_printf(buf, fmtstr,
1751 is32bit ? ctd32->cnt : (uintmax_t)ctd64->cnt);
1754 ctd32 = (objdump ? ARB_CNODE(ctd32tree,
1755 ARB_SELFIDX(ctd32tree, ctd32) + 1) :
1756 ARB_CNEXT(ctdth32, ctd32tree, ctd32));
1758 ctd64 = (objdump ? ARB_CNODE(ctd64tree,
1759 ARB_SELFIDX(ctd64tree, ctd64) + 1) :
1760 ARB_CNEXT(ctdth64, ctd64tree, ctd64));
1762 if (fmt == SB_STRFMT_JSON &&
1763 (is32bit ? NULL != ctd32 : NULL != ctd64))
1764 sbuf_putc(buf, ',');
1766 if (fmt == SB_STRFMT_JSON)
1771 stats_voistatdata_hist_tostr(enum vsd_dtype voi_dtype,
1772 const struct voistatdata_hist *hist, enum vsd_dtype hist_dtype,
1773 size_t hist_dsz, enum sb_str_fmt fmt, struct sbuf *buf, int objdump)
1775 const struct voistatdata_numeric *bkt_lb, *bkt_ub;
1780 switch (hist_dtype) {
1781 case VSD_DTYPE_CRHIST32:
1782 nbkts = HIST_VSDSZ2NBKTS(crhist32, hist_dsz);
1785 case VSD_DTYPE_DRHIST32:
1786 nbkts = HIST_VSDSZ2NBKTS(drhist32, hist_dsz);
1789 case VSD_DTYPE_DVHIST32:
1790 nbkts = HIST_VSDSZ2NBKTS(dvhist32, hist_dsz);
1793 case VSD_DTYPE_CRHIST64:
1794 nbkts = HIST_VSDSZ2NBKTS(crhist64, hist_dsz);
1797 case VSD_DTYPE_DRHIST64:
1798 nbkts = HIST_VSDSZ2NBKTS(drhist64, hist_dsz);
1801 case VSD_DTYPE_DVHIST64:
1802 nbkts = HIST_VSDSZ2NBKTS(dvhist64, hist_dsz);
1810 case SB_STRFMT_FREEFORM:
1811 fmtstr = "nbkts=%hu, ";
1813 case SB_STRFMT_JSON:
1815 fmtstr = "\"nbkts\":%hu,";
1818 sbuf_printf(buf, fmtstr, nbkts);
1821 case SB_STRFMT_FREEFORM:
1822 fmtstr = (is32bit ? "oob=%u" : "oob=%ju");
1824 case SB_STRFMT_JSON:
1826 fmtstr = (is32bit ? "\"oob\":%u,\"bkts\":[" :
1827 "\"oob\":%ju,\"bkts\":[");
1830 sbuf_printf(buf, fmtstr, is32bit ? VSD_CONSTHIST_FIELDVAL(hist,
1831 hist_dtype, oob) : (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist,
1834 for (i = 0; i < nbkts; i++) {
1835 switch (hist_dtype) {
1836 case VSD_DTYPE_CRHIST32:
1837 case VSD_DTYPE_CRHIST64:
1838 bkt_lb = VSD_CONSTCRHIST_FIELDPTR(hist, hist_dtype,
1841 bkt_ub = VSD_CONSTCRHIST_FIELDPTR(hist,
1842 hist_dtype, bkts[i + 1].lb);
1844 bkt_ub = &numeric_limits[LIM_MAX][voi_dtype];
1846 case VSD_DTYPE_DRHIST32:
1847 case VSD_DTYPE_DRHIST64:
1848 bkt_lb = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype,
1850 bkt_ub = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype,
1853 case VSD_DTYPE_DVHIST32:
1854 case VSD_DTYPE_DVHIST64:
1855 bkt_lb = bkt_ub = VSD_CONSTDVHIST_FIELDPTR(hist,
1856 hist_dtype, bkts[i].val);
1863 case SB_STRFMT_FREEFORM:
1864 fmtstr = "\n\t\t\t\t";
1866 case SB_STRFMT_JSON:
1871 sbuf_cat(buf, fmtstr);
1875 case SB_STRFMT_FREEFORM:
1876 fmtstr = "bkt[%hu].";
1878 case SB_STRFMT_JSON:
1880 fmtstr = "\"bkt\":%hu,";
1883 sbuf_printf(buf, fmtstr, i);
1887 case SB_STRFMT_FREEFORM:
1890 case SB_STRFMT_JSON:
1895 sbuf_cat(buf, fmtstr);
1896 stats_voistatdata_tostr((const struct voistatdata *)bkt_lb,
1897 voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric),
1901 case SB_STRFMT_FREEFORM:
1904 case SB_STRFMT_JSON:
1906 fmtstr = ",\"ub\":";
1909 sbuf_cat(buf, fmtstr);
1910 stats_voistatdata_tostr((const struct voistatdata *)bkt_ub,
1911 voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric),
1915 case SB_STRFMT_FREEFORM:
1916 fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}";
1918 case SB_STRFMT_JSON:
1920 fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}";
1923 sbuf_printf(buf, fmtstr, is32bit ?
1924 VSD_CONSTHIST_FIELDVAL(hist, hist_dtype, bkts[i].cnt) :
1925 (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist, hist_dtype,
1928 if (fmt == SB_STRFMT_JSON && i < nbkts - 1)
1929 sbuf_putc(buf, ',');
1931 if (fmt == SB_STRFMT_JSON)
1936 stats_voistatdata_tostr(const struct voistatdata *vsd, enum vsd_dtype voi_dtype,
1937 enum vsd_dtype vsd_dtype, size_t vsd_sz, enum sb_str_fmt fmt,
1938 struct sbuf *buf, int objdump)
1942 if (vsd == NULL || buf == NULL || voi_dtype >= VSD_NUM_DTYPES ||
1943 vsd_dtype >= VSD_NUM_DTYPES || fmt >= SB_STRFMT_NUM_FMTS)
1946 switch (vsd_dtype) {
1947 case VSD_DTYPE_VOISTATE:
1949 case SB_STRFMT_FREEFORM:
1952 case SB_STRFMT_JSON:
1954 fmtstr = "\"prev\":";
1957 sbuf_cat(buf, fmtstr);
1959 * Render prev by passing it as *vsd and voi_dtype as vsd_dtype.
1961 stats_voistatdata_tostr(
1962 (const struct voistatdata *)&CONSTVSD(voistate, vsd)->prev,
1963 voi_dtype, voi_dtype, vsd_sz, fmt, buf, objdump);
1965 case VSD_DTYPE_INT_S32:
1966 sbuf_printf(buf, "%d", vsd->int32.s32);
1968 case VSD_DTYPE_INT_U32:
1969 sbuf_printf(buf, "%u", vsd->int32.u32);
1971 case VSD_DTYPE_INT_S64:
1972 sbuf_printf(buf, "%jd", (intmax_t)vsd->int64.s64);
1974 case VSD_DTYPE_INT_U64:
1975 sbuf_printf(buf, "%ju", (uintmax_t)vsd->int64.u64);
1977 case VSD_DTYPE_INT_SLONG:
1978 sbuf_printf(buf, "%ld", vsd->intlong.slong);
1980 case VSD_DTYPE_INT_ULONG:
1981 sbuf_printf(buf, "%lu", vsd->intlong.ulong);
1983 case VSD_DTYPE_Q_S32:
1985 char qstr[Q_MAXSTRLEN(vsd->q32.sq32, 10)];
1986 Q_TOSTR((s32q_t)vsd->q32.sq32, -1, 10, qstr, sizeof(qstr));
1987 sbuf_cat(buf, qstr);
1990 case VSD_DTYPE_Q_U32:
1992 char qstr[Q_MAXSTRLEN(vsd->q32.uq32, 10)];
1993 Q_TOSTR((u32q_t)vsd->q32.uq32, -1, 10, qstr, sizeof(qstr));
1994 sbuf_cat(buf, qstr);
1997 case VSD_DTYPE_Q_S64:
1999 char qstr[Q_MAXSTRLEN(vsd->q64.sq64, 10)];
2000 Q_TOSTR((s64q_t)vsd->q64.sq64, -1, 10, qstr, sizeof(qstr));
2001 sbuf_cat(buf, qstr);
2004 case VSD_DTYPE_Q_U64:
2006 char qstr[Q_MAXSTRLEN(vsd->q64.uq64, 10)];
2007 Q_TOSTR((u64q_t)vsd->q64.uq64, -1, 10, qstr, sizeof(qstr));
2008 sbuf_cat(buf, qstr);
2011 case VSD_DTYPE_CRHIST32:
2012 case VSD_DTYPE_DRHIST32:
2013 case VSD_DTYPE_DVHIST32:
2014 case VSD_DTYPE_CRHIST64:
2015 case VSD_DTYPE_DRHIST64:
2016 case VSD_DTYPE_DVHIST64:
2017 stats_voistatdata_hist_tostr(voi_dtype, CONSTVSD(hist, vsd),
2018 vsd_dtype, vsd_sz, fmt, buf, objdump);
2020 case VSD_DTYPE_TDGSTCLUST32:
2021 case VSD_DTYPE_TDGSTCLUST64:
2022 stats_voistatdata_tdgst_tostr(voi_dtype,
2023 CONSTVSD(tdgst, vsd), vsd_dtype, vsd_sz, fmt, buf,
2030 return (sbuf_error(buf));
2034 stats_v1_itercb_tostr_freeform(struct statsblobv1 *sb, struct voi *v,
2035 struct voistat *vs, struct sb_iter_ctx *ctx)
2037 struct sb_tostrcb_ctx *sctx;
2038 struct metablob *tpl_mb;
2045 tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL;
2046 dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0);
2048 if (ctx->flags & SB_IT_FIRST_CB) {
2049 sbuf_printf(buf, "struct statsblobv1@%p", sb);
2051 sbuf_printf(buf, ", abi=%hhu, endian=%hhu, maxsz=%hu, "
2052 "cursz=%hu, created=%jd, lastrst=%jd, flags=0x%04hx, "
2053 "stats_off=%hu, statsdata_off=%hu",
2054 sb->abi, sb->endian, sb->maxsz, sb->cursz,
2055 sb->created, sb->lastrst, sb->flags, sb->stats_off,
2058 sbuf_printf(buf, ", tplhash=%u", sb->tplhash);
2061 if (ctx->flags & SB_IT_FIRST_VOISTAT) {
2062 sbuf_printf(buf, "\n\tvois[%hd]: id=%hd", ctx->vslot, v->id);
2065 sbuf_printf(buf, ", name=\"%s\"", (tpl_mb == NULL) ? "" :
2066 tpl_mb->voi_meta[v->id].name);
2068 sbuf_printf(buf, ", flags=0x%04hx, dtype=%s, "
2069 "voistatmaxid=%hhd, stats_off=%hu", v->flags,
2070 vsd_dtype2name[v->dtype], v->voistatmaxid, v->stats_off);
2073 if (!dump && vs->stype <= 0)
2076 sbuf_printf(buf, "\n\t\tvois[%hd]stat[%hhd]: stype=", v->id, ctx->vsslot);
2077 if (vs->stype < 0) {
2078 sbuf_printf(buf, "%hhd", vs->stype);
2081 sbuf_printf(buf, "%s, errs=%hu", vs_stype2name[vs->stype],
2083 vsd = BLOB_OFFSET(sb, vs->data_off);
2085 sbuf_printf(buf, ", flags=0x%04x, dtype=%s, dsz=%hu, "
2086 "data_off=%hu", vs->flags, vsd_dtype2name[vs->dtype],
2087 vs->dsz, vs->data_off);
2089 sbuf_cat(buf, "\n\t\t\tvoistatdata: ");
2090 stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz,
2091 sctx->fmt, buf, dump);
2095 stats_v1_itercb_tostr_json(struct statsblobv1 *sb, struct voi *v, struct voistat *vs,
2096 struct sb_iter_ctx *ctx)
2098 struct sb_tostrcb_ctx *sctx;
2099 struct metablob *tpl_mb;
2107 tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL;
2108 dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0);
2110 if (ctx->flags & SB_IT_FIRST_CB) {
2111 sbuf_putc(buf, '{');
2113 sbuf_printf(buf, "\"abi\":%hhu,\"endian\":%hhu,"
2114 "\"maxsz\":%hu,\"cursz\":%hu,\"created\":%jd,"
2115 "\"lastrst\":%jd,\"flags\":%hu,\"stats_off\":%hu,"
2116 "\"statsdata_off\":%hu,", sb->abi,
2117 sb->endian, sb->maxsz, sb->cursz, sb->created,
2118 sb->lastrst, sb->flags, sb->stats_off,
2123 fmtstr = "\"tplname\":%s,\"tplhash\":%u,\"vois\":{";
2125 fmtstr = "\"tplname\":\"%s\",\"tplhash\":%u,\"vois\":{";
2127 sbuf_printf(buf, fmtstr, tpl_mb ? tpl_mb->tplname : "null",
2131 if (ctx->flags & SB_IT_FIRST_VOISTAT) {
2133 sbuf_printf(buf, "\"[%d]\":{\"id\":%d", ctx->vslot,
2136 sbuf_cat(buf, "},");
2141 fmtstr = ",\"name\":%s,\"flags\":%hu,"
2142 "\"dtype\":\"%s\",\"voistatmaxid\":%hhd,"
2143 "\"stats_off\":%hu,";
2145 fmtstr = ",\"name\":\"%s\",\"flags\":%hu,"
2146 "\"dtype\":\"%s\",\"voistatmaxid\":%hhd,"
2147 "\"stats_off\":%hu,";
2149 sbuf_printf(buf, fmtstr, tpl_mb ?
2150 tpl_mb->voi_meta[v->id].name : "null", v->flags,
2151 vsd_dtype2name[v->dtype], v->voistatmaxid,
2154 if (tpl_mb == NULL) {
2155 sbuf_printf(buf, "\"[%hd]\":{", v->id);
2157 sbuf_printf(buf, "\"%s\":{",
2158 tpl_mb->voi_meta[v->id].name);
2161 sbuf_cat(buf, "\"stats\":{");
2164 vsd = BLOB_OFFSET(sb, vs->data_off);
2166 sbuf_printf(buf, "\"[%hhd]\":", ctx->vsslot);
2167 if (vs->stype < 0) {
2168 sbuf_cat(buf, "{\"stype\":-1},");
2171 sbuf_printf(buf, "{\"stype\":\"%s\",\"errs\":%hu,\"flags\":%hu,"
2172 "\"dtype\":\"%s\",\"data_off\":%hu,\"voistatdata\":{",
2173 vs_stype2name[vs->stype], vs->errs, vs->flags,
2174 vsd_dtype2name[vs->dtype], vs->data_off);
2175 } else if (vs->stype > 0) {
2177 sbuf_printf(buf, "\"[%hhd]\":", vs->stype);
2179 sbuf_printf(buf, "\"%s\":", vs_stype2name[vs->stype]);
2183 if ((vs->flags & VS_VSDVALID) || dump) {
2185 sbuf_printf(buf, "{\"errs\":%hu,", vs->errs);
2186 /* Simple non-compound VSD types need a key. */
2187 if (!vsd_compoundtype[vs->dtype])
2188 sbuf_cat(buf, "\"val\":");
2189 stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz,
2190 sctx->fmt, buf, dump);
2191 sbuf_cat(buf, dump ? "}}" : "}");
2193 sbuf_cat(buf, dump ? "null}" : "null");
2195 if (ctx->flags & SB_IT_LAST_VOISTAT)
2196 sbuf_cat(buf, "}}");
2198 if (ctx->flags & SB_IT_LAST_CB)
2199 sbuf_cat(buf, "}}");
2201 sbuf_putc(buf, ',');
2205 stats_v1_itercb_tostr(struct statsblobv1 *sb, struct voi *v, struct voistat *vs,
2206 struct sb_iter_ctx *ctx)
2208 struct sb_tostrcb_ctx *sctx;
2212 switch (sctx->fmt) {
2213 case SB_STRFMT_FREEFORM:
2214 stats_v1_itercb_tostr_freeform(sb, v, vs, ctx);
2216 case SB_STRFMT_JSON:
2217 stats_v1_itercb_tostr_json(sb, v, vs, ctx);
2223 return (sbuf_error(sctx->buf));
2227 stats_v1_blob_tostr(struct statsblobv1 *sb, struct sbuf *buf,
2228 enum sb_str_fmt fmt, uint32_t flags)
2230 struct sb_tostrcb_ctx sctx;
2233 if (sb == NULL || sb->abi != STATS_ABI_V1 || buf == NULL ||
2234 fmt >= SB_STRFMT_NUM_FMTS)
2241 if (flags & SB_TOSTR_META) {
2242 if (stats_tpl_fetch(stats_tpl_fetch_allocid(NULL, sb->tplhash),
2249 if (flags & SB_TOSTR_OBJDUMP)
2250 iflags |= (SB_IT_NULLVOI | SB_IT_NULLVOISTAT);
2251 stats_v1_blob_iter(sb, stats_v1_itercb_tostr, &sctx, iflags);
2253 return (sbuf_error(buf));
2257 stats_v1_itercb_visit(struct statsblobv1 *sb, struct voi *v,
2258 struct voistat *vs, struct sb_iter_ctx *ctx)
2260 struct sb_visitcb_ctx *vctx;
2261 struct sb_visit sbv;
2265 sbv.tplhash = sb->tplhash;
2267 sbv.voi_dtype = v->dtype;
2268 sbv.vs_stype = vs->stype;
2269 sbv.vs_dtype = vs->dtype;
2270 sbv.vs_dsz = vs->dsz;
2271 sbv.vs_data = BLOB_OFFSET(sb, vs->data_off);
2272 sbv.vs_errs = vs->errs;
2273 sbv.flags = ctx->flags & (SB_IT_FIRST_CB | SB_IT_LAST_CB |
2274 SB_IT_FIRST_VOI | SB_IT_LAST_VOI | SB_IT_FIRST_VOISTAT |
2275 SB_IT_LAST_VOISTAT);
2277 return (vctx->cb(&sbv, vctx->usrctx));
2281 stats_v1_blob_visit(struct statsblobv1 *sb, stats_blob_visitcb_t func,
2284 struct sb_visitcb_ctx vctx;
2286 if (sb == NULL || sb->abi != STATS_ABI_V1 || func == NULL)
2290 vctx.usrctx = usrctx;
2292 stats_v1_blob_iter(sb, stats_v1_itercb_visit, &vctx, 0);
2298 stats_v1_icb_reset_voistat(struct statsblobv1 *sb, struct voi *v __unused,
2299 struct voistat *vs, struct sb_iter_ctx *ctx __unused)
2303 if (vs->stype == VS_STYPE_VOISTATE)
2306 vsd = BLOB_OFFSET(sb, vs->data_off);
2308 /* Perform the stat type's default reset action. */
2309 switch (vs->stype) {
2311 switch (vs->dtype) {
2312 case VSD_DTYPE_Q_S32:
2313 Q_SIFVAL(VSD(q32, vsd)->sq32, 0);
2315 case VSD_DTYPE_Q_U32:
2316 Q_SIFVAL(VSD(q32, vsd)->uq32, 0);
2318 case VSD_DTYPE_Q_S64:
2319 Q_SIFVAL(VSD(q64, vsd)->sq64, 0);
2321 case VSD_DTYPE_Q_U64:
2322 Q_SIFVAL(VSD(q64, vsd)->uq64, 0);
2325 bzero(vsd, vs->dsz);
2330 switch (vs->dtype) {
2331 case VSD_DTYPE_Q_S32:
2332 Q_SIFVAL(VSD(q32, vsd)->sq32,
2333 Q_IFMINVAL(VSD(q32, vsd)->sq32));
2335 case VSD_DTYPE_Q_U32:
2336 Q_SIFVAL(VSD(q32, vsd)->uq32,
2337 Q_IFMINVAL(VSD(q32, vsd)->uq32));
2339 case VSD_DTYPE_Q_S64:
2340 Q_SIFVAL(VSD(q64, vsd)->sq64,
2341 Q_IFMINVAL(VSD(q64, vsd)->sq64));
2343 case VSD_DTYPE_Q_U64:
2344 Q_SIFVAL(VSD(q64, vsd)->uq64,
2345 Q_IFMINVAL(VSD(q64, vsd)->uq64));
2348 memcpy(vsd, &numeric_limits[LIM_MIN][vs->dtype],
2354 switch (vs->dtype) {
2355 case VSD_DTYPE_Q_S32:
2356 Q_SIFVAL(VSD(q32, vsd)->sq32,
2357 Q_IFMAXVAL(VSD(q32, vsd)->sq32));
2359 case VSD_DTYPE_Q_U32:
2360 Q_SIFVAL(VSD(q32, vsd)->uq32,
2361 Q_IFMAXVAL(VSD(q32, vsd)->uq32));
2363 case VSD_DTYPE_Q_S64:
2364 Q_SIFVAL(VSD(q64, vsd)->sq64,
2365 Q_IFMAXVAL(VSD(q64, vsd)->sq64));
2367 case VSD_DTYPE_Q_U64:
2368 Q_SIFVAL(VSD(q64, vsd)->uq64,
2369 Q_IFMAXVAL(VSD(q64, vsd)->uq64));
2372 memcpy(vsd, &numeric_limits[LIM_MAX][vs->dtype],
2379 /* Reset bucket counts. */
2380 struct voistatdata_hist *hist;
2384 hist = VSD(hist, vsd);
2385 switch (vs->dtype) {
2386 case VSD_DTYPE_CRHIST32:
2387 nbkts = HIST_VSDSZ2NBKTS(crhist32, vs->dsz);
2390 case VSD_DTYPE_DRHIST32:
2391 nbkts = HIST_VSDSZ2NBKTS(drhist32, vs->dsz);
2394 case VSD_DTYPE_DVHIST32:
2395 nbkts = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz);
2398 case VSD_DTYPE_CRHIST64:
2399 nbkts = HIST_VSDSZ2NBKTS(crhist64, vs->dsz);
2402 case VSD_DTYPE_DRHIST64:
2403 nbkts = HIST_VSDSZ2NBKTS(drhist64, vs->dsz);
2406 case VSD_DTYPE_DVHIST64:
2407 nbkts = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz);
2414 bzero(VSD_HIST_FIELDPTR(hist, vs->dtype, oob),
2415 is32bit ? sizeof(uint32_t) : sizeof(uint64_t));
2416 for (i = nbkts - 1; i >= 0; i--) {
2417 bzero(VSD_HIST_FIELDPTR(hist, vs->dtype,
2418 bkts[i].cnt), is32bit ? sizeof(uint32_t) :
2423 case VS_STYPE_TDGST:
2425 /* Reset sample count centroids array/tree. */
2426 struct voistatdata_tdgst *tdgst;
2427 struct ctdth32 *ctd32tree;
2428 struct ctdth64 *ctd64tree;
2429 struct voistatdata_tdgstctd32 *ctd32;
2430 struct voistatdata_tdgstctd64 *ctd64;
2432 tdgst = VSD(tdgst, vsd);
2433 switch (vs->dtype) {
2434 case VSD_DTYPE_TDGSTCLUST32:
2435 VSD(tdgstclust32, tdgst)->smplcnt = 0;
2436 VSD(tdgstclust32, tdgst)->compcnt = 0;
2437 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
2438 ARB_INIT(ctd32, ctdlnk, ctd32tree,
2439 ARB_MAXNODES(ctd32tree)) {
2441 Q_SIFVAL(ctd32->mu, 0);
2444 RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree);
2447 case VSD_DTYPE_TDGSTCLUST64:
2448 VSD(tdgstclust64, tdgst)->smplcnt = 0;
2449 VSD(tdgstclust64, tdgst)->compcnt = 0;
2450 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
2451 ARB_INIT(ctd64, ctdlnk, ctd64tree,
2452 ARB_MAXNODES(ctd64tree)) {
2454 Q_SIFVAL(ctd64->mu, 0);
2457 RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree);
2466 KASSERT(0, ("Unknown VOI stat type %d", vs->stype));
2471 vs->flags &= ~VS_VSDVALID;
2477 stats_v1_blob_snapshot(struct statsblobv1 **dst, size_t dstmaxsz,
2478 struct statsblobv1 *src, uint32_t flags)
2482 if (src != NULL && src->abi == STATS_ABI_V1) {
2483 error = stats_v1_blob_clone(dst, dstmaxsz, src, flags);
2485 if (flags & SB_CLONE_RSTSRC) {
2486 stats_v1_blob_iter(src,
2487 stats_v1_icb_reset_voistat, NULL, 0);
2488 src->lastrst = stats_sbinuptime();
2490 stats_v1_blob_finalise(*dst);
2499 stats_v1_voi_update_max(enum vsd_dtype voi_dtype __unused,
2500 struct voistatdata *voival, struct voistat *vs, void *vsd)
2504 KASSERT(vs->dtype < VSD_NUM_DTYPES,
2505 ("Unknown VSD dtype %d", vs->dtype));
2509 switch (vs->dtype) {
2510 case VSD_DTYPE_INT_S32:
2511 if (VSD(int32, vsd)->s32 < voival->int32.s32) {
2512 VSD(int32, vsd)->s32 = voival->int32.s32;
2513 vs->flags |= VS_VSDVALID;
2516 case VSD_DTYPE_INT_U32:
2517 if (VSD(int32, vsd)->u32 < voival->int32.u32) {
2518 VSD(int32, vsd)->u32 = voival->int32.u32;
2519 vs->flags |= VS_VSDVALID;
2522 case VSD_DTYPE_INT_S64:
2523 if (VSD(int64, vsd)->s64 < voival->int64.s64) {
2524 VSD(int64, vsd)->s64 = voival->int64.s64;
2525 vs->flags |= VS_VSDVALID;
2528 case VSD_DTYPE_INT_U64:
2529 if (VSD(int64, vsd)->u64 < voival->int64.u64) {
2530 VSD(int64, vsd)->u64 = voival->int64.u64;
2531 vs->flags |= VS_VSDVALID;
2534 case VSD_DTYPE_INT_SLONG:
2535 if (VSD(intlong, vsd)->slong < voival->intlong.slong) {
2536 VSD(intlong, vsd)->slong = voival->intlong.slong;
2537 vs->flags |= VS_VSDVALID;
2540 case VSD_DTYPE_INT_ULONG:
2541 if (VSD(intlong, vsd)->ulong < voival->intlong.ulong) {
2542 VSD(intlong, vsd)->ulong = voival->intlong.ulong;
2543 vs->flags |= VS_VSDVALID;
2546 case VSD_DTYPE_Q_S32:
2547 if (Q_QLTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) &&
2548 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32,
2549 voival->q32.sq32)))) {
2550 vs->flags |= VS_VSDVALID;
2553 case VSD_DTYPE_Q_U32:
2554 if (Q_QLTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) &&
2555 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32,
2556 voival->q32.uq32)))) {
2557 vs->flags |= VS_VSDVALID;
2560 case VSD_DTYPE_Q_S64:
2561 if (Q_QLTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) &&
2562 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64,
2563 voival->q64.sq64)))) {
2564 vs->flags |= VS_VSDVALID;
2567 case VSD_DTYPE_Q_U64:
2568 if (Q_QLTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) &&
2569 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64,
2570 voival->q64.uq64)))) {
2571 vs->flags |= VS_VSDVALID;
2583 stats_v1_voi_update_min(enum vsd_dtype voi_dtype __unused,
2584 struct voistatdata *voival, struct voistat *vs, void *vsd)
2588 KASSERT(vs->dtype < VSD_NUM_DTYPES,
2589 ("Unknown VSD dtype %d", vs->dtype));
2593 switch (vs->dtype) {
2594 case VSD_DTYPE_INT_S32:
2595 if (VSD(int32, vsd)->s32 > voival->int32.s32) {
2596 VSD(int32, vsd)->s32 = voival->int32.s32;
2597 vs->flags |= VS_VSDVALID;
2600 case VSD_DTYPE_INT_U32:
2601 if (VSD(int32, vsd)->u32 > voival->int32.u32) {
2602 VSD(int32, vsd)->u32 = voival->int32.u32;
2603 vs->flags |= VS_VSDVALID;
2606 case VSD_DTYPE_INT_S64:
2607 if (VSD(int64, vsd)->s64 > voival->int64.s64) {
2608 VSD(int64, vsd)->s64 = voival->int64.s64;
2609 vs->flags |= VS_VSDVALID;
2612 case VSD_DTYPE_INT_U64:
2613 if (VSD(int64, vsd)->u64 > voival->int64.u64) {
2614 VSD(int64, vsd)->u64 = voival->int64.u64;
2615 vs->flags |= VS_VSDVALID;
2618 case VSD_DTYPE_INT_SLONG:
2619 if (VSD(intlong, vsd)->slong > voival->intlong.slong) {
2620 VSD(intlong, vsd)->slong = voival->intlong.slong;
2621 vs->flags |= VS_VSDVALID;
2624 case VSD_DTYPE_INT_ULONG:
2625 if (VSD(intlong, vsd)->ulong > voival->intlong.ulong) {
2626 VSD(intlong, vsd)->ulong = voival->intlong.ulong;
2627 vs->flags |= VS_VSDVALID;
2630 case VSD_DTYPE_Q_S32:
2631 if (Q_QGTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) &&
2632 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32,
2633 voival->q32.sq32)))) {
2634 vs->flags |= VS_VSDVALID;
2637 case VSD_DTYPE_Q_U32:
2638 if (Q_QGTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) &&
2639 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32,
2640 voival->q32.uq32)))) {
2641 vs->flags |= VS_VSDVALID;
2644 case VSD_DTYPE_Q_S64:
2645 if (Q_QGTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) &&
2646 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64,
2647 voival->q64.sq64)))) {
2648 vs->flags |= VS_VSDVALID;
2651 case VSD_DTYPE_Q_U64:
2652 if (Q_QGTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) &&
2653 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64,
2654 voival->q64.uq64)))) {
2655 vs->flags |= VS_VSDVALID;
2667 stats_v1_voi_update_sum(enum vsd_dtype voi_dtype __unused,
2668 struct voistatdata *voival, struct voistat *vs, void *vsd)
2672 KASSERT(vs->dtype < VSD_NUM_DTYPES,
2673 ("Unknown VSD dtype %d", vs->dtype));
2677 switch (vs->dtype) {
2678 case VSD_DTYPE_INT_S32:
2679 VSD(int32, vsd)->s32 += voival->int32.s32;
2681 case VSD_DTYPE_INT_U32:
2682 VSD(int32, vsd)->u32 += voival->int32.u32;
2684 case VSD_DTYPE_INT_S64:
2685 VSD(int64, vsd)->s64 += voival->int64.s64;
2687 case VSD_DTYPE_INT_U64:
2688 VSD(int64, vsd)->u64 += voival->int64.u64;
2690 case VSD_DTYPE_INT_SLONG:
2691 VSD(intlong, vsd)->slong += voival->intlong.slong;
2693 case VSD_DTYPE_INT_ULONG:
2694 VSD(intlong, vsd)->ulong += voival->intlong.ulong;
2696 case VSD_DTYPE_Q_S32:
2697 error = Q_QADDQ(&VSD(q32, vsd)->sq32, voival->q32.sq32);
2699 case VSD_DTYPE_Q_U32:
2700 error = Q_QADDQ(&VSD(q32, vsd)->uq32, voival->q32.uq32);
2702 case VSD_DTYPE_Q_S64:
2703 error = Q_QADDQ(&VSD(q64, vsd)->sq64, voival->q64.sq64);
2705 case VSD_DTYPE_Q_U64:
2706 error = Q_QADDQ(&VSD(q64, vsd)->uq64, voival->q64.uq64);
2714 vs->flags |= VS_VSDVALID;
2720 stats_v1_voi_update_hist(enum vsd_dtype voi_dtype, struct voistatdata *voival,
2721 struct voistat *vs, struct voistatdata_hist *hist)
2723 struct voistatdata_numeric *bkt_lb, *bkt_ub;
2724 uint64_t *oob64, *cnt64;
2725 uint32_t *oob32, *cnt32;
2726 int error, i, found, is32bit, has_ub, eq_only;
2730 switch (vs->dtype) {
2731 case VSD_DTYPE_CRHIST32:
2732 i = HIST_VSDSZ2NBKTS(crhist32, vs->dsz);
2734 has_ub = eq_only = 0;
2735 oob32 = &VSD(crhist32, hist)->oob;
2737 case VSD_DTYPE_DRHIST32:
2738 i = HIST_VSDSZ2NBKTS(drhist32, vs->dsz);
2739 is32bit = has_ub = 1;
2741 oob32 = &VSD(drhist32, hist)->oob;
2743 case VSD_DTYPE_DVHIST32:
2744 i = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz);
2745 is32bit = eq_only = 1;
2747 oob32 = &VSD(dvhist32, hist)->oob;
2749 case VSD_DTYPE_CRHIST64:
2750 i = HIST_VSDSZ2NBKTS(crhist64, vs->dsz);
2751 is32bit = has_ub = eq_only = 0;
2752 oob64 = &VSD(crhist64, hist)->oob;
2754 case VSD_DTYPE_DRHIST64:
2755 i = HIST_VSDSZ2NBKTS(drhist64, vs->dsz);
2756 is32bit = eq_only = 0;
2758 oob64 = &VSD(drhist64, hist)->oob;
2760 case VSD_DTYPE_DVHIST64:
2761 i = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz);
2762 is32bit = has_ub = 0;
2764 oob64 = &VSD(dvhist64, hist)->oob;
2769 i--; /* Adjust for 0-based array index. */
2771 /* XXXLAS: Should probably use a better bucket search algorithm. ARB? */
2772 for (found = 0; i >= 0 && !found; i--) {
2773 switch (vs->dtype) {
2774 case VSD_DTYPE_CRHIST32:
2775 bkt_lb = &VSD(crhist32, hist)->bkts[i].lb;
2776 cnt32 = &VSD(crhist32, hist)->bkts[i].cnt;
2778 case VSD_DTYPE_DRHIST32:
2779 bkt_lb = &VSD(drhist32, hist)->bkts[i].lb;
2780 bkt_ub = &VSD(drhist32, hist)->bkts[i].ub;
2781 cnt32 = &VSD(drhist32, hist)->bkts[i].cnt;
2783 case VSD_DTYPE_DVHIST32:
2784 bkt_lb = &VSD(dvhist32, hist)->bkts[i].val;
2785 cnt32 = &VSD(dvhist32, hist)->bkts[i].cnt;
2787 case VSD_DTYPE_CRHIST64:
2788 bkt_lb = &VSD(crhist64, hist)->bkts[i].lb;
2789 cnt64 = &VSD(crhist64, hist)->bkts[i].cnt;
2791 case VSD_DTYPE_DRHIST64:
2792 bkt_lb = &VSD(drhist64, hist)->bkts[i].lb;
2793 bkt_ub = &VSD(drhist64, hist)->bkts[i].ub;
2794 cnt64 = &VSD(drhist64, hist)->bkts[i].cnt;
2796 case VSD_DTYPE_DVHIST64:
2797 bkt_lb = &VSD(dvhist64, hist)->bkts[i].val;
2798 cnt64 = &VSD(dvhist64, hist)->bkts[i].cnt;
2804 switch (voi_dtype) {
2805 case VSD_DTYPE_INT_S32:
2806 if (voival->int32.s32 >= bkt_lb->int32.s32) {
2807 if ((eq_only && voival->int32.s32 ==
2808 bkt_lb->int32.s32) ||
2809 (!eq_only && (!has_ub ||
2810 voival->int32.s32 < bkt_ub->int32.s32)))
2814 case VSD_DTYPE_INT_U32:
2815 if (voival->int32.u32 >= bkt_lb->int32.u32) {
2816 if ((eq_only && voival->int32.u32 ==
2817 bkt_lb->int32.u32) ||
2818 (!eq_only && (!has_ub ||
2819 voival->int32.u32 < bkt_ub->int32.u32)))
2823 case VSD_DTYPE_INT_S64:
2824 if (voival->int64.s64 >= bkt_lb->int64.s64)
2825 if ((eq_only && voival->int64.s64 ==
2826 bkt_lb->int64.s64) ||
2827 (!eq_only && (!has_ub ||
2828 voival->int64.s64 < bkt_ub->int64.s64)))
2831 case VSD_DTYPE_INT_U64:
2832 if (voival->int64.u64 >= bkt_lb->int64.u64)
2833 if ((eq_only && voival->int64.u64 ==
2834 bkt_lb->int64.u64) ||
2835 (!eq_only && (!has_ub ||
2836 voival->int64.u64 < bkt_ub->int64.u64)))
2839 case VSD_DTYPE_INT_SLONG:
2840 if (voival->intlong.slong >= bkt_lb->intlong.slong)
2841 if ((eq_only && voival->intlong.slong ==
2842 bkt_lb->intlong.slong) ||
2843 (!eq_only && (!has_ub ||
2844 voival->intlong.slong <
2845 bkt_ub->intlong.slong)))
2848 case VSD_DTYPE_INT_ULONG:
2849 if (voival->intlong.ulong >= bkt_lb->intlong.ulong)
2850 if ((eq_only && voival->intlong.ulong ==
2851 bkt_lb->intlong.ulong) ||
2852 (!eq_only && (!has_ub ||
2853 voival->intlong.ulong <
2854 bkt_ub->intlong.ulong)))
2857 case VSD_DTYPE_Q_S32:
2858 if (Q_QGEQ(voival->q32.sq32, bkt_lb->q32.sq32))
2859 if ((eq_only && Q_QEQ(voival->q32.sq32,
2860 bkt_lb->q32.sq32)) ||
2861 (!eq_only && (!has_ub ||
2862 Q_QLTQ(voival->q32.sq32,
2863 bkt_ub->q32.sq32))))
2866 case VSD_DTYPE_Q_U32:
2867 if (Q_QGEQ(voival->q32.uq32, bkt_lb->q32.uq32))
2868 if ((eq_only && Q_QEQ(voival->q32.uq32,
2869 bkt_lb->q32.uq32)) ||
2870 (!eq_only && (!has_ub ||
2871 Q_QLTQ(voival->q32.uq32,
2872 bkt_ub->q32.uq32))))
2875 case VSD_DTYPE_Q_S64:
2876 if (Q_QGEQ(voival->q64.sq64, bkt_lb->q64.sq64))
2877 if ((eq_only && Q_QEQ(voival->q64.sq64,
2878 bkt_lb->q64.sq64)) ||
2879 (!eq_only && (!has_ub ||
2880 Q_QLTQ(voival->q64.sq64,
2881 bkt_ub->q64.sq64))))
2884 case VSD_DTYPE_Q_U64:
2885 if (Q_QGEQ(voival->q64.uq64, bkt_lb->q64.uq64))
2886 if ((eq_only && Q_QEQ(voival->q64.uq64,
2887 bkt_lb->q64.uq64)) ||
2888 (!eq_only && (!has_ub ||
2889 Q_QLTQ(voival->q64.uq64,
2890 bkt_ub->q64.uq64))))
2910 vs->flags |= VS_VSDVALID;
2915 stats_v1_vsd_tdgst_compress(enum vsd_dtype vs_dtype,
2916 struct voistatdata_tdgst *tdgst, int attempt)
2918 struct ctdth32 *ctd32tree;
2919 struct ctdth64 *ctd64tree;
2920 struct voistatdata_tdgstctd32 *ctd32;
2921 struct voistatdata_tdgstctd64 *ctd64;
2922 uint64_t ebits, idxmask;
2923 uint32_t bitsperidx, nebits;
2924 int error, idx, is32bit, maxctds, remctds, tmperr;
2929 case VSD_DTYPE_TDGSTCLUST32:
2930 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
2931 if (!ARB_FULL(ctd32tree))
2933 VSD(tdgstclust32, tdgst)->compcnt++;
2934 maxctds = remctds = ARB_MAXNODES(ctd32tree);
2935 ARB_RESET_TREE(ctd32tree, ctdth32, maxctds);
2936 VSD(tdgstclust32, tdgst)->smplcnt = 0;
2941 RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree);
2944 case VSD_DTYPE_TDGSTCLUST64:
2945 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
2946 if (!ARB_FULL(ctd64tree))
2948 VSD(tdgstclust64, tdgst)->compcnt++;
2949 maxctds = remctds = ARB_MAXNODES(ctd64tree);
2950 ARB_RESET_TREE(ctd64tree, ctdth64, maxctds);
2951 VSD(tdgstclust64, tdgst)->smplcnt = 0;
2956 RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree);
2964 * Rebuild the t-digest ARB by pseudorandomly selecting centroids and
2965 * re-inserting the mu/cnt of each as a value and corresponding weight.
2969 * XXXCEM: random(9) is currently rand(3), not random(3). rand(3)
2970 * RAND_MAX happens to be approximately 31 bits (range [0,
2971 * 0x7ffffffd]), so the math kinda works out. When/if this portion of
2972 * the code is compiled in userspace, it gets the random(3) behavior,
2973 * which has expected range [0, 0x7fffffff].
2975 #define bitsperrand 31
2978 bitsperidx = fls(maxctds);
2979 KASSERT(bitsperidx <= sizeof(ebits) << 3,
2980 ("%s: bitsperidx=%d, ebits=%d",
2981 __func__, bitsperidx, (int)(sizeof(ebits) << 3)));
2982 idxmask = (UINT64_C(1) << bitsperidx) - 1;
2984 /* Initialise the free list with randomised centroid indices. */
2985 for (; remctds > 0; remctds--) {
2986 while (nebits < bitsperidx) {
2987 ebits |= ((uint64_t)random()) << nebits;
2988 nebits += bitsperrand;
2989 if (nebits > (sizeof(ebits) << 3))
2990 nebits = sizeof(ebits) << 3;
2992 idx = ebits & idxmask;
2993 nebits -= bitsperidx;
2994 ebits >>= bitsperidx;
2997 * Select the next centroid to put on the ARB free list. We
2998 * start with the centroid at our randomly selected array index,
2999 * and work our way forwards until finding one (the latter
3000 * aspect reduces re-insertion randomness, but is good enough).
3007 ctd32 = ARB_NODE(ctd32tree, idx);
3009 ctd64 = ARB_NODE(ctd64tree, idx);
3010 } while ((is32bit ? ARB_ISFREE(ctd32, ctdlnk) :
3011 ARB_ISFREE(ctd64, ctdlnk)) && ++idx);
3013 /* Put the centroid on the ARB free list. */
3015 ARB_RETURNFREE(ctd32tree, ctd32, ctdlnk);
3017 ARB_RETURNFREE(ctd64tree, ctd64, ctdlnk);
3021 * The free list now contains the randomised indices of every centroid.
3022 * Walk the free list from start to end, re-inserting each centroid's
3023 * mu/cnt. The tdgst_add() call may or may not consume the free centroid
3024 * we re-insert values from during each loop iteration, so we must latch
3025 * the index of the next free list centroid before the re-insertion
3026 * call. The previous loop above should have left the centroid pointer
3027 * pointing to the element at the head of the free list.
3030 ARB_FREEIDX(ctd32tree) == ARB_SELFIDX(ctd32tree, ctd32) :
3031 ARB_FREEIDX(ctd64tree) == ARB_SELFIDX(ctd64tree, ctd64)),
3032 ("%s: t-digest ARB@%p free list bug", __func__,
3033 (is32bit ? (void *)ctd32tree : (void *)ctd64tree)));
3035 while ((is32bit ? ctd32 != NULL : ctd64 != NULL)) {
3040 idx = ARB_NEXTFREEIDX(ctd32, ctdlnk);
3041 /* Cloning a s32q_t into a s64q_t should never fail. */
3042 tmperr = Q_QCLONEQ(&x, ctd32->mu);
3043 tmperr = tmperr ? tmperr : stats_v1_vsd_tdgst_add(
3044 vs_dtype, tdgst, x, ctd32->cnt, attempt);
3045 ctd32 = ARB_NODE(ctd32tree, idx);
3046 KASSERT(ctd32 == NULL || ARB_ISFREE(ctd32, ctdlnk),
3047 ("%s: t-digest ARB@%p free list bug", __func__,
3050 idx = ARB_NEXTFREEIDX(ctd64, ctdlnk);
3051 tmperr = stats_v1_vsd_tdgst_add(vs_dtype, tdgst,
3052 ctd64->mu, ctd64->cnt, attempt);
3053 ctd64 = ARB_NODE(ctd64tree, idx);
3054 KASSERT(ctd64 == NULL || ARB_ISFREE(ctd64, ctdlnk),
3055 ("%s: t-digest ARB@%p free list bug", __func__,
3059 * This process should not produce errors, bugs notwithstanding.
3060 * Just in case, latch any errors and attempt all re-insertions.
3062 error = tmperr ? tmperr : error;
3066 KASSERT(remctds == 0, ("%s: t-digest ARB@%p free list bug", __func__,
3067 (is32bit ? (void *)ctd32tree : (void *)ctd64tree)));
3073 stats_v1_vsd_tdgst_add(enum vsd_dtype vs_dtype, struct voistatdata_tdgst *tdgst,
3074 s64q_t x, uint64_t weight, int attempt)
3077 char qstr[Q_MAXSTRLEN(x, 10)];
3079 struct ctdth32 *ctd32tree;
3080 struct ctdth64 *ctd64tree;
3081 void *closest, *cur, *lb, *ub;
3082 struct voistatdata_tdgstctd32 *ctd32;
3083 struct voistatdata_tdgstctd64 *ctd64;
3084 uint64_t cnt, smplcnt, sum, tmpsum;
3085 s64q_t k, minz, q, z;
3086 int error, is32bit, n;
3089 minz = Q_INI(&z, 0, 0, Q_NFBITS(x));
3092 case VSD_DTYPE_TDGSTCLUST32:
3093 if ((UINT32_MAX - weight) < VSD(tdgstclust32, tdgst)->smplcnt)
3095 smplcnt = VSD(tdgstclust32, tdgst)->smplcnt;
3096 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
3101 case VSD_DTYPE_TDGSTCLUST64:
3102 if ((UINT64_MAX - weight) < VSD(tdgstclust64, tdgst)->smplcnt)
3104 smplcnt = VSD(tdgstclust64, tdgst)->smplcnt;
3105 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
3119 * Inspired by Ted Dunning's AVLTreeDigest.java
3122 #if defined(DIAGNOSTIC)
3123 KASSERT(attempt < 5,
3124 ("%s: Too many attempts", __func__));
3129 Q_SIFVAL(minz, Q_IFMAXVAL(minz));
3130 closest = ub = NULL;
3134 lb = cur = (void *)(ctd32 = ARB_MIN(ctdth32, ctd32tree));
3136 lb = cur = (void *)(ctd64 = ARB_MIN(ctdth64, ctd64tree));
3138 if (lb == NULL) /* Empty tree. */
3139 lb = (is32bit ? (void *)ARB_ROOT(ctd32tree) :
3140 (void *)ARB_ROOT(ctd64tree));
3143 * Find the set of centroids with minimum distance to x and
3144 * compute the sum of counts for all centroids with mean less
3145 * than the first centroid in the set.
3149 (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) :
3150 (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) {
3153 KASSERT(Q_PRECEQ(ctd32->mu, x),
3154 ("%s: Q_RELPREC(mu,x)=%d", __func__,
3155 Q_RELPREC(ctd32->mu, x)));
3156 /* Ok to assign as both have same precision. */
3160 KASSERT(Q_PRECEQ(ctd64->mu, x),
3161 ("%s: Q_RELPREC(mu,x)=%d", __func__,
3162 Q_RELPREC(ctd64->mu, x)));
3163 /* Ok to assign as both have same precision. */
3167 error = Q_QSUBQ(&z, x);
3168 #if defined(DIAGNOSTIC)
3169 KASSERT(!error, ("%s: unexpected error %d", __func__,
3176 if (Q_QLTQ(z, minz)) {
3181 } else if (Q_QGTQ(z, minz)) {
3188 (void *)(ctd32 = (struct voistatdata_tdgstctd32 *)lb) :
3189 (void *)(ctd64 = (struct voistatdata_tdgstctd64 *)lb));
3191 for (n = 0; cur != ub; cur = (is32bit ?
3192 (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) :
3193 (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) {
3201 error = Q_QFRACI(&q, 1, 2);
3203 /* [ sum + ((cnt - 1) / 2) ] / (smplcnt - 1) */
3204 error = Q_QFRACI(&q, (sum << 1) + cnt - 1,
3205 (smplcnt - 1) << 1);
3207 /* k = q x 4 x samplcnt x attempt */
3208 error |= Q_QMULI(&k, 4 * smplcnt * attempt);
3209 /* k = k x (1 - q) */
3210 error |= Q_QSUBI(&q, 1);
3212 error |= Q_QMULQ(&k, q);
3213 #if defined(DIAGNOSTIC)
3214 #if !defined(_KERNEL)
3215 double q_dbl, k_dbl, q2d, k2d;
3218 q_dbl = smplcnt == 1 ? 0.5 :
3219 (sum + ((cnt - 1) / 2.0)) / (double)(smplcnt - 1);
3220 k_dbl = 4 * smplcnt * q_dbl * (1.0 - q_dbl) * attempt;
3222 * If the difference between q and q_dbl is greater than
3223 * the fractional precision of q, something is off.
3224 * NB: q is holding the value of 1 - q
3226 q_dbl = 1.0 - q_dbl;
3227 KASSERT((q_dbl > q2d ? q_dbl - q2d : q2d - q_dbl) <
3228 (1.05 * ((double)1 / (double)(1ULL << Q_NFBITS(q)))),
3229 ("Q-type q bad precision"));
3230 KASSERT((k_dbl > k2d ? k_dbl - k2d : k2d - k_dbl) <
3231 1.0 + (0.01 * smplcnt),
3232 ("Q-type k bad precision"));
3233 #endif /* !_KERNEL */
3234 KASSERT(!error, ("%s: unexpected error %d", __func__,
3236 #endif /* DIAGNOSTIC */
3239 if ((is32bit && ((ctd32->cnt + weight) <=
3240 (uint64_t)Q_GIVAL(k))) ||
3241 (!is32bit && ((ctd64->cnt + weight) <=
3242 (uint64_t)Q_GIVAL(k)))) {
3244 /* random() produces 31 bits. */
3245 if (random() < (INT32_MAX / n))
3250 } while (closest == NULL &&
3251 (is32bit ? ARB_FULL(ctd32tree) : ARB_FULL(ctd64tree)) &&
3252 (error = stats_v1_vsd_tdgst_compress(vs_dtype, tdgst,
3258 if (closest != NULL) {
3259 /* Merge with an existing centroid. */
3261 ctd32 = (struct voistatdata_tdgstctd32 *)closest;
3262 error = Q_QSUBQ(&x, ctd32->mu);
3264 * The following calculation "x / (cnt + weight)"
3265 * computes the amount by which to adjust the centroid's
3266 * mu value in order to merge in the VOI sample.
3268 * It can underflow (Q_QDIVI() returns ERANGE) when the
3269 * user centroids' fractional precision (which is
3270 * inherited by 'x') is too low to represent the result.
3272 * A sophisticated approach to dealing with this issue
3273 * would minimise accumulation of error by tracking
3274 * underflow per centroid and making an adjustment when
3275 * a LSB's worth of underflow has accumulated.
3277 * A simpler approach is to let the result underflow
3278 * i.e. merge the VOI sample into the centroid without
3279 * adjusting the centroid's mu, and rely on the user to
3280 * specify their t-digest with sufficient centroid
3281 * fractional precision such that the accumulation of
3282 * error from multiple underflows is of no material
3283 * consequence to the centroid's final value of mu.
3285 * For the moment, the latter approach is employed by
3286 * simply ignoring ERANGE here.
3288 * XXXLAS: Per-centroid underflow tracking is likely too
3289 * onerous, but it probably makes sense to accumulate a
3290 * single underflow error variable across all centroids
3291 * and report it as part of the digest to provide
3292 * additional visibility into the digest's fidelity.
3294 error = error ? error :
3295 Q_QDIVI(&x, ctd32->cnt + weight);
3296 if ((error && error != ERANGE)
3297 || (error = Q_QADDQ(&ctd32->mu, x))) {
3299 KASSERT(!error, ("%s: unexpected error %d",
3304 ctd32->cnt += weight;
3305 error = ARB_REINSERT(ctdth32, ctd32tree, ctd32) ==
3306 NULL ? 0 : EALREADY;
3308 RB_REINSERT(rbctdth32,
3309 &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32);
3312 ctd64 = (struct voistatdata_tdgstctd64 *)closest;
3313 error = Q_QSUBQ(&x, ctd64->mu);
3314 error = error ? error :
3315 Q_QDIVI(&x, ctd64->cnt + weight);
3316 /* Refer to is32bit ERANGE discussion above. */
3317 if ((error && error != ERANGE)
3318 || (error = Q_QADDQ(&ctd64->mu, x))) {
3319 KASSERT(!error, ("%s: unexpected error %d",
3323 ctd64->cnt += weight;
3324 error = ARB_REINSERT(ctdth64, ctd64tree, ctd64) ==
3325 NULL ? 0 : EALREADY;
3327 RB_REINSERT(rbctdth64,
3328 &VSD(tdgstclust64, tdgst)->rbctdtree, ctd64);
3333 * Add a new centroid. If digest compression is working
3334 * correctly, there should always be at least one free.
3337 ctd32 = ARB_GETFREE(ctd32tree, ctdlnk);
3339 KASSERT(ctd32 != NULL,
3340 ("%s: t-digest@%p has no free centroids",
3345 if ((error = Q_QCPYVALQ(&ctd32->mu, x)))
3347 ctd32->cnt = weight;
3348 error = ARB_INSERT(ctdth32, ctd32tree, ctd32) == NULL ?
3351 RB_INSERT(rbctdth32,
3352 &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32);
3355 ctd64 = ARB_GETFREE(ctd64tree, ctdlnk);
3357 KASSERT(ctd64 != NULL,
3358 ("%s: t-digest@%p has no free centroids",
3361 if (ctd64 == NULL) /* Should not happen. */
3363 /* Direct assignment ok as both have same type/prec. */
3365 ctd64->cnt = weight;
3366 error = ARB_INSERT(ctdth64, ctd64tree, ctd64) == NULL ?
3369 RB_INSERT(rbctdth64, &VSD(tdgstclust64,
3370 tdgst)->rbctdtree, ctd64);
3376 VSD(tdgstclust32, tdgst)->smplcnt += weight;
3378 VSD(tdgstclust64, tdgst)->smplcnt += weight;
3381 struct rbctdth64 *rbctdtree =
3382 &VSD(tdgstclust64, tdgst)->rbctdtree;
3383 struct voistatdata_tdgstctd64 *rbctd64;
3385 ARB_FOREACH(ctd64, ctdth64, ctd64tree) {
3386 rbctd64 = (i == 0 ? RB_MIN(rbctdth64, rbctdtree) :
3387 RB_NEXT(rbctdth64, rbctdtree, rbctd64));
3389 if (i >= ARB_CURNODES(ctd64tree)
3391 || ARB_MIN(ctdth64, ctd64tree) !=
3392 RB_MIN(rbctdth64, rbctdtree)
3393 || ARB_MAX(ctdth64, ctd64tree) !=
3394 RB_MAX(rbctdth64, rbctdtree)
3395 || ARB_LEFTIDX(ctd64, ctdlnk) !=
3396 ARB_SELFIDX(ctd64tree, RB_LEFT(rbctd64, rblnk))
3397 || ARB_RIGHTIDX(ctd64, ctdlnk) !=
3398 ARB_SELFIDX(ctd64tree, RB_RIGHT(rbctd64, rblnk))
3399 || ARB_PARENTIDX(ctd64, ctdlnk) !=
3400 ARB_SELFIDX(ctd64tree,
3401 RB_PARENT(rbctd64, rblnk))) {
3402 Q_TOSTR(ctd64->mu, -1, 10, qstr, sizeof(qstr));
3403 printf("ARB ctd=%3d p=%3d l=%3d r=%3d c=%2d "
3405 (int)ARB_SELFIDX(ctd64tree, ctd64),
3406 ARB_PARENTIDX(ctd64, ctdlnk),
3407 ARB_LEFTIDX(ctd64, ctdlnk),
3408 ARB_RIGHTIDX(ctd64, ctdlnk),
3409 ARB_COLOR(ctd64, ctdlnk),
3412 Q_TOSTR(rbctd64->mu, -1, 10, qstr,
3414 struct voistatdata_tdgstctd64 *parent;
3415 parent = RB_PARENT(rbctd64, rblnk);
3417 parent == NULL ? 0 :
3418 RB_LEFT(parent, rblnk) == rbctd64 ?
3419 (_RB_BITSUP(parent, rblnk) & _RB_L) != 0 :
3420 (_RB_BITSUP(parent, rblnk) & _RB_R) != 0;
3421 printf(" RB ctd=%3d p=%3d l=%3d r=%3d c=%2d "
3423 (int)ARB_SELFIDX(ctd64tree, rbctd64),
3424 (int)ARB_SELFIDX(ctd64tree,
3425 RB_PARENT(rbctd64, rblnk)),
3426 (int)ARB_SELFIDX(ctd64tree,
3427 RB_LEFT(rbctd64, rblnk)),
3428 (int)ARB_SELFIDX(ctd64tree,
3429 RB_RIGHT(rbctd64, rblnk)),
3433 panic("RB@%p and ARB@%p trees differ\n",
3434 rbctdtree, ctd64tree);
3438 #endif /* DIAGNOSTIC */
3445 stats_v1_voi_update_tdgst(enum vsd_dtype voi_dtype, struct voistatdata *voival,
3446 struct voistat *vs, struct voistatdata_tdgst *tdgst)
3453 switch (vs->dtype) {
3454 case VSD_DTYPE_TDGSTCLUST32:
3455 /* Use same precision as the user's centroids. */
3456 Q_INI(&x, 0, 0, Q_NFBITS(
3457 ARB_CNODE(&VSD(tdgstclust32, tdgst)->ctdtree, 0)->mu));
3459 case VSD_DTYPE_TDGSTCLUST64:
3460 /* Use same precision as the user's centroids. */
3461 Q_INI(&x, 0, 0, Q_NFBITS(
3462 ARB_CNODE(&VSD(tdgstclust64, tdgst)->ctdtree, 0)->mu));
3465 KASSERT(vs->dtype == VSD_DTYPE_TDGSTCLUST32 ||
3466 vs->dtype == VSD_DTYPE_TDGSTCLUST64,
3467 ("%s: vs->dtype(%d) != VSD_DTYPE_TDGSTCLUST<32|64>",
3468 __func__, vs->dtype));
3473 * XXXLAS: Should have both a signed and unsigned 'x' variable to avoid
3474 * returning EOVERFLOW if the voival would have fit in a u64q_t.
3476 switch (voi_dtype) {
3477 case VSD_DTYPE_INT_S32:
3478 error = Q_QCPYVALI(&x, voival->int32.s32);
3480 case VSD_DTYPE_INT_U32:
3481 error = Q_QCPYVALI(&x, voival->int32.u32);
3483 case VSD_DTYPE_INT_S64:
3484 error = Q_QCPYVALI(&x, voival->int64.s64);
3486 case VSD_DTYPE_INT_U64:
3487 error = Q_QCPYVALI(&x, voival->int64.u64);
3489 case VSD_DTYPE_INT_SLONG:
3490 error = Q_QCPYVALI(&x, voival->intlong.slong);
3492 case VSD_DTYPE_INT_ULONG:
3493 error = Q_QCPYVALI(&x, voival->intlong.ulong);
3495 case VSD_DTYPE_Q_S32:
3496 error = Q_QCPYVALQ(&x, voival->q32.sq32);
3498 case VSD_DTYPE_Q_U32:
3499 error = Q_QCPYVALQ(&x, voival->q32.uq32);
3501 case VSD_DTYPE_Q_S64:
3502 error = Q_QCPYVALQ(&x, voival->q64.sq64);
3504 case VSD_DTYPE_Q_U64:
3505 error = Q_QCPYVALQ(&x, voival->q64.uq64);
3513 (error = stats_v1_vsd_tdgst_add(vs->dtype, tdgst, x, 1, 1)))
3516 vs->flags |= VS_VSDVALID;
3521 stats_v1_voi_update(struct statsblobv1 *sb, int32_t voi_id,
3522 enum vsd_dtype voi_dtype, struct voistatdata *voival, uint32_t flags)
3526 void *statevsd, *vsd;
3527 int error, i, tmperr;
3531 if (sb == NULL || sb->abi != STATS_ABI_V1 || voi_id >= NVOIS(sb) ||
3532 voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES || voival == NULL)
3534 v = &sb->vois[voi_id];
3535 if (voi_dtype != v->dtype || v->id < 0 ||
3536 ((flags & SB_VOI_RELUPDATE) && !(v->flags & VOI_REQSTATE)))
3539 vs = BLOB_OFFSET(sb, v->stats_off);
3540 if (v->flags & VOI_REQSTATE)
3541 statevsd = BLOB_OFFSET(sb, vs->data_off);
3545 if (flags & SB_VOI_RELUPDATE) {
3546 switch (voi_dtype) {
3547 case VSD_DTYPE_INT_S32:
3548 voival->int32.s32 +=
3549 VSD(voistate, statevsd)->prev.int32.s32;
3551 case VSD_DTYPE_INT_U32:
3552 voival->int32.u32 +=
3553 VSD(voistate, statevsd)->prev.int32.u32;
3555 case VSD_DTYPE_INT_S64:
3556 voival->int64.s64 +=
3557 VSD(voistate, statevsd)->prev.int64.s64;
3559 case VSD_DTYPE_INT_U64:
3560 voival->int64.u64 +=
3561 VSD(voistate, statevsd)->prev.int64.u64;
3563 case VSD_DTYPE_INT_SLONG:
3564 voival->intlong.slong +=
3565 VSD(voistate, statevsd)->prev.intlong.slong;
3567 case VSD_DTYPE_INT_ULONG:
3568 voival->intlong.ulong +=
3569 VSD(voistate, statevsd)->prev.intlong.ulong;
3571 case VSD_DTYPE_Q_S32:
3572 error = Q_QADDQ(&voival->q32.sq32,
3573 VSD(voistate, statevsd)->prev.q32.sq32);
3575 case VSD_DTYPE_Q_U32:
3576 error = Q_QADDQ(&voival->q32.uq32,
3577 VSD(voistate, statevsd)->prev.q32.uq32);
3579 case VSD_DTYPE_Q_S64:
3580 error = Q_QADDQ(&voival->q64.sq64,
3581 VSD(voistate, statevsd)->prev.q64.sq64);
3583 case VSD_DTYPE_Q_U64:
3584 error = Q_QADDQ(&voival->q64.uq64,
3585 VSD(voistate, statevsd)->prev.q64.uq64);
3588 KASSERT(0, ("Unknown VOI data type %d", voi_dtype));
3596 for (i = v->voistatmaxid; i > 0; i--) {
3597 vs = &((struct voistat *)BLOB_OFFSET(sb, v->stats_off))[i];
3601 vsd = BLOB_OFFSET(sb, vs->data_off);
3603 switch (vs->stype) {
3605 tmperr = stats_v1_voi_update_max(voi_dtype, voival,
3609 tmperr = stats_v1_voi_update_min(voi_dtype, voival,
3613 tmperr = stats_v1_voi_update_sum(voi_dtype, voival,
3617 tmperr = stats_v1_voi_update_hist(voi_dtype, voival,
3620 case VS_STYPE_TDGST:
3621 tmperr = stats_v1_voi_update_tdgst(voi_dtype, voival,
3625 KASSERT(0, ("Unknown VOI stat type %d", vs->stype));
3636 switch (voi_dtype) {
3637 case VSD_DTYPE_INT_S32:
3638 VSD(voistate, statevsd)->prev.int32.s32 =
3641 case VSD_DTYPE_INT_U32:
3642 VSD(voistate, statevsd)->prev.int32.u32 =
3645 case VSD_DTYPE_INT_S64:
3646 VSD(voistate, statevsd)->prev.int64.s64 =
3649 case VSD_DTYPE_INT_U64:
3650 VSD(voistate, statevsd)->prev.int64.u64 =
3653 case VSD_DTYPE_INT_SLONG:
3654 VSD(voistate, statevsd)->prev.intlong.slong =
3655 voival->intlong.slong;
3657 case VSD_DTYPE_INT_ULONG:
3658 VSD(voistate, statevsd)->prev.intlong.ulong =
3659 voival->intlong.ulong;
3661 case VSD_DTYPE_Q_S32:
3663 &VSD(voistate, statevsd)->prev.q32.sq32,
3666 case VSD_DTYPE_Q_U32:
3668 &VSD(voistate, statevsd)->prev.q32.uq32,
3671 case VSD_DTYPE_Q_S64:
3673 &VSD(voistate, statevsd)->prev.q64.sq64,
3676 case VSD_DTYPE_Q_U64:
3678 &VSD(voistate, statevsd)->prev.q64.uq64,
3682 KASSERT(0, ("Unknown VOI data type %d", voi_dtype));
3693 stats_init(void *arg)
3697 SYSINIT(stats, SI_SUB_KDTRACE, SI_ORDER_FIRST, stats_init, NULL);
3700 * Sysctl handler to display the list of available stats templates.
3703 stats_tpl_list_available(SYSCTL_HANDLER_ARGS)
3710 /* We can tolerate ntpl being stale, so do not take the lock. */
3711 s = sbuf_new(NULL, NULL, /* +1 per tpl for , */
3712 ntpl * (STATS_TPL_MAX_STR_SPEC_LEN + 1), SBUF_FIXEDLEN);
3717 for (i = 0; i < ntpl; i++) {
3718 err = sbuf_printf(s, "%s\"%s\":%u", i ? "," : "",
3719 tpllist[i]->mb->tplname, tpllist[i]->mb->tplhash);
3721 /* Sbuf overflow condition. */
3730 err = sysctl_handle_string(oidp, sbuf_data(s), 0, req);
3738 * Called by subsystem-specific sysctls to report and/or parse the list of
3739 * templates being sampled and their sampling rates. A stats_tpl_sr_cb_t
3740 * conformant function pointer must be passed in as arg1, which is used to
3741 * interact with the subsystem's stats template sample rates list. If arg2 > 0,
3742 * a zero-initialised allocation of arg2-sized contextual memory is
3743 * heap-allocated and passed in to all subsystem callbacks made during the
3744 * operation of stats_tpl_sample_rates().
3746 * XXXLAS: Assumes templates are never removed, which is currently true but may
3747 * need to be reworked in future if dynamic template management becomes a
3748 * requirement e.g. to support kernel module based templates.
3751 stats_tpl_sample_rates(SYSCTL_HANDLER_ARGS)
3753 char kvpair_fmt[16], tplspec_fmt[16];
3754 char tpl_spec[STATS_TPL_MAX_STR_SPEC_LEN];
3755 char tpl_name[TPL_MAX_NAME_LEN + 2]; /* +2 for "" */
3756 stats_tpl_sr_cb_t subsys_cb;
3758 char *buf, *new_rates_usr_str, *tpl_name_p;
3759 struct stats_tpl_sample_rate *rates;
3761 uint32_t cum_pct, pct, tpl_hash;
3762 int err, i, off, len, newlen, nrates;
3767 subsys_cb = (stats_tpl_sr_cb_t)arg1;
3768 KASSERT(subsys_cb != NULL, ("%s: subsys_cb == arg1 == NULL", __func__));
3770 subsys_ctx = malloc(arg2, M_TEMP, M_WAITOK | M_ZERO);
3774 /* Grab current count of subsystem rates. */
3775 err = subsys_cb(TPL_SR_UNLOCKED_GET, NULL, &nrates, subsys_ctx);
3779 /* +1 to ensure we can append '\0' post copyin, +5 per rate for =nnn, */
3780 len = max(req->newlen + 1, nrates * (STATS_TPL_MAX_STR_SPEC_LEN + 5));
3782 if (req->oldptr != NULL || req->newptr != NULL)
3783 buf = malloc(len, M_TEMP, M_WAITOK);
3785 if (req->oldptr != NULL) {
3787 /* No rates, so return an empty string via oldptr. */
3788 err = SYSCTL_OUT(req, "", 1);
3794 s = sbuf_new(&_s, buf, len, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
3796 /* Grab locked count of, and ptr to, subsystem rates. */
3797 err = subsys_cb(TPL_SR_RLOCKED_GET, &rates, &nrates,
3802 for (i = 0; i < nrates && !err; i++) {
3803 err = sbuf_printf(s, "%s\"%s\":%u=%u", i ? "," : "",
3804 tpllist[rates[i].tpl_slot_id]->mb->tplname,
3805 tpllist[rates[i].tpl_slot_id]->mb->tplhash,
3806 rates[i].tpl_sample_pct);
3809 /* Tell subsystem that we're done with its rates list. */
3810 err = subsys_cb(TPL_SR_RUNLOCK, &rates, &nrates, subsys_ctx);
3814 err = sbuf_finish(s);
3816 goto done; /* We lost a race for buf to be too small. */
3818 /* Return the rendered string data via oldptr. */
3819 err = SYSCTL_OUT(req, sbuf_data(s), sbuf_len(s));
3821 /* Return the upper bound size for buffer sizing requests. */
3822 err = SYSCTL_OUT(req, NULL, len);
3826 if (err || req->newptr == NULL)
3829 newlen = req->newlen - req->newidx;
3830 err = SYSCTL_IN(req, buf, newlen);
3835 * Initialise format strings at run time.
3837 * Write the max template spec string length into the
3838 * template_spec=percent key-value pair parsing format string as:
3839 * " %<width>[^=]=%u %n"
3841 * Write the max template name string length into the tplname:tplhash
3842 * parsing format string as:
3845 * Subtract 1 for \0 appended by sscanf().
3847 sprintf(kvpair_fmt, " %%%zu[^=]=%%u %%n", sizeof(tpl_spec) - 1);
3848 sprintf(tplspec_fmt, "%%%zu[^:]:%%u", sizeof(tpl_name) - 1);
3851 * Parse each CSV key-value pair specifying a template and its sample
3852 * percentage. Whitespace either side of a key-value pair is ignored.
3853 * Templates can be specified by name, hash, or name and hash per the
3854 * following formats (chars in [] are optional):
3855 * ["]<tplname>["]=<percent>
3857 * ["]<tplname>["]:hash=<percent>
3859 cum_pct = nrates = 0;
3861 buf[newlen] = '\0'; /* buf is at least newlen+1 in size. */
3862 new_rates_usr_str = buf;
3863 while (isspace(*new_rates_usr_str))
3864 new_rates_usr_str++; /* Skip leading whitespace. */
3865 while (*new_rates_usr_str != '\0') {
3866 tpl_name_p = tpl_name;
3872 * Parse key-value pair which must perform 2 conversions, then
3873 * parse the template spec to extract either name, hash, or name
3874 * and hash depending on the three possible spec formats. The
3875 * tplspec_fmt format specifier parses name or name and hash
3876 * template specs, while the ":%u" format specifier parses
3877 * hash-only template specs. If parsing is successfull, ensure
3878 * the cumulative sampling percentage does not exceed 100.
3881 if (2 != sscanf(new_rates_usr_str, kvpair_fmt, tpl_spec, &pct,
3884 if ((1 > sscanf(tpl_spec, tplspec_fmt, tpl_name, &tpl_hash)) &&
3885 (1 != sscanf(tpl_spec, ":%u", &tpl_hash)))
3887 if ((cum_pct += pct) > 100)
3891 /* Strip surrounding "" from template name if present. */
3892 len = strlen(tpl_name);
3894 if (tpl_name[len - 1] == '"')
3895 tpl_name[--len] = '\0';
3896 if (tpl_name[0] == '"') {
3902 rates = stats_realloc(rates, 0, /* oldsz is unused in kernel. */
3903 (nrates + 1) * sizeof(*rates), M_WAITOK);
3904 rates[nrates].tpl_slot_id =
3905 stats_tpl_fetch_allocid(len ? tpl_name_p : NULL, tpl_hash);
3906 if (rates[nrates].tpl_slot_id < 0) {
3907 err = -rates[nrates].tpl_slot_id;
3910 rates[nrates].tpl_sample_pct = pct;
3912 new_rates_usr_str += off;
3913 if (*new_rates_usr_str != ',')
3914 break; /* End-of-input or malformed. */
3915 new_rates_usr_str++; /* Move past comma to next pair. */
3919 if ((new_rates_usr_str - buf) < newlen) {
3920 /* Entire input has not been consumed. */
3924 * Give subsystem the new rates. They'll return the
3925 * appropriate rates pointer for us to garbage collect.
3927 err = subsys_cb(TPL_SR_PUT, &rates, &nrates,
3935 free(subsys_ctx, M_TEMP);
3939 SYSCTL_NODE(_kern, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
3942 SYSCTL_PROC(_kern_stats, OID_AUTO, templates,
3943 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3944 stats_tpl_list_available, "A",
3945 "list the name/hash of all available stats(9) templates");
3947 #else /* ! _KERNEL */
3949 static void __attribute__ ((constructor))
3950 stats_constructor(void)
3953 pthread_rwlock_init(&tpllistlock, NULL);
3956 static void __attribute__ ((destructor))
3957 stats_destructor(void)
3960 pthread_rwlock_destroy(&tpllistlock);
3963 #endif /* _KERNEL */