Skip to contentSkip to conversation

cangen: support socket priority

PATCH In review [email protected] 2 participants 8 messages Jan 20, 2025 – Jan 22, 2025 CAN network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 cangen: support socket priority

From: Zhu Yi <[email protected]>

Add '-P' option for allow user to set the socket priority. This can be useful in conjuction with queuing discipline.

---
 cangen.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
cangen.c +19 -1
index d9a0448..1346eb7 100644
@@ -189,6 +189,7 @@ static void print_usage(char *prg)
 	fprintf(stderr, "         -A <mode>     (CAN XL AF generation mode - see below, no e/o mode)\n");
 	fprintf(stderr, "         -V <mode>     (CAN XL VCID generation mode - see below, no e/o mode)\n");
 	fprintf(stderr, "         -p <timeout>  (poll on -ENOBUFS to write frames with <timeout> ms)\n");
+	fprintf(stderr, "         -P <priority> (set socket priority using SO_PRIORITY)\n");
 	fprintf(stderr, "         -n <count>    (terminate after <count> CAN frames - default infinite)\n");
 	fprintf(stderr, "         -i            (ignore -ENOBUFS return values on write() syscalls)\n");
 	fprintf(stderr, "         -x            (disable local loopback of generated CAN frames)\n");
@@ -479,6 +480,7 @@ int main(int argc, char **argv)
 	uint64_t incdata = 0;
 	__u8 *data; /* base pointer for CC/FD or XL data */
 	int incdlc = 0;
+	int priority = -1;
 	unsigned long rnd;
 	unsigned char fixdata[CANFD_MAX_DLEN];
 	unsigned char rand_position[CANFD_MAX_DLEN] = { 0 };
@@ -512,7 +514,7 @@ int main(int argc, char **argv)
 		{ 0,		0,			0, 0 },
 	};
 
