Flesh out 'athaggrstats', a utility which will display the aggregation

specific statistics for ath(4).
This commit is contained in:
Adrian Chadd 2012-06-10 06:44:04 +00:00
parent 94fe37d25c
commit 343de1138d
6 changed files with 979 additions and 0 deletions

View File

@ -0,0 +1,26 @@
# $FreeBSD$
.PATH: ${.CURDIR}/../../../../sys/dev/ath/ath_hal
PROG= athaggrstats
SRCS= main.c statfoo.c athaggrstats.c opt_ah.h ah_osdep.h
CLEANFILES+= opt_ah.h
.include <../Makefile.inc>
CFLAGS+=-DATH_SUPPORT_ANI
CFLAGS+=-DATH_SUPPORT_TDMA
opt_ah.h:
echo "#define AH_DEBUG 1" > opt_ah.h
echo "#define AH_DEBUG_COUNTRY 1" >> opt_ah.h
echo "#define AH_SUPPORT_AR5416 1" >> opt_ah.h
ah_osdep.h:
echo 'typedef void *HAL_SOFTC;' >ah_osdep.h
echo 'typedef int HAL_BUS_TAG;' >>ah_osdep.h
echo 'typedef void *HAL_BUS_HANDLE;' >>ah_osdep.h
.include <bsd.prog.mk>

View File

