Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH v4 0/4] rv: per-edge dwell-time statistics for per-cpu monitors
@ 2026-11-27  7:05 Tobias Schaffner
  2026-11-27  7:05 ` [PATCH v4 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  7:05 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 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                       |  24 +-
 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, 328 insertions(+), 8 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

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

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27  7:05 [PATCH v4 0/4] rv: per-edge dwell-time statistics for per-cpu monitors Tobias Schaffner
2026-11-27  7:05 ` [PATCH v4 1/4] rv: guard rv.h against double inclusion Tobias Schaffner
2026-11-27  7:05 ` [PATCH v4 2/4] rv: add per-edge dwell-time statistics Tobias Schaffner
2026-11-27  7:05 ` [PATCH v4 3/4] rv: collect per-edge dwell time for per-cpu DA/HA monitors Tobias Schaffner
2026-11-27  8:27   ` Gabriele Monaco
2026-11-27  9:03     ` Tobias Schaffner
2026-11-27  9:11       ` Gabriele Monaco
2026-11-27  7:05 ` [PATCH v4 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