]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kperf/kperf_kpc.c
43df937a282071449f217d04c792e2fdf33bcd0b
[apple/xnu.git] / osfmk / kperf / kperf_kpc.c
1 /*
2 * Copyright (c) 2013 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 /*
30 * Sample KPC data into kperf and manage shared context-switch and AST handlers
31 */
32
33 #include <kperf/kperf.h>
34 #include <kperf/buffer.h>
35 #include <kperf/context.h>
36 #include <kperf/pet.h>
37 #include <kperf/kperf_kpc.h>
38 #include <kern/kpc.h> /* kpc_cswitch_context, kpc_threads_counting */
39
40 void
41 kperf_kpc_thread_ast(thread_t thread)
42 {
43 kpc_thread_ast_handler(thread);
44 kperf_thread_ast_handler(thread);
45
46 thread->kperf_flags = 0;
47 }
48
49 void
50 kperf_kpc_thread_sample(struct kpcdata *kpcd, int sample_config)
51 {
52 BUF_INFO(PERF_KPC_THREAD_SAMPLE | DBG_FUNC_START, sample_config);
53
54 kpcd->running = kpc_get_running();
55 /* let kpc_get_curthread_counters set the correct count */
56 kpcd->counterc = KPC_MAX_COUNTERS;
57 if (kpc_get_curthread_counters(&kpcd->counterc,
58 kpcd->counterv)) {
59 /* if thread counters aren't ready, default to 0 */
60 memset(kpcd->counterv, 0,
61 sizeof(uint64_t) * kpcd->counterc);
62 }
63 /* help out Instruments by sampling KPC's config */
64 if (!sample_config) {
65 kpcd->configc = 0;
66 } else {
67 kpcd->configc = kpc_get_config_count(kpcd->running);
68 kpc_get_config(kpcd->running, kpcd->configv);
69 }
70
71 BUF_INFO(PERF_KPC_THREAD_SAMPLE | DBG_FUNC_END, kpcd->running, kpcd->counterc);
72 }
73
74 void
75 kperf_kpc_cpu_sample(struct kpcdata *kpcd, int sample_config)
76 {
77 BUF_INFO(PERF_KPC_CPU_SAMPLE | DBG_FUNC_START, sample_config);
78
79 kpcd->running = kpc_get_running();
80 kpcd->counterc = kpc_get_cpu_counters(0, kpcd->running,
81 &kpcd->curcpu,
82 kpcd->counterv);
83 if (!sample_config) {
84 kpcd->configc = 0;
85 } else {
86 kpcd->configc = kpc_get_config_count(kpcd->running);
87 kpc_get_config(kpcd->running, kpcd->configv);
88 }
89
90 BUF_INFO(PERF_KPC_CPU_SAMPLE | DBG_FUNC_END, kpcd->running, kpcd->counterc);
91 }
92
93 void
94 kperf_kpc_config_log(const struct kpcdata *kpcd)
95 {
96 BUF_DATA(PERF_KPC_CONFIG,
97 kpcd->running,
98 kpcd->counterc,
99 kpc_get_counter_count(KPC_CLASS_FIXED_MASK),
100 kpcd->configc);
101
102 #if __LP64__
103 unsigned int max = (kpcd->configc + 3) / 4;
104 for (unsigned int i = 0; i < max; i++) {
105 uint32_t flag = (i == 0) ? DBG_FUNC_START : ((i == (max - 1)) ? DBG_FUNC_END : DBG_FUNC_NONE);
106 BUF_DATA(PERF_KPC_CFG_REG | flag,
107 kpcd->configv[0 + i * 4], kpcd->configv[1 + i * 4],
108 kpcd->configv[2 + i * 4], kpcd->configv[3 + i * 4]);
109 }
110 #else /* __LP64__ */
111 unsigned int max = (kpcd->configc + 1) / 2;
112 for (unsigned int i = 0; i < max; i++) {
113 uint32_t flag = (i == 0) ? DBG_FUNC_START : ((i == (max - 1)) ? DBG_FUNC_END : DBG_FUNC_NONE);
114 BUF_DATA(PERF_KPC_CFG_REG32 | flag,
115 kpcd->configv[i * 2] >> 32ULL,
116 kpcd->configv[i * 2] & 0xffffffffULL,
117 kpcd->configv[i * 2 + 1] >> 32ULL,
118 kpcd->configv[i * 2 + 1] & 0xffffffffULL);
119 }
120 #endif /* !__LP64__ */
121 }
122
123 static void
124 kperf_kpc_log(uint32_t code, uint32_t code32, const struct kpcdata *kpcd)
125 {
126 #if __LP64__
127 #pragma unused(code32)
128 unsigned int max = (kpcd->counterc + 3) / 4;
129 /* and the actual counts with one 64-bit argument each */
130 for (unsigned int i = 0; i < max; i++) {
131 uint32_t flag = (i == 0) ? DBG_FUNC_START : ((i == (max - 1)) ? DBG_FUNC_END : DBG_FUNC_NONE);
132 BUF_DATA(code | flag,
133 kpcd->counterv[0 + i * 4],
134 kpcd->counterv[1 + i * 4],
135 kpcd->counterv[2 + i * 4],
136 kpcd->counterv[3 + i * 4]);
137 }
138 #else /* __LP64__ */
139 #pragma unused(code)
140 unsigned int max = (kpcd->counterc + 1) / 2;
141 /* and the actual counts with two 32-bit trace arguments each */
142 for (unsigned int i = 0; i < max; i++) {
143 uint32_t flag = (i == 0) ? DBG_FUNC_START : ((i == (max - 1)) ? DBG_FUNC_END : DBG_FUNC_NONE);
144 BUF_DATA(code32 | flag,
145 (kpcd->counterv[0 + i * 2] >> 32ULL),
146 kpcd->counterv[0 + i * 2] & 0xffffffffULL,
147 (kpcd->counterv[1 + i * 2] >> 32ULL),
148 kpcd->counterv[1 + i * 2] & 0xffffffffULL);
149 }
150 #endif /* !__LP64__ */
151 }
152
153 void
154 kperf_kpc_cpu_log(const struct kpcdata *kpcd)
155 {
156 kperf_kpc_log(PERF_KPC_DATA, PERF_KPC_DATA32, kpcd);
157 }
158
159 void
160 kperf_kpc_thread_log(const struct kpcdata *kpcd)
161 {
162 kperf_kpc_log(PERF_KPC_DATA_THREAD, PERF_KPC_DATA_THREAD32, kpcd);
163 }