Skip to contentSkip to conversation

can: add missing initialization of CAN statistics

PATCH In review Oliver Hartkopp 2 participants 4 messages Jul 6, 2016

Subsystems (2)
lore
Share & tools
lore

Oliver Hartkopp

author maintainer
#0 lore raw

PATCH 1/1 can: add missing initialization of CAN statistics

The total rx/tx rate (TXR and RXR) only start working after a statistic reset. This patch initializes the statistic data structures and the timer init value to provide proper values directly from the start.

---
 net/can/af_can.c |  1 +
 net/can/af_can.h |  1 +
 net/can/proc.c   | 21 ++++++++++++++-------
 3 files changed, 16 insertions(+), 7 deletions(-)
net/can/af_can.c +1 -0
index 1108079..cefe98c 100644
@@ -912,6 +912,7 @@ static __init int can_init(void)
 		return -ENOMEM;
 
 	if (IS_ENABLED(CONFIG_PROC_FS)) {
+		can_init_stats();
 		if (stats_timer) {
 		/* the statistics are updated every second (timer triggered) */
 			setup_timer(&can_stattimer, can_stat_update, 0);
net/can/af_can.h +1 -0
index fca0fe9..8361047 100644
@@ -115,6 +115,7 @@ extern struct dev_rcv_lists can_rx_alldev_list;
 /* function prototypes for the CAN networklayer procfs (proc.c) */
 void can_init_proc(void);
 void can_remove_proc(void);
+void can_init_stats(void);
 void can_stat_update(unsigned long data);
 
 /* structures and variables from af_can.c needed in proc.c for reading */
net/can/proc.c +14 -7
index 85ef7bb..056e9f5 100644
@@ -86,7 +86,7 @@ static const char rx_list_name[][8] = {
  * af_can statistics stuff
  */
 
-static void can_init_stats(void)
+static void can_reset_stats(void)
 {
 	/*
 	 * This memset function is called from a timer context (when
@@ -104,6 +104,13 @@ static void can_init_stats(void)
 	}
 }
 
+void can_init_stats(void)
+{
+	memset(&can_pstats, 0, sizeof(can_pstats));
+	memset(&can_stats, 0, sizeof(can_stats));
+	can_stats.jiffies_init = jiffies;
+}
+
 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
 			       unsigned long count)
 {
@@ -130,23 +137,23 @@ void can_stat_update(unsigned long data)
 
 	/* restart counting in timer context on user request */
 	if (user_reset)
-		can_init_stats();
+		can_reset_stats();
 
 	/* restart counting on jiffies overflow */
 	if (j < can_stats.jiffies_init)
-		can_init_stats();
+		can_reset_stats();
 
 	/* prevent overflow in calc_rate() */
 	if (can_stats.rx_frames > (ULONG_MAX / HZ))
-		can_init_stats();
+		can_reset_stats();
 
 	/* prevent overflow in calc_rate() */
 	if (can_stats.tx_frames > (ULONG_MAX / HZ))
-		can_init_stats();
+		can_reset_stats();
 
 	/* matches overflow - very improbable */
 	if (can_stats.matches > (ULONG_MAX / 100))
-		can_init_stats();
+		can_reset_stats();
 
 	/* calc total values */
 	if (can_stats.rx_frames)
@@ -296,7 +303,7 @@ static int can_reset_stats_proc_show(struct seq_file *m, void *v)
 
 	} else {
 		if (can_stats.jiffies_init != jiffies)
-			can_init_stats();
+			can_reset_stats();
 
 		seq_printf(m, "Performed statistic reset #%ld.\n",
 				can_pstats.stats_reset);
Signature · 1 line
2.8.1
#1 lore raw
Quoted 49 lines · Oliver Hartkopp

The total rx/tx rate (TXR and RXR) only start working after a statistic reset. This patch initializes the statistic data structures and the timer init value to provide proper values directly from the start.

---
 net/can/af_can.c |  1 +
 net/can/af_can.h |  1 +
 net/can/proc.c   | 21 ++++++++++++++-------
 3 files changed, 16 insertions(+), 7 deletions(-)
net/can/af_can.c +1 -0
index 1108079..cefe98c 100644
@@ -912,6 +912,7 @@ static __init int can_init(void)
 		return -ENOMEM;
 
 	if (IS_ENABLED(CONFIG_PROC_FS)) {
+		can_init_stats();
 		if (stats_timer) {
 		/* the statistics are updated every second (timer triggered) */
 			setup_timer(&can_stattimer, can_stat_update, 0);
net/can/af_can.h +1 -0
index fca0fe9..8361047 100644
@@ -115,6 +115,7 @@ extern struct dev_rcv_lists can_rx_alldev_list;
 /* function prototypes for the CAN networklayer procfs (proc.c) */
 void can_init_proc(void);
 void can_remove_proc(void);
+void can_init_stats(void);
 void can_stat_update(unsigned long data);
 
 /* structures and variables from af_can.c needed in proc.c for reading */
net/can/proc.c +1 -1
index 85ef7bb..056e9f5 100644
@@ -86,7 +86,7 @@ static const char rx_list_name[][8] = {
  * af_can statistics stuff
  */
 
-static void can_init_stats(void)
+static void can_reset_stats(void)
 {
 	/*
 	 * This memset function is called from a timer context (when

net/can/proc.c static void can_init_stats(void) · line 110

Quoted 7 lines
net/can/proc.c +4 -0
@@ -104,6 +104,13 @@ static void can_init_stats(void)
 	}
 }
 
+void can_init_stats(void)
+{
+	memset(&can_pstats, 0, sizeof(can_pstats));
+	memset(&can_stats, 0, sizeof(can_stats));

I think this is not needed, as both are global variables, right?

net/can/proc.c static void can_init_stats(void) · line 113

Quoted 3 lines
net/can/proc.c +3 -0
+	can_stats.jiffies_init = jiffies;
+}
+

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
author maintainer
#2 lore raw
Quoted 9 lines · Marc Kleine-Budde

net/can/proc.c static void can_init_stats(void) · line 110

Quoted 4 lines · Oliver Hartkopp
net/can/proc.c +4 -0
+void can_init_stats(void)
+{
+	memset(&can_pstats, 0, sizeof(can_pstats));
+	memset(&can_stats, 0, sizeof(can_stats));

I think this is not needed, as both are global variables, right?

Yes they are.

Is it assured that global variables are initialized with zero like static variables?

If so I can provide a far smaller patch :-)

Oliver

#3 lore raw
Quoted 13 lines · Oliver Hartkopp
Quoted 9 lines · Marc Kleine-Budde

net/can/proc.c static void can_init_stats(void) · line 110

Quoted 4 lines · Oliver Hartkopp
net/can/proc.c +4 -0
+void can_init_stats(void)
+{
+	memset(&can_pstats, 0, sizeof(can_pstats));
+	memset(&can_stats, 0, sizeof(can_stats));

I think this is not needed, as both are global variables, right?

Quoted 4 lines

Yes they are.

Is it assured that global variables are initialized with zero like static variables?

Yes.

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |