All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] canbusload: count full payload for CANFD frames
@ 2021-06-24  3:21 Kurt Van Dijck
  2021-06-24  3:21 ` [PATCH 2/2] canbusload: count databitrate seperately Kurt Van Dijck
  2021-06-24  6:22 ` [PATCH 1/2] canbusload: count full payload for CANFD frames Marc Kleine-Budde
  0 siblings, 2 replies; 6+ messages in thread
From: Kurt Van Dijck @ 2021-06-24  3:21 UTC (permalink / raw)
  To: linux-can; +Cc: Kurt Van Dijck

Signed-off-by: Kurt Van Dijck <dev.kurt@vandijck-laurijssen.be>
---
 canbusload.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/canbusload.c b/canbusload.c
index d6bfd78..bd74cb5 100644
--- a/canbusload.c
+++ b/canbusload.c
@@ -237,7 +237,7 @@ int main(int argc, char **argv)
 	int opt;
 	char *ptr, *nptr;
 	struct sockaddr_can addr;
-	struct can_frame frame;
+	struct canfd_frame frame;
 	int nbytes, i;
 	struct ifreq ifr;
 	sigset_t sigmask, savesigmask;
@@ -351,6 +351,9 @@ int main(int argc, char **argv)
 #ifdef DEBUG
 		printf("using interface name '%s'.\n", ifr.ifr_name);
 #endif
+		/* try to switch the socket into CAN FD mode */
+		const int canfd_on = 1;
+		setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
 
 		if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
 			perror("SIOCGIFINDEX");
@@ -402,9 +405,9 @@ int main(int argc, char **argv)
 				}
 
 				stat[i].recv_frames++;
-				stat[i].recv_bits_payload += frame.can_dlc*8;
-				stat[i].recv_bits_total += can_frame_length((struct canfd_frame*)&frame,
-									    mode, sizeof(frame));
+				stat[i].recv_bits_payload += frame.len*8;
+				stat[i].recv_bits_total += can_frame_length(&frame,
+									    mode, nbytes);
 			}
 		}
 	}
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] canbusload: count databitrate seperately
  2021-06-24  3:21 [PATCH 1/2] canbusload: count full payload for CANFD frames Kurt Van Dijck
