perf, intel: Add support for PEBS output to Intel PT
Subsystems (2)
Alexander Shishkin
reviewercover perf, intel: Add support for PEBS output to Intel PT
Hi Peter,
New PEBS feature: output to Intel PT stream instead of the DS area. It's theoretically useful in virtualized environments, where DS area can't be used. It's also good for those who are interested in instruction trace for context of the PEBS events. As PEBS goes, it can provide LBR context with all the branch-related information that PT doesn't provide at the moment.
PEBS records are packetized in the PT stream, so instead of extracting them in the PMI, we leave it to the perf tool, because real time PT decoding is not practical. Tooling patches are not included, but can be found here [1].
Added is an attribute bit 'aux_source' to mean that an event is a source of AUX data. This bit enables PEBS output to PT.
[1] http://git.infradead.org/users/ahunter/linux-perf.git
Alexander Shishkin (2):
perf: Allow normal events to be sources of AUX data perf/x86/intel: Support PEBS output to PT
arch/x86/events/intel/core.c | 13 +++++++ arch/x86/events/intel/ds.c | 59 +++++++++++++++++++++++++++++++- arch/x86/events/perf_event.h | 9 +++++ arch/x86/include/asm/msr-index.h | 3 ++ include/uapi/linux/perf_event.h | 3 +- kernel/events/core.c | 9 +++++ 6 files changed, 94 insertions(+), 2 deletions(-)
Signature · 1 line
2.20.1
Alexander Shishkin
reviewerPATCH 1/2 perf: Allow normal events to be sources of AUX data
In some cases, ordinary (non-AUX) events can generate data for AUX events. For example, PEBS events can come out as records in the Intel PT stream instead of their usual DS records, if configured to do so.
Add an attribute bit for such events to be configured to generate AUX data.
--- include/uapi/linux/perf_event.h | 3 ++- kernel/events/core.c | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-)
include/uapi/linux/perf_event.h +2 -1
@@ -374,7 +374,8 @@ struct perf_event_attr { namespaces : 1, /* include namespaces data */ ksymbol : 1, /* include ksymbol events */ bpf_event : 1, /* include bpf events */ - __reserved_1 : 33; + aux_source : 1, /* generate AUX records instead of events */ + __reserved_1 : 32;union {__u32 wakeup_events;/* wakeup every n events */
kernel/events/core.c +9 -0
@@ -10382,6 +10382,15 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, goto err_ns; } +/* +* If it's set at this point, the PMU didn't handle it, so +* it doesn't do it. +*/ +if (event->attr.aux_source) { + err = -EOPNOTSUPP ; + goto err_pmu; + } + err =exclusive_event_init (event);if (err) goto err_pmu;
Signature · 1 line
2.20.1
Alexander Shishkin
reviewerPATCH 2/2 perf/x86/intel: Support PEBS output to PT
In reply to Alexander ShishkinIf PEBS declares ability to output its data to Intel PT stream, use the aux_source attribute bit to enable PEBS data output to PT. This requires a PT event to be present and scheduled in the same context. Unlike the DS area, the kernel does not extract PEBS records from the PT stream to generate corresponding records in the perf stream, because that would require real time in-kernel PT decoding, which is not feasible. The PMI, however, can still be used.
The output setting is per-CPU, so all PEBS events must be either writing to PT or to the DS area, so in order to not mess up the event scheduling, we fall back to the latter in case both types of events are scheduled in.
--- arch/x86/events/intel/core.c | 13 +++++++ arch/x86/events/intel/ds.c | 59 +++++++++++++++++++++++++++++++- arch/x86/events/perf_event.h | 9 +++++ arch/x86/include/asm/msr-index.h | 3 ++ 4 files changed, 83 insertions(+), 1 deletion(-)
arch/x86/events/intel/core.c +13 -0
@@ -3291,6 +3291,19 @@ static int intel_pmu_hw_config(struct perf_event *event) } } +if (event->attr.aux_source) { +if (!event->attr.precise_ip) +return -EINVAL ; + +if (!x86_pmu.intel_cap.pebs_output_pt_available) +return -EOPNOTSUPP ; + + event->hw.flags |=PERF_X86_EVENT_PEBS_VIA_PT ; + +/* Signal to the core that we handled it */ + event->attr.aux_source =0 ; + } +if (event->attr.type !=PERF_TYPE_RAW )return 0 ;
arch/x86/events/intel/ds.c +58 -1
111 lines not on this page · raw message
arch/x86/events/perf_event.h +9 -0
@@ -76,6 +76,7 @@ static inline bool constraint_match(struct event_constraint *c, u64 ecode)#define PERF_X86_EVENT_EXCL_ACCT 0x0100/* accounted EXCL event */ #define PERF_X86_EVENT_AUTO_RELOAD 0x0200/* use PEBS auto-reload */ #define PERF_X86_EVENT_LARGE_PEBS 0x0400/* use large PEBS */ +#define PERF_X86_EVENT_PEBS_VIA_PT 0x0800/* use PT buffer for PEBS */ struct amd_nb {int nb_id;/* NorthBridge id */
@@ -85,6 +86,11 @@ struct amd_nb { };#define PEBS_COUNTER_MASK ((1ULL << MAX_PEBS_EVENTS) - 1) +#define PEBS_PMI_AFTER_EACH_RECORD BIT_ULL(60) +#define PEBS_OUTPUT_OFFSET 61 +#define PEBS_OUTPUT_MASK (3ull << PEBS_OUTPUT_OFFSET) +#define PEBS_OUTPUT_PT (1ull << PEBS_OUTPUT_OFFSET) +#define PEBS_VIA_PT_MASK (PEBS_OUTPUT_PT | PEBS_PMI_AFTER_EACH_RECORD)/* * Flags PEBS can handle without an PMI.
@@ -229,6 +235,7 @@ struct cpu_hw_events {u64 pebs_enabled;int n_pebs;int n_large_pebs; +int n_pebs_via_pt;/* Current super set of events hardware configuration */ u64 pebs_data_cfg;
@@ -528,6 +535,8 @@ union perf_capabilities {*/ u64 full_width_write:1; u64 pebs_baseline:1; + u64 pebs_metrics_available:1; + u64 pebs_output_pt_available:1; };u64 capabilities; };
arch/x86/include/asm/msr-index.h +3 -0
@@ -782,6 +782,9 @@#define MSR_CORE_PERF_GLOBAL_CTRL 0x0000038f#define MSR_CORE_PERF_GLOBAL_OVF_CTRL 0x00000390 +#define MSR_RELOAD_PMC0 0x000014c1 +#define MSR_RELOAD_FIXED_CTR0 0x00001309 +/* Geode defined MSRs */ #define MSR_GEODE_BUSCONT_CONF0 0x00001900
Signature · 1 line
2.20.1
Peter Zijlstra
maintainerRe: [PATCH 2/2] perf/x86/intel: Support PEBS output to PT
Quoted 29 lines · Alexander Shishkin
If PEBS declares ability to output its data to Intel PT stream, use the
aux_sourceattribute bit to enable PEBS data output to PT. This requires a PT event to be present and scheduled in the same context. Unlike the DS area, the kernel does not extract PEBS records from the PT stream to generate corresponding records in the perf stream, because that would require real time in-kernel PT decoding, which is not feasible. The PMI, however, can still be used.The output setting is per-CPU, so all PEBS events must be either writing to PT or to the DS area, so in order to not mess up the event scheduling, we fall back to the latter in case both types of events are scheduled in.
Signed-off-by Alexander Shishkin <[email protected]>--- arch/x86/events/intel/core.c | 13 +++++++ arch/x86/events/intel/ds.c | 59 +++++++++++++++++++++++++++++++- arch/x86/events/perf_event.h | 9 +++++ arch/x86/include/asm/msr-index.h | 3 ++ 4 files changed, 83 insertions(+), 1 deletion(-)arch/x86/events/intel/core.c +1
-0@@ -3291,6 +3291,19 @@ static int intel_pmu_hw_config(struct perf_event *event) } } +if (event->attr.aux_source) {
arch/x86/events/intel/core.c static int intel_pmu_hw_config(struct perf_event *event) · line 3304
Quoted 10 lines
arch/x86/events/intel/core.c +10
-0+if (!event->attr.precise_ip) +return -EINVAL ; + +if (!x86_pmu.intel_cap.pebs_output_pt_available) +return -EOPNOTSUPP ; + + event->hw.flags |=PERF_X86_EVENT_PEBS_VIA_PT ; + +/* Signal to the core that we handled it */ + event->attr.aux_source =0 ;
That's a bit yuck, ideally we'd not modify attrs.
Can't we change that test in perf_event_alloc() to something like:
if (event->attr.aux_source && !(pmu->capabilities & PERF_PMU_CAP_AUX_SOURCE)) // error
?
Quoted 1 line
arch/x86/events/intel/core.c +1
-0+ }
Peter Zijlstra
maintainerRe: [PATCH 2/2] perf/x86/intel: Support PEBS output to PT
In reply to Alexander ShishkinQuoted 3 lines · Alexander Shishkin
The output setting is per-CPU, so all PEBS events must be either writing to PT or to the DS area, so in order to not mess up the event scheduling, we fall back to the latter in case both types of events are scheduled in.
Urgh, that blows... I really don't like that.
Peter Zijlstra
maintainerRe: [PATCH 2/2] perf/x86/intel: Support PEBS output to PT
In reply to Alexander ShishkinQuoted 46 lines · Alexander Shishkin
The output setting is per-CPU, so all PEBS events must be either writing to PT or to the DS area, so in order to not mess up the event scheduling, we fall back to the latter in case both types of events are scheduled in.
arch/x86/events/intel/ds.c +38
-0+static void intel_pmu_pebs_via_pt_disable (struct perf_event *event) +{ +struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); + +if (!(event->hw.flags &PERF_X86_EVENT_PEBS_VIA_PT )) +return ; + +if (!(cpuc->pebs_enabled & ~PEBS_VIA_PT_MASK )) + cpuc->pebs_enabled &= ~PEBS_VIA_PT_MASK ; +} + +static void intel_pmu_pebs_via_pt_enable (struct perf_event *event) +{ +struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events); +struct hw_perf_event *hwc = &event->hw; +struct debug_store *ds = cpuc->ds; + +if (!(event->hw.flags &PERF_X86_EVENT_PEBS_VIA_PT )) +return ; + +/* +* In case there's a mix of PEBS->PT and PEBS->DS, fall back +* to DS. +*/ +if (cpuc->n_pebs != cpuc->n_pebs_via_pt) { +/* PEBS-to-DS events present, fall back to DS */ +intel_pmu_pebs_via_pt_disable (event); +return ; + } + +if (!(event->hw.flags &PERF_X86_EVENT_LARGE_PEBS )) + cpuc->pebs_enabled |=PEBS_PMI_AFTER_EACH_RECORD ; + + cpuc->pebs_enabled |=PEBS_OUTPUT_PT ; + +wrmsrl (MSR_RELOAD_PMC0 + hwc->idx, ds->pebs_event_reset[hwc->idx]); +} +void intel_pmu_pebs_enable (struct perf_event *event) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);
arch/x86/events/intel/ds.c void intel_pmu_pebs_enable(struct perf_event *event) · line 1151
Quoted 6 lines
arch/x86/events/intel/ds.c +2
-0@@ -1100,6 +1146,8 @@ void intel_pmu_pebs_enable(struct perf_event *event) }else { ds->pebs_event_reset[hwc->idx] =0 ; } + +intel_pmu_pebs_via_pt_enable (event); }
I think that doesn't even do what it says on the tin. Suppose you first schedule that PEBS-via-PT event and then the normal one, nothing then cancels the PT link.
Like I wrote in that prevoius email; I really don't like this. I think silently falling back to another output method is wrong.
Ideally we create schedulig conflicts and cause the PT and DS events to round robin.
Alexander Shishkin
reviewerQuoted 8 lines · Alexander Shishkin
Hi Peter,
New PEBS feature: output to Intel PT stream instead of the DS area. It's theoretically useful in virtualized environments, where DS area can't be used. It's also good for those who are interested in instruction trace for context of the PEBS events. As PEBS goes, it can provide LBR context with all the branch-related information that PT doesn't provide at the moment.
Quoted 7 lines
PEBS records are packetized in the PT stream, so instead of extracting them in the PMI, we leave it to the perf tool, because real time PT decoding is not practical. Tooling patches are not included, but can be found here [1].
Added is an attribute bit '
aux_source' to mean that an event is a source of AUX data. This bit enables PEBS output to PT.
Forgot to CC Kan.
Regards,
--
Alex
Peter Zijlstra
maintainerQuoted 8 lines · Alexander Shishkin
Hi Peter,
New PEBS feature: output to Intel PT stream instead of the DS area. It's theoretically useful in virtualized environments, where DS area can't be used. It's also good for those who are interested in instruction trace for context of the PEBS events. As PEBS goes, it can provide LBR context with all the branch-related information that PT doesn't provide at the moment.
Quoted 7 lines
PEBS records are packetized in the PT stream, so instead of extracting them in the PMI, we leave it to the perf tool, because real time PT decoding is not practical. Tooling patches are not included, but can be found here [1].
Added is an attribute bit '
aux_source' to mean that an event is a source of AUX data. This bit enables PEBS output to PT.
There is a distinct lack of permission checks in this. When creating this construct we should verify the creator has access to PT. And we should verify we're not (accidentally or otherwise) writing into someone else's PT buffers.