Skip to contentSkip to conversation

perf/x86/intel/pt: Add new packet enables

PATCH In review Alexander Shishkin 3 participants 21 messages Jul 17, 2015 – Sep 13, 2015

Subsystems (2)
lore
Share & tools
lore

Alexander Shishkin

author reviewer
#0 lore raw

cover perf/x86/intel/pt: Add new packet enables

Hi Peter,

There are new PT packets and handles to enable and configure them in the new version if Intel Architecture SDM. This series deals with timing related packets: adds bit definitions, cpuid enumeration and relevant userspace-visible parts.

The bulk of work, as usual, is in the tooling, which I don't include in this patchset, since it depends on Adrian's earlier work which is still bouncing between him and the maintainers, but for the sake of completeness I pushed it to my github tree [1] and Adrian is also sending it out for review. So anybody interested in trying the whole thing out can pull it from my tree.

This time around, however, there is no compile time dependency between mine and his series, since all the relevant bits are communicated through sysfs.

[1] https://github.com/virtuoso/linux-perf/commits/intel_pt

Alexander Shishkin (3):
perf/x86/intel/pt: Add new timing packet enables perf/x86/intel/pt: Add an option to not force PSB+ on every
schedule-in
perf/x86/intel/bts: Set itrace_started in pmu::start to match the new
logic

 arch/x86/include/asm/msr-index.h           |  8 +++
 arch/x86/kernel/cpu/intel_pt.h             |  6 +++
 arch/x86/kernel/cpu/perf_event.h           |  1 +
 arch/x86/kernel/cpu/perf_event_intel_bts.c |  1 +
 arch/x86/kernel/cpu/perf_event_intel_pt.c  | 78 ++++++++++++++++++++++++++++--
 kernel/events/core.c                       |  2 -
 6 files changed, 91 insertions(+), 5 deletions(-)
Signature · 1 line
2.1.4
author reviewer
#1 lore raw

PATCH 1/3 perf/x86/intel/pt: Add new timing packet enables

Intel PT chapter in the new Intel Architecture SDM adds several packets corresponding enable bits and registers that control packet generation. Also, additional bits in the Intel PT CPUID leaf were added to enumerate presence and parameters of these new packets and features.

The packets and enables are:
* CYC: cycle accurate mode, provides the number of cycles elapsed since previous CYC packet; its presence and available threshold values are enumerated via CPUID;
* MTC: mini time counter packets, used for tracking TSC time between full TSC packets; its presence and available resolution options are enumerated via CPUID;
* PSB packet period is now configurable, available period values are enumerated via CPUID.

This patch adds corresponding bit and register definitions, pmu driver capabilities based on CPUID enumeration, new attribute format bits for the new featurens and extends event configuration validation function to take these into account.

---
 arch/x86/include/asm/msr-index.h          |  8 +++++
 arch/x86/kernel/cpu/intel_pt.h            |  6 ++++
 arch/x86/kernel/cpu/perf_event_intel_pt.c | 56 ++++++++++++++++++++++++++++++-
 3 files changed, 69 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +8 -0
index 9ebc3d0093..c665d34f72 100644
@@ -80,13 +80,21 @@
 
 #define MSR_IA32_RTIT_CTL		0x00000570
 #define RTIT_CTL_TRACEEN		BIT(0)
+#define RTIT_CTL_CYCLEACC		BIT(1)
 #define RTIT_CTL_OS			BIT(2)
 #define RTIT_CTL_USR			BIT(3)
 #define RTIT_CTL_CR3EN			BIT(7)
 #define RTIT_CTL_TOPA			BIT(8)
+#define RTIT_CTL_MTC_EN			BIT(9)
 #define RTIT_CTL_TSC_EN			BIT(10)
 #define RTIT_CTL_DISRETC		BIT(11)
 #define RTIT_CTL_BRANCH_EN		BIT(13)
+#define RTIT_CTL_MTC_RANGE_OFFSET	14
+#define RTIT_CTL_MTC_RANGE		(0x0full << RTIT_CTL_MTC_RANGE_OFFSET)
+#define RTIT_CTL_CYC_THRESH_OFFSET	19
+#define RTIT_CTL_CYC_THRESH		(0x0full << RTIT_CTL_CYC_THRESH_OFFSET)
+#define RTIT_CTL_PSB_FREQ_OFFSET	24
+#define RTIT_CTL_PSB_FREQ      		(0x0full << RTIT_CTL_PSB_FREQ_OFFSET)
 #define MSR_IA32_RTIT_STATUS		0x00000571
 #define RTIT_STATUS_CONTEXTEN		BIT(1)
 #define RTIT_STATUS_TRIGGEREN		BIT(2)
arch/x86/kernel/cpu/intel_pt.h +6 -0
index 1c338b0eba..feb293e965 100644
@@ -72,9 +72,15 @@ struct topa_entry {
 enum pt_capabilities {
 	PT_CAP_max_subleaf = 0,
 	PT_CAP_cr3_filtering,
+	PT_CAP_psb_cyc,
+	PT_CAP_mtc,
 	PT_CAP_topa_output,
 	PT_CAP_topa_multiple_entries,
+	PT_CAP_single_range_output,
 	PT_CAP_payloads_lip,
+	PT_CAP_mtc_periods,
+	PT_CAP_cycle_thresholds,
+	PT_CAP_psb_periods,
 };
 
 struct pt_pmu {
arch/x86/kernel/cpu/perf_event_intel_pt.c +55 -1

91 lines not on this page · raw message

Signature · 1 line
2.1.4
maintainer
#2 lore raw

Re: [PATCH 1/3] perf/x86/intel/pt: Add new timing packet enables

arch/x86/kernel/cpu/perf_event_intel_pt.c fail: · line 195

Quoted 8 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c +7 -0
+#define PT_CONFIG_MASK (RTIT_CTL_TSC_EN     |	\
+			RTIT_CTL_DISRETC    |	\
+			RTIT_CTL_CYCLEACC   |	\
+			RTIT_CTL_MTC_EN     |	\
+			RTIT_CTL_MTC_RANGE  |	\
+			RTIT_CTL_CYC_THRESH |   \
+			RTIT_CTL_PSB_FREQ)
#define RTIT_CTL_CYC (RTIT_CTL_CYCLEACC   | \
		      RTIT_CTL_CYC_THRESH | \
		      RTIT_CTL_PSB_FREQ)

