OSDN Git Service

Replace xcount_cpus() with a call to sysconf(_SC_NPROCESSORS_CONF)
authorRob Landley <rob@landley.net>
Tue, 27 Oct 2015 10:58:19 +0000 (05:58 -0500)
committerRob Landley <rob@landley.net>
Tue, 27 Oct 2015 10:58:19 +0000 (05:58 -0500)
lib/lib.h
lib/xwrap.c
toys/other/taskset.c

index 04e81e8..90f44dc 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -136,7 +136,6 @@ void xpidfile(char *name);
 void xregcomp(regex_t *preg, char *rexec, int cflags);
 char *xtzset(char *new);
 void xsignal(int signal, void *handler);
-unsigned xcount_cpus(void);
 
 // lib.c
 void verror_msg(char *msg, int err, va_list va);
index 69f891b..4880bbe 100644 (file)
@@ -723,21 +723,3 @@ void xsignal(int signal, void *handler)
 
   if (sigaction(signal, sa, 0)) perror_exit("xsignal %d", signal);
 }
-
-unsigned xcount_cpus(void)
-{
-  int len = 0, i, fd = xopen("/proc/stat", O_RDONLY);
-  unsigned cpus = 0;
-
-  for (;;) {
-    if (1>(i = xread(fd, libbuf, sizeof(libbuf)-len))) break;
-    len += i;
-    // Each cpu# line has data after it, so last 5 bytes of file can't match
-    for (i = 0; i<len-5; i++)
-      if (!strncmp(libbuf+i, "\ncpu", 4) && isdigit(libbuf[i+4])) cpus++;
-    memmove(libbuf, libbuf+i, 5);
-  }
-  close(fd);
-
-  return cpus;
-}
index 29ce317..abc3872 100644 (file)
@@ -127,7 +127,7 @@ void nproc_main(void)
   }
 
   // If getaffinity failed or --all, count cpu entries in proc
-  if (!nproc) nproc = xcount_cpus();
+  if (!nproc) nproc = sysconf(_SC_NPROCESSORS_CONF);
 
   xprintf("%u\n", nproc);
 }