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
30 * Author: Lawrence Stewart <lstewart@netflix.com>
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/ctype.h>
39 #include <sys/errno.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 #include <sys/qmath.h>
45 #if defined(DIAGNOSTIC)
48 #include <sys/stats.h> /* Must come after qmath.h and arb.h */
49 #include <sys/stddef.h>
50 #include <sys/stdint.h>
54 #include <sys/kernel.h>
56 #include <sys/rwlock.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
67 struct voistatdata_voistate {
68 /* Previous VOI value for diff calculation. */
69 struct voistatdata_numeric prev;
72 #define VS_VSDVALID 0x0001 /* Stat's voistatdata updated at least once. */
74 int8_t stype; /* Type of stat e.g. VS_STYPE_SUM. */
75 enum vsd_dtype dtype : 8; /* Data type of this stat's data. */
76 uint16_t data_off; /* Blob offset for this stat's data. */
77 uint16_t dsz; /* Size of stat's data. */
79 uint16_t errs : VS_EBITS;/* Non-wrapping error count. */
80 uint16_t flags : 16 - VS_EBITS;
82 /* The voistat error count is capped to avoid wrapping. */
83 #define VS_INCERRS(vs) do { \
84 if ((vs)->errs < (1U << VS_EBITS) - 1) \
90 * - Global or entity specific (global would imply use of counter(9)?)
91 * - Whether to reset stats on read or not
92 * - Signal an overflow?
93 * - Compressed voistat array
95 #define VOI_REQSTATE 0x0001 /* VOI requires VS_STYPE_VOISTATE. */
97 int16_t id; /* VOI id. */
98 enum vsd_dtype dtype : 8; /* Data type of the VOI itself. */
99 int8_t voistatmaxid; /* Largest allocated voistat index. */
100 uint16_t stats_off; /* Blob offset for this VOIs stats. */
105 * Memory for the entire blob is allocated as a slab and then offsets are
106 * maintained to carve up the slab into sections holding different data types.
109 * - Compressed voi array (trade off memory usage vs search time)
110 * - Units of offsets (default bytes, flag for e.g. vm_page/KiB/Mib)
118 /* Fields from here down are opaque to consumers. */
119 uint32_t tplhash; /* Base template hash ID. */
120 uint16_t stats_off; /* voistat array blob offset. */
121 uint16_t statsdata_off; /* voistatdata array blob offset. */
122 sbintime_t created; /* Blob creation time. */
123 sbintime_t lastrst; /* Time of last reset. */
124 struct voi vois[]; /* Array indexed by [voi_id]. */
125 } __aligned(sizeof(void *));
126 _Static_assert(offsetof(struct statsblobv1, cursz) +
127 SIZEOF_MEMBER(struct statsblobv1, cursz) ==
128 offsetof(struct statsblob, opaque),
129 "statsblobv1 ABI mismatch");
131 struct statsblobv1_tpl {
133 struct statsblobv1 *sb;
136 /* Context passed to iterator callbacks. */
138 void *usrctx; /* Caller supplied context. */
139 uint32_t flags; /* Flags for current iteration. */
140 int16_t vslot; /* struct voi slot index. */
141 int8_t vsslot; /* struct voistat slot index. */
144 struct sb_tostrcb_ctx {
146 struct statsblob_tpl *tpl;
151 struct sb_visitcb_ctx {
152 stats_blob_visitcb_t cb;
156 /* Stats blob iterator callback. */
157 typedef int (*stats_v1_blob_itercb_t)(struct statsblobv1 *sb, struct voi *v,
158 struct voistat *vs, struct sb_iter_ctx *ctx);
161 static struct rwlock tpllistlock;
162 RW_SYSINIT(stats_tpl_list, &tpllistlock, "Stat template list lock");
163 #define TPL_LIST_RLOCK() rw_rlock(&tpllistlock)
164 #define TPL_LIST_RUNLOCK() rw_runlock(&tpllistlock)
165 #define TPL_LIST_WLOCK() rw_wlock(&tpllistlock)
166 #define TPL_LIST_WUNLOCK() rw_wunlock(&tpllistlock)
167 #define TPL_LIST_LOCK_ASSERT() rw_assert(&tpllistlock, RA_LOCKED)
168 #define TPL_LIST_RLOCK_ASSERT() rw_assert(&tpllistlock, RA_RLOCKED)
169 #define TPL_LIST_WLOCK_ASSERT() rw_assert(&tpllistlock, RA_WLOCKED)
170 MALLOC_DEFINE(M_STATS, "stats(9) related memory", "stats(9) related memory");
171 #define stats_free(ptr) free((ptr), M_STATS)
172 #else /* ! _KERNEL */
173 static void stats_constructor(void);
174 static void stats_destructor(void);
175 static pthread_rwlock_t tpllistlock;
176 #define TPL_LIST_UNLOCK() pthread_rwlock_unlock(&tpllistlock)
177 #define TPL_LIST_RLOCK() pthread_rwlock_rdlock(&tpllistlock)
178 #define TPL_LIST_RUNLOCK() TPL_LIST_UNLOCK()
179 #define TPL_LIST_WLOCK() pthread_rwlock_wrlock(&tpllistlock)
180 #define TPL_LIST_WUNLOCK() TPL_LIST_UNLOCK()
181 #define TPL_LIST_LOCK_ASSERT() do { } while (0)
182 #define TPL_LIST_RLOCK_ASSERT() do { } while (0)
183 #define TPL_LIST_WLOCK_ASSERT() do { } while (0)
185 #define KASSERT(cond, msg) do {} while (0)
186 #define stats_abort() do {} while (0)
188 #define KASSERT(cond, msg) do { \
193 #define stats_abort() abort()
195 #define stats_free(ptr) free(ptr)
196 #define panic(fmt, ...) do { \
197 fprintf(stderr, (fmt), ##__VA_ARGS__); \
202 #define SB_V1_MAXSZ 65535
204 /* Obtain a blob offset pointer. */
205 #define BLOB_OFFSET(sb, off) ((void *)(((uint8_t *)(sb)) + (off)))
208 * Number of VOIs in the blob's vois[] array. By virtue of struct voi being a
209 * power of 2 size, we can shift instead of divide. The shift amount must be
210 * updated if sizeof(struct voi) ever changes, which the assert should catch.
212 #define NVOIS(sb) ((int32_t)((((struct statsblobv1 *)(sb))->stats_off - \
213 sizeof(struct statsblobv1)) >> 3))
214 _Static_assert(sizeof(struct voi) == 8, "statsblobv1 voi ABI mismatch");
216 /* Try restrict names to alphanumeric and underscore to simplify JSON compat. */
217 const char *vs_stype2name[VS_NUM_STYPES] = {
218 [VS_STYPE_VOISTATE] = "VOISTATE",
219 [VS_STYPE_SUM] = "SUM",
220 [VS_STYPE_MAX] = "MAX",
221 [VS_STYPE_MIN] = "MIN",
222 [VS_STYPE_HIST] = "HIST",
223 [VS_STYPE_TDGST] = "TDGST",
226 const char *vs_stype2desc[VS_NUM_STYPES] = {
227 [VS_STYPE_VOISTATE] = "VOI related state data (not a real stat)",
228 [VS_STYPE_SUM] = "Simple arithmetic accumulator",
229 [VS_STYPE_MAX] = "Maximum observed VOI value",
230 [VS_STYPE_MIN] = "Minimum observed VOI value",
231 [VS_STYPE_HIST] = "Histogram of observed VOI values",
232 [VS_STYPE_TDGST] = "t-digest of observed VOI values",
235 const char *vsd_dtype2name[VSD_NUM_DTYPES] = {
236 [VSD_DTYPE_VOISTATE] = "VOISTATE",
237 [VSD_DTYPE_INT_S32] = "INT_S32",
238 [VSD_DTYPE_INT_U32] = "INT_U32",
239 [VSD_DTYPE_INT_S64] = "INT_S64",
240 [VSD_DTYPE_INT_U64] = "INT_U64",
241 [VSD_DTYPE_INT_SLONG] = "INT_SLONG",
242 [VSD_DTYPE_INT_ULONG] = "INT_ULONG",
243 [VSD_DTYPE_Q_S32] = "Q_S32",
244 [VSD_DTYPE_Q_U32] = "Q_U32",
245 [VSD_DTYPE_Q_S64] = "Q_S64",
246 [VSD_DTYPE_Q_U64] = "Q_U64",
247 [VSD_DTYPE_CRHIST32] = "CRHIST32",
248 [VSD_DTYPE_DRHIST32] = "DRHIST32",
249 [VSD_DTYPE_DVHIST32] = "DVHIST32",
250 [VSD_DTYPE_CRHIST64] = "CRHIST64",
251 [VSD_DTYPE_DRHIST64] = "DRHIST64",
252 [VSD_DTYPE_DVHIST64] = "DVHIST64",
253 [VSD_DTYPE_TDGSTCLUST32] = "TDGSTCLUST32",
254 [VSD_DTYPE_TDGSTCLUST64] = "TDGSTCLUST64",
257 const size_t vsd_dtype2size[VSD_NUM_DTYPES] = {
258 [VSD_DTYPE_VOISTATE] = sizeof(struct voistatdata_voistate),
259 [VSD_DTYPE_INT_S32] = sizeof(struct voistatdata_int32),
260 [VSD_DTYPE_INT_U32] = sizeof(struct voistatdata_int32),
261 [VSD_DTYPE_INT_S64] = sizeof(struct voistatdata_int64),
262 [VSD_DTYPE_INT_U64] = sizeof(struct voistatdata_int64),
263 [VSD_DTYPE_INT_SLONG] = sizeof(struct voistatdata_intlong),
264 [VSD_DTYPE_INT_ULONG] = sizeof(struct voistatdata_intlong),
265 [VSD_DTYPE_Q_S32] = sizeof(struct voistatdata_q32),
266 [VSD_DTYPE_Q_U32] = sizeof(struct voistatdata_q32),
267 [VSD_DTYPE_Q_S64] = sizeof(struct voistatdata_q64),
268 [VSD_DTYPE_Q_U64] = sizeof(struct voistatdata_q64),
269 [VSD_DTYPE_CRHIST32] = sizeof(struct voistatdata_crhist32),
270 [VSD_DTYPE_DRHIST32] = sizeof(struct voistatdata_drhist32),
271 [VSD_DTYPE_DVHIST32] = sizeof(struct voistatdata_dvhist32),
272 [VSD_DTYPE_CRHIST64] = sizeof(struct voistatdata_crhist64),
273 [VSD_DTYPE_DRHIST64] = sizeof(struct voistatdata_drhist64),
274 [VSD_DTYPE_DVHIST64] = sizeof(struct voistatdata_dvhist64),
275 [VSD_DTYPE_TDGSTCLUST32] = sizeof(struct voistatdata_tdgstclust32),
276 [VSD_DTYPE_TDGSTCLUST64] = sizeof(struct voistatdata_tdgstclust64),
279 static const bool vsd_compoundtype[VSD_NUM_DTYPES] = {
280 [VSD_DTYPE_VOISTATE] = true,
281 [VSD_DTYPE_INT_S32] = false,
282 [VSD_DTYPE_INT_U32] = false,
283 [VSD_DTYPE_INT_S64] = false,
284 [VSD_DTYPE_INT_U64] = false,
285 [VSD_DTYPE_INT_SLONG] = false,
286 [VSD_DTYPE_INT_ULONG] = false,
287 [VSD_DTYPE_Q_S32] = false,
288 [VSD_DTYPE_Q_U32] = false,
289 [VSD_DTYPE_Q_S64] = false,
290 [VSD_DTYPE_Q_U64] = false,
291 [VSD_DTYPE_CRHIST32] = true,
292 [VSD_DTYPE_DRHIST32] = true,
293 [VSD_DTYPE_DVHIST32] = true,
294 [VSD_DTYPE_CRHIST64] = true,
295 [VSD_DTYPE_DRHIST64] = true,
296 [VSD_DTYPE_DVHIST64] = true,
297 [VSD_DTYPE_TDGSTCLUST32] = true,
298 [VSD_DTYPE_TDGSTCLUST64] = true,
301 const struct voistatdata_numeric numeric_limits[2][VSD_DTYPE_Q_U64 + 1] = {
303 [VSD_DTYPE_VOISTATE] = {0},
304 [VSD_DTYPE_INT_S32] = {.int32 = {.s32 = INT32_MIN}},
305 [VSD_DTYPE_INT_U32] = {.int32 = {.u32 = 0}},
306 [VSD_DTYPE_INT_S64] = {.int64 = {.s64 = INT64_MIN}},
307 [VSD_DTYPE_INT_U64] = {.int64 = {.u64 = 0}},
308 [VSD_DTYPE_INT_SLONG] = {.intlong = {.slong = LONG_MIN}},
309 [VSD_DTYPE_INT_ULONG] = {.intlong = {.ulong = 0}},
310 [VSD_DTYPE_Q_S32] = {.q32 = {.sq32 = Q_IFMINVAL(INT32_MIN)}},
311 [VSD_DTYPE_Q_U32] = {.q32 = {.uq32 = 0}},
312 [VSD_DTYPE_Q_S64] = {.q64 = {.sq64 = Q_IFMINVAL(INT64_MIN)}},
313 [VSD_DTYPE_Q_U64] = {.q64 = {.uq64 = 0}},
316 [VSD_DTYPE_VOISTATE] = {0},
317 [VSD_DTYPE_INT_S32] = {.int32 = {.s32 = INT32_MAX}},
318 [VSD_DTYPE_INT_U32] = {.int32 = {.u32 = UINT32_MAX}},
319 [VSD_DTYPE_INT_S64] = {.int64 = {.s64 = INT64_MAX}},
320 [VSD_DTYPE_INT_U64] = {.int64 = {.u64 = UINT64_MAX}},
321 [VSD_DTYPE_INT_SLONG] = {.intlong = {.slong = LONG_MAX}},
322 [VSD_DTYPE_INT_ULONG] = {.intlong = {.ulong = ULONG_MAX}},
323 [VSD_DTYPE_Q_S32] = {.q32 = {.sq32 = Q_IFMAXVAL(INT32_MAX)}},
324 [VSD_DTYPE_Q_U32] = {.q32 = {.uq32 = Q_IFMAXVAL(UINT32_MAX)}},
325 [VSD_DTYPE_Q_S64] = {.q64 = {.sq64 = Q_IFMAXVAL(INT64_MAX)}},
326 [VSD_DTYPE_Q_U64] = {.q64 = {.uq64 = Q_IFMAXVAL(UINT64_MAX)}},
330 /* tpllistlock protects tpllist and ntpl */
331 static uint32_t ntpl;
332 static struct statsblob_tpl **tpllist;
334 static inline void * stats_realloc(void *ptr, size_t oldsz, size_t newsz,
336 //static void stats_v1_blob_finalise(struct statsblobv1 *sb);
337 static int stats_v1_blob_init_locked(struct statsblobv1 *sb, uint32_t tpl_id,
339 static int stats_v1_blob_expand(struct statsblobv1 **sbpp, int newvoibytes,
340 int newvoistatbytes, int newvoistatdatabytes);
341 static void stats_v1_blob_iter(struct statsblobv1 *sb,
342 stats_v1_blob_itercb_t icb, void *usrctx, uint32_t flags);
343 static inline int stats_v1_vsd_tdgst_add(enum vsd_dtype vs_dtype,
344 struct voistatdata_tdgst *tdgst, s64q_t x, uint64_t weight, int attempt);
347 ctd32cmp(const struct voistatdata_tdgstctd32 *c1, const struct voistatdata_tdgstctd32 *c2)
350 KASSERT(Q_PRECEQ(c1->mu, c2->mu),
351 ("%s: Q_RELPREC(c1->mu,c2->mu)=%d", __func__,
352 Q_RELPREC(c1->mu, c2->mu)));
354 return (Q_QLTQ(c1->mu, c2->mu) ? -1 : 1);
356 ARB_GENERATE_STATIC(ctdth32, voistatdata_tdgstctd32, ctdlnk, ctd32cmp);
359 ctd64cmp(const struct voistatdata_tdgstctd64 *c1, const struct voistatdata_tdgstctd64 *c2)
362 KASSERT(Q_PRECEQ(c1->mu, c2->mu),
363 ("%s: Q_RELPREC(c1->mu,c2->mu)=%d", __func__,
364 Q_RELPREC(c1->mu, c2->mu)));
366 return (Q_QLTQ(c1->mu, c2->mu) ? -1 : 1);
368 ARB_GENERATE_STATIC(ctdth64, voistatdata_tdgstctd64, ctdlnk, ctd64cmp);
371 RB_GENERATE_STATIC(rbctdth32, voistatdata_tdgstctd32, rblnk, ctd32cmp);
372 RB_GENERATE_STATIC(rbctdth64, voistatdata_tdgstctd64, rblnk, ctd64cmp);
375 static inline sbintime_t
376 stats_sbinuptime(void)
382 #else /* ! _KERNEL */
385 clock_gettime(CLOCK_MONOTONIC_FAST, &tp);
393 stats_realloc(void *ptr, size_t oldsz, size_t newsz, int flags)
397 /* Default to M_NOWAIT if neither M_NOWAIT or M_WAITOK are set. */
398 if (!(flags & (M_WAITOK | M_NOWAIT)))
400 ptr = realloc(ptr, newsz, M_STATS, flags);
401 #else /* ! _KERNEL */
402 ptr = realloc(ptr, newsz);
403 if ((flags & M_ZERO) && ptr != NULL) {
405 memset(ptr, '\0', newsz);
406 else if (newsz > oldsz)
407 memset(BLOB_OFFSET(ptr, oldsz), '\0', newsz - oldsz);
415 stats_strdup(const char *s,
422 if (!(flags & (M_WAITOK | M_NOWAIT)))
426 if ((copy = malloc(len, M_STATS, flags)) != NULL)
438 stats_tpl_update_hash(struct statsblob_tpl *tpl)
441 TPL_LIST_WLOCK_ASSERT();
442 tpl->mb->tplhash = hash32_str(tpl->mb->tplname, 0);
443 for (int voi_id = 0; voi_id < NVOIS(tpl->sb); voi_id++) {
444 if (tpl->mb->voi_meta[voi_id].name != NULL)
445 tpl->mb->tplhash = hash32_str(
446 tpl->mb->voi_meta[voi_id].name, tpl->mb->tplhash);
448 tpl->mb->tplhash = hash32_buf(tpl->sb, tpl->sb->cursz,
452 static inline uint64_t
453 stats_pow_u64(uint64_t base, uint64_t exp)
468 stats_vss_hist_bkt_hlpr(struct vss_hist_hlpr_info *info, uint32_t curbkt,
469 struct voistatdata_numeric *bkt_lb, struct voistatdata_numeric *bkt_ub)
474 switch (info->scheme) {
476 step = info->lin.stepinc;
479 step = stats_pow_u64(info->exp.stepbase,
480 info->exp.stepexp + curbkt);
484 uint64_t curstepexp = 1;
486 switch (info->voi_dtype) {
487 case VSD_DTYPE_INT_S32:
488 while ((int32_t)stats_pow_u64(info->linexp.stepbase,
489 curstepexp) <= bkt_lb->int32.s32)
492 case VSD_DTYPE_INT_U32:
493 while ((uint32_t)stats_pow_u64(info->linexp.stepbase,
494 curstepexp) <= bkt_lb->int32.u32)
497 case VSD_DTYPE_INT_S64:
498 while ((int64_t)stats_pow_u64(info->linexp.stepbase,
499 curstepexp) <= bkt_lb->int64.s64)
502 case VSD_DTYPE_INT_U64:
503 while ((uint64_t)stats_pow_u64(info->linexp.stepbase,
504 curstepexp) <= bkt_lb->int64.u64)
507 case VSD_DTYPE_INT_SLONG:
508 while ((long)stats_pow_u64(info->linexp.stepbase,
509 curstepexp) <= bkt_lb->intlong.slong)
512 case VSD_DTYPE_INT_ULONG:
513 while ((unsigned long)stats_pow_u64(info->linexp.stepbase,
514 curstepexp) <= bkt_lb->intlong.ulong)
517 case VSD_DTYPE_Q_S32:
518 while ((s32q_t)stats_pow_u64(info->linexp.stepbase,
519 curstepexp) <= Q_GIVAL(bkt_lb->q32.sq32))
521 case VSD_DTYPE_Q_U32:
522 while ((u32q_t)stats_pow_u64(info->linexp.stepbase,
523 curstepexp) <= Q_GIVAL(bkt_lb->q32.uq32))
525 case VSD_DTYPE_Q_S64:
526 while ((s64q_t)stats_pow_u64(info->linexp.stepbase,
527 curstepexp) <= Q_GIVAL(bkt_lb->q64.sq64))
530 case VSD_DTYPE_Q_U64:
531 while ((u64q_t)stats_pow_u64(info->linexp.stepbase,
532 curstepexp) <= Q_GIVAL(bkt_lb->q64.uq64))
539 step = stats_pow_u64(info->linexp.stepbase, curstepexp) /
540 info->linexp.linstepdiv;
549 if (info->scheme == BKT_USR) {
550 *bkt_lb = info->usr.bkts[curbkt].lb;
551 *bkt_ub = info->usr.bkts[curbkt].ub;
552 } else if (step != 0) {
553 switch (info->voi_dtype) {
554 case VSD_DTYPE_INT_S32:
555 bkt_ub->int32.s32 += (int32_t)step;
557 case VSD_DTYPE_INT_U32:
558 bkt_ub->int32.u32 += (uint32_t)step;
560 case VSD_DTYPE_INT_S64:
561 bkt_ub->int64.s64 += (int64_t)step;
563 case VSD_DTYPE_INT_U64:
564 bkt_ub->int64.u64 += (uint64_t)step;
566 case VSD_DTYPE_INT_SLONG:
567 bkt_ub->intlong.slong += (long)step;
569 case VSD_DTYPE_INT_ULONG:
570 bkt_ub->intlong.ulong += (unsigned long)step;
572 case VSD_DTYPE_Q_S32:
573 error = Q_QADDI(&bkt_ub->q32.sq32, step);
575 case VSD_DTYPE_Q_U32:
576 error = Q_QADDI(&bkt_ub->q32.uq32, step);
578 case VSD_DTYPE_Q_S64:
579 error = Q_QADDI(&bkt_ub->q64.sq64, step);
581 case VSD_DTYPE_Q_U64:
582 error = Q_QADDI(&bkt_ub->q64.uq64, step);
587 } else { /* info->scheme != BKT_USR && step == 0 */
595 stats_vss_hist_nbkts_hlpr(struct vss_hist_hlpr_info *info)
597 struct voistatdata_numeric bkt_lb, bkt_ub;
601 if (info->scheme == BKT_USR) {
602 /* XXXLAS: Setting info->{lb,ub} from macro is tricky. */
603 info->lb = info->usr.bkts[0].lb;
604 info->ub = info->usr.bkts[info->usr.nbkts - 1].lb;
613 if (stats_vss_hist_bkt_hlpr(info, nbkts++, &bkt_lb, &bkt_ub))
616 if (info->scheme == BKT_USR)
617 done = (nbkts == info->usr.nbkts);
619 switch (info->voi_dtype) {
620 case VSD_DTYPE_INT_S32:
621 done = (bkt_ub.int32.s32 > info->ub.int32.s32);
623 case VSD_DTYPE_INT_U32:
624 done = (bkt_ub.int32.u32 > info->ub.int32.u32);
626 case VSD_DTYPE_INT_S64:
627 done = (bkt_ub.int64.s64 > info->ub.int64.s64);
629 case VSD_DTYPE_INT_U64:
630 done = (bkt_ub.int64.u64 > info->ub.int64.u64);
632 case VSD_DTYPE_INT_SLONG:
633 done = (bkt_ub.intlong.slong >
634 info->ub.intlong.slong);
636 case VSD_DTYPE_INT_ULONG:
637 done = (bkt_ub.intlong.ulong >
638 info->ub.intlong.ulong);
640 case VSD_DTYPE_Q_S32:
641 done = Q_QGTQ(bkt_ub.q32.sq32,
644 case VSD_DTYPE_Q_U32:
645 done = Q_QGTQ(bkt_ub.q32.uq32,
648 case VSD_DTYPE_Q_S64:
649 done = Q_QGTQ(bkt_ub.q64.sq64,
652 case VSD_DTYPE_Q_U64:
653 done = Q_QGTQ(bkt_ub.q64.uq64,
662 if (info->flags & VSD_HIST_LBOUND_INF)
664 if (info->flags & VSD_HIST_UBOUND_INF)
671 stats_vss_hist_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
672 struct vss_hist_hlpr_info *info)
674 struct voistatdata_hist *hist;
675 struct voistatdata_numeric bkt_lb, bkt_ub, *lbinfbktlb, *lbinfbktub,
676 *ubinfbktlb, *ubinfbktub;
677 uint32_t bkt, nbkts, nloop;
679 if (vss == NULL || info == NULL || (info->flags &
680 (VSD_HIST_LBOUND_INF|VSD_HIST_UBOUND_INF) && (info->hist_dtype ==
681 VSD_DTYPE_DVHIST32 || info->hist_dtype == VSD_DTYPE_DVHIST64)))
684 info->voi_dtype = voi_dtype;
686 if ((nbkts = stats_vss_hist_nbkts_hlpr(info)) == 0)
689 switch (info->hist_dtype) {
690 case VSD_DTYPE_CRHIST32:
691 vss->vsdsz = HIST_NBKTS2VSDSZ(crhist32, nbkts);
693 case VSD_DTYPE_DRHIST32:
694 vss->vsdsz = HIST_NBKTS2VSDSZ(drhist32, nbkts);
696 case VSD_DTYPE_DVHIST32:
697 vss->vsdsz = HIST_NBKTS2VSDSZ(dvhist32, nbkts);
699 case VSD_DTYPE_CRHIST64:
700 vss->vsdsz = HIST_NBKTS2VSDSZ(crhist64, nbkts);
702 case VSD_DTYPE_DRHIST64:
703 vss->vsdsz = HIST_NBKTS2VSDSZ(drhist64, nbkts);
705 case VSD_DTYPE_DVHIST64:
706 vss->vsdsz = HIST_NBKTS2VSDSZ(dvhist64, nbkts);
712 vss->iv = stats_realloc(NULL, 0, vss->vsdsz, M_ZERO);
716 hist = (struct voistatdata_hist *)vss->iv;
719 for (bkt = (info->flags & VSD_HIST_LBOUND_INF), nloop = 0;
723 if (stats_vss_hist_bkt_hlpr(info, nloop, &bkt_lb, &bkt_ub))
726 switch (info->hist_dtype) {
727 case VSD_DTYPE_CRHIST32:
728 VSD(crhist32, hist)->bkts[bkt].lb = bkt_lb;
730 case VSD_DTYPE_DRHIST32:
731 VSD(drhist32, hist)->bkts[bkt].lb = bkt_lb;
732 VSD(drhist32, hist)->bkts[bkt].ub = bkt_ub;
734 case VSD_DTYPE_DVHIST32:
735 VSD(dvhist32, hist)->bkts[bkt].val = bkt_lb;
737 case VSD_DTYPE_CRHIST64:
738 VSD(crhist64, hist)->bkts[bkt].lb = bkt_lb;
740 case VSD_DTYPE_DRHIST64:
741 VSD(drhist64, hist)->bkts[bkt].lb = bkt_lb;
742 VSD(drhist64, hist)->bkts[bkt].ub = bkt_ub;
744 case VSD_DTYPE_DVHIST64:
745 VSD(dvhist64, hist)->bkts[bkt].val = bkt_lb;
752 lbinfbktlb = lbinfbktub = ubinfbktlb = ubinfbktub = NULL;
754 switch (info->hist_dtype) {
755 case VSD_DTYPE_CRHIST32:
756 lbinfbktlb = &VSD(crhist32, hist)->bkts[0].lb;
757 ubinfbktlb = &VSD(crhist32, hist)->bkts[nbkts - 1].lb;
759 case VSD_DTYPE_DRHIST32:
760 lbinfbktlb = &VSD(drhist32, hist)->bkts[0].lb;
761 lbinfbktub = &VSD(drhist32, hist)->bkts[0].ub;
762 ubinfbktlb = &VSD(drhist32, hist)->bkts[nbkts - 1].lb;
763 ubinfbktub = &VSD(drhist32, hist)->bkts[nbkts - 1].ub;
765 case VSD_DTYPE_CRHIST64:
766 lbinfbktlb = &VSD(crhist64, hist)->bkts[0].lb;
767 ubinfbktlb = &VSD(crhist64, hist)->bkts[nbkts - 1].lb;
769 case VSD_DTYPE_DRHIST64:
770 lbinfbktlb = &VSD(drhist64, hist)->bkts[0].lb;
771 lbinfbktub = &VSD(drhist64, hist)->bkts[0].ub;
772 ubinfbktlb = &VSD(drhist64, hist)->bkts[nbkts - 1].lb;
773 ubinfbktub = &VSD(drhist64, hist)->bkts[nbkts - 1].ub;
775 case VSD_DTYPE_DVHIST32:
776 case VSD_DTYPE_DVHIST64:
782 if ((info->flags & VSD_HIST_LBOUND_INF) && lbinfbktlb) {
783 *lbinfbktlb = numeric_limits[LIM_MIN][info->voi_dtype];
785 * Assignment from numeric_limit array for Q types assigns max
786 * possible integral/fractional value for underlying data type,
787 * but we must set control bits for this specific histogram per
788 * the user's choice of fractional bits, which we extract from
791 if (info->voi_dtype == VSD_DTYPE_Q_S32 ||
792 info->voi_dtype == VSD_DTYPE_Q_U32) {
793 /* Signedness doesn't matter for setting control bits. */
794 Q_SCVAL(lbinfbktlb->q32.sq32,
795 Q_GCVAL(info->lb.q32.sq32));
796 } else if (info->voi_dtype == VSD_DTYPE_Q_S64 ||
797 info->voi_dtype == VSD_DTYPE_Q_U64) {
798 /* Signedness doesn't matter for setting control bits. */
799 Q_SCVAL(lbinfbktlb->q64.sq64,
800 Q_GCVAL(info->lb.q64.sq64));
803 *lbinfbktub = info->lb;
805 if ((info->flags & VSD_HIST_UBOUND_INF) && ubinfbktlb) {
806 *ubinfbktlb = bkt_lb;
808 *ubinfbktub = numeric_limits[LIM_MAX][info->voi_dtype];
809 if (info->voi_dtype == VSD_DTYPE_Q_S32 ||
810 info->voi_dtype == VSD_DTYPE_Q_U32) {
811 Q_SCVAL(ubinfbktub->q32.sq32,
812 Q_GCVAL(info->lb.q32.sq32));
813 } else if (info->voi_dtype == VSD_DTYPE_Q_S64 ||
814 info->voi_dtype == VSD_DTYPE_Q_U64) {
815 Q_SCVAL(ubinfbktub->q64.sq64,
816 Q_GCVAL(info->lb.q64.sq64));
825 stats_vss_tdgst_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
826 struct vss_tdgst_hlpr_info *info)
828 struct voistatdata_tdgst *tdgst;
829 struct ctdth32 *ctd32tree;
830 struct ctdth64 *ctd64tree;
831 struct voistatdata_tdgstctd32 *ctd32;
832 struct voistatdata_tdgstctd64 *ctd64;
834 info->voi_dtype = voi_dtype;
836 switch (info->tdgst_dtype) {
837 case VSD_DTYPE_TDGSTCLUST32:
838 vss->vsdsz = TDGST_NCTRS2VSDSZ(tdgstclust32, info->nctds);
840 case VSD_DTYPE_TDGSTCLUST64:
841 vss->vsdsz = TDGST_NCTRS2VSDSZ(tdgstclust64, info->nctds);
847 vss->iv = stats_realloc(NULL, 0, vss->vsdsz, M_ZERO);
851 tdgst = (struct voistatdata_tdgst *)vss->iv;
853 switch (info->tdgst_dtype) {
854 case VSD_DTYPE_TDGSTCLUST32:
855 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
856 ARB_INIT(ctd32, ctdlnk, ctd32tree, info->nctds) {
857 Q_INI(&ctd32->mu, 0, 0, info->prec);
860 case VSD_DTYPE_TDGSTCLUST64:
861 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
862 ARB_INIT(ctd64, ctdlnk, ctd64tree, info->nctds) {
863 Q_INI(&ctd64->mu, 0, 0, info->prec);
874 stats_vss_numeric_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
875 struct vss_numeric_hlpr_info *info)
877 struct voistatdata_numeric iv;
879 switch (vss->stype) {
881 iv = stats_ctor_vsd_numeric(0);
884 iv = numeric_limits[LIM_MAX][voi_dtype];
887 iv = numeric_limits[LIM_MIN][voi_dtype];
893 vss->iv = stats_realloc(NULL, 0, vsd_dtype2size[voi_dtype], 0);
897 vss->vs_dtype = voi_dtype;
898 vss->vsdsz = vsd_dtype2size[voi_dtype];
900 case VSD_DTYPE_INT_S32:
901 *((int32_t *)vss->iv) = iv.int32.s32;
903 case VSD_DTYPE_INT_U32:
904 *((uint32_t *)vss->iv) = iv.int32.u32;
906 case VSD_DTYPE_INT_S64:
907 *((int64_t *)vss->iv) = iv.int64.s64;
909 case VSD_DTYPE_INT_U64:
910 *((uint64_t *)vss->iv) = iv.int64.u64;
912 case VSD_DTYPE_INT_SLONG:
913 *((long *)vss->iv) = iv.intlong.slong;
915 case VSD_DTYPE_INT_ULONG:
916 *((unsigned long *)vss->iv) = iv.intlong.ulong;
918 case VSD_DTYPE_Q_S32:
919 *((s32q_t *)vss->iv) = Q_SCVAL(iv.q32.sq32,
920 Q_CTRLINI(info->prec));
922 case VSD_DTYPE_Q_U32:
923 *((u32q_t *)vss->iv) = Q_SCVAL(iv.q32.uq32,
924 Q_CTRLINI(info->prec));
926 case VSD_DTYPE_Q_S64:
927 *((s64q_t *)vss->iv) = Q_SCVAL(iv.q64.sq64,
928 Q_CTRLINI(info->prec));
930 case VSD_DTYPE_Q_U64:
931 *((u64q_t *)vss->iv) = Q_SCVAL(iv.q64.uq64,
932 Q_CTRLINI(info->prec));
942 stats_vss_hlpr_init(enum vsd_dtype voi_dtype, uint32_t nvss,
943 struct voistatspec *vss)
947 for (i = nvss - 1; i >= 0; i--) {
948 if (vss[i].hlpr && (ret = vss[i].hlpr(voi_dtype, &vss[i],
949 vss[i].hlprinfo)) != 0)
957 stats_vss_hlpr_cleanup(uint32_t nvss, struct voistatspec *vss)
961 for (i = nvss - 1; i >= 0; i--) {
963 stats_free((void *)vss[i].iv);
970 stats_tpl_fetch(int tpl_id, struct statsblob_tpl **tpl)
977 if (tpl_id < 0 || tpl_id >= (int)ntpl) {
980 *tpl = tpllist[tpl_id];
981 /* XXXLAS: Acquire refcount on tpl. */
989 stats_tpl_fetch_allocid(const char *name, uint32_t hash)
996 for (i = ntpl - 1; i >= 0; i--) {
998 if (strlen(name) == strlen(tpllist[i]->mb->tplname) &&
999 strncmp(name, tpllist[i]->mb->tplname,
1000 TPL_MAX_NAME_LEN) == 0 && (!hash || hash ==
1001 tpllist[i]->mb->tplhash)) {
1005 } else if (hash == tpllist[i]->mb->tplhash) {
1016 stats_tpl_id2name(uint32_t tpl_id, char *buf, size_t len)
1023 if (tpl_id < ntpl) {
1024 if (buf != NULL && len > strlen(tpllist[tpl_id]->mb->tplname))
1025 strlcpy(buf, tpllist[tpl_id]->mb->tplname, len);
1036 stats_tpl_sample_rollthedice(struct stats_tpl_sample_rate *rates, int nrates,
1037 void *seed_bytes, size_t seed_len)
1039 uint32_t cum_pct, rnd_pct;
1045 * Choose a pseudorandom or seeded number in range [0,100] and use
1046 * it to make a sampling decision and template selection where required.
1047 * If no seed is supplied, a PRNG is used to generate a pseudorandom
1048 * number so that every selection is independent. If a seed is supplied,
1049 * the caller desires random selection across different seeds, but
1050 * deterministic selection given the same seed. This is achieved by
1051 * hashing the seed and using the hash as the random number source.
1053 * XXXLAS: Characterise hash function output distribution.
1055 if (seed_bytes == NULL)
1056 rnd_pct = random() / (INT32_MAX / 100);
1058 rnd_pct = hash32_buf(seed_bytes, seed_len, 0) /
1059 (UINT32_MAX / 100U);
1062 * We map the randomly selected percentage on to the interval [0,100]
1063 * consisting of the cumulatively summed template sampling percentages.
1064 * The difference between the cumulative sum of all template sampling
1065 * percentages and 100 is treated as a NULL assignment i.e. no stats
1066 * template will be assigned, and -1 returned instead.
1068 for (i = 0; i < nrates; i++) {
1069 cum_pct += rates[i].tpl_sample_pct;
1071 KASSERT(cum_pct <= 100, ("%s cum_pct %u > 100", __func__,
1073 if (rnd_pct > cum_pct || rates[i].tpl_sample_pct == 0)
1076 return (rates[i].tpl_slot_id);
1083 stats_v1_blob_clone(struct statsblobv1 **dst, size_t dstmaxsz,
1084 struct statsblobv1 *src, uint32_t flags)
1090 if (src == NULL || dst == NULL ||
1091 src->cursz < sizeof(struct statsblob) ||
1092 ((flags & SB_CLONE_ALLOCDST) &&
1093 (flags & (SB_CLONE_USRDSTNOFAULT | SB_CLONE_USRDST)))) {
1095 } else if (flags & SB_CLONE_ALLOCDST) {
1096 *dst = stats_realloc(NULL, 0, src->cursz, 0);
1098 (*dst)->maxsz = dstmaxsz = src->cursz;
1101 } else if (*dst == NULL || dstmaxsz < sizeof(struct statsblob)) {
1106 size_t postcurszlen;
1109 * Clone src into dst except for the maxsz field. If dst is too
1110 * small to hold all of src, only copy src's header and return
1114 if (flags & SB_CLONE_USRDSTNOFAULT)
1115 copyout_nofault(src, *dst,
1116 offsetof(struct statsblob, maxsz));
1117 else if (flags & SB_CLONE_USRDST)
1118 copyout(src, *dst, offsetof(struct statsblob, maxsz));
1121 memcpy(*dst, src, offsetof(struct statsblob, maxsz));
1123 if (dstmaxsz >= src->cursz) {
1124 postcurszlen = src->cursz -
1125 offsetof(struct statsblob, cursz);
1128 postcurszlen = sizeof(struct statsblob) -
1129 offsetof(struct statsblob, cursz);
1132 if (flags & SB_CLONE_USRDSTNOFAULT)
1133 copyout_nofault(&(src->cursz), &((*dst)->cursz),
1135 else if (flags & SB_CLONE_USRDST)
1136 copyout(&(src->cursz), &((*dst)->cursz), postcurszlen);
1139 memcpy(&((*dst)->cursz), &(src->cursz), postcurszlen);
1146 stats_v1_tpl_alloc(const char *name, uint32_t flags __unused)
1148 struct statsblobv1_tpl *tpl, **newtpllist;
1149 struct statsblobv1 *tpl_sb;
1150 struct metablob *tpl_mb;
1153 if (name != NULL && strlen(name) > TPL_MAX_NAME_LEN)
1156 if (name != NULL && stats_tpl_fetch_allocid(name, 0) >= 0)
1159 tpl = stats_realloc(NULL, 0, sizeof(struct statsblobv1_tpl), M_ZERO);
1160 tpl_mb = stats_realloc(NULL, 0, sizeof(struct metablob), M_ZERO);
1161 tpl_sb = stats_realloc(NULL, 0, sizeof(struct statsblobv1), M_ZERO);
1163 if (tpl_mb != NULL && name != NULL)
1164 tpl_mb->tplname = stats_strdup(name, 0);
1166 if (tpl == NULL || tpl_sb == NULL || tpl_mb == NULL ||
1167 tpl_mb->tplname == NULL) {
1170 if (tpl_mb != NULL) {
1171 stats_free(tpl_mb->tplname);
1180 tpl_sb->abi = STATS_ABI_V1;
1182 #if BYTE_ORDER == LITTLE_ENDIAN
1184 #elif BYTE_ORDER == BIG_ENDIAN
1189 tpl_sb->cursz = tpl_sb->maxsz = sizeof(struct statsblobv1);
1190 tpl_sb->stats_off = tpl_sb->statsdata_off = sizeof(struct statsblobv1);
1193 newtpllist = stats_realloc(tpllist, ntpl * sizeof(void *),
1194 (ntpl + 1) * sizeof(void *), 0);
1195 if (newtpllist != NULL) {
1197 tpllist = (struct statsblob_tpl **)newtpllist;
1198 tpllist[tpl_id] = (struct statsblob_tpl *)tpl;
1199 stats_tpl_update_hash(tpllist[tpl_id]);
1203 if (tpl_mb != NULL) {
1204 stats_free(tpl_mb->tplname);
1215 stats_v1_tpl_add_voistats(uint32_t tpl_id, int32_t voi_id, const char *voi_name,
1216 enum vsd_dtype voi_dtype, uint32_t nvss, struct voistatspec *vss,
1220 struct voistat *tmpstat;
1221 struct statsblobv1 *tpl_sb;
1222 struct metablob *tpl_mb;
1223 int error, i, newstatdataidx, newvoibytes, newvoistatbytes,
1224 newvoistatdatabytes, newvoistatmaxid;
1227 if (voi_id < 0 || voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES ||
1228 nvss == 0 || vss == NULL)
1231 error = nbytes = newvoibytes = newvoistatbytes =
1232 newvoistatdatabytes = 0;
1233 newvoistatmaxid = -1;
1235 /* Calculate the number of bytes required for the new voistats. */
1236 for (i = nvss - 1; i >= 0; i--) {
1237 if (vss[i].stype == 0 || vss[i].stype >= VS_NUM_STYPES ||
1238 vss[i].vs_dtype == 0 || vss[i].vs_dtype >= VSD_NUM_DTYPES ||
1239 vss[i].iv == NULL || vss[i].vsdsz == 0)
1241 if ((int)vss[i].stype > newvoistatmaxid)
1242 newvoistatmaxid = vss[i].stype;
1243 newvoistatdatabytes += vss[i].vsdsz;
1246 if (flags & SB_VOI_RELUPDATE) {
1247 /* XXXLAS: VOI state bytes may need to vary based on stat types. */
1248 newvoistatdatabytes += sizeof(struct voistatdata_voistate);
1250 nbytes += newvoistatdatabytes;
1253 if (tpl_id < ntpl) {
1254 tpl_sb = (struct statsblobv1 *)tpllist[tpl_id]->sb;
1255 tpl_mb = tpllist[tpl_id]->mb;
1257 if (voi_id >= NVOIS(tpl_sb) || tpl_sb->vois[voi_id].id == -1) {
1258 /* Adding a new VOI and associated stats. */
1259 if (voi_id >= NVOIS(tpl_sb)) {
1260 /* We need to grow the tpl_sb->vois array. */
1261 newvoibytes = (voi_id - (NVOIS(tpl_sb) - 1)) *
1263 nbytes += newvoibytes;
1266 (newvoistatmaxid + 1) * sizeof(struct voistat);
1268 /* Adding stats to an existing VOI. */
1269 if (newvoistatmaxid >
1270 tpl_sb->vois[voi_id].voistatmaxid) {
1271 newvoistatbytes = (newvoistatmaxid -
1272 tpl_sb->vois[voi_id].voistatmaxid) *
1273 sizeof(struct voistat);
1275 /* XXXLAS: KPI does not yet support expanding VOIs. */
1278 nbytes += newvoistatbytes;
1280 if (!error && newvoibytes > 0) {
1281 struct voi_meta *voi_meta = tpl_mb->voi_meta;
1283 voi_meta = stats_realloc(voi_meta, voi_meta == NULL ?
1284 0 : NVOIS(tpl_sb) * sizeof(struct voi_meta),
1285 (1 + voi_id) * sizeof(struct voi_meta),
1288 if (voi_meta == NULL)
1291 tpl_mb->voi_meta = voi_meta;
1295 /* NB: Resizing can change where tpl_sb points. */
1296 error = stats_v1_blob_expand(&tpl_sb, newvoibytes,
1297 newvoistatbytes, newvoistatdatabytes);
1301 tpl_mb->voi_meta[voi_id].name = stats_strdup(voi_name,
1303 if (tpl_mb->voi_meta[voi_id].name == NULL)
1308 /* Update the template list with the resized pointer. */
1309 tpllist[tpl_id]->sb = (struct statsblob *)tpl_sb;
1311 /* Update the template. */
1312 voi = &tpl_sb->vois[voi_id];
1315 /* VOI is new and needs to be initialised. */
1317 voi->dtype = voi_dtype;
1318 voi->stats_off = tpl_sb->stats_off;
1319 if (flags & SB_VOI_RELUPDATE)
1320 voi->flags |= VOI_REQSTATE;
1323 * XXXLAS: When this else block is written, the
1324 * "KPI does not yet support expanding VOIs"
1325 * error earlier in this function can be
1326 * removed. What is required here is to shuffle
1327 * the voistat array such that the new stats for
1328 * the voi are contiguous, which will displace
1329 * stats for other vois that reside after the
1330 * voi being updated. The other vois then need
1331 * to have their stats_off adjusted post
1336 voi->voistatmaxid = newvoistatmaxid;
1339 if (voi->flags & VOI_REQSTATE) {
1340 /* Initialise the voistate stat in slot 0. */
1341 tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off);
1342 tmpstat->stype = VS_STYPE_VOISTATE;
1344 tmpstat->dtype = VSD_DTYPE_VOISTATE;
1345 newstatdataidx = tmpstat->dsz =
1346 sizeof(struct voistatdata_numeric);
1347 tmpstat->data_off = tpl_sb->statsdata_off;
1350 for (i = 0; (uint32_t)i < nvss; i++) {
1351 tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off +
1352 (vss[i].stype * sizeof(struct voistat)));
1353 KASSERT(tmpstat->stype < 0, ("voistat %p "
1354 "already initialised", tmpstat));
1355 tmpstat->stype = vss[i].stype;
1356 tmpstat->flags = vss[i].flags;
1357 tmpstat->dtype = vss[i].vs_dtype;
1358 tmpstat->dsz = vss[i].vsdsz;
1359 tmpstat->data_off = tpl_sb->statsdata_off +
1361 memcpy(BLOB_OFFSET(tpl_sb, tmpstat->data_off),
1362 vss[i].iv, vss[i].vsdsz);
1363 newstatdataidx += vss[i].vsdsz;
1366 /* Update the template version hash. */
1367 stats_tpl_update_hash(tpllist[tpl_id]);
1368 /* XXXLAS: Confirm tpl name/hash pair remains unique. */
1377 struct statsblobv1 *
1378 stats_v1_blob_alloc(uint32_t tpl_id, uint32_t flags __unused)
1380 struct statsblobv1 *sb;
1386 if (tpl_id < ntpl) {
1387 sb = stats_realloc(NULL, 0, tpllist[tpl_id]->sb->maxsz, 0);
1389 sb->maxsz = tpllist[tpl_id]->sb->maxsz;
1390 error = stats_v1_blob_init_locked(sb, tpl_id, 0);
1405 stats_v1_blob_destroy(struct statsblobv1 *sb)
1412 stats_v1_voistat_fetch_dptr(struct statsblobv1 *sb, int32_t voi_id,
1413 enum voi_stype stype, enum vsd_dtype *retdtype, struct voistatdata **retvsd,
1419 if (retvsd == NULL || sb == NULL || sb->abi != STATS_ABI_V1 ||
1420 voi_id >= NVOIS(sb))
1423 v = &sb->vois[voi_id];
1424 if ((__typeof(v->voistatmaxid))stype > v->voistatmaxid)
1427 vs = BLOB_OFFSET(sb, v->stats_off + (stype * sizeof(struct voistat)));
1428 *retvsd = BLOB_OFFSET(sb, vs->data_off);
1429 if (retdtype != NULL)
1430 *retdtype = vs->dtype;
1431 if (retvsdsz != NULL)
1432 *retvsdsz = vs->dsz;
1438 stats_v1_blob_init(struct statsblobv1 *sb, uint32_t tpl_id, uint32_t flags)
1445 if (sb == NULL || tpl_id >= ntpl) {
1448 error = stats_v1_blob_init_locked(sb, tpl_id, flags);
1456 stats_v1_blob_init_locked(struct statsblobv1 *sb, uint32_t tpl_id,
1457 uint32_t flags __unused)
1461 TPL_LIST_RLOCK_ASSERT();
1462 error = (sb->maxsz >= tpllist[tpl_id]->sb->cursz) ? 0 : EOVERFLOW;
1464 ("sb %d instead of %d bytes", sb->maxsz, tpllist[tpl_id]->sb->cursz));
1467 memcpy(sb, tpllist[tpl_id]->sb, tpllist[tpl_id]->sb->cursz);
1468 sb->created = sb->lastrst = stats_sbinuptime();
1469 sb->tplhash = tpllist[tpl_id]->mb->tplhash;
1476 stats_v1_blob_expand(struct statsblobv1 **sbpp, int newvoibytes,
1477 int newvoistatbytes, int newvoistatdatabytes)
1479 struct statsblobv1 *sb;
1481 struct voistat *tmpvoistat, *voistat_array;
1482 int error, i, idxnewvois, idxnewvoistats, nbytes, nvoistats;
1484 KASSERT(newvoibytes % sizeof(struct voi) == 0,
1485 ("Bad newvoibytes %d", newvoibytes));
1486 KASSERT(newvoistatbytes % sizeof(struct voistat) == 0,
1487 ("Bad newvoistatbytes %d", newvoistatbytes));
1489 error = ((newvoibytes % sizeof(struct voi) == 0) &&
1490 (newvoistatbytes % sizeof(struct voistat) == 0)) ? 0 : EINVAL;
1492 nbytes = newvoibytes + newvoistatbytes + newvoistatdatabytes;
1495 * XXXLAS: Required until we gain support for flags which alter the
1496 * units of size/offset fields in key structs.
1498 if (!error && ((((int)sb->cursz) + nbytes) > SB_V1_MAXSZ))
1501 if (!error && (sb->cursz + nbytes > sb->maxsz)) {
1502 /* Need to expand our blob. */
1503 sb = stats_realloc(sb, sb->maxsz, sb->cursz + nbytes, M_ZERO);
1505 sb->maxsz = sb->cursz + nbytes;
1513 * Shuffle memory within the expanded blob working from the end
1514 * backwards, leaving gaps for the new voistat and voistatdata
1515 * structs at the beginning of their respective blob regions,
1516 * and for the new voi structs at the end of their blob region.
1518 memmove(BLOB_OFFSET(sb, sb->statsdata_off + nbytes),
1519 BLOB_OFFSET(sb, sb->statsdata_off),
1520 sb->cursz - sb->statsdata_off);
1521 memmove(BLOB_OFFSET(sb, sb->stats_off + newvoibytes +
1522 newvoistatbytes), BLOB_OFFSET(sb, sb->stats_off),
1523 sb->statsdata_off - sb->stats_off);
1525 /* First index of new voi/voistat structs to be initialised. */
1526 idxnewvois = NVOIS(sb);
1527 idxnewvoistats = (newvoistatbytes / sizeof(struct voistat)) - 1;
1529 /* Update housekeeping variables and offsets. */
1530 sb->cursz += nbytes;
1531 sb->stats_off += newvoibytes;
1532 sb->statsdata_off += newvoibytes + newvoistatbytes;
1534 /* XXXLAS: Zeroing not strictly needed but aids debugging. */
1535 memset(&sb->vois[idxnewvois], '\0', newvoibytes);
1536 memset(BLOB_OFFSET(sb, sb->stats_off), '\0',
1538 memset(BLOB_OFFSET(sb, sb->statsdata_off), '\0',
1539 newvoistatdatabytes);
1541 /* Initialise new voi array members and update offsets. */
1542 for (i = 0; i < NVOIS(sb); i++) {
1543 tmpvoi = &sb->vois[i];
1544 if (i >= idxnewvois) {
1545 tmpvoi->id = tmpvoi->voistatmaxid = -1;
1546 } else if (tmpvoi->id > -1) {
1547 tmpvoi->stats_off += newvoibytes +
1552 /* Initialise new voistat array members and update offsets. */
1553 nvoistats = (sb->statsdata_off - sb->stats_off) /
1554 sizeof(struct voistat);
1555 voistat_array = BLOB_OFFSET(sb, sb->stats_off);
1556 for (i = 0; i < nvoistats; i++) {
1557 tmpvoistat = &voistat_array[i];
1558 if (i <= idxnewvoistats) {
1559 tmpvoistat->stype = -1;
1560 } else if (tmpvoistat->stype > -1) {
1561 tmpvoistat->data_off += nbytes;
1570 stats_v1_blob_finalise(struct statsblobv1 *sb __unused)
1573 /* XXXLAS: Fill this in. */
1577 stats_v1_blob_iter(struct statsblobv1 *sb, stats_v1_blob_itercb_t icb,
1578 void *usrctx, uint32_t flags)
1582 struct sb_iter_ctx ctx;
1585 ctx.usrctx = usrctx;
1586 ctx.flags = SB_IT_FIRST_CB;
1589 for (i = 0; i < NVOIS(sb); i++) {
1593 ctx.flags |= SB_IT_FIRST_VOISTAT;
1596 ctx.flags |= SB_IT_FIRST_VOI;
1597 else if (i == (NVOIS(sb) - 1))
1598 ctx.flags |= SB_IT_LAST_VOI | SB_IT_LAST_CB;
1600 if (v->id < 0 && (flags & SB_IT_NULLVOI)) {
1601 if (icb(sb, v, NULL, &ctx))
1604 ctx.flags &= ~SB_IT_FIRST_CB;
1607 /* If NULL voi, v->voistatmaxid == -1 */
1608 for (j = 0; j <= v->voistatmaxid; j++) {
1609 vs = &((struct voistat *)BLOB_OFFSET(sb,
1611 if (vs->stype < 0 &&
1612 !(flags & SB_IT_NULLVOISTAT))
1615 if (j == v->voistatmaxid) {
1616 ctx.flags |= SB_IT_LAST_VOISTAT;
1617 if (i == (NVOIS(sb) - 1))
1621 ctx.flags &= ~SB_IT_LAST_CB;
1624 if (icb(sb, v, vs, &ctx))
1627 ctx.flags &= ~(SB_IT_FIRST_CB | SB_IT_FIRST_VOISTAT |
1628 SB_IT_LAST_VOISTAT);
1630 ctx.flags &= ~(SB_IT_FIRST_VOI | SB_IT_LAST_VOI);
1635 stats_voistatdata_tdgst_tostr(enum vsd_dtype voi_dtype __unused,
1636 const struct voistatdata_tdgst *tdgst, enum vsd_dtype tdgst_dtype,
1637 size_t tdgst_dsz __unused, enum sb_str_fmt fmt, struct sbuf *buf, int objdump)
1639 const struct ctdth32 *ctd32tree;
1640 const struct ctdth64 *ctd64tree;
1641 const struct voistatdata_tdgstctd32 *ctd32;
1642 const struct voistatdata_tdgstctd64 *ctd64;
1644 uint64_t smplcnt, compcnt;
1645 int is32bit, qmaxstrlen;
1646 uint16_t maxctds, curctds;
1648 switch (tdgst_dtype) {
1649 case VSD_DTYPE_TDGSTCLUST32:
1650 smplcnt = CONSTVSD(tdgstclust32, tdgst)->smplcnt;
1651 compcnt = CONSTVSD(tdgstclust32, tdgst)->compcnt;
1652 maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree);
1653 curctds = ARB_CURNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree);
1654 ctd32tree = &CONSTVSD(tdgstclust32, tdgst)->ctdtree;
1655 ctd32 = (objdump ? ARB_CNODE(ctd32tree, 0) :
1656 ARB_CMIN(ctdth32, ctd32tree));
1657 qmaxstrlen = (ctd32 == NULL) ? 1 : Q_MAXSTRLEN(ctd32->mu, 10);
1662 case VSD_DTYPE_TDGSTCLUST64:
1663 smplcnt = CONSTVSD(tdgstclust64, tdgst)->smplcnt;
1664 compcnt = CONSTVSD(tdgstclust64, tdgst)->compcnt;
1665 maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree);
1666 curctds = ARB_CURNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree);
1667 ctd64tree = &CONSTVSD(tdgstclust64, tdgst)->ctdtree;
1668 ctd64 = (objdump ? ARB_CNODE(ctd64tree, 0) :
1669 ARB_CMIN(ctdth64, ctd64tree));
1670 qmaxstrlen = (ctd64 == NULL) ? 1 : Q_MAXSTRLEN(ctd64->mu, 10);
1680 case SB_STRFMT_FREEFORM:
1681 fmtstr = "smplcnt=%ju, compcnt=%ju, maxctds=%hu, nctds=%hu";
1683 case SB_STRFMT_JSON:
1686 "\"smplcnt\":%ju,\"compcnt\":%ju,\"maxctds\":%hu,"
1687 "\"nctds\":%hu,\"ctds\":[";
1690 sbuf_printf(buf, fmtstr, (uintmax_t)smplcnt, (uintmax_t)compcnt,
1693 while ((is32bit ? NULL != ctd32 : NULL != ctd64)) {
1694 char qstr[qmaxstrlen];
1697 case SB_STRFMT_FREEFORM:
1698 fmtstr = "\n\t\t\t\t";
1700 case SB_STRFMT_JSON:
1705 sbuf_cat(buf, fmtstr);
1709 case SB_STRFMT_FREEFORM:
1710 fmtstr = "ctd[%hu].";
1712 case SB_STRFMT_JSON:
1714 fmtstr = "\"ctd\":%hu,";
1717 sbuf_printf(buf, fmtstr, is32bit ?
1718 ARB_SELFIDX(ctd32tree, ctd32) :
1719 ARB_SELFIDX(ctd64tree, ctd64));
1723 case SB_STRFMT_FREEFORM:
1726 case SB_STRFMT_JSON:
1731 sbuf_cat(buf, fmtstr);
1732 Q_TOSTR((is32bit ? ctd32->mu : ctd64->mu), -1, 10, qstr,
1734 sbuf_cat(buf, qstr);
1737 case SB_STRFMT_FREEFORM:
1738 fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}";
1740 case SB_STRFMT_JSON:
1742 fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}";
1745 sbuf_printf(buf, fmtstr,
1746 is32bit ? ctd32->cnt : (uintmax_t)ctd64->cnt);
1749 ctd32 = (objdump ? ARB_CNODE(ctd32tree,
1750 ARB_SELFIDX(ctd32tree, ctd32) + 1) :
1751 ARB_CNEXT(ctdth32, ctd32tree, ctd32));
1753 ctd64 = (objdump ? ARB_CNODE(ctd64tree,
1754 ARB_SELFIDX(ctd64tree, ctd64) + 1) :
1755 ARB_CNEXT(ctdth64, ctd64tree, ctd64));
1757 if (fmt == SB_STRFMT_JSON &&
1758 (is32bit ? NULL != ctd32 : NULL != ctd64))
1759 sbuf_putc(buf, ',');
1761 if (fmt == SB_STRFMT_JSON)
1766 stats_voistatdata_hist_tostr(enum vsd_dtype voi_dtype,
1767 const struct voistatdata_hist *hist, enum vsd_dtype hist_dtype,
1768 size_t hist_dsz, enum sb_str_fmt fmt, struct sbuf *buf, int objdump)
1770 const struct voistatdata_numeric *bkt_lb, *bkt_ub;
1775 switch (hist_dtype) {
1776 case VSD_DTYPE_CRHIST32:
1777 nbkts = HIST_VSDSZ2NBKTS(crhist32, hist_dsz);
1780 case VSD_DTYPE_DRHIST32:
1781 nbkts = HIST_VSDSZ2NBKTS(drhist32, hist_dsz);
1784 case VSD_DTYPE_DVHIST32:
1785 nbkts = HIST_VSDSZ2NBKTS(dvhist32, hist_dsz);
1788 case VSD_DTYPE_CRHIST64:
1789 nbkts = HIST_VSDSZ2NBKTS(crhist64, hist_dsz);
1792 case VSD_DTYPE_DRHIST64:
1793 nbkts = HIST_VSDSZ2NBKTS(drhist64, hist_dsz);
1796 case VSD_DTYPE_DVHIST64:
1797 nbkts = HIST_VSDSZ2NBKTS(dvhist64, hist_dsz);
1805 case SB_STRFMT_FREEFORM:
1806 fmtstr = "nbkts=%hu, ";
1808 case SB_STRFMT_JSON:
1810 fmtstr = "\"nbkts\":%hu,";
1813 sbuf_printf(buf, fmtstr, nbkts);
1816 case SB_STRFMT_FREEFORM:
1817 fmtstr = (is32bit ? "oob=%u" : "oob=%ju");
1819 case SB_STRFMT_JSON:
1821 fmtstr = (is32bit ? "\"oob\":%u,\"bkts\":[" :
1822 "\"oob\":%ju,\"bkts\":[");
1825 sbuf_printf(buf, fmtstr, is32bit ? VSD_CONSTHIST_FIELDVAL(hist,
1826 hist_dtype, oob) : (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist,
1829 for (i = 0; i < nbkts; i++) {
1830 switch (hist_dtype) {
1831 case VSD_DTYPE_CRHIST32:
1832 case VSD_DTYPE_CRHIST64:
1833 bkt_lb = VSD_CONSTCRHIST_FIELDPTR(hist, hist_dtype,
1836 bkt_ub = VSD_CONSTCRHIST_FIELDPTR(hist,
1837 hist_dtype, bkts[i + 1].lb);
1839 bkt_ub = &numeric_limits[LIM_MAX][voi_dtype];
1841 case VSD_DTYPE_DRHIST32:
1842 case VSD_DTYPE_DRHIST64:
1843 bkt_lb = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype,
1845 bkt_ub = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype,
1848 case VSD_DTYPE_DVHIST32:
1849 case VSD_DTYPE_DVHIST64:
1850 bkt_lb = bkt_ub = VSD_CONSTDVHIST_FIELDPTR(hist,
1851 hist_dtype, bkts[i].val);
1858 case SB_STRFMT_FREEFORM:
1859 fmtstr = "\n\t\t\t\t";
1861 case SB_STRFMT_JSON:
1866 sbuf_cat(buf, fmtstr);
1870 case SB_STRFMT_FREEFORM:
1871 fmtstr = "bkt[%hu].";
1873 case SB_STRFMT_JSON:
1875 fmtstr = "\"bkt\":%hu,";
1878 sbuf_printf(buf, fmtstr, i);
1882 case SB_STRFMT_FREEFORM:
1885 case SB_STRFMT_JSON:
1890 sbuf_cat(buf, fmtstr);
1891 stats_voistatdata_tostr((const struct voistatdata *)bkt_lb,
1892 voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric),
1896 case SB_STRFMT_FREEFORM:
1899 case SB_STRFMT_JSON:
1901 fmtstr = ",\"ub\":";
1904 sbuf_cat(buf, fmtstr);
1905 stats_voistatdata_tostr((const struct voistatdata *)bkt_ub,
1906 voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric),
1910 case SB_STRFMT_FREEFORM:
1911 fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}";
1913 case SB_STRFMT_JSON:
1915 fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}";
1918 sbuf_printf(buf, fmtstr, is32bit ?
1919 VSD_CONSTHIST_FIELDVAL(hist, hist_dtype, bkts[i].cnt) :
1920 (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist, hist_dtype,
1923 if (fmt == SB_STRFMT_JSON && i < nbkts - 1)
1924 sbuf_putc(buf, ',');
1926 if (fmt == SB_STRFMT_JSON)
1931 stats_voistatdata_tostr(const struct voistatdata *vsd, enum vsd_dtype voi_dtype,
1932 enum vsd_dtype vsd_dtype, size_t vsd_sz, enum sb_str_fmt fmt,
1933 struct sbuf *buf, int objdump)
1937 if (vsd == NULL || buf == NULL || voi_dtype >= VSD_NUM_DTYPES ||
1938 vsd_dtype >= VSD_NUM_DTYPES || fmt >= SB_STRFMT_NUM_FMTS)
1941 switch (vsd_dtype) {
1942 case VSD_DTYPE_VOISTATE:
1944 case SB_STRFMT_FREEFORM:
1947 case SB_STRFMT_JSON:
1949 fmtstr = "\"prev\":";
1952 sbuf_cat(buf, fmtstr);
1954 * Render prev by passing it as *vsd and voi_dtype as vsd_dtype.
1956 stats_voistatdata_tostr(
1957 (const struct voistatdata *)&CONSTVSD(voistate, vsd)->prev,
1958 voi_dtype, voi_dtype, vsd_sz, fmt, buf, objdump);
1960 case VSD_DTYPE_INT_S32:
1961 sbuf_printf(buf, "%d", vsd->int32.s32);
1963 case VSD_DTYPE_INT_U32:
1964 sbuf_printf(buf, "%u", vsd->int32.u32);
1966 case VSD_DTYPE_INT_S64:
1967 sbuf_printf(buf, "%jd", (intmax_t)vsd->int64.s64);
1969 case VSD_DTYPE_INT_U64:
1970 sbuf_printf(buf, "%ju", (uintmax_t)vsd->int64.u64);
1972 case VSD_DTYPE_INT_SLONG:
1973 sbuf_printf(buf, "%ld", vsd->intlong.slong);
1975 case VSD_DTYPE_INT_ULONG:
1976 sbuf_printf(buf, "%lu", vsd->intlong.ulong);
1978 case VSD_DTYPE_Q_S32:
1980 char qstr[Q_MAXSTRLEN(vsd->q32.sq32, 10)];
1981 Q_TOSTR((s32q_t)vsd->q32.sq32, -1, 10, qstr, sizeof(qstr));
1982 sbuf_cat(buf, qstr);
1985 case VSD_DTYPE_Q_U32:
1987 char qstr[Q_MAXSTRLEN(vsd->q32.uq32, 10)];
1988 Q_TOSTR((u32q_t)vsd->q32.uq32, -1, 10, qstr, sizeof(qstr));
1989 sbuf_cat(buf, qstr);
1992 case VSD_DTYPE_Q_S64:
1994 char qstr[Q_MAXSTRLEN(vsd->q64.sq64, 10)];
1995 Q_TOSTR((s64q_t)vsd->q64.sq64, -1, 10, qstr, sizeof(qstr));
1996 sbuf_cat(buf, qstr);
1999 case VSD_DTYPE_Q_U64:
2001 char qstr[Q_MAXSTRLEN(vsd->q64.uq64, 10)];
2002 Q_TOSTR((u64q_t)vsd->q64.uq64, -1, 10, qstr, sizeof(qstr));
2003 sbuf_cat(buf, qstr);
2006 case VSD_DTYPE_CRHIST32:
2007 case VSD_DTYPE_DRHIST32:
2008 case VSD_DTYPE_DVHIST32:
2009 case VSD_DTYPE_CRHIST64:
2010 case VSD_DTYPE_DRHIST64:
2011 case VSD_DTYPE_DVHIST64:
2012 stats_voistatdata_hist_tostr(voi_dtype, CONSTVSD(hist, vsd),
2013 vsd_dtype, vsd_sz, fmt, buf, objdump);
2015 case VSD_DTYPE_TDGSTCLUST32:
2016 case VSD_DTYPE_TDGSTCLUST64:
2017 stats_voistatdata_tdgst_tostr(voi_dtype,
2018 CONSTVSD(tdgst, vsd), vsd_dtype, vsd_sz, fmt, buf,
2025 return (sbuf_error(buf));
2029 stats_v1_itercb_tostr_freeform(struct statsblobv1 *sb, struct voi *v,
2030 struct voistat *vs, struct sb_iter_ctx *ctx)
2032 struct sb_tostrcb_ctx *sctx;
2033 struct metablob *tpl_mb;
2040 tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL;
2041 dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0);
2043 if (ctx->flags & SB_IT_FIRST_CB) {
2044 sbuf_printf(buf, "struct statsblobv1@%p", sb);
2046 sbuf_printf(buf, ", abi=%hhu, endian=%hhu, maxsz=%hu, "
2047 "cursz=%hu, created=%jd, lastrst=%jd, flags=0x%04hx, "
2048 "stats_off=%hu, statsdata_off=%hu",
2049 sb->abi, sb->endian, sb->maxsz, sb->cursz,
2050 sb->created, sb->lastrst, sb->flags, sb->stats_off,
2053 sbuf_printf(buf, ", tplhash=%u", sb->tplhash);
2056 if (ctx->flags & SB_IT_FIRST_VOISTAT) {
2057 sbuf_printf(buf, "\n\tvois[%hd]: id=%hd", ctx->vslot, v->id);
2060 sbuf_printf(buf, ", name=\"%s\"", (tpl_mb == NULL) ? "" :
2061 tpl_mb->voi_meta[v->id].name);
2063 sbuf_printf(buf, ", flags=0x%04hx, dtype=%s, "
2064 "voistatmaxid=%hhd, stats_off=%hu", v->flags,
2065 vsd_dtype2name[v->dtype], v->voistatmaxid, v->stats_off);
2068 if (!dump && vs->stype <= 0)
2071 sbuf_printf(buf, "\n\t\tvois[%hd]stat[%hhd]: stype=", v->id, ctx->vsslot);
2072 if (vs->stype < 0) {
2073 sbuf_printf(buf, "%hhd", vs->stype);
2076 sbuf_printf(buf, "%s, errs=%hu", vs_stype2name[vs->stype],
2078 vsd = BLOB_OFFSET(sb, vs->data_off);
2080 sbuf_printf(buf, ", flags=0x%04x, dtype=%s, dsz=%hu, "
2081 "data_off=%hu", vs->flags, vsd_dtype2name[vs->dtype],
2082 vs->dsz, vs->data_off);
2084 sbuf_printf(buf, "\n\t\t\tvoistatdata: ");
2085 stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz,
2086 sctx->fmt, buf, dump);
2090 stats_v1_itercb_tostr_json(struct statsblobv1 *sb, struct voi *v, struct voistat *vs,
2091 struct sb_iter_ctx *ctx)
2093 struct sb_tostrcb_ctx *sctx;
2094 struct metablob *tpl_mb;
2102 tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL;
2103 dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0);
2105 if (ctx->flags & SB_IT_FIRST_CB) {
2106 sbuf_putc(buf, '{');
2108 sbuf_printf(buf, "\"abi\":%hhu,\"endian\":%hhu,"
2109 "\"maxsz\":%hu,\"cursz\":%hu,\"created\":%jd,"
2110 "\"lastrst\":%jd,\"flags\":%hu,\"stats_off\":%hu,"
2111 "\"statsdata_off\":%hu,", sb->abi,
2112 sb->endian, sb->maxsz, sb->cursz, sb->created,
2113 sb->lastrst, sb->flags, sb->stats_off,
2118 fmtstr = "\"tplname\":%s,\"tplhash\":%u,\"vois\":{";
2120 fmtstr = "\"tplname\":\"%s\",\"tplhash\":%u,\"vois\":{";
2122 sbuf_printf(buf, fmtstr, tpl_mb ? tpl_mb->tplname : "null",
2126 if (ctx->flags & SB_IT_FIRST_VOISTAT) {
2128 sbuf_printf(buf, "\"[%d]\":{\"id\":%d", ctx->vslot,
2131 sbuf_printf(buf, "},");
2136 fmtstr = ",\"name\":%s,\"flags\":%hu,"
2137 "\"dtype\":\"%s\",\"voistatmaxid\":%hhd,"
2138 "\"stats_off\":%hu,";
2140 fmtstr = ",\"name\":\"%s\",\"flags\":%hu,"
2141 "\"dtype\":\"%s\",\"voistatmaxid\":%hhd,"
2142 "\"stats_off\":%hu,";
2144 sbuf_printf(buf, fmtstr, tpl_mb ?
2145 tpl_mb->voi_meta[v->id].name : "null", v->flags,
2146 vsd_dtype2name[v->dtype], v->voistatmaxid,
2149 if (tpl_mb == NULL) {
2150 sbuf_printf(buf, "\"[%hd]\":{", v->id);
2152 sbuf_printf(buf, "\"%s\":{",
2153 tpl_mb->voi_meta[v->id].name);
2156 sbuf_cat(buf, "\"stats\":{");
2159 vsd = BLOB_OFFSET(sb, vs->data_off);
2161 sbuf_printf(buf, "\"[%hhd]\":", ctx->vsslot);
2162 if (vs->stype < 0) {
2163 sbuf_printf(buf, "{\"stype\":-1},");
2166 sbuf_printf(buf, "{\"stype\":\"%s\",\"errs\":%hu,\"flags\":%hu,"
2167 "\"dtype\":\"%s\",\"data_off\":%hu,\"voistatdata\":{",
2168 vs_stype2name[vs->stype], vs->errs, vs->flags,
2169 vsd_dtype2name[vs->dtype], vs->data_off);
2170 } else if (vs->stype > 0) {
2172 sbuf_printf(buf, "\"[%hhd]\":", vs->stype);
2174 sbuf_printf(buf, "\"%s\":", vs_stype2name[vs->stype]);
2178 if ((vs->flags & VS_VSDVALID) || dump) {
2180 sbuf_printf(buf, "{\"errs\":%hu,", vs->errs);
2181 /* Simple non-compound VSD types need a key. */
2182 if (!vsd_compoundtype[vs->dtype])
2183 sbuf_cat(buf, "\"val\":");
2184 stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz,
2185 sctx->fmt, buf, dump);
2186 sbuf_cat(buf, dump ? "}}" : "}");
2188 sbuf_cat(buf, dump ? "null}" : "null");
2190 if (ctx->flags & SB_IT_LAST_VOISTAT)
2191 sbuf_cat(buf, "}}");
2193 if (ctx->flags & SB_IT_LAST_CB)
2194 sbuf_cat(buf, "}}");
2196 sbuf_putc(buf, ',');
2200 stats_v1_itercb_tostr(struct statsblobv1 *sb, struct voi *v, struct voistat *vs,
2201 struct sb_iter_ctx *ctx)
2203 struct sb_tostrcb_ctx *sctx;
2207 switch (sctx->fmt) {
2208 case SB_STRFMT_FREEFORM:
2209 stats_v1_itercb_tostr_freeform(sb, v, vs, ctx);
2211 case SB_STRFMT_JSON:
2212 stats_v1_itercb_tostr_json(sb, v, vs, ctx);
2218 return (sbuf_error(sctx->buf));
2222 stats_v1_blob_tostr(struct statsblobv1 *sb, struct sbuf *buf,
2223 enum sb_str_fmt fmt, uint32_t flags)
2225 struct sb_tostrcb_ctx sctx;
2228 if (sb == NULL || sb->abi != STATS_ABI_V1 || buf == NULL ||
2229 fmt >= SB_STRFMT_NUM_FMTS)
2236 if (flags & SB_TOSTR_META) {
2237 if (stats_tpl_fetch(stats_tpl_fetch_allocid(NULL, sb->tplhash),
2244 if (flags & SB_TOSTR_OBJDUMP)
2245 iflags |= (SB_IT_NULLVOI | SB_IT_NULLVOISTAT);
2246 stats_v1_blob_iter(sb, stats_v1_itercb_tostr, &sctx, iflags);
2248 return (sbuf_error(buf));
2252 stats_v1_itercb_visit(struct statsblobv1 *sb, struct voi *v,
2253 struct voistat *vs, struct sb_iter_ctx *ctx)
2255 struct sb_visitcb_ctx *vctx;
2256 struct sb_visit sbv;
2260 sbv.tplhash = sb->tplhash;
2262 sbv.voi_dtype = v->dtype;
2263 sbv.vs_stype = vs->stype;
2264 sbv.vs_dtype = vs->dtype;
2265 sbv.vs_dsz = vs->dsz;
2266 sbv.vs_data = BLOB_OFFSET(sb, vs->data_off);
2267 sbv.vs_errs = vs->errs;
2268 sbv.flags = ctx->flags & (SB_IT_FIRST_CB | SB_IT_LAST_CB |
2269 SB_IT_FIRST_VOI | SB_IT_LAST_VOI | SB_IT_FIRST_VOISTAT |
2270 SB_IT_LAST_VOISTAT);
2272 return (vctx->cb(&sbv, vctx->usrctx));
2276 stats_v1_blob_visit(struct statsblobv1 *sb, stats_blob_visitcb_t func,
2279 struct sb_visitcb_ctx vctx;
2281 if (sb == NULL || sb->abi != STATS_ABI_V1 || func == NULL)
2285 vctx.usrctx = usrctx;
2287 stats_v1_blob_iter(sb, stats_v1_itercb_visit, &vctx, 0);
2293 stats_v1_icb_reset_voistat(struct statsblobv1 *sb, struct voi *v __unused,
2294 struct voistat *vs, struct sb_iter_ctx *ctx __unused)
2298 if (vs->stype == VS_STYPE_VOISTATE)
2301 vsd = BLOB_OFFSET(sb, vs->data_off);
2303 /* Perform the stat type's default reset action. */
2304 switch (vs->stype) {
2306 switch (vs->dtype) {
2307 case VSD_DTYPE_Q_S32:
2308 Q_SIFVAL(VSD(q32, vsd)->sq32, 0);
2310 case VSD_DTYPE_Q_U32:
2311 Q_SIFVAL(VSD(q32, vsd)->uq32, 0);
2313 case VSD_DTYPE_Q_S64:
2314 Q_SIFVAL(VSD(q64, vsd)->sq64, 0);
2316 case VSD_DTYPE_Q_U64:
2317 Q_SIFVAL(VSD(q64, vsd)->uq64, 0);
2320 bzero(vsd, vs->dsz);
2325 switch (vs->dtype) {
2326 case VSD_DTYPE_Q_S32:
2327 Q_SIFVAL(VSD(q32, vsd)->sq32,
2328 Q_IFMINVAL(VSD(q32, vsd)->sq32));
2330 case VSD_DTYPE_Q_U32:
2331 Q_SIFVAL(VSD(q32, vsd)->uq32,
2332 Q_IFMINVAL(VSD(q32, vsd)->uq32));
2334 case VSD_DTYPE_Q_S64:
2335 Q_SIFVAL(VSD(q64, vsd)->sq64,
2336 Q_IFMINVAL(VSD(q64, vsd)->sq64));
2338 case VSD_DTYPE_Q_U64:
2339 Q_SIFVAL(VSD(q64, vsd)->uq64,
2340 Q_IFMINVAL(VSD(q64, vsd)->uq64));
2343 memcpy(vsd, &numeric_limits[LIM_MIN][vs->dtype],
2349 switch (vs->dtype) {
2350 case VSD_DTYPE_Q_S32:
2351 Q_SIFVAL(VSD(q32, vsd)->sq32,
2352 Q_IFMAXVAL(VSD(q32, vsd)->sq32));
2354 case VSD_DTYPE_Q_U32:
2355 Q_SIFVAL(VSD(q32, vsd)->uq32,
2356 Q_IFMAXVAL(VSD(q32, vsd)->uq32));
2358 case VSD_DTYPE_Q_S64:
2359 Q_SIFVAL(VSD(q64, vsd)->sq64,
2360 Q_IFMAXVAL(VSD(q64, vsd)->sq64));
2362 case VSD_DTYPE_Q_U64:
2363 Q_SIFVAL(VSD(q64, vsd)->uq64,
2364 Q_IFMAXVAL(VSD(q64, vsd)->uq64));
2367 memcpy(vsd, &numeric_limits[LIM_MAX][vs->dtype],
2374 /* Reset bucket counts. */
2375 struct voistatdata_hist *hist;
2379 hist = VSD(hist, vsd);
2380 switch (vs->dtype) {
2381 case VSD_DTYPE_CRHIST32:
2382 nbkts = HIST_VSDSZ2NBKTS(crhist32, vs->dsz);
2385 case VSD_DTYPE_DRHIST32:
2386 nbkts = HIST_VSDSZ2NBKTS(drhist32, vs->dsz);
2389 case VSD_DTYPE_DVHIST32:
2390 nbkts = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz);
2393 case VSD_DTYPE_CRHIST64:
2394 nbkts = HIST_VSDSZ2NBKTS(crhist64, vs->dsz);
2397 case VSD_DTYPE_DRHIST64:
2398 nbkts = HIST_VSDSZ2NBKTS(drhist64, vs->dsz);
2401 case VSD_DTYPE_DVHIST64:
2402 nbkts = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz);
2409 bzero(VSD_HIST_FIELDPTR(hist, vs->dtype, oob),
2410 is32bit ? sizeof(uint32_t) : sizeof(uint64_t));
2411 for (i = nbkts - 1; i >= 0; i--) {
2412 bzero(VSD_HIST_FIELDPTR(hist, vs->dtype,
2413 bkts[i].cnt), is32bit ? sizeof(uint32_t) :
2418 case VS_STYPE_TDGST:
2420 /* Reset sample count centroids array/tree. */
2421 struct voistatdata_tdgst *tdgst;
2422 struct ctdth32 *ctd32tree;
2423 struct ctdth64 *ctd64tree;
2424 struct voistatdata_tdgstctd32 *ctd32;
2425 struct voistatdata_tdgstctd64 *ctd64;
2427 tdgst = VSD(tdgst, vsd);
2428 switch (vs->dtype) {
2429 case VSD_DTYPE_TDGSTCLUST32:
2430 VSD(tdgstclust32, tdgst)->smplcnt = 0;
2431 VSD(tdgstclust32, tdgst)->compcnt = 0;
2432 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
2433 ARB_INIT(ctd32, ctdlnk, ctd32tree,
2434 ARB_MAXNODES(ctd32tree)) {
2436 Q_SIFVAL(ctd32->mu, 0);
2439 RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree);
2442 case VSD_DTYPE_TDGSTCLUST64:
2443 VSD(tdgstclust64, tdgst)->smplcnt = 0;
2444 VSD(tdgstclust64, tdgst)->compcnt = 0;
2445 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
2446 ARB_INIT(ctd64, ctdlnk, ctd64tree,
2447 ARB_MAXNODES(ctd64tree)) {
2449 Q_SIFVAL(ctd64->mu, 0);
2452 RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree);
2461 KASSERT(0, ("Unknown VOI stat type %d", vs->stype));
2466 vs->flags &= ~VS_VSDVALID;
2472 stats_v1_blob_snapshot(struct statsblobv1 **dst, size_t dstmaxsz,
2473 struct statsblobv1 *src, uint32_t flags)
2477 if (src != NULL && src->abi == STATS_ABI_V1) {
2478 error = stats_v1_blob_clone(dst, dstmaxsz, src, flags);
2480 if (flags & SB_CLONE_RSTSRC) {
2481 stats_v1_blob_iter(src,
2482 stats_v1_icb_reset_voistat, NULL, 0);
2483 src->lastrst = stats_sbinuptime();
2485 stats_v1_blob_finalise(*dst);
2494 stats_v1_voi_update_max(enum vsd_dtype voi_dtype __unused,
2495 struct voistatdata *voival, struct voistat *vs, void *vsd)
2499 KASSERT(vs->dtype < VSD_NUM_DTYPES,
2500 ("Unknown VSD dtype %d", vs->dtype));
2504 switch (vs->dtype) {
2505 case VSD_DTYPE_INT_S32:
2506 if (VSD(int32, vsd)->s32 < voival->int32.s32) {
2507 VSD(int32, vsd)->s32 = voival->int32.s32;
2508 vs->flags |= VS_VSDVALID;
2511 case VSD_DTYPE_INT_U32:
2512 if (VSD(int32, vsd)->u32 < voival->int32.u32) {
2513 VSD(int32, vsd)->u32 = voival->int32.u32;
2514 vs->flags |= VS_VSDVALID;
2517 case VSD_DTYPE_INT_S64:
2518 if (VSD(int64, vsd)->s64 < voival->int64.s64) {
2519 VSD(int64, vsd)->s64 = voival->int64.s64;
2520 vs->flags |= VS_VSDVALID;
2523 case VSD_DTYPE_INT_U64:
2524 if (VSD(int64, vsd)->u64 < voival->int64.u64) {
2525 VSD(int64, vsd)->u64 = voival->int64.u64;
2526 vs->flags |= VS_VSDVALID;
2529 case VSD_DTYPE_INT_SLONG:
2530 if (VSD(intlong, vsd)->slong < voival->intlong.slong) {
2531 VSD(intlong, vsd)->slong = voival->intlong.slong;
2532 vs->flags |= VS_VSDVALID;
2535 case VSD_DTYPE_INT_ULONG:
2536 if (VSD(intlong, vsd)->ulong < voival->intlong.ulong) {
2537 VSD(intlong, vsd)->ulong = voival->intlong.ulong;
2538 vs->flags |= VS_VSDVALID;
2541 case VSD_DTYPE_Q_S32:
2542 if (Q_QLTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) &&
2543 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32,
2544 voival->q32.sq32)))) {
2545 vs->flags |= VS_VSDVALID;
2548 case VSD_DTYPE_Q_U32:
2549 if (Q_QLTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) &&
2550 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32,
2551 voival->q32.uq32)))) {
2552 vs->flags |= VS_VSDVALID;
2555 case VSD_DTYPE_Q_S64:
2556 if (Q_QLTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) &&
2557 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64,
2558 voival->q64.sq64)))) {
2559 vs->flags |= VS_VSDVALID;
2562 case VSD_DTYPE_Q_U64:
2563 if (Q_QLTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) &&
2564 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64,
2565 voival->q64.uq64)))) {
2566 vs->flags |= VS_VSDVALID;
2578 stats_v1_voi_update_min(enum vsd_dtype voi_dtype __unused,
2579 struct voistatdata *voival, struct voistat *vs, void *vsd)
2583 KASSERT(vs->dtype < VSD_NUM_DTYPES,
2584 ("Unknown VSD dtype %d", vs->dtype));
2588 switch (vs->dtype) {
2589 case VSD_DTYPE_INT_S32:
2590 if (VSD(int32, vsd)->s32 > voival->int32.s32) {
2591 VSD(int32, vsd)->s32 = voival->int32.s32;
2592 vs->flags |= VS_VSDVALID;
2595 case VSD_DTYPE_INT_U32:
2596 if (VSD(int32, vsd)->u32 > voival->int32.u32) {
2597 VSD(int32, vsd)->u32 = voival->int32.u32;
2598 vs->flags |= VS_VSDVALID;
2601 case VSD_DTYPE_INT_S64:
2602 if (VSD(int64, vsd)->s64 > voival->int64.s64) {
2603 VSD(int64, vsd)->s64 = voival->int64.s64;
2604 vs->flags |= VS_VSDVALID;
2607 case VSD_DTYPE_INT_U64:
2608 if (VSD(int64, vsd)->u64 > voival->int64.u64) {
2609 VSD(int64, vsd)->u64 = voival->int64.u64;
2610 vs->flags |= VS_VSDVALID;
2613 case VSD_DTYPE_INT_SLONG:
2614 if (VSD(intlong, vsd)->slong > voival->intlong.slong) {
2615 VSD(intlong, vsd)->slong = voival->intlong.slong;
2616 vs->flags |= VS_VSDVALID;
2619 case VSD_DTYPE_INT_ULONG:
2620 if (VSD(intlong, vsd)->ulong > voival->intlong.ulong) {
2621 VSD(intlong, vsd)->ulong = voival->intlong.ulong;
2622 vs->flags |= VS_VSDVALID;
2625 case VSD_DTYPE_Q_S32:
2626 if (Q_QGTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) &&
2627 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32,
2628 voival->q32.sq32)))) {
2629 vs->flags |= VS_VSDVALID;
2632 case VSD_DTYPE_Q_U32:
2633 if (Q_QGTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) &&
2634 (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32,
2635 voival->q32.uq32)))) {
2636 vs->flags |= VS_VSDVALID;
2639 case VSD_DTYPE_Q_S64:
2640 if (Q_QGTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) &&
2641 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64,
2642 voival->q64.sq64)))) {
2643 vs->flags |= VS_VSDVALID;
2646 case VSD_DTYPE_Q_U64:
2647 if (Q_QGTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) &&
2648 (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64,
2649 voival->q64.uq64)))) {
2650 vs->flags |= VS_VSDVALID;
2662 stats_v1_voi_update_sum(enum vsd_dtype voi_dtype __unused,
2663 struct voistatdata *voival, struct voistat *vs, void *vsd)
2667 KASSERT(vs->dtype < VSD_NUM_DTYPES,
2668 ("Unknown VSD dtype %d", vs->dtype));
2672 switch (vs->dtype) {
2673 case VSD_DTYPE_INT_S32:
2674 VSD(int32, vsd)->s32 += voival->int32.s32;
2676 case VSD_DTYPE_INT_U32:
2677 VSD(int32, vsd)->u32 += voival->int32.u32;
2679 case VSD_DTYPE_INT_S64:
2680 VSD(int64, vsd)->s64 += voival->int64.s64;
2682 case VSD_DTYPE_INT_U64:
2683 VSD(int64, vsd)->u64 += voival->int64.u64;
2685 case VSD_DTYPE_INT_SLONG:
2686 VSD(intlong, vsd)->slong += voival->intlong.slong;
2688 case VSD_DTYPE_INT_ULONG:
2689 VSD(intlong, vsd)->ulong += voival->intlong.ulong;
2691 case VSD_DTYPE_Q_S32:
2692 error = Q_QADDQ(&VSD(q32, vsd)->sq32, voival->q32.sq32);
2694 case VSD_DTYPE_Q_U32:
2695 error = Q_QADDQ(&VSD(q32, vsd)->uq32, voival->q32.uq32);
2697 case VSD_DTYPE_Q_S64:
2698 error = Q_QADDQ(&VSD(q64, vsd)->sq64, voival->q64.sq64);
2700 case VSD_DTYPE_Q_U64:
2701 error = Q_QADDQ(&VSD(q64, vsd)->uq64, voival->q64.uq64);
2709 vs->flags |= VS_VSDVALID;
2715 stats_v1_voi_update_hist(enum vsd_dtype voi_dtype, struct voistatdata *voival,
2716 struct voistat *vs, struct voistatdata_hist *hist)
2718 struct voistatdata_numeric *bkt_lb, *bkt_ub;
2719 uint64_t *oob64, *cnt64;
2720 uint32_t *oob32, *cnt32;
2721 int error, i, found, is32bit, has_ub, eq_only;
2725 switch (vs->dtype) {
2726 case VSD_DTYPE_CRHIST32:
2727 i = HIST_VSDSZ2NBKTS(crhist32, vs->dsz);
2729 has_ub = eq_only = 0;
2730 oob32 = &VSD(crhist32, hist)->oob;
2732 case VSD_DTYPE_DRHIST32:
2733 i = HIST_VSDSZ2NBKTS(drhist32, vs->dsz);
2734 is32bit = has_ub = 1;
2736 oob32 = &VSD(drhist32, hist)->oob;
2738 case VSD_DTYPE_DVHIST32:
2739 i = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz);
2740 is32bit = eq_only = 1;
2742 oob32 = &VSD(dvhist32, hist)->oob;
2744 case VSD_DTYPE_CRHIST64:
2745 i = HIST_VSDSZ2NBKTS(crhist64, vs->dsz);
2746 is32bit = has_ub = eq_only = 0;
2747 oob64 = &VSD(crhist64, hist)->oob;
2749 case VSD_DTYPE_DRHIST64:
2750 i = HIST_VSDSZ2NBKTS(drhist64, vs->dsz);
2751 is32bit = eq_only = 0;
2753 oob64 = &VSD(drhist64, hist)->oob;
2755 case VSD_DTYPE_DVHIST64:
2756 i = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz);
2757 is32bit = has_ub = 0;
2759 oob64 = &VSD(dvhist64, hist)->oob;
2764 i--; /* Adjust for 0-based array index. */
2766 /* XXXLAS: Should probably use a better bucket search algorithm. ARB? */
2767 for (found = 0; i >= 0 && !found; i--) {
2768 switch (vs->dtype) {
2769 case VSD_DTYPE_CRHIST32:
2770 bkt_lb = &VSD(crhist32, hist)->bkts[i].lb;
2771 cnt32 = &VSD(crhist32, hist)->bkts[i].cnt;
2773 case VSD_DTYPE_DRHIST32:
2774 bkt_lb = &VSD(drhist32, hist)->bkts[i].lb;
2775 bkt_ub = &VSD(drhist32, hist)->bkts[i].ub;
2776 cnt32 = &VSD(drhist32, hist)->bkts[i].cnt;
2778 case VSD_DTYPE_DVHIST32:
2779 bkt_lb = &VSD(dvhist32, hist)->bkts[i].val;
2780 cnt32 = &VSD(dvhist32, hist)->bkts[i].cnt;
2782 case VSD_DTYPE_CRHIST64:
2783 bkt_lb = &VSD(crhist64, hist)->bkts[i].lb;
2784 cnt64 = &VSD(crhist64, hist)->bkts[i].cnt;
2786 case VSD_DTYPE_DRHIST64:
2787 bkt_lb = &VSD(drhist64, hist)->bkts[i].lb;
2788 bkt_ub = &VSD(drhist64, hist)->bkts[i].ub;
2789 cnt64 = &VSD(drhist64, hist)->bkts[i].cnt;
2791 case VSD_DTYPE_DVHIST64:
2792 bkt_lb = &VSD(dvhist64, hist)->bkts[i].val;
2793 cnt64 = &VSD(dvhist64, hist)->bkts[i].cnt;
2799 switch (voi_dtype) {
2800 case VSD_DTYPE_INT_S32:
2801 if (voival->int32.s32 >= bkt_lb->int32.s32) {
2802 if ((eq_only && voival->int32.s32 ==
2803 bkt_lb->int32.s32) ||
2804 (!eq_only && (!has_ub ||
2805 voival->int32.s32 < bkt_ub->int32.s32)))
2809 case VSD_DTYPE_INT_U32:
2810 if (voival->int32.u32 >= bkt_lb->int32.u32) {
2811 if ((eq_only && voival->int32.u32 ==
2812 bkt_lb->int32.u32) ||
2813 (!eq_only && (!has_ub ||
2814 voival->int32.u32 < bkt_ub->int32.u32)))
2818 case VSD_DTYPE_INT_S64:
2819 if (voival->int64.s64 >= bkt_lb->int64.s64)
2820 if ((eq_only && voival->int64.s64 ==
2821 bkt_lb->int64.s64) ||
2822 (!eq_only && (!has_ub ||
2823 voival->int64.s64 < bkt_ub->int64.s64)))
2826 case VSD_DTYPE_INT_U64:
2827 if (voival->int64.u64 >= bkt_lb->int64.u64)
2828 if ((eq_only && voival->int64.u64 ==
2829 bkt_lb->int64.u64) ||
2830 (!eq_only && (!has_ub ||
2831 voival->int64.u64 < bkt_ub->int64.u64)))
2834 case VSD_DTYPE_INT_SLONG:
2835 if (voival->intlong.slong >= bkt_lb->intlong.slong)
2836 if ((eq_only && voival->intlong.slong ==
2837 bkt_lb->intlong.slong) ||
2838 (!eq_only && (!has_ub ||
2839 voival->intlong.slong <
2840 bkt_ub->intlong.slong)))
2843 case VSD_DTYPE_INT_ULONG:
2844 if (voival->intlong.ulong >= bkt_lb->intlong.ulong)
2845 if ((eq_only && voival->intlong.ulong ==
2846 bkt_lb->intlong.ulong) ||
2847 (!eq_only && (!has_ub ||
2848 voival->intlong.ulong <
2849 bkt_ub->intlong.ulong)))
2852 case VSD_DTYPE_Q_S32:
2853 if (Q_QGEQ(voival->q32.sq32, bkt_lb->q32.sq32))
2854 if ((eq_only && Q_QEQ(voival->q32.sq32,
2855 bkt_lb->q32.sq32)) ||
2856 (!eq_only && (!has_ub ||
2857 Q_QLTQ(voival->q32.sq32,
2858 bkt_ub->q32.sq32))))
2861 case VSD_DTYPE_Q_U32:
2862 if (Q_QGEQ(voival->q32.uq32, bkt_lb->q32.uq32))
2863 if ((eq_only && Q_QEQ(voival->q32.uq32,
2864 bkt_lb->q32.uq32)) ||
2865 (!eq_only && (!has_ub ||
2866 Q_QLTQ(voival->q32.uq32,
2867 bkt_ub->q32.uq32))))
2870 case VSD_DTYPE_Q_S64:
2871 if (Q_QGEQ(voival->q64.sq64, bkt_lb->q64.sq64))
2872 if ((eq_only && Q_QEQ(voival->q64.sq64,
2873 bkt_lb->q64.sq64)) ||
2874 (!eq_only && (!has_ub ||
2875 Q_QLTQ(voival->q64.sq64,
2876 bkt_ub->q64.sq64))))
2879 case VSD_DTYPE_Q_U64:
2880 if (Q_QGEQ(voival->q64.uq64, bkt_lb->q64.uq64))
2881 if ((eq_only && Q_QEQ(voival->q64.uq64,
2882 bkt_lb->q64.uq64)) ||
2883 (!eq_only && (!has_ub ||
2884 Q_QLTQ(voival->q64.uq64,
2885 bkt_ub->q64.uq64))))
2905 vs->flags |= VS_VSDVALID;
2910 stats_v1_vsd_tdgst_compress(enum vsd_dtype vs_dtype,
2911 struct voistatdata_tdgst *tdgst, int attempt)
2913 struct ctdth32 *ctd32tree;
2914 struct ctdth64 *ctd64tree;
2915 struct voistatdata_tdgstctd32 *ctd32;
2916 struct voistatdata_tdgstctd64 *ctd64;
2917 uint64_t ebits, idxmask;
2918 uint32_t bitsperidx, nebits;
2919 int error, idx, is32bit, maxctds, remctds, tmperr;
2924 case VSD_DTYPE_TDGSTCLUST32:
2925 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
2926 if (!ARB_FULL(ctd32tree))
2928 VSD(tdgstclust32, tdgst)->compcnt++;
2929 maxctds = remctds = ARB_MAXNODES(ctd32tree);
2930 ARB_RESET_TREE(ctd32tree, ctdth32, maxctds);
2931 VSD(tdgstclust32, tdgst)->smplcnt = 0;
2936 RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree);
2939 case VSD_DTYPE_TDGSTCLUST64:
2940 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
2941 if (!ARB_FULL(ctd64tree))
2943 VSD(tdgstclust64, tdgst)->compcnt++;
2944 maxctds = remctds = ARB_MAXNODES(ctd64tree);
2945 ARB_RESET_TREE(ctd64tree, ctdth64, maxctds);
2946 VSD(tdgstclust64, tdgst)->smplcnt = 0;
2951 RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree);
2959 * Rebuild the t-digest ARB by pseudorandomly selecting centroids and
2960 * re-inserting the mu/cnt of each as a value and corresponding weight.
2964 * XXXCEM: random(9) is currently rand(3), not random(3). rand(3)
2965 * RAND_MAX happens to be approximately 31 bits (range [0,
2966 * 0x7ffffffd]), so the math kinda works out. When/if this portion of
2967 * the code is compiled in userspace, it gets the random(3) behavior,
2968 * which has expected range [0, 0x7fffffff].
2970 #define bitsperrand 31
2973 bitsperidx = fls(maxctds);
2974 KASSERT(bitsperidx <= sizeof(ebits) << 3,
2975 ("%s: bitsperidx=%d, ebits=%d",
2976 __func__, bitsperidx, (int)(sizeof(ebits) << 3)));
2977 idxmask = (UINT64_C(1) << bitsperidx) - 1;
2979 /* Initialise the free list with randomised centroid indices. */
2980 for (; remctds > 0; remctds--) {
2981 while (nebits < bitsperidx) {
2982 ebits |= ((uint64_t)random()) << nebits;
2983 nebits += bitsperrand;
2984 if (nebits > (sizeof(ebits) << 3))
2985 nebits = sizeof(ebits) << 3;
2987 idx = ebits & idxmask;
2988 nebits -= bitsperidx;
2989 ebits >>= bitsperidx;
2992 * Select the next centroid to put on the ARB free list. We
2993 * start with the centroid at our randomly selected array index,
2994 * and work our way forwards until finding one (the latter
2995 * aspect reduces re-insertion randomness, but is good enough).
3002 ctd32 = ARB_NODE(ctd32tree, idx);
3004 ctd64 = ARB_NODE(ctd64tree, idx);
3005 } while ((is32bit ? ARB_ISFREE(ctd32, ctdlnk) :
3006 ARB_ISFREE(ctd64, ctdlnk)) && ++idx);
3008 /* Put the centroid on the ARB free list. */
3010 ARB_RETURNFREE(ctd32tree, ctd32, ctdlnk);
3012 ARB_RETURNFREE(ctd64tree, ctd64, ctdlnk);
3016 * The free list now contains the randomised indices of every centroid.
3017 * Walk the free list from start to end, re-inserting each centroid's
3018 * mu/cnt. The tdgst_add() call may or may not consume the free centroid
3019 * we re-insert values from during each loop iteration, so we must latch
3020 * the index of the next free list centroid before the re-insertion
3021 * call. The previous loop above should have left the centroid pointer
3022 * pointing to the element at the head of the free list.
3025 ARB_FREEIDX(ctd32tree) == ARB_SELFIDX(ctd32tree, ctd32) :
3026 ARB_FREEIDX(ctd64tree) == ARB_SELFIDX(ctd64tree, ctd64)),
3027 ("%s: t-digest ARB@%p free list bug", __func__,
3028 (is32bit ? (void *)ctd32tree : (void *)ctd64tree)));
3030 while ((is32bit ? ctd32 != NULL : ctd64 != NULL)) {
3035 idx = ARB_NEXTFREEIDX(ctd32, ctdlnk);
3036 /* Cloning a s32q_t into a s64q_t should never fail. */
3037 tmperr = Q_QCLONEQ(&x, ctd32->mu);
3038 tmperr = tmperr ? tmperr : stats_v1_vsd_tdgst_add(
3039 vs_dtype, tdgst, x, ctd32->cnt, attempt);
3040 ctd32 = ARB_NODE(ctd32tree, idx);
3041 KASSERT(ctd32 == NULL || ARB_ISFREE(ctd32, ctdlnk),
3042 ("%s: t-digest ARB@%p free list bug", __func__,
3045 idx = ARB_NEXTFREEIDX(ctd64, ctdlnk);
3046 tmperr = stats_v1_vsd_tdgst_add(vs_dtype, tdgst,
3047 ctd64->mu, ctd64->cnt, attempt);
3048 ctd64 = ARB_NODE(ctd64tree, idx);
3049 KASSERT(ctd64 == NULL || ARB_ISFREE(ctd64, ctdlnk),
3050 ("%s: t-digest ARB@%p free list bug", __func__,
3054 * This process should not produce errors, bugs notwithstanding.
3055 * Just in case, latch any errors and attempt all re-insertions.
3057 error = tmperr ? tmperr : error;
3061 KASSERT(remctds == 0, ("%s: t-digest ARB@%p free list bug", __func__,
3062 (is32bit ? (void *)ctd32tree : (void *)ctd64tree)));
3068 stats_v1_vsd_tdgst_add(enum vsd_dtype vs_dtype, struct voistatdata_tdgst *tdgst,
3069 s64q_t x, uint64_t weight, int attempt)
3072 char qstr[Q_MAXSTRLEN(x, 10)];
3074 struct ctdth32 *ctd32tree;
3075 struct ctdth64 *ctd64tree;
3076 void *closest, *cur, *lb, *ub;
3077 struct voistatdata_tdgstctd32 *ctd32;
3078 struct voistatdata_tdgstctd64 *ctd64;
3079 uint64_t cnt, smplcnt, sum, tmpsum;
3080 s64q_t k, minz, q, z;
3081 int error, is32bit, n;
3084 minz = Q_INI(&z, 0, 0, Q_NFBITS(x));
3087 case VSD_DTYPE_TDGSTCLUST32:
3088 if ((UINT32_MAX - weight) < VSD(tdgstclust32, tdgst)->smplcnt)
3090 smplcnt = VSD(tdgstclust32, tdgst)->smplcnt;
3091 ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
3096 case VSD_DTYPE_TDGSTCLUST64:
3097 if ((UINT64_MAX - weight) < VSD(tdgstclust64, tdgst)->smplcnt)
3099 smplcnt = VSD(tdgstclust64, tdgst)->smplcnt;
3100 ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
3114 * Inspired by Ted Dunning's AVLTreeDigest.java
3117 #if defined(DIAGNOSTIC)
3118 KASSERT(attempt < 5,
3119 ("%s: Too many attempts", __func__));
3124 Q_SIFVAL(minz, Q_IFMAXVAL(minz));
3125 closest = ub = NULL;
3129 lb = cur = (void *)(ctd32 = ARB_MIN(ctdth32, ctd32tree));
3131 lb = cur = (void *)(ctd64 = ARB_MIN(ctdth64, ctd64tree));
3133 if (lb == NULL) /* Empty tree. */
3134 lb = (is32bit ? (void *)ARB_ROOT(ctd32tree) :
3135 (void *)ARB_ROOT(ctd64tree));
3138 * Find the set of centroids with minimum distance to x and
3139 * compute the sum of counts for all centroids with mean less
3140 * than the first centroid in the set.
3144 (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) :
3145 (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) {
3148 KASSERT(Q_PRECEQ(ctd32->mu, x),
3149 ("%s: Q_RELPREC(mu,x)=%d", __func__,
3150 Q_RELPREC(ctd32->mu, x)));
3151 /* Ok to assign as both have same precision. */
3155 KASSERT(Q_PRECEQ(ctd64->mu, x),
3156 ("%s: Q_RELPREC(mu,x)=%d", __func__,
3157 Q_RELPREC(ctd64->mu, x)));
3158 /* Ok to assign as both have same precision. */
3162 error = Q_QSUBQ(&z, x);
3163 #if defined(DIAGNOSTIC)
3164 KASSERT(!error, ("%s: unexpected error %d", __func__,
3171 if (Q_QLTQ(z, minz)) {
3176 } else if (Q_QGTQ(z, minz)) {
3183 (void *)(ctd32 = (struct voistatdata_tdgstctd32 *)lb) :
3184 (void *)(ctd64 = (struct voistatdata_tdgstctd64 *)lb));
3186 for (n = 0; cur != ub; cur = (is32bit ?
3187 (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) :
3188 (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) {
3196 error = Q_QFRACI(&q, 1, 2);
3198 /* [ sum + ((cnt - 1) / 2) ] / (smplcnt - 1) */
3199 error = Q_QFRACI(&q, (sum << 1) + cnt - 1,
3200 (smplcnt - 1) << 1);
3202 /* k = q x 4 x samplcnt x attempt */
3203 error |= Q_QMULI(&k, 4 * smplcnt * attempt);
3204 /* k = k x (1 - q) */
3205 error |= Q_QSUBI(&q, 1);
3207 error |= Q_QMULQ(&k, q);
3208 #if defined(DIAGNOSTIC)
3209 #if !defined(_KERNEL)
3210 double q_dbl, k_dbl, q2d, k2d;
3213 q_dbl = smplcnt == 1 ? 0.5 :
3214 (sum + ((cnt - 1) / 2.0)) / (double)(smplcnt - 1);
3215 k_dbl = 4 * smplcnt * q_dbl * (1.0 - q_dbl) * attempt;
3217 * If the difference between q and q_dbl is greater than
3218 * the fractional precision of q, something is off.
3219 * NB: q is holding the value of 1 - q
3221 q_dbl = 1.0 - q_dbl;
3222 KASSERT((q_dbl > q2d ? q_dbl - q2d : q2d - q_dbl) <
3223 (1.05 * ((double)1 / (double)(1ULL << Q_NFBITS(q)))),
3224 ("Q-type q bad precision"));
3225 KASSERT((k_dbl > k2d ? k_dbl - k2d : k2d - k_dbl) <
3226 1.0 + (0.01 * smplcnt),
3227 ("Q-type k bad precision"));
3228 #endif /* !_KERNEL */
3229 KASSERT(!error, ("%s: unexpected error %d", __func__,
3231 #endif /* DIAGNOSTIC */
3234 if ((is32bit && ((ctd32->cnt + weight) <=
3235 (uint64_t)Q_GIVAL(k))) ||
3236 (!is32bit && ((ctd64->cnt + weight) <=
3237 (uint64_t)Q_GIVAL(k)))) {
3239 /* random() produces 31 bits. */
3240 if (random() < (INT32_MAX / n))
3245 } while (closest == NULL &&
3246 (is32bit ? ARB_FULL(ctd32tree) : ARB_FULL(ctd64tree)) &&
3247 (error = stats_v1_vsd_tdgst_compress(vs_dtype, tdgst,
3253 if (closest != NULL) {
3254 /* Merge with an existing centroid. */
3256 ctd32 = (struct voistatdata_tdgstctd32 *)closest;
3257 error = Q_QSUBQ(&x, ctd32->mu);
3258 error = error ? error :
3259 Q_QDIVI(&x, ctd32->cnt + weight);
3260 if (error || (error = Q_QADDQ(&ctd32->mu, x))) {
3262 KASSERT(!error, ("%s: unexpected error %d",
3267 ctd32->cnt += weight;
3268 error = ARB_REINSERT(ctdth32, ctd32tree, ctd32) ==
3269 NULL ? 0 : EALREADY;
3271 RB_REINSERT(rbctdth32,
3272 &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32);
3275 ctd64 = (struct voistatdata_tdgstctd64 *)closest;
3276 error = Q_QSUBQ(&x, ctd64->mu);
3277 error = error ? error :
3278 Q_QDIVI(&x, ctd64->cnt + weight);
3279 if (error || (error = Q_QADDQ(&ctd64->mu, x))) {
3280 KASSERT(!error, ("%s: unexpected error %d",
3284 ctd64->cnt += weight;
3285 error = ARB_REINSERT(ctdth64, ctd64tree, ctd64) ==
3286 NULL ? 0 : EALREADY;
3288 RB_REINSERT(rbctdth64,
3289 &VSD(tdgstclust64, tdgst)->rbctdtree, ctd64);
3294 * Add a new centroid. If digest compression is working
3295 * correctly, there should always be at least one free.
3298 ctd32 = ARB_GETFREE(ctd32tree, ctdlnk);
3300 KASSERT(ctd32 != NULL,
3301 ("%s: t-digest@%p has no free centroids",
3306 if ((error = Q_QCPYVALQ(&ctd32->mu, x)))
3308 ctd32->cnt = weight;
3309 error = ARB_INSERT(ctdth32, ctd32tree, ctd32) == NULL ?
3312 RB_INSERT(rbctdth32,
3313 &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32);
3316 ctd64 = ARB_GETFREE(ctd64tree, ctdlnk);
3318 KASSERT(ctd64 != NULL,
3319 ("%s: t-digest@%p has no free centroids",
3322 if (ctd64 == NULL) /* Should not happen. */
3324 /* Direct assignment ok as both have same type/prec. */
3326 ctd64->cnt = weight;
3327 error = ARB_INSERT(ctdth64, ctd64tree, ctd64) == NULL ?
3330 RB_INSERT(rbctdth64, &VSD(tdgstclust64,
3331 tdgst)->rbctdtree, ctd64);
3337 VSD(tdgstclust32, tdgst)->smplcnt += weight;
3339 VSD(tdgstclust64, tdgst)->smplcnt += weight;
3342 struct rbctdth64 *rbctdtree =
3343 &VSD(tdgstclust64, tdgst)->rbctdtree;
3344 struct voistatdata_tdgstctd64 *rbctd64;
3346 ARB_FOREACH(ctd64, ctdth64, ctd64tree) {
3347 rbctd64 = (i == 0 ? RB_MIN(rbctdth64, rbctdtree) :
3348 RB_NEXT(rbctdth64, rbctdtree, rbctd64));
3350 if (i >= ARB_CURNODES(ctd64tree)
3352 || ARB_MIN(ctdth64, ctd64tree) !=
3353 RB_MIN(rbctdth64, rbctdtree)
3354 || ARB_MAX(ctdth64, ctd64tree) !=
3355 RB_MAX(rbctdth64, rbctdtree)
3356 || ARB_LEFTIDX(ctd64, ctdlnk) !=
3357 ARB_SELFIDX(ctd64tree, RB_LEFT(rbctd64, rblnk))
3358 || ARB_RIGHTIDX(ctd64, ctdlnk) !=
3359 ARB_SELFIDX(ctd64tree, RB_RIGHT(rbctd64, rblnk))
3360 || ARB_PARENTIDX(ctd64, ctdlnk) !=
3361 ARB_SELFIDX(ctd64tree,
3362 RB_PARENT(rbctd64, rblnk))) {
3363 Q_TOSTR(ctd64->mu, -1, 10, qstr, sizeof(qstr));
3364 printf("ARB ctd=%3d p=%3d l=%3d r=%3d c=%2d "
3366 (int)ARB_SELFIDX(ctd64tree, ctd64),
3367 ARB_PARENTIDX(ctd64, ctdlnk),
3368 ARB_LEFTIDX(ctd64, ctdlnk),
3369 ARB_RIGHTIDX(ctd64, ctdlnk),
3370 ARB_COLOR(ctd64, ctdlnk),
3373 Q_TOSTR(rbctd64->mu, -1, 10, qstr,
3375 printf(" RB ctd=%3d p=%3d l=%3d r=%3d c=%2d "
3377 (int)ARB_SELFIDX(ctd64tree, rbctd64),
3378 (int)ARB_SELFIDX(ctd64tree,
3379 RB_PARENT(rbctd64, rblnk)),
3380 (int)ARB_SELFIDX(ctd64tree,
3381 RB_LEFT(rbctd64, rblnk)),
3382 (int)ARB_SELFIDX(ctd64tree,
3383 RB_RIGHT(rbctd64, rblnk)),
3384 RB_COLOR(rbctd64, rblnk),
3387 panic("RB@%p and ARB@%p trees differ\n",
3388 rbctdtree, ctd64tree);
3392 #endif /* DIAGNOSTIC */
3399 stats_v1_voi_update_tdgst(enum vsd_dtype voi_dtype, struct voistatdata *voival,
3400 struct voistat *vs, struct voistatdata_tdgst *tdgst)
3407 switch (vs->dtype) {
3408 case VSD_DTYPE_TDGSTCLUST32:
3409 /* Use same precision as the user's centroids. */
3410 Q_INI(&x, 0, 0, Q_NFBITS(
3411 ARB_CNODE(&VSD(tdgstclust32, tdgst)->ctdtree, 0)->mu));
3413 case VSD_DTYPE_TDGSTCLUST64:
3414 /* Use same precision as the user's centroids. */
3415 Q_INI(&x, 0, 0, Q_NFBITS(
3416 ARB_CNODE(&VSD(tdgstclust64, tdgst)->ctdtree, 0)->mu));
3419 KASSERT(vs->dtype == VSD_DTYPE_TDGSTCLUST32 ||
3420 vs->dtype == VSD_DTYPE_TDGSTCLUST64,
3421 ("%s: vs->dtype(%d) != VSD_DTYPE_TDGSTCLUST<32|64>",
3422 __func__, vs->dtype));
3427 * XXXLAS: Should have both a signed and unsigned 'x' variable to avoid
3428 * returning EOVERFLOW if the voival would have fit in a u64q_t.
3430 switch (voi_dtype) {
3431 case VSD_DTYPE_INT_S32:
3432 error = Q_QCPYVALI(&x, voival->int32.s32);
3434 case VSD_DTYPE_INT_U32:
3435 error = Q_QCPYVALI(&x, voival->int32.u32);
3437 case VSD_DTYPE_INT_S64:
3438 error = Q_QCPYVALI(&x, voival->int64.s64);
3440 case VSD_DTYPE_INT_U64:
3441 error = Q_QCPYVALI(&x, voival->int64.u64);
3443 case VSD_DTYPE_INT_SLONG:
3444 error = Q_QCPYVALI(&x, voival->intlong.slong);
3446 case VSD_DTYPE_INT_ULONG:
3447 error = Q_QCPYVALI(&x, voival->intlong.ulong);
3449 case VSD_DTYPE_Q_S32:
3450 error = Q_QCPYVALQ(&x, voival->q32.sq32);
3452 case VSD_DTYPE_Q_U32:
3453 error = Q_QCPYVALQ(&x, voival->q32.uq32);
3455 case VSD_DTYPE_Q_S64:
3456 error = Q_QCPYVALQ(&x, voival->q64.sq64);
3458 case VSD_DTYPE_Q_U64:
3459 error = Q_QCPYVALQ(&x, voival->q64.uq64);
3467 (error = stats_v1_vsd_tdgst_add(vs->dtype, tdgst, x, 1, 1)))
3470 vs->flags |= VS_VSDVALID;
3475 stats_v1_voi_update(struct statsblobv1 *sb, int32_t voi_id,
3476 enum vsd_dtype voi_dtype, struct voistatdata *voival, uint32_t flags)
3480 void *statevsd, *vsd;
3481 int error, i, tmperr;
3485 if (sb == NULL || sb->abi != STATS_ABI_V1 || voi_id >= NVOIS(sb) ||
3486 voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES || voival == NULL)
3488 v = &sb->vois[voi_id];
3489 if (voi_dtype != v->dtype || v->id < 0 ||
3490 ((flags & SB_VOI_RELUPDATE) && !(v->flags & VOI_REQSTATE)))
3493 vs = BLOB_OFFSET(sb, v->stats_off);
3494 if (v->flags & VOI_REQSTATE)
3495 statevsd = BLOB_OFFSET(sb, vs->data_off);
3499 if (flags & SB_VOI_RELUPDATE) {
3500 switch (voi_dtype) {
3501 case VSD_DTYPE_INT_S32:
3502 voival->int32.s32 +=
3503 VSD(voistate, statevsd)->prev.int32.s32;
3505 case VSD_DTYPE_INT_U32:
3506 voival->int32.u32 +=
3507 VSD(voistate, statevsd)->prev.int32.u32;
3509 case VSD_DTYPE_INT_S64:
3510 voival->int64.s64 +=
3511 VSD(voistate, statevsd)->prev.int64.s64;
3513 case VSD_DTYPE_INT_U64:
3514 voival->int64.u64 +=
3515 VSD(voistate, statevsd)->prev.int64.u64;
3517 case VSD_DTYPE_INT_SLONG:
3518 voival->intlong.slong +=
3519 VSD(voistate, statevsd)->prev.intlong.slong;
3521 case VSD_DTYPE_INT_ULONG:
3522 voival->intlong.ulong +=
3523 VSD(voistate, statevsd)->prev.intlong.ulong;
3525 case VSD_DTYPE_Q_S32:
3526 error = Q_QADDQ(&voival->q32.sq32,
3527 VSD(voistate, statevsd)->prev.q32.sq32);
3529 case VSD_DTYPE_Q_U32:
3530 error = Q_QADDQ(&voival->q32.uq32,
3531 VSD(voistate, statevsd)->prev.q32.uq32);
3533 case VSD_DTYPE_Q_S64:
3534 error = Q_QADDQ(&voival->q64.sq64,
3535 VSD(voistate, statevsd)->prev.q64.sq64);
3537 case VSD_DTYPE_Q_U64:
3538 error = Q_QADDQ(&voival->q64.uq64,
3539 VSD(voistate, statevsd)->prev.q64.uq64);
3542 KASSERT(0, ("Unknown VOI data type %d", voi_dtype));
3550 for (i = v->voistatmaxid; i > 0; i--) {
3551 vs = &((struct voistat *)BLOB_OFFSET(sb, v->stats_off))[i];
3555 vsd = BLOB_OFFSET(sb, vs->data_off);
3557 switch (vs->stype) {
3559 tmperr = stats_v1_voi_update_max(voi_dtype, voival,
3563 tmperr = stats_v1_voi_update_min(voi_dtype, voival,
3567 tmperr = stats_v1_voi_update_sum(voi_dtype, voival,
3571 tmperr = stats_v1_voi_update_hist(voi_dtype, voival,
3574 case VS_STYPE_TDGST:
3575 tmperr = stats_v1_voi_update_tdgst(voi_dtype, voival,
3579 KASSERT(0, ("Unknown VOI stat type %d", vs->stype));
3590 switch (voi_dtype) {
3591 case VSD_DTYPE_INT_S32:
3592 VSD(voistate, statevsd)->prev.int32.s32 =
3595 case VSD_DTYPE_INT_U32:
3596 VSD(voistate, statevsd)->prev.int32.u32 =
3599 case VSD_DTYPE_INT_S64:
3600 VSD(voistate, statevsd)->prev.int64.s64 =
3603 case VSD_DTYPE_INT_U64:
3604 VSD(voistate, statevsd)->prev.int64.u64 =
3607 case VSD_DTYPE_INT_SLONG:
3608 VSD(voistate, statevsd)->prev.intlong.slong =
3609 voival->intlong.slong;
3611 case VSD_DTYPE_INT_ULONG:
3612 VSD(voistate, statevsd)->prev.intlong.ulong =
3613 voival->intlong.ulong;
3615 case VSD_DTYPE_Q_S32:
3617 &VSD(voistate, statevsd)->prev.q32.sq32,
3620 case VSD_DTYPE_Q_U32:
3622 &VSD(voistate, statevsd)->prev.q32.uq32,
3625 case VSD_DTYPE_Q_S64:
3627 &VSD(voistate, statevsd)->prev.q64.sq64,
3630 case VSD_DTYPE_Q_U64:
3632 &VSD(voistate, statevsd)->prev.q64.uq64,
3636 KASSERT(0, ("Unknown VOI data type %d", voi_dtype));
3647 stats_init(void *arg)
3651 SYSINIT(stats, SI_SUB_KDTRACE, SI_ORDER_FIRST, stats_init, NULL);
3654 * Sysctl handler to display the list of available stats templates.
3657 stats_tpl_list_available(SYSCTL_HANDLER_ARGS)
3664 /* We can tolerate ntpl being stale, so do not take the lock. */
3665 s = sbuf_new(NULL, NULL, /* +1 per tpl for , */
3666 ntpl * (STATS_TPL_MAX_STR_SPEC_LEN + 1), SBUF_FIXEDLEN);
3671 for (i = 0; i < ntpl; i++) {
3672 err = sbuf_printf(s, "%s\"%s\":%u", i ? "," : "",
3673 tpllist[i]->mb->tplname, tpllist[i]->mb->tplhash);
3675 /* Sbuf overflow condition. */
3684 err = sysctl_handle_string(oidp, sbuf_data(s), 0, req);
3692 * Called by subsystem-specific sysctls to report and/or parse the list of
3693 * templates being sampled and their sampling rates. A stats_tpl_sr_cb_t
3694 * conformant function pointer must be passed in as arg1, which is used to
3695 * interact with the subsystem's stats template sample rates list. If arg2 > 0,
3696 * a zero-initialised allocation of arg2-sized contextual memory is
3697 * heap-allocated and passed in to all subsystem callbacks made during the
3698 * operation of stats_tpl_sample_rates().
3700 * XXXLAS: Assumes templates are never removed, which is currently true but may
3701 * need to be reworked in future if dynamic template management becomes a
3702 * requirement e.g. to support kernel module based templates.
3705 stats_tpl_sample_rates(SYSCTL_HANDLER_ARGS)
3707 char kvpair_fmt[16], tplspec_fmt[16];
3708 char tpl_spec[STATS_TPL_MAX_STR_SPEC_LEN];
3709 char tpl_name[TPL_MAX_NAME_LEN + 2]; /* +2 for "" */
3710 stats_tpl_sr_cb_t subsys_cb;
3712 char *buf, *new_rates_usr_str, *tpl_name_p;
3713 struct stats_tpl_sample_rate *rates;
3715 uint32_t cum_pct, pct, tpl_hash;
3716 int err, i, off, len, newlen, nrates;
3721 subsys_cb = (stats_tpl_sr_cb_t)arg1;
3722 KASSERT(subsys_cb != NULL, ("%s: subsys_cb == arg1 == NULL", __func__));
3724 subsys_ctx = malloc(arg2, M_TEMP, M_WAITOK | M_ZERO);
3728 /* Grab current count of subsystem rates. */
3729 err = subsys_cb(TPL_SR_UNLOCKED_GET, NULL, &nrates, subsys_ctx);
3733 /* +1 to ensure we can append '\0' post copyin, +5 per rate for =nnn, */
3734 len = max(req->newlen + 1, nrates * (STATS_TPL_MAX_STR_SPEC_LEN + 5));
3736 if (req->oldptr != NULL || req->newptr != NULL)
3737 buf = malloc(len, M_TEMP, M_WAITOK);
3739 if (req->oldptr != NULL) {
3741 /* No rates, so return an empty string via oldptr. */
3742 err = SYSCTL_OUT(req, "", 1);
3748 s = sbuf_new(&_s, buf, len, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
3750 /* Grab locked count of, and ptr to, subsystem rates. */
3751 err = subsys_cb(TPL_SR_RLOCKED_GET, &rates, &nrates,
3756 for (i = 0; i < nrates && !err; i++) {
3757 err = sbuf_printf(s, "%s\"%s\":%u=%u", i ? "," : "",
3758 tpllist[rates[i].tpl_slot_id]->mb->tplname,
3759 tpllist[rates[i].tpl_slot_id]->mb->tplhash,
3760 rates[i].tpl_sample_pct);
3763 /* Tell subsystem that we're done with its rates list. */
3764 err = subsys_cb(TPL_SR_RUNLOCK, &rates, &nrates, subsys_ctx);
3768 err = sbuf_finish(s);
3770 goto done; /* We lost a race for buf to be too small. */
3772 /* Return the rendered string data via oldptr. */
3773 err = SYSCTL_OUT(req, sbuf_data(s), sbuf_len(s));
3775 /* Return the upper bound size for buffer sizing requests. */
3776 err = SYSCTL_OUT(req, NULL, len);
3780 if (err || req->newptr == NULL)
3783 newlen = req->newlen - req->newidx;
3784 err = SYSCTL_IN(req, buf, newlen);
3789 * Initialise format strings at run time.
3791 * Write the max template spec string length into the
3792 * template_spec=percent key-value pair parsing format string as:
3793 * " %<width>[^=]=%u %n"
3795 * Write the max template name string length into the tplname:tplhash
3796 * parsing format string as:
3799 * Subtract 1 for \0 appended by sscanf().
3801 sprintf(kvpair_fmt, " %%%zu[^=]=%%u %%n", sizeof(tpl_spec) - 1);
3802 sprintf(tplspec_fmt, "%%%zu[^:]:%%u", sizeof(tpl_name) - 1);
3805 * Parse each CSV key-value pair specifying a template and its sample
3806 * percentage. Whitespace either side of a key-value pair is ignored.
3807 * Templates can be specified by name, hash, or name and hash per the
3808 * following formats (chars in [] are optional):
3809 * ["]<tplname>["]=<percent>
3811 * ["]<tplname>["]:hash=<percent>
3813 cum_pct = nrates = 0;
3815 buf[newlen] = '\0'; /* buf is at least newlen+1 in size. */
3816 new_rates_usr_str = buf;
3817 while (isspace(*new_rates_usr_str))
3818 new_rates_usr_str++; /* Skip leading whitespace. */
3819 while (*new_rates_usr_str != '\0') {
3820 tpl_name_p = tpl_name;
3826 * Parse key-value pair which must perform 2 conversions, then
3827 * parse the template spec to extract either name, hash, or name
3828 * and hash depending on the three possible spec formats. The
3829 * tplspec_fmt format specifier parses name or name and hash
3830 * template specs, while the ":%u" format specifier parses
3831 * hash-only template specs. If parsing is successfull, ensure
3832 * the cumulative sampling percentage does not exceed 100.
3835 if (2 != sscanf(new_rates_usr_str, kvpair_fmt, tpl_spec, &pct,
3838 if ((1 > sscanf(tpl_spec, tplspec_fmt, tpl_name, &tpl_hash)) &&
3839 (1 != sscanf(tpl_spec, ":%u", &tpl_hash)))
3841 if ((cum_pct += pct) > 100)
3845 /* Strip surrounding "" from template name if present. */
3846 len = strlen(tpl_name);
3848 if (tpl_name[len - 1] == '"')
3849 tpl_name[--len] = '\0';
3850 if (tpl_name[0] == '"') {
3856 rates = stats_realloc(rates, 0, /* oldsz is unused in kernel. */
3857 (nrates + 1) * sizeof(*rates), M_WAITOK);
3858 rates[nrates].tpl_slot_id =
3859 stats_tpl_fetch_allocid(len ? tpl_name_p : NULL, tpl_hash);
3860 if (rates[nrates].tpl_slot_id < 0) {
3861 err = -rates[nrates].tpl_slot_id;
3864 rates[nrates].tpl_sample_pct = pct;
3866 new_rates_usr_str += off;
3867 if (*new_rates_usr_str != ',')
3868 break; /* End-of-input or malformed. */
3869 new_rates_usr_str++; /* Move past comma to next pair. */
3873 if ((new_rates_usr_str - buf) < newlen) {
3874 /* Entire input has not been consumed. */
3878 * Give subsystem the new rates. They'll return the
3879 * appropriate rates pointer for us to garbage collect.
3881 err = subsys_cb(TPL_SR_PUT, &rates, &nrates,
3889 free(subsys_ctx, M_TEMP);
3893 SYSCTL_NODE(_kern, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
3896 SYSCTL_PROC(_kern_stats, OID_AUTO, templates,
3897 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3898 stats_tpl_list_available, "A",
3899 "list the name/hash of all available stats(9) templates");
3901 #else /* ! _KERNEL */
3903 static void __attribute__ ((constructor))
3904 stats_constructor(void)
3907 pthread_rwlock_init(&tpllistlock, NULL);
3910 static void __attribute__ ((destructor))
3911 stats_destructor(void)
3914 pthread_rwlock_destroy(&tpllistlock);
3917 #endif /* _KERNEL */