X-Git-Url: http://git.ozlabs.org/?p=ppp.git;a=blobdiff_plain;f=pppstats%2Fpppstats.c;h=9863f32ced1fa4755f99f01564f1b041eb2a0656;hp=270ca65b851c49a7522b9918312e15bd3b323388;hb=1b024cb02056cc81b5e8a4eb3d02f50fc0bb4f5a;hpb=8fb6cb7f0bc7173864f873c1c5c192c73d43f3da diff --git a/pppstats/pppstats.c b/pppstats/pppstats.c index 270ca65..9863f32 100644 --- a/pppstats/pppstats.c +++ b/pppstats/pppstats.c @@ -6,6 +6,7 @@ * -v Verbose mode for default display * -r Show compression ratio in default display * -c Show Compression statistics instead of default display + * -a Do not show relative values. Show absolute values at all times. * * * History: @@ -36,7 +37,7 @@ */ #ifndef lint -static char rcsid[] = "$Id: pppstats.c,v 1.7 1995/05/02 04:18:40 paulus Exp $"; +static char rcsid[] = "$Id: pppstats.c,v 1.12 1995/12/11 05:18:59 paulus Exp $"; #endif #include @@ -44,31 +45,32 @@ static char rcsid[] = "$Id: pppstats.c,v 1.7 1995/05/02 04:18:40 paulus Exp $"; #include #include #include +#include #include -#include #include #include -#include -#include -#include -#include -#include -#include -#include #include +#ifdef __svr4__ +#include +#include /* SVR4, Solaris 2, etc. */ + +#else +#include +#include + #ifndef STREAMS -#include -#endif +#include /* BSD, Linux, NeXT, etc. */ -#ifdef STREAMS +#else /* SunOS 4, AIX 4, OSF/1, etc. */ #define PPP_STATS 1 /* should be defined iff it is in ppp_if.c */ #include #include #endif +#endif -int vflag, rflag, cflag; +int vflag, rflag, cflag, aflag; unsigned interval = 5; int unit; int s; /* socket file descriptor */ @@ -83,6 +85,11 @@ main(argc, argv) { --argc; ++argv; while (argc > 0) { + if (strcmp(argv[0], "-a") == 0) { + ++aflag; + ++argv, --argc; + continue; + } if (strcmp(argv[0], "-v") == 0) { ++vflag; ++argv, --argc; @@ -117,10 +124,21 @@ main(argc, argv) usage(); } +#ifdef __svr4__ + if ((s = open("/dev/ppp", O_RDONLY)) < 0) { + perror("pppstats: Couldn't open /dev/ppp: "); + exit(1); + } + if (strioctl(s, PPPIO_ATTACH, &unit, sizeof(int), 0) < 0) { + fprintf(stderr, "pppstats: ppp%d is not available\n", unit); + exit(1); + } +#else if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("couldn't create IP socket"); exit(1); } +#endif intpr(); exit(0); } @@ -131,8 +149,8 @@ usage() exit(1); } -#define V(offset) (line % 20? req.stats.offset - osc.offset: req.stats.offset) -#define W(offset) (line % 20? creq.stats.offset - csc.offset: creq.stats.offset) +#define V(offset) (line % 20? cur.offset - old.offset: cur.offset) +#define W(offset) (line % 20? ccs.offset - ocs.offset: ccs.offset) #define CRATE(comp, inc, unc) ((unc) == 0? 0.0: \ 1.0 - (double)((comp) + (inc)) / (unc)) @@ -146,36 +164,18 @@ usage() intpr() { register int line = 0; - int oldmask; - struct ifpppstatsreq req; - struct ifpppcstatsreq creq; - struct ppp_stats osc; - struct ppp_comp_stats csc; + sigset_t oldmask, mask; + struct ppp_stats cur, old; + struct ppp_comp_stats ccs, ocs; - bzero(&osc, sizeof(osc)); - bzero(&csc, sizeof(csc)); + memset(&old, 0, sizeof(old)); + memset(&ocs, 0, sizeof(ocs)); - sprintf(req.ifr_name, "ppp%d", unit); - sprintf(creq.ifr_name, "ppp%d", unit); while (1) { - if (ioctl(s, SIOCGPPPSTATS, &req) < 0) { - if (errno == ENOTTY) - fprintf(stderr, "pppstats: kernel support missing\n"); - else - perror("ioctl(SIOCGPPPSTATS)"); - exit(1); - } - if ((cflag || rflag) && ioctl(s, SIOCGPPPCSTATS, &creq) < 0) { - if (errno == ENOTTY) { - fprintf(stderr, "pppstats: no kernel compression support\n"); - if (cflag) - exit(1); - rflag = 0; - } else { - perror("ioctl(SIOCGPPPCSTATS)"); - exit(1); - } - } + get_ppp_stats(&cur); + if (cflag || rflag) + get_ppp_cstats(&ccs); + (void)signal(SIGALRM, catchalarm); signalled = 0; (void)alarm(interval); @@ -206,8 +206,8 @@ intpr() printf(" %6.6s %6.6s", "ratio", "ubyte"); putchar('\n'); } - bzero(&osc, sizeof(osc)); - bzero(&csc, sizeof(csc)); + memset(&old, 0, sizeof(old)); + memset(&ocs, 0, sizeof(ocs)); } if (cflag) { @@ -218,7 +218,7 @@ intpr() W(d.comp_packets), W(d.inc_bytes), W(d.inc_packets), - W(d.ratio) / 256.0); + W(d.ratio) == 0? 0.0: 1 - 1.0 / W(d.ratio) * 256.0); printf(" | %6d %6d %6d %6d %6d %6d %6.2f", W(c.unc_bytes), @@ -227,7 +227,7 @@ intpr() W(c.comp_packets), W(c.inc_bytes), W(c.inc_packets), - W(c.ratio) / 256.0); + W(d.ratio) == 0? 0.0: 1 - 1.0 / W(d.ratio) * 256.0); putchar('\n'); } else { @@ -263,16 +263,22 @@ intpr() line++; if (interval == 0) exit(0); - - oldmask = sigblock(sigmask(SIGALRM)); + + sigemptyset(&mask); + sigaddset(&mask, SIGALRM); + sigprocmask(SIG_BLOCK, &mask, &oldmask); if (! signalled) { - sigpause(0); + sigemptyset(&mask); + sigsuspend(&mask); } - sigsetmask(oldmask); + sigprocmask(SIG_SETMASK, &oldmask, NULL); signalled = 0; (void)alarm(interval); - osc = req.stats; - csc = creq.stats; + + if (aflag==0) { + old = cur; + ocs = ccs; + } } } @@ -285,3 +291,120 @@ void catchalarm(arg) { signalled = 1; } + +#ifndef __svr4__ +get_ppp_stats(curp) + struct ppp_stats *curp; +{ + struct ifpppstatsreq req; + + memset (&req, 0, sizeof (req)); + +#ifdef _linux_ + req.stats_ptr = (caddr_t) &req.stats; +#undef ifr_name +#define ifr_name ifr__name +#endif + + sprintf(req.ifr_name, "ppp%d", unit); + if (ioctl(s, SIOCGPPPSTATS, &req) < 0) { + if (errno == ENOTTY) + fprintf(stderr, "pppstats: kernel support missing\n"); + else + perror("ioctl(SIOCGPPPSTATS)"); + exit(1); + } + *curp = req.stats; +} + +get_ppp_cstats(csp) + struct ppp_comp_stats *csp; +{ + struct ifpppcstatsreq creq; + + memset (&creq, 0, sizeof (creq)); + +#ifdef _linux_ + creq.stats_ptr = (caddr_t) &creq.stats; +#undef ifr_name +#define ifr_name ifr__name +#endif + + sprintf(creq.ifr_name, "ppp%d", unit); + if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) { + if (errno == ENOTTY) { + fprintf(stderr, "pppstats: no kernel compression support\n"); + if (cflag) + exit(1); + rflag = 0; + } else { + perror("ioctl(SIOCGPPPCSTATS)"); + exit(1); + } + } + +#ifdef _linux_ + if (creq.stats.c.bytes_out == 0) + creq.stats.c.ratio = 0.0; + else + creq.stats.c.ratio = (double) creq.stats.c.in_count / + (double) creq.stats.c.bytes_out; + + if (creq.stats.d.bytes_out == 0) + creq.stats.d.ratio = 0.0; + else + creq.stats.d.ratio = (double) creq.stats.d.in_count / + (double) creq.stats.d.bytes_out; +#endif + + *csp = creq.stats; +} + +#else /* __svr4__ */ +get_ppp_stats(curp) + struct ppp_stats *curp; +{ + if (strioctl(s, PPPIO_GETSTAT, curp, 0, sizeof(*curp)) < 0) { + if (errno == EINVAL) + fprintf(stderr, "pppstats: kernel support missing\n"); + else + perror("pppstats: Couldn't get statistics"); + exit(1); + } +} + +get_ppp_cstats(csp) + struct ppp_comp_stats *csp; +{ + if (strioctl(s, PPPIO_GETCSTAT, csp, 0, sizeof(*csp)) < 0) { + if (errno == ENOTTY) { + fprintf(stderr, "pppstats: no kernel compression support\n"); + if (cflag) + exit(1); + rflag = 0; + } else { + perror("pppstats: Couldn't get compression statistics"); + exit(1); + } + } +} + +int +strioctl(fd, cmd, ptr, ilen, olen) + int fd, cmd, ilen, olen; + char *ptr; +{ + struct strioctl str; + + str.ic_cmd = cmd; + str.ic_timout = 0; + str.ic_len = ilen; + str.ic_dp = ptr; + if (ioctl(fd, I_STR, &str) == -1) + return -1; + if (str.ic_len != olen) + fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n", + olen, str.ic_len, cmd); + return 0; +} +#endif /* __svr4__ */