tests: pm: policy_api: test policy behavior

Add a new test to verify the policy API behavior. Test also checks that
custom policies can be implemented.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2021-12-21 10:37:51 +01:00 committed by Carles Cufí
commit 8d41e13ada
5 changed files with 166 additions and 0 deletions

View file

@ -0,0 +1,8 @@
# Copyright (c) 2021 Nordic Semiconductor ASA.
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(policy_api)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
/ {
cpus {
cpu1: cpu@1 {
compatible = "zephyr,native-posix-cpu";
reg = <1>;
cpu-power-states = <&state2>;
};
};
power-states {
state0: state0 {
compatible = "zephyr,power-state";
power-state-name = "runtime-idle";
min-residency-us = <100000>;
exit-latency-us = <10000>;
};
state1: state1 {
compatible = "zephyr,power-state";
power-state-name = "suspend-to-ram";
min-residency-us = <1000000>;
exit-latency-us = <100000>;
};
state2: state2 {
compatible = "zephyr,power-state";
power-state-name = "suspend-to-ram";
min-residency-us = <500000>;
exit-latency-us = <50000>;
};
};
};
&cpu0 {
cpu-power-states = <&state0 &state1>;
};

View file

@ -0,0 +1,5 @@
# Copyright (c) 2021 Nordic Semiconductor ASA.
# SPDX-License-Identifier: Apache-2.0
CONFIG_ZTEST=y
CONFIG_PM=y

View file

@ -0,0 +1,101 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <pm/policy.h>
#include <sys/time_units.h>
#include <sys_clock.h>
#include <ztest.h>
#ifdef CONFIG_PM_POLICY_RESIDENCY
/**
* @brief Test the behavior of pm_policy_next_state() when
* CONFIG_PM_POLICY_RESIDENCY=y.
*/
static void test_pm_policy_next_state_residency(void)
{
const struct pm_state_info *next;
/* cpu 0 */
next = pm_policy_next_state(0U, 0);
zassert_equal(next, NULL, NULL);
next = pm_policy_next_state(0U, k_us_to_ticks_floor32(10999));
zassert_equal(next, NULL, NULL);
next = pm_policy_next_state(0U, k_us_to_ticks_floor32(110000));
zassert_equal(next->state, PM_STATE_RUNTIME_IDLE, NULL);
zassert_equal(next->min_residency_us, 100000, NULL);
zassert_equal(next->exit_latency_us, 10000, NULL);
next = pm_policy_next_state(0U, k_us_to_ticks_floor32(1099999));
zassert_equal(next->state, PM_STATE_RUNTIME_IDLE, NULL);
next = pm_policy_next_state(0U, k_us_to_ticks_floor32(1100000));
zassert_equal(next->state, PM_STATE_SUSPEND_TO_RAM, NULL);
zassert_equal(next->min_residency_us, 1000000, NULL);
zassert_equal(next->exit_latency_us, 100000, NULL);
next = pm_policy_next_state(0U, K_TICKS_FOREVER);
zassert_equal(next->state, PM_STATE_SUSPEND_TO_RAM, NULL);
/* cpu 1 */
next = pm_policy_next_state(1U, 0);
zassert_equal(next, NULL, NULL);
next = pm_policy_next_state(1U, k_us_to_ticks_floor32(549999));
zassert_equal(next, NULL, NULL);
next = pm_policy_next_state(1U, k_us_to_ticks_floor32(550000));
zassert_equal(next->state, PM_STATE_SUSPEND_TO_RAM, NULL);
zassert_equal(next->min_residency_us, 500000, NULL);
zassert_equal(next->exit_latency_us, 50000, NULL);
next = pm_policy_next_state(1U, K_TICKS_FOREVER);
zassert_equal(next->state, PM_STATE_SUSPEND_TO_RAM, NULL);
}
#else
static void test_pm_policy_next_state_residency(void)
{
ztest_test_skip();
}
#endif /* CONFIG_PM_POLICY_RESIDENCY */
#ifdef CONFIG_PM_POLICY_APP
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_SOFT_OFF};
ARG_UNUSED(cpu);
ARG_UNUSED(ticks);
return &state;
}
/**
* @brief Test that a custom policy can be implemented when
* CONFIG_PM_POLICY_APP=y.
*/
static void test_pm_policy_next_state_app(void)
{
const struct pm_state_info *next;
next = pm_policy_next_state(0U, 0);
zassert_equal(next->state, PM_STATE_SOFT_OFF, NULL);
}
#else
static void test_pm_policy_next_state_app(void)
{
ztest_test_skip();
}
#endif /* CONFIG_PM_POLICY_APP */
void test_main(void)
{
ztest_test_suite(policy_api,
ztest_unit_test(test_pm_policy_next_state_residency),
ztest_unit_test(test_pm_policy_next_state_app));
ztest_run_test_suite(policy_api);
}

View file

@ -0,0 +1,11 @@
# Copyright (c) 2021 Nordic Semiconductor ASA.
# SPDX-License-Identifier: Apache-2.0
common:
tags: pm
platform_allow: native_posix native_posix_64
tests:
subsys.pm.policy_api: {}
subsys.pm.policy_api_app:
extra_configs:
- CONFIG_PM_POLICY_APP=y