freertos: Migrate port tests to test app

This commit migrates the "port" tests to the test app as a component.
This commit is contained in:
Darian Leung
2022-10-15 01:37:33 +08:00
parent df4bfeee5b
commit df9aa722af
12 changed files with 12 additions and 1 deletions

View File

@@ -5,7 +5,8 @@ cmake_minimum_required(VERSION 3.16)
# split into different directores in the test app's root directory. Each test
# type is treated as separate component
set(test_types
"kernel")
"kernel"
"port")
list(APPEND EXTRA_COMPONENT_DIRS
${test_types} # Add each test type as a component

View File

@@ -0,0 +1,10 @@
# Register all of the "port" tests as a component
# In order for the cases defined by `TEST_CASE` in "port" to be linked into
# the final elf, the component can be registered as WHOLE_ARCHIVE
idf_component_register(SRC_DIRS "."
PRIV_REQUIRES test_utils
WHOLE_ARCHIVE)
# Todo: Fix no-format errors
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

View File

@@ -0,0 +1,61 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Helper function for the test case in test_context_save_clobber.c */
#if defined(__XTENSA__)
#include "xtensa/config/core-isa.h"
#if defined(XCHAL_HAVE_WINDOWED)
.data
recursion_count:
.word 0
.text
.global test_context_save_clober_func
.type test_context_save_clober_func,@function
.align 4
/* This function recursively calls itself via call4, making sure each frame
* uses only 4 registers. For this reason, recursion count can not be
* a function argument (it would have to be in a6) and is placed into .data
* section above.
*/
test_context_save_clober_func:
entry a1, 16
/* load recursion count from memory */
movi a3, recursion_count
l32i a2, a3, 0
/* if it is zero, initialize it to 16 (=64 physical registers / 4 registers per call) */
bnez a2, 1f
movi a2, 16
1:
/* decrement the counter and write it back */
addi a2, a2, -1
s32i a2, a3, 0
/* counter not zero? do a recursive call */
beqz a2, wait
call4 test_context_save_clober_func
j end
wait:
/* Counter has reached zero, and we have 16 frames on the stack.
* Delay for a few seconds, expecting in interrupt to happen.
*/
movi a3, 100000000
1:
addi a3, a3, -1
bnez a3, 1b
end:
retw
#endif // XCHAL_HAVE_WINDOWED
#endif // __XTENSA__

View File

@@ -0,0 +1,50 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#include "unity.h"
#include "esp_intr_alloc.h"
#if defined(__XTENSA__) && CONFIG_FREERTOS_CORETIMER_0
#include "xtensa/config/core-isa.h"
#include "xtensa/hal.h"
#if defined(XCHAL_HAVE_WINDOWED)
/* Regression test for a0 register being corrupted in _xt_context_save.
*
* The idea in this test is to have a function which recursively calls itself
* with call4, eventually filling up all the register windows. At that point,
* it does some lengthy operation. If an interrupt occurs at that point, and
* corrupts a0 register of one of the windows, this will cause an exception
* when the recursive function returns.
*/
/* See test_context_save_clober_func.S */
extern void test_context_save_clober_func(void);
static void int_timer_handler(void *arg)
{
xthal_set_ccompare(1, xthal_get_ccount() + 10000);
(*(int*) arg)++;
}
TEST_CASE("context save doesn't corrupt return address register", "[freertos]")
{
/* set up an interrupt */
intr_handle_t ih;
int int_triggered = 0;
TEST_ESP_OK(esp_intr_alloc(ETS_INTERNAL_TIMER1_INTR_SOURCE, 0, int_timer_handler, &int_triggered, &ih));
xthal_set_ccompare(1, xthal_get_ccount() + 10000);
/* fill all the windows and delay a bit, waiting for an interrupt to happen */
test_context_save_clober_func();
esp_intr_free(ih);
TEST_ASSERT_GREATER_THAN(0, int_triggered);
}
#endif // XCHAL_HAVE_WINDOWED
#endif // __XTENSA__ && CONFIG_FREERTOS_CORETIMER_0

View File

@@ -0,0 +1,159 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#include <math.h>
#include "soc/soc_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "unity.h"
#include "test_utils.h"
#if SOC_CPU_HAS_FPU && CONFIG_FREERTOS_FPU_IN_ISR
// We can use xtensa API here as currently, non of the RISC-V targets have an FPU
#include "xtensa/xtensa_api.h"
#include "esp_intr_alloc.h"
#define SW_ISR_LEVEL_1 7
static void fpu_isr(void *arg)
{
// Clear the interrupt
xt_set_intclear(1 << SW_ISR_LEVEL_1);
/*
Use the FPU
- We test using a calculation that will cause a change in mantissa and exponent for extra thoroughness
- cosf(0.0f) should return 1.0f, thus we are simply doubling test_float every iteration.
- Therefore, we should end up with (0.01) * (2^8) = 2.56 at the end of the loop
*/
volatile float test_float = 0.01f;
for (int i = 0; i < 8; i++) {
test_float = test_float * 2.0f * cosf(0.0f);
}
// We allow a 0.1% delta on the final result in case of any loss of precision from floating point calculations
TEST_ASSERT_FLOAT_WITHIN(0.00256f, 2.56f, test_float);
}
/* ------------------------------------------------------------------------------------------------------------------ */
/*
Test FPU usage from a level 1 ISR
Purpose:
- Test that the FPU can be used from a level 1 ISR
- Test that the ISR using the FPU does not corrupt the interrupted task's FPU context
Procedure:
- Allocate a level 1 ISR
- Task uses the FPU then triggers the ISR
- ISR uses the FPU as well (forcing the task's FPU context to be saved)
- Task continues using the FPU (forcing its FPU context to be restored)
Expected:
- ISR should use the FPU without issue
- The interrupted task can continue using the FPU without issue
*/
TEST_CASE("FPU: Usage in level 1 ISR", "[freertos]")
{
intr_handle_t isr_handle;
TEST_ASSERT_EQUAL(ESP_OK, esp_intr_alloc(ETS_INTERNAL_SW0_INTR_SOURCE, ESP_INTR_FLAG_LEVEL1, &fpu_isr, NULL, &isr_handle));
/*
Use the FPU (calculate a different value than in the ISR)
- We test using a calculation that will cause a change in mantissa and exponent for extra thoroughness
- cosf(0.0f) should return 1.0f, thus we are simply dividing test_float every iteration.
*/
// We should end up with (2.56) / (2^4) = 0.16 at the end of the first loop
volatile float test_float = 2.56f;
for (int i = 0; i < 4; i++) {
test_float = test_float / (2.0f * cosf(0.0f));
}
// We allow a 0.1% delta on the final result in case of any loss of precision from floating point calculations
TEST_ASSERT_FLOAT_WITHIN(0.00016f, 0.16f, test_float);
// Trigger the ISR
xt_set_intset(1 << SW_ISR_LEVEL_1);
// Continue using the FPU from a task context after the interrupt returns
// We should end up with (0.16) / (2^4) = 0.01 at the end of the first loop
for (int i = 0; i < 4; i++) {
test_float = test_float / (2.0f * cosf(0.0f));
}
// We allow a 0.1% delta on the final result in case of any loss of precision from floating point calculations
TEST_ASSERT_FLOAT_WITHIN(0.00001f, 0.01f, test_float);
// Free the ISR
esp_intr_free(isr_handle);
}
/* ------------------------------------------------------------------------------------------------------------------ */
/*
Test FPU usage in ISR does not affect an unpinned tasks
Purpose:
- Test that the ISR using the FPU will not affect the interrupted task's affinity
Procedure:
- Create an unpinned task
- Unpinned task disables scheduling/preemption to ensure that it does not switch cores
- Unpinned task allocates an ISR then triggers the ISR
- The ISR interrupts the unpinned task then uses the FPU
- Task reenables scheduling/preemption and cleans up
Expected:
- The ISR using the FPU will not affect the unpinned task's affinity
*/
static void unpinned_task(void *arg)
{
// Disable scheduling/preemption to make sure the current task doesn't switch cores
#if CONFIG_FREERTOS_SMP
vTaskPreemptionDisable(NULL);
#else
vTaskSuspendAll();
#endif
// Check that the task is unpinned
#if CONFIG_FREERTOS_SMP
TEST_ASSERT_EQUAL(tskNO_AFFINITY, vTaskCoreAffinityGet(NULL));
#else
TEST_ASSERT_EQUAL(tskNO_AFFINITY, xTaskGetAffinity(NULL));
#endif
// Allocate an ISR to use the FPU
intr_handle_t isr_handle;
TEST_ASSERT_EQUAL(ESP_OK, esp_intr_alloc(ETS_INTERNAL_SW0_INTR_SOURCE, ESP_INTR_FLAG_LEVEL1, &fpu_isr, NULL, &isr_handle));
// Trigger the ISR
xt_set_intset(1 << SW_ISR_LEVEL_1);
// Free the ISR
esp_intr_free(isr_handle);
// Task should remain unpinned after the ISR uses the FPU
#if CONFIG_FREERTOS_SMP
TEST_ASSERT_EQUAL(tskNO_AFFINITY, vTaskCoreAffinityGet(NULL));
#else
TEST_ASSERT_EQUAL(tskNO_AFFINITY, xTaskGetAffinity(NULL));
#endif
// Reenable scheduling/preemption
#if CONFIG_FREERTOS_SMP
vTaskPreemptionEnable(NULL);
#else
xTaskResumeAll();
#endif
// Indicate done and self delete
xTaskNotifyGive((TaskHandle_t)arg);
vTaskDelete(NULL);
}
TEST_CASE("FPU: Level 1 ISR does not affect unpinned task", "[freertos]")
{
TaskHandle_t unity_task_handle = xTaskGetCurrentTaskHandle();
xTaskCreate(unpinned_task, "unpin", 2048, (void *)unity_task_handle, UNITY_FREERTOS_PRIORITY + 1, NULL);
// Wait for task to complete
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
vTaskDelay(10); // Short delay to allow task memory to be freed
}
#endif // SOC_CPU_HAS_FPU && CONFIG_FREERTOS_FPU_IN_ISR

View File

@@ -0,0 +1,173 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#include <math.h>
#include "soc/soc_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "unity.h"
#include "test_utils.h"
#if SOC_CPU_HAS_FPU
/* ------------------------------------------------------------------------------------------------------------------ */
/*
Test FPU usage from a task context
Purpose:
- Test that the FPU can be used from a task context
- Test that FPU context is properly saved and restored
Procedure:
- Create TEST_PINNED_NUM_TASKS tasks pinned to each core
- Start each task
- Each task updates a float variable and then blocks (to allow other tasks to run thus forcing the an FPU context
save and restore).
Expected:
- Correct float value calculated by each task
*/
#define TEST_PINNED_NUM_TASKS 3
static void pinned_task(void *arg)
{
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
/*
Use the FPU
- We test using a calculation that will cause a change in mantissa and exponent for extra thoroughness
- cosf(0.0f) should return 1.0f, thus we are simply doubling test_float every iteration.
- Therefore, we should end up with (0.01) * (2^8) = 2.56 at the end of the loop
*/
volatile float test_float = 0.01f;
for (int i = 0; i < 8; i++) {
test_float = test_float * 2.0f * cosf(0.0f);
vTaskDelay(1); // Block to cause a context switch, forcing the FPU context to be saved
}
// We allow a 0.1% delta on the final result in case of any loss of precision from floating point calculations
TEST_ASSERT_FLOAT_WITHIN(0.00256f, 2.56f, test_float);
// Indicate done wand wait to be deleted
xSemaphoreGive((SemaphoreHandle_t)arg);
vTaskSuspend(NULL);
}
TEST_CASE("FPU: Usage in task", "[freertos]")
{
SemaphoreHandle_t done_sem = xSemaphoreCreateCounting(configNUM_CORES * TEST_PINNED_NUM_TASKS, 0);
TEST_ASSERT_NOT_EQUAL(NULL, done_sem);
TaskHandle_t task_handles[configNUM_CORES][TEST_PINNED_NUM_TASKS];
// Create test tasks for each core
for (int i = 0; i < configNUM_CORES; i++) {
for (int j = 0; j < TEST_PINNED_NUM_TASKS; j++) {
TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(pinned_task, "task", 4096, (void *)done_sem, UNITY_FREERTOS_PRIORITY + 1, &task_handles[i][j], i));
}
}
// Start the created tasks simultaneously
for (int i = 0; i < configNUM_CORES; i++) {
for (int j = 0; j < TEST_PINNED_NUM_TASKS; j++) {
xTaskNotifyGive(task_handles[i][j]);
}
}
// Wait for the tasks to complete
for (int i = 0; i < configNUM_CORES * TEST_PINNED_NUM_TASKS; i++) {
xSemaphoreTake(done_sem, portMAX_DELAY);
}
// Delete the tasks
for (int i = 0; i < configNUM_CORES; i++) {
for (int j = 0; j < TEST_PINNED_NUM_TASKS; j++) {
vTaskDelete(task_handles[i][j]);
}
}
vTaskDelay(10); // Short delay to allow idle task to be free task memory and FPU contexts
vSemaphoreDelete(done_sem);
}
/* ------------------------------------------------------------------------------------------------------------------ */
/*
Test FPU usage will pin an unpinned task
Purpose:
- Test that unpinned tasks are automatically pinned to the current core on the task's first use of the FPU
Procedure:
- Create an unpinned task
- Task disables scheduling/preemption to ensure that it does not switch cores
- Task uses the FPU
- Task checks its core affinity after FPU usage
Expected:
- Task remains unpinned until its first usage of the FPU
- The task becomes pinned to the current core after first use of the FPU
*/
#if configNUM_CORES > 1
static void unpinned_task(void *arg)
{
// Disable scheduling/preemption to make sure current core ID doesn't change
#if CONFIG_FREERTOS_SMP
vTaskPreemptionDisable(NULL);
#else
vTaskSuspendAll();
#endif
BaseType_t cur_core_num = xPortGetCoreID();
// Check that the task is unpinned
#if CONFIG_FREERTOS_SMP
TEST_ASSERT_EQUAL(tskNO_AFFINITY, vTaskCoreAffinityGet(NULL));
#else
TEST_ASSERT_EQUAL(tskNO_AFFINITY, xTaskGetAffinity(NULL));
#endif
/*
Use the FPU
- We test using a calculation that will cause a change in mantissa and exponent for extra thoroughness
- cosf(0.0f) should return 1.0f, thus we are simply doubling test_float every iteration.
- Therefore, we should end up with (0.01) * (2^8) = 2.56 at the end of the loop
*/
volatile float test_float = 0.01f;
for (int i = 0; i < 8; i++) {
test_float = test_float * 2.0f * cosf(0.0f);
}
// We allow a 0.1% delta on the final result in case of any loss of precision from floating point calculations
TEST_ASSERT_FLOAT_WITHIN(0.00256f, 2.56f, test_float);
#if CONFIG_FREERTOS_SMP
TEST_ASSERT_EQUAL(1 << cur_core_num, vTaskCoreAffinityGet(NULL));
#else
TEST_ASSERT_EQUAL(cur_core_num, xTaskGetAffinity(NULL));
#endif
// Reenable scheduling/preemption
#if CONFIG_FREERTOS_SMP
vTaskPreemptionEnable(NULL);
#else
xTaskResumeAll();
#endif
// Indicate done and self delete
xTaskNotifyGive((TaskHandle_t)arg);
vTaskDelete(NULL);
}
TEST_CASE("FPU: Usage in unpinned task", "[freertos]")
{
TaskHandle_t unity_task_handle = xTaskGetCurrentTaskHandle();
// Create unpinned task
xTaskCreate(unpinned_task, "unpin", 4096, (void *)unity_task_handle, UNITY_FREERTOS_PRIORITY + 1, NULL);
// Wait for task to complete
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
vTaskDelay(10); // Short delay to allow task memory to be freed
}
#endif // configNUM_CORES > 1
#endif // SOC_CPU_HAS_FPU

