Skip to contentSkip to conversation

Stitch LBR call stack

PATCH Superseded [email protected] 3 participants 18 messages Oct 7, 2019 – Oct 8, 2019

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover Stitch LBR call stack

From: Kan Liang <[email protected]>

Start from Haswell, Linux perf can utilize the existing Last Branch Record (LBR) facility to record call stack. However, the depth of the reconstructed LBR call stack limits to the number of LBR registers. E.g. on skylake, the depth of reconstructed LBR call stack is <= 32 That's because HW will overwrite the oldest LBR registers when it's full.

However, the overwritten LBRs may still be retrieved from previous sample. At that moment, HW hasn't overwritten the LBR registers yet. Perf tools can stitch those overwritten LBRs on current call stacks to get a more complete call stack.

To determine if LBRs can be stitched, the physical index of LBR registers is required. A new sample type is introduced in patch 1 & 2 to dump the LBR Top-of-Stack (TOS) information for perf tools. Besides, the maximum number of LBRs is required as well. Patch 3 & 4 retrieve the capabilities information from sysfs and save them in perf header.
Patch 5 & 6 implements the LBR stitching approach.
Users can use the options introduced in patch 7-10 to enable the LBR stitching approach for perf report, script, top and c2c.

The stitching approach base on LBR call stack technology. The known limitations of LBR call stack technology still apply to the approach, e.g. Exception handing such as setjmp/longjmp will have calls/returns not match.
This approach is not full proof. There can be cases where it creates incorrect call stacks from incorrect matches. There is no attempt to validate any matches in another way. So it is not enabled by default. However in many common cases with call stack overflows it can recreate better call stacks than the default lbr call stack output. So if there are problems with LBR overflows this is a possible workaround.

Performance impact:
The processing time may increase with the LBR stitching approach enabled. The impact depends on the number of samples with stitched LBRs.

For sqlite's tcltest,
perf record --call-graph lbr -- make tcltest
perf report --stitch-lbr

There are 4.11% samples has stitched LBRs.
Total number of samples: 2833728
The number of samples with stitched LBRs 116478

The processing time of perf report increases 6.8%

Without --stitch-lbr:                           55906106 usec
With --stitch-lbr:                              59728701 usec

For a simple test case tchain_edit with 43 depth of call stacks. perf record --call-graph lbr -- ./tchain_edit
perf report --stitch-lbr

There are 99.9% samples has stitched LBRs.
Total number of samples: 10915
The number of samples with stitched LBRs 10905

The processing time of perf report increases 67.4%

Without --stitch-lbr:                           11970508 usec
With --stitch-lbr:                              20036055 usec

The source code of tchain_edit.c is something like as below. noinline void f43(void)

{
        int i;
        for (i = 0; i < 10000;) {

                if(i%2)
                        i++;
                else
                        i++;
        }
}

noinline void f42(void)

{
        int i;
        for (i = 0; i < 100; i++) {
                f43();
                f43();
                f43();
        }
}

noinline void f41(void)

{
        int i;
        for (i = 0; i < 100; i++) {
                f42();
                f42();
                f42();
        }
}

noinline void f40(void)

{
        f41();
}

... ...

noinline void f32(void)

{
        f33();
}

noinline void f31(void)

{
        int i;

        for (i = 0; i < 10000; i++) {
                if(i%2)
                        i++;
                else
                        i++;
        }

        f32();
}

noinline void f30(void)

{
        f31();
}

... ...

noinline void f1(void)

{
        f2();
}

int main()

{
        f1();
}

Kan Liang (10):
perf/core, x86: Add PERF_SAMPLE_LBR_TOS
perf tools: Support PERF_SAMPLE_LBR_TOS
perf pmu: Add support for PMU capabilities
perf header: Support CPU PMU capabilities
perf machine: Refine the function for LBR call stack reconstruction perf tools: Stitch LBR call stack
perf report: Add option to enable the LBR stitching approach perf script: Add option to enable the LBR stitching approach perf top: Add option to enable the LBR stitching approach perf c2c: Add option to enable the LBR stitching approach

 arch/x86/events/intel/lbr.c                   |   9 +
 include/linux/perf_event.h                    |   1 +
 include/uapi/linux/perf_event.h               |   4 +-
 kernel/events/core.c                          |  12 +
 tools/include/uapi/linux/perf_event.h         |   4 +-
 tools/perf/Documentation/perf-c2c.txt         |  11 +
 tools/perf/Documentation/perf-report.txt      |  11 +
 tools/perf/Documentation/perf-script.txt      |  11 +
 tools/perf/Documentation/perf-top.txt         |   9 +
 .../Documentation/perf.data-file-format.txt   |  16 +
 tools/perf/builtin-c2c.c                      |   6 +
 tools/perf/builtin-record.c                   |   3 +
 tools/perf/builtin-report.c                   |   6 +
 tools/perf/builtin-script.c                   |   6 +
 tools/perf/builtin-stat.c                     |   1 +
 tools/perf/builtin-top.c                      |  11 +
 tools/perf/util/branch.h                      |  10 +-
 tools/perf/util/env.h                         |   3 +
 tools/perf/util/event.h                       |   1 +
 tools/perf/util/evsel.c                       |  16 +-
 tools/perf/util/evsel.h                       |   1 +
 tools/perf/util/header.c                      | 110 +++++++
 tools/perf/util/header.h                      |   1 +
 tools/perf/util/machine.c                     | 303 ++++++++++++++----
 tools/perf/util/perf_event_attr_fprintf.c     |   2 +-
 tools/perf/util/pmu.c                         |  87 +++++
 tools/perf/util/pmu.h                         |  12 +
 tools/perf/util/synthetic-events.c            |   8 +
 tools/perf/util/thread.c                      |   3 +
 tools/perf/util/thread.h                      |  18 ++
 tools/perf/util/top.h                         |   1 +
 31 files changed, 626 insertions(+), 71 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/10 perf/core, x86: Add PERF_SAMPLE_LBR_TOS

From: Kan Liang <[email protected]>

In LBR call stack mode, the depth of reconstructed LBR call stack limits to the number of LBR registers. With LBR Top-of-Stack (TOS) information, perf tool may stitch the stacks of two samples. The reconstructed LBR call stack can break the HW limitation.

Add a new sample type for LBR TOS.

PEBS record doesn't store TOS information. For single PEBS, TOS can be directly read from MSR, because the PMI is triggered immediately after PEBS is written. TOS MSR is still unchanged.
For large PEBS, TOS MSR has stale value. Set -1ULL to indicate that the TOS information is not available.

---
 arch/x86/events/intel/lbr.c     |  9 +++++++++
 include/linux/perf_event.h      |  1 +
 include/uapi/linux/perf_event.h |  4 +++-
 kernel/events/core.c            | 12 ++++++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/events/intel/lbr.c +9 -0
index ea54634eabf3..4640ff1c9ecb 100644
@@ -562,6 +562,7 @@ static void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
 		cpuc->lbr_entries[i].reserved	= 0;
 	}
 	cpuc->lbr_stack.nr = i;
