1 /*
   2  * Copyright (c) 2010, 2018, 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_RUNTIME_SIMPLETHRESHOLDPOLICY_HPP
  26 #define SHARE_VM_RUNTIME_SIMPLETHRESHOLDPOLICY_HPP
  27 
  28 #include "code/nmethod.hpp"
  29 #include "oops/methodData.hpp"
  30 #include "runtime/compilationPolicy.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 #ifdef TIERED
  34 
  35 class CompileTask;
  36 class CompileQueue;
  37 
  38 class SimpleThresholdPolicy : public CompilationPolicy {
  39   int _c1_count, _c2_count;
  40 
  41   // Check if the counter is big enough and set carry (effectively infinity).
  42   inline void set_carry_if_necessary(InvocationCounter *counter);
  43   // Set carry flags in the counters (in Method* and MDO).
  44   inline void handle_counter_overflow(Method* method);
  45   // Call and loop predicates determine whether a transition to a higher compilation
  46   // level should be performed (pointers to predicate functions are passed to common_TF().
  47   // Predicates also take compiler load into account.
  48   typedef bool (SimpleThresholdPolicy::*Predicate)(int i, int b, CompLevel cur_level, Method* method);
  49   bool call_predicate(int i, int b, CompLevel cur_level, Method* method);
  50   bool loop_predicate(int i, int b, CompLevel cur_level, Method* method);
  51   // Common transition function. Given a predicate determines if a method should transition to another level.
  52   CompLevel common(Predicate p, Method* method, CompLevel cur_level);
  53   // Transition functions.
  54   // call_event determines if a method should be compiled at a different
  55   // level with a regular invocation entry.
  56   CompLevel call_event(Method* method, CompLevel cur_level, JavaThread* thread);
  57   // loop_event checks if a method should be OSR compiled at a different
  58   // level.
  59   CompLevel loop_event(Method* method, CompLevel cur_level, JavaThread* thread);
  60   void print_counters(const char* prefix, const methodHandle& mh);
  61 protected:
  62   int c1_count() const     { return _c1_count; }
  63   int c2_count() const     { return _c2_count; }
  64   void set_c1_count(int x) { _c1_count = x;    }
  65   void set_c2_count(int x) { _c2_count = x;    }
  66 
  67   enum EventType { CALL, LOOP, COMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT };
  68   void print_event(EventType type, const methodHandle& mh, const methodHandle& imh, int bci, CompLevel level);
  69   // Print policy-specific information if necessary
  70   virtual void print_specific(EventType type, const methodHandle& mh, const methodHandle& imh, int bci, CompLevel level) { }
  71   // Check if the method can be compiled, change level if necessary
  72   void compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread);
  73   // Submit a given method for compilation
  74   virtual void submit_compile(const methodHandle& mh, int bci, CompLevel level, JavaThread* thread);
  75   // Simple methods are as good being compiled with C1 as C2.
  76   // This function tells if it's such a function.
  77   inline bool is_trivial(Method* method);
  78 
  79   // Predicate helpers are used by .*_predicate() methods as well as others.
  80   // They check the given counter values, multiplied by the scale against the thresholds.
  81   template<CompLevel level> static inline bool call_predicate_helper(int i, int b, double scale, Method* method);
  82   template<CompLevel level> static inline bool loop_predicate_helper(int i, int b, double scale, Method* method);
  83 
  84   // Get a compilation level for a given method.
  85   static CompLevel comp_level(Method* method);
  86   virtual void method_invocation_event(const methodHandle& method, const methodHandle& inlinee,
  87                                        CompLevel level, CompiledMethod* nm, JavaThread* thread);
  88   virtual void method_back_branch_event(const methodHandle& method, const methodHandle& inlinee,
  89                                         int bci, CompLevel level, CompiledMethod* nm, JavaThread* thread);
  90 public:
  91   SimpleThresholdPolicy() : _c1_count(0), _c2_count(0) { }
  92   virtual int compiler_count(CompLevel comp_level) {
  93     if (is_c1_compile(comp_level)) return c1_count();
  94     if (is_c2_compile(comp_level)) return c2_count();
  95     return 0;
  96   }
  97   virtual CompLevel initial_compile_level() { return MIN2((CompLevel)TieredStopAtLevel, CompLevel_initial_compile); }
  98   virtual void do_safepoint_work() { }
  99   virtual void delay_compilation(Method* method) { }
 100   virtual void disable_compilation(Method* method) { }
 101   virtual void reprofile(ScopeDesc* trap_scope, bool is_osr);
 102   virtual nmethod* event(const methodHandle& method, const methodHandle& inlinee,
 103                          int branch_bci, int bci, CompLevel comp_level, CompiledMethod* nm, JavaThread* thread);
 104   // Select task is called by CompileBroker. We should return a task or NULL.
 105   virtual CompileTask* select_task(CompileQueue* compile_queue);
 106   // Tell the runtime if we think a given method is adequately profiled.
 107   virtual bool is_mature(Method* method);
 108   // Initialize: set compiler thread count
 109   virtual void initialize();
 110   virtual bool should_not_inline(ciEnv* env, ciMethod* callee) {
 111     return (env->comp_level() == CompLevel_limited_profile ||
 112             env->comp_level() == CompLevel_full_profile) &&
 113             callee->has_loops();
 114   }
 115 };
 116 
 117 #endif // TIERED
 118 
 119 #endif // SHARE_VM_RUNTIME_SIMPLETHRESHOLDPOLICY_HPP