#define RTIT_CTL_MTC (RTIT_CTL_MTC_EN | \
RTIT_CTL_MTC_RANGE)

#define PT_CONFIG_MASK (RTIT_CTL_TSC_EN     | \
			RTIT_CTL_DISRETC    | \
			RTIT_CTL_CYC	    | \
			RTIT_CTL_MTC)

arch/x86/kernel/cpu/perf_event_intel_pt.c fail: · line 206

Quoted 10 lines

static bool pt_event_valid(struct perf_event *event) {

arch/x86/kernel/cpu/perf_event_intel_pt.c +3 -0
 	u64 config = event->attr.config;
+	u64 allowed, requested;
 
 	if ((config & PT_CONFIG_MASK) != config)
 		return false;
 
+	if (config &
+	    (RTIT_CTL_CYCLEACC | RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ)) {
	if (config & RTIT_CTL_CYC) {
Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c +7 -0
+		if (!pt_cap_get(PT_CAP_psb_cyc))
+			return false;
+
+		allowed = pt_cap_get(PT_CAP_psb_periods);
+		requested = (config & RTIT_CTL_PSB_FREQ) >>
+			RTIT_CTL_PSB_FREQ_OFFSET;
+		if (requested && (!(allowed & BIT(requested))))

arch/x86/kernel/cpu/perf_event_intel_pt.c fail: · line 223

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c +10 -0
+			return false;
+
+		allowed = pt_cap_get(PT_CAP_cycle_thresholds);
+		requested = (config & RTIT_CTL_CYC_THRESH) >>
+			RTIT_CTL_CYC_THRESH_OFFSET;
+		if (requested && (!(allowed & BIT(requested))))
+			return false;
+	}
+
+	if (config & (RTIT_CTL_MTC_EN | RTIT_CTL_MTC_RANGE)) {
	if (config & RTIT_CTL_MTC) {
Quoted 4 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c +4 -0
+		allowed = pt_cap_get(PT_CAP_mtc_periods);
+
+		if (!allowed)
+			return false;

arch/x86/kernel/cpu/perf_event_intel_pt.c fail: · line 237

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c +8 -0
+
+		requested = (config & RTIT_CTL_MTC_RANGE) >>
+			RTIT_CTL_MTC_RANGE_OFFSET;
+
+		if (!(allowed & BIT(requested)))
+			return false;
+	}
+
 	return true;
 }

Would that make sense?

author reviewer
#3 lore raw

Re: [PATCH 1/3] perf/x86/intel/pt: Add new timing packet enables

Quoted 13 lines · Peter Zijlstra

arch/x86/kernel/cpu/perf_event_intel_pt.c fail: · line 195

Quoted 8 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c +7 -0
+#define PT_CONFIG_MASK (RTIT_CTL_TSC_EN     |	\
+			RTIT_CTL_DISRETC    |	\
+			RTIT_CTL_CYCLEACC   |	\
+			RTIT_CTL_MTC_EN     |	\
+			RTIT_CTL_MTC_RANGE  |	\
+			RTIT_CTL_CYC_THRESH |   \
+			RTIT_CTL_PSB_FREQ)
#define RTIT_CTL_CYC (RTIT_CTL_CYCLEACC   | \
		      RTIT_CTL_CYC_THRESH | \
		      RTIT_CTL_PSB_FREQ)

PSB_FREQ is not, strictly speaking, related to cycle accurate mode. Both adjustable psb frequency and cycle accurate mode settings are enumerated with the same CPUID bit, but they really do different things unrelated to one another.

Quoted 5 lines

arch/x86/kernel/cpu/perf_event_intel_pt.c fail: · line 223

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_pt.c +1 -0
+	if (config & (RTIT_CTL_MTC_EN | RTIT_CTL_MTC_RANGE)) {
	if (config & RTIT_CTL_MTC) {

Would that make sense?

To me either way is fine. Want me to respin it?

Regards,
--
Alex

maintainer
#4 lore raw

Re: [PATCH 1/3] perf/x86/intel/pt: Add new timing packet enables

Quoted 16 lines · Alexander Shishkin
Quoted 13 lines · Peter Zijlstra

arch/x86/kernel/cpu/perf_event_intel_pt.c fail: · line 195

Quoted 8 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c +7 -0
+#define PT_CONFIG_MASK (RTIT_CTL_TSC_EN     |	\
+			RTIT_CTL_DISRETC    |	\
+			RTIT_CTL_CYCLEACC   |	\
+			RTIT_CTL_MTC_EN     |	\
+			RTIT_CTL_MTC_RANGE  |	\
+			RTIT_CTL_CYC_THRESH |   \
+			RTIT_CTL_PSB_FREQ)
#define RTIT_CTL_CYC (RTIT_CTL_CYCLEACC   | \
		      RTIT_CTL_CYC_THRESH | \
		      RTIT_CTL_PSB_FREQ)
Quoted 4 lines

PSB_FREQ is not, strictly speaking, related to cycle accurate mode. Both adjustable psb frequency and cycle accurate mode settings are enumerated with the same CPUID bit, but they really do different things unrelated to one another.

RTIT_CTL_CYC_PSB then, to match the CPUID bit name?

Quoted 7 lines
Quoted 5 lines

