]> granicus.if.org Git - sysstat/commitdiff
Rework procedures used to count processors on the machine
authorSebastien GODARD <sysstat@orange.fr.fake>
Sun, 27 Oct 2013 14:10:12 +0000 (15:10 +0100)
committerSebastien GODARD <sysstat@orange.fr.fake>
Sun, 27 Oct 2013 14:10:12 +0000 (15:10 +0100)
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 <sysstat@orange.fr.fake>
cifsiostat.c
count.c
count.h
iostat.c
mpstat.c
nfsiostat.c
pidstat.c
sa_wrap.c

index f898420a76c4d532d68ca57a2ca7007f4a6ba884..44cd4be5d2fa8e0c6b3fffd8baa888dbca1ff6aa 100644 (file)
@@ -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 14943b0b22e005984f5d4c0d5a4d6c0f5a534c3c..116ca996ab60fd3d436c1c1202cb2b3a917333a8 100644 (file)
--- a/count.c
+++ b/count.c
 
 /*
  ***************************************************************************
- * 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 611138fcf9d711b2ba47faff88b6dcc0a06ec613..79475ee79847e166fe0b5901352996dec75af8d5 100644 (file)
--- a/count.h
+++ b/count.h
  */
 
 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
index be249933b9633ae22fb1a49290725d87e8aa1c06..9f0094ca5d072c8f3dd9fdcdff980ce44687e9a9 100644 (file)
--- 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) {
index b8121302e61cb21d9cf3f04abf4b9b86394ae197..ce0e82e03acfd801c6984cc5bed4f85077e0512b 100644 (file)
--- 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);
index 7c219b46d8ebc86b0d8ced5f7aabd51a7cd4c040..e94df902ebe72394006f740fde91b52bc288a6cd 100644 (file)
@@ -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) {
index fb8157f2e0599355eecdc4068fa55a76f0dd0655..1e5f09e448f813171ae8ae90ed94b35acbcca047 100644 (file)
--- 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 */
index 4954b3fd6cda11244eace96f8e53f5cabef5eecd..7c359d26a3f36b159539dd7479682a58b3c89729 100644 (file)
--- 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);
 }
 
 /*