Skip to contentSkip to conversation

perf tools: Record total sampling time

PATCH In review Namhyung Kim 2 participants 5 messages Dec 3, 2013 – Dec 9, 2013 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v2 1/3 perf tools: Record total sampling time

From: Namhyung Kim <[email protected]>

It's sometimes useful to see total sampling or elapsed time with normal performance result. To do that, record first and last sample time for each evsel and to display it in the header (--stdio only for now).

  $ perf record -a sleep 1
  $ perf report --stdio
  ...
  # Samples: 4K of event 'cycles'
  # Event count (approx.): 4087481688
  # Total sampling time  : 1.001260 (sec)
  #
  # Overhead       Command               Shared Object                   Symbol
  # ........  ............  ..........................  .......................
  #
      89.06%      nautilus  libgobject-2.0.so.0.3701.0  [.] 0x0000000000020bf0
       1.79%   kworker/1:0  [kernel.kallsyms]           [k] generic_exec_single
       1.75%       swapper  [kernel.kallsyms]           [k] intel_idle
       1.14%      nautilus  [unknown]                   [.] 0x0000003153f2ff61
       0.69%   kworker/0:1  [kernel.kallsyms]           [k] generic_exec_single
       0.67%      nautilus  ld-2.17.so                  [.] do_lookup_x
---
 tools/perf/builtin-report.c | 15 ++++++++++++++-
 tools/perf/util/evsel.h     |  2 ++
 tools/perf/util/hist.h      |  1 +
 tools/perf/util/session.c   |  7 +++++++
 4 files changed, 24 insertions(+), 1 deletion(-)
tools/perf/builtin-report.c +14 -1
index 8cf8e66ba594..1d47fbec4421 100644
@@ -456,6 +456,14 @@ static size_t hists__fprintf_nr_sample_events(struct perf_report *rep,
 		ret += fprintf(fp, "\n# Sort order   : %s", sort_order);
 	} else
 		ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
+	if (rep->tool.ordered_samples) {
+		u64 total_time = hists->stats.total_time;
+		u64 sec = total_time / NSEC_PER_SEC;
+		u64 usec = (total_time - sec * NSEC_PER_SEC) / NSEC_PER_USEC;
+
+		ret += fprintf(fp, "\n# Total sampling time  : "
+			       "%" PRIu64 ".%06" PRIu64 " (sec)", sec, usec);
+	}
 	return ret + fprintf(fp, "\n#\n");
 }
 
@@ -565,8 +573,13 @@ static int __cmd_report(struct perf_report *rep)
 	}
 
 	nr_samples = 0;
-	list_for_each_entry(pos, &session->evlist->entries, node)
+	list_for_each_entry(pos, &session->evlist->entries, node) {
 		nr_samples += pos->hists.nr_entries;
+		if (rep->tool.ordered_samples) {
+			pos->hists.stats.total_time = pos->last_timestamp -
+							pos->first_timestamp;
+		}
+	}
 
 	ui_progress__init(&prog, nr_samples, "Merging related events...");
 