View File

@@ -0,0 +1,94 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sdkconfig.h"
#ifndef CONFIG_FREERTOS_SMP
/*
Note: We disable this test when using the FreeRTOS SMP kernel as the port will already provide
a definition for vApplicationTickHook(). Thus this test cannot be run.
*/
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <freertos/semphr.h>
#include "unity.h"
#include "test_utils.h"
/*
Test FreeRTOS idle hook. Only compiled in if FreeRTOS idle hooks are enabled.
*/
#if ( configUSE_IDLE_HOOK == 1 )
static volatile unsigned idle_count;
void vApplicationIdleHook(void)
{
idle_count++;
}
TEST_CASE("FreeRTOS idle hook", "[freertos]")
{
idle_count = 0;
vTaskDelay(10);
TEST_ASSERT_NOT_EQUAL(0, idle_count); // The legacy idle hook should be called at least once
}
#endif // configUSE_IDLE_HOOK
/*
Test the FreeRTOS tick hook. Only compiled in if FreeRTOS tick hooks are enabled.
*/
#if ( configUSE_TICK_HOOK == 1 )
static volatile unsigned tick_count;
void vApplicationTickHook(void)
{
tick_count++;
}
TEST_CASE("FreeRTOS tick hook", "[freertos]")
{
unsigned before = xTaskGetTickCount();
const unsigned SLEEP_FOR = 20;
tick_count = before;
vTaskDelay(SLEEP_FOR);
TEST_ASSERT_UINT32_WITHIN_MESSAGE(3 * portNUM_PROCESSORS, before + SLEEP_FOR * portNUM_PROCESSORS, tick_count,
"The FreeRTOS tick hook should have been called approx 1 time per tick per CPU");
}
#endif // configUSE_TICK_HOOK
#if CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP
static volatile void *deleted_tcb;
static void taskDeletesItself(void *ignored)
{
vTaskDelete(NULL);
}
void vPortCleanUpTCB(void *pxTCB)
{
deleted_tcb = pxTCB;
}
TEST_CASE("static task cleanup hook is called based on config", "[freertos]")
{
for(int i = 0; i < portNUM_PROCESSORS; i++) {
printf("Creating task CPU %d\n", i);
TaskHandle_t new_task = NULL;
deleted_tcb = NULL;
xTaskCreatePinnedToCore(taskDeletesItself, "delete", 2048, NULL, UNITY_FREERTOS_PRIORITY, &new_task, i);
vTaskDelay(5);
TEST_ASSERT_EQUAL_PTR(deleted_tcb, new_task); // TCB & TaskHandle are the same in FreeRTOS
}
}
#endif // CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP
#endif // CONFIG_FREERTOS_SMP

