From 8145c42955acb8d14b776b0e8188ad4e4fa3899b Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Mon, 24 Oct 2022 22:34:03 +0200 Subject: [PATCH] debug_irq_disable: add module to debug time spent in irq_disable --- sys/Kconfig | 1 + sys/Makefile.dep | 4 +++ sys/debug_irq_disable/Kconfig | 21 +++++++++++ sys/debug_irq_disable/Makefile | 1 + sys/debug_irq_disable/debug_irq_disable.c | 43 ++++++++++++++++++++++ sys/include/debug_irq_disable.h | 44 +++++++++++++++++++++++ 6 files changed, 114 insertions(+) create mode 100644 sys/debug_irq_disable/Kconfig create mode 100644 sys/debug_irq_disable/Makefile create mode 100644 sys/debug_irq_disable/debug_irq_disable.c create mode 100644 sys/include/debug_irq_disable.h diff --git a/sys/Kconfig b/sys/Kconfig index 24c1cc7f9f..5eb6789c69 100644 --- a/sys/Kconfig +++ b/sys/Kconfig @@ -25,6 +25,7 @@ rsource "congure/Kconfig" rsource "cpp11-compat/Kconfig" rsource "cpp_new_delete/Kconfig" rsource "cxx_ctor_guards/Kconfig" +rsource "debug_irq_disable/Kconfig" rsource "div/Kconfig" rsource "embunit/Kconfig" rsource "entropy_source/Kconfig" diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 9839575e4b..2b254bdcf3 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -65,6 +65,10 @@ ifneq (,$(filter crc32_fast,$(USEMODULE))) USEMODULE += checksum endif +ifneq (,$(filter debug_irq_disable,$(USEMODULE))) + USEMODULE += fmt +endif + ifneq (,$(filter eepreg,$(USEMODULE))) FEATURES_REQUIRED += periph_eeprom endif diff --git a/sys/debug_irq_disable/Kconfig b/sys/debug_irq_disable/Kconfig new file mode 100644 index 0000000000..45984038fd --- /dev/null +++ b/sys/debug_irq_disable/Kconfig @@ -0,0 +1,21 @@ +# Copyright (C) 2022 Benjamin Valentin +# +# 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. +# + +menuconfig MODULE_DEBUG_IRQ_DISABLE + bool "Measure IRQ disable durations" + depends on TEST_KCONFIG + depends on CPU_CORE_CORTEX_M + help + Print time spent with IRQs disabled + +config DEBUG_IRQ_DISABLE_THRESHOLD + int "Suppress Threshold" + default 1 + depends on MODULE_DEBUG_IRQ_DISABLE + help + Threshold (in CPU ticks) below which periods with IRQs disabled are not printed. + Use this to prevent *a lot* of output when debugging. diff --git a/sys/debug_irq_disable/Makefile b/sys/debug_irq_disable/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/sys/debug_irq_disable/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/sys/debug_irq_disable/debug_irq_disable.c b/sys/debug_irq_disable/debug_irq_disable.c new file mode 100644 index 0000000000..155aaa44b5 --- /dev/null +++ b/sys/debug_irq_disable/debug_irq_disable.c @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2022 ML!PA Consulting 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 debug_irq_disable + * @{ + * + * @file + * @brief Helper for debug_irq_disable + * + * @author Benjamin Valentin + * @} + */ + +#include +#include "fmt.h" + +void debug_irq_disable_print(const char *file, unsigned line, uint32_t ticks) +{ + static bool is_printing; + + if (is_printing) { + return; + } + + /* prevent infinite recursion if stdio driver uses irq_disable() */ + is_printing = true; + + print_str("irq disabled for "); + print_u32_dec(ticks); + print_str(" ticks in "); + print_str(file); + print_str(":"); + print_u32_dec(line); + print_str("\n"); + + is_printing = false; +} diff --git a/sys/include/debug_irq_disable.h b/sys/include/debug_irq_disable.h new file mode 100644 index 0000000000..78fe79f813 --- /dev/null +++ b/sys/include/debug_irq_disable.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2022 ML!PA Consulting 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. + */ + +/** + * @defgroup debug_irq_disable IRQ Disable Debug helper + * @ingroup sys + * @brief Debug time spent with IRQ disabled + * @{ + * + * @file + * + * @author Benjamin Valentin + */ + +#ifndef DEBUG_IRQ_DISABLE_H +#define DEBUG_IRQ_DISABLE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Print time spent with IRQ disabled + * @internal + * + * @param[in] file file where irq_restore() was called + * @param[in] line line where irq_restore() was called + * @param[in] ticks CPU ticks spent with IRQ disabled + */ +void debug_irq_disable_print(const char *file, unsigned line, uint32_t ticks); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* DEBUG_IRQ_DISABLE_H */