+	cpuc->lbr_stack.tos = tos;
 }
 
 /*
@@ -657,6 +658,7 @@ static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
 		out++;
 	}
 	cpuc->lbr_stack.nr = out;
+	cpuc->lbr_stack.tos = tos;
 }
 
 void intel_pmu_lbr_read(void)
@@ -1097,6 +1099,13 @@ void intel_pmu_store_pebs_lbrs(struct pebs_lbr *lbr)
 	int i;
 
 	cpuc->lbr_stack.nr = x86_pmu.lbr_nr;
+
+	/* Cannot get TOS for large PEBS */
+	if (cpuc->n_pebs == cpuc->n_large_pebs)
+		cpuc->lbr_stack.tos = -1ULL;
+	else
+		cpuc->lbr_stack.tos = intel_pmu_lbr_tos();
+
 	for (i = 0; i < x86_pmu.lbr_nr; i++) {
 		u64 info = lbr->lbr[i].info;
 		struct perf_branch_entry *e = &cpuc->lbr_entries[i];
include/linux/perf_event.h +1 -0
index 61448c19a132..ee9ef0c4cb08 100644
@@ -100,6 +100,7 @@ struct perf_raw_record {
  */
 struct perf_branch_stack {
 	__u64				nr;
+	__u64				tos;
 	struct perf_branch_entry	entries[0];
 };
 
include/uapi/linux/perf_event.h +3 -1
index bb7b271397a6..fe36ebb7dc2e 100644
@@ -141,8 +141,9 @@ enum perf_event_sample_format {
 	PERF_SAMPLE_TRANSACTION			= 1U << 17,
 	PERF_SAMPLE_REGS_INTR			= 1U << 18,
 	PERF_SAMPLE_PHYS_ADDR			= 1U << 19,
+	PERF_SAMPLE_LBR_TOS			= 1U << 20,
 
-	PERF_SAMPLE_MAX = 1U << 20,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 21,		/* non-ABI */
 
 	__PERF_SAMPLE_CALLCHAIN_EARLY		= 1ULL << 63, /* non-ABI; internal use */
 };
@@ -864,6 +865,7 @@ enum perf_event_type {
 	 *	{ u64			abi; # enum perf_sample_regs_abi
 	 *	  u64			regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
 	 *	{ u64			phys_addr;} && PERF_SAMPLE_PHYS_ADDR
+	 *	{ u64			tos;} && PERF_SAMPLE_LBR_TOS
 	 * };
 	 */
 	PERF_RECORD_SAMPLE			= 9,
kernel/events/core.c +12 -0
index 275eae05af20..6ab0913c7b36 100644
@@ -6468,6 +6468,15 @@ void perf_output_sample(struct perf_output_handle *handle,
 	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
 		perf_output_put(handle, data->phys_addr);
 
+	if (sample_type & PERF_SAMPLE_LBR_TOS) {
+		u64 tos = -1ULL;
+
+		if (data->br_stack)
+			tos = data->br_stack->tos;
+
+		perf_output_put(handle, tos);
+	}
+
 	if (!event->attr.watermark) {
 		int wakeup_events = event->attr.wakeup_events;
 
@@ -6656,6 +6665,9 @@ void perf_prepare_sample(struct perf_event_header *header,
 
 	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
 		data->phys_addr = perf_virt_to_phys(data->addr);
+
+	if (sample_type & PERF_SAMPLE_LBR_TOS)
+		header->size += sizeof(u64);
 }
 
 static __always_inline int
Signature · 1 line
2.17.1
#9 lore raw

PATCH 4/10 perf header: Support CPU PMU capabilities

In reply to [email protected]

From: Kan Liang <[email protected]>

To stitch LBR call stack, the max LBR information is required. So the CPU PMU capabilities information has to be stored in perf header.

Add a new feature HEADER_CPU_PMU_CAPS for CPU PMU capabilities. Retrieve all CPU PMU capabilities, not just max LBR information.

Add variable max_branches to facilitate future usage.

The CPU PMU capabilities information is only useful for LBR call stack mode. Clear the feature for perf stat and other perf record mode.

---
 .../Documentation/perf.data-file-format.txt   |  16 +++
 tools/perf/builtin-record.c                   |   3 +
 tools/perf/builtin-stat.c                     |   1 +
 tools/perf/util/env.h                         |   3 +
 tools/perf/util/header.c                      | 110 ++++++++++++++++++
 tools/perf/util/header.h                      |   1 +
 6 files changed, 134 insertions(+)
tools/perf/Documentation/perf.data-file-format.txt +16 -0
index b0152e1095c5..b6472e463284 100644
@@ -373,6 +373,22 @@ struct {
 Indicates that trace contains records of PERF_RECORD_COMPRESSED type
 that have perf_events records in compressed form.
 
+	HEADER_CPU_PMU_CAPS = 28,
+
+	A list of cpu PMU capabilities. The format of data is as below.
+
+struct {
+	u32 nr_cpu_pmu_caps;
+	{
+		char	name[];
+		char	value[];
+	} [nr_cpu_pmu_caps]
+};
+
+
+Example:
+ cpu pmu capabilities: branches=32, max_precise=3, pmu_name=icelake
+
 	other bits are reserved and should ignored for now
 	HEADER_FEAT_BITS	= 256,
 
tools/perf/builtin-record.c +3 -0
index 23332861de6e..fbbeb1e625ef 100644
@@ -1057,6 +1057,9 @@ static void record__init_features(struct record *rec)
 	if (!record__comp_enabled(rec))
 		perf_header__clear_feat(&session->header, HEADER_COMPRESSED);
 
+	if (!callchain_param.enabled || (callchain_param.record_mode != CALLCHAIN_LBR))
+		perf_header__clear_feat(&session->header, HEADER_CPU_PMU_CAPS);
+
 	perf_header__clear_feat(&session->header, HEADER_STAT);
 }
 
tools/perf/builtin-stat.c +1 -0
index 468fc49420ce..26bb9794e95a 100644
@@ -1418,6 +1418,7 @@ static void init_features(struct perf_session *session)
 	perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
 	perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
 	perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
+	perf_header__clear_feat(&session->header, HEADER_CPU_PMU_CAPS);
 }
 
 static int __cmd_record(int argc, const char **argv)
tools/perf/util/env.h +3 -0
index a3059dc1abe5..dae64e20b280 100644
@@ -48,6 +48,7 @@ struct perf_env {
 	char			*cpuid;
 	unsigned long long	total_mem;
 	unsigned int		msr_pmu_type;
+	unsigned int		max_branches;
 
 	int			nr_cmdline;
 	int			nr_sibling_cores;
@@ -57,12 +58,14 @@ struct perf_env {
 	int			nr_memory_nodes;
 	int			nr_pmu_mappings;
 	int			nr_groups;
+	int			nr_cpu_pmu_caps;
 	char			*cmdline;
 	const char		**cmdline_argv;
 	char			*sibling_cores;
 	char			*sibling_dies;
 	char			*sibling_threads;
 	char			*pmu_mappings;
+	char			*cpu_pmu_caps;
 	struct cpu_topology_map	*cpu;
 	struct cpu_cache_level	*caches;
 	int			 caches_cnt;
tools/perf/util/header.c +110 -0

134 lines not on this page · raw message

tools/perf/util/header.h +1 -0
index ca53a929e9fd..ae8a7108f52b 100644
@@ -43,6 +43,7 @@ enum {
 	HEADER_BPF_PROG_INFO,
 	HEADER_BPF_BTF,
 	HEADER_COMPRESSED,
+	HEADER_CPU_PMU_CAPS,
 	HEADER_LAST_FEATURE,
 	HEADER_FEAT_BITS	= 256,
 };
Signature · 1 line
2.17.1
#11 lore raw

PATCH 6/10 perf tools: Stitch LBR call stack

In reply to [email protected]

From: Kan Liang <[email protected]>

In LBR call stack mode, the depth of reconstructed LBR call stack limits to the number of LBR registers.

For example, on skylake, the depth of reconstructed LBR call stack is always <= 32.

# To display the perf.data header info, please use # --header/--header-only options.
#
#
# Total Lost Samples: 0
#
# Samples: 6K of event 'cycles'
# Event count (approx.): 6487119731
#

  # Children      Self  Command          Shared Object       Symbol
  # ........  ........  ...............  ..................
  # ................................
    99.97%    99.97%  tchain_edit      tchain_edit        [.] f43
            |
             --99.64%--f11
                       f12
                       f13
                       f14
                       f15
                       f16
                       f17
                       f18
                       f19
                       f20
                       f21
                       f22
                       f23
                       f24
                       f25
                       f26
                       f27
                       f28
                       f29
                       f30
                       f31
                       f32
                       f33
                       f34
                       f35
                       f36
                       f37
                       f38
                       f39
                       f40
                       f41
                       f42
                       f43

For a call stack which is deeper than LBR limit, HW will overwrite the LBR register with oldest branch. Only partial call stacks can be reconstructed.

However, the overwritten LBRs may still be retrieved from previous sample. At that moment, HW hasn't overwritten the LBR registers yet. Perf tools can stitch those overwritten LBRs on current call stacks to get a more complete call stack.

To determine if LBRs can be stitched, perf tools need to compare current sample with previous sample.
- They should have identical LBR records (Same from, to and flags values, and the same physical index of LBR registers).
- The searching starts from the base-of-stack of current sample.

Add prev_sample in struct thread to save the previous sample. Add lbr_stitch_lists to save the LBRs can be used to stitch.

lbr_stitch_enable is used to indicate whether enable LBR stitch approach, which is disabled by default. The following patch will introduce a new option to enable the LBR stitch approach. This is because,
- The stitching approach base on LBR call stack technology. The known limitations of LBR call stack technology still apply to the approach, e.g. Exception handing such as setjmp/longjmp will have calls/returns not match.
- This approach is not full proof. There can be cases where it creates incorrect call stacks from incorrect matches. There is no attempt to validate any matches in another way. So it is not enabled by default. However in many common cases with call stack overflows it can recreate better call stacks than the default lbr call stack output. So if there are problems with LBR overflows this is a possible workaround.

---
 tools/perf/util/branch.h  |  10 ++-
 tools/perf/util/machine.c | 125 +++++++++++++++++++++++++++++++++++++-
 tools/perf/util/thread.c  |   3 +
 tools/perf/util/thread.h  |  18 ++++++
 4 files changed, 152 insertions(+), 4 deletions(-)
tools/perf/util/branch.h +9 -1
index 88e00d268f6f..a9c399703281 100644
@@ -34,7 +34,15 @@ struct branch_info {
 struct branch_entry {
 	u64			from;
 	u64			to;
-	struct branch_flags	flags;
+	union {
+		struct branch_flags	flags;
+		u64			flags_value;
+	};
+};
+
+struct stitch_list {
+	struct list_head	node;
+	struct branch_entry	br_entry;
 };
 
 struct branch_stack {
tools/perf/util/machine.c +122 -3

160 lines not on this page · raw message

tools/perf/util/thread.c +3 -0
index b64e9e049636..eca53b1c7de3 100644
@@ -47,8 +47,10 @@ struct thread *thread__new(pid_t pid, pid_t tid)
 		thread->tid = tid;
 		thread->ppid = -1;
 		thread->cpu = -1;
+		thread->lbr_stitch_enable = false;
 		INIT_LIST_HEAD(&thread->namespaces_list);
 		INIT_LIST_HEAD(&thread->comm_list);
+		INIT_LIST_HEAD(&thread->lbr_stitch_lists);
 		init_rwsem(&thread->namespaces_lock);
 		init_rwsem(&thread->comm_lock);
 
@@ -110,6 +112,7 @@ void thread__delete(struct thread *thread)
 
 	exit_rwsem(&thread->namespaces_lock);
 	exit_rwsem(&thread->comm_lock);
+	thread__free_stitch_list(thread);
 	free(thread);
 }
 
tools/perf/util/thread.h +18 -0
index 51bdb9a7af7f..112eccc6979b 100644
@@ -13,6 +13,9 @@
 #include <strlist.h>
 #include <intlist.h>
 #include "rwsem.h"
+#include "event.h"
+#include "map_symbol.h"
+#include "branch.h"
 
 struct addr_location;
 struct map;
@@ -46,6 +49,11 @@ struct thread {
 	struct srccode_state	srccode_state;
 	bool			filter;
 	int			filter_entry_depth;
+
+	/* stitch LBR call stack */
+	bool			lbr_stitch_enable;
+	struct list_head	lbr_stitch_lists;
+	struct perf_sample	prev_sample;
 };
 
 struct machine;
@@ -142,4 +150,14 @@ static inline bool thread__is_filtered(struct thread *thread)
 	return false;
 }
 
+static inline void thread__free_stitch_list(struct thread *thread)
+{
+	struct stitch_list *pos, *tmp;
+
+	list_for_each_entry_safe(pos, tmp, &thread->lbr_stitch_lists, node) {
+		list_del_init(&pos->node);
+		free(pos);
+	}
+}
+
 #endif	/* __PERF_THREAD_H */
Signature · 1 line
2.17.1
#13 lore raw

PATCH 8/10 perf script: Add option to enable the LBR stitching approach

In reply to [email protected]

From: Kan Liang <[email protected]>

With the LBR stitching approach, the reconstructed LBR call stack can break the HW limitation. However, it may reconstruct invalid call stacks in some cases, e.g. exception handing such as setjmp/longjmp. Also, it may impact the processing time especially when the number of samples with stitched LBRs are huge.

Add an option to enable the approach.

---
 tools/perf/Documentation/perf-script.txt | 11 +++++++++++
 tools/perf/builtin-script.c              |  6 ++++++
 2 files changed, 17 insertions(+)
tools/perf/Documentation/perf-script.txt +11 -0
index 2599b057e47b..472f20f1e479 100644
@@ -426,6 +426,17 @@ include::itrace.txt[]
 --show-on-off-events::
 	Show the --switch-on/off events too.
 
+--stitch-lbr::
+	Show callgraph with stitched LBRs, which may have more complete
+	callgraph. The perf.data file must have been obtained using
+	perf record --call-graph lbr.
+	Disabled by default. In common cases with call stack overflows,
+	it can recreate better call stacks than the default lbr call stack
+	output. But this approach is not full proof. There can be cases
+	where it creates incorrect call stacks from incorrect matches.
+	The known limitations include exception handing such as
+	setjmp/longjmp will have calls/returns not match.
+
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-script-perl[1],
tools/perf/builtin-script.c +6 -0
index 67be8d31afab..0fc4d07864d1 100644
@@ -1641,6 +1641,7 @@ struct perf_script {
 	bool			show_bpf_events;
 	bool			allocated;
 	bool			per_event_dump;
+	bool			stitch_lbr;
 	struct evswitch		evswitch;
 	struct perf_cpu_map	*cpus;
 	struct perf_thread_map *threads;
@@ -1867,6 +1868,9 @@ static void process_event(struct perf_script *script,
 	if (PRINT_FIELD(IP)) {
 		struct callchain_cursor *cursor = NULL;
 
+		if (script->stitch_lbr)
+			al->thread->lbr_stitch_enable = true;
+
 		if (symbol_conf.use_callchain && sample->callchain &&
 		    thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
 					      sample, NULL, NULL, scripting_max_stack) == 0)
@@ -3556,6 +3560,8 @@ int cmd_script(int argc, const char **argv)
 		   "file", "file saving guest os /proc/kallsyms"),
 	OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
 		   "file", "file saving guest os /proc/modules"),
+	OPT_BOOLEAN('\0', "stitch-lbr", &script.stitch_lbr,
+		    "Enable LBR callgraph stitching approach"),
 	OPTS_EVSWITCH(&script.evswitch),
 	OPT_END()
 	};
Signature · 1 line
2.17.1
#15 lore raw

PATCH 10/10 perf c2c: Add option to enable the LBR stitching approach

In reply to [email protected]

From: Kan Liang <[email protected]>

With the LBR stitching approach, the reconstructed LBR call stack can break the HW limitation. However, it may reconstruct invalid call stacks in some cases, e.g. exception handing such as setjmp/longjmp. Also, it may impact the processing time especially when the number of samples with stitched LBRs are huge.

Add an option to enable the approach.

---
 tools/perf/Documentation/perf-c2c.txt | 11 +++++++++++
 tools/perf/builtin-c2c.c              |  6 ++++++
 2 files changed, 17 insertions(+)
tools/perf/Documentation/perf-c2c.txt +11 -0
index e6150f21267d..2133eb320cb0 100644
@@ -111,6 +111,17 @@ REPORT OPTIONS
 --display::
 	Switch to HITM type (rmt, lcl) to display and sort on. Total HITMs as default.
 
+--stitch-lbr::
+	Show callgraph with stitched LBRs, which may have more complete
+	callgraph. The perf.data file must have been obtained using
+	perf c2c record --call-graph lbr.
+	Disabled by default. In common cases with call stack overflows,
+	it can recreate better call stacks than the default lbr call stack
+	output. But this approach is not full proof. There can be cases
+	where it creates incorrect call stacks from incorrect matches.
+	The known limitations include exception handing such as
+	setjmp/longjmp will have calls/returns not match.
+
 C2C RECORD
 ----------
 The perf c2c record command setup options related to HITM cacheline analysis
tools/perf/builtin-c2c.c +6 -0
index 3542b6ab9813..c3658986c38a 100644
@@ -95,6 +95,7 @@ struct perf_c2c {
 	bool			 use_stdio;
 	bool			 stats_only;
 	bool			 symbol_full;
+	bool			 stitch_lbr;
 
 	/* HITM shared clines stats */
 	struct c2c_stats	hitm_stats;
@@ -273,6 +274,9 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
 		return -1;
 	}
 
+	if (c2c.stitch_lbr)
+		al.thread->lbr_stitch_enable = true;
+
 	ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
 					evsel, &al, sysctl_perf_event_max_stack);
 	if (ret)
@@ -2746,6 +2750,8 @@ static int perf_c2c__report(int argc, const char **argv)
 	OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
 		   "coalesce fields: pid,tid,iaddr,dso"),
 	OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
+	OPT_BOOLEAN(0, "stitch-lbr", &c2c.stitch_lbr,
+		    "Enable LBR callgraph stitching approach"),
 	OPT_PARENT(c2c_options),
 	OPT_END()
 	};
Signature · 1 line
2.17.1
#14 lore raw

PATCH 9/10 perf top: Add option to enable the LBR stitching approach

In reply to [email protected]

From: Kan Liang <[email protected]>

With the LBR stitching approach, the reconstructed LBR call stack can break the HW limitation. However, it may reconstruct invalid call stacks in some cases, e.g. exception handing such as setjmp/longjmp. Also, it may impact the processing time especially when the number of samples with stitched LBRs are huge.

Add an option to enable the approach.
The option must be used with --call-graph lbr.

---
 tools/perf/Documentation/perf-top.txt |  9 +++++++++
 tools/perf/builtin-top.c              | 11 +++++++++++
 tools/perf/util/top.h                 |  1 +
 3 files changed, 21 insertions(+)
tools/perf/Documentation/perf-top.txt +9 -0
index 5596129a71cf..80b57f942a86 100644
@@ -304,6 +304,15 @@ Default is to monitor all CPUS.
 	go straight to the histogram browser, just like 'perf top' with no events
 	explicitely specified does.
 
+--stitch-lbr::
+	Show callgraph with stitched LBRs, which may have more complete
+	callgraph. The option must be used with --call-graph lbr recording.
+	Disabled by default. In common cases with call stack overflows,
+	it can recreate better call stacks than the default lbr call stack
+	output. But this approach is not full proof. There can be cases
+	where it creates incorrect call stacks from incorrect matches.
+	The known limitations include exception handing such as
+	setjmp/longjmp will have calls/returns not match.
 
 INTERACTIVE PROMPTING KEYS
 --------------------------
tools/perf/builtin-top.c +11 -0
index 611d03030abc..539670377e0f 100644
@@ -33,6 +33,7 @@
 #include "util/map.h"
 #include "util/mmap.h"
 #include "util/session.h"
+#include "util/thread.h"
 #include "util/symbol.h"
 #include "util/synthetic-events.h"
 #include "util/top.h"
@@ -764,6 +765,9 @@ static void perf_event__process_sample(struct perf_tool *tool,
 	if (machine__resolve(machine, &al, sample) < 0)
 		return;
 
+	if (top->stitch_lbr)
+		al.thread->lbr_stitch_enable = true;
+
 	if (!machine->kptr_restrict_warned &&
 	    symbol_conf.kptr_restrict &&
 	    al.cpumode == PERF_RECORD_MISC_KERNEL) {
@@ -1537,6 +1541,8 @@ int cmd_top(int argc, const char **argv)
 			"number of thread to run event synthesize"),
 	OPT_BOOLEAN(0, "namespaces", &opts->record_namespaces,
 		    "Record namespaces events"),
+	OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
+		    "Enable LBR callgraph stitching approach"),
 	OPTS_EVSWITCH(&top.evswitch),
 	OPT_END()
 	};
@@ -1599,6 +1605,11 @@ int cmd_top(int argc, const char **argv)
 		}
 	}
 