View File

@@ -0,0 +1,64 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
See if xPortInIsrContext works
*/
#include <esp_types.h>
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "unity.h"
#include "esp_intr_alloc.h"
#include "esp_rom_sys.h"
#include "esp_freertos_hooks.h"
#if CONFIG_FREERTOS_CORETIMER_0
static volatile int in_int_context, int_handled;
static void testint(void)
{
esp_rom_printf("INT!\n");
if (xPortInIsrContext()) {
in_int_context++;
}
int_handled++;
}
static void testthread(void *arg)
{
in_int_context = 0;
int_handled = 0;
TEST_ASSERT(!xPortInIsrContext());
esp_err_t err = esp_register_freertos_tick_hook_for_cpu(testint, xPortGetCoreID());
TEST_ASSERT_EQUAL_HEX32(ESP_OK, err);
vTaskDelay(100 / portTICK_PERIOD_MS);
TEST_ASSERT(int_handled);
TEST_ASSERT(in_int_context);
esp_deregister_freertos_tick_hook_for_cpu(testint, xPortGetCoreID());
vTaskDelete(NULL);
}
TEST_CASE("xPortInIsrContext test", "[freertos]")
{
xTaskCreatePinnedToCore(testthread, "tst", 4096, NULL, 3, NULL, 0);
vTaskDelay(150 / portTICK_PERIOD_MS);
#if portNUM_PROCESSORS == 2
xTaskCreatePinnedToCore(testthread, "tst", 4096, NULL, 3, NULL, 1);
vTaskDelay(150 / portTICK_PERIOD_MS);
#endif
}
#endif

