src/share/vm/compiler/compileBroker.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/compiler

src/share/vm/compiler/compileBroker.cpp

Print this page




  34 #include "oops/methodData.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/nativeLookup.hpp"
  38 #include "prims/whitebox.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/atomic.inline.hpp"
  41 #include "runtime/compilationPolicy.hpp"
  42 #include "runtime/init.hpp"
  43 #include "runtime/interfaceSupport.hpp"
  44 #include "runtime/javaCalls.hpp"
  45 #include "runtime/os.hpp"
  46 #include "runtime/sharedRuntime.hpp"
  47 #include "runtime/sweeper.hpp"
  48 #include "trace/tracing.hpp"
  49 #include "utilities/dtrace.hpp"
  50 #include "utilities/events.hpp"
  51 #ifdef COMPILER1
  52 #include "c1/c1_Compiler.hpp"
  53 #endif





  54 #ifdef COMPILER2
  55 #include "opto/c2compiler.hpp"
  56 #endif
  57 #ifdef SHARK
  58 #include "shark/sharkCompiler.hpp"
  59 #endif
  60 
  61 #ifdef DTRACE_ENABLED
  62 
  63 // Only bother with this argument setup if dtrace is available
  64 
  65 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)             \
  66   {                                                                      \
  67     Symbol* klass_name = (method)->klass_name();                         \
  68     Symbol* name = (method)->name();                                     \
  69     Symbol* signature = (method)->signature();                           \
  70     HOTSPOT_METHOD_COMPILE_BEGIN(                                        \
  71       (char *) comp_name, strlen(comp_name),                             \
  72       (char *) klass_name->bytes(), klass_name->utf8_length(),           \
  73       (char *) name->bytes(), name->utf8_length(),                       \


 369   print_compilation(st);
 370 }
 371 
 372 // ------------------------------------------------------------------
 373 // CompileTask::print_line
 374 void CompileTask::print_tty() {
 375   ttyLocker ttyl;  // keep the following output all in one block
 376   // print compiler name if requested
 377   if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler_name(comp_level()));
 378     print_compilation(tty);
 379 }
 380 
 381 // ------------------------------------------------------------------
 382 // CompileTask::print_compilation_impl
 383 void CompileTask::print_compilation_impl(outputStream* st, Method* method, int compile_id, int comp_level,
 384                                          bool is_osr_method, int osr_bci, bool is_blocking,
 385                                          const char* msg, bool short_form, bool cr) {
 386   if (!short_form) {
 387     st->print("%7d ", (int) st->time_stamp().milliseconds());  // print timestamp
 388   }


 389   st->print("%4d ", compile_id);    // print compilation number
 390 
 391   // For unloaded methods the transition to zombie occurs after the
 392   // method is cleared so it's impossible to report accurate
 393   // information for that case.
 394   bool is_synchronized = false;
 395   bool has_exception_handler = false;
 396   bool is_native = false;
 397   if (method != NULL) {
 398     is_synchronized       = method->is_synchronized();
 399     has_exception_handler = method->has_exception_handler();
 400     is_native             = method->is_native();
 401   }
 402   // method attributes
 403   const char compile_type   = is_osr_method                   ? '%' : ' ';
 404   const char sync_char      = is_synchronized                 ? 's' : ' ';
 405   const char exception_char = has_exception_handler           ? '!' : ' ';
 406   const char blocking_char  = is_blocking                     ? 'b' : ' ';
 407   const char native_char    = is_native                       ? 'n' : ' ';
 408 


 501   bool is_osr_method = osr_bci() != InvocationEntryBci;
 502   print_compilation_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form, cr);
 503 }
 504 
 505 // ------------------------------------------------------------------
 506 // CompileTask::log_task
 507 void CompileTask::log_task(xmlStream* log) {
 508   Thread* thread = Thread::current();
 509   methodHandle method(thread, this->method());
 510   ResourceMark rm(thread);
 511 
 512   // <task compiler='Cx' id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'>
 513   log->print(" compiler='%s' compile_id='%d'", _comp_level <= CompLevel_full_profile ? "C1" : "C2", _compile_id);
 514   if (_osr_bci != CompileBroker::standard_entry_bci) {
 515     log->print(" compile_kind='osr'");  // same as nmethod::compile_kind
 516   } // else compile_kind='c2c'
 517   if (!method.is_null())  log->method(method);
 518   if (_osr_bci != CompileBroker::standard_entry_bci) {
 519     log->print(" osr_bci='%d'", _osr_bci);
 520   }
 521   if (_comp_level != CompLevel_highest_tier) {

 522     log->print(" level='%d'", _comp_level);
 523   }
 524   if (_is_blocking) {
 525     log->print(" blocking='1'");
 526   }
 527   log->stamp();
 528 }
 529 
 530 
 531 // ------------------------------------------------------------------
 532 // CompileTask::log_task_queued
 533 void CompileTask::log_task_queued() {
 534   Thread* thread = Thread::current();
 535   ttyLocker ttyl;
 536   ResourceMark rm(thread);
 537 
 538   xtty->begin_elem("task_queued");
 539   log_task(xtty);
 540   if (_comment != NULL) {
 541     xtty->print(" comment='%s'", _comment);
 542   }
 543   if (_hot_method != NULL) {
 544     methodHandle hot(thread, _hot_method);
 545     methodHandle method(thread, _method);
 546     if (hot() != method()) {
 547       xtty->method(hot);
 548     }
 549   }
 550   if (_hot_count != 0) {
 551     xtty->print(" hot_count='%d'", _hot_count);
 552   }
 553   xtty->end_elem();
 554 }
 555 
 556 
 557 // ------------------------------------------------------------------


















 558 // CompileTask::log_task_start
 559 void CompileTask::log_task_start(CompileLog* log)   {
 560   log->begin_head("task");
 561   log_task(log);
 562   log->end_head();
 563 }
 564 
 565 
 566 // ------------------------------------------------------------------
 567 // CompileTask::log_task_done
 568 void CompileTask::log_task_done(CompileLog* log) {
 569   Thread* thread = Thread::current();
 570   methodHandle method(thread, this->method());
 571   ResourceMark rm(thread);
 572 
 573   if (!_is_success) {
 574     const char* reason = _failure_reason != NULL ? _failure_reason : "unknown";
 575     log->elem("failure reason='%s'", reason);
 576   }
 577 


 854     _perf_compiles = PerfDataManager::create_counter(SUN_CI, name,
 855                                                      PerfData::U_Events, CHECK);
 856   }
 857 }
 858 
 859 // ------------------------------------------------------------------
 860 // CompileBroker::compilation_init
 861 //
 862 // Initialize the Compilation object
 863 void CompileBroker::compilation_init() {
 864   _last_method_compiled[0] = '\0';
 865 
 866   // No need to initialize compilation system if we do not use it.
 867   if (!UseCompiler) {
 868     return;
 869   }
 870 #ifndef SHARK
 871   // Set the interface to the current compiler(s).
 872   int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
 873   int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
























 874 #ifdef COMPILER1
 875   if (c1_count > 0) {
 876     _compilers[0] = new Compiler();
 877   }
 878 #endif // COMPILER1
 879 
 880 #ifdef COMPILER2

 881   if (c2_count > 0) {
 882     _compilers[1] = new C2Compiler();
 883   }

 884 #endif // COMPILER2
 885 
 886 #else // SHARK
 887   int c1_count = 0;
 888   int c2_count = 1;
 889 
 890   _compilers[1] = new SharkCompiler();
 891 #endif // SHARK
 892 
 893   // Start the compiler thread(s) and the sweeper thread
 894   init_compiler_sweeper_threads(c1_count, c2_count);
 895   // totalTime performance counter is always created as it is required
 896   // by the implementation of java.lang.management.CompilationMBean.
 897   {
 898     EXCEPTION_MARK;
 899     _perf_total_compilation =
 900                  PerfDataManager::create_counter(JAVA_CI, "totalTime",
 901                                                  PerfData::U_Ticks, CHECK);
 902   }
 903 


1082   EXCEPTION_MARK;
1083 #if !defined(ZERO) && !defined(SHARK)
1084   assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
1085 #endif // !ZERO && !SHARK
1086   // Initialize the compilation queue
1087   if (c2_compiler_count > 0) {
1088     _c2_compile_queue  = new CompileQueue("C2 compile queue");
1089     _compilers[1]->set_num_compiler_threads(c2_compiler_count);
1090   }
1091   if (c1_compiler_count > 0) {
1092     _c1_compile_queue  = new CompileQueue("C1 compile queue");
1093     _compilers[0]->set_num_compiler_threads(c1_compiler_count);
1094   }
1095 
1096   int compiler_count = c1_compiler_count + c2_compiler_count;
1097 
1098   char name_buffer[256];
1099   const bool compiler_thread = true;
1100   for (int i = 0; i < c2_compiler_count; i++) {
1101     // Create a name for our thread.
1102     sprintf(name_buffer, "C2 CompilerThread%d", i);
1103     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1104     // Shark and C2
1105     make_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], compiler_thread, CHECK);
1106   }
1107 
1108   for (int i = c2_compiler_count; i < compiler_count; i++) {
1109     // Create a name for our thread.
1110     sprintf(name_buffer, "C1 CompilerThread%d", i);
1111     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1112     // C1
1113     make_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], compiler_thread, CHECK);
1114   }
1115 
1116   if (UsePerfData) {
1117     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
1118   }
1119 
1120   if (MethodFlushing) {
1121     // Initialize the sweeper thread
1122     make_thread("Sweeper thread", NULL, NULL, NULL, false, CHECK);


1152                                         const char* comment,
1153                                         Thread* thread) {
1154   // do nothing if compiler thread(s) is not available
1155   if (!_initialized) {
1156     return;
1157   }
1158 
1159   guarantee(!method->is_abstract(), "cannot compile abstract methods");
1160   assert(method->method_holder()->oop_is_instance(),
1161          "sanity check");
1162   assert(!method->method_holder()->is_not_initialized(),
1163          "method holder must be initialized");
1164   assert(!method->is_method_handle_intrinsic(), "do not enqueue these guys");
1165 
1166   if (CIPrintRequests) {
1167     tty->print("request: ");
1168     method->print_short_name(tty);
1169     if (osr_bci != InvocationEntryBci) {
1170       tty->print(" osr_bci: %d", osr_bci);
1171     }
1172     tty->print(" comment: %s count: %d", comment, hot_count);
1173     if (!hot_method.is_null()) {
1174       tty->print(" hot: ");
1175       if (hot_method() != method()) {
1176           hot_method->print_short_name(tty);
1177       } else {
1178         tty->print("yes");
1179       }
1180     }
1181     tty->cr();
1182   }
1183 
1184   // A request has been made for compilation.  Before we do any
1185   // real work, check to see if the method has been compiled
1186   // in the meantime with a definitive result.
1187   if (compilation_is_complete(method, osr_bci, comp_level)) {
1188     return;
1189   }
1190 
1191 #ifndef PRODUCT
1192   if (osr_bci != -1 && !FLAG_IS_DEFAULT(OSROnlyBCI)) {


1244 
1245     // We need to check again to see if the compilation has
1246     // completed.  A previous compilation may have registered
1247     // some result.
1248     if (compilation_is_complete(method, osr_bci, comp_level)) {
1249       return;
1250     }
1251 
1252     // We now know that this compilation is not pending, complete,
1253     // or prohibited.  Assign a compile_id to this compilation
1254     // and check to see if it is in our [Start..Stop) range.
1255     int compile_id = assign_compile_id(method, osr_bci);
1256     if (compile_id == 0) {
1257       // The compilation falls outside the allowed range.
1258       return;
1259     }
1260 
1261     // Should this thread wait for completion of the compile?
1262     blocking = is_compile_blocking();
1263 



































1264     // We will enter the compilation in the queue.
1265     // 14012000: Note that this sets the queued_for_compile bits in
1266     // the target method. We can now reason that a method cannot be
1267     // queued for compilation more than once, as follows:
1268     // Before a thread queues a task for compilation, it first acquires
1269     // the compile queue lock, then checks if the method's queued bits
1270     // are set or it has already been compiled. Thus there can not be two
1271     // instances of a compilation task for the same method on the
1272     // compilation queue. Consider now the case where the compilation
1273     // thread has already removed a task for that method from the queue
1274     // and is in the midst of compiling it. In this case, the
1275     // queued_for_compile bits must be set in the method (and these
1276     // will be visible to the current thread, since the bits were set
1277     // under protection of the compile queue lock, which we hold now.
1278     // When the compilation completes, the compiler thread first sets
1279     // the compilation result and then clears the queued_for_compile
1280     // bits. Neither of these actions are protected by a barrier (or done
1281     // under the protection of a lock), so the only guarantee we have
1282     // (on machines with TSO (Total Store Order)) is that these values
1283     // will update in that order. As a result, the only combinations of


1425       // pops off the appendix argument and jumps to the target (see gen_special_dispatch in SharedRuntime).
1426       //
1427       // Since normal compiled-to-compiled calls are not able to handle such a thing we MUST generate an adapter
1428       // in this case.  If we can't generate one and use it we can not execute the out-of-line method handle calls.
1429       AdapterHandlerLibrary::create_native_wrapper(method);
1430     } else {
1431       return NULL;
1432     }
1433   } else {
1434     // If the compiler is shut off due to code cache getting full
1435     // fail out now so blocking compiles dont hang the java thread
1436     if (!should_compile_new_jobs()) {
1437       CompilationPolicy::policy()->delay_compilation(method());
1438       return NULL;
1439     }
1440     compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, THREAD);
1441   }
1442 
1443   // return requested nmethod
1444   // We accept a higher level osr method
1445   return osr_bci  == InvocationEntryBci ? method->code() : method->lookup_osr_nmethod_for(osr_bci, comp_level, false);