+	if (top.stitch_lbr && !(callchain_param.record_mode == CALLCHAIN_LBR)) {
+		pr_err("Error: --stitch-lbr must be used with --call-graph lbr\n");
+		goto out_delete_evlist;
+	}
+
 	if (opts->branch_stack && callchain_param.enabled)
 		symbol_conf.show_branchflag_count = true;
 
tools/perf/util/top.h +1 -0
index f117d4f4821e..45dc84ddff37 100644
@@ -36,6 +36,7 @@ struct perf_top {
 	bool		   use_tui, use_stdio;
 	bool		   vmlinux_warned;
 	bool		   dump_symtab;
+	bool		   stitch_lbr;
 	struct hist_entry  *sym_filter_entry;
 	struct evsel 	   *sym_evsel;
 	struct perf_session *session;
Signature · 1 line
2.17.1
#12 lore raw

PATCH 7/10 perf report: Add option to enable the LBR stitching approach

In reply to [email protected]

From: Kan Liang <[email protected]>

With the LBR stitching approach, the reconstructed LBR call stack can break the HW limitation. However, it may reconstruct invalid call stacks in some cases, e.g. exception handing such as setjmp/longjmp. Also, it may impact the processing time especially when the number of samples with stitched LBRs are huge.

Add an option to enable the approach.

# To display the perf.data header info, please use # --header/--header-only options.
#
#
# Total Lost Samples: 0
#
# Samples: 6K of event 'cycles'
# Event count (approx.): 6492797701
#

  # Children      Self  Command          Shared Object       Symbol
  # ........  ........  ...............  ..................
  # .................................
  #
    99.99%    99.99%  tchain_edit      tchain_edit        [.] f43
            |
            ---main
               f1
               f2
               f3
               f4
               f5
               f6
               f7
               f8
               f9
               f10
               f11
               f12
               f13
               f14
               f15
               f16
               f17
               f18
               f19
               f20
               f21
               f22
               f23
               f24
               f25
               f26
               f27
               f28
               f29
               f30
               f31
               |
                --99.65%--f32
                          f33
                          f34
                          f35
                          f36
                          f37
                          f38
                          f39
                          f40
                          f41
                          f42
                          f43
---
 tools/perf/Documentation/perf-report.txt | 11 +++++++++++
 tools/perf/builtin-report.c              |  6 ++++++
 2 files changed, 17 insertions(+)
tools/perf/Documentation/perf-report.txt +11 -0
index 7315f155803f..c0651b68ed5d 100644
@@ -476,6 +476,17 @@ include::itrace.txt[]
 	This option extends the perf report to show reference callgraphs,
 	which collected by reference event, in no callgraph event.
 
+--stitch-lbr::
+	Show callgraph with stitched LBRs, which may have more complete
+	callgraph. The perf.data file must have been obtained using
+	perf record --call-graph lbr.
+	Disabled by default. In common cases with call stack overflows,
+	it can recreate better call stacks than the default lbr call stack
+	output. But this approach is not full proof. There can be cases
+	where it creates incorrect call stacks from incorrect matches.
+	The known limitations include exception handing such as
+	setjmp/longjmp will have calls/returns not match.
+
 --socket-filter::
 	Only report the samples on the processor socket that match with this filter
 
tools/perf/builtin-report.c +6 -0
index aae0e57c60fb..0d4275a46645 100644
@@ -83,6 +83,7 @@ struct report {
 	bool			header_only;
 	bool			nonany_branch_mode;
 	bool			group_set;
+	bool			stitch_lbr;
 	int			max_stack;
 	struct perf_read_values	show_threads_values;
 	struct annotation_options annotation_opts;
@@ -263,6 +264,9 @@ static int process_sample_event(struct perf_tool *tool,
 		return -1;
 	}
 
+	if (rep->stitch_lbr)
+		al.thread->lbr_stitch_enable = true;
+
 	if (symbol_conf.hide_unresolved && al.sym == NULL)
 		goto out_put;
 
@@ -1183,6 +1187,8 @@ int cmd_report(int argc, const char **argv)
 			"Show full source file name path for source lines"),
 	OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
 		    "Show callgraph from reference event"),
