[PATCH 2/4] perf python: Add clockid attribute to session

From: Lalit Maganti

Date: Sat Nov 14 2026 - 19:40:14 EST


With perf record -k/--clockid, sample times come from the given clock
instead of the perf clock, and the clock is saved in the perf.data
header. perf.session doesn't expose it, so a script can't tell which
clock its timestamps are in.

Add a "clockid" attribute with the clockid_t from the header, or None
if the default perf clock was used.

Assisted-by: LLM
Signed-off-by: Lalit Maganti <lalitmaganti@xxxxxxxxx>
---
tools/perf/python/perf.pyi | 1 +
tools/perf/util/python.c | 8 ++++++++
2 files changed, 9 insertions(+)

diff --git a/tools/perf/python/perf.pyi b/tools/perf/python/perf.pyi
index 5b68d70c5216..71bc7719ac5c 100644
--- a/tools/perf/python/perf.pyi
+++ b/tools/perf/python/perf.pyi
@@ -500,6 +500,7 @@ class session:
...
e_machine: int
is_64_bit: bool
+ clockid: Optional[int]
def process_events(self) -> None:
"""Process all events in the session."""
...
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 3ba3ce0e651c..274c66c28c1c 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -5330,6 +5330,14 @@ static PyObject *pyrf_session__getattro(struct pyrf_session *psession, PyObject
return PyBool_FromLong(sizeof(void *) == 8);
}
}
+ if (!strcmp(name_str, "clockid")) {
+ /* The clockid_t of perf record -k, None for the perf clock. */
+ struct perf_env *env = perf_session__env(psession->session);
+
+ if (!env->clock.enabled)
+ Py_RETURN_NONE;
+ return PyLong_FromLong(env->clock.clockid);
+ }
return PyObject_GenericGetAttr((PyObject *) psession, attr_name);
}

--
2.56.0