mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
ztimer: add benchmarking tool
This commit is contained in:
parent
2b53f35a28
commit
8d6d6f2bbc
26
tests/ztimer_ondemand_benchmark/Makefile
Normal file
26
tests/ztimer_ondemand_benchmark/Makefile
Normal file
@ -0,0 +1,26 @@
|
||||
include ../Makefile.tests_common
|
||||
|
||||
# required modules
|
||||
USEMODULE += ztimer
|
||||
USEMODULE += ztimer_ondemand
|
||||
USEMODULE += ztimer_ondemand_timer
|
||||
USEMODULE += ztimer_ondemand_rtt
|
||||
USEMODULE += ztimer_ondemand_rtc
|
||||
FEATURES_REQUIRED = periph_timer
|
||||
DEFAULT_MODULE += test_utils_interactive_sync
|
||||
|
||||
# Select clock to benchmark:
|
||||
# - ztimer_usec
|
||||
USEMODULE += ztimer_usec
|
||||
# - ztimer_msec
|
||||
#USEMODULE += ztimer_msec
|
||||
# - ztimer_sec
|
||||
#USEMODULE += ztimer_sec
|
||||
|
||||
# You may sepcify the compare timer device
|
||||
#CFLAGS += '-DCOMPARE_TIMER_DEV=TIMER_DEV(2)'
|
||||
|
||||
# You may sepcify the compare timer frequency
|
||||
#CFLAGS += '-DCOMPARE_TIMER_FREQ=9750000'
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
10
tests/ztimer_ondemand_benchmark/README.md
Normal file
10
tests/ztimer_ondemand_benchmark/README.md
Normal file
@ -0,0 +1,10 @@
|
||||
ztimer_ondemand_benchmark
|
||||
=========================
|
||||
|
||||
This application measures latencies introduced by `ztimer_acquire()` and
|
||||
`ztimer_release()`. Since it is directly interacting with the underlying
|
||||
peripheral, you need at least two `periph_timer` instances if you're
|
||||
benchmarking a ztimer clock based on `ztimer_periph_timer`.
|
||||
|
||||
The application tries to select a unused timer automatically. If this selection
|
||||
process fails, users may specify device and frequency in this test's Makefile.
|
122
tests/ztimer_ondemand_benchmark/main.c
Normal file
122
tests/ztimer_ondemand_benchmark/main.c
Normal file
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (C) 2022 SSV Software Systems GmbH
|
||||
*
|
||||
* 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 Benchmarks ztimer_acquire() and ztimer_release()
|
||||
*
|
||||
* @author Juergen Fitschen <me@jue.yt>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "ztimer.h"
|
||||
#include "ztimer/config.h"
|
||||
#include "periph/timer.h"
|
||||
#include "periph_conf.h"
|
||||
|
||||
#if IS_ACTIVE(MODULE_ZTIMER_SEC)
|
||||
# define CLOCK ZTIMER_SEC
|
||||
#elif IS_ACTIVE(MODULE_ZTIMER_MSEC)
|
||||
# define CLOCK ZTIMER_MSEC
|
||||
#elif IS_ACTIVE(MODULE_ZTIMER_USEC)
|
||||
# define CLOCK ZTIMER_USEC
|
||||
# define CLOCK_IS_ZTIMER_USEC
|
||||
#else
|
||||
# error "No ztimer clock selected!"
|
||||
#endif
|
||||
|
||||
#ifndef COMPARE_TIMER_DEV
|
||||
# if IS_ACTIVE(MODULE_ZTIMER_PERIPH_TIMER)
|
||||
# if CONFIG_ZTIMER_USEC_DEV == TIMER_DEV(0)
|
||||
# define COMPARE_TIMER_DEV TIMER_DEV(1)
|
||||
# else
|
||||
# define COMPARE_TIMER_DEV TIMER_DEV(0)
|
||||
# endif
|
||||
# else
|
||||
# define COMPARE_TIMER_DEV TIMER_DEV(0)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifndef COMPARE_TIMER_FREQ
|
||||
# define COMPARE_TIMER_FREQ 1000000U
|
||||
#endif
|
||||
|
||||
static inline uint32_t bench_start(uint32_t adjust)
|
||||
{
|
||||
return timer_read(COMPARE_TIMER_DEV) + adjust;
|
||||
}
|
||||
|
||||
static inline uint32_t bench_finish(const char *name, uint32_t start)
|
||||
{
|
||||
uint32_t stop = timer_read(COMPARE_TIMER_DEV);
|
||||
uint32_t diff = stop - start;
|
||||
if (name) {
|
||||
printf(" - %s took %"PRIu32" ticks\n", name, diff);
|
||||
}
|
||||
return diff;
|
||||
}
|
||||
|
||||
static inline uint32_t bench_overhead(void) {
|
||||
uint32_t start = bench_start(0);
|
||||
uint32_t diff = bench_finish(NULL, start);
|
||||
return diff;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint32_t start, adjust;
|
||||
uint32_t first_acquire, second_acquire;
|
||||
uint32_t poweron_diff;
|
||||
|
||||
timer_init(COMPARE_TIMER_DEV, COMPARE_TIMER_FREQ, NULL, NULL);
|
||||
|
||||
adjust = bench_overhead();
|
||||
|
||||
printf("Compare timer frequency: %"PRIu32"Hz\n", (uint32_t) COMPARE_TIMER_FREQ);
|
||||
printf("Compare timer overhead: %"PRIu32" ticks (will be taken into account)\n", adjust);
|
||||
|
||||
puts("---");
|
||||
|
||||
puts("Benchmarking:");
|
||||
|
||||
start = bench_start(adjust);
|
||||
ztimer_acquire(CLOCK);
|
||||
first_acquire = bench_finish("ztimer_acquire() on inactive clock", start);
|
||||
|
||||
start = bench_start(adjust);
|
||||
ztimer_acquire(CLOCK);
|
||||
second_acquire = bench_finish("ztimer_acquire() on active clock", start);
|
||||
|
||||
start = bench_start(adjust);
|
||||
ztimer_release(CLOCK);
|
||||
bench_finish("ztimer_release() with users left", start);
|
||||
|
||||
start = bench_start(adjust);
|
||||
ztimer_release(CLOCK);
|
||||
bench_finish("ztimer_release() with no users left", start);
|
||||
|
||||
puts("---");
|
||||
poweron_diff = first_acquire - second_acquire;
|
||||
|
||||
#if defined(CLOCK_IS_ZTIMER_USEC) && COMPARE_TIMER_FREQ == 1000000U
|
||||
printf("Add this to you board.h:\n");
|
||||
printf(" #define CONFIG_ZTIMER_USEC_ADJUST_CLOCK_START %"PRIu32"\n", poweron_diff);
|
||||
#else
|
||||
printf("If the ztimer clock runs with with a frequency of %"PRIu32"Hz, set\n",
|
||||
(uint32_t) COMPARE_TIMER_FREQ);
|
||||
printf(" clock->adjust_clock_start = %"PRIu32"\n", poweron_diff);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user