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 "gc/shared/gcVMOperations.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "oops/instanceKlass.hpp"
38 #include "oops/klass.inline.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "runtime/java.hpp"
41
42 bool AOTLinkedClassBulkLoader::_boot2_completed = false;
43 bool AOTLinkedClassBulkLoader::_platform_completed = false;
44 bool AOTLinkedClassBulkLoader::_app_completed = false;
45 bool AOTLinkedClassBulkLoader::_all_completed = false;
46
47 void AOTLinkedClassBulkLoader::serialize(SerializeClosure* soc, bool is_static_archive) {
48 AOTLinkedClassTable::get(is_static_archive)->serialize(soc);
49 }
50
51 void AOTLinkedClassBulkLoader::load_javabase_classes(JavaThread* current) {
52 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
53 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT1, nullptr); // only java.base classes
54 }
55
56 void AOTLinkedClassBulkLoader::load_non_javabase_classes(JavaThread* current) {
57 assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
58
59 // is_using_aot_linked_classes() requires is_using_full_module_graph(). As a result,
60 // the platform/system class loader should already have been initialized as part
61 // of the FMG support.
62 assert(CDSConfig::is_using_full_module_graph(), "must be");
63 assert(SystemDictionary::java_platform_loader() != nullptr, "must be");
64 assert(SystemDictionary::java_system_loader() != nullptr, "must be");
65
66 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT2, nullptr); // all boot classes outside of java.base
67 _boot2_completed = true;
68
69 load_classes_in_loader(current, AOTLinkedClassCategory::PLATFORM, SystemDictionary::java_platform_loader());
70 _platform_completed = true;
71
72 load_classes_in_loader(current, AOTLinkedClassCategory::APP, SystemDictionary::java_system_loader());
73 _app_completed = true;
74 _all_completed = true;
75 }
76
77 void AOTLinkedClassBulkLoader::load_classes_in_loader(JavaThread* current, AOTLinkedClassCategory class_category, oop class_loader_oop) {
78 load_classes_in_loader_impl(class_category, class_loader_oop, current);
79 if (current->has_pending_exception()) {
80 // We cannot continue, as we might have loaded some of the aot-linked classes, which
81 // may have dangling C++ pointers to other aot-linked classes that we have failed to load.
82 exit_on_exception(current);
83 }
84 }
85
86 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) {
87 assert(current->has_pending_exception(), "precondition");
88 ResourceMark rm(current);
89 if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) {
90 log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
91 "%zuM", MaxHeapSize/M);
92 } else {
93 log_error(cds)("%s: %s", current->pending_exception()->klass()->external_name(),
94 java_lang_String::as_utf8_string(java_lang_Throwable::message(current->pending_exception())));
161 initiate_loading(THREAD, category_name, loader, table->platform());
162 load_classes_impl(class_category, table->app(), category_name, loader, CHECK);
163 }
164 break;
165 case AOTLinkedClassCategory::UNREGISTERED:
166 default:
167 ShouldNotReachHere(); // Currently aot-linked classes are not supported for this category.
168 break;
169 }
170 }
171
172 void AOTLinkedClassBulkLoader::load_classes_impl(AOTLinkedClassCategory class_category, Array<InstanceKlass*>* classes,
173 const char* category_name, Handle loader, TRAPS) {
174 if (classes == nullptr) {
175 return;
176 }
177
178 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());
179
180 for (int i = 0; i < classes->length(); i++) {
181 InstanceKlass* ik = classes->at(i);
182 if (log_is_enabled(Info, cds, aot, load)) {
183 ResourceMark rm(THREAD);
184 log_info(cds, aot, load)("%-5s %s%s%s", category_name, ik->external_name(),
185 ik->is_loaded() ? " (already loaded)" : "",
186 ik->is_hidden() ? " (hidden)" : "");
187 }
188
189 if (!ik->is_loaded()) {
190 if (ik->is_hidden()) {
191 load_hidden_class(loader_data, ik, CHECK);
192 } else {
193 InstanceKlass* actual;
194 if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
195 actual = SystemDictionary::load_instance_class(ik->name(), loader, CHECK);
196 } else {
197 actual = SystemDictionaryShared::find_or_load_shared_class(ik->name(), loader, CHECK);
198 }
199
200 if (actual != ik) {
320 // Some AOT-linked classes for <class_loader> must be initialized early. This includes
321 // - classes that were AOT-initialized by AOTClassInitializer
322 // - the classes of all objects that are reachable from the archived mirrors of
323 // the AOT-linked classes for <class_loader>.
324 void AOTLinkedClassBulkLoader::init_required_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) {
325 if (classes != nullptr) {
326 for (int i = 0; i < classes->length(); i++) {
327 InstanceKlass* ik = classes->at(i);
328 if (ik->class_loader_data() == nullptr) {
329 // This class is not yet loaded. We will initialize it in a later phase.
330 // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes
331 // but k is part of AOTLinkedClassCategory::BOOT2.
332 continue;
333 }
334 if (ik->has_aot_initialized_mirror()) {
335 ik->initialize_with_aot_initialized_mirror(CHECK);
336 } else {
337 // Some cached heap objects may hold references to methods in aot-linked
338 // classes (via MemberName). We need to make sure all classes are
339 // linked to allow such MemberNames to be invoked.
340 ik->link_class(CHECK);
341 }
342 }
343 }
344
345 HeapShared::init_classes_for_special_subgraph(class_loader, CHECK);
346 }
347
348 bool AOTLinkedClassBulkLoader::is_pending_aot_linked_class(Klass* k) {
349 if (!CDSConfig::is_using_aot_linked_classes()) {
350 return false;
351 }
352
353 if (_all_completed) { // no more pending aot-linked classes
354 return false;
355 }
356
357 if (k->is_objArray_klass()) {
358 k = ObjArrayKlass::cast(k)->bottom_klass();
359 }
360 if (!k->is_instance_klass()) {
377 // AOTLinkedClassCategory::BOOT1 -- all aot-linked classes in
378 // java.base must have been loaded before a GC can ever happen.
379 return false;
380 } else {
381 // AOTLinkedClassCategory::BOOT2 classes cannot be loaded until
382 // module system is ready.
383 return !_boot2_completed;
384 }
385 } else if (ik->is_shared_platform_class()) {
386 // AOTLinkedClassCategory::PLATFORM classes cannot be loaded until
387 // the platform class loader is initialized.
388 return !_platform_completed;
389 } else if (ik->is_shared_app_class()) {
390 // AOTLinkedClassCategory::APP cannot be loaded until the app class loader
391 // is initialized.
392 return !_app_completed;
393 } else {
394 return false;
395 }
396 }
|
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/archiveBuilder.hpp"
30 #include "cds/archiveUtils.inline.hpp"
31 #include "cds/cdsAccess.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 (PrintTrainingInfo) {
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 CDSAccess::test_heap_access_api();
122 }
123
124
125 _app_completed = true;
126 Atomic::release_store(&_all_completed, true);
127 }
128
129 void AOTLinkedClassBulkLoader::load_classes_in_loader(JavaThread* current, AOTLinkedClassCategory class_category, oop class_loader_oop) {
130 load_classes_in_loader_impl(class_category, class_loader_oop, current);
131 if (current->has_pending_exception()) {
132 // We cannot continue, as we might have loaded some of the aot-linked classes, which
133 // may have dangling C++ pointers to other aot-linked classes that we have failed to load.
134 exit_on_exception(current);
135 }
136 }
137
138 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) {
139 assert(current->has_pending_exception(), "precondition");
140 ResourceMark rm(current);
141 if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) {
142 log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
143 "%zuM", MaxHeapSize/M);
144 } else {
145 log_error(cds)("%s: %s", current->pending_exception()->klass()->external_name(),
146 java_lang_String::as_utf8_string(java_lang_Throwable::message(current->pending_exception())));
213 initiate_loading(THREAD, category_name, loader, table->platform());
214 load_classes_impl(class_category, table->app(), category_name, loader, CHECK);
215 }
216 break;
217 case AOTLinkedClassCategory::UNREGISTERED:
218 default:
219 ShouldNotReachHere(); // Currently aot-linked classes are not supported for this category.
220 break;
221 }
222 }
223
224 void AOTLinkedClassBulkLoader::load_classes_impl(AOTLinkedClassCategory class_category, Array<InstanceKlass*>* classes,
225 const char* category_name, Handle loader, TRAPS) {
226 if (classes == nullptr) {
227 return;
228 }
229
230 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());
231
232 for (int i = 0; i < classes->length(); i++) {
233 if (UsePerfData) {
234 _perf_classes_preloaded->inc();
235 }
236 InstanceKlass* ik = classes->at(i);
237 if (log_is_enabled(Info, cds, aot, load)) {
238 ResourceMark rm(THREAD);
239 log_info(cds, aot, load)("%-5s %s%s%s", category_name, ik->external_name(),
240 ik->is_loaded() ? " (already loaded)" : "",
241 ik->is_hidden() ? " (hidden)" : "");
242 }
243
244 if (!ik->is_loaded()) {
245 if (ik->is_hidden()) {
246 load_hidden_class(loader_data, ik, CHECK);
247 } else {
248 InstanceKlass* actual;
249 if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
250 actual = SystemDictionary::load_instance_class(ik->name(), loader, CHECK);
251 } else {
252 actual = SystemDictionaryShared::find_or_load_shared_class(ik->name(), loader, CHECK);
253 }
254
255 if (actual != ik) {
375 // Some AOT-linked classes for <class_loader> must be initialized early. This includes
376 // - classes that were AOT-initialized by AOTClassInitializer
377 // - the classes of all objects that are reachable from the archived mirrors of
378 // the AOT-linked classes for <class_loader>.
379 void AOTLinkedClassBulkLoader::init_required_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) {
380 if (classes != nullptr) {
381 for (int i = 0; i < classes->length(); i++) {
382 InstanceKlass* ik = classes->at(i);
383 if (ik->class_loader_data() == nullptr) {
384 // This class is not yet loaded. We will initialize it in a later phase.
385 // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes
386 // but k is part of AOTLinkedClassCategory::BOOT2.
387 continue;
388 }
389 if (ik->has_aot_initialized_mirror()) {
390 ik->initialize_with_aot_initialized_mirror(CHECK);
391 } else {
392 // Some cached heap objects may hold references to methods in aot-linked
393 // classes (via MemberName). We need to make sure all classes are
394 // linked to allow such MemberNames to be invoked.
395 if (ik->is_rewritten()) {
396 // (ik->is_rewritten() == false) means the class failed verification
397 // during the assembly phase, so there's no need to link it here.
398 ik->link_class(CHECK);
399 }
400 }
401 }
402 }
403
404 HeapShared::init_classes_for_special_subgraph(class_loader, CHECK);
405 }
406
407 bool AOTLinkedClassBulkLoader::is_pending_aot_linked_class(Klass* k) {
408 if (!CDSConfig::is_using_aot_linked_classes()) {
409 return false;
410 }
411
412 if (_all_completed) { // no more pending aot-linked classes
413 return false;
414 }
415
416 if (k->is_objArray_klass()) {
417 k = ObjArrayKlass::cast(k)->bottom_klass();
418 }
419 if (!k->is_instance_klass()) {
436 // AOTLinkedClassCategory::BOOT1 -- all aot-linked classes in
437 // java.base must have been loaded before a GC can ever happen.
438 return false;
439 } else {
440 // AOTLinkedClassCategory::BOOT2 classes cannot be loaded until
441 // module system is ready.
442 return !_boot2_completed;
443 }
444 } else if (ik->is_shared_platform_class()) {
445 // AOTLinkedClassCategory::PLATFORM classes cannot be loaded until
446 // the platform class loader is initialized.
447 return !_platform_completed;
448 } else if (ik->is_shared_app_class()) {
449 // AOTLinkedClassCategory::APP cannot be loaded until the app class loader
450 // is initialized.
451 return !_app_completed;
452 } else {
453 return false;
454 }
455 }
456
457 void AOTLinkedClassBulkLoader::replay_training_at_init(Array<InstanceKlass*>* classes, TRAPS) {
458 if (classes != nullptr) {
459 for (int i = 0; i < classes->length(); i++) {
460 InstanceKlass* ik = classes->at(i);
461 if (ik->has_aot_initialized_mirror() && ik->is_initialized() && !ik->has_init_deps_processed()) {
462 CompilationPolicy::replay_training_at_init(ik, CHECK);
463 }
464 }
465 }
466 }
467
468 void AOTLinkedClassBulkLoader::replay_training_at_init_for_preloaded_classes(TRAPS) {
469 if (CDSConfig::is_using_aot_linked_classes() && TrainingData::have_data()) {
470 // Only static archive can have training data.
471 AOTLinkedClassTable* table = AOTLinkedClassTable::for_static_archive();
472 replay_training_at_init(table->boot(), CHECK);
473 replay_training_at_init(table->boot2(), CHECK);
474 replay_training_at_init(table->platform(), CHECK);
475 replay_training_at_init(table->app(), CHECK);
476 }
477 }
478
479 void AOTLinkedClassBulkLoader::print_counters_on(outputStream* st) {
480 if (UsePerfData && _perf_class_preload_counters != nullptr) {
481 st->print_cr("AOTLinkedClassBulkLoader:");
482 st->print_cr(" preload: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) " (thread) / " JLONG_FORMAT_W(5) " events",
483 _perf_class_preload_counters->elapsed_counter_value_us(),
484 _perf_class_preload_counters->thread_counter_value_us(),
485 _perf_classes_preloaded->get_value());
486 }
487 }
|