Skip to content

Commit 688b561

Browse files
nordic-krchfabiobaltieri
authored andcommitted
tests: debug: Add test for cpu_load
Add test for CPU load. Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
1 parent 0ec2ad5 commit 688b561

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(debug_cpu_load)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/ {
2+
chosen {
3+
zephyr,cpu-load-counter = &timer0;
4+
};
5+
};
6+
7+
&timer0 {
8+
status = "okay";
9+
prescaler = <0>;
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/ {
2+
chosen {
3+
zephyr,cpu-load-counter = &timer130;
4+
};
5+
};
6+
7+
&timer130 {
8+
status = "okay";
9+
prescaler = <0>;
10+
};

tests/subsys/debug/cpu_load/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_CPU_LOAD=y
3+
CONFIG_CPU_LOAD_USE_COUNTER=n
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2024, Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/debug/cpu_load.h>
9+
#include <zephyr/ztest.h>
10+
#include <zephyr/logging/log_ctrl.h>
11+
#include <zephyr/logging/log_backend.h>
12+
13+
#include <zephyr/drivers/counter.h>
14+
15+
#define DELTA 30
16+
17+
ZTEST(cpu_load, test_load)
18+
{
19+
int load;
20+
uint32_t t_ms = 100;
21+
22+
if (CONFIG_CPU_LOAD_LOG_PERIODICALLY > 0) {
23+
cpu_load_log_control(false);
24+
}
25+
26+
/* Reset the measurement */
27+
(void)cpu_load_get(true);
28+
k_busy_wait(t_ms * USEC_PER_MSEC);
29+
30+
/* Measurement is not reset. */
31+
load = cpu_load_get(false);
32+
/* Result in per mille */
33+
zassert_within(load, 1000, DELTA);
34+
35+
k_msleep(t_ms);
36+
load = cpu_load_get(false);
37+
zassert_within(load, 500, DELTA);
38+
39+
/* Reset the measurement */
40+
(void)cpu_load_get(true);
41+
k_msleep(t_ms);
42+
load = cpu_load_get(false);
43+
zassert_within(load, 0, DELTA);
44+
}
45+
46+
#if CONFIG_CPU_LOAD_LOG_PERIODICALLY > 0
47+
static int cpu_load_src_id;
48+
static atomic_t log_cnt;
49+
50+
static void process(const struct log_backend *const backend,
51+
union log_msg_generic *msg)
52+
{
53+
ARG_UNUSED(backend);
54+
const void *source = msg->log.hdr.source;
55+
int source_id = log_const_source_id((const struct log_source_const_data *)source);
56+
57+
if (source_id == cpu_load_src_id) {
58+
atomic_inc(&log_cnt);
59+
}
60+
}
61+
62+
static void init(const struct log_backend *const backend)
63+
{
64+
ARG_UNUSED(backend);
65+
}
66+
67+
const struct log_backend_api mock_log_backend_api = {
68+
.process = process,
69+
.init = init
70+
};
71+
72+
LOG_BACKEND_DEFINE(dummy, mock_log_backend_api, false, NULL);
73+
74+
ZTEST(cpu_load, test_periodic_report)
75+
{
76+
log_backend_enable(&dummy, NULL, LOG_LEVEL_INF);
77+
cpu_load_log_control(true);
78+
79+
cpu_load_src_id = log_source_id_get(STRINGIFY(cpu_load));
80+
atomic_set(&log_cnt, 0);
81+
k_msleep(3 * CONFIG_CPU_LOAD_LOG_PERIODICALLY);
82+
zassert_within(log_cnt, 3, 1);
83+
84+
cpu_load_log_control(false);
85+
k_msleep(1);
86+
atomic_set(&log_cnt, 0);
87+
k_msleep(3 * CONFIG_CPU_LOAD_LOG_PERIODICALLY);
88+
zassert_equal(log_cnt, 0);
89+
90+
cpu_load_log_control(true);
91+
k_msleep(3 * CONFIG_CPU_LOAD_LOG_PERIODICALLY);
92+
zassert_within(log_cnt, 3, 1);
93+
94+
cpu_load_log_control(false);
95+
log_backend_disable(&dummy);
96+
}
97+
#endif /* CONFIG_CPU_LOAD_LOG_PERIODICALLY > 0 */
98+
99+
ZTEST_SUITE(cpu_load, NULL, NULL, NULL, NULL, NULL);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
common:
2+
tags: cpu_load
3+
arch_allow: arm
4+
filter: CONFIG_CPU_CORTEX_M
5+
tests:
6+
debug.cpu_load:
7+
integration_platforms:
8+
- mps2/an385
9+
debug.cpu_load.counter:
10+
platform_allow:
11+
- nrf52840dk/nrf52840
12+
- nrf54h20dk/nrf54h20/cpuapp
13+
integration_platforms:
14+
- nrf52840dk/nrf52840
15+
extra_configs:
16+
- CONFIG_CPU_LOAD_USE_COUNTER=y
17+
debug.cpu_load.periodic_report:
18+
integration_platforms:
19+
- mps2/an385
20+
extra_configs:
21+
- CONFIG_CPU_LOAD_LOG_PERIODICALLY=50
22+
- CONFIG_LOG=y
23+
- CONFIG_TEST_LOGGING_DEFAULTS=n
24+
- CONFIG_LOG_PRINTK=n
25+
- CONFIG_LOG_BACKEND_UART=n
26+
- CONFIG_LOG_MODE_IMMEDIATE=y

0 commit comments

Comments
 (0)