Add a helper for querying the number of cores
authorBarret Rhoden <brho@cs.berkeley.edu>
Mon, 7 Mar 2016 19:04:04 +0000 (14:04 -0500)
committerBarret Rhoden <brho@cs.berkeley.edu>
Mon, 7 Mar 2016 19:38:17 +0000 (14:38 -0500)
The info is exposed via #vars.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
user/parlib/include/parlib/sysinfo.h [new file with mode: 0644]
user/parlib/sysinfo.c [new file with mode: 0644]

diff --git a/user/parlib/include/parlib/sysinfo.h b/user/parlib/include/parlib/sysinfo.h
new file mode 100644 (file)
index 0000000..2584d3d
--- /dev/null
@@ -0,0 +1,13 @@
+/* Copyright (c) 2016 Google Inc.
+ * Barret Rhoden <brho@cs.berkeley.edu>
+ * See LICENSE for details.
+ *
+ * Helper functions to query information about the system. */
+
+#pragma once
+
+__BEGIN_DECLS
+
+int get_num_pcores(void);
+
+__END_DECLS
diff --git a/user/parlib/sysinfo.c b/user/parlib/sysinfo.c
new file mode 100644 (file)
index 0000000..50ff1dc
--- /dev/null
@@ -0,0 +1,36 @@
+/* Copyright (c) 2016 Google Inc.
+ * Barret Rhoden <brho@cs.berkeley.edu>
+ * See LICENSE for details.
+ *
+ * Helper functions to query information about the system. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <unistd.h>
+
+#include <parlib/sysinfo.h>
+#include <ros/arch/arch.h>
+
+int get_num_pcores(void)
+{
+       int fd;
+       int ret;
+       char buf[128];
+
+       fd = open("#vars/num_cores!dw", O_RDONLY);
+       if (fd < 0)
+               return MAX_NUM_CORES;
+       if (read(fd, buf, sizeof(buf)) < 0) {
+               /* major bug */
+               perror("#vars read");
+               exit(-1);
+       }
+       ret = strtol(buf, 0, 0);
+       close(fd);
+       return ret;
+}