tests: debug: Add test for cpu_load

Add test for CPU load.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruściński 2024-03-25 09:45:22 +01:00 committed by Fabio Baltieri
commit 688b561815
6 changed files with 156 additions and 0 deletions

View file

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

View file

@ -0,0 +1,10 @@
/ {
chosen {
zephyr,cpu-load-counter = &timer0;
};
};
&timer0 {
status = "okay";
prescaler = <0>;
};

View file

@ -0,0 +1,10 @@
/ {
chosen {
zephyr,cpu-load-counter = &timer130;
};
};
&timer130 {
status = "okay";
prescaler = <0>;
};

View file

@ -0,0 +1,3 @@
CONFIG_ZTEST=y
CONFIG_CPU_LOAD=y
CONFIG_CPU_LOAD_USE_COUNTER=n

View file

@ -0,0 +1,99 @@
/*
* Copyright (c) 2024, Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/debug/cpu_load.h>
#include <zephyr/ztest.h>
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/logging/log_backend.h>
#include <zephyr/drivers/counter.h>
#define DELTA 30
ZTEST(cpu_load, test_load)
{
int load;
uint32_t t_ms = 100;
if (CONFIG_CPU_LOAD_LOG_PERIODICALLY > 0) {
cpu_load_log_control(false);
}
/* Reset the measurement */
(void)cpu_load_get(true);
k_busy_wait(t_ms * USEC_PER_MSEC);
/* Measurement is not reset. */
load = cpu_load_get(false);
/* Result in per mille */
zassert_within(load, 1000, DELTA);
k_msleep(t_ms);
load = cpu_load_get(false);
zassert_within(load, 500, DELTA);
/* Reset the measurement */
(void)cpu_load_get(true);
k_msleep(t_ms);
load = cpu_load_get(false);
zassert_within(load, 0, DELTA);
}
#if CONFIG_CPU_LOAD_LOG_PERIODICALLY > 0
static int cpu_load_src_id;
static atomic_t log_cnt;
static void process(const struct log_backend *const backend,
union log_msg_generic *msg)
{
ARG_UNUSED(backend);
const void *source = msg->log.hdr.source;
int source_id = log_const_source_id((const struct log_source_const_data *)source);
if (source_id == cpu_load_src_id) {
atomic_inc(&log_cnt);
}
}
static void init(const struct log_backend *const backend)
{
ARG_UNUSED(backend);
}
const struct log_backend_api mock_log_backend_api = {
.process = process,
.init = init
};
LOG_BACKEND_DEFINE(dummy, mock_log_backend_api, false, NULL);
ZTEST(cpu_load, test_periodic_report)
{
log_backend_enable(&dummy, NULL, LOG_LEVEL_INF);
cpu_load_log_control(true);
cpu_load_src_id = log_source_id_get(STRINGIFY(cpu_load));
atomic_set(&log_cnt, 0);
k_msleep(3 * CONFIG_CPU_LOAD_LOG_PERIODICALLY);
zassert_within(log_cnt, 3, 1);
cpu_load_log_control(false);
k_msleep(1);
atomic_set(&log_cnt, 0);
k_msleep(3 * CONFIG_CPU_LOAD_LOG_PERIODICALLY);
zassert_equal(log_cnt, 0);
cpu_load_log_control(true);
k_msleep(3 * CONFIG_CPU_LOAD_LOG_PERIODICALLY);
zassert_within(log_cnt, 3, 1);
cpu_load_log_control(false);
log_backend_disable(&dummy);
}
#endif /* CONFIG_CPU_LOAD_LOG_PERIODICALLY > 0 */
ZTEST_SUITE(cpu_load, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,26 @@
common:
tags: cpu_load
arch_allow: arm
filter: CONFIG_CPU_CORTEX_M
tests:
debug.cpu_load:
integration_platforms:
- mps2/an385
debug.cpu_load.counter:
platform_allow:
- nrf52840dk/nrf52840
- nrf54h20dk/nrf54h20/cpuapp
integration_platforms:
- nrf52840dk/nrf52840
extra_configs:
- CONFIG_CPU_LOAD_USE_COUNTER=y
debug.cpu_load.periodic_report:
integration_platforms:
- mps2/an385
extra_configs:
- CONFIG_CPU_LOAD_LOG_PERIODICALLY=50
- CONFIG_LOG=y
- CONFIG_TEST_LOGGING_DEFAULTS=n
- CONFIG_LOG_PRINTK=n
- CONFIG_LOG_BACKEND_UART=n
- CONFIG_LOG_MODE_IMMEDIATE=y