perf stat: Add per-core aggregation
[linux-3.10.git] / tools / perf / util / cpumap.c
index 6893eec..beb8cf9 100644 (file)
@@ -1,8 +1,10 @@
 #include "util.h"
+#include "sysfs.h"
 #include "../perf.h"
 #include "cpumap.h"
 #include <assert.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 static struct cpu_map *cpu_map__default_new(void)
 {
@@ -38,24 +40,19 @@ static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
        return cpus;
 }
 
-static struct cpu_map *cpu_map__read_all_cpu_map(void)
+struct cpu_map *cpu_map__read(FILE *file)
 {
        struct cpu_map *cpus = NULL;
-       FILE *onlnf;
        int nr_cpus = 0;
        int *tmp_cpus = NULL, *tmp;
        int max_entries = 0;
        int n, cpu, prev;
        char sep;
 
-       onlnf = fopen("/sys/devices/system/cpu/online", "r");
-       if (!onlnf)
-               return cpu_map__default_new();
-
        sep = 0;
        prev = -1;
        for (;;) {
-               n = fscanf(onlnf, "%u%c", &cpu, &sep);
+               n = fscanf(file, "%u%c", &cpu, &sep);
                if (n <= 0)
                        break;
                if (prev >= 0) {
@@ -95,6 +92,19 @@ static struct cpu_map *cpu_map__read_all_cpu_map(void)
                cpus = cpu_map__default_new();
 out_free_tmp:
        free(tmp_cpus);
+       return cpus;
+}
+
+static struct cpu_map *cpu_map__read_all_cpu_map(void)
+{
+       struct cpu_map *cpus = NULL;
+       FILE *onlnf;
+
+       onlnf = fopen("/sys/devices/system/cpu/online", "r");
+       if (!onlnf)
+               return cpu_map__default_new();
+
+       cpus = cpu_map__read(onlnf);
        fclose(onlnf);
        return cpus;
 }
@@ -166,6 +176,17 @@ out:
        return cpus;
 }
 
+size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
+{
+       int i;
+       size_t printed = fprintf(fp, "%d cpu%s: ",
+                                map->nr, map->nr > 1 ? "s" : "");
+       for (i = 0; i < map->nr; ++i)
+               printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
+
+       return printed + fprintf(fp, "\n");
+}
+
 struct cpu_map *cpu_map__dummy_new(void)
 {
        struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
@@ -182,3 +203,117 @@ void cpu_map__delete(struct cpu_map *map)
 {
        free(map);
 }
+
+int cpu_map__get_socket(struct cpu_map *map, int idx)
+{
+       FILE *fp;
+       const char *mnt;
+       char path[PATH_MAX];
+       int cpu, ret;
+
+       if (idx > map->nr)
+               return -1;
+
+       cpu = map->map[idx];
+
+       mnt = sysfs_find_mountpoint();
+       if (!mnt)
+               return -1;
+
+       snprintf(path, PATH_MAX,
+               "%s/devices/system/cpu/cpu%d/topology/physical_package_id",
+               mnt, cpu);
+
+       fp = fopen(path, "r");
+       if (!fp)
+               return -1;
+       ret = fscanf(fp, "%d", &cpu);
+       fclose(fp);
+       return ret == 1 ? cpu : -1;
+}
+
+static int cmp_ids(const void *a, const void *b)
+{
+       return *(int *)a - *(int *)b;
+}
+
+static int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
+                             int (*f)(struct cpu_map *map, int cpu))
+{
+       struct cpu_map *c;
+       int nr = cpus->nr;
+       int cpu, s1, s2;
+
+       /* allocate as much as possible */
+       c = calloc(1, sizeof(*c) + nr * sizeof(int));
+       if (!c)
+               return -1;
+
+       for (cpu = 0; cpu < nr; cpu++) {
+               s1 = f(cpus, cpu);
+               for (s2 = 0; s2 < c->nr; s2++) {
+                       if (s1 == c->map[s2])
+                               break;
+               }
+               if (s2 == c->nr) {
+                       c->map[c->nr] = s1;
+                       c->nr++;
+               }
+       }
+       /* ensure we process id in increasing order */
+       qsort(c->map, c->nr, sizeof(int), cmp_ids);
+
+       *res = c;
+       return 0;
+}
+
+int cpu_map__get_core(struct cpu_map *map, int idx)
+{
+       FILE *fp;
+       const char *mnt;
+       char path[PATH_MAX];
+       int cpu, ret, s;
+
+       if (idx > map->nr)
+               return -1;
+
+       cpu = map->map[idx];
+
+       mnt = sysfs_find_mountpoint();
+       if (!mnt)
+               return -1;
+
+       snprintf(path, PATH_MAX,
+               "%s/devices/system/cpu/cpu%d/topology/core_id",
+               mnt, cpu);
+
+       fp = fopen(path, "r");
+       if (!fp)
+               return -1;
+       ret = fscanf(fp, "%d", &cpu);
+       fclose(fp);
+       if (ret != 1)
+               return -1;
+
+       s = cpu_map__get_socket(map, idx);
+       if (s == -1)
+               return -1;
+
+       /*
+        * encode socket in upper 16 bits
+        * core_id is relative to socket, and
+        * we need a global id. So we combine
+        * socket+ core id
+        */
+       return (s << 16) | (cpu & 0xffff);
+}
+
+int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
+{
+       return cpu_map__build_map(cpus, sockp, cpu_map__get_socket);
+}
+
+int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
+{
+       return cpu_map__build_map(cpus, corep, cpu_map__get_core);
+}