diff --git a/CODEOWNERS b/CODEOWNERS index 6f874da0cba..ecfafded26b 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 9e9b57bf342..cac3faa3a40 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 00000000000..059ead1b49d --- /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 14be7a3d390..8bf51289659 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 4e8db269477..dd472f31807 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 520a7830e36..93c5a3c35e0 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 00000000000..8e4deff7440 --- /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.