xref: /freebsd/usr.sbin/ppp/throughput.c (revision 91cbd2eec0d10f7e8be60a45cb28464a8dd47f06)
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  *
2697d92980SPeter Wemm  * $FreeBSD$
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 
4391cbd2eeSBrian Somers 
449a0b991fSBrian Somers void
45ab2de065SBrian Somers throughput_init(struct pppThroughput *t, int period)
469a0b991fSBrian Somers {
479a0b991fSBrian Somers   t->OctetsIn = t->OctetsOut = 0;
48ab2de065SBrian Somers   t->SamplePeriod = period;
4991cbd2eeSBrian Somers   t->in.SampleOctets = (long long *)
5091cbd2eeSBrian Somers     calloc(period, sizeof *t->in.SampleOctets);
5191cbd2eeSBrian Somers   t->in.OctetsPerSecond = 0;
5291cbd2eeSBrian Somers   t->out.SampleOctets = (long long *)
5391cbd2eeSBrian Somers     calloc(period, sizeof *t->out.SampleOctets);
5491cbd2eeSBrian Somers   t->out.OctetsPerSecond = 0;
5591cbd2eeSBrian Somers   t->BestOctetsPerSecond = 0;
56ab2de065SBrian Somers   t->nSample = 0;
57ab2de065SBrian Somers   time(&t->BestOctetsPerSecondTime);
58e3c70ce9SBrian Somers   memset(&t->Timer, '\0', sizeof t->Timer);
593b0f8d2eSBrian Somers   t->Timer.name = "throughput";
60565e35e5SBrian Somers   t->uptime = 0;
61ab2de065SBrian Somers   t->downtime = 0;
621342caedSBrian Somers   t->rolling = 0;
63ab2de065SBrian Somers   t->callback.data = NULL;
64ab2de065SBrian Somers   t->callback.fn = NULL;
659a0b991fSBrian Somers   throughput_stop(t);
669a0b991fSBrian Somers }
679a0b991fSBrian Somers 
689a0b991fSBrian Somers void
69ab2de065SBrian Somers throughput_destroy(struct pppThroughput *t)
70ab2de065SBrian Somers {
7191cbd2eeSBrian Somers   if (t && t->in.SampleOctets) {
72ab2de065SBrian Somers     throughput_stop(t);
7391cbd2eeSBrian Somers     free(t->in.SampleOctets);
7491cbd2eeSBrian Somers     free(t->out.SampleOctets);
7591cbd2eeSBrian Somers     t->in.SampleOctets = NULL;
7691cbd2eeSBrian Somers     t->out.SampleOctets = NULL;
77ab2de065SBrian Somers   }
78ab2de065SBrian Somers }
79ab2de065SBrian Somers 
80ab2de065SBrian Somers int
81ab2de065SBrian Somers throughput_uptime(struct pppThroughput *t)
82ab2de065SBrian Somers {
83ab2de065SBrian Somers   time_t downat;
84ab2de065SBrian Somers 
85ab2de065SBrian Somers   downat = t->downtime ? t->downtime : time(NULL);
86dbb69e52SBrian Somers   if (t->uptime && downat < t->uptime) {
87dbb69e52SBrian Somers     /* Euch !  The clock's gone back ! */
88dbb69e52SBrian Somers     int i;
89dbb69e52SBrian Somers 
90dbb69e52SBrian Somers     for (i = 0; i < t->SamplePeriod; i++)
9191cbd2eeSBrian Somers       t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
92dbb69e52SBrian Somers     t->nSample = 0;
93dbb69e52SBrian Somers     t->uptime = downat;
94dbb69e52SBrian Somers   }
95ab2de065SBrian Somers   return t->uptime ? downat - t->uptime : 0;
96ab2de065SBrian Somers }
97ab2de065SBrian Somers 
98ab2de065SBrian Somers void
99b6217683SBrian Somers throughput_disp(struct pppThroughput *t, struct prompt *prompt)
1009a0b991fSBrian Somers {
101ab2de065SBrian Somers   int secs_up, divisor;
1029a0b991fSBrian Somers 
103ab2de065SBrian Somers   secs_up = throughput_uptime(t);
104ab2de065SBrian Somers   prompt_Printf(prompt, "Connect time: %d:%02d:%02d", secs_up / 3600,
105ab2de065SBrian Somers                 (secs_up / 60) % 60, secs_up % 60);
106ab2de065SBrian Somers   if (t->downtime)
107ab2de065SBrian Somers     prompt_Printf(prompt, " - down at %s", ctime(&t->downtime));
108ab2de065SBrian Somers   else
109ab2de065SBrian Somers     prompt_Printf(prompt, "\n");
110ab2de065SBrian Somers 
111ab2de065SBrian Somers   divisor = secs_up ? secs_up : 1;
112e531f89aSBrian Somers   prompt_Printf(prompt, "%llu octets in, %llu octets out\n",
11385b542cfSBrian Somers                 t->OctetsIn, t->OctetsOut);
1141342caedSBrian Somers   if (t->rolling) {
1155d9e6103SBrian Somers     prompt_Printf(prompt, "  overall   %6qu bytes/sec\n",
116ab2de065SBrian Somers                   (t->OctetsIn + t->OctetsOut) / divisor);
11791cbd2eeSBrian Somers     prompt_Printf(prompt, "  %s %6qu bytes/sec in, %6qu bytes/sec out "
11891cbd2eeSBrian Somers                   "(over the last %d secs)\n",
11991cbd2eeSBrian Somers                   t->downtime ? "average  " : "currently",
12091cbd2eeSBrian Somers                   t->in.OctetsPerSecond, t->out.OctetsPerSecond,
121ab2de065SBrian Somers                   secs_up > t->SamplePeriod ? t->SamplePeriod : secs_up);
1225d9e6103SBrian Somers     prompt_Printf(prompt, "  peak      %6qu bytes/sec on %s",
123255aa9e3SBrian Somers                   t->BestOctetsPerSecond, ctime(&t->BestOctetsPerSecondTime));
1249a0b991fSBrian Somers   } else
125e531f89aSBrian Somers     prompt_Printf(prompt, "Overall %llu bytes/sec\n",
126ab2de065SBrian Somers                   (t->OctetsIn + t->OctetsOut) / divisor);
1279a0b991fSBrian Somers }
1289a0b991fSBrian Somers 
1299a0b991fSBrian Somers 
1309a0b991fSBrian Somers void
1319a0b991fSBrian Somers throughput_log(struct pppThroughput *t, int level, const char *title)
1329a0b991fSBrian Somers {
1339a0b991fSBrian Somers   if (t->uptime) {
1349a0b991fSBrian Somers     int secs_up;
1359a0b991fSBrian Somers 
136ab2de065SBrian Somers     secs_up = throughput_uptime(t);
13791cbd2eeSBrian Somers     if (title == NULL)
13891cbd2eeSBrian Somers       title = "";
13991cbd2eeSBrian Somers     log_Printf(level, "%s%sConnect time: %d secs: %llu octets in, %llu octets"
14091cbd2eeSBrian Somers                 " out\n", title, *title ? ": " : "", secs_up, t->OctetsIn,
14191cbd2eeSBrian Somers                 t->OctetsOut);
1429a0b991fSBrian Somers     if (secs_up == 0)
1439a0b991fSBrian Somers       secs_up = 1;
1441342caedSBrian Somers     if (t->rolling)
145e531f89aSBrian Somers       log_Printf(level, " total %llu bytes/sec, peak %llu bytes/sec on %s",
146255aa9e3SBrian Somers                  (t->OctetsIn + t->OctetsOut) / secs_up, t->BestOctetsPerSecond,
147255aa9e3SBrian Somers                  ctime(&t->BestOctetsPerSecondTime));
1489a0b991fSBrian Somers     else
149e531f89aSBrian Somers       log_Printf(level, " total %llu bytes/sec\n",
1509a0b991fSBrian Somers                  (t->OctetsIn + t->OctetsOut) / secs_up);
1519a0b991fSBrian Somers   }
1529a0b991fSBrian Somers }
1539a0b991fSBrian Somers 
1549a0b991fSBrian Somers static void
155b6e82f33SBrian Somers throughput_sampler(void *v)
1569a0b991fSBrian Somers {
157b6e82f33SBrian Somers   struct pppThroughput *t = (struct pppThroughput *)v;
1585d9e6103SBrian Somers   unsigned long long old;
159ab2de065SBrian Somers   int uptime, divisor;
16091cbd2eeSBrian Somers   unsigned long long octets;
1619a0b991fSBrian Somers 
162dd7e2610SBrian Somers   timer_Stop(&t->Timer);
1639a0b991fSBrian Somers 
164ab2de065SBrian Somers   uptime = throughput_uptime(t);
165ab2de065SBrian Somers   divisor = uptime < t->SamplePeriod ? uptime + 1 : t->SamplePeriod;
16691cbd2eeSBrian Somers 
16791cbd2eeSBrian Somers   old = t->in.SampleOctets[t->nSample];
16891cbd2eeSBrian Somers   t->in.SampleOctets[t->nSample] = t->OctetsIn;
16991cbd2eeSBrian Somers   t->in.OctetsPerSecond = (t->in.SampleOctets[t->nSample] - old) / divisor;
17091cbd2eeSBrian Somers 
17191cbd2eeSBrian Somers   old = t->out.SampleOctets[t->nSample];
17291cbd2eeSBrian Somers   t->out.SampleOctets[t->nSample] = t->OctetsOut;
17391cbd2eeSBrian Somers   t->out.OctetsPerSecond = (t->out.SampleOctets[t->nSample] - old) / divisor;
17491cbd2eeSBrian Somers 
17591cbd2eeSBrian Somers   octets = t->in.OctetsPerSecond + t->out.OctetsPerSecond;
17691cbd2eeSBrian Somers   if (t->BestOctetsPerSecond < octets) {
17791cbd2eeSBrian Somers     t->BestOctetsPerSecond = octets;
178ab2de065SBrian Somers     time(&t->BestOctetsPerSecondTime);
179255aa9e3SBrian Somers   }
18091cbd2eeSBrian Somers 
181ab2de065SBrian Somers   if (++t->nSample == t->SamplePeriod)
1829a0b991fSBrian Somers     t->nSample = 0;
1839a0b991fSBrian Somers 
184ab2de065SBrian Somers   if (t->callback.fn != NULL && uptime >= t->SamplePeriod)
185ab2de065SBrian Somers     (*t->callback.fn)(t->callback.data);
186ab2de065SBrian Somers 
187dd7e2610SBrian Somers   timer_Start(&t->Timer);
1889a0b991fSBrian Somers }
1899a0b991fSBrian Somers 
1909a0b991fSBrian Somers void
1911342caedSBrian Somers throughput_start(struct pppThroughput *t, const char *name, int rolling)
1929a0b991fSBrian Somers {
193ab2de065SBrian Somers   int i;
194dd7e2610SBrian Somers   timer_Stop(&t->Timer);
195ab2de065SBrian Somers 
196ab2de065SBrian Somers   for (i = 0; i < t->SamplePeriod; i++)
19791cbd2eeSBrian Somers     t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
198ab2de065SBrian Somers   t->nSample = 0;
199ab2de065SBrian Somers   t->OctetsIn = t->OctetsOut = 0;
20091cbd2eeSBrian Somers   t->in.OctetsPerSecond = t->out.OctetsPerSecond = t->BestOctetsPerSecond = 0;
201ab2de065SBrian Somers   time(&t->BestOctetsPerSecondTime);
202ab2de065SBrian Somers   t->downtime = 0;
2039a0b991fSBrian Somers   time(&t->uptime);
204ab2de065SBrian Somers   throughput_restart(t, name, rolling);
205ab2de065SBrian Somers }
206ab2de065SBrian Somers 
207ab2de065SBrian Somers void
208ab2de065SBrian Somers throughput_restart(struct pppThroughput *t, const char *name, int rolling)
209ab2de065SBrian Somers {
210ab2de065SBrian Somers   timer_Stop(&t->Timer);
211ab2de065SBrian Somers   t->rolling = rolling ? 1 : 0;
2121342caedSBrian Somers   if (t->rolling) {
2139a0b991fSBrian Somers     t->Timer.load = SECTICKS;
2149a0b991fSBrian Somers     t->Timer.func = throughput_sampler;
2153b0f8d2eSBrian Somers     t->Timer.name = name;
2169a0b991fSBrian Somers     t->Timer.arg = t;
217dd7e2610SBrian Somers     timer_Start(&t->Timer);
218ab2de065SBrian Somers   } else {
219ab2de065SBrian Somers     t->Timer.load = 0;
220ab2de065SBrian Somers     t->Timer.func = NULL;
221ab2de065SBrian Somers     t->Timer.name = NULL;
222ab2de065SBrian Somers     t->Timer.arg = NULL;
2239a0b991fSBrian Somers   }
2249a0b991fSBrian Somers }
2259a0b991fSBrian Somers 
2269a0b991fSBrian Somers void
2279a0b991fSBrian Somers throughput_stop(struct pppThroughput *t)
2289a0b991fSBrian Somers {
229ab2de065SBrian Somers   if (t->Timer.state != TIMER_STOPPED)
230ab2de065SBrian Somers     time(&t->downtime);
231dd7e2610SBrian Somers   timer_Stop(&t->Timer);
2329a0b991fSBrian Somers }
2339a0b991fSBrian Somers 
2349a0b991fSBrian Somers void
2355d9e6103SBrian Somers throughput_addin(struct pppThroughput *t, long long n)
2369a0b991fSBrian Somers {
2379a0b991fSBrian Somers   t->OctetsIn += n;
2389a0b991fSBrian Somers }
2399a0b991fSBrian Somers 
2409a0b991fSBrian Somers void
2415d9e6103SBrian Somers throughput_addout(struct pppThroughput *t, long long n)
2429a0b991fSBrian Somers {
2439a0b991fSBrian Somers   t->OctetsOut += n;
2449a0b991fSBrian Somers }
24541dbe0c7SBrian Somers 
24641dbe0c7SBrian Somers void
24741dbe0c7SBrian Somers throughput_clear(struct pppThroughput *t, int clear_type, struct prompt *prompt)
24841dbe0c7SBrian Somers {
24941dbe0c7SBrian Somers   if (clear_type & (THROUGHPUT_OVERALL|THROUGHPUT_CURRENT)) {
25041dbe0c7SBrian Somers     int i;
25141dbe0c7SBrian Somers 
252ab2de065SBrian Somers     for (i = 0; i < t->SamplePeriod; i++)
25391cbd2eeSBrian Somers       t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
25441dbe0c7SBrian Somers     t->nSample = 0;
25541dbe0c7SBrian Somers   }
25641dbe0c7SBrian Somers 
25741dbe0c7SBrian Somers   if (clear_type & THROUGHPUT_OVERALL) {
258ab2de065SBrian Somers     int divisor;
25941dbe0c7SBrian Somers 
260ab2de065SBrian Somers     if ((divisor = throughput_uptime(t)) == 0)
261ab2de065SBrian Somers       divisor = 1;
2625d9e6103SBrian Somers     prompt_Printf(prompt, "overall cleared (was %6qu bytes/sec)\n",
263ab2de065SBrian Somers                   (t->OctetsIn + t->OctetsOut) / divisor);
26441dbe0c7SBrian Somers     t->OctetsIn = t->OctetsOut = 0;
265ab2de065SBrian Somers     t->downtime = 0;
266ab2de065SBrian Somers     time(&t->uptime);
26741dbe0c7SBrian Somers   }
26841dbe0c7SBrian Somers 
26941dbe0c7SBrian Somers   if (clear_type & THROUGHPUT_CURRENT) {
27091cbd2eeSBrian Somers     prompt_Printf(prompt, "current cleared (was %6qu bytes/sec in,"
27191cbd2eeSBrian Somers                   " %6qu bytes/sec out)\n",
27291cbd2eeSBrian Somers                   t->in.OctetsPerSecond, t->out.OctetsPerSecond);
27391cbd2eeSBrian Somers     t->in.OctetsPerSecond = t->out.OctetsPerSecond = 0;
27441dbe0c7SBrian Somers   }
27541dbe0c7SBrian Somers 
27641dbe0c7SBrian Somers   if (clear_type & THROUGHPUT_PEAK) {
27741dbe0c7SBrian Somers     char *time_buf, *last;
27841dbe0c7SBrian Somers 
27941dbe0c7SBrian Somers     time_buf = ctime(&t->BestOctetsPerSecondTime);
28041dbe0c7SBrian Somers     last = time_buf + strlen(time_buf);
28141dbe0c7SBrian Somers     if (last > time_buf && *--last == '\n')
28241dbe0c7SBrian Somers       *last = '\0';
2835d9e6103SBrian Somers     prompt_Printf(prompt, "peak    cleared (was %6qu bytes/sec on %s)\n",
28441dbe0c7SBrian Somers                   t->BestOctetsPerSecond, time_buf);
28541dbe0c7SBrian Somers     t->BestOctetsPerSecond = 0;
286ab2de065SBrian Somers     time(&t->BestOctetsPerSecondTime);
28741dbe0c7SBrian Somers   }
28841dbe0c7SBrian Somers }
289ab2de065SBrian Somers 
290ab2de065SBrian Somers void
291ab2de065SBrian Somers throughput_callback(struct pppThroughput *t, void (*fn)(void *), void *data)
292ab2de065SBrian Somers {
293ab2de065SBrian Somers   t->callback.fn = fn;
294ab2de065SBrian Somers   t->callback.data = data;
295ab2de065SBrian Somers }
296