]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2011-2019 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
d9a64523 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
d9a64523 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
d9a64523 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
d9a64523 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b | 27 | */ |
1c79356b | 28 | |
39037602 A |
29 | #ifndef KPERF_H |
30 | #define KPERF_H | |
1c79356b | 31 | |
3e170ce0 | 32 | #include <kern/thread.h> |
39037602 | 33 | #include <kern/locks.h> |
3e170ce0 | 34 | |
39037602 | 35 | extern lck_grp_t kperf_lck_grp; |
b0d623f7 | 36 | |
39037602 | 37 | /* the trigger types supported by kperf */ |
d9a64523 A |
38 | #define TRIGGER_TYPE_TIMER (0) |
39 | #define TRIGGER_TYPE_PMI (1) | |
40 | #define TRIGGER_TYPE_KDEBUG (2) | |
41 | #define TRIGGER_TYPE_LAZY_WAIT (3) | |
42 | #define TRIGGER_TYPE_LAZY_CPU (3) | |
1c79356b | 43 | |
94ff46dc A |
44 | uint32_t kperf_get_thread_ast(thread_t thread); |
45 | void kperf_set_thread_ast(thread_t thread, uint32_t flags); | |
1c79356b | 46 | |
39037602 A |
47 | /* |
48 | * Get and set dirtiness of thread, so kperf can track whether the thread | |
49 | * has been dispatched since it last looked. | |
50 | */ | |
51 | boolean_t kperf_thread_get_dirty(thread_t thread); | |
52 | void kperf_thread_set_dirty(thread_t thread, boolean_t dirty); | |
53 | ||
f427ee49 A |
54 | /* |
55 | * Initialize the rest of kperf lazily, upon first use. May be called multiple times. | |
56 | * The ktrace_lock must be held. | |
57 | */ | |
58 | void kperf_setup(void); | |
39037602 A |
59 | |
60 | /* | |
f427ee49 | 61 | * Configure kperf during boot and check the boot args. |
39236c6e | 62 | */ |
f427ee49 | 63 | extern void kperf_init_early(void); |
39236c6e | 64 | |
f427ee49 A |
65 | bool kperf_is_sampling(void); |
66 | int kperf_enable_sampling(void); | |
67 | int kperf_disable_sampling(void); | |
68 | int kperf_port_to_pid(mach_port_name_t portname); | |
39236c6e | 69 | |
39037602 A |
70 | /* get a per-CPU sample buffer */ |
71 | struct kperf_sample *kperf_intr_sample_buffer(void); | |
72 | ||
f427ee49 A |
73 | enum kperf_sampling { |
74 | KPERF_SAMPLING_OFF, | |
75 | KPERF_SAMPLING_SHUTDOWN, | |
76 | KPERF_SAMPLING_ON, | |
77 | }; | |
78 | ||
79 | extern enum kperf_sampling kperf_status; | |
80 | ||
81 | #pragma mark - external callbacks | |
82 | ||
39037602 | 83 | /* |
f427ee49 | 84 | * Set up kperf during system startup. |
39037602 | 85 | */ |
f427ee49 | 86 | void kperf_init(void); |
39037602 A |
87 | |
88 | /* | |
d9a64523 A |
89 | * kperf AST handler |
90 | * | |
91 | * Prevent inlining, since the sampling function allocates on the stack and | |
92 | * branches calling ast_taken (but never on a kperf AST) may blow their stacks. | |
39037602 | 93 | */ |
d9a64523 | 94 | extern __attribute__((noinline)) void kperf_thread_ast_handler(thread_t thread); |
39037602 | 95 | |
f427ee49 A |
96 | /* |
97 | * Update whether the on-CPU callback should be called. | |
98 | */ | |
39037602 A |
99 | void kperf_on_cpu_update(void); |
100 | ||
f427ee49 A |
101 | /* |
102 | * Should only be called by the scheduler when `thread` is switching on-CPU. | |
103 | */ | |
39037602 A |
104 | static inline void |
105 | kperf_on_cpu(thread_t thread, thread_continue_t continuation, | |
0a7de745 | 106 | uintptr_t *starting_fp) |
39037602 | 107 | { |
d9a64523 A |
108 | extern boolean_t kperf_on_cpu_active; |
109 | void kperf_on_cpu_internal(thread_t thread, thread_continue_t continuation, | |
0a7de745 | 110 | uintptr_t *starting_fp); |
d9a64523 | 111 | |
39037602 A |
112 | if (__improbable(kperf_on_cpu_active)) { |
113 | kperf_on_cpu_internal(thread, continuation, starting_fp); | |
114 | } | |
115 | } | |
116 | ||
f427ee49 A |
117 | /* |
118 | * Should only be called by the scheduler when `thread` is switching off-CPU. | |
119 | */ | |
d9a64523 A |
120 | static inline void |
121 | kperf_off_cpu(thread_t thread) | |
122 | { | |
123 | extern unsigned int kperf_lazy_cpu_action; | |
124 | void kperf_lazy_off_cpu(thread_t thread); | |
125 | ||
126 | if (__improbable(kperf_lazy_cpu_action != 0)) { | |
127 | kperf_lazy_off_cpu(thread); | |
128 | } | |
129 | } | |
39236c6e | 130 | |
f427ee49 A |
131 | /* |
132 | * Should only be called by the scheduler when `thread` is made runnable. | |
133 | */ | |
d9a64523 A |
134 | static inline void |
135 | kperf_make_runnable(thread_t thread, int interrupt) | |
136 | { | |
137 | extern unsigned int kperf_lazy_cpu_action; | |
138 | void kperf_lazy_make_runnable(thread_t thread, bool interrupt); | |
39037602 | 139 | |
d9a64523 A |
140 | if (__improbable(kperf_lazy_cpu_action != 0)) { |
141 | kperf_lazy_make_runnable(thread, interrupt); | |
142 | } | |
143 | } | |
39037602 | 144 | |
f427ee49 A |
145 | static inline void |
146 | kperf_running_setup(processor_t processor, uint64_t now) | |
147 | { | |
148 | if (kperf_status == KPERF_SAMPLING_ON) { | |
149 | extern void kptimer_running_setup(processor_t, uint64_t now); | |
150 | kptimer_running_setup(processor, now); | |
151 | } | |
152 | } | |
153 | ||
154 | /* | |
155 | * Should only be called by platform code at the end of each interrupt. | |
156 | */ | |
d9a64523 A |
157 | static inline void |
158 | kperf_interrupt(void) | |
159 | { | |
160 | extern unsigned int kperf_lazy_cpu_action; | |
161 | extern void kperf_lazy_cpu_sample(thread_t thread, unsigned int flags, | |
0a7de745 | 162 | bool interrupt); |
d9a64523 A |
163 | |
164 | if (__improbable(kperf_lazy_cpu_action != 0)) { | |
f427ee49 | 165 | kperf_lazy_cpu_sample(NULL, 0, true); |
d9a64523 A |
166 | } |
167 | } | |
39037602 | 168 | |
f427ee49 A |
169 | /* |
170 | * Should only be called by kdebug when an event with `debugid` is emitted | |
171 | * from the frame starting at `starting_fp`. | |
172 | */ | |
39037602 A |
173 | static inline void |
174 | kperf_kdebug_callback(uint32_t debugid, uintptr_t *starting_fp) | |
175 | { | |
d9a64523 A |
176 | extern boolean_t kperf_kdebug_active; |
177 | void kperf_kdebug_handler(uint32_t debugid, uintptr_t *starting_fp); | |
178 | ||
39037602 A |
179 | if (__improbable(kperf_kdebug_active)) { |
180 | kperf_kdebug_handler(debugid, starting_fp); | |
181 | } | |
182 | } | |
183 | ||
f427ee49 A |
184 | /* |
185 | * Should only be called by platform code to indicate kperf's per-CPU timer | |
186 | * has expired on the current CPU `cpuid` at time `now`. | |
187 | */ | |
188 | void kperf_timer_expire(void *param0, void *param1); | |
189 | ||
39037602 A |
190 | /* |
191 | * Used by ktrace to reset kperf. ktrace_lock must be held. | |
39236c6e | 192 | */ |
39037602 | 193 | extern void kperf_reset(void); |
39236c6e | 194 | |
5ba3f43e A |
195 | /* |
196 | * Configure kperf from the kernel (e.g. during boot). | |
197 | */ | |
198 | void kperf_kernel_configure(const char *config); | |
199 | ||
39037602 | 200 | #endif /* !defined(KPERF_H) */ |