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