Skip to content

Commit

Permalink
tests: Add thread suspend-resume SMP stress test
Browse files Browse the repository at this point in the history
Adds a test to stress k_thread_suspend() and k_thread_resume() on
an SMP system. The test should take about 30 seconds.

Signed-off-by: Peter Mitsis <[email protected]>
  • Loading branch information
peter-mitsis authored and carlescufi committed Jan 11, 2024
1 parent 1e6ff5f commit 9852e8e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tests/kernel/smp_suspend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(smp)

target_sources(app PRIVATE src/main.c)

target_include_directories(app PRIVATE
${ZEPHYR_BASE}/kernel/include
${ZEPHYR_BASE}/arch/${ARCH}/include
)
2 changes: 2 additions & 0 deletions tests/kernel/smp_suspend/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_SMP=y
85 changes: 85 additions & 0 deletions tests/kernel/smp_suspend/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2024 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/tc_util.h>
#include <zephyr/ztest.h>
#include <zephyr/kernel.h>

#if CONFIG_MP_MAX_NUM_CPUS < 2
#error "SMP test requires at least two CPUs!"
#endif

#define STACK_SIZE 1024

#define NUM_THREADS 6

K_THREAD_STACK_ARRAY_DEFINE(thread_stack, NUM_THREADS, STACK_SIZE);
struct k_thread thread[NUM_THREADS];

volatile uint64_t thread_counter[NUM_THREADS];

static void thread_entry(void *p1, void *p2, void *p3)
{
struct k_thread *resume_thread = p1;
unsigned int self_index = (unsigned int)(uintptr_t)p2;

while (1) {
if (resume_thread != NULL) {
k_thread_resume(resume_thread);
}

thread_counter[self_index]++;

if (self_index != 0) {
k_thread_suspend(k_current_get());
}
}
}

ZTEST(smp_suspend_resume, test_smp_thread_suspend_resume_stress)
{
unsigned int i;
uint64_t counter[NUM_THREADS] = {};

/* Create the threads */

printk("Starting ...\n");

for (i = 0; i < NUM_THREADS; i++) {

k_thread_create(&thread[i], thread_stack[i],
STACK_SIZE, thread_entry,
i < (NUM_THREADS - 1) ? &thread[i + 1] : NULL,
(void *)(uintptr_t)i, NULL,
10 - i, 0, K_FOREVER);

k_thread_suspend(&thread[i]);

k_thread_start(&thread[i]);
}

/*
* All newly created test threads are currently in the suspend state.
* Start the first thread.
*/

k_thread_resume(&thread[0]);

for (unsigned int iteration = 0; iteration < 30; iteration++) {
k_sleep(K_MSEC(1000));

for (i = 0; i < NUM_THREADS; i++) {
zassert_false(counter[i] == thread_counter[i],
" -- Thread %u appears to be hung: %llu\n",
i, thread_counter[i]);

counter[i] = thread_counter[i];

}
}
}

ZTEST_SUITE(smp_suspend_resume, NULL, NULL, NULL, NULL, NULL);
6 changes: 6 additions & 0 deletions tests/kernel/smp_suspend/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tests:
kernel.smp_suspend:
tags:
- kernel
- smp
filter: (CONFIG_MP_MAX_NUM_CPUS > 1)

0 comments on commit 9852e8e

Please sign in to comment.