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