Skip to contentSkip to conversation

perf python: Accept events larger than union perf_event

PATCH In review Lalit Maganti 5 participants 6 messages Oct 18 – Nov 9 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf python: Accept events larger than union perf_event

Samples recorded with --call-graph dwarf carry a copy of the user stack and are larger than union perf_event, so session.process_events() fails on the first one:

  $ perf record --call-graph dwarf -- ./workload
  $ python3 tools/perf/python/stackcollapse.py
  ...
  TypeError: Unexpected event size: 4168 < 8424

This makes all the standalone scripts unusable on such recordings.

pyrf_event__new() rejects events larger than union perf_event, which guarded the copy into the union that pyrf_event used to embed. Since commit 2c672ffdad4b ("perf python: Lazily copy events and samples from process_events") the event is borrowed, or copied to a buffer of header.size bytes when a callback keeps it, so nothing depends on the event fitting in the union any more. Remove the check.

With this, stackcollapse.py and flamegraph.py process a DWARF recording of a two-thread workload, with the same per-symbol sample counts as perf script, also when the callback keeps the events after returning.

Assisted-by LLM
---
 tools/perf/util/python.c | 5 -----
 1 file changed, 5 deletions(-)
tools/perf/util/python.c +0 -5
index 60dbe2dad5eb..8b4b2f3d0926 100644
@@ -1792,11 +1792,6 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev
 				    event->header.size, event->header.type);
 	}
 
-	if (event->header.size > sizeof(union perf_event)) {
-		return PyErr_Format(PyExc_TypeError, "Unexpected event size: %zu < %u",
-				    sizeof(union perf_event), event->header.size);
-	}
-
 	pevent = PyObject_New(struct pyrf_event, pyrf_event__type[event->header.type]);
 	if (pevent == NULL)
 		return PyErr_NoMemory();

base-commit: 1c90f0a3c8a1c4e38f9edd97af9e20848805a7fb

Signature · 1 line
2.56.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

Samples recorded with --call-graph dwarf carry a copy of the user stack and are larger than union perf_event, so session.process_events() fails on the first one:

  $ perf record --call-graph dwarf -- ./workload
  $ python3 tools/perf/python/stackcollapse.py
  ...
  TypeError: Unexpected event size: 4168 < 8424

This makes all the standalone scripts unusable on such recordings.

pyrf_event__new() rejects events larger than union perf_event, which guarded the copy into the union that pyrf_event used to embed. Since commit 2c672ffdad4b ("perf python: Lazily copy events and samples from process_events") the event is borrowed, or copied to a buffer of header.size bytes when a callback keeps it, so nothing depends on the event fitting in the union any more. Remove the check.

Quoted 6 lines

With this, stackcollapse.py and flamegraph.py process a DWARF recording of a two-thread workload, with the same per-symbol sample counts as perf script, also when the callback keeps the events after returning.

Assisted-by LLM

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

Hi,

Just wanted to bump this patch in case it was missed. I believe this is still an issue in main today.

Best,
Lalit

Reviewed-by on v1 1/1
#3 lore raw
In reply to Lalit Maganti
Quoted 46 lines · Lalit Maganti

Samples recorded with --call-graph dwarf carry a copy of the user stack and are larger than union perf_event, so session.process_events() fails on the first one:

  $ perf record --call-graph dwarf -- ./workload
  $ python3 tools/perf/python/stackcollapse.py
  ...
  TypeError: Unexpected event size: 4168 < 8424

This makes all the standalone scripts unusable on such recordings.

pyrf_event__new() rejects events larger than union perf_event, which guarded the copy into the union that pyrf_event used to embed. Since commit 2c672ffdad4b ("perf python: Lazily copy events and samples from process_events") the event is borrowed, or copied to a buffer of header.size bytes when a callback keeps it, so nothing depends on the event fitting in the union any more. Remove the check.

With this, stackcollapse.py and flamegraph.py process a DWARF recording of a two-thread workload, with the same per-symbol sample counts as perf script, also when the callback keeps the events after returning.

Assisted-by LLM
---
 tools/perf/util/python.c | 5 -----
 1 file changed, 5 deletions(-)
tools/perf/util/python.c +0 -5
index 60dbe2dad5eb..8b4b2f3d0926 100644
@@ -1792,11 +1792,6 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev
                                    event->header.size, event->header.type);
        }
 
-       if (event->header.size > sizeof(union perf_event)) {
-               return PyErr_Format(PyExc_TypeError, "Unexpected event size: %zu < %u",
-                                   sizeof(union perf_event), event->header.size);
-       }
-
        pevent = PyObject_New(struct pyrf_event, pyrf_event__type[event->header.type]);
        if (pevent == NULL)
                return PyErr_NoMemory();