@ 2021-06-24  3:21 ` Kurt Van Dijck
  2021-06-24  6:25   ` Marc Kleine-Budde
  2021-06-24  6:22 ` [PATCH 1/2] canbusload: count full payload for CANFD frames Marc Kleine-Budde
  1 sibling, 1 reply; 6+ messages in thread
From: Kurt Van Dijck @ 2021-06-24  3:21 UTC (permalink / raw)
  To: linux-can; +Cc: Kurt Van Dijck

Signed-off-by: Kurt Van Dijck <dev.kurt@vandijck-laurijssen.be>
---
 canbusload.c  | 20 ++++++++++++++++----
 canframelen.c | 28 ++++++++++++++++++++++++++++
 canframelen.h |  1 +
 3 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/canbusload.c b/canbusload.c
index bd74cb5..1bbfc72 100644
--- a/canbusload.c
+++ b/canbusload.c
@@ -74,9 +74,11 @@ extern int optind, opterr, optopt;
 static struct {
 	char devname[IFNAMSIZ+1];
 	unsigned int bitrate;
+	unsigned int dbitrate;
 	unsigned int recv_frames;
 	unsigned int recv_bits_total;
 	unsigned int recv_bits_payload;
+	unsigned int recv_bits_dbitrate;
 } stat[MAXSOCK+1];
 
 static int  max_devname_len; /* to prevent frazzled device name output */ 
@@ -103,7 +105,7 @@ void print_usage(char *prg)
 	fprintf(stderr, "         -e  (exact calculation of stuffed bits)\n");
 	fprintf(stderr, "\n");
 	fprintf(stderr, "Up to %d CAN interfaces with mandatory bitrate can be specified on the \n", MAXSOCK);
-	fprintf(stderr, "commandline in the form: <ifname>@<bitrate>\n\n");
+	fprintf(stderr, "commandline in the form: <ifname>@<bitrate>[,<dbitrate>]\n\n");
 	fprintf(stderr, "The bitrate is mandatory as it is needed to know the CAN bus bitrate to\n");
 	fprintf(stderr, "calculate the bus load percentage based on the received CAN frames.\n");
 	fprintf(stderr, "Due to the bitstuffing estimation the calculated busload may exceed 100%%.\n");
@@ -184,16 +186,18 @@ void printstats(int signo)
 		}
 
 		if (stat[i].bitrate)
-			percent = (stat[i].recv_bits_total*100)/stat[i].bitrate;
+			percent = ((stat[i].recv_bits_total-stat[i].recv_bits_dbitrate)*100)/stat[i].bitrate
+				+ (stat[i].recv_bits_dbitrate*100)/stat[i].dbitrate;
 		else
 			percent = 0;
 
-		printf(" %*s@%-*d %5d %7d %6d %3d%%",
+		printf(" %*s@%-*d %5d %7d %6d %6d %3d%%",
 		       max_devname_len, stat[i].devname,
 		       max_bitrate_len, stat[i].bitrate,
 		       stat[i].recv_frames,
 		       stat[i].recv_bits_total,
 		       stat[i].recv_bits_payload,
+		       stat[i].recv_bits_dbitrate,
 		       percent);
 
 		if (bargraph) {
@@ -220,6 +224,7 @@ void printstats(int signo)
 
 		stat[i].recv_frames = 0;
 		stat[i].recv_bits_total = 0;
+		stat[i].recv_bits_dbitrate = 0;
 		stat[i].recv_bits_payload = 0;
 	}
 
@@ -336,7 +341,12 @@ int main(int argc, char **argv)
 		if (nbytes > max_devname_len)
 			max_devname_len = nbytes; /* for nice printing */
 
-		stat[i].bitrate = atoi(nptr+1); /* bitrate is placed behind the '@' */
+		char *endp;
+		stat[i].bitrate = strtol(nptr+1, &endp, 0); /* bitrate is placed behind the '@' */
+		if (*endp == ',')
+			stat[i].dbitrate = strtol(endp+1, &endp, 0); /* bitrate is placed behind the ',' */
+		else
+			stat[i].dbitrate = stat[i].bitrate;
 
 		if (!stat[i].bitrate || stat[i].bitrate > 1000000) {
 			printf("invalid bitrate for CAN device '%s'!\n", ptr);
@@ -406,6 +416,8 @@ int main(int argc, char **argv)
 
 				stat[i].recv_frames++;
 				stat[i].recv_bits_payload += frame.len*8;
+				stat[i].recv_bits_dbitrate += can_frame_dbitrate_length(
+						&frame, mode, sizeof(frame));
 				stat[i].recv_bits_total += can_frame_length(&frame,
 									    mode, nbytes);
 			}
diff --git a/canframelen.c b/canframelen.c
index b708691..1a3584a 100644
--- a/canframelen.c
+++ b/canframelen.c
@@ -236,11 +236,39 @@ static unsigned cfl_exact(struct can_frame *frame)
 		3;		/* IFS */
 }
 
+unsigned can_frame_dbitrate_length(struct canfd_frame *frame, enum cfl_mode mode, int mtu)
+{
+	if (mtu != CANFD_MTU || !(frame->flags & CANFD_BRS))
+		return 0;
+	switch (mode) {
+	case CFL_NO_BITSTUFFING:
+		return 1 /* brs/crcdel */ + 1 /* esi */ + 4 /* dlc */
+			+ ((frame->len >= 16) ? 21 : 17)
+			+ frame->len*8;
+	case CFL_WORSTCASE:
+		return can_frame_dbitrate_length(frame, CFL_NO_BITSTUFFING, mtu) * 5 / 4;
+	default:
+		return 0;
+	}
+}
 
 unsigned can_frame_length(struct canfd_frame *frame, enum cfl_mode mode, int mtu)
 {
 	int eff = (frame->can_id & CAN_EFF_FLAG);
 
+	if (mtu == CANFD_MTU)
+		/* not correct, but close ? */
+		switch (mode) {
+		case CFL_NO_BITSTUFFING:
+			return 1+(eff ? 29 : 11) + ((frame->len >= 16) ? 21 : 17) +
+				+ 5 /* r1, ide, edl, r0, brs/crcdel, */ + 12 /* trail */
+				+ frame->len*8;
+		case CFL_WORSTCASE:
+			return can_frame_length(frame, CFL_NO_BITSTUFFING, mtu) * 5 / 4;
+		case CFL_EXACT:
+			return 0;	/* CANFD is not supported yet */
+		}
+	else
 	if (mtu != CAN_MTU)
 		return 0;	/* CANFD is not supported yet */
 
diff --git a/canframelen.h b/canframelen.h
index 641119d..47739b2 100644
--- a/canframelen.h
+++ b/canframelen.h
@@ -80,5 +80,6 @@ enum cfl_mode {
  * Mode determines how to deal with stuffed bits.
  */
 unsigned can_frame_length(struct canfd_frame *frame, enum cfl_mode mode, int mtu);
+unsigned can_frame_dbitrate_length(struct canfd_frame *frame, enum cfl_mode mode, int mtu);
 
 #endif
-- 
2.25.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] canbusload: count full payload for CANFD frames
  2021-06-24  3:21 [PATCH 1/2] canbusload: count full payload for CANFD frames Kurt Van Dijck
  2021-06-24  3:21 ` [PATCH 2/2] canbusload: count databitrate seperately Kurt Van Dijck
