From 7baed3dd3e3c3654c7d6a3c37a7caa1a6445352b Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Mon, 22 Nov 2021 19:08:35 +0100 Subject: [PATCH] pm: move stats code into its own file Stats are an optional feature, moving it into a separate source file improves code readability. Signed-off-by: Gerard Marull-Paretas --- subsys/pm/CMakeLists.txt | 7 ++-- subsys/pm/pm_stats.c | 75 ++++++++++++++++++++++++++++++++++++++++ subsys/pm/pm_stats.h | 23 ++++++++++++ subsys/pm/power.c | 74 ++------------------------------------- 4 files changed, 105 insertions(+), 74 deletions(-) create mode 100644 subsys/pm/pm_stats.c create mode 100644 subsys/pm/pm_stats.h diff --git a/subsys/pm/CMakeLists.txt b/subsys/pm/CMakeLists.txt index 21c9d37e8dd..0d09e0ef391 100644 --- a/subsys/pm/CMakeLists.txt +++ b/subsys/pm/CMakeLists.txt @@ -1,7 +1,10 @@ # SPDX-License-Identifier: Apache-2.0 -zephyr_sources_ifdef(CONFIG_PM power.c) -zephyr_sources_ifdef(CONFIG_PM pm_ctrl.c) +if(CONFIG_PM) + zephyr_sources(power.c pm_ctrl.c) + zephyr_sources_ifdef(CONFIG_PM_STATS pm_stats.c) +endif() + zephyr_sources_ifdef(CONFIG_PM_DEVICE device.c) zephyr_sources_ifdef(CONFIG_PM_DEVICE_RUNTIME device_runtime.c) add_subdirectory(policy) diff --git a/subsys/pm/pm_stats.c b/subsys/pm/pm_stats.c new file mode 100644 index 00000000000..5177a17bdaf --- /dev/null +++ b/subsys/pm/pm_stats.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2018 Intel Corporation. + * Copyright (c) 2021 Nordic Semiconductor ASA. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "pm_stats.h" + +#include +#include +#include +#include +#include + +struct pm_cpu_timing { + uint32_t timer_start; + uint32_t timer_end; +}; + +static struct pm_cpu_timing pm_cpu_timings[CONFIG_MP_NUM_CPUS]; + +STATS_SECT_START(pm_cpu_stats) +STATS_SECT_ENTRY32(state_count) +STATS_SECT_ENTRY32(state_last_cycles) +STATS_SECT_ENTRY32(state_total_cycles) +STATS_SECT_END; + +STATS_NAME_START(pm_cpu_stats) +STATS_NAME(pm_cpu_stats, state_count) +STATS_NAME(pm_cpu_stats, state_last_cycles) +STATS_NAME(pm_cpu_stats, state_total_cycles) +STATS_NAME_END(pm_cpu_stats); + +#define PM_STAT_NAME_LEN sizeof("pm_cpu_XXX_state_X_stats") +static char pm_cpu_stat_names[CONFIG_MP_NUM_CPUS][PM_STATE_COUNT][PM_STAT_NAME_LEN]; +static struct stats_pm_cpu_stats pm_cpu_stats[CONFIG_MP_NUM_CPUS][PM_STATE_COUNT]; + +static int pm_stats_init(const struct device *unused) +{ + for (int i = 0; i < CONFIG_MP_NUM_CPUS; i++) { + for (int j = 0; j < PM_STATE_COUNT; j++) { + snprintk(pm_cpu_stat_names[i][j], PM_STAT_NAME_LEN, + "pm_cpu_%03d_state_%1d_stats", i, j); + stats_init(&(pm_cpu_stats[i][j].s_hdr), STATS_SIZE_32, 3, + STATS_NAME_INIT_PARMS(pm_cpu_stats)); + stats_register(pm_cpu_stat_names[i][j], &(pm_cpu_stats[i][j].s_hdr)); + } + } + return 0; +} + +SYS_INIT(pm_stats_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); + +void pm_start_timer(void) +{ + pm_cpu_timings[_current_cpu->id].timer_start = k_cycle_get_32(); +} + +void pm_stop_timer(void) +{ + pm_cpu_timings[_current_cpu->id].timer_end = k_cycle_get_32(); +} + +void pm_stats_update(enum pm_state state) +{ + uint8_t cpu = _current_cpu->id; + uint32_t time_total = + pm_cpu_timings[cpu].timer_end - + pm_cpu_timings[cpu].timer_start; + + STATS_INC(pm_cpu_stats[cpu][state], state_count); + STATS_INCN(pm_cpu_stats[cpu][state], state_total_cycles, time_total); + STATS_SET(pm_cpu_stats[cpu][state], state_last_cycles, time_total); +} diff --git a/subsys/pm/pm_stats.h b/subsys/pm/pm_stats.h new file mode 100644 index 00000000000..0cde5868548 --- /dev/null +++ b/subsys/pm/pm_stats.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2018 Intel Corporation. + * Copyright (c) 2021 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_SUBSYS_PM_PM_STATS_H_ +#define ZEPHYR_SUBSYS_PM_PM_STATS_H_ + +#include + +#ifdef CONFIG_PM_STATS +void pm_start_timer(void); +void pm_stop_timer(void); +void pm_stats_update(enum pm_state state); +#else +static inline void pm_start_timer(void) {} +static inline void pm_stop_timer(void) {} +static inline void pm_stats_update(enum pm_state state) {} +#endif /* CONFIG_PM_STATS */ + +#endif /* ZEPHYR_SUBSYS_PM_PM_STATS_H_ */ diff --git a/subsys/pm/power.c b/subsys/pm/power.c index 6dc45d43d01..64ff3cd5112 100644 --- a/subsys/pm/power.c +++ b/subsys/pm/power.c @@ -17,6 +17,8 @@ #include #include +#include "pm_stats.h" + #include LOG_MODULE_REGISTER(pm, CONFIG_PM_LOG_LEVEL); @@ -30,79 +32,7 @@ static atomic_t z_cpus_active = ATOMIC_INIT(CONFIG_MP_NUM_CPUS); #endif static struct k_spinlock pm_notifier_lock; -#ifdef CONFIG_PM_STATS -#include -#include - - -struct pm_cpu_timing { - uint32_t timer_start; - uint32_t timer_end; -}; - -static struct pm_cpu_timing pm_cpu_timings[CONFIG_MP_NUM_CPUS]; - -static inline void pm_start_timer(void) -{ - pm_cpu_timings[_current_cpu->id].timer_start = k_cycle_get_32(); -} - -static inline void pm_stop_timer(void) -{ - pm_cpu_timings[_current_cpu->id].timer_end = k_cycle_get_32(); -} - -STATS_SECT_START(pm_cpu_stats) -STATS_SECT_ENTRY32(state_count) -STATS_SECT_ENTRY32(state_last_cycles) -STATS_SECT_ENTRY32(state_total_cycles) -STATS_SECT_END; - -STATS_NAME_START(pm_cpu_stats) -STATS_NAME(pm_cpu_stats, state_count) -STATS_NAME(pm_cpu_stats, state_last_cycles) -STATS_NAME(pm_cpu_stats, state_total_cycles) -STATS_NAME_END(pm_cpu_stats); - -#define PM_STAT_NAME_LEN sizeof("pm_cpu_XXX_state_X_stats") -static char pm_cpu_stat_names[CONFIG_MP_NUM_CPUS][PM_STATE_COUNT][PM_STAT_NAME_LEN]; -static struct stats_pm_cpu_stats pm_cpu_stats[CONFIG_MP_NUM_CPUS][PM_STATE_COUNT]; - -static int pm_stats_init(const struct device *unused) -{ - for (int i = 0; i < CONFIG_MP_NUM_CPUS; i++) { - for (int j = 0; j < PM_STATE_COUNT; j++) { - snprintk(pm_cpu_stat_names[i][j], PM_STAT_NAME_LEN, - "pm_cpu_%03d_state_%1d_stats", i, j); - stats_init(&(pm_cpu_stats[i][j].s_hdr), STATS_SIZE_32, 3, - STATS_NAME_INIT_PARMS(pm_cpu_stats)); - stats_register(pm_cpu_stat_names[i][j], &(pm_cpu_stats[i][j].s_hdr)); - } - } - return 0; -} - -SYS_INIT(pm_stats_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); - -static void pm_stats_update(enum pm_state state) -{ - uint8_t cpu = _current_cpu->id; - uint32_t time_total = - pm_cpu_timings[cpu].timer_end - - pm_cpu_timings[cpu].timer_start; - - STATS_INC(pm_cpu_stats[cpu][state], state_count); - STATS_INCN(pm_cpu_stats[cpu][state], state_total_cycles, time_total); - STATS_SET(pm_cpu_stats[cpu][state], state_last_cycles, time_total); -} -#else -static inline void pm_start_timer(void) {} -static inline void pm_stop_timer(void) {} - - -static void pm_stats_update(enum pm_state state) {} -#endif #ifdef CONFIG_PM_DEVICE extern const struct device *__pm_device_slots_start[];