perf: Support PERF_SAMPLE_READ with inherit
v9 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, or profile a single thread. 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 sample values, as may appear in the read_format field of a PERF_RECORD_SAMPLE, are no longer global counters. Instead the value reports the per-thread value for the active thread. Tools that expect the global total, for example when calculate a delta between samples, would need updating to take this into account when opting into this new behaviour. Previously valid event configurations (system-wide, no-inherit and so on) are unaffected.
Changes since v8:
- Rebase on v6.11-rc1
Changes since v7:
- Rebase on v6.10-rc3
- Respond to Peter Zijlstra's feedback:
- Renamed nr_pending to nr_no_switch_fast and merged in nr_inherit_read which otherwise had overlapping use
- Updated some of the commit messages to provide better justifications of usecase, behavioural changes and so on
- Cleanup perf_event_count/_cumulative
- Make it explicit that the sampling event decides whether or not the per-thread value is given in read_format for PERF_RECORD_SAMPLE and PERF_RECORD_READ; updated tools to account for this.
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: Rename perf_event_context.nr_pending to nr_no_switch_fast.
perf: Support PERF_SAMPLE_READ with inherit 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 | 8 ++-
kernel/events/core.c | 67 +++++++++++++------
tools/lib/perf/evsel.c | 48 +++++++++++++
tools/lib/perf/include/internal/evsel.h | 63 ++++++++++++++++-
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 | 25 ++++---
13 files changed, 320 insertions(+), 39 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 v9 1/4 perf: Rename perf_event_context.nr_pending to nr_no_switch_fast.
nr_pending counts the number of events in the context that either pending_sigtrap or pending_work, but it is used to prevent taking the fast path in perf_event_context_sched_out.
Renamed to reflect what it is used for, rather than what it counts. This change allows using the field to track other event properties that also require skipping the fast path without possible confusion over the name.
--- include/linux/perf_event.h | 5 +++-- kernel/events/core.c | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-)
include/linux/perf_event.h +3 -2
@@ -963,12 +963,13 @@ struct perf_event_context {struct rcu_head rcu_head;/* -* Sum (event->pending_work + event->pending_work) +* The count of events for which using the switch-out fast path +* should be avoided. * * The SIGTRAP is targeted at ctx->task, as such it won't do changing * that until the signal is delivered. */ -local_t nr_pending; +local_t nr_no_switch_fast; };struct perf_cpu_pmu_context {
kernel/events/core.c +6 -6
@@ -3516,9 +3516,9 @@ 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) || -local_read (&next_ctx->nr_pending)) { +/* PMIs are disabled; ctx->nr_no_switch_fast is stable. */ +if (local_read (&ctx->nr_no_switch_fast) || +local_read (&next_ctx->nr_no_switch_fast)) {/* * Must not swap out ctx when there's pending * events that rely on the ctx->task relation.
@@ -5204,7 +5204,7 @@ static void perf_pending_task_sync(struct perf_event *event)*/ if (task_work_cancel (current, head)) { event->pending_work =0 ; -local_dec (&event->ctx->nr_pending); +local_dec (&event->ctx->nr_no_switch_fast);return ; }
@@ -6868,7 +6868,7 @@ static void perf_pending_task(struct callback_head *head)if (event->pending_work) { event->pending_work =0 ;perf_sigtrap (event); -local_dec (&event->ctx->nr_pending); +local_dec (&event->ctx->nr_no_switch_fast);rcuwait_wake_up (&event->pending_work_wait); }rcu_read_unlock ();
@@ -9740,7 +9740,7 @@ static int __perf_event_overflow(struct perf_event *event,if (!event->pending_work && !task_work_add (current, &event->pending_task, notify_mode)) { event->pending_work = pending_id; -local_inc (&event->ctx->nr_pending); +local_inc (&event->ctx->nr_no_switch_fast); event->pending_addr =0 ;if (valid_sample && (data->sample_flags &PERF_SAMPLE_ADDR ))
Signature · 1 line
2.45.2
[tip: perf/core] perf: Rename perf_event_context.nr_pending to nr_no_switch_fast.
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 79bd233010859463e46a0a4b3926eaaba25a6110 Gitweb: https://git.kernel.org/tip/79bd233010859463e46a0a4b3926eaaba25a6110 Author: Ben Gainey <[email protected]> AuthorDate: Tue, 30 Jul 2024 09:44:14 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 02 Aug 2024 11:30:29 +02:00
perf: Rename perf_event_context.nr_pending to nr_no_switch_fast.
nr_pending counts the number of events in the context that either pending_sigtrap or pending_work, but it is used to prevent taking the fast path in perf_event_context_sched_out.
Renamed to reflect what it is used for, rather than what it counts. This change allows using the field to track other event properties that also require skipping the fast path without possible confusion over the name.
--- include/linux/perf_event.h | 5 +++-- kernel/events/core.c | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-)
include/linux/perf_event.h +3 -2
@@ -966,12 +966,13 @@ struct perf_event_context {struct rcu_head rcu_head;/* -* Sum (event->pending_work + event->pending_work) +* The count of events for which using the switch-out fast path +* should be avoided. * * The SIGTRAP is targeted at ctx->task, as such it won't do changing * that until the signal is delivered. */ -local_t nr_pending; +local_t nr_no_switch_fast; };struct perf_cpu_pmu_context {
kernel/events/core.c +6 -6
@@ -3516,9 +3516,9 @@ 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) || -local_read (&next_ctx->nr_pending)) { +/* PMIs are disabled; ctx->nr_no_switch_fast is stable. */ +if (local_read (&ctx->nr_no_switch_fast) || +local_read (&next_ctx->nr_no_switch_fast)) {/* * Must not swap out ctx when there's pending * events that rely on the ctx->task relation.
@@ -5204,7 +5204,7 @@ static void perf_pending_task_sync(struct perf_event *event)*/ if (task_work_cancel (current, head)) { event->pending_work =0 ; -local_dec (&event->ctx->nr_pending); +local_dec (&event->ctx->nr_no_switch_fast);return ; }
@@ -6868,7 +6868,7 @@ static void perf_pending_task(struct callback_head *head)if (event->pending_work) { event->pending_work =0 ;perf_sigtrap (event); -local_dec (&event->ctx->nr_pending); +local_dec (&event->ctx->nr_no_switch_fast);rcuwait_wake_up (&event->pending_work_wait); }rcu_read_unlock ();
@@ -9740,7 +9740,7 @@ static int __perf_event_overflow(struct perf_event *event,if (!event->pending_work && !task_work_add (current, &event->pending_task, notify_mode)) { event->pending_work = pending_id; -local_inc (&event->ctx->nr_pending); +local_inc (&event->ctx->nr_no_switch_fast); event->pending_addr =0 ;if (valid_sample && (data->sample_flags &PERF_SAMPLE_ADDR ))
PATCH v9 2/4 perf: Support PERF_SAMPLE_READ with inherit
In reply to Ben GaineyThis change allows events to use PERF_SAMPLE_READ with inherit so long as PERF_SAMPLE_TID is also set. This enables sample based profiling of a group of counters over a hierarchy of processes or threads. This is useful, for example, for collecting per-thread counters/metrics, event based sampling of multiple counters as a unit, access to the enabled and running time when using multiplexing and so on.
Prior to this, users were restricted to either collecting aggregate statistics for a multi-threaded/-process application (e.g. with "perf stat"), or to sample individual threads, or to profile the entire system (which requires root or CAP_PERFMON, and may produce much more data than is required). Theoretically a tool could poll for or otherwise monitor thread/process creation and construct whatever events the user is interested in using perf_event_open, for each new thread or process, but this is racy, can lead to file-descriptor exhaustion, and ultimately just replicates the behaviour of inherit, but in userspace.
This configuration differs from inherit without PERF_SAMPLE_READ in that the accumulated event count, and consequently any sample (such as if triggered by overflow of sample_period) will be on a per-thread rather than on an aggregate basis.
The meaning of read_format::value field of both PERF_RECORD_READ and PERF_RECORD_SAMPLE is changed such that if the sampled event uses this new configuration then the values reported will be per-thread rather than the global aggregate value. This is a change from the existing semantics of read_format (where PERF_SAMPLE_READ is used without inherit), but it is necessary to expose the per-thread counter values, and it avoids reinventing a separate "read_format_thread" field that otherwise replicates the same behaviour. This change should not break existing tools, since this configuration was not previously valid and was rejected by the kernel. Tools that opt into this new mode will need to account for this when calculating the counter delta for a given sample. Tools that wish to have both the per-thread and aggregate value can perform the global aggregation themselves from the per-thread values.
The change to read_format::value does not affect existing valid perf_event_attr configurations, nor does it change the behaviour of calls to "read" on an event descriptor. Both continue to report the aggregate value for the entire thread/process hierarchy. The difference between the results reported by "read" and PERF_RECORD_SAMPLE in this new configuration is justified on the basis that it is not (easily) possible for "read" to target a specific thread (the caller only has the fd for the original parent event).
--- include/linux/perf_event.h | 3 +++ kernel/events/core.c | 55 ++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 14 deletions(-)
include/linux/perf_event.h +3 -0
@@ -966,6 +966,9 @@ struct perf_event_context {* The count of events for which using the switch-out fast path * should be avoided. * +* Sum (event->pending_work + events with +* (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))) +* * The SIGTRAP is targeted at ctx->task, as such it won't do changing * that until the signal is delivered. */
kernel/events/core.c +41 -14
166 lines not on this page · raw message
Signature · 1 line
2.45.2
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 7e8b255650fcfa1d05a57e4093d8405e6d8dd488 Gitweb: https://git.kernel.org/tip/7e8b255650fcfa1d05a57e4093d8405e6d8dd488 Author: Ben Gainey <[email protected]> AuthorDate: Tue, 30 Jul 2024 09:44:15 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 02 Aug 2024 11:30:30 +02:00
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. This enables sample based profiling of a group of counters over a hierarchy of processes or threads. This is useful, for example, for collecting per-thread counters/metrics, event based sampling of multiple counters as a unit, access to the enabled and running time when using multiplexing and so on.
Prior to this, users were restricted to either collecting aggregate statistics for a multi-threaded/-process application (e.g. with "perf stat"), or to sample individual threads, or to profile the entire system (which requires root or CAP_PERFMON, and may produce much more data than is required). Theoretically a tool could poll for or otherwise monitor thread/process creation and construct whatever events the user is interested in using perf_event_open, for each new thread or process, but this is racy, can lead to file-descriptor exhaustion, and ultimately just replicates the behaviour of inherit, but in userspace.
This configuration differs from inherit without PERF_SAMPLE_READ in that the accumulated event count, and consequently any sample (such as if triggered by overflow of sample_period) will be on a per-thread rather than on an aggregate basis.
The meaning of read_format::value field of both PERF_RECORD_READ and PERF_RECORD_SAMPLE is changed such that if the sampled event uses this new configuration then the values reported will be per-thread rather than the global aggregate value. This is a change from the existing semantics of read_format (where PERF_SAMPLE_READ is used without inherit), but it is necessary to expose the per-thread counter values, and it avoids reinventing a separate "read_format_thread" field that otherwise replicates the same behaviour. This change should not break existing tools, since this configuration was not previously valid and was rejected by the kernel. Tools that opt into this new mode will need to account for this when calculating the counter delta for a given sample. Tools that wish to have both the per-thread and aggregate value can perform the global aggregation themselves from the per-thread values.
The change to read_format::value does not affect existing valid perf_event_attr configurations, nor does it change the behaviour of calls to "read" on an event descriptor. Both continue to report the aggregate value for the entire thread/process hierarchy. The difference between the results reported by "read" and PERF_RECORD_SAMPLE in this new configuration is justified on the basis that it is not (easily) possible for "read" to target a specific thread (the caller only has the fd for the original parent event).
--- include/linux/perf_event.h | 3 ++- kernel/events/core.c | 55 +++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 14 deletions(-)
include/linux/perf_event.h +3 -0
@@ -969,6 +969,9 @@ struct perf_event_context {* The count of events for which using the switch-out fast path * should be avoided. * +* Sum (event->pending_work + events with +* (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))) +* * The SIGTRAP is targeted at ctx->task, as such it won't do changing * that until the signal is delivered. */
kernel/events/core.c +41 -14
166 lines not on this page · raw message
PATCH v9 3/4 tools/perf: Correctly calculate sample period for inherited SAMPLE_READ values
In reply to Ben GaineySample period calculation in deliver_sample_value is updated to calculate the per-thread period delta for events that are inherit + PERF_SAMPLE_READ. When the sampling event has this configuration, the read_format.id is used with the tid from the sample to lookup the storage of the previously accumulated counter total before calculating the delta. All existing valid configurations where read_format.value represents some global value continue to use just the read_format.id to locate the storage of the previously accumulated total.
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.
--- tools/lib/perf/evsel.c | 48 +++++++++++++++++++ tools/lib/perf/include/internal/evsel.h | 63 ++++++++++++++++++++++++- tools/perf/util/session.c | 25 ++++++---- 3 files changed, 126 insertions(+), 10 deletions(-)
tools/lib/perf/evsel.c +48 -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,56 @@ 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) +{ +return (evsel->attr.sample_type &PERF_SAMPLE_READ ) + && (evsel->attr.sample_type &PERF_SAMPLE_TID ) + && evsel->attr.inherit; +} + +u64 *perf_sample_id__get_period_storage (struct perf_sample_id *sid,u32 tid,bool per_thread) +{ +struct hlist_head *head; +struct perf_sample_id_period *res; +int hash; + +if (!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,
tools/lib/perf/include/internal/evsel.h +61 -2
85 lines not on this page · raw message
tools/perf/util/session.c +17 -8
@@ -1474,18 +1474,24 @@ static int deliver_sample_value(struct evlist *evlist,union perf_event *event, struct perf_sample *sample, struct sample_read_value *v, - struct machine *machine) + struct machine *machine, + bool per_thread) { 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, per_thread); + } + +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 ; }
@@ -1506,14 +1512,15 @@ static int deliver_sample_group(struct evlist *evlist,union perf_event *event, struct perf_sample *sample, struct machine *machine, - u64 read_format) + u64 read_format, + bool per_thread) { int ret = -EINVAL;struct sample_read_value *v = sample->read.group.values;sample_read_group__for_each (v, sample->read.group.nr, read_format) { ret =deliver_sample_value (evlist, tool, event, sample, v, - machine); + machine, per_thread);if (ret)break ; }
@@ -1528,6 +1535,7 @@ static int evlist__deliver_sample(struct evlist *evlist, struct perf_tool *tool,/* We know evsel != NULL. */ u64 sample_type = evsel->core.attr.sample_type;u64 read_format = evsel->core.attr.read_format; +bool per_thread =perf_evsel__attr_has_per_thread_sample_period (&evsel->core);/* Standard sample delivery. */ if (!(sample_type &PERF_SAMPLE_READ ))
@@ -1536,10 +1544,11 @@ static int evlist__deliver_sample(struct evlist *evlist, struct perf_tool *tool,/* For PERF_SAMPLE_READ we have either single or group mode. */ if (read_format &PERF_FORMAT_GROUP )return deliver_sample_group (evlist, tool, event, sample, - machine, read_format); + machine, read_format, per_thread);else return deliver_sample_value (evlist, tool, event, sample, - &sample->read.one, machine); + &sample->read.one, machine, + per_thread); } static int machines__deliver_event(struct machines *machines,
Signature · 1 line
2.45.2
PATCH v9 4/4 tools/perf: Allow inherit + PERF_SAMPLE_READ when opening events
In reply to Ben GaineyThe "perf record" 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.11 [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.11 [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.11 [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
@@ -1171,7 +1171,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 ; } }
@@ -2020,6 +2028,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)
@@ -2075,7 +2085,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
@@ -206,6 +206,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
Namhyung Kim
maintainerRe: [PATCH v9 4/4] tools/perf: Allow inherit + PERF_SAMPLE_READ when opening events
Hello,
Quoted 43 lines · Ben Gainey
The "perf record" 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.
Signed-off-by Ben Gainey <[email protected]>--- 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 +0
-0
tools/perf/tests/attr/test-record-group-sampling line 5
Quoted 4 lines
tools/perf/tests/attr/test-record-group-sampling +1
-0@@ -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.11
I guess it's v6.12. :)
Quoted 196 lines
tools/perf/tests/attr/test-record-group-sampling +1
-1[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_LOSTtools/perf/tests/attr/test-record-group-sampling1 new file +51
-0@@ -0,0 +1,51 @@ +[config] +command = record +args = --no-bpf-event -e '{cycles,cache-misses}:S' kill >/dev/null 2>&1 +ret = 1 +kernel_since = 6.11 + +[event-1:base-record] +fd=1 +group_fd=-1 + +# cycles +type=0 +config=0 + +# default | PERF_SAMPLE_READ | PERF_SAMPLE_PERIOD +sample_type=343 + +# PERF_FORMAT_ID | PERF_FORMAT_GROUP | PERF_FORMAT_LOST | PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING +read_format=28|31 +task=1 +mmap=1 +comm=1 +enable_on_exec=1 +disabled=1 + +# inherit is enabled for group sampling +inherit=1 + +[event-2:base-record] +fd=2 +group_fd=1 + +# cache-misses +type=0 +config=3 + +# default | PERF_SAMPLE_READ | PERF_SAMPLE_PERIOD +sample_type=343 + +# PERF_FORMAT_ID | PERF_FORMAT_GROUP | PERF_FORMAT_LOST | PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING +read_format=28|31 +task=0 +mmap=0 +comm=0 +enable_on_exec=0 +disabled=0 +freq=0 + +# inherit is enabled for group sampling +inherit=1 +tools/perf/tests/attr/test-record-group-sampling2 new file +61
-0@@ -0,0 +1,61 @@ +[config] +command = record +args = --no-bpf-event -c 10000 -e '{cycles,cache-misses}:S' kill >/dev/null 2>&1 +ret = 1 +kernel_since = 6.11 + +[event-1:base-record] +fd=1 +group_fd=-1 + +# cycles +type=0 +config=0 + +# default | PERF_SAMPLE_READ +sample_type=87 + +# PERF_FORMAT_ID | PERF_FORMAT_GROUP | PERF_FORMAT_LOST | PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING +read_format=28|31 +task=1 +mmap=1 +comm=1 +enable_on_exec=1 +disabled=1 + +# inherit is enabled for group sampling +inherit=1 + +# sampling disabled +sample_freq=0 +sample_period=10000 +freq=0 +write_backward=0 + +[event-2:base-record] +fd=2 +group_fd=1 + +# cache-misses +type=0 +config=3 + +# default | PERF_SAMPLE_READ +sample_type=87 + +# PERF_FORMAT_ID | PERF_FORMAT_GROUP | PERF_FORMAT_LOST | PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING +read_format=28|31 +task=0 +mmap=0 +comm=0 +enable_on_exec=0 +disabled=0 + +# inherit is enabled for group sampling +inherit=1 + +# sampling disabled +sample_freq=0 +sample_period=0 +freq=0 +write_backward=0tools/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.11 [event-1:base-record] fd=1tools/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.11 [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=0tools/perf/util/evsel.c +0
-1@@ -1171,7 +1171,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 ;
tools/perf/util/evsel.c void evsel__config(struct evsel *evsel, struct record_opts *opts, · line 1183
Quoted 10 lines
tools/perf/util/evsel.c +9
-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 ; }
Also I think we should reset the inherit bit for system-wide events.
$ perf record -a --synth=no true [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.042 MB perf.data (51 samples) ]
$ perf evlist -v | tr ',' '\n' | grep inherit inherit: 1 inherit: 1
Maybe something like this:
tools/perf/util/evsel.c +1 -1
@@ -1149,7 +1149,7 @@ void evsel__config(struct evsel *evsel, struct record_opts *opts,bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread; attr->sample_id_all = perf_missing_features.sample_id_all ?0 :1 ; - attr->inherit = !opts->no_inherit; + attr->inherit =target__has_cpu (&opts->target) ?0 : !opts->no_inherit; attr->write_backward = opts->overwrite ?1 :0 ; attr->read_format =PERF_FORMAT_LOST ;
Thanks,
Namhyung
Quoted 40 lines
tools/perf/util/evsel.c +8
-1}@@ -2020,6 +2028,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)@@ -2075,7 +2085,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@@ -206,6 +206,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
Re: [PATCH v9 4/4] tools/perf: Allow inherit + PERF_SAMPLE_READ when opening events
Quoted 18 lines · Namhyung Kim
Hello,
Quoted 15 lines · Ben Gainey
The "perf record" 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.Signed-off-by Ben Gainey <[email protected]>---
snip
Quoted 51 lines
Quoted 10 lines
tools/perf/util/evsel.c +0
-1@@ -1171,7 +1171,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 ;tools/perf/util/evsel.c void evsel__config(struct evsel *evsel, · line 1184
Quoted 10 lines
tools/perf/util/evsel.c +9
-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 ; }Also I think we should reset the inherit bit for system-wide events.
$ perf record -a --synth=no true [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.042 MB perf.data (51 samples) ] $ perf evlist -v | tr ',' '\n' | grep inherit inherit: 1 inherit: 1Maybe something like this:
tools/perf/util/evsel.c +0
-0@@ -1149,7 +1149,7 @@ void evsel__config(struct evsel *evsel, struct record_opts *opts,bool
per_cpu=opts->target.default_per_cpu&& !opts-tools/perf/util/evsel.c void evsel__config(struct evsel *evsel, · line 1185
Quoted 1 line
tools/perf/util/evsel.c +0
-0target.per_thread;
attr->sample_id_all= perf_missing_features.sample_id_all ? 0 : 1;
- attr->inherit = !opts->no_inherit;
+ attr->inherit =target__has_cpu(&opts->target)? 0 :
!opts->no_inherit;
attr->write_backward=opts->overwrite? 1 : 0; attr->read_format =PERF_FORMAT_LOST;
Quoted 3 lines
Thanks,
Namhyung
Done, though this looks benign; the current tools appear to do this, and there are a couple of files in tests/attr that assumed inherit=1 for system wide mode that I've had to update.
# perf --version
perf version 6.10-1
# perf record -vvv -a --synth=no true
...
------------------------------------------------------------
perf_event_attr:
type 0 (PERF_TYPE_HARDWARE)
size 136
config 0 (PERF_COUNT_HW_CPU_CYCLES)
{ sample_period, sample_freq } 4000
sample_type IP|TID|TIME|CPU|PERIOD|IDENTIFIER
read_format ID|LOST
disabled 1
inherit 1
freq 1
precise_ip 3
sample_id_all 1
------------------------------------------------------------Testing your fix with -a, -C <n> as well as with application profiling appears to do the right thing.
Regards
Ben
Quoted 47 lines
Quoted 47 lines
}
tools/perf/util/evsel.c +0
-0@@ -2020,6 +2028,8 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,
static voidevsel__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)tools/perf/util/evsel.c +6
-1@@ -2075,7 +2085,12 @@ bool evsel__detect_missing_features(structevsel *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@@ -206,6 +206,7 @@ struct perf_missing_features { bool weight_struct; bool read_lost; bool branch_counters; +bool inherit_sample_read; };
extern structperf_missing_featuresperf_missing_features;Signature · 1 line
2.45.2
Peter Zijlstra
maintainerQuoted 24 lines · Ben Gainey
This change allows events to use
PERF_SAMPLEREAD with inherit so long asPERF_SAMPLE_TIDis also set.Currently it is not possible to use
PERF_SAMPLE_READwith inherit. This restriction assumes the user is interested in collecting aggregate statistics as perperf stat. It prevents a user from collecting per-thread samples using counter groups from a multi-threaded or multi-process application, as withperf record -e '{....}:S'. Instead users must use system-wide mode, or forgo the ability to sample counter groups, or profile a single thread. System-wide mode is often problematic as it requires specific permissions (noCAP_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_allocrelaxing the restriction against combininginheritwithPERF_SAMPLE_READso that the combination will be allowed so long asPERF_SAMPLE_TIDis 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.
Quoted 8 lines
In this configuration sample values, as may appear in the
read_formatfield of aPERF_RECORD_SAMPLE, are no longer global counters. Instead the value reports the per-thread value for the active thread. Tools that expect the global total, for example when calculate a delta between samples, would need updating to take this into account when opting into this new behaviour. Previously valid event configurations (system-wide, no-inherit and so on) are unaffected.
Thanks, I've picked up the kernel patches, and provided the robot doesn't hate on them, they will appear in tip/perf/core soonish.