< prev index next >

src/hotspot/share/compiler/compileTask.hpp

Print this page

 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_COMPILER_COMPILETASK_HPP
 26 #define SHARE_COMPILER_COMPILETASK_HPP
 27 
 28 #include "ci/ciMethod.hpp"
 29 #include "code/nmethod.hpp"
 30 #include "compiler/compileLog.hpp"
 31 #include "memory/allocation.hpp"

 32 #include "utilities/xmlstream.hpp"
 33 

 34 class CompileTrainingData;
 35 class DirectiveSet;

 36 
 37 JVMCI_ONLY(class JVMCICompileState;)
 38 
 39 enum class InliningResult { SUCCESS, FAILURE };
 40 
 41 inline InliningResult inlining_result_of(bool success) {
 42   return success ? InliningResult::SUCCESS : InliningResult::FAILURE;
 43 }
 44 
 45 // CompileTask
 46 //
 47 // An entry in the compile queue.  It represents a pending or current
 48 // compilation.
 49 
 50 class CompileTask : public CHeapObj<mtCompiler> {
 51   friend class VMStructs;
 52   friend class JVMCIVMStructs;
 53 
 54  public:
 55   // Different reasons for a compilation
 56   // The order is important - mapped to reason_names[]
 57   enum CompileReason {
 58       Reason_None,
 59       Reason_InvocationCount,  // Simple/StackWalk-policy
 60       Reason_BackedgeCount,    // Simple/StackWalk-policy
 61       Reason_Tiered,           // Tiered-policy
 62       Reason_Replay,           // ciReplay
 63       Reason_Whitebox,         // Whitebox API
 64       Reason_MustBeCompiled,   // Used for -Xcomp or AlwaysCompileLoopMethods (see CompilationPolicy::must_be_compiled())
 65       Reason_Bootstrap,        // JVMCI bootstrap



 66       Reason_Count
 67   };
 68 
 69   static const char* reason_name(CompileTask::CompileReason compile_reason) {
 70     static const char* reason_names[] = {
 71       "no_reason",
 72       "count",
 73       "backedge_count",
 74       "tiered",
 75       "replay",
 76       "whitebox",
 77       "must_be_compiled",
 78       "bootstrap"



 79     };
 80     return reason_names[compile_reason];
 81   }
 82 





 83  private:
 84   static CompileTask*  _task_free_list;
 85   Monitor*             _lock;
 86   int                  _compile_id;
 87   Method*              _method;
 88   jobject              _method_holder;
 89   int                  _osr_bci;
 90   bool                 _is_complete;
 91   bool                 _is_success;

 92   bool                 _is_blocking;
 93   CodeSection::csize_t _nm_content_size;
 94   CodeSection::csize_t _nm_total_size;
 95   CodeSection::csize_t _nm_insts_size;
 96   DirectiveSet*  _directive;


 97 #if INCLUDE_JVMCI
 98   bool                 _has_waiter;
 99   // Compilation state for a blocking JVMCI compilation
100   JVMCICompileState*   _blocking_jvmci_compile_state;
101 #endif
102   int                  _waiting_count;  // See waiting_for_completion_count()
103   int                  _comp_level;
104   int                  _num_inlined_bytecodes;
105   CompileTask*         _next, *_prev;
106   bool                 _is_free;
107   // Fields used for logging why the compilation was initiated:

108   jlong                _time_queued;  // time when task was enqueued
109   jlong                _time_started; // time when compilation started



110   int                  _hot_count;    // information about its invocation counter
111   CompileReason        _compile_reason;      // more info about the task
112   const char*          _failure_reason;
113   // Specifies if _failure_reason is on the C heap.
114   bool                 _failure_reason_on_C_heap;
115   CompileTrainingData* _training_data;

116   size_t               _arena_bytes;  // peak size of temporary memory during compilation (e.g. node arenas)
117 
118  public:
119   CompileTask() : _failure_reason(nullptr), _failure_reason_on_C_heap(false) {
120     // May hold MethodCompileQueue_lock
121     _lock = new Monitor(Mutex::safepoint-1, "CompileTask_lock");
122   }
123 
124   void initialize(int compile_id, const methodHandle& method, int osr_bci, int comp_level,
125                   int hot_count,
126                   CompileTask::CompileReason compile_reason, bool is_blocking);
127 
128   static CompileTask* allocate();
129   static void         free(CompileTask* task);
130 
131   int          compile_id() const                { return _compile_id; }
132   Method*      method() const                    { return _method; }
133   int          osr_bci() const                   { return _osr_bci; }
134   bool         is_complete() const               { return _is_complete; }
135   bool         is_blocking() const               { return _is_blocking; }
136   bool         is_success() const                { return _is_success; }
137   DirectiveSet* directive() const                { return _directive; }


138   CodeSection::csize_t nm_content_size() { return _nm_content_size; }
139   void         set_nm_content_size(CodeSection::csize_t size) { _nm_content_size = size; }
140   CodeSection::csize_t nm_insts_size() { return _nm_insts_size; }
141   void         set_nm_insts_size(CodeSection::csize_t size) { _nm_insts_size = size; }
142   CodeSection::csize_t nm_total_size() { return _nm_total_size; }
143   void         set_nm_total_size(CodeSection::csize_t size) { _nm_total_size = size; }

144   bool         can_become_stale() const          {
145     switch (_compile_reason) {
146       case Reason_BackedgeCount:
147       case Reason_InvocationCount:
148       case Reason_Tiered:
149         return !_is_blocking;
150       default:
151         return false;
152     }
153   }
154 #if INCLUDE_JVMCI
155   bool         should_wait_for_compilation() const {
156     // Wait for blocking compilation to finish.
157     switch (_compile_reason) {
158         case Reason_Replay:
159         case Reason_Whitebox:
160         case Reason_Bootstrap:
161           return _is_blocking;
162         default:
163           return false;
164     }
165   }
166 
167   bool         has_waiter() const                { return _has_waiter; }
168   void         clear_waiter()                    { _has_waiter = false; }
169   JVMCICompileState* blocking_jvmci_compile_state() const { return _blocking_jvmci_compile_state; }
170   void         set_blocking_jvmci_compile_state(JVMCICompileState* state) {
171     _blocking_jvmci_compile_state = state;
172   }
173 #endif
174 
175   Monitor*     lock() const                      { return _lock; }




176 
177   // See how many threads are waiting for this task. Must have lock to read this.
178   int waiting_for_completion_count() {
179     assert(_lock->owned_by_self(), "must have lock to use waiting_for_completion_count()");
180     return _waiting_count;
181   }
182   // Indicates that a thread is waiting for this task to complete. Must have lock to use this.
183   void inc_waiting_for_completion() {
184     assert(_lock->owned_by_self(), "must have lock to use inc_waiting_for_completion()");
185     _waiting_count++;
186   }
187   // Indicates that a thread stopped waiting for this task to complete. Must have lock to use this.
188   void dec_waiting_for_completion() {
189     assert(_lock->owned_by_self(), "must have lock to use dec_waiting_for_completion()");
190     assert(_waiting_count > 0, "waiting count is not positive");
191     _waiting_count--;
192   }
193 
194   void         mark_complete()                   { _is_complete = true; }
195   void         mark_success()                    { _is_success = true; }

196   void         mark_started(jlong time)          { _time_started = time; }
197 


198   int          comp_level()                      { return _comp_level;}
199   void         set_comp_level(int comp_level)    { _comp_level = comp_level;}
200 
201   CompileReason compile_reason()                 { return _compile_reason; }
202 
203   AbstractCompiler* compiler() const;
204   CompileTask*      select_for_compilation();
205 
206   int          num_inlined_bytecodes() const     { return _num_inlined_bytecodes; }
207   void         set_num_inlined_bytecodes(int n)  { _num_inlined_bytecodes = n; }
208 


209   CompileTask* next() const                      { return _next; }
210   void         set_next(CompileTask* next)       { _next = next; }
211   CompileTask* prev() const                      { return _prev; }
212   void         set_prev(CompileTask* prev)       { _prev = prev; }
213   bool         is_free() const                   { return _is_free; }
214   void         set_is_free(bool val)             { _is_free = val; }
215   bool         is_unloaded() const;
216 
217   CompileTrainingData* training_data() const      { return _training_data; }
218   void set_training_data(CompileTrainingData* td) { _training_data = td;   }
219 
220   // RedefineClasses support
221   void         metadata_do(MetadataClosure* f);
222   void         mark_on_stack();
223 
224   void         set_arena_bytes(size_t s)         { _arena_bytes = s; }
225   size_t       arena_bytes() const               { return _arena_bytes; }
226 
227 private:
228   static void  print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
229                                       bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,


230                                       const char* msg = nullptr, bool short_form = false, bool cr = true,
231                                       jlong time_queued = 0, jlong time_started = 0);

