1674 }
1675
1676 // Update a Java object to point its Klass* to the new location after
1677 // shared archive has been compacted.
1678 void MetaspaceShared::relocate_klass_ptr(oop o) {
1679 assert(DumpSharedSpaces, "sanity");
1680 Klass* k = ArchiveCompactor::get_relocated_klass(o->klass());
1681 o->set_klass(k);
1682 }
1683
1684 Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) {
1685 assert(DumpSharedSpaces, "sanity");
1686 k = ArchiveCompactor::get_relocated_klass(k);
1687 if (is_final) {
1688 k = (Klass*)(address(k) + final_delta());
1689 }
1690 return k;
1691 }
1692
1693 class LinkSharedClassesClosure : public KlassClosure {
1694 Thread* THREAD;
1695 bool _made_progress;
1696 public:
1697 LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
1698
1699 void reset() { _made_progress = false; }
1700 bool made_progress() const { return _made_progress; }
1701
1702 void do_klass(Klass* k) {
1703 if (k->is_instance_klass()) {
1704 InstanceKlass* ik = InstanceKlass::cast(k);
1705 // Link the class to cause the bytecodes to be rewritten and the
1706 // cpcache to be created. Class verification is done according
1707 // to -Xverify setting.
1708 _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1709 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1710
1711 ik->constants()->resolve_class_constants(THREAD);
1712 }
1713 }
1714 };
1715
1716 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) {
1717 // We need to iterate because verification may cause additional classes
1718 // to be loaded.
1719 LinkSharedClassesClosure link_closure(THREAD);
1720 do {
1721 link_closure.reset();
1722 ClassLoaderDataGraph::unlocked_loaded_classes_do(&link_closure);
1723 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1724 } while (link_closure.made_progress());
1725 }
1726
1727 void MetaspaceShared::prepare_for_dumping() {
1728 Arguments::check_unsupported_dumping_properties();
1729 ClassLoader::initialize_shared_path();
1730 }
1731
1732 // Preload classes from a list, populate the shared spaces and dump to a
1733 // file.
1734 void MetaspaceShared::preload_and_dump(TRAPS) {
1735 { TraceTime timer("Dump Shared Spaces", TRACETIME_LOG(Info, startuptime));
1736 ResourceMark rm(THREAD);
1737 char class_list_path_str[JVM_MAXPATHLEN];
1738 // Preload classes to be shared.
1739 const char* class_list_path;
1775 }
1776 log_info(cds)("Loading classes to share: done.");
1777
1778 log_info(cds)("Shared spaces: preloaded %d classes", class_count);
1779
1780 if (SharedArchiveConfigFile) {
1781 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
1782 read_extra_data(SharedArchiveConfigFile, THREAD);
1783 }
1784 log_info(cds)("Reading extra data: done.");
1785
1786 HeapShared::init_subgraph_entry_fields(THREAD);
1787
1788 // Rewrite and link classes
1789 log_info(cds)("Rewriting and linking classes ...");
1790
1791 // Link any classes which got missed. This would happen if we have loaded classes that
1792 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1793 // fails verification, all other interfaces that were not specified in the classlist but
1794 // are implemented by K are not verified.
1795 link_and_cleanup_shared_classes(CATCH);
1796 log_info(cds)("Rewriting and linking classes: done");
1797
1798 if (HeapShared::is_heap_object_archiving_allowed()) {
1799 // Avoid fragmentation while archiving heap objects.
1800 Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(true);
1801 Universe::heap()->collect(GCCause::_archive_time_gc);
1802 Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(false);
1803 }
1804
1805 VM_PopulateDumpSharedSpace op;
1806 VMThread::execute(&op);
1807 }
1808 }
1809
1810
1811 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) {
1812 ClassListParser parser(class_list_path);
1813 int class_count = 0;
1814
1815 while (parser.parse_one_line()) {
1831 if (klass->is_instance_klass()) {
1832 InstanceKlass* ik = InstanceKlass::cast(klass);
1833
1834 // Link the class to cause the bytecodes to be rewritten and the
1835 // cpcache to be created. The linking is done as soon as classes
1836 // are loaded in order that the related data structures (klass and
1837 // cpCache) are located together.
1838 try_link_class(ik, THREAD);
1839 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1840 }
1841
1842 class_count++;
1843 }
1844 }
1845
1846 return class_count;
1847 }
1848
1849 // Returns true if the class's status has changed
1850 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
1851 assert(DumpSharedSpaces, "should only be called during dumping");
1852 if (ik->init_state() < InstanceKlass::linked &&
1853 !SystemDictionaryShared::has_class_failed_verification(ik)) {
1854 bool saved = BytecodeVerificationLocal;
1855 if (ik->loader_type() == 0 && ik->class_loader() == NULL) {
1856 // The verification decision is based on BytecodeVerificationRemote
1857 // for non-system classes. Since we are using the NULL classloader
1858 // to load non-system classes for customized class loaders during dumping,
1859 // we need to temporarily change BytecodeVerificationLocal to be the same as
1860 // BytecodeVerificationRemote. Note this can cause the parent system
1861 // classes also being verified. The extra overhead is acceptable during
1862 // dumping.
1863 BytecodeVerificationLocal = BytecodeVerificationRemote;
1864 }
1865 ik->link_class(THREAD);
1866 if (HAS_PENDING_EXCEPTION) {
1867 ResourceMark rm(THREAD);
1868 log_warning(cds)("Preload Warning: Verification failed for %s",
1869 ik->external_name());
1870 CLEAR_PENDING_EXCEPTION;
1871 SystemDictionaryShared::set_class_has_failed_verification(ik);
|
1674 }
1675
1676 // Update a Java object to point its Klass* to the new location after
1677 // shared archive has been compacted.
1678 void MetaspaceShared::relocate_klass_ptr(oop o) {
1679 assert(DumpSharedSpaces, "sanity");
1680 Klass* k = ArchiveCompactor::get_relocated_klass(o->klass());
1681 o->set_klass(k);
1682 }
1683
1684 Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) {
1685 assert(DumpSharedSpaces, "sanity");
1686 k = ArchiveCompactor::get_relocated_klass(k);
1687 if (is_final) {
1688 k = (Klass*)(address(k) + final_delta());
1689 }
1690 return k;
1691 }
1692
1693 class LinkSharedClassesClosure : public KlassClosure {
1694 bool _is_static;
1695 Thread* THREAD;
1696 bool _made_progress;
1697 public:
1698 LinkSharedClassesClosure(bool is_static, Thread* thread) : _is_static(is_static), THREAD(thread), _made_progress(false) {}
1699
1700 void reset() { _made_progress = false; }
1701 bool made_progress() const { return _made_progress; }
1702
1703 void do_klass(Klass* k) {
1704 if (k->is_instance_klass()) {
1705 InstanceKlass* ik = InstanceKlass::cast(k);
1706 // For dynamic CDS dump, only link classes loaded by the builtin class loaders.
1707 bool do_linking = _is_static ? true : ik->loader_type() != 0;
1708 if (do_linking) {
1709 // Link the class to cause the bytecodes to be rewritten and the
1710 // cpcache to be created. Class verification is done according
1711 // to -Xverify setting.
1712 _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1713 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1714
1715 ik->constants()->resolve_class_constants(THREAD);
1716 }
1717 }
1718 }
1719 };
1720
1721 void MetaspaceShared::link_and_cleanup_shared_classes(bool is_static, TRAPS) {
1722 // We need to iterate because verification may cause additional classes
1723 // to be loaded.
1724 LinkSharedClassesClosure link_closure(is_static, THREAD);
1725 do {
1726 link_closure.reset();
1727 ClassLoaderDataGraph::unlocked_loaded_classes_do(&link_closure);
1728 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1729 } while (link_closure.made_progress());
1730 }
1731
1732 void MetaspaceShared::prepare_for_dumping() {
1733 Arguments::check_unsupported_dumping_properties();
1734 ClassLoader::initialize_shared_path();
1735 }
1736
1737 // Preload classes from a list, populate the shared spaces and dump to a
1738 // file.
1739 void MetaspaceShared::preload_and_dump(TRAPS) {
1740 { TraceTime timer("Dump Shared Spaces", TRACETIME_LOG(Info, startuptime));
1741 ResourceMark rm(THREAD);
1742 char class_list_path_str[JVM_MAXPATHLEN];
1743 // Preload classes to be shared.
1744 const char* class_list_path;
1780 }
1781 log_info(cds)("Loading classes to share: done.");
1782
1783 log_info(cds)("Shared spaces: preloaded %d classes", class_count);
1784
1785 if (SharedArchiveConfigFile) {
1786 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
1787 read_extra_data(SharedArchiveConfigFile, THREAD);
1788 }
1789 log_info(cds)("Reading extra data: done.");
1790
1791 HeapShared::init_subgraph_entry_fields(THREAD);
1792
1793 // Rewrite and link classes
1794 log_info(cds)("Rewriting and linking classes ...");
1795
1796 // Link any classes which got missed. This would happen if we have loaded classes that
1797 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1798 // fails verification, all other interfaces that were not specified in the classlist but
1799 // are implemented by K are not verified.
1800 link_and_cleanup_shared_classes(true, CATCH);
1801 log_info(cds)("Rewriting and linking classes: done");
1802
1803 if (HeapShared::is_heap_object_archiving_allowed()) {
1804 // Avoid fragmentation while archiving heap objects.
1805 Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(true);
1806 Universe::heap()->collect(GCCause::_archive_time_gc);
1807 Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(false);
1808 }
1809
1810 VM_PopulateDumpSharedSpace op;
1811 VMThread::execute(&op);
1812 }
1813 }
1814
1815
1816 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) {
1817 ClassListParser parser(class_list_path);
1818 int class_count = 0;
1819
1820 while (parser.parse_one_line()) {
1836 if (klass->is_instance_klass()) {
1837 InstanceKlass* ik = InstanceKlass::cast(klass);
1838
1839 // Link the class to cause the bytecodes to be rewritten and the
1840 // cpcache to be created. The linking is done as soon as classes
1841 // are loaded in order that the related data structures (klass and
1842 // cpCache) are located together.
1843 try_link_class(ik, THREAD);
1844 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1845 }
1846
1847 class_count++;
1848 }
1849 }
1850
1851 return class_count;
1852 }
1853
1854 // Returns true if the class's status has changed
1855 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
1856 Arguments::assert_is_dumping_archive();
1857 if (ik->init_state() < InstanceKlass::linked &&
1858 !SystemDictionaryShared::has_class_failed_verification(ik)) {
1859 bool saved = BytecodeVerificationLocal;
1860 if (ik->loader_type() == 0 && ik->class_loader() == NULL) {
1861 // The verification decision is based on BytecodeVerificationRemote
1862 // for non-system classes. Since we are using the NULL classloader
1863 // to load non-system classes for customized class loaders during dumping,
1864 // we need to temporarily change BytecodeVerificationLocal to be the same as
1865 // BytecodeVerificationRemote. Note this can cause the parent system
1866 // classes also being verified. The extra overhead is acceptable during
1867 // dumping.
1868 BytecodeVerificationLocal = BytecodeVerificationRemote;
1869 }
1870 ik->link_class(THREAD);
1871 if (HAS_PENDING_EXCEPTION) {
1872 ResourceMark rm(THREAD);
1873 log_warning(cds)("Preload Warning: Verification failed for %s",
1874 ik->external_name());
1875 CLEAR_PENDING_EXCEPTION;
1876 SystemDictionaryShared::set_class_has_failed_verification(ik);
|