Added CPU set data structure
authorDavide Libenzi <dlibenzi@google.com>
Sun, 8 Nov 2015 19:27:01 +0000 (11:27 -0800)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 24 Nov 2015 20:38:22 +0000 (15:38 -0500)
Added CPU set data structure.

Signed-off-by: Davide Libenzi <dlibenzi@google.com>
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/include/core_set.h [new file with mode: 0644]

diff --git a/kern/include/core_set.h b/kern/include/core_set.h
new file mode 100644 (file)
index 0000000..19b7b51
--- /dev/null
@@ -0,0 +1,63 @@
+/* Copyright (c) 2015 Google Inc
+ * Davide Libenzi <dlibenzi@google.com>
+ * See LICENSE for details.
+ */
+
+#pragma once
+
+#include <arch/arch.h>
+#include <arch/topology.h>
+#include <ros/common.h>
+#include <stdio.h>
+#include <string.h>
+#include <bitops.h>
+
+struct core_set {
+       unsigned long cpus[DIV_ROUND_UP(MAX_NUM_CORES, BITS_PER_LONG)];
+};
+
+static inline void core_set_init(struct core_set *cset)
+{
+       memset(cset, 0, sizeof(*cset));
+}
+
+static inline void core_set_setcpu(struct core_set *cset, unsigned int cpuno)
+{
+       __set_bit(cpuno, cset->cpus);
+}
+
+static inline void core_set_clearcpu(struct core_set *cset, unsigned int cpuno)
+{
+       __clear_bit(cpuno, cset->cpus);
+}
+
+static inline bool core_set_getcpu(const struct core_set *cset,
+                                                                 unsigned int cpuno)
+{
+       return test_bit(cpuno, cset->cpus);
+}
+
+static inline void core_set_fill_available(struct core_set *cset)
+{
+       for (int i = 0; i < num_cores; i++)
+               core_set_setcpu(cset, i);
+}
+
+static inline int core_set_count(const struct core_set *cset)
+{
+       int count = 0;
+
+       for (size_t i = 0; i < ARRAY_SIZE(cset->cpus); i++) {
+               for (unsigned long v = cset->cpus[i]; v; v >>= 1)
+                       count += (int) (v & 1);
+       }
+
+       return count;
+}
+
+static inline int core_set_remote_count(const struct core_set *cset)
+{
+       int count = core_set_count(cset), cpu = core_id();
+
+       return core_set_getcpu(cset, cpu) ? count - 1: count;
+}