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<typename T>
36 class Queue {
37 class QueueNode : public CHeapObj<mtCompiler> {
38 T* _value;
39 QueueNode* _next;
40 public:
41 QueueNode(T* value, QueueNode* next) : _value(value), _next(next) { }
42 T* value() const { return _value; }
43 void set_next(QueueNode* next) { _next = next; }
44 QueueNode* next() const { return _next; }
45 };
46
47 QueueNode* _head;
48 QueueNode* _tail;
49
50 void push_unlocked(T* value) {
51 QueueNode* n = new QueueNode(value, nullptr);
52 if (_tail != nullptr) {
53 _tail->set_next(n);
54 }
55 _tail = n;
56 if (_head == nullptr) {
57 _head = _tail;
58 }
59 }
60 T* pop_unlocked() {
61 QueueNode* n = _head;
62 if (_head != nullptr) {
63 _head = _head->next();
64 }
65 if (_head == nullptr) {
66 _tail = _head;
67 }
68 T* value = nullptr;
69 if (n != nullptr) {
70 value = n->value();
71 delete n;
72 }
73 return value;
74 }
75 public:
76 Queue() : _head(nullptr), _tail(nullptr) { }
77 void push(T* value, Monitor* lock, JavaThread* current) {
78 MonitorLocker locker(current, lock);
79 push_unlocked(value);
80 locker.notify_all();
81 }
82
83 bool is_empty_unlocked() const { return _head == nullptr; }
84
85 T* pop(Monitor* lock, JavaThread* current) {
86 MonitorLocker locker(current, lock);
87 while (is_empty_unlocked() && !CompileBroker::is_compilation_disabled_forever()) {
88 locker.wait();
89 }
90 T* value = pop_unlocked();
91 return value;
92 }
93
94 T* try_pop(Monitor* lock, JavaThread* current) {
95 MonitorLocker locker(current, lock);
96 T* value = pop_unlocked();
97 return value;
98 }
99 void print_on(outputStream* st);
100 };
101 } // namespace CompilationPolicyUtils
102
103 class CompileTask;
225 *
226 * - TieredStopAtLevel, is used mostly for testing. It allows to bypass the policy logic and stick
227 * to a given level. For example it's useful to set TieredStopAtLevel = 1 in order to compile everything
228 * with pure c1.
229 *
230 * - Tier0ProfilingStartPercentage allows the interpreter to start profiling when the inequalities in the
231 * 0->3 predicate are already exceeded by the given percentage but the level 3 version of the
232 * method is still not ready. We can even go directly from level 0 to 4 if c1 doesn't produce a compiled
233 * version in time. This reduces the overall transition to level 4 and decreases the startup time.
234 * Note that this behavior is also guarded by the Tier3Delay mechanism: when the c2 queue is too long
235 * these is not reason to start profiling prematurely.
236 *
237 * - TieredRateUpdateMinTime and TieredRateUpdateMaxTime are parameters of the rate computation.
238 * Basically, the rate is not computed more frequently than TieredRateUpdateMinTime and is considered
239 * to be zero if no events occurred in TieredRateUpdateMaxTime.
240 */
241
242 class CompilationPolicy : AllStatic {
243 friend class CallPredicate;
244 friend class LoopPredicate;
245
246 typedef CompilationPolicyUtils::Queue<InstanceKlass> TrainingReplayQueue;
247
248 static int64_t _start_time;
249 static int _c1_count, _c2_count;
250 static double _increase_threshold_at_ratio;
251 static TrainingReplayQueue _training_replay_queue;
252
253 // Set carry flags in the counters (in Method* and MDO).
254 inline static void handle_counter_overflow(const methodHandle& method);
255 #ifdef ASSERT
256 // Verify that a level is consistent with the compilation mode
257 static bool verify_level(CompLevel level);
258 #endif
259 // Clamp the request level according to various constraints.
260 inline static CompLevel limit_level(CompLevel level);
261 // Common transition function. Given a predicate determines if a method should transition to another level.
262 template<typename Predicate>
263 static CompLevel common(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD, bool disable_feedback = false);
264
265 template<typename Predicate>
266 static CompLevel transition_from_none(const methodHandle& method, CompLevel cur_level, bool disable_feedback);
267 template<typename Predicate>
268 static CompLevel transition_from_limited_profile(const methodHandle& method, CompLevel cur_level, bool disable_feedback);
269 template<typename Predicate>
280 // Transition functions.
281 // call_event determines if a method should be compiled at a different
282 // level with a regular invocation entry.
283 static CompLevel call_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
284 // loop_event checks if a method should be OSR compiled at a different
285 // level.
286 static CompLevel loop_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
287 static void print_counters_on(outputStream* st, const char* prefix, Method* m);
288 static void print_training_data_on(outputStream* st, const char* prefix, Method* method, CompLevel cur_level);
289 // Has a method been long around?
290 // We don't remove old methods from the compile queue even if they have
291 // very low activity (see select_task()).
292 inline static bool is_old(const methodHandle& method);
293 // Was a given method inactive for a given number of milliseconds.
294 // If it is, we would remove it from the queue (see select_task()).
295 inline static bool is_stale(int64_t t, int64_t timeout, const methodHandle& method);
296 // Compute the weight of the method for the compilation scheduling
297 inline static double weight(Method* method);
298 // Apply heuristics and return true if x should be compiled before y
299 inline static bool compare_methods(Method* x, Method* y);
300 // Compute event rate for a given method. The rate is the number of event (invocations + backedges)
301 // per millisecond.
302 inline static void update_rate(int64_t t, const methodHandle& method);
303 // Compute threshold scaling coefficient
304 inline static double threshold_scale(CompLevel level, int feedback_k);
305 // If a method is old enough and is still in the interpreter we would want to
306 // start profiling without waiting for the compiled method to arrive. This function
307 // determines whether we should do that.
308 inline static bool should_create_mdo(const methodHandle& method, CompLevel cur_level);
309 // Create MDO if necessary.
310 static void create_mdo(const methodHandle& mh, JavaThread* THREAD);
311 // Is method profiled enough?
312 static bool is_method_profiled(const methodHandle& method);
313
314 static void set_c1_count(int x) { _c1_count = x; }
315 static void set_c2_count(int x) { _c2_count = x; }
316
317 enum EventType { CALL, LOOP, COMPILE, FORCE_COMPILE, FORCE_RECOMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT };
318 static void print_event_on(outputStream *st, EventType type, Method* m, Method* im, int bci, CompLevel level);
319 static void print_event(EventType type, Method* m, Method* im, int bci, CompLevel level);
320 // Check if the method can be compiled, change level if necessary
321 static void compile(const methodHandle& mh, int bci, CompLevel level, TRAPS);
322 // Simple methods are as good being compiled with C1 as C2.
323 // This function tells if it's such a function.
324 inline static bool is_trivial(const methodHandle& method);
325 // Force method to be compiled at CompLevel_simple?
326 inline static bool force_comp_at_level_simple(const methodHandle& method);
327
328 // Get a compilation level for a given method.
329 static CompLevel comp_level(Method* method);
330 static void method_invocation_event(const methodHandle& method, const methodHandle& inlinee,
331 CompLevel level, nmethod* nm, TRAPS);
332 static void method_back_branch_event(const methodHandle& method, const methodHandle& inlinee,
333 int bci, CompLevel level, nmethod* nm, TRAPS);
334
335 static void set_increase_threshold_at_ratio() { _increase_threshold_at_ratio = 100 / (100 - (double)IncreaseFirstTierCompileThresholdAt); }
336 static void set_start_time(int64_t t) { _start_time = t; }
337 static int64_t start_time() { return _start_time; }
338
339 // m must be compiled before executing it
340 static bool must_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
341 static void maybe_compile_early(const methodHandle& m, TRAPS);
342 static void replay_training_at_init_impl(InstanceKlass* klass, JavaThread* current);
343 public:
344 static int min_invocations() { return Tier4MinInvocationThreshold; }
345 static int c1_count() { return _c1_count; }
346 static int c2_count() { return _c2_count; }
347 static int compiler_count(CompLevel comp_level);
348 // If m must_be_compiled then request a compilation from the CompileBroker.
349 // This supports the -Xcomp option.
350 static void compile_if_required(const methodHandle& m, TRAPS);
351
352 static void replay_training_at_init(InstanceKlass* klass, JavaThread* current);
353 static void replay_training_at_init_loop(JavaThread* current);
354
355 // m is allowed to be compiled
356 static bool can_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
357 // m is allowed to be osr compiled
358 static bool can_be_osr_compiled(const methodHandle& m, int comp_level = CompLevel_any);
359 static bool is_compilation_enabled();
360
361 static CompileTask* select_task_helper(CompileQueue* compile_queue);
362 // Return initial compile level to use with Xcomp (depends on compilation mode).
363 static void reprofile(ScopeDesc* trap_scope, bool is_osr);
364 static nmethod* event(const methodHandle& method, const methodHandle& inlinee,
365 int branch_bci, int bci, CompLevel comp_level, nmethod* nm, TRAPS);
366 // Select task is called by CompileBroker. We should return a task or nullptr.
367 static CompileTask* select_task(CompileQueue* compile_queue, JavaThread* THREAD);
368 // Tell the runtime if we think a given method is adequately profiled.
369 static bool is_mature(MethodData* mdo);
370 // Initialize: set compiler thread count
371 static void initialize();
372 static bool should_not_inline(ciEnv* env, ciMethod* callee);
373
374 // Return desired initial compilation level for Xcomp
375 static CompLevel initial_compile_level(const methodHandle& method);
376 // Return highest level possible
377 static CompLevel highest_compile_level();
378 static void dump();
379 };
380
381 #endif // SHARE_COMPILER_COMPILATIONPOLICY_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<typename T>
36 class Queue {
37 class QueueNode : public CHeapObj<mtCompiler> {
38 T* _value;
39 QueueNode* _next;
40 public:
41 QueueNode(T* value, QueueNode* next) : _value(value), _next(next) { }
42 T* value() const { return _value; }
43 void set_next(QueueNode* next) { _next = next; }
44 QueueNode* next() const { return _next; }
45 };
46
47 QueueNode* _head;
48 QueueNode* _tail;
49 int _processing;
50
51 void push_unlocked(T* value) {
52 QueueNode* n = new QueueNode(value, nullptr);
53 if (_tail != nullptr) {
54 _tail->set_next(n);
55 }
56 _tail = n;
57 if (_head == nullptr) {
58 _head = _tail;
59 }
60 }
61 T* pop_unlocked() {
62 QueueNode* n = _head;
63 if (_head != nullptr) {
64 _head = _head->next();
65 }
66 if (_head == nullptr) {
67 _tail = _head;
68 }
69 T* value = nullptr;
70 if (n != nullptr) {
71 value = n->value();
72 delete n;
73 }
74 return value;
75 }
76 public:
77 Queue() : _head(nullptr), _tail(nullptr) { }
78 void push(T* value, Monitor* lock, JavaThread* current) {
79 MonitorLocker locker(current, lock);
80 push_unlocked(value);
81 locker.notify_all();
82 }
83
84 bool is_empty_unlocked() const { return _head == nullptr; }
85 bool is_processing_unlocked() const { return _processing > 0; }
86
87 T* pop(Monitor* lock, JavaThread* current) {
88 MonitorLocker locker(current, lock);
89 while (is_empty_unlocked() && !CompileBroker::is_compilation_disabled_forever()) {
90 locker.wait();
91 }
92 T* value = pop_unlocked();
93 return value;
94 }
95
96 T* try_pop(Monitor* lock, JavaThread* current) {
97 MonitorLocker locker(current, lock);
98 T* value = pop_unlocked();
99 return value;
100 }
101 void print_on(outputStream* st);
102 };
103 } // namespace CompilationPolicyUtils
104
105 class CompileTask;
227 *
228 * - TieredStopAtLevel, is used mostly for testing. It allows to bypass the policy logic and stick
229 * to a given level. For example it's useful to set TieredStopAtLevel = 1 in order to compile everything
230 * with pure c1.
231 *
232 * - Tier0ProfilingStartPercentage allows the interpreter to start profiling when the inequalities in the
233 * 0->3 predicate are already exceeded by the given percentage but the level 3 version of the
234 * method is still not ready. We can even go directly from level 0 to 4 if c1 doesn't produce a compiled
235 * version in time. This reduces the overall transition to level 4 and decreases the startup time.
236 * Note that this behavior is also guarded by the Tier3Delay mechanism: when the c2 queue is too long
237 * these is not reason to start profiling prematurely.
238 *
239 * - TieredRateUpdateMinTime and TieredRateUpdateMaxTime are parameters of the rate computation.
240 * Basically, the rate is not computed more frequently than TieredRateUpdateMinTime and is considered
241 * to be zero if no events occurred in TieredRateUpdateMaxTime.
242 */
243
244 class CompilationPolicy : AllStatic {
245 friend class CallPredicate;
246 friend class LoopPredicate;
247 friend class RecompilationPolicy;
248
249 typedef CompilationPolicyUtils::Queue<InstanceKlass> TrainingReplayQueue;
250
251 static int64_t _start_time;
252 static int _c1_count, _c2_count, _ac_count;
253 static double _increase_threshold_at_ratio;
254 static TrainingReplayQueue _training_replay_queue;
255
256 // Set carry flags in the counters (in Method* and MDO).
257 inline static void handle_counter_overflow(const methodHandle& method);
258 #ifdef ASSERT
259 // Verify that a level is consistent with the compilation mode
260 static bool verify_level(CompLevel level);
261 #endif
262 // Clamp the request level according to various constraints.
263 inline static CompLevel limit_level(CompLevel level);
264 // Common transition function. Given a predicate determines if a method should transition to another level.
265 template<typename Predicate>
266 static CompLevel common(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD, bool disable_feedback = false);
267
268 template<typename Predicate>
269 static CompLevel transition_from_none(const methodHandle& method, CompLevel cur_level, bool disable_feedback);
270 template<typename Predicate>
271 static CompLevel transition_from_limited_profile(const methodHandle& method, CompLevel cur_level, bool disable_feedback);
272 template<typename Predicate>
283 // Transition functions.
284 // call_event determines if a method should be compiled at a different
285 // level with a regular invocation entry.
286 static CompLevel call_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
287 // loop_event checks if a method should be OSR compiled at a different
288 // level.
289 static CompLevel loop_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
290 static void print_counters_on(outputStream* st, const char* prefix, Method* m);
291 static void print_training_data_on(outputStream* st, const char* prefix, Method* method, CompLevel cur_level);
292 // Has a method been long around?
293 // We don't remove old methods from the compile queue even if they have
294 // very low activity (see select_task()).
295 inline static bool is_old(const methodHandle& method);
296 // Was a given method inactive for a given number of milliseconds.
297 // If it is, we would remove it from the queue (see select_task()).
298 inline static bool is_stale(int64_t t, int64_t timeout, const methodHandle& method);
299 // Compute the weight of the method for the compilation scheduling
300 inline static double weight(Method* method);
301 // Apply heuristics and return true if x should be compiled before y
302 inline static bool compare_methods(Method* x, Method* y);
303 inline static bool compare_tasks(CompileTask* x, CompileTask* y);
304 // Compute event rate for a given method. The rate is the number of event (invocations + backedges)
305 // per millisecond.
306 inline static void update_rate(int64_t t, const methodHandle& method);
307 // Compute threshold scaling coefficient
308 inline static double threshold_scale(CompLevel level, int feedback_k);
309 // If a method is old enough and is still in the interpreter we would want to
310 // start profiling without waiting for the compiled method to arrive. This function
311 // determines whether we should do that.
312 inline static bool should_create_mdo(const methodHandle& method, CompLevel cur_level);
313 // Create MDO if necessary.
314 static void create_mdo(const methodHandle& mh, JavaThread* THREAD);
315 // Is method profiled enough?
316 static bool is_method_profiled(const methodHandle& method);
317
318 static void set_c1_count(int x) { _c1_count = x; }
319 static void set_c2_count(int x) { _c2_count = x; }
320 static void set_ac_count(int x) { _ac_count = x; }
321
322 enum EventType { CALL, LOOP, COMPILE, FORCE_COMPILE, FORCE_RECOMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT };
323 static void print_event_on(outputStream *st, EventType type, Method* m, Method* im, int bci, CompLevel level);
324 static void print_event(EventType type, Method* m, Method* im, int bci, CompLevel level);
325 // Check if the method can be compiled, change level if necessary
326 static void compile(const methodHandle& mh, int bci, CompLevel level, TRAPS);
327 // Simple methods are as good being compiled with C1 as C2.
328 // This function tells if it's such a function.
329 inline static bool is_trivial(const methodHandle& method);
330 // Force method to be compiled at CompLevel_simple?
331 inline static bool force_comp_at_level_simple(const methodHandle& method);
332
333 // Get a compilation level for a given method.
334 static CompLevel comp_level(Method* method);
335 static void method_invocation_event(const methodHandle& method, const methodHandle& inlinee,
336 CompLevel level, nmethod* nm, TRAPS);
337 static void method_back_branch_event(const methodHandle& method, const methodHandle& inlinee,
338 int bci, CompLevel level, nmethod* nm, TRAPS);
339
340 static void set_increase_threshold_at_ratio() { _increase_threshold_at_ratio = 100 / (100 - (double)IncreaseFirstTierCompileThresholdAt); }
341 static void set_start_time(int64_t t) { _start_time = t; }
342 static int64_t start_time() { return _start_time; }
343
344 // m must be compiled before executing it
345 static bool must_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
346 static void maybe_compile_early(const methodHandle& m, TRAPS);
347 static void replay_training_at_init_impl(InstanceKlass* klass, JavaThread* current);
348 public:
349 static int min_invocations() { return Tier4MinInvocationThreshold; }
350 static int c1_count() { return _c1_count; }
351 static int c2_count() { return _c2_count; }
352 static int ac_count() { return _ac_count; }
353 static int compiler_count(CompLevel comp_level);
354 // If m must_be_compiled then request a compilation from the CompileBroker.
355 // This supports the -Xcomp option.
356 static void compile_if_required(const methodHandle& m, TRAPS);
357
358 static void replay_training_at_init(InstanceKlass* klass, JavaThread* current);
359 static void replay_training_at_init_loop(JavaThread* current);
360
361 // m is allowed to be compiled
362 static bool can_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
363 // m is allowed to be osr compiled
364 static bool can_be_osr_compiled(const methodHandle& m, int comp_level = CompLevel_any);
365 static bool is_compilation_enabled();
366
367 static CompileTask* select_task_helper(CompileQueue* compile_queue);
368 // Return initial compile level to use with Xcomp (depends on compilation mode).
369 static void reprofile(ScopeDesc* trap_scope, bool is_osr);
370 static nmethod* event(const methodHandle& method, const methodHandle& inlinee,
371 int branch_bci, int bci, CompLevel comp_level, nmethod* nm, TRAPS);
372 // Select task is called by CompileBroker. We should return a task or nullptr.
373 static CompileTask* select_task(CompileQueue* compile_queue, JavaThread* THREAD);
374 // Tell the runtime if we think a given method is adequately profiled.
375 static bool is_mature(MethodData* mdo);
376 // Initialize: set compiler thread count
377 static void initialize();
378 static bool should_not_inline(ciEnv* env, ciMethod* callee);
379
380 // Return desired initial compilation level for Xcomp
381 static CompLevel initial_compile_level(const methodHandle& method);
382 // Return highest level possible
383 static CompLevel highest_compile_level();
384 static void dump();
385
386 static void sample_load_average();
387 static bool have_recompilation_work();
388 static bool recompilation_step(int step, TRAPS);
389 };
390
391 #endif // SHARE_COMPILER_COMPILATIONPOLICY_HPP
|