throughput.c revision 49434
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 *	$Id: throughput.c,v 1.9 1999/05/08 11:07:47 brian Exp $
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
43void
44throughput_init(struct pppThroughput *t, int period)
45{
46  t->OctetsIn = t->OctetsOut = 0;
47  t->SamplePeriod = period;
48  t->SampleOctets = (long long *)calloc(period, sizeof *t->SampleOctets);
49  t->OctetsPerSecond = t->BestOctetsPerSecond = 0;
50  t->nSample = 0;
51  time(&t->BestOctetsPerSecondTime);
52  memset(&t->Timer, '\0', sizeof t->Timer);
53  t->Timer.name = "throughput";
54  t->uptime = 0;
55  t->downtime = 0;
56  t->rolling = 0;
57  t->callback.data = NULL;
58  t->callback.fn = NULL;
59  throughput_stop(t);
60}
61
62void
63throughput_destroy(struct pppThroughput *t)
64{
65  if (t && t->SampleOctets) {
66    throughput_stop(t);
67    free(t->SampleOctets);
68    t->SampleOctets = 0;
69  }
70}
71
72int
73throughput_uptime(struct pppThroughput *t)
74{
75  time_t downat;
76
77  downat = t->downtime ? t->downtime : time(NULL);
78  return t->uptime ? downat - t->uptime : 0;
79}
80
81void
82throughput_disp(struct pppThroughput *t, struct prompt *prompt)
83{
84  int secs_up, divisor;
85
86  secs_up = throughput_uptime(t);
87  prompt_Printf(prompt, "Connect time: %d:%02d:%02d", secs_up / 3600,
88                (secs_up / 60) % 60, secs_up % 60);
89  if (t->downtime)
90    prompt_Printf(prompt, " - down at %s", ctime(&t->downtime));
91  else
92    prompt_Printf(prompt, "\n");
93
94  divisor = secs_up ? secs_up : 1;
95  prompt_Printf(prompt, "%qu octets in, %qu octets out\n",
96                t->OctetsIn, t->OctetsOut);
97  if (t->rolling) {
98    prompt_Printf(prompt, "  overall   %6qu bytes/sec\n",
99                  (t->OctetsIn + t->OctetsOut) / divisor);
100    prompt_Printf(prompt, "  %s %6qu bytes/sec (over the last"
101                  " %d secs)\n", t->downtime ? "average  " : "currently",
102                  t->OctetsPerSecond,
103                  secs_up > t->SamplePeriod ? t->SamplePeriod : secs_up);
104    prompt_Printf(prompt, "  peak      %6qu bytes/sec on %s",
105                  t->BestOctetsPerSecond, ctime(&t->BestOctetsPerSecondTime));
106  } else
107    prompt_Printf(prompt, "Overall %qu bytes/sec\n",
108                  (t->OctetsIn + t->OctetsOut) / divisor);
109}
110
111
112void
113throughput_log(struct pppThroughput *t, int level, const char *title)
114{
115  if (t->uptime) {
116    int secs_up;
117
118    secs_up = throughput_uptime(t);
119    if (title)
120      log_Printf(level, "%s: Connect time: %d secs: %qu octets in, %qu octets"
121                " out\n", title, secs_up, t->OctetsIn, t->OctetsOut);
122    else
123      log_Printf(level, "Connect time: %d secs: %qu octets in,"
124                 " %qu octets out\n", secs_up, t->OctetsIn, t->OctetsOut);
125    if (secs_up == 0)
126      secs_up = 1;
127    if (t->rolling)
128      log_Printf(level, " total %qu bytes/sec, peak %qu bytes/sec on %s",
129                 (t->OctetsIn + t->OctetsOut) / secs_up, t->BestOctetsPerSecond,
130                 ctime(&t->BestOctetsPerSecondTime));
131    else
132      log_Printf(level, " total %qu bytes/sec\n",
133                 (t->OctetsIn + t->OctetsOut) / secs_up);
134  }
135}
136
137static void
138throughput_sampler(void *v)
139{
140  struct pppThroughput *t = (struct pppThroughput *)v;
141  unsigned long long old;
142  int uptime, divisor;
143
144  timer_Stop(&t->Timer);
145
146  uptime = throughput_uptime(t);
147  divisor = uptime < t->SamplePeriod ? uptime + 1 : t->SamplePeriod;
148  old = t->SampleOctets[t->nSample];
149  t->SampleOctets[t->nSample] = t->OctetsIn + t->OctetsOut;
150  t->OctetsPerSecond = (t->SampleOctets[t->nSample] - old) / divisor;
151  if (t->BestOctetsPerSecond < t->OctetsPerSecond) {
152    t->BestOctetsPerSecond = t->OctetsPerSecond;
153    time(&t->BestOctetsPerSecondTime);
154  }
155  if (++t->nSample == t->SamplePeriod)
156    t->nSample = 0;
157
158  if (t->callback.fn != NULL && uptime >= t->SamplePeriod)
159    (*t->callback.fn)(t->callback.data);
160
161  timer_Start(&t->Timer);
162}
163
164void
165throughput_start(struct pppThroughput *t, const char *name, int rolling)
166{
167  int i;
168  timer_Stop(&t->Timer);
169
170  for (i = 0; i < t->SamplePeriod; i++)
171    t->SampleOctets[i] = 0;
172  t->nSample = 0;
173  t->OctetsIn = t->OctetsOut = 0;
174  t->OctetsPerSecond = t->BestOctetsPerSecond = 0;
175  time(&t->BestOctetsPerSecondTime);
176  t->downtime = 0;
177  time(&t->uptime);
178  throughput_restart(t, name, rolling);
179}
180
181void
182throughput_restart(struct pppThroughput *t, const char *name, int rolling)
183{
184  timer_Stop(&t->Timer);
185  t->rolling = rolling ? 1 : 0;
186  if (t->rolling) {
187    t->Timer.load = SECTICKS;
188    t->Timer.func = throughput_sampler;
189    t->Timer.name = name;
190    t->Timer.arg = t;
191    timer_Start(&t->Timer);
192  } else {
193    t->Timer.load = 0;
194    t->Timer.func = NULL;
195    t->Timer.name = NULL;
196    t->Timer.arg = NULL;
197  }
198}
199
200void
201throughput_stop(struct pppThroughput *t)
202{
203  if (t->Timer.state != TIMER_STOPPED)
204    time(&t->downtime);
205  timer_Stop(&t->Timer);
206}
207
208void
209throughput_addin(struct pppThroughput *t, long long n)
210{
211  t->OctetsIn += n;
212}
213
214void
215throughput_addout(struct pppThroughput *t, long long n)
216{
217  t->OctetsOut += n;
218}
219
220void
221throughput_clear(struct pppThroughput *t, int clear_type, struct prompt *prompt)
222{
223  if (clear_type & (THROUGHPUT_OVERALL|THROUGHPUT_CURRENT)) {
224    int i;
225
226    for (i = 0; i < t->SamplePeriod; i++)
227      t->SampleOctets[i] = 0;
228    t->nSample = 0;
229  }
230
231  if (clear_type & THROUGHPUT_OVERALL) {
232    int divisor;
233
234    if ((divisor = throughput_uptime(t)) == 0)
235      divisor = 1;
236    prompt_Printf(prompt, "overall cleared (was %6qu bytes/sec)\n",
237                  (t->OctetsIn + t->OctetsOut) / divisor);
238    t->OctetsIn = t->OctetsOut = 0;
239    t->downtime = 0;
240    time(&t->uptime);
241  }
242
243  if (clear_type & THROUGHPUT_CURRENT) {
244    prompt_Printf(prompt, "current cleared (was %6qu bytes/sec)\n",
245                  t->OctetsPerSecond);
246    t->OctetsPerSecond = 0;
247  }
248
249  if (clear_type & THROUGHPUT_PEAK) {
250    char *time_buf, *last;
251
252    time_buf = ctime(&t->BestOctetsPerSecondTime);
253    last = time_buf + strlen(time_buf);
254    if (last > time_buf && *--last == '\n')
255      *last = '\0';
256    prompt_Printf(prompt, "peak    cleared (was %6qu bytes/sec on %s)\n",
257                  t->BestOctetsPerSecond, time_buf);
258    t->BestOctetsPerSecond = 0;
259    time(&t->BestOctetsPerSecondTime);
260  }
261}
262
263void
264throughput_callback(struct pppThroughput *t, void (*fn)(void *), void *data)
265{
266  t->callback.fn = fn;
267  t->callback.data = data;
268}
269