arch/x86/kernel/cpu/perf_event_intel_pt.c fail: · line 223

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel_pt.c +1 -0
+	if (config & (RTIT_CTL_MTC_EN | RTIT_CTL_MTC_RANGE)) {
	if (config & RTIT_CTL_MTC) {

Would that make sense?

To me either way is fine. Want me to respin it?

Please.

author reviewer
#5 lore raw

PATCH 1/1 perf/x86/intel/pt: Add new timing packet enables

Intel PT chapter in the new Intel Architecture SDM adds several packets corresponding enable bits and registers that control packet generation. Also, additional bits in the Intel PT CPUID leaf were added to enumerate presence and parameters of these new packets and features.

The packets and enables are:
* CYC: cycle accurate mode, provides the number of cycles elapsed since previous CYC packet; its presence and available threshold values are enumerated via CPUID;
* MTC: mini time counter packets, used for tracking TSC time between full TSC packets; its presence and available resolution options are enumerated via CPUID;
* PSB packet period is now configurable, available period values are enumerated via CPUID.

This patch adds corresponding bit and register definitions, pmu driver capabilities based on CPUID enumeration, new attribute format bits for the new featurens and extends event configuration validation function to take these into account.

---
 arch/x86/include/asm/msr-index.h          |  8 ++++
 arch/x86/kernel/cpu/intel_pt.h            |  6 +++
 arch/x86/kernel/cpu/perf_event_intel_pt.c | 68 ++++++++++++++++++++++++++++++-
 3 files changed, 81 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +8 -0
index 9ebc3d0093..c665d34f72 100644
@@ -80,13 +80,21 @@
 
 #define MSR_IA32_RTIT_CTL		0x00000570
 #define RTIT_CTL_TRACEEN		BIT(0)
+#define RTIT_CTL_CYCLEACC		BIT(1)
 #define RTIT_CTL_OS			BIT(2)
 #define RTIT_CTL_USR			BIT(3)
 #define RTIT_CTL_CR3EN			BIT(7)
 #define RTIT_CTL_TOPA			BIT(8)
+#define RTIT_CTL_MTC_EN			BIT(9)
 #define RTIT_CTL_TSC_EN			BIT(10)
 #define RTIT_CTL_DISRETC		BIT(11)
 #define RTIT_CTL_BRANCH_EN		BIT(13)
+#define RTIT_CTL_MTC_RANGE_OFFSET	14
+#define RTIT_CTL_MTC_RANGE		(0x0full << RTIT_CTL_MTC_RANGE_OFFSET)
+#define RTIT_CTL_CYC_THRESH_OFFSET	19
+#define RTIT_CTL_CYC_THRESH		(0x0full << RTIT_CTL_CYC_THRESH_OFFSET)
+#define RTIT_CTL_PSB_FREQ_OFFSET	24
+#define RTIT_CTL_PSB_FREQ      		(0x0full << RTIT_CTL_PSB_FREQ_OFFSET)
 #define MSR_IA32_RTIT_STATUS		0x00000571
 #define RTIT_STATUS_CONTEXTEN		BIT(1)
 #define RTIT_STATUS_TRIGGEREN		BIT(2)
arch/x86/kernel/cpu/intel_pt.h +6 -0
index 1c338b0eba..feb293e965 100644
@@ -72,9 +72,15 @@ struct topa_entry {
 enum pt_capabilities {
 	PT_CAP_max_subleaf = 0,
 	PT_CAP_cr3_filtering,
+	PT_CAP_psb_cyc,
+	PT_CAP_mtc,
 	PT_CAP_topa_output,
 	PT_CAP_topa_multiple_entries,
+	PT_CAP_single_range_output,
 	PT_CAP_payloads_lip,
+	PT_CAP_mtc_periods,
+	PT_CAP_cycle_thresholds,
+	PT_CAP_psb_periods,
 };
 
 struct pt_pmu {
arch/x86/kernel/cpu/perf_event_intel_pt.c +67 -1

103 lines not on this page · raw message

Signature · 1 line
2.4.6
#6 lore raw

[tip:perf/core] perf/x86/intel/pt: Add new timing packet enables

perf/x86/intel/pt: Add new timing packet enables
Commit-ID:  b1bf72d6691cc33fc7763fc8ec77df42ca1a8702
Gitweb:     http://git.kernel.org/tip/b1bf72d6691cc33fc7763fc8ec77df42ca1a8702
Author:     Alexander Shishkin <[email protected]>
AuthorDate: Thu, 30 Jul 2015 16:15:31 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 4 Aug 2015 10:16:55 +0200

perf/x86/intel/pt: Add new timing packet enables

Intel PT chapter in the new Intel Architecture SDM adds several packets corresponding enable bits and registers that control packet generation. Also, additional bits in the Intel PT CPUID leaf were added to enumerate presence and parameters of these new packets and features.

The packets and enables are:

* CYC: cycle accurate mode, provides the number of cycles elapsed since previous CYC packet; its presence and available threshold values are enumerated via CPUID;

* MTC: mini time counter packets, used for tracking TSC time between full TSC packets; its presence and available resolution options are enumerated via CPUID;

* PSB packet period is now configurable, available period values are enumerated via CPUID.

This patch adds corresponding bit and register definitions, pmu driver capabilities based on CPUID enumeration, new attribute format bits for the new featurens and extends event configuration validation function to take these into account.

---
 arch/x86/include/asm/msr-index.h          |  8 ++++
 arch/x86/kernel/cpu/intel_pt.h            |  6 +++
 arch/x86/kernel/cpu/perf_event_intel_pt.c | 68 ++++++++++++++++++++++++++++++-
 3 files changed, 81 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +8 -0
index 9ebc3d0..c665d34 100644
@@ -80,13 +80,21 @@
 
 #define MSR_IA32_RTIT_CTL		0x00000570
 #define RTIT_CTL_TRACEEN		BIT(0)
+#define RTIT_CTL_CYCLEACC		BIT(1)
 #define RTIT_CTL_OS			BIT(2)
 #define RTIT_CTL_USR			BIT(3)
 #define RTIT_CTL_CR3EN			BIT(7)
 #define RTIT_CTL_TOPA			BIT(8)
+#define RTIT_CTL_MTC_EN			BIT(9)
 #define RTIT_CTL_TSC_EN			BIT(10)
 #define RTIT_CTL_DISRETC		BIT(11)
 #define RTIT_CTL_BRANCH_EN		BIT(13)
+#define RTIT_CTL_MTC_RANGE_OFFSET	14
+#define RTIT_CTL_MTC_RANGE		(0x0full << RTIT_CTL_MTC_RANGE_OFFSET)
+#define RTIT_CTL_CYC_THRESH_OFFSET	19
+#define RTIT_CTL_CYC_THRESH		(0x0full << RTIT_CTL_CYC_THRESH_OFFSET)
+#define RTIT_CTL_PSB_FREQ_OFFSET	24
+#define RTIT_CTL_PSB_FREQ      		(0x0full << RTIT_CTL_PSB_FREQ_OFFSET)
 #define MSR_IA32_RTIT_STATUS		0x00000571
 #define RTIT_STATUS_CONTEXTEN		BIT(1)
 #define RTIT_STATUS_TRIGGEREN		BIT(2)
arch/x86/kernel/cpu/intel_pt.h +6 -0
index 1c338b0..feb293e 100644
@@ -72,9 +72,15 @@ struct topa_entry {
 enum pt_capabilities {
 	PT_CAP_max_subleaf = 0,
 	PT_CAP_cr3_filtering,
+	PT_CAP_psb_cyc,
+	PT_CAP_mtc,
 	PT_CAP_topa_output,
 	PT_CAP_topa_multiple_entries,
+	PT_CAP_single_range_output,
 	PT_CAP_payloads_lip,
+	PT_CAP_mtc_periods,
+	PT_CAP_cycle_thresholds,
+	PT_CAP_psb_periods,
 };
 
 struct pt_pmu {
arch/x86/kernel/cpu/perf_event_intel_pt.c +67 -1

103 lines not on this page · raw message

author reviewer
#7 lore raw

PATCH 2/3 perf/x86/intel/pt: Add an option to not force PSB+ on every schedule-in

In reply to Alexander Shishkin

Currently, the PT driver zeroes out the status register every time before starting the event. However, all the writable bits are already taken care of in pt_handle_status() function, except the new PacketByteCnt field, which in new versions of PT contains the number of packet bytes written since the last sync (PSB) packet. Zeroing it out before enabling PT forces a sync packet to be written. This means that, with the existing code, a sync packet (PSB and PSBEND, 18 bytes in total) will be generated every time a PT event is scheduled in.

To avoid these unnecessary syncs and save a WRMSR in the fast path, this patch adds a new attribute config bit "no_force_psb", which will disable this zeroing WRMSR.

One exception where we do need to force PSB like this when tracing starts, so that the decoder has a clear sync point in the trace. For this purpose we aready have hw::itrace_started flag, which we are currently using to output PERF_RECORD_ITRACE_START. This patch moves setting itrace_started from perf core to the pmu::start, where it should still be 0 on the very first run.

---
 arch/x86/kernel/cpu/perf_event.h          |  1 +
 arch/x86/kernel/cpu/perf_event_intel_pt.c | 22 ++++++++++++++++++++--
 kernel/events/core.c                      |  2 --
 3 files changed, 21 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/perf_event.h +1 -0
index 3474cf26b6..d7aa6c5630 100644
@@ -77,6 +77,7 @@ struct event_constraint {
 #define PERF_X86_EVENT_EXCL_ACCT	0x0200 /* accounted EXCL event */
 #define PERF_X86_EVENT_AUTO_RELOAD	0x0400 /* use PEBS auto-reload */
 #define PERF_X86_EVENT_FREERUNNING	0x0800 /* use freerunning PEBS */
+#define PERF_X86_EVENT_PT_NO_FORCE_PSB	0x1000 /* don't force PSB+ packets */
 
 
 struct amd_nb {
arch/x86/kernel/cpu/perf_event_intel_pt.c +20 -2
index aacd3c9bed..6c40455cd1 100644
@@ -100,6 +100,7 @@ static struct attribute_group pt_cap_group = {
 	.name	= "caps",
 };
 
+PMU_FORMAT_ATTR(no_force_psb,	"config:0"	);
 PMU_FORMAT_ATTR(cyc,		"config:1"	);
 PMU_FORMAT_ATTR(mtc,		"config:9"	);
 PMU_FORMAT_ATTR(tsc,		"config:10"	);
@@ -109,6 +110,7 @@ PMU_FORMAT_ATTR(cyc_thresh,	"config:19-22"	);
 PMU_FORMAT_ATTR(psb_period,	"config:24-27"	);
 
 static struct attribute *pt_formats_attr[] = {
+	&format_attr_no_force_psb.attr,
 	&format_attr_cyc.attr,
 	&format_attr_mtc.attr,
 	&format_attr_tsc.attr,
@@ -199,6 +201,18 @@ static bool pt_event_valid(struct perf_event *event)
 	u64 config = event->attr.config;
 	u64 allowed, requested;
 
+	/*
+	 * Re-use one of the config bits to enable resetting packet byte
+	 * count that will result in outputting PSB+ at every event enable.
+	 * Since we don't allow setting bit 0 (TraceEn) directly from
+	 * attr::config, we can re-use it for our purpose.
+	 */
+	if (config & BIT(0)) {
+		event->hw.flags |= PERF_X86_EVENT_PT_NO_FORCE_PSB;
+		event->attr.config &= ~BIT(0);
+		config = event->attr.config;
+	}
+
 	if ((config & PT_CONFIG_MASK) != config)
 		return false;
 
@@ -245,6 +259,12 @@ static void pt_config(struct perf_event *event)
 {
 	u64 reg;
 
+	if (!(event->hw.flags & PERF_X86_EVENT_PT_NO_FORCE_PSB) ||
+	    !(event->hw.itrace_started)) {
+		event->hw.itrace_started = 1;
+		wrmsrl(MSR_IA32_RTIT_STATUS, 0);
+	}
+
 	reg = RTIT_CTL_TOPA | RTIT_CTL_BRANCH_EN | RTIT_CTL_TRACEEN;
 
 	if (!event->attr.exclude_kernel)
@@ -964,7 +984,6 @@ void intel_pt_interrupt(void)
 
 		pt_config_buffer(buf->cur->table, buf->cur_idx,
 				 buf->output_off);
-		wrmsrl(MSR_IA32_RTIT_STATUS, 0);
 		pt_config(event);
 	}
 }
@@ -988,7 +1007,6 @@ static void pt_event_start(struct perf_event *event, int mode)
 
 	pt_config_buffer(buf->cur->table, buf->cur_idx,
 			 buf->output_off);
-	wrmsrl(MSR_IA32_RTIT_STATUS, 0);
 	pt_config(event);
 }
 
kernel/events/core.c +0 -2
index d3dae3419b..15da13237a 100644
@@ -6040,8 +6040,6 @@ static void perf_log_itrace_start(struct perf_event *event)
 	    event->hw.itrace_started)
 		return;
 
-	event->hw.itrace_started = 1;
-
 	rec.header.type	= PERF_RECORD_ITRACE_START;
 	rec.header.misc	= 0;
 	rec.header.size	= sizeof(rec);
Signature · 1 line
2.1.4
maintainer
#8 lore raw

Re: [PATCH 2/3] perf/x86/intel/pt: Add an option to not force PSB+ on every schedule-in

Quoted 9 lines · Alexander Shishkin

Currently, the PT driver zeroes out the status register every time before starting the event. However, all the writable bits are already taken care of in pt_handle_status() function, except the new PacketByteCnt field, which in new versions of PT contains the number of packet bytes written since the last sync (PSB) packet. Zeroing it out before enabling PT forces a sync packet to be written. This means that, with the existing code, a sync packet (PSB and PSBEND, 18 bytes in total) will be generated every time a PT event is scheduled in.

Quoted 3 lines

To avoid these unnecessary syncs and save a WRMSR in the fast path, this patch adds a new attribute config bit "no_force_psb", which will disable this zeroing WRMSR.

Why is this exposed?

author reviewer
#9 lore raw

Re: [PATCH 2/3] perf/x86/intel/pt: Add an option to not force PSB+ on every schedule-in

Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Alexander Shishkin

Currently, the PT driver zeroes out the status register every time before starting the event. However, all the writable bits are already taken care of in pt_handle_status() function, except the new PacketByteCnt field, which in new versions of PT contains the number of packet bytes written since the last sync (PSB) packet. Zeroing it out before enabling PT forces a sync packet to be written. This means that, with the existing code, a sync packet (PSB and PSBEND, 18 bytes in total) will be generated every time a PT event is scheduled in.

Quoted 5 lines
Quoted 3 lines

To avoid these unnecessary syncs and save a WRMSR in the fast path, this patch adds a new attribute config bit "no_force_psb", which will disable this zeroing WRMSR.

Why is this exposed?

The default behavior, which we have in the kernel now, is to always force the PSB, so if we change it, we need to make sure nobody is relying on it being the default behavior. Granted, for the hardware that's on the market right now it won't make a difference (it generates PSB+ at least every 256 bytes anyway), but in future if we change the default, newer hardware will produce different results with 4.2 than with the newer kernels and this would be a tad sloppy.

The tools, however, can decide to set no_force_psb=1 when no_force_psb is available (sysfs caps) and otherwise the default behavior will be the same as no_force_psb==0. Makes sense?

Regards,
--
Alex

maintainer
#10 lore raw

Re: [PATCH 2/3] perf/x86/intel/pt: Add an option to not force PSB+ on every schedule-in

Quoted 16 lines · Alexander Shishkin
Quoted 14 lines · Peter Zijlstra
Quoted 12 lines · Alexander Shishkin

Currently, the PT driver zeroes out the status register every time before starting the event. However, all the writable bits are already taken care of in pt_handle_status() function, except the new PacketByteCnt field, which in new versions of PT contains the number of packet bytes written since the last sync (PSB) packet. Zeroing it out before enabling PT forces a sync packet to be written. This means that, with the existing code, a sync packet (PSB and PSBEND, 18 bytes in total) will be generated every time a PT event is scheduled in.

To avoid these unnecessary syncs and save a WRMSR in the fast path, this patch adds a new attribute config bit "no_force_psb", which will disable this zeroing WRMSR.

Quoted 5 lines
Quoted 1 line

Why is this exposed?

The default behavior, which we have in the kernel now, is to always force the PSB, so if we change it, we need to make sure nobody is relying on it being the default behavior.

Seeing how there's hardly any tools out there for this -- and its only recently introduced to the kernel, I think we can change this behaviour.

Quoted 5 lines

Granted, for the hardware
that's on the market right now it won't make a difference (it generates PSB+ at least every 256 bytes anyway), but in future if we change the default, newer hardware will produce different results with 4.2 than with the newer kernels and this would be a tad sloppy.

Even more reason not to expose this. By the time there's hardware out there that supports this, 4.2 will be an ancient kernel :-)

Quoted 3 lines

The tools, however, can decide to set no_force_psb=1 when no_force_psb is available (sysfs caps) and otherwise the default behavior will be the same as no_force_psb==0. Makes sense?

I'd rather not expose this. It doesn't make a difference to the actual trace data other than it being bigger. Any decoder must be able to decode both versions. And you cannot really tell anything from there being too many PSB frames.

author reviewer
#11 lore raw

Re: [PATCH 2/3] perf/x86/intel/pt: Add an option to not force PSB+ on every schedule-in

Quoted 19 lines · Peter Zijlstra
Quoted 18 lines · Alexander Shishkin
Quoted 15 lines · Peter Zijlstra
Quoted 9 lines · Alexander Shishkin

Currently, the PT driver zeroes out the status register every time before starting the event. However, all the writable bits are already taken care of in pt_handle_status() function, except the new PacketByteCnt field, which in new versions of PT contains the number of packet bytes written since the last sync (PSB) packet. Zeroing it out before enabling PT forces a sync packet to be written. This means that, with the existing code, a sync packet (PSB and PSBEND, 18 bytes in total) will be generated every time a PT event is scheduled in.

Quoted 3 lines

To avoid these unnecessary syncs and save a WRMSR in the fast path, this patch adds a new attribute config bit "no_force_psb", which will disable this zeroing WRMSR.

Why is this exposed?

Quoted 7 lines
Quoted 3 lines

The default behavior, which we have in the kernel now, is to always force the PSB, so if we change it, we need to make sure nobody is relying on it being the default behavior.

Seeing how there's hardly any tools out there for this -- and its only recently introduced to the kernel, I think we can change this behaviour.

Well, aside from perf tool itself (I'm not sure what's been happening to the PT support there), there's gdb support, which has been merged to the gdb mainline some time ago and makes use of our perf interface. I don't know, however, if it relies on this or not.

Quoted 9 lines
Quoted 5 lines

Granted, for the hardware
that's on the market right now it won't make a difference (it generates PSB+ at least every 256 bytes anyway), but in future if we change the default, newer hardware will produce different results with 4.2 than with the newer kernels and this would be a tad sloppy.

Even more reason not to expose this. By the time there's hardware out there that supports this, 4.2 will be an ancient kernel :-)

Quoted 8 lines
Quoted 3 lines

The tools, however, can decide to set no_force_psb=1 when no_force_psb is available (sysfs caps) and otherwise the default behavior will be the same as no_force_psb==0. Makes sense?

I'd rather not expose this. It doesn't make a difference to the actual trace data other than it being bigger. Any decoder must be able to decode both versions. And you cannot really tell anything from there being too many PSB frames.

It kinda does make a difference: inside PSB+ you get a TSC packet, which carries the tsc timestamp as a payload, so, with the current behavior, since you'll get one of those at every event schedule-in, you'll have many more time synchronization points with the perf stream, but when the default changes, suddenly your timestamps way further apart and the decoder needs to put in additional guesswork to correlate those MMAP events in the perf stream with the PT trace.

So normally, instead of forcing the PSB+, one would enable MTC (mini time counter) packets, which would give better time reference points. If one knows to do this, one doesn't need extra PSBs, if one doesn't, but stuff sort of works for them because of the frequent PSBs, they will break after the default changes. The decoders, ideally, should eventually learn to deal with both cases, so in the long run it shouldn't matter at all. I just typed in all that to clarify exactly what the difference in trace data would mean.

Regards,
--
Alex

author reviewer
#12 lore raw

Re: [PATCH 2/3] perf/x86/intel/pt: Add an option to not force PSB+ on every schedule-in

Quoted 7 lines · Alexander Shishkin

It kinda does make a difference: inside PSB+ you get a TSC packet, which carries the tsc timestamp as a payload, so, with the current behavior, since you'll get one of those at every event schedule-in, you'll have many more time synchronization points with the perf stream, but when the default changes, suddenly your timestamps way further apart and the decoder needs to put in additional guesswork to correlate those MMAP events in the perf stream with the PT trace.

I spoke hastily, TSC packets will be generated on every schedule-in (as well as other packets that normally come in PSB+) regardless of the actual PSB, so scratch the above. Let me replace the above with a smaller patch.

Regards,
--
Alex

author reviewer
#13 lore raw

PATCH 1/1 perf/x86/intel/pt: Do not force sync packets on every schedule-in

In reply to Peter Zijlstra

Currently, the PT driver zeroes out the status register every time before starting the event. However, all the writable bits are already taken care of in pt_handle_status() function, except the new PacketByteCnt field, which in new versions of PT contains the number of packet bytes written since the last sync (PSB) packet. Zeroing it out before enabling PT forces a sync packet to be written. This means that, with the existing code, a sync packet (PSB and PSBEND, 18 bytes in total) will be generated every time a PT event is scheduled in.

To avoid these unnecessary syncs and save a WRMSR in the fast path, this patch changes the default behavior to not clear PacketByteCnt field, so that the sync packets will be generated with the period specified as "psb_period" attribute config field. This has little impact on the trace data as the other packets that are normally sent within PSB+ (between PSB and PSBEND) have their own generation scenarios which do not depend on the sync packets.

One exception where we do need to force PSB like this when tracing starts, so that the decoder has a clear sync point in the trace. For this purpose we aready have hw::itrace_started flag, which we are currently using to output PERF_RECORD_ITRACE_START. This patch moves setting itrace_started from perf core to the pmu::start, where it should still be 0 on the very first run.

perf/x86/intel/pt: fixup with no force psb

---
 arch/x86/kernel/cpu/perf_event_intel_pt.c | 7 +++++--
 kernel/events/core.c                      | 2 --
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_pt.c +5 -2
index 695fd6c562..e20cfacb5a 100644
@@ -257,6 +257,11 @@ static void pt_config(struct perf_event *event)
 {
 	u64 reg;
 
+	if (!event->hw.itrace_started) {
+		event->hw.itrace_started = 1;
+		wrmsrl(MSR_IA32_RTIT_STATUS, 0);
+	}
+
 	reg = RTIT_CTL_TOPA | RTIT_CTL_BRANCH_EN | RTIT_CTL_TRACEEN;
 
 	if (!event->attr.exclude_kernel)
@@ -976,7 +981,6 @@ void intel_pt_interrupt(void)
 
 		pt_config_buffer(buf->cur->table, buf->cur_idx,
 				 buf->output_off);
-		wrmsrl(MSR_IA32_RTIT_STATUS, 0);
 		pt_config(event);
 	}
 }
@@ -1000,7 +1004,6 @@ static void pt_event_start(struct perf_event *event, int mode)
 
 	pt_config_buffer(buf->cur->table, buf->cur_idx,
 			 buf->output_off);
-	wrmsrl(MSR_IA32_RTIT_STATUS, 0);
 	pt_config(event);
 }
 
kernel/events/core.c +0 -2
index d3dae3419b..15da13237a 100644
@@ -6040,8 +6040,6 @@ static void perf_log_itrace_start(struct perf_event *event)
 	    event->hw.itrace_started)
 		return;
 
-	event->hw.itrace_started = 1;
-
 	rec.header.type	= PERF_RECORD_ITRACE_START;
 	rec.header.misc	= 0;
 	rec.header.size	= sizeof(rec);
Signature · 1 line
2.4.6
author reviewer
#14 lore raw

Re: [PATCH v1] perf/x86/intel/pt: Do not force sync packets on every schedule-in

Quoted 24 lines · Alexander Shishkin

Currently, the PT driver zeroes out the status register every time before starting the event. However, all the writable bits are already taken care of in pt_handle_status() function, except the new PacketByteCnt field, which in new versions of PT contains the number of packet bytes written since the last sync (PSB) packet. Zeroing it out before enabling PT forces a sync packet to be written. This means that, with the existing code, a sync packet (PSB and PSBEND, 18 bytes in total) will be generated every time a PT event is scheduled in.

To avoid these unnecessary syncs and save a WRMSR in the fast path, this patch changes the default behavior to not clear PacketByteCnt field, so that the sync packets will be generated with the period specified as "psb_period" attribute config field. This has little impact on the trace data as the other packets that are normally sent within PSB+ (between PSB and PSBEND) have their own generation scenarios which do not depend on the sync packets.

One exception where we do need to force PSB like this when tracing starts, so that the decoder has a clear sync point in the trace. For this purpose we aready have hw::itrace_started flag, which we are currently using to output PERF_RECORD_ITRACE_START. This patch moves setting itrace_started from perf core to the pmu::start, where it should still be 0 on the very first run.

Quoted 5 lines

perf/x86/intel/pt: fixup with no force psb

Argh, it is not my day today.

Regards,
--
Alex

#15 lore raw

[tip:perf/core] perf/x86/intel/pt: Do not force sync packets on every schedule-in

In reply to Alexander Shishkin
perf/x86/intel/pt: Do not force sync packets on every schedule-in
Commit-ID:  9a6694cfa2390181dec936a17c0d9d21ef7b08d9
Gitweb:     http://git.kernel.org/tip/9a6694cfa2390181dec936a17c0d9d21ef7b08d9
Author:     Alexander Shishkin <[email protected]>
AuthorDate: Thu, 30 Jul 2015 16:48:24 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 4 Aug 2015 10:16:55 +0200

perf/x86/intel/pt: Do not force sync packets on every schedule-in

Currently, the PT driver zeroes out the status register every time before starting the event. However, all the writable bits are already taken care of in pt_handle_status() function, except the new PacketByteCnt field, which in new versions of PT contains the number of packet bytes written since the last sync (PSB) packet. Zeroing it out before enabling PT forces a sync packet to be written. This means that, with the existing code, a sync packet (PSB and PSBEND, 18 bytes in total) will be generated every time a PT event is scheduled in.

To avoid these unnecessary syncs and save a WRMSR in the fast path, this patch changes the default behavior to not clear PacketByteCnt field, so that the sync packets will be generated with the period specified as "psb_period" attribute config field. This has little impact on the trace data as the other packets that are normally sent within PSB+ (between PSB and PSBEND) have their own generation scenarios which do not depend on the sync packets.

One exception where we do need to force PSB like this when tracing starts, so that the decoder has a clear sync point in the trace. For this purpose we aready have hw::itrace_started flag, which we are currently using to output PERF_RECORD_ITRACE_START. This patch moves setting itrace_started from perf core to the pmu::start, where it should still be 0 on the very first run.

---
 arch/x86/kernel/cpu/perf_event_intel_pt.c | 7 +++++--
 kernel/events/core.c                      | 2 --
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_pt.c +5 -2
index 183de71..cc58ef8 100644
@@ -191,6 +191,11 @@ static void pt_config(struct perf_event *event)
 {
 	u64 reg;
 
+	if (!event->hw.itrace_started) {
+		event->hw.itrace_started = 1;
+		wrmsrl(MSR_IA32_RTIT_STATUS, 0);
+	}
+
 	reg = RTIT_CTL_TOPA | RTIT_CTL_BRANCH_EN | RTIT_CTL_TRACEEN;
 
 	if (!event->attr.exclude_kernel)
@@ -910,7 +915,6 @@ void intel_pt_interrupt(void)
 
 		pt_config_buffer(buf->cur->table, buf->cur_idx,
 				 buf->output_off);
-		wrmsrl(MSR_IA32_RTIT_STATUS, 0);
 		pt_config(event);
 	}
 }
@@ -934,7 +938,6 @@ static void pt_event_start(struct perf_event *event, int mode)
 
 	pt_config_buffer(buf->cur->table, buf->cur_idx,
 			 buf->output_off);
-	wrmsrl(MSR_IA32_RTIT_STATUS, 0);
 	pt_config(event);
 }
 
kernel/events/core.c +0 -2
index a9796c8..bdea129 100644
@@ -6139,8 +6139,6 @@ static void perf_log_itrace_start(struct perf_event *event)
 	    event->hw.itrace_started)
 		return;
 
