Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
apple_arm_events.h
Go to the documentation of this file.
1
2// Original design from:
3// =============================================================================
4// XNU kperf/kpc
5// Available for 64-bit Intel/Apple Silicon, macOS/iOS, with root privileges
6//
7// References:
8//
9// XNU source (since xnu 2422.1.72):
10// https://github.com/apple/darwin-xnu/blob/main/osfmk/kern/kpc.h
11// https://github.com/apple/darwin-xnu/blob/main/bsd/kern/kern_kpc.c
12//
13// Lightweight PET (Profile Every Thread, since xnu 3789.1.32):
14// https://github.com/apple/darwin-xnu/blob/main/osfmk/kperf/pet.c
15// https://github.com/apple/darwin-xnu/blob/main/osfmk/kperf/kperf_kpc.c
16//
17// System Private frameworks (since macOS 10.11, iOS 8.0):
18// /System/Library/PrivateFrameworks/kperf.framework
19// /System/Library/PrivateFrameworks/kperfdata.framework
20//
21// Xcode framework (since Xcode 7.0):
22// /Applications/Xcode.app/Contents/SharedFrameworks/DVTInstrumentsFoundation.framework
23//
24// CPU database (plist files)
25// macOS (since macOS 10.11):
26// /usr/share/kpep/<name>.plist
27// iOS (copied from Xcode, since iOS 10.0, Xcode 8.0):
28// /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform
29// /DeviceSupport/<version>/DeveloperDiskImage.dmg/usr/share/kpep/<name>.plist
30//
31//
32// Created by YaoYuan <ibireme@gmail.com> on 2021.
33// Released into the public domain (unlicense.org).
34// =============================================================================
35
36#ifndef M1CYCLES_H
37#define M1CYCLES_H
38
39#include <dlfcn.h> // for dlopen() and dlsym()
40#include <mach/mach_time.h> // for mach_absolute_time()
41#include <stdbool.h>
42#include <stdint.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <sys/kdebug.h> // for kdebug trace decode
47#include <sys/sysctl.h> // for sysctl()
48#include <unistd.h> // for usleep()
49
51 double cycles;
52 double branches;
55 performance_counters(uint64_t c, uint64_t b, uint64_t m, uint64_t i)
57 performance_counters(double c, double b, double m, double i)
60 : cycles(init),
61 branches(init),
62 missed_branches(init),
63 instructions(init) {}
64
66 cycles -= other.cycles;
67 branches -= other.branches;
70 return *this;
71 }
73 cycles = other.cycles < cycles ? other.cycles : cycles;
74 branches = other.branches < branches ? other.branches : branches;
76 ? other.missed_branches
80 return *this;
81 }
83 cycles += other.cycles;
84 branches += other.branches;
87 return *this;
88 }
89
90 inline performance_counters &operator/=(double numerator) {
91 cycles /= numerator;
92 branches /= numerator;
93 missed_branches /= numerator;
94 instructions /= numerator;
95 return *this;
96 }
97};
98
105
106typedef float f32;
107typedef double f64;
108typedef int8_t i8;
109typedef uint8_t u8;
110typedef int16_t i16;
111typedef uint16_t u16;
112typedef int32_t i32;
113typedef uint32_t u32;
114typedef int64_t i64;
115typedef uint64_t u64;
116typedef size_t usize;
117
118// -----------------------------------------------------------------------------
119// <kperf.framework> header (reverse engineered)
120// This framework wraps some sysctl calls to communicate with the kpc in kernel.
121// Most functions requires root privileges, or process is "blessed".
122// -----------------------------------------------------------------------------
123
124// Cross-platform class constants.
125#define KPC_CLASS_FIXED (0)
126#define KPC_CLASS_CONFIGURABLE (1)
127#define KPC_CLASS_POWER (2)
128#define KPC_CLASS_RAWPMU (3)
129
130// Cross-platform class mask constants.
131#define KPC_CLASS_FIXED_MASK (1u << KPC_CLASS_FIXED) // 1
132#define KPC_CLASS_CONFIGURABLE_MASK (1u << KPC_CLASS_CONFIGURABLE) // 2
133#define KPC_CLASS_POWER_MASK (1u << KPC_CLASS_POWER) // 4
134#define KPC_CLASS_RAWPMU_MASK (1u << KPC_CLASS_RAWPMU) // 8
135
136// PMU version constants.
137#define KPC_PMU_ERROR (0) // Error
138#define KPC_PMU_INTEL_V3 (1) // Intel
139#define KPC_PMU_ARM_APPLE (2) // ARM64
140#define KPC_PMU_INTEL_V2 (3) // Old Intel
141#define KPC_PMU_ARM_V2 (4) // Old ARM
142
143// The maximum number of counters we could read from every class in one go.
144// ARMV7: FIXED: 1, CONFIGURABLE: 4
145// ARM32: FIXED: 2, CONFIGURABLE: 6
146// ARM64: FIXED: 2, CONFIGURABLE: CORE_NCTRS - FIXED (6 or 8)
147// x86: 32
148#define KPC_MAX_COUNTERS 32
149
150// Bits for defining what to do on an action.
151// Defined in https://github.com/apple/darwin-xnu/blob/main/osfmk/kperf/action.h
152#define KPERF_SAMPLER_TH_INFO (1U << 0)
153#define KPERF_SAMPLER_TH_SNAPSHOT (1U << 1)
154#define KPERF_SAMPLER_KSTACK (1U << 2)
155#define KPERF_SAMPLER_USTACK (1U << 3)
156#define KPERF_SAMPLER_PMC_THREAD (1U << 4)
157#define KPERF_SAMPLER_PMC_CPU (1U << 5)
158#define KPERF_SAMPLER_PMC_CONFIG (1U << 6)
159#define KPERF_SAMPLER_MEMINFO (1U << 7)
160#define KPERF_SAMPLER_TH_SCHEDULING (1U << 8)
161#define KPERF_SAMPLER_TH_DISPATCH (1U << 9)
162#define KPERF_SAMPLER_TK_SNAPSHOT (1U << 10)
163#define KPERF_SAMPLER_SYS_MEM (1U << 11)
164#define KPERF_SAMPLER_TH_INSCYC (1U << 12)
165#define KPERF_SAMPLER_TK_INFO (1U << 13)
166
167// Maximum number of kperf action ids.
168#define KPERF_ACTION_MAX (32)
169
170// Maximum number of kperf timer ids.
171#define KPERF_TIMER_MAX (8)
172
173// x86/arm config registers are 64-bit
175
183static int (*kpc_cpu_string)(char *buf, usize buf_size);
184
188static u32 (*kpc_pmu_version)(void);
189
194static u32 (*kpc_get_counting)(void);
195
200static int (*kpc_set_counting)(u32 classes);
201
207
212static int (*kpc_set_thread_counting)(u32 classes);
213
221static u32 (*kpc_get_config_count)(u32 classes);
222
229static int (*kpc_get_config)(u32 classes, kpc_config_t *config);
230
237static int (*kpc_set_config)(u32 classes, kpc_config_t *config);
238
245static u32 (*kpc_get_counter_count)(u32 classes);
246
258static int (*kpc_get_cpu_counters)(bool all_cpus, u32 classes, int *curcpu,
259 u64 *buf);
260
268static int (*kpc_get_thread_counters)(u32 tid, u32 buf_count, u64 *buf);
269
274static int (*kpc_force_all_ctrs_set)(int val);
275
279static int (*kpc_force_all_ctrs_get)(int *val_out);
280
283static int (*kperf_action_count_set)(u32 count);
284
287static int (*kperf_action_count_get)(u32 *count);
288
292static int (*kperf_action_samplers_set)(u32 actionid, u32 sample);
293
296static int (*kperf_action_samplers_get)(u32 actionid, u32 *sample);
297
300static int (*kperf_action_filter_set_by_task)(u32 actionid, i32 port);
301
304static int (*kperf_action_filter_set_by_pid)(u32 actionid, i32 pid);
305
308static int (*kperf_timer_count_set)(u32 count);
309
312static int (*kperf_timer_count_get)(u32 *count);
313
316static int (*kperf_timer_period_set)(u32 actionid, u64 tick);
317
320static int (*kperf_timer_period_get)(u32 actionid, u64 *tick);
321
324static int (*kperf_timer_action_set)(u32 actionid, u32 timerid);
325
328static int (*kperf_timer_action_get)(u32 actionid, u32 *timerid);
329
332static int (*kperf_timer_pet_set)(u32 timerid);
333
336static int (*kperf_timer_pet_get)(u32 *timerid);
337
340static int (*kperf_sample_set)(u32 enabled);
341
344static int (*kperf_sample_get)(u32 *enabled);
345
348static int (*kperf_reset)(void);
349
352
354static u64 (*kperf_ticks_to_ns)(u64 ticks);
355
357static u64 (*kperf_tick_frequency)(void);
358
360static int kperf_lightweight_pet_get(u32 *enabled) {
361 if (!enabled) return -1;
362 usize size = 4;
363 return sysctlbyname("kperf.lightweight_pet", enabled, &size, NULL, 0);
364}
365
367static int kperf_lightweight_pet_set(u32 enabled) {
368 return sysctlbyname("kperf.lightweight_pet", NULL, NULL, &enabled, 4);
369}
370
371// -----------------------------------------------------------------------------
372// <kperfdata.framework> header (reverse engineered)
373// This framework provides some functions to access the local CPU database.
374// These functions do not require root privileges.
375// -----------------------------------------------------------------------------
376
377// KPEP CPU archtecture constants.
378#define KPEP_ARCH_I386 0
379#define KPEP_ARCH_X86_64 1
380#define KPEP_ARCH_ARM 2
381#define KPEP_ARCH_ARM64 3
382
384typedef struct kpep_event {
385 const char *name;
386 const char *description;
387 const char *errata;
388 const char *alias;
389 const char *fallback;
396
422
438
459
462 "none",
463 "invalid argument",
464 "out of memory",
465 "I/O",
466 "buffer too small",
467 "current system unknown",
468 "database path invalid",
469 "database not found",
470 "database architecture unsupported",
471 "database version unsupported",
472 "database corrupt",
473 "event not found",
474 "conflicting events",
475 "all counters must be forced",
476 "event unavailable",
477 "check errno"};
478
480static const char *kpep_config_error_desc(int code) {
481 if (0 <= code && code < KPEP_CONFIG_ERROR_MAX) {
482 return kpep_config_error_names[code];
483 }
484 return "unknown error";
485}
486
491static int (*kpep_config_create)(kpep_db *db, kpep_config **cfg_ptr);
492
494static void (*kpep_config_free)(kpep_config *cfg);
495
504static int (*kpep_config_add_event)(kpep_config *cfg, kpep_event **ev_ptr,
505 u32 flag, u32 *err);
506
510
514
517static int (*kpep_config_events_count)(kpep_config *cfg, usize *count_ptr);
518
524static int (*kpep_config_events)(kpep_config *cfg, kpep_event **buf,
525 usize buf_size);
526
532static int (*kpep_config_kpc)(kpep_config *cfg, kpc_config_t *buf,
533 usize buf_size);
534
537static int (*kpep_config_kpc_count)(kpep_config *cfg, usize *count_ptr);
538
542static int (*kpep_config_kpc_classes)(kpep_config *cfg, u32 *classes_ptr);
543
549static int (*kpep_config_kpc_map)(kpep_config *cfg, usize *buf, usize buf_size);
550
555static int (*kpep_db_create)(const char *name, kpep_db **db_ptr);
556
558static void (*kpep_db_free)(kpep_db *db);
559
562static int (*kpep_db_name)(kpep_db *db, const char **name);
563
566static int (*kpep_db_aliases_count)(kpep_db *db, usize *count);
567
573static int (*kpep_db_aliases)(kpep_db *db, const char **buf, usize buf_size);
574
578static int (*kpep_db_counters_count)(kpep_db *db, u8 classes, usize *count);
579
582static int (*kpep_db_events_count)(kpep_db *db, usize *count);
583
589static int (*kpep_db_events)(kpep_db *db, kpep_event **buf, usize buf_size);
590
593static int (*kpep_db_event)(kpep_db *db, const char *name, kpep_event **ev_ptr);
594
597static int (*kpep_event_name)(kpep_event *ev, const char **name_ptr);
598
601static int (*kpep_event_alias)(kpep_event *ev, const char **alias_ptr);
602
605static int (*kpep_event_description)(kpep_event *ev, const char **str_ptr);
606
607// -----------------------------------------------------------------------------
608// load kperf/kperfdata dynamic library
609// -----------------------------------------------------------------------------
610
611typedef struct {
612 const char *name;
613 void **impl;
614} lib_symbol;
615
616#define lib_nelems(x) (sizeof(x) / sizeof((x)[0]))
617#define lib_symbol_def(name) \
618 { #name, (void **)&name }
619
656
682
683#define lib_path_kperf "/System/Library/PrivateFrameworks/kperf.framework/kperf"
684#define lib_path_kperfdata \
685 "/System/Library/PrivateFrameworks/kperfdata.framework/kperfdata"
686
687static bool lib_inited = false;
688static bool lib_has_err = false;
689static char lib_err_msg[256];
690
691static void *lib_handle_kperf = NULL;
692static void *lib_handle_kperfdata = NULL;
693
694static void lib_deinit(void) {
695 lib_inited = false;
696 lib_has_err = false;
699 lib_handle_kperf = NULL;
701 for (usize i = 0; i < lib_nelems(lib_symbols_kperf); i++) {
702 const lib_symbol *symbol = &lib_symbols_kperf[i];
703 *symbol->impl = NULL;
704 }
705 for (usize i = 0; i < lib_nelems(lib_symbols_kperfdata); i++) {
706 const lib_symbol *symbol = &lib_symbols_kperfdata[i];
707 *symbol->impl = NULL;
708 }
709}
710
711static bool lib_init(void) {
712#define return_err() \
713 do { \
714 lib_deinit(); \
715 lib_inited = true; \
716 lib_has_err = true; \
717 return false; \
718 } while (false)
719
720 if (lib_inited) return !lib_has_err;
721
722 // load dynamic library
723 lib_handle_kperf = dlopen(lib_path_kperf, RTLD_LAZY);
724 if (!lib_handle_kperf) {
725 snprintf(lib_err_msg, sizeof(lib_err_msg),
726 "Failed to load kperf.framework, message: %s.", dlerror());
727 return_err();
728 }
729 lib_handle_kperfdata = dlopen(lib_path_kperfdata, RTLD_LAZY);
731 snprintf(lib_err_msg, sizeof(lib_err_msg),
732 "Failed to load kperfdata.framework, message: %s.", dlerror());
733 return_err();
734 }
735
736 // load symbol address from dynamic library
737 for (usize i = 0; i < lib_nelems(lib_symbols_kperf); i++) {
738 const lib_symbol *symbol = &lib_symbols_kperf[i];
739 *symbol->impl = dlsym(lib_handle_kperf, symbol->name);
740 if (!*symbol->impl) {
741 snprintf(lib_err_msg, sizeof(lib_err_msg),
742 "Failed to load kperf function: %s.", symbol->name);
743 return_err();
744 }
745 }
746 for (usize i = 0; i < lib_nelems(lib_symbols_kperfdata); i++) {
747 const lib_symbol *symbol = &lib_symbols_kperfdata[i];
748 *symbol->impl = dlsym(lib_handle_kperfdata, symbol->name);
749 if (!*symbol->impl) {
750 snprintf(lib_err_msg, sizeof(lib_err_msg),
751 "Failed to load kperfdata function: %s.", symbol->name);
752 return_err();
753 }
754 }
755
756 lib_inited = true;
757 lib_has_err = false;
758 return true;
759
760#undef return_err
761}
762
763// -----------------------------------------------------------------------------
764// kdebug private structs
765// https://github.com/apple/darwin-xnu/blob/main/bsd/sys_private/kdebug_private.h
766// -----------------------------------------------------------------------------
767
768/*
769 * Ensure that both LP32 and LP64 variants of arm64 use the same kd_buf
770 * structure.
771 */
772#if defined(__arm64__)
773typedef uint64_t kd_buf_argtype;
774#else
775typedef uintptr_t kd_buf_argtype;
776#endif
777
778typedef struct {
779 uint64_t timestamp;
784 kd_buf_argtype arg5; /* the thread ID */
785 uint32_t debugid; /* see <sys/kdebug.h> */
786
787/*
788 * Ensure that both LP32 and LP64 variants of arm64 use the same kd_buf
789 * structure.
790 */
791#if defined(__LP64__) || defined(__arm64__)
792 uint32_t cpuid; /* cpu index, from 0 */
793 kd_buf_argtype unused;
794#endif
795} kd_buf;
796
797/* bits for the type field of kd_regtype */
798#define KDBG_CLASSTYPE 0x10000
799#define KDBG_SUBCLSTYPE 0x20000
800#define KDBG_RANGETYPE 0x40000
801#define KDBG_TYPENONE 0x80000
802#define KDBG_CKTYPES 0xF0000
803
804/* only trace at most 4 types of events, at the code granularity */
805#define KDBG_VALCHECK 0x00200000U
806
807typedef struct {
808 unsigned int type;
809 unsigned int value1;
810 unsigned int value2;
811 unsigned int value3;
812 unsigned int value4;
813} kd_regtype;
814
815typedef struct {
816 /* number of events that can fit in the buffers */
818 /* set if trace is disabled */
819 int nolog;
820 /* kd_ctrl_page.flags */
821 unsigned int flags;
822 /* number of threads in thread map */
824 /* the owning pid */
825 int bufid;
826} kbufinfo_t;
827
828// -----------------------------------------------------------------------------
829// kdebug utils
830// -----------------------------------------------------------------------------
831
834static int kdebug_reset(void) {
835 int mib[3] = {CTL_KERN, KERN_KDEBUG, KERN_KDREMOVE};
836 return sysctl(mib, 3, NULL, NULL, NULL, 0);
837}
838
841static int kdebug_reinit(void) {
842 int mib[3] = {CTL_KERN, KERN_KDEBUG, KERN_KDSETUP};
843 return sysctl(mib, 3, NULL, NULL, NULL, 0);
844}
845
847static int kdebug_setreg(kd_regtype *kdr) {
848 int mib[3] = {CTL_KERN, KERN_KDEBUG, KERN_KDSETREG};
849 usize size = sizeof(kd_regtype);
850 return sysctl(mib, 3, kdr, &size, NULL, 0);
851}
852
856static int kdebug_trace_setbuf(int nbufs) {
857 int mib[4] = {CTL_KERN, KERN_KDEBUG, KERN_KDSETBUF, nbufs};
858 return sysctl(mib, 4, NULL, NULL, NULL, 0);
859}
860
864static int kdebug_trace_enable(bool enable) {
865 int mib[4] = {CTL_KERN, KERN_KDEBUG, KERN_KDENABLE, enable};
866 return sysctl(mib, 4, NULL, 0, NULL, 0);
867}
868
872 if (!info) return -1;
873 int mib[3] = {CTL_KERN, KERN_KDEBUG, KERN_KDGETBUF};
874 size_t needed = sizeof(kbufinfo_t);
875 return sysctl(mib, 3, info, &needed, NULL, 0);
876}
877
883static int kdebug_trace_read(void *buf, usize len, usize *count) {
884 if (count) *count = 0;
885 if (!buf || !len) return -1;
886
887 // Note: the input and output units are not the same.
888 // input: bytes
889 // output: number of kd_buf
890 int mib[3] = {CTL_KERN, KERN_KDEBUG, KERN_KDREADTR};
891 int ret = sysctl(mib, 3, buf, &len, NULL, 0);
892 if (ret != 0) return ret;
893 *count = len;
894 return 0;
895}
896
901static int kdebug_wait(usize timeout_ms, bool *suc) {
902 if (timeout_ms == 0) return -1;
903 int mib[3] = {CTL_KERN, KERN_KDEBUG, KERN_KDBUFWAIT};
904 usize val = timeout_ms;
905 int ret = sysctl(mib, 3, NULL, &val, NULL, 0);
906 if (suc) *suc = !!val;
907 return ret;
908}
909
910// -----------------------------------------------------------------------------
911// Demo
912// -----------------------------------------------------------------------------
913
914#define EVENT_NAME_MAX 8
915typedef struct {
916 const char *alias;
917 const char *names[EVENT_NAME_MAX];
919
921static const event_alias profile_events[] = {
922 {"cycles",
923 {
924 "FIXED_CYCLES", // Apple
925 // A7-A15
926 "CPU_CLK_UNHALTED.THREAD", // Intel Core 1th-10th
927 "CPU_CLK_UNHALTED.CORE", // Intel Yonah, Merom
928 }},
929 {"instructions",
930 {
931 "FIXED_INSTRUCTIONS", // Apple A7-A15
932 "INST_RETIRED.ANY" // Intel Yonah, Merom, Core 1th-10th
933 }},
934 {"branches",
935 {
936 "INST_BRANCH", // Apple A7-A15
937 "BR_INST_RETIRED.ALL_BRANCHES", // Intel Core 1th-10th
938 "INST_RETIRED.ANY", // Intel Yonah, Merom
939 }},
940 {"branch-misses",
941 {
942 "BRANCH_MISPRED_NONSPEC", // Apple A7-A15, since iOS 15, macOS 12
943 "BRANCH_MISPREDICT", // Apple A7-A14
944 "BR_MISP_RETIRED.ALL_BRANCHES", // Intel Core 2th-10th
945 "BR_INST_RETIRED.MISPRED", // Intel Yonah, Merom
946 }},
947};
948
949static kpep_event *get_event(kpep_db *db, const event_alias *alias) {
950 for (usize j = 0; j < EVENT_NAME_MAX; j++) {
951 const char *name = alias->names[j];
952 if (!name) break;
953 kpep_event *ev = NULL;
954 if (kpep_db_event(db, name, &ev) == 0) {
955 return ev;
956 }
957 }
958 return NULL;
959}
960
965const usize ev_count = sizeof(profile_events) / sizeof(profile_events[0]);
966
968 static bool init = false;
969 static bool worked = false;
970
971 if (init) {
972 return worked;
973 }
974 init = true;
975
976 // load dylib
977 if (!lib_init()) {
978 printf("Error: %s\n", lib_err_msg);
979 return (worked = false);
980 }
981
982 // check permission
983 int force_ctrs = 0;
984 if (kpc_force_all_ctrs_get(&force_ctrs)) {
985 printf("Permission denied, xnu/kpc requires root privileges.\n");
986 return (worked = false);
987 }
988 int ret;
989 // load pmc db
990 kpep_db *db = NULL;
991 if ((ret = kpep_db_create(NULL, &db))) {
992 printf("Error: cannot load pmc database: %d.\n", ret);
993 return (worked = false);
994 }
995 printf("loaded db: %s (%s)\n", db->name, db->marketing_name);
996 // printf("number of fixed counters: %zu\n", db->fixed_counter_count);
997 // printf("number of configurable counters: %zu\n", db->config_counter_count);
998
999 // create a config
1000 kpep_config *cfg = NULL;
1001 if ((ret = kpep_config_create(db, &cfg))) {
1002 printf("Failed to create kpep config: %d (%s).\n", ret,
1004 return (worked = false);
1005 }
1006 if ((ret = kpep_config_force_counters(cfg))) {
1007 printf("Failed to force counters: %d (%s).\n", ret,
1009 return (worked = false);
1010 }
1011
1012 // get events
1013 kpep_event *ev_arr[ev_count] = {0};
1014 for (usize i = 0; i < ev_count; i++) {
1015 const event_alias *alias = profile_events + i;
1016 ev_arr[i] = get_event(db, alias);
1017 if (!ev_arr[i]) {
1018 printf("Cannot find event: %s.\n", alias->alias);
1019 return (worked = false);
1020 }
1021 }
1022
1023 // add event to config
1024 for (usize i = 0; i < ev_count; i++) {
1025 kpep_event *ev = ev_arr[i];
1026 if ((ret = kpep_config_add_event(cfg, &ev, 0, NULL))) {
1027 printf("Failed to add event: %d (%s).\n", ret,
1029 return (worked = false);
1030 }
1031 }
1032
1033 // prepare buffer and config
1034 u32 classes = 0;
1035 usize reg_count = 0;
1036 if ((ret = kpep_config_kpc_classes(cfg, &classes))) {
1037 printf("Failed get kpc classes: %d (%s).\n", ret,
1039 return (worked = false);
1040 }
1041 if ((ret = kpep_config_kpc_count(cfg, &reg_count))) {
1042 printf("Failed get kpc count: %d (%s).\n", ret,
1044 return (worked = false);
1045 }
1046 if ((ret = kpep_config_kpc_map(cfg, counter_map, sizeof(counter_map)))) {
1047 printf("Failed get kpc map: %d (%s).\n", ret, kpep_config_error_desc(ret));
1048 return (worked = false);
1049 }
1050 if ((ret = kpep_config_kpc(cfg, regs, sizeof(regs)))) {
1051 printf("Failed get kpc registers: %d (%s).\n", ret,
1053 return (worked = false);
1054 }
1055
1056 // set config to kernel
1057 if ((ret = kpc_force_all_ctrs_set(1))) {
1058 printf("Failed force all ctrs: %d.\n", ret);
1059 return (worked = false);
1060 }
1061 if ((classes & KPC_CLASS_CONFIGURABLE_MASK) && reg_count) {
1062 if ((ret = kpc_set_config(classes, regs))) {
1063 printf("Failed set kpc config: %d.\n", ret);
1064 return (worked = false);
1065 }
1066 }
1067
1068 // start counting
1069 if ((ret = kpc_set_counting(classes))) {
1070 printf("Failed set counting: %d.\n", ret);
1071 return (worked = false);
1072 }
1073 if ((ret = kpc_set_thread_counting(classes))) {
1074 printf("Failed set thread counting: %d.\n", ret);
1075 return (worked = false);
1076 }
1077
1078 return (worked = true);
1079}
1080
1082 static bool warned = false;
1083 int ret;
1084 // get counters before
1086 if (!warned) {
1087 printf("Failed get thread counters before: %d.\n", ret);
1088 warned = true;
1089 }
1090 return 1;
1091 }
1092 /*
1093// We could print it out this way if we wanted to:
1094printf("counters value:\n");
1095for (usize i = 0; i < ev_count; i++) {
1096 const event_alias *alias = profile_events + i;
1097 usize idx = counter_map[i];
1098 u64 val = counters_1[idx] - counters_0[idx];
1099 printf("%14s: %llu\n", alias->alias, val);
1100}*/
1101 return performance_counters{
1104}
1105
1106#endif
static int kdebug_setreg(kd_regtype *kdr)
Set debug filter.
static void * lib_handle_kperf
static int kdebug_get_bufinfo(kbufinfo_t *info)
static int(* kperf_reset)(void)
static int(* kperf_action_filter_set_by_task)(u32 actionid, i32 port)
static int(* kperf_timer_pet_get)(u32 *timerid)
static int kdebug_reinit(void)
#define lib_path_kperf
#define lib_path_kperfdata
static int kdebug_wait(usize timeout_ms, bool *suc)
static int(* kperf_action_samplers_set)(u32 actionid, u32 sample)
static kpep_event * get_event(kpep_db *db, const event_alias *alias)
static u32(* kpc_get_counting)(void)
static bool lib_init(void)
static int(* kpep_db_create)(const char *name, kpep_db **db_ptr)
static bool lib_inited
static const char * kpep_config_error_names[KPEP_CONFIG_ERROR_MAX]
Error description for kpep_config_error_code.
static int(* kpep_config_kpc_map)(kpep_config *cfg, usize *buf, usize buf_size)
static int(* kpc_set_thread_counting)(u32 classes)
kpep_config_error_code
Error code for kpep_config_xxx() and kpep_db_xxx() functions.
@ KPEP_CONFIG_ERROR_ERRNO
@ KPEP_CONFIG_ERROR_NONE
@ KPEP_CONFIG_ERROR_BUFFER_TOO_SMALL
@ KPEP_CONFIG_ERROR_DB_NOT_FOUND
@ KPEP_CONFIG_ERROR_DB_VERSION_UNSUPPORTED
@ KPEP_CONFIG_ERROR_IO
@ KPEP_CONFIG_ERROR_EVENT_NOT_FOUND
@ KPEP_CONFIG_ERROR_INVALID_ARGUMENT
@ KPEP_CONFIG_ERROR_EVENT_UNAVAILABLE
@ KPEP_CONFIG_ERROR_DB_PATH_INVALID
@ KPEP_CONFIG_ERROR_DB_CORRUPT
@ KPEP_CONFIG_ERROR_OUT_OF_MEMORY
@ KPEP_CONFIG_ERROR_MAX
@ KPEP_CONFIG_ERROR_CUR_SYSTEM_UNKNOWN
@ KPEP_CONFIG_ERROR_COUNTERS_NOT_FORCED
@ KPEP_CONFIG_ERROR_CONFLICTING_EVENTS
@ KPEP_CONFIG_ERROR_DB_ARCH_UNSUPPORTED
#define EVENT_NAME_MAX
static const lib_symbol lib_symbols_kperfdata[]
uint64_t u64
static void(* kpep_db_free)(kpep_db *db)
Free the kpep database.
#define lib_symbol_def(name)
static int kdebug_trace_enable(bool enable)
int32_t i32
static int(* kperf_action_samplers_get)(u32 actionid, u32 *sample)
static int(* kpep_event_name)(kpep_event *ev, const char **name_ptr)
u64 kpc_config_t
static int(* kpep_config_add_event)(kpep_config *cfg, kpep_event **ev_ptr, u32 flag, u32 *err)
static int(* kpep_config_events_count)(kpep_config *cfg, usize *count_ptr)
static int(* kperf_sample_set)(u32 enabled)
static int(* kpc_set_counting)(u32 classes)
size_t usize
static const event_alias profile_events[]
Event names from /usr/share/kpep/<name>.plist.
static int kperf_lightweight_pet_get(u32 *enabled)
Get lightweight PET mode (not in kperf.framework).
static u64(* kperf_ticks_to_ns)(u64 ticks)
CPU ticks to nanoseconds.
static int(* kperf_timer_period_set)(u32 actionid, u64 tick)
static int kperf_lightweight_pet_set(u32 enabled)
Set lightweight PET mode (not in kperf.framework).
static int(* kpep_config_kpc_classes)(kpep_config *cfg, u32 *classes_ptr)
static int(* kperf_timer_action_set)(u32 actionid, u32 timerid)
static int(* kpep_db_events)(kpep_db *db, kpep_event **buf, usize buf_size)
float f32
static int(* kpep_db_aliases_count)(kpep_db *db, usize *count)
static int(* kpc_cpu_string)(char *buf, usize buf_size)
static u32(* kpc_pmu_version)(void)
static void * lib_handle_kperfdata
static u32(* kpc_get_config_count)(u32 classes)
static int(* kpep_config_kpc)(kpep_config *cfg, kpc_config_t *buf, usize buf_size)
static int(* kpc_get_thread_counters)(u32 tid, u32 buf_count, u64 *buf)
static int(* kperf_timer_pet_set)(u32 timerid)
u64 counters_1[KPC_MAX_COUNTERS]
#define KPC_MAX_COUNTERS
static int kdebug_trace_read(void *buf, usize len, usize *count)
int64_t i64
static void(* kpep_config_free)(kpep_config *cfg)
Free the config.
static int(* kpc_force_all_ctrs_set)(int val)
uint8_t u8
double f64
static int(* kperf_action_filter_set_by_pid)(u32 actionid, i32 pid)
static u64(* kperf_tick_frequency)(void)
CPU ticks frequency (mach_absolute_time).
static int(* kpep_db_aliases)(kpep_db *db, const char **buf, usize buf_size)
static const lib_symbol lib_symbols_kperf[]
static int(* kperf_timer_count_get)(u32 *count)
static int(* kpep_event_description)(kpep_event *ev, const char **str_ptr)
kpc_config_t regs[KPC_MAX_COUNTERS]
static void lib_deinit(void)
static int(* kpep_config_create)(kpep_db *db, kpep_config **cfg_ptr)
static int kdebug_trace_setbuf(int nbufs)
static int kdebug_reset(void)
#define lib_nelems(x)
u64 counters_0[KPC_MAX_COUNTERS]
static int(* kpep_db_event)(kpep_db *db, const char *name, kpep_event **ev_ptr)
static int(* kpep_config_kpc_count)(kpep_config *cfg, usize *count_ptr)
static char lib_err_msg[256]
static int(* kpep_db_events_count)(kpep_db *db, usize *count)
static int(* kpep_db_counters_count)(kpep_db *db, u8 classes, usize *count)
static u32(* kpc_get_thread_counting)(void)
bool setup_performance_counters()
static int(* kpep_event_alias)(kpep_event *ev, const char **alias_ptr)
static int(* kperf_action_count_get)(u32 *count)
static int(* kperf_timer_period_get)(u32 actionid, u64 *tick)
uintptr_t kd_buf_argtype
static u64(* kperf_ns_to_ticks)(u64 ns)
Nanoseconds to CPU ticks.
static int(* kperf_sample_get)(u32 *enabled)
#define return_err()
static const char * kpep_config_error_desc(int code)
Error description.
uint16_t u16
static u32(* kpc_get_counter_count)(u32 classes)
static int(* kpep_config_events)(kpep_config *cfg, kpep_event **buf, usize buf_size)
int16_t i16
performance_counters get_counters()
static int(* kpc_get_config)(u32 classes, kpc_config_t *config)
static bool lib_has_err
static int(* kpep_config_remove_event)(kpep_config *cfg, usize idx)
static int(* kperf_timer_action_get)(u32 actionid, u32 *timerid)
int8_t i8
#define KPC_CLASS_CONFIGURABLE_MASK
const usize ev_count
static int(* kperf_timer_count_set)(u32 count)
static int(* kpep_db_name)(kpep_db *db, const char **name)
static int(* kpc_force_all_ctrs_get)(int *val_out)
static int(* kpep_config_force_counters)(kpep_config *cfg)
usize counter_map[KPC_MAX_COUNTERS]
static int(* kpc_set_config)(u32 classes, kpc_config_t *config)
static int(* kperf_action_count_set)(u32 count)
uint32_t u32
performance_counters operator-(const performance_counters &a, const performance_counters &b)
static int(* kpc_get_cpu_counters)(bool all_cpus, u32 classes, int *curcpu, u64 *buf)
const char * names[EVENT_NAME_MAX]
name for print
const char * alias
unsigned int flags
uint32_t debugid
uint64_t timestamp
kd_buf_argtype arg4
kd_buf_argtype arg1
kd_buf_argtype arg2
kd_buf_argtype arg5
kd_buf_argtype arg3
unsigned int value1
unsigned int type
unsigned int value4
unsigned int value3
unsigned int value2
KPEP config (size: 80/44 bytes on 64/32 bit OS).
usize counter_count
kpep_config_events_count()
usize * ev_idx
(sizeof(usize *) * counter_count), init -1
usize * ev_map
(sizeof(usize *) * counter_count), init 0
u32 classes
See class mask constants above.
u32 * flags
(sizeof(u32 *) * counter_count), init 0
kpep_event ** ev_arr
(sizeof(kpep_event *) * counter_count), init NULL
u64 * kpc_periods
(sizeof(u64 *) * counter_count), init 0
KPEP database (size: 144/80 bytes on 64/32 bit OS).
kpep_event ** fixed_event_arr
u32 power_counter_bits
u32 archtecture
see KPEP CPU archtecture constants above.
u32 fixed_counter_bits
usize alias_count
void * alias_map
All aliases (CFDict<CFSTR(event_name), kpep_event *>).
void * event_map
All events (CFDict<CFSTR(event_name), kpep_event *>).
u32 config_counter_bits
usize fixed_counter_count
usize power_counter_count
usize config_counter_count
const char * cpu_id
Plist name, such as "cpu_7_8_10b282dc".
kpep_event * event_arr
Event struct buffer (sizeof(kpep_event) * events_count).
const char * name
Database name, such as "haswell".
const char * marketing_name
Marketing name, such as "Intel Haswell".
usize event_count
All events count.
void * plist_data
Plist data (CFDataRef), currently NULL.
KPEP event (size: 48/28 bytes on 64/32 bit OS).
const char * fallback
Fallback event name for fixed counter.
const char * description
Description for this event.
const char * name
Unique name of a event, such as "INST_RETIRED.ANY".
const char * errata
Errata, currently NULL.
const char * alias
Alias name, such as "Instructions", "Cycles".
const char * name
performance_counters & operator-=(const performance_counters &other)
performance_counters(double c, double b, double m, double i)
performance_counters & operator+=(const performance_counters &other)
performance_counters(double init)
performance_counters & min(const performance_counters &other)
performance_counters(uint64_t c, uint64_t b, uint64_t m, uint64_t i)
performance_counters & operator/=(double numerator)