+	OPT_BOOLEAN(0, "stitch-lbr", &report.stitch_lbr,
+		    "Enable LBR callgraph stitching approach"),
 	OPT_INTEGER(0, "socket-filter", &report.socket_filter,
 		    "only show processor socket that match with this filter"),
 	OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
Signature · 1 line
2.17.1
#10 lore raw

PATCH 5/10 perf machine: Refine the function for LBR call stack reconstruction

In reply to [email protected]

From: Kan Liang <[email protected]>

LBR only collect the user call stack. To reconstruct a call stack, both kernel call stack and user call stack are required. The function resolve_lbr_callchain_sample() mix the kernel call stack and user call stack. Now, with the help of TOS, perf tool can reconstruct a more complete call stack by adding some user call stack from previous sample. However, current implementation is hard to be extended to support it.

Abstract two new functions to resolve user call stack and kernel call stack respectively.

No functional changes.

---
 tools/perf/util/machine.c | 186 ++++++++++++++++++++++++--------------
 1 file changed, 119 insertions(+), 67 deletions(-)
tools/perf/util/machine.c +119 -67
index 70a9f8716a4b..e3e516e30093 100644
@@ -2183,6 +2183,96 @@ static int remove_loops(struct branch_entry *l, int nr,
 	return nr;
 }
 
