Skip to contentSkip to conversation

perf: Support PERF_SAMPLE_READ with inherit_stat

PATCH Superseded Ben Gainey 2 participants 7 messages Feb 8, 2024 – Mar 11, 2024 perf events

v2 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Ben Gainey

author
#0 lore raw

v2 cover perf: Support PERF_SAMPLE_READ with inherit_stat

This change allows events to use PERF_SAMPLE READ with inherit so long as both inherit_stat and PERF_SAMPLE_TID are 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.

Perf already supports the ability to collect per-thread counts with inherit via the inherit_stat flag. This patch changes perf_event_alloc relaxing the restriction to combine inherit with PERF_SAMPLE_READ so that the combination will be allowed so long as inherit_stat and PERF_SAMPLE_TID are enabled.

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 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_stat 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 + inherit_stat + PERF_SAMPLE_READ when
opening events

 include/linux/perf_event.h              |  1 +
 kernel/events/core.c                    | 53 +++++++++++++++++--------
 tools/lib/perf/evlist.c                 |  1 +
 tools/lib/perf/evsel.c                  | 48 ++++++++++++++++++++++
 tools/lib/perf/include/internal/evsel.h | 48 +++++++++++++++++++++-
 tools/perf/util/evsel.c                 | 15 ++++++-
 tools/perf/util/evsel.h                 |  1 +
 tools/perf/util/session.c               | 11 +++--
 8 files changed, 154 insertions(+), 24 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/4 perf: Support PERF_SAMPLE_READ with inherit_stat

This change allows events to use PERF_SAMPLE READ with inherit so long as both inherit_stat and PERF_SAMPLE_TID are set.

In this configuration, and 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.

---
 include/linux/perf_event.h |  1 +
 kernel/events/core.c       | 53 ++++++++++++++++++++++++++------------
 2 files changed, 37 insertions(+), 17 deletions(-)
include/linux/perf_event.h +1 -0
index d2a15c0c6f8a..7d405dff6694 100644
@@ -932,6 +932,7 @@ struct perf_event_context {
 
 	int				nr_task_data;
 	int				nr_stat;
+	int				nr_stat_read;
 	int				nr_freq;
 	int				rotate_disable;
 
kernel/events/core.c +36 -17

158 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw
Quoted 2 lines · Ben Gainey

This change allows events to use PERF_SAMPLE READ with inherit so long as both inherit_stat and PERF_SAMPLE_TID are set.

I saw there was a discussion on V1 about adding a new flag because this is an ABI break. Personally I'm not sure if it is required given that it wasn't allowed before, so there wouldn't be any users to experience it. I suppose there _could_ be a new user if they stumbled across this now, but it's not like they would see that as a regression because it wasn't allowed before anyway. Maybe it's cleaner to just use the existing flags rather than adding a new one.

Perf even guarded the condition in userspace as far as I can see, so nobody using Perf would hit this either.

Quoted 2 lines

In this configuration, and event will be inherited into any

and -> any/an?

Quoted 41 lines

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.

---
 include/linux/perf_event.h |  1 +
 kernel/events/core.c       | 53 ++++++++++++++++++++++++++------------
 2 files changed, 37 insertions(+), 17 deletions(-)
include/linux/perf_event.h +1 -0
index d2a15c0c6f8a..7d405dff6694 100644
@@ -932,6 +932,7 @@ struct perf_event_context {
 
 	int				nr_task_data;
 	int				nr_stat;
+	int				nr_stat_read;
 	int				nr_freq;
 	int				rotate_disable;
 
kernel/events/core.c +4 -1
index f0f0f71213a1..dac7093b3608 100644
@@ -1795,8 +1795,11 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx)
 	ctx->nr_events++;
 	if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT)
 		ctx->nr_user++;
-	if (event->attr.inherit_stat)
+	if (event->attr.inherit_stat) {
 		ctx->nr_stat++;
+		if (event->attr.inherit && (event->attr.sample_type & PERF_SAMPLE_READ))
+			ctx->nr_stat_read++;
+	}
 
 	if (event->state > PERF_EVENT_STATE_OFF)
 		perf_cgroup_event_enable(event, ctx);

