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