1 /*
   2  * Copyright (c) 1998, 2017, 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 #ifndef SHARE_VM_COMPILER_COMPILETASK_HPP
  26 #define SHARE_VM_COMPILER_COMPILETASK_HPP
  27 
  28 #include "code/nmethod.hpp"
  29 #include "ci/ciMethod.hpp"
  30 #include "compiler/compileLog.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "utilities/xmlstream.hpp"
  33 
  34 // CompileTask
  35 //
  36 // An entry in the compile queue.  It represents a pending or current
  37 // compilation.
  38 
  39 class CompileTask : public CHeapObj<mtCompiler> {
  40   friend class VMStructs;
  41   friend class JVMCIVMStructs;
  42 
  43  public:
  44   // Different reasons for a compilation
  45   // The order is important - Reason_Whitebox and higher can not become
  46   // stale, see CompileTask::can_become_stale()
  47   // Also mapped to reason_names[]
  48   enum CompileReason {
  49       Reason_None,
  50       Reason_InvocationCount,  // Simple/StackWalk-policy
  51       Reason_BackedgeCount,    // Simple/StackWalk-policy
  52       Reason_Tiered,           // Tiered-policy
  53       Reason_CTW,              // Compile the world
  54       Reason_Replay,           // ciReplay
  55       Reason_Whitebox,         // Whitebox API
  56       Reason_MustBeCompiled,   // Java callHelper, LinkResolver
  57       Reason_Bootstrap,        // JVMCI bootstrap
  58       Reason_Count
  59   };
  60 
  61   static const char* reason_name(CompileTask::CompileReason compile_reason) {
  62     static const char* reason_names[] = {
  63       "no_reason",
  64       "count",
  65       "backedge_count",
  66       "tiered",
  67       "CTW",
  68       "replay",
  69       "whitebox",
  70       "must_be_compiled",
  71       "bootstrap"
  72     };
  73     return reason_names[compile_reason];
  74   }
  75 
  76  private:
  77   static CompileTask* _task_free_list;
  78 #ifdef ASSERT
  79   static int          _num_allocated_tasks;
  80 #endif
  81 
  82   Monitor*     _lock;
  83   uint         _compile_id;
  84   Method*      _method;
  85   jobject      _method_holder;
  86   int          _osr_bci;
  87   bool         _is_complete;
  88   bool         _is_success;
  89   bool         _is_blocking;
  90 #if INCLUDE_JVMCI
  91   bool         _has_waiter;
  92   // Compiler thread for a blocking JVMCI compilation
  93   CompilerThread* _jvmci_compiler_thread;
  94 #endif
  95   int          _comp_level;
  96   int          _num_inlined_bytecodes;
  97   nmethodLocker* _code_handle;  // holder of eventual result
  98   CompileTask* _next, *_prev;
  99   bool         _is_free;
 100   // Fields used for logging why the compilation was initiated:
 101   jlong        _time_queued;  // in units of os::elapsed_counter()
 102   Method*      _hot_method;   // which method actually triggered this task
 103   jobject      _hot_method_holder;
 104   int          _hot_count;    // information about its invocation counter
 105   CompileReason _compile_reason;      // more info about the task
 106   const char*  _failure_reason;
 107 
 108  public:
 109   CompileTask() {
 110     _lock = new Monitor(Mutex::nonleaf+2, "CompileTaskLock");
 111   }
 112 
 113   void initialize(int compile_id, const methodHandle& method, int osr_bci, int comp_level,
 114                   const methodHandle& hot_method, int hot_count,
 115                   CompileTask::CompileReason compile_reason, bool is_blocking);
 116 
 117   static CompileTask* allocate();
 118   static void         free(CompileTask* task);
 119 
 120   int          compile_id() const                { return _compile_id; }
 121   Method*      method() const                    { return _method; }
 122   Method*      hot_method() const                { return _hot_method; }
 123   int          osr_bci() const                   { return _osr_bci; }
 124   bool         is_complete() const               { return _is_complete; }
 125   bool         is_blocking() const               { return _is_blocking; }
 126   bool         is_success() const                { return _is_success; }
 127   bool         can_become_stale() const          {
 128     switch (_compile_reason) {
 129       case Reason_BackedgeCount:
 130       case Reason_InvocationCount:
 131       case Reason_Tiered:
 132         return !_is_blocking;
 133       default:
 134         return false;
 135     }
 136   }
 137 #if INCLUDE_JVMCI
 138   bool         has_waiter() const                { return _has_waiter; }
 139   void         clear_waiter()                    { _has_waiter = false; }
 140   CompilerThread* jvmci_compiler_thread() const  { return _jvmci_compiler_thread; }
 141   void         set_jvmci_compiler_thread(CompilerThread* t) {
 142     assert(is_blocking(), "must be");
 143     assert((t == NULL) != (_jvmci_compiler_thread == NULL), "must be");
 144     _jvmci_compiler_thread = t;
 145   }
 146 #endif
 147 
 148   nmethodLocker* code_handle() const             { return _code_handle; }
 149   void         set_code_handle(nmethodLocker* l) { _code_handle = l; }
 150   nmethod*     code() const;                     // _code_handle->code()
 151   void         set_code(nmethod* nm);            // _code_handle->set_code(nm)
 152 
 153   Monitor*     lock() const                      { return _lock; }
 154 
 155   void         mark_complete()                   { _is_complete = true; }
 156   void         mark_success()                    { _is_success = true; }
 157 
 158   int          comp_level()                      { return _comp_level;}
 159   void         set_comp_level(int comp_level)    { _comp_level = comp_level;}
 160 
 161   AbstractCompiler* compiler();
 162 
 163   int          num_inlined_bytecodes() const     { return _num_inlined_bytecodes; }
 164   void         set_num_inlined_bytecodes(int n)  { _num_inlined_bytecodes = n; }
 165 
 166   CompileTask* next() const                      { return _next; }
 167   void         set_next(CompileTask* next)       { _next = next; }
 168   CompileTask* prev() const                      { return _prev; }
 169   void         set_prev(CompileTask* prev)       { _prev = prev; }
 170   bool         is_free() const                   { return _is_free; }
 171   void         set_is_free(bool val)             { _is_free = val; }
 172 
 173   // RedefineClasses support
 174   void         metadata_do(void f(Metadata*));
 175   void         mark_on_stack();
 176 
 177 private:
 178   static void  print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
 179                                       bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,
 180                                       const char* msg = NULL, bool short_form = false, bool cr = true);
 181 
 182 public:
 183   void         print(outputStream* st = tty, const char* msg = NULL, bool short_form = false, bool cr = true);
 184   void         print_ul(const char* msg = NULL){
 185     Log(jit, compilation) log;
 186     if (log.is_debug()) {
 187       print(log.debug_stream(), msg, /* short form */ true, /* cr */ true);
 188     }
 189   }
 190   static void  print(outputStream* st, const nmethod* nm, const char* msg = NULL, bool short_form = false, bool cr = true) {
 191     print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(),
 192                            nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false,
 193                            msg, short_form, cr);
 194   }
 195   static void  print_ul(const nmethod* nm, const char* msg = NULL) {
 196     Log(jit, compilation) log;
 197     if (log.is_debug()) {
 198       print_impl(log.debug_stream(), nm->method(), nm->compile_id(),
 199                  nm->comp_level(), nm->is_osr_method(),
 200                  nm->is_osr_method() ? nm->osr_entry_bci() : -1,
 201                  /*is_blocking*/ false,
 202                  msg, /* short form */ true, /* cr */ true);
 203     }
 204   }
 205 
 206   static void  print_inline_indent(int inline_level, outputStream* st = tty);
 207 
 208   void         print_tty();
 209   void         print_line_on_error(outputStream* st, char* buf, int buflen);
 210 
 211   void         log_task(xmlStream* log);
 212   void         log_task_queued();
 213   void         log_task_start(CompileLog* log);
 214   void         log_task_done(CompileLog* log);
 215 
 216   void         set_failure_reason(const char* reason) {
 217     _failure_reason = reason;
 218   }
 219 
 220   bool         check_break_at_flags();
 221 
 222   static void print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg = NULL);
 223   static void print_inlining_tty(ciMethod* method, int inline_level, int bci, const char* msg = NULL) {
 224     print_inlining_inner(tty, method, inline_level, bci, msg);
 225   }
 226   static void print_inlining_ul(ciMethod* method, int inline_level, int bci, const char* msg = NULL) {
 227     Log(jit, inlining) log;
 228     if (log.is_debug()) {
 229       print_inlining_inner(log.debug_stream(), method, inline_level, bci, msg);
 230     }
 231   }
 232 };
 233 
 234 #endif // SHARE_VM_COMPILER_COMPILETASK_HPP
--- EOF ---