< prev index next >

src/hotspot/share/memory/metaspaceShared.cpp

Print this page




1671 }
1672 
1673 // Update a Java object to point its Klass* to the new location after
1674 // shared archive has been compacted.
1675 void MetaspaceShared::relocate_klass_ptr(oop o) {
1676   assert(DumpSharedSpaces, "sanity");
1677   Klass* k = ArchiveCompactor::get_relocated_klass(o->klass());
1678   o->set_klass(k);
1679 }
1680 
1681 Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) {
1682   assert(DumpSharedSpaces, "sanity");
1683   k = ArchiveCompactor::get_relocated_klass(k);
1684   if (is_final) {
1685     k = (Klass*)(address(k) + final_delta());
1686   }
1687   return k;
1688 }
1689 
1690 class LinkSharedClassesClosure : public KlassClosure {

1691   Thread* THREAD;
1692   bool    _made_progress;
1693  public:
1694   LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
1695 
1696   void reset()               { _made_progress = false; }
1697   bool made_progress() const { return _made_progress; }
1698 
1699   void do_klass(Klass* k) {
1700     if (k->is_instance_klass()) {
1701       InstanceKlass* ik = InstanceKlass::cast(k);



1702       // Link the class to cause the bytecodes to be rewritten and the
1703       // cpcache to be created. Class verification is done according
1704       // to -Xverify setting.
1705       _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1706       guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1707 
1708       ik->constants()->resolve_class_constants(THREAD);
1709     }
1710   }

1711 };
1712 
1713 class CheckSharedClassesClosure : public KlassClosure {
1714   bool    _made_progress;
1715  public:
1716   CheckSharedClassesClosure() : _made_progress(false) {}
1717 
1718   void reset()               { _made_progress = false; }
1719   bool made_progress() const { return _made_progress; }
1720   void do_klass(Klass* k) {
1721     if (k->is_instance_klass() && InstanceKlass::cast(k)->check_sharing_error_state()) {
1722       _made_progress = true;
1723     }
1724   }
1725 };
1726 
1727 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) {
1728   // We need to iterate because verification may cause additional classes
1729   // to be loaded.
1730   LinkSharedClassesClosure link_closure(THREAD);
1731   do {
1732     link_closure.reset();
1733     ClassLoaderDataGraph::unlocked_loaded_classes_do(&link_closure);
1734     guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1735   } while (link_closure.made_progress());
1736 
1737   if (_has_error_classes) {
1738     // Mark all classes whose super class or interfaces failed verification.
1739     CheckSharedClassesClosure check_closure;
1740     do {
1741       // Not completely sure if we need to do this iteratively. Anyway,
1742       // we should come here only if there are unverifiable classes, which
1743       // shouldn't happen in normal cases. So better safe than sorry.
1744       check_closure.reset();
1745       ClassLoaderDataGraph::unlocked_loaded_classes_do(&check_closure);
1746     } while (check_closure.made_progress());
1747   }
1748 }
1749 
1750 void MetaspaceShared::prepare_for_dumping() {


1798     }
1799     log_info(cds)("Loading classes to share: done.");
1800 
1801     log_info(cds)("Shared spaces: preloaded %d classes", class_count);
1802 
1803     if (SharedArchiveConfigFile) {
1804       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
1805       read_extra_data(SharedArchiveConfigFile, THREAD);
1806     }
1807     log_info(cds)("Reading extra data: done.");
1808 
1809     HeapShared::init_subgraph_entry_fields(THREAD);
1810 
1811     // Rewrite and link classes
1812     log_info(cds)("Rewriting and linking classes ...");
1813 
1814     // Link any classes which got missed. This would happen if we have loaded classes that
1815     // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1816     // fails verification, all other interfaces that were not specified in the classlist but
1817     // are implemented by K are not verified.
1818     link_and_cleanup_shared_classes(CATCH);
1819     log_info(cds)("Rewriting and linking classes: done");
1820 
1821     if (HeapShared::is_heap_object_archiving_allowed()) {
1822       // Avoid fragmentation while archiving heap objects.
1823       Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(true);
1824       Universe::heap()->collect(GCCause::_archive_time_gc);
1825       Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(false);
1826     }
1827 
1828     VM_PopulateDumpSharedSpace op;
1829     VMThread::execute(&op);
1830   }
1831 }
1832 
1833 
1834 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) {
1835   ClassListParser parser(class_list_path);
1836   int class_count = 0;
1837 
1838   while (parser.parse_one_line()) {


1854       if (klass->is_instance_klass()) {
1855         InstanceKlass* ik = InstanceKlass::cast(klass);
1856 
1857         // Link the class to cause the bytecodes to be rewritten and the
1858         // cpcache to be created. The linking is done as soon as classes
1859         // are loaded in order that the related data structures (klass and
1860         // cpCache) are located together.
1861         try_link_class(ik, THREAD);
1862         guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1863       }
1864 
1865       class_count++;
1866     }
1867   }
1868 
1869   return class_count;
1870 }
1871 
1872 // Returns true if the class's status has changed
1873 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
1874   assert(DumpSharedSpaces, "should only be called during dumping");
1875   if (ik->init_state() < InstanceKlass::linked) {
1876     bool saved = BytecodeVerificationLocal;
1877     if (ik->loader_type() == 0 && ik->class_loader() == NULL) {
1878       // The verification decision is based on BytecodeVerificationRemote
1879       // for non-system classes. Since we are using the NULL classloader
1880       // to load non-system classes for customized class loaders during dumping,
1881       // we need to temporarily change BytecodeVerificationLocal to be the same as
1882       // BytecodeVerificationRemote. Note this can cause the parent system
1883       // classes also being verified. The extra overhead is acceptable during
1884       // dumping.
1885       BytecodeVerificationLocal = BytecodeVerificationRemote;
1886     }
1887     ik->link_class(THREAD);
1888     if (HAS_PENDING_EXCEPTION) {
1889       ResourceMark rm(THREAD);
1890       log_warning(cds)("Preload Warning: Verification failed for %s",
1891                     ik->external_name());
1892       CLEAR_PENDING_EXCEPTION;
1893       ik->set_in_error_state();
1894       _has_error_classes = true;




1671 }
1672 
1673 // Update a Java object to point its Klass* to the new location after
1674 // shared archive has been compacted.
1675 void MetaspaceShared::relocate_klass_ptr(oop o) {
1676   assert(DumpSharedSpaces, "sanity");
1677   Klass* k = ArchiveCompactor::get_relocated_klass(o->klass());
1678   o->set_klass(k);
1679 }
1680 
1681 Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) {
1682   assert(DumpSharedSpaces, "sanity");
1683   k = ArchiveCompactor::get_relocated_klass(k);
1684   if (is_final) {
1685     k = (Klass*)(address(k) + final_delta());
1686   }
1687   return k;
1688 }
1689 
1690 class LinkSharedClassesClosure : public KlassClosure {
1691   bool    _is_static;
1692   Thread* THREAD;
1693   bool    _made_progress;
1694  public:
1695   LinkSharedClassesClosure(bool is_static, Thread* thread) : _is_static(is_static), THREAD(thread), _made_progress(false) {}
1696 
1697   void reset()               { _made_progress = false; }
1698   bool made_progress() const { return _made_progress; }
1699 
1700   void do_klass(Klass* k) {
1701     if (k->is_instance_klass()) {
1702       InstanceKlass* ik = InstanceKlass::cast(k);
1703       // For dynamic CDS dump, only link classes loaded by the builtin class loaders.
1704       bool do_linking = _is_static ? true : ik->loader_type() != 0;
1705       if (do_linking) {
1706         // Link the class to cause the bytecodes to be rewritten and the
1707         // cpcache to be created. Class verification is done according
1708         // to -Xverify setting.
1709         _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1710         guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1711 
1712         ik->constants()->resolve_class_constants(THREAD);
1713       }
1714     }
1715   }
1716 };
1717 
1718 class CheckSharedClassesClosure : public KlassClosure {
1719   bool    _made_progress;
1720  public:
1721   CheckSharedClassesClosure() : _made_progress(false) {}
1722 
1723   void reset()               { _made_progress = false; }
1724   bool made_progress() const { return _made_progress; }
1725   void do_klass(Klass* k) {
1726     if (k->is_instance_klass() && InstanceKlass::cast(k)->check_sharing_error_state()) {
1727       _made_progress = true;
1728     }
1729   }
1730 };
1731 
1732 void MetaspaceShared::link_and_cleanup_shared_classes(bool is_static, TRAPS) {
1733   // We need to iterate because verification may cause additional classes
1734   // to be loaded.
1735   LinkSharedClassesClosure link_closure(is_static, THREAD);
1736   do {
1737     link_closure.reset();
1738     ClassLoaderDataGraph::unlocked_loaded_classes_do(&link_closure);
1739     guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1740   } while (link_closure.made_progress());
1741 
1742   if (_has_error_classes) {
1743     // Mark all classes whose super class or interfaces failed verification.
1744     CheckSharedClassesClosure check_closure;
1745     do {
1746       // Not completely sure if we need to do this iteratively. Anyway,
1747       // we should come here only if there are unverifiable classes, which
1748       // shouldn't happen in normal cases. So better safe than sorry.
1749       check_closure.reset();
1750       ClassLoaderDataGraph::unlocked_loaded_classes_do(&check_closure);
1751     } while (check_closure.made_progress());
1752   }
1753 }
1754 
1755 void MetaspaceShared::prepare_for_dumping() {


1803     }
1804     log_info(cds)("Loading classes to share: done.");
1805 
1806     log_info(cds)("Shared spaces: preloaded %d classes", class_count);
1807 
1808     if (SharedArchiveConfigFile) {
1809       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
1810       read_extra_data(SharedArchiveConfigFile, THREAD);
1811     }
1812     log_info(cds)("Reading extra data: done.");
1813 
1814     HeapShared::init_subgraph_entry_fields(THREAD);
1815 
1816     // Rewrite and link classes
1817     log_info(cds)("Rewriting and linking classes ...");
1818 
1819     // Link any classes which got missed. This would happen if we have loaded classes that
1820     // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1821     // fails verification, all other interfaces that were not specified in the classlist but
1822     // are implemented by K are not verified.
1823     link_and_cleanup_shared_classes(true, CATCH);
1824     log_info(cds)("Rewriting and linking classes: done");
1825 
1826     if (HeapShared::is_heap_object_archiving_allowed()) {
1827       // Avoid fragmentation while archiving heap objects.
1828       Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(true);
1829       Universe::heap()->collect(GCCause::_archive_time_gc);
1830       Universe::heap()->soft_ref_policy()->set_should_clear_all_soft_refs(false);
1831     }
1832 
1833     VM_PopulateDumpSharedSpace op;
1834     VMThread::execute(&op);
1835   }
1836 }
1837 
1838 
1839 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) {
1840   ClassListParser parser(class_list_path);
1841   int class_count = 0;
1842 
1843   while (parser.parse_one_line()) {


1859       if (klass->is_instance_klass()) {
1860         InstanceKlass* ik = InstanceKlass::cast(klass);
1861 
1862         // Link the class to cause the bytecodes to be rewritten and the
1863         // cpcache to be created. The linking is done as soon as classes
1864         // are loaded in order that the related data structures (klass and
1865         // cpCache) are located together.
1866         try_link_class(ik, THREAD);
1867         guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1868       }
1869 
1870       class_count++;
1871     }
1872   }
1873 
1874   return class_count;
1875 }
1876 
1877 // Returns true if the class's status has changed
1878 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
1879   Arguments::assert_is_dumping_archive();
1880   if (ik->init_state() < InstanceKlass::linked) {
1881     bool saved = BytecodeVerificationLocal;
1882     if (ik->loader_type() == 0 && ik->class_loader() == NULL) {
1883       // The verification decision is based on BytecodeVerificationRemote
1884       // for non-system classes. Since we are using the NULL classloader
1885       // to load non-system classes for customized class loaders during dumping,
1886       // we need to temporarily change BytecodeVerificationLocal to be the same as
1887       // BytecodeVerificationRemote. Note this can cause the parent system
1888       // classes also being verified. The extra overhead is acceptable during
1889       // dumping.
1890       BytecodeVerificationLocal = BytecodeVerificationRemote;
1891     }
1892     ik->link_class(THREAD);
1893     if (HAS_PENDING_EXCEPTION) {
1894       ResourceMark rm(THREAD);
1895       log_warning(cds)("Preload Warning: Verification failed for %s",
1896                     ik->external_name());
1897       CLEAR_PENDING_EXCEPTION;
1898       ik->set_in_error_state();
1899       _has_error_classes = true;


< prev index next >