diff mbox series

[FFmpeg-devel,v2] aarch64: Use cntvct_el0 as timer register on Android and macOS

Message ID 20240614110912.31094-1-martin@martin.st
State Accepted
Headers show
Series [FFmpeg-devel,v2] aarch64: Use cntvct_el0 as timer register on Android and macOS | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Martin Storsjö June 14, 2024, 11:09 a.m. UTC
The default timer register pmccntr_el0 usually requires enabling
access with e.g. a kernel module.

On macOS, using cntvct_el0 gives measurements with the same
magnitude as mach_absolute_time (which is used currently), but
possibly with a little less overhead/noise.
---
cntvct_el0 should have less noise than mach_absolute_time or
clock_gettime.

In one tested case, the cntvct_el0 timer has a frequency of 25 MHz
(readable via the register cntfrq_el0).
---
 libavutil/aarch64/timer.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Comments

Zhao Zhili June 16, 2024, 6:38 a.m. UTC | #1
> On Jun 14, 2024, at 19:09, Martin Storsjö <martin@martin.st> wrote:
> 
> The default timer register pmccntr_el0 usually requires enabling
> access with e.g. a kernel module.
> 
> On macOS, using cntvct_el0 gives measurements with the same
> magnitude as mach_absolute_time (which is used currently), but
> possibly with a little less overhead/noise.
> ---
> cntvct_el0 should have less noise than mach_absolute_time or
> clock_gettime.
> 
> In one tested case, the cntvct_el0 timer has a frequency of 25 MHz
> (readable via the register cntfrq_el0).
> ---
> libavutil/aarch64/timer.h | 11 ++++++++++-
> 1 file changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/libavutil/aarch64/timer.h b/libavutil/aarch64/timer.h
> index fadc9568f8..922b0c5598 100644
> --- a/libavutil/aarch64/timer.h
> +++ b/libavutil/aarch64/timer.h
> @@ -24,7 +24,7 @@
> #include <stdint.h>
> #include "config.h"
> 
> -#if HAVE_INLINE_ASM && !defined(__APPLE__)
> +#if HAVE_INLINE_ASM
> 
> #define AV_READ_TIME read_time
> 
> @@ -33,7 +33,16 @@ static inline uint64_t read_time(void)
>     uint64_t cycle_counter;
>     __asm__ volatile(
>         "isb                   \t\n"
> +#if defined(__ANDROID__) || defined(__APPLE__)
> +        // cntvct_el0 has lower resolution than pmccntr_el0, but is usually
> +        // accessible from user space by default.
> +        "mrs %0, cntvct_el0        "
> +#else
> +        // pmccntr_el0 has higher resolution, but is usually not accessible
> +        // from user space by default (but access can be enabled with a custom
> +        // kernel module).
>         "mrs %0, pmccntr_el0       "
> +#endif
>         : "=r"(cycle_counter) :: "memory" );
> 
>     return cycle_counter;

LGTM, thanks!
diff mbox series

Patch

diff --git a/libavutil/aarch64/timer.h b/libavutil/aarch64/timer.h
index fadc9568f8..922b0c5598 100644
--- a/libavutil/aarch64/timer.h
+++ b/libavutil/aarch64/timer.h
@@ -24,7 +24,7 @@ 
 #include <stdint.h>
 #include "config.h"
 
-#if HAVE_INLINE_ASM && !defined(__APPLE__)
+#if HAVE_INLINE_ASM
 
 #define AV_READ_TIME read_time
 
@@ -33,7 +33,16 @@  static inline uint64_t read_time(void)
     uint64_t cycle_counter;
     __asm__ volatile(
         "isb                   \t\n"
+#if defined(__ANDROID__) || defined(__APPLE__)
+        // cntvct_el0 has lower resolution than pmccntr_el0, but is usually
+        // accessible from user space by default.
+        "mrs %0, cntvct_el0        "
+#else
+        // pmccntr_el0 has higher resolution, but is usually not accessible
+        // from user space by default (but access can be enabled with a custom
+        // kernel module).
         "mrs %0, pmccntr_el0       "
+#endif
         : "=r"(cycle_counter) :: "memory" );
 
     return cycle_counter;