Skip to content

Commit

Permalink
8309240: Array classes should be stored in dynamic CDS archive
Browse files Browse the repository at this point in the history
Reviewed-by: iklam
  • Loading branch information
calvinccheung committed Aug 1, 2023
1 parent bf70777 commit dc14247
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 16 deletions.
19 changes: 14 additions & 5 deletions src/hotspot/share/cds/archiveBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "cds/archiveUtils.hpp"
#include "cds/cppVtables.hpp"
#include "cds/dumpAllocStats.hpp"
#include "cds/dynamicArchive.hpp"
#include "cds/heapShared.hpp"
#include "cds/metaspaceShared.hpp"
#include "cds/regeneratedClasses.hpp"
Expand Down Expand Up @@ -520,12 +521,12 @@ bool ArchiveBuilder::is_excluded(Klass* klass) {
InstanceKlass* ik = InstanceKlass::cast(klass);
return SystemDictionaryShared::is_excluded_class(ik);
} else if (klass->is_objArray_klass()) {
if (DynamicDumpSharedSpaces) {
// Don't support archiving of array klasses for now (WHY???)
return true;
}
Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass();
if (bottom->is_instance_klass()) {
if (MetaspaceShared::is_shared_static(bottom)) {
// The bottom class is in the static archive so it's clearly not excluded.
assert(DynamicDumpSharedSpaces, "sanity");
return false;
} else if (bottom->is_instance_klass()) {
return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom));
}
}
Expand Down Expand Up @@ -792,6 +793,14 @@ void ArchiveBuilder::make_klasses_shareable() {
log_info(cds)(" obj array classes = %5d", num_obj_array_klasses);
log_info(cds)(" type array classes = %5d", num_type_array_klasses);
log_info(cds)(" symbols = %5d", _symbols->length());

DynamicArchive::make_array_klasses_shareable();
}

void ArchiveBuilder::serialize_dynamic_archivable_items(SerializeClosure* soc) {
SymbolTable::serialize_shared_table_header(soc, false);
SystemDictionaryShared::serialize_dictionary_headers(soc, false);
DynamicArchive::serialize_array_klasses(soc);
}

uintx ArchiveBuilder::buffer_to_offset(address p) const {
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/cds/archiveBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ class ArchiveBuilder : public StackObj {
bool gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only);
bool gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only);
void remember_embedded_pointer_in_enclosing_obj(MetaspaceClosure::Ref* ref);
static void serialize_dynamic_archivable_items(SerializeClosure* soc);

