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>
This commit is contained in:
Peter Mitsis 2021-12-14 21:31:10 -05:00 committed by Anas Nashif
commit 572f1db56a
4 changed files with 122 additions and 21 deletions

26
include/kernel/stats.h Normal file
View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2021, Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_KERNEL_STATS_H_
#define ZEPHYR_INCLUDE_KERNEL_STATS_H_
#include <stdint.h>
/*
* [k_cycle_stats] is used to track internal statistics about both thread
* and CPU usage.
*/
struct k_cycle_stats {
uint64_t total; /* total usage in cycles */
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
uint64_t current; /* # of cycles in current usage window */
uint64_t longest; /* # of cycles in longest usage window */
uint32_t num_windows; /* # of usage windows */
#endif
};
#endif

View file

@ -11,6 +11,8 @@
#include <sys/mem_manage.h>
#endif
#include <kernel/stats.h>
/**
* @typedef k_thread_entry_t
* @brief Thread entry point function type.
@ -118,7 +120,7 @@ struct _thread_base {
#endif
#ifdef CONFIG_SCHED_THREAD_USAGE
uint64_t usage;
struct k_cycle_stats usage; /* Track thread usage statistics */
#endif
};
@ -172,6 +174,26 @@ struct _thread_userspace_local_data {
typedef struct k_thread_runtime_stats {
#ifdef CONFIG_SCHED_THREAD_USAGE
uint64_t execution_cycles;
/*
* In the context of thread statistics, [execution_cycles] is the same
* as the total # of non-idle cycles. In the context of CPU statistics,
* it refers to the sum of non-idle + idle cycles.
*/
#endif
#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
uint64_t current_cycles; /* current # of non-idle cycles */
uint64_t peak_cycles; /* peak # of non-idle cycles */
uint64_t total_cycles; /* total # of non-idle cycles */
uint64_t average_cycles; /* average # of non-idle cycles */
/*
* This field is always zero for individual threads. It only comes
* into play when gathering statistics for the CPU. In that case it
* represents the total number of cycles spent idling.
*/
uint64_t idle_cycles;
#endif
} k_thread_runtime_stats_t;