perf stat aggregation: Start using cpu_aggr_id in map
[linux-2.6-microblaze.git] / tools / perf / util / cpumap.c
index dc5c5e6..b50609b 100644 (file)
@@ -95,6 +95,23 @@ struct perf_cpu_map *perf_cpu_map__empty_new(int nr)
        return cpus;
 }
 
+struct cpu_aggr_map *cpu_aggr_map__empty_new(int nr)
+{
+       struct cpu_aggr_map *cpus = malloc(sizeof(*cpus) + sizeof(struct aggr_cpu_id) * nr);
+
+       if (cpus != NULL) {
+               int i;
+
+               cpus->nr = nr;
+               for (i = 0; i < nr; i++)
+                       cpus->map[i] = cpu_map__empty_aggr_cpu_id();
+
+               refcount_set(&cpus->refcnt, 1);
+       }
+
+       return cpus;
+}
+
 static int cpu__get_topology_int(int cpu, const char *name, int *value)
 {
        char path[PATH_MAX];
@@ -111,40 +128,48 @@ int cpu_map__get_socket_id(int cpu)
        return ret ?: value;
 }
 
-int cpu_map__get_socket(struct perf_cpu_map *map, int idx, void *data __maybe_unused)
+struct aggr_cpu_id cpu_map__get_socket(struct perf_cpu_map *map, int idx,
+                                       void *data __maybe_unused)
 {
        int cpu;
+       struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
 
        if (idx > map->nr)
-               return -1;
+               return id;
 
        cpu = map->map[idx];
 
-       return cpu_map__get_socket_id(cpu);
+       id.id = cpu_map__get_socket_id(cpu);
+       return id;
 }
 
-static int cmp_ids(const void *a, const void *b)
+static int cmp_aggr_cpu_id(const void *a_pointer, const void *b_pointer)
 {
-       return *(int *)a - *(int *)b;
+       struct aggr_cpu_id *a = (struct aggr_cpu_id *)a_pointer;
+       struct aggr_cpu_id *b = (struct aggr_cpu_id *)b_pointer;
+
+       return a->id - b->id;
 }
 
-int cpu_map__build_map(struct perf_cpu_map *cpus, struct perf_cpu_map **res,
-                      int (*f)(struct perf_cpu_map *map, int cpu, void *data),
+int cpu_map__build_map(struct perf_cpu_map *cpus, struct cpu_aggr_map **res,
+                      struct aggr_cpu_id (*f)(struct perf_cpu_map *map, int cpu, void *data),
                       void *data)
 {
-       struct perf_cpu_map *c;
        int nr = cpus->nr;
-       int cpu, s1, s2;
+       struct cpu_aggr_map *c = cpu_aggr_map__empty_new(nr);
+       int cpu, s2;
+       struct aggr_cpu_id s1;
 
-       /* allocate as much as possible */
-       c = calloc(1, sizeof(*c) + nr * sizeof(int));
        if (!c)
                return -1;
 
+       /* Reset size as it may only be partially filled */
+       c->nr = 0;
+
        for (cpu = 0; cpu < nr; cpu++) {
                s1 = f(cpus, cpu, data);
                for (s2 = 0; s2 < c->nr; s2++) {
-                       if (s1 == c->map[s2])
+                       if (cpu_map__compare_aggr_cpu_id(s1, c->map[s2]))
                                break;
                }
                if (s2 == c->nr) {
@@ -153,9 +178,8 @@ int cpu_map__build_map(struct perf_cpu_map *cpus, struct perf_cpu_map **res,
                }
        }
        /* ensure we process id in increasing order */
-       qsort(c->map, c->nr, sizeof(int), cmp_ids);
+       qsort(c->map, c->nr, sizeof(struct aggr_cpu_id), cmp_aggr_cpu_id);
 
-       refcount_set(&c->refcnt, 1);
        *res = c;
        return 0;
 }
@@ -167,23 +191,24 @@ int cpu_map__get_die_id(int cpu)
        return ret ?: value;
 }
 
-int cpu_map__get_die(struct perf_cpu_map *map, int idx, void *data)
+struct aggr_cpu_id cpu_map__get_die(struct perf_cpu_map *map, int idx, void *data)
 {
-       int cpu, die_id, s;
+       int cpu, s;
+       struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
 
        if (idx > map->nr)
-               return -1;
+               return id;
 
        cpu = map->map[idx];
 
-       die_id = cpu_map__get_die_id(cpu);
+       id.id = cpu_map__get_die_id(cpu);
        /* There is no die_id on legacy system. */
-       if (die_id == -1)
-               die_id = 0;
+       if (id.id == -1)
+               id.id = 0;
 
-       s = cpu_map__get_socket(map, idx, data);
+       s = cpu_map__get_socket(map, idx, data).id;
        if (s == -1)
-               return -1;
+               return cpu_map__empty_aggr_cpu_id();
 
        /*
         * Encode socket in bit range 15:8
@@ -191,13 +216,14 @@ int cpu_map__get_die(struct perf_cpu_map *map, int idx, void *data)
         * we need a global id. So we combine
         * socket + die id
         */
-       if (WARN_ONCE(die_id >> 8, "The die id number is too big.\n"))
-               return -1;
+       if (WARN_ONCE(id.id >> 8, "The die id number is too big.\n"))
+               return cpu_map__empty_aggr_cpu_id();
 
        if (WARN_ONCE(s >> 8, "The socket id number is too big.\n"))
-               return -1;
+               return cpu_map__empty_aggr_cpu_id();
 
-       return (s << 8) | (die_id & 0xff);
+       id.id = (s << 8) | (id.id & 0xff);
+       return id;
 }
 
 int cpu_map__get_core_id(int cpu)
@@ -211,21 +237,22 @@ int cpu_map__get_node_id(int cpu)
        return cpu__get_node(cpu);
 }
 
