FFmpeg
macos_kperf.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include "avassert.h"
20 #include "macos_kperf.h"
21 #include "thread.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <dlfcn.h>
26 
27 #define KPERF_LIST \
28  F(int, kpc_get_counting, void) \
29  F(int, kpc_force_all_ctrs_set, int) \
30  F(int, kpc_set_counting, uint32_t) \
31  F(int, kpc_set_thread_counting, uint32_t) \
32  F(int, kpc_set_config, uint32_t, void *) \
33  F(int, kpc_get_config, uint32_t, void *) \
34  F(int, kpc_set_period, uint32_t, void *) \
35  F(int, kpc_get_period, uint32_t, void *) \
36  F(uint32_t, kpc_get_counter_count, uint32_t) \
37  F(uint32_t, kpc_get_config_count, uint32_t) \
38  F(int, kperf_sample_get, int *) \
39  F(int, kpc_get_thread_counters, int, unsigned int, void *)
40 
41 #define F(ret, name, ...) \
42  typedef ret name##proc(__VA_ARGS__); \
43  static name##proc *name = NULL;
45 #undef F
46 
47 #define CFGWORD_EL0A32EN_MASK (0x10000)
48 #define CFGWORD_EL0A64EN_MASK (0x20000)
49 #define CFGWORD_EL1EN_MASK (0x40000)
50 #define CFGWORD_EL3EN_MASK (0x80000)
51 #define CFGWORD_ALLMODES_MASK (0xf0000)
52 
53 #define CPMU_NONE 0
54 #define CPMU_CORE_CYCLE 0x02
55 #define CPMU_INST_A64 0x8c
56 #define CPMU_INST_BRANCH 0x8d
57 #define CPMU_SYNC_DC_LOAD_MISS 0xbf
58 #define CPMU_SYNC_DC_STORE_MISS 0xc0
59 #define CPMU_SYNC_DTLB_MISS 0xc1
60 #define CPMU_SYNC_ST_HIT_YNGR_LD 0xc4
61 #define CPMU_SYNC_BR_ANY_MISP 0xcb
62 #define CPMU_FED_IC_MISS_DEM 0xd3
63 #define CPMU_FED_ITLB_MISS 0xd4
64 
65 #define KPC_CLASS_FIXED_MASK (1 << 0)
66 #define KPC_CLASS_CONFIGURABLE_MASK (1 << 1)
67 #define KPC_CLASS_POWER_MASK (1 << 2)
68 #define KPC_CLASS_RAWPMU_MASK (1 << 3)
69 
70 #define COUNTERS_COUNT 10
71 #define CONFIG_COUNT 8
72 #define KPC_MASK (KPC_CLASS_CONFIGURABLE_MASK | KPC_CLASS_FIXED_MASK)
73 
74 static void kperf_init(void)
75 {
76  uint64_t config[COUNTERS_COUNT] = {0};
77  void *kperf = NULL;
78 
79  av_assert0(kperf = dlopen("/System/Library/PrivateFrameworks/kperf.framework/Versions/A/kperf", RTLD_LAZY));
80 
81 #define F(ret, name, ...) av_assert0(name = (name##proc *)(dlsym(kperf, #name)));
83 #undef F
84 
85  av_assert0(kpc_get_counter_count(KPC_MASK) == COUNTERS_COUNT);
86  av_assert0(kpc_get_config_count(KPC_MASK) == CONFIG_COUNT);
87 
89  // config[3] = CPMU_INST_BRANCH | CFGWORD_EL0A64EN_MASK;
90  // config[4] = CPMU_SYNC_BR_ANY_MISP | CFGWORD_EL0A64EN_MASK;
91  // config[5] = CPMU_INST_A64 | CFGWORD_EL0A64EN_MASK;
92 
93  av_assert0(kpc_set_config(KPC_MASK, config) == 0 || !"the kperf API needs to be run as root");
94  av_assert0(kpc_force_all_ctrs_set(1) == 0);
95  av_assert0(kpc_set_counting(KPC_MASK) == 0);
96  av_assert0(kpc_set_thread_counting(KPC_MASK) == 0);
97 }
98 
99 void ff_kperf_init(void)
100 {
101  static AVOnce init_static_once = AV_ONCE_INIT;
102  ff_thread_once(&init_static_once, kperf_init);
103 }
104 
105 uint64_t ff_kperf_cycles(void)
106 {
107  uint64_t counters[COUNTERS_COUNT];
108  if (kpc_get_thread_counters(0, COUNTERS_COUNT, counters)) {
109  return -1;
110  }
111 
112  return counters[0];
113 }
thread.h
CONFIG_COUNT
#define CONFIG_COUNT
Definition: macos_kperf.c:71
tf_sess_config.config
config
Definition: tf_sess_config.py:33
CPMU_CORE_CYCLE
#define CPMU_CORE_CYCLE
Definition: macos_kperf.c:54
avassert.h
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
NULL
#define NULL
Definition: coverity.c:32
macos_kperf.h
KPC_MASK
#define KPC_MASK
Definition: macos_kperf.c:72
AVOnce
#define AVOnce
Definition: thread.h:202
KPERF_LIST
#define KPERF_LIST
Definition: macos_kperf.c:27
ff_kperf_init
void ff_kperf_init(void)
Definition: macos_kperf.c:99
COUNTERS_COUNT
#define COUNTERS_COUNT
Definition: macos_kperf.c:70
ff_kperf_cycles
uint64_t ff_kperf_cycles(void)
Definition: macos_kperf.c:105
kperf_init
static void kperf_init(void)
Definition: macos_kperf.c:74
CFGWORD_EL0A64EN_MASK
#define CFGWORD_EL0A64EN_MASK
Definition: macos_kperf.c:48