perf: Add infrastructure and support for Intel PT
Subsystems (2)
Alexander Shishkin
reviewerv8 cover perf: Add infrastructure and support for Intel PT
Hi Peter and all,
Since there weren't too many review comments on the two previous versions, here's another one with a couple more fixes.
[full description below the changelog]
Changes since v7:
* fixed a bug in aux allocation logic that caused more than requested pages to be allocated,
* fixed pt_event_add() for snapshot mode, which would return an error in case of successful completion.
Changes since v6:
* replaced kref with atomic in aux refcounting this time for real,
* added a small comment on capabilities/"caps" attribute group in PT pmu.
Changes since v5:
* dropped "bypass mask", bits that a privileged user could set in RTIT_CTL,
* merged configuring and enabling RTIT_CTL writes,
* fixed kernel-doc comments
* if LBR/BTS events are present in the system, PT event creation is disallowed and vice versa,
* fixed a bug with high order allocation under pressure,
* fixed undoing vm_locked accounting in case of AUX allocation failure,
* left out kernel counters for a separate series to keep this one concise.
This patchset adds support for Intel Processor Trace (PT) extension [1] of Intel Architecture that allows the capture of information about software execution flow, to the perf kernel infrastructure.
The single most notable thing is that while PT outputs trace data in a compressed binary format, it will still generate hundreds of megabytes of trace data per second per core. Decoding this binary stream takes 2-3 orders of magnitude the cpu time that it takes to generate it. These considerations make it impossible to carry out decoding in kernel space. Therefore, the trace data is exported to userspace as a zero-copy mapping that userspace can collect and store for later decoding. To address this, this patchset extends perf ring buffer with an "AUX space", which is allocated for hardware blocks such as PT to export their trace data with minimal overhead. This space can be configured via buffer's user page and mmapped from the same file descriptor with a given offset. Data can then be collected from it by reading the aux_head (write) pointer from the user page and updating aux_tail (read) pointer similarly to data_{head,tail} of the traditional perf buffer. There is an api between perf core and pmu drivers that wish to make use of this AUX space to export their data.
For tracing blocks that don't support hardware scatter-gather tables, we provide high-order physically contiguous allocations to minimize the overhead needed for software double buffering and PMI pressure.
This way we get a normal perf data stream that provides sideband information that is required to decode the trace data, such as MMAPs, COMMs etc, plus the actual trace in its own logical space.
If the trace buffer is mapped writable, the driver will stop tracing when it fills up (aux_head approaches aux_tail), till data is read, aux_tail pointer is moved forward and an ioctl() is issued to re-enable tracing. If the trace buffer is mapped read only, the tracing will continue, overwriting older data, so that the buffer always contains the most recent data. Tracing can be stopped with an ioctl() and restarted once the data is collected.
This patchset consists of necessary changes to the perf kernel infrastructure, and PT and BTS pmu drivers. The tooling support is not included in this series, however, it can be found in my github tree [2].
[1] chapter 36 of
http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf
[2] http://github.com/virtuoso/linux-perf/tree/intel_pt
Alexander Shishkin (13):
perf: Add data_{offset,size} to user_page
perf: Support high-order allocations for AUX space
perf: Add a capability for AUX_NO_SG pmus to do software double
buffering
perf: Add a pmu capability for "exclusive" events
perf: Add AUX record
perf: Add api for pmus to write to AUX area
perf: Support overwrite mode for AUX area
perf: Add wakeup watermark control to AUX area
x86: Add Intel Processor Trace (INTEL_PT) cpu feature detection
x86: perf: Intel PT and LBR/BTS are mutually exclusive
x86: perf: intel_pt: Intel PT PMU driver
x86: perf: intel_bts: Add BTS PMU driver
perf: add ITRACE_START record to indicate that tracing has started
Peter Zijlstra (1):
perf: Add AUX area to ring buffer for raw data streams
arch/x86/include/asm/cpufeature.h | 1 + arch/x86/include/uapi/asm/msr-index.h | 18 + arch/x86/kernel/cpu/Makefile | 1 + arch/x86/kernel/cpu/intel_pt.h | 131 ++++ arch/x86/kernel/cpu/perf_event.c | 43 ++ arch/x86/kernel/cpu/perf_event.h | 49 ++ arch/x86/kernel/cpu/perf_event_intel.c | 40 +- arch/x86/kernel/cpu/perf_event_intel_bts.c | 525 ++++++++++++++ arch/x86/kernel/cpu/perf_event_intel_ds.c | 3 +- arch/x86/kernel/cpu/perf_event_intel_pt.c | 1082 ++++++++++++++++++++++++++++ arch/x86/kernel/cpu/scattered.c | 1 + include/linux/perf_event.h | 47 +- include/uapi/linux/perf_event.h | 59 +- kernel/events/core.c | 268 ++++++- kernel/events/internal.h | 33 + kernel/events/ring_buffer.c | 327 ++++++++- 16 files changed, 2577 insertions(+), 51 deletions(-) create mode 100644 arch/x86/kernel/cpu/intel_pt.h create mode 100644 arch/x86/kernel/cpu/perf_event_intel_bts.c create mode 100644 arch/x86/kernel/cpu/perf_event_intel_pt.c
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 1/14 perf: Add data_{offset,size} to user_page
Currently, the actual perf ring buffer is one page into the mmap area, following the user page and the userspace follows this convention. This patch adds data_{offset,size} fields to user_page that can be used by userspace instead for locating perf data in the mmap area. This is also helpful when mapping existing or shared buffers if their size is not known in advance.
Right now, it is made to follow the existing convention that
data_offset == PAGE_SIZE and data_offset + data_size == mmap_size.
--- include/uapi/linux/perf_event.h | 5 +++++ kernel/events/core.c | 2 ++ 2 files changed, 7 insertions(+)
include/uapi/linux/perf_event.h +5 -0
@@ -489,9 +489,14 @@ struct perf_event_mmap_page {* In this case the kernel will not over-write unread data. * * See perf_output_put_handle() for the data ordering. +* +* data_{offset,size} indicate the location and size of the perf record +* buffer within the mmapped area. */ __u64 data_head;/* head in the data section */ __u64 data_tail;/* user-space written tail */ +__u64 data_offset;/* where the buffer starts */ +__u64 data_size;/* data buffer size */ };#define PERF_RECORD_MISC_CPUMODE_MASK (7 << 0)
kernel/events/core.c +2 -0
@@ -3942,6 +3942,8 @@ static void perf_event_init_userpage(struct perf_event *event)/* Allow new userspace to detect that bit 0 is deprecated */ userpg->cap_bit0_is_deprecated =1 ; userpg->size = offsetof(struct perf_event_mmap_page , __reserved); + userpg->data_offset =PAGE_SIZE ; + userpg->data_size =perf_data_size (rb); unlock:rcu_read_unlock ();
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 2/14 perf: Add AUX area to ring buffer for raw data streams
In reply to Alexander ShishkinFrom: Peter Zijlstra <[email protected]>
This patch introduces "AUX space" in the perf mmap buffer, intended for exporting high bandwidth data streams to userspace, such as instruction flow traces.
AUX space is a ring buffer, defined by aux_{offset,size} fields in the user_page structure, and read/write pointers aux_{head,tail}, which abide by the same rules as data_* counterparts of the main perf buffer.
In order to allocate/mmap AUX, userspace needs to set up aux_offset to such an offset that will be greater than data_offset+data_size and aux_size to be the desired buffer size. Both need to be page aligned. Then, same aux_offset and aux_size should be passed to mmap() call and if everything adds up, you should have an AUX buffer as a result.
Pages that are mapped into this buffer also come out of user's mlock rlimit plus perf_event_mlock_kb allowance.
--- include/linux/perf_event.h | 17 +++++ include/uapi/linux/perf_event.h | 16 +++++ kernel/events/core.c | 140 +++++++++++++++++++++++++++++++++------- kernel/events/internal.h | 23 +++++++ kernel/events/ring_buffer.c | 97 ++++++++++++++++++++++++++-- 5 files changed, 264 insertions(+), 29 deletions(-)
include/linux/perf_event.h +17 -0
@@ -263,6 +263,18 @@ struct pmu {* flush branch stack on context-switches (needed in cpu-wide mode) */ void (*flush_branch_stack) (void ); + +/* +* Set up pmu-private data structures for an AUX area +*/ +void *(*setup_aux) (int cpu,void **pages, +int nr_pages,bool overwrite); +/* optional */ + +/* +* Free pmu-private AUX data structures +*/ +void (*free_aux) (void *aux);/* optional */ };/**
@@ -782,6 +794,11 @@ static inline bool has_branch_stack(struct perf_event *event)return event->attr.sample_type &PERF_SAMPLE_BRANCH_STACK ; } +static inline bool has_aux (struct perf_event *event) +{ +return event->pmu->setup_aux; +} +extern int perf_output_begin (struct perf_output_handle *handle,struct perf_event *event,unsigned int size);extern void perf_output_end (struct perf_output_handle *handle);
include/uapi/linux/perf_event.h +16 -0
@@ -497,6 +497,22 @@ struct perf_event_mmap_page {__u64 data_tail;/* user-space written tail */ __u64 data_offset;/* where the buffer starts */ __u64 data_size;/* data buffer size */ + +/* +* AUX area is defined by aux_{offset,size} fields that should be set +* by the userspace, so that +* +* aux_offset >= data_offset + data_size +* +* prior to mmap()ing it. Size of the mmap()ed area should be aux_size. +* +* Ring buffer pointers aux_{head,tail} have the same semantics as +* data_{head,tail} and same ordering rules apply. +*/ +__u64 aux_head; +__u64 aux_tail; +__u64 aux_offset; +__u64 aux_size; };#define PERF_RECORD_MISC_CPUMODE_MASK (7 << 0)
kernel/events/core.c +115 -25
214 lines not on this page · raw message
kernel/events/internal.h +23 -0
@@ -35,6 +35,16 @@ struct ring_buffer {unsigned long mmap_locked;struct user_struct *mmap_user; +/* AUX area */ +unsigned long aux_pgoff; +int aux_nr_pages; +atomic_t aux_mmap_count; +unsigned long aux_mmap_locked; +void (*free_aux)(void *); +atomic_t aux_refcount; +void **aux_pages; +void *aux_priv; +struct perf_event_mmap_page *user_page;void *data_pages[0 ]; };
@@ -43,6 +53,14 @@ extern void rb_free(struct ring_buffer *rb);extern struct ring_buffer *rb_alloc (int nr_pages,long watermark,int cpu,int flags);extern void perf_event_wakeup (struct perf_event *event); +extern int rb_alloc_aux (struct ring_buffer *rb,struct perf_event *event, +pgoff_t pgoff,int nr_pages,int flags); +extern void rb_free_aux (struct ring_buffer *rb); + +static inline bool rb_has_aux (struct ring_buffer *rb) +{ +return !!rb->aux_nr_pages; +} extern void perf_event_header__init_id(struct perf_event_header *header,
@@ -81,6 +99,11 @@ static inline unsigned long perf_data_size(struct ring_buffer *rb)return rb->nr_pages << (PAGE_SHIFT +page_order (rb)); } +static inline unsigned long perf_aux_size (struct ring_buffer *rb) +{ +return rb->aux_nr_pages <<PAGE_SHIFT ; +} +#define DEFINE_OUTPUT_COPY (func_name, memcpy_func) \ static inline unsigned long \ func_name(struct perf_output_handle *handle, \
kernel/events/ring_buffer.c +93 -4
118 lines not on this page · raw message
Signature · 1 line
2.1.1
RE: [PATCH v8 02/14] perf: Add AUX area to ring buffer for raw data streams
Quoted 21 lines
-----Original Message-----
From: Alexander Shishkin [mailto:[email protected]]
Sent: Friday, November 14, 2014 2:44 PM
To: Peter Zijlstra
Cc: Ingo Molnar; [email protected]; Robert Richter; Frederic Weisbecker; Mike Galbraith; Paul Mackerras; Stephane Eranian; Andi Kleen; Liang, Kan; Hunter, Adrian; Metzger, Markus T; [email protected]; [email protected]; Peter Zijlstra; Alexander Shishkin
Subject: [PATCH v8 02/14] perf: Add AUX area to ring buffer for raw data streamsFrom: Peter Zijlstra <[email protected]>
This patch introduces "AUX space" in the perf mmap buffer, intended for exporting high bandwidth data streams to userspace, such as instruction flow traces.
AUX space is a ring buffer, defined by aux_{offset,size} fields in the
user_pagestructure, and read/write pointers aux_{head,tail}, which abide by the same rules as data_* counterparts of the main perf buffer.
Quoted 5 lines
In order to allocate/mmap AUX, userspace needs to set up
aux_offsetto such an offset that will be greater than data_offset+data_size andaux_sizeto be the desired buffer size. Both need to be page aligned. Then, sameaux_offsetandaux_sizeshould be passed tommap()call and if everything adds up, you should have an AUX buffer as a result.
Why would userspace need to fill in aux_offset and aux_size? Can't the kernel do this when userspace mmaps the aux buffer like it does for the respective data_* fields?
How would userspace fill in those fields if it mapped the data buffer read-only?
Regards,
Markus.
Intel GmbH
Dornacher Strasse 1
85622 Feldkirchen/Muenchen, Deutschland
Sitz der Gesellschaft: Feldkirchen bei Muenchen
Geschaeftsfuehrer: Christian Lamprechter, Hannes Schwaderer, Douglas Lusk
Registergericht: Muenchen HRB 47456
Ust.-IdNr./VAT Registration No.: DE129385895
Citibank Frankfurt a.M. (BLZ 502 109 00) 600119052
Peter Zijlstra
maintainerRe: [PATCH v8 02/14] perf: Add AUX area to ring buffer for raw data streams
Quoted 16 lines · Markus T
Quoted 10 lines
From: Peter Zijlstra <[email protected]>
This patch introduces "AUX space" in the perf mmap buffer, intended for exporting high bandwidth data streams to userspace, such as instruction flow traces.
AUX space is a ring buffer, defined by aux_{offset,size} fields in the
user_pagestructure, and read/write pointers aux_{head,tail}, which abide by the same rules as data_* counterparts of the main perf buffer.Quoted 5 lines
In order to allocate/mmap AUX, userspace needs to set up
aux_offsetto such an offset that will be greater than data_offset+data_size andaux_sizeto be the desired buffer size. Both need to be page aligned. Then, sameaux_offsetandaux_sizeshould be passed tommap()call and if everything adds up, you should have an AUX buffer as a result.
Quoted 3 lines
Why would userspace need to fill in
aux_offsetandaux_size? Can't the kernel do this when userspace mmaps the aux buffer like it does for the respective data_* fields?
I suppose we could; I'm trying to remember why I did it like this, I'm failing to remember much past yesterday atm :/
Quoted 1 line
How would userspace fill in those fields if it mapped the data buffer read-only?
Good point, its not used much atm. But yes. That also reminds me; we should probably finish this:
Alexander Shishkin
reviewerRe: [PATCH v8 02/14] perf: Add AUX area to ring buffer for raw data streams
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Markus T
Quoted 15 lines
From: Peter Zijlstra <[email protected]>
This patch introduces "AUX space" in the perf mmap buffer, intended for exporting high bandwidth data streams to userspace, such as instruction flow traces.
AUX space is a ring buffer, defined by aux_{offset,size} fields in the
user_pagestructure, and read/write pointers aux_{head,tail}, which abide by the same rules as data_* counterparts of the main perf buffer.In order to allocate/mmap AUX, userspace needs to set up
aux_offsetto such an offset that will be greater than data_offset+data_size andaux_sizeto be the desired buffer size. Both need to be page aligned. Then, sameaux_offsetandaux_sizeshould be passed tommap()call and if everything adds up, you should have an AUX buffer as a result.
Quoted 6 lines
Quoted 3 lines
Why would userspace need to fill in
aux_offsetandaux_size? Can't the kernel do this when userspace mmaps the aux buffer like it does for the respective data_* fields?I suppose we could; I'm trying to remember why I did it like this, I'm failing to remember much past yesterday atm :/
Well, right now we can only map things in one order: user page + perf buffer, then aux buffer. Ideally, we can reduce it to mapping the user page first (always R/W), setting up the desired offsets for data and aux buffers and then mapping those in whatever order. And we can also have holes between them if we want (not sure if that's useful, though).
If we want to stick to the current scheme of things where user page is mapped together with data, we should set up aux_{offset,size} in kernel's perf_mmap() path, similarly to data_{offset,size}.
There is another problem with it, though: it makes it impossible to run the aux buffer in "full trace mode" (where we move the tail as we read the data out), while running the data buffer in overwrite mode, because the latter requires the user_page to be mapped RO => no way to update aux_tail for the user.
Quoted 6 lines
Quoted 1 line
How would userspace fill in those fields if it mapped the data buffer read-only?
Good point, its not used much atm. But yes. That also reminds me; we should probably finish this:
Indeed, it's been on my todo list for some time.
Regards,
--
Alex
Peter Zijlstra
maintainerRe: [PATCH v8 02/14] perf: Add AUX area to ring buffer for raw data streams
Quoted 8 lines · Alexander Shishkin
Quoted 2 lines
I suppose we could; I'm trying to remember why I did it like this, I'm failing to remember much past yesterday atm :/
Well, right now we can only map things in one order: user page + perf buffer, then aux buffer. Ideally, we can reduce it to mapping the user page first (always R/W), setting up the desired offsets for data and aux buffers and then mapping those in whatever order. And we can also have holes between them if we want (not sure if that's useful, though).
Indeed, over the weekend I came up a similar argument. Suppose we want to extend the thing with yet another area, at that point we simply do not know which segment is meant to be mapp()ed.
Breaking up the control page from the data buffer is indeed a sane proposal.
Quoted 9 lines
If we want to stick to the current scheme of things where user page is mapped together with data, we should set up aux_{offset,size} in kernel's
perf_mmap()path, similarly to data_{offset,size}.There is another problem with it, though: it makes it impossible to run the aux buffer in "full trace mode" (where we move the tail as we read the data out), while running the data buffer in overwrite mode, because the latter requires the
user_pageto be mapped RO => no way to updateaux_tailfor the user.
ISTR aux bits relying on their own mmap(PROT_WRITE) bit, but yes-ish.
Alexander Shishkin
reviewerRe: [PATCH v8 02/14] perf: Add AUX area to ring buffer for raw data streams
Quoted 21 lines · Peter Zijlstra
Quoted 8 lines · Alexander Shishkin
Quoted 2 lines
I suppose we could; I'm trying to remember why I did it like this, I'm failing to remember much past yesterday atm :/
Well, right now we can only map things in one order: user page + perf buffer, then aux buffer. Ideally, we can reduce it to mapping the user page first (always R/W), setting up the desired offsets for data and aux buffers and then mapping those in whatever order. And we can also have holes between them if we want (not sure if that's useful, though).
Indeed, over the weekend I came up a similar argument. Suppose we want to extend the thing with yet another area, at that point we simply do not know which segment is meant to be mapp()ed.
Breaking up the control page from the data buffer is indeed a sane proposal.
Quoted 4 lines
If we want to stick to the current scheme of things where user page is mapped together with data, we should set up aux_{offset,size} in kernel's
perf_mmap()path, similarly to data_{offset,size}.
Quoted 7 lines
Quoted 5 lines
There is another problem with it, though: it makes it impossible to run the aux buffer in "full trace mode" (where we move the tail as we read the data out), while running the data buffer in overwrite mode, because the latter requires the
user_pageto be mapped RO => no way to updateaux_tailfor the user.ISTR aux bits relying on their own
mmap(PROT_WRITE)bit, but yes-ish.
It is. Now if data buffer is made to behave the same way, this problem goes away. Let me see if I can come up with a patch for this, bearing in mind backwards compatibility with older perfs etc.
Regards,
--
Alex
Re: [PATCH v8 02/14] perf: Add AUX area to ring buffer for raw data streams
In reply to Alexander ShishkinAlexander Shishkin [[email protected]] wrote:
| From: Peter Zijlstra <[email protected]> | | This patch introduces "AUX space" in the perf mmap buffer, intended for | exporting high bandwidth data streams to userspace, such as instruction | flow traces. | | AUX space is a ring buffer, defined by aux_{offset,size} fields in the | user_page structure, and read/write pointers aux_{head,tail}, which abide | by the same rules as data_* counterparts of the main perf buffer.
The "format" of this raw stream of data can differ across processors (and architecutres) correct ? i.e the perf tool processing of this aux data will also differ across processors ?
Power8 processors support what we call 24x7 counters that collect info on a large number of events. The current 24x7 support in (arch/powerpc/perf/hv-24x7.c) currently uses reading one counter at a time using the pmu->read() interface.
We are looking for ways to export much larger number of counters through one pmu->read() or another call. I am trying to see if the hv-24x7 pmu could use this aux interface and then have/implement a helper in perf tool to extract the counter values.
Sukadev
Re: [PATCH v8 02/14] perf: Add AUX area to ring buffer for raw data streams
Quoted 11 lines · Sukadev Bhattiprolu
Alexander Shishkin [[email protected]] wrote:
| From: Peter Zijlstra <[email protected]> | | This patch introduces "AUX space" in the perf mmap buffer, intended for | exporting high bandwidth data streams to userspace, such as instruction | flow traces. | | AUX space is a ring buffer, defined by aux_{offset,size} fields in the | user_page structure, and read/write pointers aux_{head,tail}, which abide | by the same rules as data_* counterparts of the main perf buffer.
Quoted 3 lines
The "format" of this raw stream of data can differ across processors (and architecutres) correct ? i.e the perf tool processing of this aux data will also differ across processors ?
Yes.
The main caveat is that you need special handling code in perf.
Quoted 10 lines
Power8 processors support what we call 24x7 counters that collect info on a large number of events. The current 24x7 support in (
arch/powerpc/perf/hv-24x7.c) currently uses reading one counter at a time using thepmu->read()interface.We are looking for ways to export much larger number of counters through one
pmu->read()or another call. I am trying to see if the hv-24x7 pmu could use this aux interface and then have/implement a helper in perf tool to extract the counter values.
It's probably only worth it if your amount of data is extremely high and has to be to compressed (like in PT -- no choice, cannot do it otherwise)
If your data amounts are more reasonable it will be a lot simpler to stay with the current perf interfaces, even though they have a bit higher overhead.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only
Alexander Shishkin
reviewerPATCH v8 3/14 perf: Support high-order allocations for AUX space
In reply to Alexander ShishkinSome pmus (such as BTS or Intel PT without multiple-entry ToPA capability) don't support scatter-gather and will prefer larger contiguous areas for their output regions.
This patch adds a new pmu capability to request higher order allocations.
--- include/linux/perf_event.h | 1 + kernel/events/ring_buffer.c | 56 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 6 deletions(-)
include/linux/perf_event.h +1 -0
@@ -171,6 +171,7 @@ struct perf_event;* pmu::capabilities flags */ #define PERF_PMU_CAP_NO_INTERRUPT 0x01 +#define PERF_PMU_CAP_AUX_NO_SG 0x02/** * struct pmu - generic performance monitoring unit
kernel/events/ring_buffer.c +50 -6
87 lines not on this page · raw message
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 4/14 perf: Add a capability for AUX_NO_SG pmus to do software double buffering
In reply to Alexander ShishkinFor pmus that don't support scatter-gather for AUX data in hardware, it might still make sense to implement software double buffering to avoid losing data while the user is reading data out. For this purpose, add a pmu capability that guarantees multiple high-order chunks for AUX buffer, so that the pmu driver can do switchover tricks.
To make use of this feature, add PERF_PMU_CAP_AUX_SW_DOUBLEBUF to your pmu's capability mask. This will make the ring buffer AUX allocation code ensure that the biggest high order allocation for the aux buffer pages is no bigger than half of the total requested buffer size, thus making sure that the buffer has at least two high order allocations.
--- include/linux/perf_event.h | 1 + kernel/events/ring_buffer.c | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/perf_event.h +1 -0
@@ -172,6 +172,7 @@ struct perf_event;*/ #define PERF_PMU_CAP_NO_INTERRUPT 0x01#define PERF_PMU_CAP_AUX_NO_SG 0x02 +#define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x04/** * struct pmu - generic performance monitoring unit
kernel/events/ring_buffer.c +14 -1
@@ -286,13 +286,26 @@ int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,if (!has_aux (event))return -ENOTSUPP ; -if (event->pmu->capabilities &PERF_PMU_CAP_AUX_NO_SG ) +if (event->pmu->capabilities &PERF_PMU_CAP_AUX_NO_SG ) {/* * We need to start with the max_order that fits in nr_pages, * not the other way around, hence ilog2() and not get_order. */ max_order =ilog2 (nr_pages); +/* +* PMU requests more than one contiguous chunks of memory +* for SW double buffering +*/ +if ((event->pmu->capabilities &PERF_PMU_CAP_AUX_SW_DOUBLEBUF ) && + !overwrite) { +if (!max_order) +return -EINVAL ; + + max_order--; + } + } + rb->aux_pages =kzalloc_node (nr_pages *sizeof (void *),GFP_KERNEL , node);if (!rb->aux_pages)return -ENOMEM ;
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 5/14 perf: Add a pmu capability for "exclusive" events
In reply to Alexander ShishkinUsually, pmus that do, for example, instruction tracing, would only ever be able to have one event per task per cpu (or per perf_context). For such pmus it makes sense to disallow creating conflicting events early on, so as to provide consistent behavior for the user.
This patch adds a pmu capability that indicates such constraint on event creation.
--- include/linux/perf_event.h | 1 + kernel/events/core.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)
include/linux/perf_event.h +1 -0
@@ -173,6 +173,7 @@ struct perf_event;#define PERF_PMU_CAP_NO_INTERRUPT 0x01#define PERF_PMU_CAP_AUX_NO_SG 0x02#define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x04 +#define PERF_PMU_CAP_EXCLUSIVE 0x08/** * struct pmu - generic performance monitoring unit
kernel/events/core.c +45 -0
69 lines not on this page · raw message
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 6/14 perf: Add AUX record
In reply to Alexander ShishkinWhen there's new data in the AUX space, output a record indicating its offset and size and a set of flags, such as PERF_AUX_FLAG_TRUNCATED, to mean the described data was truncated to fit in the ring buffer.
--- include/uapi/linux/perf_event.h | 19 +++++++++++++++++++ kernel/events/core.c | 34 ++++++++++++++++++++++++++++++++++ kernel/events/internal.h | 3 +++ 3 files changed, 56 insertions(+)
include/uapi/linux/perf_event.h +19 -0
@@ -733,6 +733,20 @@ enum perf_event_type {*/ PERF_RECORD_MMAP2 =10 , +/* +* Records that new data landed in the AUX buffer part. +* +* struct { +* struct perf_event_header header; +* +* u64 aux_offset; +* u64 aux_size; +* u64 flags; +* struct sample_id sample_id; +* }; +*/ +PERF_RECORD_AUX =11 , + PERF_RECORD_MAX, /* non-ABI */ };
@@ -750,6 +764,11 @@ enum perf_callchain_context { PERF_CONTEXT_MAX = (__u64)-4095, }; +/** +* PERF_RECORD_AUX::flags bits +*/ +#define PERF_AUX_FLAG_TRUNCATED 0x01/* record was truncated to fit */ +#define PERF_FLAG_FD_NO_GROUP (1UL << 0)#define PERF_FLAG_FD_OUTPUT (1UL << 1)#define PERF_FLAG_PID_CGROUP (1UL << 2)/* pid=cgroup id, per-cpu mode only */
kernel/events/core.c +34 -0
@@ -5606,6 +5606,40 @@ void perf_event_mmap(struct vm_area_struct *vma)perf_event_mmap_event (&mmap_event); } +void perf_event_aux_event (struct perf_event *event,unsigned long head, +unsigned long size,u64 flags) +{ +struct perf_output_handle handle; +struct perf_sample_data sample; +struct perf_aux_event { +struct perf_event_header header; +u64 offset; +u64 size; +u64 flags; + } rec = { + .header = { + .type =PERF_RECORD_AUX , + .misc =0 , + .size =sizeof (rec), + }, + .offset = head, + .size = size, + .flags = flags, + }; +int ret; + +perf_event_header__init_id (&rec.header, &sample, event); + ret =perf_output_begin (&handle, event, rec.header.size); + +if (ret) +return ; + +perf_output_put (&handle, rec); +perf_event__output_id_sample (event, &handle, &sample); + +perf_output_end (&handle); +} +/* * IRQ throttle logging */
kernel/events/internal.h +3 -0
@@ -62,6 +62,9 @@ static inline bool rb_has_aux(struct ring_buffer *rb)return !!rb->aux_nr_pages; } +void perf_event_aux_event (struct perf_event *event,unsigned long head, +unsigned long size,u64 flags); + extern void perf_event_header__init_id(struct perf_event_header *header, struct perf_sample_data *data,
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 7/14 perf: Add api for pmus to write to AUX area
In reply to Alexander ShishkinFor pmus that wish to write data to ring buffer's AUX area, provide perf_aux_output_{begin,end}() calls to initiate/commit data writes, similarly to perf_output_{begin,end}. These also use the same output handle structure. Also, similarly to software counterparts, these will direct inherited events' output to parents' ring buffers.
After the perf_aux_output_begin() returns successfully, handle->size is set to the maximum amount of data that can be written wrt aux_tail pointer, so that no data that the user hasn't seen will be overwritten, therefore this should always be called before hardware writing is enabled. On success, this will return the pointer to pmu driver's private structure allocated for this aux area by pmu::setup_aux. Same pointer can also be retrieved using perf_get_aux() while hardware writing is enabled.
PMU driver should pass the actual amount of data written as a parameter to perf_aux_output_end(). All hardware writes should be completed and visible before this one is called.
Additionally, perf_aux_output_skip() will adjust output handle and aux_head in case some part of the buffer has to be skipped over to maintain hardware's alignment constraints.
Nested writers are forbidden and guards are in place to catch such attempts.
--- include/linux/perf_event.h | 23 +++++++- kernel/events/core.c | 5 +- kernel/events/internal.h | 4 ++ kernel/events/ring_buffer.c | 139 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 4 deletions(-)
include/linux/perf_event.h +22 -1
@@ -555,12 +555,22 @@ struct perf_output_handle {struct ring_buffer *rb;unsigned long wakeup;unsigned long size; -void *addr; +union { +void *addr; +unsigned long head; + };int page; };#ifdef CONFIG_PERF_EVENTS +extern void *perf_aux_output_begin (struct perf_output_handle *handle, +struct perf_event *event); +extern void perf_aux_output_end (struct perf_output_handle *handle, +unsigned long size,bool truncated); +extern int perf_aux_output_skip (struct perf_output_handle *handle, +unsigned long size); +extern void *perf_get_aux (struct perf_output_handle *handle);extern int perf_pmu_register (struct pmu *pmu,const char *name,int type);extern void perf_pmu_unregister (struct pmu *pmu);
@@ -817,6 +827,17 @@ extern void perf_event_disable(struct perf_event *event);extern int __perf_event_disable (void *info);extern void perf_event_task_tick (void );#else /* !CONFIG_PERF_EVENTS: */ +static inline void * +perf_aux_output_begin (struct perf_output_handle *handle, +struct perf_event *event) {return NULL ; } +static inline void +perf_aux_output_end (struct perf_output_handle *handle,unsigned long size, +bool truncated) { } +static inline int +perf_aux_output_skip (struct perf_output_handle *handle, +unsigned long size) {return -EINVAL ; } +static inline void * +perf_get_aux (struct perf_output_handle *handle) {return NULL ; }static inline void perf_event_task_sched_in (struct task_struct *prev,struct task_struct *task) { }
kernel/events/core.c +2 -3
@@ -3293,7 +3293,6 @@ static void free_event_rcu(struct rcu_head *head)kfree (event); } -static void ring_buffer_put (struct ring_buffer *rb);static void ring_buffer_attach (struct perf_event *event,struct ring_buffer *rb);
@@ -4110,7 +4109,7 @@ static void rb_free_rcu(struct rcu_head *rcu_head)rb_free (rb); } -static struct ring_buffer *ring_buffer_get (struct perf_event *event) +struct ring_buffer *ring_buffer_get (struct perf_event *event) {struct ring_buffer *rb;
@@ -4125,7 +4124,7 @@ static struct ring_buffer *ring_buffer_get(struct perf_event *event)return rb; } -static void ring_buffer_put (struct ring_buffer *rb) +void ring_buffer_put (struct ring_buffer *rb) {if (!atomic_dec_and_test (&rb->refcount))return ;
kernel/events/internal.h +4 -0
@@ -36,6 +36,8 @@ struct ring_buffer {struct user_struct *mmap_user;/* AUX area */ +local_t aux_head; +local_t aux_nest;unsigned long aux_pgoff;int aux_nr_pages;atomic_t aux_mmap_count;
@@ -56,6 +58,8 @@ extern void perf_event_wakeup(struct perf_event *event);extern int rb_alloc_aux (struct ring_buffer *rb,struct perf_event *event,pgoff_t pgoff,int nr_pages,int flags);extern void rb_free_aux (struct ring_buffer *rb); +extern struct ring_buffer *ring_buffer_get (struct perf_event *event); +extern void ring_buffer_put (struct ring_buffer *rb);static inline bool rb_has_aux (struct ring_buffer *rb) {
kernel/events/ring_buffer.c +139 -0
145 lines not on this page · raw message
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 8/14 perf: Support overwrite mode for AUX area
In reply to Alexander ShishkinThis adds support for overwrite mode in the AUX area, which means "keep collecting data till you're stopped", turning AUX area into a circular buffer, where new data overwrites old data. It does not depend on data buffer's overwrite mode, so that it doesn't lose sideband data that is instrumental for processing AUX data.
Overwrite mode is enabled at mapping AUX area read only. Even though aux_tail in the buffer's user page might be user writable, it will be ignored in this mode.
A PERF_RECORD_AUX with PERF_AUX_FLAG_OVERWRITE set is written to the perf data stream every time an event writes new data to the AUX area. The pmu driver might not be able to infer the exact beginning of the new data in each snapshot, some drivers will only provide the tail, which is aux_offset + aux_size in the AUX record. Consumer has to be able to tell the new data from the old one, for example, by means of time stamps if such are provided in the trace.
Consumer is also responsible for disabling any events that might write to the AUX area (thus potentially racing with the consumer) before collecting the data.
--- include/uapi/linux/perf_event.h | 1 + kernel/events/internal.h | 1 + kernel/events/ring_buffer.c | 48 ++++++++++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 15 deletions(-)
include/uapi/linux/perf_event.h +1 -0
@@ -768,6 +768,7 @@ enum perf_callchain_context {* PERF_RECORD_AUX::flags bits */ #define PERF_AUX_FLAG_TRUNCATED 0x01/* record was truncated to fit */ +#define PERF_AUX_FLAG_OVERWRITE 0x02/* snapshot from overwrite mode */ #define PERF_FLAG_FD_NO_GROUP (1UL << 0)#define PERF_FLAG_FD_OUTPUT (1UL << 1)
kernel/events/internal.h +1 -0
@@ -40,6 +40,7 @@ struct ring_buffer {local_t aux_nest;unsigned long aux_pgoff;int aux_nr_pages; +int aux_overwrite;atomic_t aux_mmap_count;unsigned long aux_mmap_locked;void (*free_aux)(void *);
kernel/events/ring_buffer.c +33 -15
78 lines not on this page · raw message
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 9/14 perf: Add wakeup watermark control to AUX area
In reply to Alexander ShishkinWhen AUX area gets a certain amount of new data, we want to wake up userspace to collect it. This adds a new control to specify how much data will cause a wakeup. This is then passed down to pmu drivers via output handle's "wakeup" field, so that the driver can find the nearest point where it can generate an interrupt.
We repurpose __reserved_2 in the event attribute for this, even though it was never checked to be zero before, aux_watermark will only matter for new AUX-aware code, so the old code should still be fine.
--- include/uapi/linux/perf_event.h | 7 +++++-- kernel/events/core.c | 3 ++- kernel/events/internal.h | 4 +++- kernel/events/ring_buffer.c | 22 +++++++++++++++++++--- 4 files changed, 29 insertions(+), 7 deletions(-)
include/uapi/linux/perf_event.h +5 -2
@@ -238,6 +238,7 @@ enum perf_event_read_format {#define PERF_ATTR_SIZE_VER2 80/* add: branch_sample_type */ #define PERF_ATTR_SIZE_VER3 96/* add: sample_regs_user */ /* add: sample_stack_user */ +/* add: aux_watermark */ /* * Hardware event_id to monitor via a performance monitoring event:
@@ -332,8 +333,10 @@ struct perf_event_attr {*/ __u32 sample_stack_user; -/* Align to u64. */ -__u32 __reserved_2; +/* +* Wakeup watermark for AUX area +*/ +__u32 aux_watermark; };#define perf_flags (attr) (*(&(attr)->read_format + 1))
kernel/events/core.c +2 -1
@@ -4419,7 +4419,8 @@ accounting:perf_event_init_userpage (event);perf_event_update_userpage (event); }else { - ret =rb_alloc_aux (rb, event, vma->vm_pgoff, nr_pages, flags); + ret =rb_alloc_aux (rb, event, vma->vm_pgoff, nr_pages, + event->attr.aux_watermark, flags);if (!ret) rb->aux_mmap_locked = extra; }
kernel/events/internal.h +3 -1
@@ -27,6 +27,7 @@ struct ring_buffer {local_t lost;/* nr records lost */ long watermark;/* wakeup watermark */ +long aux_watermark;/* poll crap */ spinlock_t event_lock;struct list_head event_list;
@@ -38,6 +39,7 @@ struct ring_buffer {/* AUX area */ local_t aux_head;local_t aux_nest; +local_t aux_wakeup;unsigned long aux_pgoff;int aux_nr_pages;int aux_overwrite;
@@ -57,7 +59,7 @@ extern struct ring_buffer * rb_alloc(int nr_pages, long watermark, int cpu, int flags);extern void perf_event_wakeup (struct perf_event *event);extern int rb_alloc_aux (struct ring_buffer *rb,struct perf_event *event, -pgoff_t pgoff,int nr_pages,int flags); +pgoff_t pgoff,int nr_pages,long watermark,int flags);extern void rb_free_aux (struct ring_buffer *rb);extern struct ring_buffer *ring_buffer_get (struct perf_event *event);extern void ring_buffer_put (struct ring_buffer *rb);
kernel/events/ring_buffer.c +19 -3
@@ -295,6 +295,7 @@ void *perf_aux_output_begin(struct perf_output_handle *handle,*/ if (!rb->aux_overwrite) { aux_tail =ACCESS_ONCE (rb->user_page->aux_tail); + handle->wakeup =local_read (&rb->aux_wakeup) + rb->aux_watermark;if (aux_head - aux_tail <perf_aux_size (rb)) handle->size =CIRC_SPACE (aux_head, aux_tail,perf_aux_size (rb));
@@ -358,9 +359,12 @@ void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,perf_event_aux_event (handle->event, aux_head, size, flags); } - rb->user_page->aux_head =local_read (&rb->aux_head); + aux_head = rb->user_page->aux_head =local_read (&rb->aux_head); -perf_output_wakeup (handle); +if (aux_head -local_read (&rb->aux_wakeup) >= rb->aux_watermark) { +perf_output_wakeup (handle); +local_add (rb->aux_watermark, &rb->aux_wakeup); + } handle->event =NULL ;local_set (&rb->aux_nest,0 );
@@ -382,6 +386,14 @@ int perf_aux_output_skip(struct perf_output_handle *handle, unsigned long size)local_add (size, &rb->aux_head); + aux_head = rb->user_page->aux_head =local_read (&rb->aux_head); +if (aux_head -local_read (&rb->aux_wakeup) >= rb->aux_watermark) { +perf_output_wakeup (handle); +local_add (rb->aux_watermark, &rb->aux_wakeup); + handle->wakeup =local_read (&rb->aux_wakeup) + + rb->aux_watermark; + } + handle->head = aux_head; handle->size -= size;
@@ -432,7 +444,7 @@ static void rb_free_aux_page(struct ring_buffer *rb, int idx) }int rb_alloc_aux (struct ring_buffer *rb,struct perf_event *event, -pgoff_t pgoff,int nr_pages,int flags) +pgoff_t pgoff,int nr_pages,long watermark,int flags) {bool overwrite = !(flags &RING_BUFFER_WRITABLE );int node = (event->cpu ==-1 ) ?-1 :cpu_to_node (event->cpu);
@@ -496,6 +508,10 @@ int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,atomic_set (&rb->aux_refcount,1 ); rb->aux_overwrite = overwrite; + rb->aux_watermark = watermark; + +if (!rb->aux_watermark && !rb->aux_overwrite) + rb->aux_watermark = nr_pages << (PAGE_SHIFT -1 ); out:if (!ret)
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 10/14 x86: Add Intel Processor Trace (INTEL_PT) cpu feature detection
In reply to Alexander ShishkinIntel Processor Trace is an architecture extension that allows for program flow tracing.
--- arch/x86/include/asm/cpufeature.h | 1 + arch/x86/kernel/cpu/scattered.c | 1 + 2 files changed, 2 insertions(+)
arch/x86/include/asm/cpufeature.h +1 -0
@@ -189,6 +189,7 @@#define X86_FEATURE_DTHERM ( 7*32+ 7)/* Digital Thermal Sensor */ #define X86_FEATURE_HW_PSTATE ( 7*32+ 8)/* AMD HW-PState */ #define X86_FEATURE_PROC_FEEDBACK ( 7*32+ 9)/* AMD ProcFeedbackInterface */ +#define X86_FEATURE_INTEL_PT ( 7*32+10)/* Intel Processor Trace */ /* Virtualization flags: Linux defined, word 8 */ #define X86_FEATURE_TPR_SHADOW ( 8*32+ 0)/* Intel TPR Shadow */
arch/x86/kernel/cpu/scattered.c +1 -0
@@ -36,6 +36,7 @@ void init_scattered_cpuid_features(struct cpuinfo_x86 *c) { X86_FEATURE_ARAT, CR_EAX, 2, 0x00000006, 0 }, { X86_FEATURE_PLN, CR_EAX, 4, 0x00000006, 0 }, { X86_FEATURE_PTS, CR_EAX, 6, 0x00000006, 0 }, + { X86_FEATURE_INTEL_PT, CR_EBX,25, 0x00000007, 0 }, { X86_FEATURE_APERFMPERF, CR_ECX, 0, 0x00000006, 0 }, { X86_FEATURE_EPB, CR_ECX, 3, 0x00000006, 0 }, { X86_FEATURE_HW_PSTATE, CR_EDX, 7, 0x80000007, 0 },
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 11/14 x86: perf: Intel PT and LBR/BTS are mutually exclusive
In reply to Alexander ShishkinIntel PT cannot be used at the same time as LBR or BTS and will cause a
general protection fault if they are used together. In order to avoid
fixing up GPs in the fast path, instead we disallow creating LBR/BTS events when PT events are present and vice versa.
--- arch/x86/kernel/cpu/perf_event.c | 43 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/perf_event.h | 40 +++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/perf_event_intel.c | 26 ++++++++++---------- 3 files changed, 95 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/perf_event.c +43 -0
@@ -259,6 +259,14 @@ static void hw_perf_event_destroy(struct perf_event *event) } } +void hw_perf_lbr_event_destroy (struct perf_event *event) +{ +hw_perf_event_destroy (event); + +/* undo the lbr/bts event accounting */ +x86_del_exclusive (x86_lbr_exclusive_lbr); +} +static inline int x86_pmu_initialized (void ) {return x86_pmu.handle_irq !=NULL ;
@@ -298,6 +306,35 @@ set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)return x86_pmu_extra_regs (val, event); } +/* +* Check if we can create event of a certain type (that no conflicting events +* are present). +*/ +int x86_add_exclusive (unsigned int what) +{ +int ret = -EBUSY , i; + +if (atomic_inc_not_zero (&x86_pmu.lbr_exclusive[what])) +return 0 ; + +mutex_lock (&pmc_reserve_mutex); +for (i =0 ; i <ARRAY_SIZE (x86_pmu.lbr_exclusive); i++) +if (i != what &&atomic_read (&x86_pmu.lbr_exclusive[i])) + goto out; + +atomic_inc (&x86_pmu.lbr_exclusive[what]); + ret =0 ; + +out: +mutex_unlock (&pmc_reserve_mutex); +return ret; +} + +void x86_del_exclusive (unsigned int what) +{ +atomic_dec (&x86_pmu.lbr_exclusive[what]); +} +int x86_setup_perfctr (struct perf_event *event) {struct perf_event_attr *attr = &event->attr;
@@ -342,6 +379,12 @@ int x86_setup_perfctr(struct perf_event *event)/* BTS is currently only allowed for user-mode. */ if (!attr->exclude_kernel)return -EOPNOTSUPP ; + +/* disallow bts if conflicting events are present */ +if (x86_add_exclusive (x86_lbr_exclusive_lbr)) +return -EBUSY ; + + event->destroy = hw_perf_lbr_event_destroy; } hwc->config |= config;
arch/x86/kernel/cpu/perf_event.h +40 -0
@@ -402,6 +402,12 @@ union x86_pmu_config { #define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value +enum { + x86_lbr_exclusive_lbr, + x86_lbr_exclusive_pt, + x86_lbr_exclusive_max, +}; +/* * struct x86_pmu - generic x86 pmu */
@@ -498,6 +504,11 @@ struct x86_pmu {bool lbr_double_abort;/* duplicated lbr aborts */ /* +* Intel PT/LBR/BTS are exclusive +*/ +atomic_t lbr_exclusive[x86_lbr_exclusive_max]; + +/* * Extra registers for events */ struct extra_reg *extra_regs;
@@ -582,6 +593,12 @@ static inline int x86_pmu_rdpmc_index(int index)return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index (index) : index; } +int x86_add_exclusive (unsigned int what); + +void x86_del_exclusive (unsigned int what); + +void hw_perf_lbr_event_destroy (struct perf_event *event); +int x86_setup_perfctr (struct perf_event *event);int x86_pmu_hw_config (struct perf_event *event);
@@ -668,6 +685,29 @@ static inline int amd_pmu_init(void)#ifdef CONFIG_CPU_SUP_INTEL +static inline bool intel_pmu_needs_lbr_smpl (struct perf_event *event) +{ +/* user explicitly requested branch sampling */ +if (has_branch_stack (event)) +return true; + +/* implicit branch sampling to correct PEBS skid */ +if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip >1 && + x86_pmu.intel_cap.pebs_format <2 ) +return true; + +return false; +} + +static inline bool intel_pmu_has_bts (struct perf_event *event) +{ +if (event->attr.config ==PERF_COUNT_HW_BRANCH_INSTRUCTIONS && + !event->attr.freq && event->hw.sample_period ==1 ) +return true; + +return false; +} +int intel_pmu_save_and_restart (struct perf_event *event); struct event_constraint *
arch/x86/kernel/cpu/perf_event_intel.c +12 -14
@@ -1029,20 +1029,6 @@ static __initconst const u64 slm_hw_cache_event_ids }, }; -static inline bool intel_pmu_needs_lbr_smpl (struct perf_event *event) -{ -/* user explicitly requested branch sampling */ -if (has_branch_stack (event)) -return true; - -/* implicit branch sampling to correct PEBS skid */ -if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip >1 && - x86_pmu.intel_cap.pebs_format <2 ) -return true; - -return false; -} -static void intel_pmu_disable_all (void ) {struct cpu_hw_events *cpuc =this_cpu_ptr (&cpu_hw_events);
@@ -1751,6 +1737,18 @@ static int intel_pmu_hw_config(struct perf_event *event) ret =intel_pmu_setup_lbr_filter (event);if (ret)return ret; + +/* +* bts is set up earlier in this path, so don't account +* twice +*/ +if (!intel_pmu_has_bts (event)) { +/* disallow lbr if conflicting events are present */ +if (x86_add_exclusive (x86_lbr_exclusive_lbr)) +return -EBUSY ; + + event->destroy = hw_perf_lbr_event_destroy; + } }if (event->attr.type !=PERF_TYPE_RAW )
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 12/14 x86: perf: intel_pt: Intel PT PMU driver
In reply to Alexander ShishkinAdd support for Intel Processor Trace (PT) to kernel's perf events. PT is an extension of Intel Architecture that collects information about software execuction such as control flow, execution modes and timings and formats it into highly compressed binary packets. Even being compressed, these packets are generated at hundreds of megabytes per second per core, which makes it impractical to decode them on the fly in the kernel.
This driver exports trace data by through AUX space in the perf ring buffer, which is zero-copy mapped into userspace for faster data retrieval.
--- arch/x86/include/uapi/asm/msr-index.h | 18 + arch/x86/kernel/cpu/Makefile | 1 + arch/x86/kernel/cpu/intel_pt.h | 131 ++++ arch/x86/kernel/cpu/perf_event.h | 2 + arch/x86/kernel/cpu/perf_event_intel.c | 8 + arch/x86/kernel/cpu/perf_event_intel_pt.c | 1082 +++++++++++++++++++++++++++++ 6 files changed, 1242 insertions(+) create mode 100644 arch/x86/kernel/cpu/intel_pt.h create mode 100644 arch/x86/kernel/cpu/perf_event_intel_pt.c
arch/x86/include/uapi/asm/msr-index.h +18 -0
@@ -74,6 +74,24 @@#define MSR_IA32_PERF_CAPABILITIES 0x00000345#define MSR_PEBS_LD_LAT_THRESHOLD 0x000003f6 +#define MSR_IA32_RTIT_CTL 0x00000570 +#define RTIT_CTL_TRACEEN BIT(0) +#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_TSC_EN BIT(10) +#define RTIT_CTL_DISRETC BIT(11) +#define RTIT_CTL_BRANCH_EN BIT(13) +#define MSR_IA32_RTIT_STATUS 0x00000571 +#define RTIT_STATUS_CONTEXTEN BIT(1) +#define RTIT_STATUS_TRIGGEREN BIT(2) +#define RTIT_STATUS_ERROR BIT(4) +#define RTIT_STATUS_STOPPED BIT(5) +#define MSR_IA32_RTIT_CR3_MATCH 0x00000572 +#define MSR_IA32_RTIT_OUTPUT_BASE 0x00000560 +#define MSR_IA32_RTIT_OUTPUT_MASK 0x00000561 +#define MSR_MTRRfix64K_00000 0x00000250#define MSR_MTRRfix16K_80000 0x00000258#define MSR_MTRRfix16K_A0000 0x00000259
arch/x86/kernel/cpu/Makefile +1 -0
@@ -40,6 +40,7 @@ endif obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_p6.o perf_event_knc.o perf_event_p4.o obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_lbr.o perf_event_intel_ds.o perf_event_intel.o obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_rapl.o +obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_pt.o obj-$(CONFIG_PERF_EVENTS_INTEL_UNCORE) += perf_event_intel_uncore.o \ perf_event_intel_uncore_snb.o \
arch/x86/kernel/cpu/intel_pt.h new file +131 -0
131 lines not on this page · raw message
arch/x86/kernel/cpu/perf_event.h +2 -0
@@ -783,6 +783,8 @@ void intel_pmu_lbr_init_snb(void);int intel_pmu_setup_lbr_filter (struct perf_event *event); +void intel_pt_interrupt (void ); +int p4_pmu_init (void );int p6_pmu_init (void );
arch/x86/kernel/cpu/perf_event_intel.c +8 -0
@@ -1385,6 +1385,14 @@ again: }/* +* Intel PT +*/ +if (__test_and_clear_bit (55 , (unsigned long *)&status)) { + handled++; +intel_pt_interrupt (); + } + +/* * Checkpointed counters can lead to 'spurious' PMIs because the * rollback caused by the PMI will have cleared the overflow status * bit. Therefore always force probe these counters.
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +1082 -0
1,082 lines not on this page · raw message
Signature · 1 line
2.1.1
Peter Zijlstra
maintainerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
arch/x86/kernel/cpu/perf_event_intel_pt.c line 921
Quoted 5 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +5
-0+static void pt_event_stop (struct perf_event *event,int mode) +{ +struct pt *pt =this_cpu_ptr (&pt_ctx); + +ACCESS_ONCE (pt->handle_nmi) =0 ;
Why is this needed? Will the hardware still generate interrupts if you stop the PT thing?
Alexander Shishkin
reviewerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
Quoted 9 lines · Peter Zijlstra
arch/x86/kernel/cpu/perf_event_intel_pt.c line 921
Quoted 5 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +5
-0+static void pt_event_stop (struct perf_event *event,int mode) +{ +struct pt *pt =this_cpu_ptr (&pt_ctx); + +ACCESS_ONCE (pt->handle_nmi) =0 ;Why is this needed? Will the hardware still generate interrupts if you stop the PT thing?
It's meant to serialize nmi handler against the pmu:: callbacks, but now you mentioned it, it looks very much redundant.
Cheers,
--
Alex
Alexander Shishkin
reviewerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
In reply to Peter ZijlstraQuoted 9 lines · Peter Zijlstra
arch/x86/kernel/cpu/perf_event_intel_pt.c line 921
Quoted 5 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +5
-0+static void pt_event_stop (struct perf_event *event,int mode) +{ +struct pt *pt =this_cpu_ptr (&pt_ctx); + +ACCESS_ONCE (pt->handle_nmi) =0 ;Why is this needed? Will the hardware still generate interrupts if you stop the PT thing?
Actually, it turns out that the interrupt condition can race with the wrmsr that disables tracing and the PT bit remains set in the global status register, causing the next PMI to go into the PT PMI handler, which then may decide to re-enable the counter that should be disabled.
Regards,
--
Alex
Peter Zijlstra
maintainerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
Quoted 12 lines · Alexander Shishkin
Quoted 9 lines · Peter Zijlstra
arch/x86/kernel/cpu/perf_event_intel_pt.c line 921
Quoted 5 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +5
-0+static void pt_event_stop (struct perf_event *event,int mode) +{ +struct pt *pt =this_cpu_ptr (&pt_ctx); + +ACCESS_ONCE (pt->handle_nmi) =0 ;Why is this needed? Will the hardware still generate interrupts if you stop the PT thing?
Quoted 4 lines
Actually, it turns out that the interrupt condition can race with the wrmsr that disables tracing and the PT bit remains set in the global status register, causing the next PMI to go into the PT PMI handler, which then may decide to re-enable the counter that should be disabled.
OK, please add this information in a suitably placed comment.
Thanks!
Peter Zijlstra
maintainerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
In reply to Alexander ShishkinQuoted 23 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +23
-0+static void pt_handle_status (struct pt *pt) +{ +struct pt_buffer *buf =perf_get_aux (&pt->handle); +int advance =0 ; +u64 status; + +rdmsrl (MSR_IA32_RTIT_STATUS , status); + +if (status &RTIT_STATUS_ERROR ) { +pr_err_ratelimited ("ToPA ERROR encountered, trying to recover\n" ); +pt_topa_dump (buf); + status &= ~RTIT_STATUS_ERROR ; + } + +if (status &RTIT_STATUS_STOPPED ) { + status &= ~RTIT_STATUS_STOPPED ; + +/* +* On systems that only do single-entry ToPA, hitting STOP +* means we are already losing data; need to let the decoder +* know. +*/ + if (!pt_cap_get(PT_CAP_topa_multiple_entries) ||
arch/x86/kernel/cpu/perf_event_intel_pt.c line 535
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +10
-0+ buf->output_off == sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size)) { +local_inc (&buf->lost); + advance++; + } + } + +/* +* Also on single-entry ToPA implementations, interrupt will come +* before the output reaches its output region's boundary. +*/
My understanding was that, while yes it will attempt to generate the interrupt early, there is absolutely no guarantee it will in fact arrive in time or even it if does, guarantee that there is buffer left by the time you're ready to read it.
arch/x86/kernel/cpu/perf_event_intel_pt.c line 537
Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +2
-0+if (!pt_cap_get (PT_CAP_topa_multiple_entries) && !buf->snapshot && +pt_buffer_region_size (buf) - buf->output_off <=TOPA_PMI_MARGIN ) {
Now this check appears to indeed cater for this scenario where the buffer is overran, right?
Quoted 14 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +14
-0+void *head =pt_buffer_region (buf); + +/* everything within this margin needs to be zeroed out */ +memset (head + buf->output_off,0 , +pt_buffer_region_size (buf) - + buf->output_off); + advance++; + } + +if (advance) +pt_buffer_advance (buf); + +wrmsrl (MSR_IA32_RTIT_STATUS , status); +}
Alexander Shishkin
reviewerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
Quoted 31 lines · Peter Zijlstra
Quoted 30 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +30
-0+static void pt_handle_status (struct pt *pt) +{ +struct pt_buffer *buf =perf_get_aux (&pt->handle); +int advance =0 ; +u64 status; + +rdmsrl (MSR_IA32_RTIT_STATUS , status); + +if (status &RTIT_STATUS_ERROR ) { +pr_err_ratelimited ("ToPA ERROR encountered, trying to recover\n" ); +pt_topa_dump (buf); + status &= ~RTIT_STATUS_ERROR ; + } + +if (status &RTIT_STATUS_STOPPED ) { + status &= ~RTIT_STATUS_STOPPED ; + +/* +* On systems that only do single-entry ToPA, hitting STOP +* means we are already losing data; need to let the decoder +* know. +*/ +if (!pt_cap_get (PT_CAP_topa_multiple_entries) || + buf->output_off ==sizes (TOPA_ENTRY (buf->cur, buf->cur_idx)->size)) { +local_inc (&buf->lost); + advance++; + } + } + +/*
Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c line 535
Quoted 3 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +3
-0+* Also on single-entry ToPA implementations, interrupt will come +* before the output reaches its output region's boundary. +*/ My understanding was that, while yes it will attempt to generate the interrupt early, there is absolutely no guarantee it will in fact arrive in time or even it if does, guarantee that there is buffer left by the time you're ready to read it.
That's correct. If the PMI didn't arrive on time and the buffer got filled up, the STOPPED bit will be set in the STATUS register, which is what we're handling above.
Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c line 537
Quoted 2 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +2
-0+if (!pt_cap_get (PT_CAP_topa_multiple_entries) && !buf->snapshot && +pt_buffer_region_size (buf) - buf->output_off <=TOPA_PMI_MARGIN ) {Now this check appears to indeed cater for this scenario where the buffer is overran, right?
The check above is for the case when the PMI did make it before the end of the physical buffer was reached.
Quoted 15 lines
Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +4
-0+void *head =pt_buffer_region (buf); + +/* everything within this margin needs to be zeroed out */ + memset(head + buf->output_off, 0,arch/x86/kernel/cpu/perf_event_intel_pt.c line 551
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +10
-0+pt_buffer_region_size (buf) - + buf->output_off); + advance++; + } + +if (advance) +pt_buffer_advance (buf); + +wrmsrl (MSR_IA32_RTIT_STATUS , status); +}
Regards,
--
Alex
Peter Zijlstra
maintainerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
In reply to Alexander ShishkinQuoted 16 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +16
-0+static __init int pt_init (void ) +{ +int ret, cpu, prior_warn =0 ; + +BUILD_BUG_ON (sizeof (struct topa ) >PAGE_SIZE ); +get_online_cpus (); +for_each_online_cpu (cpu) { +u64 ctl; + + ret =rdmsrl_safe_on_cpu (cpu,MSR_IA32_RTIT_CTL , &ctl); +if (!ret && (ctl &RTIT_CTL_TRACEEN )) + prior_warn++; + } +put_online_cpus (); + + ret =pt_pmu_hw_init ();
arch/x86/kernel/cpu/perf_event_intel_pt.c line 1060
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +10
-0+if (ret) +return ret; + +if (!pt_cap_get (PT_CAP_topa_output)) { +pr_warn ("ToPA output is not supported on this CPU\n" ); +return -ENODEV ; + } + +if (prior_warn) +pr_warn ("PT is enabled at boot time, traces may be empty\n" );
Should we not also add_exclusive(pt) here?
Also, if its already enabled, should we not return ENODEV as well, no saying who or what programmed it, we should not be touching it.
Quoted 19 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +19
-0+if (!pt_cap_get (PT_CAP_topa_multiple_entries)) + pt_pmu.pmu.capabilities = +PERF_PMU_CAP_AUX_NO_SG |PERF_PMU_CAP_AUX_SW_DOUBLEBUF ; + + pt_pmu.pmu.capabilities |=PERF_PMU_CAP_EXCLUSIVE |PERF_PMU_CAP_ITRACE ; + pt_pmu.pmu.attr_groups = pt_attr_groups; + pt_pmu.pmu.task_ctx_nr = perf_hw_context; + pt_pmu.pmu.event_init = pt_event_init; + pt_pmu.pmu.add = pt_event_add; + pt_pmu.pmu.del = pt_event_del; + pt_pmu.pmu.start = pt_event_start; + pt_pmu.pmu.stop = pt_event_stop; + pt_pmu.pmu.read = pt_event_read; + pt_pmu.pmu.setup_aux = pt_buffer_setup_aux; + pt_pmu.pmu.free_aux = pt_buffer_free_aux; + ret =perf_pmu_register (&pt_pmu.pmu,"intel_pt" ,-1 ); + +return ret; +}
Alexander Shishkin
reviewerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
Quoted 21 lines · Peter Zijlstra
Quoted 20 lines · Alexander Shishkin
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +20
-0+static __init int pt_init (void ) +{ +int ret, cpu, prior_warn =0 ; + +BUILD_BUG_ON (sizeof (struct topa ) >PAGE_SIZE ); +get_online_cpus (); +for_each_online_cpu (cpu) { +u64 ctl; + + ret =rdmsrl_safe_on_cpu (cpu,MSR_IA32_RTIT_CTL , &ctl); +if (!ret && (ctl &RTIT_CTL_TRACEEN )) + prior_warn++; + } +put_online_cpus (); + + ret =pt_pmu_hw_init (); +if (ret) +return ret; + +if (!pt_cap_get (PT_CAP_topa_output)) {
Quoted 8 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c line 1060
Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +6
-0+pr_warn ("ToPA output is not supported on this CPU\n" ); +return -ENODEV ; + } + +if (prior_warn) +pr_warn ("PT is enabled at boot time, traces may be empty\n" );Should we not also
add_exclusive(pt)here?
Good point.
Quoted 2 lines
Also, if its already enabled, should we not return ENODEV as well, no saying who or what programmed it, we should not be touching it.
Indeed we should, although I'd also like to have a force-override boot time option for this.
Thanks,
--
Alex
Alexander Shishkin
reviewerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
In reply to Peter ZijlstraQuoted 2 lines · Peter Zijlstra
Also, if its already enabled, should we not return ENODEV as well, no saying who or what programmed it, we should not be touching it.
Or EBUSY?
Regards,
--
Alex
Peter Zijlstra
maintainerRe: [PATCH v8 12/14] x86: perf: intel_pt: Intel PT PMU driver
Quoted 6 lines · Alexander Shishkin
Quoted 2 lines · Peter Zijlstra
Also, if its already enabled, should we not return ENODEV as well, no saying who or what programmed it, we should not be touching it.
Or EBUSY?
Sure, same end result I imagine, driver doesn't load.
Alexander Shishkin
reviewerPATCH v8 13/14 x86: perf: intel_bts: Add BTS PMU driver
In reply to Alexander ShishkinAdd support for Branch Trace Store (BTS) via kernel perf event infrastructure. The difference with the existing implementation of BTS support is that this one is a separate PMU that exports events' trace buffers to userspace by means of AUX area of the perf buffer, which is zero-copy mapped into userspace.
The immediate benefit is that the buffer size can be much bigger, resulting in fewer interrupts and no kernel side copying is involved and little to no trace data loss. Also, kernel code can be traced with this driver.
The old way of collecting BTS traces still works.
--- arch/x86/kernel/cpu/Makefile | 2 +- arch/x86/kernel/cpu/perf_event.h | 7 + arch/x86/kernel/cpu/perf_event_intel.c | 6 +- arch/x86/kernel/cpu/perf_event_intel_bts.c | 525 +++++++++++++++++++++++++++++ arch/x86/kernel/cpu/perf_event_intel_ds.c | 3 +- 5 files changed, 540 insertions(+), 3 deletions(-) create mode 100644 arch/x86/kernel/cpu/perf_event_intel_bts.c
arch/x86/kernel/cpu/Makefile +1 -1
@@ -40,7 +40,7 @@ endif obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_p6.o perf_event_knc.o perf_event_p4.o obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_lbr.o perf_event_intel_ds.o perf_event_intel.o obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_rapl.o -obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_pt.o +obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_pt.o perf_event_intel_bts.o obj-$(CONFIG_PERF_EVENTS_INTEL_UNCORE) += perf_event_intel_uncore.o \ perf_event_intel_uncore_snb.o \
arch/x86/kernel/cpu/perf_event.h +7 -0
@@ -404,6 +404,7 @@ union x86_pmu_config {enum { x86_lbr_exclusive_lbr, + x86_lbr_exclusive_bts, x86_lbr_exclusive_pt, x86_lbr_exclusive_max, };
@@ -785,6 +786,12 @@ int intel_pmu_setup_lbr_filter(struct perf_event *event);void intel_pt_interrupt (void ); +int intel_bts_interrupt (void ); + +void intel_bts_enable_local (void ); + +void intel_bts_disable_local (void ); +int p4_pmu_init (void );int p6_pmu_init (void );
arch/x86/kernel/cpu/perf_event_intel.c +5 -1
@@ -1037,6 +1037,8 @@ static void intel_pmu_disable_all(void)if (test_bit (INTEL_PMC_IDX_FIXED_BTS , cpuc->active_mask))intel_pmu_disable_bts (); +else +intel_bts_disable_local ();intel_pmu_pebs_disable_all ();intel_pmu_lbr_disable_all ();
@@ -1059,7 +1061,8 @@ static void intel_pmu_enable_all(int added)return ;intel_pmu_enable_bts (event->hw.config); - } + }else +intel_bts_enable_local (); }/*
@@ -1345,6 +1348,7 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)apic_write (APIC_LVTPC ,APIC_DM_NMI );intel_pmu_disable_all (); handled =intel_pmu_drain_bts_buffer (); + handled +=intel_bts_interrupt (); status =intel_pmu_get_status ();if (!status) goto done;
arch/x86/kernel/cpu/perf_event_intel_bts.c new file +525 -0
525 lines not on this page · raw message
arch/x86/kernel/cpu/perf_event_intel_ds.c +2 -1
@@ -461,7 +461,8 @@ void intel_pmu_enable_bts(u64 config) debugctlmsr |=DEBUGCTLMSR_TR ; debugctlmsr |=DEBUGCTLMSR_BTS ; - debugctlmsr |=DEBUGCTLMSR_BTINT ; +if (config &ARCH_PERFMON_EVENTSEL_INT ) + debugctlmsr |=DEBUGCTLMSR_BTINT ;if (!(config &ARCH_PERFMON_EVENTSEL_OS )) debugctlmsr |=DEBUGCTLMSR_BTS_OFF_OS ;
Signature · 1 line
2.1.1
Alexander Shishkin
reviewerPATCH v8 14/14 perf: add ITRACE_START record to indicate that tracing has started
In reply to Alexander ShishkinFor counters that generate AUX data that is bound to the context of a running task, such as instruction tracing, the decoder needs to know exactly which task is running when the event is first scheduled in, before the first sched_switch. The decoder's need to know this stems from the fact that instruction flow trace decoding will almost always require program's object code in order to reconstruct said flow and for that we need at least its pid/tid in the perf stream.
To single out such instruction tracing pmus, this patch introduces ITRACE PMU capability. The reason this is not part of RECORD_AUX record is that not all pmus capable of generating AUX data need this, and the opposite is *probably* also true.
While sched_switch covers for most cases, there are two problems with it:
the consumer will need to process events out of order (that is, having found RECORD_AUX, it will have to skip forward to the nearest sched_switch to figure out which task it was, then go back to the actual trace to decode it) and it completely misses the case when the tracing is enabled and disabled before sched_switch, for example, via PERF_EVENT_IOC_DISABLE.
--- include/linux/perf_event.h | 4 ++++ include/uapi/linux/perf_event.h | 11 +++++++++++ kernel/events/core.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+)
include/linux/perf_event.h +4 -0
@@ -127,6 +127,9 @@ struct hw_perf_event {/* for tp_event->class */ struct list_head tp_list; }; +struct {/* itrace */ +int itrace_started; + }; #ifdef CONFIG_HAVE_HW_BREAKPOINT struct { /* breakpoint */ /*
@@ -174,6 +177,7 @@ struct perf_event;#define PERF_PMU_CAP_AUX_NO_SG 0x02#define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x04#define PERF_PMU_CAP_EXCLUSIVE 0x08 +#define PERF_PMU_CAP_ITRACE 0x10/** * struct pmu - generic performance monitoring unit
include/uapi/linux/perf_event.h +11 -0
@@ -750,6 +750,17 @@ enum perf_event_type {*/ PERF_RECORD_AUX =11 , +/* +* Indicates that instruction trace has started +* +* struct { +* struct perf_event_header header; +* u32 pid; +* u32 tid; +* }; +*/ +PERF_RECORD_ITRACE_START =12 , + PERF_RECORD_MAX, /* non-ABI */ };
kernel/events/core.c +41 -0
59 lines not on this page · raw message
Signature · 1 line
2.1.1
Peter Zijlstra
maintainerRe: [PATCH v8 14/14] perf: add ITRACE_START record to indicate that tracing has started
include/uapi/linux/perf_event.h enum perf_event_type { · line 759
Quoted 12 lines · Alexander Shishkin
+++
b/include/uapi/linux/perf_event.hinclude/uapi/linux/perf_event.h +7
-0@@ -750,6 +750,17 @@ enum perf_event_type {*/ PERF_RECORD_AUX = 11, +/* +* Indicates that instruction trace has started +* +* struct { +* struct perf_event_header header; +* u32 pid; +* u32 tid;
The below function suggests we should have:
struct sample_id sample_id;
Quoted 44 lines
(quoted diff) +41
-0+ * }; + */ + PERF_RECORD_ITRACE_START = 12, + PERF_RECORD_MAX, /* non-ABI */ }; +static void perf_log_itrace_start(struct perf_event *event) +{ + struct perf_output_handle handle; + struct perf_sample_data sample; + struct perf_aux_event { + struct perf_event_header header; + u32 pid; + u32 tid; + } rec; + int ret; + + if (event->parent) + event = event->parent; + + if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || + 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); + rec.pid = perf_event_pid(event, current); + rec.tid = perf_event_tid(event, current); + + perf_event_header__init_id(&rec.header, &sample, event); + ret = perf_output_begin(&handle, event, rec.header.size); + + if (ret) + return; + + perf_output_put(&handle, rec); + perf_event__output_id_sample(event, &handle, &sample); + + perf_output_end(&handle); +}
Peter Zijlstra
maintainerRe: [PATCH v8 14/14] perf: add ITRACE_START record to indicate that tracing has started
Quoted 16 lines · Peter Zijlstra
include/uapi/linux/perf_event.h enum perf_event_type { · line 759
Quoted 12 lines · Alexander Shishkin
+++
b/include/uapi/linux/perf_event.hinclude/uapi/linux/perf_event.h +7
-0@@ -750,6 +750,17 @@ enum perf_event_type {*/ PERF_RECORD_AUX = 11, +/* +* Indicates that instruction trace has started +* +* struct { +* struct perf_event_header header; +* u32 pid; +* u32 tid; The below function suggests we should have:
Quoted 5 lines
struct sample_id sample_id;include/uapi/linux/perf_event.h enum perf_event_type { · line 762
Quoted 3 lines
include/uapi/linux/perf_event.h +3
-0+* }; +*/ + PERF_RECORD_ITRACE_START = 12,
This also raises the question; why not use PERF_RECORD_COMM?
Adrian Hunter
reviewerRe: [PATCH v8 14/14] perf: add ITRACE_START record to indicate that tracing has started
Quoted 17 lines · Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
include/uapi/linux/perf_event.h enum perf_event_type { · line 759
Quoted 12 lines · Alexander Shishkin
+++
b/include/uapi/linux/perf_event.hinclude/uapi/linux/perf_event.h +7
-0@@ -750,6 +750,17 @@ enum perf_event_type {*/ PERF_RECORD_AUX = 11, +/* +* Indicates that instruction trace has started +* +* struct { +* struct perf_event_header header; +* u32 pid; +* u32 tid; The below function suggests we should have:
Quoted 7 lines
Quoted 5 lines
struct sample_id sample_id;include/uapi/linux/perf_event.h enum perf_event_type { · line 762
Quoted 3 lines
include/uapi/linux/perf_event.h +3
-0+* }; +*/ + PERF_RECORD_ITRACE_START = 12,This also raises the question; why not use
PERF_RECORD_COMM?
They mean slightly different things.
PERF_RECORD_COMM means the comm string has been set. We could infer that tid was the currently running task but we don't at the moment, and it would be a constraint that should be documented if we are going to rely on it. Also it might mess up perf tools a little which calls the idle thread "swapper" but the kernel calls it "swapper/n" on SMP where n is the cpu number.
PERF_RECORD_ITRACE_START just means that pid/tid was the currently running task.
Alexander Shishkin
reviewerQuoted 1 line · Alexander Shishkin
Hi Peter and all,
Hi again,
Quoted 2 lines
Since there weren't too many review comments on the two previous versions, here's another one with a couple more fixes.
This patchset is one month old now, I thought somebody might want to take a look before a send a rebased new version.
Cheers,
--
Alex
Alexander Shishkin
reviewerQuoted 5 lines · Alexander Shishkin
Quoted 1 line · Alexander Shishkin
Hi Peter and all,
Hi again,
Ping^2
Regards,
--
Alex