parlib: Move core_sets to parlib
authorBarret Rhoden <brho@cs.berkeley.edu>
Wed, 13 Dec 2017 20:21:09 +0000 (15:21 -0500)
committerBarret Rhoden <brho@cs.berkeley.edu>
Thu, 14 Dec 2017 18:42:20 +0000 (13:42 -0500)
I converted xopen() -> open() and xstrdup() -> strdup().

I tried putting xlib in parlib, but a bunch of other projects use those
names for the same functions, and we run into conflicts.  This is why we
can't have nice things.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
tools/dev-util/perf/Makefile
tools/dev-util/perf/akaros.c [deleted file]
tools/dev-util/perf/akaros.h [deleted file]
tools/dev-util/perf/perf.c
tools/dev-util/perf/perf_core.c
tools/dev-util/perf/perf_core.h
tools/dev-util/perf/perfconv.c
user/parlib/core_set.c [new file with mode: 0644]
user/parlib/include/parlib/core_set.h [new file with mode: 0644]

index 6e506f6..1a90f49 100644 (file)
@@ -1,6 +1,6 @@
 include ../../Makefrag
 
-SOURCES = perf.c perfconv.c xlib.c perf_core.c akaros.c symbol-elf.c
+SOURCES = perf.c perfconv.c xlib.c perf_core.c symbol-elf.c
 
 XCC = $(CROSS_COMPILE)gcc
 
