aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.h1
-rw-r--r--lib/xwrap.c18
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/lib.h b/lib/lib.h
index 90f44dcb..04e81e8a 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -136,6 +136,7 @@ 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);
diff --git a/lib/xwrap.c b/lib/xwrap.c
index 4880bbe0..69f891b9 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -723,3 +723,21 @@ 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;
+}