-	event->hw.itrace_started = 1;
-
 	rec.header.type	= PERF_RECORD_ITRACE_START;
 	rec.header.misc	= 0;
 	rec.header.size	= sizeof(rec);
author reviewer
#16 lore raw

PATCH v2 1/1 perf/x86/intel/pt: Do not force sync packets on every schedule-in

In reply to Peter Zijlstra

Currently, the PT driver zeroes out the status register every time before starting the event. However, all the writable bits are already taken care of in pt_handle_status() function, except the new PacketByteCnt field, which in new versions of PT contains the number of packet bytes written since the last sync (PSB) packet. Zeroing it out before enabling PT forces a sync packet to be written. This means that, with the existing code, a sync packet (PSB and PSBEND, 18 bytes in total) will be generated every time a PT event is scheduled in.

To avoid these unnecessary syncs and save a WRMSR in the fast path, this patch changes the default behavior to not clear PacketByteCnt field, so that the sync packets will be generated with the period specified as "psb_period" attribute config field. This has little impact on the trace data as the other packets that are normally sent within PSB+ (between PSB and PSBEND) have their own generation scenarios which do not depend on the sync packets.

One exception where we do need to force PSB like this when tracing starts, so that the decoder has a clear sync point in the trace. For this purpose we aready have hw::itrace_started flag, which we are currently using to output PERF_RECORD_ITRACE_START. This patch moves setting itrace_started from perf core to the pmu::start, where it should still be 0 on the very first run.

