[PATCH v8 16/58] perf python: Add mmap2 event

Ian Rogers
2026-04-28T07:19:51Z

If mmap is handled so should mmap2 events. Add support as a distinct
python event type.

Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/python.c | 60 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)

diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 1ca296000351..ae009bc9a0d7 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -151,6 +151,54 @@ static PyTypeObject pyrf_mmap_event__type = {
.tp_repr = (reprfunc)pyrf_mmap_event__repr,
};

+static const char pyrf_mmap2_event__doc[] = PyDoc_STR("perf mmap2 event object.");
+
+static PyMemberDef pyrf_mmap2_event__members[] = {
+ sample_members
+ member_def(perf_event_header, type, T_UINT, "event type"),
+ member_def(perf_event_header, misc, T_UINT, "event misc"),
+ member_def(perf_record_mmap2, pid, T_UINT, "event pid"),
+ member_def(perf_record_mmap2, tid, T_UINT, "event tid"),
+ member_def(perf_record_mmap2, start, T_ULONGLONG, "start of the map"),
+ member_def(perf_record_mmap2, len, T_ULONGLONG, "map length"),
+ member_def(perf_record_mmap2, pgoff, T_ULONGLONG, "page offset"),
+ member_def(perf_record_mmap2, prot, T_UINT, "protection"),
+ member_def(perf_record_mmap2, flags, T_UINT, "flags"),
+ member_def(perf_record_mmap2, filename, T_STRING_INPLACE, "backing store"),
+ { .name = NULL, },
+};
+
+static PyObject *pyrf_mmap2_event__repr(const struct pyrf_event *pevent)
+{
+ PyObject *ret;
+ char *s;
+
+ if (asprintf(&s, "{ type: mmap2, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
+ "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
+ "flags: %#x, prot: %#x, filename: %s }",
+ pevent->event.mmap2.pid, pevent->event.mmap2.tid,
+ pevent->event.mmap2.start, pevent->event.mmap2.len,
+ pevent->event.mmap2.pgoff, pevent->event.mmap2.flags,
+ pevent->event.mmap2.prot, pevent->event.mmap2.filename) < 0)
+ return PyErr_NoMemory();
+
+ ret = PyUnicode_FromString(s);
+ free(s);
+ return ret;
+}
+
+static PyTypeObject pyrf_mmap2_event__type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ .tp_name = "perf.mmap2_event",
+ .tp_basicsize = sizeof(struct pyrf_event),
+ .tp_dealloc = (destructor)pyrf_event__delete,
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_doc = pyrf_mmap2_event__doc,
+ .tp_members = pyrf_mmap2_event__members,
+ .tp_getset = pyrf_event__getset,
+ .tp_repr = (reprfunc)pyrf_mmap2_event__repr,
+};
+
static const char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");

static PyMemberDef pyrf_task_event__members[] = {
@@ -776,6 +824,9 @@ static int pyrf_event__setup_types(void)
int err;

err = PyType_Ready(&pyrf_mmap_event__type);
+ if (err < 0)
+ goto out;
+ err = PyType_Ready(&pyrf_mmap2_event__type);
if (err < 0)
goto out;
err = PyType_Ready(&pyrf_lost_event__type);
@@ -805,6 +856,7 @@ static int pyrf_event__setup_types(void)

static PyTypeObject *pyrf_event__type[] = {
[PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
+ [PERF_RECORD_MMAP2] = &pyrf_mmap2_event__type,
[PERF_RECORD_LOST] = &pyrf_lost_event__type,
[PERF_RECORD_COMM] = &pyrf_comm_event__type,
[PERF_RECORD_EXIT] = &pyrf_task_event__type,
@@ -971,6 +1023,11 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev
size_t max_len = pevent->event.header.size - offsetof(struct perf_record_mmap, filename);

pevent->event.mmap.filename[max_len - 1] = '\0';
+ } else if (event->header.type == PERF_RECORD_MMAP2) {
+ /* Ensure '\0' string termination. */
+ size_t max_len = pevent->event.header.size - offsetof(struct perf_record_mmap2, filename);
+
+ pevent->event.mmap2.filename[max_len - 1] = '\0';
}

perf_sample__init(&pevent->sample, /*all=*/true);
@@ -3234,6 +3291,9 @@ PyMODINIT_FUNC PyInit_perf(void)
Py_INCREF(&pyrf_mmap_event__type);
PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);

+ Py_INCREF(&pyrf_mmap2_event__type);
+ PyModule_AddObject(module, "mmap2_event", (PyObject *)&pyrf_mmap2_event__type);
+
Py_INCREF(&pyrf_lost_event__type);
PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);

--
2.54.0.545.g6539524ca2-goog