DumpRegion* rw_region() { return &_rw_region; }
DumpRegion* ro_region() { return &_ro_region; }
Expand Down
123 changes: 121 additions & 2 deletions src/hotspot/share/cds/dynamicArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class DynamicArchiveBuilder : public ArchiveBuilder {
void sort_methods(InstanceKlass* ik) const;
void remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const;
void write_archive(char* serialized_data);
void gather_array_klasses();

public:
DynamicArchiveBuilder() : ArchiveBuilder() { }
Expand Down Expand Up @@ -118,6 +119,7 @@ class DynamicArchiveBuilder : public ArchiveBuilder {

init_header();
gather_source_objs();
gather_array_klasses();
reserve_buffer();

log_info(cds, dynamic)("Copying %d klasses and %d symbols",
Expand All @@ -139,11 +141,11 @@ class DynamicArchiveBuilder : public ArchiveBuilder {

ArchiveBuilder::OtherROAllocMark mark;
SystemDictionaryShared::write_to_archive(false);
DynamicArchive::dump_array_klasses();

serialized_data = ro_region()->top();
WriteClosure wc(ro_region());
SymbolTable::serialize_shared_table_header(&wc, false);
SystemDictionaryShared::serialize_dictionary_headers(&wc, false);
ArchiveBuilder::serialize_dynamic_archivable_items(&wc);
}

verify_estimate_size(_estimated_hashtable_bytes, "Hashtables");
Expand All @@ -160,6 +162,7 @@ class DynamicArchiveBuilder : public ArchiveBuilder {

write_archive(serialized_data);
release_header();
DynamicArchive::post_dump();

post_dump();

Expand All @@ -170,6 +173,30 @@ class DynamicArchiveBuilder : public ArchiveBuilder {
virtual void iterate_roots(MetaspaceClosure* it) {
FileMapInfo::metaspace_pointers_do(it);
SystemDictionaryShared::dumptime_classes_do(it);
iterate_primitive_array_klasses(it);
}

void iterate_primitive_array_klasses(MetaspaceClosure* it) {
for (int i = T_BOOLEAN; i <= T_LONG; i++) {
assert(is_java_primitive((BasicType)i), "sanity");
Klass* k = Universe::typeArrayKlassObj((BasicType)i); // this give you "[I", etc
assert(MetaspaceShared::is_shared_static((void*)k),
"one-dimensional primitive array should be in static archive");
ArrayKlass* ak = ArrayKlass::cast(k);
while (ak != nullptr && ak->is_shared()) {
Klass* next_k = ak->array_klass_or_null();
if (next_k != nullptr) {
ak = ArrayKlass::cast(next_k);
} else {
ak = nullptr;
}
}
if (ak != nullptr) {
assert(ak->dimension() > 1, "sanity");
// this is the lowest dimension that's not in the static archive
it->push(&ak);
}
}
}
};

Expand Down Expand Up @@ -328,6 +355,26 @@ void DynamicArchiveBuilder::write_archive(char* serialized_data) {
log_info(cds, dynamic)("%d klasses; %d symbols", klasses()->length(), symbols()->length());
}

void DynamicArchiveBuilder::gather_array_klasses() {
for (int i = 0; i < klasses()->length(); i++) {
if (klasses()->at(i)->is_objArray_klass()) {
ObjArrayKlass* oak = ObjArrayKlass::cast(klasses()->at(i));
Klass* elem = oak->element_klass();
if (MetaspaceShared::is_shared_static(elem)) {
// Only capture the array klass whose element_klass is in the static archive.
// During run time, setup (see DynamicArchive::setup_array_klasses()) is needed
// so that the element_klass can find its array klasses from the dynamic archive.
DynamicArchive::append_array_klass(oak);
} else {
// The element_klass and its array klasses are in the same archive.
assert(!MetaspaceShared::is_shared_static(oak),
"we should not gather klasses that are already in the static archive");
}
}
}
log_debug(cds)("Total array klasses gathered for dynamic archive: %d", DynamicArchive::num_array_klasses());
}

class VM_PopulateDynamicDumpSharedSpace: public VM_GC_Sync_Operation {
DynamicArchiveBuilder _builder;
public:
Expand All @@ -349,6 +396,78 @@ class VM_PopulateDynamicDumpSharedSpace: public VM_GC_Sync_Operation {
}
};

// _array_klasses and _dynamic_archive_array_klasses only hold the array klasses
// which have element klass in the static archive.
GrowableArray<ObjArrayKlass*>* DynamicArchive::_array_klasses = nullptr;
Array<ObjArrayKlass*>* DynamicArchive::_dynamic_archive_array_klasses = nullptr;

void DynamicArchive::append_array_klass(ObjArrayKlass* ak) {
if (_array_klasses == nullptr) {
_array_klasses = new (mtClassShared) GrowableArray<ObjArrayKlass*>(50, mtClassShared);
}
_array_klasses->append(ak);
}

void DynamicArchive::dump_array_klasses() {
assert(DynamicDumpSharedSpaces, "DynamicDumpSharedSpaces only");
if (_array_klasses != nullptr) {
ArchiveBuilder* builder = ArchiveBuilder::current();
int num_array_klasses = _array_klasses->length();
_dynamic_archive_array_klasses =
ArchiveBuilder::new_ro_array<ObjArrayKlass*>(num_array_klasses);
for (int i = 0; i < num_array_klasses; i++) {
builder->write_pointer_in_buffer(_dynamic_archive_array_klasses->adr_at(i), _array_klasses->at(i));
}
}
}

void DynamicArchive::setup_array_klasses() {
if (_dynamic_archive_array_klasses != nullptr) {
for (int i = 0; i < _dynamic_archive_array_klasses->length(); i++) {
ObjArrayKlass* oak = _dynamic_archive_array_klasses->at(i);
assert(!oak->is_typeArray_klass(), "all type array classes must be in static archive");

Klass* elm = oak->element_klass();
assert(MetaspaceShared::is_shared_static((void*)elm), "must be");

if (elm->is_instance_klass()) {
assert(InstanceKlass::cast(elm)->array_klasses() == nullptr, "must be");
InstanceKlass::cast(elm)->set_array_klasses(oak);
} else {
assert(elm->is_array_klass(), "sanity");
assert(ArrayKlass::cast(elm)->higher_dimension() == nullptr, "must be");
ArrayKlass::cast(elm)->set_higher_dimension(oak);
}
}
log_debug(cds)("Total array klasses read from dynamic archive: %d", _dynamic_archive_array_klasses->length());
}
}

void DynamicArchive::serialize_array_klasses(SerializeClosure* soc) {
soc->do_ptr(&_dynamic_archive_array_klasses);
}

void DynamicArchive::make_array_klasses_shareable() {
if (_array_klasses != nullptr) {
int num_array_klasses = _array_klasses->length();
for (int i = 0; i < num_array_klasses; i++) {
ObjArrayKlass* k = ArchiveBuilder::current()->get_buffered_addr(_array_klasses->at(i));
k->remove_unshareable_info();
}
}
}

void DynamicArchive::post_dump() {
if (_array_klasses != nullptr) {
delete _array_klasses;
_array_klasses = nullptr;
}
}

int DynamicArchive::num_array_klasses() {
return _array_klasses != nullptr ? _array_klasses->length() : 0;
}

void DynamicArchive::check_for_dynamic_dump() {
if (DynamicDumpSharedSpaces && !UseSharedSpaces) {
// This could happen if SharedArchiveFile has failed to load:
Expand Down
12 changes: 12 additions & 0 deletions src/hotspot/share/cds/dynamicArchive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
#include "memory/allStatic.hpp"
#include "memory/memRegion.hpp"
#include "memory/virtualspace.hpp"
#include "oops/array.hpp"
#include "oops/oop.hpp"
#include "utilities/exceptions.hpp"
#include "utilities/growableArray.hpp"
#include "utilities/macros.hpp"

#if INCLUDE_CDS
Expand All @@ -58,12 +60,22 @@ class DynamicArchiveHeader : public FileMapHeader {
};

class DynamicArchive : AllStatic {
private:
static GrowableArray<ObjArrayKlass*>* _array_klasses;
static Array<ObjArrayKlass*>* _dynamic_archive_array_klasses;
public:
static void check_for_dynamic_dump();
static void dump_for_jcmd(const char* archive_name, TRAPS);
static void dump_at_exit(JavaThread* current, const char* archive_name);
static bool is_mapped() { return FileMapInfo::dynamic_info() != nullptr; }
static bool validate(FileMapInfo* dynamic_info);
static void dump_array_klasses();
static void setup_array_klasses();
static void append_array_klass(ObjArrayKlass* oak);
static void serialize_array_klasses(SerializeClosure* soc);
static void make_array_klasses_shareable();
static void post_dump();
static int num_array_klasses();
};
#endif // INCLUDE_CDS
#endif // SHARE_CDS_DYNAMICARCHIVE_HPP
13 changes: 11 additions & 2 deletions src/hotspot/share/cds/metaspaceShared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "cds/classPrelinker.hpp"
#include "cds/cppVtables.hpp"
#include "cds/dumpAllocStats.hpp"
#include "cds/dynamicArchive.hpp"
#include "cds/filemap.hpp"
#include "cds/heapShared.hpp"
#include "cds/lambdaFormInvokers.hpp"
Expand Down Expand Up @@ -870,6 +871,14 @@ bool MetaspaceShared::is_shared_dynamic(void* p) {
}
}

bool MetaspaceShared::is_shared_static(void* p) {
if (is_in_shared_metaspace(p) && !is_shared_dynamic(p)) {
return true;
} else {
return false;
}
}

// This function is called when the JVM is unable to load the specified archive(s) due to one
// of the following conditions.
// - There's an error that indicates that the archive(s) files were corrupt or otherwise damaged.
Expand Down Expand Up @@ -1466,8 +1475,8 @@ void MetaspaceShared::initialize_shared_spaces() {
if (dynamic_mapinfo != nullptr) {
intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data();
ReadClosure rc(&buffer);
SymbolTable::serialize_shared_table_header(&rc, false);
SystemDictionaryShared::serialize_dictionary_headers(&rc, false);
ArchiveBuilder::serialize_dynamic_archivable_items(&rc);
DynamicArchive::setup_array_klasses();
dynamic_mapinfo->close();
dynamic_mapinfo->unmap_region(MetaspaceShared::bm);
}
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/cds/metaspaceShared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class MetaspaceShared : AllStatic {
static void set_shared_metaspace_range(void* base, void *static_top, void* top) NOT_CDS_RETURN;

static bool is_shared_dynamic(void* p) NOT_CDS_RETURN_(false);
static bool is_shared_static(void* p) NOT_CDS_RETURN_(false);

static void unrecoverable_loading_error(const char* message = nullptr);
static void unrecoverable_writing_error(const char* message = nullptr);
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/logging/logTag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class outputStream;
LOG_TAG(alloc) \
LOG_TAG(annotation) \
LOG_TAG(arguments) \
LOG_TAG(array) \
LOG_TAG(attach) \
LOG_TAG(barrier) \
LOG_TAG(blocks) \
Expand Down
18 changes: 18 additions & 0 deletions src/hotspot/share/oops/arrayKlass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#include "precompiled.hpp"
#include "cds/metaspaceShared.hpp"
#include "classfile/javaClasses.hpp"
#include "classfile/moduleEntry.hpp"
#include "classfile/vmClasses.hpp"
Expand Down Expand Up @@ -96,6 +97,7 @@ ArrayKlass::ArrayKlass(Symbol* name, KlassKind kind) :
set_layout_helper(Klass::_lh_neutral_value);
set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
JFR_ONLY(INIT_ID(this);)
log_array_class_load(this);
}


Expand Down Expand Up @@ -179,6 +181,7 @@ void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle p

if (_higher_dimension != nullptr) {
ArrayKlass *ak = higher_dimension();
log_array_class_load(ak);
ak->restore_unshareable_info(loader_data, protection_domain, CHECK);
}
}
Expand All @@ -194,6 +197,21 @@ void ArrayKlass::cds_print_value_on(outputStream* st) const {
}
#endif // INCLUDE_CDS

void ArrayKlass::log_array_class_load(Klass* k) {
LogTarget(Debug, class, load, array) lt;
if (lt.is_enabled()) {
LogStream ls(lt);
ResourceMark rm;
ls.print("%s", k->name()->as_klass_external_name());
if (MetaspaceShared::is_shared_dynamic((void*)k)) {
ls.print(" source: shared objects file (top)");
} else if (MetaspaceShared::is_shared_static((void*)k)) {
ls.print(" source: shared objects file");
}
ls.cr();
}
}

// Printing

void ArrayKlass::print_on(outputStream* st) const {
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/oops/arrayKlass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class ArrayKlass: public Klass {
void cds_print_value_on(outputStream* st) const;
#endif

void log_array_class_load(Klass* k);
// Printing
void print_on(outputStream* st) const;
void print_value_on(outputStream* st) const;
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/oops/instanceKlass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ class InstanceKlass: public Klass {
ObjArrayKlass* array_klasses() const { return _array_klasses; }
inline ObjArrayKlass* array_klasses_acquire() const; // load with acquire semantics
inline void release_set_array_klasses(ObjArrayKlass* k); // store with release semantics
void set_array_klasses(ObjArrayKlass* k) { _array_klasses = k; }

// methods
Array<Method*>* methods() const { return _methods; }
Expand Down

1 comment on commit dc14247

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.