Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1236813

[PATCH 16/16] perf tools: By default use the most precise "cycles" hw counter available

From Arnaldo Carvalho de Melo <acme@kernel.org>
Newsgroups linux.kernel
Subject [PATCH 16/16] perf tools: By default use the most precise "cycles" hw counter available
Date 2015-10-01 00:00 +0200
Message-ID <qexzR-LW-29@gated-at.bofh.it> (permalink)
References <qexzQ-LW-5@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


From: Arnaldo Carvalho de Melo <acme@redhat.com>

If the user doesn't specify any event, try the most precise "cycles"
available, i.e. start by "cycles:ppp" and go on removing "p" till it
works.

E.g.

  $ perf record usleep 1
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 0.017 MB perf.data (11 samples) ]
  $ perf evlist
  cycles:pp
  $ perf evlist -v
  cycles:pp: size: 112, { sample_period, sample_freq }: 4000, sample_type:
  IP|TID|TIME|PERIOD, disabled: 1, inherit: 1, mmap: 1, comm: 1, freq: 1,
  enable_on_exec: 1, task: 1, precise_ip: 2, sample_id_all: 1,
  exclude_guest: 1, mmap2: 1, comm_exec: 1
  $ grep 'model name' /proc/cpuinfo | head -1
  model name	: Intel(R) Core(TM) i7-3667U CPU @ 2.00GHz
  $

When 'cycles' appears explicitely is specified this will not be tried,
i.e. the user has full control of the level of precision to be used:

  $ perf record -e cycles usleep 1
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 0.016 MB perf.data (9 samples) ]
  $ perf evlist
  cycles
  $ perf evlist -v
  cycles: size: 112, { sample_period, sample_freq }: 4000, sample_type:
  IP|TID|TIME|PERIOD, disabled: 1, inherit: 1, mmap: 1, comm: 1, freq: 1,
  enable_on_exec: 1, task: 1, sample_id_all: 1, exclude_guest: 1, mmap2:
  1, comm_exec: 1
  $

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Chandler Carruth <chandlerc@gmail.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Wang Nan <wangnan0@huawei.com>
Link: https://www.youtube.com/watch?v=nXaxk27zwlk
Link: http://lkml.kernel.org/n/tip-b1ywebmt22pi78vjxau01wth@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/evlist.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 89546228b8ed..e7e195d867ea 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -205,6 +205,20 @@ void perf_evlist__set_leader(struct perf_evlist *evlist)
 	}
 }
 
+static void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr)
+{
+	attr->precise_ip = 3;
+
+	while (attr->precise_ip != 0) {
+		int fd = sys_perf_event_open(attr, 0, -1, -1, 0);
+		if (fd != -1) {
+			close(fd);
+			break;
+		}
+		--attr->precise_ip;
+	}
+}
+
 int perf_evlist__add_default(struct perf_evlist *evlist)
 {
 	struct perf_event_attr attr = {
@@ -215,13 +229,15 @@ int perf_evlist__add_default(struct perf_evlist *evlist)
 
 	event_attr_init(&attr);
 
+	perf_event_attr__set_max_precise_ip(&attr);
+
 	evsel = perf_evsel__new(&attr);
 	if (evsel == NULL)
 		goto error;
 
-	/* use strdup() because free(evsel) assumes name is allocated */
-	evsel->name = strdup("cycles");
-	if (!evsel->name)
+	/* use asprintf() because free(evsel) assumes name is allocated */
+	if (asprintf(&evsel->name, "cycles%.*s",
+		     attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
 		goto error_free;
 
 	perf_evlist__add(evlist, evsel);
-- 
2.1.0

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

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[GIT PULL 00/16] perf/core improvements and fixes Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:00 +0200
  [PATCH 15/16] perf list: Remove blank lines, headers when piping output Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:00 +0200
  [PATCH 12/16] perf probe: Show correct source lines of probes on kmodules Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:00 +0200
  [PATCH 16/16] perf tools: By default use the most precise "cycles" hw counter available Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:00 +0200
  [PATCH 02/16] perf tools: Fix shadowed declaration in parse-events.c Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:00 +0200
  [PATCH 09/16] tools lib symbol: Introduce kallsyms2elf_type Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:00 +0200
  [PATCH 04/16] perf report: Amend documentation about max_stack and synthesized callchains Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:00 +0200
  [PATCH 03/16] perf maps: Introduce maps__find_symbol_by_name() Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:00 +0200
  [PATCH 01/16] tools: Fix shadowed declaration in err.h Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:00 +0200
  [PATCH 07/16] perf machine: Add method for common kernel_map(FUNCTION) operation Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-10-01 00:10 +0200
  Re: [GIT PULL 00/16] perf/core improvements and fixes Ingo Molnar <mingo@kernel.org> - 2015-10-01 09:10 +0200

csiph-web