1446 }
1447 
1448 
1449 // ------------------------------------------------------------------
1450 // CompileBroker::compilation_is_complete
1451 //
1452 // See if compilation of this method is already complete.
1453 bool CompileBroker::compilation_is_complete(methodHandle method,
1454                                             int          osr_bci,
1455                                             int          comp_level) {
1456   bool is_osr = (osr_bci != standard_entry_bci);
1457   if (is_osr) {
1458     if (method->is_not_osr_compilable(comp_level)) {
1459       return true;
1460     } else {
1461       nmethod* result = method->lookup_osr_nmethod_for(osr_bci, comp_level, true);
1462       return (result != NULL);
1463     }
1464   } else {
1465     if (method->is_not_compilable(comp_level)) {


1548     if (CIStartOSR <= id && id < CIStopOSR) {
1549       return id;
1550     }
1551   } else {
1552     id = Atomic::add(1, &_compilation_id);
1553     if (CIStart <= id && id < CIStop) {
1554       return id;
1555     }
1556   }
1557 
1558   // Method was not in the appropriate compilation range.
1559   method->set_not_compilable_quietly();
1560   return 0;
1561 #else
1562   // CICountOSR is a develop flag and set to 'false' by default. In a product built,
1563   // only _compilation_id is incremented.
1564   return Atomic::add(1, &_compilation_id);
1565 #endif
1566 }
1567 









1568 /**
1569  * Should the current thread block until this compilation request
1570  * has been fulfilled?
1571  */
1572 bool CompileBroker::is_compile_blocking() {
1573   assert(!InstanceRefKlass::owns_pending_list_lock(JavaThread::current()), "possible deadlock");
1574   return !BackgroundCompilation;
1575 }
1576 
1577 
1578 // ------------------------------------------------------------------
1579 // CompileBroker::preload_classes
1580 void CompileBroker::preload_classes(methodHandle method, TRAPS) {
1581   // Move this code over from c1_Compiler.cpp
1582   ShouldNotReachHere();
1583 }
1584 
1585 
1586 // ------------------------------------------------------------------
1587 // CompileBroker::create_compile_task


1907       tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current()));
1908 #endif
1909     ThreadInVMfromNative tivfn(JavaThread::current());
1910   }
1911 }
1912 
1913 // wrapper for CodeCache::print_summary()
1914 static void codecache_print(bool detailed)
1915 {
1916   ResourceMark rm;
1917   stringStream s;
1918   // Dump code cache  into a buffer before locking the tty,
1919   {
1920     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1921     CodeCache::print_summary(&s, detailed);
1922   }
1923   ttyLocker ttyl;
1924   tty->print("%s", s.as_string());
1925 }
1926 





























1927 // ------------------------------------------------------------------
1928 // CompileBroker::invoke_compiler_on_method
1929 //
1930 // Compile a method.
1931 //
1932 void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
1933   if (PrintCompilation) {
1934     ResourceMark rm;
1935     task->print_tty();
1936   }
1937   elapsedTimer time;
1938 
1939   CompilerThread* thread = CompilerThread::current();
1940   ResourceMark rm(thread);
1941 
1942   if (LogEvents) {
1943     _compilation_log->log_compile(thread, task);
1944   }
1945 
1946   // Common flags.


