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/simpleThresholdPolicy.hpp"
30 #include "runtime/simpleThresholdPolicy.inline.hpp"
31 #include "code/scopeDesc.hpp"
32 #if INCLUDE_JVMCI
33 #include "jvmci/jvmciRuntime.hpp"
34 #endif
35
36
37 void SimpleThresholdPolicy::print_counters(const char* prefix, methodHandle mh) {
38 int invocation_count = mh->invocation_count();
39 int backedge_count = mh->backedge_count();
40 MethodData* mdh = mh->method_data();
41 int mdo_invocations = 0, mdo_backedges = 0;
42 int mdo_invocations_start = 0, mdo_backedges_start = 0;
43 if (mdh != NULL) {
44 mdo_invocations = mdh->invocation_count();
45 mdo_backedges = mdh->backedge_count();
46 mdo_invocations_start = mdh->invocation_count_start();
47 mdo_backedges_start = mdh->backedge_count_start();
48 }
49 tty->print(" %stotal=%d,%d %smdo=%d(%d),%d(%d)", prefix,
50 invocation_count, backedge_count, prefix,
51 mdo_invocations, mdo_invocations_start,
52 mdo_backedges, mdo_backedges_start);
53 tty->print(" %smax levels=%d,%d", prefix,
54 mh->highest_comp_level(), mh->highest_osr_comp_level());
55 }
225 method_invocation_event(method, inlinee, comp_level, nm, thread);
226 } else {
227 // method == inlinee if the event originated in the main method
228 method_back_branch_event(method, inlinee, bci, comp_level, nm, thread);
229 // Check if event led to a higher level OSR compilation
230 nmethod* osr_nm = inlinee->lookup_osr_nmethod_for(bci, comp_level, false);
231 if (osr_nm != NULL && osr_nm->comp_level() > comp_level) {
232 // Perform OSR with new nmethod
233 return osr_nm;
234 }
235 }
236 return NULL;
237 }
238
239 // Check if the method can be compiled, change level if necessary
240 void SimpleThresholdPolicy::compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
241 assert(level <= TieredStopAtLevel, "Invalid compilation level");
242 if (level == CompLevel_none) {
243 return;
244 }
245
246 // Check if the method can be compiled. If it cannot be compiled with C1, continue profiling
247 // in the interpreter and then compile with C2 (the transition function will request that,
248 // see common() ). If the method cannot be compiled with C2 but still can with C1, compile it with
249 // pure C1.
250 if (!can_be_compiled(mh, level)) {
251 if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) {
252 compile(mh, bci, CompLevel_simple, thread);
253 }
254 return;
255 }
256 if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) {
257 return;
258 }
259 if (!CompileBroker::compilation_is_in_queue(mh)) {
260 if (PrintTieredEvents) {
261 print_event(COMPILE, mh, mh, bci, level);
262 }
263 submit_compile(mh, bci, level, thread);
264 }
265 }
266
267 // Tell the broker to compile the method
268 void SimpleThresholdPolicy::submit_compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
269 int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
270 CompileBroker::compile_method(mh, bci, level, mh, hot_count, CompileTask::Reason_Tiered, thread);
271 }
272
273 // Call and loop predicates determine whether a transition to a higher
274 // compilation level should be performed (pointers to predicate functions
275 // are passed to common() transition function).
276 bool SimpleThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level, Method* method) {
277 switch(cur_level) {
278 case CompLevel_none:
279 case CompLevel_limited_profile: {
280 return loop_predicate_helper<CompLevel_none>(i, b, 1.0, method);
281 }
282 case CompLevel_full_profile: {
283 return loop_predicate_helper<CompLevel_full_profile>(i, b, 1.0, method);
284 }
285 default:
286 return true;
287 }
288 }
289
290 bool SimpleThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level, Method* method) {
291 switch(cur_level) {
292 case CompLevel_none:
293 case CompLevel_limited_profile: {
294 return call_predicate_helper<CompLevel_none>(i, b, 1.0, method);
295 }
296 case CompLevel_full_profile: {
297 return call_predicate_helper<CompLevel_full_profile>(i, b, 1.0, method);
298 }
299 default:
300 return true;
301 }
302 }
303
304 // Determine is a method is mature.
305 bool SimpleThresholdPolicy::is_mature(Method* method) {
306 if (is_trivial(method)) return true;
307 MethodData* mdo = method->method_data();
308 if (mdo != NULL) {
309 int i = mdo->invocation_count();
310 int b = mdo->backedge_count();
311 double k = ProfileMaturityPercentage / 100.0;
312 return call_predicate_helper<CompLevel_full_profile>(i, b, k, method) ||
313 loop_predicate_helper<CompLevel_full_profile>(i, b, k, method);
314 }
315 return false;
316 }
317
318 // Common transition function. Given a predicate determines if a method should transition to another level.
319 CompLevel SimpleThresholdPolicy::common(Predicate p, Method* method, CompLevel cur_level) {
320 CompLevel next_level = cur_level;
321 int i = method->invocation_count();
322 int b = method->backedge_count();
323
324 if (is_trivial(method)) {
325 next_level = CompLevel_simple;
326 } else {
327 switch(cur_level) {
328 case CompLevel_none:
329 // If we were at full profile level, would we switch to full opt?
330 if (common(p, method, CompLevel_full_profile) == CompLevel_full_optimization) {
331 next_level = CompLevel_full_optimization;
332 } else if ((this->*p)(i, b, cur_level, method)) {
333 next_level = CompLevel_full_profile;
334 }
335 break;
336 case CompLevel_limited_profile:
337 case CompLevel_full_profile:
338 {
339 MethodData* mdo = method->method_data();
340 if (mdo != NULL) {
341 if (mdo->would_profile()) {
342 int mdo_i = mdo->invocation_count_delta();
343 int mdo_b = mdo->backedge_count_delta();
344 if ((this->*p)(mdo_i, mdo_b, cur_level, method)) {
345 next_level = CompLevel_full_optimization;
346 }
347 } else {
421 // Use loop event as an opportunity to also check there's been
422 // enough calls.
423 CompLevel cur_level = comp_level(mh());
424 CompLevel next_level = call_event(mh(), cur_level, thread);
425 CompLevel next_osr_level = loop_event(mh(), level, thread);
426
427 next_level = MAX2(next_level,
428 next_osr_level < CompLevel_full_optimization ? next_osr_level : cur_level);
429 bool is_compiling = false;
430 if (next_level != cur_level) {
431 compile(mh, InvocationEntryBci, next_level, thread);
432 is_compiling = true;
433 }
434
435 // Do the OSR version
436 if (!is_compiling && next_osr_level != level) {
437 compile(mh, bci, next_osr_level, thread);
438 }
439 }
440 }
|
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/simpleThresholdPolicy.hpp"
30 #include "runtime/simpleThresholdPolicy.inline.hpp"
31 #include "code/scopeDesc.hpp"
32 #if INCLUDE_JVMCI
33 #include "jvmci/jvmciRuntime.hpp"
34 #endif
35
36 #ifdef TIERED
37
38 void SimpleThresholdPolicy::print_counters(const char* prefix, methodHandle mh) {
39 int invocation_count = mh->invocation_count();
40 int backedge_count = mh->backedge_count();
41 MethodData* mdh = mh->method_data();
42 int mdo_invocations = 0, mdo_backedges = 0;
43 int mdo_invocations_start = 0, mdo_backedges_start = 0;
44 if (mdh != NULL) {
45 mdo_invocations = mdh->invocation_count();
46 mdo_backedges = mdh->backedge_count();
47 mdo_invocations_start = mdh->invocation_count_start();
48 mdo_backedges_start = mdh->backedge_count_start();
49 }
50 tty->print(" %stotal=%d,%d %smdo=%d(%d),%d(%d)", prefix,
51 invocation_count, backedge_count, prefix,
52 mdo_invocations, mdo_invocations_start,
53 mdo_backedges, mdo_backedges_start);
54 tty->print(" %smax levels=%d,%d", prefix,
55 mh->highest_comp_level(), mh->highest_osr_comp_level());
56 }
226 method_invocation_event(method, inlinee, comp_level, nm, thread);
227 } else {
228 // method == inlinee if the event originated in the main method
229 method_back_branch_event(method, inlinee, bci, comp_level, nm, thread);
230 // Check if event led to a higher level OSR compilation
231 nmethod* osr_nm = inlinee->lookup_osr_nmethod_for(bci, comp_level, false);
232 if (osr_nm != NULL && osr_nm->comp_level() > comp_level) {
233 // Perform OSR with new nmethod
234 return osr_nm;
235 }
236 }
237 return NULL;
238 }
239
240 // Check if the method can be compiled, change level if necessary
241 void SimpleThresholdPolicy::compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
242 assert(level <= TieredStopAtLevel, "Invalid compilation level");
243 if (level == CompLevel_none) {
244 return;
245 }
246 if (level == CompLevel_aot) {
247 if (mh->has_aot_code()) {
248 if (PrintTieredEvents) {
249 print_event(COMPILE, mh, mh, bci, level);
250 }
251 MutexLocker ml(Compile_lock);
252 NoSafepointVerifier nsv;
253 if (mh->has_aot_code() && mh->code() != mh->aot_code()) {
254 mh->aot_code()->make_entrant();
255 if (mh->has_compiled_code()) {
256 mh->code()->make_not_entrant();
257 }
258 Method::set_code(mh, mh->aot_code());
259 }
260 }
261 return;
262 }
263
264 // Check if the method can be compiled. If it cannot be compiled with C1, continue profiling
265 // in the interpreter and then compile with C2 (the transition function will request that,
266 // see common() ). If the method cannot be compiled with C2 but still can with C1, compile it with
267 // pure C1.
268 if (!can_be_compiled(mh, level)) {
269 if (level == CompLevel_full_optimization && can_be_compiled(mh, CompLevel_simple)) {
270 compile(mh, bci, CompLevel_simple, thread);
271 }
272 return;
273 }
274 if (bci != InvocationEntryBci && mh->is_not_osr_compilable(level)) {
275 return;
276 }
277 if (!CompileBroker::compilation_is_in_queue(mh)) {
278 if (PrintTieredEvents) {
279 print_event(COMPILE, mh, mh, bci, level);
280 }
281 submit_compile(mh, bci, level, thread);
282 }
283 }
284
285 // Tell the broker to compile the method
286 void SimpleThresholdPolicy::submit_compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread) {
287 int hot_count = (bci == InvocationEntryBci) ? mh->invocation_count() : mh->backedge_count();
288 CompileBroker::compile_method(mh, bci, level, mh, hot_count, CompileTask::Reason_Tiered, thread);
289 }
290
291 // Call and loop predicates determine whether a transition to a higher
292 // compilation level should be performed (pointers to predicate functions
293 // are passed to common() transition function).
294 bool SimpleThresholdPolicy::loop_predicate(int i, int b, CompLevel cur_level, Method* method) {
295 switch(cur_level) {
296 case CompLevel_aot: {
297 return loop_predicate_helper<CompLevel_aot>(i, b, 1.0, method);
298 }
299 case CompLevel_none:
300 case CompLevel_limited_profile: {
301 return loop_predicate_helper<CompLevel_none>(i, b, 1.0, method);
302 }
303 case CompLevel_full_profile: {
304 return loop_predicate_helper<CompLevel_full_profile>(i, b, 1.0, method);
305 }
306 default:
307 return true;
308 }
309 }
310
311 bool SimpleThresholdPolicy::call_predicate(int i, int b, CompLevel cur_level, Method* method) {
312 switch(cur_level) {
313 case CompLevel_aot: {
314 return call_predicate_helper<CompLevel_aot>(i, b, 1.0, method);
315 }
316 case CompLevel_none:
317 case CompLevel_limited_profile: {
318 return call_predicate_helper<CompLevel_none>(i, b, 1.0, method);
319 }
320 case CompLevel_full_profile: {
321 return call_predicate_helper<CompLevel_full_profile>(i, b, 1.0, method);
322 }
323 default:
324 return true;
325 }
326 }
327
328 // Determine is a method is mature.
329 bool SimpleThresholdPolicy::is_mature(Method* method) {
330 if (is_trivial(method)) return true;
331 MethodData* mdo = method->method_data();
332 if (mdo != NULL) {
333 int i = mdo->invocation_count();
334 int b = mdo->backedge_count();
335 double k = ProfileMaturityPercentage / 100.0;
336 return call_predicate_helper<CompLevel_full_profile>(i, b, k, method) ||
337 loop_predicate_helper<CompLevel_full_profile>(i, b, k, method);
338 }
339 return false;
340 }
341
342 // Common transition function. Given a predicate determines if a method should transition to another level.
343 CompLevel SimpleThresholdPolicy::common(Predicate p, Method* method, CompLevel cur_level) {
344 CompLevel next_level = cur_level;
345 int i = method->invocation_count();
346 int b = method->backedge_count();
347
348 if (is_trivial(method) && cur_level != CompLevel_aot) {
349 next_level = CompLevel_simple;
350 } else {
351 switch(cur_level) {
352 case CompLevel_aot: {
353 if ((this->*p)(i, b, cur_level, method)) {
354 next_level = CompLevel_full_profile;
355 }
356 }
357 break;
358 case CompLevel_none:
359 // If we were at full profile level, would we switch to full opt?
360 if (common(p, method, CompLevel_full_profile) == CompLevel_full_optimization) {
361 next_level = CompLevel_full_optimization;
362 } else if ((this->*p)(i, b, cur_level, method)) {
363 next_level = CompLevel_full_profile;
364 }
365 break;
366 case CompLevel_limited_profile:
367 case CompLevel_full_profile:
368 {
369 MethodData* mdo = method->method_data();
370 if (mdo != NULL) {
371 if (mdo->would_profile()) {
372 int mdo_i = mdo->invocation_count_delta();
373 int mdo_b = mdo->backedge_count_delta();
374 if ((this->*p)(mdo_i, mdo_b, cur_level, method)) {
375 next_level = CompLevel_full_optimization;
376 }
377 } else {
451 // Use loop event as an opportunity to also check there's been
452 // enough calls.
453 CompLevel cur_level = comp_level(mh());
454 CompLevel next_level = call_event(mh(), cur_level, thread);
455 CompLevel next_osr_level = loop_event(mh(), level, thread);
456
457 next_level = MAX2(next_level,
458 next_osr_level < CompLevel_full_optimization ? next_osr_level : cur_level);
459 bool is_compiling = false;
460 if (next_level != cur_level) {
461 compile(mh, InvocationEntryBci, next_level, thread);
462 is_compiling = true;
463 }
464
465 // Do the OSR version
466 if (!is_compiling && next_osr_level != level) {
467 compile(mh, bci, next_osr_level, thread);
468 }
469 }
470 }
471
472 #endif
|