1 /*
  2  * Copyright (c) 1999, 2023, 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_COMPILEBROKER_HPP
 26 #define SHARE_COMPILER_COMPILEBROKER_HPP
 27 
 28 #include "ci/compilerInterface.hpp"
 29 #include "compiler/abstractCompiler.hpp"
 30 #include "compiler/compileTask.hpp"
 31 #include "compiler/compilerDirectives.hpp"
 32 #include "compiler/compilerThread.hpp"
 33 #include "runtime/atomic.hpp"
 34 #include "runtime/javaThread.hpp"
 35 #include "runtime/perfDataTypes.hpp"
 36 #include "utilities/nonblockingQueue.inline.hpp"
 37 #include "utilities/stack.hpp"
 38 #if INCLUDE_JVMCI
 39 #include "jvmci/jvmciCompiler.hpp"
 40 #endif
 41 
 42 class nmethod;
 43 
 44 // CompilerCounters
 45 //
 46 // Per Compiler Performance Counters.
 47 //
 48 class CompilerCounters : public CHeapObj<mtCompiler> {
 49 
 50   public:
 51     enum {
 52       cmname_buffer_length = 160
 53     };
 54 
 55   private:
 56 
 57     char _current_method[cmname_buffer_length];
 58     int  _compile_type;
 59 
 60   public:
 61     CompilerCounters();
 62 
 63     // these methods should be called in a thread safe context
 64 
 65     void set_current_method(const char* method) {
 66       strncpy(_current_method, method, (size_t)cmname_buffer_length-1);
 67       _current_method[cmname_buffer_length-1] = '\0';
 68     }
 69 
 70     char* current_method()                  { return _current_method; }
 71 
 72     void set_compile_type(int compile_type) {
 73       _compile_type = compile_type;
 74     }
 75 
 76     int compile_type()                       { return _compile_type; }
 77 
 78 };
 79 
 80 // CompileQueue
 81 //
 82 // A list of CompileTasks.
 83 class CompileQueue : public CHeapObj<mtCompiler> {
 84  private:
 85   const char* _name;
 86 
 87   NonblockingQueue<CompileTask, &CompileTask::next_ptr> _queue;
 88 
 89   CompileTask* _first;
 90   CompileTask* _last;
 91 
 92   CompileTask* _first_stale;
 93 
 94   Monitor* _lock;
 95 
 96   int _size;
 97   int _peak_size;
 98   uint _total_added;
 99   uint _total_removed;
100 
101   void purge_stale_tasks();
102  public:
103   CompileQueue(const char* name, Monitor* lock) {
104     _name = name;
105     _lock = lock;
106     _first = nullptr;
107     _last = nullptr;
108     _size = 0;
109     _total_added = 0;
110     _total_removed = 0;
111     _peak_size = 0;
112     _first_stale = nullptr;
113   }
114 
115   const char*  name() const                      { return _name; }
116 
117   void         add_pending(CompileTask* task);
118   void         transfer_pending();
119 
120   void         add(CompileTask* task);
121   void         remove(CompileTask* task);
122   void         remove_and_mark_stale(CompileTask* task);
123   CompileTask* first()                           { return _first; }
124   CompileTask* last()                            { return _last;  }
125 
126   CompileTask* get(CompilerThread* thread);
127 
128   bool         is_empty() const                  { return _first == nullptr; }
129   int          size()     const                  { return _size;          }
130 
131   Monitor* lock() const { return _lock; }
132 
133   int         get_peak_size()     const          { return _peak_size; }
134   uint        get_total_added()   const          { return _total_added; }
135   uint        get_total_removed() const          { return _total_removed; }
136 
137   // Redefine Classes support
138   void mark_on_stack();
139   void free_all();
140   void print_tty();
141   void print(outputStream* st = tty);
142 
143   ~CompileQueue() {
144     assert (is_empty(), " Compile Queue must be empty");
145   }
146 };
147 
148 // CompileTaskWrapper
149 //
150 // Assign this task to the current thread.  Deallocate the task
151 // when the compilation is complete.
152 class CompileTaskWrapper : StackObj {
153 public:
154   CompileTaskWrapper(CompileTask* task);
155   ~CompileTaskWrapper();
156 };
157 
158 // Compilation
159 //
160 // The broker for all compilation requests.
161 class CompileBroker: AllStatic {
162  friend class Threads;
163  friend class CompileTaskWrapper;
164 
165  public:
166   enum {
167     name_buffer_length = 100
168   };
169 
170   // Compile type Information for print_last_compile() and CompilerCounters
171   enum { no_compile, normal_compile, osr_compile, native_compile };
172   static int assign_compile_id (const methodHandle& method, int osr_bci);
173 
174 
175  private:
176   static bool _initialized;
177   static volatile bool _should_block;
178 
179   // This flag can be used to stop compilation or turn it back on
180   static volatile jint _should_compile_new_jobs;
181 
182   // The installed compiler(s)
183   static AbstractCompiler* _compilers[3];
184 
185   // The maximum numbers of compiler threads to be determined during startup.
186   static int _c1_count, _c2_count, _c3_count, _sc_count;
187 
188   // An array of compiler thread Java objects
189   static jobject *_compiler1_objects, *_compiler2_objects, *_compiler3_objects, *_sc_objects;
190 
191   // An array of compiler logs
192   static CompileLog **_compiler1_logs, **_compiler2_logs, **_compiler3_logs, **_sc_logs;
193 
194   // These counters are used for assigning id's to each compilation
195   static volatile jint _compilation_id;
196   static volatile jint _osr_compilation_id;
197   static volatile jint _native_compilation_id;
198 
199   static CompileQueue* _c3_compile_queue;
200   static CompileQueue* _c2_compile_queue;
201   static CompileQueue* _c1_compile_queue;
202   static CompileQueue* _sc1_compile_queue;
203   static CompileQueue* _sc2_compile_queue;
204 
205   // performance counters
206   static PerfCounter* _perf_total_compilation;
207   static PerfCounter* _perf_osr_compilation;
208   static PerfCounter* _perf_standard_compilation;
209 
210   static PerfCounter* _perf_total_bailout_count;
211   static PerfCounter* _perf_total_invalidated_count;
212   static PerfCounter* _perf_total_compile_count;
213   static PerfCounter* _perf_total_osr_compile_count;
214   static PerfCounter* _perf_total_standard_compile_count;
215 
216   static PerfCounter* _perf_sum_osr_bytes_compiled;
217   static PerfCounter* _perf_sum_standard_bytes_compiled;
218   static PerfCounter* _perf_sum_nmethod_size;
219   static PerfCounter* _perf_sum_nmethod_code_size;
220 
221   static PerfStringVariable* _perf_last_method;
222   static PerfStringVariable* _perf_last_failed_method;
223   static PerfStringVariable* _perf_last_invalidated_method;
224   static PerfVariable*       _perf_last_compile_type;
225   static PerfVariable*       _perf_last_compile_size;
226   static PerfVariable*       _perf_last_failed_type;
227   static PerfVariable*       _perf_last_invalidated_type;
228 
229   // Timers and counters for generating statistics
230   static elapsedTimer _t_total_compilation;
231   static elapsedTimer _t_osr_compilation;
232   static elapsedTimer _t_standard_compilation;
233   static elapsedTimer _t_invalidated_compilation;
234   static elapsedTimer _t_bailedout_compilation;
235 
236   static uint _total_compile_count;
237   static uint _total_bailout_count;
238   static uint _total_invalidated_count;
239   static uint _total_not_entrant_count;
240   static uint _total_native_compile_count;
241   static uint _total_osr_compile_count;
242   static uint _total_standard_compile_count;
243   static uint _total_compiler_stopped_count;
244   static uint _total_compiler_restarted_count;
245   static uint _sum_osr_bytes_compiled;
246   static uint _sum_standard_bytes_compiled;
247   static uint _sum_nmethod_size;
248   static uint _sum_nmethod_code_size;
249   static jlong _peak_compilation_time;
250 
251   static CompilerStatistics _stats_per_level[];
252   static CompilerStatistics _scc_stats;
253   static CompilerStatistics _scc_stats_per_level[];
254 
255   static volatile int _print_compilation_warning;
256 
257   enum ThreadType {
258     compiler_t,
259     deoptimizer_t,
260     training_replay_t
261   };
262 
263   static Handle create_thread_oop(const char* name, TRAPS);
264   static JavaThread* make_thread(ThreadType type, jobject thread_oop, CompileQueue* queue, AbstractCompiler* comp, JavaThread* THREAD);
265   static void init_compiler_threads();
266   static void possibly_add_compiler_threads(JavaThread* THREAD);
267   static bool compilation_is_prohibited(const methodHandle& method, int osr_bci, int comp_level, bool excluded);
268 
269   static CompileTask* create_compile_task(CompileQueue*       queue,
270                                           int                 compile_id,
271                                           const methodHandle& method,
272                                           int                 osr_bci,
273                                           int                 comp_level,
274                                           const methodHandle& hot_method,
275                                           int                 hot_count,
276                                           SCCEntry*           scc_entry,
277                                           CompileTask::CompileReason compile_reason,
278                                           bool                requires_online_compilation,
279                                           bool                blocking);
280   static void wait_for_completion(CompileTask* task);
281 #if INCLUDE_JVMCI
282   static bool wait_for_jvmci_completion(JVMCICompiler* comp, CompileTask* task, JavaThread* thread);
283 #endif
284 
285   static void free_buffer_blob_if_allocated(CompilerThread* thread);
286 
287   static void invoke_compiler_on_method(CompileTask* task);
288   static void handle_compile_error(CompilerThread* thread, CompileTask* task, ciEnv* ci_env,
289                                    int compilable, const char* failure_reason);
290   static void update_compile_perf_data(CompilerThread *thread, const methodHandle& method, bool is_osr);
291 
292   static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);
293 
294   static void compile_method_base(const methodHandle& method,
295                                   int osr_bci,
296                                   int comp_level,
297                                   const methodHandle& hot_method,
298                                   int hot_count,
299                                   CompileTask::CompileReason compile_reason,
300                                   bool requires_online_compilation,
301                                   bool blocking,
302                                   Thread* thread);
303 
304   static CompileQueue* compile_queue(int comp_level, bool is_scc);
305   static bool init_compiler_runtime();
306   static void shutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread);
307 
308   static SCCEntry* find_scc_entry(const methodHandle& method, int osr_bci, int comp_level,
309                                   CompileTask::CompileReason compile_reason,
310                                   bool requires_online_compilation);
311 
312 public:
313   enum {
314     // The entry bci used for non-OSR compilations.
315     standard_entry_bci = InvocationEntryBci
316   };
317 
318   static AbstractCompiler* compiler(int comp_level) {
319     if (is_c2_compile(comp_level)) return _compilers[1]; // C2
320     if (is_c1_compile(comp_level)) return _compilers[0]; // C1
321     return nullptr;
322   }
323 
324   static bool initialized() { return _initialized; }
325   static bool compilation_is_complete(Method* method, int osr_bci, int comp_level, bool online_only,
326                                       CompileTask::CompileReason compile_reason);
327   static bool compilation_is_in_queue(const methodHandle& method);
328   static void print_compile_queues(outputStream* st);
329   static int queue_size(int comp_level, bool is_scc = false) {
330     CompileQueue *q = compile_queue(comp_level, is_scc);
331     return q != nullptr ? q->size() : 0;
332   }
333   static void compilation_init(JavaThread* THREAD);
334   static void init_compiler_thread_log();
335   static nmethod* compile_method(const methodHandle& method,
336                                  int osr_bci,
337                                  int comp_level,
338                                  const methodHandle& hot_method,
339                                  int hot_count,
340                                  bool requires_online_compilation,
341                                  CompileTask::CompileReason compile_reason,
342                                  TRAPS);
343   static CompileQueue* c1_compile_queue();
344   static CompileQueue* c2_compile_queue();
345 
346 private:
347   static nmethod* compile_method(const methodHandle& method,
348                                    int osr_bci,
349                                    int comp_level,
350                                    const methodHandle& hot_method,
351                                    int hot_count,
352                                    bool requires_online_compilation,
353                                    CompileTask::CompileReason compile_reason,
354                                    DirectiveSet* directive,
355                                    TRAPS);
356 
357 public:
358   // Acquire any needed locks and assign a compile id
359   static int assign_compile_id_unlocked(Thread* thread, const methodHandle& method, int osr_bci);
360 
361   static void compiler_thread_loop();
362   static int get_compilation_id() { return _compilation_id; }
363 
364   // Set _should_block.
365   // Call this from the VM, with Threads_lock held and a safepoint requested.
366   static void set_should_block();
367 
368   // Call this from the compiler at convenient points, to poll for _should_block.
369   static void maybe_block();
370 
371   enum CompilerActivity {
372     // Flags for toggling compiler activity
373     stop_compilation     = 0,
374     run_compilation      = 1,
375     shutdown_compilation = 2
376   };
377 
378   static inline jint get_compilation_activity_mode() { return _should_compile_new_jobs; }
379   static inline bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); }
380   static bool set_should_compile_new_jobs(jint new_state) {
381     // Return success if the current caller set it
382     jint old = Atomic::cmpxchg(&_should_compile_new_jobs, 1-new_state, new_state);
383     bool success = (old == (1-new_state));
384     if (success) {
385       if (new_state == run_compilation) {
386         _total_compiler_restarted_count++;
387       } else {
388         _total_compiler_stopped_count++;
389       }
390     }
391     return success;
392   }
393 
394   static void disable_compilation_forever() {
395     UseCompiler               = false;
396     AlwaysCompileLoopMethods  = false;
397     Atomic::xchg(&_should_compile_new_jobs, jint(shutdown_compilation));
398   }
399 
400   static bool is_compilation_disabled_forever() {
401     return _should_compile_new_jobs == shutdown_compilation;
402   }
403   static void handle_full_code_cache(CodeBlobType code_blob_type);
404   // Ensures that warning is only printed once.
405   static bool should_print_compiler_warning() {
406     jint old = Atomic::cmpxchg(&_print_compilation_warning, 0, 1);
407     return old == 0;
408   }
409   // Return total compilation ticks
410   static jlong total_compilation_ticks();
411 
412   // Redefine Classes support
413   static void mark_on_stack();
414 
415   // Print current compilation time stats for a given compiler
416   static void print_times(const char* name, CompilerStatistics* stats);
417 
418   // Print a detailed accounting of compilation time
419   static void print_times(bool per_compiler = true, bool aggregate = true);
420 
421   // compiler name for debugging
422   static const char* compiler_name(int comp_level);
423 
424   // Provide access to compiler thread Java objects
425   static jobject compiler1_object(int idx) {
426     assert(_compiler1_objects != nullptr, "must be initialized");
427     assert(idx < _c1_count, "oob");
428     return _compiler1_objects[idx];
429   }
430 
431   static jobject compiler2_object(int idx) {
432     assert(_compiler2_objects != nullptr, "must be initialized");
433     assert(idx < _c2_count, "oob");
434     return _compiler2_objects[idx];
435   }
436 
437   static jobject compiler3_object(int idx) {
438     assert(_compiler3_objects != nullptr, "must be initialized");
439     assert(idx < _c3_count, "oob");
440     return _compiler3_objects[idx];
441   }
442 
443   static jobject sc_object(int idx) {
444     assert(_sc_objects != nullptr, "must be initialized");
445     assert(idx < _sc_count, "oob");
446     return _sc_objects[idx];
447   }
448 
449   static AbstractCompiler* compiler1() { return _compilers[0]; }
450   static AbstractCompiler* compiler2() { return _compilers[1]; }
451   static AbstractCompiler* compiler3() { return _compilers[2]; }
452 
453   static bool can_remove(CompilerThread *ct, bool do_it);
454 
455   static CompileLog* get_log(CompilerThread* ct);
456 
457   static int get_c1_thread_count() {                return _compilers[0]->num_compiler_threads(); }
458   static int get_c2_thread_count() {                return _compilers[1]->num_compiler_threads(); }
459   static int get_total_compile_count() {            return _total_compile_count; }
460   static int get_total_bailout_count() {            return _total_bailout_count; }
461   static int get_total_invalidated_count() {        return _total_invalidated_count; }
462   static int get_total_native_compile_count() {     return _total_native_compile_count; }
463   static int get_total_osr_compile_count() {        return _total_osr_compile_count; }
464   static int get_total_standard_compile_count() {   return _total_standard_compile_count; }
465   static int get_total_compiler_stopped_count() {   return _total_compiler_stopped_count; }
466   static int get_total_compiler_restarted_count() { return _total_compiler_restarted_count; }
467   static int get_sum_osr_bytes_compiled() {         return _sum_osr_bytes_compiled; }
468   static int get_sum_standard_bytes_compiled() {    return _sum_standard_bytes_compiled; }
469   static int get_sum_nmethod_size() {               return _sum_nmethod_size;}
470   static int get_sum_nmethod_code_size() {          return _sum_nmethod_code_size; }
471   static jlong get_peak_compilation_time() {        return _peak_compilation_time; }
472   static jlong get_total_compilation_time() {       return _t_total_compilation.milliseconds(); }
473 
474   static void log_not_entrant(nmethod* nm);
475 
476   // Log that compilation profiling is skipped because metaspace is full.
477   static void log_metaspace_failure();
478 
479   static void print_statistics_on(outputStream* st);
480 
481   // CodeHeap State Analytics.
482   static void print_info(outputStream *out);
483   static void print_heapinfo(outputStream *out, const char* function, size_t granularity);
484 };
485 
486 class TrainingReplayThread : public JavaThread {
487   static void training_replay_thread_entry(JavaThread* thread, TRAPS);
488 public:
489   TrainingReplayThread() : JavaThread(&training_replay_thread_entry) { }
490 
491   bool is_hidden_from_external_view() const      { return true; }
492 };
493 
494 #endif // SHARE_COMPILER_COMPILEBROKER_HPP