diff --git a/tools/dev-util/perf/akaros.c b/tools/dev-util/perf/akaros.c
deleted file mode 100644 (file)
index 5ccbd81..0000000
+++ /dev/null
@@ -1,150 +0,0 @@
-/* Copyright (c) 2015 Google Inc
- * Davide Libenzi <dlibenzi@google.com>
- * See LICENSE for details.
- */
-
-#include <ros/arch/arch.h>
-#include <ros/common.h>
-#include <sys/types.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-#include <limits.h>
-#include <parlib/parlib.h>
-#include "xlib.h"
-#include "akaros.h"
-
-static const unsigned int llcores[] = {
-       0
-};
-
-void parlib_get_ll_core_set(struct core_set *cores)
-{
-       parlib_get_none_core_set(cores);
-       for (size_t i = 0; i < COUNT_OF(llcores); i++)
-               parlib_set_core(cores, llcores[i]);
-}
-
-size_t parlib_nr_ll_cores(void)
-{
-       return COUNT_OF(llcores);
-}
-
-static int guess_nr_cores(void)
-{
-       return max_vcores() + parlib_nr_ll_cores();
-}
-
-static int get_vars_nr_cores(void)
-{
-       int fd, ret;
-       char buf[10];
-
-       fd = xopen("#vars/num_cores!dw", O_READ, 0);
-       if (fd < 0)
-               return -1;
-       if (read(fd, buf, sizeof(buf)) <= 0) {
-               close(fd);
-               return -1;
-       }
-       ret = atoi(buf);
-       return ret;
-}
-
-static int nr_cores;
-
-static void set_nr_cores(void *arg)
-{
-       nr_cores = get_vars_nr_cores();
-       if (nr_cores == -1)
-               nr_cores = guess_nr_cores();
-}
-
-size_t parlib_nr_total_cores(void)
-{
-       static parlib_once_t once = PARLIB_ONCE_INIT;
-
-       parlib_run_once(&once, set_nr_cores, NULL);
-       return nr_cores;
-}
-
-void parlib_parse_cores(const char *str, struct core_set *cores)
-{
-       unsigned int fcpu, ncpu;
-       char *dstr = xstrdup(str);
-       char *sptr = NULL;
-       char *tok, *sptr2;
-
-       ZERO_DATA(*cores);
-       for (tok = strtok_r(dstr, ",", &sptr); tok;
-                tok = strtok_r(NULL, ",", &sptr)) {
-
-               if (strchr(tok, '-')) {
-                       if (sscanf(tok, "%u-%u", &fcpu, &ncpu) != 2) {
-                               fprintf(stderr, "Invalid CPU range: %s\n", tok);
-                               exit(1);
-                       }
-                       if (fcpu >= parlib_nr_total_cores()) {
-                               fprintf(stderr, "CPU number out of bound: %u\n", fcpu);
-                               exit(1);
-                       }
-                       if (ncpu >= parlib_nr_total_cores()) {
-                               fprintf(stderr, "CPU number out of bound: %u\n", ncpu);
-                               exit(1);
-                       }
-                       if (fcpu > ncpu) {
-                               fprintf(stderr, "CPU range is backwards: %u-%u\n", fcpu, ncpu);
-                               exit(1);
-                       }
-                       for (; fcpu <= ncpu; fcpu++)
-                               parlib_set_core(cores, fcpu);
-               } else {
-                       fcpu = atoi(tok);
-                       if (fcpu >= parlib_nr_total_cores()) {
-                               fprintf(stderr, "CPU number out of bound: %u\n",
-                                               fcpu);
-                               exit(1);
-                       }
-                       parlib_set_core(cores, fcpu);
-               }
-       }
-       free(dstr);
-}
-
-void parlib_get_all_core_set(struct core_set *cores)
-{
-       size_t max_cores = parlib_nr_total_cores();
-
-       memset(cores->core_set, 0xff, DIV_ROUND_UP(max_cores, CHAR_BIT));
-}
-
-void parlib_get_none_core_set(struct core_set *cores)
-{
-       size_t max_cores = parlib_nr_total_cores();
-
-       memset(cores->core_set, 0, DIV_ROUND_UP(max_cores, CHAR_BIT));
-}
-
-void parlib_not_core_set(struct core_set *dcs)
-{
-       size_t max_cores = parlib_nr_total_cores();
-
-       for (size_t i = 0; (max_cores > 0) && (i < sizeof(dcs->core_set)); i++) {
-               size_t nb = (max_cores >= CHAR_BIT) ? CHAR_BIT : max_cores;
-
-               dcs->core_set[i] = (~dcs->core_set[i]) & ((1 << nb) - 1);
-               max_cores -= nb;
-       }
-}
-
-void parlib_and_core_sets(struct core_set *dcs, const struct core_set *scs)
-{
-       for (size_t i = 0; i < sizeof(dcs->core_set); i++)
-               dcs->core_set[i] &= scs->core_set[i];
-}
-
-void parlib_or_core_sets(struct core_set *dcs, const struct core_set *scs)
-{
-       for (size_t i = 0; i < sizeof(dcs->core_set); i++)
-               dcs->core_set[i] |= scs->core_set[i];
-}
diff --git a/tools/dev-util/perf/akaros.h b/tools/dev-util/perf/akaros.h
deleted file mode 100644 (file)
index d47deb8..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/* Copyright (c) 2015 Google Inc
- * Davide Libenzi <dlibenzi@google.com>
- * See LICENSE for details.
- */
-
-#pragma once
-
-#include <ros/arch/arch.h>
-#include <ros/common.h>
-#include <sys/types.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <limits.h>
-#include <parlib/parlib.h>
-#include <parlib/bitmask.h>
-
-__BEGIN_DECLS
-
-#define CORE_SET_SIZE BYTES_FOR_BITMASK(MAX_NUM_CORES)
-
-struct core_set {
-       DECL_BITMASK(core_set, MAX_NUM_CORES);
-};
-
-void parlib_get_ll_core_set(struct core_set *cores);
-size_t parlib_nr_ll_cores(void);
-size_t parlib_nr_total_cores(void);
-void parlib_parse_cores(const char *str, struct core_set *cores);
-void parlib_get_all_core_set(struct core_set *cores);
-void parlib_get_none_core_set(struct core_set *cores);
-void parlib_not_core_set(struct core_set *dcs);
-void parlib_and_core_sets(struct core_set *dcs, const struct core_set *scs);
-void parlib_or_core_sets(struct core_set *dcs, const struct core_set *scs);
-
-static inline void parlib_set_core(struct core_set *cores, size_t coreid)
-{
-       SET_BITMASK_BIT(cores->core_set, coreid);
-}
-
-static inline void parlib_clear_core(struct core_set *cores, size_t coreid)
-{
-       CLR_BITMASK_BIT(cores->core_set, coreid);
-}
-
-static inline bool parlib_get_core(const struct core_set *cores, size_t coreid)
-{
-       return GET_BITMASK_BIT(cores->core_set, coreid);
-}
-
-__END_DECLS
index e7227bb..b018f9f 100644 (file)
@@ -19,8 +19,8 @@
 #include <time.h>
 #include <parlib/parlib.h>
 #include <parlib/timing.h>
