diff --git a/canbusload.c b/canbusload.c index 4679da5..418b2ba 100644 --- a/canbusload.c +++ b/canbusload.c @@ -74,6 +74,7 @@ #define PERCENTRES 5 /* resolution in percent for bargraph */ #define NUMBAR (100 / PERCENTRES) /* number of bargraph elements */ #define BRSTRLEN 20 +#define VISUAL_WINDOW 90 /* window width for visualization */ /* * Inspired from @@ -111,19 +112,22 @@ static struct { unsigned int load_1m; unsigned int load_5m; unsigned int load_15m; + unsigned int loads[VISUAL_WINDOW]; + unsigned int index; } stat[MAXDEVS + 1]; static volatile int running = 1; static volatile sig_atomic_t signal_num; static int max_devname_len; /* to prevent frazzled device name output */ static int max_bitratestr_len; -static int currmax; +static unsigned int currmax; static unsigned char redraw; static unsigned char timestamp; static unsigned char color; static unsigned char bargraph; static bool statistic; static bool reset; +static bool visualize; static enum cfl_mode mode = CFL_WORSTCASE; static char *prg; static struct termios old; @@ -141,6 +145,7 @@ static void print_usage(char *prg) fprintf(stderr, " -i (ignore bitstuffing in bandwidth calculation)\n"); fprintf(stderr, " -e (exact calculation of stuffed bits)\n"); fprintf(stderr, " -s (show statistics, press 'r' to reset)\n"); + fprintf(stderr, " -v (show busload visualization)\n"); fprintf(stderr, "\n"); fprintf(stderr, "Up to %d CAN interfaces with mandatory bitrate can be specified on the \n", MAXDEVS); fprintf(stderr, "commandline in the form: @[,]\n"); @@ -203,7 +208,7 @@ static unsigned int calc_load(unsigned int load_fp, static void printstats(int signo) { - int i, j, percent; + unsigned int i, j, k, percent, index; if (redraw) printf("%s", CSR_HOME); @@ -315,6 +320,28 @@ static void printstats(int signo) printf("|"); } + if (visualize) { + stat[i].loads[stat[i].index] = percent; + stat[i].index = (stat[i].index + 1) % VISUAL_WINDOW; + + printf("\n"); + for (j = 0; j < NUMBAR; j++) { + printf("%3d%%|", (NUMBAR - j) * PERCENTRES); + index = stat[i].index; + for (k = 0; k < VISUAL_WINDOW; k++) { + percent = stat[i].loads[index]; + + if ((percent / PERCENTRES) >= (NUMBAR - j)) + printf("X"); + else + printf("."); + + index = (index + 1) % VISUAL_WINDOW; + } + printf("\n"); + } + } + if (color) printf("%s", ATTRESET); @@ -353,7 +380,8 @@ int main(int argc, char **argv) struct canfd_frame frame; struct iovec iov; struct msghdr msg; - int nbytes, i; + unsigned int i; + int nbytes; int have_anydev = 0; unsigned int anydev_bitrate = 0; @@ -375,7 +403,7 @@ int main(int argc, char **argv) prg = basename(argv[0]); - while ((opt = getopt(argc, argv, "rtbciesh?")) != -1) { + while ((opt = getopt(argc, argv, "rtbciesvh?")) != -1) { switch (opt) { case 'r': redraw = 1; @@ -406,6 +434,10 @@ int main(int argc, char **argv) reset = true; break; + case 'v': + visualize = true; + break; + default: print_usage(prg); exit(1);