1 /* 2 * Copyright (c) 2023, 2025, 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 "cds/archiveBuilder.hpp" 26 #include "cds/cdsAccess.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/runTimeClassInfo.hpp" 29 #include "code/SCCache.hpp" 30 #include "compiler/compiler_globals.hpp" 31 #include "compiler/compileBroker.hpp" 32 #include "compiler/precompiler.hpp" 33 #include "logging/logStream.hpp" 34 #include "memory/allocation.hpp" 35 #include "oops/trainingData.hpp" 36 #include "runtime/handles.inline.hpp" 37 38 static int precompiled_total_count = 0; 39 40 class PrecompileIterator : StackObj { 41 private: 42 CompLevel _comp_level; 43 CompLevel _search_level; 44 bool _for_preload; 45 Thread* _thread; 46 GrowableArray<Method*> _methods; 47 48 bool precompile(Method* m, TRAPS) { 49 assert(!HAS_PENDING_EXCEPTION, ""); 50 assert(m->method_holder()->is_linked(), "required"); 51 52 ++precompiled_total_count; 53 54 uint entries_cnt_before = SCCache::store_entries_cnt(); 55 56 methodHandle mh(THREAD, m); 57 CompileTask::CompileReason compile_reason = (_for_preload ? CompileTask::Reason_PrecompileForPreload 58 : CompileTask::Reason_Precompile); 59 nmethod* nm = CompileBroker::compile_method(mh, InvocationEntryBci, _comp_level, methodHandle(), 0, 60 true /*requires_online_comp*/, compile_reason, 61 THREAD); 62 63 uint entries_cnt_after = SCCache::store_entries_cnt(); 64 65 return (nm != nullptr) || (entries_cnt_after > entries_cnt_before); 66 } 67 68 bool precompile(Method* m, ArchiveBuilder* builder, TRAPS) { 69 bool status = precompile(m, THREAD); 70 71 LogStreamHandle(Info, precompile) log; 72 if (log.is_enabled()) { 73 ResourceMark rm; 74 log.print("[%4d] T%d Compiled %s [%p", 75 precompiled_total_count, _comp_level + (_for_preload ? 1 : 0), m->external_name(), m); 76 if (builder != nullptr) { 77 Method* requested_m = builder->to_requested(builder->get_buffered_addr(m)); 78 log.print(" -> %p", requested_m); 79 } 80 log.print("] [%d] (%s)", SCCache::store_entries_cnt(), (status ? "success" : "FAILED")); 81 } 82 return status; 83 } 84 85 public: 86 PrecompileIterator(CompLevel comp_level, bool for_preload, CompLevel search_level, JavaThread* thread) 87 : _comp_level(comp_level), _search_level(search_level), _for_preload(for_preload), _thread(thread) { 88 assert(TrainingData::have_data(), "sanity"); 89 } 90 91 bool include(Method* m) { 92 if (m->is_native() || m->is_abstract()) { 93 return false; 94 } 95 DirectiveSet* directives = DirectivesStack::getMatchingDirective(methodHandle(_thread, m), nullptr); 96 if (directives->DontPrecompileOption) { 97 return false; // excluded 98 } else if (directives->PrecompileRecordedOption > 0) { 99 return true; 100 } 101 int cid = compile_id(m, _search_level); 102 return (cid < INT_MAX); 103 } 104 105 void do_value(const RunTimeClassInfo* record) { 106 Array<Method*>* methods = record->klass()->methods(); 107 for (int i = 0; i < methods->length(); i++) { 108 Method* m = methods->at(i); 109 if (include(m)) { 110 _methods.push(m); 111 } 112 } 113 } 114 void operator()(TrainingData* td) { 115 if (td->is_MethodTrainingData()) { 116 MethodTrainingData* mtd = td->as_MethodTrainingData(); 117 if (mtd->has_holder() && include((Method*)mtd->holder())) { 118 _methods.push((Method*)mtd->holder()); 119 } 120 } 121 } 122 123 static int compile_id(Method* m, int level) { 124 MethodTrainingData* mtd = m->method_holder()->is_loaded() ? MethodTrainingData::find(methodHandle(Thread::current(), m)) : nullptr; 125 if (mtd != nullptr && mtd->highest_level() == level) { 126 CompileTrainingData* ctd = mtd->last_toplevel_compile(level); 127 if (ctd != nullptr) { 128 return ctd->compile_id(); 129 } 130 } 131 return INT_MAX; // treat as the last compilation 132 } 133 134 static int compare_by_compile_id(Method** m1, Method** m2, CompLevel comp_level) { 135 int id1 = compile_id(*m1, comp_level); 136 int id2 = compile_id(*m2, comp_level); 137 return (id1 - id2); 138 } 139 140 static int compare_by_compile_id_tier1(Method** m1, Method** m2) { 141 return compare_by_compile_id(m1, m2, CompLevel_simple); 142 } 143 144 static int compare_by_compile_id_tier2(Method** m1, Method** m2) { 145 return compare_by_compile_id(m1, m2, CompLevel_limited_profile); 146 } 147 148 static int compare_by_compile_id_tier3(Method** m1, Method** m2) { 149 return compare_by_compile_id(m1, m2, CompLevel_full_profile); 150 } 151 152 static int compare_by_compile_id_tier4(Method** m1, Method** m2) { 153 return compare_by_compile_id(m1, m2, CompLevel_full_optimization); 154 } 155 156 void sort_methods_by_compile_id(GrowableArray<Method*>* methods) { 157 switch(_search_level) { 158 case CompLevel_simple: methods->sort(&compare_by_compile_id_tier1); break; 159 case CompLevel_limited_profile: methods->sort(&compare_by_compile_id_tier2); break; 160 case CompLevel_full_profile: methods->sort(&compare_by_compile_id_tier3); break; 161 case CompLevel_full_optimization: methods->sort(&compare_by_compile_id_tier4); break; 162 163 default: fatal("%d", _search_level); 164 } 165 } 166 167 void precompile(ArchiveBuilder* builder, TRAPS) { 168 sort_methods_by_compile_id(&_methods); 169 170 for (int i = 0; i < _methods.length(); i++) { 171 Method* m = _methods.at(i); 172 173 assert(!HAS_PENDING_EXCEPTION, ""); 174 precompile(m, builder, THREAD); 175 if (HAS_PENDING_EXCEPTION) { 176 CLEAR_PENDING_EXCEPTION; 177 } 178 } 179 } 180 }; 181 void Precompiler::compile_cached_code(CompLevel search_level, bool for_preload, CompLevel comp_level, TRAPS) { 182 ResourceMark rm; 183 PrecompileIterator pi(comp_level, for_preload, search_level, THREAD); 184 TrainingData::iterate(pi); 185 pi.precompile((ArchiveBuilder*)nullptr, THREAD); 186 } 187 188 void Precompiler::compile_cached_code(TRAPS) { 189 log_info(precompile)("Precompilation started"); 190 if (TrainingData::have_data()) { 191 TrainingData::iterate([&](TrainingData* td) { 192 if (td->is_KlassTrainingData()) { 193 KlassTrainingData *ktd = td->as_KlassTrainingData(); 194 if (ktd->has_holder()) { 195 assert(!HAS_PENDING_EXCEPTION, ""); 196 ktd->holder()->link_class(THREAD); 197 if (HAS_PENDING_EXCEPTION) { 198 LogStreamHandle(Warning, precompile) log; 199 if (log.is_enabled()) { 200 ResourceMark rm; 201 log.print("Linkage failed for %s: ", ktd->holder()->external_name()); 202 PENDING_EXCEPTION->print_on(&log); 203 } 204 CLEAR_PENDING_EXCEPTION; 205 } 206 } 207 } 208 }); 209 210 compile_cached_code(CompLevel_full_optimization, true, CompLevel_full_optimization, CHECK); 211 212 compile_cached_code(CompLevel_full_optimization, false, CompLevel_full_optimization, CHECK); 213 compile_cached_code(CompLevel_full_profile, false, CompLevel_limited_profile, CHECK); 214 compile_cached_code(CompLevel_limited_profile, false, CompLevel_limited_profile, CHECK); 215 compile_cached_code(CompLevel_simple, false, CompLevel_simple, CHECK); 216 } 217 log_info(precompile)("Precompilation finished (total: %d)", precompiled_total_count); 218 } 219 220 // New workflow only 221 void Precompiler::compile_cached_code(ArchiveBuilder* builder, TRAPS) { 222 assert(CDSConfig::is_dumping_final_static_archive() && StoreCachedCode, "sanity"); 223 if (TrainingData::have_data()) { 224 ResourceMark rm; 225 226 { 227 PrecompileIterator pi(CompLevel_full_optimization, true /*for_preload*/, CompLevel_full_optimization, THREAD); 228 TrainingData::iterate(pi); 229 pi.precompile(builder, THREAD); 230 } 231 232 for (int level = CompLevel_simple; level <= CompLevel_full_optimization; level++) { 233 CompLevel comp_level = (CompLevel)level; 234 if (comp_level == CompLevel_full_profile) { 235 comp_level = CompLevel_limited_profile; 236 } 237 PrecompileIterator pi(comp_level, false /*for_preload*/, (CompLevel)level, THREAD); 238 TrainingData::iterate(pi); 239 pi.precompile(builder, THREAD); 240 } 241 } 242 }