---
 arch/x86/kernel/cpu/perf_event_intel_pt.c | 7 +++++--
 kernel/events/core.c                      | 2 --
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_pt.c +5 -2
index 695fd6c562..e20cfacb5a 100644
@@ -257,6 +257,11 @@ static void pt_config(struct perf_event *event)
 {
 	u64 reg;
 
+	if (!event->hw.itrace_started) {
+		event->hw.itrace_started = 1;
+		wrmsrl(MSR_IA32_RTIT_STATUS, 0);
+	}
+
 	reg = RTIT_CTL_TOPA | RTIT_CTL_BRANCH_EN | RTIT_CTL_TRACEEN;
 
 	if (!event->attr.exclude_kernel)
@@ -976,7 +981,6 @@ void intel_pt_interrupt(void)
 
 		pt_config_buffer(buf->cur->table, buf->cur_idx,
 				 buf->output_off);
-		wrmsrl(MSR_IA32_RTIT_STATUS, 0);
 		pt_config(event);
 	}
 }
@@ -1000,7 +1004,6 @@ static void pt_event_start(struct perf_event *event, int mode)
 
 	pt_config_buffer(buf->cur->table, buf->cur_idx,
 			 buf->output_off);
-	wrmsrl(MSR_IA32_RTIT_STATUS, 0);
 	pt_config(event);
 }
 
