1 /* 2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 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 #include "precompiled.hpp" 26 #include "compiler/compilationPolicy.hpp" 27 #include "compiler/recompilationPolicy.hpp" 28 #include "oops/method.inline.hpp" 29 #include "oops/recompilationSchedule.hpp" 30 #include "runtime/handles.inline.hpp" 31 32 RecompilationPolicy::LoadAverage RecompilationPolicy::_load_average; 33 volatile bool RecompilationPolicy::_recompilation_done = false; 34 35 void RecompilationPolicy::sample_load_average() { 36 if (UseRecompilation) { 37 const int c2_queue_size = CompileBroker::queue_size(CompLevel_full_optimization); 38 _load_average.sample(c2_queue_size); 39 } 40 } 41 42 void RecompilationPolicy::print_load_average() { 43 tty->print(" load=%lf", _load_average.value()); 44 } 45 46 bool RecompilationPolicy::have_recompilation_work() { 47 if (UseRecompilation && TrainingData::have_data() && RecompilationSchedule::have_schedule() && 48 RecompilationSchedule::length() > 0 && !_recompilation_done) { 49 if (_load_average.value() <= RecompilationLoadAverageThreshold) { 50 return true; 51 } 52 } 53 return false; 54 } 55 56 bool RecompilationPolicy::recompilation_step(int step, TRAPS) { 57 if (!have_recompilation_work() || os::elapsedTime() < DelayRecompilation) { 58 return false; 59 } 60 61 const int size = RecompilationSchedule::length(); 62 int i = 0; 63 int count = 0; 64 bool repeat = false; 65 for (; i < size && count < step; i++) { 66 if (!RecompilationSchedule::status_at(i)) { 67 MethodTrainingData* mtd = RecompilationSchedule::at(i); 68 if (!mtd->has_holder()) { 69 RecompilationSchedule::set_status_at(i, true); 70 continue; 71 } 72 const Method* method = mtd->holder(); 73 InstanceKlass* klass = method->method_holder(); 74 if (klass->is_not_initialized()) { 75 repeat = true; 76 continue; 77 } 78 nmethod *nm = method->code(); 79 if (nm == nullptr) { 80 repeat = true; 81 continue; 82 } 83 84 if (!ForceRecompilation && !(nm->is_scc() && nm->comp_level() == CompLevel_full_optimization)) { 85 // If it's already online-compiled at level 4, mark it as done. 86 if (nm->comp_level() == CompLevel_full_optimization) { 87 RecompilationSchedule::set_status_at(i, true); 88 } else { 89 repeat = true; 90 } 91 continue; 92 } 93 if (RecompilationSchedule::claim_at(i)) { 94 const methodHandle m(THREAD, const_cast<Method*>(method)); 95 CompLevel next_level = CompLevel_full_optimization; 96 97 if (method->method_data() == nullptr) { 98 CompilationPolicy::create_mdo(m, THREAD); 99 } 100 101 if (PrintTieredEvents) { 102 CompilationPolicy::print_event(CompilationPolicy::FORCE_RECOMPILE, m(), m(), InvocationEntryBci, next_level); 103 } 104 CompileBroker::compile_method(m, InvocationEntryBci, CompLevel_full_optimization, methodHandle(), 0, 105 true /*requires_online_compilation*/, CompileTask::Reason_MustBeCompiled, THREAD); 106 if (HAS_PENDING_EXCEPTION) { 107 CLEAR_PENDING_EXCEPTION; 108 } 109 count++; 110 } 111 } 112 } 113 114 if (i == size && !repeat) { 115 Atomic::release_store(&_recompilation_done, true); 116 } 117 return count > 0; 118 }