From: Sebastien GODARD Date: Sun, 27 Oct 2013 14:10:12 +0000 (+0100) Subject: Rework procedures used to count processors on the machine X-Git-Tag: v10.2.0~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d26942b710bfc411697ae08efa474aa8cc644ef9;p=sysstat Rework procedures used to count processors on the machine Rework patch from troosh: No need to duplicate code used to count the number of processors available on the machine. Use a flag (@highest) instead. Signed-off-by: Sebastien GODARD --- diff --git a/cifsiostat.c b/cifsiostat.c index f898420..44cd4be 100644 --- a/cifsiostat.c +++ b/cifsiostat.c @@ -169,7 +169,7 @@ void io_sys_init(void) int i; /* How many processors on this machine? */ - cpu_nr = get_cpu_nr(~0); + cpu_nr = get_cpu_nr(~0, FALSE); /* Get number of CIFS directories in /proc/fs/cifs/Stats */ if ((cifs_nr = get_cifs_nr()) > 0) { diff --git a/count.c b/count.c index 14943b0..116ca99 100644 --- a/count.c +++ b/count.c @@ -44,15 +44,20 @@ /* *************************************************************************** - * Find maximum number of processor in /sys plus one. + * Count number of processors in /sys. * + * IN: + * @highest If set to TRUE, then look for the highest processor number. + * This is used when eg. the machine has 4 CPU numbered 0, 1, 4 + * and 5. In this case, this procedure will return 6. + * * RETURNS: * Number of processors (online and offline). * A value of 0 means that /sys was not mounted. * A value of N (!=0) means N processor(s) (cpu0 .. cpu(N-1)). *************************************************************************** */ -int get_sys_cpu_nr(void) +int get_sys_cpu_nr(int highest) { DIR *dir; struct dirent *drd; @@ -66,15 +71,21 @@ int get_sys_cpu_nr(void) /* Get current file entry */ while ((drd = readdir(dir)) != NULL) { + if (!strncmp(drd->d_name, "cpu", 3) && isdigit(drd->d_name[3])) { snprintf(line, MAX_PF_NAME, "%s/%s", SYSFS_DEVCPU, drd->d_name); line[MAX_PF_NAME - 1] = '\0'; if (stat(line, &buf) < 0) continue; if (S_ISDIR(buf.st_mode)) { - sscanf(&drd->d_name[3], "%d", &num_proc); - if (num_proc > proc_nr) { - proc_nr = num_proc; + if (highest) { + sscanf(drd->d_name + 3, "%d", &num_proc); + if (num_proc > proc_nr) { + proc_nr = num_proc; + } + } + else { + proc_nr++; } } } @@ -88,7 +99,7 @@ int get_sys_cpu_nr(void) /* *************************************************************************** - * Find maximun number of processor in /proc/stat plus one. + * Count number of processors in /proc/stat. * * RETURNS: * Number of processors. The returned value is greater than or equal to the @@ -125,11 +136,15 @@ int get_proc_cpu_nr(void) /* *************************************************************************** - * Find the maximun number of processor on the machine plus one. + * Count the number of processors on the machine, or look for the + * highest processor number. * Try to use /sys for that, or /proc/stat if /sys doesn't exist. * * IN: * @max_nr_cpus Maximum number of proc that sysstat can handle. + * @highest If set to TRUE, then look for the highest processor number. + * This is used when eg. the machine has 4 CPU numbered 0, 1, 4 + * and 5. In this case, this procedure will return 6. * * RETURNS: * Number of processors. @@ -140,11 +155,11 @@ int get_proc_cpu_nr(void) * 2: two proc... *************************************************************************** */ -int get_cpu_nr(unsigned int max_nr_cpus) +int get_cpu_nr(unsigned int max_nr_cpus, int highest) { int cpu_nr; - if ((cpu_nr = get_sys_cpu_nr()) == 0) { + if ((cpu_nr = get_sys_cpu_nr(highest)) == 0) { /* /sys may be not mounted. Use /proc/stat instead */ cpu_nr = get_proc_cpu_nr(); } @@ -157,107 +172,6 @@ int get_cpu_nr(unsigned int max_nr_cpus) return cpu_nr; } -/* - *************************************************************************** - * Count total number of processors in /sys. - * - * RETURNS: - * Number of processors (online and offline). - * A value of 0 means that /sys was not mounted. - * A value of N (!=0) means N processor(s) (cpu0 .. cpu(N-1)). - *************************************************************************** - */ -int get_sys_cpu_total_nr(void) -{ - DIR *dir; - struct dirent *drd; - struct stat buf; - char line[MAX_PF_NAME]; - int proc_nr = 0; - - /* Open relevant /sys directory */ - if ((dir = opendir(SYSFS_DEVCPU)) == NULL) - return 0; - - /* Get current file entry */ - while ((drd = readdir(dir)) != NULL) { - if (!strncmp(drd->d_name, "cpu", 3) && isdigit(drd->d_name[3])) { - snprintf(line, MAX_PF_NAME, "%s/%s", SYSFS_DEVCPU, drd->d_name); - line[MAX_PF_NAME - 1] = '\0'; - if (stat(line, &buf) < 0) - continue; - if (S_ISDIR(buf.st_mode)) { - ++proc_nr; - } - } - } - - /* Close directory */ - closedir(dir); - - return proc_nr; -} - -/* - *************************************************************************** - * Count total number of processors in /proc/stat. - * - * RETURNS: - * Number of processors. The returned value is greater than or equal to the - * number of online processors. - * A value of 0 means one processor and non SMP kernel. - * A value of N (!=0) means N processor(s) (0 .. N-1) with SMP kernel. - *************************************************************************** - */ -int get_proc_cpu_total_nr(void) -{ - FILE *fp; - char line[16]; - int proc_nr = 0; - - if ((fp = fopen(STAT, "r")) == NULL) { - fprintf(stderr, _("Cannot open %s: %s\n"), STAT, strerror(errno)); - exit(1); - } - - while (fgets(line, 16, fp) != NULL) { - - if (strncmp(line, "cpu ", 4) && !strncmp(line, "cpu", 3)) { - ++proc_nr; - } - } - - fclose(fp); - - return proc_nr; -} - -/* - *************************************************************************** - * Count the total number of processors on the machine. - * Try to use /sys for that, or /proc/stat if /sys doesn't exist. - * - * RETURNS: - * Number of processors. - * 0: one proc and non SMP kernel. - * 1: one proc and SMP kernel (NB: On SMP machines where all the CPUs but - * one have been disabled, we get the total number of proc since we use - * /sys to count them). - * 2: two proc... - *************************************************************************** - */ -int get_cpu_total_nr(void) -{ - int cpu_nr; - - if ((cpu_nr = get_sys_cpu_total_nr()) == 0) { - /* /sys may be not mounted. Use /proc/stat instead */ - cpu_nr = get_proc_cpu_total_nr(); - } - - return cpu_nr; -} - /* *************************************************************************** * Find number of interrupts available per processor (use diff --git a/count.h b/count.h index 611138f..79475ee 100644 --- a/count.h +++ b/count.h @@ -16,14 +16,11 @@ */ extern int - get_cpu_nr(unsigned int); -extern int - get_cpu_total_nr(void); + get_cpu_nr(unsigned int, int); extern int get_irqcpu_nr(char *, int, int); extern int get_diskstats_dev_nr(int, int); - extern int get_irq_nr(void); extern int diff --git a/iostat.c b/iostat.c index be24993..9f0094c 100644 --- a/iostat.c +++ b/iostat.c @@ -303,7 +303,7 @@ void io_sys_init(void) init_stats(); /* How many processors on this machine? */ - cpu_nr = get_cpu_nr(~0); + cpu_nr = get_cpu_nr(~0, FALSE); /* Get number of block devices and partitions in /proc/diskstats */ if ((iodev_nr = get_diskstats_dev_nr(CNT_PART, CNT_ALL_DEV)) > 0) { diff --git a/mpstat.c b/mpstat.c index b812130..ce0e82e 100644 --- a/mpstat.c +++ b/mpstat.c @@ -901,8 +901,8 @@ int main(int argc, char **argv) /* Get HZ */ get_HZ(); - /* How many processors on this machine ? */ - cpu_nr = get_cpu_nr(~0); + /* What is the highest processor number on this machine? */ + cpu_nr = get_cpu_nr(~0, TRUE); /* Calculate number of interrupts per processor */ irqcpu_nr = get_irqcpu_nr(INTERRUPTS, NR_IRQS, cpu_nr) + @@ -1075,7 +1075,7 @@ int main(int argc, char **argv) /* Get system name, release number and hostname */ uname(&header); print_gal_header(&(mp_tstamp[0]), header.sysname, header.release, - header.nodename, header.machine, get_cpu_total_nr()); + header.nodename, header.machine, get_cpu_nr(~0, FALSE)); /* Main loop */ rw_mpstat_loop(dis_hdr, rows); diff --git a/nfsiostat.c b/nfsiostat.c index 7c219b4..e94df90 100644 --- a/nfsiostat.c +++ b/nfsiostat.c @@ -193,7 +193,7 @@ void io_sys_init(void) int i; /* How many processors on this machine? */ - cpu_nr = get_cpu_nr(~0); + cpu_nr = get_cpu_nr(~0, FALSE); /* Get number of NFS directories in /proc/self/mountstats */ if ((ionfs_nr = get_nfs_mount_nr()) > 0) { diff --git a/pidstat.c b/pidstat.c index fb8157f..1e5f09e 100644 --- a/pidstat.c +++ b/pidstat.c @@ -795,7 +795,7 @@ void pid_sys_init(void) init_stats(); /* Count nb of proc */ - cpu_nr = get_cpu_nr(~0); + cpu_nr = get_cpu_nr(~0, FALSE); if (DISPLAY_ALL_PID(pidflag)) { /* Count PIDs and allocate structures */ diff --git a/sa_wrap.c b/sa_wrap.c index 4954b3f..7c359d2 100644 --- a/sa_wrap.c +++ b/sa_wrap.c @@ -962,7 +962,7 @@ __nr_t wrap_get_iface_nr(struct activity *a) */ __nr_t wrap_get_cpu_nr(struct activity *a) { - return (get_cpu_nr(a->bitmap->b_size) + 1); + return (get_cpu_nr(a->bitmap->b_size, FALSE) + 1); } /*