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/aotClassInitializer.hpp"
27 #include "cds/aotClassLinker.hpp"
28 #include "cds/aotLinkedClassBulkLoader.hpp"
29 #include "cds/aotLinkedClassTable.hpp"
30 #include "cds/cdsConfig.hpp"
31 #include "cds/heapShared.hpp"
32 #include "classfile/classLoaderData.hpp"
33 #include "classfile/systemDictionary.hpp"
34 #include "classfile/systemDictionaryShared.hpp"
35 #include "classfile/vmClasses.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 "runtime/handles.inline.hpp"
41 #include "runtime/java.hpp"
42
43 bool AOTLinkedClassBulkLoader::_boot2_completed = false;
44 bool AOTLinkedClassBulkLoader::_platform_completed = false;
45 bool AOTLinkedClassBulkLoader::_app_completed = false;
46 bool AOTLinkedClassBulkLoader::_all_completed = false;
47
48 void AOTLinkedClassBulkLoader::serialize(SerializeClosure* soc, bool is_static_archive) {
49 AOTLinkedClassTable::get(is_static_archive)->serialize(soc);
50 }
51
52 void AOTLinkedClassBulkLoader::load_javabase_classes(JavaThread* current) {
53 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
54 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT1, nullptr); // only java.base classes
55 }
56
57 void AOTLinkedClassBulkLoader::load_non_javabase_classes(JavaThread* current) {
58 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
59
60 // is_using_aot_linked_classes() requires is_using_full_module_graph(). As a result,
61 // the platform/system class loader should already have been initialized as part
62 // of the FMG support.
63 assert(CDSConfig::is_using_full_module_graph(), "must be");
64 assert(SystemDictionary::java_platform_loader() != nullptr, "must be");
65 assert(SystemDictionary::java_system_loader() != nullptr, "must be");
66
67 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT2, nullptr); // all boot classes outside of java.base
68 _boot2_completed = true;
69
70 load_classes_in_loader(current, AOTLinkedClassCategory::PLATFORM, SystemDictionary::java_platform_loader());
71 _platform_completed = true;
72
73 load_classes_in_loader(current, AOTLinkedClassCategory::APP, SystemDictionary::java_system_loader());
74 _app_completed = true;
75 _all_completed = true;
76 }
77
78 void AOTLinkedClassBulkLoader::load_classes_in_loader(JavaThread* current, AOTLinkedClassCategory class_category, oop class_loader_oop) {
79 load_classes_in_loader_impl(class_category, class_loader_oop, current);
80 if (current->has_pending_exception()) {
81 // We cannot continue, as we might have loaded some of the aot-linked classes, which
82 // may have dangling C++ pointers to other aot-linked classes that we have failed to load.
83 exit_on_exception(current);
84 }
85 }
86
87 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) {
88 assert(current->has_pending_exception(), "precondition");
89 ResourceMark rm(current);
90 if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) {
91 log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
92 SIZE_FORMAT "M", MaxHeapSize/M);
93 } else {
94 log_error(cds)("%s: %s", current->pending_exception()->klass()->external_name(),
95 java_lang_String::as_utf8_string(java_lang_Throwable::message(current->pending_exception())));
162 initiate_loading(THREAD, category_name, loader, table->platform());
163 load_classes_impl(class_category, table->app(), category_name, loader, CHECK);
164 }
165 break;
166 case AOTLinkedClassCategory::UNREGISTERED:
167 default:
168 ShouldNotReachHere(); // Currently aot-linked classes are not supported for this category.
169 break;
170 }
171 }
172
173 void AOTLinkedClassBulkLoader::load_classes_impl(AOTLinkedClassCategory class_category, Array<InstanceKlass*>* classes,
174 const char* category_name, Handle loader, TRAPS) {
175 if (classes == nullptr) {
176 return;
177 }
178
179 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());
180
181 for (int i = 0; i < classes->length(); i++) {
182 InstanceKlass* ik = classes->at(i);
183 if (log_is_enabled(Info, cds, aot, load)) {
184 ResourceMark rm(THREAD);
185 log_info(cds, aot, load)("%-5s %s%s%s", category_name, ik->external_name(),
186 ik->is_loaded() ? " (already loaded)" : "",
187 ik->is_hidden() ? " (hidden)" : "");
188 }
189
190 if (!ik->is_loaded()) {
191 if (ik->is_hidden()) {
192 load_hidden_class(loader_data, ik, CHECK);
193 } else {
194 InstanceKlass* actual;
195 if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
196 actual = SystemDictionary::load_instance_class(ik->name(), loader, CHECK);
197 } else {
198 actual = SystemDictionaryShared::find_or_load_shared_class(ik->name(), loader, CHECK);
199 }
200
201 if (actual != ik) {
321 // Some AOT-linked classes for <class_loader> must be initialized early. This includes
322 // - classes that were AOT-initialized by AOTClassInitializer
323 // - the classes of all objects that are reachable from the archived mirrors of
324 // the AOT-linked classes for <class_loader>.
325 void AOTLinkedClassBulkLoader::init_required_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) {
326 if (classes != nullptr) {
327 for (int i = 0; i < classes->length(); i++) {
328 InstanceKlass* ik = classes->at(i);
329 if (ik->class_loader_data() == nullptr) {
330 // This class is not yet loaded. We will initialize it in a later phase.
331 // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes
332 // but k is part of AOTLinkedClassCategory::BOOT2.
333 continue;
334 }
335 if (ik->has_aot_initialized_mirror()) {
336 ik->initialize_with_aot_initialized_mirror(CHECK);
337 } else {
338 // Some cached heap objects may hold references to methods in aot-linked
339 // classes (via MemberName). We need to make sure all classes are
340 // linked to allow such MemberNames to be invoked.
341 ik->link_class(CHECK);
342 }
343 }
344 }
345
346 HeapShared::init_classes_for_special_subgraph(class_loader, CHECK);
347 }
348
349 bool AOTLinkedClassBulkLoader::is_pending_aot_linked_class(Klass* k) {
350 if (!CDSConfig::is_using_aot_linked_classes()) {
351 return false;
352 }
353
354 if (_all_completed) { // no more pending aot-linked classes
355 return false;
356 }
357
358 if (k->is_objArray_klass()) {
359 k = ObjArrayKlass::cast(k)->bottom_klass();
360 }
361 if (!k->is_instance_klass()) {
378 // AOTLinkedClassCategory::BOOT1 -- all aot-linked classes in
379 // java.base must have been loaded before a GC can ever happen.
380 return false;
381 } else {
382 // AOTLinkedClassCategory::BOOT2 classes cannot be loaded until
383 // module system is ready.
384 return !_boot2_completed;
385 }
386 } else if (ik->is_shared_platform_class()) {
387 // AOTLinkedClassCategory::PLATFORM classes cannot be loaded until
388 // the platform class loader is initialized.
389 return !_platform_completed;
390 } else if (ik->is_shared_app_class()) {
391 // AOTLinkedClassCategory::APP cannot be loaded until the app class loader
392 // is initialized.
393 return !_app_completed;
394 } else {
395 return false;
396 }
397 }
|
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/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/cdsAccess.hpp"
33 #include "cds/cdsConfig.hpp"
34 #include "cds/cdsProtectionDomain.hpp"
35 #include "cds/heapShared.hpp"
36 #include "cds/lambdaFormInvokers.inline.hpp"
37 #include "classfile/classLoaderDataGraph.hpp"
38 #include "classfile/classLoaderData.hpp"
39 #include "classfile/classLoaderExt.hpp"
40 #include "classfile/classLoader.hpp"
41 #include "classfile/dictionary.hpp"
42 #include "classfile/javaClasses.hpp"
43 #include "classfile/systemDictionary.hpp"
44 #include "classfile/systemDictionaryShared.hpp"
45 #include "classfile/vmClasses.hpp"
46 #include "compiler/compilationPolicy.hpp"
47 #include "gc/shared/gcVMOperations.hpp"
48 #include "memory/resourceArea.hpp"
49 #include "oops/constantPool.inline.hpp"
50 #include "oops/instanceKlass.hpp"
51 #include "oops/klass.inline.hpp"
52 #include "oops/trainingData.hpp"
53 #include "runtime/handles.inline.hpp"
54 #include "runtime/javaCalls.hpp"
55 #include "runtime/java.hpp"
56 #include "runtime/perfData.inline.hpp"
57 #include "runtime/timer.hpp"
58 #include "services/management.hpp"
59
60 bool AOTLinkedClassBulkLoader::_boot2_completed = false;
61 bool AOTLinkedClassBulkLoader::_platform_completed = false;
62 bool AOTLinkedClassBulkLoader::_app_completed = false;
63 bool AOTLinkedClassBulkLoader::_all_completed = false;
64
65 Array<InstanceKlass*>* AOTLinkedClassBulkLoader::_unregistered_classes_from_preimage = nullptr;
66
67 static PerfCounter* _perf_classes_preloaded = nullptr;
68 static PerfTickCounters* _perf_class_preload_counters = nullptr;
69
70 void AOTLinkedClassBulkLoader::record_unregistered_classes() {
71 if (CDSConfig::is_dumping_preimage_static_archive()) {
72 GrowableArray<InstanceKlass*> unreg_classes;
73 GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses();
74 for (int i = 0; i < klasses->length(); i++) {
75 Klass* k = klasses->at(i);
76 if (k->is_instance_klass()) {
77 InstanceKlass* ik = InstanceKlass::cast(k);
78 if (ik->is_shared_unregistered_class()) {
79 unreg_classes.append((InstanceKlass*)ArchiveBuilder::get_buffered_klass(ik));
80 }
81 }
82 }
83 _unregistered_classes_from_preimage = ArchiveUtils::archive_array(&unreg_classes);
84 } else {
85 _unregistered_classes_from_preimage = nullptr;
86 }
87 }
88
89 void AOTLinkedClassBulkLoader::serialize(SerializeClosure* soc, bool is_static_archive) {
90 AOTLinkedClassTable::get(is_static_archive)->serialize(soc);
91
92 if (is_static_archive) {
93 soc->do_ptr((void**)&_unregistered_classes_from_preimage);
94
95 if (soc->reading() && UsePerfData) {
96 JavaThread* THREAD = JavaThread::current();
97 NEWPERFEVENTCOUNTER(_perf_classes_preloaded, SUN_CLS, "preloadedClasses");
98 NEWPERFTICKCOUNTERS(_perf_class_preload_counters, SUN_CLS, "classPreload");
99 }
100 }
101 }
102
103 bool AOTLinkedClassBulkLoader::class_preloading_finished() {
104 if (!CDSConfig::is_using_aot_linked_classes()) {
105 return true;
106 } else {
107 // The ConstantPools of preloaded classes have references to other preloaded classes. We don't
108 // want any Java code (including JVMCI compiler) to use these classes until all of them
109 // are loaded.
110 return Atomic::load_acquire(&_all_completed);
111 }
112 }
113
114 void AOTLinkedClassBulkLoader::load_javabase_classes(JavaThread* current) {
115 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
116 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT1, nullptr); // only java.base classes
117 }
118
119 void AOTLinkedClassBulkLoader::load_non_javabase_classes(JavaThread* current) {
120 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
121
122 // is_using_aot_linked_classes() requires is_using_full_module_graph(). As a result,
123 // the platform/system class loader should already have been initialized as part
124 // of the FMG support.
125 if (!CDSConfig::is_dumping_final_static_archive()) {
126 assert(CDSConfig::is_using_full_module_graph(), "must be");
127 }
128 assert(SystemDictionary::java_platform_loader() != nullptr, "must be");
129 assert(SystemDictionary::java_system_loader() != nullptr, "must be");
130
131 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT2, nullptr); // all boot classes outside of java.base
132 _boot2_completed = true;
133
134 load_classes_in_loader(current, AOTLinkedClassCategory::PLATFORM, SystemDictionary::java_platform_loader());
135 _platform_completed = true;
136
137 load_classes_in_loader(current, AOTLinkedClassCategory::APP, SystemDictionary::java_system_loader());
138
139 if (PrintTrainingInfo) {
140 tty->print_cr("==================== archived_training_data ** after all classes preloaded ====================");
141 TrainingData::print_archived_training_data_on(tty);
142 }
143
144 if (log_is_enabled(Info, cds, jit)) {
145 CDSAccess::test_heap_access_api();
146 }
147
148 if (CDSConfig::is_dumping_final_static_archive()) {
149 assert(_unregistered_classes_from_preimage != nullptr, "must be");
150 for (int i = 0; i < _unregistered_classes_from_preimage->length(); i++) {
151 InstanceKlass* ik = _unregistered_classes_from_preimage->at(i);
152 SystemDictionaryShared::init_dumptime_info(ik);
153 SystemDictionaryShared::add_unregistered_class(current, ik);
154 }
155 }
156
157 _app_completed = true;
158 Atomic::release_store(&_all_completed, true);
159 }
160
161 void AOTLinkedClassBulkLoader::load_classes_in_loader(JavaThread* current, AOTLinkedClassCategory class_category, oop class_loader_oop) {
162 load_classes_in_loader_impl(class_category, class_loader_oop, current);
163 if (current->has_pending_exception()) {
164 // We cannot continue, as we might have loaded some of the aot-linked classes, which
165 // may have dangling C++ pointers to other aot-linked classes that we have failed to load.
166 exit_on_exception(current);
167 }
168 }
169
170 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) {
171 assert(current->has_pending_exception(), "precondition");
172 ResourceMark rm(current);
173 if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) {
174 log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
175 SIZE_FORMAT "M", MaxHeapSize/M);
176 } else {
177 log_error(cds)("%s: %s", current->pending_exception()->klass()->external_name(),
178 java_lang_String::as_utf8_string(java_lang_Throwable::message(current->pending_exception())));
245 initiate_loading(THREAD, category_name, loader, table->platform());
246 load_classes_impl(class_category, table->app(), category_name, loader, CHECK);
247 }
248 break;
249 case AOTLinkedClassCategory::UNREGISTERED:
250 default:
251 ShouldNotReachHere(); // Currently aot-linked classes are not supported for this category.
252 break;
253 }
254 }
255
256 void AOTLinkedClassBulkLoader::load_classes_impl(AOTLinkedClassCategory class_category, Array<InstanceKlass*>* classes,
257 const char* category_name, Handle loader, TRAPS) {
258 if (classes == nullptr) {
259 return;
260 }
261
262 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());
263
264 for (int i = 0; i < classes->length(); i++) {
265 if (UsePerfData) {
266 _perf_classes_preloaded->inc();
267 }
268 InstanceKlass* ik = classes->at(i);
269 if (log_is_enabled(Info, cds, aot, load)) {
270 ResourceMark rm(THREAD);
271 log_info(cds, aot, load)("%-5s %s%s%s", category_name, ik->external_name(),
272 ik->is_loaded() ? " (already loaded)" : "",
273 ik->is_hidden() ? " (hidden)" : "");
274 }
275
276 if (!ik->is_loaded()) {
277 if (ik->is_hidden()) {
278 load_hidden_class(loader_data, ik, CHECK);
279 } else {
280 InstanceKlass* actual;
281 if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
282 actual = SystemDictionary::load_instance_class(ik->name(), loader, CHECK);
283 } else {
284 actual = SystemDictionaryShared::find_or_load_shared_class(ik->name(), loader, CHECK);
285 }
286
287 if (actual != ik) {
407 // Some AOT-linked classes for <class_loader> must be initialized early. This includes
408 // - classes that were AOT-initialized by AOTClassInitializer
409 // - the classes of all objects that are reachable from the archived mirrors of
410 // the AOT-linked classes for <class_loader>.
411 void AOTLinkedClassBulkLoader::init_required_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) {
412 if (classes != nullptr) {
413 for (int i = 0; i < classes->length(); i++) {
414 InstanceKlass* ik = classes->at(i);
415 if (ik->class_loader_data() == nullptr) {
416 // This class is not yet loaded. We will initialize it in a later phase.
417 // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes
418 // but k is part of AOTLinkedClassCategory::BOOT2.
419 continue;
420 }
421 if (ik->has_aot_initialized_mirror()) {
422 ik->initialize_with_aot_initialized_mirror(CHECK);
423 } else {
424 // Some cached heap objects may hold references to methods in aot-linked
425 // classes (via MemberName). We need to make sure all classes are
426 // linked to allow such MemberNames to be invoked.
427 if (ik->is_rewritten()) {
428 // (ik->is_rewritten() == false) means the class failed verification
429 // during the assembly phase, so there's no need to link it here.
430 ik->link_class(CHECK);
431 }
432 }
433 }
434 }
435
436 HeapShared::init_classes_for_special_subgraph(class_loader, CHECK);
437 }
438
439 bool AOTLinkedClassBulkLoader::is_pending_aot_linked_class(Klass* k) {
440 if (!CDSConfig::is_using_aot_linked_classes()) {
441 return false;
442 }
443
444 if (_all_completed) { // no more pending aot-linked classes
445 return false;
446 }
447
448 if (k->is_objArray_klass()) {
449 k = ObjArrayKlass::cast(k)->bottom_klass();
450 }
451 if (!k->is_instance_klass()) {
468 // AOTLinkedClassCategory::BOOT1 -- all aot-linked classes in
469 // java.base must have been loaded before a GC can ever happen.
470 return false;
471 } else {
472 // AOTLinkedClassCategory::BOOT2 classes cannot be loaded until
473 // module system is ready.
474 return !_boot2_completed;
475 }
476 } else if (ik->is_shared_platform_class()) {
477 // AOTLinkedClassCategory::PLATFORM classes cannot be loaded until
478 // the platform class loader is initialized.
479 return !_platform_completed;
480 } else if (ik->is_shared_app_class()) {
481 // AOTLinkedClassCategory::APP cannot be loaded until the app class loader
482 // is initialized.
483 return !_app_completed;
484 } else {
485 return false;
486 }
487 }
488
489 void AOTLinkedClassBulkLoader::replay_training_at_init(Array<InstanceKlass*>* classes, TRAPS) {
490 if (classes != nullptr) {
491 for (int i = 0; i < classes->length(); i++) {
492 InstanceKlass* ik = classes->at(i);
493 if (ik->has_aot_initialized_mirror() && ik->is_initialized() && !ik->has_init_deps_processed()) {
494 CompilationPolicy::replay_training_at_init(ik, CHECK);
495 }
496 }
497 }
498 }
499
500 void AOTLinkedClassBulkLoader::replay_training_at_init_for_preloaded_classes(TRAPS) {
501 if (CDSConfig::is_using_aot_linked_classes() && TrainingData::have_data()) {
502 AOTLinkedClassTable* table = AOTLinkedClassTable::for_static_archive(); // not applicable for dynamic archive (?? why??)
503 replay_training_at_init(table->boot(), CHECK);
504 replay_training_at_init(table->boot2(), CHECK);
505 replay_training_at_init(table->platform(), CHECK);
506 replay_training_at_init(table->app(), CHECK);
507
508 CompilationPolicy::replay_training_at_init(false, CHECK);
509 }
510 }
511
512 void AOTLinkedClassBulkLoader::print_counters_on(outputStream* st) {
513 if (UsePerfData && _perf_class_preload_counters != nullptr) {
514 st->print_cr("AOTLinkedClassBulkLoader:");
515 st->print_cr(" preload: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) " (thread) / " JLONG_FORMAT_W(5) " events",
516 _perf_class_preload_counters->elapsed_counter_value_us(),
517 _perf_class_preload_counters->thread_counter_value_us(),
518 _perf_classes_preloaded->get_value());
519 }
520 }
|