xref: /freebsd/usr.sbin/ppp/throughput.c (revision b6e82f33ef84ba5eedb0db2e24525e9d0f661b91)
19a0b991fSBrian Somers /*
2b6e82f33SBrian Somers  * $Id: throughput.c,v 1.1 1997/11/18 14:52:07 brian Exp $
39a0b991fSBrian Somers  */
49a0b991fSBrian Somers 
59a0b991fSBrian Somers #include <sys/param.h>
69a0b991fSBrian Somers 
79a0b991fSBrian Somers #include <stdio.h>
89a0b991fSBrian Somers #include <time.h>
99a0b991fSBrian Somers #include <netinet/in.h>
109a0b991fSBrian Somers 
11b6e82f33SBrian Somers #include "command.h"
12b6e82f33SBrian Somers #include "mbuf.h"
13b6e82f33SBrian Somers #include "log.h"
149a0b991fSBrian Somers #include "timer.h"
159a0b991fSBrian Somers #include "throughput.h"
169a0b991fSBrian Somers #include "defs.h"
179a0b991fSBrian Somers #include "loadalias.h"
189a0b991fSBrian Somers #include "vars.h"
199a0b991fSBrian Somers 
209a0b991fSBrian Somers void
219a0b991fSBrian Somers throughput_init(struct pppThroughput *t)
229a0b991fSBrian Somers {
239a0b991fSBrian Somers   int f;
249a0b991fSBrian Somers 
259a0b991fSBrian Somers   t->OctetsIn = t->OctetsOut = 0;
269a0b991fSBrian Somers   for (f = 0; f < SAMPLE_PERIOD; f++)
279a0b991fSBrian Somers     t->SampleOctets[f] = 0;
289a0b991fSBrian Somers   t->OctetsPerSecond = t->BestOctetsPerSecond = t->nSample = 0;
299a0b991fSBrian Somers   throughput_stop(t);
309a0b991fSBrian Somers }
319a0b991fSBrian Somers 
329a0b991fSBrian Somers void
339a0b991fSBrian Somers throughput_disp(struct pppThroughput *t, FILE *f)
349a0b991fSBrian Somers {
359a0b991fSBrian Somers   int secs_up;
369a0b991fSBrian Somers 
379a0b991fSBrian Somers   secs_up = time(NULL) - t->uptime;
389a0b991fSBrian Somers   fprintf(f, "Connect time: %d secs\n", secs_up);
399a0b991fSBrian Somers   if (secs_up == 0)
409a0b991fSBrian Somers     secs_up = 1;
419a0b991fSBrian Somers   fprintf(f, "%ld octets in, %ld octets out\n", t->OctetsIn, t->OctetsOut);
429a0b991fSBrian Somers   if (Enabled(ConfThroughput)) {
439a0b991fSBrian Somers     fprintf(f, "  overall   %5ld bytes/sec\n",
449a0b991fSBrian Somers             (t->OctetsIn+t->OctetsOut)/secs_up);
459a0b991fSBrian Somers     fprintf(f, "  currently %5d bytes/sec\n", t->OctetsPerSecond);
469a0b991fSBrian Somers     fprintf(f, "  peak      %5d bytes/sec\n", t->BestOctetsPerSecond);
479a0b991fSBrian Somers   } else
489a0b991fSBrian Somers     fprintf(f, "Overall %ld bytes/sec\n", (t->OctetsIn+t->OctetsOut)/secs_up);
499a0b991fSBrian Somers }
509a0b991fSBrian Somers 
519a0b991fSBrian Somers 
529a0b991fSBrian Somers void
539a0b991fSBrian Somers throughput_log(struct pppThroughput *t, int level, const char *title)
549a0b991fSBrian Somers {
559a0b991fSBrian Somers   if (t->uptime) {
569a0b991fSBrian Somers     int secs_up;
579a0b991fSBrian Somers 
589a0b991fSBrian Somers     secs_up = time(NULL) - t->uptime;
599a0b991fSBrian Somers     if (title)
609a0b991fSBrian Somers       LogPrintf(level, "%s: Connect time: %d secs: %ld octets in, %ld octets"
619a0b991fSBrian Somers                 " out\n", title, secs_up, t->OctetsIn, t->OctetsOut);
629a0b991fSBrian Somers     else
639a0b991fSBrian Somers       LogPrintf(level, "Connect time: %d secs: %ld octets in, %ld octets out\n",
649a0b991fSBrian Somers                 secs_up, t->OctetsIn, t->OctetsOut);
659a0b991fSBrian Somers     if (secs_up == 0)
669a0b991fSBrian Somers       secs_up = 1;
679a0b991fSBrian Somers     if (Enabled(ConfThroughput))
689a0b991fSBrian Somers       LogPrintf(level, " total %ld bytes/sec, peak %d bytes/sec\n",
699a0b991fSBrian Somers                 (t->OctetsIn+t->OctetsOut)/secs_up, t->BestOctetsPerSecond);
709a0b991fSBrian Somers     else
719a0b991fSBrian Somers       LogPrintf(level, " total %ld bytes/sec\n",
729a0b991fSBrian Somers                 (t->OctetsIn+t->OctetsOut)/secs_up);
739a0b991fSBrian Somers   }
749a0b991fSBrian Somers }
759a0b991fSBrian Somers 
769a0b991fSBrian Somers static void
77b6e82f33SBrian Somers throughput_sampler(void *v)
789a0b991fSBrian Somers {
79b6e82f33SBrian Somers   struct pppThroughput *t = (struct pppThroughput *)v;
809a0b991fSBrian Somers   u_long old;
819a0b991fSBrian Somers 
829a0b991fSBrian Somers   StopTimer(&t->Timer);
839a0b991fSBrian Somers   t->Timer.state = TIMER_STOPPED;
849a0b991fSBrian Somers 
859a0b991fSBrian Somers   old = t->SampleOctets[t->nSample];
869a0b991fSBrian Somers   t->SampleOctets[t->nSample] = t->OctetsIn + t->OctetsOut;
879a0b991fSBrian Somers   t->OctetsPerSecond = (t->SampleOctets[t->nSample] - old) / SAMPLE_PERIOD;
889a0b991fSBrian Somers   if (t->BestOctetsPerSecond < t->OctetsPerSecond)
899a0b991fSBrian Somers     t->BestOctetsPerSecond = t->OctetsPerSecond;
909a0b991fSBrian Somers   if (++t->nSample == SAMPLE_PERIOD)
919a0b991fSBrian Somers     t->nSample = 0;
929a0b991fSBrian Somers 
939a0b991fSBrian Somers   StartTimer(&t->Timer);
949a0b991fSBrian Somers }
959a0b991fSBrian Somers 
969a0b991fSBrian Somers void
979a0b991fSBrian Somers throughput_start(struct pppThroughput *t)
989a0b991fSBrian Somers {
999a0b991fSBrian Somers   throughput_init(t);
1009a0b991fSBrian Somers   time(&t->uptime);
1019a0b991fSBrian Somers   if (Enabled(ConfThroughput)) {
1029a0b991fSBrian Somers     t->Timer.state = TIMER_STOPPED;
1039a0b991fSBrian Somers     t->Timer.load = SECTICKS;
1049a0b991fSBrian Somers     t->Timer.func = throughput_sampler;
1059a0b991fSBrian Somers     t->Timer.arg = t;
1069a0b991fSBrian Somers     StartTimer(&t->Timer);
1079a0b991fSBrian Somers   }
1089a0b991fSBrian Somers }
1099a0b991fSBrian Somers 
1109a0b991fSBrian Somers void
1119a0b991fSBrian Somers throughput_stop(struct pppThroughput *t)
1129a0b991fSBrian Somers {
1139a0b991fSBrian Somers   if (Enabled(ConfThroughput))
1149a0b991fSBrian Somers     StopTimer(&t->Timer);
1159a0b991fSBrian Somers }
1169a0b991fSBrian Somers 
1179a0b991fSBrian Somers void
1189a0b991fSBrian Somers throughput_addin(struct pppThroughput *t, int n)
1199a0b991fSBrian Somers {
1209a0b991fSBrian Somers   t->OctetsIn += n;
1219a0b991fSBrian Somers }
1229a0b991fSBrian Somers 
1239a0b991fSBrian Somers void
1249a0b991fSBrian Somers throughput_addout(struct pppThroughput *t, int n)
1259a0b991fSBrian Somers {
1269a0b991fSBrian Somers   t->OctetsOut += n;
1279a0b991fSBrian Somers }
128