1955     // accidentally be referenced once the thread transitions to
1956     // native.  The NoHandleMark before the transition should catch
1957     // any cases where this occurs in the future.
1958     methodHandle method(thread, task->method());
1959     should_break = check_break_at(method, compile_id, is_osr);
1960     if (should_log && !CompilerOracle::should_log(method)) {
1961       should_log = false;
1962     }
1963     assert(!method->is_native(), "no longer compile natives");
1964 
1965     // Save information about this method in case of failure.
1966     set_last_compile(thread, method, is_osr, task_level);
1967 
1968     DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level));
1969   }
1970 
1971   // Allocate a new set of JNI handles.
1972   push_jni_handle_block();
1973   Method* target_handle = task->method();
1974   int compilable = ciEnv::MethodCompilable;
1975   {

1976     int system_dictionary_modification_counter;
1977     {
1978       MutexLocker locker(Compile_lock, thread);
1979       system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
1980     }














1981 
1982     NoHandleMark  nhm;
1983     ThreadToNativeFromVM ttn(thread);
1984 
1985     ciEnv ci_env(task, system_dictionary_modification_counter);
1986     if (should_break) {
1987       ci_env.set_break_at_compile(true);
1988     }
1989     if (should_log) {
1990       ci_env.set_log(thread->log());
1991     }
1992     assert(thread->env() == &ci_env, "set by ci_env");
1993     // The thread-env() field is cleared in ~CompileTaskWrapper.
1994 
1995     // Cache Jvmti state
1996     ci_env.cache_jvmti_state();
1997 
1998     // Cache DTrace flags
1999     ci_env.cache_dtrace_flags();
2000 
2001     ciMethod* target = ci_env.get_method_from_handle(target_handle);
2002 
2003     TraceTime t1("compilation", &time);
2004     EventCompilation event;
2005 
2006     AbstractCompiler *comp = compiler(task_level);
2007     if (comp == NULL) {
2008       ci_env.record_method_not_compilable("no compiler", !TieredCompilation);
2009     } else {
2010       if (WhiteBoxAPI && WhiteBox::compilation_locked) {
2011         MonitorLockerEx locker(Compilation_lock, Mutex::_no_safepoint_check_flag);
2012         while (WhiteBox::compilation_locked) {
2013           locker.wait(Mutex::_no_safepoint_check_flag);
2014         }
2015       }
2016       comp->compile_method(&ci_env, target, osr_bci);
2017     }
2018 
2019     if (!ci_env.failing() && task->code() == NULL) {
2020       //assert(false, "compiler should always document failure");
2021       // The compiler elected, without comment, not to register a result.
2022       // Do not attempt further compilations of this method.
2023       ci_env.record_method_not_compilable("compile failed", !TieredCompilation);
2024     }
2025 
2026     // Copy this bit to the enclosing block:
2027     compilable = ci_env.compilable();
2028 
2029     if (ci_env.failing()) {
2030       task->set_failure_reason(ci_env.failure_reason());
2031       ci_env.report_failure(ci_env.failure_reason());
2032       const char* retry_message = ci_env.retry_message();
2033       if (_compilation_log != NULL) {
2034         _compilation_log->log_failure(thread, task, ci_env.failure_reason(), retry_message);
2035       }
2036       if (PrintCompilation) {
2037         FormatBufferResource msg = retry_message != NULL ?
2038             err_msg_res("COMPILE SKIPPED: %s (%s)", ci_env.failure_reason(), retry_message) :
2039             err_msg_res("COMPILE SKIPPED: %s",      ci_env.failure_reason());
2040         task->print_compilation(tty, msg);
2041       }
2042     } else {
2043       task->mark_success();
2044       task->set_num_inlined_bytecodes(ci_env.num_inlined_bytecodes());
2045       if (_compilation_log != NULL) {
2046         nmethod* code = task->code();
2047         if (code != NULL) {
2048           _compilation_log->log_nmethod(thread, code);
2049         }
2050       }
2051     }
2052     // simulate crash during compilation
2053     assert(task->compile_id() != CICrashAt, "just as planned");
2054     if (event.should_commit()) {
2055       event.set_method(target->get_Method());
2056       event.set_compileID(compile_id);
2057       event.set_compileLevel(task->comp_level());
2058       event.set_succeded(task->is_success());
2059       event.set_isOsr(is_osr);
2060       event.set_codeSize((task->code() == NULL) ? 0 : task->code()->total_size());
2061       event.set_inlinedBytes(task->num_inlined_bytecodes());
2062       event.commit();
2063     }


2064   }
2065   pop_jni_handle_block();
2066 
2067   methodHandle method(thread, task->method());
2068 
2069   DTRACE_METHOD_COMPILE_END_PROBE(method, compiler_name(task_level), task->is_success());
2070 
2071   collect_statistics(thread, time, task);
2072 
2073   if (PrintCompilation && PrintCompilation2) {
2074     tty->print("%7d ", (int) tty->time_stamp().milliseconds());  // print timestamp
2075     tty->print("%4d ", compile_id);    // print compilation number
2076     tty->print("%s ", (is_osr ? "%" : " "));
2077     if (task->code() != NULL) {
2078       tty->print("size: %d(%d) ", task->code()->total_size(), task->code()->insts_size());
2079     }
2080     tty->print_cr("time: %d inlined: %d bytes", (int)time.milliseconds(), task->num_inlined_bytecodes());
2081   }
2082 
2083   if (PrintCodeCacheOnCompilation)


