71 #include "runtime/init.hpp"
72 #include "runtime/java.hpp"
73 #include "runtime/javaCalls.hpp"
74 #include "runtime/sharedRuntime.hpp"
75 #include "runtime/synchronizer.hpp"
76 #include "runtime/thread.inline.hpp"
77 #include "runtime/timerTrace.hpp"
78 #include "runtime/vmOperations.hpp"
79 #include "services/memoryService.hpp"
80 #include "utilities/align.hpp"
81 #include "utilities/autoRestore.hpp"
82 #include "utilities/copy.hpp"
83 #include "utilities/debug.hpp"
84 #include "utilities/events.hpp"
85 #include "utilities/formatBuffer.hpp"
86 #include "utilities/hashtable.inline.hpp"
87 #include "utilities/macros.hpp"
88 #include "utilities/ostream.hpp"
89 #include "utilities/preserveException.hpp"
90
91 #define PRIMITIVE_MIRRORS_DO(func) \
92 func(_int_mirror) \
93 func(_float_mirror) \
94 func(_double_mirror) \
95 func(_byte_mirror) \
96 func(_bool_mirror) \
97 func(_char_mirror) \
98 func(_long_mirror) \
99 func(_short_mirror) \
100 func(_void_mirror)
101
102 #define DEFINE_PRIMITIVE_MIRROR(m) \
103 oop Universe::m = NULL;
104
105 // Known objects
106 PRIMITIVE_MIRRORS_DO(DEFINE_PRIMITIVE_MIRROR)
107 Klass* Universe::_typeArrayKlassObjs[T_LONG+1] = { NULL /*, NULL...*/ };
108 Klass* Universe::_objectArrayKlassObj = NULL;
109 oop Universe::_mirrors[T_VOID+1] = { NULL /*, NULL...*/ };
110
111 OopHandle Universe::_main_thread_group;
112 OopHandle Universe::_system_thread_group;
113 OopHandle Universe::_the_empty_class_array;
114 OopHandle Universe::_the_null_string;
115 OopHandle Universe::_the_min_jint_string;
116
117 OopHandle Universe::_the_null_sentinel;
118
119 // _out_of_memory_errors is an objArray
120 enum OutOfMemoryInstance { _oom_java_heap,
121 _oom_metaspace,
122 _oom_class_metaspace,
123 _oom_array_size,
124 _oom_gc_overhead_limit,
125 _oom_realloc_objects,
126 _oom_retry,
127 _oom_count };
128
129 OopHandle Universe::_out_of_memory_errors;
179
180 objArrayOop Universe::the_empty_class_array () {
181 return (objArrayOop)_the_empty_class_array.resolve();
182 }
183
184 oop Universe::main_thread_group() { return _main_thread_group.resolve(); }
185 void Universe::set_main_thread_group(oop group) { _main_thread_group = OopHandle(vm_global(), group); }
186
187 oop Universe::system_thread_group() { return _system_thread_group.resolve(); }
188 void Universe::set_system_thread_group(oop group) { _system_thread_group = OopHandle(vm_global(), group); }
189
190 oop Universe::the_null_string() { return _the_null_string.resolve(); }
191 oop Universe::the_min_jint_string() { return _the_min_jint_string.resolve(); }
192
193 oop Universe::null_ptr_exception_instance() { return _null_ptr_exception_instance.resolve(); }
194 oop Universe::arithmetic_exception_instance() { return _arithmetic_exception_instance.resolve(); }
195 oop Universe::virtual_machine_error_instance() { return _virtual_machine_error_instance.resolve(); }
196
197 oop Universe::the_null_sentinel() { return _the_null_sentinel.resolve(); }
198
199 void Universe::basic_type_classes_do(void f(Klass*)) {
200 for (int i = T_BOOLEAN; i < T_LONG+1; i++) {
201 f(_typeArrayKlassObjs[i]);
202 }
203 }
204
205 void Universe::basic_type_classes_do(KlassClosure *closure) {
206 for (int i = T_BOOLEAN; i < T_LONG+1; i++) {
207 closure->do_klass(_typeArrayKlassObjs[i]);
208 }
209 }
210
211 #define DO_PRIMITIVE_MIRROR(m) \
212 f->do_oop((oop*) &m);
213
214 void Universe::oops_do(OopClosure* f) {
215 PRIMITIVE_MIRRORS_DO(DO_PRIMITIVE_MIRROR);
216
217 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
218 f->do_oop(&_mirrors[i]);
219 }
220 assert(_mirrors[0] == NULL && _mirrors[T_BOOLEAN - 1] == NULL, "checking");
221
222 f->do_oop(&_reference_pending_list);
223 ThreadsSMRSupport::exiting_threads_oops_do(f);
224 }
225
226 void LatestMethodCache::metaspace_pointers_do(MetaspaceClosure* it) {
227 it->push(&_klass);
228 }
229
230 void Universe::metaspace_pointers_do(MetaspaceClosure* it) {
231 for (int i = 0; i < T_LONG+1; i++) {
232 it->push(&_typeArrayKlassObjs[i]);
233 }
234 it->push(&_objectArrayKlassObj);
235
236 it->push(&_the_empty_int_array);
237 it->push(&_the_empty_short_array);
238 it->push(&_the_empty_klass_array);
239 it->push(&_the_empty_instance_klass_array);
240 it->push(&_the_empty_method_array);
241 it->push(&_the_array_interfaces_array);
242
243 _finalizer_register_cache->metaspace_pointers_do(it);
244 _loader_addClass_cache->metaspace_pointers_do(it);
245 _throw_illegal_access_error_cache->metaspace_pointers_do(it);
246 _throw_no_such_method_error_cache->metaspace_pointers_do(it);
247 _do_stack_walk_cache->metaspace_pointers_do(it);
248 }
249
250 #define ASSERT_MIRROR_NULL(m) \
251 assert(m == NULL, "archived mirrors should be NULL");
252
253 #define SERIALIZE_MIRROR(m) \
254 f->do_oop(&m); \
255 if (m != NULL) { java_lang_Class::update_archived_primitive_mirror_native_pointers(m); }
256
257 // Serialize metadata and pointers to primitive type mirrors in and out of CDS archive
258 void Universe::serialize(SerializeClosure* f) {
259
260 for (int i = 0; i < T_LONG+1; i++) {
261 f->do_ptr((void**)&_typeArrayKlassObjs[i]);
262 }
263
264 f->do_ptr((void**)&_objectArrayKlassObj);
265
266 #if INCLUDE_CDS_JAVA_HEAP
267 DEBUG_ONLY(if (DumpSharedSpaces && !HeapShared::is_heap_object_archiving_allowed()) {
268 PRIMITIVE_MIRRORS_DO(ASSERT_MIRROR_NULL);
269 });
270 PRIMITIVE_MIRRORS_DO(SERIALIZE_MIRROR);
271 #endif
272
273 f->do_ptr((void**)&_the_array_interfaces_array);
274 f->do_ptr((void**)&_the_empty_int_array);
275 f->do_ptr((void**)&_the_empty_short_array);
276 f->do_ptr((void**)&_the_empty_method_array);
277 f->do_ptr((void**)&_the_empty_klass_array);
278 f->do_ptr((void**)&_the_empty_instance_klass_array);
279 _finalizer_register_cache->serialize(f);
280 _loader_addClass_cache->serialize(f);
281 _throw_illegal_access_error_cache->serialize(f);
282 _throw_no_such_method_error_cache->serialize(f);
283 _do_stack_walk_cache->serialize(f);
284 }
285
286 void Universe::check_alignment(uintx size, uintx alignment, const char* name) {
287 if (size < alignment || size % alignment != 0) {
288 vm_exit_during_initialization(
289 err_msg("Size of %s (" UINTX_FORMAT " bytes) must be aligned to " UINTX_FORMAT " bytes", name, size, alignment));
290 }
291 }
292
293 void initialize_basic_type_klass(Klass* k, TRAPS) {
294 Klass* ok = SystemDictionary::Object_klass();
295 #if INCLUDE_CDS
296 if (UseSharedSpaces) {
297 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
298 assert(k->super() == ok, "u3");
299 if (k->is_instance_klass()) {
300 InstanceKlass::cast(k)->restore_unshareable_info(loader_data, Handle(), NULL, CHECK);
301 } else {
302 ArrayKlass::cast(k)->restore_unshareable_info(loader_data, Handle(), CHECK);
303 }
304 } else
305 #endif
417 int i = 0;
418 while (i < size) {
419 // Allocate dummy in old generation
420 oop dummy = SystemDictionary::Object_klass()->allocate_instance(CHECK);
421 dummy_array->obj_at_put(i++, dummy);
422 }
423 {
424 // Only modify the global variable inside the mutex.
425 // If we had a race to here, the other dummy_array instances
426 // and their elements just get dropped on the floor, which is fine.
427 MutexLocker ml(THREAD, FullGCALot_lock);
428 if (_fullgc_alot_dummy_array.is_empty()) {
429 _fullgc_alot_dummy_array = OopHandle(vm_global(), dummy_array());
430 }
431 }
432 assert(i == ((objArrayOop)_fullgc_alot_dummy_array.resolve())->length(), "just checking");
433 }
434 #endif
435 }
436
437 #define ASSERT_MIRROR_NOT_NULL(m) \
438 assert(m != NULL, "archived mirrors should not be NULL");
439
440 void Universe::initialize_basic_type_mirrors(TRAPS) {
441 #if INCLUDE_CDS_JAVA_HEAP
442 if (UseSharedSpaces &&
443 HeapShared::open_archive_heap_region_mapped() &&
444 _int_mirror != NULL) {
445 assert(HeapShared::is_heap_object_archiving_allowed(), "Sanity");
446 PRIMITIVE_MIRRORS_DO(ASSERT_MIRROR_NOT_NULL);
447 } else
448 // _int_mirror could be NULL if archived heap is not mapped.
449 #endif
450 {
451 _int_mirror =
452 java_lang_Class::create_basic_type_mirror("int", T_INT, CHECK);
453 _float_mirror =
454 java_lang_Class::create_basic_type_mirror("float", T_FLOAT, CHECK);
455 _double_mirror =
456 java_lang_Class::create_basic_type_mirror("double", T_DOUBLE, CHECK);
457 _byte_mirror =
458 java_lang_Class::create_basic_type_mirror("byte", T_BYTE, CHECK);
459 _bool_mirror =
460 java_lang_Class::create_basic_type_mirror("boolean",T_BOOLEAN, CHECK);
461 _char_mirror =
462 java_lang_Class::create_basic_type_mirror("char", T_CHAR, CHECK);
463 _long_mirror =
464 java_lang_Class::create_basic_type_mirror("long", T_LONG, CHECK);
465 _short_mirror =
466 java_lang_Class::create_basic_type_mirror("short", T_SHORT, CHECK);
467 _void_mirror =
468 java_lang_Class::create_basic_type_mirror("void", T_VOID, CHECK);
469 }
470
471 _mirrors[T_INT] = _int_mirror;
472 _mirrors[T_FLOAT] = _float_mirror;
473 _mirrors[T_DOUBLE] = _double_mirror;
474 _mirrors[T_BYTE] = _byte_mirror;
475 _mirrors[T_BOOLEAN] = _bool_mirror;
476 _mirrors[T_CHAR] = _char_mirror;
477 _mirrors[T_LONG] = _long_mirror;
478 _mirrors[T_SHORT] = _short_mirror;
479 _mirrors[T_VOID] = _void_mirror;
480 //_mirrors[T_OBJECT] = _object_klass->java_mirror();
481 //_mirrors[T_ARRAY] = _object_klass->java_mirror();
482 }
483
484 void Universe::fixup_mirrors(TRAPS) {
485 // Bootstrap problem: all classes gets a mirror (java.lang.Class instance) assigned eagerly,
486 // but we cannot do that for classes created before java.lang.Class is loaded. Here we simply
487 // walk over permanent objects created so far (mostly classes) and fixup their mirrors. Note
488 // that the number of objects allocated at this point is very small.
489 assert(SystemDictionary::Class_klass_loaded(), "java.lang.Class should be loaded");
490 HandleMark hm(THREAD);
491
492 if (!UseSharedSpaces) {
493 // Cache the start of the static fields
494 InstanceMirrorKlass::init_offset_of_static_fields();
495 }
496
497 GrowableArray <Klass*>* list = java_lang_Class::fixup_mirror_list();
498 int list_length = list->length();
499 for (int i = 0; i < list_length; i++) {
500 Klass* k = list->at(i);
501 assert(k->is_klass(), "List should only hold classes");
|
71 #include "runtime/init.hpp"
72 #include "runtime/java.hpp"
73 #include "runtime/javaCalls.hpp"
74 #include "runtime/sharedRuntime.hpp"
75 #include "runtime/synchronizer.hpp"
76 #include "runtime/thread.inline.hpp"
77 #include "runtime/timerTrace.hpp"
78 #include "runtime/vmOperations.hpp"
79 #include "services/memoryService.hpp"
80 #include "utilities/align.hpp"
81 #include "utilities/autoRestore.hpp"
82 #include "utilities/copy.hpp"
83 #include "utilities/debug.hpp"
84 #include "utilities/events.hpp"
85 #include "utilities/formatBuffer.hpp"
86 #include "utilities/hashtable.inline.hpp"
87 #include "utilities/macros.hpp"
88 #include "utilities/ostream.hpp"
89 #include "utilities/preserveException.hpp"
90
91 // Known objects
92 Klass* Universe::_typeArrayKlassObjs[T_LONG+1] = { NULL /*, NULL...*/ };
93 Klass* Universe::_objectArrayKlassObj = NULL;
94 OopHandle Universe::_mirrors[T_VOID+1];
95
96 OopHandle Universe::_main_thread_group;
97 OopHandle Universe::_system_thread_group;
98 OopHandle Universe::_the_empty_class_array;
99 OopHandle Universe::_the_null_string;
100 OopHandle Universe::_the_min_jint_string;
101
102 OopHandle Universe::_the_null_sentinel;
103
104 // _out_of_memory_errors is an objArray
105 enum OutOfMemoryInstance { _oom_java_heap,
106 _oom_metaspace,
107 _oom_class_metaspace,
108 _oom_array_size,
109 _oom_gc_overhead_limit,
110 _oom_realloc_objects,
111 _oom_retry,
112 _oom_count };
113
114 OopHandle Universe::_out_of_memory_errors;
164
165 objArrayOop Universe::the_empty_class_array () {
166 return (objArrayOop)_the_empty_class_array.resolve();
167 }
168
169 oop Universe::main_thread_group() { return _main_thread_group.resolve(); }
170 void Universe::set_main_thread_group(oop group) { _main_thread_group = OopHandle(vm_global(), group); }
171
172 oop Universe::system_thread_group() { return _system_thread_group.resolve(); }
173 void Universe::set_system_thread_group(oop group) { _system_thread_group = OopHandle(vm_global(), group); }
174
175 oop Universe::the_null_string() { return _the_null_string.resolve(); }
176 oop Universe::the_min_jint_string() { return _the_min_jint_string.resolve(); }
177
178 oop Universe::null_ptr_exception_instance() { return _null_ptr_exception_instance.resolve(); }
179 oop Universe::arithmetic_exception_instance() { return _arithmetic_exception_instance.resolve(); }
180 oop Universe::virtual_machine_error_instance() { return _virtual_machine_error_instance.resolve(); }
181
182 oop Universe::the_null_sentinel() { return _the_null_sentinel.resolve(); }
183
184 oop Universe::int_mirror() { return check_mirror(_mirrors[T_INT].resolve()); }
185 oop Universe::float_mirror() { return check_mirror(_mirrors[T_FLOAT].resolve()); }
186 oop Universe::double_mirror() { return check_mirror(_mirrors[T_DOUBLE].resolve()); }
187 oop Universe::byte_mirror() { return check_mirror(_mirrors[T_BYTE].resolve()); }
188 oop Universe::bool_mirror() { return check_mirror(_mirrors[T_BOOLEAN].resolve()); }
189 oop Universe::char_mirror() { return check_mirror(_mirrors[T_CHAR].resolve()); }
190 oop Universe::long_mirror() { return check_mirror(_mirrors[T_LONG].resolve()); }
191 oop Universe::short_mirror() { return check_mirror(_mirrors[T_SHORT].resolve()); }
192 oop Universe::void_mirror() { return check_mirror(_mirrors[T_VOID].resolve()); }
193
194 oop Universe::java_mirror(BasicType t) {
195 assert((uint)t < T_VOID+1, "range check");
196 return check_mirror(_mirrors[t].resolve());
197 }
198
199 // Used by CDS dumping
200 void Universe::replace_mirror(BasicType t, oop new_mirror) {
201 Universe::_mirrors[t].replace(new_mirror);
202 }
203
204 // Not sure why CDS has to do this
205 void Universe::clear_basic_type_mirrors() {
206 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
207 if (!is_reference_type((BasicType)i)) {
208 Universe::_mirrors[i].replace(NULL);
209 }
210 }
211 }
212
213 void Universe::basic_type_classes_do(void f(Klass*)) {
214 for (int i = T_BOOLEAN; i < T_LONG+1; i++) {
215 f(_typeArrayKlassObjs[i]);
216 }
217 }
218
219 void Universe::basic_type_classes_do(KlassClosure *closure) {
220 for (int i = T_BOOLEAN; i < T_LONG+1; i++) {
221 closure->do_klass(_typeArrayKlassObjs[i]);
222 }
223 }
224
225 void Universe::oops_do(OopClosure* f) {
226
227 f->do_oop(&_reference_pending_list);
228 ThreadsSMRSupport::exiting_threads_oops_do(f);
229 }
230
231 void LatestMethodCache::metaspace_pointers_do(MetaspaceClosure* it) {
232 it->push(&_klass);
233 }
234
235 void Universe::metaspace_pointers_do(MetaspaceClosure* it) {
236 for (int i = 0; i < T_LONG+1; i++) {
237 it->push(&_typeArrayKlassObjs[i]);
238 }
239 it->push(&_objectArrayKlassObj);
240
241 it->push(&_the_empty_int_array);
242 it->push(&_the_empty_short_array);
243 it->push(&_the_empty_klass_array);
244 it->push(&_the_empty_instance_klass_array);
245 it->push(&_the_empty_method_array);
246 it->push(&_the_array_interfaces_array);
247
248 _finalizer_register_cache->metaspace_pointers_do(it);
249 _loader_addClass_cache->metaspace_pointers_do(it);
250 _throw_illegal_access_error_cache->metaspace_pointers_do(it);
251 _throw_no_such_method_error_cache->metaspace_pointers_do(it);
252 _do_stack_walk_cache->metaspace_pointers_do(it);
253 }
254
255 // Serialize metadata and pointers to primitive type mirrors in and out of CDS archive
256 void Universe::serialize(SerializeClosure* f) {
257
258 #if INCLUDE_CDS_JAVA_HEAP
259 {
260 oop mirror_oop;
261 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
262 if (f->reading()) {
263 f->do_oop(&mirror_oop); // read from archive
264 assert(oopDesc::is_oop_or_null(mirror_oop), "is oop");
265 _mirrors[i] = OopHandle(vm_global(), mirror_oop);
266 } else {
267 mirror_oop = _mirrors[i].resolve();
268 f->do_oop(&mirror_oop); // write to archive
269 }
270 if (mirror_oop != NULL) { // may be null if archived heap is disabled
271 java_lang_Class::update_archived_primitive_mirror_native_pointers(mirror_oop);
272 }
273 }
274 }
275 #endif
276
277 for (int i = 0; i < T_LONG+1; i++) {
278 f->do_ptr((void**)&_typeArrayKlassObjs[i]);
279 }
280
281 f->do_ptr((void**)&_objectArrayKlassObj);
282 f->do_ptr((void**)&_the_array_interfaces_array);
283 f->do_ptr((void**)&_the_empty_int_array);
284 f->do_ptr((void**)&_the_empty_short_array);
285 f->do_ptr((void**)&_the_empty_method_array);
286 f->do_ptr((void**)&_the_empty_klass_array);
287 f->do_ptr((void**)&_the_empty_instance_klass_array);
288 _finalizer_register_cache->serialize(f);
289 _loader_addClass_cache->serialize(f);
290 _throw_illegal_access_error_cache->serialize(f);
291 _throw_no_such_method_error_cache->serialize(f);
292 _do_stack_walk_cache->serialize(f);
293 }
294
295
296 void Universe::check_alignment(uintx size, uintx alignment, const char* name) {
297 if (size < alignment || size % alignment != 0) {
298 vm_exit_during_initialization(
299 err_msg("Size of %s (" UINTX_FORMAT " bytes) must be aligned to " UINTX_FORMAT " bytes", name, size, alignment));
300 }
301 }
302
303 void initialize_basic_type_klass(Klass* k, TRAPS) {
304 Klass* ok = SystemDictionary::Object_klass();
305 #if INCLUDE_CDS
306 if (UseSharedSpaces) {
307 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
308 assert(k->super() == ok, "u3");
309 if (k->is_instance_klass()) {
310 InstanceKlass::cast(k)->restore_unshareable_info(loader_data, Handle(), NULL, CHECK);
311 } else {
312 ArrayKlass::cast(k)->restore_unshareable_info(loader_data, Handle(), CHECK);
313 }
314 } else
315 #endif
427 int i = 0;
428 while (i < size) {
429 // Allocate dummy in old generation
430 oop dummy = SystemDictionary::Object_klass()->allocate_instance(CHECK);
431 dummy_array->obj_at_put(i++, dummy);
432 }
433 {
434 // Only modify the global variable inside the mutex.
435 // If we had a race to here, the other dummy_array instances
436 // and their elements just get dropped on the floor, which is fine.
437 MutexLocker ml(THREAD, FullGCALot_lock);
438 if (_fullgc_alot_dummy_array.is_empty()) {
439 _fullgc_alot_dummy_array = OopHandle(vm_global(), dummy_array());
440 }
441 }
442 assert(i == ((objArrayOop)_fullgc_alot_dummy_array.resolve())->length(), "just checking");
443 }
444 #endif
445 }
446
447 void Universe::initialize_basic_type_mirrors(TRAPS) {
448 #if INCLUDE_CDS_JAVA_HEAP
449 if (UseSharedSpaces &&
450 HeapShared::open_archive_heap_region_mapped() &&
451 _mirrors[T_INT].resolve() != NULL) {
452 assert(HeapShared::is_heap_object_archiving_allowed(), "Sanity");
453
454 // check that all mirrors are mapped also
455 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
456 if (!is_reference_type((BasicType)i)) {
457 oop m = _mirrors[i].resolve();
458 assert(m != NULL, "archived mirrors should not be NULL");
459 }
460 }
461 } else
462 // _mirror[T_INT} could be NULL if archived heap is not mapped.
463 #endif
464 {
465 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
466 BasicType bt = (BasicType)i;
467 if (!is_reference_type(bt)) {
468 oop m = java_lang_Class::create_basic_type_mirror(type2name(bt), bt, CHECK);
469 _mirrors[i] = OopHandle(vm_global(), m);
470 }
471 }
472 }
473 }
474
475 void Universe::fixup_mirrors(TRAPS) {
476 // Bootstrap problem: all classes gets a mirror (java.lang.Class instance) assigned eagerly,
477 // but we cannot do that for classes created before java.lang.Class is loaded. Here we simply
478 // walk over permanent objects created so far (mostly classes) and fixup their mirrors. Note
479 // that the number of objects allocated at this point is very small.
480 assert(SystemDictionary::Class_klass_loaded(), "java.lang.Class should be loaded");
481 HandleMark hm(THREAD);
482
483 if (!UseSharedSpaces) {
484 // Cache the start of the static fields
485 InstanceMirrorKlass::init_offset_of_static_fields();
486 }
487
488 GrowableArray <Klass*>* list = java_lang_Class::fixup_mirror_list();
489 int list_length = list->length();
490 for (int i = 0; i < list_length; i++) {
491 Klass* k = list->at(i);
492 assert(k->is_klass(), "List should only hold classes");
|