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