tests: kernel: usage: thread_runtime_stats: Improve test_all_stats_usage

Improve robustness of test_all_stats_usage test. Test was relying on a
fact that system did not go to idle before this test is run. It means
that it was assuming that it will be the first test which might not be
true. Additionally, on some targets there might be some idle waits
during system boot. Test is adjusted to validate gathered statistics
with an assumption that there might have been idle period prior to the
test.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruściński 2025-02-21 12:23:42 +01:00 committed by Fabio Baltieri
commit 9323adf4af

View file

@ -100,16 +100,16 @@ ZTEST(usage_api, test_all_stats_usage)
/*
* Verify that before the system idles for 2 ticks that
* [execution_cycles] is increasing, [total_cycles] matches
* [execution_cycles] is increasing, [total_cycles + idle_cycles] matches
* [execution_cycles] and [idle_cycles] is not changing (as the
* system has been idle yet.
* system is not going to idle during that test).
*/
zassert_true(stats2.execution_cycles > stats1.execution_cycles);
zassert_true(stats3.execution_cycles > stats2.execution_cycles);
zassert_true(stats1.execution_cycles == stats1.total_cycles);
zassert_true(stats2.execution_cycles == stats2.total_cycles);
zassert_true(stats3.execution_cycles == stats3.total_cycles);
zassert_true(stats1.execution_cycles == (stats1.total_cycles + stats1.idle_cycles));
zassert_true(stats2.execution_cycles == (stats2.total_cycles + stats2.idle_cycles));
zassert_true(stats3.execution_cycles == (stats3.total_cycles + stats3.idle_cycles));
#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
zassert_true(stats1.idle_cycles == stats2.idle_cycles);
zassert_true(stats1.idle_cycles == stats3.idle_cycles);
@ -122,8 +122,8 @@ ZTEST(usage_api, test_all_stats_usage)
* going idle.
* 1. [current_cycles] increases.
* 2. [peak_cycles] matches [current_cycles].
* 3. [average_cycles] is 0 (because system has not gone idle yet)
* 4. [current_cycles] matches [execution_cycles].
* 3. [average_cycles] is 0 if system has not gone idle yet
* 4. [current_cycles] matches [execution_cycles] if system has not gone idle yet
*/
zassert_true(stats2.current_cycles > stats1.current_cycles);
@ -133,6 +133,7 @@ ZTEST(usage_api, test_all_stats_usage)
zassert_true(stats2.peak_cycles == stats2.current_cycles);
zassert_true(stats3.peak_cycles == stats3.current_cycles);
if (stats1.idle_cycles == 0) {
zassert_true(stats1.average_cycles == 0);
zassert_true(stats2.average_cycles == 0);
zassert_true(stats3.average_cycles == 0);
@ -140,6 +141,11 @@ ZTEST(usage_api, test_all_stats_usage)
zassert_true(stats1.current_cycles == stats1.execution_cycles);
zassert_true(stats2.current_cycles == stats2.execution_cycles);
zassert_true(stats3.current_cycles == stats3.execution_cycles);
} else {
zassert_true(stats1.current_cycles < stats1.execution_cycles);
zassert_true(stats2.current_cycles < stats2.execution_cycles);
zassert_true(stats3.current_cycles < stats3.execution_cycles);
}
#endif
/*
@ -172,7 +178,7 @@ ZTEST(usage_api, test_all_stats_usage)
stats3.peak_cycles, IDLE_EVENT_STATS_PRECISION), NULL);
zassert_true(stats4.peak_cycles == stats5.peak_cycles);
zassert_true(stats4.average_cycles > stats3.average_cycles);
zassert_true(stats4.average_cycles > 0);
zassert_true(stats5.average_cycles > stats4.average_cycles);
#endif