/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2002-2007 Sam Leffler, Errno Consulting4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer,11* without modification.12* 2. Redistributions in binary form must reproduce at minimum a disclaimer13* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any14* redistribution must be conditioned upon including a substantially15* similar Disclaimer requirement for further binary redistribution.16*17* NO WARRANTY18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY21* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL22* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,23* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER26* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF28* THE POSSIBILITY OF SUCH DAMAGES.29*/3031#ifndef _BSDSTAT_H_32#define _BSDSTAT_H_33/*34* Base class for managing+displaying periodically collected statistics.35*/3637/*38* Statistic definition/description. These are defined39* for stats that correspond 1-1 w/ a collected stat40* and for stats that are calculated indirectly.41*/42struct fmt {43int width; /* printed field width */44const char* name; /* stat field name referenced by user */45const char* label; /* printed header label */46const char* desc; /* verbose description */47};4849#define BSDSTAT_DECL_METHODS(_p) \50/* set the format of the statistics to display */ \51void (*setfmt)(_p, const char *); \52/* collect+store ``current statistics'' */ \53void (*collect_cur)(_p); \54/* collect+store ``total statistics'' */ \55void (*collect_tot)(_p); \56/* update ``total statistics'' if necessary from current */ \57void (*update_tot)(_p); \58/* format a statistic from the current stats */ \59int (*get_curstat)(_p, int, char [], size_t); \60/* format a statistic from the total stats */ \61int (*get_totstat)(_p, int, char [], size_t); \62/* print field headers terminated by a \n */ \63void (*print_header)(_p, FILE *); \64/* print current statistics terminated by a \n */ \65void (*print_current)(_p, FILE *); \66/* print total statistics terminated by a \n */ \67void (*print_total)(_p, FILE *); \68/* print total statistics in a verbose (1 stat/line) format */ \69void (*print_verbose)(_p, FILE *); \70/* print available statistics */ \71void (*print_fields)(_p, FILE *)7273/*74* Statistics base class. This class is not usable; only75* classes derived from it are useful.76*/77struct bsdstat {78const char *name; /* statistics name, e.g. wlanstat */79const struct fmt *stats; /* statistics in class */80int nstats; /* number of stats */81#define FMTS_IS_STAT 0x80 /* the following two bytes are the stat id */82unsigned char fmts[4096]; /* private: compiled stats to display */8384BSDSTAT_DECL_METHODS(struct bsdstat *);85};8687extern void bsdstat_init(struct bsdstat *, const char *name,88const struct fmt *stats, int nstats);8990#define BSDSTAT_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); }113114#define BSDSTAT_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 /* _BSDSTAT_H_ */128129130