Added option for lin/log scale (not yet accessible)
Added ability to specify fixed maximum for bandwidth scale.
This commit is contained in:
2
iftop.h
2
iftop.h
@@ -8,7 +8,7 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
/* 60 / 3 */
|
||||
/* 40 / 2 */
|
||||
#define HISTORY_LENGTH 20
|
||||
#define RESOLUTION 2
|
||||
|
||||
|
||||
36
options.c
36
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 <pdw@ex-parrot.com> and contributors\n"
|
||||
@@ -223,6 +252,11 @@ void options_read(int argc, char **argv) {
|
||||
set_net_filter(optarg);
|
||||
break;
|
||||
|
||||
case 'm':
|
||||
set_max_bandwidth(optarg);
|
||||
break;
|
||||
|
||||
|
||||
case 'b':
|
||||
options.showbars = 0;
|
||||
break;
|
||||
|
||||
@@ -66,6 +66,9 @@ typedef struct {
|
||||
|
||||
int show_totals;
|
||||
|
||||
long long max_bandwidth;
|
||||
int log_scale;
|
||||
|
||||
} options_t;
|
||||
|
||||
#endif /* __OPTIONS_H_ */
|
||||
|
||||
55
ui.c
55
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;
|
||||
@@ -677,7 +724,7 @@ void ui_print() {
|
||||
refresh();
|
||||
|
||||
/* Bar chart auto scale */
|
||||
if (wantbiggerrate) {
|
||||
if (wantbiggerrate && options.max_bandwidth == 0) {
|
||||
++rateidx;
|
||||
wantbiggerrate = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user