Currently we're determining the number of CPUs by the function
get_cpu_nr() which in turn calls get_sys_cpu_nr() or even
get_proc_cpu(). I think we could easily use sysconf(
_SC_NPROCESSORS_CONF), and if this symbol isn't defined, then we'll read
/proc/cpuinfo. This way we can get rid of those two not very pretty
functions.
mpstat.c | 83
+++++++++------------------------------------------------------
1 file changed, 12 insertions(+), 71 deletions(-)
TIA,
--
MP
diff --git a/procps/mpstat.c b/procps/mpstat.c
index 125bb3d..e00d04a 100644
--- a/procps/mpstat.c
+++ b/procps/mpstat.c
@@ -769,83 +769,24 @@ static void print_header(struct tm *t)
uts.sysname, uts.release, uts.nodename, cur_date, uts.machine, G.cpu_nr);
}
-/*
- * Get number of processors in /sys
- */
-static int get_sys_cpu_nr(void)
-{
- DIR *dir;
- struct dirent *d;
- struct stat buf;
- char line[MAX_PF_NAME];
- int proc_nr = 0;
-
- dir = opendir(SYSFS_DEVCPU);
- if (!dir)
- return 0; /* /sys not mounted */
-
- /* Get current file entry */
- while ((d = readdir(dir)) != NULL) {
- if (starts_with_cpu(d->d_name) && isdigit(d->d_name[3])) {
- snprintf(line, MAX_PF_NAME, "%s/%s", SYSFS_DEVCPU,
- d->d_name);
- line[MAX_PF_NAME - 1] = '\0';
- /* Get information about file */
- if (stat(line, &buf) < 0)
- continue;
- /* If found 'cpuN', we have one more processor */
- if (S_ISDIR(buf.st_mode))
- proc_nr++;
- }
- }
-
- closedir(dir);
- return proc_nr;
-}
-
-/*
- * Get number of processors in /proc/stat
- * Return value '0' means one CPU and non SMP kernel.
- * Otherwise N means N processor(s) and SMP kernel.
- */
-static int get_proc_cpu_nr(void)
+static int get_cpu_nr(void)
{
+#ifdef _SC_NPROCESSORS_CONF
+ return sysconf(_SC_NPROCESSORS_CONF);
+#else
+ char buf[16];
+ int n = 0;
FILE *fp;
- char line[256];
- int proc_nr = -1;
-
- fp = xfopen_for_read(PROCFS_STAT);
- while (fgets(line, sizeof(line), fp)) {
- if (!starts_with_cpu(line)) {
- if (proc_nr >= 0)
- break; /* we are past "cpuN..." lines */
- continue;
- }
- if (line[3] != ' ') { /* "cpuN" */
- int num_proc;
- if (sscanf(line + 3, "%u", &num_proc) == 1
- && num_proc > proc_nr
- ) {
- proc_nr = num_proc;
- }
- }
- }
- fclose(fp);
- return proc_nr + 1;
-}
+ fp = xfopen_for_read("/proc/cpuinfo");
-static int get_cpu_nr(void)
-{
- int n;
-
- /* Try to use /sys, if possible */
- n = get_sys_cpu_nr();
- if (n == 0)
- /* Otherwise use /proc/stat */
- n = get_proc_cpu_nr();
+ while (fgets(buf, sizeof(buf), fp))
+ if (strncmp(buf, "processor\t:", 11) == 0)
+ n++;
+ fclose(fp);
return n;
+#endif
}
/*
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox