Skip to content

Commit

Permalink
8244778: Archive full module graph in CDS
Browse files Browse the repository at this point in the history
Reviewed-by: erikj, coleenp, lfoltan, redestad, alanb, mchung
  • Loading branch information
iklam committed Sep 13, 2020
1 parent 998ce78 commit 03a4df0
Show file tree
Hide file tree
Showing 59 changed files with 2,052 additions and 164 deletions.
1 change: 1 addition & 0 deletions make/hotspot/lib/JvmFeatures.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ ifneq ($(call check-jvm-feature, cds), true)
archiveBuilder.cpp \
archiveUtils.cpp \
classListParser.cpp \
classLoaderDataShared.cpp \
classLoaderExt.cpp \
dumpAllocStats.cpp \
dynamicArchive.cpp \
Expand Down
1 change: 1 addition & 0 deletions make/hotspot/symbols/symbols-unix
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,6 @@ JVM_AddModuleExports
JVM_AddModuleExportsToAll
JVM_AddModuleExportsToAllUnnamed
JVM_AddReadsModule
JVM_DefineArchivedModules
JVM_DefineModule
JVM_SetBootLoaderUnnamedModule
10 changes: 6 additions & 4 deletions src/hotspot/share/classfile/classLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1644,12 +1644,14 @@ void ClassLoader::create_javabase() {

{
MutexLocker ml(THREAD, Module_lock);
ModuleEntry* jb_module = null_cld_modules->locked_create_entry(Handle(),
if (ModuleEntryTable::javabase_moduleEntry() == NULL) { // may have been inited by CDS.
ModuleEntry* jb_module = null_cld_modules->locked_create_entry(Handle(),
false, vmSymbols::java_base(), NULL, NULL, null_cld);
if (jb_module == NULL) {
vm_exit_during_initialization("Unable to create ModuleEntry for " JAVA_BASE_NAME);
if (jb_module == NULL) {
vm_exit_during_initialization("Unable to create ModuleEntry for " JAVA_BASE_NAME);
}
ModuleEntryTable::set_javabase_moduleEntry(jb_module);
}
ModuleEntryTable::set_javabase_moduleEntry(jb_module);
}
}

Expand Down
202 changes: 202 additions & 0 deletions src/hotspot/share/classfile/classLoaderDataShared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#include "precompiled.hpp"
#include "classfile/classLoaderData.inline.hpp"
#include "classfile/classLoaderDataShared.hpp"
#include "classfile/moduleEntry.hpp"
#include "classfile/packageEntry.hpp"
#include "logging/log.hpp"
#include "memory/metaspaceShared.hpp"
#include "runtime/handles.inline.hpp"

#if INCLUDE_CDS_JAVA_HEAP

class ArchivedClassLoaderData {
Array<PackageEntry*>* _packages;
Array<ModuleEntry*>* _modules;

void assert_valid(ClassLoaderData* loader_data) {
// loader_data may be NULL if the boot layer has loaded no modules for the platform or
// system loaders (e.g., if you create a custom JDK image with only java.base).
if (loader_data != NULL) {
assert(!loader_data->has_class_mirror_holder(),
"loaders for non-strong hidden classes or unsafe anonymous classes not supported");
}
}
public:
ArchivedClassLoaderData() : _packages(NULL), _modules(NULL) {}

void iterate_symbols(ClassLoaderData* loader_data, MetaspaceClosure* closure);
void allocate(ClassLoaderData* loader_data);
void init_archived_entries(ClassLoaderData* loader_data);
void init_archived_oops(ClassLoaderData* loader_data);

void serialize(SerializeClosure* f) {
f->do_ptr((void**)&_packages);
f->do_ptr((void**)&_modules);
}

void restore(ClassLoaderData* loader_data, bool do_entries, bool do_oops);
};

static ArchivedClassLoaderData _archived_boot_loader_data;
static ArchivedClassLoaderData _archived_platform_loader_data;
static ArchivedClassLoaderData _archived_system_loader_data;
static ModuleEntry* _archived_javabase_moduleEntry = NULL;

void ArchivedClassLoaderData::iterate_symbols(ClassLoaderData* loader_data, MetaspaceClosure* closure) {
assert(DumpSharedSpaces, "must be");
assert_valid(loader_data);
if (loader_data != NULL) {
loader_data->packages()->iterate_symbols(closure);
loader_data->modules() ->iterate_symbols(closure);
}
}

void ArchivedClassLoaderData::allocate(ClassLoaderData* loader_data) {
assert(DumpSharedSpaces, "must be");
assert_valid(loader_data);
if (loader_data != NULL) {
// We can't create hashtables at dump time because the hashcode depends on the
// address of the Symbols, which may be relocated at runtime due to ASLR.
// So we store the packages/modules in Arrays. At runtime, we create
// the hashtables using these arrays.
_packages = loader_data->packages()->allocate_archived_entries();
_modules = loader_data->modules() ->allocate_archived_entries();
}
}

void ArchivedClassLoaderData::init_archived_entries(ClassLoaderData* loader_data) {
assert(DumpSharedSpaces, "must be");
assert_valid(loader_data);
if (loader_data != NULL) {
loader_data->packages()->init_archived_entries(_packages);
loader_data->modules() ->init_archived_entries(_modules);
}
}

void ArchivedClassLoaderData::init_archived_oops(ClassLoaderData* loader_data) {
assert(DumpSharedSpaces, "must be");
assert_valid(loader_data);
if (loader_data != NULL) {
loader_data->modules()->init_archived_oops(_modules);
}
}

void ArchivedClassLoaderData::restore(ClassLoaderData* loader_data, bool do_entries, bool do_oops) {
assert(UseSharedSpaces, "must be");
assert_valid(loader_data);
if (_modules != NULL) { // Could be NULL if we have archived no modules for platform/system loaders
ModuleEntryTable* modules = loader_data->modules();
PackageEntryTable* packages = loader_data->packages();

MutexLocker m1(Module_lock);
if (do_entries) {
modules->load_archived_entries(loader_data, _modules);
packages->load_archived_entries(_packages);
}
if (do_oops) {
modules->restore_archived_oops(loader_data, _modules);
}
}
}

// ------------------------------

static ClassLoaderData* null_class_loader_data() {
ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
assert(loader_data != NULL, "must be");
return loader_data;
}

static ClassLoaderData* java_platform_loader_data_or_null() {
return ClassLoaderData::class_loader_data_or_null(SystemDictionary::java_platform_loader());
}

static ClassLoaderData* java_system_loader_data_or_null() {
return ClassLoaderData::class_loader_data_or_null(SystemDictionary::java_system_loader());
}

void ClassLoaderDataShared::iterate_symbols(MetaspaceClosure* closure) {
assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
_archived_boot_loader_data.iterate_symbols (null_class_loader_data(), closure);
_archived_platform_loader_data.iterate_symbols(java_platform_loader_data_or_null(), closure);
_archived_system_loader_data.iterate_symbols (java_system_loader_data_or_null(), closure);
}

void ClassLoaderDataShared::allocate_archived_tables() {
assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
_archived_boot_loader_data.allocate (null_class_loader_data());
_archived_platform_loader_data.allocate(java_platform_loader_data_or_null());
_archived_system_loader_data.allocate (java_system_loader_data_or_null());
}

void ClassLoaderDataShared::init_archived_tables() {
assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
_archived_boot_loader_data.init_archived_entries (null_class_loader_data());
_archived_platform_loader_data.init_archived_entries(java_platform_loader_data_or_null());
_archived_system_loader_data.init_archived_entries (java_system_loader_data_or_null());
_archived_javabase_moduleEntry = ModuleEntry::get_archived_entry(ModuleEntryTable::javabase_moduleEntry());
}

void ClassLoaderDataShared::init_archived_oops() {
assert(DumpSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
_archived_boot_loader_data.init_archived_oops (null_class_loader_data());
_archived_platform_loader_data.init_archived_oops(java_platform_loader_data_or_null());
_archived_system_loader_data.init_archived_oops (java_system_loader_data_or_null());
}

void ClassLoaderDataShared::serialize(SerializeClosure* f) {
_archived_boot_loader_data.serialize(f);
_archived_platform_loader_data.serialize(f);
_archived_system_loader_data.serialize(f);
f->do_ptr((void**)&_archived_javabase_moduleEntry);

if (f->reading() && MetaspaceShared::use_full_module_graph()) {
// Must be done before ClassLoader::create_javabase()
_archived_boot_loader_data.restore(null_class_loader_data(), true, false);
ModuleEntryTable::set_javabase_moduleEntry(_archived_javabase_moduleEntry);
log_info(cds)("use_full_module_graph = true; java.base = " INTPTR_FORMAT,
p2i(_archived_javabase_moduleEntry));
}
}

oop ClassLoaderDataShared::restore_archived_oops_for_null_class_loader_data() {
assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
_archived_boot_loader_data.restore(null_class_loader_data(), false, true);
return _archived_javabase_moduleEntry->module();
}

void ClassLoaderDataShared::restore_java_platform_loader_from_archive(ClassLoaderData* loader_data) {
assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
_archived_platform_loader_data.restore(loader_data, true, true);
}

void ClassLoaderDataShared::restore_java_system_loader_from_archive(ClassLoaderData* loader_data) {
assert(UseSharedSpaces && MetaspaceShared::use_full_module_graph(), "must be");
_archived_system_loader_data.restore(loader_data, true, true);
}

#endif // INCLUDE_CDS_JAVA_HEAP
47 changes: 47 additions & 0 deletions src/hotspot/share/classfile/classLoaderDataShared.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#ifndef SHARE_CLASSFILE_CLASSLOADERDATASHARED_HPP
#define SHARE_CLASSFILE_CLASSLOADERDATASHARED_HPP

#include "memory/allStatic.hpp"
#include "oops/oopsHierarchy.hpp"

class ClassLoaderData;
class MetaspaceClosure;
class SerializeClosure;

class ClassLoaderDataShared : AllStatic {
public:
static void allocate_archived_tables();
static void iterate_symbols(MetaspaceClosure* closure);
static void init_archived_tables();
static void init_archived_oops();
static void serialize(SerializeClosure* f);
static oop restore_archived_oops_for_null_class_loader_data();
static void restore_java_platform_loader_from_archive(ClassLoaderData* loader_data);
static void restore_java_system_loader_from_archive(ClassLoaderData* loader_data);
};

#endif // SHARE_CLASSFILE_CLASSLOADERDATASHARED_HPP
10 changes: 7 additions & 3 deletions src/hotspot/share/classfile/javaClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3392,12 +3392,17 @@ void java_lang_Module::set_name(oop module, oop value) {
module->obj_field_put(_name_offset, value);
}

ModuleEntry* java_lang_Module::module_entry(oop module) {
ModuleEntry* java_lang_Module::module_entry_raw(oop module) {
assert(_module_entry_offset != 0, "Uninitialized module_entry_offset");
assert(module != NULL, "module can't be null");
assert(oopDesc::is_oop(module), "module must be oop");

ModuleEntry* module_entry = (ModuleEntry*)module->address_field(_module_entry_offset);
return module_entry;
}

ModuleEntry* java_lang_Module::module_entry(oop module) {
ModuleEntry* module_entry = module_entry_raw(module);
if (module_entry == NULL) {
// If the inject field containing the ModuleEntry* is null then return the
// class loader's unnamed module.
Expand Down Expand Up @@ -4821,9 +4826,8 @@ bool JavaClasses::is_supported_for_archiving(oop obj) {
Klass* klass = obj->klass();

if (klass == SystemDictionary::ClassLoader_klass() || // ClassLoader::loader_data is malloc'ed.
klass == SystemDictionary::Module_klass() || // Module::module_entry is malloc'ed
// The next 3 classes are used to implement java.lang.invoke, and are not used directly in
// regular Java code. The implementation of java.lang.invoke uses generated anonymoys classes
// regular Java code. The implementation of java.lang.invoke uses generated anonymous classes
// (e.g., as referenced by ResolvedMethodName::vmholder) that are not yet supported by CDS.
// So for now we cannot not support these classes for archiving.
//
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/classfile/javaClasses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ class java_lang_Module {
static void set_name(oop module, oop value);

static ModuleEntry* module_entry(oop module);
static ModuleEntry* module_entry_raw(oop module);
static void set_module_entry(oop module, ModuleEntry* module_entry);

friend class JavaClasses;
Expand Down

2 comments on commit 03a4df0

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 03a4df0 Sep 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Issues

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Issues

Please sign in to comment.