tools/perf/util/evsel.h +2 -0
index 8120eeb86ac1..20a7c653b74b 100644
@@ -67,6 +67,8 @@ struct perf_evsel {
 	int			idx;
 	u32			ids;
 	struct hists		hists;
+	u64			first_timestamp;
+	u64			last_timestamp;
 	char			*name;
 	double			scale;
 	const char		*unit;
tools/perf/util/hist.h +1 -0
index b621347a1585..bc5acdfc2b4b 100644
@@ -28,6 +28,7 @@ struct symbol;
  */
 struct events_stats {
 	u64 total_period;
+	u64 total_time;
 	u64 total_lost;
 	u64 total_invalid_chains;
 	u32 nr_events[PERF_RECORD_HEADER_MAX];
tools/perf/util/session.c +7 -0
index 4ce146bae552..e4b158f0586a 100644
@@ -925,6 +925,13 @@ perf_session__deliver_sample(struct perf_session *session,
 	u64 sample_type = evsel->attr.sample_type;
 	u64 read_format = evsel->attr.read_format;
 
+	if (tool->ordered_samples) {
+		if (evsel->first_timestamp == 0)
+			evsel->first_timestamp = sample->time;
+
+		evsel->last_timestamp = sample->time;
+	}
+
 	/* Standard sample delievery. */
 	if (!(sample_type & PERF_SAMPLE_READ))
 		return tool->sample(tool, event, sample, evsel, machine);
Signature · 1 line
1.7.11.7
author maintainer
#1 lore raw

PATCH v2 2/3 perf tools: Record sampling time for each entry

From: Namhyung Kim <[email protected]>

Sometimes users might want see a relation between value and time of a sample. And the time itself is a very important information to have. As the sample->period value is a diff of counter values between samples it might make sense to add time-diff between samples into a hist entry.

To do that, calculate elapsed time for each sample and record it. The elapsed time is the diff between current sample->time and previous sample->time which was saved for each evsel.

Maybe we can use PERF_SAMPLE_READ for the precise result.

---
 tools/perf/builtin-annotate.c |  2 +-
 tools/perf/builtin-diff.c     |  2 +-
 tools/perf/builtin-report.c   |  9 +++++----
 tools/perf/builtin-top.c      |  2 +-
 tools/perf/tests/hists_link.c |  4 ++--
 tools/perf/util/evsel.h       |  1 +
 tools/perf/util/hist.c        | 10 ++++++++--
 tools/perf/util/hist.h        |  2 +-
 tools/perf/util/session.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/sort.h        |  1 +
 10 files changed, 63 insertions(+), 12 deletions(-)
tools/perf/builtin-annotate.c +1 -1
index 4087ab19823c..dc43a64bf723 100644
@@ -65,7 +65,7 @@ static int perf_evsel__add_sample(struct perf_evsel *evsel,
 		return 0;
 	}
 
-	he = __hists__add_entry(&evsel->hists, al, NULL, NULL, NULL, 1, 1, 0);
+	he = __hists__add_entry(&evsel->hists, al, NULL, NULL, NULL, 1, 0, 1, 0);
 	if (he == NULL)
 		return -ENOMEM;
 
tools/perf/builtin-diff.c +1 -1
index 3b67ea2444bd..85aa961a647e 100644
@@ -307,7 +307,7 @@ static int hists__add_entry(struct hists *hists,
 			    struct addr_location *al, u64 period,
 			    u64 weight, u64 transaction)
 {
-	if (__hists__add_entry(hists, al, NULL, NULL, NULL, period, weight,
+	if (__hists__add_entry(hists, al, NULL, NULL, NULL, period, 0, weight,
 			       transaction) != NULL)
 		return 0;
 	return -ENOMEM;
tools/perf/builtin-report.c +5 -4
index 1d47fbec4421..eb849e9f7093 100644
@@ -116,7 +116,7 @@ static int perf_report__add_mem_hist_entry(struct perf_tool *tool,
 	 * and the he_stat__add_period() function.
 	 */
 	he = __hists__add_entry(&evsel->hists, al, parent, NULL, mi,
-				cost, cost, 0);
+				cost, 0, cost, 0);
 	if (!he)
 		return -ENOMEM;
 
@@ -210,7 +210,7 @@ static int perf_report__add_branch_hist_entry(struct perf_tool *tool,
 		 * and not events sampled. Thus we use a pseudo period of 1.
 		 */
 		he = __hists__add_entry(&evsel->hists, al, parent, &bi[i], NULL,
-					1, 1, 0);
+					1, 0, 1, 0);
 		if (he) {
 			struct annotation *notes;
 			bx = he->branch_info;
@@ -272,8 +272,9 @@ static int perf_evsel__add_hist_entry(struct perf_tool *tool,
 	}
 
 	he = __hists__add_entry(&evsel->hists, al, parent, NULL, NULL,
-				sample->period, sample->weight,
-				sample->transaction);
+				sample->period,
+				tool->ordered_samples ? sample->read.time_enabled : 0,
+				sample->weight, sample->transaction);
 	if (he == NULL)
 		return -ENOMEM;
 
tools/perf/builtin-top.c +1 -1
index 03d37a76c612..175bde2a0ece 100644
@@ -247,7 +247,7 @@ static struct hist_entry *perf_evsel__add_hist_entry(struct perf_evsel *evsel,
 
 	pthread_mutex_lock(&evsel->hists.lock);
 	he = __hists__add_entry(&evsel->hists, al, NULL, NULL, NULL,
-				sample->period, sample->weight,
+				sample->period, 0, sample->weight,
 				sample->transaction);
 	pthread_mutex_unlock(&evsel->hists.lock);
 	if (he == NULL)
tools/perf/tests/hists_link.c +2 -2
index 173bf42cc03e..7bb952e6be62 100644
@@ -223,7 +223,7 @@ static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine)
 				goto out;
 
 			he = __hists__add_entry(&evsel->hists, &al, NULL,
-						NULL, NULL, 1, 1, 0);
+						NULL, NULL, 1, 0, 1, 0);
 			if (he == NULL)
 				goto out;
 
@@ -246,7 +246,7 @@ static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine)
 				goto out;
 
 			he = __hists__add_entry(&evsel->hists, &al, NULL,
-						NULL, NULL, 1, 1, 0);
+						NULL, NULL, 1, 0, 1, 0);
 			if (he == NULL)
 				goto out;
 
tools/perf/util/evsel.h +1 -0
index 20a7c653b74b..ac65fc67972c 100644
@@ -69,6 +69,7 @@ struct perf_evsel {
 	struct hists		hists;
 	u64			first_timestamp;
 	u64			last_timestamp;
+	u64			*prev_timestamps;
 	char			*name;
 	double			scale;
 	const char		*unit;
tools/perf/util/hist.c +8 -2
index 6ea46ec6593e..8b66921fc1bd 100644
@@ -204,11 +204,12 @@ static void hist_entry__add_cpumode_period(struct hist_entry *he,
 }
 
 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
-				u64 weight)
+				u64 sample_time, u64 weight)
 {
 
 	he_stat->period		+= period;
 	he_stat->weight		+= weight;
+	he_stat->time		+= sample_time;
 	he_stat->nr_events	+= 1;
 }
 
@@ -221,10 +222,12 @@ static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
 	dest->period_guest_us	+= src->period_guest_us;
 	dest->nr_events		+= src->nr_events;
 	dest->weight		+= src->weight;
+	dest->time		+= src->time;
 }
 
 static void hist_entry__decay(struct hist_entry *he)
 {
+	he->stat.time = (he->stat.time * 7) / 8;
 	he->stat.period = (he->stat.period * 7) / 8;
 	he->stat.nr_events = (he->stat.nr_events * 7) / 8;
 	/* XXX need decay for weight too? */
@@ -366,6 +369,7 @@ static struct hist_entry *add_hist_entry(struct hists *hists,
 
 		if (!cmp) {
 			he_stat__add_period(&he->stat, entry->stat.period,
+					    entry->stat.time,
 					    entry->stat.weight);
 
 			/*
@@ -411,7 +415,8 @@ struct hist_entry *__hists__add_entry(struct hists *hists,
 				      struct symbol *sym_parent,
 				      struct branch_info *bi,
 				      struct mem_info *mi,
-				      u64 period, u64 weight, u64 transaction)
+				      u64 period, u64 sample_time,
+				      u64 weight, u64 transaction)
 {
 	struct hist_entry entry = {
 		.thread	= al->thread,
@@ -427,6 +432,7 @@ struct hist_entry *__hists__add_entry(struct hists *hists,
 			.nr_events = 1,
 			.period	= period,
 			.weight = weight,
+			.time = sample_time,
 		},
 		.parent = sym_parent,
 		.filtered = symbol__parent_filter(sym_parent),
tools/perf/util/hist.h +1 -1
index bc5acdfc2b4b..0433469812dc 100644
@@ -89,7 +89,7 @@ struct hist_entry *__hists__add_entry(struct hists *hists,
 				      struct addr_location *al,
 				      struct symbol *parent,
 				      struct branch_info *bi,
-				      struct mem_info *mi, u64 period,
+				      struct mem_info *mi, u64 period, u64 time,
 				      u64 weight, u64 transaction);
 int64_t hist_entry__cmp(struct hist_entry *left, struct hist_entry *right);
 int64_t hist_entry__collapse(struct hist_entry *left, struct hist_entry *right);
tools/perf/util/session.c +42 -0

48 lines not on this page · raw message

tools/perf/util/sort.h +1 -0
index 43e5ff42a609..16aaf0a47346 100644
@@ -51,6 +51,7 @@ struct he_stat {
 	u64			period_guest_sys;
 	u64			period_guest_us;
 	u64			weight;
+	u64			time;
 	u32			nr_events;
 };
 
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [PATCH v2 2/3] perf tools: Record sampling time for each entry

tools/perf/util/evsel.h struct perf_evsel { · line 72

Quoted 9 lines · Namhyung Kim
tools/perf/util/evsel.h +1 -0
index 20a7c653b74b..ac65fc67972c 100644
@@ -69,6 +69,7 @@ struct perf_evsel {
  	struct hists		hists;
  	u64			first_timestamp;
  	u64			last_timestamp;
+	u64			*prev_timestamps;

Why plural and why dynamically allocated? The allocation only does a single u64, not an array.

David

author maintainer
#3 lore raw

Re: [PATCH v2 2/3] perf tools: Record sampling time for each entry

Hi David,

Quoted 14 lines · David Ahern

tools/perf/util/evsel.h struct perf_evsel { · line 72

Quoted 9 lines · Namhyung Kim
tools/perf/util/evsel.h +1 -0
index 20a7c653b74b..ac65fc67972c 100644
@@ -69,6 +69,7 @@ struct perf_evsel {
  	struct hists		hists;
  	u64			first_timestamp;
  	u64			last_timestamp;
+	u64			*prev_timestamps;

Why plural and why dynamically allocated? The allocation only does a single u64, not an array.

Nope, it'll be an array if the session was a system-wide one, so plural.

But, I think the current code won't work well if there're multiple unrelated processes recorded - e.g. perf record -u id -u - since it'll intermix all timestamps between the samples regardless of process.

Hmm.. I think there's not much thing we can do for this without help from kernel side (PERF_SAMPLE_READ?). So I'll just drop this unless I can come up with a better idea. But the patch 1/3 still makes some sense and worth to merge by itself IMHO.

What do you think?

Thanks,
Namhyung

author maintainer
#4 lore raw

PATCH v2 3/3 perf report: Add --event-time option

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

The --event-time option is for displaying elapsed sampling time information for each entry.

  $ perf record -- perf bench sched messaging
  $ perf report --stdio --event-time
  ...
  # Samples: 10K of event 'cycles'
  # Event count (approx.): 5118793536
  # Total sampling time  : 0.184277 (sec)
  #
  # Overhead        Time          Command      Shared Object                       Symbol
  # ........  ..........  ...............  .................  ...........................
  #
       7.26%    0.007413  sched-messaging  [unknown]          [.] 0x0000003153e7a6e7
       4.79%    0.006088  sched-messaging  [kernel.kallsyms]  [k] avc_has_perm_flags
       4.38%    0.004809  sched-messaging  [kernel.kallsyms]  [k] copy_user_generic_string
       3.81%    0.005457  sched-messaging  [kernel.kallsyms]  [k] _raw_spin_lock
       2.92%    0.002745  sched-messaging  [kernel.kallsyms]  [k] _raw_spin_lock_irqsave
       2.72%    0.002896  sched-messaging  [kernel.kallsyms]  [k] unix_stream_recvmsg
       2.55%    0.003745  sched-messaging  [kernel.kallsyms]  [k] __slab_free
       2.45%    0.002807  sched-messaging  [kernel.kallsyms]  [k] page_fault
       2.08%    0.002258  sched-messaging  [kernel.kallsyms]  [k] sock_has_perm
---
 tools/perf/Documentation/perf-report.txt |  6 +++
 tools/perf/builtin-report.c              |  2 +
 tools/perf/ui/hist.c                     | 68 ++++++++++++++++++++++++++------
 tools/perf/util/hist.h                   |  1 +
 tools/perf/util/symbol.h                 |  3 +-
 5 files changed, 68 insertions(+), 12 deletions(-)
tools/perf/Documentation/perf-report.txt +6 -0
index 10a279871251..f420eef2b77f 100644
@@ -237,6 +237,12 @@ OPTIONS
 	Do not show entries which have an overhead under that percent.
 	(Default: 0).
 
+--event-time::
+	Show a column with elapsed sample times.  It could be useful when you
+	want to see a relation between time and period of samples.  The elapsed
+	time is a sum of time-diff between two adjacent samples of a event as
+	sample period is a diff of counter values between the two samples.
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-annotate[1]
tools/perf/builtin-report.c +2 -0
index eb849e9f7093..c4e958bc96a7 100644
@@ -898,6 +898,8 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
 	OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
 	OPT_CALLBACK(0, "percent-limit", &report, "percent",
 		     "Don't show entries under that percent", parse_percent_limit),
+	OPT_BOOLEAN(0, "event-time", &symbol_conf.show_event_time,
+		    "Show a column with the elapsed time"),
 	OPT_END()
 	};
 	struct perf_data_file file = {
tools/perf/ui/hist.c +57 -11

152 lines not on this page · raw message

tools/perf/util/hist.h +1 -0
index 0433469812dc..17e89fb31db9 100644
@@ -162,6 +162,7 @@ enum {
 	PERF_HPP__OVERHEAD_GUEST_US,
 	PERF_HPP__SAMPLES,
 	PERF_HPP__PERIOD,
+	PERF_HPP__TIME,
 
 	PERF_HPP__MAX_INDEX
 };
tools/perf/util/symbol.h +2 -1
index f1031a1358a1..599892b75cbc 100644
@@ -99,7 +99,8 @@ struct symbol_conf {
 			annotate_asm_raw,
 			annotate_src,
 			event_group,
-			demangle;
+			demangle,
+			show_event_time;
 	const char	*vmlinux_name,
 			*kallsyms_name,
 			*source_prefix,
Signature · 1 line
1.7.11.7