-	while ((opt = getopt_long(argc, argv, "g:atefbEXR8mI:L:D:F:S:A:V:p:n:ixc:vh?", long_options, NULL)) != -1) {
+	while ((opt = getopt_long(argc, argv, "g:atefbEXR8mI:L:D:F:S:A:V:p:P:n:ixc:vh?", long_options, NULL)) != -1) {
 		switch (opt) {
 		case 'g':
 			gap = strtod(optarg, NULL);
@@ -682,6 +684,14 @@ int main(int argc, char **argv)
 			}
 			break;
 
+		case 'P':
+			priority = atoi(optarg);
+			if (priority < 0) {
+				printf("socket priority has to be >= 0\n");
+				return 1;
+			}
+			break;
+
 		case 'i':
 			ignore_enobufs = true;
 			break;
@@ -750,6 +760,14 @@ int main(int argc, char **argv)
 	 */
 	setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
 
+	/*
+	 * user can use tc to configure the queuing discipline (e.g. mqprio),
+	 * together with SO_PRIORITY option to specify the message send from
+	 * this socket should go to which queue.
+	 */
+	if (priority >= 0)
+		setsockopt(s, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority));
+
 	if (loopback_disable) {
 		const int loopback = 0;
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/3 canbusload: support busload statistic

From: Zhu Yi <[email protected]>

Add '-s' option for display busload statistic, the output contains minimal, maximum and exponentially-damped moving sums of one second average (borrowed from Linux load average algorithm) since start or reset (press 'r' while running).

canbusload 2024-09-23 17:15:18 (exact bitstuffing)
can0@500k 942 107535 60168 0 18% min: 0%, max: 21%, load: 16% 6% 2% |XXX.................|

---
 canbusload.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 91 insertions(+), 1 deletion(-)
canbusload.c +91 -1
index 753d658..577d99c 100644
@@ -48,11 +48,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <termios.h>
 #include <time.h>
 #include <unistd.h>
 
 #include <net/if.h>
 #include <sys/ioctl.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/types.h>
@@ -72,6 +74,34 @@
 #define NUMBAR (100 / PERCENTRES) /* number of bargraph elements */
 #define BRSTRLEN 20
 
+/*
+ * Inspired from
+ * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/
+ * include/linux/sched/loadavg.h
+ *
+ * Following are the fixed-point math constants and the exponential-damping
+ * factors for:
+ *  - 1 samples/s in 1 minute
+ *  - 1 samples/s in 5 minutes
+ *  - 1 samples/s in 15 minutes
+ * in fixed-point representation.
+ */
+#define FP_SHIFT 12              /* bits of precision */
+#define FP_ONE   (1 << FP_SHIFT) /* 1.0 fixed-point representation */
+#define EXP_1    4028            /* (1 / e ^ (1 /  60)) * FP_ONE */
+#define EXP_5    4082            /* (1 / e ^ (1 / 300)) * FP_ONE */
+#define EXP_15   4091            /* (1 / e ^ (1 / 900)) * FP_ONE */
+
+inline unsigned int calc_load(unsigned int load_fp,
+                              unsigned int exp_fp,
+                              unsigned int sample)
+{
+	unsigned int sample_fp  = sample << FP_SHIFT;
+	unsigned int damped_sum = (load_fp * exp_fp) +
+	                          (sample_fp * (FP_ONE - exp_fp));
+	return damped_sum >> FP_SHIFT;
+}
+
 extern int optind, opterr, optopt;
 
 static struct {
@@ -85,6 +115,11 @@ static struct {
 	unsigned int recv_bits_total;
 	unsigned int recv_bits_payload;
 	unsigned int recv_bits_dbitrate;
+	unsigned int load_min;
+	unsigned int load_max;
+	unsigned int load_1m;
+	unsigned int load_5m;
+	unsigned int load_15m;
 } stat[MAXDEVS + 1];
 
 static volatile int running = 1;
@@ -96,8 +131,11 @@ static unsigned char redraw;
 static unsigned char timestamp;
 static unsigned char color;
 static unsigned char bargraph;
+static unsigned char statistic;
+static unsigned char reset;
 static enum cfl_mode mode = CFL_WORSTCASE;
 static char *prg;
+static struct termios old;
 
 static void print_usage(char *prg)
 {
@@ -111,6 +149,7 @@ static void print_usage(char *prg)
 	fprintf(stderr, "         -r  (redraw the terminal - similar to top)\n");
 	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, "\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");
@@ -234,6 +273,30 @@ static void printstats(int signo)
 		       stat[i].recv_bits_dbitrate,
 		       percent);
 
+		if (statistic) {
+			if (reset) {
+				stat[i].load_min = UINT_MAX;
+				stat[i].load_max = 0;
+				stat[i].load_1m = 0;
+				stat[i].load_5m = 0;
+				stat[i].load_15m = 0;
+			}
+
+			stat[i].load_min = MIN(stat[i].load_min, percent);
+			stat[i].load_max = MAX(stat[i].load_max, percent);
+
+			stat[i].load_1m = calc_load(stat[i].load_1m, EXP_1, percent);
+			stat[i].load_5m = calc_load(stat[i].load_5m, EXP_5, percent);
+			stat[i].load_15m = calc_load(stat[i].load_15m, EXP_15, percent);
+
+			printf(" min:%3d%%, max:%3d%%, load:%3d%% %3d%% %3d%%",
+			       stat[i].load_min,
+			       stat[i].load_max,
+			       (stat[i].load_1m + (FP_ONE >> 1)) >> FP_SHIFT,
+			       (stat[i].load_5m + (FP_ONE >> 1)) >> FP_SHIFT,
+			       (stat[i].load_15m + (FP_ONE >> 1)) >> FP_SHIFT);
+		}
+
 		if (bargraph) {
 
 			printf(" |");
@@ -264,6 +327,8 @@ static void printstats(int signo)
 		stat[i].recv_direction = '.';
 	}
 
+	reset = 0;
+
 	if (!redraw)
 		printf("\n");
 
@@ -272,6 +337,11 @@ static void printstats(int signo)
 	alarm(1);
 }
 
+void cleanup()
+{
+	tcsetattr(STDIN_FILENO, TCSANOW, &old);
+}
+
 int main(int argc, char **argv)
 {
 	fd_set rdfs;
@@ -289,6 +359,14 @@ int main(int argc, char **argv)
 	unsigned int anydev_dbitrate = 0;
 	char anydev_bitratestr[BRSTRLEN]; /* 100000/2000000 => 100k/2M */
 
+	struct termios temp;
+
+	tcgetattr(STDIN_FILENO, &old);
+	atexit(cleanup);
+	temp = old;
+	temp.c_lflag &= ~(ICANON | ECHO);
+	tcsetattr(STDIN_FILENO, TCSANOW, &temp);
+
 	signal(SIGTERM, sigterm);
 	signal(SIGHUP, sigterm);
 	signal(SIGINT, sigterm);
@@ -297,7 +375,7 @@ int main(int argc, char **argv)
 
 	prg = basename(argv[0]);
 
-	while ((opt = getopt(argc, argv, "rtbcieh?")) != -1) {
+	while ((opt = getopt(argc, argv, "rtbciesh?")) != -1) {
 		switch (opt) {
 		case 'r':
 			redraw = 1;
@@ -323,6 +401,11 @@ int main(int argc, char **argv)
 			mode = CFL_EXACT;
 			break;
 
+		case 's':
+			statistic = 1;
+			reset = 1;
+			break;
+
 		default:
 			print_usage(prg);
 			exit(1);
@@ -449,12 +532,19 @@ int main(int argc, char **argv)
 	while (running) {
 		FD_ZERO(&rdfs);
 		FD_SET(s, &rdfs);
+		FD_SET(STDIN_FILENO, &rdfs);
 
 		if (select(s + 1, &rdfs, NULL, NULL, NULL) < 0) {
 			//perror("pselect");
 			continue;
 		}
 
+		if (FD_ISSET(STDIN_FILENO, &rdfs)) {
+			if (getchar() == 'r') {
+				reset = 1;
+			}
+		}
+
 		/* these settings may be modified by recvmsg() */
 		iov.iov_len = sizeof(frame);
 		msg.msg_namelen = sizeof(addr);
Signature · 1 line
2.34.1
#2 lore raw

Re: [can-utils][PATCH 2/3] canbusload: support busload statistic

Quoted 57 lines · On 20.01.2025 17:23:31, [email protected] wrote:

From: Zhu Yi <[email protected]>

Add '-s' option for display busload statistic, the output contains minimal, maximum and exponentially-damped moving sums of one second average (borrowed from Linux load average algorithm) since start or reset (press 'r' while running).

canbusload 2024-09-23 17:15:18 (exact bitstuffing)
can0@500k 942 107535 60168 0 18% min: 0%, max: 21%, load: 16% 6% 2% |XXX.................|

---
 canbusload.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 91 insertions(+), 1 deletion(-)
canbusload.c +20 -0
index 753d658..577d99c 100644
@@ -48,11 +48,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <termios.h>
 #include <time.h>
 #include <unistd.h>
 
 #include <net/if.h>
 #include <sys/ioctl.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/types.h>
@@ -72,6 +74,34 @@
 #define NUMBAR (100 / PERCENTRES) /* number of bargraph elements */
 #define BRSTRLEN 20
 
+/*
+ * Inspired from
+ * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/
+ * include/linux/sched/loadavg.h
+ *
+ * Following are the fixed-point math constants and the exponential-damping
+ * factors for:
+ *  - 1 samples/s in 1 minute
+ *  - 1 samples/s in 5 minutes
+ *  - 1 samples/s in 15 minutes
+ * in fixed-point representation.
+ */
+#define FP_SHIFT 12              /* bits of precision */
+#define FP_ONE   (1 << FP_SHIFT) /* 1.0 fixed-point representation */
+#define EXP_1    4028            /* (1 / e ^ (1 /  60)) * FP_ONE */
+#define EXP_5    4082            /* (1 / e ^ (1 / 300)) * FP_ONE */
+#define EXP_15   4091            /* (1 / e ^ (1 / 900)) * FP_ONE */
+

canbusload.c line 104

Quoted 10 lines
canbusload.c +10 -0
+inline unsigned int calc_load(unsigned int load_fp,
+                              unsigned int exp_fp,
+                              unsigned int sample)
+{
+	unsigned int sample_fp  = sample << FP_SHIFT;
+	unsigned int damped_sum = (load_fp * exp_fp) +
+	                          (sample_fp * (FP_ONE - exp_fp));
+	return damped_sum >> FP_SHIFT;
+}
+

Had to make this a "static" function, otherwise it fails to link. Some singed/unsigned changes were also needed. Can you check if this still works for you:

| https://github.com/linux-can/can-utils/pull/571

So far compile tested only.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#3 lore raw

AW: [can-utils][PATCH 2/3] canbusload: support busload statistic

Hi Marc,

Quoted 88 lines

-----Ursprüngliche Nachricht-----
Von: Marc Kleine-Budde <[email protected]>
Gesendet: Montag, 20. Januar 2025 18:11
An: Jonas Mark (BT-FS/ENG1-Mue) <[email protected]>
Cc: [email protected]; Zhu Yi <[email protected]>; Streidl Hubert (BT-FS/ENG1-Mue) <[email protected]>
Betreff: Re: [can-utils][PATCH 2/3] canbusload: support busload statistic

Quoted 9 lines · On 20.01.2025 17:23:31, [email protected] wrote:

From: Zhu Yi <[email protected]>

Add '-s' option for display busload statistic, the output contains minimal, maximum and exponentially-damped moving sums of one second average (borrowed from Linux load average algorithm) since start or reset (press 'r' while running).

canbusload 2024-09-23 17:15:18 (exact bitstuffing)
can0@500k 942 107535 60168 0 18% min: 0%, max: 21%,

load: 16% 6% 2% |XXX.................|

canbusload.c index 753d658..577d99c

Quoted 10 lines
---
 canbusload.c | 92
+++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 91 insertions(+), 1 deletion(-)
canbusload.c index 753d658..577d99c renamed from canbusload.c +0 -0

100644

Quoted 17 lines
canbusload.c +2 -0
@@ -48,11 +48,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <termios.h>
 #include <time.h>
 #include <unistd.h>
 
 #include <net/if.h>
 #include <sys/ioctl.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/types.h>

canbusload.c line 80

Quoted 7 lines
canbusload.c +3 -0
@@ -72,6 +74,34 @@
 #define NUMBAR (100 / PERCENTRES) /* number of bargraph elements */
 #define BRSTRLEN 20
 
+/*
+ * Inspired from
+ *
 

+https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tr

canbusload.c

Quoted 13 lines
canbusload.c +13 -0
+ee/
+ * include/linux/sched/loadavg.h
+ *
+ * Following are the fixed-point math constants and the
+exponential-damping
+ * factors for:
+ *  - 1 samples/s in 1 minute
+ *  - 1 samples/s in 5 minutes
+ *  - 1 samples/s in 15 minutes
+ * in fixed-point representation.
+ */
+#define FP_SHIFT 12              /* bits of precision */
+#define FP_ONE   (1 << FP_SHIFT) /* 1.0 fixed-point representation

*/

canbusload.c line 104

Quoted 13 lines
canbusload.c +13 -0
+#define EXP_1    4028            /* (1 / e ^ (1 /  60)) * FP_ONE */
+#define EXP_5    4082            /* (1 / e ^ (1 / 300)) * FP_ONE */
+#define EXP_15   4091            /* (1 / e ^ (1 / 900)) * FP_ONE */
+
+inline unsigned int calc_load(unsigned int load_fp,
+                              unsigned int exp_fp,
+                              unsigned int sample) {
+	unsigned int sample_fp  = sample << FP_SHIFT;
+	unsigned int damped_sum = (load_fp * exp_fp) +
+	                          (sample_fp * (FP_ONE - exp_fp));
+	return damped_sum >> FP_SHIFT;
+}
+

Had to make this a "static" function, otherwise it fails to link. Some singed/unsigned changes were also needed. Can you check if this still works for you:

Quoted 3 lines

| https://github.com/linux-can/can-utils/pull/571

So far compile tested only.

Thank you for your feedback. We'll have a look it by the end of the week.

We compiled and tested with Ubuntu 22.04 on amd64 (standard PC). That is, we used gcc 11.4 and ld (binutils) 2.38. For us it linked flawlessly and compiled without warnings. Which version of gcc and ld did you use?

Thanks,
Mark

#4 lore raw

Re: AW: [can-utils][PATCH 2/3] canbusload: support busload statistic

Quoted 6 lines · On 21.01.2025 07:51:50, Jonas Mark (BT-FS/ENG1-Mue) wrote:
Quoted 6 lines

Had to make this a "static" function, otherwise it fails to link. Some singed/unsigned changes were also needed. Can you check if this still works for you:

| https://github.com/linux-can/can-utils/pull/571

Quoted 8 lines
Quoted 1 line

So far compile tested only.

Thank you for your feedback. We'll have a look it by the end of the week.

We compiled and tested with Ubuntu 22.04 on amd64 (standard PC). That is, we used gcc 11.4 and ld (binutils) 2.38. For us it linked flawlessly and compiled without warnings. Which version of gcc and ld did you use?

It basically fails on all of our CI plaforms (ubuntu and debian):

| https://github.com/linux-can/can-utils/actions/runs/12872465505/job/35887996190

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#5 lore raw

AW: AW: [can-utils][PATCH 2/3] canbusload: support busload statistic

Hi Marc,

Quoted 9 lines

-----Ursprüngliche Nachricht-----
Von: Marc Kleine-Budde <[email protected]>
Gesendet: Dienstag, 21. Januar 2025 10:32
An: Jonas Mark (BT-FS/ENG1-Mue) <[email protected]>
Cc: [email protected]; Streidl Hubert (BT-FS/ENG1-Mue) <[email protected]>
Betreff: Re: AW: [can-utils][PATCH 2/3] canbusload: support busload statistic

Quoted 9 lines
Quoted 2 lines · On 21.01.2025 07:51:50, Jonas Mark (BT-FS/ENG1-Mue) wrote:
Quoted 2 lines

Had to make this a "static" function, otherwise it fails to link. Some singed/unsigned changes were also needed. Can you check if

this

Quoted 5 lines
Quoted 5 lines

still works for you:

| https://github.com/linux-can/can-utils/pull/571

So far compile tested only.

We compiled and tested the code from https://github.com/marckleinebudde/can-utils/tree/cangen-canbusload. It works as expected. Thank you for the fixes.

Quoted 7 lines
Quoted 1 line

Thank you for your feedback. We'll have a look it by the end of the

week.

Quoted 2 lines

We compiled and tested with Ubuntu 22.04 on amd64 (standard PC).

That

Quoted 2 lines

is, we used gcc 11.4 and ld (binutils) 2.38. For us it linked flawlessly and compiled without warnings. Which version of gcc and

Quoted 8 lines

ld

Quoted 1 line

did you use?

It basically fails on all of our CI plaforms (ubuntu and debian):

| https://github.com/linux-can/can-
utils/actions/runs/12872465505/job/35
| 887996190

We now understood what the problem was: We only compiled using gcc but the project supports gcc and clang. So we missed the clang problems we introduced. Next time we'll use the project's GitHub actions to validate our code.

Cheers,
Mark

#6 lore raw

Re: AW: AW: [can-utils][PATCH 2/3] canbusload: support busload statistic

Quoted 8 lines · On 22.01.2025 14:53:30, Jonas Mark (BT-FS/ENG1-Mue) wrote:
Quoted 3 lines

It basically fails on all of our CI plaforms (ubuntu and debian):

| https://github.com/linux-can/can-utils/actions/runs/12872465505/job/35887996190

We now understood what the problem was: We only compiled using gcc but the project supports gcc and clang. So we missed the clang problems we introduced. Next time we'll use the project's GitHub actions to validate our code.

Thanks again for contributing.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#7 lore raw

PATCH 3/3 canbusload: support busload visualization

In reply to [email protected]

From: Zhu Yi <[email protected]>

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
---
 canbusload.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
canbusload.c +33 -2
index 577d99c..139d3d7 100644
@@ -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);
Signature · 1 line
2.34.1