Skip to contentSkip to conversation

perf: Support PERF_SAMPLE_READ with inherit_stat

PATCH Superseded Ben Gainey 2 participants 7 messages Mar 22, 2024 perf events

v3 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Ben Gainey

author
#0 lore raw

v3 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 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_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                          | 62 ++++++++++----
 tools/lib/perf/evlist.c                       |  1 +
 tools/lib/perf/evsel.c                        | 48 +++++++++++
 tools/lib/perf/include/internal/evsel.h       | 55 ++++++++++++-
 tools/perf/tests/attr/base-record             |  2 +-
 tools/perf/tests/attr/base-stat               |  2 +-
 tools/perf/tests/attr/system-wide-dummy       |  2 +-
 tools/perf/tests/attr/test-record-dummy-C0    |  2 +-
 .../test-record-group-sampling-inherit-stat   | 62 ++++++++++++++
 tools/perf/util/evsel.c                       | 82 ++++++++++++++++++-
 tools/perf/util/evsel.h                       |  1 +
 tools/perf/util/session.c                     | 11 ++-
 13 files changed, 305 insertions(+), 26 deletions(-)
 create mode 100644 tools/perf/tests/attr/test-record-group-sampling-inherit-stat
Signature · 1 line
2.44.0
author
#1 lore raw

PATCH v3 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, 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.

---
 include/linux/perf_event.h |  1 +
 kernel/events/core.c       | 62 +++++++++++++++++++++++++++++---------
 2 files changed, 48 insertions(+), 15 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 +47 -15

161 lines not on this page · raw message

Signature · 1 line
2.44.0
author
#2 lore raw

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

In reply to Ben Gainey

perf_sample_ids and related evlist/evsel code are modified to track which events combine inherit+inherit_stat+PERF_SAMPLE_READ.

Events with this combination of properties must be handled differently when tracking each streams sample period, as their stream-id is now only unique per thread, rather than globally unique.

---
 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
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 +9 -0
index 5cd220a61962..8dd58149986c 100644
@@ -36,6 +36,13 @@ 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 +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.44.0
author
#3 lore raw

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

In reply to Ben Gainey

Sample period calculation is updated to take into account the fact that the stream-id for events with inherit+inherit_stat+PERF_SAMPLE_READ is now unique per thread, rather than globally unique.

perf_sample_id is modified to support tracking per-stream+per-thread values, along with the existing global per-stream values. In the per-thread case, values are stored in a hash by tid.

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 | 46 +++++++++++++++++++++++--
 tools/perf/util/session.c               | 11 ++++--
 3 files changed, 93 insertions(+), 5 deletions(-)
tools/lib/perf/evsel.c +41 -0
index dd60ee0557d8..2bdba78cc43e 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 +44 -2

74 lines not on this page · raw message

tools/perf/util/session.c +8 -3
index 199d3e8df315..2d6a92374847 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.44.0
author
#4 lore raw

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

In reply to Ben Gainey

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.

The user must pass --stat option to perf record to opt into this new behaviour.

---
 tools/perf/tests/attr/base-record             |  2 +-
 tools/perf/tests/attr/base-stat               |  2 +-
 tools/perf/tests/attr/system-wide-dummy       |  2 +-
 tools/perf/tests/attr/test-record-dummy-C0    |  2 +-
 .../test-record-group-sampling-inherit-stat   | 62 ++++++++++++++
 tools/perf/util/evsel.c                       | 82 ++++++++++++++++++-
 tools/perf/util/evsel.h                       |  1 +
 7 files changed, 147 insertions(+), 6 deletions(-)
 create mode 100644 tools/perf/tests/attr/test-record-group-sampling-inherit-stat
tools/perf/tests/attr/base-record +1 -1
index b44e4e6e4443..dd76fbdb628f 100644
@@ -5,7 +5,7 @@ group_fd=-1
 flags=0|8
 cpu=*
 type=0|1
-size=136
+size=144
 config=0|1
 sample_period=*
 sample_type=263
tools/perf/tests/attr/base-stat +1 -1
index fccd8ec4d1b0..d769e69bb4f5 100644
@@ -5,7 +5,7 @@ group_fd=-1
 flags=0|8
 cpu=*
 type=0
-size=136
+size=144
 config=0
 sample_period=0
 sample_type=65536
tools/perf/tests/attr/system-wide-dummy +1 -1
index a1e1d6a263bf..9806de3d9c9e 100644
@@ -7,7 +7,7 @@ cpu=*
 pid=-1
 flags=8
 type=1
-size=136
+size=144
 config=9
 sample_period=1
 # PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME |
tools/perf/tests/attr/test-record-dummy-C0 +1 -1
index 576ec48b3aaf..87f5c8db5ba1 100644
@@ -10,7 +10,7 @@ cpu=0
 pid=-1
 flags=8
 type=1
-size=136
+size=144
 config=9
 sample_period=4000
 # PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME |
tools/perf/tests/attr/test-record-group-sampling-inherit-stat new file +62 -0

62 lines not on this page · raw message

tools/perf/util/evsel.c +80 -2

112 lines not on this page · raw message

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.44.0
author
#5 lore raw

Re: [PATCH v3 4/4] tools/perf: Allow inherit + inherit_stat + PERF_SAMPLE_READ when opening events

Quoted 30 lines · Ben Gainey

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.

The user must pass --stat option to perf record to opt into this new behaviour.

---
 tools/perf/tests/attr/base-record             |  2 +-
 tools/perf/tests/attr/base-stat               |  2 +-
 tools/perf/tests/attr/system-wide-dummy       |  2 +-
 tools/perf/tests/attr/test-record-dummy-C0    |  2 +-
 .../test-record-group-sampling-inherit-stat   | 62 ++++++++++++++
 tools/perf/util/evsel.c                       | 82
++++++++++++++++++-
 tools/perf/util/evsel.h                       |  1 +
 7 files changed, 147 insertions(+), 6 deletions(-)
 create mode 100644 tools/perf/tests/attr/test-record-group-sampling-

inherit-stat

tools/perf/tests/attr/base-record +0 -0
index b44e4e6e4443..dd76fbdb628f 100644

tools/perf/tests/attr/base-record group_fd=-1 · line 8

Quoted 5 lines
tools/perf/tests/attr/base-record +1 -1
@@ -5,7 +5,7 @@ group_fd=-1
 flags=0|8
 cpu=*
 type=0|1
-size=136
+size=144

Sigh... this one belongs elsewhere. Too many spinning plates.

I'll have to redo this one with the correct size= value.

Regards
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.

#6 lore raw

Re: [PATCH v3 4/4] tools/perf: Allow inherit + inherit_stat + PERF_SAMPLE_READ when opening events

Quoted 11 lines · Ben Gainey

tools/perf/tests/attr/base-record group_fd=-1 · line 8

Quoted 7 lines

+++ b/tools/perf/tests/attr/base-record

tools/perf/tests/attr/base-record +1 -1
@@ -5,7 +5,7 @@ group_fd=-1
 flags=0|8
 cpu=*
 type=0|1
-size=136
+size=144

Sigh... this one belongs elsewhere. Too many spinning plates.

I'll have to redo this one with the correct size= value.

It happens, thanks for noticing it and promply reporting, this is _very much_ appreciated, saves me time. :-)

Just fix it and send a v4.

- Arnaldo