annotate src/hotspot/share/memory/heapShared.cpp @ 53594:991fe09c698c

8213033: Archive remaining primitive box caches Reviewed-by: jiangli, alanb
author redestad
date Tue, 20 Nov 2018 21:12:46 +0100
parents f4e3900c8d08
children 00c47178ea6c
rev   line source
jiangli@51612 1 /*
jiangli@51612 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
jiangli@51612 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jiangli@51612 4 *
jiangli@51612 5 * This code is free software; you can redistribute it and/or modify it
jiangli@51612 6 * under the terms of the GNU General Public License version 2 only, as
jiangli@51612 7 * published by the Free Software Foundation.
jiangli@51612 8 *
jiangli@51612 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jiangli@51612 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jiangli@51612 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jiangli@51612 12 * version 2 for more details (a copy is included in the LICENSE file that
jiangli@51612 13 * accompanied this code).
jiangli@51612 14 *
jiangli@51612 15 * You should have received a copy of the GNU General Public License version
jiangli@51612 16 * 2 along with this work; if not, write to the Free Software Foundation,
jiangli@51612 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jiangli@51612 18 *
jiangli@51612 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jiangli@51612 20 * or visit www.oracle.com if you need additional information or have any
jiangli@51612 21 * questions.
jiangli@51612 22 *
jiangli@51612 23 */
jiangli@51612 24
jiangli@51612 25 #include "precompiled.hpp"
jiangli@51612 26 #include "classfile/javaClasses.inline.hpp"
jiangli@52991 27 #include "classfile/stringTable.hpp"
iklam@52155 28 #include "classfile/symbolTable.hpp"
jiangli@51612 29 #include "classfile/vmSymbols.hpp"
jiangli@51612 30 #include "logging/log.hpp"
jiangli@51612 31 #include "logging/logMessage.hpp"
jiangli@51612 32 #include "logging/logStream.hpp"
jiangli@52991 33 #include "memory/filemap.hpp"
iklam@52217 34 #include "memory/heapShared.inline.hpp"
jiangli@51612 35 #include "memory/iterator.inline.hpp"
jiangli@51612 36 #include "memory/metadataFactory.hpp"
jiangli@51612 37 #include "memory/metaspaceClosure.hpp"
jiangli@51612 38 #include "memory/resourceArea.hpp"
jiangli@51612 39 #include "oops/compressedOops.inline.hpp"
jiangli@53331 40 #include "oops/fieldStreams.hpp"
jiangli@51612 41 #include "oops/oop.inline.hpp"
iklam@52193 42 #include "runtime/fieldDescriptor.inline.hpp"
jiangli@52991 43 #include "runtime/safepointVerifiers.hpp"
iklam@52217 44 #include "utilities/bitMap.inline.hpp"
jiangli@52991 45 #if INCLUDE_G1GC
jiangli@52991 46 #include "gc/g1/g1CollectedHeap.hpp"
jiangli@52991 47 #endif
jiangli@51612 48
jiangli@51612 49 #if INCLUDE_CDS_JAVA_HEAP
jiangli@52991 50
jiangli@53331 51 bool HeapShared::_closed_archive_heap_region_mapped = false;
jiangli@52991 52 bool HeapShared::_open_archive_heap_region_mapped = false;
jiangli@52991 53 bool HeapShared::_archive_heap_region_fixed = false;
jiangli@52991 54
iklam@52976 55 address HeapShared::_narrow_oop_base;
iklam@52976 56 int HeapShared::_narrow_oop_shift;
jiangli@52991 57
jiangli@53331 58 //
jiangli@53331 59 // If you add new entries to the following tables, you should know what you're doing!
jiangli@53331 60 //
jiangli@53331 61
jiangli@53331 62 // Entry fields for shareable subgraphs archived in the closed archive heap
jiangli@53331 63 // region. Warning: Objects in the subgraphs should not have reference fields
jiangli@53331 64 // assigned at runtime.
jiangli@53331 65 static ArchivableStaticFieldInfo closed_archive_subgraph_entry_fields[] = {
jiangli@53331 66 {"java/lang/Integer$IntegerCache", "archivedCache"},
redestad@53594 67 {"java/lang/Long$LongCache", "archivedCache"},
redestad@53594 68 {"java/lang/Byte$ByteCache", "archivedCache"},
redestad@53594 69 {"java/lang/Short$ShortCache", "archivedCache"},
redestad@53594 70 {"java/lang/Character$CharacterCache", "archivedCache"},
jiangli@53331 71 };
jiangli@53331 72 // Entry fields for subgraphs archived in the open archive heap region.
jiangli@53331 73 static ArchivableStaticFieldInfo open_archive_subgraph_entry_fields[] = {
jiangli@53331 74 {"jdk/internal/module/ArchivedModuleGraph", "archivedSystemModules"},
jiangli@53331 75 {"jdk/internal/module/ArchivedModuleGraph", "archivedModuleFinder"},
jiangli@53331 76 {"jdk/internal/module/ArchivedModuleGraph", "archivedMainModule"},
jiangli@53331 77 {"jdk/internal/module/ArchivedModuleGraph", "archivedConfiguration"},
jiangli@53331 78 {"java/util/ImmutableCollections$ListN", "EMPTY_LIST"},
jiangli@53331 79 {"java/util/ImmutableCollections$MapN", "EMPTY_MAP"},
jiangli@53331 80 {"java/util/ImmutableCollections$SetN", "EMPTY_SET"},
jiangli@53331 81 {"java/lang/module/Configuration", "EMPTY_CONFIGURATION"},
jiangli@53331 82 };
jiangli@53331 83
jiangli@53331 84 const static int num_closed_archive_subgraph_entry_fields =
jiangli@53331 85 sizeof(closed_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
jiangli@53331 86 const static int num_open_archive_subgraph_entry_fields =
jiangli@53331 87 sizeof(open_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
jiangli@53331 88
jiangli@52991 89 ////////////////////////////////////////////////////////////////
jiangli@52991 90 //
jiangli@52991 91 // Java heap object archiving support
jiangli@52991 92 //
jiangli@52991 93 ////////////////////////////////////////////////////////////////
jiangli@52991 94 void HeapShared::fixup_mapped_heap_regions() {
jiangli@52991 95 FileMapInfo *mapinfo = FileMapInfo::current_info();
jiangli@52991 96 mapinfo->fixup_mapped_heap_regions();
jiangli@52991 97 set_archive_heap_region_fixed();
jiangli@52991 98 }
jiangli@52991 99
jiangli@52991 100 unsigned HeapShared::oop_hash(oop const& p) {
jiangli@52991 101 assert(!p->mark()->has_bias_pattern(),
jiangli@52991 102 "this object should never have been locked"); // so identity_hash won't safepoin
jiangli@52991 103 unsigned hash = (unsigned)p->identity_hash();
jiangli@52991 104 return hash;
jiangli@52991 105 }
jiangli@52991 106
jiangli@52991 107 HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = NULL;
jiangli@52991 108 oop HeapShared::find_archived_heap_object(oop obj) {
jiangli@52991 109 assert(DumpSharedSpaces, "dump-time only");
jiangli@52991 110 ArchivedObjectCache* cache = archived_object_cache();
jiangli@52991 111 oop* p = cache->get(obj);
jiangli@52991 112 if (p != NULL) {
jiangli@52991 113 return *p;
jiangli@52991 114 } else {
jiangli@52991 115 return NULL;
jiangli@52991 116 }
jiangli@52991 117 }
jiangli@52991 118
jiangli@52991 119 oop HeapShared::archive_heap_object(oop obj, Thread* THREAD) {
jiangli@52991 120 assert(DumpSharedSpaces, "dump-time only");
jiangli@52991 121
jiangli@52991 122 oop ao = find_archived_heap_object(obj);
jiangli@52991 123 if (ao != NULL) {
jiangli@52991 124 // already archived
jiangli@52991 125 return ao;
jiangli@52991 126 }
jiangli@52991 127
jiangli@52991 128 int len = obj->size();
jiangli@52991 129 if (G1CollectedHeap::heap()->is_archive_alloc_too_large(len)) {
jiangli@52991 130 log_debug(cds, heap)("Cannot archive, object (" PTR_FORMAT ") is too large: " SIZE_FORMAT,
jiangli@52991 131 p2i(obj), (size_t)obj->size());
jiangli@52991 132 return NULL;
jiangli@52991 133 }
jiangli@52991 134
jiangli@52991 135 // Pre-compute object identity hash at CDS dump time.
jiangli@52991 136 obj->identity_hash();
jiangli@52991 137
jiangli@52991 138 oop archived_oop = (oop)G1CollectedHeap::heap()->archive_mem_allocate(len);
jiangli@52991 139 if (archived_oop != NULL) {
jiangli@52991 140 Copy::aligned_disjoint_words((HeapWord*)obj, (HeapWord*)archived_oop, len);
jiangli@52991 141 MetaspaceShared::relocate_klass_ptr(archived_oop);
jiangli@52991 142 ArchivedObjectCache* cache = archived_object_cache();
jiangli@52991 143 cache->put(obj, archived_oop);
jiangli@52991 144 log_debug(cds, heap)("Archived heap object " PTR_FORMAT " ==> " PTR_FORMAT,
jiangli@52991 145 p2i(obj), p2i(archived_oop));
jiangli@52991 146 } else {
jiangli@52991 147 log_error(cds, heap)(
jiangli@52991 148 "Cannot allocate space for object " PTR_FORMAT " in archived heap region",
jiangli@52991 149 p2i(obj));
jiangli@52991 150 vm_exit(1);
jiangli@52991 151 }
jiangli@52991 152 return archived_oop;
jiangli@52991 153 }
jiangli@52991 154
jiangli@52991 155 oop HeapShared::materialize_archived_object(narrowOop v) {
jiangli@52991 156 assert(archive_heap_region_fixed(),
jiangli@52991 157 "must be called after archive heap regions are fixed");
jiangli@52991 158 if (!CompressedOops::is_null(v)) {
jiangli@52991 159 oop obj = HeapShared::decode_from_archive(v);
jiangli@52991 160 return G1CollectedHeap::heap()->materialize_archived_object(obj);
jiangli@52991 161 }
jiangli@52991 162 return NULL;
jiangli@52991 163 }
jiangli@52991 164
jiangli@52991 165 void HeapShared::archive_klass_objects(Thread* THREAD) {
jiangli@52991 166 GrowableArray<Klass*>* klasses = MetaspaceShared::collected_klasses();
jiangli@52991 167 assert(klasses != NULL, "sanity");
jiangli@52991 168 for (int i = 0; i < klasses->length(); i++) {
jiangli@52991 169 Klass* k = klasses->at(i);
jiangli@52991 170
jiangli@52991 171 // archive mirror object
jiangli@52991 172 java_lang_Class::archive_mirror(k, CHECK);
jiangli@52991 173
jiangli@52991 174 // archive the resolved_referenes array
jiangli@52991 175 if (k->is_instance_klass()) {
jiangli@52991 176 InstanceKlass* ik = InstanceKlass::cast(k);
jiangli@52991 177 ik->constants()->archive_resolved_references(THREAD);
jiangli@52991 178 }
jiangli@52991 179 }
jiangli@52991 180 }
jiangli@52991 181
jiangli@52991 182 void HeapShared::archive_java_heap_objects(GrowableArray<MemRegion> *closed,
jiangli@52991 183 GrowableArray<MemRegion> *open) {
jiangli@52991 184 if (!is_heap_object_archiving_allowed()) {
jiangli@52991 185 if (log_is_enabled(Info, cds)) {
jiangli@52991 186 log_info(cds)(
jiangli@52991 187 "Archived java heap is not supported as UseG1GC, "
jiangli@52991 188 "UseCompressedOops and UseCompressedClassPointers are required."
jiangli@52991 189 "Current settings: UseG1GC=%s, UseCompressedOops=%s, UseCompressedClassPointers=%s.",
jiangli@52991 190 BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedOops),
jiangli@52991 191 BOOL_TO_STR(UseCompressedClassPointers));
jiangli@52991 192 }
jiangli@52991 193 return;
jiangli@52991 194 }
jiangli@52991 195
jiangli@52991 196 {
jiangli@52991 197 NoSafepointVerifier nsv;
jiangli@52991 198
jiangli@52991 199 // Cache for recording where the archived objects are copied to
jiangli@52991 200 create_archived_object_cache();
jiangli@52991 201
jiangli@52991 202 tty->print_cr("Dumping objects to closed archive heap region ...");
jiangli@52991 203 NOT_PRODUCT(StringTable::verify());
jiangli@52991 204 copy_closed_archive_heap_objects(closed);
jiangli@52991 205
jiangli@52991 206 tty->print_cr("Dumping objects to open archive heap region ...");
jiangli@52991 207 copy_open_archive_heap_objects(open);
jiangli@52991 208
jiangli@52991 209 destroy_archived_object_cache();
jiangli@52991 210 }
jiangli@52991 211
jiangli@52991 212 G1HeapVerifier::verify_archive_regions();
jiangli@52991 213 }
jiangli@52991 214
jiangli@52991 215 void HeapShared::copy_closed_archive_heap_objects(
jiangli@52991 216 GrowableArray<MemRegion> * closed_archive) {
jiangli@52991 217 assert(is_heap_object_archiving_allowed(), "Cannot archive java heap objects");
jiangli@52991 218
jiangli@52991 219 Thread* THREAD = Thread::current();
jiangli@52991 220 G1CollectedHeap::heap()->begin_archive_alloc_range();
jiangli@52991 221
jiangli@52991 222 // Archive interned string objects
jiangli@52991 223 StringTable::write_to_archive();
jiangli@52991 224
jiangli@53331 225 archive_object_subgraphs(closed_archive_subgraph_entry_fields,
jiangli@53331 226 num_closed_archive_subgraph_entry_fields,
jiangli@53331 227 true /* is_closed_archive */, THREAD);
jiangli@53331 228
jiangli@52991 229 G1CollectedHeap::heap()->end_archive_alloc_range(closed_archive,
jiangli@52991 230 os::vm_allocation_granularity());
jiangli@52991 231 }
jiangli@52991 232
jiangli@52991 233 void HeapShared::copy_open_archive_heap_objects(
jiangli@52991 234 GrowableArray<MemRegion> * open_archive) {
jiangli@52991 235 assert(is_heap_object_archiving_allowed(), "Cannot archive java heap objects");
jiangli@52991 236
jiangli@52991 237 Thread* THREAD = Thread::current();
jiangli@52991 238 G1CollectedHeap::heap()->begin_archive_alloc_range(true /* open */);
jiangli@52991 239
jiangli@52991 240 java_lang_Class::archive_basic_type_mirrors(THREAD);
jiangli@52991 241
jiangli@52991 242 archive_klass_objects(THREAD);
jiangli@52991 243
jiangli@53331 244 archive_object_subgraphs(open_archive_subgraph_entry_fields,
jiangli@53331 245 num_open_archive_subgraph_entry_fields,
jiangli@53331 246 false /* is_closed_archive */,
jiangli@53331 247 THREAD);
jiangli@52991 248
jiangli@52991 249 G1CollectedHeap::heap()->end_archive_alloc_range(open_archive,
jiangli@52991 250 os::vm_allocation_granularity());
jiangli@52991 251 }
jiangli@52991 252
jiangli@52991 253 void HeapShared::init_narrow_oop_decoding(address base, int shift) {
jiangli@52991 254 _narrow_oop_base = base;
jiangli@52991 255 _narrow_oop_shift = shift;
jiangli@52991 256 }
jiangli@52991 257
jiangli@52991 258 //
jiangli@52991 259 // Subgraph archiving support
jiangli@52991 260 //
iklam@52976 261 HeapShared::DumpTimeKlassSubGraphInfoTable* HeapShared::_dump_time_subgraph_info_table = NULL;
iklam@52976 262 HeapShared::RunTimeKlassSubGraphInfoTable HeapShared::_run_time_subgraph_info_table;
jiangli@51612 263
jiangli@51612 264 // Get the subgraph_info for Klass k. A new subgraph_info is created if
jiangli@51612 265 // there is no existing one for k. The subgraph_info records the relocated
jiangli@51612 266 // Klass* of the original k.
jiangli@51612 267 KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) {
iklam@52976 268 assert(DumpSharedSpaces, "dump time only");
jiangli@51612 269 Klass* relocated_k = MetaspaceShared::get_relocated_klass(k);
iklam@52976 270 KlassSubGraphInfo* info = _dump_time_subgraph_info_table->get(relocated_k);
iklam@52976 271 if (info == NULL) {
iklam@52976 272 _dump_time_subgraph_info_table->put(relocated_k, KlassSubGraphInfo(relocated_k));
iklam@52976 273 info = _dump_time_subgraph_info_table->get(relocated_k);
iklam@52976 274 ++ _dump_time_subgraph_info_table->_count;
jiangli@51612 275 }
jiangli@51612 276 return info;
jiangli@51612 277 }
jiangli@51612 278
jiangli@51612 279 // Add an entry field to the current KlassSubGraphInfo.
jiangli@53331 280 void KlassSubGraphInfo::add_subgraph_entry_field(
jiangli@53331 281 int static_field_offset, oop v, bool is_closed_archive) {
jiangli@51612 282 assert(DumpSharedSpaces, "dump time only");
jiangli@51612 283 if (_subgraph_entry_fields == NULL) {
jiangli@51612 284 _subgraph_entry_fields =
jiangli@51612 285 new(ResourceObj::C_HEAP, mtClass) GrowableArray<juint>(10, true);
jiangli@51612 286 }
jiangli@51612 287 _subgraph_entry_fields->append((juint)static_field_offset);
jiangli@51612 288 _subgraph_entry_fields->append(CompressedOops::encode(v));
jiangli@53331 289 _subgraph_entry_fields->append(is_closed_archive ? 1 : 0);
jiangli@51612 290 }
jiangli@51612 291
jiangli@51612 292 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs.
jiangli@51612 293 // Only objects of boot classes can be included in sub-graph.
jiangli@51612 294 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k, Klass *relocated_k) {
jiangli@51612 295 assert(DumpSharedSpaces, "dump time only");
jiangli@51612 296 assert(relocated_k == MetaspaceShared::get_relocated_klass(orig_k),
jiangli@51612 297 "must be the relocated Klass in the shared space");
jiangli@51612 298
jiangli@51612 299 if (_subgraph_object_klasses == NULL) {
jiangli@51612 300 _subgraph_object_klasses =
jiangli@51612 301 new(ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(50, true);
jiangli@51612 302 }
jiangli@51612 303
jiangli@51612 304 assert(relocated_k->is_shared(), "must be a shared class");
jiangli@52083 305
jiangli@52083 306 if (_k == relocated_k) {
jiangli@52083 307 // Don't add the Klass containing the sub-graph to it's own klass
jiangli@52083 308 // initialization list.
jiangli@52083 309 return;
jiangli@52083 310 }
jiangli@52083 311
jiangli@51612 312 if (relocated_k->is_instance_klass()) {
jiangli@51612 313 assert(InstanceKlass::cast(relocated_k)->is_shared_boot_class(),
jiangli@51612 314 "must be boot class");
jiangli@51612 315 // SystemDictionary::xxx_klass() are not updated, need to check
jiangli@51612 316 // the original Klass*
jiangli@51612 317 if (orig_k == SystemDictionary::String_klass() ||
jiangli@51612 318 orig_k == SystemDictionary::Object_klass()) {
jiangli@51612 319 // Initialized early during VM initialization. No need to be added
jiangli@51612 320 // to the sub-graph object class list.
jiangli@51612 321 return;
jiangli@51612 322 }
jiangli@51612 323 } else if (relocated_k->is_objArray_klass()) {
jiangli@51612 324 Klass* abk = ObjArrayKlass::cast(relocated_k)->bottom_klass();
jiangli@51612 325 if (abk->is_instance_klass()) {
jiangli@51612 326 assert(InstanceKlass::cast(abk)->is_shared_boot_class(),
jiangli@51612 327 "must be boot class");
jiangli@51612 328 }
jiangli@51612 329 if (relocated_k == Universe::objectArrayKlassObj()) {
jiangli@51612 330 // Initialized early during Universe::genesis. No need to be added
jiangli@51612 331 // to the list.
jiangli@51612 332 return;
jiangli@51612 333 }
jiangli@51612 334 } else {
jiangli@51612 335 assert(relocated_k->is_typeArray_klass(), "must be");
jiangli@51612 336 // Primitive type arrays are created early during Universe::genesis.
jiangli@51612 337 return;
jiangli@51612 338 }
jiangli@51612 339
iklam@52469 340 if (log_is_enabled(Debug, cds, heap)) {
iklam@52469 341 if (!_subgraph_object_klasses->contains(relocated_k)) {
iklam@52469 342 ResourceMark rm;
iklam@52469 343 log_debug(cds, heap)("Adding klass %s", orig_k->external_name());
iklam@52469 344 }
iklam@52469 345 }
iklam@52469 346
jiangli@51612 347 _subgraph_object_klasses->append_if_missing(relocated_k);
jiangli@51612 348 }
jiangli@51612 349
jiangli@51612 350 // Initialize an archived subgraph_info_record from the given KlassSubGraphInfo.
jiangli@51612 351 void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) {
jiangli@51612 352 _k = info->klass();
jiangli@51612 353 _entry_field_records = NULL;
iklam@52469 354 _subgraph_object_klasses = NULL;
jiangli@51612 355
jiangli@51612 356 // populate the entry fields
jiangli@51612 357 GrowableArray<juint>* entry_fields = info->subgraph_entry_fields();
jiangli@51612 358 if (entry_fields != NULL) {
jiangli@51612 359 int num_entry_fields = entry_fields->length();
jiangli@53331 360 assert(num_entry_fields % 3 == 0, "sanity");
jiangli@51612 361 _entry_field_records =
jiangli@51612 362 MetaspaceShared::new_ro_array<juint>(num_entry_fields);
jiangli@51612 363 for (int i = 0 ; i < num_entry_fields; i++) {
jiangli@51612 364 _entry_field_records->at_put(i, entry_fields->at(i));
jiangli@51612 365 }
jiangli@51612 366 }
jiangli@51612 367
jiangli@51612 368 // the Klasses of the objects in the sub-graphs
iklam@52469 369 GrowableArray<Klass*>* subgraph_object_klasses = info->subgraph_object_klasses();
iklam@52469 370 if (subgraph_object_klasses != NULL) {
iklam@52469 371 int num_subgraphs_klasses = subgraph_object_klasses->length();
iklam@52469 372 _subgraph_object_klasses =
jiangli@51612 373 MetaspaceShared::new_ro_array<Klass*>(num_subgraphs_klasses);
jiangli@51612 374 for (int i = 0; i < num_subgraphs_klasses; i++) {
iklam@52469 375 Klass* subgraph_k = subgraph_object_klasses->at(i);
jiangli@51612 376 if (log_is_enabled(Info, cds, heap)) {
jiangli@51612 377 ResourceMark rm;
jiangli@51612 378 log_info(cds, heap)(
iklam@52469 379 "Archived object klass %s (%2d) => %s",
iklam@52469 380 _k->external_name(), i, subgraph_k->external_name());
jiangli@51612 381 }
iklam@52469 382 _subgraph_object_klasses->at_put(i, subgraph_k);
jiangli@51612 383 }
jiangli@51612 384 }
jiangli@51612 385 }
jiangli@51612 386
iklam@52976 387 struct CopyKlassSubGraphInfoToArchive : StackObj {
iklam@52976 388 CompactHashtableWriter* _writer;
iklam@52976 389 CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {}
iklam@52976 390
iklam@52976 391 bool do_entry(Klass* klass, KlassSubGraphInfo& info) {
iklam@52976 392 if (info.subgraph_object_klasses() != NULL || info.subgraph_entry_fields() != NULL) {
iklam@52976 393 ArchivedKlassSubGraphInfoRecord* record =
iklam@52976 394 (ArchivedKlassSubGraphInfoRecord*)MetaspaceShared::read_only_space_alloc(sizeof(ArchivedKlassSubGraphInfoRecord));
iklam@52976 395 record->init(&info);
iklam@52976 396
iklam@52976 397 unsigned int hash = primitive_hash<Klass*>(klass);
iklam@53482 398 u4 delta = MetaspaceShared::object_delta_u4(record);
iklam@52976 399 _writer->add(hash, delta);
iklam@52976 400 }
iklam@52976 401 return true; // keep on iterating
iklam@52976 402 }
iklam@52976 403 };
iklam@52976 404
jiangli@51612 405 // Build the records of archived subgraph infos, which include:
jiangli@51612 406 // - Entry points to all subgraphs from the containing class mirror. The entry
jiangli@51612 407 // points are static fields in the mirror. For each entry point, the field
jiangli@53331 408 // offset, value and is_closed_archive flag are recorded in the sub-graph
jiangli@53331 409 // info. The value is stored back to the corresponding field at runtime.
jiangli@51612 410 // - A list of klasses that need to be loaded/initialized before archived
jiangli@51612 411 // java object sub-graph can be accessed at runtime.
iklam@52976 412 void HeapShared::write_subgraph_info_table() {
iklam@52976 413 // Allocate the contents of the hashtable(s) inside the RO region of the CDS archive.
iklam@52976 414 DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table;
iklam@52976 415 CompactHashtableStats stats;
jiangli@51612 416
iklam@52976 417 _run_time_subgraph_info_table.reset();
jiangli@51612 418
iklam@52976 419 int num_buckets = CompactHashtableWriter::default_num_buckets(d_table->_count);
iklam@52976 420 CompactHashtableWriter writer(num_buckets, &stats);
iklam@52976 421 CopyKlassSubGraphInfoToArchive copy(&writer);
iklam@53482 422 d_table->iterate(&copy);
jiangli@51612 423
iklam@52976 424 writer.dump(&_run_time_subgraph_info_table, "subgraphs");
jiangli@51612 425 }
jiangli@51612 426
iklam@52976 427 void HeapShared::serialize_subgraph_info_table_header(SerializeClosure* soc) {
iklam@52976 428 _run_time_subgraph_info_table.serialize_header(soc);
jiangli@51612 429 }
jiangli@51612 430
jiangli@51612 431 void HeapShared::initialize_from_archived_subgraph(Klass* k) {
jiangli@52991 432 if (!open_archive_heap_region_mapped()) {
jiangli@51612 433 return; // nothing to do
jiangli@51612 434 }
iklam@52976 435 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
jiangli@51612 436
iklam@52976 437 unsigned int hash = primitive_hash<Klass*>(k);
iklam@53482 438 const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
jiangli@51612 439
iklam@52469 440 // Initialize from archived data. Currently this is done only
iklam@52469 441 // during VM initialization time. No lock is needed.
iklam@52976 442 if (record != NULL) {
iklam@52976 443 Thread* THREAD = Thread::current();
iklam@52976 444 if (log_is_enabled(Info, cds, heap)) {
iklam@52976 445 ResourceMark rm;
iklam@52976 446 log_info(cds, heap)("initialize_from_archived_subgraph " PTR_FORMAT " %s", p2i(k),
iklam@52976 447 k->external_name());
iklam@52976 448 }
iklam@52976 449
iklam@52976 450 int i;
iklam@52976 451 // Load/link/initialize the klasses of the objects in the subgraph.
iklam@52976 452 // NULL class loader is used.
iklam@52976 453 Array<Klass*>* klasses = record->subgraph_object_klasses();
iklam@52976 454 if (klasses != NULL) {
iklam@52976 455 for (i = 0; i < klasses->length(); i++) {
iklam@52976 456 Klass* obj_k = klasses->at(i);
iklam@52976 457 Klass* resolved_k = SystemDictionary::resolve_or_null(
iklam@52976 458 (obj_k)->name(), THREAD);
iklam@52976 459 if (resolved_k != obj_k) {
iklam@53248 460 assert(!SystemDictionary::is_well_known_klass(resolved_k),
iklam@53248 461 "shared well-known classes must not be replaced by JVMTI ClassFileLoadHook");
iklam@53248 462 ResourceMark rm(THREAD);
iklam@53248 463 log_info(cds, heap)("Failed to load subgraph because %s was not loaded from archive",
iklam@53248 464 resolved_k->external_name());
iklam@52976 465 return;
iklam@52976 466 }
iklam@52976 467 if ((obj_k)->is_instance_klass()) {
iklam@52976 468 InstanceKlass* ik = InstanceKlass::cast(obj_k);
iklam@52976 469 ik->initialize(THREAD);
iklam@52976 470 } else if ((obj_k)->is_objArray_klass()) {
iklam@52976 471 ObjArrayKlass* oak = ObjArrayKlass::cast(obj_k);
iklam@52976 472 oak->initialize(THREAD);
jiangli@51612 473 }
jiangli@51612 474 }
iklam@52976 475 }
jiangli@51612 476
iklam@52976 477 if (HAS_PENDING_EXCEPTION) {
iklam@52976 478 CLEAR_PENDING_EXCEPTION;
iklam@52976 479 // None of the field value will be set if there was an exception.
iklam@52976 480 // The java code will not see any of the archived objects in the
iklam@52976 481 // subgraphs referenced from k in this case.
iklam@52976 482 return;
iklam@52976 483 }
iklam@52976 484
iklam@52976 485 // Load the subgraph entry fields from the record and store them back to
iklam@52976 486 // the corresponding fields within the mirror.
iklam@52976 487 oop m = k->java_mirror();
iklam@52976 488 Array<juint>* entry_field_records = record->entry_field_records();
iklam@52976 489 if (entry_field_records != NULL) {
iklam@52976 490 int efr_len = entry_field_records->length();
jiangli@53331 491 assert(efr_len % 3 == 0, "sanity");
iklam@52976 492 for (i = 0; i < efr_len;) {
iklam@52976 493 int field_offset = entry_field_records->at(i);
jiangli@53331 494 narrowOop nv = entry_field_records->at(i+1);
jiangli@53331 495 int is_closed_archive = entry_field_records->at(i+2);
jiangli@53331 496 oop v;
jiangli@53331 497 if (is_closed_archive == 0) {
jiangli@53331 498 // It's an archived object in the open archive heap regions, not shared.
jiangli@53331 499 // The object refereced by the field becomes 'known' by GC from this
jiangli@53331 500 // point. All objects in the subgraph reachable from the object are
jiangli@53331 501 // also 'known' by GC.
jiangli@53331 502 v = materialize_archived_object(nv);
jiangli@53331 503 } else {
jiangli@53331 504 // Shared object in the closed archive heap regions. Decode directly.
jiangli@53331 505 assert(!CompressedOops::is_null(nv), "shared object is null");
jiangli@53331 506 v = HeapShared::decode_from_archive(nv);
jiangli@53331 507 }
iklam@52976 508 m->obj_field_put(field_offset, v);
jiangli@53331 509 i += 3;
iklam@52976 510
iklam@52976 511 log_debug(cds, heap)(" " PTR_FORMAT " init field @ %2d = " PTR_FORMAT, p2i(k), field_offset, p2i(v));
jiangli@51612 512 }
jiangli@51612 513
iklam@52976 514 // Done. Java code can see the archived sub-graphs referenced from k's
iklam@52976 515 // mirror after this point.
jiangli@51612 516 }
jiangli@51612 517 }
jiangli@51612 518 }
jiangli@51612 519
jiangli@51612 520 class WalkOopAndArchiveClosure: public BasicOopIterateClosure {
jiangli@51612 521 int _level;
jiangli@53331 522 bool _is_closed_archive;
iklam@52469 523 bool _record_klasses_only;
jiangli@51612 524 KlassSubGraphInfo* _subgraph_info;
jiangli@51612 525 oop _orig_referencing_obj;
jiangli@51612 526 oop _archived_referencing_obj;
iklam@52469 527 Thread* _thread;
jiangli@51612 528 public:
jiangli@53331 529 WalkOopAndArchiveClosure(int level,
jiangli@53331 530 bool is_closed_archive,
jiangli@53331 531 bool record_klasses_only,
iklam@52469 532 KlassSubGraphInfo* subgraph_info,
iklam@52469 533 oop orig, oop archived, TRAPS) :
jiangli@53331 534 _level(level), _is_closed_archive(is_closed_archive),
jiangli@53331 535 _record_klasses_only(record_klasses_only),
iklam@52469 536 _subgraph_info(subgraph_info),
iklam@52469 537 _orig_referencing_obj(orig), _archived_referencing_obj(archived),
iklam@52469 538 _thread(THREAD) {}
jiangli@51612 539 void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
jiangli@51612 540 void do_oop( oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
jiangli@51612 541
jiangli@51612 542 protected:
jiangli@51612 543 template <class T> void do_oop_work(T *p) {
jiangli@51612 544 oop obj = RawAccess<>::oop_load(p);
jiangli@51612 545 if (!CompressedOops::is_null(obj)) {
jiangli@52991 546 assert(!HeapShared::is_archived_object(obj),
iklam@52469 547 "original objects must not point to archived objects");
iklam@52469 548
iklam@52469 549 size_t field_delta = pointer_delta(p, _orig_referencing_obj, sizeof(char));
iklam@52469 550 T* new_p = (T*)(address(_archived_referencing_obj) + field_delta);
iklam@52469 551 Thread* THREAD = _thread;
iklam@52469 552
iklam@52469 553 if (!_record_klasses_only && log_is_enabled(Debug, cds, heap)) {
iklam@52469 554 ResourceMark rm;
iklam@52469 555 log_debug(cds, heap)("(%d) %s[" SIZE_FORMAT "] ==> " PTR_FORMAT " size %d %s", _level,
iklam@52469 556 _orig_referencing_obj->klass()->external_name(), field_delta,
iklam@52469 557 p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name());
iklam@52469 558 LogTarget(Trace, cds, heap) log;
iklam@52469 559 LogStream out(log);
iklam@52469 560 obj->print_on(&out);
jiangli@51612 561 }
jiangli@51612 562
jiangli@53331 563 oop archived = HeapShared::archive_reachable_objects_from(
jiangli@53331 564 _level + 1, _subgraph_info, obj, _is_closed_archive, THREAD);
iklam@52469 565 assert(archived != NULL, "VM should have exited with unarchivable objects for _level > 1");
jiangli@52991 566 assert(HeapShared::is_archived_object(archived), "must be");
iklam@52469 567
iklam@52469 568 if (!_record_klasses_only) {
iklam@52469 569 // Update the reference in the archived copy of the referencing object.
iklam@52469 570 log_debug(cds, heap)("(%d) updating oop @[" PTR_FORMAT "] " PTR_FORMAT " ==> " PTR_FORMAT,
iklam@52469 571 _level, p2i(new_p), p2i(obj), p2i(archived));
iklam@52469 572 RawAccess<IS_NOT_NULL>::oop_store(new_p, archived);
jiangli@51612 573 }
jiangli@51612 574 }
jiangli@51612 575 }
jiangli@51612 576 };
jiangli@51612 577
jiangli@53331 578 void HeapShared::check_closed_archive_heap_region_object(InstanceKlass* k,
jiangli@53331 579 Thread* THREAD) {
jiangli@53331 580 // Check fields in the object
jiangli@53331 581 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
jiangli@53331 582 if (!fs.access_flags().is_static()) {
jiangli@53331 583 BasicType ft = fs.field_descriptor().field_type();
jiangli@53331 584 if (!fs.access_flags().is_final() && (ft == T_ARRAY || T_OBJECT)) {
jiangli@53331 585 ResourceMark rm(THREAD);
jiangli@53331 586 log_warning(cds, heap)(
jiangli@53331 587 "Please check reference field in %s instance in closed archive heap region: %s %s",
jiangli@53331 588 k->external_name(), (fs.name())->as_C_string(),
jiangli@53331 589 (fs.signature())->as_C_string());
jiangli@53331 590 }
jiangli@53331 591 }
jiangli@53331 592 }
jiangli@53331 593 }
jiangli@53331 594
iklam@52469 595 // (1) If orig_obj has not been archived yet, archive it.
iklam@52469 596 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
iklam@52469 597 // trace all objects that are reachable from it, and make sure these objects are archived.
iklam@52469 598 // (3) Record the klasses of all orig_obj and all reachable objects.
jiangli@53331 599 oop HeapShared::archive_reachable_objects_from(int level,
jiangli@53331 600 KlassSubGraphInfo* subgraph_info,
jiangli@53331 601 oop orig_obj,
jiangli@53331 602 bool is_closed_archive,
jiangli@53331 603 TRAPS) {
iklam@52469 604 assert(orig_obj != NULL, "must be");
jiangli@52991 605 assert(!is_archived_object(orig_obj), "sanity");
iklam@52469 606
iklam@52469 607 // java.lang.Class instances cannot be included in an archived
iklam@52469 608 // object sub-graph.
iklam@52469 609 if (java_lang_Class::is_instance(orig_obj)) {
iklam@52469 610 log_error(cds, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level);
iklam@52469 611 vm_exit(1);
iklam@52469 612 }
iklam@52469 613
jiangli@52991 614 oop archived_obj = find_archived_heap_object(orig_obj);
iklam@52469 615 if (java_lang_String::is_instance(orig_obj) && archived_obj != NULL) {
iklam@52469 616 // To save time, don't walk strings that are already archived. They just contain
iklam@52469 617 // pointers to a type array, whose klass doesn't need to be recorded.
iklam@52469 618 return archived_obj;
iklam@52469 619 }
iklam@52469 620
iklam@52469 621 if (has_been_seen_during_subgraph_recording(orig_obj)) {
iklam@52469 622 // orig_obj has already been archived and traced. Nothing more to do.
iklam@52469 623 return archived_obj;
iklam@52469 624 } else {
iklam@52469 625 set_has_been_seen_during_subgraph_recording(orig_obj);
iklam@52469 626 }
iklam@52469 627
iklam@52469 628 bool record_klasses_only = (archived_obj != NULL);
iklam@52469 629 if (archived_obj == NULL) {
iklam@52469 630 ++_num_new_archived_objs;
jiangli@52991 631 archived_obj = archive_heap_object(orig_obj, THREAD);
iklam@52469 632 if (archived_obj == NULL) {
iklam@52469 633 // Skip archiving the sub-graph referenced from the current entry field.
iklam@52469 634 ResourceMark rm;
iklam@52469 635 log_error(cds, heap)(
iklam@52469 636 "Cannot archive the sub-graph referenced from %s object ("
iklam@52469 637 PTR_FORMAT ") size %d, skipped.",
iklam@52469 638 orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize);
iklam@52469 639 if (level == 1) {
iklam@52469 640 // Don't archive a subgraph root that's too big. For archives static fields, that's OK
iklam@52469 641 // as the Java code will take care of initializing this field dynamically.
iklam@52469 642 return NULL;
iklam@52469 643 } else {
iklam@52469 644 // We don't know how to handle an object that has been archived, but some of its reachable
iklam@52469 645 // objects cannot be archived. Bail out for now. We might need to fix this in the future if
iklam@52469 646 // we have a real use case.
iklam@52469 647 vm_exit(1);
iklam@52469 648 }
iklam@52469 649 }
iklam@52469 650 }
iklam@52469 651
iklam@52469 652 assert(archived_obj != NULL, "must be");
iklam@52469 653 Klass *orig_k = orig_obj->klass();
iklam@52469 654 Klass *relocated_k = archived_obj->klass();
iklam@52469 655 subgraph_info->add_subgraph_object_klass(orig_k, relocated_k);
iklam@52469 656
jiangli@53331 657 WalkOopAndArchiveClosure walker(level, is_closed_archive, record_klasses_only,
jiangli@53331 658 subgraph_info, orig_obj, archived_obj, THREAD);
iklam@52469 659 orig_obj->oop_iterate(&walker);
jiangli@53331 660 if (is_closed_archive && orig_k->is_instance_klass()) {
jiangli@53331 661 check_closed_archive_heap_region_object(InstanceKlass::cast(orig_k), THREAD);
jiangli@53331 662 }
iklam@52469 663 return archived_obj;
iklam@52469 664 }
iklam@52469 665
jiangli@51612 666 //
jiangli@51612 667 // Start from the given static field in a java mirror and archive the
jiangli@51612 668 // complete sub-graph of java heap objects that are reached directly
jiangli@51612 669 // or indirectly from the starting object by following references.
iklam@52469 670 // Sub-graph archiving restrictions (current):
jiangli@51612 671 //
jiangli@51612 672 // - All classes of objects in the archived sub-graph (including the
jiangli@51612 673 // entry class) must be boot class only.
jiangli@51612 674 // - No java.lang.Class instance (java mirror) can be included inside
jiangli@51612 675 // an archived sub-graph. Mirror can only be the sub-graph entry object.
jiangli@51612 676 //
jiangli@51612 677 // The Java heap object sub-graph archiving process (see
jiangli@51612 678 // WalkOopAndArchiveClosure):
jiangli@51612 679 //
jiangli@51612 680 // 1) Java object sub-graph archiving starts from a given static field
jiangli@51612 681 // within a Class instance (java mirror). If the static field is a
jiangli@51612 682 // refererence field and points to a non-null java object, proceed to
jiangli@51612 683 // the next step.
jiangli@51612 684 //
jiangli@51612 685 // 2) Archives the referenced java object. If an archived copy of the
jiangli@51612 686 // current object already exists, updates the pointer in the archived
jiangli@51612 687 // copy of the referencing object to point to the current archived object.
jiangli@51612 688 // Otherwise, proceed to the next step.
jiangli@51612 689 //
jiangli@51612 690 // 3) Follows all references within the current java object and recursively
jiangli@51612 691 // archive the sub-graph of objects starting from each reference.
jiangli@51612 692 //
jiangli@51612 693 // 4) Updates the pointer in the archived copy of referencing object to
iklam@52469 694 // point to the current archived object.
jiangli@51612 695 //
jiangli@51612 696 // 5) The Klass of the current java object is added to the list of Klasses
jiangli@51612 697 // for loading and initialzing before any object in the archived graph can
jiangli@51612 698 // be accessed at runtime.
jiangli@51612 699 //
iklam@52469 700 void HeapShared::archive_reachable_objects_from_static_field(InstanceKlass *k,
iklam@52469 701 const char* klass_name,
jiangli@51612 702 int field_offset,
iklam@52469 703 const char* field_name,
jiangli@53331 704 bool is_closed_archive,
jiangli@51612 705 TRAPS) {
jiangli@51612 706 assert(DumpSharedSpaces, "dump time only");
iklam@52469 707 assert(k->is_shared_boot_class(), "must be boot class");
jiangli@51612 708
jiangli@51612 709 oop m = k->java_mirror();
jiangli@51612 710
iklam@52469 711 KlassSubGraphInfo* subgraph_info = get_subgraph_info(k);
iklam@52469 712 oop f = m->obj_field(field_offset);
jiangli@51612 713
iklam@52469 714 log_debug(cds, heap)("Start archiving from: %s::%s (" PTR_FORMAT ")", klass_name, field_name, p2i(f));
jiangli@51612 715
iklam@52469 716 if (!CompressedOops::is_null(f)) {
iklam@52469 717 if (log_is_enabled(Trace, cds, heap)) {
iklam@52469 718 LogTarget(Trace, cds, heap) log;
iklam@52469 719 LogStream out(log);
iklam@52469 720 f->print_on(&out);
iklam@52469 721 }
jiangli@51612 722
jiangli@53331 723 oop af = archive_reachable_objects_from(1, subgraph_info, f,
jiangli@53331 724 is_closed_archive, CHECK);
iklam@52469 725
iklam@52469 726 if (af == NULL) {
iklam@52469 727 log_error(cds, heap)("Archiving failed %s::%s (some reachable objects cannot be archived)",
iklam@52469 728 klass_name, field_name);
iklam@52469 729 } else {
iklam@52469 730 // Note: the field value is not preserved in the archived mirror.
jiangli@51612 731 // Record the field as a new subGraph entry point. The recorded
jiangli@51612 732 // information is restored from the archive at runtime.
jiangli@53331 733 subgraph_info->add_subgraph_entry_field(field_offset, af, is_closed_archive);
iklam@52469 734 log_info(cds, heap)("Archived field %s::%s => " PTR_FORMAT, klass_name, field_name, p2i(af));
jiangli@51612 735 }
jiangli@51612 736 } else {
iklam@52469 737 // The field contains null, we still need to record the entry point,
iklam@52469 738 // so it can be restored at runtime.
jiangli@53331 739 subgraph_info->add_subgraph_entry_field(field_offset, NULL, false);
jiangli@51612 740 }
jiangli@51612 741 }
jiangli@51612 742
iklam@52469 743 #ifndef PRODUCT
iklam@52469 744 class VerifySharedOopClosure: public BasicOopIterateClosure {
iklam@52469 745 private:
iklam@52469 746 bool _is_archived;
iklam@52469 747
iklam@52469 748 public:
iklam@52469 749 VerifySharedOopClosure(bool is_archived) : _is_archived(is_archived) {}
iklam@52469 750
iklam@52469 751 void do_oop(narrowOop *p) { VerifySharedOopClosure::do_oop_work(p); }
iklam@52469 752 void do_oop( oop *p) { VerifySharedOopClosure::do_oop_work(p); }
iklam@52469 753
iklam@52469 754 protected:
iklam@52469 755 template <class T> void do_oop_work(T *p) {
iklam@52469 756 oop obj = RawAccess<>::oop_load(p);
iklam@52469 757 if (!CompressedOops::is_null(obj)) {
iklam@52469 758 HeapShared::verify_reachable_objects_from(obj, _is_archived);
iklam@52469 759 }
iklam@52469 760 }
iklam@52469 761 };
iklam@52469 762
iklam@52469 763 void HeapShared::verify_subgraph_from_static_field(InstanceKlass* k, int field_offset) {
iklam@52469 764 assert(DumpSharedSpaces, "dump time only");
iklam@52469 765 assert(k->is_shared_boot_class(), "must be boot class");
iklam@52469 766
iklam@52469 767 oop m = k->java_mirror();
iklam@52469 768 oop f = m->obj_field(field_offset);
iklam@52469 769 if (!CompressedOops::is_null(f)) {
iklam@52469 770 verify_subgraph_from(f);
iklam@52469 771 }
iklam@52469 772 }
iklam@52469 773
iklam@52469 774 void HeapShared::verify_subgraph_from(oop orig_obj) {
jiangli@52991 775 oop archived_obj = find_archived_heap_object(orig_obj);
iklam@52469 776 if (archived_obj == NULL) {
iklam@52469 777 // It's OK for the root of a subgraph to be not archived. See comments in
iklam@52469 778 // archive_reachable_objects_from().
iklam@52469 779 return;
iklam@52469 780 }
iklam@52469 781
iklam@52469 782 // Verify that all objects reachable from orig_obj are archived.
iklam@52469 783 init_seen_objects_table();
iklam@52469 784 verify_reachable_objects_from(orig_obj, false);
iklam@52469 785 delete_seen_objects_table();
iklam@52469 786
iklam@52469 787 // Note: we could also verify that all objects reachable from the archived
iklam@52469 788 // copy of orig_obj can only point to archived objects, with:
iklam@52469 789 // init_seen_objects_table();
iklam@52469 790 // verify_reachable_objects_from(archived_obj, true);
iklam@52469 791 // init_seen_objects_table();
iklam@52469 792 // but that's already done in G1HeapVerifier::verify_archive_regions so we
iklam@52469 793 // won't do it here.
iklam@52469 794 }
iklam@52469 795
iklam@52469 796 void HeapShared::verify_reachable_objects_from(oop obj, bool is_archived) {
iklam@52469 797 _num_total_verifications ++;
iklam@52469 798 if (!has_been_seen_during_subgraph_recording(obj)) {
iklam@52469 799 set_has_been_seen_during_subgraph_recording(obj);
iklam@52469 800
iklam@52469 801 if (is_archived) {
jiangli@52991 802 assert(is_archived_object(obj), "must be");
jiangli@52991 803 assert(find_archived_heap_object(obj) == NULL, "must be");
iklam@52469 804 } else {
jiangli@52991 805 assert(!is_archived_object(obj), "must be");
jiangli@52991 806 assert(find_archived_heap_object(obj) != NULL, "must be");
iklam@52469 807 }
iklam@52469 808
iklam@52469 809 VerifySharedOopClosure walker(is_archived);
iklam@52469 810 obj->oop_iterate(&walker);
iklam@52469 811 }
iklam@52469 812 }
iklam@52469 813 #endif
iklam@52469 814
iklam@52469 815 HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = NULL;
iklam@52469 816 int HeapShared::_num_new_walked_objs;
iklam@52469 817 int HeapShared::_num_new_archived_objs;
iklam@52469 818 int HeapShared::_num_old_recorded_klasses;
iklam@52469 819
iklam@52469 820 int HeapShared::_num_total_subgraph_recordings = 0;
iklam@52469 821 int HeapShared::_num_total_walked_objs = 0;
iklam@52469 822 int HeapShared::_num_total_archived_objs = 0;
iklam@52469 823 int HeapShared::_num_total_recorded_klasses = 0;
iklam@52469 824 int HeapShared::_num_total_verifications = 0;
iklam@52469 825
iklam@52469 826 bool HeapShared::has_been_seen_during_subgraph_recording(oop obj) {
iklam@52469 827 return _seen_objects_table->get(obj) != NULL;
iklam@52469 828 }
iklam@52469 829
iklam@52469 830 void HeapShared::set_has_been_seen_during_subgraph_recording(oop obj) {
iklam@52469 831 assert(!has_been_seen_during_subgraph_recording(obj), "sanity");
iklam@52469 832 _seen_objects_table->put(obj, true);
iklam@52469 833 ++ _num_new_walked_objs;
iklam@52469 834 }
iklam@52469 835
iklam@52469 836 void HeapShared::start_recording_subgraph(InstanceKlass *k, const char* class_name) {
iklam@52469 837 log_info(cds, heap)("Start recording subgraph(s) for archived fields in %s", class_name);
iklam@52469 838 init_seen_objects_table();
iklam@52469 839 _num_new_walked_objs = 0;
iklam@52469 840 _num_new_archived_objs = 0;
iklam@52469 841 _num_old_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses();
iklam@52469 842 }
iklam@52469 843
iklam@52469 844 void HeapShared::done_recording_subgraph(InstanceKlass *k, const char* class_name) {
iklam@52469 845 int num_new_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses() -
iklam@52469 846 _num_old_recorded_klasses;
iklam@52469 847 log_info(cds, heap)("Done recording subgraph(s) for archived fields in %s: "
iklam@52469 848 "walked %d objs, archived %d new objs, recorded %d classes",
iklam@52469 849 class_name, _num_new_walked_objs, _num_new_archived_objs,
iklam@52469 850 num_new_recorded_klasses);
iklam@52469 851
iklam@52469 852 delete_seen_objects_table();
iklam@52469 853
iklam@52469 854 _num_total_subgraph_recordings ++;
iklam@52469 855 _num_total_walked_objs += _num_new_walked_objs;
iklam@52469 856 _num_total_archived_objs += _num_new_archived_objs;
iklam@52469 857 _num_total_recorded_klasses += num_new_recorded_klasses;
iklam@52469 858 }
iklam@52469 859
iklam@52155 860 class ArchivableStaticFieldFinder: public FieldClosure {
iklam@52155 861 InstanceKlass* _ik;
iklam@52155 862 Symbol* _field_name;
iklam@52155 863 bool _found;
iklam@52155 864 int _offset;
iklam@52155 865 public:
iklam@52155 866 ArchivableStaticFieldFinder(InstanceKlass* ik, Symbol* field_name) :
iklam@52469 867 _ik(ik), _field_name(field_name), _found(false), _offset(-1) {}
iklam@52155 868
iklam@52155 869 virtual void do_field(fieldDescriptor* fd) {
iklam@52155 870 if (fd->name() == _field_name) {
iklam@52155 871 assert(!_found, "fields cannot be overloaded");
iklam@52469 872 assert(fd->field_type() == T_OBJECT || fd->field_type() == T_ARRAY, "can archive only obj or array fields");
iklam@52155 873 _found = true;
iklam@52155 874 _offset = fd->offset();
iklam@52155 875 }
iklam@52155 876 }
iklam@52155 877 bool found() { return _found; }
iklam@52155 878 int offset() { return _offset; }
iklam@52155 879 };
iklam@52155 880
jiangli@53331 881 void HeapShared::init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[],
jiangli@53331 882 int num, Thread* THREAD) {
jiangli@53331 883 for (int i = 0; i < num; i++) {
jiangli@53331 884 ArchivableStaticFieldInfo* info = &fields[i];
iklam@52469 885 TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name, THREAD);
iklam@52155 886 TempNewSymbol field_name = SymbolTable::new_symbol(info->field_name, THREAD);
iklam@52155 887
iklam@52469 888 Klass* k = SystemDictionary::resolve_or_null(klass_name, THREAD);
iklam@52155 889 assert(k != NULL && !HAS_PENDING_EXCEPTION, "class must exist");
iklam@52155 890 InstanceKlass* ik = InstanceKlass::cast(k);
jiangli@53413 891 assert(InstanceKlass::cast(ik)->is_shared_boot_class(),
jiangli@53413 892 "Only support boot classes");
jiangli@53413 893 ik->initialize(THREAD);
jiangli@53413 894 guarantee(!HAS_PENDING_EXCEPTION, "exception in initialize");
iklam@52155 895
iklam@52155 896 ArchivableStaticFieldFinder finder(ik, field_name);
iklam@52155 897 ik->do_local_static_fields(&finder);
iklam@52155 898 assert(finder.found(), "field must exist");
iklam@52155 899
iklam@52155 900 info->klass = ik;
iklam@52155 901 info->offset = finder.offset();
iklam@52155 902 }
iklam@52155 903 }
jiangli@51612 904
jiangli@53331 905 void HeapShared::init_subgraph_entry_fields(Thread* THREAD) {
jiangli@53331 906 _dump_time_subgraph_info_table = new (ResourceObj::C_HEAP, mtClass)DumpTimeKlassSubGraphInfoTable();
jiangli@53331 907
jiangli@53331 908 init_subgraph_entry_fields(closed_archive_subgraph_entry_fields,
jiangli@53331 909 num_closed_archive_subgraph_entry_fields,
jiangli@53331 910 THREAD);
jiangli@53331 911 init_subgraph_entry_fields(open_archive_subgraph_entry_fields,
jiangli@53331 912 num_open_archive_subgraph_entry_fields,
jiangli@53331 913 THREAD);
jiangli@53331 914 }
jiangli@53331 915
jiangli@53331 916 void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
jiangli@53331 917 int num, bool is_closed_archive,
jiangli@53331 918 Thread* THREAD) {
jiangli@53331 919 _num_total_subgraph_recordings = 0;
jiangli@53331 920 _num_total_walked_objs = 0;
jiangli@53331 921 _num_total_archived_objs = 0;
jiangli@53331 922 _num_total_recorded_klasses = 0;
jiangli@53331 923 _num_total_verifications = 0;
jiangli@53331 924
iklam@52469 925 // For each class X that has one or more archived fields:
iklam@52469 926 // [1] Dump the subgraph of each archived field
iklam@52469 927 // [2] Create a list of all the class of the objects that can be reached
iklam@52469 928 // by any of these static fields.
iklam@52469 929 // At runtime, these classes are initialized before X's archived fields
iklam@52469 930 // are restored by HeapShared::initialize_from_archived_subgraph().
iklam@52469 931 int i;
jiangli@53331 932 for (i = 0; i < num; ) {
jiangli@53331 933 ArchivableStaticFieldInfo* info = &fields[i];
iklam@52469 934 const char* klass_name = info->klass_name;
iklam@52469 935 start_recording_subgraph(info->klass, klass_name);
iklam@52469 936
iklam@52469 937 // If you have specified consecutive fields of the same klass in
jiangli@53331 938 // fields[], these will be archived in the same
iklam@52469 939 // {start_recording_subgraph ... done_recording_subgraph} pass to
iklam@52469 940 // save time.
jiangli@53331 941 for (; i < num; i++) {
jiangli@53331 942 ArchivableStaticFieldInfo* f = &fields[i];
iklam@52469 943 if (f->klass_name != klass_name) {
iklam@52469 944 break;
iklam@52469 945 }
iklam@52469 946 archive_reachable_objects_from_static_field(f->klass, f->klass_name,
jiangli@53331 947 f->offset, f->field_name,
jiangli@53331 948 is_closed_archive, CHECK);
iklam@52469 949 }
iklam@52469 950 done_recording_subgraph(info->klass, klass_name);
iklam@52469 951 }
iklam@52469 952
jiangli@53331 953 log_info(cds, heap)("Archived subgraph records in %s archive heap region = %d",
jiangli@53331 954 is_closed_archive ? "closed" : "open",
jiangli@53331 955 _num_total_subgraph_recordings);
jiangli@53331 956 log_info(cds, heap)(" Walked %d objects", _num_total_walked_objs);
jiangli@53331 957 log_info(cds, heap)(" Archived %d objects", _num_total_archived_objs);
jiangli@53331 958 log_info(cds, heap)(" Recorded %d klasses", _num_total_recorded_klasses);
iklam@52469 959
iklam@52469 960 #ifndef PRODUCT
jiangli@53331 961 for (int i = 0; i < num; i++) {
jiangli@53331 962 ArchivableStaticFieldInfo* f = &fields[i];
iklam@52469 963 verify_subgraph_from_static_field(f->klass, f->offset);
iklam@52155 964 }
jiangli@53331 965 log_info(cds, heap)(" Verified %d references", _num_total_verifications);
iklam@52469 966 #endif
jiangli@51612 967 }
iklam@52217 968
iklam@52217 969 // At dump-time, find the location of all the non-null oop pointers in an archived heap
iklam@52217 970 // region. This way we can quickly relocate all the pointers without using
iklam@52217 971 // BasicOopIterateClosure at runtime.
iklam@52217 972 class FindEmbeddedNonNullPointers: public BasicOopIterateClosure {
iklam@52217 973 narrowOop* _start;
iklam@52217 974 BitMap *_oopmap;
iklam@52217 975 int _num_total_oops;
iklam@52217 976 int _num_null_oops;
iklam@52217 977 public:
iklam@52217 978 FindEmbeddedNonNullPointers(narrowOop* start, BitMap* oopmap)
iklam@52217 979 : _start(start), _oopmap(oopmap), _num_total_oops(0), _num_null_oops(0) {}
iklam@52217 980
iklam@52217 981 virtual bool should_verify_oops(void) {
iklam@52217 982 return false;
iklam@52217 983 }
iklam@52217 984 virtual void do_oop(narrowOop* p) {
iklam@52217 985 _num_total_oops ++;
iklam@52217 986 narrowOop v = *p;
iklam@52217 987 if (!CompressedOops::is_null(v)) {
iklam@52217 988 size_t idx = p - _start;
iklam@52217 989 _oopmap->set_bit(idx);
iklam@52217 990 } else {
iklam@52217 991 _num_null_oops ++;
iklam@52217 992 }
iklam@52217 993 }
iklam@52217 994 virtual void do_oop(oop *p) {
iklam@52217 995 ShouldNotReachHere();
iklam@52217 996 }
iklam@52217 997 int num_total_oops() const { return _num_total_oops; }
iklam@52217 998 int num_null_oops() const { return _num_null_oops; }
iklam@52217 999 };
iklam@52217 1000
iklam@52217 1001 ResourceBitMap HeapShared::calculate_oopmap(MemRegion region) {
iklam@52217 1002 assert(UseCompressedOops, "must be");
iklam@52217 1003 size_t num_bits = region.byte_size() / sizeof(narrowOop);
iklam@52217 1004 ResourceBitMap oopmap(num_bits);
iklam@52217 1005
iklam@52217 1006 HeapWord* p = region.start();
iklam@52217 1007 HeapWord* end = region.end();
iklam@52217 1008 FindEmbeddedNonNullPointers finder((narrowOop*)p, &oopmap);
iklam@52217 1009
iklam@52217 1010 int num_objs = 0;
iklam@52217 1011 while (p < end) {
iklam@52217 1012 oop o = (oop)p;
iklam@52217 1013 o->oop_iterate(&finder);
iklam@52217 1014 p += o->size();
iklam@52217 1015 ++ num_objs;
iklam@52217 1016 }
iklam@52217 1017
iklam@52217 1018 log_info(cds, heap)("calculate_oopmap: objects = %6d, embedded oops = %7d, nulls = %7d",
iklam@52217 1019 num_objs, finder.num_total_oops(), finder.num_null_oops());
iklam@52217 1020 return oopmap;
iklam@52217 1021 }
iklam@52217 1022
iklam@52217 1023 // Patch all the embedded oop pointers inside an archived heap region,
iklam@52217 1024 // to be consistent with the runtime oop encoding.
iklam@52217 1025 class PatchEmbeddedPointers: public BitMapClosure {
iklam@52217 1026 narrowOop* _start;
iklam@52217 1027
iklam@52217 1028 public:
iklam@52217 1029 PatchEmbeddedPointers(narrowOop* start) : _start(start) {}
iklam@52217 1030
iklam@52217 1031 bool do_bit(size_t offset) {
iklam@52217 1032 narrowOop* p = _start + offset;
iklam@52217 1033 narrowOop v = *p;
iklam@52217 1034 assert(!CompressedOops::is_null(v), "null oops should have been filtered out at dump time");
iklam@52488 1035 oop o = HeapShared::decode_from_archive(v);
iklam@52217 1036 RawAccess<IS_NOT_NULL>::oop_store(p, o);
iklam@52217 1037 return true;
iklam@52217 1038 }
iklam@52217 1039 };
iklam@52217 1040
iklam@52217 1041 void HeapShared::patch_archived_heap_embedded_pointers(MemRegion region, address oopmap,
iklam@52217 1042 size_t oopmap_size_in_bits) {
iklam@52217 1043 BitMapView bm((BitMap::bm_word_t*)oopmap, oopmap_size_in_bits);
iklam@52217 1044
iklam@52217 1045 #ifndef PRODUCT
iklam@52217 1046 ResourceMark rm;
iklam@52217 1047 ResourceBitMap checkBm = calculate_oopmap(region);
iklam@52217 1048 assert(bm.is_same(checkBm), "sanity");
iklam@52217 1049 #endif
iklam@52217 1050
iklam@52217 1051 PatchEmbeddedPointers patcher((narrowOop*)region.start());
iklam@52217 1052 bm.iterate(&patcher);
iklam@52217 1053 }
iklam@52217 1054
jiangli@51612 1055 #endif // INCLUDE_CDS_JAVA_HEAP