@ -0,0 +1,404 @@
/*-
* Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
* redistribution must be conditioned upon including a substantially
* similar Disclaimer requirement for further binary redistribution.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGES.
*
* $FreeBSD$
*/
#include "opt_ah.h"
/*
* ath statistics class.
*/
#include <sys/types.h>
#include <sys/file.h>
#include <sys/sockio.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net/if_var.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include "ah.h"
#include "ah_desc.h"
#include "net80211/ieee80211_ioctl.h"
#include "net80211/ieee80211_radiotap.h"
#include "if_athioctl.h"
#include "athaggrstats.h"
#define NOTPRESENT { 0, "", "" }
#define AFTER(prev) ((prev)+1)
static const struct fmt athaggrstats[] = {
#define S_SINGLE_PKT 0
{ 4, "singlepkt", "spkt", "single frames scheduled" },
#define S_NONBAW_PKT AFTER(S_SINGLE_PKT)
{ 5, "nonbawpkt", "nbpkt", "frames outside of the BAW" },
#define S_AGGR_PKT AFTER(S_NONBAW_PKT)
{ 6, "aggrpkt", "aggpkt", "aggregate frames scheduled" },
#define S_BAW_CLOSED_SINGLE_PKT AFTER(S_AGGR_PKT)
{ 8, "bawclosedpkt", "bawclpkt", "single frames due to closed BAW" },
#define S_LOW_HWQ_SINGLE_PKT AFTER(S_BAW_CLOSED_SINGLE_PKT)
{ 6, "lhsinglepkt", "lhspkt", "single frames scheduled due to low HWQ depth" },
#define S_SCHED_NOPKT AFTER(S_LOW_HWQ_SINGLE_PKT)
{ 6, "schednopkt", "snopkt", "sched called with no frames" },
#define S_RTS_AGGR_LIMITED AFTER(S_SCHED_NOPKT)
{ 8, "rtsaggrlimit", "rtslimit", "RTS limited aggregates" },
#define S_PKT0 AFTER(S_RTS_AGGR_LIMITED)
{ 2, "p0", "p0", "" },
#define S_PKT1 AFTER(S_PKT0)
{ 2, "p1", "p1", "" },
#define S_PKT2 AFTER(S_PKT1)
{ 2, "p2", "p2", "" },
#define S_PKT3 AFTER(S_PKT2)
{ 2, "p3", "p3", "" },
#define S_PKT4 AFTER(S_PKT3)
{ 2, "p4", "p4", "" },
#define S_PKT5 AFTER(S_PKT4)
{ 2, "p5", "p5", "" },
#define S_PKT6 AFTER(S_PKT5)
{ 2, "p6", "p6", "" },
#define S_PKT7 AFTER(S_PKT6)
{ 2, "p7", "p7", "" },
#define S_PKT8 AFTER(S_PKT7)
{ 2, "p8", "p8", "" },
#define S_PKT9 AFTER(S_PKT8)
{ 2, "p9", "p9", "" },
#define S_PKT10 AFTER(S_PKT9)
{ 3, "p10", "p10", "" },
#define S_PKT11 AFTER(S_PKT10)
{ 3, "p11", "p11", "" },
#define S_PKT12 AFTER(S_PKT11)
{ 3, "p12", "p12", "" },
#define S_PKT13 AFTER(S_PKT12)
{ 3, "p13", "p13", "" },
#define S_PKT14 AFTER(S_PKT13)
{ 3, "p14", "p14", "" },
#define S_PKT15 AFTER(S_PKT14)
{ 3, "p15", "p15", "" },
#define S_PKT16 AFTER(S_PKT15)
{ 3, "p16", "p16", "" },
#define S_PKT17 AFTER(S_PKT16)
{ 3, "p17", "p17", "" },
#define S_PKT18 AFTER(S_PKT17)
{ 3, "p18", "p18", "" },
#define S_PKT19 AFTER(S_PKT18)
{ 3, "p19", "p19", "" },
#define S_PKT20 AFTER(S_PKT19)
{ 3, "p20", "p20", "" },
#define S_PKT21 AFTER(S_PKT20)
{ 3, "p21", "p21", "" },
#define S_PKT22 AFTER(S_PKT21)
{ 3, "p22", "p22", "" },
#define S_PKT23 AFTER(S_PKT22)
{ 3, "p23", "p23", "" },
#define S_PKT24 AFTER(S_PKT23)
{ 3, "p24", "p24", "" },
#define S_PKT25 AFTER(S_PKT24)
{ 3, "p25", "p25", "" },
#define S_PKT26 AFTER(S_PKT25)
{ 3, "p26", "p26", "" },
#define S_PKT27 AFTER(S_PKT26)
{ 3, "p27", "p27", "" },
#define S_PKT28 AFTER(S_PKT27)
{ 3, "p28", "p28", "" },
#define S_PKT29 AFTER(S_PKT28)
{ 3, "p29", "p29", "" },
#define S_PKT30 AFTER(S_PKT29)
{ 3, "p30", "p30", "" },
#define S_PKT31 AFTER(S_PKT30)
{ 3, "p31", "p31", "" },
};
#define S_LAST S_RTS_AGGR_LIMITED
#define S_MAX (S_PKT31 + 1)
struct athaggrstatfoo_p {
struct athaggrstatfoo base;
int s;
int optstats;
struct ifreq ifr;
struct ath_diag atd;
struct ath_tx_aggr_stats cur;
struct ath_tx_aggr_stats total;
};
static void
ath_setifname(struct athaggrstatfoo *wf0, const char *ifname)
{
struct athaggrstatfoo_p *wf = (struct athaggrstatfoo_p *) wf0;
strncpy(wf->ifr.ifr_name, ifname, sizeof (wf->ifr.ifr_name));
}
static void
ath_zerostats(struct athaggrstatfoo *wf0)
{
#if 0
struct athaggrstatfoo_p *wf = (struct athaggrstatfoo_p *) wf0;
if (ioctl(wf->s, SIOCZATHSTATS, &wf->ifr) < 0)
err(-1, wf->ifr.ifr_name);
#endif
}
static void
ath_collect(struct athaggrstatfoo_p *wf, struct ath_tx_aggr_stats *stats)
{
wf->ifr.ifr_data = (caddr_t) stats;
if (ioctl(wf->s, SIOCGATHAGSTATS, &wf->ifr) < 0)
err(1, wf->ifr.ifr_name);
}
static void
ath_collect_cur(struct statfoo *sf)
{
struct athaggrstatfoo_p *wf = (struct athaggrstatfoo_p *) sf;
ath_collect(wf, &wf->cur);
}
static void
ath_collect_tot(struct statfoo *sf)
{
struct athaggrstatfoo_p *wf = (struct athaggrstatfoo_p *) sf;
ath_collect(wf, &wf->total);
}
static void
ath_update_tot(struct statfoo *sf)
{
struct athaggrstatfoo_p *wf = (struct athaggrstatfoo_p *) sf;
wf->total = wf->cur;
}
static void
snprintrate(char b[], size_t bs, int rate)
{
if (rate & IEEE80211_RATE_MCS)
snprintf(b, bs, "MCS%u", rate &~ IEEE80211_RATE_MCS);
else if (rate & 1)
snprintf(b, bs, "%u.5M", rate / 2);
else
snprintf(b, bs, "%uM", rate / 2);
}
static int
ath_get_curstat(struct statfoo *sf, int s, char b[], size_t bs)
{
struct athaggrstatfoo_p *wf = (struct athaggrstatfoo_p *) sf;
#define STAT(x) \
snprintf(b, bs, "%u", wf->cur.aggr_##x - wf->total.aggr_##x); return 1
#define PKT(x) \
snprintf(b, bs, "%u", wf->cur.aggr_pkts[x] - wf->total.aggr_pkts[x]); return 1
switch (s) {
case S_SINGLE_PKT: STAT(single_pkt);
case S_NONBAW_PKT: STAT(nonbaw_pkt);
case S_AGGR_PKT: STAT(aggr_pkt);
case S_BAW_CLOSED_SINGLE_PKT: STAT(baw_closed_single_pkt);
case S_LOW_HWQ_SINGLE_PKT: STAT(low_hwq_single_pkt);
case S_SCHED_NOPKT: STAT(sched_nopkt);
case S_RTS_AGGR_LIMITED: STAT(rts_aggr_limited);
case S_PKT0: PKT(0);
case S_PKT1: PKT(1);
case S_PKT2: PKT(2);
case S_PKT3: PKT(3);
case S_PKT4: PKT(4);
case S_PKT5: PKT(5);
case S_PKT6: PKT(6);
case S_PKT7: PKT(7);
case S_PKT8: PKT(8);
case S_PKT9: PKT(9);
case S_PKT10: PKT(10);
case S_PKT11: PKT(11);
case S_PKT12: PKT(12);
case S_PKT13: PKT(13);
case S_PKT14: PKT(14);
case S_PKT15: PKT(15);
case S_PKT16: PKT(16);
case S_PKT17: PKT(17);
case S_PKT18: PKT(18);
case S_PKT19: PKT(19);
case S_PKT20: PKT(20);
case S_PKT21: PKT(21);
case S_PKT22: PKT(22);
case S_PKT23: PKT(23);
case S_PKT24: PKT(24);
case S_PKT25: PKT(25);
case S_PKT26: PKT(26);
case S_PKT27: PKT(27);
case S_PKT28: PKT(28);
case S_PKT29: PKT(29);
case S_PKT30: PKT(30);
case S_PKT31: PKT(31);
}
b[0] = '\0';
return 0;
#undef PKT
#undef STAT
}
static int
ath_get_totstat(struct statfoo *sf, int s, char b[], size_t bs)
{
struct athaggrstatfoo_p *wf = (struct athaggrstatfoo_p *) sf;
#define STAT(x) \
snprintf(b, bs, "%u", wf->total.aggr_##x); return 1
#define PKT(x) \
snprintf(b, bs, "%u", wf->total.aggr_pkts[x]); return 1
switch (s) {
case S_SINGLE_PKT: STAT(single_pkt);
case S_NONBAW_PKT: STAT(nonbaw_pkt);
case S_AGGR_PKT: STAT(aggr_pkt);
case S_BAW_CLOSED_SINGLE_PKT: STAT(baw_closed_single_pkt);
case S_LOW_HWQ_SINGLE_PKT: STAT(low_hwq_single_pkt);
case S_SCHED_NOPKT: STAT(sched_nopkt);
case S_RTS_AGGR_LIMITED: STAT(rts_aggr_limited);
case S_PKT0: PKT(0);
case S_PKT1: PKT(1);
case S_PKT2: PKT(2);
case S_PKT3: PKT(3);
case S_PKT4: PKT(4);
case S_PKT5: PKT(5);
case S_PKT6: PKT(6);
case S_PKT7: PKT(7);
case S_PKT8: PKT(8);
case S_PKT9: PKT(9);
case S_PKT10: PKT(10);
case S_PKT11: PKT(11);
case S_PKT12: PKT(12);
case S_PKT13: PKT(13);
case S_PKT14: PKT(14);
case S_PKT15: PKT(15);
case S_PKT16: PKT(16);
case S_PKT17: PKT(17);
case S_PKT18: PKT(18);
case S_PKT19: PKT(19);
case S_PKT20: PKT(20);
case S_PKT21: PKT(21);
case S_PKT22: PKT(22);
case S_PKT23: PKT(23);
case S_PKT24: PKT(24);
case S_PKT25: PKT(25);
case S_PKT26: PKT(26);
case S_PKT27: PKT(27);
case S_PKT28: PKT(28);
case S_PKT29: PKT(29);
case S_PKT30: PKT(30);
case S_PKT31: PKT(31);
}
b[0] = '\0';
return 0;
#undef PKT
#undef STAT
}
static void
ath_print_verbose(struct statfoo *sf, FILE *fd)
{
struct athaggrstatfoo_p *wf = (struct athaggrstatfoo_p *) sf;
const struct fmt *f;
char s[32];
const char *indent;
int i, width;
width = 0;
for (i = 0; i < S_LAST; i++) {
f = &sf->stats[i];
if (f->width > width)
width = f->width;
}
for (i = 0; i < S_LAST; i++) {
if (ath_get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0")) {
indent = "";
fprintf(fd, "%s%-*s %s\n", indent, width, s,
athaggrstats[i].desc);
}
}
fprintf(fd, "\nAggregate size profile:\n\n");
for (i = 0; i < 64; i++) {
fprintf(fd, "%2d: %12u%s",
i,
wf->total.aggr_pkts[i],
(i % 4 == 3) ? "\n" : " ");
}
fprintf(fd, "\n");
}
STATFOO_DEFINE_BOUNCE(athaggrstatfoo)
struct athaggrstatfoo *
athaggrstats_new(const char *ifname, const char *fmtstring)
{
#define N(a) (sizeof(a) / sizeof(a[0]))
struct athaggrstatfoo_p *wf;
wf = calloc(1, sizeof(struct athaggrstatfoo_p));
if (wf != NULL) {
statfoo_init(&wf->base.base, "athaggrstats",
athaggrstats, N(athaggrstats));
/* override base methods */
wf->base.base.collect_cur = ath_collect_cur;
wf->base.base.collect_tot = ath_collect_tot;
wf->base.base.get_curstat = ath_get_curstat;
wf->base.base.get_totstat = ath_get_totstat;
wf->base.base.update_tot = ath_update_tot;
wf->base.base.print_verbose = ath_print_verbose;
/* setup bounce functions for public methods */
STATFOO_BOUNCE(wf, athaggrstatfoo);
/* setup our public methods */
wf->base.setifname = ath_setifname;
#if 0
wf->base.setstamac = wlan_setstamac;
#endif
wf->base.zerostats = ath_zerostats;
wf->s = socket(AF_INET, SOCK_DGRAM, 0);
if (wf->s < 0)
err(1, "socket");
ath_setifname(&wf->base, ifname);
wf->base.setfmt(&wf->base, fmtstring);
}
return &wf->base;
#undef N
}

View File

@ -0,0 +1,56 @@
/*-
* Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
* redistribution must be conditioned upon including a substantially
* similar Disclaimer requirement for further binary redistribution.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGES.
*
* $FreeBSD$
*/
#ifndef _ATHAGGRSTATS_H_
#define _ATHAGGRSTATS_H_
#include "statfoo.h"
/*
* ath statistics class.
*/
struct athaggrstatfoo {
struct statfoo base;
STATFOO_DECL_METHODS(struct athaggrstatfoo *);
/* set the network interface name for collection */
void (*setifname)(struct athaggrstatfoo *, const char *ifname);
/* set the mac address of the associated station/ap */
void (*setstamac)(struct athaggrstatfoo *, const uint8_t mac[]);
/* zero in-kernel statistics */
void (*zerostats)(struct athaggrstatfoo *);
};
struct athaggrstatfoo *athaggrstats_new(const char *ifname,
const char *fmtstring);
#endif /* _ATHAGGRSTATS_H_ */

View File

@ -0,0 +1,159 @@
/*-
* Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
* 2012, Adrian Chadd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
* redistribution must be conditioned upon including a substantially
* similar Disclaimer requirement for further binary redistribution.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGES.
*
* $FreeBSD$
*/
/*
* Simple Atheros-specific tool to inspect and monitor software queue
* and aggregate statistics.
*
* athaggrstats [-i interface] [-bz] [-l] [-o fmtstring] [interval]
*
* (default interface is ath0). If interval is specified a rolling output
* a la netstat -i is displayed every interval seconds. The format of
* the rolling display can be controlled a la ps. The -l option will
* print a list of all possible statistics for use with the -o option.
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include "athaggrstats.h"
static struct {
const char *tag;
const char *fmt;
} tags[] = {
{ "default",
"singlepkt,nonbawpkt,aggrpkt,bawclosedpkt,lhsinglepkt,schednopkt,rtsaggrlimit"
},
};
static const char *
getfmt(const char *tag)
{
#define N(a) (sizeof(a)/sizeof(a[0]))
int i;
for (i = 0; i < N(tags); i++)
if (strcasecmp(tags[i].tag, tag) == 0)
return tags[i].fmt;
return tag;
#undef N
}
static int signalled;
static void
catchalarm(int signo __unused)
{
signalled = 1;
}
int
main(int argc, char *argv[])
{
struct athaggrstatfoo *wf;
const char *ifname;
int c, banner = 1;
ifname = getenv("ATH");
if (ifname == NULL)
ifname = "ath0";
wf = athaggrstats_new(ifname, getfmt("default"));
while ((c = getopt(argc, argv, "bi:lo:z")) != -1) {
switch (c) {
case 'b':
banner = 0;
break;
case 'i':
wf->setifname(wf, optarg);
break;
case 'l':
wf->print_fields(wf, stdout);
return 0;
case 'o':
wf->setfmt(wf, getfmt(optarg));
break;
case 'z':
wf->zerostats(wf);
break;
default:
errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [-z] [interval]\n", argv[0]);
/*NOTREACHED*/
}
}
argc -= optind;
argv += optind;
if (argc > 0) {
u_long interval = strtoul(argv[0], NULL, 0);
int line, omask;
if (interval < 1)
interval = 1;
signal(SIGALRM, catchalarm);
signalled = 0;
alarm(interval);
banner:
if (banner)
wf->print_header(wf, stdout);
line = 0;
loop:
if (line != 0) {
wf->collect_cur(wf);
wf->print_current(wf, stdout);
wf->update_tot(wf);
} else {
wf->collect_tot(wf);
wf->print_total(wf, stdout);
}
fflush(stdout);
omask = sigblock(sigmask(SIGALRM));
if (!signalled)
sigpause(0);
sigsetmask(omask);
signalled = 0;
alarm(interval);
line++;
if (line == 21) /* XXX tty line count */
goto banner;
else
goto loop;
/*NOTREACHED*/
} else {
wf->collect_tot(wf);
wf->print_verbose(wf, stdout);
}
return 0;
}

