|
1 /* |
|
2 * Copyright (c) 2010, 2011 Oracle and/or its affiliates. All rights reserved. |
|
3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
|
4 */ |
|
5 |
|
6 #include "precompiled.hpp" |
|
7 #include "runtime/advancedThresholdPolicy.hpp" |
|
8 #include "runtime/simpleThresholdPolicy.inline.hpp" |
|
9 |
|
10 #ifdef TIERED |
|
11 // Print an event. |
|
12 void AdvancedThresholdPolicy::print_specific(EventType type, methodHandle mh, methodHandle imh, |
|
13 int bci, CompLevel level) { |
|
14 tty->print(" rate: "); |
|
15 if (mh->prev_time() == 0) tty->print("n/a"); |
|
16 else tty->print("%f", mh->rate()); |
|
17 |
|
18 tty->print(" k: %.2lf,%.2lf", threshold_scale(CompLevel_full_profile, Tier3LoadFeedback), |
|
19 threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback)); |
|
20 |
|
21 } |
|
22 |
|
23 void AdvancedThresholdPolicy::initialize() { |
|
24 // Turn on ergonomic compiler count selection |
|
25 if (FLAG_IS_DEFAULT(CICompilerCountPerCPU) && FLAG_IS_DEFAULT(CICompilerCount)) { |
|
26 FLAG_SET_DEFAULT(CICompilerCountPerCPU, true); |
|
27 } |
|
28 int count = CICompilerCount; |
|
29 if (CICompilerCountPerCPU) { |
|
30 // Simple log n seems to grow too slowly for tiered, try something faster: log n * log log n |
|
31 int log_cpu = log2_intptr(os::active_processor_count()); |
|
32 int loglog_cpu = log2_intptr(MAX2(log_cpu, 1)); |
|
33 count = MAX2(log_cpu * loglog_cpu, 1) * 3 / 2; |
|
34 } |
|
35 |
|
36 set_c1_count(MAX2(count / 3, 1)); |
|
37 set_c2_count(MAX2(count - count / 3, 1)); |
|
38 |
|
39 // Some inlining tuning |
|
40 #ifdef X86 |
|
41 if (FLAG_IS_DEFAULT(InlineSmallCode)) { |
|
42 FLAG_SET_DEFAULT(InlineSmallCode, 2000); |
|
43 } |
|
44 #endif |
|
45 |
|
46 #ifdef SPARC |
|
47 if (FLAG_IS_DEFAULT(InlineSmallCode)) { |
|
48 FLAG_SET_DEFAULT(InlineSmallCode, 2500); |
|
49 } |
|
50 #endif |
|
51 |
|
52 |
|
53 set_start_time(os::javaTimeMillis()); |
|
54 } |
|
55 |
|
56 // update_rate() is called from select_task() while holding a compile queue lock. |
|
57 void AdvancedThresholdPolicy::update_rate(jlong t, methodOop m) { |
|
58 if (is_old(m)) { |
|
59 // We don't remove old methods from the queue, |
|
60 // so we can just zero the rate. |
|
61 m->set_rate(0); |
|
62 return; |
|
63 } |
|
64 |
|
65 // We don't update the rate if we've just came out of a safepoint. |
|
66 // delta_s is the time since last safepoint in milliseconds. |
|
67 jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint(); |
|
68 jlong delta_t = t - (m->prev_time() != 0 ? m->prev_time() : start_time()); // milliseconds since the last measurement |
|
69 // How many events were there since the last time? |
|
70 int event_count = m->invocation_count() + m->backedge_count(); |
|
71 int delta_e = event_count - m->prev_event_count(); |
|
72 |
|
73 // We should be running for at least 1ms. |
|
74 if (delta_s >= TieredRateUpdateMinTime) { |
|
75 // And we must've taken the previous point at least 1ms before. |
|
76 if (delta_t >= TieredRateUpdateMinTime && delta_e > 0) { |
|
77 m->set_prev_time(t); |
|
78 m->set_prev_event_count(event_count); |
|
79 m->set_rate((float)delta_e / (float)delta_t); // Rate is events per millisecond |
|
80 } else |
|
81 if (delta_t > TieredRateUpdateMaxTime && delta_e == 0) { |
|
82 // If nothing happened for 25ms, zero the rate. Don't modify prev values. |
|
83 m->set_rate(0); |
|
84 } |
|
85 } |
|
86 } |
|
87 |
|
88 // Check if this method has been stale from a given number of milliseconds. |
|
89 // See select_task(). |
|
90 bool AdvancedThresholdPolicy::is_stale(jlong t, jlong timeout, methodOop m) { |
|
91 jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint(); |
|
92 jlong delta_t = t - m->prev_time(); |
|
93 if (delta_t > timeout && delta_s > timeout) { |
|
94 int event_count = m->invocation_count() + m->backedge_count(); |
|
95 int delta_e = event_count - m->prev_event_count(); |
|
96 // Return true if there were no events. |
|
97 return delta_e == 0; |
|
98 } |
|
99 return false; |
|
100 } |
|
101 |
|
102 // We don't remove old methods from the compile queue even if they have |
|
103 // very low activity. See select_task(). |
|
104 bool AdvancedThresholdPolicy::is_old(methodOop method) { |
|
105 return method->invocation_count() > 50000 || method->backedge_count() > 500000; |
|
106 } |
|
107 |
|
108 double AdvancedThresholdPolicy::weight(methodOop method) { |
|
109 return (method->rate() + 1) * ((method->invocation_count() + 1) * (method->backedge_count() + 1)); |
|
110 } |
|
111 |
|
112 // Apply heuristics and return true if x should be compiled before y |
|
113 bool AdvancedThresholdPolicy::compare_methods(methodOop x, methodOop y) { |
|
114 if (x->highest_comp_level() > y->highest_comp_level()) { |
|
115 // recompilation after deopt |
|
116 return true; |
|
117 } else |
|
118 if (x->highest_comp_level() == y->highest_comp_level()) { |
|
119 if (weight(x) > weight(y)) { |
|
120 return true; |
|
121 } |
|
122 } |
|
123 return false; |
|
124 } |
|
125 |
|
126 // Is method profiled enough? |
|
127 bool AdvancedThresholdPolicy::is_method_profiled(methodOop method) { |
|
128 methodDataOop mdo = method->method_data(); |
|
129 if (mdo != NULL) { |
|
130 int i = mdo->invocation_count_delta(); |
|
131 int b = mdo->backedge_count_delta(); |
|
132 return call_predicate_helper<CompLevel_full_profile>(i, b, 1); |
|
133 } |
|
134 return false; |
|
135 } |
|
136 |
|
137 // Called with the queue locked and with at least one element |
|
138 CompileTask* AdvancedThresholdPolicy::select_task(CompileQueue* compile_queue) { |
|
139 CompileTask *max_task = NULL; |
|
140 methodOop max_method; |
|
141 jlong t = os::javaTimeMillis(); |
|
142 // Iterate through the queue and find a method with a maximum rate. |
|
143 for (CompileTask* task = compile_queue->first(); task != NULL;) { |
|
144 CompileTask* next_task = task->next(); |
|
145 methodOop method = (methodOop)JNIHandles::resolve(task->method_handle()); |
|
146 methodDataOop mdo = method->method_data(); |
|
147 update_rate(t, method); |
|
148 if (max_task == NULL) { |
|
149 max_task = task; |
|
150 max_method = method; |
|
151 } else { |
|
152 // If a method has been stale for some time, remove it from the queue. |
|
153 if (is_stale(t, TieredCompileTaskTimeout, method) && !is_old(method)) { |
|
154 if (PrintTieredEvents) { |
|
155 print_event(KILL, method, method, task->osr_bci(), (CompLevel)task->comp_level()); |
|
156 } |
|
157 CompileTaskWrapper ctw(task); // Frees the task |
|
158 compile_queue->remove(task); |
|
159 method->clear_queued_for_compilation(); |
|
160 task = next_task; |
|
161 continue; |
|
162 } |
|
163 |
|
164 // Select a method with a higher rate |
|
165 if (compare_methods(method, max_method)) { |
|
166 max_task = task; |
|
167 max_method = method; |
|
168 } |
|
169 } |
|
170 task = next_task; |
|
171 } |
|
172 |
|
173 if (max_task->comp_level() == CompLevel_full_profile && is_method_profiled(max_method)) { |
|
174 max_task->set_comp_level(CompLevel_limited_profile); |
|
175 if (PrintTieredEvents) { |
|
176 print_event(UPDATE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level()); |
|
177 } |
|
178 } |
|
179 |
|
180 return max_task; |
|
181 } |
|
182 |
|
183 double AdvancedThresholdPolicy::threshold_scale(CompLevel level, int feedback_k) { |
|
184 double queue_size = CompileBroker::queue_size(level); |
|
185 int comp_count = compiler_count(level); |
|
186 double k = queue_size / (feedback_k * comp_count) + 1; |
|
187 return k; |
|
188 } |
|
189 |
|
190 // Call and loop predicates determine whether a transition to a higher |
|
191 // compilation level should be performed (pointers to predicate functions |
|
192 // are passed to common()). |
|
193 // Tier?LoadFeedback is basically a coefficient that determines of |
|
194 // how many methods per compiler thread can be in the queue before |
|
195 // the threshold values double. |
|
196 bool AdvancedThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level) { |
|
197 switch(cur_level) { |
|
198 case CompLevel_none: |
|
199 case CompLevel_limited_profile: { |
|
200 double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback); |
|
201 return loop_predicate_helper<CompLevel_none>(i, b, k); |
|
202 } |
|
203 case CompLevel_full_profile: { |
|
204 double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback); |
|
205 return loop_predicate_helper<CompLevel_full_profile>(i, b, k); |
|
206 } |
|
207 default: |
|
208 return true; |
|
209 } |
|
210 } |
|
211 |
|
212 bool AdvancedThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level) { |
|
213 switch(cur_level) { |
|
214 case CompLevel_none: |
|
215 case CompLevel_limited_profile: { |
|
216 double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback); |
|
217 return call_predicate_helper<CompLevel_none>(i, b, k); |
|
218 } |
|
219 case CompLevel_full_profile: { |
|
220 double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback); |
|
221 return call_predicate_helper<CompLevel_full_profile>(i, b, k); |
|
222 } |
|
223 default: |
|
224 return true; |
|
225 } |
|
226 } |
|
227 |
|
228 // If a method is old enough and is still in the interpreter we would want to |
|
229 // start profiling without waiting for the compiled method to arrive. |
|
230 // We also take the load on compilers into the account. |
|
231 bool AdvancedThresholdPolicy::should_create_mdo(methodOop method, CompLevel cur_level) { |
|
232 if (cur_level == CompLevel_none && |
|
233 CompileBroker::queue_size(CompLevel_full_optimization) <= |
|
234 Tier3DelayOn * compiler_count(CompLevel_full_optimization)) { |
|
235 int i = method->invocation_count(); |
|
236 int b = method->backedge_count(); |
|
237 double k = Tier0ProfilingStartPercentage / 100.0; |
|
238 return call_predicate_helper<CompLevel_none>(i, b, k) || loop_predicate_helper<CompLevel_none>(i, b, k); |
|
239 } |
|
240 return false; |
|
241 } |
|
242 |
|
243 // Create MDO if necessary. |
|
244 void AdvancedThresholdPolicy::create_mdo(methodHandle mh, TRAPS) { |
|
245 if (mh->is_native() || mh->is_abstract() || mh->is_accessor()) return; |
|
246 if (mh->method_data() == NULL) { |
|
247 methodOopDesc::build_interpreter_method_data(mh, THREAD); |
|
248 if (HAS_PENDING_EXCEPTION) { |
|
249 CLEAR_PENDING_EXCEPTION; |
|
250 } |
|
251 } |
|
252 } |
|
253 |
|
254 |
|
255 /* |
|
256 * Method states: |
|
257 * 0 - interpreter (CompLevel_none) |
|
258 * 1 - pure C1 (CompLevel_simple) |
|
259 * 2 - C1 with invocation and backedge counting (CompLevel_limited_profile) |
|
260 * 3 - C1 with full profiling (CompLevel_full_profile) |
|
261 * 4 - C2 (CompLevel_full_optimization) |
|
262 * |
|
263 * Common state transition patterns: |
|
264 * a. 0 -> 3 -> 4. |
|
265 * The most common path. But note that even in this straightforward case |
|
266 * profiling can start at level 0 and finish at level 3. |
|
267 * |
|
268 * b. 0 -> 2 -> 3 -> 4. |
|
269 * This case occures when the load on C2 is deemed too high. So, instead of transitioning |
|
270 * into state 3 directly and over-profiling while a method is in the C2 queue we transition to |
|
271 * level 2 and wait until the load on C2 decreases. This path is disabled for OSRs. |
|
272 * |
|
273 * c. 0 -> (3->2) -> 4. |
|
274 * In this case we enqueue a method for compilation at level 3, but the C1 queue is long enough |
|
275 * to enable the profiling to fully occur at level 0. In this case we change the compilation level |
|
276 * of the method to 2, because it'll allow it to run much faster without full profiling while c2 |
|
277 * is compiling. |
|
278 * |
|
279 * d. 0 -> 3 -> 1 or 0 -> 2 -> 1. |
|
280 * After a method was once compiled with C1 it can be identified as trivial and be compiled to |
|
281 * level 1. These transition can also occur if a method can't be compiled with C2 but can with C1. |
|
282 * |
|
283 * e. 0 -> 4. |
|
284 * This can happen if a method fails C1 compilation (it will still be profiled in the interpreter) |
|
285 * or because of a deopt that didn't require reprofiling (compilation won't happen in this case because |
|
286 * the compiled version already exists). |
|
287 * |
|
288 * Note that since state 0 can be reached from any other state via deoptimization different loops |
|
289 * are possible. |
|
290 * |
|
291 */ |
|
292 |
|
293 // Common transition function. Given a predicate determines if a method should transition to another level. |
|
294 CompLevel AdvancedThresholdPolicy::common(Predicate p, methodOop method, CompLevel cur_level) { |
|
295 if (is_trivial(method)) return CompLevel_simple; |
|
296 |
|
297 CompLevel next_level = cur_level; |
|
298 int i = method->invocation_count(); |
|
299 int b = method->backedge_count(); |
|
300 |
|
301 switch(cur_level) { |
|
302 case CompLevel_none: |
|
303 // If we were at full profile level, would we switch to full opt? |
|
304 if (common(p, method, CompLevel_full_profile) == CompLevel_full_optimization) { |
|
305 next_level = CompLevel_full_optimization; |
|
306 } else if ((this->*p)(i, b, cur_level)) { |
|
307 // C1-generated fully profiled code is about 30% slower than the limited profile |
|
308 // code that has only invocation and backedge counters. The observation is that |
|
309 // if C2 queue is large enough we can spend too much time in the fully profiled code |
|
310 // while waiting for C2 to pick the method from the queue. To alleviate this problem |
|
311 // we introduce a feedback on the C2 queue size. If the C2 queue is sufficiently long |
|
312 // we choose to compile a limited profiled version and then recompile with full profiling |
|
313 // when the load on C2 goes down. |
|
314 if (CompileBroker::queue_size(CompLevel_full_optimization) > |
|
315 Tier3DelayOn * compiler_count(CompLevel_full_optimization)) { |
|
316 next_level = CompLevel_limited_profile; |
|
317 } else { |
|
318 next_level = CompLevel_full_profile; |
|
319 } |
|
320 } |
|
321 break; |
|
322 case CompLevel_limited_profile: |
|
323 if (is_method_profiled(method)) { |
|
324 // Special case: we got here because this method was fully profiled in the interpreter. |
|
325 next_level = CompLevel_full_optimization; |
|
326 } else { |
|
327 methodDataOop mdo = method->method_data(); |
|
328 if (mdo != NULL) { |
|
329 if (mdo->would_profile()) { |
|
330 if (CompileBroker::queue_size(CompLevel_full_optimization) <= |
|
331 Tier3DelayOff * compiler_count(CompLevel_full_optimization) && |
|
332 (this->*p)(i, b, cur_level)) { |
|
333 next_level = CompLevel_full_profile; |
|
334 } |
|
335 } else { |
|
336 next_level = CompLevel_full_optimization; |
|
337 } |
|
338 } |
|
339 } |
|
340 break; |
|
341 case CompLevel_full_profile: |
|
342 { |
|
343 methodDataOop mdo = method->method_data(); |
|
344 if (mdo != NULL) { |
|
345 if (mdo->would_profile()) { |
|
346 int mdo_i = mdo->invocation_count_delta(); |
|
347 int mdo_b = mdo->backedge_count_delta(); |
|
348 if ((this->*p)(mdo_i, mdo_b, cur_level)) { |
|
349 next_level = CompLevel_full_optimization; |
|
350 } |
|
351 } else { |
|
352 next_level = CompLevel_full_optimization; |
|
353 } |
|
354 } |
|
355 } |
|
356 break; |
|
357 } |
|
358 return next_level; |
|
359 } |
|
360 |
|
361 // Determine if a method should be compiled with a normal entry point at a different level. |
|
362 CompLevel AdvancedThresholdPolicy::call_event(methodOop method, CompLevel cur_level) { |
|
363 CompLevel osr_level = (CompLevel) method->highest_osr_comp_level(); |
|
364 CompLevel next_level = common(&AdvancedThresholdPolicy::call_predicate, method, cur_level); |
|
365 |
|
366 // If OSR method level is greater than the regular method level, the levels should be |
|
367 // equalized by raising the regular method level in order to avoid OSRs during each |
|
368 // invocation of the method. |
|
369 if (osr_level == CompLevel_full_optimization && cur_level == CompLevel_full_profile) { |
|
370 methodDataOop mdo = method->method_data(); |
|
371 guarantee(mdo != NULL, "MDO should not be NULL"); |
|
372 if (mdo->invocation_count() >= 1) { |
|
373 next_level = CompLevel_full_optimization; |
|
374 } |
|
375 } else { |
|
376 next_level = MAX2(osr_level, next_level); |
|
377 } |
|
378 |
|
379 return next_level; |
|
380 } |
|
381 |
|
382 // Determine if we should do an OSR compilation of a given method. |
|
383 CompLevel AdvancedThresholdPolicy::loop_event(methodOop method, CompLevel cur_level) { |
|
384 if (cur_level == CompLevel_none) { |
|
385 // If there is a live OSR method that means that we deopted to the interpreter |
|
386 // for the transition. |
|
387 CompLevel osr_level = (CompLevel)method->highest_osr_comp_level(); |
|
388 if (osr_level > CompLevel_none) { |
|
389 return osr_level; |
|
390 } |
|
391 } |
|
392 return common(&AdvancedThresholdPolicy::loop_predicate, method, cur_level); |
|
393 } |
|
394 |
|
395 // Update the rate and submit compile |
|
396 void AdvancedThresholdPolicy::submit_compile(methodHandle mh, int bci, CompLevel level, TRAPS) { |
|
397 int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count(); |
|
398 update_rate(os::javaTimeMillis(), mh()); |
|
399 CompileBroker::compile_method(mh, bci, level, mh, hot_count, "tiered", THREAD); |
|
400 } |
|
401 |
|
402 |
|
403 // Handle the invocation event. |
|
404 void AdvancedThresholdPolicy::method_invocation_event(methodHandle mh, methodHandle imh, |
|
405 CompLevel level, TRAPS) { |
|
406 if (should_create_mdo(mh(), level)) { |
|
407 create_mdo(mh, THREAD); |
|
408 } |
|
409 if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh, InvocationEntryBci)) { |
|
410 CompLevel next_level = call_event(mh(), level); |
|
411 if (next_level != level) { |
|
412 compile(mh, InvocationEntryBci, next_level, THREAD); |
|
413 } |
|
414 } |
|
415 } |
|
416 |
|
417 // Handle the back branch event. Notice that we can compile the method |
|
418 // with a regular entry from here. |
|
419 void AdvancedThresholdPolicy::method_back_branch_event(methodHandle mh, methodHandle imh, |
|
420 int bci, CompLevel level, TRAPS) { |
|
421 if (should_create_mdo(mh(), level)) { |
|
422 create_mdo(mh, THREAD); |
|
423 } |
|
424 |
|
425 // If the method is already compiling, quickly bail out. |
|
426 if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh, bci)) { |
|
427 // Use loop event as an opportinity to also check there's been |
|
428 // enough calls. |
|
429 CompLevel cur_level = comp_level(mh()); |
|
430 CompLevel next_level = call_event(mh(), cur_level); |
|
431 CompLevel next_osr_level = loop_event(mh(), level); |
|
432 if (next_osr_level == CompLevel_limited_profile) { |
|
433 next_osr_level = CompLevel_full_profile; // OSRs are supposed to be for very hot methods. |
|
434 } |
|
435 next_level = MAX2(next_level, |
|
436 next_osr_level < CompLevel_full_optimization ? next_osr_level : cur_level); |
|
437 bool is_compiling = false; |
|
438 if (next_level != cur_level) { |
|
439 compile(mh, InvocationEntryBci, next_level, THREAD); |
|
440 is_compiling = true; |
|
441 } |
|
442 |
|
443 // Do the OSR version |
|
444 if (!is_compiling && next_osr_level != level) { |
|
445 compile(mh, bci, next_osr_level, THREAD); |
|
446 } |
|
447 } |
|
448 } |
|
449 |
|
450 #endif // TIERED |