[PATCH 2/2] perf,tools: check and re-organize evsel cpu maps

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


From: Kan Liang <kan.liang@xxxxxxxxx>

Some PMU events have cpumask, e.g uncore events. The cpu list set by
user may be incompatible with event's cpumask.
This patch will check the user defined cpu list. If the incompatible cpu
is found, it will warn the user and discard the incompatible cpu. Only
available cpu can be stored in evsel->cpus->map. If there is no cpu from
cpu list compatible with event's cpumask. It will error out.

Here is an example.
According to cpumask, uncore should only available on CPU0 and CPU18.
So the S0-C1 for uncore should not count.

Without this patch
$ sudo ./perf stat -e cycles,uncore_imc_0/cas_count_read/ -C0,1,18
--per-core sleep 2

Performance counter stats for 'CPU(s) 0,1,18':

S0-C0 1 6749638 cycles
S0-C0 1 0.83 MiB uncore_imc_0/cas_count_read/
(100.00%)
S0-C1 1 232421 cycles
S0-C1 1 0.83 MiB uncore_imc_0/cas_count_read/
S1-C0 1 236997 cycles
S1-C0 1 0.35 MiB uncore_imc_0/cas_count_read/

2.001094019 seconds time elapsed

With this patch
$ perf stat -e cycles,uncore_imc_0/cas_count_read/ -C0,1,18 --per-core
sleep 2
event uncore_imc_0/cas_count_read/ can only be monitored on CPU 0 18.
Other CPUs will be discard.

Performance counter stats for 'CPU(s) 0,1,18':

S0-C0 1 5557406 cycles
S0-C0 1 0.21 MiB uncore_imc_0/cas_count_read/
S0-C1 1 1012534 cycles
S0-C1 0 <not counted> MiB uncore_imc_0/cas_count_read/
S1-C0 1 916130 cycles
S1-C0 1 0.08 MiB uncore_imc_0/cas_count_read/

2.001110843 seconds time elapsed

Signed-off-by: Kan Liang <kan.liang@xxxxxxxxx>
---
tools/perf/util/evlist.c | 77 ++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 72 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 6cfdee6..f179379 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1101,6 +1101,71 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
}

+static int cmp_ids(const void *a, const void *b)
+{
+ return *(int *)a - *(int *)b;
+}
+
+static int perf_evlist__check_evsel_cpus(struct perf_evlist *evlist, struct perf_evsel *evsel)
+{
+ const struct cpu_map *cpus = evlist->cpus;
+ const int ncpus = cpu_map__nr(evlist->cpus);
+ int j = 0, cpu_nr = 0, tmp = 0;
+ int i;
+
+ /* ensure we process id in increasing order */
+ qsort(evlist->cpus->map, evlist->cpus->nr, sizeof(int), cmp_ids);
+
+ /* find the common cpus between evsel and evlist. */
+ for (i = 0; i < cpu_map__nr(evsel->cpus);) {
+
+ if (j >= ncpus) {
+ evsel->cpus->map[i++] = -1;
+ continue;
+ }
+ for (; j < ncpus; j++) {
+ if (cpus->map[j] < evsel->cpus->map[i])
+ continue;
+ if (cpus->map[j] == evsel->cpus->map[i]) {
+ cpu_nr++;
+ j++;
+ i++;
+ } else
+ evsel->cpus->map[i++] = -1;
+ break;
+ }
+ }
+
+ if (cpu_nr == 0) {
+ pr_warning("event %s cannot be monitored on the given cpus."
+ "Please check cpumask\n", evsel->name);
+ return -1;
+ }
+
+ if (ncpus > cpu_nr)
+ pr_warning("event %s can only be monitored on CPU", evsel->name);
+
+ /* order evsel cpus */
+ for (i = 0; i < cpu_nr; i++) {
+ if (evsel->cpus->map[i] == -1) {
+ while (evsel->cpus->map[tmp] == -1) {
+ tmp++;
+ BUG_ON(tmp >= cpu_map__nr(evsel->cpus));
+ }
+ evsel->cpus->map[i] = evsel->cpus->map[tmp];
+ evsel->cpus->map[tmp] = -1;
+ }
+ if (ncpus > cpu_nr)
+ pr_warning(" %d", evsel->cpus->map[i]);
+ tmp++;
+ }
+ evsel->cpus->nr = cpu_nr;
+ if (ncpus > cpu_nr)
+ pr_warning(". Other CPUs will be discard.\n");
+
+ return 0;
+}
+
static int perf_evlist__propagate_maps(struct perf_evlist *evlist,
struct target *target)
{
@@ -1108,13 +1173,15 @@ static int perf_evlist__propagate_maps(struct perf_evlist *evlist,

evlist__for_each(evlist, evsel) {
/*
- * We already have cpus for evsel (via PMU sysfs) so
- * keep it, if there's no target cpu list defined.
+ * We already have cpus for evsel (via PMU sysfs)
+ * and target cpu list defined, check if they are
+ * compatible. If not, discard incompatible cpus.
*/
- if (evsel->cpus && target->cpu_list)
- cpu_map__put(evsel->cpus);
+ if (evsel->cpus && target->cpu_list &&
+ perf_evlist__check_evsel_cpus(evlist, evsel))
+ return -EINVAL;

- if (!evsel->cpus || target->cpu_list)
+ if (!evsel->cpus)
evsel->cpus = cpu_map__get(evlist->cpus);

evsel->threads = thread_map__get(evlist->threads);
--
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/