kernel/events/core.c list_del_event(struct perf_event *event, struct perf_event_context *ctx) · line 2027

Quoted 7 lines
kernel/events/core.c +2 -1
@@ -2019,8 +2022,11 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx)
 	ctx->nr_events--;
 	if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT)
 		ctx->nr_user--;
-	if (event->attr.inherit_stat)
+	if (event->attr.inherit_stat) {
 		ctx->nr_stat--;
+		if (event->attr.inherit && (event->attr.sample_type & PERF_SAMPLE_READ))

This condition is repeated a few times, maybe we could add a macro like "has_sample_read_thread()" or something or whatever we're calling it exactly.

It might have prevented the mistake in copying the condition below in perf_event_count()...

Quoted 33 lines
kernel/events/core.c +10 -1
+			ctx->nr_stat_read--;
+	}
 
 	list_del_rcu(&event->event_entry);
 
@@ -3529,11 +3535,17 @@ 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_stat_read ||
+			    next_ctx->nr_stat_read ||
+			    local_read(&ctx->nr_pending) ||
 			    local_read(&next_ctx->nr_pending)) {
 				/*
 				 * Must not swap out ctx when there's pending
 				 * events that rely on the ctx->task relation.
+				 *
+				 * Likewise, when a context contains inherit+inherit_stat+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();
@@ -3545,6 +3557,7 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)
 
 			perf_ctx_sched_task_cb(ctx, false);
 			perf_event_swap_task_ctx_data(ctx, next_ctx);
+			perf_event_sync_stat(ctx, next_ctx);
 
 			perf_ctx_enable(ctx, false);
 

kernel/events/core.c perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) · line 3563

Quoted 5 lines
kernel/events/core.c +0 -2
@@ -3559,8 +3572,6 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next)
 			RCU_INIT_POINTER(next->perf_event_ctxp, ctx);
 
 			do_switch = 0;
-
-			perf_event_sync_stat(ctx, next_ctx);

The commit message gives a very high level summary of the user visible changes, but it doesn't say what changes have been made to the code to accomplish it.

I wasn't sure what this move of perf_even_sync_stat() is for, because it's actually skipped over when nr_stat_read != 0, which is in this new case?

kernel/events/core.c static void __perf_event_read(void *info) · line 4547

Quoted 9 lines
kernel/events/core.c +1 -1
 		}
 		raw_spin_unlock(&next_ctx->lock);
 		raw_spin_unlock(&ctx->lock);
@@ -4533,8 +4544,13 @@ 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(struct perf_event *event, bool from_sample)

There are only two trues, and 7 existing falses, so you could just add a new function like perf_event_count_sample(). But I suppose that's a style thing.

kernel/events/core.c static void __perf_event_read(void *info) · line 4551

Quoted 4 lines
kernel/events/core.c +3 -0
 {
+	if (from_sample && event->attr.inherit &&
+       event->attr.inherit &&
+       (event->attr.sample_type & PERF_SAMPLE_TID)) {

There's something suspicious about this condition with the event->attr.inherit twice. Should it be && inherit_stat? I don't know if there's any test that could have caught this, if it's affecting the existing inherit but not inherit_stat case?

And it's also probably not very helpful at this stage, but if you run checkpatch.pl on your patches it will point out some of the style issues.

author
#3 lore raw

PATCH v2 2/4 tools/perf: Track where perf_sample_ids need per-thread periods

In reply to Ben Gainey

When PERF_SAMPLE_READ is used with inherit+inherit_stat the perf_sample_id is no longer globally unique, but instead is unique per each inherited thread.

Track this fact in perf_sample_ids as it will be needed to correctly calculate the period.

---
 tools/lib/perf/evlist.c                 | 1 +
 tools/lib/perf/evsel.c                  | 7 +++++++
 tools/lib/perf/include/internal/evsel.h | 7 +++++++
 3 files changed, 15 insertions(+)
tools/lib/perf/evlist.c +1 -0
index 058e3ff10f9b..c585c49491a5 100644
@@ -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
index c07160953224..dd60ee0557d8 100644
@@ -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.inherit
+		&& evsel->attr.inherit_stat;
+}
+
 void perf_counts_values__scale(struct perf_counts_values *count,
 			       bool scale, __s8 *pscaled)
 {
tools/lib/perf/include/internal/evsel.h +7 -0
index 5cd220a61962..97658f1c9ca3 100644
@@ -36,6 +36,11 @@ struct perf_sample_id {
 
 	/* Holds total ID period value for PERF_SAMPLE_READ processing. */
 	u64			 period;
+
+	/* When inherit+inherit_stat is combined with PERF_SAMPLE_READ, the
+	 * period value is per (sample_id, thread) tuple, rather than per
+	 * sample_id. */
+	bool 			 period_per_thread;
 };
 
 struct perf_evsel {
@@ -88,4 +93,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.43.0
author
#4 lore raw

PATCH v2 3/4 tools/perf: Correctly calculate sample period for inherited SAMPLE_READ values

In reply to Ben Gainey

Calculate the per-thread period when using PERF_SAMPLE_READ with inherit+inherit_stat.

Stores a per-thread period per perf_sample_id, hashed by tid. For other configurations, maintain a global period per perf_sample_id.

---
 tools/lib/perf/evsel.c                  | 41 +++++++++++++++++++++++++
 tools/lib/perf/include/internal/evsel.h | 41 +++++++++++++++++++++++--
 tools/perf/util/session.c               | 11 +++++--
 3 files changed, 88 insertions(+), 5 deletions(-)
tools/lib/perf/evsel.c +41 -0
index dd60ee0557d8..4e173151e183 100644
@@ -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->period_per_thread_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);
+		zfree(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_stat;
 }
 
