Skip to content

Commit 90db5b7

Browse files
teburdcfriedt
authored andcommitted
pm: Fixes a data race in the debug stats on SMP
Previously stats were kept in a single static but would be updated by an idle thread per cpu core. Stats/debug info is now kept per cpu core. Signed-off-by: Tom Burdick <[email protected]>
1 parent 99ef25a commit 90db5b7

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

subsys/pm/power.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,50 @@ static struct k_spinlock pm_notifier_lock;
2828

2929
#ifdef CONFIG_PM_DEBUG
3030

31-
struct pm_debug_info {
31+
struct pm_state_debug_info {
3232
uint32_t count;
3333
uint32_t last_res;
3434
uint32_t total_res;
3535
};
3636

37-
static struct pm_debug_info pm_dbg_info[PM_STATES_LEN];
38-
static uint32_t timer_start, timer_end;
37+
struct pm_cpu_debug_info {
38+
uint32_t timer_start;
39+
uint32_t timer_end;
40+
struct pm_state_debug_info state_info[PM_STATES_LEN];
41+
};
42+
43+
static struct pm_cpu_debug_info pm_cpu_dbg_info[CONFIG_MP_NUM_CPUS];
3944

4045
static inline void pm_debug_start_timer(void)
4146
{
42-
timer_start = k_cycle_get_32();
47+
pm_cpu_dbg_info[_current_cpu->id].timer_start = k_cycle_get_32();
4348
}
4449

4550
static inline void pm_debug_stop_timer(void)
4651
{
47-
timer_end = k_cycle_get_32();
52+
pm_cpu_dbg_info[_current_cpu->id].timer_end = k_cycle_get_32();
4853
}
4954

5055
static void pm_log_debug_info(enum pm_state state)
5156
{
52-
uint32_t res = timer_end - timer_start;
57+
uint32_t res = pm_cpu_dbg_info[_current_cpu->id].timer_end
58+
- pm_cpu_dbg_info[_current_cpu->id].timer_start;
5359

54-
pm_dbg_info[state].count++;
55-
pm_dbg_info[state].last_res = res;
56-
pm_dbg_info[state].total_res += res;
60+
pm_cpu_dbg_info[_current_cpu->id].state_info[state].count++;
61+
pm_cpu_dbg_info[_current_cpu->id].state_info[state].last_res = res;
62+
pm_cpu_dbg_info[_current_cpu->id].state_info[state].total_res += res;
5763
}
5864

5965
void pm_dump_debug_info(void)
6066
{
61-
for (int i = 0; i < PM_STATES_LEN; i++) {
62-
LOG_DBG("PM:state = %d, count = %d last_res = %d, "
63-
"total_res = %d\n", i, pm_dbg_info[i].count,
64-
pm_dbg_info[i].last_res, pm_dbg_info[i].total_res);
67+
for (int i = 0; i < CONFIG_MP_NUM_CPUS; i++) {
68+
for (int j = 0; j < PM_STATES_LEN; j++) {
69+
LOG_DBG("PM:cpu = %d state = %d, count = %u last_res = %u, "
70+
"total_res = %u\n", i, j,
71+
pm_cpu_dbg_info[i].state_info[j].count,
72+
pm_cpu_dbg_info[i].state_info[j].last_res,
73+
pm_cpu_dbg_info[i].state_info[j].total_res);
74+
}
6575
}
6676
}
6777
#else

0 commit comments

Comments
 (0)