]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/kpc_thread.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / kern / kpc_thread.c
CommitLineData
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 51int kpc_threads_counting = 0;
39236c6e 52
39037602
A
53/* whether to call into KPC when a thread goes off CPU */
54boolean_t kpc_off_cpu_active = FALSE;
55
39236c6e
A
56/* current config and number of counters in that config */
57static uint32_t kpc_thread_classes = 0;
58static uint32_t kpc_thread_classes_count = 0;
59
f427ee49
A
60static LCK_GRP_DECLARE(kpc_thread_lckgrp, "kpc thread");
61static LCK_MTX_DECLARE(kpc_thread_lock, &kpc_thread_lckgrp);
39236c6e
A
62
63uint32_t
64kpc_get_thread_counting(void)
65{
66 uint32_t kpc_thread_classes_tmp;
67 int kpc_threads_counting_tmp;
68
69 /* Make sure we get a consistent snapshot of these values */
70 lck_mtx_lock(&kpc_thread_lock);
71
72 kpc_thread_classes_tmp = kpc_thread_classes;
73 kpc_threads_counting_tmp = kpc_threads_counting;
74
75 lck_mtx_unlock(&kpc_thread_lock);
76
0a7de745 77 if (kpc_threads_counting_tmp) {
39236c6e 78 return kpc_thread_classes_tmp;
0a7de745 79 } else {
39236c6e 80 return 0;
0a7de745 81 }
39236c6e
A
82}
83
84int
85kpc_set_thread_counting(uint32_t classes)
86{
87 uint32_t count;
88
89 lck_mtx_lock(&kpc_thread_lock);
90
91 count = kpc_get_counter_count(classes);
92
0a7de745
A
93 if ((classes == 0)
94 || (count == 0)) {
39236c6e
A
95 /* shut down */
96 kpc_threads_counting = FALSE;
0a7de745 97 } else {
39236c6e
A
98 /* stash the config */
99 kpc_thread_classes = classes;
100
101 /* work out the size */
102 kpc_thread_classes_count = count;
103 assert(kpc_thread_classes_count <= KPC_MAX_COUNTERS);
104
105 /* enable switch */
106 kpc_threads_counting = TRUE;
107
108 /* and schedule an AST for this thread... */
0a7de745 109 if (!current_thread()->kpc_buf) {
94ff46dc 110 current_thread()->kperf_ast |= T_KPC_ALLOC;
39236c6e 111 act_set_kperf(current_thread());
39037602 112 }
39236c6e
A
113 }
114
0a7de745 115 kpc_off_cpu_update();
39236c6e
A
116 lck_mtx_unlock(&kpc_thread_lock);
117
118 return 0;
119}
120
121/* snapshot current PMCs and update counters in the current thread */
122static void
123kpc_update_thread_counters( thread_t thread )
124{
125 uint32_t i;
126 uint64_t *tmp = NULL;
127 cpu_data_t *cpu = NULL;
128
39236c6e 129 cpu = current_cpu_datap();
39236c6e
A
130
131 /* 1. stash current PMCs into latest CPU block */
0a7de745
A
132 kpc_get_cpu_counters( FALSE, kpc_thread_classes,
133 NULL, cpu->cpu_kpc_buf[1] );
39236c6e
A
134
135 /* 2. apply delta to old thread */
0a7de745
A
136 if (thread->kpc_buf) {
137 for (i = 0; i < kpc_thread_classes_count; i++) {
39236c6e 138 thread->kpc_buf[i] += cpu->cpu_kpc_buf[1][i] - cpu->cpu_kpc_buf[0][i];
0a7de745
A
139 }
140 }
39236c6e 141
39236c6e 142 /* schedule any necessary allocations */
0a7de745 143 if (!current_thread()->kpc_buf) {
94ff46dc 144 current_thread()->kperf_ast |= T_KPC_ALLOC;
39236c6e 145 act_set_kperf(current_thread());
39037602 146 }
39236c6e
A
147
148 /* 3. switch the PMC block pointers */
149 tmp = cpu->cpu_kpc_buf[1];
150 cpu->cpu_kpc_buf[1] = cpu->cpu_kpc_buf[0];
151 cpu->cpu_kpc_buf[0] = tmp;
152}
153
39236c6e
A
154/* get counter values for a thread */
155int
156kpc_get_curthread_counters(uint32_t *inoutcount, uint64_t *buf)
157{
158 thread_t thread = current_thread();
159 boolean_t enabled;
160
161 /* buffer too small :( */
0a7de745 162 if (*inoutcount < kpc_thread_classes_count) {
39236c6e 163 return EINVAL;
0a7de745 164 }
39236c6e
A
165
166 /* copy data and actual size */
0a7de745 167 if (!thread->kpc_buf) {
39236c6e 168 return EINVAL;
0a7de745 169 }
39236c6e
A
170
171 enabled = ml_set_interrupts_enabled(FALSE);
172
173 /* snap latest version of counters for this thread */
0a7de745
A
174 kpc_update_thread_counters( current_thread());
175
39236c6e 176 /* copy out */
0a7de745
A
177 memcpy( buf, thread->kpc_buf,
178 kpc_thread_classes_count * sizeof(*buf));
39236c6e
A
179 *inoutcount = kpc_thread_classes_count;
180
181 ml_set_interrupts_enabled(enabled);
182
183 return 0;
184}
185
39037602
A
186void
187kpc_off_cpu_update(void)
188{
189 kpc_off_cpu_active = kpc_threads_counting;
190}
191
192void
193kpc_off_cpu_internal(thread_t thread)
194{
195 if (kpc_threads_counting) {
196 kpc_update_thread_counters(thread);
197 }
198}
39236c6e
A
199
200void
201kpc_thread_create(thread_t thread)
202{
203 /* nothing to do if we're not counting */
0a7de745 204 if (!kpc_threads_counting) {
39236c6e 205 return;
0a7de745 206 }
39236c6e
A
207
208 /* give the new thread a counterbuf */
209 thread->kpc_buf = kpc_counterbuf_alloc();
210}
211
212void
213kpc_thread_destroy(thread_t thread)
214{
215 uint64_t *buf = NULL;
216
217 /* usual case: no kpc buf, just return */
0a7de745 218 if (!thread->kpc_buf) {
39236c6e 219 return;
0a7de745 220 }
39236c6e
A
221
222 /* otherwise, don't leak */
223 buf = thread->kpc_buf;
224 thread->kpc_buf = NULL;
225 kpc_counterbuf_free(buf);
226}
227
39236c6e 228void
94ff46dc 229kpc_thread_ast_handler(thread_t thread)
39236c6e 230{
94ff46dc 231 if (thread->kperf_ast & T_KPC_ALLOC) {
39236c6e 232 thread->kpc_buf = kpc_counterbuf_alloc();
0a7de745 233 }
39236c6e 234}