]> granicus.if.org Git - sysstat/commitdiff
sadc: Rework softnet stats reading procedure
authorSebastien GODARD <sysstat@users.noreply.github.com>
Sat, 26 Sep 2020 14:07:02 +0000 (16:07 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Sat, 26 Sep 2020 14:07:02 +0000 (16:07 +0200)
Make sure that all lines from /proc/net/softnet_stat are read.
If not enough slots have been allocated then allocate more of them.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
rd_stats.c
sa_wrap.c

index fb93f23fed12bcef96f8e731ea915a749f34c773..bf7b4ce2dbf88a30c53ecf663599d4b19773d932 100644 (file)
@@ -2724,6 +2724,7 @@ __nr_t read_fchost(struct stats_fchost *st_fc, __nr_t nr_alloc)
  *
  * RETURNS:
  * 1 if stats have been sucessfully read, or 0 otherwise.
+ * Returns -1 if the buffer was too small and needs to be reallocated.
  ***************************************************************************
  */
 int read_softnet(struct stats_softnet *st_softnet, __nr_t nr_alloc,
@@ -2732,21 +2733,27 @@ int read_softnet(struct stats_softnet *st_softnet, __nr_t nr_alloc,
        FILE *fp;
        struct stats_softnet *st_softnet_i;
        char line[1024];
-       int cpu;
+       int cpu = 1, rc = 1;
 
        /* Open /proc/net/softnet_stat file */
        if ((fp = fopen(NET_SOFTNET, "r")) == NULL)
                return 0;
 
-       for (cpu = 1; cpu < nr_alloc; cpu++) {
-               if (!(online_cpu_bitmap[(cpu - 1) >> 3] & (1 << ((cpu - 1) & 0x07))))
-                       /* CPU is offline */
-                       continue;
+       while (fgets(line, sizeof(line), fp) != NULL) {
+
+               while ((!(online_cpu_bitmap[(cpu - 1) >> 3] & (1 << ((cpu - 1) & 0x07)))) && (cpu <= NR_CPUS + 1)) {
+                       cpu++;
+               }
+               if (cpu > NR_CPUS + 1)
+                       /* Should never happen */
+                       return 0;
 
-               if (fgets(line, sizeof(line), fp) == NULL)
+               if (cpu + 1 > nr_alloc) {
+                       rc = -1;
                        break;
+               }
 
-               st_softnet_i = st_softnet + cpu;
+               st_softnet_i = st_softnet + cpu++;
                sscanf(line, "%x %x %x %*x %*x %*x %*x %*x %*x %x %x",
                       &st_softnet_i->processed,
                       &st_softnet_i->dropped,
@@ -2756,7 +2763,7 @@ int read_softnet(struct stats_softnet *st_softnet, __nr_t nr_alloc,
        }
 
        fclose(fp);
-       return 1;
+       return rc;
 }
 
 /*
index fa78add379b589f7b6fcb9edf5709b86abaa43fb..bfc2a81827ec8d82c16d70ad01686d506357db31 100644 (file)
--- a/sa_wrap.c
+++ b/sa_wrap.c
@@ -1070,7 +1070,7 @@ __read_funct_t wrap_read_fchost(struct activity *a)
  *             CPU bitmap which has been filled.
  *
  * RETURNS:
- * Number of CPU for which statistics have to be be read.
+ * Number of CPU for which statistics have to be read.
  * 1 means CPU "all", 2 means CPU "all" and CPU 0, etc.
  * Or -1 if the buffer was too small and needs to be reallocated.
  ***************************************************************************
@@ -1139,19 +1139,16 @@ __read_funct_t wrap_read_softnet(struct activity *a)
 
                /* Get online CPU list */
                nr_read = get_online_cpu_list(online_cpu_bitmap, bitmap_size);
-               if (!nr_read)
-                       break;
+
+               if (nr_read > 0) {
+                       /* Read /proc/net/softnet stats */
+                       nr_read *= read_softnet(st_softnet, a->nr_allocated, online_cpu_bitmap);
+               }
 
                if (nr_read < 0) {
                        /* Buffer needs to be reallocated */
                        st_softnet = (struct stats_softnet *) reallocate_buffer(a);
                }
-               else {
-                       if (!read_softnet(st_softnet, a->nr_allocated, online_cpu_bitmap)) {
-                               /* File /proc/net/softnet doesn't exist */
-                               nr_read = 0;
-                       }
-               }
        }
        while (nr_read < 0);