kernel/events/core.c +0 -2
index d3dae3419b..15da13237a 100644
@@ -6040,8 +6040,6 @@ static void perf_log_itrace_start(struct perf_event *event)
 	    event->hw.itrace_started)
 		return;
 
-	event->hw.itrace_started = 1;
-
 	rec.header.type	= PERF_RECORD_ITRACE_START;
 	rec.header.misc	= 0;
 	rec.header.size	= sizeof(rec);
Signature · 1 line
2.4.6
author reviewer
#17 lore raw

PATCH 3/3 perf/x86/intel/bts: Set itrace_started in pmu::start to match the new logic

In reply to Alexander Shishkin

Since hw::itrace_started is now set in pmu::start to signal the beginning of the trace, do so also in the intel_bts driver.

---
 arch/x86/kernel/cpu/perf_event_intel_bts.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/perf_event_intel_bts.c +1 -0
index 54690e8857..d1c0f254af 100644
@@ -222,6 +222,7 @@ static void __bts_event_start(struct perf_event *event)
 	if (!buf || bts_buffer_is_full(buf, bts))
 		return;
 
+	event->hw.itrace_started = 1;
 	event->hw.state = 0;
 
 	if (!buf->snapshot)
Signature · 1 line
2.1.4
author reviewer
#18 lore raw

