xref: /freebsd/lib/libbsdstat/bsdstat.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * $FreeBSD$
32  */
33 
34 #ifndef	_BSDSTAT_H_
35 #define	_BSDSTAT_H_
36 /*
37  * Base class for managing+displaying periodically collected statistics.
38  */
39 
40 /*
41  * Statistic definition/description.  The are defined
42  * for stats that correspond 1-1 w/ a collected stat
43  * and for stats that are calculated indirectly.
44  */
45 struct fmt {
46 	int	width;			/* printed field width */
47 	const char* name;		/* stat field name referenced by user */
48 	const char* label;		/* printed header label */
49 	const char* desc;		/* verbose description */
50 };
51 
52 #define	BSDSTAT_DECL_METHODS(_p) \
53 	/* set the format of the statistics to display */	\
54 	void (*setfmt)(_p, const char *);			\
55 	/* collect+store ``current statistics'' */		\
56 	void (*collect_cur)(_p);				\
57 	/* collect+store ``total statistics'' */		\
58 	void (*collect_tot)(_p);				\
59 	/* update ``total statistics'' if necessary from current */ \
60 	void (*update_tot)(_p);					\
61 	/* format a statistic from the current stats */		\
62 	int (*get_curstat)(_p, int, char [], size_t);		\
63 	/* format a statistic from the total stats */		\
64 	int (*get_totstat)(_p, int, char [], size_t);		\
65 	/* print field headers terminated by a \n */		\
66 	void (*print_header)(_p, FILE *);			\
67 	/* print current statistics terminated by a \n */	\
68 	void (*print_current)(_p, FILE *);			\
69 	/* print total statistics terminated by a \n */		\
70 	void (*print_total)(_p, FILE *);			\
71 	/* print total statistics in a verbose (1 stat/line) format */ \
72 	void (*print_verbose)(_p, FILE *);			\
73 	/* print available statistics */			\
74 	void (*print_fields)(_p, FILE *)
75 
76 /*
77  * Statistics base class.  This class is not usable; only
78  * classes derived from it are useful.
79  */
80 struct bsdstat {
81 	const char *name;		/* statistics name, e.g. wlanstats */
82 	const struct fmt *stats;	/* statistics in class */
83 	int nstats;			/* number of stats */
84 #define	FMTS_IS_STAT	0x80	/* the following two bytes are the stat id */
85 	unsigned char fmts[4096];	/* private: compiled stats to display */
86 
87 	BSDSTAT_DECL_METHODS(struct bsdstat *);
88 };
89 
90 extern	void bsdstat_init(struct bsdstat *, const char *name,
91 		    const struct fmt *stats, int nstats);
92 
93 #define	BSDSTAT_DEFINE_BOUNCE(_t) \
94 static void _t##_setfmt(struct _t *wf, const char *fmt0)	\
95 	{ wf->base.setfmt(&wf->base, fmt0); }			\
96 static void _t##_collect_cur(struct _t *wf)			\
97 	{ wf->base.collect_cur(&wf->base); }			\
98 static void _t##_collect_tot(struct _t *wf)			\
99 	{ wf->base.collect_tot(&wf->base); }			\
100 static void _t##_update_tot(struct _t *wf)			\
101 	{ wf->base.update_tot(&wf->base); }			\
102 static int _t##_get_curstat(struct _t *wf, int s, char b[], size_t bs) \
103 	{ return wf->base.get_curstat(&wf->base, s, b, bs); }	\
104 static int _t##_get_totstat(struct _t *wf, int s, char b[], size_t bs) \
105 	{ return wf->base.get_totstat(&wf->base, s, b, bs); }	\
106 static void _t##_print_header(struct _t *wf, FILE *fd)		\
107 	{ wf->base.print_header(&wf->base, fd); }		\
108 static void _t##_print_current(struct _t *wf, FILE *fd)		\
109 	{ wf->base.print_current(&wf->base, fd); }		\
110 static void _t##_print_total(struct _t *wf, FILE *fd)		\
111 	{ wf->base.print_total(&wf->base, fd); }		\
112 static void _t##_print_verbose(struct _t *wf, FILE *fd)		\
113 	{ wf->base.print_verbose(&wf->base, fd); }		\
114 static void _t##_print_fields(struct _t *wf, FILE *fd)		\
115 	{ wf->base.print_fields(&wf->base, fd); }
116 
117 #define	BSDSTAT_BOUNCE(_p, _t) do {				\
118 	_p->base.setfmt = _t##_setfmt;				\
119 	_p->base.collect_cur = _t##_collect_cur;		\
120 	_p->base.collect_tot = _t##_collect_tot;		\
121 	_p->base.update_tot = _t##_update_tot;			\
122 	_p->base.get_curstat = _t##_get_curstat;		\
123 	_p->base.get_totstat = _t##_get_totstat;		\
124 	_p->base.print_header = _t##_print_header;		\
125 	_p->base.print_current = _t##_print_current;		\
126 	_p->base.print_total = _t##_print_total;		\
127 	_p->base.print_verbose = _t##_print_verbose;		\
128 	_p->base.print_fields = _t##_print_fields;		\
129 } while (0)
130 #endif /* _BSDSTAT_H_ */
131