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 | |
39 | unsigned int kperf_lazy_wait_action = 0; |
40 | unsigned int kperf_lazy_cpu_action = 0; |
41 | uint64_t kperf_lazy_wait_time_threshold = 0; |
42 | uint64_t kperf_lazy_cpu_time_threshold = 0; |
43 | |
44 | void |
45 | kperf_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 | |
54 | void |
55 | kperf_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, flags: 0, interrupt: 0); |
60 | } |
61 | } |
62 | |
63 | void |
64 | kperf_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), |
75 | thread->sched_pri, in_interrupt ? 1 : 0); |
76 | } |
77 | } |
78 | |
79 | void |
80 | kperf_lazy_wait_sample(thread_t thread, thread_continue_t continuation, |
81 | uintptr_t *starting_fp) |
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 runnable_time = timer_grab(timer: &thread->runnable_timer); |
96 | uint64_t running_time = recount_thread_time_mach(thread); |
97 | |
98 | BUF_DATA(PERF_LZ_WAITSAMPLE, wait_time, runnable_time, running_time); |
99 | |
100 | task_t task = get_threadtask(thread); |
101 | struct kperf_context ctx = { |
102 | .cur_thread = thread, |
103 | .cur_task = task, |
104 | .cur_pid = task_pid(task), |
105 | .trigger_type = TRIGGER_TYPE_LAZY_WAIT, |
106 | .starting_fp = starting_fp, |
107 | }; |
108 | |
109 | struct kperf_sample *sample = kperf_intr_sample_buffer(); |
110 | if (!sample) { |
111 | return; |
112 | } |
113 | |
114 | unsigned int flags = SAMPLE_FLAG_PEND_USER; |
115 | flags |= continuation ? SAMPLE_FLAG_CONTINUATION : 0; |
116 | flags |= !ml_at_interrupt_context() ? SAMPLE_FLAG_NON_INTERRUPT : 0; |
117 | |
118 | kperf_sample(sbuf: sample, ctx: &ctx, actionid: kperf_lazy_wait_action, sample_flags: flags); |
119 | } |
120 | } |
121 | |
122 | void |
123 | kperf_lazy_cpu_sample(thread_t thread, unsigned int flags, bool interrupt) |
124 | { |
125 | assert(ml_get_interrupts_enabled() == FALSE); |
126 | if (!thread) { |
127 | thread = current_thread(); |
128 | } |
129 | |
130 | /* take a sample if this CPU's last sample time is beyond the threshold */ |
131 | processor_t processor = current_processor(); |
132 | #if __arm__ || __arm64__ |
133 | uint64_t time_now = ml_get_speculative_timebase(); |
134 | #else // __arm__ || __arm64__ |
135 | uint64_t time_now = mach_absolute_time(); |
136 | #endif // !__arm__ && !__arm64__ |
137 | |
138 | uint64_t since_last_sample = time_now - processor->kperf_last_sample_time; |
139 | if (since_last_sample > kperf_lazy_cpu_time_threshold) { |
140 | processor->kperf_last_sample_time = time_now; |
141 | uint64_t runnable_time = timer_grab(timer: &thread->runnable_timer); |
142 | uint64_t running_time = recount_thread_time_mach(thread); |
143 | |
144 | BUF_DATA(PERF_LZ_CPUSAMPLE, running_time, runnable_time, |
145 | thread->sched_pri, interrupt ? 1 : 0); |
146 | |
147 | task_t task = get_threadtask(thread); |
148 | struct kperf_context ctx = { |
149 | .cur_thread = thread, |
150 | .cur_task = task, |
151 | .cur_pid = task_pid(task), |
152 | .trigger_type = TRIGGER_TYPE_LAZY_CPU, |
153 | .starting_fp = 0, |
154 | }; |
155 | |
156 | struct kperf_sample *sample = kperf_intr_sample_buffer(); |
157 | if (!sample) { |
158 | return; |
159 | } |
160 | |
161 | kperf_sample(sbuf: sample, ctx: &ctx, actionid: kperf_lazy_cpu_action, |
162 | SAMPLE_FLAG_PEND_USER | flags); |
163 | } |
164 | } |
165 | |
166 | /* |
167 | * Accessors for configuration. |
168 | */ |
169 | |
170 | int |
171 | kperf_lazy_get_wait_action(void) |
172 | { |
173 | return kperf_lazy_wait_action; |
174 | } |
175 | |
176 | int |
177 | kperf_lazy_set_wait_action(int action_id) |
178 | { |
179 | if (action_id < 0 || (unsigned int)action_id > kperf_action_get_count()) { |
180 | return 1; |
181 | } |
182 | |
183 | kperf_lazy_wait_action = action_id; |
184 | kperf_on_cpu_update(); |
185 | return 0; |
186 | } |
187 | |
188 | uint64_t |
189 | kperf_lazy_get_wait_time_threshold(void) |
190 | { |
191 | return kperf_lazy_wait_time_threshold; |
192 | } |
193 | |
194 | int |
195 | kperf_lazy_set_wait_time_threshold(uint64_t threshold) |
196 | { |
197 | kperf_lazy_wait_time_threshold = threshold; |
198 | return 0; |
199 | } |
200 | |
201 | int |
202 | kperf_lazy_get_cpu_action(void) |
203 | { |
204 | return kperf_lazy_cpu_action; |
205 | } |
206 | |
207 | int |
208 | kperf_lazy_set_cpu_action(int action_id) |
209 | { |
210 | if (action_id < 0 || (unsigned int)action_id > kperf_action_get_count()) { |
211 | return 1; |
212 | } |
213 | |
214 | kperf_lazy_cpu_action = action_id; |
215 | return 0; |
216 | } |
217 | |
218 | uint64_t |
219 | kperf_lazy_get_cpu_time_threshold(void) |
220 | { |
221 | return kperf_lazy_cpu_time_threshold; |
222 | } |
223 | |
224 | int |
225 | kperf_lazy_set_cpu_time_threshold(uint64_t threshold) |
226 | { |
227 | kperf_lazy_cpu_time_threshold = threshold; |
228 | return 0; |
229 | } |
230 | |