]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kperf/kperf.c
77fc0cc46636fa75396f70cbd0fb63c24221a643
[apple/xnu.git] / osfmk / kperf / kperf.c
1 /*
2 * Copyright (c) 2011-2018 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 */
28
29 #include <kern/ipc_tt.h> /* port_name_to_task */
30 #include <kern/thread.h>
31 #include <kern/machine.h>
32 #include <kern/kalloc.h>
33 #include <mach/mach_types.h>
34 #include <sys/errno.h>
35 #include <sys/ktrace.h>
36
37 #include <kperf/action.h>
38 #include <kperf/buffer.h>
39 #include <kperf/kdebug_trigger.h>
40 #include <kperf/kperf.h>
41 #include <kperf/kptimer.h>
42 #include <kperf/lazy.h>
43 #include <kperf/pet.h>
44 #include <kperf/sample.h>
45
46 /* from libkern/libkern.h */
47 extern uint64_t strtouq(const char *, char **, int);
48
49 LCK_GRP_DECLARE(kperf_lck_grp, "kperf");
50
51 /* one wired sample buffer per CPU */
52 static struct kperf_sample *intr_samplev;
53 static unsigned int intr_samplec = 0;
54
55 /* current sampling status */
56 enum kperf_sampling kperf_status = KPERF_SAMPLING_OFF;
57
58 /*
59 * Only set up kperf once.
60 */
61 static bool kperf_is_setup = false;
62
63 /* whether or not to callback to kperf on context switch */
64 boolean_t kperf_on_cpu_active = FALSE;
65
66 unsigned int kperf_thread_blocked_action;
67 unsigned int kperf_cpu_sample_action;
68
69 struct kperf_sample *
70 kperf_intr_sample_buffer(void)
71 {
72 unsigned ncpu = cpu_number();
73
74 assert(ml_get_interrupts_enabled() == FALSE);
75 assert(ncpu < intr_samplec);
76
77 return &(intr_samplev[ncpu]);
78 }
79
80 void
81 kperf_init_early(void)
82 {
83 /*
84 * kperf allocates based on the number of CPUs and requires them to all be
85 * accounted for.
86 */
87 ml_wait_max_cpus();
88
89 boolean_t found_kperf = FALSE;
90 char kperf_config_str[64];
91 found_kperf = PE_parse_boot_arg_str("kperf", kperf_config_str, sizeof(kperf_config_str));
92 if (found_kperf && kperf_config_str[0] != '\0') {
93 kperf_kernel_configure(kperf_config_str);
94 }
95 }
96
97 void
98 kperf_init(void)
99 {
100 kptimer_init();
101 }
102
103 void
104 kperf_setup(void)
105 {
106 if (kperf_is_setup) {
107 return;
108 }
109
110 intr_samplec = machine_info.logical_cpu_max;
111 size_t intr_samplev_size = intr_samplec * sizeof(*intr_samplev);
112 intr_samplev = kalloc_tag(intr_samplev_size, VM_KERN_MEMORY_DIAG);
113 memset(intr_samplev, 0, intr_samplev_size);
114
115 kperf_kdebug_setup();
116
117 kperf_is_setup = true;
118 }
119
120 void
121 kperf_reset(void)
122 {
123 /*
124 * Make sure samples aren't being taken before tearing everything down.
125 */
126 (void)kperf_disable_sampling();
127
128 kperf_lazy_reset();
129 (void)kperf_kdbg_cswitch_set(0);
130 kperf_kdebug_reset();
131 kptimer_reset();
132 kppet_reset();
133
134 /*
135 * Most of the other systems call into actions, so reset them last.
136 */
137 kperf_action_reset();
138 }
139
140 void
141 kperf_kernel_configure(const char *config)
142 {
143 int pairs = 0;
144 char *end;
145 bool pet = false;
146
147 assert(config != NULL);
148
149 ktrace_start_single_threaded();
150
151 ktrace_kernel_configure(KTRACE_KPERF);
152
153 if (config[0] == 'p') {
154 pet = true;
155 config++;
156 }
157
158 do {
159 uint32_t action_samplers;
160 uint64_t timer_period_ns;
161 uint64_t timer_period;
162
163 pairs += 1;
164 kperf_action_set_count(pairs);
165 kptimer_set_count(pairs);
166
167 action_samplers = (uint32_t)strtouq(config, &end, 0);
168 if (config == end) {
169 kprintf("kperf: unable to parse '%s' as action sampler\n", config);
170 goto out;
171 }
172 config = end;
173
174 kperf_action_set_samplers(pairs, action_samplers);
175
176 if (config[0] == '\0') {
177 kprintf("kperf: missing timer period in config\n");
178 goto out;
179 }
180 config++;
181
182 timer_period_ns = strtouq(config, &end, 0);
183 if (config == end) {
184 kprintf("kperf: unable to parse '%s' as timer period\n", config);
185 goto out;
186 }
187 nanoseconds_to_absolutetime(timer_period_ns, &timer_period);
188 config = end;
189
190 kptimer_set_period(pairs - 1, timer_period);
191 kptimer_set_action(pairs - 1, pairs);
192
193 if (pet) {
194 kptimer_set_pet_timerid(pairs - 1);
195 kppet_set_lightweight_pet(1);
196 pet = false;
197 }
198 } while (*(config++) == ',');
199
200 int error = kperf_enable_sampling();
201 if (error) {
202 printf("kperf: cannot enable sampling at boot: %d\n", error);
203 }
204
205 out:
206 ktrace_end_single_threaded();
207 }
208
209 void kperf_on_cpu_internal(thread_t thread, thread_continue_t continuation,
210 uintptr_t *starting_fp);
211 void
212 kperf_on_cpu_internal(thread_t thread, thread_continue_t continuation,
213 uintptr_t *starting_fp)
214 {
215 if (kperf_kdebug_cswitch) {
216 /* trace the new thread's PID for Instruments */
217 int pid = task_pid(get_threadtask(thread));
218 BUF_DATA(PERF_TI_CSWITCH, thread_tid(thread), pid);
219 }
220 if (kppet_lightweight_active) {
221 kppet_on_cpu(thread, continuation, starting_fp);
222 }
223 if (kperf_lazy_wait_action != 0) {
224 kperf_lazy_wait_sample(thread, continuation, starting_fp);
225 }
226 }
227
228 void
229 kperf_on_cpu_update(void)
230 {
231 kperf_on_cpu_active = kperf_kdebug_cswitch ||
232 kppet_lightweight_active ||
233 kperf_lazy_wait_action != 0;
234 }
235
236 bool
237 kperf_is_sampling(void)
238 {
239 return kperf_status == KPERF_SAMPLING_ON;
240 }
241
242 int
243 kperf_enable_sampling(void)
244 {
245 if (kperf_status == KPERF_SAMPLING_ON) {
246 return 0;
247 }
248
249 if (kperf_status != KPERF_SAMPLING_OFF) {
250 panic("kperf: sampling was %d when asked to enable", kperf_status);
251 }
252
253 /* make sure interrupt tables and actions are initted */
254 if (!kperf_is_setup || (kperf_action_get_count() == 0)) {
255 return ECANCELED;
256 }
257
258 kperf_status = KPERF_SAMPLING_ON;
259 kppet_lightweight_active_update();
260 kptimer_start();
261
262 return 0;
263 }
264
265 int
266 kperf_disable_sampling(void)
267 {
268 if (kperf_status != KPERF_SAMPLING_ON) {
269 return 0;
270 }
271
272 /* mark a shutting down */
273 kperf_status = KPERF_SAMPLING_SHUTDOWN;
274
275 /* tell timers to disable */
276 kptimer_stop();
277
278 /* mark as off */
279 kperf_status = KPERF_SAMPLING_OFF;
280 kppet_lightweight_active_update();
281
282 return 0;
283 }
284
285 void
286 kperf_timer_expire(void *param0, void * __unused param1)
287 {
288 processor_t processor = param0;
289 int cpuid = processor->cpu_id;
290
291 kptimer_expire(processor, cpuid, mach_absolute_time());
292 }
293
294 boolean_t
295 kperf_thread_get_dirty(thread_t thread)
296 {
297 return thread->c_switch != thread->kperf_c_switch;
298 }
299
300 void
301 kperf_thread_set_dirty(thread_t thread, boolean_t dirty)
302 {
303 if (dirty) {
304 thread->kperf_c_switch = thread->c_switch - 1;
305 } else {
306 thread->kperf_c_switch = thread->c_switch;
307 }
308 }
309
310 int
311 kperf_port_to_pid(mach_port_name_t portname)
312 {
313 if (!MACH_PORT_VALID(portname)) {
314 return -1;
315 }
316
317 task_t task = port_name_to_task(portname);
318 if (task == TASK_NULL) {
319 return -1;
320 }
321
322 pid_t pid = task_pid(task);
323
324 os_ref_count_t __assert_only count = task_deallocate_internal(task);
325 assert(count != 0);
326
327 return pid;
328 }