throughput.c revision 64670
1/*-
2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/usr.sbin/ppp/throughput.c 64670 2000-08-15 10:25:42Z brian $
27 */
28
29#include <sys/types.h>
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <termios.h>
35#include <time.h>
36
37#include "log.h"
38#include "timer.h"
39#include "throughput.h"
40#include "descriptor.h"
41#include "prompt.h"
42
43
44void
45throughput_init(struct pppThroughput *t, int period)
46{
47  t->OctetsIn = t->OctetsOut = 0;
48  t->SamplePeriod = period;
49  t->in.SampleOctets = (long long *)
50    calloc(period, sizeof *t->in.SampleOctets);
51  t->in.OctetsPerSecond = 0;
52  t->out.SampleOctets = (long long *)
53    calloc(period, sizeof *t->out.SampleOctets);
54  t->out.OctetsPerSecond = 0;
55  t->BestOctetsPerSecond = 0;
56  t->nSample = 0;
57  time(&t->BestOctetsPerSecondTime);
58  memset(&t->Timer, '\0', sizeof t->Timer);
59  t->Timer.name = "throughput";
60  t->uptime = 0;
61  t->downtime = 0;
62  t->rolling = 0;
63  t->callback.data = NULL;
64  t->callback.fn = NULL;
65  throughput_stop(t);
66}
67
68void
69throughput_destroy(struct pppThroughput *t)
70{
71  if (t && t->in.SampleOctets) {
72    throughput_stop(t);
73    free(t->in.SampleOctets);
74    free(t->out.SampleOctets);
75    t->in.SampleOctets = NULL;
76    t->out.SampleOctets = NULL;
77  }
78}
79
80int
81throughput_uptime(struct pppThroughput *t)
82{
83  time_t downat;
84
85  downat = t->downtime ? t->downtime : time(NULL);
86  if (t->uptime && downat < t->uptime) {
87    /* Euch !  The clock's gone back ! */
88    int i;
89
90    for (i = 0; i < t->SamplePeriod; i++)
91      t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
92    t->nSample = 0;
93    t->uptime = downat;
94  }
95  return t->uptime ? downat - t->uptime : 0;
96}
97
98void
99throughput_disp(struct pppThroughput *t, struct prompt *prompt)
100{
101  int secs_up, divisor;
102
103  secs_up = throughput_uptime(t);
104  prompt_Printf(prompt, "Connect time: %d:%02d:%02d", secs_up / 3600,
105                (secs_up / 60) % 60, secs_up % 60);
106  if (t->downtime)
107    prompt_Printf(prompt, " - down at %s", ctime(&t->downtime));
108  else
109    prompt_Printf(prompt, "\n");
110
111  divisor = secs_up ? secs_up : 1;
112  prompt_Printf(prompt, "%llu octets in, %llu octets out\n",
113                t->OctetsIn, t->OctetsOut);
114  if (t->rolling) {
115    prompt_Printf(prompt, "  overall   %6qu bytes/sec\n",
116                  (t->OctetsIn + t->OctetsOut) / divisor);
117    prompt_Printf(prompt, "  %s %6qu bytes/sec in, %6qu bytes/sec out "
118                  "(over the last %d secs)\n",
119                  t->downtime ? "average  " : "currently",
120                  t->in.OctetsPerSecond, t->out.OctetsPerSecond,
121                  secs_up > t->SamplePeriod ? t->SamplePeriod : secs_up);
122    prompt_Printf(prompt, "  peak      %6qu bytes/sec on %s",
123                  t->BestOctetsPerSecond, ctime(&t->BestOctetsPerSecondTime));
124  } else
125    prompt_Printf(prompt, "Overall %llu bytes/sec\n",
126                  (t->OctetsIn + t->OctetsOut) / divisor);
127}
128
129
130void
131throughput_log(struct pppThroughput *t, int level, const char *title)
132{
133  if (t->uptime) {
134    int secs_up;
135
136    secs_up = throughput_uptime(t);
137    if (title == NULL)
138      title = "";
139    log_Printf(level, "%s%sConnect time: %d secs: %llu octets in, %llu octets"
140                " out\n", title, *title ? ": " : "", secs_up, t->OctetsIn,
141                t->OctetsOut);
142    if (secs_up == 0)
143      secs_up = 1;
144    if (t->rolling)
145      log_Printf(level, " total %llu bytes/sec, peak %llu bytes/sec on %s",
146                 (t->OctetsIn + t->OctetsOut) / secs_up, t->BestOctetsPerSecond,
147                 ctime(&t->BestOctetsPerSecondTime));
148    else
149      log_Printf(level, " total %llu bytes/sec\n",
150                 (t->OctetsIn + t->OctetsOut) / secs_up);
151  }
152}
153
154static void
155throughput_sampler(void *v)
156{
157  struct pppThroughput *t = (struct pppThroughput *)v;
158  unsigned long long old;
159  int uptime, divisor;
160  unsigned long long octets;
161
162  timer_Stop(&t->Timer);
163
164  uptime = throughput_uptime(t);
165  divisor = uptime < t->SamplePeriod ? uptime + 1 : t->SamplePeriod;
166
167  old = t->in.SampleOctets[t->nSample];
168  t->in.SampleOctets[t->nSample] = t->OctetsIn;
169  t->in.OctetsPerSecond = (t->in.SampleOctets[t->nSample] - old) / divisor;
170
171  old = t->out.SampleOctets[t->nSample];
172  t->out.SampleOctets[t->nSample] = t->OctetsOut;
173  t->out.OctetsPerSecond = (t->out.SampleOctets[t->nSample] - old) / divisor;
174
175  octets = t->in.OctetsPerSecond + t->out.OctetsPerSecond;
176  if (t->BestOctetsPerSecond < octets) {
177    t->BestOctetsPerSecond = octets;
178    time(&t->BestOctetsPerSecondTime);
179  }
180
181  if (++t->nSample == t->SamplePeriod)
182    t->nSample = 0;
183
184  if (t->callback.fn != NULL && uptime >= t->SamplePeriod)
185    (*t->callback.fn)(t->callback.data);
186
187  timer_Start(&t->Timer);
188}
189
190void
191throughput_start(struct pppThroughput *t, const char *name, int rolling)
192{
193  int i;
194  timer_Stop(&t->Timer);
195
196  for (i = 0; i < t->SamplePeriod; i++)
197    t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
198  t->nSample = 0;
199  t->OctetsIn = t->OctetsOut = 0;
200  t->in.OctetsPerSecond = t->out.OctetsPerSecond = t->BestOctetsPerSecond = 0;
201  time(&t->BestOctetsPerSecondTime);
202  t->downtime = 0;
203  time(&t->uptime);
204  throughput_restart(t, name, rolling);
205}
206
207void
208throughput_restart(struct pppThroughput *t, const char *name, int rolling)
209{
210  timer_Stop(&t->Timer);
211  t->rolling = rolling ? 1 : 0;
212  if (t->rolling) {
213    t->Timer.load = SECTICKS;
214    t->Timer.func = throughput_sampler;
215    t->Timer.name = name;
216    t->Timer.arg = t;
217    timer_Start(&t->Timer);
218  } else {
219    t->Timer.load = 0;
220    t->Timer.func = NULL;
221    t->Timer.name = NULL;
222    t->Timer.arg = NULL;
223  }
224}
225
226void
227throughput_stop(struct pppThroughput *t)
228{
229  if (t->Timer.state != TIMER_STOPPED)
230    time(&t->downtime);
231  timer_Stop(&t->Timer);
232}
233
234void
235throughput_addin(struct pppThroughput *t, long long n)
236{
237  t->OctetsIn += n;
238}
239
240void
241throughput_addout(struct pppThroughput *t, long long n)
242{
243  t->OctetsOut += n;
244}
245
246void
247throughput_clear(struct pppThroughput *t, int clear_type, struct prompt *prompt)
248{
249  if (clear_type & (THROUGHPUT_OVERALL|THROUGHPUT_CURRENT)) {
250    int i;
251
252    for (i = 0; i < t->SamplePeriod; i++)
253      t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
254    t->nSample = 0;
255  }
256
257  if (clear_type & THROUGHPUT_OVERALL) {
258    int divisor;
259
260    if ((divisor = throughput_uptime(t)) == 0)
261      divisor = 1;
262    prompt_Printf(prompt, "overall cleared (was %6qu bytes/sec)\n",
263                  (t->OctetsIn + t->OctetsOut) / divisor);
264    t->OctetsIn = t->OctetsOut = 0;
265    t->downtime = 0;
266    time(&t->uptime);
267  }
268
269  if (clear_type & THROUGHPUT_CURRENT) {
270    prompt_Printf(prompt, "current cleared (was %6qu bytes/sec in,"
271                  " %6qu bytes/sec out)\n",
272                  t->in.OctetsPerSecond, t->out.OctetsPerSecond);
273    t->in.OctetsPerSecond = t->out.OctetsPerSecond = 0;
274  }
275
276  if (clear_type & THROUGHPUT_PEAK) {
277    char *time_buf, *last;
278
279    time_buf = ctime(&t->BestOctetsPerSecondTime);
280    last = time_buf + strlen(time_buf);
281    if (last > time_buf && *--last == '\n')
282      *last = '\0';
283    prompt_Printf(prompt, "peak    cleared (was %6qu bytes/sec on %s)\n",
284                  t->BestOctetsPerSecond, time_buf);
285    t->BestOctetsPerSecond = 0;
286    time(&t->BestOctetsPerSecondTime);
287  }
288}
289
290void
291throughput_callback(struct pppThroughput *t, void (*fn)(void *), void *data)
292{
293  t->callback.fn = fn;
294  t->callback.data = data;
295}
296