pm: policy: return a reference to the next state

Return a constant reference to the next state instead of a copy of
struct pm_state_info. When the next state should be active, just return
NULL. Struct copying should be in general avoided, specially in code
paths executed frequently as is this one.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2021-12-20 20:31:08 +01:00 committed by Carles Cufí
parent e5df3a8cc4
commit 696caa0524
7 changed files with 27 additions and 17 deletions

View file

@ -26,9 +26,10 @@ extern "C" {
* @param cpu CPU index.
* @param ticks The number of ticks to the next scheduled event.
*
* @return The power state the system should use for the given cpu.
* @return The power state the system should use for the given cpu. The function
* will return NULL if system should remain into PM_STATE_ACTIVE.
*/
struct pm_state_info pm_policy_next_state(uint8_t cpu, int32_t ticks);
const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks);
/** @endcond */

View file

@ -194,7 +194,12 @@ bool pm_system_suspend(int32_t ticks)
SYS_PORT_TRACING_FUNC_ENTER(pm, system_suspend, ticks);
if (!atomic_test_and_set_bit(z_power_states_forced, id)) {
z_power_states[id] = pm_policy_next_state(id, ticks);
const struct pm_state_info *info;
info = pm_policy_next_state(id, ticks);
if (info != NULL) {
z_power_states[id] = *info;
}
}
if (z_power_states[id].state == PM_STATE_ACTIVE) {

View file

@ -12,7 +12,7 @@
#include <logging/log.h>
LOG_MODULE_DECLARE(pm, CONFIG_PM_LOG_LEVEL);
struct pm_state_info pm_policy_next_state(uint8_t cpu, int32_t ticks)
const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks)
{
uint8_t num_cpu_states;
const struct pm_state_info *cpu_states;
@ -36,10 +36,10 @@ struct pm_state_info pm_policy_next_state(uint8_t cpu, int32_t ticks)
"(ticks: %d, min_residency: %u) to cpu %d",
state->state, ticks, state->min_residency_us,
cpu);
return *state;
return state;
}
}
LOG_DBG("No suitable power state found for cpu: %d!", cpu);
return (struct pm_state_info){PM_STATE_ACTIVE, 0, 0};
return NULL;
}

View file

@ -23,7 +23,7 @@ static void tdata_dump_callback(const struct k_thread *thread, void *user_data)
}
/* Our PM policy handler */
struct pm_state_info pm_policy_next_state(uint8_t cpu, int32_t ticks)
const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks)
{
static bool test_flag;
@ -37,7 +37,7 @@ struct pm_state_info pm_policy_next_state(uint8_t cpu, int32_t ticks)
test_flag = true;
}
return (struct pm_state_info){PM_STATE_ACTIVE, 0, 0};
return NULL;
}
/*work handler*/

View file

@ -57,16 +57,20 @@ void pm_power_state_exit_post_ops(struct pm_state_info info)
irq_unlock(0);
}
struct pm_state_info pm_policy_next_state(uint8_t cpu, int32_t ticks)
const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks)
{
static const struct pm_state_info state = {
.state = PM_STATE_SUSPEND_TO_RAM
};
ARG_UNUSED(cpu);
while (sleep_count < 3) {
sleep_count++;
return (struct pm_state_info){PM_STATE_SUSPEND_TO_RAM, 0, 0, 0};
return &state;
}
return (struct pm_state_info){PM_STATE_ACTIVE, 0, 0, 0};
return NULL;
}
void test_wakeup_device_api(void)

View file

@ -198,9 +198,9 @@ void pm_power_state_exit_post_ops(struct pm_state_info info)
}
/* Our PM policy handler */
struct pm_state_info pm_policy_next_state(uint8_t cpu, int ticks)
const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks)
{
struct pm_state_info info = {};
static struct pm_state_info info;
ARG_UNUSED(cpu);
@ -219,7 +219,7 @@ struct pm_state_info pm_policy_next_state(uint8_t cpu, int ticks)
*/
info.state = PM_STATE_ACTIVE;
}
return info;
return &info;
}
/* implement in application, called by idle thread */

View file

@ -60,9 +60,9 @@ void pm_power_state_exit_post_ops(struct pm_state_info info)
irq_unlock(0);
}
struct pm_state_info pm_policy_next_state(uint8_t cpu, int ticks)
const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int ticks)
{
struct pm_state_info info = {};
static struct pm_state_info info = {};
int32_t msecs = k_ticks_to_ms_floor64(ticks);
if (msecs < ACTIVE_MSEC) {
@ -81,7 +81,7 @@ struct pm_state_info pm_policy_next_state(uint8_t cpu, int ticks)
state_testing[_current_cpu->id] = info.state;
return info;
return &info;
}
/*