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>
diff --git a/kernel/usage.c b/kernel/usage.c
index d695bf5..fae72ce 100644
--- a/kernel/usage.c
+++ b/kernel/usage.c
@@ -63,15 +63,28 @@
k_spin_unlock(&usage_lock, k);
}
-uint64_t z_sched_thread_usage(struct k_thread *thread)
+void z_sched_thread_usage(struct k_thread *thread,
+ struct k_thread_runtime_stats *stats)
{
- k_spinlock_key_t k = k_spin_lock(&usage_lock);
- uint32_t u0 = _current_cpu->usage0, now = usage_now();
- uint64_t ret = thread->base.usage;
+ uint32_t u0;
+ uint32_t now;
+ struct _cpu *cpu;
+ k_spinlock_key_t key;
- if (u0 != 0) {
+ cpu = _current_cpu;
+ key = k_spin_lock(&usage_lock);
+
+ u0 = cpu->usage0;
+ now = usage_now();
+
+ if ((u0 != 0) && (thread == cpu->current)) {
uint32_t dt = now - u0;
+ /*
+ * Update the thread's usage stats if it is the current thread
+ * running on the current core.
+ */
+
#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
if (z_is_idle_thread_object(thread)) {
_kernel.idle_thread_usage += dt;
@@ -80,11 +93,11 @@
}
#endif
- ret += dt;
- thread->base.usage = ret;
- _current_cpu->usage0 = now;
+ thread->base.usage += dt;
+ cpu->usage0 = now;
}
- k_spin_unlock(&usage_lock, k);
- return ret;
+ stats->execution_cycles = thread->base.usage;
+
+ k_spin_unlock(&usage_lock, key);
}