From 92b66e4281b27eb1143e774a542a29968bc3b6c8 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 19 Apr 2025 09:54:33 -0400 Subject: [PATCH 1/4] posix: semaphores: use a default minimal heap-add for semaphores The implementation of POSIX_SEMAPHORES historically used heap allocation and has not yet been transitioned to a pool allocator. However, since 590258b3814, the default heap-add with CONFIG_POSIX_API has been reduced from 1 kiB which causes tests/posix/semaphores to fail due to NULL being returned from a call to k_calloc(). Create a minimal heap-add for the POSIX_SEMAPHORES Option Group. This can be removed at a future date if semaphores are changed to use a pooled allocator and fixed-size name, rather than heap allocation. Signed-off-by: Chris Friedt --- lib/posix/options/Kconfig.semaphore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/posix/options/Kconfig.semaphore b/lib/posix/options/Kconfig.semaphore index b6b839c9b1fe..b2640d970411 100644 --- a/lib/posix/options/Kconfig.semaphore +++ b/lib/posix/options/Kconfig.semaphore @@ -33,4 +33,7 @@ config POSIX_SEM_NAMELEN_MAX Maximum length of name for a named semaphore. The max value of 255 corresponds to {NAME_MAX}. +config HEAP_MEM_POOL_ADD_SIZE_POSIX_SEMAPHORES + def_int 256 + endif # POSIX_SEMAPHORES From d0de2ef00c7924da002e9ad7bd9a01cd99869e1c Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 19 Apr 2025 10:02:22 -0400 Subject: [PATCH 2/4] tests: posix: semaphores: reduce execution time by 5s There is no reason to use a 5s wait time in tests. This can add up quite significantly across multiple platforms that execute in real-time under qemu (or even just on real hardware). Speedup observed with qemu_cortex_a53/qemu_cortex_a53/smp Before: ``` START - test_named_semaphore PASS - test_named_semaphore in 5.688 seconds ``` After: ``` START - test_named_semaphore PASS - test_named_semaphore in 0.783 seconds ``` Signed-off-by: Chris Friedt --- tests/posix/semaphores/src/main.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/posix/semaphores/src/main.c b/tests/posix/semaphores/src/main.c index 228165fb131f..a44571fed865 100644 --- a/tests/posix/semaphores/src/main.c +++ b/tests/posix/semaphores/src/main.c @@ -13,6 +13,9 @@ #include #include +#define WAIT_TIME_MS 100 +BUILD_ASSERT(WAIT_TIME_MS > 0, "WAIT_TIME_MS must be posistive"); + static void *child_func(void *p1) { sem_t *sem = (sem_t *)p1; @@ -51,11 +54,10 @@ static void semaphore_test(sem_t *sem) zassert_equal(clock_gettime(CLOCK_REALTIME, &abstime), 0, "clock_gettime failed"); - abstime.tv_sec += 5; + abstime.tv_sec += WAIT_TIME_MS / MSEC_PER_SEC; + abstime.tv_nsec += (WAIT_TIME_MS % MSEC_PER_SEC) * NSEC_PER_MSEC; - /* TESPOINT: Wait for 5 seconds and acquire sema given - * by thread1 - */ + /* TESPOINT: Wait to acquire sem given by thread1 */ zassert_equal(sem_timedwait(sem, &abstime), 0); /* TESTPOINT: Semaphore is already acquired, check if From 20b8322f0c8f5977e559800c280b1127ab20c60d Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Sat, 19 Apr 2025 10:07:00 -0400 Subject: [PATCH 3/4] tests: posix: semaphores: coalesce string constants Just a formatting change, in a separate commit. Signed-off-by: Chris Friedt --- tests/posix/semaphores/src/main.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/posix/semaphores/src/main.c b/tests/posix/semaphores/src/main.c index a44571fed865..24cbb85b32af 100644 --- a/tests/posix/semaphores/src/main.c +++ b/tests/posix/semaphores/src/main.c @@ -73,9 +73,7 @@ static void semaphore_test(sem_t *sem) zassert_equal(sem_getvalue(sem, &val), 0); zassert_equal(val, 1); - zassert_equal(sem_destroy(sem), -1, - "acquired semaphore" - " is destroyed"); + zassert_equal(sem_destroy(sem), -1, "acquired semaphore is destroyed"); zassert_equal(errno, EBUSY); /* TESTPOINT: take semaphore which is initialized with 1 */ From a6a106c19ad2296394f1821b170bd8f319dcabd7 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Thu, 17 Apr 2025 09:27:36 -0400 Subject: [PATCH 4/4] tests: posix: semaphores: ensure test is not skipped Commit f7633a55aa098de8409724f3a6211af4a4687101 moved the tests for the POSIX_SEMAPHORES Option Group from the tests/posix/common testsuite to its own dedicated testsuite. However, there was a copy-paste error. Previously, tests would have been run only once when dynamic threads were enabled, and then skipped when dynamic threads were disabled, since that follows the posix programming model better. However, dynamic threads were never actually enabled after moving to the new testsuite. So all tests were effectively skipped. Add the necessary options to prj.conf in order to ensure that there are sufficient dynamic threads available to run the testsuite. Signed-off-by: Chris Friedt --- tests/posix/semaphores/prj.conf | 4 ++++ tests/posix/semaphores/src/main.c | 15 ++++----------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/tests/posix/semaphores/prj.conf b/tests/posix/semaphores/prj.conf index 5376678d6a90..49b867610a0c 100644 --- a/tests/posix/semaphores/prj.conf +++ b/tests/posix/semaphores/prj.conf @@ -3,3 +3,7 @@ CONFIG_ZTEST=y CONFIG_POSIX_AEP_CHOICE_BASE=y CONFIG_POSIX_SEMAPHORES=y + +CONFIG_DYNAMIC_THREAD=y +CONFIG_DYNAMIC_THREAD_POOL_SIZE=2 +CONFIG_THREAD_STACK_INFO=y diff --git a/tests/posix/semaphores/src/main.c b/tests/posix/semaphores/src/main.c index 24cbb85b32af..c99e0fca1e95 100644 --- a/tests/posix/semaphores/src/main.c +++ b/tests/posix/semaphores/src/main.c @@ -16,6 +16,9 @@ #define WAIT_TIME_MS 100 BUILD_ASSERT(WAIT_TIME_MS > 0, "WAIT_TIME_MS must be posistive"); +/* based on the current structure of this unit test */ +BUILD_ASSERT(CONFIG_DYNAMIC_THREAD_POOL_SIZE >= 2, "CONFIG_DYNAMIC_THREAD_POOL_SIZE must be >= 2"); + static void *child_func(void *p1) { sem_t *sem = (sem_t *)p1; @@ -312,14 +315,4 @@ ZTEST(posix_semaphores, test_named_semaphore) zassert_equal(nsem_get_list_len(), 0); } -static void before(void *arg) -{ - ARG_UNUSED(arg); - - if (!IS_ENABLED(CONFIG_DYNAMIC_THREAD)) { - /* skip redundant testing if there is no thread pool / heap allocation */ - ztest_test_skip(); - } -} - -ZTEST_SUITE(posix_semaphores, NULL, NULL, before, NULL, NULL); +ZTEST_SUITE(posix_semaphores, NULL, NULL, NULL, NULL, NULL);