]> granicus.if.org Git - sysstat/commitdiff
No longer use /proc/stat to calculate system uptime
authorSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 13 Oct 2017 07:29:19 +0000 (09:29 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 13 Oct 2017 07:29:19 +0000 (09:29 +0200)
sysstat commands used the /proc/stat file to compute system uptime based
on the number of jiffies spent by CPU#0 (which cannot be set offline).
Now only use the /proc/uptime file which we assume always exists on a
Linux machine.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
cifsiostat.c
iostat.c
mpstat.c
pidstat.c
rd_stats.c
rd_stats.h
sa_wrap.c
sadc.c

index 02c5d06a2bae39d4d154ea565145734bf508c7c6..3bbad25b222a226e9525f862128e9cc71e35b44f 100644 (file)
@@ -544,10 +544,9 @@ void rw_io_stat_loop(long int count, struct tm *rectime)
 
        do {
                /* Read system uptime (reduced to one processor) */
-               uptime0[curr] = 0;
                read_uptime(&(uptime0[curr]));
                if (!uptime0[curr])
-                       /* Cannot read system uptime (/proc/uptime doesn't exist) */
+                       /* Cannot read system uptime */
                        exit(2);
 
                /* Read CIFS stats */
index 458584b11f6cfef62f9b94449820ae7015fc448d..8425830d0e19e3da7316675400d6f03d957a94d4 100644 (file)
--- a/iostat.c
+++ b/iostat.c
@@ -1620,21 +1620,12 @@ void rw_io_stat_loop(long int count, struct tm *rectime)
 
        do {
                if (cpu_nr > 1) {
-                       /*
-                        * Read system uptime (only for SMP machines).
-                        * Init uptime0. So if /proc/uptime cannot fill it,
-                        * this will be done by /proc/stat.
-                        */
-                       uptime0[curr] = 0;
+                       /* Read system uptime (only for SMP machines) */
                        read_uptime(&(uptime0[curr]));
                }
 
-               /*
-                * Read stats for CPU "all" and 0.
-                * Note that stats for CPU 0 are not used per se. It only makes
-                * read_stat_cpu() fill uptime0.
-                */
-               read_stat_cpu(st_cpu[curr], 2, &(uptime[curr]), &(uptime0[curr]));
+               /* Read stats for CPU "all" and system uptime in jiffies */
+               read_stat_cpu(st_cpu[curr], 1, &(uptime[curr]));
 
                if (dlist_idx) {
                        /*
index 2ab67c65a90e58866d4a318641a0cb483cc85e27..8485f9b20827faef6d53c271007e226db8b061ca 100644 (file)
--- a/mpstat.c
+++ b/mpstat.c
@@ -1832,14 +1832,10 @@ void rw_mpstat_loop(int dis_hdr, int rows)
 
        /* Read uptime and CPU stats */
        if (cpu_nr > 1) {
-               /*
-                * Init uptime0. So if /proc/uptime cannot fill it,
-                * this will be done by /proc/stat.
-                */
-               uptime0[0] = 0;
+               /* Read system uptime (only for SMP machines) */
                read_uptime(&(uptime0[0]));
        }
-       read_stat_cpu(st_cpu[0], cpu_nr + 1, &(uptime[0]), &(uptime0[0]));
+       read_stat_cpu(st_cpu[0], cpu_nr + 1, &(uptime[0]));
        if (DISPLAY_NODE(actflags)) {
                set_node_cpu_stats(st_node[0], st_cpu[0]);
        }
@@ -1932,10 +1928,9 @@ void rw_mpstat_loop(int dis_hdr, int rows)
 
                /* Read uptime and CPU stats */
                if (cpu_nr > 1) {
-                       uptime0[curr] = 0;
                        read_uptime(&(uptime0[curr]));
                }
-               read_stat_cpu(st_cpu[curr], cpu_nr + 1, &(uptime[curr]), &(uptime0[curr]));
+               read_stat_cpu(st_cpu[curr], cpu_nr + 1, &(uptime[curr]));
                if (DISPLAY_NODE(actflags)) {
                        set_node_cpu_stats(st_node[curr], st_cpu[curr]);
                }
index 3029897e1f3dbf67c15a93354969e74760fa0b39..6d419d76ed85eb32d9c7eb354e2e593489c4672e 100644 (file)
--- a/pidstat.c
+++ b/pidstat.c
@@ -1007,8 +1007,8 @@ void read_stats(int curr)
                perror("malloc");
                exit(4);
        }
-       /* Read statistics for CPUs "all" and 0 */
-       read_stat_cpu(st_cpu, 2, &uptime[curr], &uptime0[curr]);
+       /* Read statistics for CPUs "all" and system uptime (in jiffies) */
+       read_stat_cpu(st_cpu, 1, &uptime[curr]);
        free(st_cpu);
 
        if (DISPLAY_ALL_PID(pidflag)) {
@@ -2477,12 +2477,7 @@ void rw_pidstat_loop(int dis_hdr, int rows)
        setbuf(stdout, NULL);
 
        if (cpu_nr > 1) {
-               /*
-                * Read system uptime (only for SMP machines).
-                * Init uptime0. So if /proc/uptime cannot fill it, this will be
-                * done by /proc/stat.
-                */
-               uptime0[0] = 0;
+               /* Read system uptime (only for SMP machines) */
                read_uptime(&uptime0[0]);
        }
        read_stats(0);
@@ -2529,12 +2524,7 @@ void rw_pidstat_loop(int dis_hdr, int rows)
                get_localtime(&ps_tstamp[curr], 0);
 
                if (cpu_nr > 1) {
-                       /*
-                        * Read system uptime (only for SMP machines).
-                        * Init uptime0. So if /proc/uptime cannot fill it, this will be
-                        * done by /proc/stat.
-                        */
-                       uptime0[curr] = 0;
+                       /* Read system uptime (only for SMP machines) */
                        read_uptime(&(uptime0[curr]));
                }
 
index 0788b5539e62176fb8a38d9670fd4d8fdb0435e8..a397ea85d186f44760e33178de7c7c2babcef40e 100644 (file)
  * OUT:
  * @st_cpu     Structure with statistics.
  * @uptime     Machine uptime multiplied by the number of processors.
- * @uptime0    Machine uptime. Filled only if previously set to zero.
  ***************************************************************************
  */
-void read_stat_cpu(struct stats_cpu *st_cpu, int nbr,
-                  unsigned long long *uptime, unsigned long long *uptime0)
+void read_stat_cpu(struct stats_cpu *st_cpu, int nbr, unsigned long long *uptime)
 {
        FILE *fp;
        struct stats_cpu *st_cpu_i;
@@ -141,20 +139,6 @@ void read_stat_cpu(struct stats_cpu *st_cpu, int nbr,
                                 * else additional CPUs have been dynamically registered
                                 * in /proc/stat.
                                 */
-
-                               if (!proc_nb && !*uptime0) {
-                                       /*
-                                        * Compute uptime reduced to one proc using proc#0.
-                                        * Done if /proc/uptime was unavailable.
-                                        *
-                                        * NB: Don't add cpu_guest/cpu_guest_nice because cpu_user/cpu_nice
-                                        * already include them.
-                                        */
-                                       *uptime0 = sc.cpu_user + sc.cpu_nice  +
-                                               sc.cpu_sys     + sc.cpu_idle  +
-                                               sc.cpu_iowait  + sc.cpu_steal +
-                                               sc.cpu_hardirq + sc.cpu_softirq;
-                               }
                        }
                }
        }
@@ -311,20 +295,23 @@ void read_uptime(unsigned long long *uptime)
        char line[128];
        unsigned long up_sec, up_cent;
 
-       if ((fp = fopen(UPTIME, "r")) == NULL)
-               return;
-
-       if (fgets(line, sizeof(line), fp) == NULL) {
-               fclose(fp);
+       if ((fp = fopen(UPTIME, "r")) == NULL) {
+               fprintf(stderr, _("Cannot open %s: %s\n"), UPTIME, strerror(errno));
+               exit(2);
                return;
        }
 
-       sscanf(line, "%lu.%lu", &up_sec, &up_cent);
-       *uptime = (unsigned long long) up_sec * HZ +
-                 (unsigned long long) up_cent * HZ / 100;
+       if (fgets(line, sizeof(line), fp) != NULL) {
+               sscanf(line, "%lu.%lu", &up_sec, &up_cent);
+               *uptime = (unsigned long long) up_sec * HZ +
+                         (unsigned long long) up_cent * HZ / 100;
+       }
+       else {
+               /* Couldn't read system uptime */
+               *uptime = 0;
+       }
 
        fclose(fp);
-
 }
 
 #ifdef SOURCE_SADC
index 69a9f9065be0ce1e86f4c19cf2b49a1c7ab254bb..f2b6a383e5bb31f36e82d213f45192302fde735b 100644 (file)
@@ -595,7 +595,7 @@ struct stats_softnet {
 void oct2chr
        (char *);
 void read_stat_cpu
-       (struct stats_cpu *, int, unsigned long long *, unsigned long long *);
+       (struct stats_cpu *, int, unsigned long long *);
 void read_stat_irq
        (struct stats_irq *, int);
 void read_meminfo
index f6ed4eb824b92df35e78112e0290eb0d58c53fe7..12f99ca304b59332ad5e54103269bfe81a024866 100644 (file)
--- a/sa_wrap.c
+++ b/sa_wrap.c
@@ -47,7 +47,7 @@ __read_funct_t wrap_read_stat_cpu(struct activity *a)
                = (struct stats_cpu *) a->_buf0;
 
        /* Read CPU statistics */
-       read_stat_cpu(st_cpu, a->nr, &record_hdr.uptime, &record_hdr.uptime0);
+       read_stat_cpu(st_cpu, a->nr, &record_hdr.uptime);
 
        return;
 }
diff --git a/sadc.c b/sadc.c
index d28575acc5817aeeb7828c17896ea401e47b2a57..c4eb4eaffb4b17fe93b8a6c922b42212ee4a9834 100644 (file)
--- a/sadc.c
+++ b/sadc.c
@@ -1067,18 +1067,8 @@ void read_stats(void)
        int i;
        __nr_t cpu_nr = act[get_activity_position(act, A_CPU, EXIT_IF_NOT_FOUND)]->nr;
 
-       /*
-        * Init uptime0. So if /proc/uptime cannot fill it,
-        * this will be done by /proc/stat.
-        * If cpu_nr = 2, force /proc/stat to fill it.
-        * If cpu_nr = 1, uptime0 and uptime are equal.
-        * NB: uptime0 is always filled.
-        * Remember that cpu_nr = 1 means one CPU and no SMP kernel
-        * (one structure for CPU "all") and cpu_nr = 2 means one CPU
-        * and an SMP kernel (two structures for CPUs "all" and "0").
-        */
-       record_hdr.uptime0 = 0;
-       if (cpu_nr > 2) {
+       if (cpu_nr > 1) {
+               /* Read system uptime in jiffies */
                read_uptime(&(record_hdr.uptime0));
        }
 
@@ -1091,8 +1081,10 @@ void read_stats(void)
 
        if (cpu_nr == 1) {
                /*
-                * uptime has been filled by read_uptime()
-                * or when reading CPU stats from /proc/stat.
+                * If cpu_nr = 1, uptime0 and uptime are equal.
+                * Remember that cpu_nr = 1 means one CPU and no SMP kernel
+                * (one structure for CPU "all") and cpu_nr = 2 means one CPU
+                * and an SMP kernel (two structures for CPUs "all" and "0").
                 */
                record_hdr.uptime0 = record_hdr.uptime;
        }