372 // CDS code for dumping shared archive. |
372 // CDS code for dumping shared archive. |
373 |
373 |
374 // Global object for holding classes that have been loaded. Since this |
374 // Global object for holding classes that have been loaded. Since this |
375 // is run at a safepoint just before exit, this is the entire set of classes. |
375 // is run at a safepoint just before exit, this is the entire set of classes. |
376 static GrowableArray<Klass*>* _global_klass_objects; |
376 static GrowableArray<Klass*>* _global_klass_objects; |
|
377 |
|
378 static void collect_array_classes(Klass* k) { |
|
379 _global_klass_objects->append_if_missing(k); |
|
380 if (k->is_array_klass()) { |
|
381 // Add in the array classes too |
|
382 ArrayKlass* ak = ArrayKlass::cast(k); |
|
383 Klass* h = ak->higher_dimension(); |
|
384 if (h != NULL) { |
|
385 h->array_klasses_do(collect_array_classes); |
|
386 } |
|
387 } |
|
388 } |
|
389 |
377 class CollectClassesClosure : public KlassClosure { |
390 class CollectClassesClosure : public KlassClosure { |
378 void do_klass(Klass* k) { |
391 void do_klass(Klass* k) { |
379 if (!(k->is_instance_klass() && InstanceKlass::cast(k)->is_in_error_state())) { |
392 if (!(k->is_instance_klass() && InstanceKlass::cast(k)->is_in_error_state())) { |
380 _global_klass_objects->append_if_missing(k); |
393 _global_klass_objects->append_if_missing(k); |
381 } |
394 } |
|
395 if (k->is_array_klass()) { |
|
396 // Add in the array classes too |
|
397 ArrayKlass* ak = ArrayKlass::cast(k); |
|
398 Klass* h = ak->higher_dimension(); |
|
399 if (h != NULL) { |
|
400 h->array_klasses_do(collect_array_classes); |
|
401 } |
|
402 } |
382 } |
403 } |
383 }; |
404 }; |
384 |
405 |
385 static void remove_unshareable_in_classes() { |
406 static void remove_unshareable_in_classes() { |
386 for (int i = 0; i < _global_klass_objects->length(); i++) { |
407 for (int i = 0; i < _global_klass_objects->length(); i++) { |
387 Klass* k = _global_klass_objects->at(i); |
408 Klass* k = _global_klass_objects->at(i); |
388 k->remove_unshareable_info(); |
409 if (!k->is_objArray_klass()) { |
|
410 // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info |
|
411 // on their array classes. |
|
412 assert(k->is_instance_klass() || k->is_typeArray_klass(), "must be"); |
|
413 k->remove_unshareable_info(); |
|
414 } |
|
415 } |
|
416 } |
|
417 |
|
418 static void remove_java_mirror_in_classes() { |
|
419 for (int i = 0; i < _global_klass_objects->length(); i++) { |
|
420 Klass* k = _global_klass_objects->at(i); |
|
421 if (!k->is_objArray_klass()) { |
|
422 // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info |
|
423 // on their array classes. |
|
424 assert(k->is_instance_klass() || k->is_typeArray_klass(), "must be"); |
|
425 k->remove_java_mirror(); |
|
426 } |
389 } |
427 } |
390 } |
428 } |
391 |
429 |
392 static void rewrite_nofast_bytecode(Method* method) { |
430 static void rewrite_nofast_bytecode(Method* method) { |
393 RawBytecodeStream bcs(method); |
431 BytecodeStream bcs(method); |
394 while (!bcs.is_last_bytecode()) { |
432 while (!bcs.is_last_bytecode()) { |
395 Bytecodes::Code opcode = bcs.raw_next(); |
433 Bytecodes::Code opcode = bcs.next(); |
396 switch (opcode) { |
434 switch (opcode) { |
397 case Bytecodes::_getfield: *bcs.bcp() = Bytecodes::_nofast_getfield; break; |
435 case Bytecodes::_getfield: *bcs.bcp() = Bytecodes::_nofast_getfield; break; |
398 case Bytecodes::_putfield: *bcs.bcp() = Bytecodes::_nofast_putfield; break; |
436 case Bytecodes::_putfield: *bcs.bcp() = Bytecodes::_nofast_putfield; break; |
399 case Bytecodes::_aload_0: *bcs.bcp() = Bytecodes::_nofast_aload_0; break; |
437 case Bytecodes::_aload_0: *bcs.bcp() = Bytecodes::_nofast_aload_0; break; |
400 case Bytecodes::_iload: { |
438 case Bytecodes::_iload: { |
443 ik->set_archived_class_data(q); |
481 ik->set_archived_class_data(q); |
444 } |
482 } |
445 } |
483 } |
446 } |
484 } |
447 } |
485 } |
|
486 |
|
487 NOT_PRODUCT( |
|
488 static void assert_not_anonymous_class(InstanceKlass* k) { |
|
489 assert(!(k->is_anonymous()), "cannot archive anonymous classes"); |
|
490 } |
|
491 |
|
492 // Anonymous classes are not stored inside any dictionaries. They are created by |
|
493 // SystemDictionary::parse_stream() with a non-null host_klass. |
|
494 static void assert_no_anonymoys_classes_in_dictionaries() { |
|
495 ClassLoaderDataGraph::dictionary_classes_do(assert_not_anonymous_class); |
|
496 }) |
448 |
497 |
449 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables. |
498 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables. |
450 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.) |
499 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.) |
451 // |
500 // |
452 // Addresses of the vtables and the methods may be different across JVM runs, |
501 // Addresses of the vtables and the methods may be different across JVM runs, |
955 p = _rw_region.allocate(bytes, alignment); |
1004 p = _rw_region.allocate(bytes, alignment); |
956 newtop = _rw_region.top(); |
1005 newtop = _rw_region.top(); |
957 } |
1006 } |
958 memcpy(p, obj, bytes); |
1007 memcpy(p, obj, bytes); |
959 bool isnew = _new_loc_table->put(obj, (address)p); |
1008 bool isnew = _new_loc_table->put(obj, (address)p); |
|
1009 log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(obj), p2i(p), bytes); |
960 assert(isnew, "must be"); |
1010 assert(isnew, "must be"); |
961 log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(obj), p2i(p), bytes); |
|
962 |
1011 |
963 _alloc_stats->record(ref->msotype(), int(newtop - oldtop), read_only); |
1012 _alloc_stats->record(ref->msotype(), int(newtop - oldtop), read_only); |
964 if (ref->msotype() == MetaspaceObj::SymbolType) { |
1013 if (ref->msotype() == MetaspaceObj::SymbolType) { |
965 uintx delta = MetaspaceShared::object_delta(p); |
1014 uintx delta = MetaspaceShared::object_delta(p); |
966 if (delta > MAX_SHARED_DELTA) { |
1015 if (delta > MAX_SHARED_DELTA) { |
1149 char* VM_PopulateDumpSharedSpace::dump_read_only_tables() { |
1198 char* VM_PopulateDumpSharedSpace::dump_read_only_tables() { |
1150 char* oldtop = _ro_region.top(); |
1199 char* oldtop = _ro_region.top(); |
1151 // Reorder the system dictionary. Moving the symbols affects |
1200 // Reorder the system dictionary. Moving the symbols affects |
1152 // how the hash table indices are calculated. |
1201 // how the hash table indices are calculated. |
1153 SystemDictionary::reorder_dictionary_for_sharing(); |
1202 SystemDictionary::reorder_dictionary_for_sharing(); |
|
1203 tty->print("Removing java_mirror ... "); |
|
1204 remove_java_mirror_in_classes(); |
|
1205 tty->print_cr("done. "); |
1154 NOT_PRODUCT(SystemDictionary::verify();) |
1206 NOT_PRODUCT(SystemDictionary::verify();) |
1155 |
1207 |
1156 size_t buckets_bytes = SystemDictionary::count_bytes_for_buckets(); |
1208 size_t buckets_bytes = SystemDictionary::count_bytes_for_buckets(); |
1157 char* buckets_top = _ro_region.allocate(buckets_bytes, sizeof(intptr_t)); |
1209 char* buckets_top = _ro_region.allocate(buckets_bytes, sizeof(intptr_t)); |
1158 SystemDictionary::copy_buckets(buckets_top, _ro_region.top()); |
1210 SystemDictionary::copy_buckets(buckets_top, _ro_region.top()); |
1216 // Ensure the ConstMethods won't be modified at run-time |
1268 // Ensure the ConstMethods won't be modified at run-time |
1217 tty->print("Updating ConstMethods ... "); |
1269 tty->print("Updating ConstMethods ... "); |
1218 rewrite_nofast_bytecodes_and_calculate_fingerprints(); |
1270 rewrite_nofast_bytecodes_and_calculate_fingerprints(); |
1219 tty->print_cr("done. "); |
1271 tty->print_cr("done. "); |
1220 |
1272 |
|
1273 // Move classes from platform/system dictionaries into the boot dictionary |
|
1274 SystemDictionary::combine_shared_dictionaries(); |
|
1275 |
1221 // Remove all references outside the metadata |
1276 // Remove all references outside the metadata |
1222 tty->print("Removing unshareable information ... "); |
1277 tty->print("Removing unshareable information ... "); |
1223 remove_unshareable_in_classes(); |
1278 remove_unshareable_in_classes(); |
1224 tty->print_cr("done. "); |
1279 tty->print_cr("done. "); |
|
1280 |
|
1281 // We don't support archiving anonymous classes. Verify that they are not stored in |
|
1282 // the any dictionaries. |
|
1283 NOT_PRODUCT(assert_no_anonymoys_classes_in_dictionaries()); |
|
1284 |
|
1285 SystemDictionaryShared::finalize_verification_constraints(); |
1225 |
1286 |
1226 ArchiveCompactor::initialize(); |
1287 ArchiveCompactor::initialize(); |
1227 ArchiveCompactor::copy_and_compact(); |
1288 ArchiveCompactor::copy_and_compact(); |
1228 |
1289 |
1229 dump_symbols(); |
1290 dump_symbols(); |
1310 |
1371 |
1311 if (log_is_enabled(Info, cds)) { |
1372 if (log_is_enabled(Info, cds)) { |
1312 ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()), |
1373 ArchiveCompactor::alloc_stats()->print_stats(int(_ro_region.used()), int(_rw_region.used()), |
1313 int(_mc_region.used()), int(_md_region.used())); |
1374 int(_mc_region.used()), int(_md_region.used())); |
1314 } |
1375 } |
|
1376 |
|
1377 if (PrintSystemDictionaryAtExit) { |
|
1378 SystemDictionary::print(); |
|
1379 } |
|
1380 // There may be other pending VM operations that operate on the InstanceKlasses, |
|
1381 // which will fail because InstanceKlasses::remove_unshareable_info() |
|
1382 // has been called. Forget these operations and exit the VM directly. |
|
1383 vm_direct_exit(0); |
1315 } |
1384 } |
1316 |
1385 |
1317 void VM_PopulateDumpSharedSpace::print_region_stats() { |
1386 void VM_PopulateDumpSharedSpace::print_region_stats() { |
1318 // Print statistics of all the regions |
1387 // Print statistics of all the regions |
1319 const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() + |
1388 const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() + |
1507 // fails verification, all other interfaces that were not specified in the classlist but |
1572 // fails verification, all other interfaces that were not specified in the classlist but |
1508 // are implemented by K are not verified. |
1573 // are implemented by K are not verified. |
1509 link_and_cleanup_shared_classes(CATCH); |
1574 link_and_cleanup_shared_classes(CATCH); |
1510 tty->print_cr("Rewriting and linking classes: done"); |
1575 tty->print_cr("Rewriting and linking classes: done"); |
1511 |
1576 |
|
1577 SystemDictionary::clear_invoke_method_table(); |
|
1578 |
1512 VM_PopulateDumpSharedSpace op; |
1579 VM_PopulateDumpSharedSpace op; |
1513 VMThread::execute(&op); |
1580 VMThread::execute(&op); |
1514 } |
1581 } |
1515 |
|
1516 if (PrintSystemDictionaryAtExit) { |
|
1517 SystemDictionary::print(); |
|
1518 } |
|
1519 |
|
1520 // Since various initialization steps have been undone by this process, |
|
1521 // it is not reasonable to continue running a java process. |
|
1522 exit(0); |
|
1523 } |
1582 } |
1524 |
1583 |
1525 |
1584 |
1526 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) { |
1585 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) { |
1527 ClassListParser parser(class_list_path); |
1586 ClassListParser parser(class_list_path); |
1528 int class_count = 0; |
1587 int class_count = 0; |
1529 |
1588 |
1530 while (parser.parse_one_line()) { |
1589 while (parser.parse_one_line()) { |
1531 Klass* klass = ClassLoaderExt::load_one_class(&parser, THREAD); |
1590 Klass* klass = ClassLoaderExt::load_one_class(&parser, THREAD); |
1532 |
1591 if (HAS_PENDING_EXCEPTION) { |
1533 CLEAR_PENDING_EXCEPTION; |
1592 if (klass == NULL && |
|
1593 (PENDING_EXCEPTION->klass()->name() == vmSymbols::java_lang_ClassNotFoundException())) { |
|
1594 // print a warning only when the pending exception is class not found |
|
1595 tty->print_cr("Preload Warning: Cannot find %s", parser.current_class_name()); |
|
1596 } |
|
1597 CLEAR_PENDING_EXCEPTION; |
|
1598 } |
1534 if (klass != NULL) { |
1599 if (klass != NULL) { |
1535 if (log_is_enabled(Trace, cds)) { |
1600 if (log_is_enabled(Trace, cds)) { |
1536 ResourceMark rm; |
1601 ResourceMark rm; |
1537 log_trace(cds)("Shared spaces preloaded: %s", klass->external_name()); |
1602 log_trace(cds)("Shared spaces preloaded: %s", klass->external_name()); |
1538 } |
1603 } |