1 /*
   2  * Copyright (c) 1998, 2016, 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/compileTask.hpp"
  27 #include "compiler/compileLog.hpp"
  28 #include "compiler/compileBroker.hpp"
  29 #include "compiler/compilerDirectives.hpp"
  30 #include "memory/resourceArea.hpp"
  31 
  32 CompileTask*  CompileTask::_task_free_list = NULL;
  33 #ifdef ASSERT
  34 int CompileTask::_num_allocated_tasks = 0;
  35 #endif
  36 
  37 /**
  38  * Allocate a CompileTask, from the free list if possible.
  39  */
  40 CompileTask* CompileTask::allocate() {
  41   MutexLocker locker(CompileTaskAlloc_lock);
  42   CompileTask* task = NULL;
  43 
  44   if (_task_free_list != NULL) {
  45     task = _task_free_list;
  46     _task_free_list = task->next();
  47     task->set_next(NULL);
  48   } else {
  49     task = new CompileTask();
  50     DEBUG_ONLY(_num_allocated_tasks++;)
  51     assert (WhiteBoxAPI || JVMCI_ONLY(UseJVMCICompiler ||) _num_allocated_tasks < 10000, "Leaking compilation tasks?");
  52     task->set_next(NULL);
  53     task->set_is_free(true);
  54   }
  55   assert(task->is_free(), "Task must be free.");
  56   task->set_is_free(false);
  57   return task;
  58 }
  59 
  60 /**
  61 * Add a task to the free list.
  62 */
  63 
  64 void CompileTask::free(CompileTask* task) {
  65  MutexLocker locker(CompileTaskAlloc_lock);
  66  if (!task->is_free()) {
  67    task->set_code(NULL);
  68    assert(!task->lock()->is_locked(), "Should not be locked when freed");
  69    JNIHandles::destroy_global(task->_method_holder);
  70    JNIHandles::destroy_global(task->_hot_method_holder);
  71 
  72    task->set_is_free(true);
  73    task->set_next(_task_free_list);
  74    _task_free_list = task;
  75  }
  76 }
  77 
  78 
  79 void CompileTask::initialize(int compile_id,
  80                              const methodHandle& method,
  81                              int osr_bci,
  82                              int comp_level,
  83                              const methodHandle& hot_method,
  84                              int hot_count,
  85                              CompileTask::CompileReason compile_reason,
  86                              bool is_blocking) {
  87   assert(!_lock->is_locked(), "bad locking");
  88 
  89   _compile_id = compile_id;
  90   _method = method();
  91   _method_holder = JNIHandles::make_global(method->method_holder()->klass_holder());
  92   _osr_bci = osr_bci;
  93   _is_blocking = is_blocking;
  94   JVMCI_ONLY(_has_waiter = CompileBroker::compiler(comp_level)->is_jvmci();)
  95   JVMCI_ONLY(_jvmci_compiler_thread = NULL;)
  96   _comp_level = comp_level;
  97   _num_inlined_bytecodes = 0;
  98 
  99   _is_complete = false;
 100   _is_success = false;
 101   _code_handle = NULL;
 102 
 103   _hot_method = NULL;
 104   _hot_method_holder = NULL;
 105   _hot_count = hot_count;
 106   _time_queued = 0;  // tidy
 107   _compile_reason = compile_reason;
 108   _failure_reason = NULL;
 109 
 110   if (LogCompilation) {
 111     _time_queued = os::elapsed_counter();
 112     if (hot_method.not_null()) {
 113       if (hot_method == method) {
 114         _hot_method = _method;
 115       } else {
 116         _hot_method = hot_method();
 117         // only add loader or mirror if different from _method_holder
 118         _hot_method_holder = JNIHandles::make_global(hot_method->method_holder()->klass_holder());
 119       }
 120     }
 121   }
 122 
 123   _next = NULL;
 124 }
 125 
 126 /**
 127  * Returns the compiler for this task.
 128  */
 129 AbstractCompiler* CompileTask::compiler() {
 130   return CompileBroker::compiler(_comp_level);
 131 }
 132 
 133 // ------------------------------------------------------------------
 134 // CompileTask::code/set_code
 135 //
 136 nmethod* CompileTask::code() const {
 137   if (_code_handle == NULL)  return NULL;
 138   return _code_handle->code();
 139 }
 140 
 141 void CompileTask::set_code(nmethod* nm) {
 142   if (_code_handle == NULL && nm == NULL)  return;
 143   guarantee(_code_handle != NULL, "");
 144   _code_handle->set_code(nm);
 145   if (nm == NULL)  _code_handle = NULL;  // drop the handle also
 146 }
 147 
 148 void CompileTask::mark_on_stack() {
 149   // Mark these methods as something redefine classes cannot remove.
 150   _method->set_on_stack(true);
 151   if (_hot_method != NULL) {
 152     _hot_method->set_on_stack(true);
 153   }
 154 }
 155 
 156 // RedefineClasses support
 157 void CompileTask::metadata_do(void f(Metadata*)) {
 158   f(method());
 159   if (hot_method() != NULL && hot_method() != method()) {
 160     f(hot_method());
 161   }
 162 }
 163 
 164 // ------------------------------------------------------------------
 165 // CompileTask::print_line_on_error
 166 //
 167 // This function is called by fatal error handler when the thread
 168 // causing troubles is a compiler thread.
 169 //
 170 // Do not grab any lock, do not allocate memory.
 171 //
 172 // Otherwise it's the same as CompileTask::print_line()
 173 //
 174 void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) {
 175   // print compiler name
 176   st->print("%s:", CompileBroker::compiler_name(comp_level()));
 177   print(st);
 178 }
 179 
 180 // ------------------------------------------------------------------
 181 // CompileTask::print_tty
 182 void CompileTask::print_tty() {
 183   ttyLocker ttyl;  // keep the following output all in one block
 184   // print compiler name if requested
 185   if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler_name(comp_level()));
 186     print(tty);
 187 }
 188 
 189 // ------------------------------------------------------------------
 190 // CompileTask::print_impl
 191 void CompileTask::print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
 192                                          bool is_osr_method, int osr_bci, bool is_blocking,
 193                                          const char* msg, bool short_form, bool cr) {
 194   if (!short_form) {
 195     st->print("%7d ", (int) st->time_stamp().milliseconds());  // print timestamp
 196   }
 197   // print compiler name if requested
 198   if (CIPrintCompilerName) {
 199     st->print("%s:", CompileBroker::compiler_name(comp_level));
 200   }
 201   st->print("%4d ", compile_id);    // print compilation number
 202 
 203   // For unloaded methods the transition to zombie occurs after the
 204   // method is cleared so it's impossible to report accurate
 205   // information for that case.
 206   bool is_synchronized = false;
 207   bool has_exception_handler = false;
 208   bool is_native = false;
 209   if (method != NULL) {
 210     is_synchronized       = method->is_synchronized();
 211     has_exception_handler = method->has_exception_handler();
 212     is_native             = method->is_native();
 213   }
 214   // method attributes
 215   const char compile_type   = is_osr_method                   ? '%' : ' ';
 216   const char sync_char      = is_synchronized                 ? 's' : ' ';
 217   const char exception_char = has_exception_handler           ? '!' : ' ';
 218   const char blocking_char  = is_blocking                     ? 'b' : ' ';
 219   const char native_char    = is_native                       ? 'n' : ' ';
 220 
 221   // print method attributes
 222   st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char);
 223 
 224   if (TieredCompilation) {
 225     if (comp_level != -1)  st->print("%d ", comp_level);
 226     else                   st->print("- ");
 227   }
 228   st->print("     ");  // more indent
 229 
 230   if (method == NULL) {
 231     st->print("(method)");
 232   } else {
 233     method->print_short_name(st);
 234     if (is_osr_method) {
 235       st->print(" @ %d", osr_bci);
 236     }
 237     if (method->is_native())
 238       st->print(" (native)");
 239     else
 240       st->print(" (%d bytes)", method->code_size());
 241   }
 242 
 243   if (msg != NULL) {
 244     st->print("   %s", msg);
 245   }
 246   if (cr) {
 247     st->cr();
 248   }
 249 }
 250 
 251 void CompileTask::print_inline_indent(int inline_level, outputStream* st) {
 252   //         1234567
 253   st->print("        ");     // print timestamp
 254   //         1234
 255   st->print("     ");        // print compilation number
 256   //         %s!bn
 257   st->print("      ");       // print method attributes
 258   if (TieredCompilation) {
 259     st->print("  ");
 260   }
 261   st->print("     ");        // more indent
 262   st->print("    ");         // initial inlining indent
 263   for (int i = 0; i < inline_level; i++)  st->print("  ");
 264 }
 265 
 266 // ------------------------------------------------------------------
 267 // CompileTask::print_compilation
 268 void CompileTask::print(outputStream* st, const char* msg, bool short_form, bool cr) {
 269   bool is_osr_method = osr_bci() != InvocationEntryBci;
 270   print_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form, cr);
 271 }
 272 
 273 // ------------------------------------------------------------------
 274 // CompileTask::log_task
 275 void CompileTask::log_task(xmlStream* log) {
 276   Thread* thread = Thread::current();
 277   methodHandle method(thread, this->method());
 278   ResourceMark rm(thread);
 279 
 280   // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'>
 281   log->print(" compile_id='%d'", _compile_id);
 282   if (_osr_bci != CompileBroker::standard_entry_bci) {
 283     log->print(" compile_kind='osr'");  // same as nmethod::compile_kind
 284   } // else compile_kind='c2c'
 285   if (!method.is_null())  log->method(method);
 286   if (_osr_bci != CompileBroker::standard_entry_bci) {
 287     log->print(" osr_bci='%d'", _osr_bci);
 288   }
 289   // Always print the level in tiered.
 290   if (_comp_level != CompLevel_highest_tier || TieredCompilation) {
 291     log->print(" level='%d'", _comp_level);
 292   }
 293   if (_is_blocking) {
 294     log->print(" blocking='1'");
 295   }
 296   log->stamp();
 297 }
 298 
 299 // ------------------------------------------------------------------
 300 // CompileTask::log_task_queued
 301 void CompileTask::log_task_queued() {
 302   Thread* thread = Thread::current();
 303   ttyLocker ttyl;
 304   ResourceMark rm(thread);
 305 
 306   xtty->begin_elem("task_queued");
 307   log_task(xtty);
 308   assert(_compile_reason > CompileTask::Reason_None && _compile_reason < CompileTask::Reason_Count, "Valid values");
 309   xtty->print(" comment='%s'", reason_name(_compile_reason));
 310 
 311   if (_hot_method != NULL) {
 312     methodHandle hot(thread, _hot_method);
 313     methodHandle method(thread, _method);
 314     if (hot() != method()) {
 315       xtty->method(hot);
 316     }
 317   }
 318   if (_hot_count != 0) {
 319     xtty->print(" hot_count='%d'", _hot_count);
 320   }
 321   xtty->end_elem();
 322 }
 323 
 324 
 325 // ------------------------------------------------------------------
 326 // CompileTask::log_task_dequeued
 327 void CompileTask::log_task_dequeued(const char* comment) {
 328   if (LogCompilation && xtty != NULL) {
 329     Thread* thread = Thread::current();
 330     ttyLocker ttyl;
 331     ResourceMark rm(thread);
 332 
 333     xtty->begin_elem("task_dequeued");
 334     log_task(xtty);
 335     if (comment != NULL) {
 336       xtty->print(" comment='%s'", comment);
 337     }
 338     xtty->end_elem();
 339   }
 340 }
 341 
 342 
 343 // ------------------------------------------------------------------
 344 // CompileTask::log_task_start
 345 void CompileTask::log_task_start(CompileLog* log)   {
 346   log->begin_head("task");
 347   log_task(log);
 348   log->end_head();
 349 }
 350 
 351 
 352 // ------------------------------------------------------------------
 353 // CompileTask::log_task_done
 354 void CompileTask::log_task_done(CompileLog* log) {
 355   Thread* thread = Thread::current();
 356   methodHandle method(thread, this->method());
 357   ResourceMark rm(thread);
 358 
 359   if (!_is_success) {
 360     const char* reason = _failure_reason != NULL ? _failure_reason : "unknown";
 361     log->elem("failure reason='%s'", reason);
 362   }
 363 
 364   // <task_done ... stamp='1.234'>  </task>
 365   nmethod* nm = code();
 366   log->begin_elem("task_done success='%d' nmsize='%d' count='%d'",
 367                   _is_success, nm == NULL ? 0 : nm->content_size(),
 368                   method->invocation_count());
 369   int bec = method->backedge_count();
 370   if (bec != 0)  log->print(" backedge_count='%d'", bec);
 371   // Note:  "_is_complete" is about to be set, but is not.
 372   if (_num_inlined_bytecodes != 0) {
 373     log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);
 374   }
 375   log->stamp();
 376   log->end_elem();
 377   log->clear_identities();   // next task will have different CI
 378   log->tail("task");
 379   if (log->unflushed_count() > 2000) {
 380     log->flush();
 381   }
 382   log->mark_file_end();
 383 }
 384 
 385 // ------------------------------------------------------------------
 386 // CompileTask::check_break_at_flags
 387 bool CompileTask::check_break_at_flags() {
 388   int compile_id = this->_compile_id;
 389   bool is_osr = (_osr_bci != CompileBroker::standard_entry_bci);
 390 
 391   if (CICountOSR && is_osr && (compile_id == CIBreakAtOSR)) {
 392     return true;
 393   } else {
 394     return (compile_id == CIBreakAt);
 395   }
 396 }
 397 
 398 // ------------------------------------------------------------------
 399 // CompileTask::print_inlining
 400 void CompileTask::print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) {
 401   //         1234567
 402   st->print("        ");     // print timestamp
 403   //         1234
 404   st->print("     ");        // print compilation number
 405 
 406   // method attributes
 407   if (method->is_loaded()) {
 408     const char sync_char      = method->is_synchronized()        ? 's' : ' ';
 409     const char exception_char = method->has_exception_handlers() ? '!' : ' ';
 410     const char monitors_char  = method->has_monitor_bytecodes()  ? 'm' : ' ';
 411 
 412     // print method attributes
 413     st->print(" %c%c%c  ", sync_char, exception_char, monitors_char);
 414   } else {
 415     //         %s!bn
 416     st->print("      ");     // print method attributes
 417   }
 418 
 419   if (TieredCompilation) {
 420     st->print("  ");
 421   }
 422   st->print("     ");        // more indent
 423   st->print("    ");         // initial inlining indent
 424 
 425   for (int i = 0; i < inline_level; i++)  st->print("  ");
 426 
 427   st->print("@ %d  ", bci);  // print bci
 428   method->print_short_name(st);
 429   if (method->is_loaded())
 430     st->print(" (%d bytes)", method->code_size());
 431   else
 432     st->print(" (not loaded)");
 433 
 434   if (msg != NULL) {
 435     st->print("   %s", msg);
 436   }
 437   st->cr();
 438 }
 439 
 440