xref: /freebsd/usr.sbin/ppp/throughput.c (revision dbb69e525e073474073e02b8abea6484442fcda9)
1c39934eaSBrian Somers /*-
2c39934eaSBrian Somers  * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3c39934eaSBrian Somers  * All rights reserved.
4c39934eaSBrian Somers  *
5c39934eaSBrian Somers  * Redistribution and use in source and binary forms, with or without
6c39934eaSBrian Somers  * modification, are permitted provided that the following conditions
7c39934eaSBrian Somers  * are met:
8c39934eaSBrian Somers  * 1. Redistributions of source code must retain the above copyright
9c39934eaSBrian Somers  *    notice, this list of conditions and the following disclaimer.
10c39934eaSBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
11c39934eaSBrian Somers  *    notice, this list of conditions and the following disclaimer in the
12c39934eaSBrian Somers  *    documentation and/or other materials provided with the distribution.
13c39934eaSBrian Somers  *
14c39934eaSBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15c39934eaSBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c39934eaSBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c39934eaSBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18c39934eaSBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c39934eaSBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c39934eaSBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c39934eaSBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c39934eaSBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c39934eaSBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c39934eaSBrian Somers  * SUCH DAMAGE.
25c39934eaSBrian Somers  *
26dbb69e52SBrian Somers  *	$Id: throughput.c,v 1.10 1999/08/05 10:32:15 brian Exp $
279a0b991fSBrian Somers  */
289a0b991fSBrian Somers 
292764b86aSBrian Somers #include <sys/types.h>
309a0b991fSBrian Somers 
319a0b991fSBrian Somers #include <stdio.h>
32ab2de065SBrian Somers #include <stdlib.h>
33e3c70ce9SBrian Somers #include <string.h>
3485b542cfSBrian Somers #include <termios.h>
359a0b991fSBrian Somers #include <time.h>
369a0b991fSBrian Somers 
37b6e82f33SBrian Somers #include "log.h"
389a0b991fSBrian Somers #include "timer.h"
399a0b991fSBrian Somers #include "throughput.h"
4085b542cfSBrian Somers #include "descriptor.h"
4185b542cfSBrian Somers #include "prompt.h"
429a0b991fSBrian Somers 
439a0b991fSBrian Somers void
44ab2de065SBrian Somers throughput_init(struct pppThroughput *t, int period)
459a0b991fSBrian Somers {
469a0b991fSBrian Somers   t->OctetsIn = t->OctetsOut = 0;
47ab2de065SBrian Somers   t->SamplePeriod = period;
48ab2de065SBrian Somers   t->SampleOctets = (long long *)calloc(period, sizeof *t->SampleOctets);
49ab2de065SBrian Somers   t->OctetsPerSecond = t->BestOctetsPerSecond = 0;
50ab2de065SBrian Somers   t->nSample = 0;
51ab2de065SBrian Somers   time(&t->BestOctetsPerSecondTime);
52e3c70ce9SBrian Somers   memset(&t->Timer, '\0', sizeof t->Timer);
533b0f8d2eSBrian Somers   t->Timer.name = "throughput";
54565e35e5SBrian Somers   t->uptime = 0;
55ab2de065SBrian Somers   t->downtime = 0;
561342caedSBrian Somers   t->rolling = 0;
57ab2de065SBrian Somers   t->callback.data = NULL;
58ab2de065SBrian Somers   t->callback.fn = NULL;
599a0b991fSBrian Somers   throughput_stop(t);
609a0b991fSBrian Somers }
619a0b991fSBrian Somers 
629a0b991fSBrian Somers void
63ab2de065SBrian Somers throughput_destroy(struct pppThroughput *t)
64ab2de065SBrian Somers {
65ab2de065SBrian Somers   if (t && t->SampleOctets) {
66ab2de065SBrian Somers     throughput_stop(t);
67ab2de065SBrian Somers     free(t->SampleOctets);
68ab2de065SBrian Somers     t->SampleOctets = 0;
69ab2de065SBrian Somers   }
70ab2de065SBrian Somers }
71ab2de065SBrian Somers 
72ab2de065SBrian Somers int
73ab2de065SBrian Somers throughput_uptime(struct pppThroughput *t)
74ab2de065SBrian Somers {
75ab2de065SBrian Somers   time_t downat;
76ab2de065SBrian Somers 
77ab2de065SBrian Somers   downat = t->downtime ? t->downtime : time(NULL);
78dbb69e52SBrian Somers   if (t->uptime && downat < t->uptime) {
79dbb69e52SBrian Somers     /* Euch !  The clock's gone back ! */
80dbb69e52SBrian Somers     int i;
81dbb69e52SBrian Somers 
82dbb69e52SBrian Somers     for (i = 0; i < t->SamplePeriod; i++)
83dbb69e52SBrian Somers       t->SampleOctets[i] = 0;
84dbb69e52SBrian Somers     t->nSample = 0;
85dbb69e52SBrian Somers     t->uptime = downat;
86dbb69e52SBrian Somers   }
87ab2de065SBrian Somers   return t->uptime ? downat - t->uptime : 0;
88ab2de065SBrian Somers }
89ab2de065SBrian Somers 
90ab2de065SBrian Somers void
91b6217683SBrian Somers throughput_disp(struct pppThroughput *t, struct prompt *prompt)
929a0b991fSBrian Somers {
93ab2de065SBrian Somers   int secs_up, divisor;
949a0b991fSBrian Somers 
95ab2de065SBrian Somers   secs_up = throughput_uptime(t);
96ab2de065SBrian Somers   prompt_Printf(prompt, "Connect time: %d:%02d:%02d", secs_up / 3600,
97ab2de065SBrian Somers                 (secs_up / 60) % 60, secs_up % 60);
98ab2de065SBrian Somers   if (t->downtime)
99ab2de065SBrian Somers     prompt_Printf(prompt, " - down at %s", ctime(&t->downtime));
100ab2de065SBrian Somers   else
101ab2de065SBrian Somers     prompt_Printf(prompt, "\n");
102ab2de065SBrian Somers 
103ab2de065SBrian Somers   divisor = secs_up ? secs_up : 1;
1045d9e6103SBrian Somers   prompt_Printf(prompt, "%qu octets in, %qu octets out\n",
10585b542cfSBrian Somers                 t->OctetsIn, t->OctetsOut);
1061342caedSBrian Somers   if (t->rolling) {
1075d9e6103SBrian Somers     prompt_Printf(prompt, "  overall   %6qu bytes/sec\n",
108ab2de065SBrian Somers                   (t->OctetsIn + t->OctetsOut) / divisor);
109ab2de065SBrian Somers     prompt_Printf(prompt, "  %s %6qu bytes/sec (over the last"
110ab2de065SBrian Somers                   " %d secs)\n", t->downtime ? "average  " : "currently",
111ab2de065SBrian Somers                   t->OctetsPerSecond,
112ab2de065SBrian Somers                   secs_up > t->SamplePeriod ? t->SamplePeriod : secs_up);
1135d9e6103SBrian Somers     prompt_Printf(prompt, "  peak      %6qu bytes/sec on %s",
114255aa9e3SBrian Somers                   t->BestOctetsPerSecond, ctime(&t->BestOctetsPerSecondTime));
1159a0b991fSBrian Somers   } else
1165d9e6103SBrian Somers     prompt_Printf(prompt, "Overall %qu bytes/sec\n",
117ab2de065SBrian Somers                   (t->OctetsIn + t->OctetsOut) / divisor);
1189a0b991fSBrian Somers }
1199a0b991fSBrian Somers 
1209a0b991fSBrian Somers 
1219a0b991fSBrian Somers void
1229a0b991fSBrian Somers throughput_log(struct pppThroughput *t, int level, const char *title)
1239a0b991fSBrian Somers {
1249a0b991fSBrian Somers   if (t->uptime) {
1259a0b991fSBrian Somers     int secs_up;
1269a0b991fSBrian Somers 
127ab2de065SBrian Somers     secs_up = throughput_uptime(t);
1289a0b991fSBrian Somers     if (title)
1295d9e6103SBrian Somers       log_Printf(level, "%s: Connect time: %d secs: %qu octets in, %qu octets"
1309a0b991fSBrian Somers                 " out\n", title, secs_up, t->OctetsIn, t->OctetsOut);
1319a0b991fSBrian Somers     else
132ab2de065SBrian Somers       log_Printf(level, "Connect time: %d secs: %qu octets in,"
133ab2de065SBrian Somers                  " %qu octets out\n", secs_up, t->OctetsIn, t->OctetsOut);
1349a0b991fSBrian Somers     if (secs_up == 0)
1359a0b991fSBrian Somers       secs_up = 1;
1361342caedSBrian Somers     if (t->rolling)
1375d9e6103SBrian Somers       log_Printf(level, " total %qu bytes/sec, peak %qu bytes/sec on %s",
138255aa9e3SBrian Somers                  (t->OctetsIn + t->OctetsOut) / secs_up, t->BestOctetsPerSecond,
139255aa9e3SBrian Somers                  ctime(&t->BestOctetsPerSecondTime));
1409a0b991fSBrian Somers     else
1415d9e6103SBrian Somers       log_Printf(level, " total %qu bytes/sec\n",
1429a0b991fSBrian Somers                  (t->OctetsIn + t->OctetsOut) / secs_up);
1439a0b991fSBrian Somers   }
1449a0b991fSBrian Somers }
1459a0b991fSBrian Somers 
1469a0b991fSBrian Somers static void
147b6e82f33SBrian Somers throughput_sampler(void *v)
1489a0b991fSBrian Somers {
149b6e82f33SBrian Somers   struct pppThroughput *t = (struct pppThroughput *)v;
1505d9e6103SBrian Somers   unsigned long long old;
151ab2de065SBrian Somers   int uptime, divisor;
1529a0b991fSBrian Somers 
153dd7e2610SBrian Somers   timer_Stop(&t->Timer);
1549a0b991fSBrian Somers 
155ab2de065SBrian Somers   uptime = throughput_uptime(t);
156ab2de065SBrian Somers   divisor = uptime < t->SamplePeriod ? uptime + 1 : t->SamplePeriod;
1579a0b991fSBrian Somers   old = t->SampleOctets[t->nSample];
1589a0b991fSBrian Somers   t->SampleOctets[t->nSample] = t->OctetsIn + t->OctetsOut;
159ab2de065SBrian Somers   t->OctetsPerSecond = (t->SampleOctets[t->nSample] - old) / divisor;
160255aa9e3SBrian Somers   if (t->BestOctetsPerSecond < t->OctetsPerSecond) {
1619a0b991fSBrian Somers     t->BestOctetsPerSecond = t->OctetsPerSecond;
162ab2de065SBrian Somers     time(&t->BestOctetsPerSecondTime);
163255aa9e3SBrian Somers   }
164ab2de065SBrian Somers   if (++t->nSample == t->SamplePeriod)
1659a0b991fSBrian Somers     t->nSample = 0;
1669a0b991fSBrian Somers 
167ab2de065SBrian Somers   if (t->callback.fn != NULL && uptime >= t->SamplePeriod)
168ab2de065SBrian Somers     (*t->callback.fn)(t->callback.data);
169ab2de065SBrian Somers 
170dd7e2610SBrian Somers   timer_Start(&t->Timer);
1719a0b991fSBrian Somers }
1729a0b991fSBrian Somers 
1739a0b991fSBrian Somers void
1741342caedSBrian Somers throughput_start(struct pppThroughput *t, const char *name, int rolling)
1759a0b991fSBrian Somers {
176ab2de065SBrian Somers   int i;
177dd7e2610SBrian Somers   timer_Stop(&t->Timer);
178ab2de065SBrian Somers 
179ab2de065SBrian Somers   for (i = 0; i < t->SamplePeriod; i++)
180ab2de065SBrian Somers     t->SampleOctets[i] = 0;
181ab2de065SBrian Somers   t->nSample = 0;
182ab2de065SBrian Somers   t->OctetsIn = t->OctetsOut = 0;
183ab2de065SBrian Somers   t->OctetsPerSecond = t->BestOctetsPerSecond = 0;
184ab2de065SBrian Somers   time(&t->BestOctetsPerSecondTime);
185ab2de065SBrian Somers   t->downtime = 0;
1869a0b991fSBrian Somers   time(&t->uptime);
187ab2de065SBrian Somers   throughput_restart(t, name, rolling);
188ab2de065SBrian Somers }
189ab2de065SBrian Somers 
190ab2de065SBrian Somers void
191ab2de065SBrian Somers throughput_restart(struct pppThroughput *t, const char *name, int rolling)
192ab2de065SBrian Somers {
193ab2de065SBrian Somers   timer_Stop(&t->Timer);
194ab2de065SBrian Somers   t->rolling = rolling ? 1 : 0;
1951342caedSBrian Somers   if (t->rolling) {
1969a0b991fSBrian Somers     t->Timer.load = SECTICKS;
1979a0b991fSBrian Somers     t->Timer.func = throughput_sampler;
1983b0f8d2eSBrian Somers     t->Timer.name = name;
1999a0b991fSBrian Somers     t->Timer.arg = t;
200dd7e2610SBrian Somers     timer_Start(&t->Timer);
201ab2de065SBrian Somers   } else {
202ab2de065SBrian Somers     t->Timer.load = 0;
203ab2de065SBrian Somers     t->Timer.func = NULL;
204ab2de065SBrian Somers     t->Timer.name = NULL;
205ab2de065SBrian Somers     t->Timer.arg = NULL;
2069a0b991fSBrian Somers   }
2079a0b991fSBrian Somers }
2089a0b991fSBrian Somers 
2099a0b991fSBrian Somers void
2109a0b991fSBrian Somers throughput_stop(struct pppThroughput *t)
2119a0b991fSBrian Somers {
212ab2de065SBrian Somers   if (t->Timer.state != TIMER_STOPPED)
213ab2de065SBrian Somers     time(&t->downtime);
214dd7e2610SBrian Somers   timer_Stop(&t->Timer);
2159a0b991fSBrian Somers }
2169a0b991fSBrian Somers 
2179a0b991fSBrian Somers void
2185d9e6103SBrian Somers throughput_addin(struct pppThroughput *t, long long n)
2199a0b991fSBrian Somers {
2209a0b991fSBrian Somers   t->OctetsIn += n;
2219a0b991fSBrian Somers }
2229a0b991fSBrian Somers 
2239a0b991fSBrian Somers void
2245d9e6103SBrian Somers throughput_addout(struct pppThroughput *t, long long n)
2259a0b991fSBrian Somers {
2269a0b991fSBrian Somers   t->OctetsOut += n;
2279a0b991fSBrian Somers }
22841dbe0c7SBrian Somers 
22941dbe0c7SBrian Somers void
23041dbe0c7SBrian Somers throughput_clear(struct pppThroughput *t, int clear_type, struct prompt *prompt)
23141dbe0c7SBrian Somers {
23241dbe0c7SBrian Somers   if (clear_type & (THROUGHPUT_OVERALL|THROUGHPUT_CURRENT)) {
23341dbe0c7SBrian Somers     int i;
23441dbe0c7SBrian Somers 
235ab2de065SBrian Somers     for (i = 0; i < t->SamplePeriod; i++)
23641dbe0c7SBrian Somers       t->SampleOctets[i] = 0;
23741dbe0c7SBrian Somers     t->nSample = 0;
23841dbe0c7SBrian Somers   }
23941dbe0c7SBrian Somers 
24041dbe0c7SBrian Somers   if (clear_type & THROUGHPUT_OVERALL) {
241ab2de065SBrian Somers     int divisor;
24241dbe0c7SBrian Somers 
243ab2de065SBrian Somers     if ((divisor = throughput_uptime(t)) == 0)
244ab2de065SBrian Somers       divisor = 1;
2455d9e6103SBrian Somers     prompt_Printf(prompt, "overall cleared (was %6qu bytes/sec)\n",
246ab2de065SBrian Somers                   (t->OctetsIn + t->OctetsOut) / divisor);
24741dbe0c7SBrian Somers     t->OctetsIn = t->OctetsOut = 0;
248ab2de065SBrian Somers     t->downtime = 0;
249ab2de065SBrian Somers     time(&t->uptime);
25041dbe0c7SBrian Somers   }
25141dbe0c7SBrian Somers 
25241dbe0c7SBrian Somers   if (clear_type & THROUGHPUT_CURRENT) {
2535d9e6103SBrian Somers     prompt_Printf(prompt, "current cleared (was %6qu bytes/sec)\n",
25441dbe0c7SBrian Somers                   t->OctetsPerSecond);
25541dbe0c7SBrian Somers     t->OctetsPerSecond = 0;
25641dbe0c7SBrian Somers   }
25741dbe0c7SBrian Somers 
25841dbe0c7SBrian Somers   if (clear_type & THROUGHPUT_PEAK) {
25941dbe0c7SBrian Somers     char *time_buf, *last;
26041dbe0c7SBrian Somers 
26141dbe0c7SBrian Somers     time_buf = ctime(&t->BestOctetsPerSecondTime);
26241dbe0c7SBrian Somers     last = time_buf + strlen(time_buf);
26341dbe0c7SBrian Somers     if (last > time_buf && *--last == '\n')
26441dbe0c7SBrian Somers       *last = '\0';
2655d9e6103SBrian Somers     prompt_Printf(prompt, "peak    cleared (was %6qu bytes/sec on %s)\n",
26641dbe0c7SBrian Somers                   t->BestOctetsPerSecond, time_buf);
26741dbe0c7SBrian Somers     t->BestOctetsPerSecond = 0;
268ab2de065SBrian Somers     time(&t->BestOctetsPerSecondTime);
26941dbe0c7SBrian Somers   }
27041dbe0c7SBrian Somers }
271ab2de065SBrian Somers 
272ab2de065SBrian Somers void
273ab2de065SBrian Somers throughput_callback(struct pppThroughput *t, void (*fn)(void *), void *data)
274ab2de065SBrian Somers {
275ab2de065SBrian Somers   t->callback.fn = fn;
276ab2de065SBrian Somers   t->callback.data = data;
277ab2de065SBrian Somers }
278