From c928acc9ffed574b139d7508a1338ce71d772291 Mon Sep 17 00:00:00 2001 From: wangming9 Date: Tue, 8 Aug 2023 11:12:41 +0800 Subject: [PATCH] perf: The new configuration supports hardware performance counting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding the CONFIG_ARCH_PERF_EVENTS configuration to enable hardware performance countingļ¼Œsolve the problem that some platform hardware counting support is not perfect, you can choose to use software interface. This is configured using CONFIG_ARCH_PERF_EVENTS, so weak_functions are removed to prevent confusion To use hardware performance counting, must: 1. Configure CONFIG_ARCH_PERF_EVENTS, default selection 2. Call up_perf_init for initialization Signed-off-by: wangming9 --- arch/Kconfig | 7 +++++++ arch/arm/src/armv7-a/CMakeLists.txt | 5 ++++- arch/arm/src/armv7-a/arm_perf.c | 3 +++ arch/arm/src/armv7-m/CMakeLists.txt | 5 ++++- arch/arm/src/armv7-m/arm_perf.c | 3 +++ arch/arm/src/armv7-r/arm_perf.c | 3 +++ arch/arm/src/armv8-m/CMakeLists.txt | 5 ++++- arch/arm/src/armv8-m/arm_perf.c | 3 +++ arch/arm64/src/common/arm64_perf.c | 3 +++ arch/risc-v/src/esp32c3/esp32c3_perf.c | 3 +++ arch/xtensa/src/common/xtensa_perf.c | 3 +++ boards/risc-v/bl602/bl602evb/configs/dma/defconfig | 1 - boards/risc-v/bl602/bl602evb/configs/elf/defconfig | 1 - boards/risc-v/bl602/bl602evb/configs/fpu/defconfig | 1 - .../risc-v/bl602/bl602evb/configs/gpio/defconfig | 1 - boards/risc-v/bl602/bl602evb/configs/i2c/defconfig | 1 - boards/risc-v/bl602/bl602evb/configs/nsh/defconfig | 1 - boards/risc-v/bl602/bl602evb/configs/spi/defconfig | 1 - .../bl602/bl602evb/configs/spiflash/defconfig | 1 - .../risc-v/bl602/bl602evb/configs/timer/defconfig | 1 - drivers/timers/arch_alarm.c | 12 ++++++------ drivers/timers/arch_timer.c | 14 +++++++------- 22 files changed, 53 insertions(+), 25 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index e479f69c23..7f5b0ac554 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -487,6 +487,13 @@ config ARCH_HAVE_BACKTRACE config ARCH_HAVE_PERF_EVENTS bool default n + ---help--- + The architecture supports hardware performance counting. + +config ARCH_PERF_EVENTS + bool "Configure hardware performance counting" + default y + depends on ARCH_HAVE_PERF_EVENTS ---help--- Enable hardware performance counter support for perf events. If disabled, perf events will use software events only. diff --git a/arch/arm/src/armv7-a/CMakeLists.txt b/arch/arm/src/armv7-a/CMakeLists.txt index 75cca9eda8..700381a1c3 100644 --- a/arch/arm/src/armv7-a/CMakeLists.txt +++ b/arch/arm/src/armv7-a/CMakeLists.txt @@ -47,9 +47,12 @@ list( arm_syscall.c arm_tcbinfo.c arm_undefinedinsn.c - arm_perf.c cp15_cacheops.c) +if(CONFIG_ARCH_PERF_EVENTS) + list(APPEND SRCS arm_perf.c) +endif() + if(CONFIG_ARMV7A_HAVE_PTM) list(APPEND SRCS arm_timer.c) endif() diff --git a/arch/arm/src/armv7-a/arm_perf.c b/arch/arm/src/armv7-a/arm_perf.c index beed522aae..9768bf7d89 100644 --- a/arch/arm/src/armv7-a/arm_perf.c +++ b/arch/arm/src/armv7-a/arm_perf.c @@ -28,6 +28,8 @@ #include "arm_internal.h" #include "sctlr.h" +#ifdef CONFIG_ARCH_PERF_EVENTS + /**************************************************************************** * Private Data ****************************************************************************/ @@ -85,3 +87,4 @@ void up_perf_convert(unsigned long elapsed, struct timespec *ts) left = elapsed - ts->tv_sec * g_cpu_freq; ts->tv_nsec = NSEC_PER_SEC * (uint64_t)left / g_cpu_freq; } +#endif diff --git a/arch/arm/src/armv7-m/CMakeLists.txt b/arch/arm/src/armv7-m/CMakeLists.txt index 3adbedce30..95ed96d88e 100644 --- a/arch/arm/src/armv7-m/CMakeLists.txt +++ b/arch/arm/src/armv7-m/CMakeLists.txt @@ -31,7 +31,6 @@ set(SRCS arm_initialstate.c arm_itm.c arm_memfault.c - arm_perf.c arm_schedulesigaction.c arm_sigdeliver.c arm_svcall.c @@ -41,6 +40,10 @@ set(SRCS arm_usagefault.c arm_vectors.c) +if(CONFIG_ARCH_PERF_EVENTS) + list(APPEND SRCS arm_perf.c) +endif() + if(CONFIG_ARMV7M_SYSTICK) list(APPEND SRCS arm_systick.c) endif() diff --git a/arch/arm/src/armv7-m/arm_perf.c b/arch/arm/src/armv7-m/arm_perf.c index c977bdadf8..8f1f76e65a 100644 --- a/arch/arm/src/armv7-m/arm_perf.c +++ b/arch/arm/src/armv7-m/arm_perf.c @@ -30,6 +30,8 @@ #include "itm.h" #include "nvic.h" +#ifdef CONFIG_ARCH_PERF_EVENTS + /**************************************************************************** * Private Data ****************************************************************************/ @@ -74,3 +76,4 @@ void up_perf_convert(unsigned long elapsed, struct timespec *ts) left = elapsed - ts->tv_sec * g_cpu_freq; ts->tv_nsec = NSEC_PER_SEC * (uint64_t)left / g_cpu_freq; } +#endif diff --git a/arch/arm/src/armv7-r/arm_perf.c b/arch/arm/src/armv7-r/arm_perf.c index 3c5c91b72d..c3d1310862 100644 --- a/arch/arm/src/armv7-r/arm_perf.c +++ b/arch/arm/src/armv7-r/arm_perf.c @@ -28,6 +28,8 @@ #include "arm_internal.h" #include "sctlr.h" +#ifdef CONFIG_ARCH_PERF_EVENTS + /**************************************************************************** * Private Data ****************************************************************************/ @@ -85,3 +87,4 @@ void up_perf_convert(unsigned long elapsed, struct timespec *ts) left = elapsed - ts->tv_sec * g_cpu_freq; ts->tv_nsec = NSEC_PER_SEC * (uint64_t)left / g_cpu_freq; } +#endif diff --git a/arch/arm/src/armv8-m/CMakeLists.txt b/arch/arm/src/armv8-m/CMakeLists.txt index 78f85cc122..db57c8b24c 100644 --- a/arch/arm/src/armv8-m/CMakeLists.txt +++ b/arch/arm/src/armv8-m/CMakeLists.txt @@ -29,7 +29,6 @@ set(SRCS arm_initialstate.c arm_itm.c arm_memfault.c - arm_perf.c arm_sau.c arm_schedulesigaction.c arm_securefault.c @@ -42,6 +41,10 @@ set(SRCS arm_usagefault.c arm_vectors.c) +if(CONFIG_ARCH_PERF_EVENTS) + list(APPEND SRCS arm_perf.c) +endif() + if(CONFIG_ARMV8M_SYSTICK) list(APPEND SRCS arm_systick.c) endif() diff --git a/arch/arm/src/armv8-m/arm_perf.c b/arch/arm/src/armv8-m/arm_perf.c index de7f7d45c9..5b0f0d359d 100644 --- a/arch/arm/src/armv8-m/arm_perf.c +++ b/arch/arm/src/armv8-m/arm_perf.c @@ -30,6 +30,8 @@ #include "itm.h" #include "nvic.h" +#ifdef CONFIG_ARCH_PERF_EVENTS + /**************************************************************************** * Private Data ****************************************************************************/ @@ -74,3 +76,4 @@ void up_perf_convert(unsigned long elapsed, struct timespec *ts) left = elapsed - ts->tv_sec * g_cpu_freq; ts->tv_nsec = NSEC_PER_SEC * (uint64_t)left / g_cpu_freq; } +#endif diff --git a/arch/arm64/src/common/arm64_perf.c b/arch/arm64/src/common/arm64_perf.c index 3b8ffd420c..fa19f64905 100644 --- a/arch/arm64/src/common/arm64_perf.c +++ b/arch/arm64/src/common/arm64_perf.c @@ -26,6 +26,8 @@ #include "arm64_pmu.h" +#ifdef CONFIG_ARCH_PERF_EVENTS + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -58,3 +60,4 @@ void up_perf_convert(unsigned long elapsed, struct timespec *ts) left = elapsed - ts->tv_sec * cpu_freq; ts->tv_nsec = NSEC_PER_SEC * left / cpu_freq; } +#endif diff --git a/arch/risc-v/src/esp32c3/esp32c3_perf.c b/arch/risc-v/src/esp32c3/esp32c3_perf.c index aaa07d1d16..5e187b4ac3 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_perf.c +++ b/arch/risc-v/src/esp32c3/esp32c3_perf.c @@ -33,6 +33,8 @@ #include "hardware/esp32c3_system.h" #include "esp32c3_clockconfig.h" +#ifdef CONFIG_ARCH_PERF_EVENTS + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -82,3 +84,4 @@ void up_perf_convert(unsigned long elapsed, struct timespec *ts) elapsed -= ts->tv_sec * CYCLE_PER_SEC; ts->tv_nsec = elapsed * NSEC_PER_CYCLE; } +#endif diff --git a/arch/xtensa/src/common/xtensa_perf.c b/arch/xtensa/src/common/xtensa_perf.c index f1f7d2a2ca..efa76a1fcd 100644 --- a/arch/xtensa/src/common/xtensa_perf.c +++ b/arch/xtensa/src/common/xtensa_perf.c @@ -28,6 +28,8 @@ #include "xtensa_counter.h" #include "xtensa.h" +#ifdef CONFIG_ARCH_PERF_EVENTS + /**************************************************************************** * Private Data ****************************************************************************/ @@ -61,3 +63,4 @@ void up_perf_convert(unsigned long elapsed, struct timespec *ts) left = elapsed - ts->tv_sec * g_cpu_freq; ts->tv_nsec = NSEC_PER_SEC * (uint64_t)left / g_cpu_freq; } +#endif diff --git a/boards/risc-v/bl602/bl602evb/configs/dma/defconfig b/boards/risc-v/bl602/bl602evb/configs/dma/defconfig index ccf291a816..efc553cd53 100644 --- a/boards/risc-v/bl602/bl602evb/configs/dma/defconfig +++ b/boards/risc-v/bl602/bl602evb/configs/dma/defconfig @@ -74,7 +74,6 @@ CONFIG_SYSTEM_SPITOOL=y CONFIG_TASK_NAME_SIZE=12 CONFIG_TESTING_GETPRIME=y CONFIG_TIMER=y -CONFIG_TIMER_ARCH=y CONFIG_UART0_BAUD=2000000 CONFIG_UART0_RXBUFSIZE=128 CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/bl602/bl602evb/configs/elf/defconfig b/boards/risc-v/bl602/bl602evb/configs/elf/defconfig index 42c31ab606..a0e83bb893 100644 --- a/boards/risc-v/bl602/bl602evb/configs/elf/defconfig +++ b/boards/risc-v/bl602/bl602evb/configs/elf/defconfig @@ -77,7 +77,6 @@ CONFIG_TASK_NAME_SIZE=12 CONFIG_TESTING_GETPRIME=y CONFIG_TESTING_OSTEST=y CONFIG_TIMER=y -CONFIG_TIMER_ARCH=y CONFIG_TLS_NELEM=4 CONFIG_UART0_BAUD=2000000 CONFIG_UART0_RXBUFSIZE=128 diff --git a/boards/risc-v/bl602/bl602evb/configs/fpu/defconfig b/boards/risc-v/bl602/bl602evb/configs/fpu/defconfig index aec31bdf99..2a4a3c6382 100644 --- a/boards/risc-v/bl602/bl602evb/configs/fpu/defconfig +++ b/boards/risc-v/bl602/bl602evb/configs/fpu/defconfig @@ -69,7 +69,6 @@ CONFIG_TASK_NAME_SIZE=12 CONFIG_TESTING_GETPRIME=y CONFIG_TESTING_OSTEST=y CONFIG_TIMER=y -CONFIG_TIMER_ARCH=y CONFIG_UART0_BAUD=2000000 CONFIG_UART0_RXBUFSIZE=128 CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/bl602/bl602evb/configs/gpio/defconfig b/boards/risc-v/bl602/bl602evb/configs/gpio/defconfig index 1b66e28e45..d1148a058f 100644 --- a/boards/risc-v/bl602/bl602evb/configs/gpio/defconfig +++ b/boards/risc-v/bl602/bl602evb/configs/gpio/defconfig @@ -69,7 +69,6 @@ CONFIG_SYSTEM_NSH=y CONFIG_TASK_NAME_SIZE=12 CONFIG_TESTING_GETPRIME=y CONFIG_TIMER=y -CONFIG_TIMER_ARCH=y CONFIG_UART0_BAUD=2000000 CONFIG_UART0_RXBUFSIZE=128 CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/bl602/bl602evb/configs/i2c/defconfig b/boards/risc-v/bl602/bl602evb/configs/i2c/defconfig index 25b906e534..a4a81d1f95 100644 --- a/boards/risc-v/bl602/bl602evb/configs/i2c/defconfig +++ b/boards/risc-v/bl602/bl602evb/configs/i2c/defconfig @@ -70,7 +70,6 @@ CONFIG_SYSTEM_NSH=y CONFIG_TASK_NAME_SIZE=12 CONFIG_TESTING_GETPRIME=y CONFIG_TIMER=y -CONFIG_TIMER_ARCH=y CONFIG_UART0_BAUD=2000000 CONFIG_UART0_RXBUFSIZE=128 CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/bl602/bl602evb/configs/nsh/defconfig b/boards/risc-v/bl602/bl602evb/configs/nsh/defconfig index c1c8414d6b..4d14d3a5de 100644 --- a/boards/risc-v/bl602/bl602evb/configs/nsh/defconfig +++ b/boards/risc-v/bl602/bl602evb/configs/nsh/defconfig @@ -67,7 +67,6 @@ CONFIG_SYSTEM_NSH=y CONFIG_TASK_NAME_SIZE=12 CONFIG_TESTING_GETPRIME=y CONFIG_TIMER=y -CONFIG_TIMER_ARCH=y CONFIG_UART0_BAUD=2000000 CONFIG_UART0_RXBUFSIZE=128 CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/bl602/bl602evb/configs/spi/defconfig b/boards/risc-v/bl602/bl602evb/configs/spi/defconfig index 1df21d2964..36d1b09b0e 100644 --- a/boards/risc-v/bl602/bl602evb/configs/spi/defconfig +++ b/boards/risc-v/bl602/bl602evb/configs/spi/defconfig @@ -71,7 +71,6 @@ CONFIG_SYSTEM_SPITOOL=y CONFIG_TASK_NAME_SIZE=12 CONFIG_TESTING_GETPRIME=y CONFIG_TIMER=y -CONFIG_TIMER_ARCH=y CONFIG_UART0_BAUD=2000000 CONFIG_UART0_RXBUFSIZE=128 CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/bl602/bl602evb/configs/spiflash/defconfig b/boards/risc-v/bl602/bl602evb/configs/spiflash/defconfig index 58dac55ec6..142405dc99 100644 --- a/boards/risc-v/bl602/bl602evb/configs/spiflash/defconfig +++ b/boards/risc-v/bl602/bl602evb/configs/spiflash/defconfig @@ -70,7 +70,6 @@ CONFIG_SYSTEM_NSH=y CONFIG_TASK_NAME_SIZE=12 CONFIG_TESTING_GETPRIME=y CONFIG_TIMER=y -CONFIG_TIMER_ARCH=y CONFIG_UART0_BAUD=2000000 CONFIG_UART0_RXBUFSIZE=128 CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/bl602/bl602evb/configs/timer/defconfig b/boards/risc-v/bl602/bl602evb/configs/timer/defconfig index f8224a94af..37632bde29 100644 --- a/boards/risc-v/bl602/bl602evb/configs/timer/defconfig +++ b/boards/risc-v/bl602/bl602evb/configs/timer/defconfig @@ -75,7 +75,6 @@ CONFIG_SYSTEM_NSH=y CONFIG_TASK_NAME_SIZE=12 CONFIG_TESTING_GETPRIME=y CONFIG_TIMER=y -CONFIG_TIMER_ARCH=y CONFIG_UART0_BAUD=2000000 CONFIG_UART0_RXBUFSIZE=128 CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index bfc4a1bea8..3223b79eb0 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -356,13 +356,13 @@ int weak_function up_alarm_tick_start(clock_t ticks) * units. ****************************************************************************/ -#ifndef CONFIG_ARCH_HAVE_PERF_EVENTS -void weak_function up_perf_init(FAR void *arg) +#ifndef CONFIG_ARCH_PERF_EVENTS +void up_perf_init(FAR void *arg) { UNUSED(arg); } -unsigned long weak_function up_perf_gettime(void) +unsigned long up_perf_gettime(void) { unsigned long ret = 0; @@ -377,17 +377,17 @@ unsigned long weak_function up_perf_gettime(void) return ret; } -unsigned long weak_function up_perf_getfreq(void) +unsigned long up_perf_getfreq(void) { return USEC_PER_SEC; } -void weak_function up_perf_convert(unsigned long elapsed, +void up_perf_convert(unsigned long elapsed, FAR struct timespec *ts) { timespec_from_usec(ts, elapsed); } -#endif /* CONFIG_ARCH_HAVE_PERF_EVENTS */ +#endif /* CONFIG_ARCH_PERF_EVENTS */ /**************************************************************************** * Name: up_mdelay diff --git a/drivers/timers/arch_timer.c b/drivers/timers/arch_timer.c index e82b83e84c..fb2b24939a 100644 --- a/drivers/timers/arch_timer.c +++ b/drivers/timers/arch_timer.c @@ -395,13 +395,13 @@ int weak_function up_timer_tick_start(clock_t ticks) * units. ****************************************************************************/ -#ifndef CONFIG_ARCH_HAVE_PERF_EVENTS -void weak_function up_perf_init(FAR void *arg) +#ifndef CONFIG_ARCH_PERF_EVENTS +void up_perf_init(FAR void *arg) { UNUSED(arg); } -unsigned long weak_function up_perf_gettime(void) +unsigned long up_perf_gettime(void) { unsigned long ret = 0; @@ -413,17 +413,17 @@ unsigned long weak_function up_perf_gettime(void) return ret; } -unsigned long weak_function up_perf_getfreq(void) +unsigned long up_perf_getfreq(void) { return USEC_PER_SEC; } -void weak_function up_perf_convert(unsigned long elapsed, - FAR struct timespec *ts) +void up_perf_convert(unsigned long elapsed, + FAR struct timespec *ts) { timespec_from_usec(ts, elapsed); } -#endif /* CONFIG_ARCH_HAVE_PERF_EVENTS */ +#endif /* CONFIG_ARCH_PERF_EVENTS */ /**************************************************************************** * Name: up_mdelay