throughput.c revision 36285
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.4.4.9 1998/05/01 19:26:04 brian Exp $ 27 */ 28 29#include <sys/types.h> 30 31#include <stdio.h> 32#include <string.h> 33#include <termios.h> 34#include <time.h> 35 36#include "log.h" 37#include "timer.h" 38#include "throughput.h" 39#include "descriptor.h" 40#include "prompt.h" 41 42void 43throughput_init(struct pppThroughput *t) 44{ 45 int f; 46 47 t->OctetsIn = t->OctetsOut = 0; 48 for (f = 0; f < SAMPLE_PERIOD; f++) 49 t->SampleOctets[f] = 0; 50 t->OctetsPerSecond = t->BestOctetsPerSecond = t->nSample = 0; 51 memset(&t->Timer, '\0', sizeof t->Timer); 52 t->Timer.name = "throughput"; 53 t->uptime = 0; 54 t->rolling = 0; 55 throughput_stop(t); 56} 57 58void 59throughput_disp(struct pppThroughput *t, struct prompt *prompt) 60{ 61 int secs_up; 62 63 secs_up = t->uptime ? time(NULL) - t->uptime : 0; 64 prompt_Printf(prompt, "Connect time: %d secs\n", secs_up); 65 if (secs_up == 0) 66 secs_up = 1; 67 prompt_Printf(prompt, "%ld octets in, %ld octets out\n", 68 t->OctetsIn, t->OctetsOut); 69 if (t->rolling) { 70 prompt_Printf(prompt, " overall %5ld bytes/sec\n", 71 (t->OctetsIn+t->OctetsOut)/secs_up); 72 prompt_Printf(prompt, " currently %5d bytes/sec\n", t->OctetsPerSecond); 73 prompt_Printf(prompt, " peak %5d bytes/sec\n", 74 t->BestOctetsPerSecond); 75 } else 76 prompt_Printf(prompt, "Overall %ld bytes/sec\n", 77 (t->OctetsIn+t->OctetsOut)/secs_up); 78} 79 80 81void 82throughput_log(struct pppThroughput *t, int level, const char *title) 83{ 84 if (t->uptime) { 85 int secs_up; 86 87 secs_up = t->uptime ? time(NULL) - t->uptime : 0; 88 if (title) 89 log_Printf(level, "%s: Connect time: %d secs: %ld octets in, %ld octets" 90 " out\n", title, secs_up, t->OctetsIn, t->OctetsOut); 91 else 92 log_Printf(level, "Connect time: %d secs: %ld octets in, %ld octets out\n", 93 secs_up, t->OctetsIn, t->OctetsOut); 94 if (secs_up == 0) 95 secs_up = 1; 96 if (t->rolling) 97 log_Printf(level, " total %ld bytes/sec, peak %d bytes/sec\n", 98 (t->OctetsIn+t->OctetsOut)/secs_up, t->BestOctetsPerSecond); 99 else 100 log_Printf(level, " total %ld bytes/sec\n", 101 (t->OctetsIn+t->OctetsOut)/secs_up); 102 } 103} 104 105static void 106throughput_sampler(void *v) 107{ 108 struct pppThroughput *t = (struct pppThroughput *)v; 109 u_long old; 110 111 timer_Stop(&t->Timer); 112 113 old = t->SampleOctets[t->nSample]; 114 t->SampleOctets[t->nSample] = t->OctetsIn + t->OctetsOut; 115 t->OctetsPerSecond = (t->SampleOctets[t->nSample] - old) / SAMPLE_PERIOD; 116 if (t->BestOctetsPerSecond < t->OctetsPerSecond) 117 t->BestOctetsPerSecond = t->OctetsPerSecond; 118 if (++t->nSample == SAMPLE_PERIOD) 119 t->nSample = 0; 120 121 timer_Start(&t->Timer); 122} 123 124void 125throughput_start(struct pppThroughput *t, const char *name, int rolling) 126{ 127 timer_Stop(&t->Timer); 128 throughput_init(t); 129 t->rolling = rolling ? 1 : 0; 130 time(&t->uptime); 131 if (t->rolling) { 132 t->Timer.load = SECTICKS; 133 t->Timer.func = throughput_sampler; 134 t->Timer.name = name; 135 t->Timer.arg = t; 136 timer_Start(&t->Timer); 137 } 138} 139 140void 141throughput_stop(struct pppThroughput *t) 142{ 143 timer_Stop(&t->Timer); 144} 145 146void 147throughput_addin(struct pppThroughput *t, int n) 148{ 149 t->OctetsIn += n; 150} 151 152void 153throughput_addout(struct pppThroughput *t, int n) 154{ 155 t->OctetsOut += n; 156} 157