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 delay_profiling, bool disable_feedback);
267 template<typename Predicate>
268 static CompLevel transition_from_limited_profile(const methodHandle& method, CompLevel cur_level, bool delay_profiling, bool disable_feedback);
269 template<typename Predicate>
279 // Transition functions.
280 // call_event determines if a method should be compiled at a different
281 // level with a regular invocation entry.
282 static CompLevel call_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
283 // loop_event checks if a method should be OSR compiled at a different
284 // level.
285 static CompLevel loop_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
286 static void print_counters_on(outputStream* st, const char* prefix, Method* m);
287 static void print_training_data_on(outputStream* st, const char* prefix, Method* method);
288 // Has a method been long around?
289 // We don't remove old methods from the compile queue even if they have
290 // very low activity (see select_task()).
291 inline static bool is_old(const methodHandle& method);
292 // Was a given method inactive for a given number of milliseconds.
293 // If it is, we would remove it from the queue (see select_task()).
294 inline static bool is_stale(int64_t t, int64_t timeout, const methodHandle& method);
295 // Compute the weight of the method for the compilation scheduling
296 inline static double weight(Method* method);
297 // Apply heuristics and return true if x should be compiled before y
298 inline static bool compare_methods(Method* x, Method* y);
299 // Compute event rate for a given method. The rate is the number of event (invocations + backedges)
300 // per millisecond.
301 inline static void update_rate(int64_t t, const methodHandle& method);
302 // Compute threshold scaling coefficient
303 inline static double threshold_scale(CompLevel level, int feedback_k);
304 // If a method is old enough and is still in the interpreter we would want to
305 // start profiling without waiting for the compiled method to arrive. This function
306 // determines whether we should do that.
307 inline static bool should_create_mdo(const methodHandle& method, CompLevel cur_level);
308 // Create MDO if necessary.
309 static void create_mdo(const methodHandle& mh, JavaThread* THREAD);
310 // Is method profiled enough?
311 static bool is_method_profiled(const methodHandle& method);
312
313 static void set_c1_count(int x) { _c1_count = x; }
314 static void set_c2_count(int x) { _c2_count = x; }
315
316 enum EventType { CALL, LOOP, COMPILE, FORCE_COMPILE, FORCE_RECOMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT };
317 static void print_event_on(outputStream *st, EventType type, Method* m, Method* im, int bci, CompLevel level);
318 static void print_event(EventType type, Method* m, Method* im, int bci, CompLevel level);
319 // Check if the method can be compiled, change level if necessary
320 static void compile(const methodHandle& mh, int bci, CompLevel level, TRAPS);
321 // Simple methods are as good being compiled with C1 as C2.
322 // This function tells if it's such a function.
323 inline static bool is_trivial(const methodHandle& method);
324 // Force method to be compiled at CompLevel_simple?
325 inline static bool force_comp_at_level_simple(const methodHandle& method);
326
327 // Get a compilation level for a given method.
328 static CompLevel comp_level(Method* method);
329 static void method_invocation_event(const methodHandle& method, const methodHandle& inlinee,
330 CompLevel level, nmethod* nm, TRAPS);
331 static void method_back_branch_event(const methodHandle& method, const methodHandle& inlinee,
332 int bci, CompLevel level, nmethod* nm, TRAPS);
333
334 static void set_increase_threshold_at_ratio() { _increase_threshold_at_ratio = 100 / (100 - (double)IncreaseFirstTierCompileThresholdAt); }
335 static void set_start_time(int64_t t) { _start_time = t; }
336 static int64_t start_time() { return _start_time; }
337
338 // m must be compiled before executing it
339 static bool must_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
340 static void maybe_compile_early(const methodHandle& m, TRAPS);
341 static void replay_training_at_init_impl(InstanceKlass* klass, JavaThread* current);
342 public:
343 static int min_invocations() { return Tier4MinInvocationThreshold; }
344 static int c1_count() { return _c1_count; }
345 static int c2_count() { return _c2_count; }
346 static int compiler_count(CompLevel comp_level);
347 // If m must_be_compiled then request a compilation from the CompileBroker.
348 // This supports the -Xcomp option.
349 static void compile_if_required(const methodHandle& m, TRAPS);
350
351 static void replay_training_at_init(InstanceKlass* klass, JavaThread* current);
352 static void replay_training_at_init_loop(JavaThread* current);
353
354 // m is allowed to be compiled
355 static bool can_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
356 // m is allowed to be osr compiled
357 static bool can_be_osr_compiled(const methodHandle& m, int comp_level = CompLevel_any);
358 static bool is_compilation_enabled();
359
360 static CompileTask* select_task_helper(CompileQueue* compile_queue);
361 // Return initial compile level to use with Xcomp (depends on compilation mode).
362 static void reprofile(ScopeDesc* trap_scope, bool is_osr);
363 static nmethod* event(const methodHandle& method, const methodHandle& inlinee,
364 int branch_bci, int bci, CompLevel comp_level, nmethod* nm, TRAPS);
365 // Select task is called by CompileBroker. We should return a task or nullptr.
366 static CompileTask* select_task(CompileQueue* compile_queue, JavaThread* THREAD);
367 // Tell the runtime if we think a given method is adequately profiled.
368 static bool is_mature(MethodData* mdo);
369 // Initialize: set compiler thread count
370 static void initialize();
371 static bool should_not_inline(ciEnv* env, ciMethod* callee);
372
373 // Return desired initial compilation level for Xcomp
374 static CompLevel initial_compile_level(const methodHandle& method);
375 // Return highest level possible
376 static CompLevel highest_compile_level();
377 static void dump();
378 };
379
380 #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 delay_profiling, bool disable_feedback);
270 template<typename Predicate>
271 static CompLevel transition_from_limited_profile(const methodHandle& method, CompLevel cur_level, bool delay_profiling, bool disable_feedback);
272 template<typename Predicate>
282 // Transition functions.
283 // call_event determines if a method should be compiled at a different
284 // level with a regular invocation entry.
285 static CompLevel call_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
286 // loop_event checks if a method should be OSR compiled at a different
287 // level.
288 static CompLevel loop_event(const methodHandle& method, CompLevel cur_level, JavaThread* THREAD);
289 static void print_counters_on(outputStream* st, const char* prefix, Method* m);
290 static void print_training_data_on(outputStream* st, const char* prefix, Method* method);
291 // Has a method been long around?
292 // We don't remove old methods from the compile queue even if they have
293 // very low activity (see select_task()).
294 inline static bool is_old(const methodHandle& method);
295 // Was a given method inactive for a given number of milliseconds.
296 // If it is, we would remove it from the queue (see select_task()).
297 inline static bool is_stale(int64_t t, int64_t timeout, const methodHandle& method);
298 // Compute the weight of the method for the compilation scheduling
299 inline static double weight(Method* method);
300 // Apply heuristics and return true if x should be compiled before y
301 inline static bool compare_methods(Method* x, Method* y);
302 inline static bool compare_tasks(CompileTask* x, CompileTask* y);
303 // Compute event rate for a given method. The rate is the number of event (invocations + backedges)
304 // per millisecond.
305 inline static void update_rate(int64_t t, const methodHandle& method);
306 // Compute threshold scaling coefficient
307 inline static double threshold_scale(CompLevel level, int feedback_k);
308 // If a method is old enough and is still in the interpreter we would want to
309 // start profiling without waiting for the compiled method to arrive. This function
310 // determines whether we should do that.
311 inline static bool should_create_mdo(const methodHandle& method, CompLevel cur_level);
312 // Create MDO if necessary.
313 static void create_mdo(const methodHandle& mh, JavaThread* THREAD);
314 // Is method profiled enough?
315 static bool is_method_profiled(const methodHandle& method);
316
317 static void set_c1_count(int x) { _c1_count = x; }
318 static void set_c2_count(int x) { _c2_count = x; }
319 static void set_ac_count(int x) { _ac_count = x; }
320
321 enum EventType { CALL, LOOP, COMPILE, FORCE_COMPILE, FORCE_RECOMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT };
322 static void print_event_on(outputStream *st, EventType type, Method* m, Method* im, int bci, CompLevel level);
323 static void print_event(EventType type, Method* m, Method* im, int bci, CompLevel level);
324 // Check if the method can be compiled, change level if necessary
325 static void compile(const methodHandle& mh, int bci, CompLevel level, TRAPS);
326 // Simple methods are as good being compiled with C1 as C2.
327 // This function tells if it's such a function.
328 inline static bool is_trivial(const methodHandle& method);
329 // Force method to be compiled at CompLevel_simple?
330 inline static bool force_comp_at_level_simple(const methodHandle& method);
331
332 // Get a compilation level for a given method.
333 static CompLevel comp_level(Method* method);
334 static void method_invocation_event(const methodHandle& method, const methodHandle& inlinee,
335 CompLevel level, nmethod* nm, TRAPS);
336 static void method_back_branch_event(const methodHandle& method, const methodHandle& inlinee,
337 int bci, CompLevel level, nmethod* nm, TRAPS);
338
339 static void set_increase_threshold_at_ratio() { _increase_threshold_at_ratio = 100 / (100 - (double)IncreaseFirstTierCompileThresholdAt); }
340 static void set_start_time(int64_t t) { _start_time = t; }
341 static int64_t start_time() { return _start_time; }
342
343 // m must be compiled before executing it
344 static bool must_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
345 static void maybe_compile_early(const methodHandle& m, TRAPS);
346 static void replay_training_at_init_impl(InstanceKlass* klass, JavaThread* current);
347 public:
348 static int min_invocations() { return Tier4MinInvocationThreshold; }
349 static int c1_count() { return _c1_count; }
350 static int c2_count() { return _c2_count; }
351 static int ac_count() { return _ac_count; }
352 static int compiler_count(CompLevel comp_level);
353 // If m must_be_compiled then request a compilation from the CompileBroker.
354 // This supports the -Xcomp option.
355 static void compile_if_required(const methodHandle& m, TRAPS);
356
357 static void replay_training_at_init(InstanceKlass* klass, JavaThread* current);
358 static void replay_training_at_init_loop(JavaThread* current);
359
360 // m is allowed to be compiled
361 static bool can_be_compiled(const methodHandle& m, int comp_level = CompLevel_any);
362 // m is allowed to be osr compiled
363 static bool can_be_osr_compiled(const methodHandle& m, int comp_level = CompLevel_any);
364 static bool is_compilation_enabled();
365
366 static CompileTask* select_task_helper(CompileQueue* compile_queue);
367 // Return initial compile level to use with Xcomp (depends on compilation mode).
368 static void reprofile(ScopeDesc* trap_scope, bool is_osr);
369 static nmethod* event(const methodHandle& method, const methodHandle& inlinee,
370 int branch_bci, int bci, CompLevel comp_level, nmethod* nm, TRAPS);
371 // Select task is called by CompileBroker. We should return a task or nullptr.
372 static CompileTask* select_task(CompileQueue* compile_queue, JavaThread* THREAD);
373 // Tell the runtime if we think a given method is adequately profiled.
374 static bool is_mature(MethodData* mdo);
375 // Initialize: set compiler thread count
376 static void initialize();
377 static bool should_not_inline(ciEnv* env, ciMethod* callee);
378
379 // Return desired initial compilation level for Xcomp
380 static CompLevel initial_compile_level(const methodHandle& method);
381 // Return highest level possible
382 static CompLevel highest_compile_level();
383 static void dump();
384
385 static void sample_load_average();
386 static bool have_recompilation_work();
387 static bool recompilation_step(int step, TRAPS);
388 };
389
390 #endif // SHARE_COMPILER_COMPILATIONPOLICY_HPP
|