Commit graph zephyr/kernel/usage.c
Author SHA1 Message Date
Yiren Guo
279f7fc171 kernel: usage: fix unused-variable warning when THREAD_USAGE_ALL=n
With CONFIG_SCHED_THREAD_USAGE_ANALYSIS=y and
CONFIG_SCHED_THREAD_USAGE_ALL=n, sched_cpu_update_usage() is a no-op
macro, so the cycles local in k_thread_runtime_stats_enable() and
z_thread_stats_reset() is set but never read, triggering a compiler
warning. Pass the expression straight to the macro to keep
that configuration warning-free.

Signed-off-by: Yiren Guo <guoyr_2013@hotmail.com>
2026-08-19 17:12:24 +01:00
Yiren Guo
fc7a8936a4 kernel: usage: fix double-count in k_thread_runtime_stats_disable()
Disabling stats on the current thread accounts the in-progress window
into the CPU stats but forgets to update cpu->usage0. The next
z_sched_usage_stop() recomputes the same window and adds it again.

Signed-off-by: Yiren Guo <guoyr_2013@hotmail.com>
2026-08-15 08:11:37 -04:00
Anas Nashif
cdb04e25b9 kernel: widen CPU-iteration loop indices to match bound
The per-CPU loops in k_thread_runtime_stats_all_get() and the runtime
stats enable/disable paths used a uint8_t index while the loop bound
arch_num_cpus() is unsigned int.
Widen the index to unsigned int to match the bound type.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-21 14:09:50 -04:00
Anas Nashif
11426c0d7b kernel: usage: account in-progress cycles of another CPU
A CPU's usage counters only advance when it context switches, or when it
reports on itself through z_sched_cpu_usage(). Reading the runtime stats
of a *different* CPU therefore misses whatever time that CPU has spent in
its current thread since then.

The effect is worst for an idle CPU: a CPU that ran briefly and then went
idle has the active cycles of that run in its counters, but none of the
idle time that followed, because the idle thread has not been switched
out yet. Both k_thread_runtime_stats_cpu_get() and everything built on it
therefore see a window made up purely of execution cycles, i.e. a 100%
load for a CPU that is doing nothing.

Fold the in-progress cycles into the returned view, attributing them to
the idle thread or to the CPU depending on what it is currently running.
This is done for the returned stats only, without mutating the other
CPU's accounting, as that CPU will account for the same cycles itself
once it next updates. The read is consistent because usage_lock, which is
held here, also covers the usage0 timestamp and the counters.

Reading the current CPU is unchanged.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-17 11:55:28 -04:00
James Roy
465d3de809 kernel: Add k_thread_runtime_stats_is_enabled function
Add 'k_thread_runtime_stats_is_enabled' function, whichs
used to check whether runtime statistics collection is
enabled for a thread.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2026-06-10 14:52:53 -04:00
Nicolas Pitre
b5363d5fff kernel: usage: Fix CPU stats retrieval in z_sched_cpu_usage()
The z_sched_cpu_usage() function was incorrectly using _current_cpu
instead of the requested cpu_id parameter when retrieving CPU usage
statistics. This caused it to always return stats from the current CPU
rather than the specified CPU.

This bug manifested in SMP systems when k_thread_runtime_stats_all_get()
looped through all CPUs - it would get stats from the wrong CPU for
each iteration, leading to inconsistent time values. For example, in
the times() POSIX function, this caused time to appear to move backwards:

  t0: utime: 59908
  t1: utime: 824

The fix ensures that:
1. cpu pointer is set to &_kernel.cpus[cpu_id] (the requested CPU)
2. The check for "is this the current CPU" is correctly written as
   (cpu == _current_cpu)

This fixes the portability.posix.muti_process.newlib test failure
on FVP SMP platforms where times() was reporting backwards time.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-10-22 09:04:13 +02:00
Eric Johnson
69c5c6d511 kernel: Remove duplicate execution_cycles write and improve docstring
There is a duplicate write in `z_sched_thread_usage()` that can be
removed. Also modified the docstrings to `k_thread_runtime_stats` to
help better describe the differences between execution_cycles and
total_cycles when getting stats for the CPU or a thread

