< prev index next >

src/hotspot/share/compiler/compilationPolicy.hpp

Print this page

 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_COMPILATIONPOLICY_HPP
 26 #define SHARE_COMPILER_COMPILATIONPOLICY_HPP
 27 
 28 #include "code/nmethod.hpp"
 29 #include "compiler/compileBroker.hpp"
 30 #include "oops/methodData.hpp"

 31 #include "utilities/globalDefinitions.hpp"
 32 














































































































 33 class CompileTask;
 34 class CompileQueue;
 35 /*
 36  *  The system supports 5 execution levels:
 37  *  * level 0 - interpreter (Profiling is tracked by a MethodData object, or MDO in short)
 38  *  * level 1 - C1 with full optimization (no profiling)
 39  *  * level 2 - C1 with invocation and backedge counters
 40  *  * level 3 - C1 with full profiling (level 2 + All other MDO profiling information)
 41  *  * level 4 - C2 with full profile guided optimization
 42  *
 43  * The MethodData object is created by both the interpreter or either compiler to store any
 44  * profiling information collected on a method (ciMethod::ensure_method_data() for C1 and C2
 45  * and CompilationPolicy::create_mdo() for the interpreter). Both the interpreter and code
 46  * compiled by C1 at level 3 will constantly update profiling information in the MDO during
 47  * execution. The information in the MDO is then used by C1 and C2 during compilation, via
 48  * the compiler interface (ciMethodXXX).
 49  * See ciMethod.cpp and ciMethodData.cpp for information transfer from an MDO to the compilers
 50  * through the compiler interface.
 51  *
 52  * Levels 0, 2 and 3 periodically notify the runtime about the current value of the counters

156  * - TieredStopAtLevel, is used mostly for testing. It allows to bypass the policy logic and stick
157  *   to a given level. For example it's useful to set TieredStopAtLevel = 1 in order to compile everything
158  *   with pure c1.
159  *
160  * - Tier0ProfilingStartPercentage allows the interpreter to start profiling when the inequalities in the
161  *   0->3 predicate are already exceeded by the given percentage but the level 3 version of the
162  *   method is still not ready. We can even go directly from level 0 to 4 if c1 doesn't produce a compiled
163  *   version in time. This reduces the overall transition to level 4 and decreases the startup time.
164  *   Note that this behavior is also guarded by the Tier3Delay mechanism: when the c2 queue is too long
165  *   these is not reason to start profiling prematurely.
166  *
167  * - TieredRateUpdateMinTime and TieredRateUpdateMaxTime are parameters of the rate computation.
168  *   Basically, the rate is not computed more frequently than TieredRateUpdateMinTime and is considered
169  *   to be zero if no events occurred in TieredRateUpdateMaxTime.
170  */
171 
172 class CompilationPolicy : AllStatic {
173   friend class CallPredicate;
174   friend class LoopPredicate;
175 
176   static jlong _start_time;
177   static int _c1_count, _c2_count;



178   static double _increase_threshold_at_ratio;



179 
180   // Set carry flags in the counters (in Method* and MDO).
181   inline static void handle_counter_overflow(const methodHandle& method);
182   // Verify that a level is consistent with the compilation mode
183   static bool verify_level(CompLevel level);
184   // Clamp the request level according to various constraints.
185   inline static CompLevel limit_level(CompLevel level);
186   // Common transition function. Given a predicate determines if a method should transition to another level.
187   template<typename Predicate>
188   static CompLevel common(const methodHandle& method, CompLevel cur_level, bool disable_feedback = false);















189   // Transition functions.
190   // call_event determines if a method should be compiled at a different
191   // level with a regular invocation entry.
192   static CompLevel call_event(const methodHandle& method, CompLevel cur_level, Thread* thread);
193   // loop_event checks if a method should be OSR compiled at a different
194   // level.
195   static CompLevel loop_event(const methodHandle& method, CompLevel cur_level, Thread* thread);
196   static void print_counters(const char* prefix, const Method* m);

197   // Has a method been long around?
198   // We don't remove old methods from the compile queue even if they have
199   // very low activity (see select_task()).
200   inline static bool is_old(const methodHandle& method);
201   // Was a given method inactive for a given number of milliseconds.
202   // If it is, we would remove it from the queue (see select_task()).
203   inline static bool is_stale(jlong t, jlong timeout, const methodHandle& method);
204   // Compute the weight of the method for the compilation scheduling
205   inline static double weight(Method* method);
206   // Apply heuristics and return true if x should be compiled before y
207   inline static bool compare_methods(Method* x, Method* y);

208   // Compute event rate for a given method. The rate is the number of event (invocations + backedges)
209   // per millisecond.
210   inline static void update_rate(jlong t, const methodHandle& method);
211   // Compute threshold scaling coefficient
212   inline static double threshold_scale(CompLevel level, int feedback_k);
213   // If a method is old enough and is still in the interpreter we would want to
214   // start profiling without waiting for the compiled method to arrive. This function
215   // determines whether we should do that.
216   inline static bool should_create_mdo(const methodHandle& method, CompLevel cur_level);
217   // Create MDO if necessary.
218   static void create_mdo(const methodHandle& mh, JavaThread* THREAD);
219   // Is method profiled enough?
220   static bool is_method_profiled(const methodHandle& method);
221 
222   static void set_c1_count(int x) { _c1_count = x;    }
223   static void set_c2_count(int x) { _c2_count = x;    }


224 
225   enum EventType { CALL, LOOP, COMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT };
226   static void print_event(EventType type, const Method* m, const Method* im, int bci, CompLevel level);
227   // Check if the method can be compiled, change level if necessary
228   static void compile(const methodHandle& mh, int bci, CompLevel level, TRAPS);
229   // Simple methods are as good being compiled with C1 as C2.
230   // This function tells if it's such a function.
231   inline static bool is_trivial(const methodHandle& method);
232   // Force method to be compiled at CompLevel_simple?
233   inline static bool force_comp_at_level_simple(const methodHandle& method);
234 
235   // Get a compilation level for a given method.
236   static CompLevel comp_level(Method* method);
237   static void method_invocation_event(const methodHandle& method, const methodHandle& inlinee,
238                                CompLevel level, CompiledMethod* nm, TRAPS);
239   static void method_back_branch_event(const methodHandle& method, const methodHandle& inlinee,
240                                 int bci, CompLevel level, CompiledMethod* nm, TRAPS);
241 
242   static void set_increase_threshold_at_ratio() { _increase_threshold_at_ratio = 100 / (100 - (double)IncreaseFirstTierCompileThresholdAt); }
243   static void set_start_time(jlong t) { _start_time = t;    }
244   static jlong start_time()           { return _start_time; }
245 
246   // m must be compiled before executing it
247   static bool must_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
248 public:



249   static int min_invocations() { return Tier4MinInvocationThreshold; }
250   static int c1_count() { return _c1_count; }
251   static int c2_count() { return _c2_count; }


252   static int compiler_count(CompLevel comp_level);
253 
254   // If m must_be_compiled then request a compilation from the CompileBroker.
255   // This supports the -Xcomp option.
256   static void compile_if_required(const methodHandle& m, TRAPS);


257 
258   // m is allowed to be compiled
259   static bool can_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
260   // m is allowed to be osr compiled
261   static bool can_be_osr_compiled(const methodHandle& m, int comp_level = CompLevel_any);
262   static bool is_compilation_enabled();
263 
264   static CompileTask* select_task_helper(CompileQueue* compile_queue);
265   // Return initial compile level to use with Xcomp (depends on compilation mode).
266   static void reprofile(ScopeDesc* trap_scope, bool is_osr);
267   static nmethod* event(const methodHandle& method, const methodHandle& inlinee,
268                  int branch_bci, int bci, CompLevel comp_level, CompiledMethod* nm, TRAPS);
269   // Select task is called by CompileBroker. We should return a task or nullptr.
270   static CompileTask* select_task(CompileQueue* compile_queue);
271   // Tell the runtime if we think a given method is adequately profiled.
272   static bool is_mature(Method* method);
273   // Initialize: set compiler thread count
274   static void initialize();
275   static bool should_not_inline(ciEnv* env, ciMethod* callee);
276 
277   // Return desired initial compilation level for Xcomp
278   static CompLevel initial_compile_level(const methodHandle& method);
279   // Return highest level possible
280   static CompLevel highest_compile_level();





281 };
282 
283 #endif // SHARE_COMPILER_COMPILATIONPOLICY_HPP

 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_COMPILATIONPOLICY_HPP
 26 #define SHARE_COMPILER_COMPILATIONPOLICY_HPP
 27 
 28 #include "code/nmethod.hpp"
 29 #include "compiler/compileBroker.hpp"
 30 #include "oops/methodData.hpp"
 31 #include "oops/trainingData.hpp"
 32 #include "utilities/globalDefinitions.hpp"
 33 
 34 namespace CompilationPolicyUtils {
 35 template<int SAMPLE_COUNT = 256>
 36 class WeightedMovingAverage {
 37   int _current;
 38   int _samples[SAMPLE_COUNT];
 39   int64_t _timestamps[SAMPLE_COUNT];
 40 
 41   void sample(int s, int64_t t) {
 42     assert(s >= 0, "Negative sample values are not supported");
 43     _samples[_current] = s;
 44     _timestamps[_current] = t;
 45     if (++_current >= SAMPLE_COUNT) {
 46       _current = 0;
 47     }
 48   }
 49 
 50   // Since sampling happens at irregular invervals the solution is to
 51   // discount the older samples proportionally to the time between
 52   // the now and the time of the sample.
 53   double value(int64_t t) const {
 54     double decay_speed = 1;
 55     double weighted_sum = 0;
 56     int count = 0;
 57     for (int i = 0; i < SAMPLE_COUNT; i++) {
 58       if (_samples[i] >= 0) {
 59         count++;
 60         double delta_t = (t - _timestamps[i]) / 1000.0; // in seconds
 61         if (delta_t < 1) delta_t = 1;
 62         weighted_sum += (double) _samples[i] / (delta_t * decay_speed);
 63       }
 64     }
 65     if (count > 0) {
 66       return weighted_sum / count;
 67     } else {
 68       return 0;
 69     }
 70   }
 71   static int64_t time() {
 72     return nanos_to_millis(os::javaTimeNanos());
 73   }
 74 public:
 75   WeightedMovingAverage() : _current(0) {
 76     for (int i = 0; i < SAMPLE_COUNT; i++) {
 77       _samples[i] = -1;
 78     }
 79   }
 80   void sample(int s) { sample(s, time()); }
 81   double value() const { return value(time()); }
 82 };
 83 
 84 template<typename T>
 85 class Queue {
 86   class QueueNode : public CHeapObj<mtCompiler> {
 87     T* _value;
 88     QueueNode* _next;
 89   public:
 90     QueueNode(T* value, QueueNode* next) : _value(value), _next(next) { }
 91     T* value() const { return _value; }
 92     void set_next(QueueNode* next) { _next = next; }
 93     QueueNode* next() const { return _next; }
 94   };
 95 
 96   QueueNode* _head;
 97   QueueNode* _tail;
 98 
 99   bool is_empty_unlocked() const { return _head == nullptr; }
100   void push_unlocked(T* value) {
101     QueueNode* n = new QueueNode(value, nullptr);
102     if (_tail != nullptr) {
103       _tail->set_next(n);
104     }
105     _tail = n;
106     if (_head == nullptr) {
107       _head = _tail;
108     }
109   }
110   T* pop_unlocked() {
111     QueueNode* n = _head;
112     if (_head != nullptr) {
113       _head = _head->next();
114     }
115     if (_head == nullptr) {
116       _tail = _head;
117     }
118     T* value = nullptr;
119     if (n != nullptr) {
120       value = n->value();
121       delete n;
122     }
123     return value;
124   }
125 public:
126   Queue() : _head(nullptr), _tail(nullptr) { }
127   void push(T* value, Monitor* lock, TRAPS) {
128     MonitorLocker locker(THREAD, lock);
129     push_unlocked(value);
130     locker.notify_all();
131   }
132 
133   T* pop(Monitor* lock, TRAPS) {
134     MonitorLocker locker(THREAD, lock);
135     while(is_empty_unlocked() && !CompileBroker::is_compilation_disabled_forever()) {
136       locker.wait();
137     }
138     T* value = pop_unlocked();
139     return value;
140   }
141 };
142 } // namespace CompilationPolicyUtils
143 
144 class CompileTask;
145 class CompileQueue;
146 /*
147  *  The system supports 5 execution levels:
148  *  * level 0 - interpreter (Profiling is tracked by a MethodData object, or MDO in short)
149  *  * level 1 - C1 with full optimization (no profiling)
150  *  * level 2 - C1 with invocation and backedge counters
151  *  * level 3 - C1 with full profiling (level 2 + All other MDO profiling information)
152  *  * level 4 - C2 with full profile guided optimization
153  *
154  * The MethodData object is created by both the interpreter or either compiler to store any
155  * profiling information collected on a method (ciMethod::ensure_method_data() for C1 and C2
156  * and CompilationPolicy::create_mdo() for the interpreter). Both the interpreter and code
157  * compiled by C1 at level 3 will constantly update profiling information in the MDO during
158  * execution. The information in the MDO is then used by C1 and C2 during compilation, via
159  * the compiler interface (ciMethodXXX).
160  * See ciMethod.cpp and ciMethodData.cpp for information transfer from an MDO to the compilers
161  * through the compiler interface.
162  *
163  * Levels 0, 2 and 3 periodically notify the runtime about the current value of the counters

267  * - TieredStopAtLevel, is used mostly for testing. It allows to bypass the policy logic and stick
268  *   to a given level. For example it's useful to set TieredStopAtLevel = 1 in order to compile everything
269  *   with pure c1.
270  *
271  * - Tier0ProfilingStartPercentage allows the interpreter to start profiling when the inequalities in the
272  *   0->3 predicate are already exceeded by the given percentage but the level 3 version of the
273  *   method is still not ready. We can even go directly from level 0 to 4 if c1 doesn't produce a compiled
274  *   version in time. This reduces the overall transition to level 4 and decreases the startup time.
275  *   Note that this behavior is also guarded by the Tier3Delay mechanism: when the c2 queue is too long
276  *   these is not reason to start profiling prematurely.
277  *
278  * - TieredRateUpdateMinTime and TieredRateUpdateMaxTime are parameters of the rate computation.
279  *   Basically, the rate is not computed more frequently than TieredRateUpdateMinTime and is considered
280  *   to be zero if no events occurred in TieredRateUpdateMaxTime.
281  */
282 
283 class CompilationPolicy : AllStatic {
284   friend class CallPredicate;
285   friend class LoopPredicate;
286 
287   typedef CompilationPolicyUtils::WeightedMovingAverage<> LoadAverage;
288   typedef CompilationPolicyUtils::Queue<InstanceKlass> TrainingReplayQueue;
289 
290   static int64_t _start_time;
291   static int _c1_count, _c2_count, _c3_count, _sc_count;
292   static double _increase_threshold_at_ratio;
293   static LoadAverage _load_average;
294   static volatile bool _recompilation_done;
295   static TrainingReplayQueue _training_replay_queue;
296 
297   // Set carry flags in the counters (in Method* and MDO).
298   inline static void handle_counter_overflow(const methodHandle& method);
299   // Verify that a level is consistent with the compilation mode
300   static bool verify_level(CompLevel level);
301   // Clamp the request level according to various constraints.
302   inline static CompLevel limit_level(CompLevel level);
303   // Common transition function. Given a predicate determines if a method should transition to another level.
304   template<typename Predicate>
305   static CompLevel common(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD, bool disable_feedback = false);
306 
307   template<typename Predicate>
308   static CompLevel transition_from_none(const methodHandle& method, CompLevel cur_level, bool delay_profiling, bool disable_feedback);
309   template<typename Predicate>
310   static CompLevel transition_from_limited_profile(const methodHandle& method, CompLevel cur_level, bool delay_profiling, bool disable_feedback);
311   template<typename Predicate>
312   static CompLevel transition_from_full_profile(const methodHandle& method, CompLevel cur_level);
313   template<typename Predicate>
314   static CompLevel standard_transition(const methodHandle& method, CompLevel cur_level, bool delayprof, bool disable_feedback);
315 
316   static CompLevel trained_transition_from_none(const methodHandle& method, CompLevel cur_level, MethodTrainingData* mtd, JavaThread* THREAD);
317   static CompLevel trained_transition_from_limited_profile(const methodHandle& method, CompLevel cur_level, MethodTrainingData* mtd, JavaThread* THREAD);
318   static CompLevel trained_transition_from_full_profile(const methodHandle& method, CompLevel cur_level, MethodTrainingData* mtd, JavaThread* THREAD);
319   static CompLevel trained_transition(const methodHandle& method, CompLevel cur_level, MethodTrainingData* mtd, JavaThread* THREAD);
320 
321   // Transition functions.
322   // call_event determines if a method should be compiled at a different
323   // level with a regular invocation entry.
324   static CompLevel call_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
325   // loop_event checks if a method should be OSR compiled at a different
326   // level.
327   static CompLevel loop_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
328   static void print_counters(const char* prefix, Method* m);
329   static void print_training_data(const char* prefix, Method* method);
330   // Has a method been long around?
331   // We don't remove old methods from the compile queue even if they have
332   // very low activity (see select_task()).
333   inline static bool is_old(const methodHandle& method);
334   // Was a given method inactive for a given number of milliseconds.
335   // If it is, we would remove it from the queue (see select_task()).
336   inline static bool is_stale(int64_t t, int64_t timeout, const methodHandle& method);
337   // Compute the weight of the method for the compilation scheduling
338   inline static double weight(Method* method);
339   // Apply heuristics and return true if x should be compiled before y
340   inline static bool compare_methods(Method* x, Method* y);
341   inline static bool compare_tasks(CompileTask* x, CompileTask* y);
342   // Compute event rate for a given method. The rate is the number of event (invocations + backedges)
343   // per millisecond.
344   inline static void update_rate(int64_t t, const methodHandle& method);
345   // Compute threshold scaling coefficient
346   inline static double threshold_scale(CompLevel level, int feedback_k);
347   // If a method is old enough and is still in the interpreter we would want to
348   // start profiling without waiting for the compiled method to arrive. This function
349   // determines whether we should do that.
350   inline static bool should_create_mdo(const methodHandle& method, CompLevel cur_level);
351   // Create MDO if necessary.
352   static void create_mdo(const methodHandle& mh, JavaThread* THREAD);
353   // Is method profiled enough?
354   static bool is_method_profiled(const methodHandle& method);
355 
356   static void set_c1_count(int x) { _c1_count = x;    }
357   static void set_c2_count(int x) { _c2_count = x;    }
358   static void set_c3_count(int x) { _c3_count = x;    }
359   static void set_sc_count(int x) { _sc_count = x;    }
360 
361   enum EventType { CALL, LOOP, COMPILE, FORCE_COMPILE, FORCE_RECOMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT };
362   static void print_event(EventType type, Method* m, Method* im, int bci, CompLevel level);
363   // Check if the method can be compiled, change level if necessary
364   static void compile(const methodHandle& mh, int bci, CompLevel level, TRAPS);
365   // Simple methods are as good being compiled with C1 as C2.
366   // This function tells if it's such a function.
367   inline static bool is_trivial(const methodHandle& method);
368   // Force method to be compiled at CompLevel_simple?
369   inline static bool force_comp_at_level_simple(const methodHandle& method);
370 
371   // Get a compilation level for a given method.
372   static CompLevel comp_level(Method* method);
373   static void method_invocation_event(const methodHandle& method, const methodHandle& inlinee,
374                                CompLevel level, CompiledMethod* nm, TRAPS);
375   static void method_back_branch_event(const methodHandle& method, const methodHandle& inlinee,
376                                 int bci, CompLevel level, CompiledMethod* nm, TRAPS);
377 
378   static void set_increase_threshold_at_ratio() { _increase_threshold_at_ratio = 100 / (100 - (double)IncreaseFirstTierCompileThresholdAt); }
379   static void set_start_time(int64_t t) { _start_time = t;    }
380   static int64_t start_time()           { return _start_time; }
381 
382   // m must be compiled before executing it
383   static bool must_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
384   static void maybe_compile_early(const methodHandle& m, TRAPS);
385   static void maybe_compile_early_after_init(const methodHandle& m, TRAPS);
386   static void replay_training_at_init_impl(InstanceKlass* klass, TRAPS);
387  public:
388   static int min_invocations() { return Tier4MinInvocationThreshold; }
389   static int c1_count() { return _c1_count; }
390   static int c2_count() { return _c2_count; }
391   static int c3_count() { return _c3_count; }
392   static int sc_count() { return _sc_count; }
393   static int compiler_count(CompLevel comp_level);

394   // If m must_be_compiled then request a compilation from the CompileBroker.
395   // This supports the -Xcomp option.
396   static void compile_if_required(const methodHandle& m, TRAPS);
397   static void replay_training_at_init(InstanceKlass* klass, TRAPS);
398   static void replay_training_at_init_loop(TRAPS);
399 
400   // m is allowed to be compiled
401   static bool can_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
402   // m is allowed to be osr compiled
403   static bool can_be_osr_compiled(const methodHandle& m, int comp_level = CompLevel_any);
404   static bool is_compilation_enabled();
405 
406   static CompileTask* select_task_helper(CompileQueue* compile_queue);
407   // Return initial compile level to use with Xcomp (depends on compilation mode).
408   static void reprofile(ScopeDesc* trap_scope, bool is_osr);
409   static nmethod* event(const methodHandle& method, const methodHandle& inlinee,
410                  int branch_bci, int bci, CompLevel comp_level, CompiledMethod* nm, TRAPS);
411   // Select task is called by CompileBroker. We should return a task or nullptr.
412   static CompileTask* select_task(CompileQueue* compile_queue, JavaThread* THREAD);
413   // Tell the runtime if we think a given method is adequately profiled.
414   static bool is_mature(MethodData* mdo);
415   // Initialize: set compiler thread count
416   static void initialize();
417   static bool should_not_inline(ciEnv* env, ciMethod* callee);
418 
419   // Return desired initial compilation level for Xcomp
420   static CompLevel initial_compile_level(const methodHandle& method);
421   // Return highest level possible
422   static CompLevel highest_compile_level();
423   static void dump();
424 
425   static void sample_load_average();
426   static bool have_recompilation_work();
427   static bool recompilation_step(int step, TRAPS);
428 };
429 
430 #endif // SHARE_COMPILER_COMPILATIONPOLICY_HPP
< prev index next >