cirron 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,902 @@
1
+ // Original design from:
2
+ // =============================================================================
3
+ // XNU kperf/kpc
4
+ // Available for 64-bit Intel/Apple Silicon, macOS/iOS, with root privileges
5
+ //
6
+ // References:
7
+ //
8
+ // XNU source (since xnu 2422.1.72):
9
+ // https://github.com/apple/darwin-xnu/blob/main/osfmk/kern/kpc.h
10
+ // https://github.com/apple/darwin-xnu/blob/main/bsd/kern/kern_kpc.c
11
+ //
12
+ // Lightweight PET (Profile Every Thread, since xnu 3789.1.32):
13
+ // https://github.com/apple/darwin-xnu/blob/main/osfmk/kperf/pet.c
14
+ // https://github.com/apple/darwin-xnu/blob/main/osfmk/kperf/kperf_kpc.c
15
+ //
16
+ // System Private frameworks (since macOS 10.11, iOS 8.0):
17
+ // /System/Library/PrivateFrameworks/kperf.framework
18
+ // /System/Library/PrivateFrameworks/kperfdata.framework
19
+ //
20
+ // Xcode framework (since Xcode 7.0):
21
+ // /Applications/Xcode.app/Contents/SharedFrameworks/DVTInstrumentsFoundation.framework
22
+ //
23
+ // CPU database (plist files)
24
+ // macOS (since macOS 10.11):
25
+ // /usr/share/kpep/<name>.plist
26
+ // iOS (copied from Xcode, since iOS 10.0, Xcode 8.0):
27
+ // /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform
28
+ // /DeviceSupport/<version>/DeveloperDiskImage.dmg/usr/share/kpep/<name>.plist
29
+ //
30
+ //
31
+ // Created by YaoYuan <ibireme@gmail.com> on 2021.
32
+ // Released into the public domain (unlicense.org).
33
+ // =============================================================================
34
+
35
+ #ifndef M1CYCLES_H
36
+ #define M1CYCLES_H
37
+
38
+ #include <stdbool.h>
39
+ #include <stdint.h>
40
+ #include <stdio.h>
41
+ #include <stdlib.h>
42
+ #include <string.h>
43
+
44
+ #include <dlfcn.h> // for dlopen() and dlsym()
45
+ #include <mach/mach_time.h> // for mach_absolute_time()
46
+ #include <sys/kdebug.h> // for kdebug trace decode
47
+ #include <sys/sysctl.h> // for sysctl()
48
+ #include <unistd.h> // for usleep()
49
+
50
+ struct performance_counters
51
+ {
52
+ double cycles;
53
+ double branches;
54
+ double missed_branches;
55
+ double instructions;
56
+ performance_counters(uint64_t c, uint64_t b, uint64_t m, uint64_t i)
57
+ : cycles(c), branches(b), missed_branches(m), instructions(i) {}
58
+ performance_counters(double c, double b, double m, double i)
59
+ : cycles(c), branches(b), missed_branches(m), instructions(i) {}
60
+ performance_counters(double init)
61
+ : cycles(init),
62
+ branches(init),
63
+ missed_branches(init),
64
+ instructions(init) {}
65
+
66
+ inline performance_counters &operator-=(const performance_counters &other)
67
+ {
68
+ cycles -= other.cycles;
69
+ branches -= other.branches;
70
+ missed_branches -= other.missed_branches;
71
+ instructions -= other.instructions;
72
+ return *this;
73
+ }
74
+ inline performance_counters &min(const performance_counters &other)
75
+ {
76
+ cycles = other.cycles < cycles ? other.cycles : cycles;
77
+ branches = other.branches < branches ? other.branches : branches;
78
+ missed_branches = other.missed_branches < missed_branches
79
+ ? other.missed_branches
80
+ : missed_branches;
81
+ instructions =
82
+ other.instructions < instructions ? other.instructions : instructions;
83
+ return *this;
84
+ }
85
+ inline performance_counters &operator+=(const performance_counters &other)
86
+ {
87
+ cycles += other.cycles;
88
+ branches += other.branches;
89
+ missed_branches += other.missed_branches;
90
+ instructions += other.instructions;
91
+ return *this;
92
+ }
93
+
94
+ inline performance_counters &operator/=(double numerator)
95
+ {
96
+ cycles /= numerator;
97
+ branches /= numerator;
98
+ missed_branches /= numerator;
99
+ instructions /= numerator;
100
+ return *this;
101
+ }
102
+ };
103
+
104
+ inline performance_counters operator-(const performance_counters &a,
105
+ const performance_counters &b)
106
+ {
107
+ return performance_counters(a.cycles - b.cycles, a.branches - b.branches,
108
+ a.missed_branches - b.missed_branches,
109
+ a.instructions - b.instructions);
110
+ }
111
+
112
+ typedef float f32;
113
+ typedef double f64;
114
+ typedef int8_t i8;
115
+ typedef uint8_t u8;
116
+ typedef int16_t i16;
117
+ typedef uint16_t u16;
118
+ typedef int32_t i32;
119
+ typedef uint32_t u32;
120
+ typedef int64_t i64;
121
+ typedef uint64_t u64;
122
+ typedef size_t usize;
123
+
124
+ // -----------------------------------------------------------------------------
125
+ // <kperf.framework> header (reverse engineered)
126
+ // This framework wraps some sysctl calls to communicate with the kpc in kernel.
127
+ // Most functions requires root privileges, or process is "blessed".
128
+ // -----------------------------------------------------------------------------
129
+
130
+ // Cross-platform class constants.
131
+ #define KPC_CLASS_FIXED (0)
132
+ #define KPC_CLASS_CONFIGURABLE (1)
133
+ #define KPC_CLASS_POWER (2)
134
+ #define KPC_CLASS_RAWPMU (3)
135
+
136
+ // Cross-platform class mask constants.
137
+ #define KPC_CLASS_FIXED_MASK (1u << KPC_CLASS_FIXED) // 1
138
+ #define KPC_CLASS_CONFIGURABLE_MASK (1u << KPC_CLASS_CONFIGURABLE) // 2
139
+ #define KPC_CLASS_POWER_MASK (1u << KPC_CLASS_POWER) // 4
140
+ #define KPC_CLASS_RAWPMU_MASK (1u << KPC_CLASS_RAWPMU) // 8
141
+
142
+ // PMU version constants.
143
+ #define KPC_PMU_ERROR (0) // Error
144
+ #define KPC_PMU_INTEL_V3 (1) // Intel
145
+ #define KPC_PMU_ARM_APPLE (2) // ARM64
146
+ #define KPC_PMU_INTEL_V2 (3) // Old Intel
147
+ #define KPC_PMU_ARM_V2 (4) // Old ARM
148
+
149
+ // The maximum number of counters we could read from every class in one go.
150
+ // ARMV7: FIXED: 1, CONFIGURABLE: 4
151
+ // ARM32: FIXED: 2, CONFIGURABLE: 6
152
+ // ARM64: FIXED: 2, CONFIGURABLE: CORE_NCTRS - FIXED (6 or 8)
153
+ // x86: 32
154
+ #define KPC_MAX_COUNTERS 10
155
+
156
+ // Bits for defining what to do on an action.
157
+ // Defined in https://github.com/apple/darwin-xnu/blob/main/osfmk/kperf/action.h
158
+ #define KPERF_SAMPLER_TH_INFO (1U << 0)
159
+ #define KPERF_SAMPLER_TH_SNAPSHOT (1U << 1)
160
+ #define KPERF_SAMPLER_KSTACK (1U << 2)
161
+ #define KPERF_SAMPLER_USTACK (1U << 3)
162
+ #define KPERF_SAMPLER_PMC_THREAD (1U << 4)
163
+ #define KPERF_SAMPLER_PMC_CPU (1U << 5)
164
+ #define KPERF_SAMPLER_PMC_CONFIG (1U << 6)
165
+ #define KPERF_SAMPLER_MEMINFO (1U << 7)
166
+ #define KPERF_SAMPLER_TH_SCHEDULING (1U << 8)
167
+ #define KPERF_SAMPLER_TH_DISPATCH (1U << 9)
168
+ #define KPERF_SAMPLER_TK_SNAPSHOT (1U << 10)
169
+ #define KPERF_SAMPLER_SYS_MEM (1U << 11)
170
+ #define KPERF_SAMPLER_TH_INSCYC (1U << 12)
171
+ #define KPERF_SAMPLER_TK_INFO (1U << 13)
172
+
173
+ // Maximum number of kperf action ids.
174
+ #define KPERF_ACTION_MAX (32)
175
+
176
+ // Maximum number of kperf timer ids.
177
+ #define KPERF_TIMER_MAX (8)
178
+
179
+ // x86/arm config registers are 64-bit
180
+ typedef u64 kpc_config_t;
181
+
182
+ /// Print current CPU identification string to the buffer (same as snprintf),
183
+ /// such as "cpu_7_8_10b282dc_46". This string can be used to locate the PMC
184
+ /// database in /usr/share/kpep.
185
+ /// @return string's length, or negative value if error occurs.
186
+ /// @note This method does not requires root privileges.
187
+ /// @details sysctl get(hw.cputype), get(hw.cpusubtype),
188
+ /// get(hw.cpufamily), get(machdep.cpu.model)
189
+ static int (*kpc_cpu_string)(char *buf, usize buf_size);
190
+
191
+ /// Get the version of KPC that's being run.
192
+ /// @return See `PMU version constants` above.
193
+ /// @details sysctl get(kpc.pmu_version)
194
+ static u32 (*kpc_pmu_version)(void);
195
+
196
+ /// Get running PMC classes.
197
+ /// @return See `class mask constants` above,
198
+ /// 0 if error occurs or no class is set.
199
+ /// @details sysctl get(kpc.counting)
200
+ static u32 (*kpc_get_counting)(void);
201
+
202
+ /// Set PMC classes to enable counting.
203
+ /// @param classes See `class mask constants` above, set 0 to shutdown counting.
204
+ /// @return 0 for success.
205
+ /// @details sysctl set(kpc.counting)
206
+ static int (*kpc_set_counting)(u32 classes);
207
+
208
+ /// Get running PMC classes for current thread.
209
+ /// @return See `class mask constants` above,
210
+ /// 0 if error occurs or no class is set.
211
+ /// @details sysctl get(kpc.thread_counting)
212
+ static u32 (*kpc_get_thread_counting)(void);
213
+
214
+ /// Set PMC classes to enable counting for current thread.
215
+ /// @param classes See `class mask constants` above, set 0 to shutdown counting.
216
+ /// @return 0 for success.
217
+ /// @details sysctl set(kpc.thread_counting)
218
+ static int (*kpc_set_thread_counting)(u32 classes);
219
+
220
+ /// Get how many config registers there are for a given mask.
221
+ /// For example: Intel may returns 1 for `KPC_CLASS_FIXED_MASK`,
222
+ /// returns 4 for `KPC_CLASS_CONFIGURABLE_MASK`.
223
+ /// @param classes See `class mask constants` above.
224
+ /// @return 0 if error occurs or no class is set.
225
+ /// @note This method does not requires root privileges.
226
+ /// @details sysctl get(kpc.config_count)
227
+ static u32 (*kpc_get_config_count)(u32 classes);
228
+
229
+ /// Get config registers.
230
+ /// @param classes see `class mask constants` above.
231
+ /// @param config Config buffer to receive values, should not smaller than
232
+ /// kpc_get_config_count(classes) * sizeof(kpc_config_t).
233
+ /// @return 0 for success.
234
+ /// @details sysctl get(kpc.config_count), get(kpc.config)
235
+ static int (*kpc_get_config)(u32 classes, kpc_config_t *config);
236
+
237
+ /// Set config registers.
238
+ /// @param classes see `class mask constants` above.
239
+ /// @param config Config buffer, should not smaller than
240
+ /// kpc_get_config_count(classes) * sizeof(kpc_config_t).
241
+ /// @return 0 for success.
242
+ /// @details sysctl get(kpc.config_count), set(kpc.config)
243
+ static int (*kpc_set_config)(u32 classes, kpc_config_t *config);
244
+
245
+ /// Get how many counters there are for a given mask.
246
+ /// For example: Intel may returns 3 for `KPC_CLASS_FIXED_MASK`,
247
+ /// returns 4 for `KPC_CLASS_CONFIGURABLE_MASK`.
248
+ /// @param classes See `class mask constants` above.
249
+ /// @note This method does not requires root privileges.
250
+ /// @details sysctl get(kpc.counter_count)
251
+ static u32 (*kpc_get_counter_count)(u32 classes);
252
+
253
+ /// Get counter accumulations.
254
+ /// If `all_cpus` is true, the buffer count should not smaller than
255
+ /// (cpu_count * counter_count). Otherwize, the buffer count should not smaller
256
+ /// than (counter_count).
257
+ /// @see kpc_get_counter_count(), kpc_cpu_count().
258
+ /// @param all_cpus true for all CPUs, false for current cpu.
259
+ /// @param classes See `class mask constants` above.
260
+ /// @param curcpu A pointer to receive current cpu id, can be NULL.
261
+ /// @param buf Buffer to receive counter's value.
262
+ /// @return 0 for success.
263
+ /// @details sysctl get(hw.ncpu), get(kpc.counter_count), get(kpc.counters)
264
+ static int (*kpc_get_cpu_counters)(bool all_cpus, u32 classes, int *curcpu,
265
+ u64 *buf);
266
+
267
+ /// Get counter accumulations for current thread.
268
+ /// @param tid Thread id, should be 0.
269
+ /// @param buf_count The number of buf's elements (not bytes),
270
+ /// should not smaller than kpc_get_counter_count().
271
+ /// @param buf Buffer to receive counter's value.
272
+ /// @return 0 for success.
273
+ /// @details sysctl get(kpc.thread_counters)
274
+ static int (*kpc_get_thread_counters)(u32 tid, u32 buf_count, u64 *buf);
275
+
276
+ /// Acquire/release the counters used by the Power Manager.
277
+ /// @param val 1:acquire, 0:release
278
+ /// @return 0 for success.
279
+ /// @details sysctl set(kpc.force_all_ctrs)
280
+ static int (*kpc_force_all_ctrs_set)(int val);
281
+
282
+ /// Get the state of all_ctrs.
283
+ /// @return 0 for success.
284
+ /// @details sysctl get(kpc.force_all_ctrs)
285
+ static int (*kpc_force_all_ctrs_get)(int *val_out);
286
+
287
+ /// Set number of actions, should be `KPERF_ACTION_MAX`.
288
+ /// @details sysctl set(kperf.action.count)
289
+ static int (*kperf_action_count_set)(u32 count);
290
+
291
+ /// Get number of actions.
292
+ /// @details sysctl get(kperf.action.count)
293
+ static int (*kperf_action_count_get)(u32 *count);
294
+
295
+ /// Set what to sample when a trigger fires an action, e.g.
296
+ /// `KPERF_SAMPLER_PMC_CPU`.
297
+ /// @details sysctl set(kperf.action.samplers)
298
+ static int (*kperf_action_samplers_set)(u32 actionid, u32 sample);
299
+
300
+ /// Get what to sample when a trigger fires an action.
301
+ /// @details sysctl get(kperf.action.samplers)
302
+ static int (*kperf_action_samplers_get)(u32 actionid, u32 *sample);
303
+
304
+ /// Apply a task filter to the action, -1 to disable filter.
305
+ /// @details sysctl set(kperf.action.filter_by_task)
306
+ static int (*kperf_action_filter_set_by_task)(u32 actionid, i32 port);
307
+
308
+ /// Apply a pid filter to the action, -1 to disable filter.
309
+ /// @details sysctl set(kperf.action.filter_by_pid)
310
+ static int (*kperf_action_filter_set_by_pid)(u32 actionid, i32 pid);
311
+
312
+ /// Set number of time triggers, should be `KPERF_TIMER_MAX`.
313
+ /// @details sysctl set(kperf.timer.count)
314
+ static int (*kperf_timer_count_set)(u32 count);
315
+
316
+ /// Get number of time triggers.
317
+ /// @details sysctl get(kperf.timer.count)
318
+ static int (*kperf_timer_count_get)(u32 *count);
319
+
320
+ /// Set timer number and period.
321
+ /// @details sysctl set(kperf.timer.period)
322
+ static int (*kperf_timer_period_set)(u32 actionid, u64 tick);
323
+
324
+ /// Get timer number and period.
325
+ /// @details sysctl get(kperf.timer.period)
326
+ static int (*kperf_timer_period_get)(u32 actionid, u64 *tick);
327
+
328
+ /// Set timer number and actionid.
329
+ /// @details sysctl set(kperf.timer.action)
330
+ static int (*kperf_timer_action_set)(u32 actionid, u32 timerid);
331
+
332
+ /// Get timer number and actionid.
333
+ /// @details sysctl get(kperf.timer.action)
334
+ static int (*kperf_timer_action_get)(u32 actionid, u32 *timerid);
335
+
336
+ /// Set which timer ID does PET (Profile Every Thread).
337
+ /// @details sysctl set(kperf.timer.pet_timer)
338
+ static int (*kperf_timer_pet_set)(u32 timerid);
339
+
340
+ /// Get which timer ID does PET (Profile Every Thread).
341
+ /// @details sysctl get(kperf.timer.pet_timer)
342
+ static int (*kperf_timer_pet_get)(u32 *timerid);
343
+
344
+ /// Enable or disable sampling.
345
+ /// @details sysctl set(kperf.sampling)
346
+ static int (*kperf_sample_set)(u32 enabled);
347
+
348
+ /// Get is currently sampling.
349
+ /// @details sysctl get(kperf.sampling)
350
+ static int (*kperf_sample_get)(u32 *enabled);
351
+
352
+ /// Reset kperf: stop sampling, kdebug, timers and actions.
353
+ /// @return 0 for success.
354
+ static int (*kperf_reset)(void);
355
+
356
+ /// Nanoseconds to CPU ticks.
357
+ static u64 (*kperf_ns_to_ticks)(u64 ns);
358
+
359
+ /// CPU ticks to nanoseconds.
360
+ static u64 (*kperf_ticks_to_ns)(u64 ticks);
361
+
362
+ /// CPU ticks frequency (mach_absolute_time).
363
+ static u64 (*kperf_tick_frequency)(void);
364
+
365
+ // -----------------------------------------------------------------------------
366
+ // <kperfdata.framework> header (reverse engineered)
367
+ // This framework provides some functions to access the local CPU database.
368
+ // These functions do not require root privileges.
369
+ // -----------------------------------------------------------------------------
370
+
371
+ // KPEP CPU archtecture constants.
372
+ #define KPEP_ARCH_I386 0
373
+ #define KPEP_ARCH_X86_64 1
374
+ #define KPEP_ARCH_ARM 2
375
+ #define KPEP_ARCH_ARM64 3
376
+
377
+ /// KPEP event (size: 48/28 bytes on 64/32 bit OS)
378
+ typedef struct kpep_event
379
+ {
380
+ const char *name; ///< Unique name of a event, such as "INST_RETIRED.ANY".
381
+ const char *description; ///< Description for this event.
382
+ const char *errata; ///< Errata, currently NULL.
383
+ const char *alias; ///< Alias name, such as "Instructions", "Cycles".
384
+ const char *fallback; ///< Fallback event name for fixed counter.
385
+ u32 mask;
386
+ u8 number;
387
+ u8 umask;
388
+ u8 reserved;
389
+ u8 is_fixed;
390
+ } kpep_event;
391
+
392
+ /// KPEP database (size: 144/80 bytes on 64/32 bit OS)
393
+ typedef struct kpep_db
394
+ {
395
+ const char *name; ///< Database name, such as "haswell".
396
+ const char *cpu_id; ///< Plist name, such as "cpu_7_8_10b282dc".
397
+ const char *marketing_name; ///< Marketing name, such as "Intel Haswell".
398
+ void *plist_data; ///< Plist data (CFDataRef), currently NULL.
399
+ void *event_map; ///< All events (CFDict<CFSTR(event_name), kpep_event *>).
400
+ kpep_event
401
+ *event_arr; ///< Event struct buffer (sizeof(kpep_event) * events_count).
402
+ kpep_event **fixed_event_arr; ///< Fixed counter events (sizeof(kpep_event *)
403
+ ///< * fixed_counter_count)
404
+ void *alias_map; ///< All aliases (CFDict<CFSTR(event_name), kpep_event *>).
405
+ usize reserved_1;
406
+ usize reserved_2;
407
+ usize reserved_3;
408
+ usize event_count; ///< All events count.
409
+ usize alias_count;
410
+ usize fixed_counter_count;
411
+ usize config_counter_count;
412
+ usize power_counter_count;
413
+ u32 archtecture; ///< see `KPEP CPU archtecture constants` above.
414
+ u32 fixed_counter_bits;
415
+ u32 config_counter_bits;
416
+ u32 power_counter_bits;
417
+ } kpep_db;
418
+
419
+ /// KPEP config (size: 80/44 bytes on 64/32 bit OS)
420
+ typedef struct kpep_config
421
+ {
422
+ kpep_db *db;
423
+ kpep_event **ev_arr; ///< (sizeof(kpep_event *) * counter_count), init NULL
424
+ usize *ev_map; ///< (sizeof(usize *) * counter_count), init 0
425
+ usize *ev_idx; ///< (sizeof(usize *) * counter_count), init -1
426
+ u32 *flags; ///< (sizeof(u32 *) * counter_count), init 0
427
+ u64 *kpc_periods; ///< (sizeof(u64 *) * counter_count), init 0
428
+ usize event_count; /// kpep_config_events_count()
429
+ usize counter_count;
430
+ u32 classes; ///< See `class mask constants` above.
431
+ u32 config_counter;
432
+ u32 power_counter;
433
+ u32 reserved;
434
+ } kpep_config;
435
+
436
+ /// Error code for kpep_config_xxx() and kpep_db_xxx() functions.
437
+ typedef enum
438
+ {
439
+ KPEP_CONFIG_ERROR_NONE = 0,
440
+ KPEP_CONFIG_ERROR_INVALID_ARGUMENT = 1,
441
+ KPEP_CONFIG_ERROR_OUT_OF_MEMORY = 2,
442
+ KPEP_CONFIG_ERROR_IO = 3,
443
+ KPEP_CONFIG_ERROR_BUFFER_TOO_SMALL = 4,
444
+ KPEP_CONFIG_ERROR_CUR_SYSTEM_UNKNOWN = 5,
445
+ KPEP_CONFIG_ERROR_DB_PATH_INVALID = 6,
446
+ KPEP_CONFIG_ERROR_DB_NOT_FOUND = 7,
447
+ KPEP_CONFIG_ERROR_DB_ARCH_UNSUPPORTED = 8,
448
+ KPEP_CONFIG_ERROR_DB_VERSION_UNSUPPORTED = 9,
449
+ KPEP_CONFIG_ERROR_DB_CORRUPT = 10,
450
+ KPEP_CONFIG_ERROR_EVENT_NOT_FOUND = 11,
451
+ KPEP_CONFIG_ERROR_CONFLICTING_EVENTS = 12,
452
+ KPEP_CONFIG_ERROR_COUNTERS_NOT_FORCED = 13,
453
+ KPEP_CONFIG_ERROR_EVENT_UNAVAILABLE = 14,
454
+ KPEP_CONFIG_ERROR_ERRNO = 15,
455
+ KPEP_CONFIG_ERROR_MAX
456
+ } kpep_config_error_code;
457
+
458
+ /// Error description for kpep_config_error_code.
459
+ static const char *kpep_config_error_names[KPEP_CONFIG_ERROR_MAX] = {
460
+ "none",
461
+ "invalid argument",
462
+ "out of memory",
463
+ "I/O",
464
+ "buffer too small",
465
+ "current system unknown",
466
+ "database path invalid",
467
+ "database not found",
468
+ "database architecture unsupported",
469
+ "database version unsupported",
470
+ "database corrupt",
471
+ "event not found",
472
+ "conflicting events",
473
+ "all counters must be forced",
474
+ "event unavailable",
475
+ "check errno"};
476
+
477
+ /// Error description.
478
+ static const char *kpep_config_error_desc(int code)
479
+ {
480
+ if (0 <= code && code < KPEP_CONFIG_ERROR_MAX)
481
+ {
482
+ return kpep_config_error_names[code];
483
+ }
484
+ return "unknown error";
485
+ }
486
+
487
+ /// Create a config.
488
+ /// @param db A kpep db, see kpep_db_create()
489
+ /// @param cfg_ptr A pointer to receive the new config.
490
+ /// @return kpep_config_error_code, 0 for success.
491
+ static int (*kpep_config_create)(kpep_db *db, kpep_config **cfg_ptr);
492
+
493
+ /// Free the config.
494
+ static void (*kpep_config_free)(kpep_config *cfg);
495
+
496
+ /// Add an event to config.
497
+ /// @param cfg The config.
498
+ /// @param ev_ptr A event pointer.
499
+ /// @param flag 0: all, 1: user space only
500
+ /// @param err Error bitmap pointer, can be NULL.
501
+ /// If return value is `CONFLICTING_EVENTS`, this bitmap contains
502
+ /// the conflicted event indices, e.g. "1 << 2" means index 2.
503
+ /// @return kpep_config_error_code, 0 for success.
504
+ static int (*kpep_config_add_event)(kpep_config *cfg, kpep_event **ev_ptr,
505
+ u32 flag, u32 *err);
506
+
507
+ /// Remove event at index.
508
+ /// @return kpep_config_error_code, 0 for success.
509
+ static int (*kpep_config_remove_event)(kpep_config *cfg, usize idx);
510
+
511
+ /// Force all counters.
512
+ /// @return kpep_config_error_code, 0 for success.
513
+ static int (*kpep_config_force_counters)(kpep_config *cfg);
514
+
515
+ /// Get events count.
516
+ /// @return kpep_config_error_code, 0 for success.
517
+ static int (*kpep_config_events_count)(kpep_config *cfg, usize *count_ptr);
518
+
519
+ /// Get all event pointers.
520
+ /// @param buf A buffer to receive event pointers.
521
+ /// @param buf_size The buffer's size in bytes, should not smaller than
522
+ /// kpep_config_events_count() * sizeof(void *).
523
+ /// @return kpep_config_error_code, 0 for success.
524
+ static int (*kpep_config_events)(kpep_config *cfg, kpep_event **buf,
525
+ usize buf_size);
526
+
527
+ /// Get kpc register configs.
528
+ /// @param buf A buffer to receive kpc register configs.
529
+ /// @param buf_size The buffer's size in bytes, should not smaller than
530
+ /// kpep_config_kpc_count() * sizeof(kpc_config_t).
531
+ /// @return kpep_config_error_code, 0 for success.
532
+ static int (*kpep_config_kpc)(kpep_config *cfg, kpc_config_t *buf,
533
+ usize buf_size);
534
+
535
+ /// Get kpc register config count.
536
+ /// @return kpep_config_error_code, 0 for success.
537
+ static int (*kpep_config_kpc_count)(kpep_config *cfg, usize *count_ptr);
538
+
539
+ /// Get kpc classes.
540
+ /// @param classes See `class mask constants` above.
541
+ /// @return kpep_config_error_code, 0 for success.
542
+ static int (*kpep_config_kpc_classes)(kpep_config *cfg, u32 *classes_ptr);
543
+
544
+ /// Get the index mapping from event to counter.
545
+ /// @param buf A buffer to receive indexes.
546
+ /// @param buf_size The buffer's size in bytes, should not smaller than
547
+ /// kpep_config_events_count() * sizeof(kpc_config_t).
548
+ /// @return kpep_config_error_code, 0 for success.
549
+ static int (*kpep_config_kpc_map)(kpep_config *cfg, usize *buf, usize buf_size);
550
+
551
+ /// Open a kpep database file in "/usr/share/kpep/" or "/usr/local/share/kpep/".
552
+ /// @param name File name, for example "haswell", "cpu_100000c_1_92fb37c8".
553
+ /// Pass NULL for current CPU.
554
+ /// @return kpep_config_error_code, 0 for success.
555
+ static int (*kpep_db_create)(const char *name, kpep_db **db_ptr);
556
+
557
+ /// Free the kpep database.
558
+ static void (*kpep_db_free)(kpep_db *db);
559
+
560
+ /// Get the database's name.
561
+ /// @return kpep_config_error_code, 0 for success.
562
+ static int (*kpep_db_name)(kpep_db *db, const char **name);
563
+
564
+ /// Get the event alias count.
565
+ /// @return kpep_config_error_code, 0 for success.
566
+ static int (*kpep_db_aliases_count)(kpep_db *db, usize *count);
567
+
568
+ /// Get all alias.
569
+ /// @param buf A buffer to receive all alias strings.
570
+ /// @param buf_size The buffer's size in bytes,
571
+ /// should not smaller than kpep_db_aliases_count() * sizeof(void *).
572
+ /// @return kpep_config_error_code, 0 for success.
573
+ static int (*kpep_db_aliases)(kpep_db *db, const char **buf, usize buf_size);
574
+
575
+ /// Get counters count for given classes.
576
+ /// @param classes 1: Fixed, 2: Configurable.
577
+ /// @return kpep_config_error_code, 0 for success.
578
+ static int (*kpep_db_counters_count)(kpep_db *db, u8 classes, usize *count);
579
+
580
+ /// Get all event count.
581
+ /// @return kpep_config_error_code, 0 for success.
582
+ static int (*kpep_db_events_count)(kpep_db *db, usize *count);
583
+
584
+ /// Get all events.
585
+ /// @param buf A buffer to receive all event pointers.
586
+ /// @param buf_size The buffer's size in bytes,
587
+ /// should not smaller than kpep_db_events_count() * sizeof(void *).
588
+ /// @return kpep_config_error_code, 0 for success.
589
+ static int (*kpep_db_events)(kpep_db *db, kpep_event **buf, usize buf_size);
590
+
591
+ /// Get one event by name.
592
+ /// @return kpep_config_error_code, 0 for success.
593
+ static int (*kpep_db_event)(kpep_db *db, const char *name, kpep_event **ev_ptr);
594
+
595
+ /// Get event's name.
596
+ /// @return kpep_config_error_code, 0 for success.
597
+ static int (*kpep_event_name)(kpep_event *ev, const char **name_ptr);
598
+
599
+ /// Get event's alias.
600
+ /// @return kpep_config_error_code, 0 for success.
601
+ static int (*kpep_event_alias)(kpep_event *ev, const char **alias_ptr);
602
+
603
+ /// Get event's description.
604
+ /// @return kpep_config_error_code, 0 for success.
605
+ static int (*kpep_event_description)(kpep_event *ev, const char **str_ptr);
606
+
607
+ // -----------------------------------------------------------------------------
608
+ // load kperf/kperfdata dynamic library
609
+ // -----------------------------------------------------------------------------
610
+
611
+ typedef struct
612
+ {
613
+ const char *name;
614
+ void **impl;
615
+ } lib_symbol;
616
+
617
+ #define lib_nelems(x) (sizeof(x) / sizeof((x)[0]))
618
+ #define lib_symbol_def(name) \
619
+ { \
620
+ #name, (void **)&name \
621
+ }
622
+
623
+ static const lib_symbol lib_symbols_kperf[] = {
624
+ lib_symbol_def(kpc_pmu_version),
625
+ lib_symbol_def(kpc_cpu_string),
626
+ lib_symbol_def(kpc_set_counting),
627
+ lib_symbol_def(kpc_get_counting),
628
+ lib_symbol_def(kpc_set_thread_counting),
629
+ lib_symbol_def(kpc_get_thread_counting),
630
+ lib_symbol_def(kpc_get_config_count),
631
+ lib_symbol_def(kpc_get_counter_count),
632
+ lib_symbol_def(kpc_set_config),
633
+ lib_symbol_def(kpc_get_config),
634
+ lib_symbol_def(kpc_get_cpu_counters),
635
+ lib_symbol_def(kpc_get_thread_counters),
636
+ lib_symbol_def(kpc_force_all_ctrs_set),
637
+ lib_symbol_def(kpc_force_all_ctrs_get),
638
+ lib_symbol_def(kperf_action_count_set),
639
+ lib_symbol_def(kperf_action_count_get),
640
+ lib_symbol_def(kperf_action_samplers_set),
641
+ lib_symbol_def(kperf_action_samplers_get),
642
+ lib_symbol_def(kperf_action_filter_set_by_task),
643
+ lib_symbol_def(kperf_action_filter_set_by_pid),
644
+ lib_symbol_def(kperf_timer_count_set),
645
+ lib_symbol_def(kperf_timer_count_get),
646
+ lib_symbol_def(kperf_timer_period_set),
647
+ lib_symbol_def(kperf_timer_period_get),
648
+ lib_symbol_def(kperf_timer_action_set),
649
+ lib_symbol_def(kperf_timer_action_get),
650
+ lib_symbol_def(kperf_sample_set),
651
+ lib_symbol_def(kperf_sample_get),
652
+ lib_symbol_def(kperf_reset),
653
+ lib_symbol_def(kperf_timer_pet_set),
654
+ lib_symbol_def(kperf_timer_pet_get),
655
+ lib_symbol_def(kperf_ns_to_ticks),
656
+ lib_symbol_def(kperf_ticks_to_ns),
657
+ lib_symbol_def(kperf_tick_frequency),
658
+ };
659
+
660
+ static const lib_symbol lib_symbols_kperfdata[] = {
661
+ lib_symbol_def(kpep_config_create),
662
+ lib_symbol_def(kpep_config_free),
663
+ lib_symbol_def(kpep_config_add_event),
664
+ lib_symbol_def(kpep_config_remove_event),
665
+ lib_symbol_def(kpep_config_force_counters),
666
+ lib_symbol_def(kpep_config_events_count),
667
+ lib_symbol_def(kpep_config_events),
668
+ lib_symbol_def(kpep_config_kpc),
669
+ lib_symbol_def(kpep_config_kpc_count),
670
+ lib_symbol_def(kpep_config_kpc_classes),
671
+ lib_symbol_def(kpep_config_kpc_map),
672
+ lib_symbol_def(kpep_db_create),
673
+ lib_symbol_def(kpep_db_free),
674
+ lib_symbol_def(kpep_db_name),
675
+ lib_symbol_def(kpep_db_aliases_count),
676
+ lib_symbol_def(kpep_db_aliases),
677
+ lib_symbol_def(kpep_db_counters_count),
678
+ lib_symbol_def(kpep_db_events_count),
679
+ lib_symbol_def(kpep_db_events),
680
+ lib_symbol_def(kpep_db_event),
681
+ lib_symbol_def(kpep_event_name),
682
+ lib_symbol_def(kpep_event_alias),
683
+ lib_symbol_def(kpep_event_description),
684
+ };
685
+
686
+ #define lib_path_kperf "/System/Library/PrivateFrameworks/kperf.framework/kperf"
687
+ #define lib_path_kperfdata \
688
+ "/System/Library/PrivateFrameworks/kperfdata.framework/kperfdata"
689
+
690
+ static bool lib_inited = false;
691
+ static bool lib_has_err = false;
692
+ static char lib_err_msg[256];
693
+
694
+ static void *lib_handle_kperf = NULL;
695
+ static void *lib_handle_kperfdata = NULL;
696
+
697
+ static void lib_deinit(void)
698
+ {
699
+ lib_inited = false;
700
+ lib_has_err = false;
701
+ if (lib_handle_kperf)
702
+ dlclose(lib_handle_kperf);
703
+ if (lib_handle_kperfdata)
704
+ dlclose(lib_handle_kperfdata);
705
+ lib_handle_kperf = NULL;
706
+ lib_handle_kperfdata = NULL;
707
+ for (usize i = 0; i < lib_nelems(lib_symbols_kperf); i++)
708
+ {
709
+ const lib_symbol *symbol = &lib_symbols_kperf[i];
710
+ *symbol->impl = NULL;
711
+ }
712
+ for (usize i = 0; i < lib_nelems(lib_symbols_kperfdata); i++)
713
+ {
714
+ const lib_symbol *symbol = &lib_symbols_kperfdata[i];
715
+ *symbol->impl = NULL;
716
+ }
717
+ }
718
+
719
+ static bool lib_init(void)
720
+ {
721
+ #define return_err() \
722
+ do \
723
+ { \
724
+ lib_deinit(); \
725
+ lib_inited = true; \
726
+ lib_has_err = true; \
727
+ return false; \
728
+ } while (false)
729
+
730
+ if (lib_inited)
731
+ return !lib_has_err;
732
+
733
+ // load dynamic library
734
+ lib_handle_kperf = dlopen(lib_path_kperf, RTLD_LAZY);
735
+ if (!lib_handle_kperf)
736
+ {
737
+ snprintf(lib_err_msg, sizeof(lib_err_msg),
738
+ "Failed to load kperf.framework, message: %s.", dlerror());
739
+ return_err();
740
+ }
741
+ lib_handle_kperfdata = dlopen(lib_path_kperfdata, RTLD_LAZY);
742
+ if (!lib_handle_kperfdata)
743
+ {
744
+ snprintf(lib_err_msg, sizeof(lib_err_msg),
745
+ "Failed to load kperfdata.framework, message: %s.", dlerror());
746
+ return_err();
747
+ }
748
+
749
+ // load symbol address from dynamic library
750
+ for (usize i = 0; i < lib_nelems(lib_symbols_kperf); i++)
751
+ {
752
+ const lib_symbol *symbol = &lib_symbols_kperf[i];
753
+ *symbol->impl = dlsym(lib_handle_kperf, symbol->name);
754
+ if (!*symbol->impl)
755
+ {
756
+ snprintf(lib_err_msg, sizeof(lib_err_msg),
757
+ "Failed to load kperf function: %s.", symbol->name);
758
+ return_err();
759
+ }
760
+ }
761
+ for (usize i = 0; i < lib_nelems(lib_symbols_kperfdata); i++)
762
+ {
763
+ const lib_symbol *symbol = &lib_symbols_kperfdata[i];
764
+ *symbol->impl = dlsym(lib_handle_kperfdata, symbol->name);
765
+ if (!*symbol->impl)
766
+ {
767
+ snprintf(lib_err_msg, sizeof(lib_err_msg),
768
+ "Failed to load kperfdata function: %s.", symbol->name);
769
+ return_err();
770
+ }
771
+ }
772
+
773
+ lib_inited = true;
774
+ lib_has_err = false;
775
+ return true;
776
+
777
+ #undef return_err
778
+ }
779
+
780
+ // -----------------------------------------------------------------------------
781
+ // kdebug private structs
782
+ // https://github.com/apple/darwin-xnu/blob/main/bsd/sys_private/kdebug_private.h
783
+ // -----------------------------------------------------------------------------
784
+
785
+ /*
786
+ * Ensure that both LP32 and LP64 variants of arm64 use the same kd_buf
787
+ * structure.
788
+ */
789
+ #if defined(__arm64__)
790
+ typedef uint64_t kd_buf_argtype;
791
+ #else
792
+ typedef uintptr_t kd_buf_argtype;
793
+ #endif
794
+
795
+ typedef struct
796
+ {
797
+ uint64_t timestamp;
798
+ kd_buf_argtype arg1;
799
+ kd_buf_argtype arg2;
800
+ kd_buf_argtype arg3;
801
+ kd_buf_argtype arg4;
802
+ kd_buf_argtype arg5; /* the thread ID */
803
+ uint32_t debugid; /* see <sys/kdebug.h> */
804
+
805
+ /*
806
+ * Ensure that both LP32 and LP64 variants of arm64 use the same kd_buf
807
+ * structure.
808
+ */
809
+ #if defined(__LP64__) || defined(__arm64__)
810
+ uint32_t cpuid; /* cpu index, from 0 */
811
+ kd_buf_argtype unused;
812
+ #endif
813
+ } kd_buf;
814
+
815
+ /* bits for the type field of kd_regtype */
816
+ #define KDBG_CLASSTYPE 0x10000
817
+ #define KDBG_SUBCLSTYPE 0x20000
818
+ #define KDBG_RANGETYPE 0x40000
819
+ #define KDBG_TYPENONE 0x80000
820
+ #define KDBG_CKTYPES 0xF0000
821
+
822
+ /* only trace at most 4 types of events, at the code granularity */
823
+ #define KDBG_VALCHECK 0x00200000U
824
+
825
+ typedef struct
826
+ {
827
+ unsigned int type;
828
+ unsigned int value1;
829
+ unsigned int value2;
830
+ unsigned int value3;
831
+ unsigned int value4;
832
+ } kd_regtype;
833
+
834
+ typedef struct
835
+ {
836
+ /* number of events that can fit in the buffers */
837
+ int nkdbufs;
838
+ /* set if trace is disabled */
839
+ int nolog;
840
+ /* kd_ctrl_page.flags */
841
+ unsigned int flags;
842
+ /* number of threads in thread map */
843
+ int nkdthreads;
844
+ /* the owning pid */
845
+ int bufid;
846
+ } kbufinfo_t;
847
+
848
+ // -----------------------------------------------------------------------------
849
+ // kdebug utils
850
+ // -----------------------------------------------------------------------------
851
+
852
+ #define EVENT_NAME_MAX 8
853
+ typedef struct
854
+ {
855
+ const char *alias; /// name for print
856
+ const char *names[EVENT_NAME_MAX]; /// name from pmc db
857
+ } event_alias;
858
+
859
+ /// Event names from /usr/share/kpep/<name>.plist
860
+ static const event_alias profile_events[] = {
861
+ {"cycles",
862
+ {
863
+ "FIXED_CYCLES", // Apple A7-A15
864
+ "CPU_CLK_UNHALTED.THREAD", // Intel Core 1th-10th
865
+ "CPU_CLK_UNHALTED.CORE", // Intel Yonah, Merom
866
+ }},
867
+ {"instructions",
868
+ {
869
+ "FIXED_INSTRUCTIONS", // Apple A7-A15
870
+ "INST_RETIRED.ANY" // Intel Yonah, Merom, Core 1th-10th
871
+ }},
872
+ {"branches",
873
+ {
874
+ "INST_BRANCH", // Apple A7-A15
875
+ "BR_INST_RETIRED.ALL_BRANCHES", // Intel Core 1th-10th
876
+ "INST_RETIRED.ANY", // Intel Yonah, Merom
877
+ }},
878
+ {"branch-misses",
879
+ {
880
+ "BRANCH_MISPRED_NONSPEC", // Apple A7-A15, since iOS 15, macOS 12
881
+ "BRANCH_MISPREDICT", // Apple A7-A14
882
+ "BR_MISP_RETIRED.ALL_BRANCHES", // Intel Core 2th-10th
883
+ "BR_INST_RETIRED.MISPRED", // Intel Yonah, Merom
884
+ }},
885
+ };
886
+
887
+ static kpep_event *get_event(kpep_db *db, const event_alias *alias)
888
+ {
889
+ for (usize j = 0; j < EVENT_NAME_MAX; j++)
890
+ {
891
+ const char *name = alias->names[j];
892
+ if (!name)
893
+ break;
894
+ kpep_event *ev = NULL;
895
+ if (kpep_db_event(db, name, &ev) == 0)
896
+ {
897
+ return ev;
898
+ }
899
+ }
900
+ return NULL;
901
+ }
902
+ #endif