From 10f518c1dea8c36ef5123e482344edebe3d79460 Mon Sep 17 00:00:00 2001 From: pdw <> Date: Mon, 13 Oct 2003 21:55:33 +0000 Subject: [PATCH] Added option for lin/log scale (not yet accessible) Added ability to specify fixed maximum for bandwidth scale. --- iftop.h | 2 +- options.c | 38 ++++++++++++++++++++++++++++++-- options.h | 3 +++ ui.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 96 insertions(+), 12 deletions(-) diff --git a/iftop.h b/iftop.h index b924642..42947af 100644 --- a/iftop.h +++ b/iftop.h @@ -8,7 +8,7 @@ #include "config.h" -/* 60 / 3 */ +/* 40 / 2 */ #define HISTORY_LENGTH 20 #define RESOLUTION 2 diff --git a/options.c b/options.c index 12bff2e..ad673f4 100644 --- a/options.c +++ b/options.c @@ -28,7 +28,7 @@ options_t options; -char optstr[] = "+i:f:nN:hpbBP"; +char optstr[] = "+i:f:nN:hpbBPm:"; /* Global options. */ @@ -112,6 +112,8 @@ static void set_defaults() { options.linedisplay = OPTION_LINEDISPLAY_TWO_LINE; options.screen_offset = 0; options.show_totals = 0; + options.max_bandwidth = 0; /* auto */ + options.log_scale = 1; /* auto */ } static void die(char *msg) { @@ -119,6 +121,32 @@ static void die(char *msg) { exit(1); } +static void set_max_bandwidth(char* arg) { + char* units; + long long mult = 1; + long long value; + units = arg + strspn(arg, "0123456789"); + if(strlen(units) > 1) { + die("Invalid units\n"); + } + if(strlen(units) == 1) { + if(*units == 'k' || *units == 'K') { + mult = 1024; + } + else if(*units == 'm' || *units == 'M') { + mult = 1024 * 1024; + } + else if(*units == 'g' || *units == 'G') { + mult = 1024 * 1024 * 1024; + } + } + *units = '\0'; + if(sscanf(arg, "%lld", &value) != 1) { + die("Error reading max bandwidth\n"); + } + options.max_bandwidth = value * mult; +} + static void set_net_filter(char* arg) { char* mask; @@ -180,6 +208,7 @@ static void usage(FILE *fp) { " (default: none, but only IP packets are counted)\n" " -N net/mask show traffic flows in/out of network\n" " -P show ports as well as hosts\n" +" -m limit sets the upper limit for the bandwidth scale\n" "\n" "iftop, version " IFTOP_VERSION "\n" "copyright (c) 2002 Paul Warren and contributors\n" @@ -212,7 +241,7 @@ void options_read(int argc, char **argv) { case 'p': options.promiscuous = 1; - options.promiscuous_but_choosy = 0; + options.promiscuous_but_choosy = 0; break; case 'P': @@ -222,6 +251,11 @@ void options_read(int argc, char **argv) { case 'N': set_net_filter(optarg); break; + + case 'm': + set_max_bandwidth(optarg); + break; + case 'b': options.showbars = 0; diff --git a/options.h b/options.h index 5289a1e..f2d886e 100644 --- a/options.h +++ b/options.h @@ -66,6 +66,9 @@ typedef struct { int show_totals; + long long max_bandwidth; + int log_scale; + } options_t; #endif /* __OPTIONS_H_ */ diff --git a/ui.c b/ui.c index 683cffb..efd86a2 100644 --- a/ui.c +++ b/ui.c @@ -202,6 +202,25 @@ static struct { }; static int rateidx = 0, wantbiggerrate; +static int get_bar_interval(float bandwidth) { + int i = 10; + if(bandwidth > 100000000) { + i = 100; + } + return i; +} + +static float get_max_bandwidth() { + float max; + if(options.max_bandwidth > 0) { + max = options.max_bandwidth; + } + else { + max = scale[rateidx].max; + } + return max; +} + /* rate in bits */ static int get_bar_length(const int rate) { float l; @@ -209,28 +228,56 @@ static int get_bar_length(const int rate) { return 0; if (rate > scale[rateidx].max) wantbiggerrate = 1; - l = log(rate) / log(scale[rateidx].max); + if(options.log_scale) { + l = log(rate) / log(get_max_bandwidth()); + } + else { + l = rate / get_max_bandwidth(); + } return (l * COLS); } static void draw_bar_scale(int* y) { float i; + float max,interval; + max = get_max_bandwidth(); + interval = get_bar_interval(max); if(options.showbars) { + float stop; /* Draw bar graph scale on top of the window. */ move(*y, 0); clrtoeol(); mvhline(*y + 1, 0, 0, COLS); /* i in bytes */ - for (i = 1.25; i * 8 <= scale[rateidx].max; i *= scale[rateidx].interval) { + + if(options.log_scale) { + i = 1.25; + stop = max / 8; + } + else { + i = max / (5 * 8); + stop = max / 8; + } + + /* for (i = 1.25; i * 8 <= max; i *= interval) { */ + while(i <= stop) { char s[40], *p; int x; - readable_size(i, s, sizeof s, 1000, 0); + /* This 1024 vs 1000 stuff is just plain evil */ + readable_size(i, s, sizeof s, options.log_scale ? 1000 : 1024, 0); p = s + strspn(s, " "); x = get_bar_length(i * 8); mvaddch(*y + 1, x, ACS_BTEE); if (x + strlen(p) >= COLS) x = COLS - strlen(p); mvaddstr(*y, x, p); + + if(options.log_scale) { + i *= interval; + } + else { + i += max / (5 * 8); + } } mvaddch(*y + 1, 0, ACS_LLCORNER); *y += 2; @@ -273,11 +320,11 @@ void draw_line_total(float sent, float recv, int y, int x, option_linedisplay_t } void draw_bar(float n, int y) { - int L; - mvchgat(y, 0, -1, A_NORMAL, 0, NULL); - L = get_bar_length(8 * n); - if (L > 0) - mvchgat(y, 0, L + 1, A_REVERSE, 0, NULL); + int L; + mvchgat(y, 0, -1, A_NORMAL, 0, NULL); + L = get_bar_length(8 * n); + if (L > 0) + mvchgat(y, 0, L + 1, A_REVERSE, 0, NULL); } void draw_line_totals(int y, host_pair_line* line, option_linedisplay_t linedisplay) { @@ -677,7 +724,7 @@ void ui_print() { refresh(); /* Bar chart auto scale */ - if (wantbiggerrate) { + if (wantbiggerrate && options.max_bandwidth == 0) { ++rateidx; wantbiggerrate = 0; }