Skip to content

Commit d01c538

Browse files
committed
[nrf fromlist] 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. Upstream PR #: 86139 Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent 0aa2141 commit d01c538

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

tests/kernel/usage/thread_runtime_stats/src/test_thread_runtime_stats.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,16 @@ ZTEST(usage_api, test_all_stats_usage)
100100

101101
/*
102102
* Verify that before the system idles for 2 ticks that
103-
* [execution_cycles] is increasing, [total_cycles] matches
103+
* [execution_cycles] is increasing, [total_cycles + idle_cycles] matches
104104
* [execution_cycles] and [idle_cycles] is not changing (as the
105-
* system has been idle yet.
105+
* system is not going to idle during that test).
106106
*/
107107

108108
zassert_true(stats2.execution_cycles > stats1.execution_cycles);
109109
zassert_true(stats3.execution_cycles > stats2.execution_cycles);
110-
zassert_true(stats1.execution_cycles == stats1.total_cycles);
111-
zassert_true(stats2.execution_cycles == stats2.total_cycles);
112-
zassert_true(stats3.execution_cycles == stats3.total_cycles);
110+
zassert_true(stats1.execution_cycles == (stats1.total_cycles + stats1.idle_cycles));
111+
zassert_true(stats2.execution_cycles == (stats2.total_cycles + stats2.idle_cycles));
112+
zassert_true(stats3.execution_cycles == (stats3.total_cycles + stats3.idle_cycles));
113113
#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
114114
zassert_true(stats1.idle_cycles == stats2.idle_cycles);
115115
zassert_true(stats1.idle_cycles == stats3.idle_cycles);
@@ -122,8 +122,8 @@ ZTEST(usage_api, test_all_stats_usage)
122122
* going idle.
123123
* 1. [current_cycles] increases.
124124
* 2. [peak_cycles] matches [current_cycles].
125-
* 3. [average_cycles] is 0 (because system has not gone idle yet)
126-
* 4. [current_cycles] matches [execution_cycles].
125+
* 3. [average_cycles] is 0 if system has not gone idle yet
126+
* 4. [current_cycles] matches [execution_cycles] if system has not gone idle yet
127127
*/
128128

129129
zassert_true(stats2.current_cycles > stats1.current_cycles);
@@ -133,13 +133,19 @@ ZTEST(usage_api, test_all_stats_usage)
133133
zassert_true(stats2.peak_cycles == stats2.current_cycles);
134134
zassert_true(stats3.peak_cycles == stats3.current_cycles);
135135

136-
zassert_true(stats1.average_cycles == 0);
137-
zassert_true(stats2.average_cycles == 0);
138-
zassert_true(stats3.average_cycles == 0);
139-
140-
zassert_true(stats1.current_cycles == stats1.execution_cycles);
141-
zassert_true(stats2.current_cycles == stats2.execution_cycles);
142-
zassert_true(stats3.current_cycles == stats3.execution_cycles);
136+
if (stats1.idle_cycles == 0) {
137+
zassert_true(stats1.average_cycles == 0);
138+
zassert_true(stats2.average_cycles == 0);
139+
zassert_true(stats3.average_cycles == 0);
140+
141+
zassert_true(stats1.current_cycles == stats1.execution_cycles);
142+
zassert_true(stats2.current_cycles == stats2.execution_cycles);
143+
zassert_true(stats3.current_cycles == stats3.execution_cycles);
144+
} else {
145+
zassert_true(stats1.current_cycles < stats1.execution_cycles);
146+
zassert_true(stats2.current_cycles < stats2.execution_cycles);
147+
zassert_true(stats3.current_cycles < stats3.execution_cycles);
148+
}
143149
#endif
144150

145151
/*
@@ -172,7 +178,7 @@ ZTEST(usage_api, test_all_stats_usage)
172178
stats3.peak_cycles, IDLE_EVENT_STATS_PRECISION), NULL);
173179
zassert_true(stats4.peak_cycles == stats5.peak_cycles);
174180

175-
zassert_true(stats4.average_cycles > stats3.average_cycles);
181+
zassert_true(stats4.average_cycles > 0);
176182
zassert_true(stats5.average_cycles > stats4.average_cycles);
177183
#endif
178184

0 commit comments

Comments
 (0)