throughput.c revision 31343
1/* 2 * $Id: throughput.c,v 1.1 1997/11/18 14:52:07 brian Exp $ 3 */ 4 5#include <sys/param.h> 6 7#include <stdio.h> 8#include <time.h> 9#include <netinet/in.h> 10 11#include "command.h" 12#include "mbuf.h" 13#include "log.h" 14#include "timer.h" 15#include "throughput.h" 16#include "defs.h" 17#include "loadalias.h" 18#include "vars.h" 19 20void 21throughput_init(struct pppThroughput *t) 22{ 23 int f; 24 25 t->OctetsIn = t->OctetsOut = 0; 26 for (f = 0; f < SAMPLE_PERIOD; f++) 27 t->SampleOctets[f] = 0; 28 t->OctetsPerSecond = t->BestOctetsPerSecond = t->nSample = 0; 29 throughput_stop(t); 30} 31 32void 33throughput_disp(struct pppThroughput *t, FILE *f) 34{ 35 int secs_up; 36 37 secs_up = time(NULL) - t->uptime; 38 fprintf(f, "Connect time: %d secs\n", secs_up); 39 if (secs_up == 0) 40 secs_up = 1; 41 fprintf(f, "%ld octets in, %ld octets out\n", t->OctetsIn, t->OctetsOut); 42 if (Enabled(ConfThroughput)) { 43 fprintf(f, " overall %5ld bytes/sec\n", 44 (t->OctetsIn+t->OctetsOut)/secs_up); 45 fprintf(f, " currently %5d bytes/sec\n", t->OctetsPerSecond); 46 fprintf(f, " peak %5d bytes/sec\n", t->BestOctetsPerSecond); 47 } else 48 fprintf(f, "Overall %ld bytes/sec\n", (t->OctetsIn+t->OctetsOut)/secs_up); 49} 50 51 52void 53throughput_log(struct pppThroughput *t, int level, const char *title) 54{ 55 if (t->uptime) { 56 int secs_up; 57 58 secs_up = time(NULL) - t->uptime; 59 if (title) 60 LogPrintf(level, "%s: Connect time: %d secs: %ld octets in, %ld octets" 61 " out\n", title, secs_up, t->OctetsIn, t->OctetsOut); 62 else 63 LogPrintf(level, "Connect time: %d secs: %ld octets in, %ld octets out\n", 64 secs_up, t->OctetsIn, t->OctetsOut); 65 if (secs_up == 0) 66 secs_up = 1; 67 if (Enabled(ConfThroughput)) 68 LogPrintf(level, " total %ld bytes/sec, peak %d bytes/sec\n", 69 (t->OctetsIn+t->OctetsOut)/secs_up, t->BestOctetsPerSecond); 70 else 71 LogPrintf(level, " total %ld bytes/sec\n", 72 (t->OctetsIn+t->OctetsOut)/secs_up); 73 } 74} 75 76static void 77throughput_sampler(void *v) 78{ 79 struct pppThroughput *t = (struct pppThroughput *)v; 80 u_long old; 81 82 StopTimer(&t->Timer); 83 t->Timer.state = TIMER_STOPPED; 84 85 old = t->SampleOctets[t->nSample]; 86 t->SampleOctets[t->nSample] = t->OctetsIn + t->OctetsOut; 87 t->OctetsPerSecond = (t->SampleOctets[t->nSample] - old) / SAMPLE_PERIOD; 88 if (t->BestOctetsPerSecond < t->OctetsPerSecond) 89 t->BestOctetsPerSecond = t->OctetsPerSecond; 90 if (++t->nSample == SAMPLE_PERIOD) 91 t->nSample = 0; 92 93 StartTimer(&t->Timer); 94} 95 96void 97throughput_start(struct pppThroughput *t) 98{ 99 throughput_init(t); 100 time(&t->uptime); 101 if (Enabled(ConfThroughput)) { 102 t->Timer.state = TIMER_STOPPED; 103 t->Timer.load = SECTICKS; 104 t->Timer.func = throughput_sampler; 105 t->Timer.arg = t; 106 StartTimer(&t->Timer); 107 } 108} 109 110void 111throughput_stop(struct pppThroughput *t) 112{ 113 if (Enabled(ConfThroughput)) 114 StopTimer(&t->Timer); 115} 116 117void 118throughput_addin(struct pppThroughput *t, int n) 119{ 120 t->OctetsIn += n; 121} 122 123void 124throughput_addout(struct pppThroughput *t, int n) 125{ 126 t->OctetsOut += n; 127} 128