-int cpu_map__get_core(struct perf_cpu_map *map, int idx, void *data)
+struct aggr_cpu_id cpu_map__get_core(struct perf_cpu_map *map, int idx, void *data)
 {
-       int cpu, s_die;
+       int cpu;
+       struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
 
        if (idx > map->nr)
-               return -1;
+               return id;
 
        cpu = map->map[idx];
 
        cpu = cpu_map__get_core_id(cpu);
 
-       /* s_die is the combination of socket + die id */
-       s_die = cpu_map__get_die(map, idx, data);
-       if (s_die == -1)
-               return -1;
+       /* cpu_map__get_die returns the combination of socket + die id */
+       id = cpu_map__get_die(map, idx, data);
+       if (cpu_map__aggr_cpu_id_is_empty(id))
+               return id;
 
        /*
         * encode socket in bit range 31:24
@@ -235,35 +262,39 @@ int cpu_map__get_core(struct perf_cpu_map *map, int idx, void *data)
         * socket + die id + core id
         */
        if (WARN_ONCE(cpu >> 16, "The core id number is too big.\n"))
-               return -1;
+               return cpu_map__empty_aggr_cpu_id();
 
-       return (s_die << 16) | (cpu & 0xffff);
+       id.id = (id.id << 16) | (cpu & 0xffff);
+       return id;
 }
 
-int cpu_map__get_node(struct perf_cpu_map *map, int idx, void *data __maybe_unused)
+struct aggr_cpu_id cpu_map__get_node(struct perf_cpu_map *map, int idx, void *data __maybe_unused)
 {
+       struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
+
        if (idx < 0 || idx >= map->nr)
-               return -1;
+               return id;
 
-       return cpu_map__get_node_id(map->map[idx]);
+       id.id = cpu_map__get_node_id(map->map[idx]);
+       return id;
 }
 
-int cpu_map__build_socket_map(struct perf_cpu_map *cpus, struct perf_cpu_map **sockp)
+int cpu_map__build_socket_map(struct perf_cpu_map *cpus, struct cpu_aggr_map **sockp)
 {
        return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
 }
 
-int cpu_map__build_die_map(struct perf_cpu_map *cpus, struct perf_cpu_map **diep)
+int cpu_map__build_die_map(struct perf_cpu_map *cpus, struct cpu_aggr_map **diep)
 {
        return cpu_map__build_map(cpus, diep, cpu_map__get_die, NULL);
 }
 
-int cpu_map__build_core_map(struct perf_cpu_map *cpus, struct perf_cpu_map **corep)
+int cpu_map__build_core_map(struct perf_cpu_map *cpus, struct cpu_aggr_map **corep)
 {
        return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
 }
 
-int cpu_map__build_node_map(struct perf_cpu_map *cpus, struct perf_cpu_map **numap)
+int cpu_map__build_node_map(struct perf_cpu_map *cpus, struct cpu_aggr_map **numap)
 {
        return cpu_map__build_map(cpus, numap, cpu_map__get_node, NULL);
 }
@@ -586,3 +617,21 @@ const struct perf_cpu_map *cpu_map__online(void) /* thread unsafe */
 
        return online;
 }
+
+bool cpu_map__compare_aggr_cpu_id(struct aggr_cpu_id a, struct aggr_cpu_id b)
+{
+       return a.id == b.id;
+}
+
+bool cpu_map__aggr_cpu_id_is_empty(struct aggr_cpu_id a)
+{
+       return a.id == -1;
+}
+
+struct aggr_cpu_id cpu_map__empty_aggr_cpu_id(void)
+{
+       struct aggr_cpu_id ret = {
+               .id = -1
+       };
+       return ret;
+}