2 * Copyright (c) 2014-2018 Netflix, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * A kernel and user space statistics gathering API + infrastructure.
30 * Author: Lawrence Stewart <lstewart@netflix.com>
33 * - Register callbacks for events e.g. counter stat passing a threshold
35 * - How could this become SIFTRv2? Perhaps publishing records to a ring
36 * mapped between userspace and kernel?
38 * - Potential stat types:
39 * RATE: events per unit time
40 * TIMESERIES: timestamped records. Stored in voistate?
41 * EWMA: Exponential weighted moving average.
43 * - How should second order stats work e.g. stat "A" depends on "B"
45 * - How do variable time windows work e.g. give me per-RTT stats
47 * - Should the API always require the caller to manage locking? Or should the
48 * API provide optional functionality to lock a blob during operations.
50 * - Should we continue to store unpacked naturally aligned structs in the
51 * blob or move to packed structs? Relates to inter-host
52 * serialisation/endian issues.
58 #include <sys/limits.h>
65 * XXXLAS: Hacks to enable sharing template creation code between kernel and
66 * userland e.g. tcp_stats.c
69 #define VNET_DEFINE(t, n) static t n __unused
70 #endif /* ! _KERNEL */
72 #define TPL_MAX_NAME_LEN 64
75 * The longest template string spec format i.e. the normative spec format, is:
77 * "<tplname>":<tplhash>
79 * Therefore, the max string length of a template string spec is:
83 * - 1 char for : separating name and hash
84 * - 10 chars for 32bit hash
86 #define STATS_TPL_MAX_STR_SPEC_LEN (TPL_MAX_NAME_LEN + 13)
93 SB_STRFMT_FREEFORM = 0,
95 SB_STRFMT_NUM_FMTS /* +1 to highest numbered format type. */
100 VS_STYPE_VOISTATE = 0, /* Reserved for internal API use. */
106 VS_NUM_STYPES /* +1 to highest numbered stat type. */
110 * VOI stat data types used as storage for certain stat types and to marshall
111 * data through various API calls.
114 VSD_DTYPE_VOISTATE = 0, /* Reserved for internal API use. */
115 VSD_DTYPE_INT_S32, /* int32_t */
116 VSD_DTYPE_INT_U32, /* uint32_t */
117 VSD_DTYPE_INT_S64, /* int64_t */
118 VSD_DTYPE_INT_U64, /* uint64_t */
119 VSD_DTYPE_INT_SLONG, /* long */
120 VSD_DTYPE_INT_ULONG, /* unsigned long */
121 VSD_DTYPE_Q_S32, /* s32q_t */
122 VSD_DTYPE_Q_U32, /* u32q_t */
123 VSD_DTYPE_Q_S64, /* s64q_t */
124 VSD_DTYPE_Q_U64, /* u64q_t */
125 VSD_DTYPE_CRHIST32, /* continuous range histogram, 32bit buckets */
126 VSD_DTYPE_DRHIST32, /* discrete range histogram, 32bit buckets */
127 VSD_DTYPE_DVHIST32, /* discrete value histogram, 32bit buckets */
128 VSD_DTYPE_CRHIST64, /* continuous range histogram, 64bit buckets */
129 VSD_DTYPE_DRHIST64, /* discrete range histogram, 64bit buckets */
130 VSD_DTYPE_DVHIST64, /* discrete value histogram, 64bit buckets */
131 VSD_DTYPE_TDGSTCLUST32, /* clustering variant t-digest, 32bit buckets */
132 VSD_DTYPE_TDGSTCLUST64, /* clustering variant t-digest, 64bit buckets */
133 VSD_NUM_DTYPES /* +1 to highest numbered data type. */
136 struct voistatdata_int32 {
143 struct voistatdata_int64 {
147 //counter_u64_t u64pcpu;
151 struct voistatdata_intlong {
158 struct voistatdata_q32 {
165 struct voistatdata_q64 {
172 struct voistatdata_numeric {
175 #if BYTE_ORDER == BIG_ENDIAN
182 #if BYTE_ORDER == LITTLE_ENDIAN
188 #if BYTE_ORDER == BIG_ENDIAN
195 #if BYTE_ORDER == LITTLE_ENDIAN
201 #if BYTE_ORDER == BIG_ENDIAN && LONG_BIT == 32
208 #if BYTE_ORDER == LITTLE_ENDIAN && LONG_BIT == 32
213 struct voistatdata_int64 int64;
214 struct voistatdata_q64 q64;
218 /* Continuous range histogram with 32bit buckets. */
219 struct voistatdata_crhist32 {
222 struct voistatdata_numeric lb;
227 /* Continuous range histogram with 64bit buckets. */
228 struct voistatdata_crhist64 {
231 struct voistatdata_numeric lb;
236 /* Discrete range histogram with 32bit buckets. */
237 struct voistatdata_drhist32 {
240 struct voistatdata_numeric lb, ub;
245 /* Discrete range histogram with 64bit buckets. */
246 struct voistatdata_drhist64 {
249 struct voistatdata_numeric lb, ub;
254 /* Discrete value histogram with 32bit buckets. */
255 struct voistatdata_dvhist32 {
258 struct voistatdata_numeric val;
263 /* Discrete value histogram with 64bit buckets. */
264 struct voistatdata_dvhist64 {
267 struct voistatdata_numeric val;
272 struct voistatdata_hist {
274 struct voistatdata_crhist32 crhist32;
275 struct voistatdata_crhist64 crhist64;
276 struct voistatdata_dvhist32 dvhist32;
277 struct voistatdata_dvhist64 dvhist64;
278 struct voistatdata_drhist32 drhist32;
279 struct voistatdata_drhist64 drhist64;
283 struct voistatdata_tdgstctd32 {
284 ARB16_ENTRY() ctdlnk;
286 RB_ENTRY(voistatdata_tdgstctd32) rblnk;
292 struct voistatdata_tdgstctd64 {
293 ARB16_ENTRY() ctdlnk;
295 RB_ENTRY(voistatdata_tdgstctd64) rblnk;
301 struct voistatdata_tdgstctd {
303 struct voistatdata_tdgstctd32 tdgstctd32;
304 struct voistatdata_tdgstctd64 tdgstctd64;
308 /* Clustering variant, fixed-point t-digest with 32bit mu/counts. */
309 struct voistatdata_tdgstclust32 {
310 uint32_t smplcnt; /* Count of samples. */
311 uint32_t compcnt; /* Count of digest compressions. */
313 RB_HEAD(rbctdth32, voistatdata_tdgstctd32) rbctdtree;
315 /* Array-based red-black tree of centroids. */
316 ARB16_HEAD(ctdth32, voistatdata_tdgstctd32) ctdtree;
319 /* Clustering variant, fixed-point t-digest with 64bit mu/counts. */
320 struct voistatdata_tdgstclust64 {
321 uint64_t smplcnt; /* Count of samples. */
322 uint32_t compcnt; /* Count of digest compressions. */
324 RB_HEAD(rbctdth64, voistatdata_tdgstctd64) rbctdtree;
326 /* Array-based red-black tree of centroids. */
327 ARB16_HEAD(ctdth64, voistatdata_tdgstctd64) ctdtree;
330 struct voistatdata_tdgst {
332 struct voistatdata_tdgstclust32 tdgstclust32;
333 struct voistatdata_tdgstclust64 tdgstclust64;
339 struct voistatdata_int32 int32;
340 struct voistatdata_int64 int64;
341 struct voistatdata_intlong intlong;
342 struct voistatdata_q32 q32;
343 struct voistatdata_q64 q64;
344 struct voistatdata_crhist32 crhist32;
345 struct voistatdata_crhist64 crhist64;
346 struct voistatdata_dvhist32 dvhist32;
347 struct voistatdata_dvhist64 dvhist64;
348 struct voistatdata_drhist32 drhist32;
349 struct voistatdata_drhist64 drhist64;
350 struct voistatdata_tdgstclust32 tdgstclust32;
351 struct voistatdata_tdgstclust64 tdgstclust64;
355 #define VSD_HIST_LBOUND_INF 0x01
356 #define VSD_HIST_UBOUND_INF 0x02
357 struct vss_hist_hlpr_info {
358 enum hist_bkt_alloc {
359 BKT_LIN, /* Linear steps. */
360 BKT_EXP, /* Exponential steps. */
361 BKT_LINEXP, /* Exponential steps, linear sub-steps. */
362 BKT_USR /* User specified buckets. */
364 enum vsd_dtype voi_dtype;
365 enum vsd_dtype hist_dtype;
367 struct voistatdata_numeric lb;
368 struct voistatdata_numeric ub;
371 const uint64_t stepinc;
374 const uint64_t stepbase;
375 const uint64_t stepexp;
378 const uint64_t stepbase;
379 const uint64_t linstepdiv;
382 const uint16_t nbkts;
384 struct voistatdata_numeric lb, ub;
390 struct vss_tdgst_hlpr_info {
391 enum vsd_dtype voi_dtype;
392 enum vsd_dtype tdgst_dtype;
395 } __aligned(sizeof(void *));
397 struct vss_numeric_hlpr_info {
401 struct vss_hlpr_info {
403 struct vss_tdgst_hlpr_info tdgst;
404 struct vss_hist_hlpr_info hist;
405 struct vss_numeric_hlpr_info numeric;
410 typedef int (*vss_hlpr_fn)(enum vsd_dtype, struct voistatspec *,
411 struct vss_hlpr_info *);
414 vss_hlpr_fn hlpr; /* iv helper function. */
415 struct vss_hlpr_info *hlprinfo; /* Helper function context. */
416 struct voistatdata *iv; /* Initialisation value. */
417 size_t vsdsz; /* Size of iv. */
418 uint32_t flags; /* Stat flags. */
419 enum vsd_dtype vs_dtype : 8; /* Stat's dtype. */
420 enum voi_stype stype : 8; /* Stat type. */
423 extern const char *vs_stype2name[VS_NUM_STYPES];
424 extern const char *vs_stype2desc[VS_NUM_STYPES];
425 extern const char *vsd_dtype2name[VSD_NUM_DTYPES];
426 extern const size_t vsd_dtype2size[VSD_NUM_DTYPES];
429 extern const struct voistatdata_numeric numeric_limits[2][VSD_DTYPE_Q_U64 + 1];
431 #define TYPEOF_MEMBER(type, member) __typeof(((type *)0)->member)
432 #define TYPEOF_MEMBER_PTR(type, member) __typeof(*(((type *)0)->member))
433 #define SIZEOF_MEMBER(type, member) sizeof(TYPEOF_MEMBER(type, member))
435 /* Cast a pointer to a voistatdata struct of requested type. */
436 #define _VSD(cnst, type, ptr) ((cnst struct voistatdata_##type *)(ptr))
437 #define VSD(type, ptr) _VSD(, type, ptr)
438 #define CONSTVSD(type, ptr) _VSD(const, type, ptr)
440 #define NVSS(vss_slots) (sizeof((vss_slots)) / sizeof(struct voistatspec))
441 #define STATS_VSS(st, vsf, dt, hlp, hlpi) \
442 ((struct voistatspec){ \
447 .hlprinfo = (hlpi), \
450 #define STATS_VSS_SUM() STATS_VSS(VS_STYPE_SUM, 0, 0, \
451 (vss_hlpr_fn)&stats_vss_numeric_hlpr, NULL)
453 #define STATS_VSS_MAX() STATS_VSS(VS_STYPE_MAX, 0, 0, \
454 (vss_hlpr_fn)&stats_vss_numeric_hlpr, NULL)
456 #define STATS_VSS_MIN() STATS_VSS(VS_STYPE_MIN, 0, 0, \
457 (vss_hlpr_fn)&stats_vss_numeric_hlpr, NULL)
459 #define STATS_VSS_HIST(htype, hist_hlpr_info) STATS_VSS(VS_STYPE_HIST, 0, \
460 htype, (vss_hlpr_fn)&stats_vss_hist_hlpr, \
461 (struct vss_hlpr_info *)(hist_hlpr_info))
463 #define STATS_VSS_TDIGEST(tdtype, tdgst_hlpr_info) STATS_VSS(VS_STYPE_TDGST, \
464 0, tdtype, (vss_hlpr_fn)&stats_vss_tdgst_hlpr, \
465 (struct vss_hlpr_info *)(tdgst_hlpr_info))
467 #define TDGST_NCTRS2VSDSZ(tdtype, nctds) (sizeof(struct voistatdata_##tdtype) + \
468 ((nctds) * sizeof(TYPEOF_MEMBER_PTR(struct voistatdata_##tdtype, \
469 ctdtree.arb_nodes))))
471 #define TDGST_HLPR_INFO(dt, nc, nf) \
472 (&(struct vss_tdgst_hlpr_info){ \
473 .tdgst_dtype = (dt), \
478 #define STATS_VSS_TDGSTCLUST32(nctds, prec) \
479 STATS_VSS_TDIGEST(VSD_DTYPE_TDGSTCLUST32, \
480 TDGST_HLPR_INFO(VSD_DTYPE_TDGSTCLUST32, nctds, prec))
482 #define STATS_VSS_TDGSTCLUST64(nctds, prec) \
483 STATS_VSS_TDIGEST(VSD_DTYPE_TDGSTCLUST64, \
484 TDGST_HLPR_INFO(VSD_DTYPE_TDGSTCLUST64, nctds, prec))
486 #define HIST_VSDSZ2NBKTS(htype, dsz) \
487 ((dsz - sizeof(struct voistatdata_##htype)) / \
488 sizeof(TYPEOF_MEMBER(struct voistatdata_##htype, bkts[0])))
490 #define HIST_NBKTS2VSDSZ(htype, nbkts) (sizeof(struct voistatdata_##htype) + \
491 ((nbkts) * sizeof(TYPEOF_MEMBER_PTR(struct voistatdata_##htype, bkts))))
493 #define HIST_HLPR_INFO_LIN_FIELDS(si) .lin.stepinc = (si)
495 #define HIST_HLPR_INFO_EXP_FIELDS(sb, se) \
496 .exp.stepbase = (sb), .exp.stepexp = (se)
498 #define HIST_HLPR_INFO_LINEXP_FIELDS(nss, sb) \
499 .linexp.linstepdiv = (nss), .linexp.stepbase = (sb)
501 #define HIST_HLPR_INFO_USR_FIELDS(bbs) \
502 .usr.bkts = (TYPEOF_MEMBER(struct vss_hist_hlpr_info, usr.bkts))(bbs), \
503 .usr.nbkts = (sizeof(bbs) / sizeof(struct voistatdata_numeric[2]))
505 #define HIST_HLPR_INFO(dt, sch, f, lbd, ubd, bkthlpr_fields) \
506 (&(struct vss_hist_hlpr_info){ \
508 .hist_dtype = (dt), \
510 .lb = stats_ctor_vsd_numeric(lbd), \
511 .ub = stats_ctor_vsd_numeric(ubd), \
515 #define STATS_VSS_CRHIST32_LIN(lb, ub, stepinc, vsdflags) \
516 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
517 BKT_LIN, vsdflags, lb, ub, HIST_HLPR_INFO_LIN_FIELDS(stepinc)))
518 #define STATS_VSS_CRHIST64_LIN(lb, ub, stepinc, vsdflags) \
519 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
520 BKT_LIN, vsdflags, lb, ub, HIST_HLPR_INFO_LIN_FIELDS(stepinc)))
522 #define STATS_VSS_CRHIST32_EXP(lb, ub, stepbase, stepexp, vsdflags) \
523 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
524 BKT_EXP, vsdflags, lb, ub, HIST_HLPR_INFO_EXP_FIELDS(stepbase, stepexp)))
525 #define STATS_VSS_CRHIST64_EXP(lb, ub, stepbase, stepexp, vsdflags) \
526 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
527 BKT_EXP, vsdflags, lb, ub, HIST_HLPR_INFO_EXP_FIELDS(stepbase, stepexp)))
529 #define STATS_VSS_CRHIST32_LINEXP(lb, ub, nlinsteps, stepbase, vsdflags) \
530 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
531 BKT_LINEXP, vsdflags, lb, ub, HIST_HLPR_INFO_LINEXP_FIELDS(nlinsteps, \
533 #define STATS_VSS_CRHIST64_LINEXP(lb, ub, nlinsteps, stepbase, vsdflags) \
534 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
535 BKT_LINEXP, vsdflags, lb, ub, HIST_HLPR_INFO_LINEXP_FIELDS(nlinsteps, \
538 #define STATS_VSS_CRHIST32_USR(bkts, vsdflags) \
539 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
540 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
541 #define STATS_VSS_CRHIST64_USR(bkts, vsdflags) \
542 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
543 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
545 #define STATS_VSS_DRHIST32_USR(bkts, vsdflags) \
546 STATS_VSS_HIST(VSD_DTYPE_DRHIST32, HIST_HLPR_INFO(VSD_DTYPE_DRHIST32, \
547 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
548 #define STATS_VSS_DRHIST64_USR(bkts, vsdflags) \
549 STATS_VSS_HIST(VSD_DTYPE_DRHIST64, HIST_HLPR_INFO(VSD_DTYPE_DRHIST64, \
550 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
552 #define STATS_VSS_DVHIST32_USR(vals, vsdflags) \
553 STATS_VSS_HIST(VSD_DTYPE_DVHIST32, HIST_HLPR_INFO(VSD_DTYPE_DVHIST32, \
554 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(vals)))
555 #define STATS_VSS_DVHIST64_USR(vals, vsdflags) \
556 STATS_VSS_HIST(VSD_DTYPE_DVHIST64, HIST_HLPR_INFO(VSD_DTYPE_DVHIST64, \
557 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(vals)))
558 #define DRBKT(lb, ub) { stats_ctor_vsd_numeric(lb), stats_ctor_vsd_numeric(ub) }
559 #define DVBKT(val) DRBKT(val, val)
560 #define CRBKT(lb) DRBKT(lb, lb)
561 #define HBKTS(...) ((struct voistatdata_numeric [][2]){__VA_ARGS__})
563 #define VSD_HIST_FIELD(hist, cnst, hist_dtype, op, field) \
564 (VSD_DTYPE_CRHIST32 == (hist_dtype) ? \
565 op(_VSD(cnst, crhist32, hist)->field) : \
566 (VSD_DTYPE_DRHIST32 == (hist_dtype) ? \
567 op(_VSD(cnst, drhist32, hist)->field) : \
568 (VSD_DTYPE_DVHIST32 == (hist_dtype) ? \
569 op(_VSD(cnst, dvhist32, hist)->field) : \
570 (VSD_DTYPE_CRHIST64 == (hist_dtype) ? \
571 op(_VSD(cnst, crhist64, hist)->field) : \
572 (VSD_DTYPE_DRHIST64 == (hist_dtype) ? \
573 op(_VSD(cnst, drhist64, hist)->field) : \
574 (op(_VSD(cnst, dvhist64, hist)->field)))))))
575 #define VSD_HIST_FIELDVAL(hist, hist_dtype, field) \
576 VSD_HIST_FIELD(hist, , hist_dtype, ,field)
577 #define VSD_CONSTHIST_FIELDVAL(hist, hist_dtype, field) \
578 VSD_HIST_FIELD(hist, const, hist_dtype, ,field)
579 #define VSD_HIST_FIELDPTR(hist, hist_dtype, field) \
580 VSD_HIST_FIELD(hist, , hist_dtype, (void *)&,field)
581 #define VSD_CONSTHIST_FIELDPTR(hist, hist_dtype, field) \
582 VSD_HIST_FIELD(hist, const, hist_dtype, (void *)&,field)
584 #define VSD_CRHIST_FIELD(hist, cnst, hist_dtype, op, field) \
585 (VSD_DTYPE_CRHIST32 == (hist_dtype) ? \
586 op(_VSD(cnst, crhist32, hist)->field) : \
587 op(_VSD(cnst, crhist64, hist)->field))
588 #define VSD_CRHIST_FIELDVAL(hist, hist_dtype, field) \
589 VSD_CRHIST_FIELD(hist, , hist_dtype, , field)
590 #define VSD_CONSTCRHIST_FIELDVAL(hist, hist_dtype, field) \
591 VSD_CRHIST_FIELD(hist, const, hist_dtype, , field)
592 #define VSD_CRHIST_FIELDPTR(hist, hist_dtype, field) \
593 VSD_CRHIST_FIELD(hist, , hist_dtype, &, field)
594 #define VSD_CONSTCRHIST_FIELDPTR(hist, hist_dtype, field) \
595 VSD_CRHIST_FIELD(hist, const, hist_dtype, &, field)
597 #define VSD_DRHIST_FIELD(hist, cnst, hist_dtype, op, field) \
598 (VSD_DTYPE_DRHIST32 == (hist_dtype) ? \
599 op(_VSD(cnst, drhist32, hist)->field) : \
600 op(_VSD(cnst, drhist64, hist)->field))
601 #define VSD_DRHIST_FIELDVAL(hist, hist_dtype, field) \
602 VSD_DRHIST_FIELD(hist, , hist_dtype, , field)
603 #define VSD_CONSTDRHIST_FIELDVAL(hist, hist_dtype, field) \
604 VSD_DRHIST_FIELD(hist, const, hist_dtype, , field)
605 #define VSD_DRHIST_FIELDPTR(hist, hist_dtype, field) \
606 VSD_DRHIST_FIELD(hist, , hist_dtype, &, field)
607 #define VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype, field) \
608 VSD_DRHIST_FIELD(hist, const, hist_dtype, &, field)
610 #define VSD_DVHIST_FIELD(hist, cnst, hist_dtype, op, field) \
611 (VSD_DTYPE_DVHIST32 == (hist_dtype) ? \
612 op(_VSD(cnst, dvhist32, hist)->field) : \
613 op(_VSD(cnst, dvhist64, hist)->field))
614 #define VSD_DVHIST_FIELDVAL(hist, hist_dtype, field) \
615 VSD_DVHIST_FIELD(hist, , hist_dtype, , field)
616 #define VSD_CONSTDVHIST_FIELDVAL(hist, hist_dtype, field) \
617 VSD_DVHIST_FIELD(hist, const, hist_dtype, , field)
618 #define VSD_DVHIST_FIELDPTR(hist, hist_dtype, field) \
619 VSD_DVHIST_FIELD(hist, , hist_dtype, &, field)
620 #define VSD_CONSTDVHIST_FIELDPTR(hist, hist_dtype, field) \
621 VSD_DVHIST_FIELD(hist, const, hist_dtype, &, field)
623 #define STATS_ABI_V1 1
627 SB_UE = 0, /* Unknown endian. */
628 SB_LE, /* Little endian. */
629 SB_BE /* Big endian. */
639 } __aligned(sizeof(void *));
650 struct statsblob_tpl {
651 struct metablob *mb; /* Template metadata */
652 struct statsblob *sb; /* Template schema */
655 struct stats_tpl_sample_rate {
656 /* XXXLAS: Storing slot_id assumes templates are never removed. */
658 uint32_t tpl_sample_pct;
661 /* Template sample rates list management callback actions. */
662 enum stats_tpl_sr_cb_action {
670 * Callback function pointer passed as arg1 to stats_tpl_sample_rates(). ctx is
671 * a heap-allocated, zero-initialised blob of contextual memory valid during a
672 * single stats_tpl_sample_rates() call and sized per the value passed as arg2.
673 * Returns 0 on success, an errno on error.
674 * - When called with "action == TPL_SR_*_GET", return the subsystem's rates
675 * list ptr and count, locked or unlocked as requested.
676 * - When called with "action == TPL_SR_RUNLOCK", unlock the subsystem's rates
677 * list ptr and count. Pair with a prior "action == TPL_SR_RLOCKED_GET" call.
678 * - When called with "action == TPL_SR_PUT, update the subsystem's rates list
679 * ptr and count to the sysctl processed values and return the inactive list
680 * details in rates/nrates for garbage collection by stats_tpl_sample_rates().
682 typedef int (*stats_tpl_sr_cb_t)(enum stats_tpl_sr_cb_action action,
683 struct stats_tpl_sample_rate **rates, int *nrates, void *ctx);
685 /* Flags related to iterating over a stats blob. */
686 #define SB_IT_FIRST_CB 0x0001
687 #define SB_IT_LAST_CB 0x0002
688 #define SB_IT_FIRST_VOI 0x0004
689 #define SB_IT_LAST_VOI 0x0008
690 #define SB_IT_FIRST_VOISTAT 0x0010
691 #define SB_IT_LAST_VOISTAT 0x0020
692 #define SB_IT_NULLVOI 0x0040
693 #define SB_IT_NULLVOISTAT 0x0080
696 struct voistatdata *vs_data;
702 enum vsd_dtype voi_dtype : 8;
703 enum vsd_dtype vs_dtype : 8;
707 /* Stats blob iterator callback called for each struct voi. */
708 typedef int (*stats_blob_visitcb_t)(struct sb_visit *sbv, void *usrctx);
710 /* ABI specific functions. */
711 int stats_v1_tpl_alloc(const char *name, uint32_t flags);
712 int stats_v1_tpl_add_voistats(uint32_t tpl_id, int32_t voi_id,
713 const char *voi_name, enum vsd_dtype voi_dtype, uint32_t nvss,
714 struct voistatspec *vss, uint32_t flags);
715 int stats_v1_blob_init(struct statsblobv1 *sb, uint32_t tpl_id, uint32_t flags);
716 struct statsblobv1 * stats_v1_blob_alloc(uint32_t tpl_id, uint32_t flags);
717 int stats_v1_blob_clone(struct statsblobv1 **dst, size_t dstmaxsz,
718 struct statsblobv1 *src, uint32_t flags);
719 void stats_v1_blob_destroy(struct statsblobv1 *sb);
720 #define SB_CLONE_RSTSRC 0x0001 /* Reset src blob if clone successful. */
721 #define SB_CLONE_ALLOCDST 0x0002 /* Allocate src->cursz memory for dst. */
722 #define SB_CLONE_USRDSTNOFAULT 0x0004 /* Clone to wired userspace dst. */
723 #define SB_CLONE_USRDST 0x0008 /* Clone to unwired userspace dst. */
724 int stats_v1_blob_snapshot(struct statsblobv1 **dst, size_t dstmaxsz,
725 struct statsblobv1 *src, uint32_t flags);
726 #define SB_TOSTR_OBJDUMP 0x00000001
727 #define SB_TOSTR_META 0x00000002 /* Lookup metablob and render metadata */
728 int stats_v1_blob_tostr(struct statsblobv1 *sb, struct sbuf *buf,
729 enum sb_str_fmt fmt, uint32_t flags);
730 int stats_v1_blob_visit(struct statsblobv1 *sb, stats_blob_visitcb_t func,
732 /* VOI related function flags. */
733 #define SB_VOI_RELUPDATE 0x00000001 /* voival is relative to previous value. */
734 int stats_v1_voi_update(struct statsblobv1 *sb, int32_t voi_id,
735 enum vsd_dtype voi_dtype, struct voistatdata *voival, uint32_t flags);
736 int stats_v1_voistat_fetch_dptr(struct statsblobv1 *sb, int32_t voi_id,
737 enum voi_stype stype, enum vsd_dtype *retdtype, struct voistatdata **retvsd,
740 /* End ABI specific functions. */
742 /* ABI agnostic functions. */
743 int stats_vss_hlpr_init(enum vsd_dtype voi_dtype, uint32_t nvss,
744 struct voistatspec *vss);
745 void stats_vss_hlpr_cleanup(uint32_t nvss, struct voistatspec *vss);
746 int stats_vss_hist_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
747 struct vss_hist_hlpr_info *info);
748 int stats_vss_numeric_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
749 struct vss_numeric_hlpr_info *info);
750 int stats_vss_tdgst_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
751 struct vss_tdgst_hlpr_info *info);
752 int stats_tpl_fetch(int tpl_id, struct statsblob_tpl **tpl);
753 int stats_tpl_fetch_allocid(const char *name, uint32_t hash);
754 int stats_tpl_id2name(uint32_t tpl_id, char *buf, size_t len);
755 int stats_tpl_sample_rates(struct sysctl_oid *oidp, void *arg1, intmax_t arg2,
756 struct sysctl_req *req);
757 int stats_tpl_sample_rollthedice(struct stats_tpl_sample_rate *rates,
758 int nrates, void *seed_bytes, size_t seed_len);
759 int stats_voistatdata_tostr(const struct voistatdata *vsd,
760 enum vsd_dtype voi_dtype, enum vsd_dtype vsd_dtype, size_t vsd_sz,
761 enum sb_str_fmt fmt, struct sbuf *buf, int objdump);
763 static inline struct voistatdata_numeric
764 stats_ctor_vsd_numeric(uint64_t val)
766 struct voistatdata_numeric tmp;
774 stats_tpl_alloc(const char *name, uint32_t flags)
777 return (stats_v1_tpl_alloc(name, flags));
781 stats_tpl_add_voistats(uint32_t tpl_id, int32_t voi_id, const char *voi_name,
782 enum vsd_dtype voi_dtype, uint32_t nvss, struct voistatspec *vss,
787 if ((ret = stats_vss_hlpr_init(voi_dtype, nvss, vss)) == 0) {
788 ret = stats_v1_tpl_add_voistats(tpl_id, voi_id, voi_name,
789 voi_dtype, nvss, vss, flags);
791 stats_vss_hlpr_cleanup(nvss, vss);
797 stats_blob_init(struct statsblob *sb, uint32_t tpl_id, uint32_t flags)
800 return (stats_v1_blob_init((struct statsblobv1 *)sb, tpl_id, flags));
803 static inline struct statsblob *
804 stats_blob_alloc(uint32_t tpl_id, uint32_t flags)
807 return ((struct statsblob *)stats_v1_blob_alloc(tpl_id, flags));
811 stats_blob_clone(struct statsblob **dst, size_t dstmaxsz, struct statsblob *src,
815 return (stats_v1_blob_clone((struct statsblobv1 **)dst, dstmaxsz,
816 (struct statsblobv1 *)src, flags));
820 stats_blob_destroy(struct statsblob *sb)
823 stats_v1_blob_destroy((struct statsblobv1 *)sb);
827 stats_blob_visit(struct statsblob *sb, stats_blob_visitcb_t func, void *usrctx)
830 return (stats_v1_blob_visit((struct statsblobv1 *)sb, func, usrctx));
834 stats_blob_tostr(struct statsblob *sb, struct sbuf *buf,
835 enum sb_str_fmt fmt, uint32_t flags)
838 return (stats_v1_blob_tostr((struct statsblobv1 *)sb, buf, fmt, flags));
842 stats_voistat_fetch_dptr(struct statsblob *sb, int32_t voi_id,
843 enum voi_stype stype, enum vsd_dtype *retdtype, struct voistatdata **retvsd,
847 return (stats_v1_voistat_fetch_dptr((struct statsblobv1 *)sb,
848 voi_id, stype, retdtype, retvsd, retvsdsz));
852 stats_voistat_fetch_s64(struct statsblob *sb, int32_t voi_id,
853 enum voi_stype stype, int64_t *ret)
855 struct voistatdata *vsd;
856 enum vsd_dtype vs_dtype;
859 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
862 else if (VSD_DTYPE_INT_S64 != vs_dtype)
865 *ret = vsd->int64.s64;
870 stats_voistat_fetch_u64(struct statsblob *sb, int32_t voi_id,
871 enum voi_stype stype, uint64_t *ret)
873 struct voistatdata *vsd;
874 enum vsd_dtype vs_dtype;
877 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
880 else if (VSD_DTYPE_INT_U64 != vs_dtype)
883 *ret = vsd->int64.u64;
888 stats_voistat_fetch_s32(struct statsblob *sb, int32_t voi_id,
889 enum voi_stype stype, int32_t *ret)
891 struct voistatdata *vsd;
892 enum vsd_dtype vs_dtype;
895 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
898 else if (VSD_DTYPE_INT_S32 != vs_dtype)
901 *ret = vsd->int32.s32;
906 stats_voistat_fetch_u32(struct statsblob *sb, int32_t voi_id,
907 enum voi_stype stype, uint32_t *ret)
909 struct voistatdata *vsd;
910 enum vsd_dtype vs_dtype;
913 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
916 else if (VSD_DTYPE_INT_U32 != vs_dtype)
919 *ret = vsd->int32.u32;
924 stats_voistat_fetch_slong(struct statsblob *sb, int32_t voi_id,
925 enum voi_stype stype, long *ret)
927 struct voistatdata *vsd;
928 enum vsd_dtype vs_dtype;
931 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
934 else if (VSD_DTYPE_INT_SLONG != vs_dtype)
937 *ret = vsd->intlong.slong;
942 stats_voistat_fetch_ulong(struct statsblob *sb, int32_t voi_id,
943 enum voi_stype stype, unsigned long *ret)
945 struct voistatdata *vsd;
946 enum vsd_dtype vs_dtype;
949 if ((error = stats_voistat_fetch_dptr(sb, voi_id, stype, &vs_dtype, &vsd,
952 else if (VSD_DTYPE_INT_ULONG != vs_dtype)
955 *ret = vsd->intlong.ulong;
960 stats_blob_snapshot(struct statsblob **dst, size_t dstmaxsz,
961 struct statsblob *src, uint32_t flags)
964 return (stats_v1_blob_snapshot((struct statsblobv1 **)dst, dstmaxsz,
965 (struct statsblobv1 *)src, flags));
969 stats_voi_update_abs_s32(struct statsblob *sb, int32_t voi_id, int32_t voival)
975 struct voistatdata tmp;
976 tmp.int32.s32 = voival;
978 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
979 VSD_DTYPE_INT_S32, &tmp, 0));
983 stats_voi_update_rel_s32(struct statsblob *sb, int32_t voi_id, int32_t voival)
989 struct voistatdata tmp;
990 tmp.int32.s32 = voival;
992 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
993 VSD_DTYPE_INT_S32, &tmp, SB_VOI_RELUPDATE));
997 stats_voi_update_abs_u32(struct statsblob *sb, int32_t voi_id, uint32_t voival)
1003 struct voistatdata tmp;
1004 tmp.int32.u32 = voival;
1006 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1007 VSD_DTYPE_INT_U32, &tmp, 0));
1011 stats_voi_update_rel_u32(struct statsblob *sb, int32_t voi_id, uint32_t voival)
1017 struct voistatdata tmp;
1018 tmp.int32.u32 = voival;
1020 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1021 VSD_DTYPE_INT_U32, &tmp, SB_VOI_RELUPDATE));
1025 stats_voi_update_abs_s64(struct statsblob *sb, int32_t voi_id, int64_t voival)
1031 struct voistatdata tmp;
1032 tmp.int64.s64 = voival;
1034 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1035 VSD_DTYPE_INT_S64, &tmp, 0));
1039 stats_voi_update_rel_s64(struct statsblob *sb, int32_t voi_id, int64_t voival)
1045 struct voistatdata tmp;
1046 tmp.int64.s64 = voival;
1048 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1049 VSD_DTYPE_INT_S64, &tmp, SB_VOI_RELUPDATE));
1053 stats_voi_update_abs_u64(struct statsblob *sb, int32_t voi_id, uint64_t voival)
1059 struct voistatdata tmp;
1060 tmp.int64.u64 = voival;
1062 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1063 VSD_DTYPE_INT_U64, &tmp, 0));
1067 stats_voi_update_rel_u64(struct statsblob *sb, int32_t voi_id, uint64_t voival)
1073 struct voistatdata tmp;
1074 tmp.int64.u64 = voival;
1076 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1077 VSD_DTYPE_INT_U64, &tmp, SB_VOI_RELUPDATE));
1081 stats_voi_update_abs_slong(struct statsblob *sb, int32_t voi_id, long voival)
1087 struct voistatdata tmp;
1088 tmp.intlong.slong = voival;
1090 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1091 VSD_DTYPE_INT_SLONG, &tmp, 0));
1095 stats_voi_update_rel_slong(struct statsblob *sb, int32_t voi_id, long voival)
1101 struct voistatdata tmp;
1102 tmp.intlong.slong = voival;
1104 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1105 VSD_DTYPE_INT_SLONG, &tmp, SB_VOI_RELUPDATE));
1109 stats_voi_update_abs_ulong(struct statsblob *sb, int32_t voi_id,
1110 unsigned long voival)
1116 struct voistatdata tmp;
1117 tmp.intlong.ulong = voival;
1119 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1120 VSD_DTYPE_INT_ULONG, &tmp, 0));
1124 stats_voi_update_rel_ulong(struct statsblob *sb, int32_t voi_id,
1125 unsigned long voival)
1131 struct voistatdata tmp;
1132 tmp.intlong.ulong = voival;
1134 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1135 VSD_DTYPE_INT_ULONG, &tmp, SB_VOI_RELUPDATE));
1139 stats_voi_update_abs_sq32(struct statsblob *sb, int32_t voi_id, s32q_t voival)
1145 struct voistatdata tmp;
1146 tmp.q32.sq32 = voival;
1148 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1149 VSD_DTYPE_Q_S32, &tmp, 0));
1153 stats_voi_update_rel_sq32(struct statsblob *sb, int32_t voi_id, s32q_t voival)
1159 struct voistatdata tmp;
1160 tmp.q32.sq32 = voival;
1162 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1163 VSD_DTYPE_Q_S32, &tmp, SB_VOI_RELUPDATE));
1167 stats_voi_update_abs_uq32(struct statsblob *sb, int32_t voi_id, u32q_t voival)
1173 struct voistatdata tmp;
1174 tmp.q32.uq32 = voival;
1176 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1177 VSD_DTYPE_Q_U32, &tmp, 0));
1181 stats_voi_update_rel_uq32(struct statsblob *sb, int32_t voi_id, u32q_t voival)
1187 struct voistatdata tmp;
1188 tmp.q32.uq32 = voival;
1190 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1191 VSD_DTYPE_Q_U32, &tmp, SB_VOI_RELUPDATE));
1195 stats_voi_update_abs_sq64(struct statsblob *sb, int32_t voi_id, s64q_t voival)
1201 struct voistatdata tmp;
1202 tmp.q64.sq64 = voival;
1204 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1205 VSD_DTYPE_Q_S64, &tmp, 0));
1209 stats_voi_update_rel_sq64(struct statsblob *sb, int32_t voi_id, s64q_t voival)
1215 struct voistatdata tmp;
1216 tmp.q64.sq64 = voival;
1218 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1219 VSD_DTYPE_Q_S64, &tmp, SB_VOI_RELUPDATE));
1223 stats_voi_update_abs_uq64(struct statsblob *sb, int32_t voi_id, u64q_t voival)
1229 struct voistatdata tmp;
1230 tmp.q64.uq64 = voival;
1232 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1233 VSD_DTYPE_Q_U64, &tmp, 0));
1237 stats_voi_update_rel_uq64(struct statsblob *sb, int32_t voi_id, u64q_t voival)
1243 struct voistatdata tmp;
1244 tmp.q64.uq64 = voival;
1246 return (stats_v1_voi_update((struct statsblobv1 *)sb, voi_id,
1247 VSD_DTYPE_Q_U64, &tmp, SB_VOI_RELUPDATE));
1250 /* End ABI agnostic functions. */
1252 #endif /* _SYS_STATS_H_ */