src/share/vm/memory/metaspaceShared.cpp

changeset 7089
6e0cb14ce59b
parent 6680
78bbf4d43a14
child 7103
622c6e0ad4d6
equal deleted inserted replaced
7088:999824269b71 7089:6e0cb14ce59b
24 24
25 #include "precompiled.hpp" 25 #include "precompiled.hpp"
26 #include "classfile/dictionary.hpp" 26 #include "classfile/dictionary.hpp"
27 #include "classfile/loaderConstraints.hpp" 27 #include "classfile/loaderConstraints.hpp"
28 #include "classfile/placeholders.hpp" 28 #include "classfile/placeholders.hpp"
29 #include "classfile/sharedClassUtil.hpp"
29 #include "classfile/symbolTable.hpp" 30 #include "classfile/symbolTable.hpp"
30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/systemDictionary.hpp"
31 #include "code/codeCache.hpp" 32 #include "code/codeCache.hpp"
32 #include "memory/filemap.hpp" 33 #include "memory/filemap.hpp"
33 #include "memory/gcLocker.hpp" 34 #include "memory/gcLocker.hpp"
44 45
45 int MetaspaceShared::_max_alignment = 0; 46 int MetaspaceShared::_max_alignment = 0;
46 47
47 ReservedSpace* MetaspaceShared::_shared_rs = NULL; 48 ReservedSpace* MetaspaceShared::_shared_rs = NULL;
48 49
50 bool MetaspaceShared::_link_classes_made_progress;
51 bool MetaspaceShared::_check_classes_made_progress;
52 bool MetaspaceShared::_has_error_classes;
53 bool MetaspaceShared::_archive_loading_failed = false;
49 // Read/write a data stream for restoring/preserving metadata pointers and 54 // Read/write a data stream for restoring/preserving metadata pointers and
50 // miscellaneous data from/to the shared archive file. 55 // miscellaneous data from/to the shared archive file.
51 56
52 void MetaspaceShared::serialize(SerializeClosure* soc) { 57 void MetaspaceShared::serialize(SerializeClosure* soc) {
53 int tag = 0; 58 int tag = 0;
443 _global_klass_objects = new GrowableArray<Klass*>(1000); 448 _global_klass_objects = new GrowableArray<Klass*>(1000);
444 Universe::basic_type_classes_do(collect_classes); 449 Universe::basic_type_classes_do(collect_classes);
445 SystemDictionary::classes_do(collect_classes); 450 SystemDictionary::classes_do(collect_classes);
446 451
447 tty->print_cr("Number of classes %d", _global_klass_objects->length()); 452 tty->print_cr("Number of classes %d", _global_klass_objects->length());
453 {
454 int num_type_array = 0, num_obj_array = 0, num_inst = 0;
455 for (int i = 0; i < _global_klass_objects->length(); i++) {
456 Klass* k = _global_klass_objects->at(i);
457 if (k->oop_is_instance()) {
458 num_inst ++;
459 } else if (k->oop_is_objArray()) {
460 num_obj_array ++;
461 } else {
462 assert(k->oop_is_typeArray(), "sanity");
463 num_type_array ++;
464 }
465 }
466 tty->print_cr(" instance classes = %5d", num_inst);
467 tty->print_cr(" obj array classes = %5d", num_obj_array);
468 tty->print_cr(" type array classes = %5d", num_type_array);
469 }
448 470
449 // Update all the fingerprints in the shared methods. 471 // Update all the fingerprints in the shared methods.
450 tty->print("Calculating fingerprints ... "); 472 tty->print("Calculating fingerprints ... ");
451 calculate_fingerprints(); 473 calculate_fingerprints();
452 tty->print_cr("done. "); 474 tty->print_cr("done. ");
608 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes)); 630 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes));
609 } 631 }
610 #undef fmt_space 632 #undef fmt_space
611 } 633 }
612 634
613 static void link_shared_classes(Klass* obj, TRAPS) { 635
636 void MetaspaceShared::link_one_shared_class(Klass* obj, TRAPS) {
614 Klass* k = obj; 637 Klass* k = obj;
615 if (k->oop_is_instance()) { 638 if (k->oop_is_instance()) {
616 InstanceKlass* ik = (InstanceKlass*) k; 639 InstanceKlass* ik = (InstanceKlass*) k;
617 // Link the class to cause the bytecodes to be rewritten and the 640 // Link the class to cause the bytecodes to be rewritten and the
618 // cpcache to be created. 641 // cpcache to be created. Class verification is done according
619 if (ik->init_state() < InstanceKlass::linked) { 642 // to -Xverify setting.
620 ik->link_class(THREAD); 643 _link_classes_made_progress |= try_link_class(ik, THREAD);
621 guarantee(!HAS_PENDING_EXCEPTION, "exception in class rewriting"); 644 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
622 } 645 }
623 } 646 }
624 } 647
625 648 void MetaspaceShared::check_one_shared_class(Klass* k) {
626 649 if (k->oop_is_instance() && InstanceKlass::cast(k)->check_sharing_error_state()) {
627 // Support for a simple checksum of the contents of the class list 650 _check_classes_made_progress = true;
628 // file to prevent trivial tampering. The algorithm matches that in 651 }
629 // the MakeClassList program used by the J2SE build process. 652 }
630 #define JSUM_SEED ((jlong)CONST64(0xcafebabebabecafe)) 653
631 static jlong 654 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) {
632 jsum(jlong start, const char *buf, const int len) 655 // We need to iterate because verification may cause additional classes
633 { 656 // to be loaded.
634 jlong h = start; 657 do {
635 char *p = (char *)buf, *e = p + len; 658 _link_classes_made_progress = false;
636 while (p < e) { 659 SystemDictionary::classes_do(link_one_shared_class, THREAD);
637 char c = *p++; 660 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
638 if (c <= ' ') { 661 } while (_link_classes_made_progress);
639 /* Skip spaces and control characters */ 662
640 continue; 663 if (_has_error_classes) {
641 } 664 // Mark all classes whose super class or interfaces failed verification.
642 h = 31 * h + c; 665 do {
643 } 666 // Not completely sure if we need to do this iteratively. Anyway,
644 return h; 667 // we should come here only if there are unverifiable classes, which
668 // shouldn't happen in normal cases. So better safe than sorry.
669 _check_classes_made_progress = false;
670 SystemDictionary::classes_do(check_one_shared_class);
671 } while (_check_classes_made_progress);
672
673 if (IgnoreUnverifiableClassesDuringDump) {
674 // This is useful when running JCK or SQE tests. You should not
675 // enable this when running real apps.
676 SystemDictionary::remove_classes_in_error_state();
677 } else {
678 tty->print_cr("Please remove the unverifiable classes from your class list and try again");
679 exit(1);
680 }
681 }
682 }
683
684 void MetaspaceShared::prepare_for_dumping() {
685 ClassLoader::initialize_shared_path();
686 FileMapInfo::allocate_classpath_entry_table();
645 } 687 }
646 688
647 // Preload classes from a list, populate the shared spaces and dump to a 689 // Preload classes from a list, populate the shared spaces and dump to a
648 // file. 690 // file.
649 void MetaspaceShared::preload_and_dump(TRAPS) { 691 void MetaspaceShared::preload_and_dump(TRAPS) {
650 TraceTime timer("Dump Shared Spaces", TraceStartupTime); 692 TraceTime timer("Dump Shared Spaces", TraceStartupTime);
651 ResourceMark rm; 693 ResourceMark rm;
652 694
695 tty->print_cr("Allocated shared space: %d bytes at " PTR_FORMAT,
696 MetaspaceShared::shared_rs()->size(),
697 MetaspaceShared::shared_rs()->base());
698
653 // Preload classes to be shared. 699 // Preload classes to be shared.
654 // Should use some os:: method rather than fopen() here. aB. 700 // Should use some os:: method rather than fopen() here. aB.
655 // Construct the path to the class list (in jre/lib) 701 const char* class_list_path;
656 // Walk up two directories from the location of the VM and 702 if (SharedClassListFile == NULL) {
657 // optionally tack on "lib" (depending on platform) 703 // Construct the path to the class list (in jre/lib)
658 char class_list_path[JVM_MAXPATHLEN]; 704 // Walk up two directories from the location of the VM and
659 os::jvm_path(class_list_path, sizeof(class_list_path)); 705 // optionally tack on "lib" (depending on platform)
660 for (int i = 0; i < 3; i++) { 706 char class_list_path_str[JVM_MAXPATHLEN];
661 char *end = strrchr(class_list_path, *os::file_separator()); 707 os::jvm_path(class_list_path_str, sizeof(class_list_path_str));
662 if (end != NULL) *end = '\0'; 708 for (int i = 0; i < 3; i++) {
663 } 709 char *end = strrchr(class_list_path_str, *os::file_separator());
664 int class_list_path_len = (int)strlen(class_list_path); 710 if (end != NULL) *end = '\0';
665 if (class_list_path_len >= 3) { 711 }
666 if (strcmp(class_list_path + class_list_path_len - 3, "lib") != 0) { 712 int class_list_path_len = (int)strlen(class_list_path_str);
667 strcat(class_list_path, os::file_separator()); 713 if (class_list_path_len >= 3) {
668 strcat(class_list_path, "lib"); 714 if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) {
669 } 715 strcat(class_list_path_str, os::file_separator());
670 } 716 strcat(class_list_path_str, "lib");
671 strcat(class_list_path, os::file_separator()); 717 }
672 strcat(class_list_path, "classlist"); 718 }
673 719 strcat(class_list_path_str, os::file_separator());
720 strcat(class_list_path_str, "classlist");
721 class_list_path = class_list_path_str;
722 } else {
723 class_list_path = SharedClassListFile;
724 }
725
726 int class_count = 0;
727 GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>();
728
729 // sun.io.Converters
730 static const char obj_array_sig[] = "[[Ljava/lang/Object;";
731 SymbolTable::new_permanent_symbol(obj_array_sig, THREAD);
732
733 // java.util.HashMap
734 static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
735 SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD);
736
737 tty->print_cr("Loading classes to share ...");
738 _has_error_classes = false;
739 class_count += preload_and_dump(class_list_path, class_promote_order,
740 THREAD);
741 if (ExtraSharedClassListFile) {
742 class_count += preload_and_dump(ExtraSharedClassListFile, class_promote_order,
743 THREAD);
744 }
745 tty->print_cr("Loading classes to share: done.");
746
747 if (PrintSharedSpaces) {
748 tty->print_cr("Shared spaces: preloaded %d classes", class_count);
749 }
750
751 // Rewrite and link classes
752 tty->print_cr("Rewriting and linking classes ...");
753
754 // Link any classes which got missed. This would happen if we have loaded classes that
755 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
756 // fails verification, all other interfaces that were not specified in the classlist but
757 // are implemented by K are not verified.
758 link_and_cleanup_shared_classes(CATCH);
759 tty->print_cr("Rewriting and linking classes: done");
760
761 // Create and dump the shared spaces. Everything so far is loaded
762 // with the null class loader.
763 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
764 VM_PopulateDumpSharedSpace op(loader_data, class_promote_order);
765 VMThread::execute(&op);
766
767 // Since various initialization steps have been undone by this process,
768 // it is not reasonable to continue running a java process.
769 exit(0);
770 }
771
772 int MetaspaceShared::preload_and_dump(const char * class_list_path,
773 GrowableArray<Klass*>* class_promote_order,
774 TRAPS) {
674 FILE* file = fopen(class_list_path, "r"); 775 FILE* file = fopen(class_list_path, "r");
776 char class_name[256];
777 int class_count = 0;
778
675 if (file != NULL) { 779 if (file != NULL) {
676 jlong computed_jsum = JSUM_SEED;
677 jlong file_jsum = 0;
678
679 char class_name[256];
680 int class_count = 0;
681 GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>();
682
683 // sun.io.Converters
684 static const char obj_array_sig[] = "[[Ljava/lang/Object;";
685 SymbolTable::new_permanent_symbol(obj_array_sig, THREAD);
686
687 // java.util.HashMap
688 static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
689 SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD);
690
691 tty->print("Loading classes to share ... ");
692 while ((fgets(class_name, sizeof class_name, file)) != NULL) { 780 while ((fgets(class_name, sizeof class_name, file)) != NULL) {
693 if (*class_name == '#') { 781 if (*class_name == '#') { // comment
694 jint fsh, fsl;
695 if (sscanf(class_name, "# %8x%8x\n", &fsh, &fsl) == 2) {
696 file_jsum = ((jlong)(fsh) << 32) | (fsl & 0xffffffff);
697 }
698
699 continue; 782 continue;
700 } 783 }
701 // Remove trailing newline 784 // Remove trailing newline
702 size_t name_len = strlen(class_name); 785 size_t name_len = strlen(class_name);
703 class_name[name_len-1] = '\0'; 786 if (class_name[name_len-1] == '\n') {
704 787 class_name[name_len-1] = '\0';
705 computed_jsum = jsum(computed_jsum, class_name, (const int)name_len - 1); 788 }
706 789
707 // Got a class name - load it. 790 // Got a class name - load it.
708 TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD); 791 TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD);
709 guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol."); 792 guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol.");
710 Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol, 793 Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol,
711 THREAD); 794 THREAD);
712 guarantee(!HAS_PENDING_EXCEPTION, "Exception resolving a class."); 795 CLEAR_PENDING_EXCEPTION;
713 if (klass != NULL) { 796 if (klass != NULL) {
714 if (PrintSharedSpaces && Verbose && WizardMode) { 797 if (PrintSharedSpaces && Verbose && WizardMode) {
715 tty->print_cr("Shared spaces preloaded: %s", class_name); 798 tty->print_cr("Shared spaces preloaded: %s", class_name);
716 } 799 }
717 800
718
719 InstanceKlass* ik = InstanceKlass::cast(klass); 801 InstanceKlass* ik = InstanceKlass::cast(klass);
720 802
721 // Should be class load order as per -XX:+TraceClassLoadingPreorder 803 // Should be class load order as per -XX:+TraceClassLoadingPreorder
722 class_promote_order->append(ik); 804 class_promote_order->append(ik);
723 805
724 // Link the class to cause the bytecodes to be rewritten and the 806 // Link the class to cause the bytecodes to be rewritten and the
725 // cpcache to be created. The linking is done as soon as classes 807 // cpcache to be created. The linking is done as soon as classes
726 // are loaded in order that the related data structures (klass and 808 // are loaded in order that the related data structures (klass and
727 // cpCache) are located together. 809 // cpCache) are located together.
728 810 try_link_class(ik, THREAD);
729 if (ik->init_state() < InstanceKlass::linked) { 811 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
730 ik->link_class(THREAD);
731 guarantee(!(HAS_PENDING_EXCEPTION), "exception in class rewriting");
732 }
733
734 // TODO: Resolve klasses in constant pool
735 ik->constants()->resolve_class_constants(THREAD);
736 812
737 class_count++; 813 class_count++;
738 } else { 814 } else {
739 if (PrintSharedSpaces && Verbose && WizardMode) { 815 //tty->print_cr("Preload failed: %s", class_name);
740 tty->cr();
741 tty->print_cr(" Preload failed: %s", class_name);
742 }
743 } 816 }
744 file_jsum = 0; // Checksum must be on last line of file 817 }
745 }
746 if (computed_jsum != file_jsum) {
747 tty->cr();
748 tty->print_cr("Preload failed: checksum of class list was incorrect.");
749 exit(1);
750 }
751
752 tty->print_cr("done. ");
753
754 if (PrintSharedSpaces) {
755 tty->print_cr("Shared spaces: preloaded %d classes", class_count);
756 }
757
758 // Rewrite and unlink classes.
759 tty->print("Rewriting and linking classes ... ");
760
761 // Link any classes which got missed. (It's not quite clear why
762 // they got missed.) This iteration would be unsafe if we weren't
763 // single-threaded at this point; however we can't do it on the VM
764 // thread because it requires object allocation.
765 SystemDictionary::classes_do(link_shared_classes, CATCH);
766 tty->print_cr("done. ");
767
768 // Create and dump the shared spaces. Everything so far is loaded
769 // with the null class loader.
770 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
771 VM_PopulateDumpSharedSpace op(loader_data, class_promote_order);
772 VMThread::execute(&op);
773
774 } else { 818 } else {
775 char errmsg[JVM_MAXPATHLEN]; 819 char errmsg[JVM_MAXPATHLEN];
776 os::lasterror(errmsg, JVM_MAXPATHLEN); 820 os::lasterror(errmsg, JVM_MAXPATHLEN);
777 tty->print_cr("Loading classlist failed: %s", errmsg); 821 tty->print_cr("Loading classlist failed: %s", errmsg);
778 exit(1); 822 exit(1);
779 } 823 }
780 824
781 // Since various initialization steps have been undone by this process, 825 return class_count;
782 // it is not reasonable to continue running a java process. 826 }
783 exit(0); 827
784 } 828 // Returns true if the class's status has changed
785 829 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
830 assert(DumpSharedSpaces, "should only be called during dumping");
831 if (ik->init_state() < InstanceKlass::linked) {
832 bool saved = BytecodeVerificationLocal;
833 if (!SharedClassUtil::is_shared_boot_class(ik)) {
834 // The verification decision is based on BytecodeVerificationRemote
835 // for non-system classes. Since we are using the NULL classloader
836 // to load non-system classes during dumping, we need to temporarily
837 // change BytecodeVerificationLocal to be the same as
838 // BytecodeVerificationRemote. Note this can cause the parent system
839 // classes also being verified. The extra overhead is acceptable during
840 // dumping.
841 BytecodeVerificationLocal = BytecodeVerificationRemote;
842 }
843 ik->link_class(THREAD);
844 if (HAS_PENDING_EXCEPTION) {
845 ResourceMark rm;
846 tty->print_cr("Preload Error: Verification failed for %s",
847 ik->external_name());
848 CLEAR_PENDING_EXCEPTION;
849 ik->set_in_error_state();
850 _has_error_classes = true;
851 }
852 BytecodeVerificationLocal = saved;
853 return true;
854 } else {
855 return false;
856 }
857 }
786 858
787 // Closure for serializing initialization data in from a data area 859 // Closure for serializing initialization data in from a data area
788 // (ptr_array) read from the shared file. 860 // (ptr_array) read from the shared file.
789 861
790 class ReadClosure : public SerializeClosure { 862 class ReadClosure : public SerializeClosure {
864 // Map each shared region 936 // Map each shared region
865 if ((_ro_base = mapinfo->map_region(ro)) != NULL && 937 if ((_ro_base = mapinfo->map_region(ro)) != NULL &&
866 (_rw_base = mapinfo->map_region(rw)) != NULL && 938 (_rw_base = mapinfo->map_region(rw)) != NULL &&
867 (_md_base = mapinfo->map_region(md)) != NULL && 939 (_md_base = mapinfo->map_region(md)) != NULL &&
868 (_mc_base = mapinfo->map_region(mc)) != NULL && 940 (_mc_base = mapinfo->map_region(mc)) != NULL &&
869 (image_alignment == (size_t)max_alignment())) { 941 (image_alignment == (size_t)max_alignment()) &&
942 mapinfo->validate_classpath_entry_table()) {
870 // Success (no need to do anything) 943 // Success (no need to do anything)
871 return true; 944 return true;
872 } else { 945 } else {
873 // If there was a failure in mapping any of the spaces, unmap the ones 946 // If there was a failure in mapping any of the spaces, unmap the ones
874 // that succeeded 947 // that succeeded
881 shared_rs.release(); 954 shared_rs.release();
882 #endif 955 #endif
883 // If -Xshare:on is specified, print out the error message and exit VM, 956 // If -Xshare:on is specified, print out the error message and exit VM,
884 // otherwise, set UseSharedSpaces to false and continue. 957 // otherwise, set UseSharedSpaces to false and continue.
885 if (RequireSharedSpaces) { 958 if (RequireSharedSpaces) {
886 vm_exit_during_initialization("Unable to use shared archive.", NULL); 959 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
887 } else { 960 } else {
888 FLAG_SET_DEFAULT(UseSharedSpaces, false); 961 FLAG_SET_DEFAULT(UseSharedSpaces, false);
889 } 962 }
890 return false; 963 return false;
891 } 964 }
981 ReadClosure rc(&array); 1054 ReadClosure rc(&array);
982 serialize(&rc); 1055 serialize(&rc);
983 1056
984 // Close the mapinfo file 1057 // Close the mapinfo file
985 mapinfo->close(); 1058 mapinfo->close();
1059
1060 if (PrintSharedArchiveAndExit) {
1061 if (PrintSharedDictionary) {
1062 tty->print_cr("\nShared classes:\n");
1063 SystemDictionary::print_shared(false);
1064 }
1065 if (_archive_loading_failed) {
1066 tty->print_cr("archive is invalid");
1067 vm_exit(1);
1068 } else {
1069 tty->print_cr("archive is valid");
1070 vm_exit(0);
1071 }
1072 }
986 } 1073 }
987 1074
988 // JVM/TI RedefineClasses() support: 1075 // JVM/TI RedefineClasses() support:
989 bool MetaspaceShared::remap_shared_readonly_as_readwrite() { 1076 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
990 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1077 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");

mercurial