aboutsummaryrefslogtreecommitdiffstats
path: root/libavutil
diff options
context:
space:
mode:
authorJ. Dekker <jdek@itanimul.li>2021-07-21 16:21:34 +0200
committerJ. Dekker <jdek@itanimul.li>2021-07-21 16:35:27 +0200
commitc866a099b297203306165be3c444d481fcb22553 (patch)
tree8c550437858d60d51bb91c4249d8d0b5bc0ead84 /libavutil
parentf614390eccbef569cdff7bcccebf88f2920389b8 (diff)
downloadffmpeg-c866a099b297203306165be3c444d481fcb22553.tar.gz
lavu/kperf: use ff_thread_once()
Signed-off-by: J. Dekker <jdek@itanimul.li>
Diffstat (limited to 'libavutil')
-rw-r--r--libavutil/macos_kperf.c65
-rw-r--r--libavutil/macos_kperf.h2
-rw-r--r--libavutil/timer.h7
3 files changed, 21 insertions, 53 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 \