]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kperf/lazy.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / kperf / lazy.c
CommitLineData
d9a64523
A
1/*
2 * Copyright (c) 2018 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#include <stdint.h>
30
31#include <kern/thread.h>
32
33#include <kperf/action.h>
34#include <kperf/buffer.h>
35#include <kperf/kperf.h>
36#include <kperf/lazy.h>
37#include <kperf/sample.h>
38
39unsigned int kperf_lazy_wait_action = 0;
40unsigned int kperf_lazy_cpu_action = 0;
41uint64_t kperf_lazy_wait_time_threshold = 0;
42uint64_t kperf_lazy_cpu_time_threshold = 0;
43
44void
45kperf_lazy_reset(void)
46{
47 kperf_lazy_wait_action = 0;
48 kperf_lazy_wait_time_threshold = 0;
49 kperf_lazy_cpu_action = 0;
50 kperf_lazy_cpu_time_threshold = 0;
51 kperf_on_cpu_update();
52}
53
54void
55kperf_lazy_off_cpu(thread_t thread)
56{
57 /* try to lazily sample the CPU if the thread was pre-empted */
58 if ((thread->reason & AST_SCHEDULING) != 0) {
59 kperf_lazy_cpu_sample(thread, 0, 0);
0a7de745 60 }
d9a64523
A
61}
62
63void
64kperf_lazy_make_runnable(thread_t thread, bool in_interrupt)
65{
66 assert(thread->last_made_runnable_time != THREAD_NOT_RUNNABLE);
67 /* ignore threads that race to wait and in waking up */
68 if (thread->last_run_time > thread->last_made_runnable_time) {
69 return;
70 }
71
72 uint64_t wait_time = thread_get_last_wait_duration(thread);
73 if (wait_time > kperf_lazy_wait_time_threshold) {
74 BUF_DATA(PERF_LZ_MKRUNNABLE, (uintptr_t)thread_tid(thread),
0a7de745 75 thread->sched_pri, in_interrupt ? 1 : 0);
d9a64523
A
76 }
77}
78
79void
80kperf_lazy_wait_sample(thread_t thread, thread_continue_t continuation,
0a7de745 81 uintptr_t *starting_fp)
d9a64523
A
82{
83 /* ignore idle threads */
84 if (thread->last_made_runnable_time == THREAD_NOT_RUNNABLE) {
85 return;
86 }
87 /* ignore invalid made runnable times */
88 if (thread->last_made_runnable_time < thread->last_run_time) {
89 return;
90 }
91
92 /* take a sample if thread was waiting for longer than threshold */
93 uint64_t wait_time = thread_get_last_wait_duration(thread);
94 if (wait_time > kperf_lazy_wait_time_threshold) {
95 uint64_t time_now = mach_absolute_time();
96 timer_update(&thread->runnable_timer, time_now);
97 timer_update(&thread->system_timer, time_now);
98
99 uint64_t runnable_time = timer_grab(&thread->runnable_timer);
100 uint64_t running_time = timer_grab(&thread->user_timer) +
0a7de745 101 timer_grab(&thread->system_timer);
d9a64523
A
102
103 BUF_DATA(PERF_LZ_WAITSAMPLE, wait_time, runnable_time, running_time);
104
105 task_t task = get_threadtask(thread);
106 struct kperf_context ctx = {
107 .cur_thread = thread,
108 .cur_task = task,
109 .cur_pid = task_pid(task),
110 .trigger_type = TRIGGER_TYPE_LAZY_WAIT,
111 .starting_fp = starting_fp,
112 };
113
114 struct kperf_sample *sample = kperf_intr_sample_buffer();
115 if (!sample) {
116 return;
117 }
118
119 unsigned int flags = SAMPLE_FLAG_PEND_USER;
120 flags |= continuation ? SAMPLE_FLAG_CONTINUATION : 0;
121 flags |= !ml_at_interrupt_context() ? SAMPLE_FLAG_NON_INTERRUPT : 0;
122
123 kperf_sample(sample, &ctx, kperf_lazy_wait_action, flags);
124 }
125}
126
127void
128kperf_lazy_cpu_sample(thread_t thread, unsigned int flags, bool interrupt)
129{
130 assert(ml_get_interrupts_enabled() == FALSE);
131
132 /* take a sample if this CPU's last sample time is beyond the threshold */
133 processor_t processor = current_processor();
134 uint64_t time_now = mach_absolute_time();
135 uint64_t since_last_sample = time_now - processor->kperf_last_sample_time;
136 if (since_last_sample > kperf_lazy_cpu_time_threshold) {
137 processor->kperf_last_sample_time = time_now;
138 timer_update(&thread->runnable_timer, time_now);
139 timer_update(&thread->system_timer, time_now);
140
141 uint64_t runnable_time = timer_grab(&thread->runnable_timer);
142 uint64_t running_time = timer_grab(&thread->user_timer) +
0a7de745 143 timer_grab(&thread->system_timer);
d9a64523
A
144
145 BUF_DATA(PERF_LZ_CPUSAMPLE, running_time, runnable_time,
0a7de745 146 thread->sched_pri, interrupt ? 1 : 0);
d9a64523
A
147
148 task_t task = get_threadtask(thread);
149 struct kperf_context ctx = {
150 .cur_thread = thread,
151 .cur_task = task,
152 .cur_pid = task_pid(task),
153 .trigger_type = TRIGGER_TYPE_LAZY_CPU,
154 .starting_fp = 0,
155 };
156
157 struct kperf_sample *sample = kperf_intr_sample_buffer();
158 if (!sample) {
159 return;
160 }
161
162 kperf_sample(sample, &ctx, kperf_lazy_cpu_action,
0a7de745 163 SAMPLE_FLAG_PEND_USER | flags);
d9a64523
A
164 }
165}
166
167/*
168 * Accessors for configuration.
169 */
170
0a7de745
A
171int
172kperf_lazy_get_wait_action(void)
173{
174 return kperf_lazy_wait_action;
175}
d9a64523
A
176
177int
178kperf_lazy_set_wait_action(int action_id)
179{
180 if (action_id < 0 || (unsigned int)action_id > kperf_action_get_count()) {
181 return 1;
182 }
183
184 kperf_lazy_wait_action = action_id;
185 kperf_on_cpu_update();
186 return 0;
187}
188
189uint64_t
190kperf_lazy_get_wait_time_threshold(void)
191{
192 return kperf_lazy_wait_time_threshold;
193}
194
195int
196kperf_lazy_set_wait_time_threshold(uint64_t threshold)
197{
198 kperf_lazy_wait_time_threshold = threshold;
199 return 0;
200}
201
0a7de745
A
202int
203kperf_lazy_get_cpu_action(void)
204{
205 return kperf_lazy_cpu_action;
206}
d9a64523
A
207
208int
209kperf_lazy_set_cpu_action(int action_id)
210{
211 if (action_id < 0 || (unsigned int)action_id > kperf_action_get_count()) {
212 return 1;
213 }
214
215 kperf_lazy_cpu_action = action_id;
216 return 0;
217}
218
219uint64_t
220kperf_lazy_get_cpu_time_threshold(void)
221{
222 return kperf_lazy_cpu_time_threshold;
223}
224
225int
226kperf_lazy_set_cpu_time_threshold(uint64_t threshold)
227{
228 kperf_lazy_cpu_time_threshold = threshold;
229 return 0;
230}