mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v5 0/4] rv: per-edge dwell-time statistics for per-cpu monitors
@ 2026-11-27 11:36 Tobias Schaffner
  2026-11-27 11:36 ` [PATCH v5 1/4] rv: guard rv.h against double inclusion Tobias Schaffner
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Tobias Schaffner @ 2026-11-27 11:36 UTC (permalink / raw)
  To: Steven Rostedt, Gabriele Monaco
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Masami Hiramatsu,
	Mathieu Desnoyers, linux-trace-kernel, linux-doc,
	linux-kselftest, linux-kernel, rpm, jan.kiszka, Tobias Schaffner,
	Shuah Khan

Hi Steven, Gabriele,

I am using the RV system for a downstream project called EVL (aka Xenomai4).
It is a small co-kernel system, small enough that I can model the whole
co-kernel with RV instead of just a fragment of it.

The patches in this series add a per-monitor "stats" file that exposes how
often an edge has been visited and the dwell time in the prior state for
per-cpu monitors.

This allows me to not only check if the model is correct but also to see if
all paths are exercised by my stress tooling and to decompose the latency of
the co-kernel's wakeup path.

I think that some of the upstream monitors like e.g. the sts monitor could also
profit from this.

As an example, here is the sts monitor on a StarFive VisionFive2 running a
PREEMPT_RT kernel with this series (isolcpus=2-3 nohz_full=2-3). While it
was enabled, hackbench (-g 4 -l 2000) ran on cpus 0-1, a SCHED_FIFO
stress-ng cyclic thread (1 ms period) ran on the isolated cpu3 for 40 s,
and cpu2 stayed idle. Only edges with a non-zero count are shown; all of
them are valid (column 4) and every undefined edge stayed at zero:

  # cat monitors/sched/sts/stats
  # cpu edge label valid count sum_ns max_ns
  0 0 can_sched:irq_disable 1 11367959 39357857250 3955250
  0 4 can_sched:schedule_entry 1 912841 1953227750 54500
  0 7 cant_sched:irq_enable 1 11367959 15829311500 44750
  0 8 cant_sched:irq_entry 1 64567 105061000 6500
  0 13 disable_to_switch:irq_enable 1 8637 24605500 14250
  0 14 disable_to_switch:irq_entry 1 170 263500 5500
  0 15 disable_to_switch:sched_switch 1 904204 3682090250 24000
  0 18 enable_to_exit:irq_disable 1 8488 16681750 3500
  0 19 enable_to_exit:irq_enable 1 8488 55429250 23750
  0 20 enable_to_exit:irq_entry 1 8324 13597750 5500
  0 23 enable_to_exit:schedule_exit 1 912841 1015160500 2750
  0 25 in_irq:irq_enable 1 170 1154000 15500
  0 26 in_irq:irq_entry 1 2 2500 1250
  0 30 scheduling:irq_disable 1 913011 1096248500 2250
  0 37 switching:irq_enable 1 904204 2809182000 13000
  [ cpus 1-2 omitted ]
  3 0 can_sched:irq_disable 1 308000 44282747500 1991893250
  3 4 can_sched:schedule_entry 1 77831 99574500 6000
  3 7 cant_sched:irq_enable 1 308000 781368250 25750
  3 8 cant_sched:irq_entry 1 48789 71474750 4750
  3 13 disable_to_switch:irq_enable 1 7 24000 9000
  3 14 disable_to_switch:irq_entry 1 21 26750 1500
  3 15 disable_to_switch:sched_switch 1 77824 179601750 10500
  3 18 enable_to_exit:irq_disable 1 147 266000 3250
  3 19 enable_to_exit:irq_enable 1 147 1193250 14000
  3 20 enable_to_exit:irq_entry 1 143 198250 1750
  3 23 enable_to_exit:schedule_exit 1 77831 78554500 4250
  3 25 in_irq:irq_enable 1 21 199500 11250
  3 30 scheduling:irq_disable 1 77852 89848000 2500
  3 37 switching:irq_enable 1 77824 130814500 7000

The sts model splits the schedule->switch path into separate states, so the
per-edge dwell decomposes the scheduler's interrupts-off window: on cpu0 the
prep phase (disable_to_switch, irqs off until the switch) tops out at 24.0us
and the switch itself (switching:irq_enable) at 13.0us, over 904k switches.
The counts also line up with the model, e.g. every irq_disable in can_sched
has a matching irq_enable in cant_sched (11367959 == 11367959) and every
switch in disable_to_switch leaves switching once (904204 == 904204).

The isolated cpu3 only runs the cyclic thread, 77824 switches in 40 s, and
its worst case is tighter (10.5us prep, 7.0us switch), while the idle
isolated cpu2 shows no transitions at all. So the same file also makes the
effect of cpu isolation visible per cpu.

The changes are gated by CONFIG_RV_EDGE_STAT and dormant until a monitor is
enabled, so existing setups are unaffected. When enabled, each accepted
transition adds one local_clock() and a few lock-free local64_t updates. With
the config off there is no code on the hot path at all.

I focused on per-cpu monitors as a first step. Per-task and per-object
monitors would have to aggregate entities that share a cpu, which is harder
to get right, so I left them out for now.

What is your opinion on this? Do you think this is worth getting upstreamed?

Thanks for taking a look,
Tobias

---

Changes since v4:
 - Keep da_monitor_start() as it is and add
   da_monitor_start_unaccounted() for the run-start path instead of
   passing the state word. Both share __da_monitor_start() (Gabriele).

Changes since v3:
 - Based on Gabriele's BTF fix for rv.h [1]. Thanks for this!
 - Stamp the initial state only when the monitor starts on entering it
   (da_handle_start_event()). da_handle_start_run_event() starts a
   monitor in a state the system has been in for an unknown time, so
   its first dwell was a bogus few nanoseconds (sashiko). Documented
   that this first transition is neither counted or timed.
 - Made da_state_t a typedef instead of a define (Gabriele).
 - Moved RV_EDGE_STAT to the end of the RV Kconfig (Gabriele).

[1] https://lore.kernel.org/r/20261123083709.62502-1-gmonaco@redhat.com

Changes since v2:
 - Resplit the series: one patch adds the facility, one wires it into the
   DA/HA layer and one adds the selftest. da_state_t is now defined before
   its first use (Gabriele).
 - Used rv_create_file() / rv_remove() with RV_MODE_READ and included
   "rv.h" rather than <linux/tracefs.h> (Gabriele). That makes rv.h
   reachable from a header every monitor pulls in, so patch 1 guards it
   against double inclusion first.
 - Replaced the pack/unpack macros with FIELD_GET() and FIELD_PREP()
   (Gabriele).
 - Dropped struct rv_this_edges in favour of a plain per-cpu array of
   struct rv_edge_stat, used through per_cpu_ptr() and sizeof().
 - Added DA_EDGE(), DA_EDGE_STATE() and DA_EDGE_EVENT() instead of open
   coding the state * EVENT_MAX + event arithmetic.
 - New "valid" column, from model_get_next_state() != INVALID_STATE, so a
   non-zero count on an edge the model does not define is visible instead
   of having to be inferred from the label. The selftest checks it too.
 - unlikely() on the first-sample check, and dropped the (unsigned int)
   casts from both da_state_of() variants.

Changes since v1:
 - Packed the state and its entry timestamp into one word, so one cmpxchg
   commits both and a nested IRQ can no longer charge a dwell to the
   wrong edge. Hope this approach is fine.
 - Moved the header to include/rv/edge_stat.h (Gabriele).
 - Implemented it all in the da_monitor layer so that rv.c is untouched
   and the file is created from da_monitor_init() (Gabriele). I did not
   create a matrix living in struct da_monitor though. I think that this
   would need a fixed upper bound as rv.h does not know how many states
   and events the monitor will have. But this would lead to a large array
   in any monitor including per-task monitors.
 - Used local64_try_cmpxchg(), bound the loop at
   MAX_DA_RETRY_RACING_EVENTS and warn now if it is hit (Gabriele).
 - Static per-cpu matrix instead of alloc_percpu() (sashiko).
 - depend on 64BIT, so GENERIC_ATOMIC64 is unreachable and the 32-bit
   NMI deadlock cannot happen at all (sashiko).
 - Reset and report for_each_possible_cpu(), and reset before the
   tracepoints are registered (sashiko).
 - stats file is 0440 now, and a failed create is propagated.
 - Columns are now count sum_ns max_ns.

Tobias Schaffner (4):
  rv: guard rv.h against double inclusion
  rv: add per-edge dwell-time statistics
  rv: collect per-edge dwell time for per-cpu DA/HA monitors
  selftests/verification: test per-edge dwell-time statistics

 .../trace/rv/runtime-verification.rst         |  30 +++
 include/linux/rv.h                            |   9 +-
 include/rv/da_monitor.h                       |  40 +++-
 include/rv/edge_stat.h                        | 214 ++++++++++++++++++
 include/rv/ha_monitor.h                       |   2 +-
 kernel/trace/rv/Kconfig                       |  12 +
 kernel/trace/rv/rv.h                          |   5 +
 tools/testing/selftests/verification/config   |   3 +
 .../verification/test.d/rv_edge_stats.tc      |  37 +++
 9 files changed, 343 insertions(+), 9 deletions(-)
 create mode 100644 include/rv/edge_stat.h
 create mode 100644 tools/testing/selftests/verification/test.d/rv_edge_stats.tc


base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
prerequisite-patch-id: 206f940ad5b807994984989e14eb713386bf8b59
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v5 1/4] rv: guard rv.h against double inclusion
  2026-11-27 11:36 [PATCH v5 0/4] rv: per-edge dwell-time statistics for per-cpu monitors Tobias Schaffner
@ 2026-11-27 11:36 ` Tobias Schaffner
  2026-11-27 11:36 ` [PATCH v5 2/4] rv: add per-edge dwell-time statistics Tobias Schaffner
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Tobias Schaffner @ 2026-11-27 11:36 UTC (permalink / raw)
  To: Steven Rostedt, Gabriele Monaco
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Masami Hiramatsu,
	Mathieu Desnoyers, linux-trace-kernel, linux-doc,
	linux-kselftest, linux-kernel, rpm, jan.kiszka, Tobias Schaffner,
	Shuah Khan

rv.h has no include guard. Only three C files include it directly and
none of them twice, but anything that includes it from a header hits
this.

Add the guard, so rv.h can be included from a header as well.

Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
 kernel/trace/rv/rv.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/kernel/trace/rv/rv.h b/kernel/trace/rv/rv.h
index 8e8406b42436..dbcb7a3a41a8 100644
--- a/kernel/trace/rv/rv.h
+++ b/kernel/trace/rv/rv.h
@@ -1,4 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _KERNEL_TRACE_RV_H
+#define _KERNEL_TRACE_RV_H
+
 #include <linux/mutex.h>
 
 struct rv_interface {
@@ -45,3 +48,5 @@ static inline int init_rv_reactors(struct dentry *root_dir)
 	return 0;
 }
 #endif
+
+#endif /* _KERNEL_TRACE_RV_H */
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v5 2/4] rv: add per-edge dwell-time statistics
  2026-11-27 11:36 [PATCH v5 0/4] rv: per-edge dwell-time statistics for per-cpu monitors Tobias Schaffner
  2026-11-27 11:36 ` [PATCH v5 1/4] rv: guard rv.h against double inclusion Tobias Schaffner