View File

@@ -0,0 +1,61 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
Test for multicore FreeRTOS. This test spins up threads, fiddles with queues etc.
*/
#include <esp_types.h>
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "unity.h"
#include "test_utils.h"
volatile static int done;
volatile static int error;
static void tskTestRand(void *pvParameters)
{
int l;
srand(0x1234);
vTaskDelay((int)pvParameters / portTICK_PERIOD_MS);
l = rand();
printf("Rand1: %d\n", l);
if (l != 869320854) {
error++;
}
vTaskDelay((int)pvParameters / portTICK_PERIOD_MS);
l = rand();
printf("Rand2: %d\n", l);
if (l != 1148737841) {
error++;
}
done++;
vTaskDelete(NULL);
}
// TODO: split this thing into separate orthogonal tests
TEST_CASE("Test for per-task non-reentrant tasks", "[freertos]")
{
done = 0;
error = 0;
const uint32_t stack_size = 3072;
xTaskCreatePinnedToCore(tskTestRand, "tsk1", stack_size, (void *)100, 3, NULL, 0);
xTaskCreatePinnedToCore(tskTestRand, "tsk2", stack_size, (void *)200, 3, NULL, 0);
xTaskCreatePinnedToCore(tskTestRand, "tsk3", stack_size, (void *)300, 3, NULL, portNUM_PROCESSORS - 1);
xTaskCreatePinnedToCore(tskTestRand, "tsk4", stack_size, (void *)400, 3, NULL, 0);
while (done != 4) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
TEST_ASSERT(error == 0);
}