Re: [PATCH 3/3] perf/x86/intel/bts: Set itrace_started in pmu::start to match the new logic

Quoted 4 lines · Alexander Shishkin

Since hw::itrace_started is now set in pmu::start to signal the beginning of the trace, do so also in the intel_bts driver.

Peter, seems like you overlooked this one. Its counterpart to is in Linus' master already:

commit 9a6694cfa2390181dec936a17c0d9d21ef7b08d9

Author: Alexander Shishkin <[email protected]>
Date:   Thu Jul 30 16:48:24 2015 +0300

perf/x86/intel/pt: Do not force sync packets on every schedule-in

Cheers,
--
Alex

maintainer
#19 lore raw

Re: [PATCH 3/3] perf/x86/intel/bts: Set itrace_started in pmu::start to match the new logic

Quoted 7 lines · Alexander Shishkin
Quoted 4 lines · Alexander Shishkin

Since hw::itrace_started is now set in pmu::start to signal the beginning of the trace, do so also in the intel_bts driver.

Quoted 9 lines

Peter, seems like you overlooked this one. Its counterpart to is in Linus' master already:

commit 9a6694cfa2390181dec936a17c0d9d21ef7b08d9

Author: Alexander Shishkin <[email protected]>
Date:   Thu Jul 30 16:48:24 2015 +0300