232 
233 public:
234   void         print(outputStream* st = tty, const char* msg = nullptr, bool short_form = false, bool cr = true);
235   void         print_ul(const char* msg = nullptr);
236   static void  print(outputStream* st, const nmethod* nm, const char* msg = nullptr, bool short_form = false, bool cr = true) {
237     print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(),
238                            nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false,
239                            msg, short_form, cr);

240   }
241   static void  print_ul(const nmethod* nm, const char* msg = nullptr);
242 
243   /**
244    * @deprecated Please rely on Compile::inline_printer. Do not directly write inlining information to tty.
245    */
246   static void  print_inline_indent(int inline_level, outputStream* st = tty);
247 
248   void         print_tty();
249   void         print_line_on_error(outputStream* st, char* buf, int buflen);
250 
251   void         log_task(xmlStream* log);
252   void         log_task_queued();
253   void         log_task_start(CompileLog* log);
254   void         log_task_done(CompileLog* log);
255 
256   void         set_failure_reason(const char* reason, bool on_C_heap = false) {
257     _failure_reason = reason;
258     _failure_reason_on_C_heap = on_C_heap;
259   }

 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_COMPILER_COMPILETASK_HPP
 26 #define SHARE_COMPILER_COMPILETASK_HPP
 27 
 28 #include "ci/ciMethod.hpp"
 29 #include "code/nmethod.hpp"
 30 #include "compiler/compileLog.hpp"
 31 #include "memory/allocation.hpp"
 32 #include "runtime/mutexLocker.hpp"
 33 #include "utilities/xmlstream.hpp"
 34 
 35 class CompileQueue;
 36 class CompileTrainingData;
 37 class DirectiveSet;
 38 class AOTCodeEntry;
 39 
 40 JVMCI_ONLY(class JVMCICompileState;)
 41 
 42 enum class InliningResult { SUCCESS, FAILURE };
 43 
 44 inline InliningResult inlining_result_of(bool success) {
 45   return success ? InliningResult::SUCCESS : InliningResult::FAILURE;
 46 }
 47 
 48 // CompileTask
 49 //
 50 // An entry in the compile queue.  It represents a pending or current
 51 // compilation.
 52 
 53 class CompileTask : public CHeapObj<mtCompiler> {
 54   friend class VMStructs;
 55   friend class JVMCIVMStructs;
 56 
 57  public:
 58   // Different reasons for a compilation
 59   // The order is important - mapped to reason_names[]
 60   enum CompileReason {
 61       Reason_None,
 62       Reason_InvocationCount,  // Simple/StackWalk-policy
 63       Reason_BackedgeCount,    // Simple/StackWalk-policy
 64       Reason_Tiered,           // Tiered-policy
 65       Reason_Replay,           // ciReplay
 66       Reason_Whitebox,         // Whitebox API
 67       Reason_MustBeCompiled,   // Used for -Xcomp or AlwaysCompileLoopMethods (see CompilationPolicy::must_be_compiled())
 68       Reason_Bootstrap,        // JVMCI bootstrap
 69       Reason_Preload,          // pre-load AOT code
 70       Reason_Precompile,
 71       Reason_PrecompileForPreload,
 72       Reason_Count
 73   };
 74 
 75   static const char* reason_name(CompileTask::CompileReason compile_reason) {
 76     static const char* reason_names[] = {
 77       "no_reason",
 78       "count",
 79       "backedge_count",
 80       "tiered",
 81       "replay",
 82       "whitebox",
 83       "must_be_compiled",
 84       "bootstrap",
 85       "preload",
 86       "precompile",
 87       "precompile_for_preload",
 88     };
 89     return reason_names[compile_reason];
 90   }
 91 
 92   static bool reason_is_precompiled(CompileTask::CompileReason compile_reason) {
 93     return (compile_reason == CompileTask::Reason_Precompile) ||
 94            (compile_reason == CompileTask::Reason_PrecompileForPreload);
 95   }
 96 
 97  private:
 98   static int           _active_tasks;

 99   int                  _compile_id;
100   Method*              _method;
101   jobject              _method_holder;
102   int                  _osr_bci;
103   bool                 _is_complete;
104   bool                 _is_success;
105   bool                 _requires_online_compilation;
106   bool                 _is_blocking;
107   CodeSection::csize_t _nm_content_size;
108   CodeSection::csize_t _nm_total_size;
109   CodeSection::csize_t _nm_insts_size;
110   DirectiveSet*  _directive;
111   AbstractCompiler*    _compiler;
112   AOTCodeEntry*        _aot_code_entry;
113 #if INCLUDE_JVMCI
114   bool                 _has_waiter;
115   // Compilation state for a blocking JVMCI compilation
116   JVMCICompileState*   _blocking_jvmci_compile_state;
117 #endif
118   int                  _waiting_count;  // See waiting_for_completion_count()
119   int                  _comp_level;
120   int                  _num_inlined_bytecodes;
121   CompileTask*         _next;
122   CompileTask*         _prev;
123   // Fields used for logging why the compilation was initiated:
124   jlong                _time_created; // time when task was created
125   jlong                _time_queued;  // time when task was enqueued
126   jlong                _time_started; // time when compilation started
127   jlong                _time_finished; // time when compilation finished
128   jlong                _aot_load_start;
129   jlong                _aot_load_finish;
130   int                  _hot_count;    // information about its invocation counter
131   CompileReason        _compile_reason;      // more info about the task
132   const char*          _failure_reason;
133   // Specifies if _failure_reason is on the C heap.
134   bool                 _failure_reason_on_C_heap;
135   CompileTrainingData* _training_data;
136   CompileQueue*        _compile_queue;
137   size_t               _arena_bytes;  // peak size of temporary memory during compilation (e.g. node arenas)
138 
139  public:
140   CompileTask(int compile_id, const methodHandle& method, int osr_bci, int comp_level,
141                   int hot_count, AOTCodeEntry* aot_code_entry,
142                   CompileTask::CompileReason compile_reason,
143                   CompileQueue* compile_queue,
144                   bool requires_online_compilation, bool is_blocking);
145   ~CompileTask();
146 
147   static void         wait_for_no_active_tasks();
148 
149   int          compile_id() const                   { return _compile_id; }
150   Method*      method() const                       { return _method; }
151   int          osr_bci() const                      { return _osr_bci; }
152   bool         is_complete() const                  { return _is_complete; }
153   bool         is_blocking() const                  { return _is_blocking; }
154   bool         is_success() const                   { return _is_success; }
155   bool         is_aot() const                       { return _aot_code_entry != nullptr; }
156   void         clear_aot()                          { _aot_code_entry = nullptr; }
157   AOTCodeEntry* aot_code_entry()                    { return _aot_code_entry; }
158   bool         requires_online_compilation() const  { return _requires_online_compilation; }
159   DirectiveSet* directive() const                   { return _directive; }
160   CompileReason compile_reason() const              { return _compile_reason; }
161   CodeSection::csize_t nm_content_size() { return _nm_content_size; }
162   void         set_nm_content_size(CodeSection::csize_t size) { _nm_content_size = size; }
163   CodeSection::csize_t nm_insts_size() { return _nm_insts_size; }
164   void         set_nm_insts_size(CodeSection::csize_t size) { _nm_insts_size = size; }
165   CodeSection::csize_t nm_total_size() { return _nm_total_size; }
166   void         set_nm_total_size(CodeSection::csize_t size) { _nm_total_size = size; }
167   bool         preload() const                   { return (_compile_reason == Reason_Preload); }
168   bool         can_become_stale() const          {
169     switch (_compile_reason) {
170       case Reason_BackedgeCount:
171       case Reason_InvocationCount:
172       case Reason_Tiered:
173         return !_is_blocking;
174       default:
175         return false;
176     }
177   }
178 #if INCLUDE_JVMCI
179   bool         should_wait_for_compilation() const {
180     // Wait for blocking compilation to finish.
181     switch (_compile_reason) {
182         case Reason_Replay:
183         case Reason_Whitebox:
184         case Reason_Bootstrap:
185           return _is_blocking;
186         default:
187           return false;
188     }
189   }
190 
191   bool         has_waiter() const                { return _has_waiter; }
192   void         clear_waiter()                    { _has_waiter = false; }
193   JVMCICompileState* blocking_jvmci_compile_state() const { return _blocking_jvmci_compile_state; }
194   void         set_blocking_jvmci_compile_state(JVMCICompileState* state) {
195     _blocking_jvmci_compile_state = state;
196   }
197 #endif
198 
199   bool is_precompiled() {
200     return reason_is_precompiled(compile_reason());
201   }
202 
203   CompileQueue* compile_queue() const            { return _compile_queue; }
204 
205   // See how many threads are waiting for this task. Must have lock to read this.
206   int waiting_for_completion_count() {
207     assert(CompileTaskWait_lock->owned_by_self(), "must have lock to use waiting_for_completion_count()");
208     return _waiting_count;
209   }
210   // Indicates that a thread is waiting for this task to complete. Must have lock to use this.
211   void inc_waiting_for_completion() {
212     assert(CompileTaskWait_lock->owned_by_self(), "must have lock to use inc_waiting_for_completion()");
213     _waiting_count++;
214   }
215   // Indicates that a thread stopped waiting for this task to complete. Must have lock to use this.
216   void dec_waiting_for_completion() {
217     assert(CompileTaskWait_lock->owned_by_self(), "must have lock to use dec_waiting_for_completion()");
218     assert(_waiting_count > 0, "waiting count is not positive");
219     _waiting_count--;
220   }
221 
222   void         mark_complete()                   { _is_complete = true; }
223   void         mark_success()                    { _is_success = true; }
224   void         mark_queued(jlong time)           { _time_queued = time; }
225   void         mark_started(jlong time)          { _time_started = time; }
226   void         mark_finished(jlong time)         { _time_finished = time; }
227   void         mark_aot_load_start(jlong time)   { _aot_load_start = time; }
228   void         mark_aot_load_finish(jlong time)  { _aot_load_finish = time; }
229   int          comp_level()                      { return _comp_level;}
230   void         set_comp_level(int comp_level)    { _comp_level = comp_level;}
231 
232   CompileReason compile_reason()                 { return _compile_reason; }
233 
234   AbstractCompiler* compiler() const;
235   CompileTask*      select_for_compilation();
236 
237   int          num_inlined_bytecodes() const     { return _num_inlined_bytecodes; }
238   void         set_num_inlined_bytecodes(int n)  { _num_inlined_bytecodes = n; }
239 
240   static CompileTask* volatile* next_ptr(CompileTask& task) { return &task._next; }
241 
242   CompileTask* next() const                      { return _next; }
243   void         set_next(CompileTask* next)       { _next = next; }
244   CompileTask* prev() const                      { return _prev; }
245   void         set_prev(CompileTask* prev)       { _prev = prev; }


246   bool         is_unloaded() const;
247 
248   CompileTrainingData* training_data() const      { return _training_data; }
249   void set_training_data(CompileTrainingData* td) { _training_data = td;   }
250 
251   // RedefineClasses support
252   void         metadata_do(MetadataClosure* f);
253   void         mark_on_stack();
254 
255   void         set_arena_bytes(size_t s)         { _arena_bytes = s; }
256   size_t       arena_bytes() const               { return _arena_bytes; }
257 
258 private:
259   static void  print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
260                                       bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,
261                                       bool is_aot = false, bool is_preload = false,
262                                       const char* compiler_name = nullptr,
263                                       const char* msg = nullptr, bool short_form = false, bool cr = true,
264                                       jlong time_created = 0, jlong time_queued = 0, jlong time_started = 0, jlong time_finished = 0,
265                                       jlong aot_load_start = 0, jlong aot_load_finish = 0);
266 
267 public:
268   void         print(outputStream* st = tty, const char* msg = nullptr, bool short_form = false, bool cr = true);
269   void         print_ul(const char* msg = nullptr);
270   static void  print(outputStream* st, const nmethod* nm, const char* msg = nullptr, bool short_form = false, bool cr = true) {
271     print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(),
272                            nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false,
273                            nm->aot_code_entry() != nullptr, nm->preloaded(),
274                            nm->compiler_name(), msg, short_form, cr);
275   }
276   static void  print_ul(const nmethod* nm, const char* msg = nullptr);
277 
278   /**
279    * @deprecated Please rely on Compile::inline_printer. Do not directly write inlining information to tty.
280    */
281   static void  print_inline_indent(int inline_level, outputStream* st = tty);
282 
283   void         print_tty();
284   void         print_line_on_error(outputStream* st, char* buf, int buflen);
285 
286   void         log_task(xmlStream* log);
287   void         log_task_queued();
288   void         log_task_start(CompileLog* log);
289   void         log_task_done(CompileLog* log);
290 
291   void         set_failure_reason(const char* reason, bool on_C_heap = false) {
292     _failure_reason = reason;
293     _failure_reason_on_C_heap = on_C_heap;
294   }
< prev index next >