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>
64 * XXXLAS: Hacks to enable sharing template creation code between kernel and
65 * userland e.g. tcp_stats.c
68 #define VNET_DEFINE(t, n) static t n __unused
69 #endif /* ! _KERNEL */
71 #define TPL_MAX_NAME_LEN 64
74 * The longest template string spec format i.e. the normative spec format, is:
76 * "<tplname>":<tplhash>
78 * Therefore, the max string length of a template string spec is:
82 * - 1 char for : separating name and hash
83 * - 10 chars for 32bit hash
85 #define STATS_TPL_MAX_STR_SPEC_LEN (TPL_MAX_NAME_LEN + 13)
92 SB_STRFMT_FREEFORM = 0,
94 SB_STRFMT_NUM_FMTS /* +1 to highest numbered format type. */
99 VS_STYPE_VOISTATE = 0, /* Reserved for internal API use. */
105 VS_NUM_STYPES /* +1 to highest numbered stat type. */
109 * VOI stat data types used as storage for certain stat types and to marshall
110 * data through various API calls.
113 VSD_DTYPE_VOISTATE = 0, /* Reserved for internal API use. */
114 VSD_DTYPE_INT_S32, /* int32_t */
115 VSD_DTYPE_INT_U32, /* uint32_t */
116 VSD_DTYPE_INT_S64, /* int64_t */
117 VSD_DTYPE_INT_U64, /* uint64_t */
118 VSD_DTYPE_INT_SLONG, /* long */
119 VSD_DTYPE_INT_ULONG, /* unsigned long */
120 VSD_DTYPE_Q_S32, /* s32q_t */
121 VSD_DTYPE_Q_U32, /* u32q_t */
122 VSD_DTYPE_Q_S64, /* s64q_t */
123 VSD_DTYPE_Q_U64, /* u64q_t */
124 VSD_DTYPE_CRHIST32, /* continuous range histogram, 32bit buckets */
125 VSD_DTYPE_DRHIST32, /* discrete range histogram, 32bit buckets */
126 VSD_DTYPE_DVHIST32, /* discrete value histogram, 32bit buckets */
127 VSD_DTYPE_CRHIST64, /* continuous range histogram, 64bit buckets */
128 VSD_DTYPE_DRHIST64, /* discrete range histogram, 64bit buckets */
129 VSD_DTYPE_DVHIST64, /* discrete value histogram, 64bit buckets */
130 VSD_DTYPE_TDGSTCLUST32, /* clustering variant t-digest, 32bit buckets */
131 VSD_DTYPE_TDGSTCLUST64, /* clustering variant t-digest, 64bit buckets */
132 VSD_NUM_DTYPES /* +1 to highest numbered data type. */
135 struct voistatdata_int32 {
142 struct voistatdata_int64 {
146 //counter_u64_t u64pcpu;
150 struct voistatdata_intlong {
157 struct voistatdata_q32 {
164 struct voistatdata_q64 {
171 struct voistatdata_numeric {
174 #if BYTE_ORDER == BIG_ENDIAN
181 #if BYTE_ORDER == LITTLE_ENDIAN
187 #if BYTE_ORDER == BIG_ENDIAN
194 #if BYTE_ORDER == LITTLE_ENDIAN
200 #if BYTE_ORDER == BIG_ENDIAN && LONG_BIT == 32
207 #if BYTE_ORDER == LITTLE_ENDIAN && LONG_BIT == 32
212 struct voistatdata_int64 int64;
213 struct voistatdata_q64 q64;
217 /* Continuous range histogram with 32bit buckets. */
218 struct voistatdata_crhist32 {
221 struct voistatdata_numeric lb;
226 /* Continuous range histogram with 64bit buckets. */
227 struct voistatdata_crhist64 {
230 struct voistatdata_numeric lb;
235 /* Discrete range histogram with 32bit buckets. */
236 struct voistatdata_drhist32 {
239 struct voistatdata_numeric lb, ub;
244 /* Discrete range histogram with 64bit buckets. */
245 struct voistatdata_drhist64 {
248 struct voistatdata_numeric lb, ub;
253 /* Discrete value histogram with 32bit buckets. */
254 struct voistatdata_dvhist32 {
257 struct voistatdata_numeric val;
262 /* Discrete value histogram with 64bit buckets. */
263 struct voistatdata_dvhist64 {
266 struct voistatdata_numeric val;
271 struct voistatdata_hist {
273 struct voistatdata_crhist32 crhist32;
274 struct voistatdata_crhist64 crhist64;
275 struct voistatdata_dvhist32 dvhist32;
276 struct voistatdata_dvhist64 dvhist64;
277 struct voistatdata_drhist32 drhist32;
278 struct voistatdata_drhist64 drhist64;
282 struct voistatdata_tdgstctd32 {
283 ARB16_ENTRY() ctdlnk;
285 RB_ENTRY(voistatdata_tdgstctd32) rblnk;
291 struct voistatdata_tdgstctd64 {
292 ARB16_ENTRY() ctdlnk;
294 RB_ENTRY(voistatdata_tdgstctd64) rblnk;
300 struct voistatdata_tdgstctd {
302 struct voistatdata_tdgstctd32 tdgstctd32;
303 struct voistatdata_tdgstctd64 tdgstctd64;
307 /* Clustering variant, fixed-point t-digest with 32bit mu/counts. */
308 struct voistatdata_tdgstclust32 {
309 uint32_t smplcnt; /* Count of samples. */
310 uint32_t compcnt; /* Count of digest compressions. */
312 RB_HEAD(rbctdth32, voistatdata_tdgstctd32) rbctdtree;
314 /* Array-based red-black tree of centroids. */
315 ARB16_HEAD(ctdth32, voistatdata_tdgstctd32) ctdtree;
318 /* Clustering variant, fixed-point t-digest with 64bit mu/counts. */
319 struct voistatdata_tdgstclust64 {
320 uint64_t smplcnt; /* Count of samples. */
321 uint32_t compcnt; /* Count of digest compressions. */
323 RB_HEAD(rbctdth64, voistatdata_tdgstctd64) rbctdtree;
325 /* Array-based red-black tree of centroids. */
326 ARB16_HEAD(ctdth64, voistatdata_tdgstctd64) ctdtree;
329 struct voistatdata_tdgst {
331 struct voistatdata_tdgstclust32 tdgstclust32;
332 struct voistatdata_tdgstclust64 tdgstclust64;
338 struct voistatdata_int32 int32;
339 struct voistatdata_int64 int64;
340 struct voistatdata_intlong intlong;
341 struct voistatdata_q32 q32;
342 struct voistatdata_q64 q64;
343 struct voistatdata_crhist32 crhist32;
344 struct voistatdata_crhist64 crhist64;
345 struct voistatdata_dvhist32 dvhist32;
346 struct voistatdata_dvhist64 dvhist64;
347 struct voistatdata_drhist32 drhist32;
348 struct voistatdata_drhist64 drhist64;
349 struct voistatdata_tdgstclust32 tdgstclust32;
350 struct voistatdata_tdgstclust64 tdgstclust64;
354 #define VSD_HIST_LBOUND_INF 0x01
355 #define VSD_HIST_UBOUND_INF 0x02
356 struct vss_hist_hlpr_info {
357 enum hist_bkt_alloc {
358 BKT_LIN, /* Linear steps. */
359 BKT_EXP, /* Exponential steps. */
360 BKT_LINEXP, /* Exponential steps, linear sub-steps. */
361 BKT_USR /* User specified buckets. */
363 enum vsd_dtype voi_dtype;
364 enum vsd_dtype hist_dtype;
366 struct voistatdata_numeric lb;
367 struct voistatdata_numeric ub;
370 const uint64_t stepinc;
373 const uint64_t stepbase;
374 const uint64_t stepexp;
377 const uint64_t stepbase;
378 const uint64_t linstepdiv;
381 const uint16_t nbkts;
383 struct voistatdata_numeric lb, ub;
389 struct vss_tdgst_hlpr_info {
390 enum vsd_dtype voi_dtype;
391 enum vsd_dtype tdgst_dtype;
394 } __aligned(sizeof(void *));
396 struct vss_numeric_hlpr_info {
400 struct vss_hlpr_info {
402 struct vss_tdgst_hlpr_info tdgst;
403 struct vss_hist_hlpr_info hist;
404 struct vss_numeric_hlpr_info numeric;
409 typedef int (*vss_hlpr_fn)(enum vsd_dtype, struct voistatspec *,
410 struct vss_hlpr_info *);
413 vss_hlpr_fn hlpr; /* iv helper function. */
414 struct vss_hlpr_info *hlprinfo; /* Helper function context. */
415 struct voistatdata *iv; /* Initialisation value. */
416 size_t vsdsz; /* Size of iv. */
417 uint32_t flags; /* Stat flags. */
418 enum vsd_dtype vs_dtype : 8; /* Stat's dtype. */
419 enum voi_stype stype : 8; /* Stat type. */
422 extern const char *vs_stype2name[VS_NUM_STYPES];
423 extern const char *vs_stype2desc[VS_NUM_STYPES];
424 extern const char *vsd_dtype2name[VSD_NUM_DTYPES];
425 extern const size_t vsd_dtype2size[VSD_NUM_DTYPES];
428 extern const struct voistatdata_numeric numeric_limits[2][VSD_DTYPE_Q_U64 + 1];
430 #define TYPEOF_MEMBER(type, member) __typeof(((type *)0)->member)
431 #define TYPEOF_MEMBER_PTR(type, member) __typeof(*(((type *)0)->member))
432 #define SIZEOF_MEMBER(type, member) sizeof(TYPEOF_MEMBER(type, member))
434 /* Cast a pointer to a voistatdata struct of requested type. */
435 #define _VSD(cnst, type, ptr) ((cnst struct voistatdata_##type *)(ptr))
436 #define VSD(type, ptr) _VSD(, type, ptr)
437 #define CONSTVSD(type, ptr) _VSD(const, type, ptr)
439 #define NVSS(vss_slots) (sizeof((vss_slots)) / sizeof(struct voistatspec))
440 #define STATS_VSS(st, vsf, dt, hlp, hlpi) \
441 ((struct voistatspec){ \
446 .hlprinfo = (hlpi), \
449 #define STATS_VSS_SUM() STATS_VSS(VS_STYPE_SUM, 0, 0, \
450 (vss_hlpr_fn)&stats_vss_numeric_hlpr, NULL)
452 #define STATS_VSS_MAX() STATS_VSS(VS_STYPE_MAX, 0, 0, \
453 (vss_hlpr_fn)&stats_vss_numeric_hlpr, NULL)
455 #define STATS_VSS_MIN() STATS_VSS(VS_STYPE_MIN, 0, 0, \
456 (vss_hlpr_fn)&stats_vss_numeric_hlpr, NULL)
458 #define STATS_VSS_HIST(htype, hist_hlpr_info) STATS_VSS(VS_STYPE_HIST, 0, \
459 htype, (vss_hlpr_fn)&stats_vss_hist_hlpr, \
460 (struct vss_hlpr_info *)(hist_hlpr_info))
462 #define STATS_VSS_TDIGEST(tdtype, tdgst_hlpr_info) STATS_VSS(VS_STYPE_TDGST, \
463 0, tdtype, (vss_hlpr_fn)&stats_vss_tdgst_hlpr, \
464 (struct vss_hlpr_info *)(tdgst_hlpr_info))
466 #define TDGST_NCTRS2VSDSZ(tdtype, nctds) (sizeof(struct voistatdata_##tdtype) + \
467 ((nctds) * sizeof(TYPEOF_MEMBER_PTR(struct voistatdata_##tdtype, \
468 ctdtree.arb_nodes))))
470 #define TDGST_HLPR_INFO(dt, nc, nf) \
471 (&(struct vss_tdgst_hlpr_info){ \
472 .tdgst_dtype = (dt), \
477 #define STATS_VSS_TDGSTCLUST32(nctds, prec) \
478 STATS_VSS_TDIGEST(VSD_DTYPE_TDGSTCLUST32, \
479 TDGST_HLPR_INFO(VSD_DTYPE_TDGSTCLUST32, nctds, prec))
481 #define STATS_VSS_TDGSTCLUST64(nctds, prec) \
482 STATS_VSS_TDIGEST(VSD_DTYPE_TDGSTCLUST64, \
483 TDGST_HLPR_INFO(VSD_DTYPE_TDGSTCLUST64, nctds, prec))
485 #define HIST_VSDSZ2NBKTS(htype, dsz) \
486 ((dsz - sizeof(struct voistatdata_##htype)) / \
487 sizeof(TYPEOF_MEMBER(struct voistatdata_##htype, bkts[0])))
489 #define HIST_NBKTS2VSDSZ(htype, nbkts) (sizeof(struct voistatdata_##htype) + \
490 ((nbkts) * sizeof(TYPEOF_MEMBER_PTR(struct voistatdata_##htype, bkts))))
492 #define HIST_HLPR_INFO_LIN_FIELDS(si) .lin.stepinc = (si)
494 #define HIST_HLPR_INFO_EXP_FIELDS(sb, se) \
495 .exp.stepbase = (sb), .exp.stepexp = (se)
497 #define HIST_HLPR_INFO_LINEXP_FIELDS(nss, sb) \
498 .linexp.linstepdiv = (nss), .linexp.stepbase = (sb)
500 #define HIST_HLPR_INFO_USR_FIELDS(bbs) \
501 .usr.bkts = (TYPEOF_MEMBER(struct vss_hist_hlpr_info, usr.bkts))(bbs), \
502 .usr.nbkts = (sizeof(bbs) / sizeof(struct voistatdata_numeric[2]))
504 #define HIST_HLPR_INFO(dt, sch, f, lbd, ubd, bkthlpr_fields) \
505 (&(struct vss_hist_hlpr_info){ \
507 .hist_dtype = (dt), \
509 .lb = stats_ctor_vsd_numeric(lbd), \
510 .ub = stats_ctor_vsd_numeric(ubd), \
514 #define STATS_VSS_CRHIST32_LIN(lb, ub, stepinc, vsdflags) \
515 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
516 BKT_LIN, vsdflags, lb, ub, HIST_HLPR_INFO_LIN_FIELDS(stepinc)))
517 #define STATS_VSS_CRHIST64_LIN(lb, ub, stepinc, vsdflags) \
518 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
519 BKT_LIN, vsdflags, lb, ub, HIST_HLPR_INFO_LIN_FIELDS(stepinc)))
521 #define STATS_VSS_CRHIST32_EXP(lb, ub, stepbase, stepexp, vsdflags) \
522 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
523 BKT_EXP, vsdflags, lb, ub, HIST_HLPR_INFO_EXP_FIELDS(stepbase, stepexp)))
524 #define STATS_VSS_CRHIST64_EXP(lb, ub, stepbase, stepexp, vsdflags) \
525 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
526 BKT_EXP, vsdflags, lb, ub, HIST_HLPR_INFO_EXP_FIELDS(stepbase, stepexp)))
528 #define STATS_VSS_CRHIST32_LINEXP(lb, ub, nlinsteps, stepbase, vsdflags) \
529 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
530 BKT_LINEXP, vsdflags, lb, ub, HIST_HLPR_INFO_LINEXP_FIELDS(nlinsteps, \
532 #define STATS_VSS_CRHIST64_LINEXP(lb, ub, nlinsteps, stepbase, vsdflags) \
533 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
534 BKT_LINEXP, vsdflags, lb, ub, HIST_HLPR_INFO_LINEXP_FIELDS(nlinsteps, \
537 #define STATS_VSS_CRHIST32_USR(bkts, vsdflags) \
538 STATS_VSS_HIST(VSD_DTYPE_CRHIST32, HIST_HLPR_INFO(VSD_DTYPE_CRHIST32, \
539 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
540 #define STATS_VSS_CRHIST64_USR(bkts, vsdflags) \
541 STATS_VSS_HIST(VSD_DTYPE_CRHIST64, HIST_HLPR_INFO(VSD_DTYPE_CRHIST64, \
542 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
544 #define STATS_VSS_DRHIST32_USR(bkts, vsdflags) \
545 STATS_VSS_HIST(VSD_DTYPE_DRHIST32, HIST_HLPR_INFO(VSD_DTYPE_DRHIST32, \
546 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
547 #define STATS_VSS_DRHIST64_USR(bkts, vsdflags) \
548 STATS_VSS_HIST(VSD_DTYPE_DRHIST64, HIST_HLPR_INFO(VSD_DTYPE_DRHIST64, \
549 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(bkts)))
551 #define STATS_VSS_DVHIST32_USR(vals, vsdflags) \
552 STATS_VSS_HIST(VSD_DTYPE_DVHIST32, HIST_HLPR_INFO(VSD_DTYPE_DVHIST32, \
553 BKT_USR, vsdflags, 0, 0, HIST_HLPR_INFO_USR_FIELDS(vals)))
554 #define STATS_VSS_DVHIST64_USR(vals, vsdflags) \
555 STATS_VSS_HIST(VSD_DTYPE_DVHIST64, HIST_HLPR_INFO(VSD_DTYPE_DVHIST64, \
556 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_ */