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/aotClassInitializer.hpp"
26 #include "cds/aotClassLinker.hpp"
27 #include "cds/aotLinkedClassBulkLoader.hpp"
28 #include "cds/aotLinkedClassTable.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/heapShared.hpp"
31 #include "classfile/classLoaderData.hpp"
32 #include "classfile/systemDictionary.hpp"
33 #include "classfile/systemDictionaryShared.hpp"
34 #include "classfile/vmClasses.hpp"
35 #include "compiler/compilationPolicy.hpp"
36 #include "gc/shared/gcVMOperations.hpp"
37 #include "memory/resourceArea.hpp"
38 #include "oops/instanceKlass.hpp"
39 #include "oops/klass.inline.hpp"
40 #include "oops/trainingData.hpp"
41 #include "runtime/handles.inline.hpp"
42 #include "runtime/java.hpp"
43
44 bool AOTLinkedClassBulkLoader::_boot2_completed = false;
45 bool AOTLinkedClassBulkLoader::_platform_completed = false;
46 bool AOTLinkedClassBulkLoader::_app_completed = false;
47 bool AOTLinkedClassBulkLoader::_all_completed = false;
48
49 void AOTLinkedClassBulkLoader::serialize(SerializeClosure* soc, bool is_static_archive) {
50 AOTLinkedClassTable::get(is_static_archive)->serialize(soc);
51 }
52
53 bool AOTLinkedClassBulkLoader::class_preloading_finished() {
54 if (!CDSConfig::is_using_aot_linked_classes()) {
55 return true;
56 } else {
57 // The ConstantPools of preloaded classes have references to other preloaded classes. We don't
58 // want any Java code (including JVMCI compiler) to use these classes until all of them
59 // are loaded.
60 return Atomic::load_acquire(&_all_completed);
61 }
62 }
63
64 void AOTLinkedClassBulkLoader::load_javabase_classes(JavaThread* current) {
65 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
66 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT1, nullptr); // only java.base classes
67 }
68
69 void AOTLinkedClassBulkLoader::load_non_javabase_classes(JavaThread* current) {
70 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
71
72 // is_using_aot_linked_classes() requires is_using_full_module_graph(). As a result,
73 // the platform/system class loader should already have been initialized as part
74 // of the FMG support.
75 assert(CDSConfig::is_using_full_module_graph(), "must be");
76 assert(SystemDictionary::java_platform_loader() != nullptr, "must be");
77 assert(SystemDictionary::java_system_loader() != nullptr, "must be");
78
79 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT2, nullptr); // all boot classes outside of java.base
80 _boot2_completed = true;
81
82 load_classes_in_loader(current, AOTLinkedClassCategory::PLATFORM, SystemDictionary::java_platform_loader());
83 _platform_completed = true;
84
85 load_classes_in_loader(current, AOTLinkedClassCategory::APP, SystemDictionary::java_system_loader());
86
87 if (AOTPrintTrainingInfo) {
88 tty->print_cr("==================== archived_training_data ** after all classes preloaded ====================");
89 TrainingData::print_archived_training_data_on(tty);
90 }
91
92 _app_completed = true;
93 Atomic::release_store(&_all_completed, true);
94 }
95
96 void AOTLinkedClassBulkLoader::load_classes_in_loader(JavaThread* current, AOTLinkedClassCategory class_category, oop class_loader_oop) {
97 load_classes_in_loader_impl(class_category, class_loader_oop, current);
98 if (current->has_pending_exception()) {
99 // We cannot continue, as we might have loaded some of the aot-linked classes, which
100 // may have dangling C++ pointers to other aot-linked classes that we have failed to load.
101 exit_on_exception(current);
102 }
103 }
104
105 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) {
106 assert(current->has_pending_exception(), "precondition");
107 ResourceMark rm(current);
108 if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) {
109 log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
110 "%zuM", MaxHeapSize/M);
111 } else {
180 initiate_loading(THREAD, category_name, loader, table->platform());
181 load_classes_impl(class_category, table->app(), category_name, loader, CHECK);
182 }
183 break;
184 case AOTLinkedClassCategory::UNREGISTERED:
185 default:
186 ShouldNotReachHere(); // Currently aot-linked classes are not supported for this category.
187 break;
188 }
189 }
190
191 void AOTLinkedClassBulkLoader::load_classes_impl(AOTLinkedClassCategory class_category, Array<InstanceKlass*>* classes,
192 const char* category_name, Handle loader, TRAPS) {
193 if (classes == nullptr) {
194 return;
195 }
196
197 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());
198
199 for (int i = 0; i < classes->length(); i++) {
200 InstanceKlass* ik = classes->at(i);
201 if (log_is_enabled(Info, aot, load)) {
202 ResourceMark rm(THREAD);
203 log_info(aot, load)("%-5s %s%s%s", category_name, ik->external_name(),
204 ik->is_loaded() ? " (already loaded)" : "",
205 ik->is_hidden() ? " (hidden)" : "");
206 }
207
208 if (!ik->is_loaded()) {
209 if (ik->is_hidden()) {
210 load_hidden_class(loader_data, ik, CHECK);
211 } else {
212 InstanceKlass* actual;
213 if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
214 actual = SystemDictionary::load_instance_class(ik->name(), loader, CHECK);
215 } else {
216 actual = SystemDictionaryShared::find_or_load_shared_class(ik->name(), loader, CHECK);
217 }
218
219 if (actual != ik) {
339 // Some AOT-linked classes for <class_loader> must be initialized early. This includes
340 // - classes that were AOT-initialized by AOTClassInitializer
341 // - the classes of all objects that are reachable from the archived mirrors of
342 // the AOT-linked classes for <class_loader>.
343 void AOTLinkedClassBulkLoader::init_required_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) {
344 if (classes != nullptr) {
345 for (int i = 0; i < classes->length(); i++) {
346 InstanceKlass* ik = classes->at(i);
347 if (ik->class_loader_data() == nullptr) {
348 // This class is not yet loaded. We will initialize it in a later phase.
349 // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes
350 // but k is part of AOTLinkedClassCategory::BOOT2.
351 continue;
352 }
353 if (ik->has_aot_initialized_mirror()) {
354 ik->initialize_with_aot_initialized_mirror(CHECK);
355 } else {
356 // Some cached heap objects may hold references to methods in aot-linked
357 // classes (via MemberName). We need to make sure all classes are
358 // linked to allow such MemberNames to be invoked.
359 ik->link_class(CHECK);
360 }
361 }
362 }
363
364 HeapShared::init_classes_for_special_subgraph(class_loader, CHECK);
365 }
366
367 bool AOTLinkedClassBulkLoader::is_pending_aot_linked_class(Klass* k) {
368 if (!CDSConfig::is_using_aot_linked_classes()) {
369 return false;
370 }
371
372 if (_all_completed) { // no more pending aot-linked classes
373 return false;
374 }
375
376 if (k->is_objArray_klass()) {
377 k = ObjArrayKlass::cast(k)->bottom_klass();
378 }
379 if (!k->is_instance_klass()) {
418 if (classes != nullptr) {
419 for (int i = 0; i < classes->length(); i++) {
420 InstanceKlass* ik = classes->at(i);
421 if (ik->has_aot_initialized_mirror() && ik->is_initialized() && !ik->has_init_deps_processed()) {
422 CompilationPolicy::replay_training_at_init(ik, CHECK);
423 }
424 }
425 }
426 }
427
428 void AOTLinkedClassBulkLoader::replay_training_at_init_for_preloaded_classes(TRAPS) {
429 if (CDSConfig::is_using_aot_linked_classes() && TrainingData::have_data()) {
430 // Only static archive can have training data.
431 AOTLinkedClassTable* table = AOTLinkedClassTable::for_static_archive();
432 replay_training_at_init(table->boot(), CHECK);
433 replay_training_at_init(table->boot2(), CHECK);
434 replay_training_at_init(table->platform(), CHECK);
435 replay_training_at_init(table->app(), CHECK);
436 }
437 }
|
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/aotCacheAccess.hpp"
26 #include "cds/aotClassInitializer.hpp"
27 #include "cds/aotClassLinker.hpp"
28 #include "cds/aotLinkedClassBulkLoader.hpp"
29 #include "cds/aotLinkedClassTable.hpp"
30 #include "cds/archiveBuilder.hpp"
31 #include "cds/archiveUtils.inline.hpp"
32 #include "cds/cdsConfig.hpp"
33 #include "cds/cdsProtectionDomain.hpp"
34 #include "cds/heapShared.hpp"
35 #include "cds/lambdaFormInvokers.inline.hpp"
36 #include "classfile/classLoader.hpp"
37 #include "classfile/classLoaderData.hpp"
38 #include "classfile/classLoaderDataGraph.hpp"
39 #include "classfile/dictionary.hpp"
40 #include "classfile/javaClasses.hpp"
41 #include "classfile/systemDictionary.hpp"
42 #include "classfile/systemDictionaryShared.hpp"
43 #include "classfile/vmClasses.hpp"
44 #include "compiler/compilationPolicy.hpp"
45 #include "gc/shared/gcVMOperations.hpp"
46 #include "memory/resourceArea.hpp"
47 #include "oops/constantPool.inline.hpp"
48 #include "oops/instanceKlass.hpp"
49 #include "oops/klass.inline.hpp"
50 #include "oops/trainingData.hpp"
51 #include "runtime/handles.inline.hpp"
52 #include "runtime/java.hpp"
53 #include "runtime/javaCalls.hpp"
54 #include "runtime/perfData.inline.hpp"
55 #include "runtime/timer.hpp"
56 #include "services/management.hpp"
57
58 bool AOTLinkedClassBulkLoader::_boot2_completed = false;
59 bool AOTLinkedClassBulkLoader::_platform_completed = false;
60 bool AOTLinkedClassBulkLoader::_app_completed = false;
61 bool AOTLinkedClassBulkLoader::_all_completed = false;
62
63 static PerfCounter* _perf_classes_preloaded = nullptr;
64 static PerfTickCounters* _perf_class_preload_counters = nullptr;
65
66 void AOTLinkedClassBulkLoader::serialize(SerializeClosure* soc, bool is_static_archive) {
67 AOTLinkedClassTable::get(is_static_archive)->serialize(soc);
68
69 if (is_static_archive) {
70 if (soc->reading() && UsePerfData) {
71 JavaThread* THREAD = JavaThread::current();
72 NEWPERFEVENTCOUNTER(_perf_classes_preloaded, SUN_CLS, "preloadedClasses");
73 NEWPERFTICKCOUNTERS(_perf_class_preload_counters, SUN_CLS, "classPreload");
74 }
75 }
76 }
77
78 bool AOTLinkedClassBulkLoader::class_preloading_finished() {
79 if (!CDSConfig::is_using_aot_linked_classes()) {
80 return true;
81 } else {
82 // The ConstantPools of preloaded classes have references to other preloaded classes. We don't
83 // want any Java code (including JVMCI compiler) to use these classes until all of them
84 // are loaded.
85 return Atomic::load_acquire(&_all_completed);
86 }
87 }
88
89 void AOTLinkedClassBulkLoader::load_javabase_classes(JavaThread* current) {
90 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
91 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT1, nullptr); // only java.base classes
92 }
93
94 void AOTLinkedClassBulkLoader::load_non_javabase_classes(JavaThread* current) {
95 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
96
97 // is_using_aot_linked_classes() requires is_using_full_module_graph(). As a result,
98 // the platform/system class loader should already have been initialized as part
99 // of the FMG support.
100 if (!CDSConfig::is_dumping_final_static_archive()) {
101 assert(CDSConfig::is_using_full_module_graph(), "must be");
102 }
103 assert(SystemDictionary::java_platform_loader() != nullptr, "must be");
104 assert(SystemDictionary::java_system_loader() != nullptr, "must be");
105
106 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT2, nullptr); // all boot classes outside of java.base
107 _boot2_completed = true;
108
109 load_classes_in_loader(current, AOTLinkedClassCategory::PLATFORM, SystemDictionary::java_platform_loader());
110 _platform_completed = true;
111
112 load_classes_in_loader(current, AOTLinkedClassCategory::APP, SystemDictionary::java_system_loader());
113
114 if (AOTPrintTrainingInfo) {
115 tty->print_cr("==================== archived_training_data ** after all classes preloaded ====================");
116 TrainingData::print_archived_training_data_on(tty);
117 }
118
119 if (log_is_enabled(Info, cds, jit)) {
120 AOTCacheAccess::test_heap_access_api();
121 }
122
123 _app_completed = true;
124 Atomic::release_store(&_all_completed, true);
125 }
126
127 void AOTLinkedClassBulkLoader::load_classes_in_loader(JavaThread* current, AOTLinkedClassCategory class_category, oop class_loader_oop) {
128 load_classes_in_loader_impl(class_category, class_loader_oop, current);
129 if (current->has_pending_exception()) {
130 // We cannot continue, as we might have loaded some of the aot-linked classes, which
131 // may have dangling C++ pointers to other aot-linked classes that we have failed to load.
132 exit_on_exception(current);
133 }
134 }
135
136 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) {
137 assert(current->has_pending_exception(), "precondition");
138 ResourceMark rm(current);
139 if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) {
140 log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
141 "%zuM", MaxHeapSize/M);
142 } else {
211 initiate_loading(THREAD, category_name, loader, table->platform());
212 load_classes_impl(class_category, table->app(), category_name, loader, CHECK);
213 }
214 break;
215 case AOTLinkedClassCategory::UNREGISTERED:
216 default:
217 ShouldNotReachHere(); // Currently aot-linked classes are not supported for this category.
218 break;
219 }
220 }
221
222 void AOTLinkedClassBulkLoader::load_classes_impl(AOTLinkedClassCategory class_category, Array<InstanceKlass*>* classes,
223 const char* category_name, Handle loader, TRAPS) {
224 if (classes == nullptr) {
225 return;
226 }
227
228 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());
229
230 for (int i = 0; i < classes->length(); i++) {
231 if (UsePerfData) {
232 _perf_classes_preloaded->inc();
233 }
234 InstanceKlass* ik = classes->at(i);
235 if (log_is_enabled(Info, aot, load)) {
236 ResourceMark rm(THREAD);
237 log_info(aot, load)("%-5s %s%s%s", category_name, ik->external_name(),
238 ik->is_loaded() ? " (already loaded)" : "",
239 ik->is_hidden() ? " (hidden)" : "");
240 }
241
242 if (!ik->is_loaded()) {
243 if (ik->is_hidden()) {
244 load_hidden_class(loader_data, ik, CHECK);
245 } else {
246 InstanceKlass* actual;
247 if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
248 actual = SystemDictionary::load_instance_class(ik->name(), loader, CHECK);
249 } else {
250 actual = SystemDictionaryShared::find_or_load_shared_class(ik->name(), loader, CHECK);
251 }
252
253 if (actual != ik) {
373 // Some AOT-linked classes for <class_loader> must be initialized early. This includes
374 // - classes that were AOT-initialized by AOTClassInitializer
375 // - the classes of all objects that are reachable from the archived mirrors of
376 // the AOT-linked classes for <class_loader>.
377 void AOTLinkedClassBulkLoader::init_required_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) {
378 if (classes != nullptr) {
379 for (int i = 0; i < classes->length(); i++) {
380 InstanceKlass* ik = classes->at(i);
381 if (ik->class_loader_data() == nullptr) {
382 // This class is not yet loaded. We will initialize it in a later phase.
383 // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes
384 // but k is part of AOTLinkedClassCategory::BOOT2.
385 continue;
386 }
387 if (ik->has_aot_initialized_mirror()) {
388 ik->initialize_with_aot_initialized_mirror(CHECK);
389 } else {
390 // Some cached heap objects may hold references to methods in aot-linked
391 // classes (via MemberName). We need to make sure all classes are
392 // linked to allow such MemberNames to be invoked.
393 if (ik->is_rewritten()) {
394 // (ik->is_rewritten() == false) means the class failed verification
395 // during the assembly phase, so there's no need to link it here.
396 ik->link_class(CHECK);
397 }
398 }
399 }
400 }
401
402 HeapShared::init_classes_for_special_subgraph(class_loader, CHECK);
403 }
404
405 bool AOTLinkedClassBulkLoader::is_pending_aot_linked_class(Klass* k) {
406 if (!CDSConfig::is_using_aot_linked_classes()) {
407 return false;
408 }
409
410 if (_all_completed) { // no more pending aot-linked classes
411 return false;
412 }
413
414 if (k->is_objArray_klass()) {
415 k = ObjArrayKlass::cast(k)->bottom_klass();
416 }
417 if (!k->is_instance_klass()) {
456 if (classes != nullptr) {
457 for (int i = 0; i < classes->length(); i++) {
458 InstanceKlass* ik = classes->at(i);
459 if (ik->has_aot_initialized_mirror() && ik->is_initialized() && !ik->has_init_deps_processed()) {
460 CompilationPolicy::replay_training_at_init(ik, CHECK);
461 }
462 }
463 }
464 }
465
466 void AOTLinkedClassBulkLoader::replay_training_at_init_for_preloaded_classes(TRAPS) {
467 if (CDSConfig::is_using_aot_linked_classes() && TrainingData::have_data()) {
468 // Only static archive can have training data.
469 AOTLinkedClassTable* table = AOTLinkedClassTable::for_static_archive();
470 replay_training_at_init(table->boot(), CHECK);
471 replay_training_at_init(table->boot2(), CHECK);
472 replay_training_at_init(table->platform(), CHECK);
473 replay_training_at_init(table->app(), CHECK);
474 }
475 }
476
477 void AOTLinkedClassBulkLoader::print_counters_on(outputStream* st) {
478 if (UsePerfData && _perf_class_preload_counters != nullptr) {
479 st->print_cr("AOTLinkedClassBulkLoader:");
480 st->print_cr(" preload: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) " (thread) / " JLONG_FORMAT_W(5) " events",
481 _perf_class_preload_counters->elapsed_counter_value_us(),
482 _perf_class_preload_counters->thread_counter_value_us(),
483 _perf_classes_preloaded->get_value());
484 }
485 }
|