2294       _perf_total_bailout_count->inc();
2295     }
2296     _t_bailedout_compilation.add(time);
2297   } else if (code == NULL) {
2298     if (UsePerfData) {
2299       _perf_last_invalidated_method->set_value(counters->current_method());
2300       _perf_last_invalidated_type->set_value(counters->compile_type());
2301       _perf_total_invalidated_count->inc();
2302     }
2303     _total_invalidated_count++;
2304     _t_invalidated_compilation.add(time);
2305   } else {
2306     // Compilation succeeded
2307 
2308     // update compilation ticks - used by the implementation of
2309     // java.lang.management.CompilationMBean
2310     _perf_total_compilation->inc(time.ticks());
2311     _peak_compilation_time = time.milliseconds() > _peak_compilation_time ? time.milliseconds() : _peak_compilation_time;
2312 
2313     if (CITime) {


2314       if (is_osr) {
2315         _t_osr_compilation.add(time);
2316         _sum_osr_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();

2317       } else {
2318         _t_standard_compilation.add(time);
2319         _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();

2320       }


2321     }
2322 
2323     if (UsePerfData) {
2324       // save the name of the last method compiled
2325       _perf_last_method->set_value(counters->current_method());
2326       _perf_last_compile_type->set_value(counters->compile_type());
2327       _perf_last_compile_size->set_value(method->code_size() +
2328                                          task->num_inlined_bytecodes());
2329       if (is_osr) {
2330         _perf_osr_compilation->inc(time.ticks());
2331         _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
2332       } else {
2333         _perf_standard_compilation->inc(time.ticks());
2334         _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
2335       }
2336     }
2337 
2338     if (CITimeEach) {
2339       float bytes_per_sec = 1.0 * (method->code_size() + task->num_inlined_bytecodes()) / time.seconds();
2340       tty->print_cr("%3d   seconds: %f bytes/sec : %f (bytes %d + %d inlined)",


2356       if (UsePerfData) _perf_total_osr_compile_count->inc();
2357       _total_osr_compile_count++;
2358     } else {
2359       if (UsePerfData) _perf_total_standard_compile_count->inc();
2360       _total_standard_compile_count++;
2361     }
2362   }
2363   // set the current method for the thread to null
2364   if (UsePerfData) counters->set_current_method("");
2365 }
2366 
2367 const char* CompileBroker::compiler_name(int comp_level) {
2368   AbstractCompiler *comp = CompileBroker::compiler(comp_level);
2369   if (comp == NULL) {
2370     return "no compiler";
2371   } else {
2372     return (comp->name());
2373   }
2374 }
2375 
2376 void CompileBroker::print_times() {




















































































2377   tty->cr();
2378   tty->print_cr("Accumulated compiler times");
2379   tty->print_cr("----------------------------------------------------------");
2380                //0000000000111111111122222222223333333333444444444455555555556666666666
2381                //0123456789012345678901234567890123456789012345678901234567890123456789
2382   tty->print_cr("  Total compilation time   : %7.3f s", CompileBroker::_t_total_compilation.seconds());
2383   tty->print_cr("    Standard compilation   : %7.3f s, Average : %2.3f s",
2384                 CompileBroker::_t_standard_compilation.seconds(),
2385                 CompileBroker::_t_standard_compilation.seconds() / CompileBroker::_total_standard_compile_count);
2386   tty->print_cr("    Bailed out compilation : %7.3f s, Average : %2.3f s",
2387                 CompileBroker::_t_bailedout_compilation.seconds(),
2388                 CompileBroker::_t_bailedout_compilation.seconds() / CompileBroker::_total_bailout_count);
2389   tty->print_cr("    On stack replacement   : %7.3f s, Average : %2.3f s",
2390                 CompileBroker::_t_osr_compilation.seconds(),
2391                 CompileBroker::_t_osr_compilation.seconds() / CompileBroker::_total_osr_compile_count);
2392   tty->print_cr("    Invalidated            : %7.3f s, Average : %2.3f s",
2393                 CompileBroker::_t_invalidated_compilation.seconds(),
2394                 CompileBroker::_t_invalidated_compilation.seconds() / CompileBroker::_total_invalidated_count);
2395 
2396   AbstractCompiler *comp = compiler(CompLevel_simple);
2397   if (comp != NULL) {
2398     tty->cr();
2399     comp->print_timers();
2400   }
2401   comp = compiler(CompLevel_full_optimization);
2402   if (comp != NULL) {
2403     tty->cr();
2404     comp->print_timers();
2405   }
2406   tty->cr();
2407   tty->print_cr("  Total compiled methods    : %8d methods", CompileBroker::_total_compile_count);
2408   tty->print_cr("    Standard compilation    : %8d methods", CompileBroker::_total_standard_compile_count);
2409   tty->print_cr("    On stack replacement    : %8d methods", CompileBroker::_total_osr_compile_count);
2410   int tcb = CompileBroker::_sum_osr_bytes_compiled + CompileBroker::_sum_standard_bytes_compiled;
2411   tty->print_cr("  Total compiled bytecodes  : %8d bytes", tcb);
2412   tty->print_cr("    Standard compilation    : %8d bytes", CompileBroker::_sum_standard_bytes_compiled);
2413   tty->print_cr("    On stack replacement    : %8d bytes", CompileBroker::_sum_osr_bytes_compiled);
2414   int bps = (int)(tcb / CompileBroker::_t_total_compilation.seconds());

2415   tty->print_cr("  Average compilation speed : %8d bytes/s", bps);
2416   tty->cr();
2417   tty->print_cr("  nmethod code size         : %8d bytes", CompileBroker::_sum_nmethod_code_size);
2418   tty->print_cr("  nmethod total size        : %8d bytes", CompileBroker::_sum_nmethod_size);
2419 }
2420 
2421 // Debugging output for failure
2422 void CompileBroker::print_last_compile() {
2423   if ( _last_compile_level != CompLevel_none &&
2424        compiler(_last_compile_level) != NULL &&
2425        _last_method_compiled != NULL &&
2426        _last_compile_type != no_compile) {
2427     if (_last_compile_type == osr_compile) {
2428       tty->print_cr("Last parse:  [osr]%d+++(%d) %s",
2429                     _osr_compilation_id, _last_compile_level, _last_method_compiled);
2430     } else {
2431       tty->print_cr("Last parse:  %d+++(%d) %s",
2432                     _compilation_id, _last_compile_level, _last_method_compiled);
2433     }
2434   }
2435 }
2436 
2437 
2438 void CompileBroker::print_compiler_threads_on(outputStream* st) {


  34 #include "oops/methodData.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/nativeLookup.hpp"
  38 #include "prims/whitebox.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/atomic.inline.hpp"
  41 #include "runtime/compilationPolicy.hpp"
  42 #include "runtime/init.hpp"
  43 #include "runtime/interfaceSupport.hpp"
  44 #include "runtime/javaCalls.hpp"
  45 #include "runtime/os.hpp"
  46 #include "runtime/sharedRuntime.hpp"
  47 #include "runtime/sweeper.hpp"
  48 #include "trace/tracing.hpp"
  49 #include "utilities/dtrace.hpp"
  50 #include "utilities/events.hpp"
  51 #ifdef COMPILER1
  52 #include "c1/c1_Compiler.hpp"
  53 #endif
  54 #if INCLUDE_JVMCI
  55 #include "jvmci/jvmciCompiler.hpp"
  56 #include "jvmci/jvmciRuntime.hpp"
  57 #include "runtime/vframe.hpp"
  58 #endif
  59 #ifdef COMPILER2
  60 #include "opto/c2compiler.hpp"
  61 #endif
  62 #ifdef SHARK
  63 #include "shark/sharkCompiler.hpp"
  64 #endif
  65 
  66 #ifdef DTRACE_ENABLED
  67 
  68 // Only bother with this argument setup if dtrace is available
  69 
  70 #define DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, comp_name)             \
  71   {                                                                      \
  72     Symbol* klass_name = (method)->klass_name();                         \
  73     Symbol* name = (method)->name();                                     \
  74     Symbol* signature = (method)->signature();                           \
  75     HOTSPOT_METHOD_COMPILE_BEGIN(                                        \
  76       (char *) comp_name, strlen(comp_name),                             \
  77       (char *) klass_name->bytes(), klass_name->utf8_length(),           \
  78       (char *) name->bytes(), name->utf8_length(),                       \


 374   print_compilation(st);
 375 }
 376 
 377 // ------------------------------------------------------------------
 378 // CompileTask::print_line
 379 void CompileTask::print_tty() {
 380   ttyLocker ttyl;  // keep the following output all in one block
 381   // print compiler name if requested
 382   if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler_name(comp_level()));
 383     print_compilation(tty);
 384 }
 385 
 386 // ------------------------------------------------------------------
 387 // CompileTask::print_compilation_impl
 388 void CompileTask::print_compilation_impl(outputStream* st, Method* method, int compile_id, int comp_level,
 389                                          bool is_osr_method, int osr_bci, bool is_blocking,
 390                                          const char* msg, bool short_form, bool cr) {
 391   if (!short_form) {
 392     st->print("%7d ", (int) st->time_stamp().milliseconds());  // print timestamp
 393   }
 394   // print compiler name if requested
 395   if (CIPrintCompilerName) st->print("%s:", CompileBroker::compiler_name(comp_level));
 396   st->print("%4d ", compile_id);    // print compilation number
 397 
 398   // For unloaded methods the transition to zombie occurs after the
 399   // method is cleared so it's impossible to report accurate
 400   // information for that case.
 401   bool is_synchronized = false;
 402   bool has_exception_handler = false;
 403   bool is_native = false;
 404   if (method != NULL) {
 405     is_synchronized       = method->is_synchronized();
 406     has_exception_handler = method->has_exception_handler();
 407     is_native             = method->is_native();
 408   }
 409   // method attributes
 410   const char compile_type   = is_osr_method                   ? '%' : ' ';
 411   const char sync_char      = is_synchronized                 ? 's' : ' ';
 412   const char exception_char = has_exception_handler           ? '!' : ' ';
 413   const char blocking_char  = is_blocking                     ? 'b' : ' ';
 414   const char native_char    = is_native                       ? 'n' : ' ';
 415 


 508   bool is_osr_method = osr_bci() != InvocationEntryBci;
 509   print_compilation_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form, cr);
 510 }
 511 
 512 // ------------------------------------------------------------------
 513 // CompileTask::log_task
 514 void CompileTask::log_task(xmlStream* log) {
 515   Thread* thread = Thread::current();
 516   methodHandle method(thread, this->method());
 517   ResourceMark rm(thread);
 518 
 519   // <task compiler='Cx' id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'>
 520   log->print(" compiler='%s' compile_id='%d'", _comp_level <= CompLevel_full_profile ? "C1" : "C2", _compile_id);
 521   if (_osr_bci != CompileBroker::standard_entry_bci) {
 522     log->print(" compile_kind='osr'");  // same as nmethod::compile_kind
 523   } // else compile_kind='c2c'
 524   if (!method.is_null())  log->method(method);
 525   if (_osr_bci != CompileBroker::standard_entry_bci) {
 526     log->print(" osr_bci='%d'", _osr_bci);
 527   }
 528   // Always print the level in tiered.
 529   if (_comp_level != CompLevel_highest_tier || TieredCompilation) {
 530     log->print(" level='%d'", _comp_level);
 531   }
 532   if (_is_blocking) {
 533     log->print(" blocking='1'");
 534   }
 535   log->stamp();
 536 }
 537 
 538 
 539 // ------------------------------------------------------------------
 540 // CompileTask::log_task_queued
 541 void CompileTask::log_task_queued() {
 542   Thread* thread = Thread::current();
 543   ttyLocker ttyl;
 544   ResourceMark rm(thread);
 545 
 546   xtty->begin_elem("task_queued");
 547   log_task(xtty);
 548   if (_comment != NULL) {
 549     xtty->print(" comment='%s'", _comment);
 550   }
 551   if (_hot_method != NULL) {
 552     methodHandle hot(thread, _hot_method);
 553     methodHandle method(thread, _method);
 554     if (hot() != method()) {
 555       xtty->method(hot);
 556     }
 557   }
 558   if (_hot_count != 0) {
 559     xtty->print(" hot_count='%d'", _hot_count);
 560   }
 561   xtty->end_elem();
 562 }
 563 
 564 
 565 // ------------------------------------------------------------------
 566 // CompileTask::log_task_dequeued
 567 void CompileTask::log_task_dequeued(const char* comment) {
 568   if (LogCompilation && xtty != NULL) {
 569     Thread* thread = Thread::current();
 570     ttyLocker ttyl;
 571     ResourceMark rm(thread);
 572 
 573     xtty->begin_elem("task_dequeued");
 574     log_task(xtty);
 575     if (comment != NULL) {
 576       xtty->print(" comment='%s'", comment);
 577     }
 578     xtty->end_elem();
 579   }
 580 }
 581 
 582 
 583 // ------------------------------------------------------------------
 584 // CompileTask::log_task_start
 585 void CompileTask::log_task_start(CompileLog* log)   {
 586   log->begin_head("task");
 587   log_task(log);
 588   log->end_head();
 589 }
 590 
 591 
 592 // ------------------------------------------------------------------
 593 // CompileTask::log_task_done
 594 void CompileTask::log_task_done(CompileLog* log) {
 595   Thread* thread = Thread::current();
 596   methodHandle method(thread, this->method());
 597   ResourceMark rm(thread);
 598 
 599   if (!_is_success) {
 600     const char* reason = _failure_reason != NULL ? _failure_reason : "unknown";
 601     log->elem("failure reason='%s'", reason);
 602   }
 603 


 880     _perf_compiles = PerfDataManager::create_counter(SUN_CI, name,
 881                                                      PerfData::U_Events, CHECK);
 882   }
 883 }
 884 
 885 // ------------------------------------------------------------------
 886 // CompileBroker::compilation_init
 887 //
 888 // Initialize the Compilation object
 889 void CompileBroker::compilation_init() {
 890   _last_method_compiled[0] = '\0';
 891 
 892   // No need to initialize compilation system if we do not use it.
 893   if (!UseCompiler) {
 894     return;
 895   }
 896 #ifndef SHARK
 897   // Set the interface to the current compiler(s).
 898   int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple);
 899   int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization);
 900 
 901 #if INCLUDE_JVMCI
 902   if (EnableJVMCI) {
 903     // This is creating a JVMCICompiler singleton.
 904     JVMCICompiler* jvmci = new JVMCICompiler();
 905 
 906     if (UseJVMCICompiler) {
 907       _compilers[1] = jvmci;
 908       if (FLAG_IS_DEFAULT(JVMCIThreads)) {
 909         if (BootstrapJVMCI) {
 910           // JVMCI will bootstrap so give it more threads
 911           c2_count = MIN2(32, os::active_processor_count());
 912         }
 913       } else {
 914         c2_count = JVMCIThreads;
 915       }
 916       if (FLAG_IS_DEFAULT(JVMCIHostThreads)) {
 917       } else {
 918         c1_count = JVMCIHostThreads;
 919       }
 920     }
 921   }
 922 #endif
 923 
 924 #ifdef COMPILER1
 925   if (c1_count > 0) {
 926     _compilers[0] = new Compiler();
 927   }
 928 #endif // COMPILER1
 929 
 930 #ifdef COMPILER2
 931   if (true JVMCI_ONLY( && !UseJVMCICompiler)) {
 932     if (c2_count > 0) {
 933       _compilers[1] = new C2Compiler();
 934     }
 935   }
 936 #endif // COMPILER2
 937 
 938 #else // SHARK
 939   int c1_count = 0;
 940   int c2_count = 1;
 941 
 942   _compilers[1] = new SharkCompiler();
 943 #endif // SHARK
 944 
 945   // Start the compiler thread(s) and the sweeper thread
 946   init_compiler_sweeper_threads(c1_count, c2_count);
 947   // totalTime performance counter is always created as it is required
 948   // by the implementation of java.lang.management.CompilationMBean.
 949   {
 950     EXCEPTION_MARK;
 951     _perf_total_compilation =
 952                  PerfDataManager::create_counter(JAVA_CI, "totalTime",
 953                                                  PerfData::U_Ticks, CHECK);
 954   }
 955 