Signed-off-by: Eric Johnson <eric@memfault.com>
2024-04-28 13:04:20 -04:00
Simon Hein
bcd1d19322 kernel: add closing comments to config endifs
Add a closing comment to the endif with the configuration
information to which the endif belongs too.
To make the code more clearer if the configs need adaptions.

Signed-off-by: Simon Hein <Shein@baumer.com>
2024-03-25 18:03:31 -04:00
Peter Mitsis
e6f1090553 kernel: Integrate object core statistics
Integrates object core statistics framework into the following
kernel objects:
  sys_mem_blocks, k_mem_slab
  threads, _cpu, z_kernel

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2023-09-30 08:04:14 +03:00
Peter Mitsis
9bedfd82a2 kernel: Refactor CPU usage
Refactors CPU usage (thread runtime stats) to make it easier to
integrate with the object core statistics framework.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2023-09-30 08:04:14 +03:00
Kumar Gala
a1195ae39b smp: Move for loops to use arch_num_cpus instead of CONFIG_MP_NUM_CPUS
Change for loops of the form:

for (i = 0; i < CONFIG_MP_NUM_CPUS; i++)
   ...

to

unsigned int num_cpus = arch_num_cpus();
for (i = 0; i < num_cpus; i++)
   ...

We do the call outside of the for loop so that it only happens once,
rather than on every iteration.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2022-10-21 13:14:58 +02:00
Tom Burdick
9c0bf4b071 kernel: Obtain current cpu inside of locks for thread usage
Obtaining the CPU outside of the spin locks on SMP would
result in an assert failing on __ASSERT(!z_smp_mobile())
which makes sense as the current cpu may change.

Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
2022-09-26 07:55:33 +00:00
Peter Mitsis
976e4087ec kernel: Fix gathering of runtime thread stats
The function k_thread_runtime_stats_all_get() now populates the
current_cycles field in the thread runtime stats structure.

Resets the number of cycles in the CPU's current usage window once
the idle thread is scheduled.

Fixes the average_cycles calcuation.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2022-05-13 10:19:53 -05:00
Gerard Marull-Paretas
cffefc818d kernel: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all kernel code to the
new prefix <zephyr/...>. Note that the conversion has been scripted,
refer to zephyrproject-rtos#45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-09 09:26:20 +02:00
Peter Mitsis
11f8f6697f kernel: Update CPU runtime stats of non-idle time
Updates sched_cpu_update_usage() such that the CPU runtime stats
only update the its non-idle time when the current thread is not the
idle thread. This is necessary as otherwise the CPUs idle-time will
be double counted in k_thread_runtime_stats.execution_cycles.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2022-01-20 08:22:01 -05:00
Peter Mitsis
0ebd6c7f26 kernel/sched: enable/disable runtime stats
Adds support to enable/disable both thread and cpu runtime
stats.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2022-01-10 10:38:06 -05:00
Peter Mitsis
4eb1dd02cc kernel: extend CPU runtime stats
Extends the CPU usage runtime stats to track current, total, peak
and average usage (as bounded by the scheduling of the idle thread).
This permits a developer to obtain more system information if desired
to tune the system.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2022-01-10 10:38:06 -05:00
Peter Mitsis
572f1db56a kernel: extend thread runtime stats
When the new Kconfig option CONFIG_SCHED_THREAD_USAGE_ANALYSIS
is enabled, additional timing stats are collected during context
switches. This extra information allows a developer to obtain the
the current, longest, average and total lengths of the time that
a thread has been scheduled to execute.

A developer can in turn use this information to tune their app and/or
alter their scheduling policies.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2022-01-10 10:38:06 -05:00
Peter Mitsis
5deaffb2ee kernel: update z_sched_thread_usage()
This commit does two things to the z_sched_thread_usage(). First,
it updates the API so that it accepts a pointer to the runtime
stats instead of simply returning the usage cycles. This gives it
the flexibility to retrieve additional statistics in the future.

Second, the runtime stats are only updated if the specified thread
is the current thread running on the current core.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2022-01-10 10:38:06 -05:00
Peter Mitsis
82c3d531a6 kernel: move thread usage routines to own file
Moves the CONFIG_SCHED_THREAD_USAGE block of code out of sched.c
into its own file. Not only do they employ their own private
spin lock, but it is expected that additional usage routines will be
added in the future.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2022-01-10 10:38:06 -05:00