perf/x86/intel/pt: Do not force sync packets on every schedule-in

Urgh, sorry about that.

#20 lore raw

[tip:perf/core] perf/x86/intel/bts: Set event-> hw.itrace_started in pmu::start to match the new logic

In reply to Alexander Shishkin
perf/x86/intel/bts: Set event->hw.itrace_started in pmu::start to match the new logic
Commit-ID:  d249872939bfa86c9cce44a56a8cbdbc7086519b
Gitweb:     http://git.kernel.org/tip/d249872939bfa86c9cce44a56a8cbdbc7086519b
Author:     Alexander Shishkin <[email protected]>
AuthorDate: Fri, 17 Jul 2015 16:34:10 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 11 Sep 2015 10:06:03 +0200

perf/x86/intel/bts: Set event->hw.itrace_started in pmu::start to match the new logic

Since event->hw.itrace_started is now set in pmu::start() to signal the beginning of the trace, do so also in the intel_bts driver.

---
 arch/x86/kernel/cpu/perf_event_intel_bts.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/perf_event_intel_bts.c +1 -0
index 54690e8..d1c0f25 100644
@@ -222,6 +222,7 @@ static void __bts_event_start(struct perf_event *event)
 	if (!buf || bts_buffer_is_full(buf, bts))
 		return;
 
+	event->hw.itrace_started = 1;
 	event->hw.state = 0;
 
 	if (!buf->snapshot)