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