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