@ 2026-11-27 11:36 ` Tobias Schaffner
  2026-11-27 11:36 ` [PATCH v5 3/4] rv: collect per-edge dwell time for per-cpu DA/HA monitors Tobias Schaffner
  2026-11-27 11:36 ` [PATCH v5 4/4] selftests/verification: test per-edge dwell-time statistics Tobias Schaffner
  3 siblings, 0 replies; 8+ messages in thread
From: Tobias Schaffner @ 2026-11-27 11:36 UTC (permalink / raw)
  To: Steven Rostedt, Gabriele Monaco
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Masami Hiramatsu,
	Mathieu Desnoyers, linux-trace-kernel, linux-doc,
	linux-kselftest, linux-kernel, rpm, jan.kiszka, Tobias Schaffner,
	Shuah Khan

Add CONFIG_RV_EDGE_STAT, an optional facility that records how long a
monitor's automaton dwells in a state before leaving it and accounts that
dwell to the edge it leaves by.

Keep the state and its entry timestamp in one word so a transition commits
both with a single cmpxchg and nested transitions cannot charge a dwell to
the wrong edge. This requires a native 64-bit cmpxchg.

The counters are per-CPU and lock-free, so a monitor's hot path can update
them without disabling interrupts and without perturbing the latency being
measured. Only the owning CPU writes them, so a reader snapshots them with
local64_read() with no IPI and no locking on the accounting path.

Storage is a static per-CPU array of STATE_MAX * EVENT_MAX edges per
monitor, exposed through a per-monitor "stats" tracefs file. Report whether
the model defines each edge, so a count on an edge that should never be
taken is visible. Nothing uses the facility yet.

Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
 include/linux/rv.h      |   7 ++
 include/rv/edge_stat.h  | 211 ++++++++++++++++++++++++++++++++++++++++
 kernel/trace/rv/Kconfig |  12 +++
 3 files changed, 230 insertions(+)
 create mode 100644 include/rv/edge_stat.h

diff --git a/include/linux/rv.h b/include/linux/rv.h
index 541ba404926a..bf75cf2300ee 100644
--- a/include/linux/rv.h
+++ b/include/linux/rv.h
@@ -21,6 +21,13 @@
 #include <linux/list.h>
 #include <linux/types.h>
 
+/* Edge statistics pack the state and its entry timestamp into one word. */
+#ifdef CONFIG_RV_EDGE_STAT
+typedef unsigned long da_state_t;
+#else
+typedef unsigned int da_state_t;
+#endif
+
 /*
  * Deterministic automaton per-object variables.
  */
diff --git a/include/rv/edge_stat.h b/include/rv/edge_stat.h
new file mode 100644
index 000000000000..8b99e8520e1e
--- /dev/null
+++ b/include/rv/edge_stat.h
@@ -0,0 +1,211 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Per-edge dwell-time statistics for RV monitors.
+ *
+ * Copyright (C) 2026 Siemens AG
+ * Author: Tobias Schaffner <tobias.schaffner@siemens.com>
+ */
+#ifndef _RV_EDGE_STAT_H
+#define _RV_EDGE_STAT_H
+
+#include <linux/rv.h>
+
+#ifdef CONFIG_RV_EDGE_STAT
+
+#include <linux/args.h>
+#include <linux/atomic.h>
+#include <linux/bitfield.h>
+#include <linux/bug.h>
+#include <linux/compiler.h>
+#include <linux/percpu.h>
+#include <linux/sched/clock.h>
+#include <linux/seq_file.h>
+#include <linux/types.h>
+#include <asm/local64.h>
+#include <rv/automata.h>
+#include "rv.h"
+
+/*
+ * Per-CPU counters kept in local64_t so accounting is safe against interrupt
+ * and NMI nesting on the owning CPU without disabling interrupts. Only the
+ * owning CPU writes.
+ */
+struct rv_edge_stat {
+	local64_t	count;
+	local64_t	sum_ns;
+	local64_t	max_ns;
+};
+
+/*
+ * State and entry timestamp share one word so a transition commits both with
+ * one cmpxchg. Comparing the timestamp also detects nested transitions that
+ * return to the same state.
+ */
+#define RV_STATE_BITS		8
+#define RV_STATE_MASK		GENMASK(RV_STATE_BITS - 1, 0)
+#define RV_TS_MASK		GENMASK(BITS_PER_LONG - 1, RV_STATE_BITS)
+
+#define da_state_of(w)		FIELD_GET(RV_STATE_MASK, w)
+#define da_ts_of(w)		FIELD_GET(RV_TS_MASK, w)
+#define da_state_pack(s, ts)	(FIELD_PREP(RV_TS_MASK, ts) | \
+				 FIELD_PREP(RV_STATE_MASK, s))
+
+static __always_inline
+void rv_edge_stat_account(struct rv_edge_stat *s, u64 dwell_ns)
+{
+	s64 max;
+	int i;
+
+	local64_inc(&s->count);
+	local64_add(dwell_ns, &s->sum_ns);
+
+	/* Keep the largest dwell; bound retries if a nested update races us. */
+	max = local64_read(&s->max_ns);
+	for (i = 0; dwell_ns > (u64)max; i++) {
+		if (i == MAX_DA_RETRY_RACING_EVENTS) {
+			WARN_ONCE(1, "rv: edge-stat max update exceeded %d retries\n",
+				  MAX_DA_RETRY_RACING_EVENTS);
+			break;
+		}
+		if (local64_try_cmpxchg(&s->max_ns, &max, dwell_ns))
+			break;
+	}
+}
+
+static_assert(STATE_MAX <= (1U << RV_STATE_BITS),
+	      "automaton has more states than the packed state word can hold");
+
+#if RV_MON_TYPE == RV_MON_PER_CPU
+
+/*
+ * One edge per (state, event) pair. Static storage avoids allocation lifetime
+ * races. Only the owning CPU writes, so readers can snapshot counters with
+ * local64_read().
+ */
+#define RV_THIS_NR_EDGES	(STATE_MAX * EVENT_MAX)
+
+#define DA_EDGE(state, event)	((state) * EVENT_MAX + (event))
+#define DA_EDGE_STATE(edge)	((edge) / EVENT_MAX)
+#define DA_EDGE_EVENT(edge)	((edge) % EVENT_MAX)
+
+#define DA_MON_EDGES		CONCATENATE(da_mon_edges_, MONITOR_NAME)
+
+static DEFINE_PER_CPU(struct rv_edge_stat[RV_THIS_NR_EDGES], DA_MON_EDGES);
+static struct dentry *rv_this_stats_file;
+
+/* Called before tracepoints are registered, so memset cannot race an update. */
+static void rv_edge_stats_reset(void)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu)
+		memset(per_cpu_ptr(DA_MON_EDGES, cpu), 0, sizeof(DA_MON_EDGES));
+}
+
+static int rv_edge_stats_show(struct seq_file *seq, void *v)
+{
+	unsigned int e;
+	int cpu;
+
+	seq_puts(seq, "# cpu edge label valid count sum_ns max_ns\n");
+	/* Include offline CPUs and keep output stable across CPU hotplug. */
+	for_each_possible_cpu(cpu) {
+		struct rv_edge_stat *edges = per_cpu_ptr(DA_MON_EDGES, cpu);
+
+		for (e = 0; e < RV_THIS_NR_EDGES; e++) {
+			enum states state = DA_EDGE_STATE(e);
+			enum events event = DA_EDGE_EVENT(e);
+
+			/* Edges the model does not define must stay at zero. */
+			seq_printf(seq, "%d %u %s:%s %d %llu %llu %llu\n",
+				   cpu, e,
+				   model_get_state_name(state),
+				   model_get_event_name(event),
+				   model_get_next_state(state, event) != INVALID_STATE,
+				   (u64)local64_read(&edges[e].count),
+				   (u64)local64_read(&edges[e].sum_ns),
+				   (u64)local64_read(&edges[e].max_ns));
+		}
+	}
+	return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(rv_edge_stats);
+
+static int rv_edge_stats_create(struct dentry *parent)
+{
+	rv_edge_stats_reset();
+	rv_this_stats_file = rv_create_file("stats", RV_MODE_READ, parent,
+					    NULL, &rv_edge_stats_fops);
+	return rv_this_stats_file ? 0 : -ENOMEM;
+}
+
+static void rv_edge_stats_remove(void)
+{
+	rv_remove(rv_this_stats_file);
+	rv_this_stats_file = NULL;
+}
+
+/* A zero timestamp marks resets, which can run on a different CPU. */
+#define da_state_entered(s)	da_state_pack((s), local_clock())
+
+static __always_inline void
+rv_edge_account(da_state_t old, da_state_t new, enum states curr, enum events ev)
+{
+	struct rv_edge_stat *edges;
+	u64 prev = da_ts_of(old);
+
+	if (unlikely(!prev))
+		return;
+
+	edges = this_cpu_ptr(DA_MON_EDGES);
+	rv_edge_stat_account(&edges[DA_EDGE(curr, ev)],
+			     (da_ts_of(new) - prev) & FIELD_MAX(RV_TS_MASK));
+}
+
+#else /* per-cpu accounting off for this monitor type */
+
+#define da_state_entered(s)	((da_state_t)(s))
+
+static __always_inline void
+rv_edge_account(da_state_t old, da_state_t new, enum states curr, enum events ev) { }
+
+#endif /* RV_MON_TYPE == RV_MON_PER_CPU */
+
+static __always_inline bool
+da_state_try_commit(da_state_t *word, da_state_t *old, enum states next,
+		    enum events event)
+{
+	da_state_t prev = *old;
+	da_state_t new = da_state_entered(next);
+
+	if (!try_cmpxchg(word, old, new))
+		return false;
+
+	rv_edge_account(prev, new, da_state_of(prev), event);
+	return true;
+}
+
+#else /* !CONFIG_RV_EDGE_STAT */
+
+/*
+ * Feature off: only the hooks the DA/HA layer calls are provided, as trivial
+ * pass-throughs. da_state_t is a plain state word with no packed timestamp.
+ */
+#define da_state_of(w)		(w)
+#define da_state_entered(s)	(s)
+
+static __always_inline bool
+da_state_try_commit(da_state_t *word, da_state_t *old, enum states next,
+		    enum events event)
+{
+	return try_cmpxchg(word, old, next);
+}
+
+#if RV_MON_TYPE == RV_MON_PER_CPU
+static inline int rv_edge_stats_create(struct dentry *parent) { return 0; }
+static inline void rv_edge_stats_remove(void) { }
+#endif
+
+#endif /* CONFIG_RV_EDGE_STAT */
+
+#endif /* _RV_EDGE_STAT_H */
diff --git a/kernel/trace/rv/Kconfig b/kernel/trace/rv/Kconfig
index efa930f94ea4..1de88081fd2a 100644
--- a/kernel/trace/rv/Kconfig
+++ b/kernel/trace/rv/Kconfig
@@ -126,3 +126,15 @@ config RV_MONITORS_KUNIT_TEST
 	  unrelated KUnit test is running.
 
 	  If unsure, say N.
+
+config RV_EDGE_STAT
+	bool "Per-edge dwell-time statistics"
+	depends on RV && 64BIT && DA_MON_EVENTS_IMPLICIT
+	help
+	  Record per-edge dwell-time statistics for per-cpu deterministic and
+	  hybrid automaton monitors and expose them through a per-monitor
+	  "stats" tracefs file. This times each monitored automaton transition
+	  with local_clock(), so leave it off if you do not need the
+	  statistics.
+
+	  If unsure, say N.
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v5 3/4] rv: collect per-edge dwell time for per-cpu DA/HA monitors
  2026-11-27 11:36 [PATCH v5 0/4] rv: per-edge dwell-time statistics for per-cpu monitors Tobias Schaffner
  2026-11-27 11:36 ` [PATCH v5 1/4] rv: guard rv.h against double inclusion Tobias Schaffner
  2026-11-27 11:36 ` [PATCH v5 2/4] rv: add per-edge dwell-time statistics Tobias Schaffner
@ 2026-11-27 11:36 ` Tobias Schaffner
  2026-11-27 13:57   ` Gabriele Monaco
  2026-11-27 11:36 ` [PATCH v5 4/4] selftests/verification: test per-edge dwell-time statistics Tobias Schaffner
  3 siblings, 1 reply; 8+ messages in thread