+
+static int lbr_callchain_add_kernel_ip(struct thread *thread,
+				       struct callchain_cursor *cursor,
+				       struct perf_sample *sample,
+				       struct symbol **parent,
+				       struct addr_location *root_al,
+				       bool callee, int end)
+{
+	struct ip_callchain *chain = sample->callchain;
+	u8 cpumode = PERF_RECORD_MISC_USER;
+	int err, i;
+
+	if (callee) {
+		for (i = 0; i < end + 1; i++) {
+			err = add_callchain_ip(thread, cursor, parent,
+					       root_al, &cpumode, chain->ips[i],
+					       false, NULL, NULL, 0);
+			if (err)
+				return err;
+		}
+	} else {
+		for (i = end; i >= 0; i--) {
+			err = add_callchain_ip(thread, cursor, parent,
+					       root_al, &cpumode, chain->ips[i],
+					       false, NULL, NULL, 0);
+			if (err)
+				return err;
+		}
+	}
+
+	return 0;
+}
+
+static int lbr_callchain_add_lbr_ip(struct thread *thread,
+				    struct callchain_cursor *cursor,
+				    struct perf_sample *sample,
+				    struct symbol **parent,
+				    struct addr_location *root_al,
+				    bool callee)
+{
+	struct branch_stack *lbr_stack = sample->branch_stack;
+	u8 cpumode = PERF_RECORD_MISC_USER;
+	int lbr_nr = lbr_stack->nr;
+	struct branch_flags *flags;
+	u64 ip, branch_from = 0;
+	int err, i;
+
+	if (callee) {
+		ip = lbr_stack->entries[0].to;
+		flags = &lbr_stack->entries[0].flags;
+		branch_from = lbr_stack->entries[0].from;
+		err = add_callchain_ip(thread, cursor, parent,
+				       root_al, &cpumode, ip,
+				       true, flags, NULL, branch_from);
+		if (err)
+			return err;
+
+		for (i = 0; i < lbr_nr; i++) {
+			ip = lbr_stack->entries[i].from;
+			flags = &lbr_stack->entries[i].flags;
+			err = add_callchain_ip(thread, cursor, parent,
+					       root_al, &cpumode, ip,
+					       true, flags, NULL, branch_from);
+			if (err)
+				return err;
+		}
+	} else {
+		for (i = lbr_nr - 1; i >= 0; i--) {
+			ip = lbr_stack->entries[i].from;
+			flags = &lbr_stack->entries[i].flags;
+			err = add_callchain_ip(thread, cursor, parent,
+					       root_al, &cpumode, ip,
+					       true, flags, NULL, branch_from);
+			if (err)
+				return err;
+		}
+
+		ip = lbr_stack->entries[0].to;
+		flags = &lbr_stack->entries[0].flags;
+		branch_from = lbr_stack->entries[0].from;
+		err = add_callchain_ip(thread, cursor, parent,
+				       root_al, &cpumode, ip,
+				       true, flags, NULL, branch_from);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 /*
  * Recolve LBR callstack chain sample
  * Return:
@@ -2198,82 +2288,44 @@ static int resolve_lbr_callchain_sample(struct thread *thread,
 					int max_stack)
 {
 	struct ip_callchain *chain = sample->callchain;
-	int chain_nr = min(max_stack, (int)chain->nr), i;
-	u8 cpumode = PERF_RECORD_MISC_USER;
-	u64 ip, branch_from = 0;
+	int chain_nr = min(max_stack, (int)chain->nr);
+	int i, err;
 
 	for (i = 0; i < chain_nr; i++) {
 		if (chain->ips[i] == PERF_CONTEXT_USER)
 			break;
 	}
 
-	/* LBR only affects the user callchain */
-	if (i != chain_nr) {
-		struct branch_stack *lbr_stack = sample->branch_stack;
-		int lbr_nr = lbr_stack->nr, j, k;
-		bool branch;
-		struct branch_flags *flags;
-		/*
-		 * LBR callstack can only get user call chain.
-		 * The mix_chain_nr is kernel call chain
-		 * number plus LBR user call chain number.
-		 * i is kernel call chain number,
-		 * 1 is PERF_CONTEXT_USER,
-		 * lbr_nr + 1 is the user call chain number.
-		 * For details, please refer to the comments
-		 * in callchain__printf
-		 */
-		int mix_chain_nr = i + 1 + lbr_nr + 1;
-
-		for (j = 0; j < mix_chain_nr; j++) {
-			int err;
-			branch = false;
-			flags = NULL;
-
-			if (callchain_param.order == ORDER_CALLEE) {
-				if (j < i + 1)
-					ip = chain->ips[j];
-				else if (j > i + 1) {
-					k = j - i - 2;
-					ip = lbr_stack->entries[k].from;
-					branch = true;
-					flags = &lbr_stack->entries[k].flags;
-				} else {
-					ip = lbr_stack->entries[0].to;
-					branch = true;
-					flags = &lbr_stack->entries[0].flags;
-					branch_from =
-						lbr_stack->entries[0].from;
-				}
-			} else {
-				if (j < lbr_nr) {
-					k = lbr_nr - j - 1;
-					ip = lbr_stack->entries[k].from;
-					branch = true;
-					flags = &lbr_stack->entries[k].flags;
-				}
-				else if (j > lbr_nr)
-					ip = chain->ips[i + 1 - (j - lbr_nr)];
-				else {
-					ip = lbr_stack->entries[0].to;
-					branch = true;
-					flags = &lbr_stack->entries[0].flags;
-					branch_from =
-						lbr_stack->entries[0].from;
-				}
-			}
+	/*
+	 * LBR only affects the user callchain.
+	 * Fall back if there is no user callchain.
+	 */
+	if (i == chain_nr)
+		return 0;
 
-			err = add_callchain_ip(thread, cursor, parent,
-					       root_al, &cpumode, ip,
-					       branch, flags, NULL,
-					       branch_from);
-			if (err)
-				return (err < 0) ? err : 0;
-		}
-		return 1;
+	if (callchain_param.order == ORDER_CALLEE) {
+		err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
+						  parent, root_al, true, i);
+		if (err)
+			goto error;
+		err = lbr_callchain_add_lbr_ip(thread, cursor, sample,
+					       parent, root_al, true);
+		if (err)
+			goto error;
+	} else {
+		err = lbr_callchain_add_lbr_ip(thread, cursor, sample,
+					       parent, root_al, false);
+		if (err)
+			goto error;
+		err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
+						  parent, root_al, false, i);
+		if (err)
+			goto error;
 	}
 
-	return 0;
+	return 1;
+error:
+	return (err < 0) ? err : 0;
 }
 
 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
Signature · 1 line
2.17.1
#8 lore raw

PATCH 3/10 perf pmu: Add support for PMU capabilities

In reply to [email protected]

From: Kan Liang <[email protected]>

The PMU capabilities information, which is located at /sys/bus/event_source/devices/<dev>/caps, is required by perf tool. For example, the max LBR information is required to stitch LBR call stack.