1134   EXCEPTION_MARK;
1135 #if !defined(ZERO) && !defined(SHARK)
1136   assert(c2_compiler_count > 0 || c1_compiler_count > 0, "No compilers?");
1137 #endif // !ZERO && !SHARK
1138   // Initialize the compilation queue
1139   if (c2_compiler_count > 0) {
1140     _c2_compile_queue  = new CompileQueue("C2 compile queue");
1141     _compilers[1]->set_num_compiler_threads(c2_compiler_count);
1142   }
1143   if (c1_compiler_count > 0) {
1144     _c1_compile_queue  = new CompileQueue("C1 compile queue");
1145     _compilers[0]->set_num_compiler_threads(c1_compiler_count);
1146   }
1147 
1148   int compiler_count = c1_compiler_count + c2_compiler_count;
1149 
1150   char name_buffer[256];
1151   const bool compiler_thread = true;
1152   for (int i = 0; i < c2_compiler_count; i++) {
1153     // Create a name for our thread.
1154     sprintf(name_buffer, "%s CompilerThread%d", _compilers[1]->name(), i);
1155     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1156     // Shark and C2
1157     make_thread(name_buffer, _c2_compile_queue, counters, _compilers[1], compiler_thread, CHECK);
1158   }
1159 
1160   for (int i = c2_compiler_count; i < compiler_count; i++) {
1161     // Create a name for our thread.
1162     sprintf(name_buffer, "C1 CompilerThread%d", i);
1163     CompilerCounters* counters = new CompilerCounters("compilerThread", i, CHECK);
1164     // C1
1165     make_thread(name_buffer, _c1_compile_queue, counters, _compilers[0], compiler_thread, CHECK);
1166   }
1167 
1168   if (UsePerfData) {
1169     PerfDataManager::create_constant(SUN_CI, "threads", PerfData::U_Bytes, compiler_count, CHECK);
1170   }
1171 
1172   if (MethodFlushing) {
1173     // Initialize the sweeper thread
1174     make_thread("Sweeper thread", NULL, NULL, NULL, false, CHECK);


1204                                         const char* comment,
1205                                         Thread* thread) {
1206   // do nothing if compiler thread(s) is not available
1207   if (!_initialized) {
1208     return;
1209   }
1210 
1211   guarantee(!method->is_abstract(), "cannot compile abstract methods");
1212   assert(method->method_holder()->oop_is_instance(),
1213          "sanity check");
1214   assert(!method->method_holder()->is_not_initialized(),
1215          "method holder must be initialized");
1216   assert(!method->is_method_handle_intrinsic(), "do not enqueue these guys");
1217 
1218   if (CIPrintRequests) {
1219     tty->print("request: ");
1220     method->print_short_name(tty);
1221     if (osr_bci != InvocationEntryBci) {
1222       tty->print(" osr_bci: %d", osr_bci);
1223     }
1224     tty->print(" level: %d comment: %s count: %d", comp_level, comment, hot_count);
1225     if (!hot_method.is_null()) {
1226       tty->print(" hot: ");
1227       if (hot_method() != method()) {
1228           hot_method->print_short_name(tty);
1229       } else {
1230         tty->print("yes");
1231       }
1232     }
1233     tty->cr();
1234   }
1235 
1236   // A request has been made for compilation.  Before we do any
1237   // real work, check to see if the method has been compiled
1238   // in the meantime with a definitive result.
1239   if (compilation_is_complete(method, osr_bci, comp_level)) {
1240     return;
1241   }
1242 
1243 #ifndef PRODUCT
1244   if (osr_bci != -1 && !FLAG_IS_DEFAULT(OSROnlyBCI)) {


1296 
1297     // We need to check again to see if the compilation has
1298     // completed.  A previous compilation may have registered
1299     // some result.
1300     if (compilation_is_complete(method, osr_bci, comp_level)) {
1301       return;
1302     }
1303 
1304     // We now know that this compilation is not pending, complete,
1305     // or prohibited.  Assign a compile_id to this compilation
1306     // and check to see if it is in our [Start..Stop) range.
1307     int compile_id = assign_compile_id(method, osr_bci);
1308     if (compile_id == 0) {
1309       // The compilation falls outside the allowed range.
1310       return;
1311     }
1312 
1313     // Should this thread wait for completion of the compile?
1314     blocking = is_compile_blocking();
1315 
1316 #if INCLUDE_JVMCI
1317     if (UseJVMCICompiler) {
1318       if (blocking) {
1319         // Don't allow blocking compiles for requests triggered by JVMCI.
1320         if (thread->is_Compiler_thread()) {
1321           blocking = false;
1322         }
1323 
1324         // Don't allow blocking compiles if inside a class initializer or while performing class loading
1325         vframeStream vfst((JavaThread*) thread);
1326         for (; !vfst.at_end(); vfst.next()) {
1327           if (vfst.method()->is_static_initializer() ||
1328               (vfst.method()->method_holder()->is_subclass_of(SystemDictionary::ClassLoader_klass()) &&
1329                   vfst.method()->name() == vmSymbols::loadClass_name())) {
1330             blocking = false;
1331             break;
1332           }
1333         }
1334 
1335         // Don't allow blocking compilation requests to JVMCI
1336         // if JVMCI itself is not yet initialized
1337         if (!JVMCIRuntime::is_HotSpotJVMCIRuntime_initialized() && compiler(comp_level)->is_jvmci()) {
1338           blocking = false;
1339         }
1340 
1341         // Don't allow blocking compilation requests if we are in JVMCIRuntime::shutdown
1342         // to avoid deadlock between compiler thread(s) and threads run at shutdown
1343         // such as the DestroyJavaVM thread.
1344         if (JVMCIRuntime::shutdown_called()) {
1345           blocking = false;
1346         }
1347       }
1348     }
1349 #endif
1350 
1351     // We will enter the compilation in the queue.
1352     // 14012000: Note that this sets the queued_for_compile bits in
1353     // the target method. We can now reason that a method cannot be
1354     // queued for compilation more than once, as follows:
1355     // Before a thread queues a task for compilation, it first acquires
1356     // the compile queue lock, then checks if the method's queued bits
1357     // are set or it has already been compiled. Thus there can not be two
1358     // instances of a compilation task for the same method on the
1359     // compilation queue. Consider now the case where the compilation
1360     // thread has already removed a task for that method from the queue
1361     // and is in the midst of compiling it. In this case, the
1362     // queued_for_compile bits must be set in the method (and these
1363     // will be visible to the current thread, since the bits were set
1364     // under protection of the compile queue lock, which we hold now.
1365     // When the compilation completes, the compiler thread first sets
1366     // the compilation result and then clears the queued_for_compile
1367     // bits. Neither of these actions are protected by a barrier (or done
1368     // under the protection of a lock), so the only guarantee we have
1369     // (on machines with TSO (Total Store Order)) is that these values
1370     // will update in that order. As a result, the only combinations of


1512       // pops off the appendix argument and jumps to the target (see gen_special_dispatch in SharedRuntime).
1513       //
1514       // Since normal compiled-to-compiled calls are not able to handle such a thing we MUST generate an adapter
1515       // in this case.  If we can't generate one and use it we can not execute the out-of-line method handle calls.
1516       AdapterHandlerLibrary::create_native_wrapper(method);
1517     } else {
1518       return NULL;
1519     }
1520   } else {
1521     // If the compiler is shut off due to code cache getting full
1522     // fail out now so blocking compiles dont hang the java thread
1523     if (!should_compile_new_jobs()) {
1524       CompilationPolicy::policy()->delay_compilation(method());
1525       return NULL;
1526     }
1527     compile_method_base(method, osr_bci, comp_level, hot_method, hot_count, comment, THREAD);
1528   }
1529 
1530   // return requested nmethod
1531   // We accept a higher level osr method
1532   if (osr_bci == InvocationEntryBci) {
1533     return method->code();
1534   }
1535   return method->lookup_osr_nmethod_for(osr_bci, comp_level, false);
1536 }
1537 
1538 
1539 // ------------------------------------------------------------------
1540 // CompileBroker::compilation_is_complete
1541 //
1542 // See if compilation of this method is already complete.
1543 bool CompileBroker::compilation_is_complete(methodHandle method,
1544                                             int          osr_bci,
1545                                             int          comp_level) {
1546   bool is_osr = (osr_bci != standard_entry_bci);
1547   if (is_osr) {
1548     if (method->is_not_osr_compilable(comp_level)) {
1549       return true;
1550     } else {
1551       nmethod* result = method->lookup_osr_nmethod_for(osr_bci, comp_level, true);
1552       return (result != NULL);
1553     }
1554   } else {
1555     if (method->is_not_compilable(comp_level)) {


1638     if (CIStartOSR <= id && id < CIStopOSR) {
1639       return id;
1640     }
1641   } else {
1642     id = Atomic::add(1, &_compilation_id);
1643     if (CIStart <= id && id < CIStop) {
1644       return id;
1645     }
1646   }
1647 
1648   // Method was not in the appropriate compilation range.
1649   method->set_not_compilable_quietly();
1650   return 0;
1651 #else
1652   // CICountOSR is a develop flag and set to 'false' by default. In a product built,
1653   // only _compilation_id is incremented.
1654   return Atomic::add(1, &_compilation_id);
1655 #endif
1656 }
1657 
1658 // ------------------------------------------------------------------
1659 // CompileBroker::assign_compile_id_unlocked
1660 //
1661 // Public wrapper for assign_compile_id that acquires the needed locks
1662 uint CompileBroker::assign_compile_id_unlocked(Thread* thread, methodHandle method, int osr_bci) {
1663   MutexLocker locker(MethodCompileQueue_lock, thread);
1664   return assign_compile_id(method, osr_bci);
1665 }
1666 
1667 /**
1668  * Should the current thread block until this compilation request
1669  * has been fulfilled?
1670  */
1671 bool CompileBroker::is_compile_blocking() {
1672   assert(!InstanceRefKlass::owns_pending_list_lock(JavaThread::current()), "possible deadlock");
1673   return !BackgroundCompilation;
1674 }
1675 
1676 
1677 // ------------------------------------------------------------------
1678 // CompileBroker::preload_classes
1679 void CompileBroker::preload_classes(methodHandle method, TRAPS) {
1680   // Move this code over from c1_Compiler.cpp
1681   ShouldNotReachHere();
1682 }
1683 
1684 
1685 // ------------------------------------------------------------------
1686 // CompileBroker::create_compile_task


2006       tty->print_cr("compiler thread " INTPTR_FORMAT " poll detects block request", p2i(Thread::current()));
2007 #endif
2008     ThreadInVMfromNative tivfn(JavaThread::current());
2009   }
2010 }
2011 
2012 // wrapper for CodeCache::print_summary()
2013 static void codecache_print(bool detailed)
2014 {
2015   ResourceMark rm;
2016   stringStream s;
2017   // Dump code cache  into a buffer before locking the tty,
2018   {
2019     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
2020     CodeCache::print_summary(&s, detailed);
2021   }
2022   ttyLocker ttyl;
2023   tty->print("%s", s.as_string());
2024 }
2025 
2026 void CompileBroker::post_compile(CompilerThread* thread, CompileTask* task, EventCompilation& event, bool success, ciEnv* ci_env) {
2027 
2028   if (success) {
2029     task->mark_success();
2030     if (ci_env != NULL) {
2031       task->set_num_inlined_bytecodes(ci_env->num_inlined_bytecodes());
2032     }
2033     if (_compilation_log != NULL) {
2034       nmethod* code = task->code();
2035       if (code != NULL) {
2036         _compilation_log->log_nmethod(thread, code);
2037       }
2038     }
2039   }
2040 
2041   // simulate crash during compilation
2042   assert(task->compile_id() != CICrashAt, "just as planned");
2043   if (event.should_commit()) {
2044     event.set_method(task->method());
2045     event.set_compileID(task->compile_id());
2046     event.set_compileLevel(task->comp_level());
2047     event.set_succeded(task->is_success());
2048     event.set_isOsr(task->osr_bci() != CompileBroker::standard_entry_bci);
2049     event.set_codeSize((task->code() == NULL) ? 0 : task->code()->total_size());
2050     event.set_inlinedBytes(task->num_inlined_bytecodes());
2051     event.commit();
2052   }
2053 }
2054 
2055 // ------------------------------------------------------------------
2056 // CompileBroker::invoke_compiler_on_method
2057 //
2058 // Compile a method.
2059 //
2060 void CompileBroker::invoke_compiler_on_method(CompileTask* task) {
2061   if (PrintCompilation) {
2062     ResourceMark rm;
2063     task->print_tty();
2064   }
2065   elapsedTimer time;
2066 
2067   CompilerThread* thread = CompilerThread::current();
2068   ResourceMark rm(thread);
2069 
2070   if (LogEvents) {
2071     _compilation_log->log_compile(thread, task);
2072   }
2073 
2074   // Common flags.


2083     // accidentally be referenced once the thread transitions to
2084     // native.  The NoHandleMark before the transition should catch
2085     // any cases where this occurs in the future.
2086     methodHandle method(thread, task->method());
2087     should_break = check_break_at(method, compile_id, is_osr);
2088     if (should_log && !CompilerOracle::should_log(method)) {
2089       should_log = false;
2090     }
2091     assert(!method->is_native(), "no longer compile natives");
2092 
2093     // Save information about this method in case of failure.
2094     set_last_compile(thread, method, is_osr, task_level);
2095 
2096     DTRACE_METHOD_COMPILE_BEGIN_PROBE(method, compiler_name(task_level));
2097   }
2098 
2099   // Allocate a new set of JNI handles.
2100   push_jni_handle_block();
2101   Method* target_handle = task->method();
2102   int compilable = ciEnv::MethodCompilable;
2103   AbstractCompiler *comp = compiler(task_level);
2104 
2105   int system_dictionary_modification_counter;
2106   {
2107     MutexLocker locker(Compile_lock, thread);
2108     system_dictionary_modification_counter = SystemDictionary::number_of_modifications();
2109   }
2110 #if INCLUDE_JVMCI
2111   if (UseJVMCICompiler && comp != NULL && comp->is_jvmci()) {
2112     JVMCICompiler* jvmci = (JVMCICompiler*) comp;
2113 
2114     TraceTime t1("compilation", &time);
2115     EventCompilation event;
2116 
2117     JVMCIEnv env(task, system_dictionary_modification_counter);
2118     jvmci->compile_method(target_handle, osr_bci, &env);
2119 
2120     post_compile(thread, task, event, task->code() != NULL, NULL);
2121   } else
2122 #endif
2123   {
2124 
2125     NoHandleMark  nhm;
2126     ThreadToNativeFromVM ttn(thread);
2127 
2128     ciEnv ci_env(task, system_dictionary_modification_counter);
2129     if (should_break) {
2130       ci_env.set_break_at_compile(true);
2131     }
2132     if (should_log) {
2133       ci_env.set_log(thread->log());
2134     }
2135     assert(thread->env() == &ci_env, "set by ci_env");
2136     // The thread-env() field is cleared in ~CompileTaskWrapper.
2137 
2138     // Cache Jvmti state
2139     ci_env.cache_jvmti_state();
2140 
2141     // Cache DTrace flags
2142     ci_env.cache_dtrace_flags();
2143 
2144     ciMethod* target = ci_env.get_method_from_handle(target_handle);
2145 
2146     TraceTime t1("compilation", &time);
2147     EventCompilation event;
2148 

2149     if (comp == NULL) {
2150       ci_env.record_method_not_compilable("no compiler", !TieredCompilation);
2151     } else {
2152       if (WhiteBoxAPI && WhiteBox::compilation_locked) {
2153         MonitorLockerEx locker(Compilation_lock, Mutex::_no_safepoint_check_flag);
2154         while (WhiteBox::compilation_locked) {
2155           locker.wait(Mutex::_no_safepoint_check_flag);
2156         }
2157       }
2158       comp->compile_method(&ci_env, target, osr_bci);
2159     }
2160 
2161     if (!ci_env.failing() && task->code() == NULL) {
2162       //assert(false, "compiler should always document failure");
2163       // The compiler elected, without comment, not to register a result.
2164       // Do not attempt further compilations of this method.
2165       ci_env.record_method_not_compilable("compile failed", !TieredCompilation);
2166     }
2167 
2168     // Copy this bit to the enclosing block:
2169     compilable = ci_env.compilable();
2170 
2171     if (ci_env.failing()) {
2172       task->set_failure_reason(ci_env.failure_reason());
2173       ci_env.report_failure(ci_env.failure_reason());
2174       const char* retry_message = ci_env.retry_message();
2175       if (_compilation_log != NULL) {
2176         _compilation_log->log_failure(thread, task, ci_env.failure_reason(), retry_message);
2177       }
2178       if (PrintCompilation) {
2179         FormatBufferResource msg = retry_message != NULL ?
2180             err_msg_res("COMPILE SKIPPED: %s (%s)", ci_env.failure_reason(), retry_message) :
2181             err_msg_res("COMPILE SKIPPED: %s",      ci_env.failure_reason());
2182         task->print_compilation(tty, msg);
2183       }





















2184     }
2185 
2186     post_compile(thread, task, event, !ci_env.failing(), &ci_env);
2187   }
2188   pop_jni_handle_block();
2189 
2190   methodHandle method(thread, task->method());
2191 
2192   DTRACE_METHOD_COMPILE_END_PROBE(method, compiler_name(task_level), task->is_success());
2193 
2194   collect_statistics(thread, time, task);
2195 
2196   if (PrintCompilation && PrintCompilation2) {
2197     tty->print("%7d ", (int) tty->time_stamp().milliseconds());  // print timestamp
2198     tty->print("%4d ", compile_id);    // print compilation number
2199     tty->print("%s ", (is_osr ? "%" : " "));
2200     if (task->code() != NULL) {
2201       tty->print("size: %d(%d) ", task->code()->total_size(), task->code()->insts_size());
2202     }
2203     tty->print_cr("time: %d inlined: %d bytes", (int)time.milliseconds(), task->num_inlined_bytecodes());
2204   }
2205 
2206   if (PrintCodeCacheOnCompilation)


