throughput.c revision 31272
1/*
2 * $Id$
3 */
4
5#include <sys/param.h>
6
7#include <stdio.h>
8#include <time.h>
9#include <netinet/in.h>
10
11#include "timer.h"
12#include "throughput.h"
13#include "defs.h"
14#include "loadalias.h"
15#include "command.h"
16#include "vars.h"
17#include "mbuf.h"
18#include "log.h"
19
20void
21throughput_init(struct pppThroughput *t)
22{
23  int f;
24
25  t->OctetsIn = t->OctetsOut = 0;
26  for (f = 0; f < SAMPLE_PERIOD; f++)
27    t->SampleOctets[f] = 0;
28  t->OctetsPerSecond = t->BestOctetsPerSecond = t->nSample = 0;
29  throughput_stop(t);
30}
31
32void
33throughput_disp(struct pppThroughput *t, FILE *f)
34{
35  int secs_up;
36
37  secs_up = time(NULL) - t->uptime;
38  fprintf(f, "Connect time: %d secs\n", secs_up);
39  if (secs_up == 0)
40    secs_up = 1;
41  fprintf(f, "%ld octets in, %ld octets out\n", t->OctetsIn, t->OctetsOut);
42  if (Enabled(ConfThroughput)) {
43    fprintf(f, "  overall   %5ld bytes/sec\n",
44            (t->OctetsIn+t->OctetsOut)/secs_up);
45    fprintf(f, "  currently %5d bytes/sec\n", t->OctetsPerSecond);
46    fprintf(f, "  peak      %5d bytes/sec\n", t->BestOctetsPerSecond);
47  } else
48    fprintf(f, "Overall %ld bytes/sec\n", (t->OctetsIn+t->OctetsOut)/secs_up);
49}
50
51
52void
53throughput_log(struct pppThroughput *t, int level, const char *title)
54{
55  if (t->uptime) {
56    int secs_up;
57
58    secs_up = time(NULL) - t->uptime;
59    if (title)
60      LogPrintf(level, "%s: Connect time: %d secs: %ld octets in, %ld octets"
61                " out\n", title, secs_up, t->OctetsIn, t->OctetsOut);
62    else
63      LogPrintf(level, "Connect time: %d secs: %ld octets in, %ld octets out\n",
64                secs_up, t->OctetsIn, t->OctetsOut);
65    if (secs_up == 0)
66      secs_up = 1;
67    if (Enabled(ConfThroughput))
68      LogPrintf(level, " total %ld bytes/sec, peak %d bytes/sec\n",
69                (t->OctetsIn+t->OctetsOut)/secs_up, t->BestOctetsPerSecond);
70    else
71      LogPrintf(level, " total %ld bytes/sec\n",
72                (t->OctetsIn+t->OctetsOut)/secs_up);
73  }
74}
75
76static void
77throughput_sampler(struct pppThroughput *t)
78{
79  u_long old;
80
81  StopTimer(&t->Timer);
82  t->Timer.state = TIMER_STOPPED;
83
84  old = t->SampleOctets[t->nSample];
85  t->SampleOctets[t->nSample] = t->OctetsIn + t->OctetsOut;
86  t->OctetsPerSecond = (t->SampleOctets[t->nSample] - old) / SAMPLE_PERIOD;
87  if (t->BestOctetsPerSecond < t->OctetsPerSecond)
88    t->BestOctetsPerSecond = t->OctetsPerSecond;
89  if (++t->nSample == SAMPLE_PERIOD)
90    t->nSample = 0;
91
92  StartTimer(&t->Timer);
93}
94
95void
96throughput_start(struct pppThroughput *t)
97{
98  throughput_init(t);
99  time(&t->uptime);
100  if (Enabled(ConfThroughput)) {
101    t->Timer.state = TIMER_STOPPED;
102    t->Timer.load = SECTICKS;
103    t->Timer.func = throughput_sampler;
104    t->Timer.arg = t;
105    StartTimer(&t->Timer);
106  }
107}
108
109void
110throughput_stop(struct pppThroughput *t)
111{
112  if (Enabled(ConfThroughput))
113    StopTimer(&t->Timer);
114}
115
116void
117throughput_addin(struct pppThroughput *t, int n)
118{
119  t->OctetsIn += n;
120}
121
122void
123throughput_addout(struct pppThroughput *t, int n)
124{
125  t->OctetsOut += n;
126}
127