From: Zhu Yi <yi.zhu5@xxxxxxxxxxxx> Add '-v' option for visualize busload, the output shows a moving histogram of the past 90 seconds. canbusload 2024-09-23 17:19:33 (exact bitstuffing) can0@500k 487 55558 31048 0 99% |XXXXXXXXXXXXXXXXXXX.| 100%|.......................................................................................... 95%|..............................................................................XXXXXXXXXXXX 90%|.............................................................................XXXXXXXXXXXXX 85%|.............................................................................XXXXXXXXXXXXX 80%|.............................................................................XXXXXXXXXXXXX 75%|.............................................................................XXXXXXXXXXXXX 70%|.............................................................................XXXXXXXXXXXXX 65%|.............................................................................XXXXXXXXXXXXX 60%|............................................................................XXXXXXXXXXXXXX 55%|............................................................................XXXXXXXXXXXXXX 50%|............................................................................XXXXXXXXXXXXXX 45%|............................................................................XXXXXXXXXXXXXX 40%|............................................................................XXXXXXXXXXXXXX 35%|.........................................XXX................................XXXXXXXXXXXXXX 30%|.........................................XXXX...............................XXXXXXXXXXXXXX 25%|........................................XXXXXX.............................XXXXXXXXXXXXXXX 20%|XXXXXXXX...............................XXXXXXXXXXXXXXXXX....XXXXXXXXXXX...XXXXXXXXXXXXXXXX 15%|XXXXXXXXX.............................XXXXXXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 10%|XXXXXXXXX.XXXXXXXXXXXXXXXXXXX..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 5%|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Signed-off-by: Zhu Yi <yi.zhu5@xxxxxxxxxxxx> Signed-off-by: Hubert Streidl <hubert.streidl@xxxxxxxxxxxx> Signed-off-by: Mark Jonas <mark.jonas@xxxxxxxxxxxx> --- canbusload.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/canbusload.c b/canbusload.c index 577d99c..139d3d7 100644 --- a/canbusload.c +++ b/canbusload.c @@ -73,6 +73,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 @@ -120,6 +121,8 @@ 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; @@ -133,6 +136,7 @@ static unsigned char color; static unsigned char bargraph; static unsigned char statistic; static unsigned char reset; +static unsigned char visualize; static enum cfl_mode mode = CFL_WORSTCASE; static char *prg; static struct termios old; @@ -150,6 +154,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: <ifname>@<bitrate>[,<dbitrate>]\n"); @@ -202,7 +207,7 @@ static void create_bitrate_string(int stat_idx, int *max_bitratestr_len) static void printstats(int signo) { - int i, j, percent; + int i, j, k, percent, index; if (redraw) printf("%s", CSR_HOME); @@ -314,6 +319,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); @@ -375,7 +402,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 +433,10 @@ int main(int argc, char **argv) reset = 1; break; + case 'v': + visualize = 1; + break; + default: print_usage(prg); exit(1); -- 2.34.1