< prev index next >

src/hotspot/share/compiler/compilationPolicy.hpp

Print this page
*** 26,12 ***
--- 26,136 ---
  #define SHARE_COMPILER_COMPILATIONPOLICY_HPP
  
  #include "code/nmethod.hpp"
  #include "compiler/compileBroker.hpp"
  #include "oops/methodData.hpp"
+ #include "oops/trainingData.hpp"
  #include "utilities/globalDefinitions.hpp"
  
+ namespace CompilationPolicyUtils {
+ template<int SAMPLE_COUNT = 256>
+ class WeightedMovingAverage {
+   int _current;
+   int _samples[SAMPLE_COUNT];
+   int64_t _timestamps[SAMPLE_COUNT];
+ 
+   void sample(int s, int64_t t) {
+     assert(s >= 0, "Negative sample values are not supported");
+     _samples[_current] = s;
+     _timestamps[_current] = t;
+     if (++_current >= SAMPLE_COUNT) {
+       _current = 0;
+     }
+   }
+ 
+   // Since sampling happens at irregular invervals the solution is to
+   // discount the older samples proportionally to the time between
+   // the now and the time of the sample.
+   double value(int64_t t) const {
+     double decay_speed = 1;
+     double weighted_sum = 0;
+     int count = 0;
+     for (int i = 0; i < SAMPLE_COUNT; i++) {
+       if (_samples[i] >= 0) {
+         count++;
+         double delta_t = (t - _timestamps[i]) / 1000.0; // in seconds
+         if (delta_t < 1) delta_t = 1;
+         weighted_sum += (double) _samples[i] / (delta_t * decay_speed);
+       }
+     }
+     if (count > 0) {
+       return weighted_sum / count;
+     } else {
+       return 0;
+     }
+   }
+   static int64_t time() {
+     return nanos_to_millis(os::javaTimeNanos());
+   }
+ public:
+   WeightedMovingAverage() : _current(0) {
+     for (int i = 0; i < SAMPLE_COUNT; i++) {
+       _samples[i] = -1;
+     }
+   }
+   void sample(int s) { sample(s, time()); }
+   double value() const { return value(time()); }
+ };
+ 
+ template<typename T>
+ class Queue {
+   class QueueNode : public CHeapObj<mtCompiler> {
+     T* _value;
+     QueueNode* _next;
+   public:
+     QueueNode(T* value, QueueNode* next) : _value(value), _next(next) { }
+     T* value() const { return _value; }
+     void set_next(QueueNode* next) { _next = next; }
+     QueueNode* next() const { return _next; }
+   };
+ 
+   QueueNode* _head;
+   QueueNode* _tail;
+ 
+   void push_unlocked(T* value) {
+     QueueNode* n = new QueueNode(value, nullptr);
+     if (_tail != nullptr) {
+       _tail->set_next(n);
+     }
+     _tail = n;
+     if (_head == nullptr) {
+       _head = _tail;
+     }
+   }
+   T* pop_unlocked() {
+     QueueNode* n = _head;
+     if (_head != nullptr) {
+       _head = _head->next();
+     }
+     if (_head == nullptr) {
+       _tail = _head;
+     }
+     T* value = nullptr;
+     if (n != nullptr) {
+       value = n->value();
+       delete n;
+     }
+     return value;
+   }
+ public:
+   Queue() : _head(nullptr), _tail(nullptr) { }
+   void push(T* value, Monitor* lock, TRAPS) {
+     MonitorLocker locker(THREAD, lock);
+     push_unlocked(value);
+     locker.notify_all();
+   }
+ 
+   bool is_empty_unlocked() const { return _head == nullptr; }
+ 
+   T* pop(Monitor* lock, TRAPS) {
+     MonitorLocker locker(THREAD, lock);
+     while(is_empty_unlocked() && !CompileBroker::is_compilation_disabled_forever()) {
+       locker.notify_all(); // notify that queue is empty
+       locker.wait();
+     }
+     T* value = pop_unlocked();
+     return value;
+   }
+ 
+   T* try_pop(Monitor* lock, TRAPS) {
+     MonitorLocker locker(THREAD, lock);
+     T* value = nullptr;
+     if (!is_empty_unlocked()) {
+       value = pop_unlocked();
+     }
+     return value;
+   }
+ 
+   void print_on(outputStream* st);
+ };
+ } // namespace CompilationPolicyUtils
+ 
  class CompileTask;
  class CompileQueue;
  /*
   *  The system supports 5 execution levels:
   *  * level 0 - interpreter (Profiling is tracked by a MethodData object, or MDO in short)

*** 171,13 ***
  
  class CompilationPolicy : AllStatic {
    friend class CallPredicate;
    friend class LoopPredicate;
  
!   static jlong _start_time;
!   static int _c1_count, _c2_count;
    static double _increase_threshold_at_ratio;
  
    // Set carry flags in the counters (in Method* and MDO).
    inline static void handle_counter_overflow(const methodHandle& method);
  #ifdef ASSERT
    // Verify that a level is consistent with the compilation mode
--- 295,19 ---
  
  class CompilationPolicy : AllStatic {
    friend class CallPredicate;
    friend class LoopPredicate;
  
!   typedef CompilationPolicyUtils::WeightedMovingAverage<> LoadAverage;
!   typedef CompilationPolicyUtils::Queue<InstanceKlass> TrainingReplayQueue;
+ 
+   static int64_t _start_time;
+   static int _c1_count, _c2_count, _c3_count, _sc_count;
    static double _increase_threshold_at_ratio;
+   static LoadAverage _load_average;
+   static volatile bool _recompilation_done;
+   static TrainingReplayQueue _training_replay_queue;
  
    // Set carry flags in the counters (in Method* and MDO).
    inline static void handle_counter_overflow(const methodHandle& method);
  #ifdef ASSERT
    // Verify that a level is consistent with the compilation mode

*** 185,33 ***
  #endif
    // Clamp the request level according to various constraints.
    inline static CompLevel limit_level(CompLevel level);
    // Common transition function. Given a predicate determines if a method should transition to another level.
    template<typename Predicate>
!   static CompLevel common(const methodHandle& method, CompLevel cur_level, bool disable_feedback = false);
    // Transition functions.
    // call_event determines if a method should be compiled at a different
    // level with a regular invocation entry.
!   static CompLevel call_event(const methodHandle& method, CompLevel cur_level, Thread* thread);
    // loop_event checks if a method should be OSR compiled at a different
    // level.
!   static CompLevel loop_event(const methodHandle& method, CompLevel cur_level, Thread* thread);
!   static void print_counters(const char* prefix, const Method* m);
    // Has a method been long around?
    // We don't remove old methods from the compile queue even if they have
    // very low activity (see select_task()).
    inline static bool is_old(const methodHandle& method);
    // Was a given method inactive for a given number of milliseconds.
    // If it is, we would remove it from the queue (see select_task()).
!   inline static bool is_stale(jlong t, jlong timeout, const methodHandle& method);
    // Compute the weight of the method for the compilation scheduling
    inline static double weight(Method* method);
    // Apply heuristics and return true if x should be compiled before y
    inline static bool compare_methods(Method* x, Method* y);
    // Compute event rate for a given method. The rate is the number of event (invocations + backedges)
    // per millisecond.
!   inline static void update_rate(jlong t, const methodHandle& method);
    // Compute threshold scaling coefficient
    inline static double threshold_scale(CompLevel level, int feedback_k);
    // If a method is old enough and is still in the interpreter we would want to
    // start profiling without waiting for the compiled method to arrive. This function
    // determines whether we should do that.
--- 315,50 ---
  #endif
    // Clamp the request level according to various constraints.
    inline static CompLevel limit_level(CompLevel level);
    // Common transition function. Given a predicate determines if a method should transition to another level.
    template<typename Predicate>
!   static CompLevel common(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD, bool disable_feedback = false);
+ 
+   template<typename Predicate>
+   static CompLevel transition_from_none(const methodHandle& method, CompLevel cur_level, bool delay_profiling, bool disable_feedback);
+   template<typename Predicate>
+   static CompLevel transition_from_limited_profile(const methodHandle& method, CompLevel cur_level, bool delay_profiling, bool disable_feedback);
+   template<typename Predicate>
+   static CompLevel transition_from_full_profile(const methodHandle& method, CompLevel cur_level);
+   template<typename Predicate>
+   static CompLevel standard_transition(const methodHandle& method, CompLevel cur_level, bool delayprof, bool disable_feedback);
+ 
+   static CompLevel trained_transition_from_none(const methodHandle& method, CompLevel cur_level, MethodTrainingData* mtd, JavaThread* THREAD);
+   static CompLevel trained_transition_from_limited_profile(const methodHandle& method, CompLevel cur_level, MethodTrainingData* mtd, JavaThread* THREAD);
+   static CompLevel trained_transition_from_full_profile(const methodHandle& method, CompLevel cur_level, MethodTrainingData* mtd, JavaThread* THREAD);
+   static CompLevel trained_transition(const methodHandle& method, CompLevel cur_level, MethodTrainingData* mtd, JavaThread* THREAD);
+ 
    // Transition functions.
    // call_event determines if a method should be compiled at a different
    // level with a regular invocation entry.
!   static CompLevel call_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
    // loop_event checks if a method should be OSR compiled at a different
    // level.
!   static CompLevel loop_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
!   static void print_counters(const char* prefix, Method* m);
+   static void print_training_data(const char* prefix, Method* method);
    // Has a method been long around?
    // We don't remove old methods from the compile queue even if they have
    // very low activity (see select_task()).
    inline static bool is_old(const methodHandle& method);
    // Was a given method inactive for a given number of milliseconds.
    // If it is, we would remove it from the queue (see select_task()).
!   inline static bool is_stale(int64_t t, int64_t timeout, const methodHandle& method);
    // Compute the weight of the method for the compilation scheduling
    inline static double weight(Method* method);
    // Apply heuristics and return true if x should be compiled before y
    inline static bool compare_methods(Method* x, Method* y);
+   inline static bool compare_tasks(CompileTask* x, CompileTask* y);
    // Compute event rate for a given method. The rate is the number of event (invocations + backedges)
    // per millisecond.
!   inline static void update_rate(int64_t t, const methodHandle& method);
    // Compute threshold scaling coefficient
    inline static double threshold_scale(CompLevel level, int feedback_k);
    // If a method is old enough and is still in the interpreter we would want to
    // start profiling without waiting for the compiled method to arrive. This function
    // determines whether we should do that.

*** 221,13 ***
    // Is method profiled enough?
    static bool is_method_profiled(const methodHandle& method);
  
    static void set_c1_count(int x) { _c1_count = x;    }
    static void set_c2_count(int x) { _c2_count = x;    }
  
!   enum EventType { CALL, LOOP, COMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT };
!   static void print_event(EventType type, const Method* m, const Method* im, int bci, CompLevel level);
    // Check if the method can be compiled, change level if necessary
    static void compile(const methodHandle& mh, int bci, CompLevel level, TRAPS);
    // Simple methods are as good being compiled with C1 as C2.
    // This function tells if it's such a function.
    inline static bool is_trivial(const methodHandle& method);
--- 368,15 ---
    // Is method profiled enough?
    static bool is_method_profiled(const methodHandle& method);
  
    static void set_c1_count(int x) { _c1_count = x;    }
    static void set_c2_count(int x) { _c2_count = x;    }
+   static void set_c3_count(int x) { _c3_count = x;    }
+   static void set_sc_count(int x) { _sc_count = x;    }
  
!   enum EventType { CALL, LOOP, COMPILE, FORCE_COMPILE, FORCE_RECOMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT };
!   static void print_event(EventType type, Method* m, Method* im, int bci, CompLevel level);
    // Check if the method can be compiled, change level if necessary
    static void compile(const methodHandle& mh, int bci, CompLevel level, TRAPS);
    // Simple methods are as good being compiled with C1 as C2.
    // This function tells if it's such a function.
    inline static bool is_trivial(const methodHandle& method);

*** 240,25 ***
                                        CompLevel level, nmethod* nm, TRAPS);
    static void method_back_branch_event(const methodHandle& method, const methodHandle& inlinee,
                                        int bci, CompLevel level, nmethod* nm, TRAPS);
  
    static void set_increase_threshold_at_ratio() { _increase_threshold_at_ratio = 100 / (100 - (double)IncreaseFirstTierCompileThresholdAt); }
!   static void set_start_time(jlong t) { _start_time = t;    }
!   static jlong start_time()           { return _start_time; }
  
    // m must be compiled before executing it
    static bool must_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
! public:
    static int min_invocations() { return Tier4MinInvocationThreshold; }
    static int c1_count() { return _c1_count; }
    static int c2_count() { return _c2_count; }
    static int compiler_count(CompLevel comp_level);
- 
    // If m must_be_compiled then request a compilation from the CompileBroker.
    // This supports the -Xcomp option.
    static void compile_if_required(const methodHandle& m, TRAPS);
  
    // m is allowed to be compiled
    static bool can_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
    // m is allowed to be osr compiled
    static bool can_be_osr_compiled(const methodHandle& m, int comp_level = CompLevel_any);
    static bool is_compilation_enabled();
--- 389,33 ---
                                        CompLevel level, nmethod* nm, TRAPS);
    static void method_back_branch_event(const methodHandle& method, const methodHandle& inlinee,
                                        int bci, CompLevel level, nmethod* nm, TRAPS);
  
    static void set_increase_threshold_at_ratio() { _increase_threshold_at_ratio = 100 / (100 - (double)IncreaseFirstTierCompileThresholdAt); }
!   static void set_start_time(int64_t t) { _start_time = t;    }
!   static int64_t start_time()           { return _start_time; }
  
    // m must be compiled before executing it
    static bool must_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
!   static void maybe_compile_early(const methodHandle& m, TRAPS);
+   static void maybe_compile_early_after_init(const methodHandle& m, TRAPS);
+   static void replay_training_at_init_impl(InstanceKlass* klass, TRAPS);
+  public:
    static int min_invocations() { return Tier4MinInvocationThreshold; }
    static int c1_count() { return _c1_count; }
    static int c2_count() { return _c2_count; }
+   static int c3_count() { return _c3_count; }
+   static int sc_count() { return _sc_count; }
    static int compiler_count(CompLevel comp_level);
    // If m must_be_compiled then request a compilation from the CompileBroker.
    // This supports the -Xcomp option.
    static void compile_if_required(const methodHandle& m, TRAPS);
  
+   static void replay_training_at_init(bool is_on_shutdown, TRAPS);
+   static void replay_training_at_init(InstanceKlass* klass, TRAPS);
+   static void replay_training_at_init_loop(TRAPS);
+ 
    // m is allowed to be compiled
    static bool can_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
    // m is allowed to be osr compiled
    static bool can_be_osr_compiled(const methodHandle& m, int comp_level = CompLevel_any);
    static bool is_compilation_enabled();

*** 267,19 ***
    // Return initial compile level to use with Xcomp (depends on compilation mode).
    static void reprofile(ScopeDesc* trap_scope, bool is_osr);
    static nmethod* event(const methodHandle& method, const methodHandle& inlinee,
                          int branch_bci, int bci, CompLevel comp_level, nmethod* nm, TRAPS);
    // Select task is called by CompileBroker. We should return a task or nullptr.
!   static CompileTask* select_task(CompileQueue* compile_queue);
    // Tell the runtime if we think a given method is adequately profiled.
!   static bool is_mature(Method* method);
    // Initialize: set compiler thread count
    static void initialize();
    static bool should_not_inline(ciEnv* env, ciMethod* callee);
  
    // Return desired initial compilation level for Xcomp
    static CompLevel initial_compile_level(const methodHandle& method);
    // Return highest level possible
    static CompLevel highest_compile_level();
  };
  
  #endif // SHARE_COMPILER_COMPILATIONPOLICY_HPP
--- 424,24 ---
    // Return initial compile level to use with Xcomp (depends on compilation mode).
    static void reprofile(ScopeDesc* trap_scope, bool is_osr);
    static nmethod* event(const methodHandle& method, const methodHandle& inlinee,
                          int branch_bci, int bci, CompLevel comp_level, nmethod* nm, TRAPS);
    // Select task is called by CompileBroker. We should return a task or nullptr.
!   static CompileTask* select_task(CompileQueue* compile_queue, JavaThread* THREAD);
    // Tell the runtime if we think a given method is adequately profiled.
!   static bool is_mature(MethodData* mdo);
    // Initialize: set compiler thread count
    static void initialize();
    static bool should_not_inline(ciEnv* env, ciMethod* callee);
  
    // Return desired initial compilation level for Xcomp
    static CompLevel initial_compile_level(const methodHandle& method);
    // Return highest level possible
    static CompLevel highest_compile_level();
+   static void dump();
+ 
+   static void sample_load_average();
+   static bool have_recompilation_work();
+   static bool recompilation_step(int step, TRAPS);
  };
  
  #endif // SHARE_COMPILER_COMPILATIONPOLICY_HPP
< prev index next >