+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->period_per_thread_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 +39 -2

69 lines not on this page · raw message

tools/perf/util/session.c +8 -3
index 199d3e8df315..22a8598ee849 100644
@@ -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.43.0
author
#5 lore raw

PATCH v2 4/4 tools/perf: Allow inherit + inherit_stat + PERF_SAMPLE_READ when opening events

In reply to Ben Gainey

When PERF_SAMPLE_READ is used will enable inherit_stat when inherit is set.

Provides a fallback path to disable inherit when this feature is not available, which is inline with the previous behaviour.

---
 tools/perf/util/evsel.c | 15 +++++++++++++--
 tools/perf/util/evsel.h |  1 +
 2 files changed, 14 insertions(+), 2 deletions(-)
tools/perf/util/evsel.c +13 -2
index 6d7c9c58a9bc..dc74b39a2254 100644
@@ -1156,7 +1156,11 @@ 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 + READ requires inherit_stat */
+		if (attr->inherit) {
+			attr->inherit_stat = true;
 		}
 	}
 
@@ -1832,6 +1836,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)
@@ -1887,7 +1893,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
index efbb6e848287..11cc9b8bee27 100644
@@ -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.43.0
author
#6 lore raw
In reply to Ben Gainey
Quoted 53 lines · Ben Gainey

This change allows events to use PERF_SAMPLE READ with inherit so long
as both inherit_stat and PERF_SAMPLE_TID are 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.

Perf already supports the ability to collect per-thread counts with inherit via the inherit_stat flag. This patch changes perf_event_alloc relaxing the restriction to combine inherit with PERF_SAMPLE_READ so that the combination will be allowed so long as inherit_stat and PERF_SAMPLE_TID are enabled.

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 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_stat 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 + inherit_stat + PERF_SAMPLE_READ when
opening events

Quoted 11 lines
 include/linux/perf_event.h              |  1 +
 kernel/events/core.c                    | 53 +++++++++++++++++------
--
 tools/lib/perf/evlist.c                 |  1 +
 tools/lib/perf/evsel.c                  | 48 ++++++++++++++++++++++
 tools/lib/perf/include/internal/evsel.h | 48 +++++++++++++++++++++-
 tools/perf/util/evsel.c                 | 15 ++++++-
 tools/perf/util/evsel.h                 |  1 +
 tools/perf/util/session.c               | 11 +++--
 8 files changed, 154 insertions(+), 24 deletions(-)

Hello all, appreciate everyone is busy. Is there any feedback on these? I expect they will need rebasing, but before I do that, are you happy with the approach?

Cheers
Ben
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.