View File

@ -0,0 +1,206 @@
/*-
* Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
* redistribution must be conditioned upon including a substantially
* similar Disclaimer requirement for further binary redistribution.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGES.
*
* $FreeBSD$
*/
#include <stdio.h>
#include <string.h>
#include "statfoo.h"
static void
statfoo_setfmt(struct statfoo *sf, const char *fmt0)
{
#define N(a) (sizeof(a)/sizeof(a[0]))
char fmt[4096];
char *fp, *tok;
int i, j;
j = 0;
strlcpy(fmt, fmt0, sizeof(fmt));
for (fp = fmt; (tok = strsep(&fp, ", ")) != NULL;) {
for (i = 0; i < sf->nstats; i++)
if (strcasecmp(tok, sf->stats[i].name) == 0)
break;
if (i >= sf->nstats) {
fprintf(stderr, "%s: unknown statistic name \"%s\" "
"skipped\n", sf->name, tok);
continue;
}
if (j+3 > sizeof(sf->fmts)) {
fprintf(stderr, "%s: not enough room for all stats; "
"stopped at %s\n", sf->name, tok);
break;
}
if (j != 0)
sf->fmts[j++] = ' ';
sf->fmts[j++] = FMTS_IS_STAT;
sf->fmts[j++] = i & 0xff;
sf->fmts[j++] = (i >> 8) & 0xff;
}
sf->fmts[j] = '\0';
#undef N
}
static void
statfoo_collect(struct statfoo *sf)
{
fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
}
static void
statfoo_update_tot(struct statfoo *sf)
{
fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
}
static int
statfoo_get(struct statfoo *sf, int s, char b[], size_t bs)
{
fprintf(stderr, "%s: don't know how to get stat #%u\n", sf->name, s);
return 0;
}
static void
statfoo_print_header(struct statfoo *sf, FILE *fd)
{
const unsigned char *cp;
int i;
const struct fmt *f;
for (cp = sf->fmts; *cp != '\0'; cp++) {
if (*cp == FMTS_IS_STAT) {
i = *(++cp);
i |= ((int) *(++cp)) << 8;
f = &sf->stats[i];
fprintf(fd, "%*s", f->width, f->label);
} else
putc(*cp, fd);
}
putc('\n', fd);
}
static void
statfoo_print_current(struct statfoo *sf, FILE *fd)
{
char buf[32];
const unsigned char *cp;
int i;
const struct fmt *f;
for (cp = sf->fmts; *cp != '\0'; cp++) {
if (*cp == FMTS_IS_STAT) {
i = *(++cp);
i |= ((int) *(++cp)) << 8;
f = &sf->stats[i];
if (sf->get_curstat(sf, i, buf, sizeof(buf)))
fprintf(fd, "%*s", f->width, buf);
} else
putc(*cp, fd);
}
putc('\n', fd);
}
static void
statfoo_print_total(struct statfoo *sf, FILE *fd)
{
char buf[32];
const unsigned char *cp;
const struct fmt *f;
int i;
for (cp = sf->fmts; *cp != '\0'; cp++) {
if (*cp == FMTS_IS_STAT) {
i = *(++cp);
i |= ((int) *(++cp)) << 8;
f = &sf->stats[i];
if (sf->get_totstat(sf, i, buf, sizeof(buf)))
fprintf(fd, "%*s", f->width, buf);
} else
putc(*cp, fd);
}
putc('\n', fd);
}
static void
statfoo_print_verbose(struct statfoo *sf, FILE *fd)
{
const struct fmt *f;
char s[32];
int i, width;
width = 0;
for (i = 0; i < sf->nstats; i++) {
f = &sf->stats[i];
if (f->width > width)
width = f->width;
}
for (i = 0; i < sf->nstats; i++) {
f = &sf->stats[i];
if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
fprintf(fd, "%-*s %s\n", width, s, f->desc);
}
}
static void
statfoo_print_fields(struct statfoo *sf, FILE *fd)
{
int i, w, width;
width = 0;
for (i = 0; i < sf->nstats; i++) {
w = strlen(sf->stats[i].name);
if (w > width)
width = w;
}
for (i = 0; i < sf->nstats; i++) {
const struct fmt *f = &sf->stats[i];
if (f->width != 0)
fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
}
}
void
statfoo_init(struct statfoo *sf, const char *name, const struct fmt *stats, int nstats)
{
sf->name = name;
sf->stats = stats;
sf->nstats = nstats;
sf->setfmt = statfoo_setfmt;
sf->collect_cur = statfoo_collect;
sf->collect_tot = statfoo_collect;
sf->update_tot = statfoo_update_tot;
sf->get_curstat = statfoo_get;
sf->get_totstat = statfoo_get;
sf->print_header = statfoo_print_header;
sf->print_current = statfoo_print_current;
sf->print_total = statfoo_print_total;
sf->print_verbose = statfoo_print_verbose;
sf->print_fields = statfoo_print_fields;
}