From: Tobias Schaffner @ 2026-11-27 11:36 UTC (permalink / raw)
  To: Steven Rostedt, Gabriele Monaco
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Masami Hiramatsu,
	Mathieu Desnoyers, linux-trace-kernel, linux-doc,
	linux-kselftest, linux-kernel, rpm, jan.kiszka, Tobias Schaffner,
	Shuah Khan

Hook the edge-stat facility into the DA/HA layer, so that any per-cpu
automaton monitor also reports how long it lingers in each state.

Account a transition as soon as its state change is committed. An HA
constraint that rejects the transition can therefore reset the monitor
without leaving an accounting window for nested events.

Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
 .../trace/rv/runtime-verification.rst         | 30 ++++++++++++++
 include/linux/rv.h                            |  2 +-
 include/rv/da_monitor.h                       | 40 +++++++++++++++----
 include/rv/edge_stat.h                        |  5 ++-
 include/rv/ha_monitor.h                       |  2 +-
 5 files changed, 69 insertions(+), 10 deletions(-)

diff --git a/Documentation/trace/rv/runtime-verification.rst b/Documentation/trace/rv/runtime-verification.rst
index c700dde9259c..ac16b5532e86 100644
--- a/Documentation/trace/rv/runtime-verification.rst
+++ b/Documentation/trace/rv/runtime-verification.rst
@@ -229,3 +229,33 @@ For example::
    nop
    [panic]
    printk
