1 /* |
|
2 * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 #include "precompiled.hpp" |
|
26 #include "compiler/compileBroker.hpp" |
|
27 #include "memory/resourceArea.hpp" |
|
28 #include "runtime/arguments.hpp" |
|
29 #include "runtime/handles.inline.hpp" |
|
30 #include "runtime/safepointVerifiers.hpp" |
|
31 #include "runtime/simpleThresholdPolicy.hpp" |
|
32 #include "runtime/simpleThresholdPolicy.inline.hpp" |
|
33 #include "code/scopeDesc.hpp" |
|
34 #if INCLUDE_JVMCI |
|
35 #include "jvmci/jvmciRuntime.hpp" |
|
36 #endif |
|
37 |
|
38 #ifdef TIERED |
|
39 |
|
40 void SimpleThresholdPolicy::print_counters(const char* prefix, const methodHandle& mh) { |
|
41 int invocation_count = mh->invocation_count(); |
|
42 int backedge_count = mh->backedge_count(); |
|
43 MethodData* mdh = mh->method_data(); |
|
44 int mdo_invocations = 0, mdo_backedges = 0; |
|
45 int mdo_invocations_start = 0, mdo_backedges_start = 0; |
|
46 if (mdh != NULL) { |
|
47 mdo_invocations = mdh->invocation_count(); |
|
48 mdo_backedges = mdh->backedge_count(); |
|
49 mdo_invocations_start = mdh->invocation_count_start(); |
|
50 mdo_backedges_start = mdh->backedge_count_start(); |
|
51 } |
|
52 tty->print(" %stotal=%d,%d %smdo=%d(%d),%d(%d)", prefix, |
|
53 invocation_count, backedge_count, prefix, |
|
54 mdo_invocations, mdo_invocations_start, |
|
55 mdo_backedges, mdo_backedges_start); |
|
56 tty->print(" %smax levels=%d,%d", prefix, |
|
57 mh->highest_comp_level(), mh->highest_osr_comp_level()); |
|
58 } |
|
59 |
|
60 // Print an event. |
|
61 void SimpleThresholdPolicy::print_event(EventType type, const methodHandle& mh, const methodHandle& imh, |
|
62 int bci, CompLevel level) { |
|
63 bool inlinee_event = mh() != imh(); |
|
64 |
|
65 ttyLocker tty_lock; |
|
66 tty->print("%lf: [", os::elapsedTime()); |
|
67 |
|
68 switch(type) { |
|
69 case CALL: |
|
70 tty->print("call"); |
|
71 break; |
|
72 case LOOP: |
|
73 tty->print("loop"); |
|
74 break; |
|
75 case COMPILE: |
|
76 tty->print("compile"); |
|
77 break; |
|
78 case REMOVE_FROM_QUEUE: |
|
79 tty->print("remove-from-queue"); |
|
80 break; |
|
81 case UPDATE_IN_QUEUE: |
|
82 tty->print("update-in-queue"); |
|
83 break; |
|
84 case REPROFILE: |
|
85 tty->print("reprofile"); |
|
86 break; |
|
87 case MAKE_NOT_ENTRANT: |
|
88 tty->print("make-not-entrant"); |
|
89 break; |
|
90 default: |
|
91 tty->print("unknown"); |
|
92 } |
|
93 |
|
94 tty->print(" level=%d ", level); |
|
95 |
|
96 ResourceMark rm; |
|
97 char *method_name = mh->name_and_sig_as_C_string(); |
|
98 tty->print("[%s", method_name); |
|
99 if (inlinee_event) { |
|
100 char *inlinee_name = imh->name_and_sig_as_C_string(); |
|
101 tty->print(" [%s]] ", inlinee_name); |
|
102 } |
|
103 else tty->print("] "); |
|
104 tty->print("@%d queues=%d,%d", bci, CompileBroker::queue_size(CompLevel_full_profile), |
|
105 CompileBroker::queue_size(CompLevel_full_optimization)); |
|
106 |
|
107 print_specific(type, mh, imh, bci, level); |
|
108 |
|
109 if (type != COMPILE) { |
|
110 print_counters("", mh); |
|
111 if (inlinee_event) { |
|
112 print_counters("inlinee ", imh); |
|
113 } |
|
114 tty->print(" compilable="); |
|
115 bool need_comma = false; |
|
116 if (!mh->is_not_compilable(CompLevel_full_profile)) { |
|
117 tty->print("c1"); |
|
118 need_comma = true; |
|
119 } |
|
120 if (!mh->is_not_osr_compilable(CompLevel_full_profile)) { |
|
121 if (need_comma) tty->print(","); |
|
122 tty->print("c1-osr"); |
|
123 need_comma = true; |
|
124 } |
|
125 if (!mh->is_not_compilable(CompLevel_full_optimization)) { |
|
126 if (need_comma) tty->print(","); |
|
127 tty->print("c2"); |
|
128 need_comma = true; |
|
129 } |
|
130 if (!mh->is_not_osr_compilable(CompLevel_full_optimization)) { |
|
131 if (need_comma) tty->print(","); |
|
132 tty->print("c2-osr"); |
|
133 } |
|
134 tty->print(" status="); |
|
135 if (mh->queued_for_compilation()) { |
|
136 tty->print("in-queue"); |
|
137 } else tty->print("idle"); |
|
138 } |
|
139 tty->print_cr("]"); |
|
140 } |
|
141 |
|
142 void SimpleThresholdPolicy::initialize() { |
|
143 int count = CICompilerCount; |
|
144 #ifdef _LP64 |
|
145 // Turn on ergonomic compiler count selection |
|
146 if (FLAG_IS_DEFAULT(CICompilerCountPerCPU) && FLAG_IS_DEFAULT(CICompilerCount)) { |
|
147 FLAG_SET_DEFAULT(CICompilerCountPerCPU, true); |
|
148 } |
|
149 if (CICompilerCountPerCPU) { |
|
150 // Simple log n seems to grow too slowly for tiered, try something faster: log n * log log n |
|
151 int log_cpu = log2_intptr(os::active_processor_count()); |
|
152 int loglog_cpu = log2_intptr(MAX2(log_cpu, 1)); |
|
153 count = MAX2(log_cpu * loglog_cpu * 3 / 2, 2); |
|
154 FLAG_SET_ERGO(intx, CICompilerCount, count); |
|
155 } |
|
156 #else |
|
157 // On 32-bit systems, the number of compiler threads is limited to 3. |
|
158 // On these systems, the virtual address space available to the JVM |
|
159 // is usually limited to 2-4 GB (the exact value depends on the platform). |
|
160 // As the compilers (especially C2) can consume a large amount of |
|
161 // memory, scaling the number of compiler threads with the number of |
|
162 // available cores can result in the exhaustion of the address space |
|
163 /// available to the VM and thus cause the VM to crash. |
|
164 if (FLAG_IS_DEFAULT(CICompilerCount)) { |
|
165 count = 3; |
|
166 FLAG_SET_ERGO(intx, CICompilerCount, count); |
|
167 } |
|
168 #endif |
|
169 |
|
170 if (TieredStopAtLevel < CompLevel_full_optimization) { |
|
171 // No C2 compiler thread required |
|
172 set_c1_count(count); |
|
173 } else { |
|
174 set_c1_count(MAX2(count / 3, 1)); |
|
175 set_c2_count(MAX2(count - c1_count(), 1)); |
|
176 } |
|
177 assert(count == c1_count() + c2_count(), "inconsistent compiler thread count"); |
|
178 |
|
179 // Some inlining tuning |
|
180 #ifdef X86 |
|
181 if (FLAG_IS_DEFAULT(InlineSmallCode)) { |
|
182 FLAG_SET_DEFAULT(InlineSmallCode, 2000); |
|
183 } |
|
184 #endif |
|
185 |
|
186 #if defined SPARC || defined AARCH64 |
|
187 if (FLAG_IS_DEFAULT(InlineSmallCode)) { |
|
188 FLAG_SET_DEFAULT(InlineSmallCode, 2500); |
|
189 } |
|
190 #endif |
|
191 |
|
192 set_increase_threshold_at_ratio(); |
|
193 set_start_time(os::javaTimeMillis()); |
|
194 } |
|
195 |
|
196 void SimpleThresholdPolicy::set_carry_if_necessary(InvocationCounter *counter) { |
|
197 if (!counter->carry() && counter->count() > InvocationCounter::count_limit / 2) { |
|
198 counter->set_carry_flag(); |
|
199 } |
|
200 } |
|
201 |
|
202 // Set carry flags on the counters if necessary |
|
203 void SimpleThresholdPolicy::handle_counter_overflow(Method* method) { |
|
204 MethodCounters *mcs = method->method_counters(); |
|
205 if (mcs != NULL) { |
|
206 set_carry_if_necessary(mcs->invocation_counter()); |
|
207 set_carry_if_necessary(mcs->backedge_counter()); |
|
208 } |
|
209 MethodData* mdo = method->method_data(); |
|
210 if (mdo != NULL) { |
|
211 set_carry_if_necessary(mdo->invocation_counter()); |
|
212 set_carry_if_necessary(mdo->backedge_counter()); |
|
213 } |
|
214 } |
|
215 |
|
216 // Called with the queue locked and with at least one element |
|
217 CompileTask* SimpleThresholdPolicy::select_task(CompileQueue* compile_queue) { |
|
218 CompileTask *max_blocking_task = NULL; |
|
219 CompileTask *max_task = NULL; |
|
220 Method* max_method = NULL; |
|
221 jlong t = os::javaTimeMillis(); |
|
222 // Iterate through the queue and find a method with a maximum rate. |
|
223 for (CompileTask* task = compile_queue->first(); task != NULL;) { |
|
224 CompileTask* next_task = task->next(); |
|
225 Method* method = task->method(); |
|
226 update_rate(t, method); |
|
227 if (max_task == NULL) { |
|
228 max_task = task; |
|
229 max_method = method; |
|
230 } else { |
|
231 // If a method has been stale for some time, remove it from the queue. |
|
232 // Blocking tasks and tasks submitted from whitebox API don't become stale |
|
233 if (task->can_become_stale() && is_stale(t, TieredCompileTaskTimeout, method) && !is_old(method)) { |
|
234 if (PrintTieredEvents) { |
|
235 print_event(REMOVE_FROM_QUEUE, method, method, task->osr_bci(), (CompLevel)task->comp_level()); |
|
236 } |
|
237 compile_queue->remove_and_mark_stale(task); |
|
238 method->clear_queued_for_compilation(); |
|
239 task = next_task; |
|
240 continue; |
|
241 } |
|
242 |
|
243 // Select a method with a higher rate |
|
244 if (compare_methods(method, max_method)) { |
|
245 max_task = task; |
|
246 max_method = method; |
|
247 } |
|
248 } |
|
249 |
|
250 if (task->is_blocking()) { |
|
251 if (max_blocking_task == NULL || compare_methods(method, max_blocking_task->method())) { |
|
252 max_blocking_task = task; |
|
253 } |
|
254 } |
|
255 |
|
256 task = next_task; |
|
257 } |
|
258 |
|
259 if (max_blocking_task != NULL) { |
|
260 // In blocking compilation mode, the CompileBroker will make |
|
261 // compilations submitted by a JVMCI compiler thread non-blocking. These |
|
262 // compilations should be scheduled after all blocking compilations |
|
263 // to service non-compiler related compilations sooner and reduce the |
|
264 // chance of such compilations timing out. |
|
265 max_task = max_blocking_task; |
|
266 max_method = max_task->method(); |
|
267 } |
|
268 |
|
269 if (max_task != NULL && max_task->comp_level() == CompLevel_full_profile && |
|
270 TieredStopAtLevel > CompLevel_full_profile && |
|
271 max_method != NULL && is_method_profiled(max_method)) { |
|
272 max_task->set_comp_level(CompLevel_limited_profile); |
|
273 if (PrintTieredEvents) { |
|
274 print_event(UPDATE_IN_QUEUE, max_method, max_method, max_task->osr_bci(), (CompLevel)max_task->comp_level()); |
|
275 } |
|
276 } |
|
277 |
|
278 return max_task; |
|
279 } |
|
280 |
|
281 void SimpleThresholdPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) { |
|
282 for (ScopeDesc* sd = trap_scope;; sd = sd->sender()) { |
|
283 if (PrintTieredEvents) { |
|
284 methodHandle mh(sd->method()); |
|
285 print_event(REPROFILE, mh, mh, InvocationEntryBci, CompLevel_none); |
|
286 } |
|
287 MethodData* mdo = sd->method()->method_data(); |
|
288 if (mdo != NULL) { |
|
289 mdo->reset_start_counters(); |
|
290 } |
|
291 if (sd->is_top()) break; |
|
292 } |
|
293 } |
|
294 |
|
295 nmethod* SimpleThresholdPolicy::event(const methodHandle& method, const methodHandle& inlinee, |
|
296 int branch_bci, int bci, CompLevel comp_level, CompiledMethod* nm, JavaThread* thread) { |
|
297 if (comp_level == CompLevel_none && |
|
298 JvmtiExport::can_post_interpreter_events() && |
|
299 thread->is_interp_only_mode()) { |
|
300 return NULL; |
|
301 } |
|
302 if (CompileTheWorld || ReplayCompiles) { |
|
303 // Don't trigger other compiles in testing mode |
|
304 return NULL; |
|
305 } |
|
306 |
|
307 handle_counter_overflow(method()); |
|
308 if (method() != inlinee()) { |
|
309 handle_counter_overflow(inlinee()); |
|
310 } |
|
311 |
|
312 if (PrintTieredEvents) { |
|
313 print_event(bci == InvocationEntryBci ? CALL : LOOP, method, inlinee, bci, comp_level); |
|
314 } |
|
315 |
|
316 if (bci == InvocationEntryBci) { |
|
317 method_invocation_event(method, inlinee, comp_level, nm, thread); |
|
318 } else { |
|
319 // method == inlinee if the event originated in the main method |
|
320 method_back_branch_event(method, inlinee, bci, comp_level, nm, thread); |
|
321 // Check if event led to a higher level OSR compilation |
|
322 nmethod* osr_nm = inlinee->lookup_osr_nmethod_for(bci, comp_level, false); |
|
323 if (osr_nm != NULL && osr_nm->comp_level() > comp_level) { |
|
324 // Perform OSR with new nmethod |
|
325 return osr_nm; |
|
326 } |
|
327 } |
|
328 return NULL; |
|
329 } |
|
330 |
|
331 // Check if the method can be compiled, change level if necessary |
|
332 void SimpleThresholdPolicy::compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) { |
|
333 assert(level <= TieredStopAtLevel, "Invalid compilation level"); |
|
334 if (level == CompLevel_none) { |
|
335 return; |
|
336 } |
|
337 if (level == CompLevel_aot) { |
|
338 if (mh->has_aot_code()) { |
|
339 if (PrintTieredEvents) { |
|
340 print_event(COMPILE, mh, mh, bci, level); |
|
341 } |
|
342 MutexLocker ml(Compile_lock); |
|
343 NoSafepointVerifier nsv; |
|
344 if (mh->has_aot_code() && mh->code() != mh->aot_code()) { |
|
345 mh->aot_code()->make_entrant(); |
|
346 if (mh->has_compiled_code()) { |
|
347 mh->code()->make_not_entrant(); |
|
348 } |
|
349 Method::set_code(mh, mh->aot_code()); |
|
350 } |
|
351 } |
|
352 return; |
|
353 } |
|
354 |
|
355 // Check if the method can be compiled. If it cannot be compiled with C1, continue profiling |
|
356 // in the interpreter and then compile with C2 (the transition function will request that, |
|
357 // see common() ). If the method cannot be compiled with C2 but still can with C1, compile it with |
|
358 // pure C1. |
|
359 if (!can_be_compiled(mh, level)) { |
|
360 if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) { |
|
361 compile(mh, bci, CompLevel_simple, thread); |
|
362 } |
|
363 return; |
|
364 } |
|
365 if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) { |
|
366 return; |
|
367 } |
|
368 if (!CompileBroker::compilation_is_in_queue(mh)) { |
|
369 if (PrintTieredEvents) { |
|
370 print_event(COMPILE, mh, mh, bci, level); |
|
371 } |
|
372 submit_compile(mh, bci, level, thread); |
|
373 } |
|
374 } |
|
375 |
|
376 // Update the rate and submit compile |
|
377 void SimpleThresholdPolicy::submit_compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) { |
|
378 int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count(); |
|
379 update_rate(os::javaTimeMillis(), mh()); |
|
380 CompileBroker::compile_method(mh, bci, level, mh, hot_count, CompileTask::Reason_Tiered, thread); |
|
381 } |
|
382 |
|
383 // Print an event. |
|
384 void SimpleThresholdPolicy::print_specific(EventType type, const methodHandle& mh, const methodHandle& imh, |
|
385 int bci, CompLevel level) { |
|
386 tty->print(" rate="); |
|
387 if (mh->prev_time() == 0) tty->print("n/a"); |
|
388 else tty->print("%f", mh->rate()); |
|
389 |
|
390 tty->print(" k=%.2lf,%.2lf", threshold_scale(CompLevel_full_profile, Tier3LoadFeedback), |
|
391 threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback)); |
|
392 |
|
393 } |
|
394 |
|
395 // update_rate() is called from select_task() while holding a compile queue lock. |
|
396 void SimpleThresholdPolicy::update_rate(jlong t, Method* m) { |
|
397 // Skip update if counters are absent. |
|
398 // Can't allocate them since we are holding compile queue lock. |
|
399 if (m->method_counters() == NULL) return; |
|
400 |
|
401 if (is_old(m)) { |
|
402 // We don't remove old methods from the queue, |
|
403 // so we can just zero the rate. |
|
404 m->set_rate(0); |
|
405 return; |
|
406 } |
|
407 |
|
408 // We don't update the rate if we've just came out of a safepoint. |
|
409 // delta_s is the time since last safepoint in milliseconds. |
|
410 jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint(); |
|
411 jlong delta_t = t - (m->prev_time() != 0 ? m->prev_time() : start_time()); // milliseconds since the last measurement |
|
412 // How many events were there since the last time? |
|
413 int event_count = m->invocation_count() + m->backedge_count(); |
|
414 int delta_e = event_count - m->prev_event_count(); |
|
415 |
|
416 // We should be running for at least 1ms. |
|
417 if (delta_s >= TieredRateUpdateMinTime) { |
|
418 // And we must've taken the previous point at least 1ms before. |
|
419 if (delta_t >= TieredRateUpdateMinTime && delta_e > 0) { |
|
420 m->set_prev_time(t); |
|
421 m->set_prev_event_count(event_count); |
|
422 m->set_rate((float)delta_e / (float)delta_t); // Rate is events per millisecond |
|
423 } else { |
|
424 if (delta_t > TieredRateUpdateMaxTime && delta_e == 0) { |
|
425 // If nothing happened for 25ms, zero the rate. Don't modify prev values. |
|
426 m->set_rate(0); |
|
427 } |
|
428 } |
|
429 } |
|
430 } |
|
431 |
|
432 // Check if this method has been stale from a given number of milliseconds. |
|
433 // See select_task(). |
|
434 bool SimpleThresholdPolicy::is_stale(jlong t, jlong timeout, Method* m) { |
|
435 jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint(); |
|
436 jlong delta_t = t - m->prev_time(); |
|
437 if (delta_t > timeout && delta_s > timeout) { |
|
438 int event_count = m->invocation_count() + m->backedge_count(); |
|
439 int delta_e = event_count - m->prev_event_count(); |
|
440 // Return true if there were no events. |
|
441 return delta_e == 0; |
|
442 } |
|
443 return false; |
|
444 } |
|
445 |
|
446 // We don't remove old methods from the compile queue even if they have |
|
447 // very low activity. See select_task(). |
|
448 bool SimpleThresholdPolicy::is_old(Method* method) { |
|
449 return method->invocation_count() > 50000 || method->backedge_count() > 500000; |
|
450 } |
|
451 |
|
452 double SimpleThresholdPolicy::weight(Method* method) { |
|
453 return (double)(method->rate() + 1) * |
|
454 (method->invocation_count() + 1) * (method->backedge_count() + 1); |
|
455 } |
|
456 |
|
457 // Apply heuristics and return true if x should be compiled before y |
|
458 bool SimpleThresholdPolicy::compare_methods(Method* x, Method* y) { |
|
459 if (x->highest_comp_level() > y->highest_comp_level()) { |
|
460 // recompilation after deopt |
|
461 return true; |
|
462 } else |
|
463 if (x->highest_comp_level() == y->highest_comp_level()) { |
|
464 if (weight(x) > weight(y)) { |
|
465 return true; |
|
466 } |
|
467 } |
|
468 return false; |
|
469 } |
|
470 |
|
471 // Is method profiled enough? |
|
472 bool SimpleThresholdPolicy::is_method_profiled(Method* method) { |
|
473 MethodData* mdo = method->method_data(); |
|
474 if (mdo != NULL) { |
|
475 int i = mdo->invocation_count_delta(); |
|
476 int b = mdo->backedge_count_delta(); |
|
477 return call_predicate_helper<CompLevel_full_profile>(i, b, 1, method); |
|
478 } |
|
479 return false; |
|
480 } |
|
481 |
|
482 double SimpleThresholdPolicy::threshold_scale(CompLevel level, int feedback_k) { |
|
483 double queue_size = CompileBroker::queue_size(level); |
|
484 int comp_count = compiler_count(level); |
|
485 double k = queue_size / (feedback_k * comp_count) + 1; |
|
486 |
|
487 // Increase C1 compile threshold when the code cache is filled more |
|
488 // than specified by IncreaseFirstTierCompileThresholdAt percentage. |
|
489 // The main intention is to keep enough free space for C2 compiled code |
|
490 // to achieve peak performance if the code cache is under stress. |
|
491 if ((TieredStopAtLevel == CompLevel_full_optimization) && (level != CompLevel_full_optimization)) { |
|
492 double current_reverse_free_ratio = CodeCache::reverse_free_ratio(CodeCache::get_code_blob_type(level)); |
|
493 if (current_reverse_free_ratio > _increase_threshold_at_ratio) { |
|
494 k *= exp(current_reverse_free_ratio - _increase_threshold_at_ratio); |
|
495 } |
|
496 } |
|
497 return k; |
|
498 } |
|
499 |
|
500 // Call and loop predicates determine whether a transition to a higher |
|
501 // compilation level should be performed (pointers to predicate functions |
|
502 // are passed to common()). |
|
503 // Tier?LoadFeedback is basically a coefficient that determines of |
|
504 // how many methods per compiler thread can be in the queue before |
|
505 // the threshold values double. |
|
506 bool SimpleThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level, Method* method) { |
|
507 switch(cur_level) { |
|
508 case CompLevel_aot: { |
|
509 double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback); |
|
510 return loop_predicate_helper<CompLevel_aot>(i, b, k, method); |
|
511 } |
|
512 case CompLevel_none: |
|
513 case CompLevel_limited_profile: { |
|
514 double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback); |
|
515 return loop_predicate_helper<CompLevel_none>(i, b, k, method); |
|
516 } |
|
517 case CompLevel_full_profile: { |
|
518 double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback); |
|
519 return loop_predicate_helper<CompLevel_full_profile>(i, b, k, method); |
|
520 } |
|
521 default: |
|
522 return true; |
|
523 } |
|
524 } |
|
525 |
|
526 bool SimpleThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level, Method* method) { |
|
527 switch(cur_level) { |
|
528 case CompLevel_aot: { |
|
529 double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback); |
|
530 return call_predicate_helper<CompLevel_aot>(i, b, k, method); |
|
531 } |
|
532 case CompLevel_none: |
|
533 case CompLevel_limited_profile: { |
|
534 double k = threshold_scale(CompLevel_full_profile, Tier3LoadFeedback); |
|
535 return call_predicate_helper<CompLevel_none>(i, b, k, method); |
|
536 } |
|
537 case CompLevel_full_profile: { |
|
538 double k = threshold_scale(CompLevel_full_optimization, Tier4LoadFeedback); |
|
539 return call_predicate_helper<CompLevel_full_profile>(i, b, k, method); |
|
540 } |
|
541 default: |
|
542 return true; |
|
543 } |
|
544 } |
|
545 |
|
546 // Determine is a method is mature. |
|
547 bool SimpleThresholdPolicy::is_mature(Method* method) { |
|
548 if (is_trivial(method)) return true; |
|
549 MethodData* mdo = method->method_data(); |
|
550 if (mdo != NULL) { |
|
551 int i = mdo->invocation_count(); |
|
552 int b = mdo->backedge_count(); |
|
553 double k = ProfileMaturityPercentage / 100.0; |
|
554 return call_predicate_helper<CompLevel_full_profile>(i, b, k, method) || |
|
555 loop_predicate_helper<CompLevel_full_profile>(i, b, k, method); |
|
556 } |
|
557 return false; |
|
558 } |
|
559 |
|
560 // If a method is old enough and is still in the interpreter we would want to |
|
561 // start profiling without waiting for the compiled method to arrive. |
|
562 // We also take the load on compilers into the account. |
|
563 bool SimpleThresholdPolicy::should_create_mdo(Method* method, CompLevel cur_level) { |
|
564 if (cur_level == CompLevel_none && |
|
565 CompileBroker::queue_size(CompLevel_full_optimization) <= |
|
566 Tier3DelayOn * compiler_count(CompLevel_full_optimization)) { |
|
567 int i = method->invocation_count(); |
|
568 int b = method->backedge_count(); |
|
569 double k = Tier0ProfilingStartPercentage / 100.0; |
|
570 return call_predicate_helper<CompLevel_none>(i, b, k, method) || loop_predicate_helper<CompLevel_none>(i, b, k, method); |
|
571 } |
|
572 return false; |
|
573 } |
|
574 |
|
575 // Inlining control: if we're compiling a profiled method with C1 and the callee |
|
576 // is known to have OSRed in a C2 version, don't inline it. |
|
577 bool SimpleThresholdPolicy::should_not_inline(ciEnv* env, ciMethod* callee) { |
|
578 CompLevel comp_level = (CompLevel)env->comp_level(); |
|
579 if (comp_level == CompLevel_full_profile || |
|
580 comp_level == CompLevel_limited_profile) { |
|
581 return callee->highest_osr_comp_level() == CompLevel_full_optimization; |
|
582 } |
|
583 return false; |
|
584 } |
|
585 |
|
586 // Create MDO if necessary. |
|
587 void SimpleThresholdPolicy::create_mdo(const methodHandle& mh, JavaThread* THREAD) { |
|
588 if (mh->is_native() || |
|
589 mh->is_abstract() || |
|
590 mh->is_accessor() || |
|
591 mh->is_constant_getter()) { |
|
592 return; |
|
593 } |
|
594 if (mh->method_data() == NULL) { |
|
595 Method::build_interpreter_method_data(mh, CHECK_AND_CLEAR); |
|
596 } |
|
597 } |
|
598 |
|
599 |
|
600 /* |
|
601 * Method states: |
|
602 * 0 - interpreter (CompLevel_none) |
|
603 * 1 - pure C1 (CompLevel_simple) |
|
604 * 2 - C1 with invocation and backedge counting (CompLevel_limited_profile) |
|
605 * 3 - C1 with full profiling (CompLevel_full_profile) |
|
606 * 4 - C2 (CompLevel_full_optimization) |
|
607 * |
|
608 * Common state transition patterns: |
|
609 * a. 0 -> 3 -> 4. |
|
610 * The most common path. But note that even in this straightforward case |
|
611 * profiling can start at level 0 and finish at level 3. |
|
612 * |
|
613 * b. 0 -> 2 -> 3 -> 4. |
|
614 * This case occurs when the load on C2 is deemed too high. So, instead of transitioning |
|
615 * into state 3 directly and over-profiling while a method is in the C2 queue we transition to |
|
616 * level 2 and wait until the load on C2 decreases. This path is disabled for OSRs. |
|
617 * |
|
618 * c. 0 -> (3->2) -> 4. |
|
619 * In this case we enqueue a method for compilation at level 3, but the C1 queue is long enough |
|
620 * to enable the profiling to fully occur at level 0. In this case we change the compilation level |
|
621 * of the method to 2 while the request is still in-queue, because it'll allow it to run much faster |
|
622 * without full profiling while c2 is compiling. |
|
623 * |
|
624 * d. 0 -> 3 -> 1 or 0 -> 2 -> 1. |
|
625 * After a method was once compiled with C1 it can be identified as trivial and be compiled to |
|
626 * level 1. These transition can also occur if a method can't be compiled with C2 but can with C1. |
|
627 * |
|
628 * e. 0 -> 4. |
|
629 * This can happen if a method fails C1 compilation (it will still be profiled in the interpreter) |
|
630 * or because of a deopt that didn't require reprofiling (compilation won't happen in this case because |
|
631 * the compiled version already exists). |
|
632 * |
|
633 * Note that since state 0 can be reached from any other state via deoptimization different loops |
|
634 * are possible. |
|
635 * |
|
636 */ |
|
637 |
|
638 // Common transition function. Given a predicate determines if a method should transition to another level. |
|
639 CompLevel SimpleThresholdPolicy::common(Predicate p, Method* method, CompLevel cur_level, bool disable_feedback) { |
|
640 CompLevel next_level = cur_level; |
|
641 int i = method->invocation_count(); |
|
642 int b = method->backedge_count(); |
|
643 |
|
644 if (is_trivial(method)) { |
|
645 next_level = CompLevel_simple; |
|
646 } else { |
|
647 switch(cur_level) { |
|
648 default: break; |
|
649 case CompLevel_aot: { |
|
650 // If we were at full profile level, would we switch to full opt? |
|
651 if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) { |
|
652 next_level = CompLevel_full_optimization; |
|
653 } else if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <= |
|
654 Tier3DelayOff * compiler_count(CompLevel_full_optimization) && |
|
655 (this->*p)(i, b, cur_level, method))) { |
|
656 next_level = CompLevel_full_profile; |
|
657 } |
|
658 } |
|
659 break; |
|
660 case CompLevel_none: |
|
661 // If we were at full profile level, would we switch to full opt? |
|
662 if (common(p, method, CompLevel_full_profile, disable_feedback) == CompLevel_full_optimization) { |
|
663 next_level = CompLevel_full_optimization; |
|
664 } else if ((this->*p)(i, b, cur_level, method)) { |
|
665 #if INCLUDE_JVMCI |
|
666 if (EnableJVMCI && UseJVMCICompiler) { |
|
667 // Since JVMCI takes a while to warm up, its queue inevitably backs up during |
|
668 // early VM execution. As of 2014-06-13, JVMCI's inliner assumes that the root |
|
669 // compilation method and all potential inlinees have mature profiles (which |
|
670 // includes type profiling). If it sees immature profiles, JVMCI's inliner |
|
671 // can perform pathologically bad (e.g., causing OutOfMemoryErrors due to |
|
672 // exploring/inlining too many graphs). Since a rewrite of the inliner is |
|
673 // in progress, we simply disable the dialing back heuristic for now and will |
|
674 // revisit this decision once the new inliner is completed. |
|
675 next_level = CompLevel_full_profile; |
|
676 } else |
|
677 #endif |
|
678 { |
|
679 // C1-generated fully profiled code is about 30% slower than the limited profile |
|
680 // code that has only invocation and backedge counters. The observation is that |
|
681 // if C2 queue is large enough we can spend too much time in the fully profiled code |
|
682 // while waiting for C2 to pick the method from the queue. To alleviate this problem |
|
683 // we introduce a feedback on the C2 queue size. If the C2 queue is sufficiently long |
|
684 // we choose to compile a limited profiled version and then recompile with full profiling |
|
685 // when the load on C2 goes down. |
|
686 if (!disable_feedback && CompileBroker::queue_size(CompLevel_full_optimization) > |
|
687 Tier3DelayOn * compiler_count(CompLevel_full_optimization)) { |
|
688 next_level = CompLevel_limited_profile; |
|
689 } else { |
|
690 next_level = CompLevel_full_profile; |
|
691 } |
|
692 } |
|
693 } |
|
694 break; |
|
695 case CompLevel_limited_profile: |
|
696 if (is_method_profiled(method)) { |
|
697 // Special case: we got here because this method was fully profiled in the interpreter. |
|
698 next_level = CompLevel_full_optimization; |
|
699 } else { |
|
700 MethodData* mdo = method->method_data(); |
|
701 if (mdo != NULL) { |
|
702 if (mdo->would_profile()) { |
|
703 if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <= |
|
704 Tier3DelayOff * compiler_count(CompLevel_full_optimization) && |
|
705 (this->*p)(i, b, cur_level, method))) { |
|
706 next_level = CompLevel_full_profile; |
|
707 } |
|
708 } else { |
|
709 next_level = CompLevel_full_optimization; |
|
710 } |
|
711 } else { |
|
712 // If there is no MDO we need to profile |
|
713 if (disable_feedback || (CompileBroker::queue_size(CompLevel_full_optimization) <= |
|
714 Tier3DelayOff * compiler_count(CompLevel_full_optimization) && |
|
715 (this->*p)(i, b, cur_level, method))) { |
|
716 next_level = CompLevel_full_profile; |
|
717 } |
|
718 } |
|
719 } |
|
720 break; |
|
721 case CompLevel_full_profile: |
|
722 { |
|
723 MethodData* mdo = method->method_data(); |
|
724 if (mdo != NULL) { |
|
725 if (mdo->would_profile()) { |
|
726 int mdo_i = mdo->invocation_count_delta(); |
|
727 int mdo_b = mdo->backedge_count_delta(); |
|
728 if ((this->*p)(mdo_i, mdo_b, cur_level, method)) { |
|
729 next_level = CompLevel_full_optimization; |
|
730 } |
|
731 } else { |
|
732 next_level = CompLevel_full_optimization; |
|
733 } |
|
734 } |
|
735 } |
|
736 break; |
|
737 } |
|
738 } |
|
739 return MIN2(next_level, (CompLevel)TieredStopAtLevel); |
|
740 } |
|
741 |
|
742 // Determine if a method should be compiled with a normal entry point at a different level. |
|
743 CompLevel SimpleThresholdPolicy::call_event(Method* method, CompLevel cur_level, JavaThread * thread) { |
|
744 CompLevel osr_level = MIN2((CompLevel) method->highest_osr_comp_level(), |
|
745 common(&SimpleThresholdPolicy::loop_predicate, method, cur_level, true)); |
|
746 CompLevel next_level = common(&SimpleThresholdPolicy::call_predicate, method, cur_level); |
|
747 |
|
748 // If OSR method level is greater than the regular method level, the levels should be |
|
749 // equalized by raising the regular method level in order to avoid OSRs during each |
|
750 // invocation of the method. |
|
751 if (osr_level == CompLevel_full_optimization && cur_level == CompLevel_full_profile) { |
|
752 MethodData* mdo = method->method_data(); |
|
753 guarantee(mdo != NULL, "MDO should not be NULL"); |
|
754 if (mdo->invocation_count() >= 1) { |
|
755 next_level = CompLevel_full_optimization; |
|
756 } |
|
757 } else { |
|
758 next_level = MAX2(osr_level, next_level); |
|
759 } |
|
760 #if INCLUDE_JVMCI |
|
761 if (UseJVMCICompiler) { |
|
762 next_level = JVMCIRuntime::adjust_comp_level(method, false, next_level, thread); |
|
763 } |
|
764 #endif |
|
765 return next_level; |
|
766 } |
|
767 |
|
768 // Determine if we should do an OSR compilation of a given method. |
|
769 CompLevel SimpleThresholdPolicy::loop_event(Method* method, CompLevel cur_level, JavaThread* thread) { |
|
770 CompLevel next_level = common(&SimpleThresholdPolicy::loop_predicate, method, cur_level, true); |
|
771 if (cur_level == CompLevel_none) { |
|
772 // If there is a live OSR method that means that we deopted to the interpreter |
|
773 // for the transition. |
|
774 CompLevel osr_level = MIN2((CompLevel)method->highest_osr_comp_level(), next_level); |
|
775 if (osr_level > CompLevel_none) { |
|
776 return osr_level; |
|
777 } |
|
778 } |
|
779 #if INCLUDE_JVMCI |
|
780 if (UseJVMCICompiler) { |
|
781 next_level = JVMCIRuntime::adjust_comp_level(method, true, next_level, thread); |
|
782 } |
|
783 #endif |
|
784 return next_level; |
|
785 } |
|
786 |
|
787 bool SimpleThresholdPolicy::maybe_switch_to_aot(const methodHandle& mh, CompLevel cur_level, CompLevel next_level, JavaThread* thread) { |
|
788 if (UseAOT && !delay_compilation_during_startup()) { |
|
789 if (cur_level == CompLevel_full_profile || cur_level == CompLevel_none) { |
|
790 // If the current level is full profile or interpreter and we're switching to any other level, |
|
791 // activate the AOT code back first so that we won't waste time overprofiling. |
|
792 compile(mh, InvocationEntryBci, CompLevel_aot, thread); |
|
793 // Fall through for JIT compilation. |
|
794 } |
|
795 if (next_level == CompLevel_limited_profile && cur_level != CompLevel_aot && mh->has_aot_code()) { |
|
796 // If the next level is limited profile, use the aot code (if there is any), |
|
797 // since it's essentially the same thing. |
|
798 compile(mh, InvocationEntryBci, CompLevel_aot, thread); |
|
799 // Not need to JIT, we're done. |
|
800 return true; |
|
801 } |
|
802 } |
|
803 return false; |
|
804 } |
|
805 |
|
806 |
|
807 // Handle the invocation event. |
|
808 void SimpleThresholdPolicy::method_invocation_event(const methodHandle& mh, const methodHandle& imh, |
|
809 CompLevel level, CompiledMethod* nm, JavaThread* thread) { |
|
810 if (should_create_mdo(mh(), level)) { |
|
811 create_mdo(mh, thread); |
|
812 } |
|
813 CompLevel next_level = call_event(mh(), level, thread); |
|
814 if (next_level != level) { |
|
815 if (maybe_switch_to_aot(mh, level, next_level, thread)) { |
|
816 // No JITting necessary |
|
817 return; |
|
818 } |
|
819 if (is_compilation_enabled() && !CompileBroker::compilation_is_in_queue(mh)) { |
|
820 compile(mh, InvocationEntryBci, next_level, thread); |
|
821 } |
|
822 } |
|
823 } |
|
824 |
|
825 // Handle the back branch event. Notice that we can compile the method |
|
826 // with a regular entry from here. |
|
827 void SimpleThresholdPolicy::method_back_branch_event(const methodHandle& mh, const methodHandle& imh, |
|
828 int bci, CompLevel level, CompiledMethod* nm, JavaThread* thread) { |
|
829 if (should_create_mdo(mh(), level)) { |
|
830 create_mdo(mh, thread); |
|
831 } |
|
832 // Check if MDO should be created for the inlined method |
|
833 if (should_create_mdo(imh(), level)) { |
|
834 create_mdo(imh, thread); |
|
835 } |
|
836 |
|
837 if (is_compilation_enabled()) { |
|
838 CompLevel next_osr_level = loop_event(imh(), level, thread); |
|
839 CompLevel max_osr_level = (CompLevel)imh->highest_osr_comp_level(); |
|
840 // At the very least compile the OSR version |
|
841 if (!CompileBroker::compilation_is_in_queue(imh) && (next_osr_level != level)) { |
|
842 compile(imh, bci, next_osr_level, thread); |
|
843 } |
|
844 |
|
845 // Use loop event as an opportunity to also check if there's been |
|
846 // enough calls. |
|
847 CompLevel cur_level, next_level; |
|
848 if (mh() != imh()) { // If there is an enclosing method |
|
849 if (level == CompLevel_aot) { |
|
850 // Recompile the enclosing method to prevent infinite OSRs. Stay at AOT level while it's compiling. |
|
851 if (max_osr_level != CompLevel_none && !CompileBroker::compilation_is_in_queue(mh)) { |
|
852 compile(mh, InvocationEntryBci, MIN2((CompLevel)TieredStopAtLevel, CompLevel_full_profile), thread); |
|
853 } |
|
854 } else { |
|
855 // Current loop event level is not AOT |
|
856 guarantee(nm != NULL, "Should have nmethod here"); |
|
857 cur_level = comp_level(mh()); |
|
858 next_level = call_event(mh(), cur_level, thread); |
|
859 |
|
860 if (max_osr_level == CompLevel_full_optimization) { |
|
861 // The inlinee OSRed to full opt, we need to modify the enclosing method to avoid deopts |
|
862 bool make_not_entrant = false; |
|
863 if (nm->is_osr_method()) { |
|
864 // This is an osr method, just make it not entrant and recompile later if needed |
|
865 make_not_entrant = true; |
|
866 } else { |
|
867 if (next_level != CompLevel_full_optimization) { |
|
868 // next_level is not full opt, so we need to recompile the |
|
869 // enclosing method without the inlinee |
|
870 cur_level = CompLevel_none; |
|
871 make_not_entrant = true; |
|
872 } |
|
873 } |
|
874 if (make_not_entrant) { |
|
875 if (PrintTieredEvents) { |
|
876 int osr_bci = nm->is_osr_method() ? nm->osr_entry_bci() : InvocationEntryBci; |
|
877 print_event(MAKE_NOT_ENTRANT, mh(), mh(), osr_bci, level); |
|
878 } |
|
879 nm->make_not_entrant(); |
|
880 } |
|
881 } |
|
882 // Fix up next_level if necessary to avoid deopts |
|
883 if (next_level == CompLevel_limited_profile && max_osr_level == CompLevel_full_profile) { |
|
884 next_level = CompLevel_full_profile; |
|
885 } |
|
886 if (cur_level != next_level) { |
|
887 if (!maybe_switch_to_aot(mh, cur_level, next_level, thread) && !CompileBroker::compilation_is_in_queue(mh)) { |
|
888 compile(mh, InvocationEntryBci, next_level, thread); |
|
889 } |
|
890 } |
|
891 } |
|
892 } else { |
|
893 cur_level = comp_level(mh()); |
|
894 next_level = call_event(mh(), cur_level, thread); |
|
895 if (next_level != cur_level) { |
|
896 if (!maybe_switch_to_aot(mh, cur_level, next_level, thread) && !CompileBroker::compilation_is_in_queue(mh)) { |
|
897 compile(mh, InvocationEntryBci, next_level, thread); |
|
898 } |
|
899 } |
|
900 } |
|
901 } |
|
902 } |
|
903 |
|
904 #endif |
|