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