From 0ffcfa96334dd5316e035bf013278be0b6f95c9b Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Wed, 5 Aug 2020 12:57:00 -0700 Subject: [PATCH] timing: introduce timing functions as a generic feature Add timing functions and APIs. This is now used with some of the tests we have for performance and metrics and will be used whereever timing informations are needed, for example for tracing, profiling and other operations where timing info is critical. Signed-off-by: Daniel Leung Signed-off-by: Anas Nashif --- CODEOWNERS | 1 + boards/Kconfig | 6 +++ include/timing/timing.h | 92 +++++++++++++++++++++++++++++++++++++++++ kernel/Kconfig | 1 + soc/Kconfig | 6 +++ subsys/Kconfig | 2 + subsys/timing/Kconfig | 9 ++++ 7 files changed, 117 insertions(+) create mode 100644 include/timing/timing.h create mode 100644 subsys/timing/Kconfig diff --git a/CODEOWNERS b/CODEOWNERS index 6f874da0cb..ecfafded26 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -522,6 +522,7 @@ /subsys/stats/ @nvlsianpu /subsys/storage/ @nvlsianpu /subsys/testsuite/ @nashif +/subsys/timing/ @nashif @dcpleung /subsys/usb/ @jfischer-phytec-iot @finikorg /tests/ @nashif /tests/application_development/libcxx/ @pabigot diff --git a/boards/Kconfig b/boards/Kconfig index 9e9b57bf34..cac3faa3a4 100644 --- a/boards/Kconfig +++ b/boards/Kconfig @@ -47,3 +47,9 @@ config QEMU_ICOUNT_SHIFT # There might not be any board options, hence the optional source osource "$(BOARD_DIR)/Kconfig" endmenu + +config BOARD_HAS_TIMING_FUNCTIONS + bool + help + Should be selected if board provides custom method for retrieving + timestamps and cycle count. diff --git a/include/timing/timing.h b/include/timing/timing.h new file mode 100644 index 0000000000..059ead1b49 --- /dev/null +++ b/include/timing/timing.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2020 Intel Corporation. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_INCLUDE_TIMING_TIMING_H_ +#define ZEPHYR_INCLUDE_TIMING_TIMING_H_ + +#include + + +/** + * @brief Timing Measurement APIs + * @defgroup timing_api Timing APIs + * @{ + */ + + +typedef uint64_t timing_t; + +/** + * @brief Initialize the timing subsystem. + * + * Perform the necessary steps to initialize the timing subsystem. + */ +void timing_init(void); + +/** + * @brief Signal the start of the timing information gathering. + * + * Signal to the timing subsystem that timing information + * will be gathered from this point forward. + */ +void timing_start(void); + +/** + * @brief Signal the end of the timing information gathering. + * + * Signal to the timing subsystem that timing information + * is no longer being gathered from this point forward. + */ +void timing_stop(void); + +/** + * @brief Return timing counter. + * + * @return Timing counter. + */ +timing_t timing_counter_get(void); + +/** + * @brief Get number of cycles between @p start and @p end. + * + * For some architectures or SoCs, the raw numbers from counter + * need to be scaled to obtain actual number of cycles. + * + * @param start Pointer to counter at start of a measured execution. + * @param end Pointer to counter at stop of a measured execution. + * @return Number of cycles between start and end. + */ +uint64_t timing_cycles_get(volatile timing_t *const start, + volatile timing_t *const end); + +/** + * @brief Get frequency of counter used (in Hz). + * + * @return Frequency of counter used for timing in Hz. + */ +uint64_t timing_freq_get(void); + +/** + * @brief Convert number of @p cycles into nanoseconds. + * + * @param cycles Number of cycles + * @return Converted time value + */ +uint64_t timing_cycles_to_ns(uint64_t cycles); + +uint64_t timing_cycles_to_ns_avg(uint64_t cycles, uint32_t count); + +/** + * @brief Get frequency of counter used (in MHz). + * + * @return Frequency of counter used for timing in MHz. + */ +uint32_t timing_freq_get_mhz(void); + +/** + * @} + */ +#endif /* ZEPHYR_INCLUDE_TIMING_TIMING_H_ */ diff --git a/kernel/Kconfig b/kernel/Kconfig index 14be7a3d39..8bf5128965 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -316,6 +316,7 @@ config BOOT_DELAY config EXECUTION_BENCHMARKING bool "Timing metrics" + select TIMING_FUNCTIONS help This option enables the tracking of various times inside the kernel the exact set of metrics being tracked is board-dependent. diff --git a/soc/Kconfig b/soc/Kconfig index 4e8db26947..dd472f3180 100644 --- a/soc/Kconfig +++ b/soc/Kconfig @@ -41,3 +41,9 @@ config SOC_DEPRECATED_RELEASE the Zephyr release that the SoC configuration will be removed. When set, any build for that SoC will generate a clearly visible deprecation warning. + +config SOC_HAS_TIMING_FUNCTIONS + bool + help + Should be selected if SoC provides custom method for retrieving + timestamps and cycle count. diff --git a/subsys/Kconfig b/subsys/Kconfig index 520a7830e3..93c5a3c35e 100644 --- a/subsys/Kconfig +++ b/subsys/Kconfig @@ -49,6 +49,8 @@ source "subsys/settings/Kconfig" source "subsys/testsuite/Kconfig" +source "subsys/timing/Kconfig" + source "subsys/tracing/Kconfig" endmenu diff --git a/subsys/timing/Kconfig b/subsys/timing/Kconfig new file mode 100644 index 0000000000..8e4deff744 --- /dev/null +++ b/subsys/timing/Kconfig @@ -0,0 +1,9 @@ +# Copyright (c) 2020 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 + +config TIMING_FUNCTIONS + bool "Timing Functions" + help + When enabled, timing related functions are compiled. This is + useful for gathering timing on code execution.