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 06/10] rtla/trace: Add helper to gather "now ts" over a CPU set
Date: Fri, 27 Nov 2026 16:32:02 +0100	[thread overview]
Message-ID: <20261127153206.134322-7-tglozar@redhat.com> (raw)
In-Reply-To: <20261127153206.134322-1-tglozar@redhat.com>

Add trace_get_now_ts_cpus() to read the current timestamp of a set of
CPUs. One thread is spawned per CPU; each thread pins itself to its CPU
and reads the clock there, storing the value in its own per-CPU slot of
the output array.

Reading the clock from a thread pinned to the target CPU avoids
migrating the calling thread around, keeping its affinity untouched.

The threads are held behind a gate until all of them have been created,
then released together by a barrier, so the clock reads happen at nearly
the same time.

CPUs that cannot be pinned or read are left at 0, and the helper returns
-1, while still filling the CPUs that succeeded.

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

diff --git a/tools/tracing/rtla/src/trace.c b/tools/tracing/rtla/src/trace.c
index e068f25759c00..a007479768f99 100644
--- a/tools/tracing/rtla/src/trace.c
+++ b/tools/tracing/rtla/src/trace.c
@@ -4,6 +4,7 @@
 #include <tracefs.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <pthread.h>
 #include <errno.h>
 
 #include "trace.h"
@@ -317,6 +318,145 @@ int trace_get_cpu_now_ts(struct tracefs_instance *inst, int cpu,
 	return ret;
 }
 
+struct trace_cpu_ts_ctx {
+	pthread_mutex_t mutex;
+	pthread_cond_t cond;
+	pthread_barrier_t barrier;
+	bool go;
+	bool abort;
+};
+
+struct trace_cpu_ts_arg {
+	struct tracefs_instance *inst;
+	unsigned long long *ts;
+	struct trace_cpu_ts_ctx *ctx;
+	pthread_t thread;
+	bool created;
+	int cpu;
+	int retval;
+};
+
+static void *trace_cpu_ts_thread(void *data)
+{
+	struct trace_cpu_ts_arg *arg = data;
+	struct trace_cpu_ts_ctx *ctx = arg->ctx;
+	cpu_set_t affinity;
+	bool pinned;
+
+	CPU_ZERO(&affinity);
+	CPU_SET(arg->cpu, &affinity);
+
+	/*
+	 * Never read the clock without being pinned: the timestamp would be
+	 * the one of whichever CPU this thread happens to be running on.
+	 * A failure to pin must still reach the barrier, or the other threads
+	 * would wait forever.
+	 */
+	pinned = !sched_setaffinity(0, sizeof(affinity), &affinity);
+
+	pthread_mutex_lock(&ctx->mutex);
+	while (!ctx->go)
+		pthread_cond_wait(&ctx->cond, &ctx->mutex);
+	pthread_mutex_unlock(&ctx->mutex);
+
+	if (ctx->abort)
+		return NULL;
+
+	pthread_barrier_wait(&ctx->barrier);
+
+	if (pinned)
+		arg->retval = trace_get_cpu_now_ts(arg->inst, arg->cpu,
+						   &arg->ts[arg->cpu]);
+	return NULL;
+}
+
+/*
+ * trace_get_now_ts_cpus - get the current timestamp of a set of CPUs
+ * @inst: the trace instance, NULL for the top instance
+ * @cpus: the set of CPUs to read
+ * @ts: output array with nr_cpus entries, indexed by CPU
+ *
+ * Spawn a thread for each CPU in @cpus. Each thread pins itself to its CPU
+ * and reads the clock there, writing the timestamp to its own ts[cpu] slot.
+ * CPUs that cannot be read are left at 0.
+ *
+ * The threads are held after pinning until all of them were created, then
+ * released together by a barrier, so that the per-CPU clock readings happen
+ * at (nearly) the same time.
+ *
+ * Returns 0 on success, -1 if any CPU in @cpus could not be read.
+ */
+int trace_get_now_ts_cpus(struct tracefs_instance *inst, const cpu_set_t *cpus,
+			  unsigned long long *ts)
+{
+	struct trace_cpu_ts_ctx ctx = {
+		.mutex = PTHREAD_MUTEX_INITIALIZER,
+		.cond = PTHREAD_COND_INITIALIZER,
+	};
+	struct trace_cpu_ts_arg *args;
+	int cpu, count = 0, ret = 0;
+
+	args = calloc_fatal(nr_cpus, sizeof(*args));
+
+	for (cpu = 0; cpu < nr_cpus; cpu++) {
+		if (!CPU_ISSET(cpu, cpus))
+			continue;
+
+		ts[cpu] = 0;
+
+		args[cpu].inst = inst;
+		args[cpu].ts = ts;
+		args[cpu].ctx = &ctx;
+		args[cpu].cpu = cpu;
+		args[cpu].retval = -1;
+
+		if (pthread_create(&args[cpu].thread, NULL, trace_cpu_ts_thread,
+				   &args[cpu])) {
+			debug_msg("Could not create thread for cpu %d\n", cpu);
+			ret = -1;
+			continue;
+		}
+
+		args[cpu].created = true;
+		count++;
+	}
+
+	if (count) {
+		/*
+		 * The barrier can only be sized now that the number of created
+		 * threads is known. If it cannot be set up, release the threads
+		 * without reading the clock.
+		 */
+		if (pthread_barrier_init(&ctx.barrier, NULL, count)) {
+			debug_msg("Could not initialize thread barrier\n");
+			ctx.abort = true;
+		}
+
+		pthread_mutex_lock(&ctx.mutex);
+		ctx.go = true;
+		pthread_cond_broadcast(&ctx.cond);
+		pthread_mutex_unlock(&ctx.mutex);
+	}
+
+	for (cpu = 0; cpu < nr_cpus; cpu++) {
+		if (!args[cpu].created)
+			continue;
+
+		pthread_join(args[cpu].thread, NULL);
+		if (args[cpu].retval)
+			ret = -1;
+	}
+
+	if (count && !ctx.abort)
+		pthread_barrier_destroy(&ctx.barrier);
+	pthread_cond_destroy(&ctx.cond);
+	pthread_mutex_destroy(&ctx.mutex);
+
+	free(args);
+
+	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 4ec8259ccdd71..ad9eaaa61b77a 100644
--- a/tools/tracing/rtla/src/trace.h
+++ b/tools/tracing/rtla/src/trace.h
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <tracefs.h>
 #include <stddef.h>
+#include <sched.h>
 
 struct trace_events {
 	struct trace_events *next;
@@ -39,6 +40,8 @@ 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 trace_get_now_ts_cpus(struct tracefs_instance *inst, const cpu_set_t *cpus,
+			  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 ` [RFC PATCH 05/10] rtla/trace: Add helper to read "now ts" stats field Tomas Glozar
2026-11-27 15:32 ` Tomas Glozar [this message]
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-7-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