[PATCH v2 2/3] rv: add per-monitor edge-stat facility and stats file

From: Tobias Schaffner

Date: Fri Sep 11 2026 - 08:42:48 EST


Add CONFIG_RV_EDGE_STAT, an optional feature that records how long a
monitor's automaton dwells in a state and exposes it per edge through a
per-monitor "stats" tracefs file.

The facility uses a static per-CPU matrix and reports, per edge and per
CPU, the count, summed dwell time and maximum dwell time. Only the owning
CPU writes the counters, so a reader snapshots them with local64_read()
with no IPI and no locking on the accounting path.

Signed-off-by: Tobias Schaffner <tobias.schaffner@xxxxxxxxxxx>
---
.../trace/rv/runtime-verification.rst | 26 ++++
include/rv/edge_stat.h | 135 ++++++++++++++++++
kernel/trace/rv/Kconfig | 12 ++
3 files changed, 173 insertions(+)

diff --git a/Documentation/trace/rv/runtime-verification.rst b/Documentation/trace/rv/runtime-verification.rst
index c700dde9259c..88ea04ff64be 100644
--- a/Documentation/trace/rv/runtime-verification.rst
+++ b/Documentation/trace/rv/runtime-verification.rst
@@ -229,3 +229,29 @@ 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 on one CPU::
+
+ cpu edge label count sum_ns max_ns
+
+ *count* is the number of times the transition was committed, *sum_ns* and
+ *max_ns* are the total and worst dwell in nanoseconds, and *label* is
+ "state:event".
+
+The counters are reset each time the monitor is enabled.
+
+For example::
+
+ # cat monitors/wip/stats
+ # cpu edge label count sum_ns max_ns
+ 0 0 preemptive:preempt_disable 4210 95501200 183200
+ 0 4 non_preemptive:preempt_enable 4208 3812900 42600
diff --git a/include/rv/edge_stat.h b/include/rv/edge_stat.h
index c5f04fd34aee..aff00c6fbb39 100644
--- a/include/rv/edge_stat.h
+++ b/include/rv/edge_stat.h
@@ -8,12 +8,18 @@
#ifndef _RV_EDGE_STAT_H
#define _RV_EDGE_STAT_H

+#include <linux/args.h>
#include <linux/atomic.h>
#include <linux/bug.h>
#include <linux/compiler.h>
+#include <linux/percpu.h>
#include <linux/rv.h>
+#include <linux/sched/clock.h>
+#include <linux/seq_file.h>
+#include <linux/tracefs.h>
#include <linux/types.h>
#include <asm/local64.h>
+#include <rv/automata.h>

#ifdef CONFIG_RV_EDGE_STAT

@@ -64,6 +70,135 @@ void rv_edge_stat_account(struct rv_edge_stat *s, u64 dwell_ns)
}
}

+#define DA_MON_EDGES CONCATENATE(da_mon_edges_, MONITOR_NAME)
+
+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
+
+/*
+ * 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)
+
+struct rv_this_edges {
+ struct rv_edge_stat edge[RV_THIS_NR_EDGES];
+};
+
+static DEFINE_PER_CPU(struct rv_this_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(struct rv_this_edges));
+}
+
+static int rv_edge_stats_show(struct seq_file *seq, void *v)
+{
+ unsigned int e;
+ int cpu;
+
+ seq_puts(seq, "# cpu edge label count sum_ns max_ns\n");
+ /* Include offline CPUs and keep output stable across CPU hotplug. */
+ for_each_possible_cpu(cpu) {
+ struct rv_this_edges *m = per_cpu_ptr(&DA_MON_EDGES, cpu);
+
+ for (e = 0; e < RV_THIS_NR_EDGES; e++)
+ seq_printf(seq, "%d %u %s:%s %llu %llu %llu\n",
+ cpu, e,
+ model_get_state_name(e / EVENT_MAX),
+ model_get_event_name(e % EVENT_MAX),
+ (u64)local64_read(&m->edge[e].count),
+ (u64)local64_read(&m->edge[e].sum_ns),
+ (u64)local64_read(&m->edge[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_MODE_READ is private to kernel/trace/rv/. */
+ rv_this_stats_file = tracefs_create_file("stats", 0440, parent,
+ NULL, &rv_edge_stats_fops);
+ return rv_this_stats_file ? 0 : -ENOMEM;
+}
+
+static void rv_edge_stats_remove(void)
+{
+ tracefs_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() & RV_TS_MASK)
+
+static __always_inline void
+rv_edge_account(da_state_t old, da_state_t new, enum states curr, enum events ev)
+{
+ struct rv_this_edges *m;
+ u64 prev = da_ts_of(old);
+
+ if (!prev)
+ return;
+
+ m = this_cpu_ptr(&DA_MON_EDGES);
+ rv_edge_stat_account(&m->edge[curr * EVENT_MAX + ev],
+ (da_ts_of(new) - prev) & 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) ((unsigned int)(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 3884b14df375..3045037fb36a 100644
--- a/kernel/trace/rv/Kconfig
+++ b/kernel/trace/rv/Kconfig
@@ -59,6 +59,18 @@ config RV_PER_TASK_MONITORS
This option configures the maximum number of per-task RV monitors that can run
simultaneously.

+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.
+
source "kernel/trace/rv/monitors/wip/Kconfig"
source "kernel/trace/rv/monitors/wwnr/Kconfig"

--
2.43.0