1161200Ssam/*-
2174244Ssam * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3161200Ssam * All rights reserved.
4161200Ssam *
5161200Ssam * Redistribution and use in source and binary forms, with or without
6161200Ssam * modification, are permitted provided that the following conditions
7161200Ssam * are met:
8161200Ssam * 1. Redistributions of source code must retain the above copyright
9161200Ssam *    notice, this list of conditions and the following disclaimer,
10161200Ssam *    without modification.
11161200Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12161200Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13161200Ssam *    redistribution must be conditioned upon including a substantially
14161200Ssam *    similar Disclaimer requirement for further binary redistribution.
15161200Ssam *
16161200Ssam * NO WARRANTY
17161200Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18161200Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19161200Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20161200Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21161200Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22161200Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23161200Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24161200Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25161200Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26161200Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27161200Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28161200Ssam *
29161200Ssam * $FreeBSD$
30161200Ssam */
31161200Ssam
32161200Ssam#ifndef _STATFOO_H_
33161200Ssam#define	_STATFOO_H_
34161200Ssam/*
35161200Ssam * Base class for managing+displaying periodically collected statistics.
36161200Ssam */
37161200Ssam
38161200Ssam/*
39161200Ssam * Statistic definition/description.  The are defined
40161200Ssam * for stats that correspond 1-1 w/ a collected stat
41161200Ssam * and for stats that are calculated indirectly.
42161200Ssam */
43161200Ssamstruct fmt {
44161200Ssam	int	width;			/* printed field width */
45161200Ssam	const char* name;		/* stat field name referenced by user */
46161200Ssam	const char* label;		/* printed header label */
47161200Ssam	const char* desc;		/* verbose description */
48161200Ssam};
49161200Ssam
50161200Ssam#define	STATFOO_DECL_METHODS(_p) \
51161200Ssam	/* set the format of the statistics to display */	\
52161200Ssam	void (*setfmt)(_p, const char *);			\
53161200Ssam	/* collect+store ``current statistics'' */		\
54161200Ssam	void (*collect_cur)(_p);				\
55161200Ssam	/* collect+store ``total statistics'' */		\
56161200Ssam	void (*collect_tot)(_p);				\
57161200Ssam	/* update ``total statistics'' if necessary from current */ \
58161200Ssam	void (*update_tot)(_p);					\
59161200Ssam	/* format a statistic from the current stats */		\
60161200Ssam	int (*get_curstat)(_p, int, char [], size_t);		\
61161200Ssam	/* format a statistic from the total stats */		\
62161200Ssam	int (*get_totstat)(_p, int, char [], size_t);		\
63161200Ssam	/* print field headers terminated by a \n */		\
64161200Ssam	void (*print_header)(_p, FILE *);			\
65161200Ssam	/* print current statistics terminated by a \n */	\
66161200Ssam	void (*print_current)(_p, FILE *);			\
67161200Ssam	/* print total statistics terminated by a \n */		\
68161200Ssam	void (*print_total)(_p, FILE *);			\
69161200Ssam	/* print total statistics in a verbose (1 stat/line) format */ \
70161200Ssam	void (*print_verbose)(_p, FILE *);			\
71161200Ssam	/* print available statistics */			\
72161200Ssam	void (*print_fields)(_p, FILE *)
73161200Ssam
74161200Ssam/*
75161200Ssam * Statistics base class.  This class is not usable; only
76161200Ssam * classes derived from it are useful.
77161200Ssam */
78161200Ssamstruct statfoo {
79161200Ssam	const char *name;		/* statistics name, e.g. wlanstats */
80161200Ssam	const struct fmt *stats;	/* statistics in class */
81161200Ssam	int nstats;			/* number of stats */
82178699Ssam	int fields[128];		/* index of field referenced in fmts */
83161200Ssam	unsigned char fmts[4096];	/* private: compiled stats to display */
84161200Ssam
85161200Ssam	STATFOO_DECL_METHODS(struct statfoo *);
86161200Ssam};
87161200Ssam
88161200Ssamvoid	statfoo_init(struct statfoo *, const char *name,
89161200Ssam		const struct fmt *stats, int nstats);
90161200Ssam
91161200Ssam#define	STATFOO_DEFINE_BOUNCE(_t) \
92161200Ssamstatic void _t##_setfmt(struct _t *wf, const char *fmt0)	\
93161200Ssam	{ wf->base.setfmt(&wf->base, fmt0); }			\
94161200Ssamstatic void _t##_collect_cur(struct _t *wf)			\
95161200Ssam	{ wf->base.collect_cur(&wf->base); }			\
96161200Ssamstatic void _t##_collect_tot(struct _t *wf)			\
97161200Ssam	{ wf->base.collect_tot(&wf->base); }			\
98161200Ssamstatic void _t##_update_tot(struct _t *wf)			\
99161200Ssam	{ wf->base.update_tot(&wf->base); }			\
100161200Ssamstatic int _t##_get_curstat(struct _t *wf, int s, char b[], size_t bs) \
101161200Ssam	{ return wf->base.get_curstat(&wf->base, s, b, bs); }	\
102161200Ssamstatic int _t##_get_totstat(struct _t *wf, int s, char b[], size_t bs) \
103161200Ssam	{ return wf->base.get_totstat(&wf->base, s, b, bs); }	\
104161200Ssamstatic void _t##_print_header(struct _t *wf, FILE *fd)		\
105161200Ssam	{ wf->base.print_header(&wf->base, fd); }		\
106161200Ssamstatic void _t##_print_current(struct _t *wf, FILE *fd)		\
107161200Ssam	{ wf->base.print_current(&wf->base, fd); }		\
108161200Ssamstatic void _t##_print_total(struct _t *wf, FILE *fd)		\
109161200Ssam	{ wf->base.print_total(&wf->base, fd); }		\
110161200Ssamstatic void _t##_print_verbose(struct _t *wf, FILE *fd)		\
111161200Ssam	{ wf->base.print_verbose(&wf->base, fd); }		\
112161200Ssamstatic void _t##_print_fields(struct _t *wf, FILE *fd)		\
113161200Ssam	{ wf->base.print_fields(&wf->base, fd); }
114161200Ssam
115161200Ssam#define	STATFOO_BOUNCE(_p, _t) do {				\
116161200Ssam	_p->base.setfmt = _t##_setfmt;				\
117161200Ssam	_p->base.collect_cur = _t##_collect_cur;		\
118161200Ssam	_p->base.collect_tot = _t##_collect_tot;		\
119161200Ssam	_p->base.update_tot = _t##_update_tot;			\
120161200Ssam	_p->base.get_curstat = _t##_get_curstat;		\
121161200Ssam	_p->base.get_totstat = _t##_get_totstat;		\
122161200Ssam	_p->base.print_header = _t##_print_header;		\
123161200Ssam	_p->base.print_current = _t##_print_current;		\
124161200Ssam	_p->base.print_total = _t##_print_total;		\
125161200Ssam	_p->base.print_verbose = _t##_print_verbose;		\
126161200Ssam	_p->base.print_fields = _t##_print_fields;		\
127161200Ssam} while (0)
128161200Ssam#endif /* _STATFOO_H_ */
129