Zephyr Project API  3.2.0
A Scalable Open Source RTOS
ztress.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef TESTSUITE_ZTEST_INCLUDE_ZTRESS_H__
7#define TESTSUITE_ZTEST_INCLUDE_ZTRESS_H__
8
9#include <zephyr/sys/util.h>
10#include <zephyr/kernel.h>
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
17#define ZTRESS_ID_THREAD 0
18#define ZTRESS_ID_K_TIMER 1
19
39#define ZTRESS_TIMER(handler, user_data, exec_cnt, init_timeout) \
40 (ZTRESS_ID_K_TIMER, handler, user_data, exec_cnt, 0, init_timeout)
41
64#define ZTRESS_THREAD(handler, user_data, exec_cnt, preempt_cnt, init_timeout) \
65 (ZTRESS_ID_THREAD, handler, user_data, exec_cnt, preempt_cnt, init_timeout)
66
81typedef bool (*ztress_handler)(void *user_data, uint32_t cnt, bool last, int prio);
82
85 /* Handler. */
87
88 /* User data */
89 void *user_data;
90
91 /* Minimum number of executions to complete the test. */
93
94 /* Minimum number of preemptions to complete the test. Valid only for
95 * thread context.
96 */
98
99 /* Initial timeout. */
101};
102
114#define ZTRESS_CONTEXT_INITIALIZER(_handler, _user_data, _exec_cnt, _preempt_cnt, _t) \
115 { \
116 .handler = (_handler), \
117 .user_data = (_user_data), \
118 .exec_cnt = (_exec_cnt), \
119 .preempt_cnt = (_preempt_cnt), \
120 .t = (_t) \
121 }
122
124#define Z_ZTRESS_GET_HANDLER_DATA2(_, ...) \
125 ZTRESS_CONTEXT_INITIALIZER(__VA_ARGS__)
126
128#define Z_ZTRESS_GET_HANDLER_DATA(data) \
129 Z_ZTRESS_GET_HANDLER_DATA2 data
130
132#define Z_ZTRESS_HAS_TIMER(data, ...) \
133 GET_ARG_N(1, __DEBRACKET data)
134
138#define Z_ZTRESS_TIMER_IDX(idx, data) \
139 ((GET_ARG_N(1, __DEBRACKET data)) == ZTRESS_ID_K_TIMER ? idx : 0)
140
144#define Z_ZTRESS_TIMER_CONTEXT_VALIDATE(...) \
145 BUILD_ASSERT((FOR_EACH_IDX(Z_ZTRESS_TIMER_IDX, (+), __VA_ARGS__)) == 0, \
146 "There can only be up to one ZTRESS_TIMER context and it must " \
147 "be the first in the list")
148
159#define ZTRESS_EXECUTE(...) do { \
160 Z_ZTRESS_TIMER_CONTEXT_VALIDATE(__VA_ARGS__); \
161 int has_timer = Z_ZTRESS_HAS_TIMER(__VA_ARGS__); \
162 struct ztress_context_data data1[] = { \
163 FOR_EACH(Z_ZTRESS_GET_HANDLER_DATA, (,), __VA_ARGS__) \
164 }; \
165 size_t cnt = ARRAY_SIZE(data1) - has_timer; \
166 static struct ztress_context_data data[ARRAY_SIZE(data1)]; \
167 for (int i = 0; i < ARRAY_SIZE(data1); i++) { \
168 data[i] = data1[i]; \
169 } \
170 int err = ztress_execute(has_timer ? &data[0] : NULL, &data[has_timer], cnt); \
171 \
172 zassert_equal(err, 0, "ztress_execute failed (err: %d)", err); \
173} while (0)
174
192 size_t cnt);
193
195void ztress_abort(void);
196
205
211void ztress_report(void);
212
220
228
239
240#ifdef __cplusplus
241}
242#endif
243
244#endif /* TESTSUITE_ZTEST_INCLUDE_ZTRESS_H__ */
Public kernel APIs.
struct k_thread t
Definition: kobject.c:1327
#define bool
Definition: stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
Kernel timeout type.
Definition: sys_clock.h:65
Definition: test_sched.h:21
Definition: clock.c:25
Definition: ztress.h:84
k_timeout_t t
Definition: ztress.h:100
uint32_t preempt_cnt
Definition: ztress.h:97
void * user_data
Definition: ztress.h:89
ztress_handler handler
Definition: ztress.h:86
uint32_t exec_cnt
Definition: ztress.h:92
static const intptr_t user_data[5]
Definition: main.c:588
Misc utilities.
int ztress_preempt_count(uint32_t id)
Get number of preemptions of a given context in the last test.
void ztress_abort(void)
Abort ongoing stress test.
void ztress_set_timeout(k_timeout_t t)
Set test timeout.
bool(* ztress_handler)(void *user_data, uint32_t cnt, bool last, int prio)
User handler called in one of the configured contexts.
Definition: ztress.h:81
int ztress_exec_count(uint32_t id)
Get number of executions of a given context in the last test.
uint32_t ztress_optimized_ticks(uint32_t id)
Get optimized timeout base of a given context in the last test.
void ztress_report(void)
Print last test report.
int ztress_execute(struct ztress_context_data *timer_data, struct ztress_context_data *thread_data, size_t cnt)