]>
Commit | Line | Data |
---|---|---|
316670eb A |
1 | /* |
2 | * Copyright (c) 2011 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
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. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
39037602 | 28 | #include <kern/ipc_tt.h> /* port_name_to_task */ |
316670eb A |
29 | #include <kern/thread.h> |
30 | #include <kern/machine.h> | |
31 | #include <kern/kalloc.h> | |
39037602 | 32 | #include <mach/mach_types.h> |
316670eb | 33 | #include <sys/errno.h> |
39037602 | 34 | #include <sys/ktrace.h> |
316670eb | 35 | |
316670eb | 36 | #include <kperf/action.h> |
39037602 A |
37 | #include <kperf/buffer.h> |
38 | #include <kperf/kdebug_trigger.h> | |
316670eb | 39 | #include <kperf/kperf.h> |
39037602 A |
40 | #include <kperf/kperf_timer.h> |
41 | #include <kperf/pet.h> | |
42 | #include <kperf/sample.h> | |
39236c6e | 43 | |
5ba3f43e A |
44 | /* from libkern/libkern.h */ |
45 | extern uint64_t strtouq(const char *, char **, int); | |
46 | ||
39037602 | 47 | lck_grp_t kperf_lck_grp; |
316670eb | 48 | |
5ba3f43e A |
49 | /* IDs of threads on CPUs before starting the PET thread */ |
50 | uint64_t *kperf_tid_on_cpus = NULL; | |
39236c6e | 51 | |
39037602 A |
52 | /* one wired sample buffer per CPU */ |
53 | static struct kperf_sample *intr_samplev; | |
54 | static unsigned int intr_samplec = 0; | |
39236c6e | 55 | |
39037602 | 56 | /* current sampling status */ |
316670eb | 57 | static unsigned sampling_status = KPERF_SAMPLING_OFF; |
316670eb | 58 | |
39037602 A |
59 | /* only init once */ |
60 | static boolean_t kperf_initted = FALSE; | |
316670eb | 61 | |
39037602 A |
62 | /* whether or not to callback to kperf on context switch */ |
63 | boolean_t kperf_on_cpu_active = FALSE; | |
316670eb | 64 | |
39037602 | 65 | struct kperf_sample * |
316670eb A |
66 | kperf_intr_sample_buffer(void) |
67 | { | |
39037602 | 68 | unsigned ncpu = cpu_number(); |
316670eb | 69 | |
39037602 A |
70 | assert(ml_get_interrupts_enabled() == FALSE); |
71 | assert(ncpu < intr_samplec); | |
316670eb | 72 | |
39037602 | 73 | return &(intr_samplev[ncpu]); |
39236c6e A |
74 | } |
75 | ||
316670eb A |
76 | /* setup interrupt sample buffers */ |
77 | int | |
78 | kperf_init(void) | |
79 | { | |
39037602 A |
80 | static lck_grp_attr_t lck_grp_attr; |
81 | ||
316670eb | 82 | unsigned ncpus = 0; |
39236c6e | 83 | int err; |
316670eb | 84 | |
39037602 | 85 | if (kperf_initted) { |
316670eb | 86 | return 0; |
39037602 A |
87 | } |
88 | ||
89 | lck_grp_attr_setdefault(&lck_grp_attr); | |
90 | lck_grp_init(&kperf_lck_grp, "kperf", &lck_grp_attr); | |
316670eb | 91 | |
316670eb A |
92 | ncpus = machine_info.logical_cpu_max; |
93 | ||
39037602 | 94 | /* create buffers to remember which threads don't need to be sampled by PET */ |
5ba3f43e | 95 | kperf_tid_on_cpus = kalloc_tag(ncpus * sizeof(*kperf_tid_on_cpus), |
39037602 | 96 | VM_KERN_MEMORY_DIAG); |
5ba3f43e | 97 | if (kperf_tid_on_cpus == NULL) { |
39236c6e A |
98 | err = ENOMEM; |
99 | goto error; | |
100 | } | |
5ba3f43e | 101 | bzero(kperf_tid_on_cpus, ncpus * sizeof(*kperf_tid_on_cpus)); |
39236c6e | 102 | |
39037602 | 103 | /* create the interrupt buffers */ |
39236c6e | 104 | intr_samplec = ncpus; |
39037602 A |
105 | intr_samplev = kalloc_tag(ncpus * sizeof(*intr_samplev), |
106 | VM_KERN_MEMORY_DIAG); | |
107 | if (intr_samplev == NULL) { | |
39236c6e A |
108 | err = ENOMEM; |
109 | goto error; | |
110 | } | |
39037602 | 111 | bzero(intr_samplev, ncpus * sizeof(*intr_samplev)); |
316670eb | 112 | |
39037602 A |
113 | /* create kdebug trigger filter buffers */ |
114 | if ((err = kperf_kdebug_init())) { | |
115 | goto error; | |
116 | } | |
39236c6e | 117 | |
39037602 A |
118 | kperf_initted = TRUE; |
119 | return 0; | |
39236c6e | 120 | |
39037602 A |
121 | error: |
122 | if (intr_samplev) { | |
123 | kfree(intr_samplev, ncpus * sizeof(*intr_samplev)); | |
124 | intr_samplev = NULL; | |
125 | intr_samplec = 0; | |
126 | } | |
316670eb | 127 | |
5ba3f43e A |
128 | if (kperf_tid_on_cpus) { |
129 | kfree(kperf_tid_on_cpus, ncpus * sizeof(*kperf_tid_on_cpus)); | |
130 | kperf_tid_on_cpus = NULL; | |
39037602 | 131 | } |
316670eb | 132 | |
39236c6e | 133 | return err; |
316670eb A |
134 | } |
135 | ||
39037602 A |
136 | void |
137 | kperf_reset(void) | |
316670eb | 138 | { |
39037602 A |
139 | /* turn off sampling first */ |
140 | (void)kperf_sampling_disable(); | |
141 | ||
142 | /* cleanup miscellaneous configuration first */ | |
143 | (void)kperf_kdbg_cswitch_set(0); | |
144 | (void)kperf_set_lightweight_pet(0); | |
145 | kperf_kdebug_reset(); | |
146 | ||
147 | /* timers, which require actions, first */ | |
148 | kperf_timer_reset(); | |
149 | kperf_action_reset(); | |
316670eb A |
150 | } |
151 | ||
5ba3f43e A |
152 | void |
153 | kperf_kernel_configure(const char *config) | |
154 | { | |
155 | int pairs = 0; | |
156 | char *end; | |
157 | bool pet = false; | |
158 | ||
159 | assert(config != NULL); | |
160 | ||
161 | ktrace_start_single_threaded(); | |
162 | ||
163 | ktrace_kernel_configure(KTRACE_KPERF); | |
164 | ||
165 | if (config[0] == 'p') { | |
166 | pet = true; | |
167 | config++; | |
168 | } | |
169 | ||
170 | do { | |
171 | uint32_t action_samplers; | |
cc8bc92a | 172 | uint64_t timer_period_ns; |
5ba3f43e A |
173 | uint64_t timer_period; |
174 | ||
175 | pairs += 1; | |
176 | kperf_action_set_count(pairs); | |
177 | kperf_timer_set_count(pairs); | |
178 | ||
179 | action_samplers = (uint32_t)strtouq(config, &end, 0); | |
180 | if (config == end) { | |
181 | kprintf("kperf: unable to parse '%s' as action sampler\n", config); | |
182 | goto out; | |
183 | } | |
184 | config = end; | |
185 | ||
186 | kperf_action_set_samplers(pairs, action_samplers); | |
187 | ||
188 | if (config[0] == '\0') { | |
189 | kprintf("kperf: missing timer period in config\n"); | |
190 | goto out; | |
191 | } | |
192 | config++; | |
193 | ||
cc8bc92a | 194 | timer_period_ns = strtouq(config, &end, 0); |
5ba3f43e A |
195 | if (config == end) { |
196 | kprintf("kperf: unable to parse '%s' as timer period\n", config); | |
197 | goto out; | |
198 | } | |
cc8bc92a | 199 | nanoseconds_to_absolutetime(timer_period_ns, &timer_period); |
5ba3f43e A |
200 | config = end; |
201 | ||
202 | kperf_timer_set_period(pairs - 1, timer_period); | |
203 | kperf_timer_set_action(pairs - 1, pairs); | |
204 | ||
205 | if (pet) { | |
206 | kperf_timer_set_petid(pairs - 1); | |
207 | kperf_set_lightweight_pet(1); | |
208 | pet = false; | |
209 | } | |
210 | } while (*(config++) == ','); | |
211 | ||
212 | kperf_sampling_enable(); | |
213 | ||
214 | out: | |
215 | ktrace_end_single_threaded(); | |
216 | } | |
217 | ||
316670eb | 218 | void |
39037602 A |
219 | kperf_on_cpu_internal(thread_t thread, thread_continue_t continuation, |
220 | uintptr_t *starting_fp) | |
316670eb | 221 | { |
39037602 A |
222 | if (kperf_kdebug_cswitch) { |
223 | /* trace the new thread's PID for Instruments */ | |
224 | int pid = task_pid(get_threadtask(thread)); | |
225 | ||
226 | BUF_DATA(PERF_TI_CSWITCH, thread_tid(thread), pid); | |
227 | } | |
228 | if (kperf_lightweight_pet_active) { | |
229 | kperf_pet_on_cpu(thread, continuation, starting_fp); | |
230 | } | |
316670eb A |
231 | } |
232 | ||
316670eb | 233 | void |
39037602 | 234 | kperf_on_cpu_update(void) |
316670eb | 235 | { |
39037602 A |
236 | kperf_on_cpu_active = kperf_kdebug_cswitch || |
237 | kperf_lightweight_pet_active; | |
238 | } | |
239 | ||
240 | /* random misc-ish functions */ | |
241 | uint32_t | |
242 | kperf_get_thread_flags(thread_t thread) | |
243 | { | |
244 | return thread->kperf_flags; | |
245 | } | |
316670eb | 246 | |
39037602 A |
247 | void |
248 | kperf_set_thread_flags(thread_t thread, uint32_t flags) | |
249 | { | |
250 | thread->kperf_flags = flags; | |
316670eb A |
251 | } |
252 | ||
39037602 | 253 | unsigned int |
316670eb A |
254 | kperf_sampling_status(void) |
255 | { | |
256 | return sampling_status; | |
257 | } | |
258 | ||
259 | int | |
260 | kperf_sampling_enable(void) | |
261 | { | |
39037602 | 262 | if (sampling_status == KPERF_SAMPLING_ON) { |
316670eb | 263 | return 0; |
39037602 | 264 | } |
316670eb | 265 | |
39037602 A |
266 | if (sampling_status != KPERF_SAMPLING_OFF) { |
267 | panic("kperf: sampling was %d when asked to enable", sampling_status); | |
268 | } | |
316670eb A |
269 | |
270 | /* make sure interrupt tables and actions are initted */ | |
39037602 | 271 | if (!kperf_initted || (kperf_action_get_count() == 0)) { |
316670eb | 272 | return ECANCELED; |
39037602 | 273 | } |
316670eb A |
274 | |
275 | /* mark as running */ | |
276 | sampling_status = KPERF_SAMPLING_ON; | |
39037602 | 277 | kperf_lightweight_pet_active_update(); |
316670eb A |
278 | |
279 | /* tell timers to enable */ | |
280 | kperf_timer_go(); | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
285 | int | |
286 | kperf_sampling_disable(void) | |
287 | { | |
39037602 | 288 | if (sampling_status != KPERF_SAMPLING_ON) { |
316670eb | 289 | return 0; |
39037602 | 290 | } |
316670eb A |
291 | |
292 | /* mark a shutting down */ | |
293 | sampling_status = KPERF_SAMPLING_SHUTDOWN; | |
294 | ||
295 | /* tell timers to disable */ | |
296 | kperf_timer_stop(); | |
297 | ||
298 | /* mark as off */ | |
299 | sampling_status = KPERF_SAMPLING_OFF; | |
39037602 | 300 | kperf_lightweight_pet_active_update(); |
316670eb A |
301 | |
302 | return 0; | |
303 | } | |
39236c6e | 304 | |
39037602 A |
305 | boolean_t |
306 | kperf_thread_get_dirty(thread_t thread) | |
307 | { | |
308 | return (thread->c_switch != thread->kperf_c_switch); | |
309 | } | |
310 | ||
311 | void | |
312 | kperf_thread_set_dirty(thread_t thread, boolean_t dirty) | |
313 | { | |
314 | if (dirty) { | |
315 | thread->kperf_c_switch = thread->c_switch - 1; | |
316 | } else { | |
317 | thread->kperf_c_switch = thread->c_switch; | |
318 | } | |
319 | } | |
320 | ||
39236c6e A |
321 | int |
322 | kperf_port_to_pid(mach_port_name_t portname) | |
323 | { | |
324 | task_t task; | |
325 | int pid; | |
326 | ||
39037602 | 327 | if (!MACH_PORT_VALID(portname)) { |
39236c6e | 328 | return -1; |
39037602 | 329 | } |
39236c6e A |
330 | |
331 | task = port_name_to_task(portname); | |
39236c6e | 332 | |
39037602 A |
333 | if (task == TASK_NULL) { |
334 | return -1; | |
335 | } | |
39236c6e | 336 | |
39037602 | 337 | pid = task_pid(task); |
39236c6e A |
338 | |
339 | task_deallocate_internal(task); | |
340 | ||
341 | return pid; | |
342 | } |