xref: /freebsd/usr.sbin/ppp/throughput.c (revision 794c9bbc803a1898f02495519de1fde31500aa3b)
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 {
47794c9bbcSBrian Somers   t->OctetsIn = t->OctetsOut = t->PacketsIn = t->PacketsOut = 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);
114794c9bbcSBrian Somers   prompt_Printf(prompt, "%llu packets in, %llu packets out\n",
115794c9bbcSBrian Somers                 t->PacketsIn, t->PacketsOut);
1161342caedSBrian Somers   if (t->rolling) {
1175d9e6103SBrian Somers     prompt_Printf(prompt, "  overall   %6qu bytes/sec\n",
118ab2de065SBrian Somers                   (t->OctetsIn + t->OctetsOut) / divisor);
11991cbd2eeSBrian Somers     prompt_Printf(prompt, "  %s %6qu bytes/sec in, %6qu bytes/sec out "
12091cbd2eeSBrian Somers                   "(over the last %d secs)\n",
12191cbd2eeSBrian Somers                   t->downtime ? "average  " : "currently",
12291cbd2eeSBrian Somers                   t->in.OctetsPerSecond, t->out.OctetsPerSecond,
123ab2de065SBrian Somers                   secs_up > t->SamplePeriod ? t->SamplePeriod : secs_up);
1245d9e6103SBrian Somers     prompt_Printf(prompt, "  peak      %6qu bytes/sec on %s",
125255aa9e3SBrian Somers                   t->BestOctetsPerSecond, ctime(&t->BestOctetsPerSecondTime));
1269a0b991fSBrian Somers   } else
127e531f89aSBrian Somers     prompt_Printf(prompt, "Overall %llu bytes/sec\n",
128ab2de065SBrian Somers                   (t->OctetsIn + t->OctetsOut) / divisor);
1299a0b991fSBrian Somers }
1309a0b991fSBrian Somers 
1319a0b991fSBrian Somers 
1329a0b991fSBrian Somers void
1339a0b991fSBrian Somers throughput_log(struct pppThroughput *t, int level, const char *title)
1349a0b991fSBrian Somers {
1359a0b991fSBrian Somers   if (t->uptime) {
1369a0b991fSBrian Somers     int secs_up;
1379a0b991fSBrian Somers 
138ab2de065SBrian Somers     secs_up = throughput_uptime(t);
13991cbd2eeSBrian Somers     if (title == NULL)
14091cbd2eeSBrian Somers       title = "";
14191cbd2eeSBrian Somers     log_Printf(level, "%s%sConnect time: %d secs: %llu octets in, %llu octets"
14291cbd2eeSBrian Somers                " out\n", title, *title ? ": " : "", secs_up, t->OctetsIn,
14391cbd2eeSBrian Somers                t->OctetsOut);
144794c9bbcSBrian Somers     log_Printf(level, "%s%s: %llu packets in, %llu packets out\n",
145794c9bbcSBrian Somers                title, *title ? ": " : "",  t->PacketsIn, t->PacketsOut);
1469a0b991fSBrian Somers     if (secs_up == 0)
1479a0b991fSBrian Somers       secs_up = 1;
1481342caedSBrian Somers     if (t->rolling)
149e531f89aSBrian Somers       log_Printf(level, " total %llu bytes/sec, peak %llu bytes/sec on %s",
150255aa9e3SBrian Somers                  (t->OctetsIn + t->OctetsOut) / secs_up, t->BestOctetsPerSecond,
151255aa9e3SBrian Somers                  ctime(&t->BestOctetsPerSecondTime));
1529a0b991fSBrian Somers     else
153e531f89aSBrian Somers       log_Printf(level, " total %llu bytes/sec\n",
1549a0b991fSBrian Somers                  (t->OctetsIn + t->OctetsOut) / secs_up);
1559a0b991fSBrian Somers   }
1569a0b991fSBrian Somers }
1579a0b991fSBrian Somers 
1589a0b991fSBrian Somers static void
159b6e82f33SBrian Somers throughput_sampler(void *v)
1609a0b991fSBrian Somers {
161b6e82f33SBrian Somers   struct pppThroughput *t = (struct pppThroughput *)v;
1625d9e6103SBrian Somers   unsigned long long old;
163ab2de065SBrian Somers   int uptime, divisor;
16491cbd2eeSBrian Somers   unsigned long long octets;
1659a0b991fSBrian Somers 
166dd7e2610SBrian Somers   timer_Stop(&t->Timer);
1679a0b991fSBrian Somers 
168ab2de065SBrian Somers   uptime = throughput_uptime(t);
169ab2de065SBrian Somers   divisor = uptime < t->SamplePeriod ? uptime + 1 : t->SamplePeriod;
17091cbd2eeSBrian Somers 
17191cbd2eeSBrian Somers   old = t->in.SampleOctets[t->nSample];
17291cbd2eeSBrian Somers   t->in.SampleOctets[t->nSample] = t->OctetsIn;
17391cbd2eeSBrian Somers   t->in.OctetsPerSecond = (t->in.SampleOctets[t->nSample] - old) / divisor;
17491cbd2eeSBrian Somers 
17591cbd2eeSBrian Somers   old = t->out.SampleOctets[t->nSample];
17691cbd2eeSBrian Somers   t->out.SampleOctets[t->nSample] = t->OctetsOut;
17791cbd2eeSBrian Somers   t->out.OctetsPerSecond = (t->out.SampleOctets[t->nSample] - old) / divisor;
17891cbd2eeSBrian Somers 
17991cbd2eeSBrian Somers   octets = t->in.OctetsPerSecond + t->out.OctetsPerSecond;
18091cbd2eeSBrian Somers   if (t->BestOctetsPerSecond < octets) {
18191cbd2eeSBrian Somers     t->BestOctetsPerSecond = octets;
182ab2de065SBrian Somers     time(&t->BestOctetsPerSecondTime);
183255aa9e3SBrian Somers   }
18491cbd2eeSBrian Somers 
185ab2de065SBrian Somers   if (++t->nSample == t->SamplePeriod)
1869a0b991fSBrian Somers     t->nSample = 0;
1879a0b991fSBrian Somers 
188ab2de065SBrian Somers   if (t->callback.fn != NULL && uptime >= t->SamplePeriod)
189ab2de065SBrian Somers     (*t->callback.fn)(t->callback.data);
190ab2de065SBrian Somers 
191dd7e2610SBrian Somers   timer_Start(&t->Timer);
1929a0b991fSBrian Somers }
1939a0b991fSBrian Somers 
1949a0b991fSBrian Somers void
1951342caedSBrian Somers throughput_start(struct pppThroughput *t, const char *name, int rolling)
1969a0b991fSBrian Somers {
197ab2de065SBrian Somers   int i;
198dd7e2610SBrian Somers   timer_Stop(&t->Timer);
199ab2de065SBrian Somers 
200ab2de065SBrian Somers   for (i = 0; i < t->SamplePeriod; i++)
20191cbd2eeSBrian Somers     t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
202ab2de065SBrian Somers   t->nSample = 0;
203ab2de065SBrian Somers   t->OctetsIn = t->OctetsOut = 0;
20491cbd2eeSBrian Somers   t->in.OctetsPerSecond = t->out.OctetsPerSecond = t->BestOctetsPerSecond = 0;
205ab2de065SBrian Somers   time(&t->BestOctetsPerSecondTime);
206ab2de065SBrian Somers   t->downtime = 0;
2079a0b991fSBrian Somers   time(&t->uptime);
208ab2de065SBrian Somers   throughput_restart(t, name, rolling);
209ab2de065SBrian Somers }
210ab2de065SBrian Somers 
211ab2de065SBrian Somers void
212ab2de065SBrian Somers throughput_restart(struct pppThroughput *t, const char *name, int rolling)
213ab2de065SBrian Somers {
214ab2de065SBrian Somers   timer_Stop(&t->Timer);
215ab2de065SBrian Somers   t->rolling = rolling ? 1 : 0;
2161342caedSBrian Somers   if (t->rolling) {
2179a0b991fSBrian Somers     t->Timer.load = SECTICKS;
2189a0b991fSBrian Somers     t->Timer.func = throughput_sampler;
2193b0f8d2eSBrian Somers     t->Timer.name = name;
2209a0b991fSBrian Somers     t->Timer.arg = t;
221dd7e2610SBrian Somers     timer_Start(&t->Timer);
222ab2de065SBrian Somers   } else {
223ab2de065SBrian Somers     t->Timer.load = 0;
224ab2de065SBrian Somers     t->Timer.func = NULL;
225ab2de065SBrian Somers     t->Timer.name = NULL;
226ab2de065SBrian Somers     t->Timer.arg = NULL;
2279a0b991fSBrian Somers   }
2289a0b991fSBrian Somers }
2299a0b991fSBrian Somers 
2309a0b991fSBrian Somers void
2319a0b991fSBrian Somers throughput_stop(struct pppThroughput *t)
2329a0b991fSBrian Somers {
233ab2de065SBrian Somers   if (t->Timer.state != TIMER_STOPPED)
234ab2de065SBrian Somers     time(&t->downtime);
235dd7e2610SBrian Somers   timer_Stop(&t->Timer);
2369a0b991fSBrian Somers }
2379a0b991fSBrian Somers 
2389a0b991fSBrian Somers void
2395d9e6103SBrian Somers throughput_addin(struct pppThroughput *t, long long n)
2409a0b991fSBrian Somers {
2419a0b991fSBrian Somers   t->OctetsIn += n;
242794c9bbcSBrian Somers   t->PacketsIn++;
2439a0b991fSBrian Somers }
2449a0b991fSBrian Somers 
2459a0b991fSBrian Somers void
2465d9e6103SBrian Somers throughput_addout(struct pppThroughput *t, long long n)
2479a0b991fSBrian Somers {
2489a0b991fSBrian Somers   t->OctetsOut += n;
249794c9bbcSBrian Somers   t->PacketsOut++;
2509a0b991fSBrian Somers }
25141dbe0c7SBrian Somers 
25241dbe0c7SBrian Somers void
25341dbe0c7SBrian Somers throughput_clear(struct pppThroughput *t, int clear_type, struct prompt *prompt)
25441dbe0c7SBrian Somers {
25541dbe0c7SBrian Somers   if (clear_type & (THROUGHPUT_OVERALL|THROUGHPUT_CURRENT)) {
25641dbe0c7SBrian Somers     int i;
25741dbe0c7SBrian Somers 
258ab2de065SBrian Somers     for (i = 0; i < t->SamplePeriod; i++)
25991cbd2eeSBrian Somers       t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
26041dbe0c7SBrian Somers     t->nSample = 0;
26141dbe0c7SBrian Somers   }
26241dbe0c7SBrian Somers 
26341dbe0c7SBrian Somers   if (clear_type & THROUGHPUT_OVERALL) {
264ab2de065SBrian Somers     int divisor;
26541dbe0c7SBrian Somers 
266ab2de065SBrian Somers     if ((divisor = throughput_uptime(t)) == 0)
267ab2de065SBrian Somers       divisor = 1;
2685d9e6103SBrian Somers     prompt_Printf(prompt, "overall cleared (was %6qu bytes/sec)\n",
269ab2de065SBrian Somers                   (t->OctetsIn + t->OctetsOut) / divisor);
27041dbe0c7SBrian Somers     t->OctetsIn = t->OctetsOut = 0;
271ab2de065SBrian Somers     t->downtime = 0;
272ab2de065SBrian Somers     time(&t->uptime);
27341dbe0c7SBrian Somers   }
27441dbe0c7SBrian Somers 
27541dbe0c7SBrian Somers   if (clear_type & THROUGHPUT_CURRENT) {
27691cbd2eeSBrian Somers     prompt_Printf(prompt, "current cleared (was %6qu bytes/sec in,"
27791cbd2eeSBrian Somers                   " %6qu bytes/sec out)\n",
27891cbd2eeSBrian Somers                   t->in.OctetsPerSecond, t->out.OctetsPerSecond);
27991cbd2eeSBrian Somers     t->in.OctetsPerSecond = t->out.OctetsPerSecond = 0;
28041dbe0c7SBrian Somers   }
28141dbe0c7SBrian Somers 
28241dbe0c7SBrian Somers   if (clear_type & THROUGHPUT_PEAK) {
28341dbe0c7SBrian Somers     char *time_buf, *last;
28441dbe0c7SBrian Somers 
28541dbe0c7SBrian Somers     time_buf = ctime(&t->BestOctetsPerSecondTime);
28641dbe0c7SBrian Somers     last = time_buf + strlen(time_buf);
28741dbe0c7SBrian Somers     if (last > time_buf && *--last == '\n')
28841dbe0c7SBrian Somers       *last = '\0';
2895d9e6103SBrian Somers     prompt_Printf(prompt, "peak    cleared (was %6qu bytes/sec on %s)\n",
29041dbe0c7SBrian Somers                   t->BestOctetsPerSecond, time_buf);
29141dbe0c7SBrian Somers     t->BestOctetsPerSecond = 0;
292ab2de065SBrian Somers     time(&t->BestOctetsPerSecondTime);
29341dbe0c7SBrian Somers   }
29441dbe0c7SBrian Somers }
295ab2de065SBrian Somers 
296ab2de065SBrian Somers void
297ab2de065SBrian Somers throughput_callback(struct pppThroughput *t, void (*fn)(void *), void *data)
298ab2de065SBrian Somers {
299ab2de065SBrian Somers   t->callback.fn = fn;
300ab2de065SBrian Somers   t->callback.data = data;
301ab2de065SBrian Somers }
302