perf: Support PERF_SAMPLE_READ with inherit
v7 cover perf: Support PERF_SAMPLE_READ with inherit
This change allows events to use PERF_SAMPLE READ with inherit so long as PERF_SAMPLE_TID is also set.
Currently it is not possible to use PERF_SAMPLE_READ with inherit. This restriction assumes the user is interested in collecting aggregate statistics as per perf stat. It prevents a user from collecting per-thread samples using counter groups from a multi-threaded or multi-process application, as with perf record -e '{....}:S'. Instead users must use system-wide mode, or forgo the ability to sample counter groups. System-wide mode is often problematic as it requires specific permissions (no CAP_PERFMON / root access), or may lead to capture of significant amounts of extra data from other processes running on the system.
This patch changes perf_event_alloc relaxing the restriction against combining inherit with PERF_SAMPLE_READ so that the combination will be allowed so long as PERF_SAMPLE_TID is enabled. It modifies sampling so that only the count associated with the active thread is recorded into the buffer. It modifies the context switch handling so that perf contexts are always switched out if they have this kind of event so that the correct per-thread state is maintained. Finally, the tools are updated to allow perf record to specify this combination and to correctly decode the sample data.
In this configuration stream ids (such as may appear in the read_format field of a PERF_RECORD_SAMPLE) are no longer globally unique, rather the pair of (stream id, tid) uniquely identify each event. Tools that rely on this, for example to calculate a delta between samples, would need updating to take this into account. Previously valid event configurations (system-wide, no-inherit and so on) where each stream id is the identifier are unaffected.
Changes since v6:
- Rebase on v6.10-rc2
- Make additional "perf test" tests succeed / skip based on kernel version as per feedback from Namhyung.
Changes since v5:
- Rebase on v6.9
- Cleanup feedback from Namhyung Kim
Changes since v4:
- Rebase on v6.9-rc1
- Removed the dependency on inherit_stat that was previously assumed necessary as per feedback from Namhyung Kim.
- Fixed an incorrect use of zfree instead of free in the tools leading to an abort on tool shutdown.
- Additional test coverage improvements added to perf test.
- Cleaned up the remaining bit of irrelevant change missed between v3 and v4.
Changes since v3:
- Cleaned up perf test data changes incorrectly included into this series from elsewhere.
Changes since v2:
- Rebase on v6.8
- Respond to James Clarke's feedback; fixup some typos and move some repeated checks into a helper macro.
- Cleaned up checkpatch lints.
- Updated perf test; fixed evsel handling so that existing tests pass and added new tests to cover the new behaviour.
Changes since v1:
- Rebase on v6.8-rc1
- Fixed value written into sample after child exists.
- Modified handling of switch-out so that context with these events take the slow path, so that the per-event/per-thread PMU state is correctly switched.
- Modified perf tools to support this mode of operation.
Ben Gainey (4):
perf: Support PERF_SAMPLE_READ with inherit tools/perf: Track where perf_sample_ids need per-thread periods tools/perf: Correctly calculate sample period for inherited
SAMPLE_READ values
tools/perf: Allow inherit + PERF_SAMPLE_READ when opening events
include/linux/perf_event.h | 1 +
kernel/events/core.c | 78 ++++++++++++++-----
tools/lib/perf/evlist.c | 1 +
tools/lib/perf/evsel.c | 48 ++++++++++++
tools/lib/perf/include/internal/evsel.h | 54 ++++++++++++-
tools/perf/tests/attr/README | 2 +
.../tests/attr/test-record-group-sampling | 3 +-
.../tests/attr/test-record-group-sampling1 | 51 ++++++++++++
.../tests/attr/test-record-group-sampling2 | 61 +++++++++++++++
tools/perf/tests/attr/test-record-group2 | 1 +
...{test-record-group2 => test-record-group3} | 10 ++-
tools/perf/util/evsel.c | 19 ++++-
tools/perf/util/evsel.h | 1 +
tools/perf/util/session.c | 11 ++-
14 files changed, 308 insertions(+), 33 deletions(-)
create mode 100644 tools/perf/tests/attr/test-record-group-sampling1
create mode 100644 tools/perf/tests/attr/test-record-group-sampling2
copy tools/perf/tests/attr/{test-record-group2 => test-record-group3} (81%)Signature · 1 line
2.45.2
PATCH v7 1/4 perf: Support PERF_SAMPLE_READ with inherit
This change allows events to use PERF_SAMPLE READ with inherit so long as PERF_SAMPLE_TID is also set.
In this configuration, an event will be inherited into any child processes / threads, allowing convenient profiling of a multiprocess or multithreaded application, whilst allowing profiling tools to collect per-thread samples, in particular of groups of counters.
The read_format field of both PERF_RECORD_READ and PERF_RECORD_SAMPLE are changed by this new configuration, but calls to read() on the same event file descriptor are unaffected and continue to return the cumulative total.
--- include/linux/perf_event.h | 1 + kernel/events/core.c | 78 ++++++++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 21 deletions(-)
include/linux/perf_event.h +1 -0
@@ -935,6 +935,7 @@ struct perf_event_context {int nr_task_data;int nr_stat; +int nr_inherit_read;int nr_freq;int rotate_disable;
kernel/events/core.c +57 -21
216 lines not on this page · raw message
Signature · 1 line
2.45.2
Peter Zijlstra
maintainerQuoted 8 lines · Ben Gainey
This change allows events to use
PERF_SAMPLEREAD with inherit so long asPERF_SAMPLE_TIDis also set.In this configuration, an event will be inherited into any child processes / threads, allowing convenient profiling of a multiprocess or multithreaded application, whilst allowing profiling tools to collect per-thread samples, in particular of groups of counters.
Perhaps a few words on *WHY* this is important.
Quoted 4 lines
The
read_formatfield of bothPERF_RECORD_READandPERF_RECORD_SAMPLEare changed by this new configuration, but calls toread()on the same event file descriptor are unaffected and continue to return the cumulative total.
This is unfortunate. Up to this point they were the same. Also, I see no change to the uapi file. So were you trying to say that only read_format::value is changed to be the thread local value as opposed to the hierarchy total?
Please fix the wording to be unambiguous as to what is actually meant. Also try and justify why it is okay to break this symmetry.
kernel/events/core.c perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) · line 3550
Quoted 9 lines
kernel/events/core.c +3
-1@@ -3532,11 +3544,18 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)perf_ctx_disable (ctx, false);/* PMIs are disabled; ctx->nr_pending is stable. */ -if (local_read (&ctx->nr_pending) || +if (ctx->nr_inherit_read || + next_ctx->nr_inherit_read || +local_read (&ctx->nr_pending) ||local_read (&next_ctx->nr_pending)) {
This seems unfortunate, nr_pending and nr_inherit_read are both used exclusively to inhibit this context switch optimization. Surely they can share the exact same counter.
That is, rename nr_pending and use it for both?
Quoted 12 lines
kernel/events/core.c +5
-0/* * Must not swap out ctx when there's pending * events that rely on the ctx->task relation. +* +* Likewise, when a context contains inherit + +* SAMPLE_READ events they should be switched +* out using the slow path so that they are +* treated as if they were distinct contexts. */ raw_spin_unlock (&next_ctx->lock);rcu_read_unlock ();
kernel/events/core.c static void __perf_event_read(void *info) · line 4574
Quoted 5 lines
kernel/events/core.c +1
-1@@ -4552,11 +4571,19 @@ static void __perf_event_read(void *info)raw_spin_unlock (&ctx->lock); } -static inline u64 perf_event_count (struct perf_event *event) +static inline u64 perf_event_count_cumulative (struct perf_event *event)
I don't think you need this -- overly long and hard to type function name...
kernel/events/core.c static void __perf_event_read(void *info) · line 4582
Quoted 8 lines
kernel/events/core.c +4
-0{return local64_read (&event->count) +atomic64_read (&event->child_count); } +static inline u64 perf_event_count (struct perf_event *event,bool self_value_only) +{ +if (self_value_only &&has_inherit_and_sample_read (&event->attr)) +return local64_read (&event->count);
... if this @self_value_only argument was actually used as such -- it isn't, see how you use 'from_sample' which is something else entirely. Which then also caused to you fix it up and make a mess with that && has_inherit_and_sample_read() nonsense. (also, shorter function names, more good)
kernel/events/core.c static void __perf_event_read(void *info) · line 4585
Quoted 3 lines
kernel/events/core.c +3
-0+ +return perf_event_count_cumulative (event); +}
That is, I would really rather you had:
static inline u64 perf_event_count(struct perf_event *event, bool self)
{
if (self)
return local64_read(&event->count);
return local64_read(&event->count) + local64_read(&event->child_count);
}And then actually use that argument as intended.
Quoted 4 lines
kernel/events/core.c +0
-0@@ -7205,13 +7232,14 @@ void perf_event__output_id_sample(struct perf_event *event, static void perf_output_read_one(struct perf_output_handle *handle, struct perf_event *event,
kernel/events/core.c void perf_event__output_id_sample(struct perf_event *event, · line 7242
Quoted 10 lines
kernel/events/core.c +3
-2- u64 enabled, u64 running) + u64 enabled, u64 running, + bool from_sample) { u64 read_format = event->attr.read_format;u64 values[5 ];int n =0 ; - values[n++] =perf_event_count (event); + values[n++] =perf_event_count (event, from_sample);
...observe the fail... from_sample != self-value-only
Quoted 3 lines
kernel/events/core.c +0
-0if (read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) { values[n++] = enabled +atomic64_read (&event->child_total_time_enabled);
Quoted 26 lines · Peter Zijlstra
Quoted 8 lines · Ben Gainey
This change allows events to use
PERF_SAMPLEREAD with inherit so long asPERF_SAMPLE_TIDis also set.In this configuration, an event will be inherited into any child processes / threads, allowing convenient profiling of a multiprocess or multithreaded application, whilst allowing profiling tools to collect per-thread samples, in particular of groups of counters.
Perhaps a few words on *WHY* this is important.
Quoted 6 lines
The
read_formatfield of bothPERF_RECORD_READandPERF_RECORD_SAMPLE
are changed by this new configuration, but calls toread()on the same
event file descriptor are unaffected and continue to return the cumulative total.This is unfortunate. Up to this point they were the same. Also, I see no
change to the uapi file. So were you trying to say that onlyread_format::valueis changed to be the thread local value as opposed to
the hierarchy total?
Quoted 3 lines
Please fix the wording to be unambiguous as to what is actually meant.
Also try and justify why it is okay to break this symmetry.
Yes, the meaning of the PERF_RECORD_SAMPLE's read_format value changes in this specific scenario to be a thread-local value rather than the total.
I'll update and add some justification.
Quoted 17 lines
Quoted 11 lines
(quoted diff) +0
-0@@ -3532,11 +3544,18 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) perf_ctx_disable(ctx, false);
/* PMIs are disabled;ctx->nr_pendingis stable. */
- if (local_read(&ctx->nr_pending)||
+ if (ctx->nr_inherit_read||
+next_ctx->nr_inherit_read||
+local_read(&ctx->nr_pending)||local_read(&next_ctx->nr_pending)) {This seems unfortunate,
nr_pendingandnr_inherit_readare both used exclusively to inhibit this context switch optimization. Surely they can
share the exact same counter.
Quoted 1 line
That is, rename
nr_pendingand use it for both?
Sure, how about "nr_no_switch_fast" ?
Quoted 14 lines
Quoted 14 lines
/*
* Must not swap out ctx when there's pending
* events that rely on thectx->taskrelation.
+ *
+ * Likewise, when a context contains inherit +
+ *SAMPLE_READevents they should be switched
+ * out using the slow path so that they are
+ * treated as if they were distinct contexts.
*/
raw_spin_unlock(&next_ctx->lock);
rcu_read_unlock();kernel/events/core.c +0
-0@@ -4552,11 +4571,19 @@ static void __perf_event_read(void *info) raw_spin_unlock(&ctx->lock);
Quoted 8 lines
Quoted 5 lines
(quoted diff) +0
-0}(quoted diff) +1
-1-static inline u64 perf_event_count(struct perf_event *event) +static inline u64 perf_event_count_cumulative(struct perf_event*event)
I don't think you need this -- overly long and hard to type function name...
Sure, presumably you are happy with just calling "perf_event_count(event, false)" everywhere it is currently used, rather than renaming it to something shorter and keeping the two functions?
Quoted 32 lines
Quoted 11 lines
{
returnlocal64_read(&event->count)+ atomic64_read(&event-Quoted 1 line
child_count);}
+static inline u64perf_event_count(struct perf_event *event, bool self_value_only)
+{
+ if (self_value_only&&has_inherit_and_sample_read(&event->attr))
+ returnlocal64_read(&event->count);... if this @self_value_only argument was actually used as such -- it isn't, see how you use '
from_sample' which is something else entirely.
Which then also caused to you fix it up and make a mess with that &&has_inherit_and_sample_read()nonsense. (also, shorter function names,
more good)kernel/events/core.c static void __perf_event_read(void *info) · line 4585
Quoted 3 lines
kernel/events/core.c +3
-0+ +return perf_event_count_cumulative (event); +}That is, I would really rather you had:
static inline u64
perf_event_count(struct perf_event *event, bool self)
{
if (self)
returnlocal64_read(&event->count);
Quoted 5 lines
return
local64_read(&event->count)+ local64_read(&event-Quoted 1 line
child_count);}
And then actually use that argument as intended.
Fair point.
I was trying to avoid the 3 subsequent uses all having to repeat "from_sample && has_inherit_and_sample_read(&event->attr)", which feels a bit of a pit-trappy.
I suppose I could pull that into a "use_self_value(from_sample,event)"?
Quoted 11 lines
Quoted 11 lines
(quoted diff) +0
-0@@ -7205,13 +7232,14 @@ void perf_event__output_id_sample(struct perf_event *event,
static void perf_output_read_one(structperf_output_handle*handle,
structperf_event*event,
- u64 enabled, u64 running)
+ u64 enabled, u64 running,
+ boolfrom_sample)
{
Quoted 8 lines
Quoted 6 lines
u64
read_format=event->attr.read_format; u64 values[5];
int n = 0;
- values[n++] =perf_event_count(event);
+ values[n++] =perf_event_count(event, from_sample);...observe the fail...
from_sample!= self-value-only
By fail you are referring to the difference in names?
Quoted 5 lines
Quoted 4 lines
if (
read_format&PERF_FORMAT_TOTAL_TIME_ENABLED) { values[n++] = enabled +atomic64_read(&event->child_total_time_enabled);
Thanks
Ben
Peter Zijlstra
maintainerQuoted 16 lines · Ben Gainey
Quoted 16 lines
Quoted 10 lines
(quoted diff) +0
-0@@ -3532,11 +3544,18 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) perf_ctx_disable(ctx, false);
/* PMIs are disabled;ctx->nr_pendingis stable. */
- if (local_read(&ctx->nr_pending)||
+ if (ctx->nr_inherit_read||
+next_ctx->nr_inherit_read||
+local_read(&ctx->nr_pending)||local_read(&next_ctx->nr_pending)) {This seems unfortunate,
nr_pendingandnr_inherit_readare both used exclusively to inhibit this context switch optimization. Surely they can
share the exact same counter.
Quoted 3 lines
Quoted 1 line
That is, rename
nr_pendingand use it for both?Sure, how about "
nr_no_switch_fast" ?
Yeah, I suppose.
Quoted 4 lines
Sure, presumably you are happy with just calling "
perf_event_count(event, false)" everywhere it is currently used, rather than renaming it to something shorter and keeping the two functions?
Yeah, there aren't *that* many instances. Your current patch already touches them all anyway.
Quoted 15 lines
Quoted 8 lines
That is, I would really rather you had:
static inline u64
perf_event_count(struct perf_event *event, bool self)
{
if (self)
returnlocal64_read(&event->count);Quoted 5 lines
return
local64_read(&event->count)+ local64_read(&event-Quoted 1 line
child_count);}
And then actually use that argument as intended.
Quoted 7 lines
Fair point.
I was trying to avoid the 3 subsequent uses all having to repeat "
from_sample&&has_inherit_and_sample_read(&event->attr)", which feels a bit of a pit-trappy.I suppose I could pull that into a "
use_self_value(from_sample,event)"?
IIRC they all originate in a single location around perf_output_read(), that already has the event and could easily 'correct' the semantic meaning by doing the above once or so.
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
(quoted diff) +0
-0@@ -7205,13 +7232,14 @@ void perf_event__output_id_sample(struct perf_event *event,
static void perf_output_read_one(structperf_output_handle*handle,
structperf_event*event,
- u64 enabled, u64 running)
+ u64 enabled, u64 running,
+ boolfrom_sample)
{Quoted 8 lines
Quoted 6 lines
u64
read_format=event->attr.read_format; u64 values[5];
int n = 0;
- values[n++] =perf_event_count(event);
+ values[n++] =perf_event_count(event, from_sample);...observe the fail...
from_sample!= self-value-only
Quoted 1 line
By fail you are referring to the difference in names?
The difference in meaning, one is from-sample, the other is self-value. Per the extra condition squirrelled away they are not equivalent.
Quoted 57 lines · Peter Zijlstra
Quoted 18 lines · Ben Gainey
Quoted 18 lines
Quoted 10 lines
(quoted diff) +0
-0@@ -3532,11 +3544,18 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) � perf_ctx_disable(ctx, false);�
� /* PMIs are disabled;ctx->nr_pendingis stable. */
- if (local_read(&ctx->nr_pending)||
+ if (ctx->nr_inherit_read||
+ ���next_ctx->nr_inherit_read||
+ ���local_read(&ctx->nr_pending)|| � ���local_read(&next_ctx->nr_pending)) {This seems unfortunate,
nr_pendingandnr_inherit_readare both used
exclusively to inhibit this context switch optimization. Surely they
can
share the exact same counter.Quoted 3 lines
Quoted 1 line
That is, rename
nr_pendingand use it for both?Sure, how about "
nr_no_switch_fast" ?Yeah, I suppose.
Quoted 4 lines
Sure, presumably you are happy with just calling "
perf_event_count(event, false)" everywhere it is currently used, rather than renaming it to something shorter and keeping the two functions?Yeah, there aren't *that* many instances. Your current patch already touches them all anyway.
Quoted 22 lines
Quoted 8 lines
That is, I would really rather you had:
static inline u64
perf_event_count(struct perf_event *event, bool self)
{
�if (self)
�returnlocal64_read(&event->count);Quoted 5 lines
�return
local64_read(&event->count)+ local64_read(&event-Quoted 1 line
child_count);}
And then actually use that argument as intended.
Fair point.
I was trying to avoid the 3 subsequent uses all having to repeat "
from_sample&&has_inherit_and_sample_read(&event->attr)", which feels
a bit of a pit-trappy.�
Quoted 8 lines
Quoted 2 lines
I suppose I could pull that into a
"use_self_value(from_sample,event)"?IIRC they all originate in a single location around
perf_output_read(),
that already has the event and could easily 'correct' the semantic meaning by doing the above once or so.
As far as I can tell, you can mix events in a group with inconsistent values of PERF_SAMPLE_READ which means that doing it at the top level introduces an inconsistency/confusing behaviour since it makes the "thread-local-ness" of the read_format values a property of the event that caused the sample, rather than of the specific event to which the value belongs. The current implementation makes it a property of the specific event not the sample event. Specifically, when perf_output_read_group reads a child event that does not have PERF_SAMPLE_READ, the sample event must have PERF_SAMPLE_READ, meaning the child event will give the thread-local value even though it was not created as inherit+PERF_SAMPLE_READ
I can either:
* Keep it so that the perf_output_read_group uses per-event value for
self
* Rework the deliver_sample_value in session.c to base its decision on
the sample event rather than the specific event
* Forbid inconsistent PERF_SAMPLE_READ for events in a group
* Something else?
Quoted 26 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
(quoted diff) +0
-0@@ -7205,13 +7232,14 @@ voidperf_event__output_id_sample(struct
perf_event*event,
�
�static void perf_output_read_one(structperf_output_handle*handle,
� structperf_event*event,
- u64 enabled, u64 running)
+ u64 enabled, u64 running,
+ boolfrom_sample)
�{Quoted 8 lines
Quoted 6 lines
� u64
read_format=event->attr.read_format; � u64 values[5];
� int n = 0;
�
- values[n++] =perf_event_count(event);
+ values[n++] =perf_event_count(event, from_sample);...observe the fail...
from_sample!= self-value-onlyQuoted 1 line
By fail you are referring to the difference in names?
The difference in meaning, one is from-sample, the other is self- value.
Per the extra condition squirrelled away they are not equivalent.
Peter Zijlstra
maintainerQuoted 18 lines · Ben Gainey
Quoted 5 lines
IIRC they all originate in a single location around
perf_output_read(),
that already has the event and could easily 'correct' the semantic meaning by doing the above once or so.As far as I can tell, you can mix events in a group with inconsistent values of
PERF_SAMPLE_READwhich means that doing it at the top level introduces an inconsistency/confusing behaviour since it makes the "thread-local-ness" of theread_formatvalues a property of the event that caused the sample, rather than of the specific event to which the value belongs. The current implementation makes it a property of the specific event not the sample event. Specifically, whenperf_output_read_groupreads a child event that does not havePERF_SAMPLE_READ, the sample event must havePERF_SAMPLE_READ, meaning the child event will give the thread-local value even though it was not created as inherit+PERF_SAMPLE_READ
Quoted 4 lines
I can either:
* Keep it so that the
perf_output_read_groupuses per-event value for
self
Having the lot be inconsistent seems bad.
Quoted 2 lines
* Rework the
deliver_sample_valueinsession.cto base its decision on
the sample event rather than the specific event
The code as it exists seems to use the read_format from the event that actually triggers the sample -- which makes sense. I would expect this new thing to follow that.
Mixing inherit between events in a group gets you crap, but that's what you asked for I suppose :-) But otherwise let the sampling event set the format.
Quoted 1 line
* Forbid inconsistent
PERF_SAMPLE_READfor events in a group
Not possible I think, you can have non-sampling events in a group.
PATCH v7 2/4 tools/perf: Track where perf_sample_ids need per-thread periods
In reply to Ben Gaineyperf_sample_ids and related evlist/evsel code are modified to track which events combine inherit+PERF_SAMPLE_READ+PERF_SAMPLE_TID.
Events with this combination of properties must be handled differently when calculating each sample period. They must use the combination of (ID + TID) to uniquely identify each distinct sequence of values.
--- tools/lib/perf/evlist.c | 1 + tools/lib/perf/evsel.c | 7 +++++++ tools/lib/perf/include/internal/evsel.h | 9 +++++++++ 3 files changed, 17 insertions(+)
tools/lib/perf/evlist.c +1 -0
@@ -255,6 +255,7 @@ static void perf_evlist__id_hash(struct perf_evlist *evlist, sid->id = id; sid->evsel = evsel; + sid->period_per_thread =perf_evsel__attr_has_per_thread_sample_period (evsel); hash =hash_64 (sid->id,PERF_EVLIST__HLIST_BITS );hlist_add_head (&sid->node, &evlist->heads[hash]); }
tools/lib/perf/evsel.c +7 -0
@@ -537,6 +537,13 @@ void perf_evsel__free_id(struct perf_evsel *evsel) evsel->ids =0 ; } +bool perf_evsel__attr_has_per_thread_sample_period (struct perf_evsel *evsel) +{ +return (evsel->attr.sample_type &PERF_SAMPLE_READ ) + && (evsel->attr.sample_type &PERF_SAMPLE_TID ) + && evsel->attr.inherit; +} +void perf_counts_values__scale (struct perf_counts_values *count,bool scale,__s8 *pscaled) {
tools/lib/perf/include/internal/evsel.h +9 -0
@@ -36,6 +36,13 @@ struct perf_sample_id {/* Holds total ID period value for PERF_SAMPLE_READ processing. */ u64 period; + +/* +* When inherit is combined with PERF_SAMPLE_READ, the period value is +* per (id, thread) tuple, rather than per id, so use the stream_id to +* uniquely identify the period, rather than the id. +*/ +bool period_per_thread; };struct perf_evsel {
@@ -88,4 +95,6 @@ int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter);int perf_evsel__alloc_id (struct perf_evsel *evsel,int ncpus,int nthreads);void perf_evsel__free_id (struct perf_evsel *evsel); +bool perf_evsel__attr_has_per_thread_sample_period (struct perf_evsel *evsel); +#endif /* __LIBPERF_INTERNAL_EVSEL_H */
Signature · 1 line
2.45.2
PATCH v7 3/4 tools/perf: Correctly calculate sample period for inherited SAMPLE_READ values
In reply to Ben GaineySample period calculation is updated to take into account the fact that events with inherit+PERF_SAMPLE_READ+PERF_SAMPLE_TID should use the TID in combination with the ID field in the read_format data to identify which value represents the previous accumulated counter total used to calculate the period delta since the last sample.
perf_sample_id is modified to support tracking per-thread values, along with the existing global per-id values. In the per-thread case, values are stored in a hash by TID within the perf_sample_id, and are dynamically allocated as the number is not known ahead of time.
deliver_sample_value is modified to correctly locate the previous sample storage based on the attribute, stream id and thread id.
--- tools/lib/perf/evsel.c | 41 ++++++++++++++++++++++ tools/lib/perf/include/internal/evsel.h | 45 +++++++++++++++++++++++-- tools/perf/util/session.c | 11 ++++-- 3 files changed, 92 insertions(+), 5 deletions(-)
tools/lib/perf/evsel.c +41 -0
@@ -5,6 +5,7 @@#include <perf/evsel.h> #include <perf/cpumap.h> #include <perf/threadmap.h> +#include <linux/hash.h> #include <linux/list.h> #include <internal/evsel.h> #include <linux/zalloc.h>
@@ -23,6 +24,7 @@ void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr, int idx) {INIT_LIST_HEAD (&evsel->node); +INIT_LIST_HEAD (&evsel->per_stream_periods); evsel->attr = *attr; evsel->idx = idx; evsel->leader = evsel;
@@ -531,10 +533,17 @@ int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)void perf_evsel__free_id (struct perf_evsel *evsel) { +struct perf_sample_id_period *pos, *n; +xyarray__delete (evsel->sample_id); evsel->sample_id =NULL ;zfree (&evsel->id); evsel->ids =0 ; + +perf_evsel_for_each_per_thread_period_safe (evsel, n, pos) { +list_del_init (&pos->node); +free (pos); + } }bool perf_evsel__attr_has_per_thread_sample_period (struct perf_evsel *evsel)
@@ -544,6 +553,38 @@ bool perf_evsel__attr_has_per_thread_sample_period(struct perf_evsel *evsel) && evsel->attr.inherit; } +u64 *perf_sample_id__get_period_storage (struct perf_sample_id *sid,u32 tid) +{ +struct hlist_head *head; +struct perf_sample_id_period *res; +int hash; + +if (!sid->period_per_thread) +return &sid->period; + + hash =hash_32 (tid,PERF_SAMPLE_ID__HLIST_BITS ); + head = &sid->periods[hash]; + +hlist_for_each_entry (res, head, hnode) +if (res->tid == tid) +return &res->period; + +if (sid->evsel ==NULL ) +return NULL ; + + res =zalloc (sizeof (struct perf_sample_id_period )); +if (res ==NULL ) +return NULL ; + +INIT_LIST_HEAD (&res->node); + res->tid = tid; + +list_add_tail (&res->node, &sid->evsel->per_stream_periods); +hlist_add_head (&res->hnode, &sid->periods[hash]); + +return &res->period; +} +void perf_counts_values__scale (struct perf_counts_values *count,bool scale,__s8 *pscaled) {
tools/lib/perf/include/internal/evsel.h +43 -2
67 lines not on this page · raw message
tools/perf/util/session.c +8 -3
@@ -1478,14 +1478,19 @@ static int deliver_sample_value(struct evlist *evlist, {struct perf_sample_id *sid =evlist__id2sid (evlist, v->id);struct evsel *evsel; +u64 *storage =NULL ;if (sid) { + storage =perf_sample_id__get_period_storage (sid, sample->tid); + } + +if (storage) { sample->id = v->id; - sample->period = v->value - sid->period; - sid->period = v->value; + sample->period = v->value - *storage; + *storage = v->value; } -if (!sid || sid->evsel ==NULL ) { +if (!storage || sid->evsel ==NULL ) { ++evlist->stats.nr_unknown_id;return 0 ; }
Signature · 1 line
2.45.2
PATCH v7 4/4 tools/perf: Allow inherit + PERF_SAMPLE_READ when opening events
In reply to Ben GaineyThe tool will now default to this new mode if the user specifies a sampling group when not in system-wide mode, and when --no-inherit is not specified.
This change updates evsel to allow the combination of inherit and PERF_SAMPLE_READ.
A fallback is implemented for kernel versions where this feature is not supported.
---
tools/perf/tests/attr/README | 2 +
.../tests/attr/test-record-group-sampling | 3 +-
.../tests/attr/test-record-group-sampling1 | 51 ++++++++++++++++
.../tests/attr/test-record-group-sampling2 | 61 +++++++++++++++++++
tools/perf/tests/attr/test-record-group2 | 1 +
...{test-record-group2 => test-record-group3} | 10 +--
tools/perf/util/evsel.c | 19 +++++-
tools/perf/util/evsel.h | 1 +
8 files changed, 141 insertions(+), 7 deletions(-)
create mode 100644 tools/perf/tests/attr/test-record-group-sampling1
create mode 100644 tools/perf/tests/attr/test-record-group-sampling2
copy tools/perf/tests/attr/{test-record-group2 => test-record-group3} (81%)tools/perf/tests/attr/README +2 -0
@@ -51,6 +51,8 @@ Following tests are defined (with perf commands): perf record --call-graph fp kill (test-record-graph-fp-aarch64) perf record -e '{cycles,instructions}' kill (test-record-group1) perf record -e '{cycles/period=1/,instructions/period=2/}:S' kill (test-record-group2) + perf record -e '{cycles,cache-misses}:S' kill (test-record-group-sampling1) + perf record -c 10000 -e '{cycles,cache-misses}:S' kill (test-record-group-sampling2) perf record -D kill (test-record-no-delay) perf record -i kill (test-record-no-inherit) perf record -n kill (test-record-no-samples)
tools/perf/tests/attr/test-record-group-sampling +2 -1
@@ -2,6 +2,7 @@ command = record args = --no-bpf-event -e '{cycles,cache-misses}:S' kill >/dev/null 2>&1 ret = 1 +kernel_until = 6.10 [event-1:base-record] fd=1
@@ -18,7 +19,7 @@ group_fd=1 type=0 config=3 -# default | PERF_SAMPLE_READ +# default | PERF_SAMPLE_READ | PERF_SAMPLE_PERIOD sample_type=343 # PERF_FORMAT_ID | PERF_FORMAT_GROUP | PERF_FORMAT_LOST
tools/perf/tests/attr/test-record-group-sampling1 new file +51 -0
51 lines not on this page · raw message
tools/perf/tests/attr/test-record-group-sampling2 new file +61 -0
61 lines not on this page · raw message
tools/perf/tests/attr/test-record-group2 +1 -0
@@ -2,6 +2,7 @@ command = record args = --no-bpf-event -e '{cycles/period=1234000/,instructions/period=6789000/}:S' kill >/dev/null 2>&1 ret = 1 +kernel_until = 6.10 [event-1:base-record] fd=1
tools/perf/tests/attr/test-record-group3 renamed from tools/perf/tests/attr/test-record-group2 +6 -4
@@ -2,6 +2,7 @@ command = record args = --no-bpf-event -e '{cycles/period=1234000/,instructions/period=6789000/}:S' kill >/dev/null 2>&1 ret = 1 +kernel_since = 6.10 [event-1:base-record] fd=1
@@ -9,8 +10,9 @@ group_fd=-1 config=0|1 sample_period=1234000 sample_type=87 -read_format=12|28 -inherit=0 +read_format=28|31 +disabled=1 +inherit=1 freq=0 [event-2:base-record]
@@ -19,9 +21,9 @@ group_fd=1 config=0|1 sample_period=6789000 sample_type=87 -read_format=12|28 +read_format=28|31 disabled=0 -inherit=0 +inherit=1 mmap=0 comm=0 freq=0
tools/perf/util/evsel.c +17 -2
@@ -1162,7 +1162,15 @@ void evsel__config(struct evsel *evsel, struct record_opts *opts,*/ if (leader->core.nr_members >1 ) { attr->read_format |=PERF_FORMAT_GROUP ; - attr->inherit =0 ; + } + +/* +* Inherit + SAMPLE_READ requires SAMPLE_TID in the read_format +*/ +if (attr->inherit) { +evsel__set_sample_bit (evsel,TID ); + evsel->core.attr.read_format |= +PERF_FORMAT_ID ; } }
@@ -1838,6 +1846,8 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,static void evsel__disable_missing_features (struct evsel *evsel) { +if (perf_missing_features.inherit_sample_read) + evsel->core.attr.inherit =0 ;if (perf_missing_features.branch_counters) evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_COUNTERS ;if (perf_missing_features.read_lost)
@@ -1893,7 +1903,12 @@ bool evsel__detect_missing_features(struct evsel *evsel)* Must probe features in the order they were added to the * perf_event_attr interface. */ -if (!perf_missing_features.branch_counters && +if (!perf_missing_features.inherit_sample_read && + evsel->core.attr.inherit && (evsel->core.attr.sample_type &PERF_SAMPLE_READ )) { + perf_missing_features.inherit_sample_read = true; +pr_debug2 ("Using PERF_SAMPLE_READ / :S modifier is not compatible with inherit, falling back to no-inherit.\n" ); +return true; + }else if (!perf_missing_features.branch_counters && (evsel->core.attr.branch_sample_type &PERF_SAMPLE_BRANCH_COUNTERS )) { perf_missing_features.branch_counters = true;pr_debug2 ("switching off branch counters support\n" );
tools/perf/util/evsel.h +1 -0
@@ -192,6 +192,7 @@ struct perf_missing_features {bool weight_struct;bool read_lost;bool branch_counters; +bool inherit_sample_read; };extern struct perf_missing_features perf_missing_features;
Signature · 1 line
2.45.2