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 _STATFOO_H_
33#define	_STATFOO_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 */
43struct 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	STATFOO_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 */
78struct statfoo {
79	const char *name;		/* statistics name, e.g. wlanstats */
80	const struct fmt *stats;	/* statistics in class */
81	int nstats;			/* number of stats */
82	unsigned char fmts[4096];	/* private: compiled stats to display */
83
84	STATFOO_DECL_METHODS(struct statfoo *);
85};
86
87void	statfoo_init(struct statfoo *, const char *name,
88		const struct fmt *stats, int nstats);
89
90#define	STATFOO_DEFINE_BOUNCE(_t) \
91static void _t##_setfmt(struct _t *wf, const char *fmt0)	\
92	{ wf->base.setfmt(&wf->base, fmt0); }			\
93static void _t##_collect_cur(struct _t *wf)			\
94	{ wf->base.collect_cur(&wf->base); }			\
95static void _t##_collect_tot(struct _t *wf)			\
96	{ wf->base.collect_tot(&wf->base); }			\
97static void _t##_update_tot(struct _t *wf)			\
98	{ wf->base.update_tot(&wf->base); }			\
99static int _t##_get_curstat(struct _t *wf, int s, char b[], size_t bs) \
100	{ return wf->base.get_curstat(&wf->base, s, b, bs); }	\
101static int _t##_get_totstat(struct _t *wf, int s, char b[], size_t bs) \
102	{ return wf->base.get_totstat(&wf->base, s, b, bs); }	\
103static void _t##_print_header(struct _t *wf, FILE *fd)		\
104	{ wf->base.print_header(&wf->base, fd); }		\
105static void _t##_print_current(struct _t *wf, FILE *fd)		\
106	{ wf->base.print_current(&wf->base, fd); }		\
107static void _t##_print_total(struct _t *wf, FILE *fd)		\
108	{ wf->base.print_total(&wf->base, fd); }		\
109static void _t##_print_verbose(struct _t *wf, FILE *fd)		\
110	{ wf->base.print_verbose(&wf->base, fd); }		\
111static void _t##_print_fields(struct _t *wf, FILE *fd)		\
112	{ wf->base.print_fields(&wf->base, fd); }
113
114#define	STATFOO_BOUNCE(_p, _t) do {				\
115	_p->base.setfmt = _t##_setfmt;				\
116	_p->base.collect_cur = _t##_collect_cur;		\
117	_p->base.collect_tot = _t##_collect_tot;		\
118	_p->base.update_tot = _t##_update_tot;			\
119	_p->base.get_curstat = _t##_get_curstat;		\
120	_p->base.get_totstat = _t##_get_totstat;		\
121	_p->base.print_header = _t##_print_header;		\
122	_p->base.print_current = _t##_print_current;		\
123	_p->base.print_total = _t##_print_total;		\
124	_p->base.print_verbose = _t##_print_verbose;		\
125	_p->base.print_fields = _t##_print_fields;		\
126} while (0)
127#endif /* _STATFOO_H_ */
128