Add perf_pmu__caps_parse() to parse the PMU capabilities information. The information is stored in a list.

Add perf_pmu__scan_caps() to scan the capabilities once by one.

The following patch will store the capabilities information in perf header.

---
 tools/perf/util/pmu.c | 87 +++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/pmu.h | 12 ++++++
 2 files changed, 99 insertions(+)
tools/perf/util/pmu.c +87 -0
index 5608da82ad23..d3dc9d4f9479 100644
@@ -847,6 +847,7 @@ static struct perf_pmu *pmu_lookup(const char *name)
 
 	INIT_LIST_HEAD(&pmu->format);
 	INIT_LIST_HEAD(&pmu->aliases);
+	INIT_LIST_HEAD(&pmu->caps);
 	list_splice(&format, &pmu->format);
 	list_splice(&aliases, &pmu->aliases);
 	list_add_tail(&pmu->list, &pmus);
@@ -1552,3 +1553,89 @@ int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
 	va_end(args);
 	return ret;
 }
+
+static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
+{
+	struct perf_pmu_caps *caps;
+
+	caps = zalloc(sizeof(*caps));
+	if (!caps)
+		return -ENOMEM;
+
+	caps->name = strdup(name);
+	caps->value = strndup(value, strlen(value) - 1);
+	list_add_tail(&caps->list, list);
+	return 0;
+}
+
+/*
+ * Reading/parsing the given pmu capabilities, which should be located at:
+ * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
+ * Return the number of capabilities
+ */
+int perf_pmu__caps_parse(struct perf_pmu *pmu)
+{
+	struct stat st;
+	char caps_path[PATH_MAX];
+	const char *sysfs = sysfs__mountpoint();
+	DIR *caps_dir;
+	struct dirent *evt_ent;
+	int nr_caps = 0;
+
+	if (!sysfs)
+		return -1;
+
+	snprintf(caps_path, PATH_MAX,
+		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name);
+
+	if (stat(caps_path, &st) < 0)
+		return 0;	/* no error if caps does not exist */
+
+	caps_dir = opendir(caps_path);
+	if (!caps_dir)
+		return -EINVAL;
+
+	while ((evt_ent = readdir(caps_dir)) != NULL) {
+		char *name = evt_ent->d_name;
+		char path[PATH_MAX];
+		char value[128];
+		FILE *file;
+
+		if (!strcmp(name, ".") || !strcmp(name, ".."))
+			continue;
+
+		snprintf(path, PATH_MAX, "%s/%s", caps_path, name);
+
+		file = fopen(path, "r");
+		if (!file)
+			break;
+
+		if (!fgets(value, sizeof(value), file) ||
+		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
+			fclose(file);
+			break;
+		}
+
+		nr_caps++;
+		fclose(file);
+	}
+
+	closedir(caps_dir);
+
+	return nr_caps;
+}
+
+struct perf_pmu_caps *perf_pmu__scan_caps(struct perf_pmu *pmu,
+					  struct perf_pmu_caps *caps)
+{
+	if (!pmu)
+		return NULL;
+
+	if (!caps)
+		caps = list_prepare_entry(caps, &pmu->caps, list);
+
+	list_for_each_entry_continue(caps, &pmu->caps, list)
+		return caps;
+
+	return NULL;
+}
tools/perf/util/pmu.h +12 -0
index f36ade6df76d..5ded4e3e28e4 100644
@@ -21,6 +21,12 @@ enum {
 
 struct perf_event_attr;
 
+struct perf_pmu_caps {
+	char *name;
+	char *value;
+	struct list_head list;
+};
+
 struct perf_pmu {
 	char *name;
 	__u32 type;
@@ -31,6 +37,7 @@ struct perf_pmu {
 	struct perf_cpu_map *cpus;
 	struct list_head format;  /* HEAD struct perf_pmu_format -> list */
 	struct list_head aliases; /* HEAD struct perf_pmu_alias -> list */
+	struct list_head caps;    /* HEAD struct perf_pmu_caps -> list */
 	struct list_head list;    /* ELEM */
 };
 
@@ -98,4 +105,9 @@ struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu);
 
 int perf_pmu__convert_scale(const char *scale, char **end, double *sval);
 
+int perf_pmu__caps_parse(struct perf_pmu *pmu);
+
+struct perf_pmu_caps *perf_pmu__scan_caps(struct perf_pmu *pmu,
+					  struct perf_pmu_caps *caps);
+
 #endif /* __PMU_H */
Signature · 1 line
2.17.1
#7 lore raw

PATCH 2/10 perf tools: Support PERF_SAMPLE_LBR_TOS

In reply to [email protected]

From: Kan Liang <[email protected]>

Support new sample type PERF_SAMPLE_LBR_TOS.

Enable LBR_TOS by default in LBR call stack mode. If kernel doesn't support the sample type, switching it off.

---
 tools/include/uapi/linux/perf_event.h     |  4 +++-
 tools/perf/util/event.h                   |  1 +
 tools/perf/util/evsel.c                   | 16 +++++++++++++++-
 tools/perf/util/evsel.h                   |  1 +
 tools/perf/util/perf_event_attr_fprintf.c |  2 +-
 tools/perf/util/synthetic-events.c        |  8 ++++++++
 6 files changed, 29 insertions(+), 3 deletions(-)
tools/include/uapi/linux/perf_event.h +3 -1
index bb7b271397a6..fe36ebb7dc2e 100644
@@ -141,8 +141,9 @@ enum perf_event_sample_format {
 	PERF_SAMPLE_TRANSACTION			= 1U << 17,
 	PERF_SAMPLE_REGS_INTR			= 1U << 18,
 	PERF_SAMPLE_PHYS_ADDR			= 1U << 19,
+	PERF_SAMPLE_LBR_TOS			= 1U << 20,
 
-	PERF_SAMPLE_MAX = 1U << 20,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 21,		/* non-ABI */
 
 	__PERF_SAMPLE_CALLCHAIN_EARLY		= 1ULL << 63, /* non-ABI; internal use */
 };
@@ -864,6 +865,7 @@ enum perf_event_type {
 	 *	{ u64			abi; # enum perf_sample_regs_abi
 	 *	  u64			regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
 	 *	{ u64			phys_addr;} && PERF_SAMPLE_PHYS_ADDR
+	 *	{ u64			tos;} && PERF_SAMPLE_LBR_TOS
 	 * };
 	 */
 	PERF_RECORD_SAMPLE			= 9,
tools/perf/util/event.h +1 -0
index a0a0c91cde4a..268c1715e032 100644
@@ -130,6 +130,7 @@ struct perf_sample {
 	u32 raw_size;
 	u64 data_src;
 	u64 phys_addr;
+	u64 tos;
 	u32 flags;
 	u16 insn_len;
 	u8  cpumode;
tools/perf/util/evsel.c +15 -1
index abc7fda4a0fe..0752417bbc45 100644
@@ -709,6 +709,7 @@ static void __perf_evsel__config_callchain(struct evsel *evsel,
 					   "Falling back to framepointers.\n");
 			} else {
 				perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
+				perf_evsel__set_sample_bit(evsel, LBR_TOS);
 				attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
 							PERF_SAMPLE_BRANCH_CALL_STACK |
 							PERF_SAMPLE_BRANCH_NO_CYCLES |
@@ -762,6 +763,7 @@ perf_evsel__reset_callgraph(struct evsel *evsel,
 	perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
 	if (param->record_mode == CALLCHAIN_LBR) {
 		perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
+		perf_evsel__reset_sample_bit(evsel, LBR_TOS);
 		attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
 					      PERF_SAMPLE_BRANCH_CALL_STACK);
 	}
@@ -1641,6 +1643,8 @@ int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
 		evsel->core.attr.ksymbol = 0;
 	if (perf_missing_features.bpf)
 		evsel->core.attr.bpf_event = 0;
+	if (perf_missing_features.lbr_tos)
+		perf_evsel__reset_sample_bit(evsel, LBR_TOS);
 retry_sample_id:
 	if (perf_missing_features.sample_id_all)
 		evsel->core.attr.sample_id_all = 0;
@@ -1752,7 +1756,11 @@ int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
 	 * Must probe features in the order they were added to the
 	 * perf_event_attr interface.
 	 */
-	if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
+	if (!perf_missing_features.lbr_tos && (evsel->core.attr.sample_type & PERF_SAMPLE_LBR_TOS)) {
+		perf_missing_features.lbr_tos = true;
+		pr_debug2("switching off LBR TOS support\n");
+		goto fallback_missing_features;
+	} else if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) {
 		perf_missing_features.aux_output = true;
 		pr_debug2("Kernel has no attr.aux_output support, bailing out\n");
 		goto out_close;
@@ -2206,6 +2214,12 @@ int perf_evsel__parse_sample(struct evsel *evsel, union perf_event *event,
 		array++;
 	}
 
+	data->tos = -1ULL;
+	if (type & PERF_SAMPLE_LBR_TOS) {
+		data->tos = *array;
+		array++;
+	}
+
 	return 0;
 }
 
tools/perf/util/evsel.h +1 -0
index ddc5ee6f6592..e4768c60da93 100644
@@ -115,6 +115,7 @@ struct perf_missing_features {
 	bool ksymbol;
 	bool bpf;
 	bool aux_output;
+	bool lbr_tos;
 };
 
 extern struct perf_missing_features perf_missing_features;
tools/perf/util/perf_event_attr_fprintf.c +1 -1
index d4ad3f04923a..254f1bf8dcae 100644
@@ -34,7 +34,7 @@ static void __p_sample_type(char *buf, size_t size, u64 value)
 		bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
 		bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
 		bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
-		bit_name(WEIGHT), bit_name(PHYS_ADDR),
+		bit_name(WEIGHT), bit_name(PHYS_ADDR), bit_name(LBR_TOS),
 		{ .name = NULL, }
 	};
 #undef bit_name
tools/perf/util/synthetic-events.c +8 -0
index 807cbca403a7..a7d02e81defe 100644
@@ -1228,6 +1228,9 @@ size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
 	if (type & PERF_SAMPLE_PHYS_ADDR)
 		result += sizeof(u64);
 
+	if (type & PERF_SAMPLE_LBR_TOS)
+		result += sizeof(u64);
+
 	return result;
 }
 
