summaryrefslogtreecommitdiff
path: root/libavutil
diff options
context:
space:
mode:
authorJ. Dekker <jdek@itanimul.li>2021-07-20 19:09:22 +0200
committerJ. Dekker <jdek@itanimul.li>2021-07-20 19:40:03 +0200
commit9a727235fd497c22f2370e48dd1443d1376953e7 (patch)
treeaa45dd828846649404001aea6b8e88de8d94bffc /libavutil
parent4f49fa6abe89e2fca2585cac4c63190315972cf0 (diff)
lavu/checkasm: add (private) kperf timing for macOS
Signed-off-by: J. Dekker <jdek@itanimul.li>
Diffstat (limited to 'libavutil')
-rw-r--r--libavutil/Makefile1
-rw-r--r--libavutil/macos_kperf.c144
-rw-r--r--libavutil/macos_kperf.h22
-rw-r--r--libavutil/timer.h15
4 files changed, 181 insertions, 1 deletions
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 47efb718d2..18dc5f22d9 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -181,6 +181,7 @@ OBJS-$(CONFIG_D3D11VA) += hwcontext_d3d11va.o
OBJS-$(CONFIG_DXVA2) += hwcontext_dxva2.o
OBJS-$(CONFIG_LIBDRM) += hwcontext_drm.o
OBJS-$(CONFIG_LZO) += lzo.o
+OBJS-$(CONFIG_MACOS_KPERF) += macos_kperf.o
OBJS-$(CONFIG_MEDIACODEC) += hwcontext_mediacodec.o
OBJS-$(CONFIG_OPENCL) += hwcontext_opencl.o
OBJS-$(CONFIG_QSV) += hwcontext_qsv.o
diff --git a/libavutil/macos_kperf.c b/libavutil/macos_kperf.c
new file mode 100644
index 0000000000..cb229130f5
--- /dev/null
+++ b/libavutil/macos_kperf.c
@@ -0,0 +1,144 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "macos_kperf.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+
+#define KPERF_LIST \
+ F(int, kpc_get_counting, void) \
+ F(int, kpc_force_all_ctrs_set, int) \
+ F(int, kpc_set_counting, uint32_t) \
+ F(int, kpc_set_thread_counting, uint32_t) \
+ F(int, kpc_set_config, uint32_t, void *) \
+ F(int, kpc_get_config, uint32_t, void *) \
+ F(int, kpc_set_period, uint32_t, void *) \
+ F(int, kpc_get_period, uint32_t, void *) \
+ F(uint32_t, kpc_get_counter_count, uint32_t) \
+ F(uint32_t, kpc_get_config_count, uint32_t) \
+ F(int, kperf_sample_get, int *) \
+ F(int, kpc_get_thread_counters, int, unsigned int, void *)
+
+#define F(ret, name, ...) \
+ typedef ret name##proc(__VA_ARGS__); \
+ static name##proc *name = NULL;
+KPERF_LIST
+#undef F
+
+#define CFGWORD_EL0A32EN_MASK (0x10000)
+#define CFGWORD_EL0A64EN_MASK (0x20000)
+#define CFGWORD_EL1EN_MASK (0x40000)
+#define CFGWORD_EL3EN_MASK (0x80000)
+#define CFGWORD_ALLMODES_MASK (0xf0000)
+
+#define CPMU_NONE 0
+#define CPMU_CORE_CYCLE 0x02
+#define CPMU_INST_A64 0x8c
+#define CPMU_INST_BRANCH 0x8d
+#define CPMU_SYNC_DC_LOAD_MISS 0xbf
+#define CPMU_SYNC_DC_STORE_MISS 0xc0
+#define CPMU_SYNC_DTLB_MISS 0xc1
+#define CPMU_SYNC_ST_HIT_YNGR_LD 0xc4
+#define CPMU_SYNC_BR_ANY_MISP 0xcb
+#define CPMU_FED_IC_MISS_DEM 0xd3
+#define CPMU_FED_ITLB_MISS 0xd4
+
+#define KPC_CLASS_FIXED_MASK (1 << 0)
+#define KPC_CLASS_CONFIGURABLE_MASK (1 << 1)
+#define KPC_CLASS_POWER_MASK (1 << 2)
+#define KPC_CLASS_RAWPMU_MASK (1 << 3)
+
+#define COUNTERS_COUNT 10
+#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()
+{
+ uint64_t config[COUNTERS_COUNT] = {0};
+ void *kperf = NULL;
+
+ if (ff_kperf_was_init)
+ return 0;
+
+ 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; \
+ }
+ 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;
+ }
+
+ 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;
+
+ return 0;
+}
+
+uint64_t ff_kperf_cycles()
+{
+ uint64_t counters[COUNTERS_COUNT];
+ if (kpc_get_thread_counters(0, COUNTERS_COUNT, counters)) {
+ return -1;
+ }
+
+ return counters[0];
+}
diff --git a/libavutil/macos_kperf.h b/libavutil/macos_kperf.h
new file mode 100644
index 0000000000..a295824803
--- /dev/null
+++ b/libavutil/macos_kperf.h
@@ -0,0 +1,22 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdint.h>
+
+int ff_kperf_init(void);
+uint64_t ff_kperf_cycles(void);
diff --git a/libavutil/timer.h b/libavutil/timer.h
index 36f920e96a..1cf384d772 100644
--- a/libavutil/timer.h
+++ b/libavutil/timer.h
@@ -42,7 +42,9 @@
#include <stdint.h>
#include <inttypes.h>
-#if HAVE_MACH_ABSOLUTE_TIME
+#if CONFIG_MACOS_KPERF
+#include "macos_kperf.h"
+#elif HAVE_MACH_ABSOLUTE_TIME
#include <mach/mach_time.h>
#endif
@@ -125,6 +127,17 @@
read(linux_perf_fd, &tperf, sizeof(tperf)); \
TIMER_REPORT(id, tperf)
+#elif CONFIG_MACOS_KPERF
+
+#define START_TIMER \
+ uint64_t tperf; \
+ if (ff_kperf_init()) \
+ av_log(NULL, AV_LOG_ERROR, "ff_kperf_init() failed\n"); \
+ tperf = kperf_cycles();
+
+#define STOP_TIMER(id) \
+ TIMER_REPORT(id, kperf_cycles() - tperf);
+
#elif defined(AV_READ_TIME)
#define START_TIMER \
uint64_t tend; \