aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_RUNTIME_SIMPLETHRESHOLDPOLICY_HPP aoqi@0: #define SHARE_VM_RUNTIME_SIMPLETHRESHOLDPOLICY_HPP aoqi@0: aoqi@0: #include "code/nmethod.hpp" aoqi@0: #include "oops/methodData.hpp" aoqi@0: #include "runtime/compilationPolicy.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: aoqi@0: class CompileTask; aoqi@0: class CompileQueue; aoqi@0: aoqi@0: class SimpleThresholdPolicy : public CompilationPolicy { aoqi@0: int _c1_count, _c2_count; aoqi@0: aoqi@0: // Check if the counter is big enough and set carry (effectively infinity). aoqi@0: inline void set_carry_if_necessary(InvocationCounter *counter); aoqi@0: // Set carry flags in the counters (in Method* and MDO). aoqi@0: inline void handle_counter_overflow(Method* method); aoqi@0: // Call and loop predicates determine whether a transition to a higher compilation aoqi@0: // level should be performed (pointers to predicate functions are passed to common_TF(). aoqi@0: // Predicates also take compiler load into account. aoqi@0: typedef bool (SimpleThresholdPolicy::*Predicate)(int i, int b, CompLevel cur_level); aoqi@0: bool call_predicate(int i, int b, CompLevel cur_level); aoqi@0: bool loop_predicate(int i, int b, CompLevel cur_level); aoqi@0: // Common transition function. Given a predicate determines if a method should transition to another level. aoqi@0: CompLevel common(Predicate p, Method* method, CompLevel cur_level); aoqi@0: // Transition functions. aoqi@0: // call_event determines if a method should be compiled at a different aoqi@0: // level with a regular invocation entry. aoqi@0: CompLevel call_event(Method* method, CompLevel cur_level); aoqi@0: // loop_event checks if a method should be OSR compiled at a different aoqi@0: // level. aoqi@0: CompLevel loop_event(Method* method, CompLevel cur_level); aoqi@0: void print_counters(const char* prefix, methodHandle mh); aoqi@0: protected: aoqi@0: int c1_count() const { return _c1_count; } aoqi@0: int c2_count() const { return _c2_count; } aoqi@0: void set_c1_count(int x) { _c1_count = x; } aoqi@0: void set_c2_count(int x) { _c2_count = x; } aoqi@0: aoqi@0: enum EventType { CALL, LOOP, COMPILE, REMOVE_FROM_QUEUE, UPDATE_IN_QUEUE, REPROFILE, MAKE_NOT_ENTRANT }; aoqi@0: void print_event(EventType type, methodHandle mh, methodHandle imh, int bci, CompLevel level); aoqi@0: // Print policy-specific information if necessary aoqi@0: virtual void print_specific(EventType type, methodHandle mh, methodHandle imh, int bci, CompLevel level) { } aoqi@0: // Check if the method can be compiled, change level if necessary aoqi@0: void compile(methodHandle mh, int bci, CompLevel level, JavaThread* thread); aoqi@0: // Submit a given method for compilation aoqi@0: virtual void submit_compile(methodHandle mh, int bci, CompLevel level, JavaThread* thread); aoqi@0: // Simple methods are as good being compiled with C1 as C2. aoqi@0: // This function tells if it's such a function. aoqi@0: inline bool is_trivial(Method* method); aoqi@0: aoqi@0: // Predicate helpers are used by .*_predicate() methods as well as others. aoqi@0: // They check the given counter values, multiplied by the scale against the thresholds. aoqi@0: template static inline bool call_predicate_helper(int i, int b, double scale); aoqi@0: template static inline bool loop_predicate_helper(int i, int b, double scale); aoqi@0: aoqi@0: // Get a compilation level for a given method. aoqi@0: static CompLevel comp_level(Method* method) { aoqi@0: nmethod *nm = method->code(); aoqi@0: if (nm != NULL && nm->is_in_use()) { aoqi@0: return (CompLevel)nm->comp_level(); aoqi@0: } aoqi@0: return CompLevel_none; aoqi@0: } aoqi@0: virtual void method_invocation_event(methodHandle method, methodHandle inlinee, aoqi@0: CompLevel level, nmethod* nm, JavaThread* thread); aoqi@0: virtual void method_back_branch_event(methodHandle method, methodHandle inlinee, aoqi@0: int bci, CompLevel level, nmethod* nm, JavaThread* thread); aoqi@0: public: aoqi@0: SimpleThresholdPolicy() : _c1_count(0), _c2_count(0) { } aoqi@0: virtual int compiler_count(CompLevel comp_level) { aoqi@0: if (is_c1_compile(comp_level)) return c1_count(); aoqi@0: if (is_c2_compile(comp_level)) return c2_count(); aoqi@0: return 0; aoqi@0: } aoqi@0: virtual CompLevel initial_compile_level() { return MIN2((CompLevel)TieredStopAtLevel, CompLevel_initial_compile); } aoqi@0: virtual void do_safepoint_work() { } aoqi@0: virtual void delay_compilation(Method* method) { } aoqi@0: virtual void disable_compilation(Method* method) { } aoqi@0: virtual void reprofile(ScopeDesc* trap_scope, bool is_osr); aoqi@0: virtual nmethod* event(methodHandle method, methodHandle inlinee, aoqi@0: int branch_bci, int bci, CompLevel comp_level, nmethod* nm, JavaThread* thread); aoqi@0: // Select task is called by CompileBroker. We should return a task or NULL. aoqi@0: virtual CompileTask* select_task(CompileQueue* compile_queue); aoqi@0: // Tell the runtime if we think a given method is adequately profiled. aoqi@0: virtual bool is_mature(Method* method); aoqi@0: // Initialize: set compiler thread count aoqi@0: virtual void initialize(); aoqi@0: virtual bool should_not_inline(ciEnv* env, ciMethod* callee) { aoqi@0: return (env->comp_level() == CompLevel_limited_profile || aoqi@0: env->comp_level() == CompLevel_full_profile) && aoqi@0: callee->has_loops(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_RUNTIME_SIMPLETHRESHOLDPOLICY_HPP