]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kperf/kperf.h
xnu-6153.141.1.tar.gz
[apple/xnu.git] / osfmk / kperf / kperf.h
CommitLineData
1c79356b 1/*
d9a64523 2 * Copyright (c) 2011-2018 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
d9a64523 5 *
2d21ac55
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.
d9a64523 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
d9a64523 17 *
2d21ac55
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
d9a64523 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b 28
39037602
A
29#ifndef KPERF_H
30#define KPERF_H
1c79356b 31
3e170ce0 32#include <kern/thread.h>
39037602 33#include <kern/locks.h>
3e170ce0 34
39037602 35extern lck_grp_t kperf_lck_grp;
b0d623f7 36
39037602 37/* the trigger types supported by kperf */
d9a64523
A
38#define TRIGGER_TYPE_TIMER (0)
39#define TRIGGER_TYPE_PMI (1)
40#define TRIGGER_TYPE_KDEBUG (2)
41#define TRIGGER_TYPE_LAZY_WAIT (3)
42#define TRIGGER_TYPE_LAZY_CPU (3)
1c79356b 43
94ff46dc
A
44uint32_t kperf_get_thread_ast(thread_t thread);
45void kperf_set_thread_ast(thread_t thread, uint32_t flags);
1c79356b 46
39037602
A
47/*
48 * Get and set dirtiness of thread, so kperf can track whether the thread
49 * has been dispatched since it last looked.
50 */
51boolean_t kperf_thread_get_dirty(thread_t thread);
52void kperf_thread_set_dirty(thread_t thread, boolean_t dirty);
53
54/* possible states of kperf sampling */
55#define KPERF_SAMPLING_OFF (0)
56#define KPERF_SAMPLING_ON (1)
57#define KPERF_SAMPLING_SHUTDOWN (2)
58
59/*
60 * Initialize kperf. Must be called before use and can be called multiple times.
39236c6e 61 */
316670eb 62extern int kperf_init(void);
39236c6e 63
39037602 64/* get and set sampling status */
316670eb
A
65extern unsigned kperf_sampling_status(void);
66extern int kperf_sampling_enable(void);
67extern int kperf_sampling_disable(void);
39236c6e 68
39037602
A
69/* get a per-CPU sample buffer */
70struct kperf_sample *kperf_intr_sample_buffer(void);
71
72/*
d9a64523 73 * Callbacks into kperf from other systems.
39037602 74 */
39037602
A
75
76/*
d9a64523
A
77 * kperf AST handler
78 *
79 * Prevent inlining, since the sampling function allocates on the stack and
80 * branches calling ast_taken (but never on a kperf AST) may blow their stacks.
39037602 81 */
d9a64523 82extern __attribute__((noinline)) void kperf_thread_ast_handler(thread_t thread);
39037602
A
83
84/* update whether the callback is set */
85void kperf_on_cpu_update(void);
86
d9a64523 87/* for scheduler switching threads on */
39037602
A
88static inline void
89kperf_on_cpu(thread_t thread, thread_continue_t continuation,
0a7de745 90 uintptr_t *starting_fp)
39037602 91{
d9a64523
A
92 extern boolean_t kperf_on_cpu_active;
93 void kperf_on_cpu_internal(thread_t thread, thread_continue_t continuation,
0a7de745 94 uintptr_t *starting_fp);
d9a64523 95
39037602
A
96 if (__improbable(kperf_on_cpu_active)) {
97 kperf_on_cpu_internal(thread, continuation, starting_fp);
98 }
99}
100
d9a64523
A
101/* for scheduler switching threads off */
102static inline void
103kperf_off_cpu(thread_t thread)
104{
105 extern unsigned int kperf_lazy_cpu_action;
106 void kperf_lazy_off_cpu(thread_t thread);
107
108 if (__improbable(kperf_lazy_cpu_action != 0)) {
109 kperf_lazy_off_cpu(thread);
110 }
111}
39236c6e 112
d9a64523
A
113/* for scheduler making threads runnable */
114static inline void
115kperf_make_runnable(thread_t thread, int interrupt)
116{
117 extern unsigned int kperf_lazy_cpu_action;
118 void kperf_lazy_make_runnable(thread_t thread, bool interrupt);
39037602 119
d9a64523
A
120 if (__improbable(kperf_lazy_cpu_action != 0)) {
121 kperf_lazy_make_runnable(thread, interrupt);
122 }
123}
39037602 124
d9a64523
A
125/* for interrupt handler epilogue */
126static inline void
127kperf_interrupt(void)
128{
129 extern unsigned int kperf_lazy_cpu_action;
130 extern void kperf_lazy_cpu_sample(thread_t thread, unsigned int flags,
0a7de745 131 bool interrupt);
d9a64523
A
132
133 if (__improbable(kperf_lazy_cpu_action != 0)) {
134 kperf_lazy_cpu_sample(current_thread(), 0, true);
135 }
136}
39037602 137
d9a64523 138/* for kdebug on every traced event */
39037602
A
139static inline void
140kperf_kdebug_callback(uint32_t debugid, uintptr_t *starting_fp)
141{
d9a64523
A
142 extern boolean_t kperf_kdebug_active;
143 void kperf_kdebug_handler(uint32_t debugid, uintptr_t *starting_fp);
144
39037602
A
145 if (__improbable(kperf_kdebug_active)) {
146 kperf_kdebug_handler(debugid, starting_fp);
147 }
148}
149
150/*
151 * Used by ktrace to reset kperf. ktrace_lock must be held.
39236c6e 152 */
39037602 153extern void kperf_reset(void);
39236c6e 154
5ba3f43e
A
155/*
156 * Configure kperf from the kernel (e.g. during boot).
157 */
158void kperf_kernel_configure(const char *config);
159
d9a64523
A
160/* given a task port, find out its pid */
161int kperf_port_to_pid(mach_port_name_t portname);
3e170ce0 162
39037602
A
163#if DEVELOPMENT || DEBUG
164extern _Atomic long long kperf_pending_ipis;
165#endif /* DEVELOPMENT || DEBUG */
3e170ce0 166
39037602 167#endif /* !defined(KPERF_H) */