@ 2021-06-24  6:22 ` Marc Kleine-Budde
  1 sibling, 0 replies; 6+ messages in thread
From: Marc Kleine-Budde @ 2021-06-24  6:22 UTC (permalink / raw)
  To: Kurt Van Dijck; +Cc: linux-can

[-- Attachment #1: Type: text/plain, Size: 1884 bytes --]

On 24.06.2021 05:21:10, Kurt Van Dijck wrote:
> Signed-off-by: Kurt Van Dijck <dev.kurt@vandijck-laurijssen.be>
> ---
>  canbusload.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/canbusload.c b/canbusload.c
> index d6bfd78..bd74cb5 100644
> --- a/canbusload.c
> +++ b/canbusload.c
> @@ -237,7 +237,7 @@ int main(int argc, char **argv)
>  	int opt;
>  	char *ptr, *nptr;
>  	struct sockaddr_can addr;
> -	struct can_frame frame;
> +	struct canfd_frame frame;
>  	int nbytes, i;
>  	struct ifreq ifr;
>  	sigset_t sigmask, savesigmask;
> @@ -351,6 +351,9 @@ int main(int argc, char **argv)
>  #ifdef DEBUG
>  		printf("using interface name '%s'.\n", ifr.ifr_name);
>  #endif
> +		/* try to switch the socket into CAN FD mode */
> +		const int canfd_on = 1;
> +		setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
>  
>  		if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
>  			perror("SIOCGIFINDEX");
> @@ -402,9 +405,9 @@ int main(int argc, char **argv)
>  				}
>  
>  				stat[i].recv_frames++;
> -				stat[i].recv_bits_payload += frame.can_dlc*8;
> -				stat[i].recv_bits_total += can_frame_length((struct canfd_frame*)&frame,
> -									    mode, sizeof(frame));
> +				stat[i].recv_bits_payload += frame.len*8;

nitpick: Please add spaces around the "*", even if these are not in the
original code. Create a github pull request or send a v2 and I'll apply
the patches.

> +				stat[i].recv_bits_total += can_frame_length(&frame,
> +									    mode, nbytes);
>  			}
>  		}
>  	}

regard,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] canbusload: count databitrate seperately
  2021-06-24  3:21 ` [PATCH 2/2] canbusload: count databitrate seperately Kurt Van Dijck
@ 2021-06-24  6:25   ` Marc Kleine-Budde
  2021-06-24 10:48     ` Kurt Van Dijck
  0 siblings, 1 reply; 6+ messages in thread
From: Marc Kleine-Budde @ 2021-06-24  6:25 UTC (permalink / raw)
  To: Kurt Van Dijck; +Cc: linux-can

[-- Attachment #1: Type: text/plain, Size: 462 bytes --]

On 24.06.2021 05:21:11, Kurt Van Dijck wrote:
> Signed-off-by: Kurt Van Dijck <dev.kurt@vandijck-laurijssen.be>

For the whole file, please add the usual spaces around operators.

Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] canbusload: count databitrate seperately
  2021-06-24  6:25   ` Marc Kleine-Budde
@ 2021-06-24 10:48     ` Kurt Van Dijck
  2021-06-24 10:56       ` Marc Kleine-Budde
  0 siblings, 1 reply; 6+ messages in thread
From: Kurt Van Dijck @ 2021-06-24 10:48 UTC (permalink / raw)
  To: Marc Kleine-Budde; +Cc: linux-can

On Thu, 24 Jun 2021 08:25:51 +0200, Marc Kleine-Budde wrote:
> On 24.06.2021 05:21:11, Kurt Van Dijck wrote:
> > Signed-off-by: Kurt Van Dijck <dev.kurt@vandijck-laurijssen.be>
> 
> For the whole file, please add the usual spaces around operators.

only the ones I introduced, I suppose?

Kurt

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] canbusload: count databitrate seperately
  2021-06-24 10:48     ` Kurt Van Dijck
@ 2021-06-24 10:56       ` Marc Kleine-Budde
  0 siblings, 0 replies; 6+ messages in thread
From: Marc Kleine-Budde @ 2021-06-24 10:56 UTC (permalink / raw)
  To: Kurt Van Dijck; +Cc: linux-can

[-- Attachment #1: Type: text/plain, Size: 721 bytes --]

On 24.06.2021 12:48:40, Kurt Van Dijck wrote:
> On Thu, 24 Jun 2021 08:25:51 +0200, Marc Kleine-Budde wrote:
> > On 24.06.2021 05:21:11, Kurt Van Dijck wrote:
> > > Signed-off-by: Kurt Van Dijck <dev.kurt@vandijck-laurijssen.be>
> > 
> > For the whole file, please add the usual spaces around operators.
> 
> only the ones I introduced, I suppose?

Yes, don't introduce new style problem and in every line you touch also
fix them.

Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-06-24 10:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-24  3:21 [PATCH 1/2] canbusload: count full payload for CANFD frames Kurt Van Dijck
2021-06-24  3:21 ` [PATCH 2/2] canbusload: count databitrate seperately Kurt Van Dijck
2021-06-24  6:25   ` Marc Kleine-Budde
2021-06-24 10:48     ` Kurt Van Dijck
2021-06-24 10:56       ` Marc Kleine-Budde
2021-06-24  6:22 ` [PATCH 1/2] canbusload: count full payload for CANFD frames Marc Kleine-Budde

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.