View File

@@ -0,0 +1,164 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
Combined unit tests & benchmarking for spinlock "portMUX" functionality
*/
#include <esp_types.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "unity.h"
#include "esp_cpu.h"
#include "test_utils.h"
#define REPEAT_OPS 10000
static uint32_t start, end;
#define BENCHMARK_START() do { \
start = esp_cpu_get_cycle_count(); \
} while(0)
#define BENCHMARK_END(OPERATION) do { \
end = esp_cpu_get_cycle_count(); \
printf("%s took %d cycles/op (%d cycles for %d ops)\n", \
OPERATION, (end - start)/REPEAT_OPS, \
(end - start), REPEAT_OPS); \
} while(0)
TEST_CASE("portMUX spinlocks (no contention)", "[freertos]")
{
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
BENCHMARK_START();
for (int i = 0; i < REPEAT_OPS; i++) {
portENTER_CRITICAL_ISR(&mux);
portEXIT_CRITICAL_ISR(&mux);
}
BENCHMARK_END("no contention lock");
#ifdef CONFIG_FREERTOS_UNICORE
TEST_PERFORMANCE_LESS_THAN(FREERTOS_SPINLOCK_CYCLES_PER_OP_UNICORE, "%d cycles/op", ((end - start)/REPEAT_OPS));
#else
#if CONFIG_SPIRAM
TEST_PERFORMANCE_LESS_THAN(FREERTOS_SPINLOCK_CYCLES_PER_OP_PSRAM, "%d cycles/op", ((end - start)/REPEAT_OPS));
#else
TEST_PERFORMANCE_LESS_THAN(FREERTOS_SPINLOCK_CYCLES_PER_OP, "%d cycles/op", ((end - start)/REPEAT_OPS));
#endif
#endif
}
TEST_CASE("portMUX recursive locks (no contention)", "[freertos]")
{
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
BENCHMARK_START();
const int RECURSE_COUNT = 25;
for (int i = 0; i < REPEAT_OPS / RECURSE_COUNT; i++) {
for (int j = 0; j < RECURSE_COUNT; j++) {
portENTER_CRITICAL(&mux);
}
for (int j = 0; j < RECURSE_COUNT; j++) {
portEXIT_CRITICAL(&mux);
}
}
BENCHMARK_END("no contention recursive");
}
#if portNUM_PROCESSORS == 2
static volatile int shared_value;
static portMUX_TYPE *shared_mux;
static SemaphoreHandle_t done_sem;
static void task_shared_value_increment(void *ignore)
{
for (int i = 0; i < REPEAT_OPS; i++) {
portENTER_CRITICAL(shared_mux);
shared_value++;
portEXIT_CRITICAL(shared_mux);
}
xSemaphoreGive(done_sem);
vTaskDelete(NULL);
}
TEST_CASE("portMUX cross-core locking", "[freertos]")
{
shared_mux = heap_caps_malloc(sizeof(portMUX_TYPE), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
done_sem = xSemaphoreCreateCounting(2, 0);
portMUX_INITIALIZE(shared_mux);
shared_value = 0;
BENCHMARK_START();
xTaskCreatePinnedToCore(task_shared_value_increment, "INC0", 2048, NULL, UNITY_FREERTOS_PRIORITY + 1, NULL, UNITY_FREERTOS_CPU ? 0 : 1);
xTaskCreatePinnedToCore(task_shared_value_increment, "INC1", 2048, NULL, UNITY_FREERTOS_PRIORITY + 1, NULL, UNITY_FREERTOS_CPU);
for(int i = 0; i < 2; i++) {
if(!xSemaphoreTake(done_sem, 10000/portTICK_PERIOD_MS)) {
TEST_FAIL_MESSAGE("done_sem not released by test task");
}
}
BENCHMARK_END("cross-core incrementing");
vSemaphoreDelete(done_sem);
free(shared_mux);
TEST_ASSERT_EQUAL_INT(REPEAT_OPS * 2, shared_value);
}
void portmux_high_contention_test(uint32_t lock_malloc_caps)
{
const int TOTAL_TASKS = 8; /* half on each core */
shared_mux = heap_caps_malloc(sizeof(portMUX_TYPE), lock_malloc_caps);
done_sem = xSemaphoreCreateCounting(TOTAL_TASKS, 0);
portMUX_INITIALIZE(shared_mux);
shared_value = 0;
BENCHMARK_START();
for (int i = 0; i < TOTAL_TASKS / 2; i++) {
/* as each task has a higher priority than previous, expect
them to preempt the earlier created task, at least on the
other core (this core has the unity task, until that
blocks)... */
xTaskCreatePinnedToCore(task_shared_value_increment, "INC0", 2048, NULL, tskIDLE_PRIORITY + 1 + i, NULL, UNITY_FREERTOS_CPU ? 0 : 1);
xTaskCreatePinnedToCore(task_shared_value_increment, "INC1", 2048, NULL, tskIDLE_PRIORITY + 1 + i, NULL, UNITY_FREERTOS_CPU);
}
for(int i = 0; i < TOTAL_TASKS; i++) {
if(!xSemaphoreTake(done_sem, 10000/portTICK_PERIOD_MS)) {
TEST_FAIL_MESSAGE("done_sem not released by test task");
}
}
BENCHMARK_END("cross-core high contention");
vSemaphoreDelete(done_sem);
free(shared_mux);
TEST_ASSERT_EQUAL_INT(REPEAT_OPS * TOTAL_TASKS, shared_value);
}
TEST_CASE("portMUX high contention", "[freertos]")
{
portmux_high_contention_test(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
}
#if CONFIG_SPIRAM_USE_MALLOC || CONFIG_SPIRAM_USE_CAPS_ALLOC
TEST_CASE("portMUX high contention, PSRAM", "[freertos]")
{
portmux_high_contention_test(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
}
#endif// CONFIG_SPIRAM_USE_MALLOC || CONFIG_SPIRAM_USE_CAPS_ALLOC
#endif // portNUM_PROCESSORS == 2

View File

@@ -0,0 +1,126 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
Test for thread local storage support.
*/
#include <string.h>
#include <stdlib.h>
#include <esp_types.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "unity.h"
#include "test_utils.h"
#include "sdkconfig.h"
#if defined(__XTENSA__)
#define GET_THREADPTR(tp_dest) do { asm volatile ("rur.threadptr %0":"=r"(tp_dest)); } while(0)
#elif defined (__riscv)
#define GET_THREADPTR(tp_dest) do { register uint32_t _tp asm("tp"); tp_dest = _tp; } while(0)
#endif
static __thread int tl_test_var1;
static __thread uint8_t tl_test_var2 = 55;
static __thread uint16_t tl_test_var3 = 44;
static __thread uint8_t tl_test_arr_var[10];
static __thread struct test_tls_var {
int f32;
uint8_t f8;
uint16_t f16;
uint8_t farr[10];
} tl_test_struct_var;
static void task_test_tls(void *arg)
{
bool *running = (bool *)arg;
uint32_t tp = (uint32_t) -1;
int test_var1_old = 0;
uint8_t test_var2_old = 0;
uint16_t test_var3_old = 0;
int f32_old = 0;
uint8_t f8_old = 0;
uint16_t f16_old = 0;
srand((int) xTaskGetCurrentTaskHandle());
int step = (rand() % 10) + 1;
GET_THREADPTR(tp);
for (int i = 0; i < 5; i++) {
printf("Task[%x]: var = 0x%x 0x%x step=%d\n", tp, tl_test_var1, tl_test_var2, step);
if (i == 0) {
TEST_ASSERT_EQUAL(0, tl_test_var1);
TEST_ASSERT_EQUAL(55, tl_test_var2);
TEST_ASSERT_EQUAL(44, tl_test_var3);
for (int k = 0; k < sizeof(tl_test_arr_var); k++) {
TEST_ASSERT_EQUAL(0, tl_test_arr_var[k]);
}
TEST_ASSERT_EQUAL(0, tl_test_struct_var.f32);
TEST_ASSERT_EQUAL(0, tl_test_struct_var.f8);
TEST_ASSERT_EQUAL(0, tl_test_struct_var.f16);
for (int k = 0; k < sizeof(tl_test_struct_var.farr); k++) {
TEST_ASSERT_EQUAL(0, tl_test_struct_var.farr[k]);
}
} else {
TEST_ASSERT_EQUAL(test_var1_old + step, tl_test_var1);
TEST_ASSERT_EQUAL(test_var2_old + step, tl_test_var2);
TEST_ASSERT_EQUAL(test_var3_old + step, tl_test_var3);
for (int k = 0; k < sizeof(tl_test_arr_var); k++) {
TEST_ASSERT_EQUAL((i - 1) * step, tl_test_arr_var[k]);
}
TEST_ASSERT_EQUAL(f32_old + step, tl_test_struct_var.f32);
TEST_ASSERT_EQUAL(f8_old + step, tl_test_struct_var.f8);
TEST_ASSERT_EQUAL(f16_old + step, tl_test_struct_var.f16);
for (int k = 0; k < sizeof(tl_test_struct_var.farr); k++) {
TEST_ASSERT_EQUAL((i - 1) * step, tl_test_struct_var.farr[k]);
}
}
test_var1_old = tl_test_var1;
test_var2_old = tl_test_var2;
test_var3_old = tl_test_var3;
f32_old = tl_test_struct_var.f32;
f8_old = tl_test_struct_var.f8;
f16_old = tl_test_struct_var.f16;
tl_test_var1 += step;
tl_test_var2 += step;
tl_test_var3 += step;
memset(tl_test_arr_var, i * step, sizeof(tl_test_arr_var));
tl_test_struct_var.f32 += step;
tl_test_struct_var.f8 += step;
tl_test_struct_var.f16 += step;
memset(tl_test_struct_var.farr, i * step, sizeof(tl_test_struct_var.farr));
vTaskDelay(10);
}
if (running) {
*running = false;
vTaskDelete(NULL);
}
}
TEST_CASE("TLS test", "[freertos]")
{
const size_t stack_size = 3072;
StackType_t s_stack[stack_size]; /* with 8KB test task stack (default) this test still has ~3KB headroom */
StaticTask_t s_task;
bool running[2] = {true, true};
#if CONFIG_FREERTOS_UNICORE == 0
int other_core = 1;
#else
int other_core = 0;
#endif
xTaskCreatePinnedToCore((TaskFunction_t)&task_test_tls, "task_test_tls", stack_size, &running[0],
UNITY_FREERTOS_PRIORITY, NULL, 0);
xTaskCreateStaticPinnedToCore((TaskFunction_t)&task_test_tls, "task_test_tls", stack_size, &running[1],
UNITY_FREERTOS_PRIORITY, s_stack, &s_task, other_core);
while (running[0] || running[1]) {
vTaskDelay(10);
}
vTaskDelay(10); /* Make sure idle task can clean up s_task, before it goes out of scope */
}

View File

@@ -0,0 +1,132 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
Test for LoadStore exception handlers. This test performs unaligned load and store in 32bit aligned addresses
*/
#include <esp_types.h>
#include <stdio.h>
#include <esp_heap_caps.h>
#include "sdkconfig.h"
#include "esp_random.h"
#include "unity.h"
#if CONFIG_IDF_TARGET_ARCH_XTENSA
#include "freertos/xtensa_api.h"
#ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
TEST_CASE("LoadStore Exception handler", "[freertos]")
{
int32_t val0 = 0xDEADBEEF;
int32_t val1 = 0xBBAA9988;
int32_t val2 = 0x77665544;
int32_t val3 = 0x33221100;
int8_t val8_0 = val0 & 0xff;
int8_t val8_1 = val1 & 0xff;
int8_t val8_2 = val2 & 0xff;
int8_t val8_3 = val3 & 0xff;
int16_t val16_0 = val0 & 0xffff;
int16_t val16_1 = val1 & 0xffff;
int16_t val16_2 = val2 & 0xffff;
int16_t val16_3 = val3 & 0xffff;
uint32_t largest_free = heap_caps_get_largest_free_block(MALLOC_CAP_IRAM_8BIT);
int8_t *arr = heap_caps_malloc(largest_free * sizeof(int8_t), MALLOC_CAP_IRAM_8BIT);
TEST_ASSERT(arr != NULL);
int8_t *arr8 = arr;
int16_t *arr16 = (int16_t *)arr;
int32_t *arr32 = (int32_t *)arr;
for (int i = 0; i < 1024; i++) {
// LoadStoreError
uint32_t offset = esp_random() % (largest_free - 20);
uint32_t offset8, offset16, offset32;
// Get word aligned offset
offset8 = offset & ~3;
offset16 = offset8 / 2;
offset32 = offset8 / 4;
arr8[offset8] = val8_0;
arr8[offset8+1] = val8_1;
arr8[offset8+2] = val8_2;
arr8[offset8+3] = val8_3;
// Just to make sure compiler doesn't read stale data
asm volatile("memw\n");
TEST_ASSERT_EQUAL(val8_0, arr8[offset8]);
TEST_ASSERT_EQUAL(val8_1, arr8[offset8+1]);
TEST_ASSERT_EQUAL(val8_2, arr8[offset8+2]);
TEST_ASSERT_EQUAL(val8_3, arr8[offset8+3]);
arr16[offset16] = val16_0;
arr16[offset16+1] = val16_1;
arr16[offset16+2] = val16_2;
arr16[offset16+3] = val16_3;
// Just to make sure compiler doesn't read stale data
asm volatile("memw\n");
TEST_ASSERT_EQUAL(val16_0, arr16[offset16]);
TEST_ASSERT_EQUAL(val16_1, arr16[offset16+1]);
TEST_ASSERT_EQUAL(val16_2, arr16[offset16+2]);
TEST_ASSERT_EQUAL(val16_3, arr16[offset16+3]);
// LoadStoreAlignement Error
// Check that it doesn't write to adjacent bytes
int8_t *ptr8_0 = (void *)&arr8[offset8];
int8_t *ptr8_1 = (void *)&arr8[offset8] + 5;
int8_t *ptr8_2 = (void *)&arr8[offset8] + 10;
int8_t *ptr8_3 = (void *)&arr8[offset8] + 15;
*ptr8_0 = 0x73;
*ptr8_1 = 0x73;
*ptr8_2 = 0x73;
*ptr8_3 = 0x73;
int16_t *ptr16_0 = (void *)&arr16[offset16] + 1;
int16_t *ptr16_1 = (void *)&arr16[offset16] + 3;
*ptr16_0 = val16_0;
*ptr16_1 = val16_1;
// Just to make sure compiler doesn't read stale data
asm volatile("memw\n");
TEST_ASSERT_EQUAL(val16_0, *ptr16_0);
TEST_ASSERT_EQUAL(0x73, *ptr8_0);
TEST_ASSERT_EQUAL(val16_1, *ptr16_1);
TEST_ASSERT_EQUAL(0x73, *ptr8_1);
int32_t *ptr32_0 = (void *)&arr32[offset32] + 1;
int32_t *ptr32_1 = (void *)&arr32[offset32] + 6;
int32_t *ptr32_2 = (void *)&arr32[offset32] + 11;
*ptr32_0 = val0;
*ptr32_1 = val1;
*ptr32_2 = val2;
// Just to make sure compiler doesn't read stale data
asm volatile ("memw");
TEST_ASSERT_EQUAL(0x73, *ptr8_0);
TEST_ASSERT_EQUAL(val0, *ptr32_0);
TEST_ASSERT_EQUAL(0x73, *ptr8_1);
TEST_ASSERT_EQUAL(val1, *ptr32_1);
TEST_ASSERT_EQUAL(0x73, *ptr8_2);
TEST_ASSERT_EQUAL(val2, *ptr32_2);
TEST_ASSERT_EQUAL(0x73, *ptr8_3);
}
TEST_ASSERT_TRUE(heap_caps_check_integrity_all(true));
heap_caps_free(arr);
}
#endif // CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
#endif // CONFIG_IDF_TARGET_ARCH_XTENSA