From 5f7c6bd95bd6089473db3ba4f033584f5de0ee8a Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 24 Sep 2024 14:49:32 +0100 Subject: [PATCH] [profile] Standardise return type of profile_timestamp() All consumers of profile_timestamp() currently treat the value as an unsigned long. Only the elapsed number of ticks is ever relevant: the absolute value of the timestamp is not used. Profiling is used to measure short durations that are generally fewer than a million CPU cycles, for which an unsigned long is easily large enough. Standardise the return type of profile_timestamp() as unsigned long across all CPU architectures. This allows 32-bit architectures such as i386 and riscv32 to omit all logic associated with retrieving the upper 32 bits of the 64-bit hardware counter, which simplifies the code and allows riscv32 and riscv64 to share the same implementation. Signed-off-by: Michael Brown --- src/arch/arm32/include/bits/profile.h | 2 +- src/arch/arm64/include/bits/profile.h | 2 +- src/arch/i386/include/bits/profile.h | 6 ++-- src/arch/loong64/include/bits/profile.h | 2 +- .../{riscv64 => riscv}/include/bits/profile.h | 4 +-- src/arch/riscv32/include/bits/profile.h | 36 ------------------- src/arch/x86_64/include/bits/profile.h | 2 +- src/include/ipxe/profile.h | 2 ++ 8 files changed, 11 insertions(+), 45 deletions(-) rename src/arch/{riscv64 => riscv}/include/bits/profile.h (81%) delete mode 100644 src/arch/riscv32/include/bits/profile.h diff --git a/src/arch/arm32/include/bits/profile.h b/src/arch/arm32/include/bits/profile.h index 2b15d1604..4e4bf48a3 100644 --- a/src/arch/arm32/include/bits/profile.h +++ b/src/arch/arm32/include/bits/profile.h @@ -16,7 +16,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * * @ret timestamp Timestamp */ -static inline __attribute__ (( always_inline )) uint64_t +static inline __attribute__ (( always_inline )) unsigned long profile_timestamp ( void ) { uint32_t cycles; diff --git a/src/arch/arm64/include/bits/profile.h b/src/arch/arm64/include/bits/profile.h index 62ffa3772..4a5b3f7a1 100644 --- a/src/arch/arm64/include/bits/profile.h +++ b/src/arch/arm64/include/bits/profile.h @@ -16,7 +16,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * * @ret timestamp Timestamp */ -static inline __attribute__ (( always_inline )) uint64_t +static inline __attribute__ (( always_inline )) unsigned long profile_timestamp ( void ) { uint64_t cycles; diff --git a/src/arch/i386/include/bits/profile.h b/src/arch/i386/include/bits/profile.h index e184d7b51..21c216a81 100644 --- a/src/arch/i386/include/bits/profile.h +++ b/src/arch/i386/include/bits/profile.h @@ -16,12 +16,12 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * * @ret timestamp Timestamp */ -static inline __attribute__ (( always_inline )) uint64_t +static inline __attribute__ (( always_inline )) unsigned long profile_timestamp ( void ) { - uint64_t tsc; + uint32_t tsc; /* Read timestamp counter */ - __asm__ __volatile__ ( "rdtsc" : "=A" ( tsc ) ); + __asm__ __volatile__ ( "rdtsc" : "=a" ( tsc ) : : "edx" ); return tsc; } diff --git a/src/arch/loong64/include/bits/profile.h b/src/arch/loong64/include/bits/profile.h index 9f597ce2d..02f8d4b7c 100644 --- a/src/arch/loong64/include/bits/profile.h +++ b/src/arch/loong64/include/bits/profile.h @@ -16,7 +16,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * * @ret timestamp Timestamp */ -static inline __attribute__ (( always_inline )) uint64_t +static inline __attribute__ (( always_inline )) unsigned long profile_timestamp ( void ) { uint64_t cycles; diff --git a/src/arch/riscv64/include/bits/profile.h b/src/arch/riscv/include/bits/profile.h similarity index 81% rename from src/arch/riscv64/include/bits/profile.h rename to src/arch/riscv/include/bits/profile.h index 2c8e29a22..e9e003dab 100644 --- a/src/arch/riscv64/include/bits/profile.h +++ b/src/arch/riscv/include/bits/profile.h @@ -16,9 +16,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * * @ret timestamp Timestamp */ -static inline __attribute__ (( always_inline )) uint64_t +static inline __attribute__ (( always_inline )) unsigned long profile_timestamp ( void ) { - uint64_t cycles; + unsigned long cycles; /* Read timestamp counter */ __asm__ __volatile__ ( "rdcycle %0" : "=r" ( cycles ) ); diff --git a/src/arch/riscv32/include/bits/profile.h b/src/arch/riscv32/include/bits/profile.h deleted file mode 100644 index cb969077d..000000000 --- a/src/arch/riscv32/include/bits/profile.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef _BITS_PROFILE_H -#define _BITS_PROFILE_H - -/** @file - * - * Profiling - * - */ - -FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); - -#include - -/** - * Get profiling timestamp - * - * @ret timestamp Timestamp - */ -static inline __attribute__ (( always_inline )) uint64_t -profile_timestamp ( void ) { - uint32_t cycles_lo; - uint32_t cycles_hi; - uint32_t tmp; - - /* Read timestamp counter */ - __asm__ __volatile__ ( "\n1:\n\t" - "rdcycleh %1\n\t" - "rdcycle %0\n\t" - "rdcycleh %2\n\t" - "bne %1, %2, 1b\n\t" - : "=r" ( cycles_lo ), "=r" ( cycles_hi ), - "=r" ( tmp ) ); - return ( ( ( ( uint64_t ) cycles_hi ) << 32 ) | cycles_lo ); -} - -#endif /* _BITS_PROFILE_H */ diff --git a/src/arch/x86_64/include/bits/profile.h b/src/arch/x86_64/include/bits/profile.h index b7c74fbe7..c85b6fe5c 100644 --- a/src/arch/x86_64/include/bits/profile.h +++ b/src/arch/x86_64/include/bits/profile.h @@ -16,7 +16,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * * @ret timestamp Timestamp */ -static inline __attribute__ (( always_inline )) uint64_t +static inline __attribute__ (( always_inline )) unsigned long profile_timestamp ( void ) { uint32_t eax; uint32_t edx; diff --git a/src/include/ipxe/profile.h b/src/include/ipxe/profile.h index 2c69e1208..fd45b3cdc 100644 --- a/src/include/ipxe/profile.h +++ b/src/include/ipxe/profile.h @@ -60,6 +60,8 @@ struct profiler { #define __profiler #endif +unsigned long profile_timestamp ( void ); + extern unsigned long profile_excluded; extern void profile_update ( struct profiler *profiler, unsigned long sample );