+
+**monitors/MONITOR/stats**
+
+Present only when the kernel is built with CONFIG_RV_EDGE_STAT=y and *MONITOR*
+is a per-cpu deterministic or hybrid automaton monitor. The file exists while
+the monitor is enabled and reports how long the automaton dwells in each state
+before leaving it, timed with local_clock() and accounted per outgoing edge and
+per CPU.
+
+- The first line is a header naming the columns.
+- Each following line describes one edge on one CPU::
+
+   cpu edge label valid count sum_ns max_ns
+
+  *label* is "state:event", *valid* is 1 if the model defines the edge and 0
+  otherwise, *count* is the number of times the transition was committed, and
+  *sum_ns* and *max_ns* are the total and worst dwell in nanoseconds. An
+  invalid edge always reports a zero count.
+
+The counters are reset each time the monitor is enabled. A monitor that starts
+on an event leaving its initial state does not know since when it was in that
+state, so this first transition is neither counted nor timed.
+
+For example::
+
+   # cat monitors/wip/stats
+   # cpu edge label valid count sum_ns max_ns
+   0 0 preemptive:preempt_disable 1 4210 95501200 183200
+   0 1 preemptive:preempt_enable 0 0 0 0
+   0 4 non_preemptive:preempt_enable 1 4208 3812900 42600
diff --git a/include/linux/rv.h b/include/linux/rv.h
index bf75cf2300ee..d83d1a98344e 100644
--- a/include/linux/rv.h
+++ b/include/linux/rv.h
@@ -33,7 +33,7 @@ typedef unsigned int da_state_t;
  */
 struct da_monitor {
 	bool		monitoring;
-	unsigned int	curr_state;
+	da_state_t	curr_state;
 };
 
 #ifdef CONFIG_RV_LTL_MONITOR
diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h
index 6fc5ef8142ac..942b35428ccd 100644
--- a/include/rv/da_monitor.h
+++ b/include/rv/da_monitor.h
@@ -17,6 +17,7 @@
 #include <rv/automata.h>
 #include <linux/rv.h>
 #include <rv/kunit.h>
+#include <rv/edge_stat.h>
 #include <linux/stringify.h>
 #include <linux/bug.h>
 #include <linux/sched.h>
@@ -105,6 +106,14 @@ static inline void da_monitor_reset(struct da_monitor *da_mon)
 	da_monitor_reset_state(da_mon);
 }
 
+static inline void __da_monitor_start(struct da_monitor *da_mon, da_state_t state)
+{
+	da_mon->curr_state = state;
+	da_monitor_init_hook(da_mon);
+	/* Pairs with smp_load_acquire in da_monitoring(). */
+	smp_store_release(&da_mon->monitoring, 1);
+}
+
 /*
  * da_monitor_start - start monitoring
  *
@@ -113,10 +122,18 @@ static inline void da_monitor_reset(struct da_monitor *da_mon)
  */
 static inline void da_monitor_start(struct da_monitor *da_mon)
 {
-	da_mon->curr_state = model_get_initial_state();
-	da_monitor_init_hook(da_mon);
-	/* Pairs with smp_load_acquire in da_monitoring(). */
-	smp_store_release(&da_mon->monitoring, 1);
+	__da_monitor_start(da_mon, da_state_entered(model_get_initial_state()));
+}
+
+/*
+ * da_monitor_start_unaccounted - start monitoring without accounting
+ *
+ * Like da_monitor_start(), for a monitor that has been in its initial state
+ * since an unknown time. The first transition is neither counted nor timed.
+ */
+static inline void da_monitor_start_unaccounted(struct da_monitor *da_mon)
+{
+	__da_monitor_start(da_mon, model_get_initial_state());
 }
 
 /*
@@ -276,6 +293,11 @@ static inline void da_monitor_reset_state_all(void)
  */
 static inline int da_monitor_init(void)
 {
+	int retval = rv_edge_stats_create(rv_this.root_d);
+
+	if (retval)
+		return retval;
+
 	da_monitor_reset_state_all();
 	return 0;
 }
@@ -287,6 +309,7 @@ static inline void da_monitor_destroy(void)
 {
 	da_monitor_reset_all();
 	da_monitor_sync_hook();
+	rv_edge_stats_remove();
 }
 
 #ifndef da_implicit_guard