+#include <parlib/core_set.h>
 #include "xlib.h"
-#include "akaros.h"
 #include "perfconv.h"
 #include "perf_core.h"
 
index 17b4811..4143c9c 100644 (file)
 #include <errno.h>
 #include <regex.h>
 #include <parlib/parlib.h>
+#include <parlib/core_set.h>
 #include <perfmon/err.h>
 #include <perfmon/pfmlib.h>
 #include "xlib.h"
 #include "perfconv.h"
-#include "akaros.h"
 #include "perf_core.h"
 #include "elf.h"
 
index ef67083..42e7419 100644 (file)
@@ -12,7 +12,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <limits.h>
-#include "akaros.h"
+#include <parlib/core_set.h>
 #include "perfconv.h"
 
 #define MAX_CPU_EVENTS 256
index f00a222..abc6379 100644 (file)
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <errno.h>
 #include <stdarg.h>
+#include <assert.h>
 #include "perf_format.h"
 #include "xlib.h"
 #include "perf_core.h"
diff --git a/user/parlib/core_set.c b/user/parlib/core_set.c
new file mode 100644 (file)
index 0000000..8c41245
--- /dev/null
@@ -0,0 +1,157 @@
+/* Copyright (c) 2015 Google Inc
+ * Davide Libenzi <dlibenzi@google.com>
+ * See LICENSE for details.
+ */
+
+#include <ros/arch/arch.h>
+#include <ros/common.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <unistd.h>
+#include <parlib/parlib.h>
+#include <parlib/core_set.h>
+
+static const unsigned int llcores[] = {
+       0
+};
+
+void parlib_get_ll_core_set(struct core_set *cores)
+{
+       parlib_get_none_core_set(cores);
+       for (size_t i = 0; i < COUNT_OF(llcores); i++)
+               parlib_set_core(cores, llcores[i]);
+}
+
+size_t parlib_nr_ll_cores(void)
+{
+       return COUNT_OF(llcores);
+}
+
+static int guess_nr_cores(void)
+{
+       return max_vcores() + parlib_nr_ll_cores();
+}
+
+static int get_vars_nr_cores(void)
+{
+       int fd, ret;
+       char buf[10];
+
+       fd = open("#vars/num_cores!dw", O_READ);
+       if (fd < 0)
+               return -1;
+       if (read(fd, buf, sizeof(buf)) <= 0) {
+               close(fd);
+               return -1;
+       }
+       ret = atoi(buf);
+       return ret;
+}
+
+static int nr_cores;
+
+static void set_nr_cores(void *arg)
+{
+       nr_cores = get_vars_nr_cores();
+       if (nr_cores == -1)
+               nr_cores = guess_nr_cores();
+}
+
+size_t parlib_nr_total_cores(void)
+{
+       static parlib_once_t once = PARLIB_ONCE_INIT;
+
+       parlib_run_once(&once, set_nr_cores, NULL);
+       return nr_cores;
+}
+
+void parlib_parse_cores(const char *str, struct core_set *cores)
+{
+       unsigned int fcpu, ncpu;
+       char *dstr = strdup(str);
+       char *sptr = NULL;
+       char *tok, *sptr2;
+
+       if (!dstr) {
+               perror("Duplicating a string");
+               exit(1);
+       }
+       ZERO_DATA(*cores);
+       for (tok = strtok_r(dstr, ",", &sptr); tok;
+                tok = strtok_r(NULL, ",", &sptr)) {
+
+               if (strchr(tok, '-')) {
+                       if (sscanf(tok, "%u-%u", &fcpu, &ncpu) != 2) {
+                               fprintf(stderr, "Invalid CPU range: %s\n", tok);
+                               exit(1);
+                       }
+                       if (fcpu >= parlib_nr_total_cores()) {
+                               fprintf(stderr, "CPU number out of bound: %u\n", fcpu);
+                               exit(1);
+                       }
+                       if (ncpu >= parlib_nr_total_cores()) {
+                               fprintf(stderr, "CPU number out of bound: %u\n", ncpu);
+                               exit(1);
+                       }
+                       if (fcpu > ncpu) {
+                               fprintf(stderr, "CPU range is backwards: %u-%u\n", fcpu, ncpu);
+                               exit(1);
+                       }
+                       for (; fcpu <= ncpu; fcpu++)
+                               parlib_set_core(cores, fcpu);
+               } else {
+                       fcpu = atoi(tok);
+                       if (fcpu >= parlib_nr_total_cores()) {
+                               fprintf(stderr, "CPU number out of bound: %u\n",
+                                               fcpu);
+                               exit(1);
+                       }
+                       parlib_set_core(cores, fcpu);
+               }
+       }
+       free(dstr);
+}
+
+void parlib_get_all_core_set(struct core_set *cores)
+{
+       size_t max_cores = parlib_nr_total_cores();
+
+       memset(cores->core_set, 0xff, DIV_ROUND_UP(max_cores, CHAR_BIT));
+}
+
+void parlib_get_none_core_set(struct core_set *cores)
+{
+       size_t max_cores = parlib_nr_total_cores();
+
+       memset(cores->core_set, 0, DIV_ROUND_UP(max_cores, CHAR_BIT));
+}
+
+void parlib_not_core_set(struct core_set *dcs)
+{
+       size_t max_cores = parlib_nr_total_cores();
+
+       for (size_t i = 0; (max_cores > 0) && (i < sizeof(dcs->core_set)); i++) {
+               size_t nb = (max_cores >= CHAR_BIT) ? CHAR_BIT : max_cores;
+
+               dcs->core_set[i] = (~dcs->core_set[i]) & ((1 << nb) - 1);
+               max_cores -= nb;
+       }
+}
+
+void parlib_and_core_sets(struct core_set *dcs, const struct core_set *scs)
+{
+       for (size_t i = 0; i < sizeof(dcs->core_set); i++)
+               dcs->core_set[i] &= scs->core_set[i];
+}
+
+void parlib_or_core_sets(struct core_set *dcs, const struct core_set *scs)
+{
+       for (size_t i = 0; i < sizeof(dcs->core_set); i++)
+               dcs->core_set[i] |= scs->core_set[i];
+}
diff --git a/user/parlib/include/parlib/core_set.h b/user/parlib/include/parlib/core_set.h
new file mode 100644 (file)
index 0000000..0832931
--- /dev/null
@@ -0,0 +1,43 @@
+/* Copyright (c) 2015 Google Inc
+ * Davide Libenzi <dlibenzi@google.com>
+ * See LICENSE for details.
+ */
+
+#pragma once
+
+#include <parlib/bitmask.h>
+
+__BEGIN_DECLS
+
+#define CORE_SET_SIZE BYTES_FOR_BITMASK(MAX_NUM_CORES)
+
+struct core_set {
+       DECL_BITMASK(core_set, MAX_NUM_CORES);
+};
+
+void parlib_get_ll_core_set(struct core_set *cores);
+size_t parlib_nr_ll_cores(void);
+size_t parlib_nr_total_cores(void);
+void parlib_parse_cores(const char *str, struct core_set *cores);
+void parlib_get_all_core_set(struct core_set *cores);
+void parlib_get_none_core_set(struct core_set *cores);
+void parlib_not_core_set(struct core_set *dcs);
+void parlib_and_core_sets(struct core_set *dcs, const struct core_set *scs);
+void parlib_or_core_sets(struct core_set *dcs, const struct core_set *scs);
+
+static inline void parlib_set_core(struct core_set *cores, size_t coreid)
+{
+       SET_BITMASK_BIT(cores->core_set, coreid);
+}
+
+static inline void parlib_clear_core(struct core_set *cores, size_t coreid)
+{
+       CLR_BITMASK_BIT(cores->core_set, coreid);
+}
+
+static inline bool parlib_get_core(const struct core_set *cores, size_t coreid)
+{
+       return GET_BITMASK_BIT(cores->core_set, coreid);
+}
+
+__END_DECLS