[FFmpeg-devel] [PATCH 3/7] checkasm: use pointers for start/stop functions

Rémi Denis-Courmont remi at remlab.net
Sat Jul 15 18:23:35 EEST 2023


This makes all calls to the bench start and stop functions via
function pointers. While the primary goal is to support run-time
selection of the performance measurement back-end in later commits,
this has the side benefit of containing platform dependencies in to
checkasm.c and out of checkasm.h.
---
 tests/checkasm/checkasm.c | 33 ++++++++++++++++++++++++++++-----
 tests/checkasm/checkasm.h | 33 ++++++---------------------------
 2 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c
index d8f571f301..e921257257 100644
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -57,6 +57,14 @@
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#if CONFIG_LINUX_PERF
+#include <sys/ioctl.h>
+#include <asm/unistd.h>
+#include <linux/perf_event.h>
+#endif
+#if CONFIG_MACOS_KPERF
+#include "libavutil/macos_kperf.h"
+#endif
 
 #if !HAVE_ISATTY
 #define isatty(fd) 1
@@ -506,6 +514,9 @@ static int cmp_nop(const void *a, const void *b)
     return *(const uint16_t*)a - *(const uint16_t*)b;
 }
 
+static uint64_t (*checkasm_bench_start)(void);
+static uint64_t (*checkasm_bench_stop)(void);
+
 /* Measure the overhead of the timing code (in decicycles) */
 static int measure_nop_time(void)
 {
@@ -649,7 +660,7 @@ static void print_cpu_name(void)
 }
 
 #if CONFIG_LINUX_PERF
-uint64_t checkasm_bench_linux_perf_start(void)
+static uint64_t checkasm_bench_linux_perf_start(void)
 {
     int fd = state.sysfd;
 
@@ -658,7 +669,7 @@ uint64_t checkasm_bench_linux_perf_start(void)
     return 0;
 }
 
-uint64_t checkasm_bench_linux_perf_stop(void)
+static uint64_t checkasm_bench_linux_perf_stop(void)
 {
     uint64_t t;
     int fd = state.sysfd;
@@ -685,24 +696,34 @@ static int bench_init_linux(void)
         perror("perf_event_open");
         return -1;
     }
+    checkasm_bench_start = checkasm_bench_linux_perf_start;
+    checkasm_bench_stop = checkasm_bench_linux_perf_stop;
     return 0;
 }
 #elif CONFIG_MACOS_KPERF
 static int bench_init_kperf(void)
 {
     ff_kperf_init();
+    checkasm_bench_start = checkasm_bench_stop = ff_kperf_cycles;
     return 0;
 }
-#else
+#elif defined (AV_READ_TIME)
+static uint64_t ff_read_time(void)
+{
+    return AV_READ_TIME();
+}
+
 static int bench_init_ffmpeg(void)
 {
-#ifdef AV_READ_TIME
     printf("benchmarking with native FFmpeg timers\n");
+    checkasm_bench_start = checkasm_bench_stop = ff_read_time;
     return 0;
+}
 #else
+static int bench_init_ffmpeg(void)
+{
     fprintf(stderr, "checkasm: --bench is not supported on your system\n");
     return -1;
-#endif
 }
 #endif
 
@@ -869,6 +890,8 @@ CheckasmPerf *checkasm_get_perf_context(void)
     CheckasmPerf *perf = &state.current_func_ver->perf;
     memset(perf, 0, sizeof(*perf));
     perf->sysfd = state.sysfd;
+    perf->start = checkasm_bench_start;
+    perf->stop = checkasm_bench_start;
     return perf;
 }
 
diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
index 8a62b98f3e..8f966f49af 100644
--- a/tests/checkasm/checkasm.h
+++ b/tests/checkasm/checkasm.h
@@ -26,15 +26,6 @@
 #include <stdint.h>
 #include "config.h"
 
-#if CONFIG_LINUX_PERF
-#include <unistd.h> // read(3)
-#include <sys/ioctl.h>
-#include <asm/unistd.h>
-#include <linux/perf_event.h>
-#elif CONFIG_MACOS_KPERF
-#include "libavutil/macos_kperf.h"
-#endif
-
 #include "libavutil/avstring.h"
 #include "libavutil/cpu.h"
 #include "libavutil/internal.h"
@@ -236,41 +227,29 @@ typedef struct CheckasmPerf {
     int sysfd;
     uint64_t cycles;
     int iterations;
+    uint64_t (*start)(void);
+    uint64_t (*stop)(void);
 } CheckasmPerf;
 
-#if CONFIG_LINUX_PERF
-uint64_t checkasm_bench_linux_perf_start(void);
-uint64_t checkasm_bench_linux_perf_stop(void);
-#define checkasm_bench_start() checkasm_bench_linux_perf_start()
-#define checkasm_bench_stop()  checkasm_bench_linux_perf_stop()
-#elif CONFIG_MACOS_KPERF
-#define checkasm_bench_start() ff_kperf_cycles()
-#define checkasm_bench_stop()  ff_kperf_cycles()
-#elif defined (AV_READ_TIME)
-#define checkasm_bench_start() AV_READ_TIME()
-#define checkasm_bench_stop()  AV_READ_TIME()
-#else
-#define checkasm_bench_start() UINT64_C(0)
-#define checkasm_bench_stop()  UINT64_C(0)
-#endif
-
 /* Benchmark the function */
 #define bench_new(...)\
     do {\
         if (checkasm_bench_func()) {\
             struct CheckasmPerf *perf = checkasm_get_perf_context();\
             av_unused const int sysfd = perf->sysfd;\
+            uint64_t (*const start)(void) = perf->start;\
+            uint64_t (*const stop)(void) = perf->stop;\
             func_type *tfunc = func_new;\
             uint64_t tsum = 0;\
             int ti, tcount = 0;\
             uint64_t t = 0; \
             for (ti = 0; ti < BENCH_RUNS; ti++) {\
-                t = checkasm_bench_start(); \
+                t = start(); \
                 tfunc(__VA_ARGS__);\
                 tfunc(__VA_ARGS__);\
                 tfunc(__VA_ARGS__);\
                 tfunc(__VA_ARGS__);\
-                t = checkasm_bench_stop() - t;\
+                t = stop() - t;\
                 if (t*tcount <= tsum*4 && ti > 0) {\
                     tsum += t;\
                     tcount++;\
-- 
2.40.1



More information about the ffmpeg-devel mailing list