@@ -689,9 +712,11 @@ static inline void da_trace_error(struct da_monitor *da_mon,
 static inline bool da_event(struct da_monitor *da_mon, enum events event, da_id_type id)
 {
 	enum states curr_state, next_state;
+	da_state_t old;
 
-	curr_state = READ_ONCE(da_mon->curr_state);
+	old = READ_ONCE(da_mon->curr_state);
 	for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) {
+		curr_state = da_state_of(old);
 		next_state = model_get_next_state(curr_state, event);
 		if (next_state == INVALID_STATE) {
 			react(curr_state, event);
@@ -699,7 +724,8 @@ static inline bool da_event(struct da_monitor *da_mon, enum events event, da_id_
 				       model_get_event_name(event), id);
 			return false;
 		}
-		if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) {
+		if (likely(da_state_try_commit(&da_mon->curr_state, &old,
+					       next_state, event))) {
 			if (!da_monitor_event_hook(da_mon, curr_state, event, next_state, id))
 				return false;
 			da_trace_event(da_mon, model_get_state_name(curr_state),
@@ -752,7 +778,7 @@ static inline bool __da_handle_start_run_event(struct da_monitor *da_mon,
 	if (!da_monitor_enabled())
 		return 0;
 	if (unlikely(!da_monitoring(da_mon)))
-		da_monitor_start(da_mon);
+		da_monitor_start_unaccounted(da_mon);
 
 	__da_handle_event_common(da_mon, event, id);
 
diff --git a/include/rv/edge_stat.h b/include/rv/edge_stat.h
index 8b99e8520e1e..16c28aedabdd 100644
--- a/include/rv/edge_stat.h
+++ b/include/rv/edge_stat.h
@@ -145,7 +145,10 @@ static void rv_edge_stats_remove(void)
 	rv_this_stats_file = NULL;
 }
 
-/* A zero timestamp marks resets, which can run on a different CPU. */
+/*
+ * A zero timestamp marks an unknown entry time: resets can run on a different
+ * CPU, and da_handle_start_run_event() starts in a state entered earlier.
+ */
 #define da_state_entered(s)	da_state_pack((s), local_clock())
 
 static __always_inline void
diff --git a/include/rv/ha_monitor.h b/include/rv/ha_monitor.h
index 6e1c7fe5449a..78d1bb80aefd 100644
--- a/include/rv/ha_monitor.h
+++ b/include/rv/ha_monitor.h
@@ -312,7 +312,7 @@ static inline void __ha_monitor_timer_callback(struct ha_monitor *ha_mon)
 	if (unlikely(READ_ONCE(ha_mon_destroying)))
 		return;
 	/* Ensure consistent curr_state if we race with da_monitor_reset */
-	curr_state = smp_load_acquire(&ha_mon->da_mon.curr_state);
+	curr_state = da_state_of(smp_load_acquire(&ha_mon->da_mon.curr_state));
 	if (unlikely(!da_monitor_handling_event(&ha_mon->da_mon)))
 		return;
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v5 4/4] selftests/verification: test per-edge dwell-time statistics
  2026-11-27 11:36 [PATCH v5 0/4] rv: per-edge dwell-time statistics for per-cpu monitors Tobias Schaffner
                   ` (2 preceding siblings ...)
  2026-11-27 11:36 ` [PATCH v5 3/4] rv: collect per-edge dwell time for per-cpu DA/HA monitors Tobias Schaffner
@ 2026-11-27 11:36 ` Tobias Schaffner
  3 siblings, 0 replies; 8+ messages in thread
From: Tobias Schaffner @ 2026-11-27 11:36 UTC (permalink / raw)
  To: Steven Rostedt, Gabriele Monaco
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Masami Hiramatsu,
	Mathieu Desnoyers, linux-trace-kernel, linux-doc,
	linux-kselftest, linux-kernel, rpm, jan.kiszka, Tobias Schaffner,
	Shuah Khan

Enable a per-cpu monitor, check that the stats file appears and is
populated under load, and skip cleanly when CONFIG_RV_EDGE_STAT is off.

Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
 tools/testing/selftests/verification/config   |  3 ++
 .../verification/test.d/rv_edge_stats.tc      | 37 +++++++++++++++++++
 2 files changed, 40 insertions(+)
 create mode 100644 tools/testing/selftests/verification/test.d/rv_edge_stats.tc

diff --git a/tools/testing/selftests/verification/config b/tools/testing/selftests/verification/config
index 43072c1c38f4..caf3a96ac8b4 100644
--- a/tools/testing/selftests/verification/config
+++ b/tools/testing/selftests/verification/config
@@ -1 +1,4 @@
 CONFIG_RV=y
+CONFIG_PREEMPT_TRACER=y
+CONFIG_RV_MON_WIP=y
+CONFIG_RV_EDGE_STAT=y
diff --git a/tools/testing/selftests/verification/test.d/rv_edge_stats.tc b/tools/testing/selftests/verification/test.d/rv_edge_stats.tc
new file mode 100644
index 000000000000..c03ed6682b80
--- /dev/null
+++ b/tools/testing/selftests/verification/test.d/rv_edge_stats.tc
@@ -0,0 +1,37 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# description: Test per-edge dwell-time statistics (stats)
+
+MON=wip
+
+check_requires "$MON:monitor"
+
+# The "stats" file is created on enable; if it is still missing the kernel was
+# built without CONFIG_RV_EDGE_STAT, so skip.
+echo 1 > "monitors/$MON/enable"
+if [ ! -e "monitors/$MON/stats" ]; then
+	echo 0 > "monitors/$MON/enable"
+	echo "CONFIG_RV_EDGE_STAT is not enabled."
+	exit_unsupported
+fi
+
+# The first line is the column header; the body has one line per (cpu, edge).
+head -n1 "monitors/$MON/stats" | grep -q "^# cpu edge label valid count sum_ns max_ns"
+[ "$(grep -cvE '^#' "monitors/$MON/stats")" -gt 0 ]
+
+# Drive some scheduler activity so the automaton records transitions.
+for _ in 1 2 3 4 5 6 7 8 9 10; do
+	(true) &
+	wait
+done
+
+# At least one valid edge must now show a non-zero count. Do not require a
+# non-zero dwell: a coarse local_clock() may legitimately return the same
+# value twice.
+grep -vE '^#' "monitors/$MON/stats" | \
+	awk '$4 == 1 && $5 > 0 && $6 >= $7 { hit = 1 } END { exit !hit }'
+
+# An edge the model does not define must never be taken.
+grep -vE '^#' "monitors/$MON/stats" | awk '$4 == 0 && $5 > 0 { exit 1 }'
+
+echo 0 > "monitors/$MON/enable"
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v5 3/4] rv: collect per-edge dwell time for per-cpu DA/HA monitors
  2026-11-27 11:36 ` [PATCH v5 3/4] rv: collect per-edge dwell time for per-cpu DA/HA monitors Tobias Schaffner
@ 2026-11-27 13:57   ` Gabriele Monaco
  2026-11-27 14:13     ` Tobias Schaffner
  0 siblings, 1 reply; 8+ messages in thread
From: Gabriele Monaco @ 2026-11-27 13:57 UTC (permalink / raw)
  To: Tobias Schaffner, Steven Rostedt
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Masami Hiramatsu,
	Mathieu Desnoyers, linux-trace-kernel, linux-doc,
	linux-kselftest, linux-kernel, rpm, jan.kiszka, Shuah Khan

On Fri, 2026-11-27 at 12:36 +0100, Tobias Schaffner wrote:
> Hook the edge-stat facility into the DA/HA layer, so that any per-cpu
> automaton monitor also reports how long it lingers in each state.
> 
> Account a transition as soon as its state change is committed. An HA
> constraint that rejects the transition can therefore reset the monitor
> without leaving an accounting window for nested events.
> 
> Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
> ---

Looks good to me, thanks!
I'm taking the series.

Reviewed-by: Gabriele Monaco <gmonaco@redhat.com>

>  .../trace/rv/runtime-verification.rst         | 30 ++++++++++++++
>  include/linux/rv.h                            |  2 +-
>  include/rv/da_monitor.h                       | 40 +++++++++++++++----
>  include/rv/edge_stat.h                        |  5 ++-
>  include/rv/ha_monitor.h                       |  2 +-
>  5 files changed, 69 insertions(+), 10 deletions(-)
> 
> diff --git a/Documentation/trace/rv/runtime-verification.rst
> b/Documentation/trace/rv/runtime-verification.rst
> index c700dde9259c..ac16b5532e86 100644
> --- a/Documentation/trace/rv/runtime-verification.rst
> +++ b/Documentation/trace/rv/runtime-verification.rst
> @@ -229,3 +229,33 @@ For example::
>     nop
>     [panic]
>     printk
> +
> +**monitors/MONITOR/stats**
> +
> +Present only when the kernel is built with CONFIG_RV_EDGE_STAT=y and
> *MONITOR*
> +is a per-cpu deterministic or hybrid automaton monitor. The file exists while
> +the monitor is enabled and reports how long the automaton dwells in each
> state
> +before leaving it, timed with local_clock() and accounted per outgoing edge
> and
> +per CPU.
> +
> +- The first line is a header naming the columns.
> +- Each following line describes one edge on one CPU::
> +
> +   cpu edge label valid count sum_ns max_ns
> +
> +  *label* is "state:event", *valid* is 1 if the model defines the edge and 0
> +  otherwise, *count* is the number of times the transition was committed, and
> +  *sum_ns* and *max_ns* are the total and worst dwell in nanoseconds. An
> +  invalid edge always reports a zero count.
> +
> +The counters are reset each time the monitor is enabled. A monitor that
> starts
> +on an event leaving its initial state does not know since when it was in that
> +state, so this first transition is neither counted nor timed.
> +
> +For example::
> +
> +   # cat monitors/wip/stats
> +   # cpu edge label valid count sum_ns max_ns
> +   0 0 preemptive:preempt_disable 1 4210 95501200 183200
> +   0 1 preemptive:preempt_enable 0 0 0 0
> +   0 4 non_preemptive:preempt_enable 1 4208 3812900 42600
> diff --git a/include/linux/rv.h b/include/linux/rv.h
> index bf75cf2300ee..d83d1a98344e 100644
> --- a/include/linux/rv.h
> +++ b/include/linux/rv.h
> @@ -33,7 +33,7 @@ typedef unsigned int da_state_t;
>   */
>  struct da_monitor {
>  	bool		monitoring;
> -	unsigned int	curr_state;
> +	da_state_t	curr_state;
>  };
>  
>  #ifdef CONFIG_RV_LTL_MONITOR
> diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h
> index 6fc5ef8142ac..942b35428ccd 100644
> --- a/include/rv/da_monitor.h
> +++ b/include/rv/da_monitor.h
> @@ -17,6 +17,7 @@
>  #include <rv/automata.h>
>  #include <linux/rv.h>
>  #include <rv/kunit.h>
> +#include <rv/edge_stat.h>
>  #include <linux/stringify.h>
>  #include <linux/bug.h>
>  #include <linux/sched.h>
> @@ -105,6 +106,14 @@ static inline void da_monitor_reset(struct da_monitor
> *da_mon)
>  	da_monitor_reset_state(da_mon);
>  }
>  
> +static inline void __da_monitor_start(struct da_monitor *da_mon, da_state_t
> state)
> +{
> +	da_mon->curr_state = state;
> +	da_monitor_init_hook(da_mon);
> +	/* Pairs with smp_load_acquire in da_monitoring(). */
> +	smp_store_release(&da_mon->monitoring, 1);
> +}
> +
>  /*
>   * da_monitor_start - start monitoring
>   *
> @@ -113,10 +122,18 @@ static inline void da_monitor_reset(struct da_monitor
> *da_mon)
>   */
>  static inline void da_monitor_start(struct da_monitor *da_mon)
>  {
> -	da_mon->curr_state = model_get_initial_state();
> -	da_monitor_init_hook(da_mon);
> -	/* Pairs with smp_load_acquire in da_monitoring(). */
> -	smp_store_release(&da_mon->monitoring, 1);
> +	__da_monitor_start(da_mon,
> da_state_entered(model_get_initial_state()));
> +}
> +
> +/*
> + * da_monitor_start_unaccounted - start monitoring without accounting
> + *
> + * Like da_monitor_start(), for a monitor that has been in its initial state
> + * since an unknown time. The first transition is neither counted nor timed.
> + */
> +static inline void da_monitor_start_unaccounted(struct da_monitor *da_mon)
> +{
> +	__da_monitor_start(da_mon, model_get_initial_state());
>  }
>  
>  /*
> @@ -276,6 +293,11 @@ static inline void da_monitor_reset_state_all(void)
>   */
>  static inline int da_monitor_init(void)
>  {
> +	int retval = rv_edge_stats_create(rv_this.root_d);
> +
> +	if (retval)
> +		return retval;
> +
>  	da_monitor_reset_state_all();
>  	return 0;
>  }
> @@ -287,6 +309,7 @@ static inline void da_monitor_destroy(void)
>  {
>  	da_monitor_reset_all();
>  	da_monitor_sync_hook();
> +	rv_edge_stats_remove();
>  }
>  
>  #ifndef da_implicit_guard
> @@ -689,9 +712,11 @@ static inline void da_trace_error(struct da_monitor
> *da_mon,
>  static inline bool da_event(struct da_monitor *da_mon, enum events event,
> da_id_type id)
>  {
>  	enum states curr_state, next_state;
> +	da_state_t old;
>  
> -	curr_state = READ_ONCE(da_mon->curr_state);
> +	old = READ_ONCE(da_mon->curr_state);
>  	for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) {
> +		curr_state = da_state_of(old);
>  		next_state = model_get_next_state(curr_state, event);
>  		if (next_state == INVALID_STATE) {
>  			react(curr_state, event);
> @@ -699,7 +724,8 @@ static inline bool da_event(struct da_monitor *da_mon,
> enum events event, da_id_
>  				       model_get_event_name(event), id);
>  			return false;
>  		}
> -		if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state,
> next_state))) {
> +		if (likely(da_state_try_commit(&da_mon->curr_state, &old,
> +					       next_state, event))) {
>  			if (!da_monitor_event_hook(da_mon, curr_state, event,
> next_state, id))
>  				return false;
>  			da_trace_event(da_mon,
> model_get_state_name(curr_state),
> @@ -752,7 +778,7 @@ static inline bool __da_handle_start_run_event(struct
> da_monitor *da_mon,
>  	if (!da_monitor_enabled())
>  		return 0;
>  	if (unlikely(!da_monitoring(da_mon)))
> -		da_monitor_start(da_mon);
> +		da_monitor_start_unaccounted(da_mon);
>  
>  	__da_handle_event_common(da_mon, event, id);
>  
> diff --git a/include/rv/edge_stat.h b/include/rv/edge_stat.h
> index 8b99e8520e1e..16c28aedabdd 100644
> --- a/include/rv/edge_stat.h
> +++ b/include/rv/edge_stat.h
> @@ -145,7 +145,10 @@ static void rv_edge_stats_remove(void)
>  	rv_this_stats_file = NULL;
>  }
>  
> -/* A zero timestamp marks resets, which can run on a different CPU. */
> +/*
> + * A zero timestamp marks an unknown entry time: resets can run on a
> different
> + * CPU, and da_handle_start_run_event() starts in a state entered earlier.
> + */
>  #define da_state_entered(s)	da_state_pack((s), local_clock())
>  
>  static __always_inline void
> diff --git a/include/rv/ha_monitor.h b/include/rv/ha_monitor.h
> index 6e1c7fe5449a..78d1bb80aefd 100644
> --- a/include/rv/ha_monitor.h
> +++ b/include/rv/ha_monitor.h
> @@ -312,7 +312,7 @@ static inline void __ha_monitor_timer_callback(struct
> ha_monitor *ha_mon)
>  	if (unlikely(READ_ONCE(ha_mon_destroying)))
>  		return;
>  	/* Ensure consistent curr_state if we race with da_monitor_reset */
> -	curr_state = smp_load_acquire(&ha_mon->da_mon.curr_state);
> +	curr_state = da_state_of(smp_load_acquire(&ha_mon-
> >da_mon.curr_state));
>  	if (unlikely(!da_monitor_handling_event(&ha_mon->da_mon)))
>  		return;
>  


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v5 3/4] rv: collect per-edge dwell time for per-cpu DA/HA monitors
  2026-11-27 13:57   ` Gabriele Monaco
@ 2026-11-27 14:13     ` Tobias Schaffner
  2026-11-27 14:36       ` Gabriele Monaco
  0 siblings, 1 reply; 8+ messages in thread
From: Tobias Schaffner @ 2026-11-27 14:13 UTC (permalink / raw)
  To: Gabriele Monaco, Steven Rostedt
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Masami Hiramatsu,
	Mathieu Desnoyers, linux-trace-kernel, linux-doc,
	linux-kselftest, linux-kernel, rpm, jan.kiszka, Shuah Khan

On 11/27/26 14:57, Gabriele Monaco wrote:
> On Fri, 2026-11-27 at 12:36 +0100, Tobias Schaffner wrote:
>> Hook the edge-stat facility into the DA/HA layer, so that any per-cpu
>> automaton monitor also reports how long it lingers in each state.
>>
>> Account a transition as soon as its state change is committed. An HA
>> constraint that rejects the transition can therefore reset the monitor
>> without leaving an accounting window for nested events.
>>
>> Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
>> ---
> 
> Looks good to me, thanks!
> I'm taking the series.

Thanks for all the support! I will continue with the missing bits for
per_task and maybe also per-object and global monitors soon.

Best,
Tobias

> Reviewed-by: Gabriele Monaco <gmonaco@redhat.com>
> 
>>   .../trace/rv/runtime-verification.rst         | 30 ++++++++++++++
>>   include/linux/rv.h                            |  2 +-
>>   include/rv/da_monitor.h                       | 40 +++++++++++++++----
>>   include/rv/edge_stat.h                        |  5 ++-
>>   include/rv/ha_monitor.h                       |  2 +-
>>   5 files changed, 69 insertions(+), 10 deletions(-)
>>
>> diff --git a/Documentation/trace/rv/runtime-verification.rst
>> b/Documentation/trace/rv/runtime-verification.rst
>> index c700dde9259c..ac16b5532e86 100644
>> --- a/Documentation/trace/rv/runtime-verification.rst
>> +++ b/Documentation/trace/rv/runtime-verification.rst
>> @@ -229,3 +229,33 @@ For example::
>>      nop
>>      [panic]
>>      printk
>> +
>> +**monitors/MONITOR/stats**
>> +
>> +Present only when the kernel is built with CONFIG_RV_EDGE_STAT=y and
>> *MONITOR*
>> +is a per-cpu deterministic or hybrid automaton monitor. The file exists while
>> +the monitor is enabled and reports how long the automaton dwells in each
>> state
>> +before leaving it, timed with local_clock() and accounted per outgoing edge
>> and
>> +per CPU.
>> +
>> +- The first line is a header naming the columns.
>> +- Each following line describes one edge on one CPU::
>> +
>> +   cpu edge label valid count sum_ns max_ns
>> +
>> +  *label* is "state:event", *valid* is 1 if the model defines the edge and 0
>> +  otherwise, *count* is the number of times the transition was committed, and
>> +  *sum_ns* and *max_ns* are the total and worst dwell in nanoseconds. An
>> +  invalid edge always reports a zero count.
>> +
>> +The counters are reset each time the monitor is enabled. A monitor that
>> starts
>> +on an event leaving its initial state does not know since when it was in that
>> +state, so this first transition is neither counted nor timed.
>> +
>> +For example::
>> +
>> +   # cat monitors/wip/stats
>> +   # cpu edge label valid count sum_ns max_ns
>> +   0 0 preemptive:preempt_disable 1 4210 95501200 183200
>> +   0 1 preemptive:preempt_enable 0 0 0 0
>> +   0 4 non_preemptive:preempt_enable 1 4208 3812900 42600
>> diff --git a/include/linux/rv.h b/include/linux/rv.h
>> index bf75cf2300ee..d83d1a98344e 100644
>> --- a/include/linux/rv.h
>> +++ b/include/linux/rv.h
>> @@ -33,7 +33,7 @@ typedef unsigned int da_state_t;
>>    */
>>   struct da_monitor {
>>   	bool		monitoring;
>> -	unsigned int	curr_state;
>> +	da_state_t	curr_state;
>>   };
>>   
>>   #ifdef CONFIG_RV_LTL_MONITOR
>> diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h
>> index 6fc5ef8142ac..942b35428ccd 100644
>> --- a/include/rv/da_monitor.h
>> +++ b/include/rv/da_monitor.h
>> @@ -17,6 +17,7 @@
>>   #include <rv/automata.h>
>>   #include <linux/rv.h>
>>   #include <rv/kunit.h>
>> +#include <rv/edge_stat.h>
>>   #include <linux/stringify.h>
>>   #include <linux/bug.h>
>>   #include <linux/sched.h>
>> @@ -105,6 +106,14 @@ static inline void da_monitor_reset(struct da_monitor
>> *da_mon)
>>   	da_monitor_reset_state(da_mon);
>>   }
>>   
>> +static inline void __da_monitor_start(struct da_monitor *da_mon, da_state_t
>> state)
>> +{
>> +	da_mon->curr_state = state;
>> +	da_monitor_init_hook(da_mon);
>> +	/* Pairs with smp_load_acquire in da_monitoring(). */
>> +	smp_store_release(&da_mon->monitoring, 1);
>> +}
>> +
>>   /*
>>    * da_monitor_start - start monitoring
>>    *
>> @@ -113,10 +122,18 @@ static inline void da_monitor_reset(struct da_monitor
>> *da_mon)
>>    */
>>   static inline void da_monitor_start(struct da_monitor *da_mon)
>>   {
>> -	da_mon->curr_state = model_get_initial_state();
>> -	da_monitor_init_hook(da_mon);
>> -	/* Pairs with smp_load_acquire in da_monitoring(). */
>> -	smp_store_release(&da_mon->monitoring, 1);
>> +	__da_monitor_start(da_mon,
>> da_state_entered(model_get_initial_state()));
>> +}
>> +
>> +/*
>> + * da_monitor_start_unaccounted - start monitoring without accounting
>> + *
>> + * Like da_monitor_start(), for a monitor that has been in its initial state
>> + * since an unknown time. The first transition is neither counted nor timed.
>> + */
>> +static inline void da_monitor_start_unaccounted(struct da_monitor *da_mon)
>> +{
>> +	__da_monitor_start(da_mon, model_get_initial_state());
>>   }
>>   
>>   /*
>> @@ -276,6 +293,11 @@ static inline void da_monitor_reset_state_all(void)
>>    */
>>   static inline int da_monitor_init(void)
>>   {
>> +	int retval = rv_edge_stats_create(rv_this.root_d);
>> +
>> +	if (retval)
>> +		return retval;
>> +
>>   	da_monitor_reset_state_all();
>>   	return 0;
>>   }
>> @@ -287,6 +309,7 @@ static inline void da_monitor_destroy(void)
>>   {
>>   	da_monitor_reset_all();
>>   	da_monitor_sync_hook();
>> +	rv_edge_stats_remove();
>>   }
>>   
>>   #ifndef da_implicit_guard
>> @@ -689,9 +712,11 @@ static inline void da_trace_error(struct da_monitor
>> *da_mon,
>>   static inline bool da_event(struct da_monitor *da_mon, enum events event,
>> da_id_type id)
>>   {
>>   	enum states curr_state, next_state;
>> +	da_state_t old;
>>   
>> -	curr_state = READ_ONCE(da_mon->curr_state);
>> +	old = READ_ONCE(da_mon->curr_state);
>>   	for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) {
>> +		curr_state = da_state_of(old);
>>   		next_state = model_get_next_state(curr_state, event);
>>   		if (next_state == INVALID_STATE) {
>>   			react(curr_state, event);
>> @@ -699,7 +724,8 @@ static inline bool da_event(struct da_monitor *da_mon,
>> enum events event, da_id_
>>   				       model_get_event_name(event), id);
>>   			return false;
>>   		}
>> -		if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state,
>> next_state))) {
>> +		if (likely(da_state_try_commit(&da_mon->curr_state, &old,
>> +					       next_state, event))) {
>>   			if (!da_monitor_event_hook(da_mon, curr_state, event,
>> next_state, id))
>>   				return false;
>>   			da_trace_event(da_mon,
>> model_get_state_name(curr_state),
>> @@ -752,7 +778,7 @@ static inline bool __da_handle_start_run_event(struct
>> da_monitor *da_mon,
>>   	if (!da_monitor_enabled())
>>   		return 0;
>>   	if (unlikely(!da_monitoring(da_mon)))
>> -		da_monitor_start(da_mon);
>> +		da_monitor_start_unaccounted(da_mon);
>>   
>>   	__da_handle_event_common(da_mon, event, id);
>>   
>> diff --git a/include/rv/edge_stat.h b/include/rv/edge_stat.h
>> index 8b99e8520e1e..16c28aedabdd 100644
>> --- a/include/rv/edge_stat.h
>> +++ b/include/rv/edge_stat.h
>> @@ -145,7 +145,10 @@ static void rv_edge_stats_remove(void)
>>   	rv_this_stats_file = NULL;
>>   }
>>   
>> -/* A zero timestamp marks resets, which can run on a different CPU. */
>> +/*
>> + * A zero timestamp marks an unknown entry time: resets can run on a
>> different
>> + * CPU, and da_handle_start_run_event() starts in a state entered earlier.
>> + */
>>   #define da_state_entered(s)	da_state_pack((s), local_clock())
>>   
>>   static __always_inline void
>> diff --git a/include/rv/ha_monitor.h b/include/rv/ha_monitor.h
>> index 6e1c7fe5449a..78d1bb80aefd 100644
>> --- a/include/rv/ha_monitor.h
>> +++ b/include/rv/ha_monitor.h
>> @@ -312,7 +312,7 @@ static inline void __ha_monitor_timer_callback(struct
>> ha_monitor *ha_mon)
>>   	if (unlikely(READ_ONCE(ha_mon_destroying)))
>>   		return;
>>   	/* Ensure consistent curr_state if we race with da_monitor_reset */
>> -	curr_state = smp_load_acquire(&ha_mon->da_mon.curr_state);
>> +	curr_state = da_state_of(smp_load_acquire(&ha_mon-
>>> da_mon.curr_state));
>>   	if (unlikely(!da_monitor_handling_event(&ha_mon->da_mon)))
>>   		return;
>>   
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v5 3/4] rv: collect per-edge dwell time for per-cpu DA/HA monitors
  2026-11-27 14:13     ` Tobias Schaffner
@ 2026-11-27 14:36       ` Gabriele Monaco
  0 siblings, 0 replies; 8+ messages in thread
From: Gabriele Monaco @ 2026-11-27 14:36 UTC (permalink / raw)
  To: Tobias Schaffner, Steven Rostedt
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Masami Hiramatsu,
	Mathieu Desnoyers, linux-trace-kernel, linux-doc,
	linux-kselftest, linux-kernel, rpm, jan.kiszka, Shuah Khan

On Fri, 2026-11-27 at 15:13 +0100, Tobias Schaffner wrote:
> On 11/27/26 14:57, Gabriele Monaco wrote:
> > On Fri, 2026-11-27 at 12:36 +0100, Tobias Schaffner wrote:
> > > Hook the edge-stat facility into the DA/HA layer, so that any per-cpu
> > > automaton monitor also reports how long it lingers in each state.
> > > 
> > > Account a transition as soon as its state change is committed. An HA
> > > constraint that rejects the transition can therefore reset the monitor
> > > without leaving an accounting window for nested events.
> > > 
> > > Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
> > > ---
> > 
> > Looks good to me, thanks!
> > I'm taking the series.
> 
> Thanks for all the support! I will continue with the missing bits for
> per_task and maybe also per-object and global monitors soon.

Great, thank you for the nice addition.
On the long run we may also want to have this integrated in the rv tool, perhaps
to do some filtering, aggregation, or pretty-printing.

One thing at a time though ;)

Cheers,
Gabriele

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-27 14:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 11:36 [PATCH v5 0/4] rv: per-edge dwell-time statistics for per-cpu monitors Tobias Schaffner
2026-11-27 11:36 ` [PATCH v5 1/4] rv: guard rv.h against double inclusion Tobias Schaffner
2026-11-27 11:36 ` [PATCH v5 2/4] rv: add per-edge dwell-time statistics Tobias Schaffner
2026-11-27 11:36 ` [PATCH v5 3/4] rv: collect per-edge dwell time for per-cpu DA/HA monitors Tobias Schaffner
2026-11-27 13:57   ` Gabriele Monaco
2026-11-27 14:13     ` Tobias Schaffner
2026-11-27 14:36       ` Gabriele Monaco
2026-11-27 11:36 ` [PATCH v5 4/4] selftests/verification: test per-edge dwell-time statistics Tobias Schaffner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®