8309240: Array classes should be stored in dynamic CDS archive

Reviewed-by: iklam
This commit is contained in:
Calvin Cheung 2023-08-01 22:08:55 +00:00
parent bf7077752a
commit dc14247077
12 changed files with 334 additions and 16 deletions

@ -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"
@ -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));
}
}
@ -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 {

@ -341,6 +341,7 @@ public:
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; }

@ -88,6 +88,7 @@ public:
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() { }
@ -118,6 +119,7 @@ public:
init_header();
gather_source_objs();
gather_array_klasses();
reserve_buffer();
log_info(cds, dynamic)("Copying %d klasses and %d symbols",
@ -139,11 +141,11 @@ public:
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");
@ -160,6 +162,7 @@ public:
write_archive(serialized_data);
release_header();
DynamicArchive::post_dump();
post_dump();
@ -170,6 +173,30 @@ public:
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);
}
}
}
};
@ -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:
@ -349,6 +396,78 @@ public:
}
};
// _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:

@ -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
@ -58,12 +60,22 @@ public:
};
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

@ -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"
@ -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.
@ -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);
}

@ -101,6 +101,7 @@ public:
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);

@ -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) \

@ -23,6 +23,7 @@
*/
#include "precompiled.hpp"
#include "cds/metaspaceShared.hpp"
#include "classfile/javaClasses.hpp"
#include "classfile/moduleEntry.hpp"
#include "classfile/vmClasses.hpp"
@ -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);
}
@ -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);
}
}
@ -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 {

@ -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;

@ -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; }

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, 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
@ -47,6 +47,109 @@ public class ArrayKlasses extends DynamicArchiveTestBase {
String appJar = ClassFileInstaller.getJarPath("ArrayKlasses.jar");
String mainClass = "ArrayKlassesApp";
dumpAndRun(topArchiveName, "-Xlog:cds+dynamic=debug", "-cp", appJar, mainClass);
// Case 1
// Create a dynamic archive with the ArrayKlassesApp app class and its
// array classes.
dump2(null, topArchiveName,
"-Xlog:cds+dynamic=debug,cds+class=debug",
"-cp", appJar, mainClass)
.assertNormalExit(output -> {
output.shouldMatch("cds.class.*klasses.*array \\[LArrayKlassesApp;")
.shouldMatch("cds.class.*klasses.*array \\[\\[LArrayKlassesApp;")
.shouldMatch("cds.class.*klasses.*array \\[\\[\\[LArrayKlassesApp;");
});
// Case 1
// At runtime , the ArrayKlasesApp and its array class should be loaded
// from the dynamic archive.
run2(null, topArchiveName,
"-Xlog:class+load,class+load+array=debug,cds+dynamic=debug,cds=debug",
"-cp", appJar, mainClass)
.assertNormalExit(output -> {
output.shouldContain("ArrayKlassesApp source: shared objects file (top)")
.shouldContain("[LArrayKlassesApp; source: shared objects file (top)")
.shouldContain("[[LArrayKlassesApp; source: shared objects file (top)")
.shouldContain("[[[LArrayKlassesApp; source: shared objects file (top)")
.shouldHaveExitValue(0);
});
// Case 2
// Create a dynamic archive with the array classes of java/util/Date which
// is in the default CDS archive.
topArchiveName = getNewArchiveName();
dump2(null, topArchiveName,
"-Xlog:class+load,cds+dynamic=debug,cds+class=debug",
"-cp", appJar, mainClass, "system")
.assertNormalExit(output -> {
output.shouldContain("java.util.Date source: shared objects file")
.shouldMatch("cds.class.*klasses.*array \\[Ljava.util.Date;")
.shouldMatch("cds.class.*klasses.*array \\[\\[Ljava.util.Date;")
.shouldMatch("cds.class.*klasses.*array \\[\\[\\[Ljava.util.Date;");
});
// Case 2
// At runtime, the java/util/Date class should be loaded from the default
// CDS archive; its array class should be loaded from the dynamic archive.
run2(null, topArchiveName,
"-Xlog:class+load,class+load+array=debug,cds+dynamic=debug,cds=debug",
"-cp", appJar, mainClass, "system")
.assertNormalExit(output -> {
output.shouldContain("java.util.Date source: shared objects file")
.shouldContain("[Ljava.util.Date; source: shared objects file (top)")
.shouldContain("[[Ljava.util.Date; source: shared objects file (top)")
.shouldContain("[[[Ljava.util.Date; source: shared objects file (top)")
.shouldHaveExitValue(0);
});
// Case 3
// Create a dynamic archive with primitive arrays [[J and [[[J with [J
// already in the default CDS archive
topArchiveName = getNewArchiveName();
dump2(null, topArchiveName,
"-Xlog:class+load,cds+dynamic=debug,cds+class=debug",
"-cp", appJar, mainClass, "primitive")
.assertNormalExit(output -> {
output.shouldMatch("cds.class.*klasses.*array \\[\\[J")
.shouldMatch("cds.class.*klasses.*array \\[\\[\\[J");
});
// Case 3
// At runtime, the [J should be loaded from the default CDS archive;
// the higher-dimension array should be loaded from the dynamic archive.
run2(null, topArchiveName,
"-Xlog:class+load,class+load+array=debug,cds+dynamic=debug,cds=debug",
"-cp", appJar, mainClass, "primitive")
.assertNormalExit(output -> {
output.shouldContain("[J source: shared objects file")
.shouldContain("[[J source: shared objects file (top)")
.shouldContain("[[[J source: shared objects file (top)")
.shouldHaveExitValue(0);
});
// Case 4
// Create a dynamic archive with 2-, 3- and 4-dimension arrays of java/lang/Integer.
// The java/lang/Integer class and the 1-dimension array is in the default archive.
topArchiveName = getNewArchiveName();
dump2(null, topArchiveName,
"-Xlog:class+load,cds+dynamic=debug,cds+class=debug",
"-cp", appJar, mainClass, "integer-array")
.assertNormalExit(output -> {
output.shouldMatch("cds.class.*klasses.*array \\[\\[Ljava.lang.Integer;")
.shouldMatch("cds.class.*klasses.*array \\[\\[\\[Ljava.lang.Integer;")
.shouldMatch("cds.class.*klasses.*array \\[\\[\\[\\[Ljava.lang.Integer;");
});
// Case 4
// At runtime, the 4-dimension array of java/lang/Integer should be
// loaded from the dynamic archive.
run2(null, topArchiveName,
"-Xlog:class+load,class+load+array=debug,cds+dynamic=debug,cds=debug",
"-cp", appJar, mainClass, "integer-array")
.assertNormalExit(output -> {
output.shouldContain("[[Ljava.lang.Integer; source: shared objects file (top)")
.shouldContain("[[[Ljava.lang.Integer; source: shared objects file (top)")
.shouldContain("[[[[Ljava.lang.Integer; source: shared objects file (top)")
.shouldHaveExitValue(0);
});
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, 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
@ -22,13 +22,56 @@
*
*/
import java.lang.reflect.Array;
import java.util.Date;
public class ArrayKlassesApp {
public static void main(String args[]) {
ArrayKlassesApp[][] array = new ArrayKlassesApp[1][2];
for (int i=0; i<1; i++) {
for (int j=0; j<2; j++) {
array[i][j] = new ArrayKlassesApp();
if (args.length == 1) {
if (args[0].equals("system")) {
Date[][][] array = new Date[1][2][2];
int count = 0;
for (int i=0; i<1; i++) {
for (int j=0; j<2; j++) {
for (int k=0; k<2; k++) {
array[i][j][k] = new Date();
count++;
array[i][j][k].setTime(20000 * count);
}
}
}
} else if (args[0].equals("primitive")) {
long[][][] larray = new long[1][2][2];
long lcount = 0;
for (int i=0; i<1; i++) {
for (int j=0; j<2; j++) {
for (int k=0; k<2; k++) {
lcount++;
larray[i][j][k] = lcount;
}
}
}
} else if (args[0].equals("integer-array")) {
Integer[][][][] iarray = new Integer[4][4][4][4];
int count = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 4; l++) {
count++;
iarray[i][j][k][l] = new Integer(count);
}
}
}
}
System.out.println(iarray);
System.out.println(iarray.getClass());
}
} else {
Object x = Array.newInstance(ArrayKlassesApp.class, 3,3,3);
System.out.println(x);
System.out.println(x.getClass());
System.out.println(Array.getLength(x));
}
}
}