[FFmpeg-cvslog] lavu/kperf: use ff_thread_once()

J. Dekker git at videolan.org
Wed Jul 21 17:46:31 EEST 2021


ffmpeg | branch: master | J. Dekker <jdek at itanimul.li> | Wed Jul 21 16:21:34 2021 +0200| [c866a099b297203306165be3c444d481fcb22553] | committer: J. Dekker

lavu/kperf: use ff_thread_once()

Signed-off-by: J. Dekker <jdek at itanimul.li>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=c866a099b297203306165be3c444d481fcb22553
---

 libavutil/macos_kperf.c   | 65 +++++++++++++----------------------------------
 libavutil/macos_kperf.h   |  2 +-
 libavutil/timer.h         |  7 +++--
 tests/checkasm/checkasm.c |  6 +----
 4 files changed, 22 insertions(+), 58 deletions(-)

diff --git a/libavutil/macos_kperf.c b/libavutil/macos_kperf.c
index cb229130f5..9fc04c6349 100644
--- a/libavutil/macos_kperf.c
+++ b/libavutil/macos_kperf.c
@@ -16,7 +16,10 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include "avassert.h"
 #include "macos_kperf.h"
+#include "thread.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <dlfcn.h>
@@ -68,69 +71,35 @@ KPERF_LIST
 #define CONFIG_COUNT 8
 #define KPC_MASK (KPC_CLASS_CONFIGURABLE_MASK | KPC_CLASS_FIXED_MASK)
 
-static int ff_kperf_was_init = 0;
-
-int ff_kperf_init()
+static void kperf_init(void)
 {
     uint64_t config[COUNTERS_COUNT] = {0};
     void *kperf = NULL;
 
-    if (ff_kperf_was_init)
-        return 0;
+    av_assert0(kperf = dlopen("/System/Library/PrivateFrameworks/kperf.framework/Versions/A/kperf", RTLD_LAZY));
 
-    kperf = dlopen("/System/Library/PrivateFrameworks/kperf.framework/Versions/A/kperf", RTLD_LAZY);
-    if (!kperf) {
-        fprintf(stderr, "kperf: kperf = %p\n", kperf);
-        return -1;
-    }
-
-#define F(ret, name, ...)                            \
-    name = (name##proc *)(dlsym(kperf, #name));      \
-    if (!name) {                                     \
-        fprintf(stderr, "kperf: %s = %p\n", #name, (void *)name);    \
-        return -1;                                   \
-    }
+#define F(ret, name, ...) av_assert0(name = (name##proc *)(dlsym(kperf, #name)));
     KPERF_LIST
 #undef F
 
-    if (kpc_get_counter_count(KPC_MASK) != COUNTERS_COUNT) {
-        fprintf(stderr, "kperf: wrong fixed counters count\n");
-        return -1;
-    }
-
-    if (kpc_get_config_count(KPC_MASK) != CONFIG_COUNT) {
-        fprintf(stderr, "kperf: wrong fixed config count\n");
-        return -1;
-    }
+    av_assert0(kpc_get_counter_count(KPC_MASK) == COUNTERS_COUNT);
+    av_assert0(kpc_get_config_count(KPC_MASK) == CONFIG_COUNT);
 
     config[0] = CPMU_CORE_CYCLE | CFGWORD_EL0A64EN_MASK;
     // config[3] = CPMU_INST_BRANCH | CFGWORD_EL0A64EN_MASK;
     // config[4] = CPMU_SYNC_BR_ANY_MISP | CFGWORD_EL0A64EN_MASK;
     // config[5] = CPMU_INST_A64 | CFGWORD_EL0A64EN_MASK;
 
-    if (kpc_set_config(KPC_MASK, config)) {
-        fprintf(stderr, "kperf: kpc_set_config failed\n");
-        return -1;
-    }
-
-    if (kpc_force_all_ctrs_set(1)) {
-        fprintf(stderr, "kperf: kpc_force_all_ctrs_set failed\n");
-        return -1;
-    }
-
-    if (kpc_set_counting(KPC_MASK)) {
-        fprintf(stderr, "kperf: kpc_set_counting failed\n");
-        return -1;
-    }
-
-    if (kpc_set_thread_counting(KPC_MASK)) {
-        fprintf(stderr, "kperf: kpc_set_thread_counting failed\n");
-        return -1;
-    }
-
-    ff_kperf_was_init = 1;
+    av_assert0(kpc_set_config(KPC_MASK, config) == 0 || !"the kperf API needs to be run as root");
+    av_assert0(kpc_force_all_ctrs_set(1) == 0);
+    av_assert0(kpc_set_counting(KPC_MASK) == 0);
+    av_assert0(kpc_set_thread_counting(KPC_MASK) == 0);
+}
 
-    return 0;
+void ff_kperf_init(void)
+{
+    static AVOnce init_static_once = AV_ONCE_INIT;
+    ff_thread_once(&init_static_once, kperf_init);
 }
 
 uint64_t ff_kperf_cycles()
diff --git a/libavutil/macos_kperf.h b/libavutil/macos_kperf.h
index 63b004214e..d039691340 100644
--- a/libavutil/macos_kperf.h
+++ b/libavutil/macos_kperf.h
@@ -21,7 +21,7 @@
 
 #include <stdint.h>
 
-int ff_kperf_init(void);
+void ff_kperf_init(void);
 uint64_t ff_kperf_cycles(void);
 
 #endif /* AVUTIL_MACOS_KPERF_H */
diff --git a/libavutil/timer.h b/libavutil/timer.h
index 1cf384d772..71ea2f912e 100644
--- a/libavutil/timer.h
+++ b/libavutil/timer.h
@@ -131,12 +131,11 @@
 
 #define START_TIMER                                                         \
     uint64_t tperf;                                                         \
-    if (ff_kperf_init())                                                    \
-        av_log(NULL, AV_LOG_ERROR, "ff_kperf_init() failed\n");             \
-    tperf = kperf_cycles();
+    ff_kperf_init();                                                        \
+    tperf = ff_kperf_cycles();
 
 #define STOP_TIMER(id)                                                      \
-    TIMER_REPORT(id, kperf_cycles() - tperf);
+    TIMER_REPORT(id, ff_kperf_cycles() - tperf);
 
 #elif defined(AV_READ_TIME)
 #define START_TIMER                             \
diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c
index eb9e2cd677..be5c17cd2a 100644
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -641,11 +641,7 @@ static int bench_init_linux(void)
 #elif CONFIG_MACOS_KPERF
 static int bench_init_kperf(void)
 {
-    if (ff_kperf_init()) {
-        fprintf(stderr, "checkasm must be run as root to use kperf on macOS\n");
-        return -1;
-    }
-
+    ff_kperf_init();
     return 0;
 }
 #else



More information about the ffmpeg-cvslog mailing list