]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kperf/kperf.c
xnu-3789.1.32.tar.gz
[apple/xnu.git] / osfmk / kperf / kperf.c
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 */
28 #include <kern/ipc_tt.h> /* port_name_to_task */
29 #include <kern/thread.h>
30 #include <kern/machine.h>
31 #include <kern/kalloc.h>
32 #include <mach/mach_types.h>
33 #include <sys/errno.h>
34 #include <sys/ktrace.h>
35
36 #include <kperf/action.h>
37 #include <kperf/buffer.h>
38 #include <kperf/kdebug_trigger.h>
39 #include <kperf/kperf.h>
40 #include <kperf/kperf_timer.h>
41 #include <kperf/pet.h>
42 #include <kperf/sample.h>
43
44 lck_grp_t kperf_lck_grp;
45
46 /* thread on CPUs before starting the PET thread */
47 thread_t *kperf_thread_on_cpus = NULL;
48
49 /* one wired sample buffer per CPU */
50 static struct kperf_sample *intr_samplev;
51 static unsigned int intr_samplec = 0;
52
53 /* current sampling status */
54 static unsigned sampling_status = KPERF_SAMPLING_OFF;
55
56 /* only init once */
57 static boolean_t kperf_initted = FALSE;
58
59 /* whether or not to callback to kperf on context switch */
60 boolean_t kperf_on_cpu_active = FALSE;
61
62 struct kperf_sample *
63 kperf_intr_sample_buffer(void)
64 {
65 unsigned ncpu = cpu_number();
66
67 assert(ml_get_interrupts_enabled() == FALSE);
68 assert(ncpu < intr_samplec);
69
70 return &(intr_samplev[ncpu]);
71 }
72
73 /* setup interrupt sample buffers */
74 int
75 kperf_init(void)
76 {
77 static lck_grp_attr_t lck_grp_attr;
78
79 lck_mtx_assert(ktrace_lock, LCK_MTX_ASSERT_OWNED);
80
81 unsigned ncpus = 0;
82 int err;
83
84 if (kperf_initted) {
85 return 0;
86 }
87
88 lck_grp_attr_setdefault(&lck_grp_attr);
89 lck_grp_init(&kperf_lck_grp, "kperf", &lck_grp_attr);
90
91 ncpus = machine_info.logical_cpu_max;
92
93 /* create buffers to remember which threads don't need to be sampled by PET */
94 kperf_thread_on_cpus = kalloc_tag(ncpus * sizeof(*kperf_thread_on_cpus),
95 VM_KERN_MEMORY_DIAG);
96 if (kperf_thread_on_cpus == NULL) {
97 err = ENOMEM;
98 goto error;
99 }
100 bzero(kperf_thread_on_cpus, ncpus * sizeof(*kperf_thread_on_cpus));
101
102 /* create the interrupt buffers */
103 intr_samplec = ncpus;
104 intr_samplev = kalloc_tag(ncpus * sizeof(*intr_samplev),
105 VM_KERN_MEMORY_DIAG);
106 if (intr_samplev == NULL) {
107 err = ENOMEM;
108 goto error;
109 }
110 bzero(intr_samplev, ncpus * sizeof(*intr_samplev));
111
112 /* create kdebug trigger filter buffers */
113 if ((err = kperf_kdebug_init())) {
114 goto error;
115 }
116
117 kperf_initted = TRUE;
118 return 0;
119
120 error:
121 if (intr_samplev) {
122 kfree(intr_samplev, ncpus * sizeof(*intr_samplev));
123 intr_samplev = NULL;
124 intr_samplec = 0;
125 }
126
127 if (kperf_thread_on_cpus) {
128 kfree(kperf_thread_on_cpus, ncpus * sizeof(*kperf_thread_on_cpus));
129 kperf_thread_on_cpus = NULL;
130 }
131
132 return err;
133 }
134
135 void
136 kperf_reset(void)
137 {
138 lck_mtx_assert(ktrace_lock, LCK_MTX_ASSERT_OWNED);
139
140 /* turn off sampling first */
141 (void)kperf_sampling_disable();
142
143 /* cleanup miscellaneous configuration first */
144 (void)kperf_kdbg_cswitch_set(0);
145 (void)kperf_set_lightweight_pet(0);
146 kperf_kdebug_reset();
147
148 /* timers, which require actions, first */
149 kperf_timer_reset();
150 kperf_action_reset();
151 }
152
153 void
154 kperf_on_cpu_internal(thread_t thread, thread_continue_t continuation,
155 uintptr_t *starting_fp)
156 {
157 if (kperf_kdebug_cswitch) {
158 /* trace the new thread's PID for Instruments */
159 int pid = task_pid(get_threadtask(thread));
160
161 BUF_DATA(PERF_TI_CSWITCH, thread_tid(thread), pid);
162 }
163 if (kperf_lightweight_pet_active) {
164 kperf_pet_on_cpu(thread, continuation, starting_fp);
165 }
166 }
167
168 void
169 kperf_on_cpu_update(void)
170 {
171 kperf_on_cpu_active = kperf_kdebug_cswitch ||
172 kperf_lightweight_pet_active;
173 }
174
175 /* random misc-ish functions */
176 uint32_t
177 kperf_get_thread_flags(thread_t thread)
178 {
179 return thread->kperf_flags;
180 }
181
182 void
183 kperf_set_thread_flags(thread_t thread, uint32_t flags)
184 {
185 thread->kperf_flags = flags;
186 }
187
188 unsigned int
189 kperf_sampling_status(void)
190 {
191 return sampling_status;
192 }
193
194 int
195 kperf_sampling_enable(void)
196 {
197 if (sampling_status == KPERF_SAMPLING_ON) {
198 return 0;
199 }
200
201 if (sampling_status != KPERF_SAMPLING_OFF) {
202 panic("kperf: sampling was %d when asked to enable", sampling_status);
203 }
204
205 /* make sure interrupt tables and actions are initted */
206 if (!kperf_initted || (kperf_action_get_count() == 0)) {
207 return ECANCELED;
208 }
209
210 /* mark as running */
211 sampling_status = KPERF_SAMPLING_ON;
212 kperf_lightweight_pet_active_update();
213
214 /* tell timers to enable */
215 kperf_timer_go();
216
217 return 0;
218 }
219
220 int
221 kperf_sampling_disable(void)
222 {
223 if (sampling_status != KPERF_SAMPLING_ON) {
224 return 0;
225 }
226
227 /* mark a shutting down */
228 sampling_status = KPERF_SAMPLING_SHUTDOWN;
229
230 /* tell timers to disable */
231 kperf_timer_stop();
232
233 /* mark as off */
234 sampling_status = KPERF_SAMPLING_OFF;
235 kperf_lightweight_pet_active_update();
236
237 return 0;
238 }
239
240 boolean_t
241 kperf_thread_get_dirty(thread_t thread)
242 {
243 return (thread->c_switch != thread->kperf_c_switch);
244 }
245
246 void
247 kperf_thread_set_dirty(thread_t thread, boolean_t dirty)
248 {
249 if (dirty) {
250 thread->kperf_c_switch = thread->c_switch - 1;
251 } else {
252 thread->kperf_c_switch = thread->c_switch;
253 }
254 }
255
256 int
257 kperf_port_to_pid(mach_port_name_t portname)
258 {
259 task_t task;
260 int pid;
261
262 if (!MACH_PORT_VALID(portname)) {
263 return -1;
264 }
265
266 task = port_name_to_task(portname);
267
268 if (task == TASK_NULL) {
269 return -1;
270 }
271
272 pid = task_pid(task);
273
274 task_deallocate_internal(task);
275
276 return pid;
277 }