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