Skip to content

Commit 8eac5c2

Browse files
committed
tests: kernel/smp: rework signals in stress test
The k_poll signal and event code is reworked a bit such that signal is raised and event is processed only when appropriate. We want to avoid changing the internal of event and signal objects at the same time we try to raise the signal (which changes the internal states too). In addition, print out some information on how many signals raised and received to indicate we are actually switching all related threads. Fixes #98136 Signed-off-by: Daniel Leung <daniel.leung@intel.com>
1 parent df8b43d commit 8eac5c2

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

tests/kernel/smp/src/main.c

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,12 @@ ZTEST(smp, test_inc_concurrency)
11111111
"total count %d is wrong(M)", global_cnt);
11121112
}
11131113

1114+
/** Keep track of how many signals raised. */
1115+
static unsigned int t_signal_raised;
1116+
1117+
/** Keep track of how many signals received per thread. */
1118+
static unsigned int t_signals_rcvd[MAX_NUM_THREADS];
1119+
11141120
/**
11151121
* @brief Torture test for context switching code
11161122
*
@@ -1125,27 +1131,56 @@ static void process_events(void *arg0, void *arg1, void *arg2)
11251131
ARG_UNUSED(arg2);
11261132

11271133
uintptr_t id = (uintptr_t) arg0;
1134+
unsigned int signaled;
1135+
int result;
11281136

11291137
while (1) {
1130-
k_poll(&tevent[id], 1, K_FOREVER);
1138+
/* Retry if no event(s) are ready.
1139+
* For example, -EINTR where polling is interrupted.
1140+
*/
1141+
if (k_poll(&tevent[id], 1, K_FOREVER) != 0) {
1142+
continue;
1143+
}
11311144

1132-
if (tevent[id].signal->result != 0x55) {
1145+
/* Grab the raised signal. */
1146+
k_poll_signal_check(tevent[id].signal, &signaled, &result);
1147+
1148+
/* Check correct result. */
1149+
if (result != 0x55) {
11331150
ztest_test_fail();
11341151
}
11351152

1136-
tevent[id].signal->signaled = 0;
1137-
tevent[id].state = K_POLL_STATE_NOT_READY;
1153+
t_signals_rcvd[id]++;
11381154

1139-
k_poll_signal_reset(&tsignal[id]);
1155+
/* Reset both event and signal. */
1156+
tevent[id].state = K_POLL_STATE_NOT_READY;
1157+
tevent[id].signal->result = 0;
1158+
k_poll_signal_reset(tevent[id].signal);
11401159
}
11411160
}
11421161

11431162
static void signal_raise(void *arg0, void *arg1, void *arg2)
11441163
{
11451164
unsigned int num_threads = arch_num_cpus();
1165+
unsigned int signaled;
1166+
int result;
1167+
1168+
t_signal_raised = 0U;
11461169

11471170
while (1) {
11481171
for (uintptr_t i = 0; i < num_threads; i++) {
1172+
/* Only raise signal when it is okay to do so.
1173+
* We don't want to raise a signal while the signal
1174+
* and the associated event are still in the process
1175+
* of being reset (see above).
1176+
*/
1177+
k_poll_signal_check(tevent[i].signal, &signaled, &result);
1178+
1179+
if (signaled != 0U) {
1180+
continue;
1181+
}
1182+
1183+
t_signal_raised++;
11491184
k_poll_signal_raise(&tsignal[i], 0x55);
11501185
}
11511186
}
@@ -1169,6 +1204,8 @@ ZTEST(smp, test_smp_switch_torture)
11691204
}
11701205

11711206
for (uintptr_t i = 0; i < num_threads; i++) {
1207+
t_signals_rcvd[i] = 0;
1208+
11721209
k_poll_signal_init(&tsignal[i]);
11731210
k_poll_event_init(&tevent[i], K_POLL_TYPE_SIGNAL,
11741211
K_POLL_MODE_NOTIFY_ONLY, &tsignal[i]);
@@ -1190,6 +1227,18 @@ ZTEST(smp, test_smp_switch_torture)
11901227
k_thread_abort(&tthread[i]);
11911228
k_thread_join(&tthread[i], K_FOREVER);
11921229
}
1230+
1231+
TC_PRINT("Total signals raised %u\n", t_signal_raised);
1232+
1233+
for (unsigned int i = 0; i < num_threads; i++) {
1234+
TC_PRINT("Thread #%d received %u signals\n", i, t_signals_rcvd[i]);
1235+
}
1236+
1237+
/* Check if we at least have done some switching. */
1238+
for (unsigned int i = 0; i < num_threads; i++) {
1239+
zassert_not_equal(0, t_signals_rcvd[i],
1240+
"Thread #%d has not received any signals", i);
1241+
}
11931242
}
11941243

11951244
/**

0 commit comments

Comments
 (0)