| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#if defined(__linux__) |
| 38 |
#if !defined(_GNU_SOURCE) |
| 39 |
|
| 40 |
#define _GNU_SOURCE |
| 41 |
#endif |
| 42 |
#endif |
| 43 |
|
| 44 |
#include "hal.h" |
| 45 |
|
| 46 |
#if defined(PMU_CYCLES) |
| 47 |
|
| 48 |
#if defined(__x86_64__) |
| 49 |
|
| 50 |
void enable_cyclecounter(void) {} |
| 51 |
|
| 52 |
void disable_cyclecounter(void) {} |
| 53 |
|
| 54 |
uint64_t get_cyclecounter(void) |
| 55 |
{ |
| 56 |
uint64_t result; |
| 57 |
|
| 58 |
__asm__ volatile("rdtsc; shlq $32,%%rdx; orq %%rdx,%%rax" |
| 59 |
: "=a"(result) |
| 60 |
: |
| 61 |
: "%rdx"); |
| 62 |
|
| 63 |
return result; |
| 64 |
} |
| 65 |
|
| 66 |
#elif defined(__AARCH64EL__) || defined(_M_ARM64) |
| 67 |
|
| 68 |
void enable_cyclecounter(void) |
| 69 |
{ |
| 70 |
uint64_t tmp; |
| 71 |
__asm __volatile( |
| 72 |
"mrs %[tmp], pmcr_el0\n" |
| 73 |
"orr %[tmp], %[tmp], #1\n" |
| 74 |
"msr pmcr_el0, %[tmp]\n" |
| 75 |
"mrs %[tmp], pmcntenset_el0\n" |
| 76 |
"orr %[tmp], %[tmp], #1<<31\n" |
| 77 |
"msr pmcntenset_el0, %[tmp]\n" |
| 78 |
: [tmp] "=r"(tmp)); |
| 79 |
} |
| 80 |
|
| 81 |
void disable_cyclecounter(void) |
| 82 |
{ |
| 83 |
uint64_t tmp; |
| 84 |
__asm __volatile( |
| 85 |
"mov %[tmp], #0x3f\n" |
| 86 |
"orr %[tmp], %[tmp], #1<<31\n" |
| 87 |
"msr pmcntenclr_el0, %[tmp]\n" |
| 88 |
: [tmp] "=r"(tmp)); |
| 89 |
} |
| 90 |
|
| 91 |
uint64_t get_cyclecounter(void) |
| 92 |
{ |
| 93 |
uint64_t retval; |
| 94 |
__asm __volatile("mrs %[retval], pmccntr_el0\n" : [retval] "=r"(retval)); |
| 95 |
return retval; |
| 96 |
} |
| 97 |
|
| 98 |
#else |
| 99 |
#error PMU_CYCLES option only supported on x86_64 and AArch64 |
| 100 |
#endif |
| 101 |
|
| 102 |
#elif defined(PERF_CYCLES) |
| 103 |
|
| 104 |
#include <asm/unistd.h> |
| 105 |
#include <linux/perf_event.h> |
| 106 |
#include <stdio.h> |
| 107 |
#include <stdlib.h> |
| 108 |
#include <string.h> |
| 109 |
#include <sys/ioctl.h> |
| 110 |
#include <sys/syscall.h> |
| 111 |
#include <unistd.h> |
| 112 |
|
| 113 |
static int perf_fd = 0; |
| 114 |
void enable_cyclecounter(void) |
| 115 |
{ |
| 116 |
struct perf_event_attr pe; |
| 117 |
memset(&pe, 0, sizeof(struct perf_event_attr)); |
| 118 |
pe.type = PERF_TYPE_HARDWARE; |
| 119 |
pe.size = sizeof(struct perf_event_attr); |
| 120 |
pe.config = PERF_COUNT_HW_CPU_CYCLES; |
| 121 |
pe.disabled = 1; |
| 122 |
pe.exclude_kernel = 1; |
| 123 |
pe.exclude_hv = 1; |
| 124 |
|
| 125 |
perf_fd = (int)syscall(__NR_perf_event_open, &pe, 0, -1, -1, 0); |
| 126 |
|
| 127 |
ioctl(perf_fd, PERF_EVENT_IOC_RESET, 0); |
| 128 |
ioctl(perf_fd, PERF_EVENT_IOC_ENABLE, 0); |
| 129 |
} |
| 130 |
|
| 131 |
void disable_cyclecounter(void) |
| 132 |
{ |
| 133 |
ioctl(perf_fd, PERF_EVENT_IOC_DISABLE, 0); |
| 134 |
close(perf_fd); |
| 135 |
} |
| 136 |
|
| 137 |
uint64_t get_cyclecounter(void) |
| 138 |
{ |
| 139 |
long long cpu_cycles; |
| 140 |
ioctl(perf_fd, PERF_EVENT_IOC_DISABLE, 0); |
| 141 |
ssize_t read_count = read(perf_fd, &cpu_cycles, sizeof(cpu_cycles)); |
| 142 |
if (read_count < 0) |
| 143 |
{ |
| 144 |
perror("read"); |
| 145 |
exit(EXIT_FAILURE); |
| 146 |
} |
| 147 |
else if (read_count == 0) |
| 148 |
{ |
| 149 |
|
| 150 |
printf("perf counter empty\n"); |
| 151 |
exit(EXIT_FAILURE); |
| 152 |
} |
| 153 |
ioctl(perf_fd, PERF_EVENT_IOC_ENABLE, 0); |
| 154 |
return (uint64_t)cpu_cycles; |
| 155 |
} |
| 156 |
#elif defined(MAC_CYCLES) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
#include <dlfcn.h> |
| 162 |
#include <pthread.h> |
| 163 |
#include <stdio.h> |
| 164 |
#include <stdlib.h> |
| 165 |
|
| 166 |
#define KPERF_LIST \ |
| 167 |
\ |
| 168 |
F(int, kpc_get_counting, void) \ |
| 169 |
F(int, kpc_force_all_ctrs_set, int) \ |
| 170 |
F(int, kpc_set_counting, uint32_t) \ |
| 171 |
F(int, kpc_set_thread_counting, uint32_t) \ |
| 172 |
F(int, kpc_set_config, uint32_t, void *) \ |
| 173 |
F(int, kpc_get_config, uint32_t, void *) \ |
| 174 |
F(int, kpc_set_period, uint32_t, void *) \ |
| 175 |
F(int, kpc_get_period, uint32_t, void *) \ |
| 176 |
F(uint32_t, kpc_get_counter_count, uint32_t) \ |
| 177 |
F(uint32_t, kpc_get_config_count, uint32_t) \ |
| 178 |
F(int, kperf_sample_get, int *) \ |
| 179 |
F(int, kpc_get_thread_counters, int, unsigned, void *) |
| 180 |
|
| 181 |
#define F(ret, name, ...) \ |
| 182 |
typedef ret name##proc(__VA_ARGS__); \ |
| 183 |
static name##proc *name; |
| 184 |
KPERF_LIST |
| 185 |
#undef F |
| 186 |
|
| 187 |
#define CFGWORD_EL0A32EN_MASK (0x10000) |
| 188 |
#define CFGWORD_EL0A64EN_MASK (0x20000) |
| 189 |
#define CFGWORD_EL1EN_MASK (0x40000) |
| 190 |
#define CFGWORD_EL3EN_MASK (0x80000) |
| 191 |
#define CFGWORD_ALLMODES_MASK (0xf0000) |
| 192 |
|
| 193 |
#define CPMU_NONE 0 |
| 194 |
#define CPMU_CORE_CYCLE 0x02 |
| 195 |
#define CPMU_INST_A64 0x8c |
| 196 |
#define CPMU_INST_BRANCH 0x8d |
| 197 |
#define CPMU_SYNC_DC_LOAD_MISS 0xbf |
| 198 |
#define CPMU_SYNC_DC_STORE_MISS 0xc0 |
| 199 |
#define CPMU_SYNC_DTLB_MISS 0xc1 |
| 200 |
#define CPMU_SYNC_ST_HIT_YNGR_LD 0xc4 |
| 201 |
#define CPMU_SYNC_BR_ANY_MISP 0xcb |
| 202 |
#define CPMU_FED_IC_MISS_DEM 0xd3 |
| 203 |
#define CPMU_FED_ITLB_MISS 0xd4 |
| 204 |
|
| 205 |
#define KPC_CLASS_FIXED (0) |
| 206 |
#define KPC_CLASS_CONFIGURABLE (1) |
| 207 |
#define KPC_CLASS_POWER (2) |
| 208 |
#define KPC_CLASS_RAWPMU (3) |
| 209 |
#define KPC_CLASS_FIXED_MASK (1u << KPC_CLASS_FIXED) |
| 210 |
#define KPC_CLASS_CONFIGURABLE_MASK (1u << KPC_CLASS_CONFIGURABLE) |
| 211 |
#define KPC_CLASS_POWER_MASK (1u << KPC_CLASS_POWER) |
| 212 |
#define KPC_CLASS_RAWPMU_MASK (1u << KPC_CLASS_RAWPMU) |
| 213 |
|
| 214 |
#define COUNTERS_COUNT 10 |
| 215 |
#define CONFIG_COUNT 8 |
| 216 |
#define KPC_MASK (KPC_CLASS_CONFIGURABLE_MASK | KPC_CLASS_FIXED_MASK) |
| 217 |
uint64_t g_counters[COUNTERS_COUNT]; |
| 218 |
uint64_t g_config[COUNTERS_COUNT]; |
| 219 |
|
| 220 |
|
| 221 |
static void configure_rdtsc(void) |
| 222 |
{ |
| 223 |
if (kpc_force_all_ctrs_set(1)) |
| 224 |
{ |
| 225 |
printf("kpc_force_all_ctrs_set failed\n"); |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
if (kpc_set_counting(KPC_MASK)) |
| 230 |
{ |
| 231 |
printf("kpc_set_counting failed\n"); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
if (kpc_set_thread_counting(KPC_MASK)) |
| 236 |
{ |
| 237 |
printf("kpc_set_thread_counting failed\n"); |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
if (kpc_set_config(KPC_MASK, g_config)) |
| 242 |
{ |
| 243 |
printf("kpc_set_config failed\n"); |
| 244 |
return; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
static void init_rdtsc(void) |
| 249 |
{ |
| 250 |
void *kperf = dlopen( |
| 251 |
"/System/Library/PrivateFrameworks/kperf.framework/Versions/A/kperf", |
| 252 |
RTLD_LAZY); |
| 253 |
if (!kperf) |
| 254 |
{ |
| 255 |
printf("kperf = %p\n", kperf); |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
#pragma GCC diagnostic push |
| 263 |
#pragma GCC diagnostic ignored "-Wpedantic" |
| 264 |
|
| 265 |
#define F(ret, name, ...) \ |
| 266 |
name = (name##proc *)(dlsym(kperf, #name)); \ |
| 267 |
if (!name) \ |
| 268 |
{ \ |
| 269 |
printf("%s = %p\n", #name, (void *)name); \ |
| 270 |
return; \ |
| 271 |
} |
| 272 |
KPERF_LIST |
| 273 |
#undef F |
| 274 |
|
| 275 |
#pragma GCC diagnostic pop |
| 276 |
|
| 277 |
g_config[0] = CPMU_CORE_CYCLE | CFGWORD_EL0A64EN_MASK; |
| 278 |
} |
| 279 |
|
| 280 |
void enable_cyclecounter(void) |
| 281 |
{ |
| 282 |
int test_high_perf_cores = 1; |
| 283 |
|
| 284 |
if (test_high_perf_cores) |
| 285 |
{ |
| 286 |
pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0); |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
pthread_set_qos_class_self_np(QOS_CLASS_BACKGROUND, 0); |
| 291 |
} |
| 292 |
init_rdtsc(); |
| 293 |
configure_rdtsc(); |
| 294 |
} |
| 295 |
|
| 296 |
void disable_cyclecounter(void) { return; } |
| 297 |
|
| 298 |
uint64_t get_cyclecounter(void) |
| 299 |
{ |
| 300 |
if (kpc_get_thread_counters(0, COUNTERS_COUNT, g_counters)) |
| 301 |
{ |
| 302 |
printf("kpc_get_thread_counters failed\n"); |
| 303 |
return 1; |
| 304 |
} |
| 305 |
return g_counters[2]; |
| 306 |
} |
| 307 |
|
| 308 |
#else |
| 309 |
|
| 310 |
void enable_cyclecounter(void) { return; } |
| 311 |
void disable_cyclecounter(void) { return; } |
| 312 |
uint64_t get_cyclecounter(void) { return (0); } |
| 313 |
|
| 314 |
#endif |