Quoted 4 lines

base-commit: 1c90f0a3c8a1c4e38f9edd97af9e20848805a7fb --
2.56.0

reviewer Reviewed-by
#4 lore raw
Quoted 21 lines · Samuel Wu
Quoted 19 lines · Lalit Maganti

Samples recorded with --call-graph dwarf carry a copy of the user stack and are larger than union perf_event, so session.process_events() fails on the first one:

  $ perf record --call-graph dwarf -- ./workload
  $ python3 tools/perf/python/stackcollapse.py
  ...
  TypeError: Unexpected event size: 4168 < 8424

This makes all the standalone scripts unusable on such recordings.

pyrf_event__new() rejects events larger than union perf_event, which guarded the copy into the union that pyrf_event used to embed. Since commit 2c672ffdad4b ("perf python: Lazily copy events and samples from process_events") the event is borrowed, or copied to a buffer of header.size bytes when a callback keeps it, so nothing depends on the event fitting in the union any more. Remove the check.

Quoted 6 lines
Quoted 6 lines

With this, stackcollapse.py and flamegraph.py process a DWARF recording of a two-thread workload, with the same per-symbol sample counts as perf script, also when the callback keeps the events after returning.

Assisted-by LLM

Probably worth having:

Fixes 2c672ffdad4b ("perf python: Lazily copy events and samples from

process_events")

Thanks,
Ian

Quoted 27 lines
Quoted 21 lines
---
 tools/perf/util/python.c | 5 -----
 1 file changed, 5 deletions(-)
tools/perf/util/python.c +0 -5
index 60dbe2dad5eb..8b4b2f3d0926 100644
@@ -1792,11 +1792,6 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev
                                    event->header.size, event->header.type);
        }
 
-       if (event->header.size > sizeof(union perf_event)) {
-               return PyErr_Format(PyExc_TypeError, "Unexpected event size: %zu < %u",
-                                   sizeof(union perf_event), event->header.size);
-       }
-
        pevent = PyObject_New(struct pyrf_event, pyrf_event__type[event->header.type]);
        if (pevent == NULL)
                return PyErr_NoMemory();
Quoted 4 lines

base-commit: 1c90f0a3c8a1c4e38f9edd97af9e20848805a7fb --
2.56.0

#5 lore raw
Quoted 29 lines · Ian Rogers
Quoted 21 lines · Samuel Wu
Quoted 19 lines · Lalit Maganti

Samples recorded with --call-graph dwarf carry a copy of the user stack and are larger than union perf_event, so session.process_events() fails on the first one:

  $ perf record --call-graph dwarf -- ./workload
  $ python3 tools/perf/python/stackcollapse.py
  ...
  TypeError: Unexpected event size: 4168 < 8424

This makes all the standalone scripts unusable on such recordings.

pyrf_event__new() rejects events larger than union perf_event, which guarded the copy into the union that pyrf_event used to embed. Since commit 2c672ffdad4b ("perf python: Lazily copy events and samples from process_events") the event is borrowed, or copied to a buffer of header.size bytes when a callback keeps it, so nothing depends on the event fitting in the union any more. Remove the check.

Quoted 6 lines
Quoted 6 lines

With this, stackcollapse.py and flamegraph.py process a DWARF recording of a two-thread workload, with the same per-symbol sample counts as perf script, also when the callback keeps the events after returning.

Assisted-by LLM
Quoted 6 lines

Probably worth having:

Fixes 2c672ffdad4b ("perf python: Lazily copy events and samples from

process_events")

I'll pick it for v7.4,

Thanks,

- Arnaldo

Quoted 32 lines

Thanks,
Ian

Quoted 27 lines
Quoted 21 lines
---
 tools/perf/util/python.c | 5 -----
 1 file changed, 5 deletions(-)
tools/perf/util/python.c +0 -5
index 60dbe2dad5eb..8b4b2f3d0926 100644
@@ -1792,11 +1792,6 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev
                                    event->header.size, event->header.type);
        }
 
-       if (event->header.size > sizeof(union perf_event)) {
-               return PyErr_Format(PyExc_TypeError, "Unexpected event size: %zu < %u",
-                                   sizeof(union perf_event), event->header.size);
-       }
-
        pevent = PyObject_New(struct pyrf_event, pyrf_event__type[event->header.type]);
        if (pevent == NULL)
                return PyErr_NoMemory();
Quoted 4 lines

base-commit: 1c90f0a3c8a1c4e38f9edd97af9e20848805a7fb --
2.56.0

- Arnaldo