From 984eb7cd6c2872f3204c51168945a79dca6a067e Mon Sep 17 00:00:00 2001 From: JulianHolzwarth Date: Fri, 30 Aug 2019 17:29:39 +0200 Subject: [PATCH] tests/pthread_flood: test for max pthread creation --- tests/pthread_flood/Makefile | 16 +++++ tests/pthread_flood/main.c | 98 +++++++++++++++++++++++++++++ tests/pthread_flood/tests/01-run.py | 15 +++++ 3 files changed, 129 insertions(+) create mode 100644 tests/pthread_flood/Makefile create mode 100644 tests/pthread_flood/main.c create mode 100755 tests/pthread_flood/tests/01-run.py diff --git a/tests/pthread_flood/Makefile b/tests/pthread_flood/Makefile new file mode 100644 index 0000000000..89544eb304 --- /dev/null +++ b/tests/pthread_flood/Makefile @@ -0,0 +1,16 @@ +include ../Makefile.tests_common + +BOARD_INSUFFICIENT_MEMORY := nucleo-f031k6 + +BOARD_BLACKLIST := arduino-duemilanove arduino-leonardo arduino-mega2560 \ + arduino-nano arduino-uno jiminy-mega256rfr2 mega-xplained \ + waspmote-pro +# arduino mega2560 uno duemilanove : unknown type name: clockid_t +# jiminy-mega256rfr2: unknown type name: clockid_t +# mega-xplained: unknown type name: clockid_t + +USEMODULE += posix_headers +USEMODULE += pthread +CFLAGS += -DMAXTHREADS=8 + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pthread_flood/main.c b/tests/pthread_flood/main.c new file mode 100644 index 0000000000..e0ada3427d --- /dev/null +++ b/tests/pthread_flood/main.c @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2019 Freie Universität Berlin, + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief pthread test application + * + * Spawns pthreads till the scheduler's capacity is exhausted. + * + * @author Julian Holzwarth + * + * @} + */ + +#include + +#include "kernel_types.h" +#include "thread.h" +#include "pthread.h" +#include "mutex.h" + +static char dummy_stack[MAXTHREADS][THREAD_STACKSIZE_IDLE]; +static mutex_t testing_mutex; + +static void *thread_func(void *arg) +{ + (void)arg; + mutex_lock(&testing_mutex); + mutex_unlock(&testing_mutex); + return NULL; +} + +int main(void) +{ + mutex_init(&testing_mutex); + mutex_lock(&testing_mutex); + + + int pthread_cnt = 0; + + pthread_t pthread_ids[MAXTHREADS]; + + pthread_attr_t th_attr; + pthread_attr_init(&th_attr); + + if (-1 == + pthread_create(&(pthread_ids[pthread_cnt % MAXTHREADS]), &th_attr, thread_func, + NULL)) { + puts("[ERROR] cannot create pthreads"); + return 0; + } + volatile int numthread_check = sched_num_threads; + + int exit_loop = -1; + + puts("[START] Spawning threads"); + do { + pthread_attr_setstackaddr(&th_attr, + &(dummy_stack[pthread_cnt % MAXTHREADS])); + pthread_attr_setstacksize(&th_attr, THREAD_STACKSIZE_IDLE); + exit_loop = pthread_create(&(pthread_ids[pthread_cnt + 1 % MAXTHREADS]), &th_attr, + thread_func, NULL); + if (exit_loop == 0) { + ++pthread_cnt; + printf("."); + } + } while (-1 != exit_loop); + volatile int numthread_check_after = sched_num_threads; + + puts(""); + if (numthread_check_after - numthread_check == pthread_cnt && + numthread_check_after == MAXTHREADS) { + printf("[SUCCESS]\n"); + } + else { + printf("[ERROR] expected %d,", (MAXTHREADS - numthread_check)); + } + printf("created %d pthreads\n", pthread_cnt); + printf("created %d threads\n", numthread_check_after - numthread_check); + mutex_unlock(&testing_mutex); + + for (int i = 0; i < pthread_cnt; i++) { + if (pthread_ids[i] != 0) { + pthread_join(pthread_ids[i], NULL); + } + } + puts("test end"); + + return 0; +} diff --git a/tests/pthread_flood/tests/01-run.py b/tests/pthread_flood/tests/01-run.py new file mode 100755 index 0000000000..b303ebc299 --- /dev/null +++ b/tests/pthread_flood/tests/01-run.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import sys +from testrunner import run + + +def testfunc(child): + child.expect_exact(u'[START] Spawning threads') + child.expect(r'\.+') + child.expect("[SUCCESS]") + child.expect("test end") + + +if __name__ == "__main__": + sys.exit(run(testfunc))