2417       _perf_total_bailout_count->inc();
2418     }
2419     _t_bailedout_compilation.add(time);
2420   } else if (code == NULL) {
2421     if (UsePerfData) {
2422       _perf_last_invalidated_method->set_value(counters->current_method());
2423       _perf_last_invalidated_type->set_value(counters->compile_type());
2424       _perf_total_invalidated_count->inc();
2425     }
2426     _total_invalidated_count++;
2427     _t_invalidated_compilation.add(time);
2428   } else {
2429     // Compilation succeeded
2430 
2431     // update compilation ticks - used by the implementation of
2432     // java.lang.management.CompilationMBean
2433     _perf_total_compilation->inc(time.ticks());
2434     _peak_compilation_time = time.milliseconds() > _peak_compilation_time ? time.milliseconds() : _peak_compilation_time;
2435 
2436     if (CITime) {
2437       int bytes_compiled = method->code_size() + task->num_inlined_bytecodes();
2438       JVMCI_ONLY(CompilerStatistics* stats = compiler(task->comp_level())->stats();)
2439       if (is_osr) {
2440         _t_osr_compilation.add(time);
2441         _sum_osr_bytes_compiled += bytes_compiled;
2442         JVMCI_ONLY(stats->_osr.update(time, bytes_compiled);)
2443       } else {
2444         _t_standard_compilation.add(time);
2445         _sum_standard_bytes_compiled += method->code_size() + task->num_inlined_bytecodes();
2446         JVMCI_ONLY(stats->_standard.update(time, bytes_compiled);)
2447       }
2448       JVMCI_ONLY(stats->_nmethods_size += code->total_size();)
2449       JVMCI_ONLY(stats->_nmethods_code_size += code->insts_size();)
2450     }
2451 
2452     if (UsePerfData) {
2453       // save the name of the last method compiled
2454       _perf_last_method->set_value(counters->current_method());
2455       _perf_last_compile_type->set_value(counters->compile_type());
2456       _perf_last_compile_size->set_value(method->code_size() +
2457                                          task->num_inlined_bytecodes());
2458       if (is_osr) {
2459         _perf_osr_compilation->inc(time.ticks());
2460         _perf_sum_osr_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
2461       } else {
2462         _perf_standard_compilation->inc(time.ticks());
2463         _perf_sum_standard_bytes_compiled->inc(method->code_size() + task->num_inlined_bytecodes());
2464       }
2465     }
2466 
2467     if (CITimeEach) {
2468       float bytes_per_sec = 1.0 * (method->code_size() + task->num_inlined_bytecodes()) / time.seconds();
2469       tty->print_cr("%3d   seconds: %f bytes/sec : %f (bytes %d + %d inlined)",


2485       if (UsePerfData) _perf_total_osr_compile_count->inc();
2486       _total_osr_compile_count++;
2487     } else {
2488       if (UsePerfData) _perf_total_standard_compile_count->inc();
2489       _total_standard_compile_count++;
2490     }
2491   }
2492   // set the current method for the thread to null
2493   if (UsePerfData) counters->set_current_method("");
2494 }
2495 
2496 const char* CompileBroker::compiler_name(int comp_level) {
2497   AbstractCompiler *comp = CompileBroker::compiler(comp_level);
2498   if (comp == NULL) {
2499     return "no compiler";
2500   } else {
2501     return (comp->name());
2502   }
2503 }
2504 
2505 #if INCLUDE_JVMCI
2506 void CompileBroker::print_times(AbstractCompiler* comp) {
2507   CompilerStatistics* stats = comp->stats();
2508   tty->print_cr("  %s {speed: %d bytes/s; standard: %6.3f s, %d bytes, %d methods; osr: %6.3f s, %d bytes, %d methods; nmethods_size: %d bytes; nmethods_code_size: %d bytes}",
2509                 comp->name(), stats->bytes_per_second(),
2510                 stats->_standard._time.seconds(), stats->_standard._bytes, stats->_standard._count,
2511                 stats->_osr._time.seconds(), stats->_osr._bytes, stats->_osr._count,
2512                 stats->_nmethods_size, stats->_nmethods_code_size);
2513   comp->print_timers();
2514 }
2515 #endif
2516 
2517 void CompileBroker::print_times(bool per_compiler, bool aggregate) {
2518 #if INCLUDE_JVMCI
2519   elapsedTimer standard_compilation;
2520   elapsedTimer total_compilation;
2521   elapsedTimer osr_compilation;
2522 
2523   int standard_bytes_compiled = 0;
2524   int osr_bytes_compiled = 0;
2525 
2526   int standard_compile_count = 0;
2527   int osr_compile_count = 0;
2528   int total_compile_count = 0;
2529 
2530   int nmethods_size = 0;
2531   int nmethods_code_size = 0;
2532   bool printedHeader = false;
2533 
2534   for (unsigned int i = 0; i < sizeof(_compilers) / sizeof(AbstractCompiler*); i++) {
2535     AbstractCompiler* comp = _compilers[i];
2536     if (comp != NULL) {
2537       if (per_compiler && aggregate && !printedHeader) {
2538         printedHeader = true;
2539         tty->cr();
2540         tty->print_cr("Individual compiler times (for compiled methods only)");
2541         tty->print_cr("------------------------------------------------");
2542         tty->cr();
2543       }
2544       CompilerStatistics* stats = comp->stats();
2545 
2546       standard_compilation.add(stats->_standard._time);
2547       osr_compilation.add(stats->_osr._time);
2548 
2549       standard_bytes_compiled += stats->_standard._bytes;
2550       osr_bytes_compiled += stats->_osr._bytes;
2551 
2552       standard_compile_count += stats->_standard._count;
2553       osr_compile_count += stats->_osr._count;
2554 
2555       nmethods_size += stats->_nmethods_size;
2556       nmethods_code_size += stats->_nmethods_code_size;
2557 
2558       if (per_compiler) {
2559         print_times(comp);
2560       }
2561     }
2562   }
2563   total_compile_count = osr_compile_count + standard_compile_count;
2564   total_compilation.add(osr_compilation);
2565   total_compilation.add(standard_compilation);
2566 
2567   // In hosted mode, print the JVMCI compiler specific counters manually.
2568   if (!UseJVMCICompiler) {
2569     JVMCICompiler::print_compilation_timers();
2570   }
2571 #else
2572   elapsedTimer standard_compilation = CompileBroker::_t_standard_compilation;
2573   elapsedTimer osr_compilation = CompileBroker::_t_osr_compilation;
2574   elapsedTimer total_compilation = CompileBroker::_t_total_compilation;
2575 
2576   int standard_bytes_compiled = CompileBroker::_sum_standard_bytes_compiled;
2577   int osr_bytes_compiled = CompileBroker::_sum_osr_bytes_compiled;
2578 
2579   int standard_compile_count = CompileBroker::_total_standard_compile_count;
2580   int osr_compile_count = CompileBroker::_total_osr_compile_count;
2581   int total_compile_count = CompileBroker::_total_compile_count;
2582 
2583   int nmethods_size = CompileBroker::_sum_nmethod_code_size;
2584   int nmethods_code_size = CompileBroker::_sum_nmethod_size;
2585 #endif
2586 
2587   if (!aggregate) {
2588     return;
2589   }
2590   tty->cr();
2591   tty->print_cr("Accumulated compiler times");
2592   tty->print_cr("----------------------------------------------------------");
2593                //0000000000111111111122222222223333333333444444444455555555556666666666
2594                //0123456789012345678901234567890123456789012345678901234567890123456789
2595   tty->print_cr("  Total compilation time   : %7.3f s", total_compilation.seconds());
2596   tty->print_cr("    Standard compilation   : %7.3f s, Average : %2.3f s",
2597                 standard_compilation.seconds(),
2598                 standard_compilation.seconds() / standard_compile_count);
2599   tty->print_cr("    Bailed out compilation : %7.3f s, Average : %2.3f s",
2600                 CompileBroker::_t_bailedout_compilation.seconds(),
2601                 CompileBroker::_t_bailedout_compilation.seconds() / CompileBroker::_total_bailout_count);
2602   tty->print_cr("    On stack replacement   : %7.3f s, Average : %2.3f s",
2603                 osr_compilation.seconds(),
2604                 osr_compilation.seconds() / osr_compile_count);
2605   tty->print_cr("    Invalidated            : %7.3f s, Average : %2.3f s",
2606                 CompileBroker::_t_invalidated_compilation.seconds(),
2607                 CompileBroker::_t_invalidated_compilation.seconds() / CompileBroker::_total_invalidated_count);
2608 
2609   AbstractCompiler *comp = compiler(CompLevel_simple);
2610   if (comp != NULL) {
2611     tty->cr();
2612     comp->print_timers();
2613   }
2614   comp = compiler(CompLevel_full_optimization);
2615   if (comp != NULL) {
2616     tty->cr();
2617     comp->print_timers();
2618   }
2619   tty->cr();
2620   tty->print_cr("  Total compiled methods    : %8d methods", total_compile_count);
2621   tty->print_cr("    Standard compilation    : %8d methods", standard_compile_count);
2622   tty->print_cr("    On stack replacement    : %8d methods", osr_compile_count);
2623   int tcb = osr_bytes_compiled + standard_bytes_compiled;
2624   tty->print_cr("  Total compiled bytecodes  : %8d bytes", tcb);
2625   tty->print_cr("    Standard compilation    : %8d bytes", standard_bytes_compiled);
2626   tty->print_cr("    On stack replacement    : %8d bytes", osr_bytes_compiled);
2627   double tcs = total_compilation.seconds();
2628   int bps = tcs == 0.0 ? 0 : (int)(tcb / tcs);
2629   tty->print_cr("  Average compilation speed : %8d bytes/s", bps);
2630   tty->cr();
2631   tty->print_cr("  nmethod code size         : %8d bytes", nmethods_code_size);
2632   tty->print_cr("  nmethod total size        : %8d bytes", nmethods_size);
2633 }
2634 
2635 // Debugging output for failure
2636 void CompileBroker::print_last_compile() {
2637   if ( _last_compile_level != CompLevel_none &&
2638        compiler(_last_compile_level) != NULL &&
2639        _last_method_compiled != NULL &&
2640        _last_compile_type != no_compile) {
2641     if (_last_compile_type == osr_compile) {
2642       tty->print_cr("Last parse:  [osr]%d+++(%d) %s",
2643                     _osr_compilation_id, _last_compile_level, _last_method_compiled);
2644     } else {
2645       tty->print_cr("Last parse:  %d+++(%d) %s",
2646                     _compilation_id, _last_compile_level, _last_method_compiled);
2647     }
2648   }
2649 }
2650 
2651 
2652 void CompileBroker::print_compiler_threads_on(outputStream* st) {
src/share/vm/compiler/compileBroker.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File