public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: kan.liang@intel.com
To: acme@kernel.org, jolsa@kernel.org
Cc: ak@linux.intel.com, namhyung@kernel.org, eranian@google.com,
	adrian.hunter@intel.com, dsahern@gmail.com,
	a.p.zijlstra@chello.nl, mingo@redhat.com,
	linux-kernel@vger.kernel.org, Kan Liang <kan.liang@intel.com>
Subject: [PATCH V2 2/3] perf,tools: check and re-organize evsel cpu maps
Date: Thu,  2 Jul 2015 03:08:44 -0400	[thread overview]
Message-ID: <1435820925-51091-2-git-send-email-kan.liang@intel.com> (raw)
In-Reply-To: <1435820925-51091-1-git-send-email-kan.liang@intel.com>

From: Kan Liang <kan.liang@intel.com>

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 be default on CPU0 and CPU18.
So the S0-C1 for uncore should show "<not counted>".

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@intel.com>
---

Changes since V1
 - Re-implement the way to find the common cpus
 - Add test case

 tools/perf/util/evlist.c | 72 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 67 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 6cfdee6..d67414f 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1101,6 +1101,66 @@ 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 int ncpus = cpu_map__nr(evlist->cpus);
+	int i = 0, j = 0, cpu_nr = 0, tmp;
+
+	/* 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. */
+	while ((i < cpu_map__nr(evlist->cpus)) &&
+	       (j < cpu_map__nr(evsel->cpus))) {
+
+		if (evlist->cpus->map[i] == evsel->cpus->map[j]) {
+			cpu_nr++;
+			i++;
+			j++;
+		} else if (evlist->cpus->map[i] > evsel->cpus->map[j]) {
+			evsel->cpus->map[j++] = -1;
+		} else
+			i++;
+
+	}
+	while (j < cpu_map__nr(evsel->cpus))
+		evsel->cpus->map[j++] = -1;
+
+	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, tmp = 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 +1168,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


  reply	other threads:[~2015-07-02 14:23 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-02  7:08 [PATCH V2 1/3] perf,tools: get correct cpu id for print_aggr kan.liang
2015-07-02  7:08 ` kan.liang [this message]
2015-07-15 21:14   ` [PATCH V2 2/3] perf,tools: check and re-organize evsel cpu maps Liang, Kan
2015-07-02  7:08 ` [PATCH V2 3/3] perf,tests: Add cpu_map tests kan.liang
2015-07-02 16:12   ` Jiri Olsa
2015-07-02 16:58     ` Liang, Kan
2015-07-02 16:13 ` [PATCH V2 1/3] perf,tools: get correct cpu id for print_aggr Jiri Olsa
2015-08-28 13:33   ` Liang, Kan
2015-08-28 14:23     ` acme
2015-08-28 14:39       ` Liang, Kan
2015-08-28 14:45         ` Stephane Eranian
2015-08-28 14:47         ` Arnaldo Carvalho de Melo
2015-08-28 14:51           ` Liang, Kan
2015-08-31  8:27 ` [tip:perf/urgent] perf stat: Get " tip-bot for Kan Liang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1435820925-51091-2-git-send-email-kan.liang@intel.com \
    --to=kan.liang@intel.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=dsahern@gmail.com \
    --cc=eranian@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox