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