View File

@ -0,0 +1,128 @@
/*-
* Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
* redistribution must be conditioned upon including a substantially
* similar Disclaimer requirement for further binary redistribution.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGES.
*
* $FreeBSD$
*/
#ifndef _STATFOO_H_
#define _STATFOO_H_
/*
* Base class for managing+displaying periodically collected statistics.
*/
/*
* Statistic definition/description. The are defined
* for stats that correspond 1-1 w/ a collected stat
* and for stats that are calculated indirectly.
*/
struct fmt {
int width; /* printed field width */
const char* name; /* stat field name referenced by user */
const char* label; /* printed header label */
const char* desc; /* verbose description */
};
#define STATFOO_DECL_METHODS(_p) \
/* set the format of the statistics to display */ \
void (*setfmt)(_p, const char *); \
/* collect+store ``current statistics'' */ \
void (*collect_cur)(_p); \
/* collect+store ``total statistics'' */ \
void (*collect_tot)(_p); \
/* update ``total statistics'' if necessary from current */ \
void (*update_tot)(_p); \
/* format a statistic from the current stats */ \
int (*get_curstat)(_p, int, char [], size_t); \
/* format a statistic from the total stats */ \
int (*get_totstat)(_p, int, char [], size_t); \
/* print field headers terminated by a \n */ \
void (*print_header)(_p, FILE *); \
/* print current statistics terminated by a \n */ \
void (*print_current)(_p, FILE *); \
/* print total statistics terminated by a \n */ \
void (*print_total)(_p, FILE *); \
/* print total statistics in a verbose (1 stat/line) format */ \
void (*print_verbose)(_p, FILE *); \
/* print available statistics */ \
void (*print_fields)(_p, FILE *)
/*
* Statistics base class. This class is not usable; only
* classes derived from it are useful.
*/
struct statfoo {
const char *name; /* statistics name, e.g. wlanstats */
const struct fmt *stats; /* statistics in class */
int nstats; /* number of stats */
#define FMTS_IS_STAT 0x80 /* the following two bytes are the stat id */
unsigned char fmts[4096]; /* private: compiled stats to display */
STATFOO_DECL_METHODS(struct statfoo *);
};
void statfoo_init(struct statfoo *, const char *name,
const struct fmt *stats, int nstats);
#define STATFOO_DEFINE_BOUNCE(_t) \
static void _t##_setfmt(struct _t *wf, const char *fmt0) \
{ wf->base.setfmt(&wf->base, fmt0); } \
static void _t##_collect_cur(struct _t *wf) \
{ wf->base.collect_cur(&wf->base); } \
static void _t##_collect_tot(struct _t *wf) \
{ wf->base.collect_tot(&wf->base); } \
static void _t##_update_tot(struct _t *wf) \
{ wf->base.update_tot(&wf->base); } \
static int _t##_get_curstat(struct _t *wf, int s, char b[], size_t bs) \
{ return wf->base.get_curstat(&wf->base, s, b, bs); } \
static int _t##_get_totstat(struct _t *wf, int s, char b[], size_t bs) \
{ return wf->base.get_totstat(&wf->base, s, b, bs); } \
static void _t##_print_header(struct _t *wf, FILE *fd) \
{ wf->base.print_header(&wf->base, fd); } \
static void _t##_print_current(struct _t *wf, FILE *fd) \
{ wf->base.print_current(&wf->base, fd); } \
static void _t##_print_total(struct _t *wf, FILE *fd) \
{ wf->base.print_total(&wf->base, fd); } \
static void _t##_print_verbose(struct _t *wf, FILE *fd) \
{ wf->base.print_verbose(&wf->base, fd); } \
static void _t##_print_fields(struct _t *wf, FILE *fd) \
{ wf->base.print_fields(&wf->base, fd); }
#define STATFOO_BOUNCE(_p, _t) do { \
_p->base.setfmt = _t##_setfmt; \
_p->base.collect_cur = _t##_collect_cur; \
_p->base.collect_tot = _t##_collect_tot; \
_p->base.update_tot = _t##_update_tot; \
_p->base.get_curstat = _t##_get_curstat; \
_p->base.get_totstat = _t##_get_totstat; \
_p->base.print_header = _t##_print_header; \
_p->base.print_current = _t##_print_current; \
_p->base.print_total = _t##_print_total; \
_p->base.print_verbose = _t##_print_verbose; \
_p->base.print_fields = _t##_print_fields; \
} while (0)
#endif /* _STATFOO_H_ */