Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
8236604: Optimize SystemDictionary::resolve_well_known_classes for CDS
Browse files Browse the repository at this point in the history
Serialize SystemDictionary::_well_known_classes into CDS and quickly resolve them at runtime in vm startup stage.

Reviewed-by: iklam, coleenp
  • Loading branch information
yminqi committed Feb 28, 2020
1 parent b247e6d commit add146c
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 23 deletions.
65 changes: 54 additions & 11 deletions src/hotspot/share/classfile/systemDictionary.cpp
Expand Up @@ -1336,11 +1336,16 @@ InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
ik->restore_unshareable_info(loader_data, protection_domain, CHECK_NULL);
}

load_shared_class_misc(ik, loader_data, CHECK_NULL);
return ik;
}

void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data, TRAPS) {
ik->print_class_load_logging(loader_data, NULL, NULL);

// For boot loader, ensure that GetSystemPackage knows that a class in this
// package was loaded.
if (class_loader.is_null()) {
if (loader_data->is_the_null_class_loader_data()) {
int path_index = ik->shared_classpath_index();
ResourceMark rm(THREAD);
ClassLoader::add_package(ik->name()->as_C_string(), path_index, THREAD);
Expand Down Expand Up @@ -1372,8 +1377,6 @@ InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
}
}
}

return ik;
}
#endif // INCLUDE_CDS

