[PATCH 1/2] perf,tools: get correct cpu id for print_aggr

From: kan . liang
Date: Mon Jun 29 2015 - 23:09:23 EST


From: Kan Liang <kan.liang@xxxxxxxxx>

print_aggr fails to print per-core/per-socket statistics after commit
b7f0c203586b ("perf evlist: Propagate cpu maps to evsels in an evlist"),
if events have differnt cpus. Because in print_aggr, aggr_get_id needs
index (not cpu id) to find core/pkg id.
This patch introduced perf_evsel__get_cpumap_index to get the index by
cpu id for a given event. The index can be used to find correct cpu id
for print_aggr.

Here is an example.
Counting events cycles,uncore_imc_0/cas_count_read/. (Uncore event has
cpumask 0,18)

"perf stat -e cycles,uncore_imc_0/cas_count_read/ -C0,18 --per-core
sleep 2"

Without this patch, it failes to get CPU 18 result.
Performance counter stats for 'CPU(s) 0,18':

S0-C0 1 7526851 cycles
S0-C0 1 1.05 MiB uncore_imc_0/cas_count_read/
S1-C0 0 <not counted> cycles
S1-C0 0 <not counted> MiB uncore_imc_0/cas_count_read/

With this patch, it can get both CPU0 and CPU18 result.
Performance counter stats for 'CPU(s) 0,18':

S0-C0 1 6327768 cycles
S0-C0 1 0.47 MiB uncore_imc_0/cas_count_read/
S1-C0 1 330228 cycles
S1-C0 1 0.29 MiB uncore_imc_0/cas_count_read/

Signed-off-by: Kan Liang <kan.liang@xxxxxxxxx>
---
tools/perf/builtin-stat.c | 1 +
tools/perf/util/cpumap.c | 4 ++--
tools/perf/util/evsel.c | 14 ++++++++++++++
tools/perf/util/evsel.h | 2 ++
4 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 37e301a..a3ea735 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -708,6 +708,7 @@ static void print_aggr(char *prefix)
nr = 0;
for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
cpu2 = perf_evsel__cpus(counter)->map[cpu];
+ cpu2 = perf_evsel__get_cpumap_index(cpu2, evsel_list->cpus);
s2 = aggr_get_id(evsel_list->cpus, cpu2);
if (s2 != id)
continue;
diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
index 3667e21..34843e5 100644
--- a/tools/perf/util/cpumap.c
+++ b/tools/perf/util/cpumap.c
@@ -232,7 +232,7 @@ int cpu_map__get_socket(struct cpu_map *map, int idx)
char path[PATH_MAX];
int cpu, ret;

- if (idx > map->nr)
+ if ((idx > map->nr) || (idx < 0))
return -1;

cpu = map->map[idx];
@@ -296,7 +296,7 @@ int cpu_map__get_core(struct cpu_map *map, int idx)
char path[PATH_MAX];
int cpu, ret, s;

- if (idx > map->nr)
+ if ((idx > map->nr) || (idx < 0))
return -1;

cpu = map->map[idx];
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 2936b30..32094d3 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -974,6 +974,20 @@ int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
return 0;
}

+int perf_evsel__get_cpumap_index(int cpu, struct cpu_map *evsel_cpus)
+{
+ int i;
+
+ if (evsel_cpus == NULL)
+ return -1;
+
+ for (i = 0; i < evsel_cpus->nr; i++) {
+ if (cpu == evsel_cpus->map[i])
+ return i;
+ }
+ return -1;
+}
+
static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
{
struct perf_evsel *leader = evsel->leader;
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 4a7ed56..05e68a0 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -355,4 +355,6 @@ typedef int (*attr__fprintf_f)(FILE *, const char *, const char *, void *);
int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
attr__fprintf_f attr__fprintf, void *priv);

+int perf_evsel__get_cpumap_index(int cpu, struct cpu_map *evsel_cpus);
+
#endif /* __PERF_EVSEL_H */
--
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/