Linux Trace Kernel
 help / color / mirror / Atom feed
From: Tomas Glozar <tglozar@redhat.com>
To: Steven Rostedt <rostedt@goodmis.org>, Tomas Glozar <tglozar@redhat.com>
Cc: John Kacur <jkacur@redhat.com>,
	Luis Goncalves <lgoncalv@redhat.com>,
	Crystal Wood <crwood@redhat.com>,
	Costa Shulyupin <costa.shul@redhat.com>,
	Wander Lairson Costa <wander@redhat.com>,
	linux-trace-kernel <linux-trace-kernel@vger.kernel.org>
Subject: [RFC PATCH 05/10] rtla/trace: Add helper to read "now ts" stats field
Date: Fri, 27 Nov 2026 16:32:01 +0100	[thread overview]
Message-ID: <20261127153206.134322-6-tglozar@redhat.com> (raw)
In-Reply-To: <20261127153206.134322-1-tglozar@redhat.com>

Add trace_get_cpu_now_ts() to read the current timestamp of a given CPU
from the per-CPU stats file.

The returned value is in nanoseconds in the instance clock, matching the
trace event timestamps, so it can be used as a per-CPU reference to
compute how long before a sample the measurement started.

The "now ts" field reports the clock of the CPU executing the read, not
the clock of the CPU owning the stats file, so callers must be running
on the CPU they are reading.

The stats file reports the timestamp as seconds.microseconds, so the
value is reported with microsecond granularity. Clocks that use a
non-nanosecond unit (in_ns == 0) are unsupported.

Signed-off-by: Tomas Glozar <tglozar@redhat.com>
---
 tools/tracing/rtla/src/trace.c | 42 ++++++++++++++++++++++++++++++++++
 tools/tracing/rtla/src/trace.h |  2 ++
 2 files changed, 44 insertions(+)

diff --git a/tools/tracing/rtla/src/trace.c b/tools/tracing/rtla/src/trace.c
index c608393533494..e068f25759c00 100644
--- a/tools/tracing/rtla/src/trace.c
+++ b/tools/tracing/rtla/src/trace.c
@@ -275,6 +275,48 @@ int trace_instance_clear(struct trace_instance *trace)
 	return tracefs_instance_file_write(trace->inst, "trace", "");
 }
 
+/*
+ * trace_get_cpu_now_ts - get the current timestamp of a CPU
+ * @inst: the trace instance, NULL for the top instance
+ * @cpu: the CPU to read
+ * @ts: output, timestamp in nanoseconds in the instance clock
+ *
+ * The "now ts" field reports the clock of the CPU executing the read, not
+ * the clock of the CPU owning the stats file, so the caller must be running
+ * on @cpu.
+ *
+ * The output has microsecond granularity, matching the format of per-cpu stats
+ * reported by tracefs. Clocks that operate in non-nanosecond time units
+ * (in_ns == 0) are not supported.
+ *
+ * Returns 0 on success, -1 otherwise.
+ */
+int trace_get_cpu_now_ts(struct tracefs_instance *inst, int cpu,
+			 unsigned long long *ts)
+{
+	unsigned long long sec, usec;
+	char path[MAX_PATH];
+	char *buf, *line;
+	int ret = -1;
+
+	snprintf(path, sizeof(path), "per_cpu/cpu%d/stats", cpu);
+
+	buf = tracefs_instance_file_read(inst, path, NULL);
+	if (!buf)
+		return -1;
+
+	line = strstr(buf, "now ts:");
+	if (!line || sscanf(line, "now ts: %llu.%llu", &sec, &usec) != 2)
+		goto out;
+
+	*ts = sec * 1000000000ULL + usec * 1000ULL;
+	ret = 0;
+
+out:
+	free(buf);
+	return ret;
+}
+
 /*
  * trace_events_free - free a list of trace events
  */
diff --git a/tools/tracing/rtla/src/trace.h b/tools/tracing/rtla/src/trace.h
index b3a626b676b3d..4ec8259ccdd71 100644
--- a/tools/tracing/rtla/src/trace.h
+++ b/tools/tracing/rtla/src/trace.h
@@ -37,6 +37,8 @@ struct tracefs_instance *create_instance(char *instance_name);
 void destroy_instance(struct tracefs_instance *inst);
 
 int save_trace_to_file(struct tracefs_instance *inst, const char *filename);
+int trace_get_cpu_now_ts(struct tracefs_instance *inst, int cpu,
+			 unsigned long long *ts);
 int collect_registered_events(struct tep_event *tep, struct tep_record *record,
 			      int cpu, void *context);
 
-- 
2.56.1


  parent reply	other threads:[~2026-11-27 15:33 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 15:31 [RFC PATCH 00/10] rtla/timerlat: Add auto-analysis action and report spike timestamps Tomas Glozar
2026-11-27 15:31 ` [RFC PATCH 01/10] rtla: Support auto-analysis as an action Tomas Glozar
2026-11-27 15:31 ` [RFC PATCH 02/10] rtla/tests: Add unit tests for aa action Tomas Glozar
2026-11-27 15:31 ` [RFC PATCH 03/10] rtla/tests: Add runtime " Tomas Glozar
2026-11-27 15:32 ` [RFC PATCH 04/10] Documentation/rtla: Document " Tomas Glozar
2026-11-27 15:32 ` Tomas Glozar [this message]
2026-11-27 15:32 ` [RFC PATCH 06/10] rtla/trace: Add helper to gather "now ts" over a CPU set Tomas Glozar
2026-11-27 15:32 ` [RFC PATCH 07/10] rtla/trace: Allow storing start clock value of tracefs instances Tomas Glozar
2026-11-27 15:32 ` [RFC PATCH 08/10] rtla/timerlat: Save the trace clock at measurement start Tomas Glozar
2026-11-27 15:32 ` [RFC PATCH 09/10] rtla/timerlat_aa: Print time offset and seqnum of the spike Tomas Glozar
2026-11-27 15:32 ` [RFC PATCH 10/10] Documentation/rtla: Document auto-analysis timestamps Tomas Glozar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127153206.134322-6-tglozar@redhat.com \
    --to=tglozar@redhat.com \
    --cc=costa.shul@redhat.com \
    --cc=crwood@redhat.com \
    --cc=jkacur@redhat.com \
    --cc=lgoncalv@redhat.com \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=wander@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox