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) {
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) {
464 // AOTLinkedClassCategory::BOOT1 -- all aot-linked classes in
465 // java.base must have been loaded before a GC can ever happen.
466 return false;
467 } else {
468 // AOTLinkedClassCategory::BOOT2 classes cannot be loaded until
469 // module system is ready.
470 return !_boot2_completed;
471 }
472 } else if (ik->is_shared_platform_class()) {
473 // AOTLinkedClassCategory::PLATFORM classes cannot be loaded until
474 // the platform class loader is initialized.
475 return !_platform_completed;
476 } else if (ik->is_shared_app_class()) {
477 // AOTLinkedClassCategory::APP cannot be loaded until the app class loader
478 // is initialized.
479 return !_app_completed;
480 } else {
481 return false;
482 }
483 }
484
485 void AOTLinkedClassBulkLoader::replay_training_at_init(Array<InstanceKlass*>* classes, TRAPS) {
486 if (classes != nullptr) {
487 for (int i = 0; i < classes->length(); i++) {
488 InstanceKlass* ik = classes->at(i);
489 if (ik->has_aot_initialized_mirror() && ik->is_initialized() && !ik->has_init_deps_processed()) {
490 CompilationPolicy::replay_training_at_init(ik, CHECK);
491 }
492 }
493 }
494 }
495
496 void AOTLinkedClassBulkLoader::replay_training_at_init_for_preloaded_classes(TRAPS) {
497 if (CDSConfig::is_using_aot_linked_classes() && TrainingData::have_data()) {
498 AOTLinkedClassTable* table = AOTLinkedClassTable::for_static_archive(); // not applicable for dynamic archive (?? why??)
499 replay_training_at_init(table->boot(), CHECK);
500 replay_training_at_init(table->boot2(), CHECK);
501 replay_training_at_init(table->platform(), CHECK);
502 replay_training_at_init(table->app(), CHECK);
503
504 CompilationPolicy::replay_training_at_init(false, CHECK);
505 }
506 }
507
508 void AOTLinkedClassBulkLoader::print_counters_on(outputStream* st) {
509 if (UsePerfData && _perf_class_preload_counters != nullptr) {
510 st->print_cr("AOTLinkedClassBulkLoader:");
511 st->print_cr(" preload: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) " (thread) / " JLONG_FORMAT_W(5) " events",
512 _perf_class_preload_counters->elapsed_counter_value_us(),
513 _perf_class_preload_counters->thread_counter_value_us(),
514 _perf_classes_preloaded->get_value());
515 }
516 }
|