1 /*
   2  * Copyright (c) 1998, 2015, 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/compilerDirectives.hpp"
  31 #include "compiler/compileLog.hpp"
  32 #include "memory/allocation.inline.hpp"
  33 #include "utilities/xmlstream.hpp"
  34 
  35 // CompileTask
  36 //
  37 // An entry in the compile queue.  It represents a pending or current
  38 // compilation.
  39 
  40 class CompileTask : public CHeapObj<mtCompiler> {
  41   friend class VMStructs;
  42 
  43  private:
  44   static CompileTask* _task_free_list;
  45 #ifdef ASSERT
  46   static int          _num_allocated_tasks;
  47 #endif
  48 
  49   Monitor*     _lock;
  50   uint         _compile_id;
  51   Method*      _method;
  52   jobject      _method_holder;
  53   int          _osr_bci;
  54   bool         _is_complete;
  55   bool         _is_success;
  56   bool         _is_blocking;
  57   int          _comp_level;
  58   int          _num_inlined_bytecodes;
  59   nmethodLocker* _code_handle;  // holder of eventual result
  60   CompileTask* _next, *_prev;
  61   bool         _is_free;
  62   // Fields used for logging why the compilation was initiated:
  63   jlong        _time_queued;  // in units of os::elapsed_counter()
  64   Method*      _hot_method;   // which method actually triggered this task
  65   jobject      _hot_method_holder;
  66   int          _hot_count;    // information about its invocation counter
  67   const char*  _comment;      // more info about the task
  68   const char*  _failure_reason;
  69   CompilerDirectives* _directives;
  70 
  71  public:
  72   CompileTask() {
  73     _lock = new Monitor(Mutex::nonleaf+2, "CompileTaskLock");
  74   }
  75 
  76   void initialize(int compile_id, methodHandle method, int osr_bci, int comp_level,
  77                   methodHandle hot_method, int hot_count, const char* comment,
  78                   bool is_blocking);
  79 
  80   static CompileTask* allocate();
  81   static void         free(CompileTask* task);
  82 
  83   int          compile_id() const                { return _compile_id; }
  84   Method*      method() const                    { return _method; }
  85   Method*      hot_method() const                { return _hot_method; }
  86   int          osr_bci() const                   { return _osr_bci; }
  87   bool         is_complete() const               { return _is_complete; }
  88   bool         is_blocking() const               { return _is_blocking; }
  89   bool         is_success() const                { return _is_success; }
  90 
  91   nmethodLocker* code_handle() const             { return _code_handle; }
  92   void         set_code_handle(nmethodLocker* l) { _code_handle = l; }
  93   nmethod*     code() const;                     // _code_handle->code()
  94   void         set_code(nmethod* nm);            // _code_handle->set_code(nm)
  95 
  96   Monitor*     lock() const                      { return _lock; }
  97 
  98   void         mark_complete()                   { _is_complete = true; }
  99   void         mark_success()                    { _is_success = true; }
 100 
 101   int          comp_level()                      { return _comp_level;}
 102   void         set_comp_level(int comp_level)    { _comp_level = comp_level;}
 103 
 104   int          num_inlined_bytecodes() const     { return _num_inlined_bytecodes; }
 105   void         set_num_inlined_bytecodes(int n)  { _num_inlined_bytecodes = n; }
 106 
 107   CompileTask* next() const                      { return _next; }
 108   void         set_next(CompileTask* next)       { _next = next; }
 109   CompileTask* prev() const                      { return _prev; }
 110   void         set_prev(CompileTask* prev)       { _prev = prev; }
 111   bool         is_free() const                   { return _is_free; }
 112   void         set_is_free(bool val)             { _is_free = val; }
 113 
 114   // RedefineClasses support
 115   void         metadata_do(void f(Metadata*));
 116   void         mark_on_stack();
 117 
 118 private:
 119   static void  print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
 120                                       bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,
 121                                       const char* msg = NULL, bool short_form = false, bool cr = true);
 122 
 123 public:
 124   void         print(outputStream* st = tty, const char* msg = NULL, bool short_form = false, bool cr = true);
 125   static void  print(outputStream* st, const nmethod* nm, const char* msg = NULL, bool short_form = false, bool cr = true) {
 126     print_impl(st, nm->method(), nm->compile_id(), nm->comp_level(),
 127                            nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false,
 128                            msg, short_form, cr);
 129   }
 130 
 131   static void  print_inline_indent(int inline_level, outputStream* st = tty);
 132 
 133   void         print_tty();
 134   void         print_line_on_error(outputStream* st, char* buf, int buflen);
 135 
 136   void         log_task(xmlStream* log);
 137   void         log_task_queued();
 138   void         log_task_start(CompileLog* log);
 139   void         log_task_done(CompileLog* log);
 140 
 141   void         set_failure_reason(const char* reason) {
 142     _failure_reason = reason;
 143   }
 144 
 145   bool         check_break_at_flags();
 146 
 147   static void print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg = NULL);
 148   static void print_inlining_tty(ciMethod* method, int inline_level, int bci, const char* msg = NULL) {
 149     print_inlining_inner(tty, method, inline_level, bci, msg);
 150   }
 151 };
 152 
 153 #endif // SHARE_VM_COMPILER_COMPILETASK_HPP