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