From: Sebastien GODARD Date: Fri, 13 Oct 2017 07:29:19 +0000 (+0200) Subject: No longer use /proc/stat to calculate system uptime X-Git-Tag: v11.7.1~64 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7c1b7c22dda2a637c6782834a075c896749fe18a;p=sysstat No longer use /proc/stat to calculate system uptime 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 --- diff --git a/cifsiostat.c b/cifsiostat.c index 02c5d06..3bbad25 100644 --- a/cifsiostat.c +++ b/cifsiostat.c @@ -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 */ diff --git a/iostat.c b/iostat.c index 458584b..8425830 100644 --- 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) { /* diff --git a/mpstat.c b/mpstat.c index 2ab67c6..8485f9b 100644 --- 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]); } diff --git a/pidstat.c b/pidstat.c index 3029897..6d419d7 100644 --- 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])); } diff --git a/rd_stats.c b/rd_stats.c index 0788b55..a397ea8 100644 --- a/rd_stats.c +++ b/rd_stats.c @@ -53,11 +53,9 @@ * 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 diff --git a/rd_stats.h b/rd_stats.h index 69a9f90..f2b6a38 100644 --- a/rd_stats.h +++ b/rd_stats.h @@ -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 diff --git a/sa_wrap.c b/sa_wrap.c index f6ed4eb..12f99ca 100644 --- 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 d28575a..c4eb4ea 100644 --- 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; }