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 "utilities/xmlstream.hpp"
 33 
 34 class DirectiveSet;
 35 
 36 JVMCI_ONLY(class JVMCICompileState;)
 37 
 38 enum class InliningResult { SUCCESS, FAILURE };
 39 
 40 inline InliningResult inlining_result_of(bool success) {
 41   return success ? InliningResult::SUCCESS : InliningResult::FAILURE;
 42 }
 43 
 44 // CompileTask
 45 //
 46 // An entry in the compile queue.  It represents a pending or current
 47 // compilation.
 48 
 49 class CompileTask : public CHeapObj<mtCompiler> {
 50   friend class VMStructs;
 51   friend class JVMCIVMStructs;
 52 
 53  public:
 54   // Different reasons for a compilation
 55   // The order is important - mapped to reason_names[]
 56   enum CompileReason {
 57       Reason_None,
 58       Reason_InvocationCount,  // Simple/StackWalk-policy
 59       Reason_BackedgeCount,    // Simple/StackWalk-policy
 60       Reason_Tiered,           // Tiered-policy
 61       Reason_Replay,           // ciReplay
 62       Reason_Whitebox,         // Whitebox API
 63       Reason_MustBeCompiled,   // Used for -Xcomp or AlwaysCompileLoopMethods (see CompilationPolicy::must_be_compiled())
 64       Reason_Bootstrap,        // JVMCI bootstrap
 65       Reason_Count
 66   };
 67 
 68   static const char* reason_name(CompileTask::CompileReason compile_reason) {
 69     static const char* reason_names[] = {
 70       "no_reason",
 71       "count",
 72       "backedge_count",
 73       "tiered",
 74       "replay",
 75       "whitebox",
 76       "must_be_compiled",
 77       "bootstrap"
 78     };
 79     return reason_names[compile_reason];
 80   }
 81 
 82  private:
 83   static CompileTask*  _task_free_list;
 84   Monitor*             _lock;
 85   int                  _compile_id;
 86   Method*              _method;
 87   jobject              _method_holder;
 88   int                  _osr_bci;
 89   bool                 _is_complete;
 90   bool                 _is_success;
 91   bool                 _is_blocking;
 92   CodeSection::csize_t _nm_content_size;
 93   CodeSection::csize_t _nm_total_size;
 94   CodeSection::csize_t _nm_insts_size;
 95   DirectiveSet*  _directive;
 96 #if INCLUDE_JVMCI
 97   bool                 _has_waiter;
 98   // Compilation state for a blocking JVMCI compilation
 99   JVMCICompileState*   _blocking_jvmci_compile_state;
100 #endif
101   int                  _comp_level;
102   int                  _num_inlined_bytecodes;
103   CompileTask*         _next, *_prev;
104   bool                 _is_free;
105   // Fields used for logging why the compilation was initiated:
106   jlong                _time_queued;  // time when task was enqueued
107   jlong                _time_started; // time when compilation started
108   Method*              _hot_method;   // which method actually triggered this task
109   jobject              _hot_method_holder;
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   size_t               _arena_bytes;  // peak size of temporary memory during compilation (e.g. node arenas)
116 
117  public:
118   CompileTask() : _failure_reason(nullptr), _failure_reason_on_C_heap(false) {
119     // May hold MethodCompileQueue_lock
120     _lock = new Monitor(Mutex::safepoint-1, "CompileTask_lock");
121   }
122 
123   void initialize(int compile_id, const methodHandle& method, int osr_bci, int comp_level,
124                   const methodHandle& hot_method, int hot_count,
125                   CompileTask::CompileReason compile_reason, bool is_blocking);
126 
127   static CompileTask* allocate();
128   static void         free(CompileTask* task);
129 
130   int          compile_id() const                { return _compile_id; }
131   Method*      method() const                    { return _method; }
132   Method*      hot_method() const                { return _hot_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   void         mark_complete()                   { _is_complete = true; }
178   void         mark_success()                    { _is_success = true; }
179   void         mark_started(jlong time)          { _time_started = time; }
180 
181   int          comp_level()                      { return _comp_level;}
182   void         set_comp_level(int comp_level)    { _comp_level = comp_level;}
183 
184   AbstractCompiler* compiler() const;
185   CompileTask*      select_for_compilation();
186 
187   int          num_inlined_bytecodes() const     { return _num_inlined_bytecodes; }
188   void         set_num_inlined_bytecodes(int n)  { _num_inlined_bytecodes = n; }
189 
190   CompileTask* next() const                      { return _next; }
191   void         set_next(CompileTask* next)       { _next = next; }
192   CompileTask* prev() const                      { return _prev; }
193   void         set_prev(CompileTask* prev)       { _prev = prev; }
194   bool         is_free() const                   { return _is_free; }
195   void         set_is_free(bool val)             { _is_free = val; }
196   bool         is_unloaded() const;
197 
198   // RedefineClasses support
199   void         metadata_do(MetadataClosure* f);
200   void         mark_on_stack();
201 
202   void         set_arena_bytes(size_t s)         { _arena_bytes = s; }
203   size_t       arena_bytes() const               { return _arena_bytes; }
204 
205 private:
206   static void  print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
207                                       bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,
208                                       const char* msg = nullptr, bool short_form = false, bool cr = true,
209                                       jlong time_queued = 0, jlong time_started = 0);
210 
211 public:
212   void         print(outputStream* st = tty, const char* msg = nullptr, bool short_form = false, bool cr = true);
213   void         print_ul(const char* msg = nullptr);
214   static void  print(outputStream* st, const nmethod* nm, const char* msg = nullptr, bool short_form = false, bool cr = true) {
215     print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(),
216                            nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false,
217                            msg, short_form, cr);
218   }
219   static void  print_ul(const nmethod* nm, const char* msg = nullptr);
220 
221   static void  print_inline_indent(int inline_level, outputStream* st = tty);
222 
223   void         print_tty();
224   void         print_line_on_error(outputStream* st, char* buf, int buflen);
225 
226   void         log_task(xmlStream* log);
227   void         log_task_queued();
228   void         log_task_start(CompileLog* log);
229   void         log_task_done(CompileLog* log);
230 
231   void         set_failure_reason(const char* reason, bool on_C_heap = false) {
232     _failure_reason = reason;
233     _failure_reason_on_C_heap = on_C_heap;
234   }
235 
236   bool         check_break_at_flags();
237 
238   static void print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr);
239   static void print_inlining_tty(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr) {
240     print_inlining_inner(tty, method, inline_level, bci, result, msg);
241   }
242   static void print_inlining_ul(ciMethod* method, int inline_level, int bci, InliningResult result, const char* msg = nullptr);
243 };
244 
245 #endif // SHARE_COMPILER_COMPILETASK_HPP