From e90c834eea0a9d330ca38c1a39c508e7b7bca88c Mon Sep 17 00:00:00 2001 From: Alexander Troosh Date: Wed, 9 Oct 2013 18:57:11 +0400 Subject: [PATCH] Print total number of CPUs in header --- count.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- count.h | 2 ++ mpstat.c | 2 +- 3 files changed, 107 insertions(+), 4 deletions(-) diff --git a/count.c b/count.c index a7604a4..14943b0 100644 --- a/count.c +++ b/count.c @@ -44,7 +44,7 @@ /* *************************************************************************** - * Count number of processors in /sys. + * Find maximum number of processor in /sys plus one. * * RETURNS: * Number of processors (online and offline). @@ -88,7 +88,7 @@ int get_sys_cpu_nr(void) /* *************************************************************************** - * Count number of processors in /proc/stat. + * Find maximun number of processor in /proc/stat plus one. * * RETURNS: * Number of processors. The returned value is greater than or equal to the @@ -125,7 +125,7 @@ int get_proc_cpu_nr(void) /* *************************************************************************** - * Count the number of processors on the machine. + * Find the maximun number of processor on the machine plus one. * Try to use /sys for that, or /proc/stat if /sys doesn't exist. * * IN: @@ -157,6 +157,107 @@ 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 56b3e53..611138f 100644 --- a/count.h +++ b/count.h @@ -17,6 +17,8 @@ extern int get_cpu_nr(unsigned int); +extern int + get_cpu_total_nr(void); extern int get_irqcpu_nr(char *, int, int); extern int diff --git a/mpstat.c b/mpstat.c index f28a367..b812130 100644 --- a/mpstat.c +++ b/mpstat.c @@ -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, cpu_nr); + header.nodename, header.machine, get_cpu_total_nr()); /* Main loop */ rw_mpstat_loop(dis_hdr, rows); -- 2.50.1