diff --git a/tests/bench_mutex_pingpong/Makefile b/tests/bench_mutex_pingpong/Makefile new file mode 100644 index 0000000000..85437c51a2 --- /dev/null +++ b/tests/bench_mutex_pingpong/Makefile @@ -0,0 +1,12 @@ +include ../Makefile.tests_common + +BOARD_INSUFFICIENT_MEMORY := nucleo-f031k6 + +USEMODULE += xtimer + +TEST_ON_CI_WHITELIST += all + +include $(RIOTBASE)/Makefile.include + +test: + tests/01-run.py diff --git a/tests/bench_mutex_pingpong/README.md b/tests/bench_mutex_pingpong/README.md new file mode 100644 index 0000000000..60cdc790e7 --- /dev/null +++ b/tests/bench_mutex_pingpong/README.md @@ -0,0 +1,8 @@ +# About + +In this test, one thread will repeatedly lock a mutex, while another thread +will unlock it. The result is the number of unlocks done in an interval of one +second, which amounts to half the number of incurred context switches. + +This test application intentionally duplicates code with some similar benchmark +applications in order to be able to compare code sizes. diff --git a/tests/bench_mutex_pingpong/main.c b/tests/bench_mutex_pingpong/main.c new file mode 100644 index 0000000000..965f801224 --- /dev/null +++ b/tests/bench_mutex_pingpong/main.c @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2017 Kaspar Schleiser + * + * 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 Mutex context switch benchmark + * + * @author Kaspar Schleiser + * + * @} + */ + +#include + +#include "mutex.h" +#include "thread.h" +#include "xtimer.h" + +#ifndef TEST_DURATION +#define TEST_DURATION (1000000U) +#endif + +volatile unsigned _flag = 0; +static char _stack[THREAD_STACKSIZE_MAIN]; +static mutex_t _mutex = MUTEX_INIT; + +static void _timer_callback(void*arg) +{ + (void)arg; + + _flag = 1; +} + + +static void *_second_thread(void *arg) +{ + (void)arg; + + while(1) { + mutex_lock(&_mutex); + } + + return NULL; +} + +int main(void) +{ + printf("main starting\n"); + + thread_create(_stack, + sizeof(_stack), + THREAD_PRIORITY_MAIN - 1, + THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST, + _second_thread, + NULL, + "second_thread"); + + /* lock the mutex, then yield to second_thread */ + mutex_lock(&_mutex); + thread_yield_higher(); + + xtimer_t timer; + timer.callback = _timer_callback; + + uint32_t n = 0; + + xtimer_set(&timer, TEST_DURATION); + while(!_flag) { + mutex_unlock(&_mutex); + n++; + } + + printf("{ \"result\" : %"PRIu32" }\n", n); + + return 0; +} diff --git a/tests/bench_mutex_pingpong/tests/01-run.py b/tests/bench_mutex_pingpong/tests/01-run.py new file mode 100755 index 0000000000..3a1e733980 --- /dev/null +++ b/tests/bench_mutex_pingpong/tests/01-run.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2018 Kaspar Schleiser +# 2017 Sebastian Meiling +# +# 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. + +import os +import sys + + +def testfunc(child): + child.expect(r"{ \"result\" : \d+ }") + + +if __name__ == "__main__": + sys.path.append(os.path.join(os.environ['RIOTTOOLS'], 'testrunner')) + from testrunner import run + sys.exit(run(testfunc))