Expand Down Expand Up @@ -1785,7 +1788,9 @@ InstanceKlass* SystemDictionary::find_class(Symbol* class_name, ClassLoaderData*

void SystemDictionary::add_to_hierarchy(InstanceKlass* k, TRAPS) {
assert(k != NULL, "just checking");
assert_locked_or_safepoint(Compile_lock);
if (Universe::is_fully_initialized()) {
assert_locked_or_safepoint(Compile_lock);
}

k->set_init_state(InstanceKlass::loaded);
// make sure init_state store is already done.
Expand All @@ -1798,7 +1803,9 @@ void SystemDictionary::add_to_hierarchy(InstanceKlass* k, TRAPS) {

// Now flush all code that depended on old class hierarchy.
// Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
CodeCache::flush_dependents_on(k);
if (Universe::is_fully_initialized()) {
CodeCache::flush_dependents_on(k);
}
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -1911,13 +1918,53 @@ bool SystemDictionary::is_well_known_klass(Symbol* class_name) {
}
#endif

void SystemDictionary::quick_resolve(InstanceKlass* klass, ClassLoaderData* loader_data, Handle domain, TRAPS) {
assert(!Universe::is_fully_initialized(), "We can make short cuts only during VM initialization");
assert(klass->is_shared(), "Must be shared class");
if (klass->class_loader_data() != NULL) {
return;
}

// add super and interfaces first
Klass* super = klass->super();
if (super != NULL && super->class_loader_data() == NULL) {
assert(super->is_instance_klass(), "Super should be instance klass");
quick_resolve(InstanceKlass::cast(super), loader_data, domain, CHECK);
}

Array<InstanceKlass*>* ifs = klass->local_interfaces();
for (int i = 0; i < ifs->length(); i++) {
InstanceKlass* ik = ifs->at(i);
if (ik->class_loader_data() == NULL) {
quick_resolve(ik, loader_data, domain, CHECK);
}
}

klass->restore_unshareable_info(loader_data, domain, THREAD);
load_shared_class_misc(klass, loader_data, CHECK);
Dictionary* dictionary = loader_data->dictionary();
unsigned int hash = dictionary->compute_hash(klass->name());
dictionary->add_klass(hash, klass->name(), klass);
add_to_hierarchy(klass, CHECK);
assert(klass->is_loaded(), "Must be in at least loaded state");
}

bool SystemDictionary::resolve_wk_klass(WKID id, TRAPS) {
assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
int sid = wk_init_info[id - FIRST_WKID];
Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);
InstanceKlass** klassp = &_well_known_klasses[id];

if ((*klassp) == NULL) {
if (UseSharedSpaces && !JvmtiExport::should_post_class_prepare()) {
InstanceKlass* k = *klassp;
assert(k->is_shared_boot_class(), "must be");

ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
quick_resolve(k, loader_data, Handle(), CHECK_false);
return true;
}

if (!is_wk_klass_loaded(*klassp)) {
Klass* k = resolve_or_fail(symbol, true, CHECK_false);
(*klassp) = InstanceKlass::cast(k);
}
Expand All @@ -1936,7 +1983,7 @@ void SystemDictionary::resolve_wk_klasses_until(WKID limit_id, WKID &start_id, T
}

void SystemDictionary::resolve_well_known_classes(TRAPS) {
assert(WK_KLASS(Object_klass) == NULL, "well-known classes should only be initialized once");
assert(!Object_klass_loaded(), "well-known classes should only be initialized once");

// Create the ModuleEntry for java.base. This call needs to be done here,
// after vmSymbols::initialize() is called but before any classes are pre-loaded.
Expand Down Expand Up @@ -1984,10 +2031,6 @@ void SystemDictionary::resolve_well_known_classes(TRAPS) {
java_lang_Class::compute_offsets();

// Fixup mirrors for classes loaded before java.lang.Class.
// These calls iterate over the objects currently in the perm gen
// so calling them at this point is matters (not before when there
// are fewer objects and not later after there are more objects
// in the perm gen.
Universe::initialize_basic_type_mirrors(CHECK);
Universe::fixup_mirrors(CHECK);

Expand Down
25 changes: 16 additions & 9 deletions src/hotspot/share/classfile/systemDictionary.hpp
Expand Up @@ -387,13 +387,15 @@ class SystemDictionary : AllStatic {
resolve_wk_klasses_until((WKID) limit, start_id, THREAD);
}
public:
#define WK_KLASS(name) _well_known_klasses[SystemDictionary::WK_KLASS_ENUM_NAME(name)]

#define WK_KLASS_DECLARE(name, symbol) \
static InstanceKlass* name() { return check_klass(_well_known_klasses[WK_KLASS_ENUM_NAME(name)]); } \
static InstanceKlass** name##_addr() { \
return &_well_known_klasses[SystemDictionary::WK_KLASS_ENUM_NAME(name)]; \
} \
static bool name##_is_loaded() { \
return _well_known_klasses[SystemDictionary::WK_KLASS_ENUM_NAME(name)] != NULL; \
return is_wk_klass_loaded(WK_KLASS(name)); \
}
WK_KLASSES_DO(WK_KLASS_DECLARE);
#undef WK_KLASS_DECLARE
Expand All @@ -409,9 +411,6 @@ class SystemDictionary : AllStatic {
}
static void well_known_klasses_do(MetaspaceClosure* it);

// Local definition for direct access to the private array:
#define WK_KLASS(name) _well_known_klasses[SystemDictionary::WK_KLASS_ENUM_NAME(name)]

static InstanceKlass* box_klass(BasicType t) {
assert((uint)t < T_VOID+1, "range check");
return check_klass(_box_klasses[t]);
Expand All @@ -431,12 +430,16 @@ class SystemDictionary : AllStatic {
return ClassLoaderData::class_loader_data(class_loader());
}

static bool is_wk_klass_loaded(InstanceKlass* klass) {
return !(klass == NULL || !klass->is_loaded());
}

public:
static bool Parameter_klass_loaded() { return WK_KLASS(reflect_Parameter_klass) != NULL; }
static bool Class_klass_loaded() { return WK_KLASS(Class_klass) != NULL; }
static bool Cloneable_klass_loaded() { return WK_KLASS(Cloneable_klass) != NULL; }
static bool Object_klass_loaded() { return WK_KLASS(Object_klass) != NULL; }
static bool ClassLoader_klass_loaded() { return WK_KLASS(ClassLoader_klass) != NULL; }
static bool Object_klass_loaded() { return is_wk_klass_loaded(WK_KLASS(Object_klass)); }
static bool Class_klass_loaded() { return is_wk_klass_loaded(WK_KLASS(Class_klass)); }
static bool Cloneable_klass_loaded() { return is_wk_klass_loaded(WK_KLASS(Cloneable_klass)); }
static bool Parameter_klass_loaded() { return is_wk_klass_loaded(WK_KLASS(reflect_Parameter_klass)); }
static bool ClassLoader_klass_loaded() { return is_wk_klass_loaded(WK_KLASS(ClassLoader_klass)); }

// Returns java system loader
static oop java_system_loader();
Expand Down Expand Up @@ -596,6 +599,8 @@ class SystemDictionary : AllStatic {
Handle protection_domain,
const ClassFileStream *cfs,
TRAPS);
// Second part of load_shared_class
static void load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data, TRAPS);
static InstanceKlass* load_shared_boot_class(Symbol* class_name,
TRAPS);
static InstanceKlass* load_instance_class(Symbol* class_name, Handle class_loader, TRAPS);
Expand Down Expand Up @@ -628,6 +633,8 @@ class SystemDictionary : AllStatic {

// Resolve well-known classes so they can be used like SystemDictionary::String_klass()
static void resolve_well_known_classes(TRAPS);
// quick resolve using CDS for well-known classes only.
static void quick_resolve(InstanceKlass* klass, ClassLoaderData* loader_data, Handle domain, TRAPS);

// Class loader constraints
static void check_constraints(unsigned int hash,
Expand Down
6 changes: 6 additions & 0 deletions src/hotspot/share/classfile/systemDictionaryShared.cpp
Expand Up @@ -1410,6 +1410,12 @@ void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc,
}
}

void SystemDictionaryShared::serialize_well_known_klasses(SerializeClosure* soc) {
for (int i = FIRST_WKID; i < WKID_LIMIT; i++) {
soc->do_ptr((void**)&_well_known_klasses[i]);
}
}

const RunTimeSharedClassInfo*
SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
if (!UseSharedSpaces || !name->is_shared()) {
Expand Down
3 changes: 2 additions & 1 deletion src/hotspot/share/classfile/systemDictionaryShared.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 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
Expand Down Expand Up @@ -303,6 +303,7 @@ class SystemDictionaryShared: public SystemDictionary {
static void write_to_archive(bool is_static_archive = true);
static void serialize_dictionary_headers(class SerializeClosure* soc,
bool is_static_archive = true);
static void serialize_well_known_klasses(class SerializeClosure* soc);
static void print() { return print_on(tty); }
static void print_on(outputStream* st) NOT_CDS_RETURN;
static void print_table_statistics(outputStream* st) NOT_CDS_RETURN;
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/memory/metaspaceShared.cpp
Expand Up @@ -463,6 +463,9 @@ void MetaspaceShared::serialize(SerializeClosure* soc) {
SystemDictionaryShared::serialize_dictionary_headers(soc);

InstanceMirrorKlass::serialize_offsets(soc);

// Dump/restore well known classes (pointers)
SystemDictionaryShared::serialize_well_known_klasses(soc);
soc->do_tag(--tag);

serialize_cloned_cpp_vtptrs(soc);
Expand Down
4 changes: 3 additions & 1 deletion src/hotspot/share/oops/instanceKlass.cpp
Expand Up @@ -1153,7 +1153,9 @@ int InstanceKlass::nof_implementors() const {
//
// The _implementor field only exists for interfaces.
void InstanceKlass::add_implementor(Klass* k) {
assert_lock_strong(Compile_lock);
if (Universe::is_fully_initialized()) {
assert_lock_strong(Compile_lock);
}
assert(is_interface(), "not interface");
// Filter out my subinterfaces.
// (Note: Interfaces are never on the subklass list.)
Expand Down
4 changes: 3 additions & 1 deletion src/hotspot/share/oops/klass.cpp
Expand Up @@ -413,7 +413,9 @@ void Klass::set_next_sibling(Klass* s) {
}

void Klass::append_to_sibling_list() {
assert_locked_or_safepoint(Compile_lock);
if (Universe::is_fully_initialized()) {
assert_locked_or_safepoint(Compile_lock);
}
debug_only(verify();)
// add ourselves to superklass' subklass list
InstanceKlass* super = superklass();
Expand Down

0 comments on commit add146c

Please sign in to comment.