perf: Use Linux-style parsing of the core list
authorBarret Rhoden <brho@cs.berkeley.edu>
Tue, 7 Jun 2016 01:40:44 +0000 (21:40 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Thu, 16 Jun 2016 16:20:31 +0000 (12:20 -0400)
-C x,y,i-j,z

No need for the LL cores, the negation, or the all cores.  We do all
cores by default.

This also checks with the kernel to find out how many cores there are
total, instead of allowing people to set for cores that do not exist.
We'll see if that's a bad idea or not.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
tools/profile/perf/akaros.c

index 62fe493..3ae2a22 100644 (file)
@@ -29,11 +29,43 @@ size_t ros_get_low_latency_core_count(void)
        return COUNT_OF(llcores);
 }
 
-size_t ros_total_cores(void)
+static int guess_nr_cores(void)
 {
        return max_vcores() + ros_get_low_latency_core_count();
 }
 
+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)
+{
+       nr_cores = get_vars_nr_cores();
+       if (nr_cores == -1)
+               nr_cores = guess_nr_cores();
+}
+
+
+size_t ros_total_cores(void)
+{
+       run_once(set_nr_cores());
+       return nr_cores;
+}
+
 void ros_parse_cores(const char *str, struct core_set *cores)
 {
        unsigned int fcpu, ncpu;
@@ -42,62 +74,36 @@ void ros_parse_cores(const char *str, struct core_set *cores)
        char *tok, *sptr2;
 
        ZERO_DATA(*cores);
-       for (tok = strtok_r(dstr, ":", &sptr); tok;
-                tok = strtok_r(NULL, ":", &sptr)) {
-               bool neg_core = FALSE;
-
-               if (*tok == '!') {
-                       neg_core = TRUE;
-                       tok++;
-               }
-               if (!strcmp(tok, "all")) {
-                       size_t max_cores = ros_total_cores();
+       for (tok = strtok_r(dstr, ",", &sptr); tok;
+                tok = strtok_r(NULL, ",", &sptr)) {
 
-                       if (max_cores > MAX_NUM_CORES) {
-                               fprintf(stderr, "The number of system CPU exceeds the "
-                                               "structure limits: num_cores=%u limits=%u\n", max_cores,
-                                               CHAR_BIT * CORE_SET_SIZE);
-                               exit(1);
-                       }
-                       if (neg_core)
-                               memset(cores->core_set, 0,
-                                          DIV_ROUND_UP(max_cores, CHAR_BIT));
-                       else
-                               memset(cores->core_set, 0xff,
-                                          DIV_ROUND_UP(max_cores, CHAR_BIT));
-               } else if (!strcmp(tok, "llall")) {
-                       ros_get_low_latency_core_set(cores);
-               } else if (strchr(tok, '-')) {
+               if (strchr(tok, '-')) {
                        if (sscanf(tok, "%u-%u", &fcpu, &ncpu) != 2) {
                                fprintf(stderr, "Invalid CPU range: %s\n", tok);
                                exit(1);
                        }
-                       if ((fcpu >= MAX_NUM_CORES) ||
-                               (ncpu >= MAX_NUM_CORES) || (fcpu > ncpu)) {
-                               fprintf(stderr, "CPU number out of bound: %u\n",
-                                               fcpu);
+                       if (fcpu >= ros_total_cores()) {
+                               fprintf(stderr, "CPU number out of bound: %u\n", fcpu);
                                exit(1);
                        }
-                       for (; fcpu <= ncpu; fcpu++) {
-                               if (neg_core)
-                                       ros_clear_bit(cores->core_set, fcpu);
-                               else
-                                       ros_set_bit(cores->core_set, fcpu);
+                       if (ncpu >= ros_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++)
+                               ros_set_bit(cores->core_set, fcpu);
                } else {
-                       for (tok = strtok_r(tok, ".", &sptr2); tok;
-                                tok = strtok_r(NULL, ".", &sptr2)) {
-                               fcpu = atoi(tok);
-                               if (fcpu >= MAX_NUM_CORES) {
-                                       fprintf(stderr, "CPU number out of bound: %u\n",
-                                                       fcpu);
-                                       exit(1);
-                               }
-                               if (neg_core)
-                                       ros_clear_bit(cores->core_set, fcpu);
-                               else
-                                       ros_set_bit(cores->core_set, fcpu);
+                       fcpu = atoi(tok);
+                       if (fcpu >= ros_total_cores()) {
+                               fprintf(stderr, "CPU number out of bound: %u\n",
+                                               fcpu);
+                               exit(1);
                        }
+                       ros_set_bit(cores->core_set, fcpu);
                }
        }
        free(dstr);