2 * Copyright (c) 2014-2018 Netflix, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * A kernel and user space statistics gathering API + infrastructure.
32 * Author: Lawrence Stewart <lstewart@netflix.com>
35 * - Register callbacks for events e.g. counter stat passing a threshold
37 * - How could this become SIFTRv2? Perhaps publishing records to a ring
38 * mapped between userspace and kernel?
40 * - Potential stat types:
41 * RATE: events per unit time
42 * TIMESERIES: timestamped records. Stored in voistate?
43 * EWMA: Exponential weighted moving average.
45 * - How should second order stats work e.g. stat "A" depends on "B"
47 * - How do variable time windows work e.g. give me per-RTT stats
49 * - Should the API always require the caller to manage locking? Or should the
50 * API provide optional functionality to lock a blob during operations.
52 * - Should we continue to store unpacked naturally aligned structs in the
53 * blob or move to packed structs? Relates to inter-host
54 * serialisation/endian issues.
60 #include <sys/limits.h>
67 * XXXLAS: Hacks to enable sharing template creation code between kernel and
68 * userland e.g. tcp_stats.c
71 #define VNET_DEFINE(t, n) static t n __unused
72 #endif /* ! _KERNEL */
74 #define TPL_MAX_NAME_LEN 64
77 * The longest template string spec format i.e. the normative spec format, is:
79 * "<tplname>":<tplhash>
81 * Therefore, the max string length of a template string spec is:
85 * - 1 char for : separating name and hash
86 * - 10 chars for 32bit hash
88 #define STATS_TPL_MAX_STR_SPEC_LEN (TPL_MAX_NAME_LEN + 13)
95 SB_STRFMT_FREEFORM = 0,
97 SB_STRFMT_NUM_FMTS /* +1 to highest numbered format type. */
100 /* VOI stat types. */
102 VS_STYPE_VOISTATE = 0, /* Reserved for internal API use. */
108 VS_NUM_STYPES /* +1 to highest numbered stat type. */
112 * VOI stat data types used as storage for certain stat types and to marshall
113 * data through various API calls.
116 VSD_DTYPE_VOISTATE = 0, /* Reserved for internal API use. */
117 VSD_DTYPE_INT_S32, /* int32_t */
118 VSD_DTYPE_INT_U32, /* uint32_t */
119 VSD_DTYPE_INT_S64, /* int64_t */
120 VSD_DTYPE_INT_U64, /* uint64_t */
121 VSD_DTYPE_INT_SLONG, /* long */
122 VSD_DTYPE_INT_ULONG, /* unsigned long */
123 VSD_DTYPE_Q_S32, /* s32q_t */
124 VSD_DTYPE_Q_U32, /* u32q_t */
125 VSD_DTYPE_Q_S64, /* s64q_t */
126 VSD_DTYPE_Q_U64, /* u64q_t */
127 VSD_DTYPE_CRHIST32, /* continuous range histogram, 32bit buckets */
128 VSD_DTYPE_DRHIST32, /* discrete range histogram, 32bit buckets */
129 VSD_DTYPE_DVHIST32, /* discrete value histogram, 32bit buckets */
130 VSD_DTYPE_CRHIST64, /* continuous range histogram, 64bit buckets */
131 VSD_DTYPE_DRHIST64, /* discrete range histogram, 64bit buckets */
132 VSD_DTYPE_DVHIST64, /* discrete value histogram, 64bit buckets */
133 VSD_DTYPE_TDGSTCLUST32, /* clustering variant t-digest, 32bit buckets */
134 VSD_DTYPE_TDGSTCLUST64, /* clustering variant t-digest, 64bit buckets */
135 VSD_NUM_DTYPES /* +1 to highest numbered data type. */
138 struct voistatdata_int32 {
145 struct voistatdata_int64 {
149 //counter_u64_t u64pcpu;
153 struct voistatdata_intlong {
160 struct voistatdata_q32 {
167 struct voistatdata_q64 {
174 struct voistatdata_numeric {
177 #if BYTE_ORDER == BIG_ENDIAN
184 #if BYTE_ORDER == LITTLE_ENDIAN
190 #if BYTE_ORDER == BIG_ENDIAN
197 #if BYTE_ORDER == LITTLE_ENDIAN
203 #if BYTE_ORDER == BIG_ENDIAN && LONG_BIT == 32
210 #if BYTE_ORDER == LITTLE_ENDIAN && LONG_BIT == 32
215 struct voistatdata_int64 int64;
216 struct voistatdata_q64 q64;
220 /* Continuous range histogram with 32bit buckets. */
221 struct voistatdata_crhist32 {
224 struct voistatdata_numeric lb;
229 /* Continuous range histogram with 64bit buckets. */
230 struct voistatdata_crhist64 {
233 struct voistatdata_numeric lb;
238 /* Discrete range histogram with 32bit buckets. */
239 struct voistatdata_drhist32 {
242 struct voistatdata_numeric lb, ub;
247 /* Discrete range histogram with 64bit buckets. */
248 struct voistatdata_drhist64 {
251 struct voistatdata_numeric lb, ub;
256 /* Discrete value histogram with 32bit buckets. */
257 struct voistatdata_dvhist32 {
260 struct voistatdata_numeric val;
265 /* Discrete value histogram with 64bit buckets. */
266 struct voistatdata_dvhist64 {
269 struct voistatdata_numeric val;
274 struct voistatdata_hist {
276 struct voistatdata_crhist32 crhist32;
277 struct voistatdata_crhist64 crhist64;
278 struct voistatdata_dvhist32 dvhist32;
279 struct voistatdata_dvhist64 dvhist64;
280 struct voistatdata_drhist32 drhist32;
281 struct voistatdata_drhist64 drhist64;
285 struct voistatdata_tdgstctd32 {
286 ARB16_ENTRY() ctdlnk;
288 RB_ENTRY(voistatdata_tdgstctd32) rblnk;
294 struct voistatdata_tdgstctd64 {
295 ARB16_ENTRY() ctdlnk;
297 RB_ENTRY(voistatdata_tdgstctd64) rblnk;
303 struct voistatdata_tdgstctd {
305 struct voistatdata_tdgstctd32 tdgstctd32;
306 struct voistatdata_tdgstctd64 tdgstctd64;
310 /* Clustering variant, fixed-point t-digest with 32bit mu/counts. */
311 struct voistatdata_tdgstclust32 {
312 uint32_t smplcnt; /* Count of samples. */
313 uint32_t compcnt; /* Count of digest compressions. */
315 RB_HEAD(rbctdth32, voistatdata_tdgstctd32) rbctdtree;
317 /* Array-based red-black tree of centroids. */
318 ARB16_HEAD(ctdth32, voistatdata_tdgstctd32) ctdtree;
321 /* Clustering variant, fixed-point t-digest with 64bit mu/counts. */
322 struct voistatdata_tdgstclust64 {
323 uint64_t smplcnt; /* Count of samples. */
324 uint32_t compcnt; /* Count of digest compressions. */
326 RB_HEAD(rbctdth64, voistatdata_tdgstctd64) rbctdtree;
328 /* Array-based red-black tree of centroids. */
329 ARB16_HEAD(ctdth64, voistatdata_tdgstctd64) ctdtree;
332 struct voistatdata_tdgst {
334 struct voistatdata_tdgstclust32 tdgstclust32;
335 struct voistatdata_tdgstclust64 tdgstclust64;
341 struct voistatdata_int32 int32;
342 struct voistatdata_int64 int64;
343 struct voistatdata_intlong intlong;
344 struct voistatdata_q32 q32;
345 struct voistatdata_q64 q64;
346 struct voistatdata_crhist32 crhist32;
347 struct voistatdata_crhist64 crhist64;
348 struct voistatdata_dvhist32 dvhist32;
349 struct voistatdata_dvhist64 dvhist64;
350 struct voistatdata_drhist32 drhist32;
351 struct voistatdata_drhist64 drhist64;
352 struct voistatdata_tdgstclust32 tdgstclust32;
353 struct voistatdata_tdgstclust64 tdgstclust64;
357 #define VSD_HIST_LBOUND_INF 0x01
358 #define VSD_HIST_UBOUND_INF 0x02
359 struct vss_hist_hlpr_info {
360 enum hist_bkt_alloc {
361 BKT_LIN, /* Linear steps. */
362 BKT_EXP, /* Exponential steps. */
363 BKT_LINEXP, /* Exponential steps, linear sub-steps. */
364 BKT_USR /* User specified buckets. */
366 enum vsd_dtype voi_dtype;
367 enum vsd_dtype hist_dtype;
369 struct voistatdata_numeric lb;
370 struct voistatdata_numeric ub;
373 const uint64_t stepinc;
376 const uint64_t stepbase;
377 const uint64_t stepexp;
380 const uint64_t stepbase;
381 const uint64_t linstepdiv;
384 const uint16_t nbkts;
386 struct voistatdata_numeric lb, ub;
392 struct vss_tdgst_hlpr_info {
393 enum vsd_dtype voi_dtype;
394 enum vsd_dtype tdgst_dtype;
397 } __aligned(sizeof(void *));
399 struct vss_numeric_hlpr_info {
403 struct vss_hlpr_info {
405 struct vss_tdgst_hlpr_info tdgst;
406 struct vss_hist_hlpr_info hist;
407 struct vss_numeric_hlpr_info numeric;
412 typedef int (*vss_hlpr_fn)(enum vsd_dtype, struct voistatspec *,
413 struct vss_hlpr_info *);
416 vss_hlpr_fn hlpr; /* iv helper function. */
417 struct vss_hlpr_info *hlprinfo; /* Helper function context. */
418 struct voistatdata *iv; /* Initialisation value. */
419 size_t vsdsz; /* Size of iv. */
420 uint32_t flags; /* Stat flags. */
421 enum vsd_dtype vs_dtype : 8; /* Stat's dtype. */
422 enum voi_stype stype : 8; /* Stat type. */
425 extern const char *vs_stype2name[VS_NUM_STYPES];
426 extern const char *vs_stype2desc[VS_NUM_STYPES];
427 extern const char *vsd_dtype2name[VSD_NUM_DTYPES];
428 extern const size_t vsd_dtype2size[VSD_NUM_DTYPES];
431 extern const struct voistatdata_numeric numeric_limits[2][VSD_DTYPE_Q_U64 + 1];
433 #define TYPEOF_MEMBER(type, member) __typeof(((type *)0)->member)
434 #define TYPEOF_MEMBER_PTR(type, member) __typeof(*(((type *)0)->member))
435 #define SIZEOF_MEMBER(type, member) sizeof(TYPEOF_MEMBER(type, member))
437 /* Cast a pointer to a voistatdata struct of requested type. */
438 #define _VSD(cnst, type, ptr) ((cnst struct voistatdata_##type *)(ptr))
439 #define VSD(type, ptr) _VSD(, type, ptr)
440 #define CONSTVSD(type, ptr) _VSD(const, type, ptr)
442 #define NVSS(vss_slots) (sizeof((vss_slots)) / sizeof(struct voistatspec))
443 #define STATS_VSS(st, vsf, dt, hlp, hlpi) \
444 ((struct voistatspec){ \
449 .hlprinfo = (hlpi), \
452 #define STATS_VSS_SUM() STATS_VSS(VS_STYPE_SUM, 0, 0, \
453 (vss_hlpr_fn)&stats_vss_numeric_hlpr, NULL)
455 #define STATS_VSS_MAX() STATS_VSS(VS_STYPE_MAX, 0, 0, \
456 (vss_hlpr_fn)&stats_vss_numeric_hlpr, NULL)
458 #define STATS_VSS_MIN() STATS_VSS(VS_STYPE_MIN, 0, 0, \
459 (vss_hlpr_fn)&stats_vss_numeric_hlpr, NULL)
461 #define STATS_VSS_HIST(htype, hist_hlpr_info) STATS_VSS(VS_STYPE_HIST, 0, \
462 htype, (vss_hlpr_fn)&stats_vss_hist_hlpr, \
463 (struct vss_hlpr_info *)(hist_hlpr_info))
465 #define STATS_VSS_TDIGEST(tdtype, tdgst_hlpr_info) STATS_VSS(VS_STYPE_TDGST, \
466 0, tdtype, (vss_hlpr_fn)&stats_vss_tdgst_hlpr, \
467 (struct vss_hlpr_info *)(tdgst_hlpr_info))
469 #define TDGST_NCTRS2VSDSZ(tdtype, nctds) (sizeof(struct voistatdata_##tdtype) + \
470 ((nctds) * sizeof(TYPEOF_MEMBER_PTR(struct voistatdata_##tdtype, \
471 ctdtree.arb_nodes))))
473 #define TDGST_HLPR_INFO(dt, nc, nf) \
474 (&(struct vss_tdgst_hlpr_info){ \
475 .tdgst_dtype = (dt), \
480 #define STATS_VSS_TDGSTCLUST32(nctds, prec) \
481 STATS_VSS_TDIGEST(VSD_DTYPE_TDGSTCLUST32, \
482 TDGST_HLPR_INFO(VSD_DTYPE_TDGSTCLUST32, nctds, prec))
484 #define STATS_VSS_TDGSTCLUST64(nctds, prec) \
485 STATS_VSS_TDIGEST(VSD_DTYPE_TDGSTCLUST64, \
486 TDGST_HLPR_INFO(VSD_DTYPE_TDGSTCLUST64, nctds, prec))
488 #define HIST_VSDSZ2NBKTS(htype, dsz) \
489 ((dsz - sizeof(struct voistatdata_##htype)) / \
490 sizeof(TYPEOF_MEMBER(struct voistatdata_##htype, bkts[0])))
492 #define HIST_NBKTS2VSDSZ(htype, nbkts) (sizeof(struct voistatdata_##htype) + \
493 ((nbkts) * sizeof(TYPEOF_MEMBER_PTR(struct voistatdata_##htype, bkts))))
495 #define HIST_HLPR_INFO_LIN_FIELDS(si) .lin.stepinc = (si)
497 #define HIST_HLPR_INFO_EXP_FIELDS(sb, se) \
498 .exp.stepbase = (sb), .exp.stepexp = (se)
500 #define HIST_HLPR_INFO_LINEXP_FIELDS(nss, sb) \
501 .linexp.linstepdiv = (nss), .linexp.stepbase = (sb)
503 #define HIST_HLPR_INFO_USR_FIELDS(bbs) \
504 .usr.bkts = (TYPEOF_MEMBER(struct vss_hist_hlpr_info, usr.bkts))(bbs), \
505 .usr.nbkts = (sizeof(bbs) / sizeof(struct voistatdata_numeric[2]))
507 #define HIST_HLPR_INFO(dt, sch, f, lbd, ubd, bkthlpr_fields) \
508 (&(struct vss_hist_hlpr_info){ \
510 .hist_dtype = (dt), \
512 .lb = stats_ctor_vsd_numeric(lbd), \
513 .ub = stats_ctor_vsd_numeric(ubd), \
517 #define STATS_VSS_CRHIST32_LIN(lb, ub, stepinc, vsdflags) \
518 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
519 BKT_LIN, vsdflags, lb, ub, HIST_HLPR_INFO_LIN_FIELDS(stepinc)))
520 #define STATS_VSS_CRHIST64_LIN(lb, ub, stepinc, vsdflags) \
521 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
522 BKT_LIN, vsdflags, lb, ub, HIST_HLPR_INFO_LIN_FIELDS(stepinc)))
524 #define STATS_VSS_CRHIST32_EXP(lb, ub, stepbase, stepexp, vsdflags) \
525 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
526 BKT_EXP, vsdflags, lb, ub, HIST_HLPR_INFO_EXP_FIELDS(stepbase, stepexp)))
527 #define STATS_VSS_CRHIST64_EXP(lb, ub, stepbase, stepexp, vsdflags) \
528 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
529 BKT_EXP, vsdflags, lb, ub, HIST_HLPR_INFO_EXP_FIELDS(stepbase, stepexp)))
531 #define STATS_VSS_CRHIST32_LINEXP(lb, ub, nlinsteps, stepbase, vsdflags) \
532 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
533 BKT_LINEXP, vsdflags, lb, ub, HIST_HLPR_INFO_LINEXP_FIELDS(nlinsteps, \
535 #define STATS_VSS_CRHIST64_LINEXP(lb, ub, nlinsteps, stepbase, vsdflags) \
536 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
537 BKT_LINEXP, vsdflags, lb, ub, HIST_HLPR_INFO_LINEXP_FIELDS(nlinsteps, \
540 #define STATS_VSS_CRHIST32_USR(bkts, vsdflags) \
541 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
542 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
543 #define STATS_VSS_CRHIST64_USR(bkts, vsdflags) \
544 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
545 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
547 #define STATS_VSS_DRHIST32_USR(bkts, vsdflags) \
548 STATS_VSS_HIST(VSD_DTYPE_DRHIST32, HIST_HLPR_INFO(VSD_DTYPE_DRHIST32, \
549 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
550 #define STATS_VSS_DRHIST64_USR(bkts, vsdflags) \
551 STATS_VSS_HIST(VSD_DTYPE_DRHIST64, HIST_HLPR_INFO(VSD_DTYPE_DRHIST64, \
552 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
554 #define STATS_VSS_DVHIST32_USR(vals, vsdflags) \
555 STATS_VSS_HIST(VSD_DTYPE_DVHIST32, HIST_HLPR_INFO(VSD_DTYPE_DVHIST32, \
556 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(vals)))
557 #define STATS_VSS_DVHIST64_USR(vals, vsdflags) \
558 STATS_VSS_HIST(VSD_DTYPE_DVHIST64, HIST_HLPR_INFO(VSD_DTYPE_DVHIST64, \
559 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(vals)))
560 #define DRBKT(lb, ub) { stats_ctor_vsd_numeric(lb), stats_ctor_vsd_numeric(ub) }
561 #define DVBKT(val) DRBKT(val, val)
562 #define CRBKT(lb) DRBKT(lb, lb)
563 #define HBKTS(...) ((struct voistatdata_numeric [][2]){__VA_ARGS__})
565 #define VSD_HIST_FIELD(hist, cnst, hist_dtype, op, field) \
566 (VSD_DTYPE_CRHIST32 == (hist_dtype) ? \
567 op(_VSD(cnst, crhist32, hist)->field) : \
568 (VSD_DTYPE_DRHIST32 == (hist_dtype) ? \
569 op(_VSD(cnst, drhist32, hist)->field) : \
570 (VSD_DTYPE_DVHIST32 == (hist_dtype) ? \
571 op(_VSD(cnst, dvhist32, hist)->field) : \
572 (VSD_DTYPE_CRHIST64 == (hist_dtype) ? \
573 op(_VSD(cnst, crhist64, hist)->field) : \
574 (VSD_DTYPE_DRHIST64 == (hist_dtype) ? \
575 op(_VSD(cnst, drhist64, hist)->field) : \
576 (op(_VSD(cnst, dvhist64, hist)->field)))))))
577 #define VSD_HIST_FIELDVAL(hist, hist_dtype, field) \
578 VSD_HIST_FIELD(hist, , hist_dtype, ,field)
579 #define VSD_CONSTHIST_FIELDVAL(hist, hist_dtype, field) \
580 VSD_HIST_FIELD(hist, const, hist_dtype, ,field)
581 #define VSD_HIST_FIELDPTR(hist, hist_dtype, field) \
582 VSD_HIST_FIELD(hist, , hist_dtype, (void *)&,field)
583 #define VSD_CONSTHIST_FIELDPTR(hist, hist_dtype, field) \
584 VSD_HIST_FIELD(hist, const, hist_dtype, (void *)&,field)
586 #define VSD_CRHIST_FIELD(hist, cnst, hist_dtype, op, field) \
587 (VSD_DTYPE_CRHIST32 == (hist_dtype) ? \
588 op(_VSD(cnst, crhist32, hist)->field) : \
589 op(_VSD(cnst, crhist64, hist)->field))
590 #define VSD_CRHIST_FIELDVAL(hist, hist_dtype, field) \
591 VSD_CRHIST_FIELD(hist, , hist_dtype, , field)
592 #define VSD_CONSTCRHIST_FIELDVAL(hist, hist_dtype, field) \
593 VSD_CRHIST_FIELD(hist, const, hist_dtype, , field)
594 #define VSD_CRHIST_FIELDPTR(hist, hist_dtype, field) \
595 VSD_CRHIST_FIELD(hist, , hist_dtype, &, field)
596 #define VSD_CONSTCRHIST_FIELDPTR(hist, hist_dtype, field) \
597 VSD_CRHIST_FIELD(hist, const, hist_dtype, &, field)
599 #define VSD_DRHIST_FIELD(hist, cnst, hist_dtype, op, field) \
600 (VSD_DTYPE_DRHIST32 == (hist_dtype) ? \
601 op(_VSD(cnst, drhist32, hist)->field) : \
602 op(_VSD(cnst, drhist64, hist)->field))
603 #define VSD_DRHIST_FIELDVAL(hist, hist_dtype, field) \
604 VSD_DRHIST_FIELD(hist, , hist_dtype, , field)
605 #define VSD_CONSTDRHIST_FIELDVAL(hist, hist_dtype, field) \
606 VSD_DRHIST_FIELD(hist, const, hist_dtype, , field)
607 #define VSD_DRHIST_FIELDPTR(hist, hist_dtype, field) \
608 VSD_DRHIST_FIELD(hist, , hist_dtype, &, field)
609 #define VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype, field) \
610 VSD_DRHIST_FIELD(hist, const, hist_dtype, &, field)
612 #define VSD_DVHIST_FIELD(hist, cnst, hist_dtype, op, field) \
613 (VSD_DTYPE_DVHIST32 == (hist_dtype) ? \
614 op(_VSD(cnst, dvhist32, hist)->field) : \
615 op(_VSD(cnst, dvhist64, hist)->field))
616 #define VSD_DVHIST_FIELDVAL(hist, hist_dtype, field) \
617 VSD_DVHIST_FIELD(hist, , hist_dtype, , field)
618 #define VSD_CONSTDVHIST_FIELDVAL(hist, hist_dtype, field) \
619 VSD_DVHIST_FIELD(hist, const, hist_dtype, , field)
620 #define VSD_DVHIST_FIELDPTR(hist, hist_dtype, field) \
621 VSD_DVHIST_FIELD(hist, , hist_dtype, &, field)
622 #define VSD_CONSTDVHIST_FIELDPTR(hist, hist_dtype, field) \
623 VSD_DVHIST_FIELD(hist, const, hist_dtype, &, field)
625 #define STATS_ABI_V1 1
629 SB_UE = 0, /* Unknown endian. */
630 SB_LE, /* Little endian. */
631 SB_BE /* Big endian. */
641 } __aligned(sizeof(void *));
652 struct statsblob_tpl {
653 struct metablob *mb; /* Template metadata */
654 struct statsblob *sb; /* Template schema */
657 struct stats_tpl_sample_rate {
658 /* XXXLAS: Storing slot_id assumes templates are never removed. */
660 uint32_t tpl_sample_pct;
663 /* Template sample rates list management callback actions. */
664 enum stats_tpl_sr_cb_action {
672 * Callback function pointer passed as arg1 to stats_tpl_sample_rates(). ctx is
673 * a heap-allocated, zero-initialised blob of contextual memory valid during a
674 * single stats_tpl_sample_rates() call and sized per the value passed as arg2.
675 * Returns 0 on success, an errno on error.
676 * - When called with "action == TPL_SR_*_GET", return the subsystem's rates
677 * list ptr and count, locked or unlocked as requested.
678 * - When called with "action == TPL_SR_RUNLOCK", unlock the subsystem's rates
679 * list ptr and count. Pair with a prior "action == TPL_SR_RLOCKED_GET" call.
680 * - When called with "action == TPL_SR_PUT, update the subsystem's rates list
681 * ptr and count to the sysctl processed values and return the inactive list
682 * details in rates/nrates for garbage collection by stats_tpl_sample_rates().
684 typedef int (*stats_tpl_sr_cb_t)(enum stats_tpl_sr_cb_action action,
685 struct stats_tpl_sample_rate **rates, int *nrates, void *ctx);
687 /* Flags related to iterating over a stats blob. */
688 #define SB_IT_FIRST_CB 0x0001
689 #define SB_IT_LAST_CB 0x0002
690 #define SB_IT_FIRST_VOI 0x0004
691 #define SB_IT_LAST_VOI 0x0008
692 #define SB_IT_FIRST_VOISTAT 0x0010
693 #define SB_IT_LAST_VOISTAT 0x0020
694 #define SB_IT_NULLVOI 0x0040
695 #define SB_IT_NULLVOISTAT 0x0080
698 struct voistatdata *vs_data;
704 enum vsd_dtype voi_dtype : 8;
705 enum vsd_dtype vs_dtype : 8;
709 /* Stats blob iterator callback called for each struct voi. */
710 typedef int (*stats_blob_visitcb_t)(struct sb_visit *sbv, void *usrctx);
712 /* ABI specific functions. */
713 int stats_v1_tpl_alloc(const char *name, uint32_t flags);
714 int stats_v1_tpl_add_voistats(uint32_t tpl_id, int32_t voi_id,
715 const char *voi_name, enum vsd_dtype voi_dtype, uint32_t nvss,
716 struct voistatspec *vss, uint32_t flags);
717 int stats_v1_blob_init(struct statsblobv1 *sb, uint32_t tpl_id, uint32_t flags);
718 struct statsblobv1 * stats_v1_blob_alloc(uint32_t tpl_id, uint32_t flags);
719 int stats_v1_blob_clone(struct statsblobv1 **dst, size_t dstmaxsz,
720 struct statsblobv1 *src, uint32_t flags);
721 void stats_v1_blob_destroy(struct statsblobv1 *sb);
722 #define SB_CLONE_RSTSRC 0x0001 /* Reset src blob if clone successful. */
723 #define SB_CLONE_ALLOCDST 0x0002 /* Allocate src->cursz memory for dst. */
724 #define SB_CLONE_USRDSTNOFAULT 0x0004 /* Clone to wired userspace dst. */
725 #define SB_CLONE_USRDST 0x0008 /* Clone to unwired userspace dst. */
726 int stats_v1_blob_snapshot(struct statsblobv1 **dst, size_t dstmaxsz,
727 struct statsblobv1 *src, uint32_t flags);
728 #define SB_TOSTR_OBJDUMP 0x00000001
729 #define SB_TOSTR_META 0x00000002 /* Lookup metablob and render metadata */
730 int stats_v1_blob_tostr(struct statsblobv1 *sb, struct sbuf *buf,
731 enum sb_str_fmt fmt, uint32_t flags);
732 int stats_v1_blob_visit(struct statsblobv1 *sb, stats_blob_visitcb_t func,
734 /* VOI related function flags. */
735 #define SB_VOI_RELUPDATE 0x00000001 /* voival is relative to previous value. */
736 int stats_v1_voi_update(struct statsblobv1 *sb, int32_t voi_id,
737 enum vsd_dtype voi_dtype, struct voistatdata *voival, uint32_t flags);
738 int stats_v1_voistat_fetch_dptr(struct statsblobv1 *sb, int32_t voi_id,
739 enum voi_stype stype, enum vsd_dtype *retdtype, struct voistatdata **retvsd,
742 /* End ABI specific functions. */
744 /* ABI agnostic functions. */
745 int stats_vss_hlpr_init(enum vsd_dtype voi_dtype, uint32_t nvss,
746 struct voistatspec *vss);
747 void stats_vss_hlpr_cleanup(uint32_t nvss, struct voistatspec *vss);
748 int stats_vss_hist_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
749 struct vss_hist_hlpr_info *info);
750 int stats_vss_numeric_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
751 struct vss_numeric_hlpr_info *info);
752 int stats_vss_tdgst_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
753 struct vss_tdgst_hlpr_info *info);
754 int stats_tpl_fetch(int tpl_id, struct statsblob_tpl **tpl);
755 int stats_tpl_fetch_allocid(const char *name, uint32_t hash);
756 int stats_tpl_id2name(uint32_t tpl_id, char *buf, size_t len);
757 int stats_tpl_sample_rates(struct sysctl_oid *oidp, void *arg1, intmax_t arg2,
758 struct sysctl_req *req);
759 int stats_tpl_sample_rollthedice(struct stats_tpl_sample_rate *rates,
760 int nrates, void *seed_bytes, size_t seed_len);
761 int stats_voistatdata_tostr(const struct voistatdata *vsd,
762 enum vsd_dtype voi_dtype, enum vsd_dtype vsd_dtype, size_t vsd_sz,
763 enum sb_str_fmt fmt, struct sbuf *buf, int objdump);
765 static inline struct voistatdata_numeric
766 stats_ctor_vsd_numeric(uint64_t val)
768 struct voistatdata_numeric tmp;
776 stats_tpl_alloc(const char *name, uint32_t flags)
779 return (stats_v1_tpl_alloc(name, flags));
783 stats_tpl_add_voistats(uint32_t tpl_id, int32_t voi_id, const char *voi_name,
784 enum vsd_dtype voi_dtype, uint32_t nvss, struct voistatspec *vss,
789 if ((ret = stats_vss_hlpr_init(voi_dtype, nvss, vss)) == 0) {
790 ret = stats_v1_tpl_add_voistats(tpl_id, voi_id, voi_name,
791 voi_dtype, nvss, vss, flags);
793 stats_vss_hlpr_cleanup(nvss, vss);
799 stats_blob_init(struct statsblob *sb, uint32_t tpl_id, uint32_t flags)
802 return (stats_v1_blob_init((struct statsblobv1 *)sb, tpl_id, flags));
805 static inline struct statsblob *
806 stats_blob_alloc(uint32_t tpl_id, uint32_t flags)
809 return ((struct statsblob *)stats_v1_blob_alloc(tpl_id, flags));
813 stats_blob_clone(struct statsblob **dst, size_t dstmaxsz, struct statsblob *src,
817 return (stats_v1_blob_clone((struct statsblobv1 **)dst, dstmaxsz,
818 (struct statsblobv1 *)src, flags));
822 stats_blob_destroy(struct statsblob *sb)
825 stats_v1_blob_destroy((struct statsblobv1 *)sb);
829 stats_blob_visit(struct statsblob *sb, stats_blob_visitcb_t func, void *usrctx)
832 return (stats_v1_blob_visit((struct statsblobv1 *)sb, func, usrctx));
836 stats_blob_tostr(struct statsblob *sb, struct sbuf *buf,
837 enum sb_str_fmt fmt, uint32_t flags)
840 return (stats_v1_blob_tostr((struct statsblobv1 *)sb, buf, fmt, flags));
844 stats_voistat_fetch_dptr(struct statsblob *sb, int32_t voi_id,
845 enum voi_stype stype, enum vsd_dtype *retdtype, struct voistatdata **retvsd,
849 return (stats_v1_voistat_fetch_dptr((struct statsblobv1 *)sb,
850 voi_id, stype, retdtype, retvsd, retvsdsz));
854 stats_voistat_fetch_s64(struct statsblob *sb, int32_t voi_id,
855 enum voi_stype stype, int64_t *ret)
857 struct voistatdata *vsd;
858 enum vsd_dtype vs_dtype;
861 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
864 else if (VSD_DTYPE_INT_S64 != vs_dtype)
867 *ret = vsd->int64.s64;
872 stats_voistat_fetch_u64(struct statsblob *sb, int32_t voi_id,
873 enum voi_stype stype, uint64_t *ret)
875 struct voistatdata *vsd;
876 enum vsd_dtype vs_dtype;
879 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
882 else if (VSD_DTYPE_INT_U64 != vs_dtype)
885 *ret = vsd->int64.u64;
890 stats_voistat_fetch_s32(struct statsblob *sb, int32_t voi_id,
891 enum voi_stype stype, int32_t *ret)
893 struct voistatdata *vsd;
894 enum vsd_dtype vs_dtype;
897 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
900 else if (VSD_DTYPE_INT_S32 != vs_dtype)
903 *ret = vsd->int32.s32;
908 stats_voistat_fetch_u32(struct statsblob *sb, int32_t voi_id,
909 enum voi_stype stype, uint32_t *ret)
911 struct voistatdata *vsd;
912 enum vsd_dtype vs_dtype;
915 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
918 else if (VSD_DTYPE_INT_U32 != vs_dtype)
921 *ret = vsd->int32.u32;
926 stats_voistat_fetch_slong(struct statsblob *sb, int32_t voi_id,
927 enum voi_stype stype, long *ret)
929 struct voistatdata *vsd;
930 enum vsd_dtype vs_dtype;
933 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
936 else if (VSD_DTYPE_INT_SLONG != vs_dtype)
939 *ret = vsd->intlong.slong;
944 stats_voistat_fetch_ulong(struct statsblob *sb, int32_t voi_id,
945 enum voi_stype stype, unsigned long *ret)
947 struct voistatdata *vsd;
948 enum vsd_dtype vs_dtype;
951 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
954 else if (VSD_DTYPE_INT_ULONG != vs_dtype)
957 *ret = vsd->intlong.ulong;
962 stats_blob_snapshot(struct statsblob **dst, size_t dstmaxsz,
963 struct statsblob *src, uint32_t flags)
966 return (stats_v1_blob_snapshot((struct statsblobv1 **)dst, dstmaxsz,
967 (struct statsblobv1 *)src, flags));
971 stats_voi_update_abs_s32(struct statsblob *sb, int32_t voi_id, int32_t voival)
977 struct voistatdata tmp;
978 tmp.int32.s32 = voival;
980 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
981 VSD_DTYPE_INT_S32, &tmp, 0));
985 stats_voi_update_rel_s32(struct statsblob *sb, int32_t voi_id, int32_t voival)
991 struct voistatdata tmp;
992 tmp.int32.s32 = voival;
994 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
995 VSD_DTYPE_INT_S32, &tmp, SB_VOI_RELUPDATE));
999 stats_voi_update_abs_u32(struct statsblob *sb, int32_t voi_id, uint32_t voival)
1005 struct voistatdata tmp;
1006 tmp.int32.u32 = voival;
1008 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1009 VSD_DTYPE_INT_U32, &tmp, 0));
1013 stats_voi_update_rel_u32(struct statsblob *sb, int32_t voi_id, uint32_t voival)
1019 struct voistatdata tmp;
1020 tmp.int32.u32 = voival;
1022 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1023 VSD_DTYPE_INT_U32, &tmp, SB_VOI_RELUPDATE));
1027 stats_voi_update_abs_s64(struct statsblob *sb, int32_t voi_id, int64_t voival)
1033 struct voistatdata tmp;
1034 tmp.int64.s64 = voival;
1036 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1037 VSD_DTYPE_INT_S64, &tmp, 0));
1041 stats_voi_update_rel_s64(struct statsblob *sb, int32_t voi_id, int64_t voival)
1047 struct voistatdata tmp;
1048 tmp.int64.s64 = voival;
1050 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1051 VSD_DTYPE_INT_S64, &tmp, SB_VOI_RELUPDATE));
1055 stats_voi_update_abs_u64(struct statsblob *sb, int32_t voi_id, uint64_t voival)
1061 struct voistatdata tmp;
1062 tmp.int64.u64 = voival;
1064 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1065 VSD_DTYPE_INT_U64, &tmp, 0));
1069 stats_voi_update_rel_u64(struct statsblob *sb, int32_t voi_id, uint64_t voival)
1075 struct voistatdata tmp;
1076 tmp.int64.u64 = voival;
1078 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1079 VSD_DTYPE_INT_U64, &tmp, SB_VOI_RELUPDATE));
1083 stats_voi_update_abs_slong(struct statsblob *sb, int32_t voi_id, long voival)
1089 struct voistatdata tmp;
1090 tmp.intlong.slong = voival;
1092 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1093 VSD_DTYPE_INT_SLONG, &tmp, 0));
1097 stats_voi_update_rel_slong(struct statsblob *sb, int32_t voi_id, long voival)
1103 struct voistatdata tmp;
1104 tmp.intlong.slong = voival;
1106 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1107 VSD_DTYPE_INT_SLONG, &tmp, SB_VOI_RELUPDATE));
1111 stats_voi_update_abs_ulong(struct statsblob *sb, int32_t voi_id,
1112 unsigned long voival)
1118 struct voistatdata tmp;
1119 tmp.intlong.ulong = voival;
1121 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1122 VSD_DTYPE_INT_ULONG, &tmp, 0));
1126 stats_voi_update_rel_ulong(struct statsblob *sb, int32_t voi_id,
1127 unsigned long voival)
1133 struct voistatdata tmp;
1134 tmp.intlong.ulong = voival;
1136 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1137 VSD_DTYPE_INT_ULONG, &tmp, SB_VOI_RELUPDATE));
1141 stats_voi_update_abs_sq32(struct statsblob *sb, int32_t voi_id, s32q_t voival)
1147 struct voistatdata tmp;
1148 tmp.q32.sq32 = voival;
1150 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1151 VSD_DTYPE_Q_S32, &tmp, 0));
1155 stats_voi_update_rel_sq32(struct statsblob *sb, int32_t voi_id, s32q_t voival)
1161 struct voistatdata tmp;
1162 tmp.q32.sq32 = voival;
1164 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1165 VSD_DTYPE_Q_S32, &tmp, SB_VOI_RELUPDATE));
1169 stats_voi_update_abs_uq32(struct statsblob *sb, int32_t voi_id, u32q_t voival)
1175 struct voistatdata tmp;
1176 tmp.q32.uq32 = voival;
1178 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1179 VSD_DTYPE_Q_U32, &tmp, 0));
1183 stats_voi_update_rel_uq32(struct statsblob *sb, int32_t voi_id, u32q_t voival)
1189 struct voistatdata tmp;
1190 tmp.q32.uq32 = voival;
1192 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1193 VSD_DTYPE_Q_U32, &tmp, SB_VOI_RELUPDATE));
1197 stats_voi_update_abs_sq64(struct statsblob *sb, int32_t voi_id, s64q_t voival)
1203 struct voistatdata tmp;
1204 tmp.q64.sq64 = voival;
1206 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1207 VSD_DTYPE_Q_S64, &tmp, 0));
1211 stats_voi_update_rel_sq64(struct statsblob *sb, int32_t voi_id, s64q_t voival)
1217 struct voistatdata tmp;
1218 tmp.q64.sq64 = voival;
1220 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1221 VSD_DTYPE_Q_S64, &tmp, SB_VOI_RELUPDATE));
1225 stats_voi_update_abs_uq64(struct statsblob *sb, int32_t voi_id, u64q_t voival)
1231 struct voistatdata tmp;
1232 tmp.q64.uq64 = voival;
1234 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1235 VSD_DTYPE_Q_U64, &tmp, 0));
1239 stats_voi_update_rel_uq64(struct statsblob *sb, int32_t voi_id, u64q_t voival)
1245 struct voistatdata tmp;
1246 tmp.q64.uq64 = voival;
1248 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1249 VSD_DTYPE_Q_U64, &tmp, SB_VOI_RELUPDATE));
1252 /* End ABI agnostic functions. */
1254 #endif /* _SYS_STATS_H_ */