[FFmpeg-devel] [PATCH 1/6] checkasm: make perf macros functional

Rémi Denis-Courmont remi at remlab.net
Wed Jul 19 22:55:35 EEST 2023


This converts the bench/perf start/stop macros into functional macros,
and for that to work, take the Linux perf code out of line.
---
 tests/checkasm/checkasm.c | 24 ++++++++++++++++++++----
 tests/checkasm/checkasm.h | 36 +++++++++++++-----------------------
 2 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c
index 30e2dc6fed..63141e1f7a 100644
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -514,11 +514,9 @@ static int measure_nop_time(void)
     int i, nop_sum = 0;
     av_unused const int sysfd = state.sysfd;
 
-    uint64_t t = 0;
     for (i = 0; i < 10000; i++) {
-        PERF_START(t);
-        PERF_STOP(t);
-        nops[i] = t;
+        uint64_t t = checkasm_bench_start();
+        nops[i] = checkasm_bench_stop() - t;
     }
 
     qsort(nops, 10000, sizeof(uint16_t), cmp_nop);
@@ -652,6 +650,24 @@ static void print_cpu_name(void)
 }
 
 #if CONFIG_LINUX_PERF
+uint64_t checkasm_bench_linux_perf_start(void)
+{
+    int fd = state.sysfd;
+
+    ioctl(fd, PERF_EVENT_IOC_RESET, 0);
+    ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
+    return 0;
+}
+
+uint64_t checkasm_bench_linux_perf_stop(void)
+{
+    uint64_t t;
+    int fd = state.sysfd;
+
+    ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
+    return (read(fd, &t, sizeof(t)) == sizeof (t)) ? t : 0;
+}
+
 static int bench_init_linux(void)
 {
     struct perf_event_attr attr = {
diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
index 117d4dd35c..8a62b98f3e 100644
--- a/tests/checkasm/checkasm.h
+++ b/tests/checkasm/checkasm.h
@@ -238,25 +238,20 @@ typedef struct CheckasmPerf {
     int iterations;
 } CheckasmPerf;
 
-#if defined(AV_READ_TIME) || CONFIG_LINUX_PERF || CONFIG_MACOS_KPERF
-
 #if CONFIG_LINUX_PERF
-#define PERF_START(t) do {                              \
-    ioctl(sysfd, PERF_EVENT_IOC_RESET, 0);              \
-    ioctl(sysfd, PERF_EVENT_IOC_ENABLE, 0);             \
-} while (0)
-#define PERF_STOP(t) do {                               \
-    int ret;                                            \
-    ioctl(sysfd, PERF_EVENT_IOC_DISABLE, 0);            \
-    ret = read(sysfd, &t, sizeof(t));                   \
-    (void)ret;                                          \
-} while (0)
+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 PERF_START(t) t = ff_kperf_cycles()
-#define PERF_STOP(t)  t = ff_kperf_cycles() - t
+#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 PERF_START(t) t = AV_READ_TIME()
-#define PERF_STOP(t)  t = AV_READ_TIME() - t
+#define checkasm_bench_start() UINT64_C(0)
+#define checkasm_bench_stop()  UINT64_C(0)
 #endif
 
 /* Benchmark the function */
@@ -270,12 +265,12 @@ typedef struct CheckasmPerf {
             int ti, tcount = 0;\
             uint64_t t = 0; \
             for (ti = 0; ti < BENCH_RUNS; ti++) {\
-                PERF_START(t);\
+                t = checkasm_bench_start(); \
                 tfunc(__VA_ARGS__);\
                 tfunc(__VA_ARGS__);\
                 tfunc(__VA_ARGS__);\
                 tfunc(__VA_ARGS__);\
-                PERF_STOP(t);\
+                t = checkasm_bench_stop() - t;\
                 if (t*tcount <= tsum*4 && ti > 0) {\
                     tsum += t;\
                     tcount++;\
@@ -286,11 +281,6 @@ typedef struct CheckasmPerf {
             perf->iterations++;\
         }\
     } while (0)
-#else
-#define bench_new(...) while(0)
-#define PERF_START(t)  while(0)
-#define PERF_STOP(t)   while(0)
-#endif
 
 #define DECL_CHECKASM_CHECK_FUNC(type) \
 int checkasm_check_##type(const char *const file, const int line, \
-- 
2.40.1



More information about the ffmpeg-devel mailing list