@@ -1396,6 +1399,11 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_fo
 		array++;
 	}
 
+	if (type & PERF_SAMPLE_LBR_TOS) {
+		*array = sample->tos;
+		array++;
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
#16 lore raw
In reply to [email protected]
Quoted 20 lines · * [email protected] <[email protected]> wrote:

Performance impact:
The processing time may increase with the LBR stitching approach enabled. The impact depends on the number of samples with stitched LBRs.

For sqlite's tcltest,
perf record --call-graph lbr -- make tcltest
perf report --stitch-lbr

There are 4.11% samples has stitched LBRs.
Total number of samples: 2833728
The number of samples with stitched LBRs 116478

The processing time of perf report increases 6.8%

Without --stitch-lbr:                           55906106 usec
With --stitch-lbr:                              59728701 usec

For a simple test case tchain_edit with 43 depth of call stacks. perf record --call-graph lbr -- ./tchain_edit
perf report --stitch-lbr

Quoted 7 lines

There are 99.9% samples has stitched LBRs.
Total number of samples: 10915
The number of samples with stitched LBRs 10905

The processing time of perf report increases 67.4%

Without --stitch-lbr:                           11970508 usec
With --stitch-lbr:                              20036055 usec

That cost seems pretty high, while the feature sounds useful - is there any way to speed this up?

Thanks,

Ingo

author
#17 lore raw
Quoted 27 lines · Ingo Molnar
Quoted 24 lines · * [email protected] <[email protected]> wrote:

Performance impact:
The processing time may increase with the LBR stitching approach enabled. The impact depends on the number of samples with stitched LBRs.

For sqlite's tcltest,
perf record --call-graph lbr -- make tcltest
perf report --stitch-lbr

There are 4.11% samples has stitched LBRs.
Total number of samples: 2833728
The number of samples with stitched LBRs 116478

The processing time of perf report increases 6.8%

Without --stitch-lbr:                           55906106 usec
With --stitch-lbr:                              59728701 usec

For a simple test case tchain_edit with 43 depth of call stacks. perf record --call-graph lbr -- ./tchain_edit
perf report --stitch-lbr

There are 99.9% samples has stitched LBRs.
Total number of samples: 10915
The number of samples with stitched LBRs 10905

Quoted 7 lines
Quoted 3 lines

The processing time of perf report increases 67.4%

Without --stitch-lbr:                           11970508 usec
With --stitch-lbr:                              20036055 usec

That cost seems pretty high, while the feature sounds useful - is there any way to speed this up?

For each LBR entry, perf tool will calculate and generate an appended node for callchain_cursor.
The stitched LBR entries are from previous sample. It looks like we don't need to do the calculation again for them. That should speed up the whole process. I will do more test for it.

Thanks,
Kan

maintainer
#2 lore raw

Re: [PATCH 01/10] perf/core, x86: Add PERF_SAMPLE_LBR_TOS

In reply to [email protected]
Quoted 28 lines · On Mon, Oct 07, 2019 at 10:59:01AM -0700, [email protected] wrote:
include/linux/perf_event.h +1 -0
index 61448c19a132..ee9ef0c4cb08 100644
@@ -100,6 +100,7 @@ struct perf_raw_record {
  */
 struct perf_branch_stack {
 	__u64				nr;
+	__u64				tos;
 	struct perf_branch_entry	entries[0];
 };
 
include/uapi/linux/perf_event.h +2 -1
index bb7b271397a6..fe36ebb7dc2e 100644
@@ -141,8 +141,9 @@ enum perf_event_sample_format {
 	PERF_SAMPLE_TRANSACTION			= 1U << 17,
 	PERF_SAMPLE_REGS_INTR			= 1U << 18,
 	PERF_SAMPLE_PHYS_ADDR			= 1U << 19,
+	PERF_SAMPLE_LBR_TOS			= 1U << 20,
 
-	PERF_SAMPLE_MAX = 1U << 20,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 21,		/* non-ABI */
 
 	__PERF_SAMPLE_CALLCHAIN_EARLY		= 1ULL << 63, /* non-ABI; internal use */
 };

include/uapi/linux/perf_event.h enum perf_event_type { · line 871

Quoted 7 lines
include/uapi/linux/perf_event.h +1 -0
@@ -864,6 +865,7 @@ enum perf_event_type {
 	 *	{ u64			abi; # enum perf_sample_regs_abi
 	 *	  u64			regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
 	 *	{ u64			phys_addr;} && PERF_SAMPLE_PHYS_ADDR
+	 *	{ u64			tos;} && PERF_SAMPLE_LBR_TOS
 	 * };
 	 */
 	PERF_RECORD_SAMPLE			= 9,

I have problems with the API.. You're introducing the intel specific LBR naming, and adding a whole new sample type vs extending the existing BRANCH_STACK (like you really already do with struct perf_branch_stack).

So why not add a bit to PERF_SAMPLE_BRANCH_* to request the presence of the TOS field in the PERF_SAMPLE_BRANCH_STACK output?

author
#3 lore raw

Re: [PATCH 01/10] perf/core, x86: Add PERF_SAMPLE_LBR_TOS

Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · On Mon, Oct 07, 2019 at 10:59:01AM -0700, [email protected] wrote:
include/linux/perf_event.h +1 -0
index 61448c19a132..ee9ef0c4cb08 100644
@@ -100,6 +100,7 @@ struct perf_raw_record {
   */
  struct perf_branch_stack {
  	__u64				nr;
+	__u64				tos;
  	struct perf_branch_entry	entries[0];
  };
 
include/uapi/linux/perf_event.h +2 -1
index bb7b271397a6..fe36ebb7dc2e 100644
@@ -141,8 +141,9 @@ enum perf_event_sample_format {
  	PERF_SAMPLE_TRANSACTION			= 1U << 17,
  	PERF_SAMPLE_REGS_INTR			= 1U << 18,
  	PERF_SAMPLE_PHYS_ADDR			= 1U << 19,
+	PERF_SAMPLE_LBR_TOS			= 1U << 20,
 
-	PERF_SAMPLE_MAX = 1U << 20,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 21,		/* non-ABI */
 
Quoted 16 lines

include/uapi/linux/perf_event.h enum perf_event_type { · line 871

Quoted 10 lines
include/uapi/linux/perf_event.h +1 -0
  	__PERF_SAMPLE_CALLCHAIN_EARLY		= 1ULL << 63, /* non-ABI; internal use */
  };
@@ -864,6 +865,7 @@ enum perf_event_type {
  	 *	{ u64			abi; # enum perf_sample_regs_abi
  	 *	  u64			regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
  	 *	{ u64			phys_addr;} && PERF_SAMPLE_PHYS_ADDR
+	 *	{ u64			tos;} && PERF_SAMPLE_LBR_TOS
  	 * };
  	 */
  	PERF_RECORD_SAMPLE			= 9,

I have problems with the API.. You're introducing the intel specific LBR naming, and adding a whole new sample type vs extending the existing BRANCH_STACK (like you really already do with struct perf_branch_stack). > So why not add a bit to PERF_SAMPLE_BRANCH_* to request the presence of the TOS field in the PERF_SAMPLE_BRANCH_STACK output?

We never store PERF_SAMPLE_BRANCH_* in a sample. The perf tool cannot tell if the sample includes TOS field. There will be a problem when a new perf tool parsing the data generated by an old kernel.

Can we rename the new sample type PERF_SAMPLE_BRANCH_STACK_EXTENSION?

{ u64 version;
u64 tos;} && PERF_SAMPLE_LBR_TOS

If other platforms want to add their extension, we just need to increase the version number. Perf tool will check the version before parsing the sample.

Thanks,
Kan

maintainer
#4 lore raw

Re: [PATCH 01/10] perf/core, x86: Add PERF_SAMPLE_LBR_TOS

Quoted 43 lines · Kan
Quoted 31 lines · Peter Zijlstra
Quoted 30 lines · On Mon, Oct 07, 2019 at 10:59:01AM -0700, [email protected] wrote:
include/linux/perf_event.h +1 -0
index 61448c19a132..ee9ef0c4cb08 100644
@@ -100,6 +100,7 @@ struct perf_raw_record {
   */
  struct perf_branch_stack {
  	__u64				nr;
+	__u64				tos;
  	struct perf_branch_entry	entries[0];
  };
include/uapi/linux/perf_event.h +3 -1
index bb7b271397a6..fe36ebb7dc2e 100644
@@ -141,8 +141,9 @@ enum perf_event_sample_format {
  	PERF_SAMPLE_TRANSACTION			= 1U << 17,
  	PERF_SAMPLE_REGS_INTR			= 1U << 18,
  	PERF_SAMPLE_PHYS_ADDR			= 1U << 19,
+	PERF_SAMPLE_LBR_TOS			= 1U << 20,
-	PERF_SAMPLE_MAX = 1U << 20,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 21,		/* non-ABI */
  	__PERF_SAMPLE_CALLCHAIN_EARLY		= 1ULL << 63, /* non-ABI; internal use */
  };
@@ -864,6 +865,7 @@ enum perf_event_type {
  	 *	{ u64			abi; # enum perf_sample_regs_abi
  	 *	  u64			regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
  	 *	{ u64			phys_addr;} && PERF_SAMPLE_PHYS_ADDR
+	 *	{ u64			tos;} && PERF_SAMPLE_LBR_TOS
  	 * };
Quoted 8 lines

include/uapi/linux/perf_event.h enum perf_event_type { · line 871

Quoted 2 lines
include/uapi/linux/perf_event.h +0 -0
  	 */
  	PERF_RECORD_SAMPLE			= 9,

I have problems with the API.. You're introducing the intel specific LBR naming, and adding a whole new sample type vs extending the existing BRANCH_STACK (like you really already do with struct perf_branch_stack). > So why not add a bit to PERF_SAMPLE_BRANCH_* to request the presence of the TOS field in the PERF_SAMPLE_BRANCH_STACK output?

Quoted 2 lines

We never store PERF_SAMPLE_BRANCH_* in a sample. The perf tool cannot tell if the sample includes TOS field.

The perf tool bloody sets the perf_event_attr::branch_sample_type value! Of course it knows to expect the TOS field when it asks for it in the first place.

Quoted 2 lines

There will be a problem when a new perf tool parsing the data generated by an old kernel.

ISTR perf stores the full perf_event_attr in the .data file these days, and therefore such confusion should never happen.

author
#5 lore raw

Re: [PATCH 01/10] perf/core, x86: Add PERF_SAMPLE_LBR_TOS

Quoted 44 lines · Peter Zijlstra
Quoted 43 lines · Kan
Quoted 39 lines · Peter Zijlstra
Quoted 25 lines · On Mon, Oct 07, 2019 at 10:59:01AM -0700, [email protected] wrote:
include/linux/perf_event.h +1 -0
index 61448c19a132..ee9ef0c4cb08 100644
@@ -100,6 +100,7 @@ struct perf_raw_record {
    */
   struct perf_branch_stack {
   	__u64				nr;
+	__u64				tos;
   	struct perf_branch_entry	entries[0];
   };
include/uapi/linux/perf_event.h +2 -1
index bb7b271397a6..fe36ebb7dc2e 100644
@@ -141,8 +141,9 @@ enum perf_event_sample_format {
   	PERF_SAMPLE_TRANSACTION			= 1U << 17,
   	PERF_SAMPLE_REGS_INTR			= 1U << 18,
   	PERF_SAMPLE_PHYS_ADDR			= 1U << 19,
+	PERF_SAMPLE_LBR_TOS			= 1U << 20,
-	PERF_SAMPLE_MAX = 1U << 20,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 21,		/* non-ABI */
   	__PERF_SAMPLE_CALLCHAIN_EARLY		= 1ULL << 63, /* non-ABI; internal use */
   };

include/uapi/linux/perf_event.h enum perf_event_type { · line 871

Quoted 7 lines
include/uapi/linux/perf_event.h +1 -0
@@ -864,6 +865,7 @@ enum perf_event_type {
   	 *	{ u64			abi; # enum perf_sample_regs_abi
   	 *	  u64			regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
   	 *	{ u64			phys_addr;} && PERF_SAMPLE_PHYS_ADDR
+	 *	{ u64			tos;} && PERF_SAMPLE_LBR_TOS
   	 * };
   	 */
   	PERF_RECORD_SAMPLE			= 9,

I have problems with the API.. You're introducing the intel specific LBR naming, and adding a whole new sample type vs extending the existing BRANCH_STACK (like you really already do with struct perf_branch_stack). > So why not add a bit to PERF_SAMPLE_BRANCH_* to request the presence of the TOS field in the PERF_SAMPLE_BRANCH_STACK output?

Quoted 7 lines
Quoted 2 lines

We never store PERF_SAMPLE_BRANCH_* in a sample. The perf tool cannot tell if the sample includes TOS field.

The perf tool bloody sets the perf_event_attr::branch_sample_type value! Of course it knows to expect the TOS field when it asks for it in the first place.

Users may generate the perf.data on one machine, and parse the data on another machine.
If the perf.data is from a new kernel with a new perf tool on one machine, but users have an old perf tool on another machine to parse it. The old perf tool doesn't know the exists of TOS field.

Thanks,
Kan

Quoted 6 lines
Quoted 2 lines

There will be a problem when a new perf tool parsing the data generated by an old kernel.

ISTR perf stores the full perf_event_attr in the .data file these days, and therefore such confusion should never happen.

maintainer
#6 lore raw

Re: [PATCH 01/10] perf/core, x86: Add PERF_SAMPLE_LBR_TOS

Quoted 10 lines · Kan
Quoted 4 lines

The perf tool bloody sets the perf_event_attr::branch_sample_type value! Of course it knows to expect the TOS field when it asks for it in the first place.

Users may generate the perf.data on one machine, and parse the data on another machine.
If the perf.data is from a new kernel with a new perf tool on one machine, but users have an old perf tool on another machine to parse it. The old perf tool doesn't know the exists of TOS field.

Feh, the tool should check for unknown input bits in attr.

Anyway, the proposed API is horrendous crap, that's just not going to happen.