]>
Commit | Line | Data |
---|---|---|
39236c6e A |
1 | /* |
2 | * Copyright (c) 2012 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
39236c6e 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. | |
0a7de745 | 14 | * |
39236c6e A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
39236c6e 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 | |
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. | |
0a7de745 | 25 | * |
39236c6e A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | ||
29 | #include <mach/mach_types.h> | |
30 | #include <kern/processor.h> | |
31 | #include <kern/thread.h> | |
32 | #include <kern/assert.h> | |
33 | #include <kern/locks.h> | |
34 | #include <sys/errno.h> | |
35 | ||
3e170ce0 A |
36 | #include <kperf/kperf.h> |
37 | #include <kperf/buffer.h> | |
38 | #include <kperf/context.h> | |
39 | #include <kperf/sample.h> | |
40 | #include <kperf/action.h> | |
41 | #include <kperf/kperf_kpc.h> | |
39236c6e A |
42 | #include <kern/kpc.h> |
43 | ||
5ba3f43e A |
44 | #if defined (__arm64__) |
45 | #include <arm/cpu_data_internal.h> | |
46 | #elif defined (__arm__) | |
47 | #include <arm/cpu_data_internal.h> | |
48 | #endif | |
39236c6e A |
49 | |
50 | /* global for whether to read PMCs on context switch */ | |
3e170ce0 | 51 | int kpc_threads_counting = 0; |
39236c6e | 52 | |
39037602 A |
53 | /* whether to call into KPC when a thread goes off CPU */ |
54 | boolean_t kpc_off_cpu_active = FALSE; | |
55 | ||
39236c6e A |
56 | /* current config and number of counters in that config */ |
57 | static uint32_t kpc_thread_classes = 0; | |
58 | static uint32_t kpc_thread_classes_count = 0; | |
59 | ||
60 | static lck_grp_attr_t *kpc_thread_lckgrp_attr = NULL; | |
61 | static lck_grp_t *kpc_thread_lckgrp = NULL; | |
62 | static lck_mtx_t kpc_thread_lock; | |
63 | ||
39236c6e A |
64 | void |
65 | kpc_thread_init(void) | |
66 | { | |
67 | kpc_thread_lckgrp_attr = lck_grp_attr_alloc_init(); | |
68 | kpc_thread_lckgrp = lck_grp_alloc_init("kpc", kpc_thread_lckgrp_attr); | |
69 | lck_mtx_init(&kpc_thread_lock, kpc_thread_lckgrp, LCK_ATTR_NULL); | |
70 | } | |
71 | ||
72 | uint32_t | |
73 | kpc_get_thread_counting(void) | |
74 | { | |
75 | uint32_t kpc_thread_classes_tmp; | |
76 | int kpc_threads_counting_tmp; | |
77 | ||
78 | /* Make sure we get a consistent snapshot of these values */ | |
79 | lck_mtx_lock(&kpc_thread_lock); | |
80 | ||
81 | kpc_thread_classes_tmp = kpc_thread_classes; | |
82 | kpc_threads_counting_tmp = kpc_threads_counting; | |
83 | ||
84 | lck_mtx_unlock(&kpc_thread_lock); | |
85 | ||
0a7de745 | 86 | if (kpc_threads_counting_tmp) { |
39236c6e | 87 | return kpc_thread_classes_tmp; |
0a7de745 | 88 | } else { |
39236c6e | 89 | return 0; |
0a7de745 | 90 | } |
39236c6e A |
91 | } |
92 | ||
93 | int | |
94 | kpc_set_thread_counting(uint32_t classes) | |
95 | { | |
96 | uint32_t count; | |
97 | ||
98 | lck_mtx_lock(&kpc_thread_lock); | |
99 | ||
100 | count = kpc_get_counter_count(classes); | |
101 | ||
0a7de745 A |
102 | if ((classes == 0) |
103 | || (count == 0)) { | |
39236c6e A |
104 | /* shut down */ |
105 | kpc_threads_counting = FALSE; | |
0a7de745 | 106 | } else { |
39236c6e A |
107 | /* stash the config */ |
108 | kpc_thread_classes = classes; | |
109 | ||
110 | /* work out the size */ | |
111 | kpc_thread_classes_count = count; | |
112 | assert(kpc_thread_classes_count <= KPC_MAX_COUNTERS); | |
113 | ||
114 | /* enable switch */ | |
115 | kpc_threads_counting = TRUE; | |
116 | ||
117 | /* and schedule an AST for this thread... */ | |
0a7de745 | 118 | if (!current_thread()->kpc_buf) { |
94ff46dc | 119 | current_thread()->kperf_ast |= T_KPC_ALLOC; |
39236c6e | 120 | act_set_kperf(current_thread()); |
39037602 | 121 | } |
39236c6e A |
122 | } |
123 | ||
0a7de745 | 124 | kpc_off_cpu_update(); |
39236c6e A |
125 | lck_mtx_unlock(&kpc_thread_lock); |
126 | ||
127 | return 0; | |
128 | } | |
129 | ||
130 | /* snapshot current PMCs and update counters in the current thread */ | |
131 | static void | |
132 | kpc_update_thread_counters( thread_t thread ) | |
133 | { | |
134 | uint32_t i; | |
135 | uint64_t *tmp = NULL; | |
136 | cpu_data_t *cpu = NULL; | |
137 | ||
39236c6e | 138 | cpu = current_cpu_datap(); |
39236c6e A |
139 | |
140 | /* 1. stash current PMCs into latest CPU block */ | |
0a7de745 A |
141 | kpc_get_cpu_counters( FALSE, kpc_thread_classes, |
142 | NULL, cpu->cpu_kpc_buf[1] ); | |
39236c6e A |
143 | |
144 | /* 2. apply delta to old thread */ | |
0a7de745 A |
145 | if (thread->kpc_buf) { |
146 | for (i = 0; i < kpc_thread_classes_count; i++) { | |
39236c6e | 147 | thread->kpc_buf[i] += cpu->cpu_kpc_buf[1][i] - cpu->cpu_kpc_buf[0][i]; |
0a7de745 A |
148 | } |
149 | } | |
39236c6e | 150 | |
39236c6e | 151 | /* schedule any necessary allocations */ |
0a7de745 | 152 | if (!current_thread()->kpc_buf) { |
94ff46dc | 153 | current_thread()->kperf_ast |= T_KPC_ALLOC; |
39236c6e | 154 | act_set_kperf(current_thread()); |
39037602 | 155 | } |
39236c6e A |
156 | |
157 | /* 3. switch the PMC block pointers */ | |
158 | tmp = cpu->cpu_kpc_buf[1]; | |
159 | cpu->cpu_kpc_buf[1] = cpu->cpu_kpc_buf[0]; | |
160 | cpu->cpu_kpc_buf[0] = tmp; | |
161 | } | |
162 | ||
39236c6e A |
163 | /* get counter values for a thread */ |
164 | int | |
165 | kpc_get_curthread_counters(uint32_t *inoutcount, uint64_t *buf) | |
166 | { | |
167 | thread_t thread = current_thread(); | |
168 | boolean_t enabled; | |
169 | ||
170 | /* buffer too small :( */ | |
0a7de745 | 171 | if (*inoutcount < kpc_thread_classes_count) { |
39236c6e | 172 | return EINVAL; |
0a7de745 | 173 | } |
39236c6e A |
174 | |
175 | /* copy data and actual size */ | |
0a7de745 | 176 | if (!thread->kpc_buf) { |
39236c6e | 177 | return EINVAL; |
0a7de745 | 178 | } |
39236c6e A |
179 | |
180 | enabled = ml_set_interrupts_enabled(FALSE); | |
181 | ||
182 | /* snap latest version of counters for this thread */ | |
0a7de745 A |
183 | kpc_update_thread_counters( current_thread()); |
184 | ||
39236c6e | 185 | /* copy out */ |
0a7de745 A |
186 | memcpy( buf, thread->kpc_buf, |
187 | kpc_thread_classes_count * sizeof(*buf)); | |
39236c6e A |
188 | *inoutcount = kpc_thread_classes_count; |
189 | ||
190 | ml_set_interrupts_enabled(enabled); | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
39037602 A |
195 | void |
196 | kpc_off_cpu_update(void) | |
197 | { | |
198 | kpc_off_cpu_active = kpc_threads_counting; | |
199 | } | |
200 | ||
201 | void | |
202 | kpc_off_cpu_internal(thread_t thread) | |
203 | { | |
204 | if (kpc_threads_counting) { | |
205 | kpc_update_thread_counters(thread); | |
206 | } | |
207 | } | |
39236c6e A |
208 | |
209 | void | |
210 | kpc_thread_create(thread_t thread) | |
211 | { | |
212 | /* nothing to do if we're not counting */ | |
0a7de745 | 213 | if (!kpc_threads_counting) { |
39236c6e | 214 | return; |
0a7de745 | 215 | } |
39236c6e A |
216 | |
217 | /* give the new thread a counterbuf */ | |
218 | thread->kpc_buf = kpc_counterbuf_alloc(); | |
219 | } | |
220 | ||
221 | void | |
222 | kpc_thread_destroy(thread_t thread) | |
223 | { | |
224 | uint64_t *buf = NULL; | |
225 | ||
226 | /* usual case: no kpc buf, just return */ | |
0a7de745 | 227 | if (!thread->kpc_buf) { |
39236c6e | 228 | return; |
0a7de745 | 229 | } |
39236c6e A |
230 | |
231 | /* otherwise, don't leak */ | |
232 | buf = thread->kpc_buf; | |
233 | thread->kpc_buf = NULL; | |
234 | kpc_counterbuf_free(buf); | |
235 | } | |
236 | ||
39236c6e | 237 | void |
94ff46dc | 238 | kpc_thread_ast_handler(thread_t thread) |
39236c6e | 239 | { |
94ff46dc | 240 | if (thread->kperf_ast & T_KPC_ALLOC) { |
39236c6e | 241 | thread->kpc_buf = kpc_counterbuf_alloc(); |
0a7de745 | 242 | } |
39236c6e | 243 | } |