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 { |