1 /*
  2  * Copyright (c) 1998, 2024, 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_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_precompile(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                  _comp_level;
119   int                  _num_inlined_bytecodes;
120   CompileTask*         _next;
121   CompileTask*         _prev;
122   // Fields used for logging why the compilation was initiated:
123   jlong                _time_created; // time when task was created
124   jlong                _time_queued;  // time when task was enqueued
125   jlong                _time_started; // time when compilation started
126   jlong                _time_finished; // time when compilation finished
127   jlong                _aot_load_start;
128   jlong                _aot_load_finish;
129   int                  _hot_count;    // information about its invocation counter
130   CompileReason        _compile_reason;      // more info about the task
131   const char*          _failure_reason;
132   // Specifies if _failure_reason is on the C heap.
133   bool                 _failure_reason_on_C_heap;
134   CompileTrainingData* _training_data;
135   CompileQueue*        _compile_queue;
136   size_t               _arena_bytes;  // peak size of temporary memory during compilation (e.g. node arenas)
137 
138  public:
139   CompileTask(int compile_id, const methodHandle& method, int osr_bci, int comp_level,
140                   int hot_count, AOTCodeEntry* aot_code_entry,
141                   CompileTask::CompileReason compile_reason,
142                   CompileQueue* compile_queue,
143                   bool requires_online_compilation, bool is_blocking);
144   ~CompileTask();
145 
146   static void         wait_for_no_active_tasks();
147 
148   int          compile_id() const                   { return _compile_id; }
149   Method*      method() const                       { return _method; }
150   int          osr_bci() const                      { return _osr_bci; }
151   bool         is_complete() const                  { return _is_complete; }
152   bool         is_blocking() const                  { return _is_blocking; }
153   bool         is_success() const                   { return _is_success; }
154   bool         is_aot_load() const                  { return _aot_code_entry != nullptr; }
155   void         clear_aot()                          { _aot_code_entry = nullptr; }
156   AOTCodeEntry* aot_code_entry()                    { return _aot_code_entry; }
157   bool         requires_online_compilation() const  { return _requires_online_compilation; }
158   DirectiveSet* directive() const                   { return _directive; }
159   CompileReason compile_reason() const              { return _compile_reason; }
160   CodeSection::csize_t nm_content_size() { return _nm_content_size; }
161   void         set_nm_content_size(CodeSection::csize_t size) { _nm_content_size = size; }
162   CodeSection::csize_t nm_insts_size() { return _nm_insts_size; }
163   void         set_nm_insts_size(CodeSection::csize_t size) { _nm_insts_size = size; }
164   CodeSection::csize_t nm_total_size() { return _nm_total_size; }
165   void         set_nm_total_size(CodeSection::csize_t size) { _nm_total_size = size; }
166   bool         preload() const                   { return (_compile_reason == Reason_Preload); }
167   bool         can_become_stale() const          {
168     switch (_compile_reason) {
169       case Reason_BackedgeCount:
170       case Reason_InvocationCount:
171       case Reason_Tiered:
172         return !_is_blocking;
173       default:
174         return false;
175     }
176   }
177 #if INCLUDE_JVMCI
178   bool         should_wait_for_compilation() const {
179     // Wait for blocking compilation to finish.
180     switch (_compile_reason) {
181         case Reason_Replay:
182         case Reason_Whitebox:
183         case Reason_Bootstrap:
184           return _is_blocking;
185         default:
186           return false;
187     }
188   }
189 
190   bool         has_waiter() const                { return _has_waiter; }
191   void         clear_waiter()                    { _has_waiter = false; }
192   JVMCICompileState* blocking_jvmci_compile_state() const { return _blocking_jvmci_compile_state; }
193   void         set_blocking_jvmci_compile_state(JVMCICompileState* state) {
194     _blocking_jvmci_compile_state = state;
195   }
196 #endif
197 
198   bool is_precompile() {
199     return reason_is_precompile(compile_reason());
200   }
201 
202   CompileQueue* compile_queue() const            { return _compile_queue; }
203 
204   void         mark_complete()                   { _is_complete = true; }
205   void         mark_success()                    { _is_success = true; }
206   void         mark_queued(jlong time)           { _time_queued = time; }
207   void         mark_started(jlong time)          { _time_started = time; }
208   void         mark_finished(jlong time)         { _time_finished = time; }
209   void         mark_aot_load_start(jlong time)   { _aot_load_start = time; }
210   void         mark_aot_load_finish(jlong time)  { _aot_load_finish = time; }
211   int          comp_level()                      { return _comp_level;}
212   void         set_comp_level(int comp_level)    { _comp_level = comp_level;}
213 
214   CompileReason compile_reason()                 { return _compile_reason; }
215 
216   AbstractCompiler* compiler() const;
217   CompileTask*      select_for_compilation();
218 
219   int          num_inlined_bytecodes() const     { return _num_inlined_bytecodes; }
220   void         set_num_inlined_bytecodes(int n)  { _num_inlined_bytecodes = n; }
221 
222   static CompileTask* volatile* next_ptr(CompileTask& task) { return &task._next; }
223 
224   CompileTask* next() const                      { return _next; }
225   void         set_next(CompileTask* next)       { _next = next; }
226   CompileTask* prev() const                      { return _prev; }
227   void         set_prev(CompileTask* prev)       { _prev = prev; }
228   bool         is_unloaded() const;
229 
230   CompileTrainingData* training_data() const      { return _training_data; }
231   void set_training_data(CompileTrainingData* td) { _training_data = td;   }
232 
233   // RedefineClasses support
234   void         metadata_do(MetadataClosure* f);
235   void         mark_on_stack();
236 
237   void         set_arena_bytes(size_t s)         { _arena_bytes = s; }
238   size_t       arena_bytes() const               { return _arena_bytes; }
239 
240 private:
241   static void  print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
242                                       bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,
243                                       bool is_aot = false, bool is_preload = false,
244                                       const char* compiler_name = nullptr,
245                                       const char* msg = nullptr, bool short_form = false, bool cr = true,
246                                       jlong time_created = 0, jlong time_queued = 0, jlong time_started = 0, jlong time_finished = 0,
247                                       jlong aot_load_start = 0, jlong aot_load_finish = 0);
248 
249 public:
250   void         print(outputStream* st = tty, const char* msg = nullptr, bool short_form = false, bool cr = true);
251   void         print_ul(const char* msg = nullptr);
252   static void  print(outputStream* st, const nmethod* nm, const char* msg = nullptr, bool short_form = false, bool cr = true) {
253     print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(),
254                            nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false,
255                            nm->is_aot(), nm->preloaded(),
256                            nm->compiler_name(), msg, short_form, cr);
257   }
258   static void  print_ul(const nmethod* nm, const char* msg = nullptr);
259 
260   /**
261    * @deprecated Please rely on Compile::inline_printer. Do not directly write inlining information to tty.
262    */
263   static void  print_inline_indent(int inline_level, outputStream* st = tty);
264 
265   void         print_tty();
266   void         print_line_on_error(outputStream* st, char* buf, int buflen);
267 
268   void         log_task(xmlStream* log);
269   void         log_task_queued();
270   void         log_task_start(CompileLog* log);
271   void         log_task_done(CompileLog* log);
272 
273   void         set_failure_reason(const char* reason, bool on_C_heap = false) {
274     _failure_reason = reason;
275     _failure_reason_on_C_heap = on_C_heap;
276   }
277 
278   bool         check_break_at_flags();
279 
280   static void print_inlining_header(outputStream* st, ciMethod* method, int inline_level, int bci);
281   static void print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr);
282   static void print_inline_inner_method_info(outputStream* st, ciMethod* method);
283   static void print_inlining_inner_message(outputStream* st, InliningResult result, const char* msg);
284 
285   static void print_inlining_tty(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr) {
286     print_inlining_inner(tty, method, inline_level, bci, result, msg);
287   }
288   static void print_inlining_ul(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr);
289 };
290 
291 #endif // SHARE_COMPILER_COMPILETASK_HPP