throughput.c revision 212829
138494Sobrien/*-
2174294Sobrien * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
338494Sobrien * All rights reserved.
438494Sobrien *
538494Sobrien * Redistribution and use in source and binary forms, with or without
638494Sobrien * modification, are permitted provided that the following conditions
738494Sobrien * are met:
838494Sobrien * 1. Redistributions of source code must retain the above copyright
938494Sobrien *    notice, this list of conditions and the following disclaimer.
1038494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1138494Sobrien *    notice, this list of conditions and the following disclaimer in the
1238494Sobrien *    documentation and/or other materials provided with the distribution.
1338494Sobrien *
1438494Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1538494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1638494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1738494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1838494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1938494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2042629Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2138494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2238494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2338494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2438494Sobrien * SUCH DAMAGE.
2538494Sobrien *
2638494Sobrien * $FreeBSD: head/usr.sbin/ppp/throughput.c 212829 2010-09-18 22:26:50Z n_hibma $
2738494Sobrien */
2838494Sobrien
2938494Sobrien#include <sys/types.h>
3038494Sobrien
3138494Sobrien#include <stdarg.h>
3238494Sobrien#include <stdio.h>
3338494Sobrien#include <stdlib.h>
3438494Sobrien#include <string.h>
3538494Sobrien#include <termios.h>
3638494Sobrien#include <time.h>
3738494Sobrien
3838494Sobrien#include "log.h"
3938494Sobrien#include "timer.h"
40174294Sobrien#include "throughput.h"
4138494Sobrien#include "descriptor.h"
4238494Sobrien#include "prompt.h"
4338494Sobrien
4438494Sobrien
4538494Sobrienvoid
4638494Sobrienthroughput_init(struct pppThroughput *t, int period)
4738494Sobrien{
4838494Sobrien  t->OctetsIn = t->OctetsOut = t->PacketsIn = t->PacketsOut = 0;
4938494Sobrien  t->SamplePeriod = period;
5038494Sobrien  t->in.SampleOctets = (long long *)
5138494Sobrien    calloc(period, sizeof *t->in.SampleOctets);
5238494Sobrien  t->in.OctetsPerSecond = 0;
5338494Sobrien  t->out.SampleOctets = (long long *)
5438494Sobrien    calloc(period, sizeof *t->out.SampleOctets);
5538494Sobrien  t->out.OctetsPerSecond = 0;
5638494Sobrien  t->BestOctetsPerSecond = 0;
5738494Sobrien  t->nSample = 0;
5838494Sobrien  time(&t->BestOctetsPerSecondTime);
5938494Sobrien  memset(&t->Timer, '\0', sizeof t->Timer);
6038494Sobrien  t->Timer.name = "throughput";
6138494Sobrien  t->uptime = 0;
6238494Sobrien  t->downtime = 0;
6338494Sobrien  t->rolling = 0;
6438494Sobrien  t->callback.data = NULL;
6538494Sobrien  t->callback.fn = NULL;
6638494Sobrien  throughput_stop(t);
6738494Sobrien}
6838494Sobrien
6938494Sobrienvoid
7038494Sobrienthroughput_destroy(struct pppThroughput *t)
7138494Sobrien{
7238494Sobrien  if (t && t->in.SampleOctets) {
7338494Sobrien    throughput_stop(t);
7438494Sobrien    free(t->in.SampleOctets);
7538494Sobrien    free(t->out.SampleOctets);
7638494Sobrien    t->in.SampleOctets = NULL;
7738494Sobrien    t->out.SampleOctets = NULL;
78  }
79}
80
81int
82throughput_uptime(struct pppThroughput *t)
83{
84  time_t downat;
85
86  downat = t->downtime ? t->downtime : time(NULL);
87  if (t->uptime && downat < t->uptime) {
88    /* Euch !  The clock's gone back ! */
89    int i;
90
91    for (i = 0; i < t->SamplePeriod; i++)
92      t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
93    t->nSample = 0;
94    t->uptime = downat;
95  }
96  return t->uptime ? downat - t->uptime : 0;
97}
98
99void
100throughput_disp(struct pppThroughput *t, struct prompt *prompt)
101{
102  int secs_up, divisor;
103
104  secs_up = throughput_uptime(t);
105  prompt_Printf(prompt, "Connect time: %d:%02d:%02d", secs_up / 3600,
106                (secs_up / 60) % 60, secs_up % 60);
107  if (t->downtime)
108    prompt_Printf(prompt, " - down at %s", ctime(&t->downtime));
109  else
110    prompt_Printf(prompt, "\n");
111
112  divisor = secs_up ? secs_up : 1;
113  prompt_Printf(prompt, "%llu octets in, %llu octets out\n",
114                t->OctetsIn, t->OctetsOut);
115  prompt_Printf(prompt, "%llu packets in, %llu packets out\n",
116                t->PacketsIn, t->PacketsOut);
117  if (t->rolling) {
118    prompt_Printf(prompt, "  overall   %6qu bytes/sec\n",
119                  (t->OctetsIn + t->OctetsOut) / divisor);
120    prompt_Printf(prompt, "  %s %6qu bytes/sec in, %6qu bytes/sec out "
121                  "(over the last %d secs)\n",
122                  t->downtime ? "average  " : "currently",
123                  t->in.OctetsPerSecond, t->out.OctetsPerSecond,
124                  secs_up > t->SamplePeriod ? t->SamplePeriod : secs_up);
125    prompt_Printf(prompt, "  peak      %6qu bytes/sec on %s",
126                  t->BestOctetsPerSecond, ctime(&t->BestOctetsPerSecondTime));
127  } else
128    prompt_Printf(prompt, "Overall %llu bytes/sec\n",
129                  (t->OctetsIn + t->OctetsOut) / divisor);
130}
131
132
133void
134throughput_log(struct pppThroughput *t, int level, const char *title)
135{
136  if (t->uptime) {
137    int secs_up;
138
139    secs_up = throughput_uptime(t);
140    if (title == NULL)
141      title = "";
142    log_Printf(level, "%s%sConnect time: %d secs: %llu octets in, %llu octets"
143               " out\n", title, *title ? ": " : "", secs_up, t->OctetsIn,
144               t->OctetsOut);
145    log_Printf(level, "%s%s%llu packets in, %llu packets out\n",
146               title, *title ? ": " : "",  t->PacketsIn, t->PacketsOut);
147    if (secs_up == 0)
148      secs_up = 1;
149    if (t->rolling)
150      log_Printf(level, " total %llu bytes/sec, peak %llu bytes/sec on %s",
151                 (t->OctetsIn + t->OctetsOut) / secs_up, t->BestOctetsPerSecond,
152                 ctime(&t->BestOctetsPerSecondTime));
153    else
154      log_Printf(level, " total %llu bytes/sec\n",
155                 (t->OctetsIn + t->OctetsOut) / secs_up);
156  }
157}
158
159static void
160throughput_sampler(void *v)
161{
162  struct pppThroughput *t = (struct pppThroughput *)v;
163  unsigned long long old;
164  int uptime, divisor;
165  unsigned long long octets;
166
167  timer_Stop(&t->Timer);
168
169  uptime = throughput_uptime(t);
170  divisor = uptime < t->SamplePeriod ? uptime + 1 : t->SamplePeriod;
171
172  old = t->in.SampleOctets[t->nSample];
173  t->in.SampleOctets[t->nSample] = t->OctetsIn;
174  t->in.OctetsPerSecond = (t->in.SampleOctets[t->nSample] - old) / divisor;
175
176  old = t->out.SampleOctets[t->nSample];
177  t->out.SampleOctets[t->nSample] = t->OctetsOut;
178  t->out.OctetsPerSecond = (t->out.SampleOctets[t->nSample] - old) / divisor;
179
180  octets = t->in.OctetsPerSecond + t->out.OctetsPerSecond;
181  if (t->BestOctetsPerSecond < octets) {
182    t->BestOctetsPerSecond = octets;
183    time(&t->BestOctetsPerSecondTime);
184  }
185
186  if (++t->nSample == t->SamplePeriod)
187    t->nSample = 0;
188
189  if (t->callback.fn != NULL && uptime >= t->SamplePeriod)
190    (*t->callback.fn)(t->callback.data);
191
192  timer_Start(&t->Timer);
193}
194
195void
196throughput_start(struct pppThroughput *t, const char *name, int rolling)
197{
198  int i;
199  timer_Stop(&t->Timer);
200
201  for (i = 0; i < t->SamplePeriod; i++)
202    t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
203  t->nSample = 0;
204  t->OctetsIn = t->OctetsOut = t->PacketsIn = t->PacketsOut = 0;
205  t->in.OctetsPerSecond = t->out.OctetsPerSecond = t->BestOctetsPerSecond = 0;
206  time(&t->BestOctetsPerSecondTime);
207  t->downtime = 0;
208  time(&t->uptime);
209  throughput_restart(t, name, rolling);
210}
211
212void
213throughput_restart(struct pppThroughput *t, const char *name, int rolling)
214{
215  timer_Stop(&t->Timer);
216  t->rolling = rolling ? 1 : 0;
217  if (t->rolling) {
218    t->Timer.load = SECTICKS;
219    t->Timer.func = throughput_sampler;
220    t->Timer.name = name;
221    t->Timer.arg = t;
222    timer_Start(&t->Timer);
223  } else {
224    t->Timer.load = 0;
225    t->Timer.func = NULL;
226    t->Timer.name = NULL;
227    t->Timer.arg = NULL;
228  }
229}
230
231void
232throughput_stop(struct pppThroughput *t)
233{
234  if (t->Timer.state != TIMER_STOPPED)
235    time(&t->downtime);
236  timer_Stop(&t->Timer);
237}
238
239void
240throughput_addin(struct pppThroughput *t, long long n)
241{
242  t->OctetsIn += n;
243  t->PacketsIn++;
244}
245
246void
247throughput_addout(struct pppThroughput *t, long long n)
248{
249  t->OctetsOut += n;
250  t->PacketsOut++;
251}
252
253void
254throughput_clear(struct pppThroughput *t, int clear_type, struct prompt *prompt)
255{
256  if (clear_type & (THROUGHPUT_OVERALL|THROUGHPUT_CURRENT)) {
257    int i;
258
259    for (i = 0; i < t->SamplePeriod; i++)
260      t->in.SampleOctets[i] = t->out.SampleOctets[i] = 0;
261    t->nSample = 0;
262  }
263
264  if (clear_type & THROUGHPUT_OVERALL) {
265    int divisor;
266
267    if ((divisor = throughput_uptime(t)) == 0)
268      divisor = 1;
269    prompt_Printf(prompt, "overall cleared (was %6qu bytes/sec)\n",
270                  (t->OctetsIn + t->OctetsOut) / divisor);
271    t->OctetsIn = t->OctetsOut = t->PacketsIn = t->PacketsOut = 0;
272    t->downtime = 0;
273    time(&t->uptime);
274  }
275
276  if (clear_type & THROUGHPUT_CURRENT) {
277    prompt_Printf(prompt, "current cleared (was %6qu bytes/sec in,"
278                  " %6qu bytes/sec out)\n",
279                  t->in.OctetsPerSecond, t->out.OctetsPerSecond);
280    t->in.OctetsPerSecond = t->out.OctetsPerSecond = 0;
281  }
282
283  if (clear_type & THROUGHPUT_PEAK) {
284    char *time_buf, *last;
285
286    time_buf = ctime(&t->BestOctetsPerSecondTime);
287    last = time_buf + strlen(time_buf);
288    if (last > time_buf && *--last == '\n')
289      *last = '\0';
290    prompt_Printf(prompt, "peak    cleared (was %6qu bytes/sec on %s)\n",
291                  t->BestOctetsPerSecond, time_buf);
292    t->BestOctetsPerSecond = 0;
293    time(&t->BestOctetsPerSecondTime);
294  }
295}
296
297void
298throughput_callback(struct pppThroughput *t, void (*fn)(void *), void *data)
299{
300  t->callback.fn = fn;
301  t->callback.data = data;
302}
303