1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/javaClasses.inline.hpp" 27 #include "classfile/vmSymbols.hpp" 28 #include "logging/log.hpp" 29 #include "logging/logMessage.hpp" 30 #include "logging/logStream.hpp" 31 #include "memory/heapShared.hpp" 32 #include "memory/iterator.inline.hpp" 33 #include "memory/metadataFactory.hpp" 34 #include "memory/metaspaceClosure.hpp" 35 #include "memory/metaspaceShared.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "oops/compressedOops.inline.hpp" 38 #include "oops/oop.inline.hpp" 39 40 #if INCLUDE_CDS_JAVA_HEAP 41 KlassSubGraphInfo* HeapShared::_subgraph_info_list = NULL; 42 int HeapShared::_num_archived_subgraph_info_records = 0; 43 Array<ArchivedKlassSubGraphInfoRecord>* HeapShared::_archived_subgraph_info_records = NULL; 44 45 // Currently there is only one class mirror (ArchivedModuleGraph) with archived 46 // sub-graphs. 47 KlassSubGraphInfo* HeapShared::find_subgraph_info(Klass* k) { 48 KlassSubGraphInfo* info = _subgraph_info_list; 49 while (info != NULL) { 50 if (info->klass() == k) { 51 return info; 52 } 53 info = info->next(); 54 } 55 return NULL; 56 } 57 58 // Get the subgraph_info for Klass k. A new subgraph_info is created if 59 // there is no existing one for k. The subgraph_info records the relocated 60 // Klass* of the original k. 61 KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) { 62 Klass* relocated_k = MetaspaceShared::get_relocated_klass(k); 63 KlassSubGraphInfo* info = find_subgraph_info(relocated_k); 64 if (info != NULL) { 65 return info; 66 } 67 68 info = new KlassSubGraphInfo(relocated_k, _subgraph_info_list); 69 _subgraph_info_list = info; 70 return info; 71 } 72 73 int HeapShared::num_of_subgraph_infos() { 74 int num = 0; 75 KlassSubGraphInfo* info = _subgraph_info_list; 76 while (info != NULL) { 77 num ++; 78 info = info->next(); 79 } 80 return num; 81 } 82 83 // Add an entry field to the current KlassSubGraphInfo. 84 void KlassSubGraphInfo::add_subgraph_entry_field(int static_field_offset, oop v) { 85 assert(DumpSharedSpaces, "dump time only"); 86 if (_subgraph_entry_fields == NULL) { 87 _subgraph_entry_fields = 88 new(ResourceObj::C_HEAP, mtClass) GrowableArray<juint>(10, true); 89 } 90 _subgraph_entry_fields->append((juint)static_field_offset); 91 _subgraph_entry_fields->append(CompressedOops::encode(v)); 92 } 93 94 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs. 95 // Only objects of boot classes can be included in sub-graph. 96 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k, Klass *relocated_k) { 97 assert(DumpSharedSpaces, "dump time only"); 98 assert(relocated_k == MetaspaceShared::get_relocated_klass(orig_k), 99 "must be the relocated Klass in the shared space"); 100 101 if (_subgraph_object_klasses == NULL) { 102 _subgraph_object_klasses = 103 new(ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(50, true); 104 } 105 106 assert(relocated_k->is_shared(), "must be a shared class"); 107 108 if (_k == relocated_k) { 109 // Don't add the Klass containing the sub-graph to it's own klass 110 // initialization list. 111 return; 112 } 113 114 if (relocated_k->is_instance_klass()) { 115 assert(InstanceKlass::cast(relocated_k)->is_shared_boot_class(), 116 "must be boot class"); 117 // SystemDictionary::xxx_klass() are not updated, need to check 118 // the original Klass* 119 if (orig_k == SystemDictionary::String_klass() || 120 orig_k == SystemDictionary::Object_klass()) { 121 // Initialized early during VM initialization. No need to be added 122 // to the sub-graph object class list. 123 return; 124 } 125 } else if (relocated_k->is_objArray_klass()) { 126 Klass* abk = ObjArrayKlass::cast(relocated_k)->bottom_klass(); 127 if (abk->is_instance_klass()) { 128 assert(InstanceKlass::cast(abk)->is_shared_boot_class(), 129 "must be boot class"); 130 } 131 if (relocated_k == Universe::objectArrayKlassObj()) { 132 // Initialized early during Universe::genesis. No need to be added 133 // to the list. 134 return; 135 } 136 } else { 137 assert(relocated_k->is_typeArray_klass(), "must be"); 138 // Primitive type arrays are created early during Universe::genesis. 139 return; 140 } 141 142 _subgraph_object_klasses->append_if_missing(relocated_k); 143 } 144 145 // Initialize an archived subgraph_info_record from the given KlassSubGraphInfo. 146 void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) { 147 _k = info->klass(); 148 _next = NULL; 149 _entry_field_records = NULL; 150 _subgraph_klasses = NULL; 151 152 // populate the entry fields 153 GrowableArray<juint>* entry_fields = info->subgraph_entry_fields(); 154 if (entry_fields != NULL) { 155 int num_entry_fields = entry_fields->length(); 156 assert(num_entry_fields % 2 == 0, "sanity"); 157 _entry_field_records = 158 MetaspaceShared::new_ro_array<juint>(num_entry_fields); 159 for (int i = 0 ; i < num_entry_fields; i++) { 160 _entry_field_records->at_put(i, entry_fields->at(i)); 161 } 162 } 163 164 // the Klasses of the objects in the sub-graphs 165 GrowableArray<Klass*>* subgraph_klasses = info->subgraph_object_klasses(); 166 if (subgraph_klasses != NULL) { 167 int num_subgraphs_klasses = subgraph_klasses->length(); 168 _subgraph_klasses = 169 MetaspaceShared::new_ro_array<Klass*>(num_subgraphs_klasses); 170 for (int i = 0; i < num_subgraphs_klasses; i++) { 171 Klass* subgraph_k = subgraph_klasses->at(i); 172 if (log_is_enabled(Info, cds, heap)) { 173 ResourceMark rm; 174 log_info(cds, heap)( 175 "Archived object klass (%d): %s in %s sub-graphs", 176 i, subgraph_k->external_name(), _k->external_name()); 177 } 178 _subgraph_klasses->at_put(i, subgraph_k); 179 } 180 } 181 } 182 183 // Build the records of archived subgraph infos, which include: 184 // - Entry points to all subgraphs from the containing class mirror. The entry 185 // points are static fields in the mirror. For each entry point, the field 186 // offset and value are recorded in the sub-graph info. The value are stored 187 // back to the corresponding field at runtime. 188 // - A list of klasses that need to be loaded/initialized before archived 189 // java object sub-graph can be accessed at runtime. 190 // 191 // The records are saved in the archive file and reloaded at runtime. Currently 192 // there is only one class mirror (ArchivedModuleGraph) with archived sub-graphs. 193 // 194 // Layout of the archived subgraph info records: 195 // 196 // records_size | num_records | records* 197 // ArchivedKlassSubGraphInfoRecord | entry_fields | subgraph_object_klasses 198 size_t HeapShared::build_archived_subgraph_info_records(int num_records) { 199 // remember the start address 200 char* start_p = MetaspaceShared::read_only_space_top(); 201 202 // now populate the archived subgraph infos, which will be saved in the 203 // archive file 204 _archived_subgraph_info_records = 205 MetaspaceShared::new_ro_array<ArchivedKlassSubGraphInfoRecord>(num_records); 206 KlassSubGraphInfo* info = _subgraph_info_list; 207 int i = 0; 208 while (info != NULL) { 209 assert(i < _archived_subgraph_info_records->length(), "sanity"); 210 ArchivedKlassSubGraphInfoRecord* record = 211 _archived_subgraph_info_records->adr_at(i); 212 record->init(info); 213 info = info->next(); 214 i ++; 215 } 216 217 // _subgraph_info_list is no longer needed 218 delete _subgraph_info_list; 219 _subgraph_info_list = NULL; 220 221 char* end_p = MetaspaceShared::read_only_space_top(); 222 size_t records_size = end_p - start_p; 223 return records_size; 224 } 225 226 // Write the subgraph info records in the shared _ro region 227 void HeapShared::write_archived_subgraph_infos() { 228 assert(DumpSharedSpaces, "dump time only"); 229 230 Array<intptr_t>* records_header = MetaspaceShared::new_ro_array<intptr_t>(3); 231 232 _num_archived_subgraph_info_records = num_of_subgraph_infos(); 233 size_t records_size = build_archived_subgraph_info_records( 234 _num_archived_subgraph_info_records); 235 236 // Now write the header information: 237 // records_size, num_records, _archived_subgraph_info_records 238 assert(records_header != NULL, "sanity"); 239 intptr_t* p = (intptr_t*)(records_header->data()); 240 *p = (intptr_t)records_size; 241 p ++; 242 *p = (intptr_t)_num_archived_subgraph_info_records; 243 p ++; 244 *p = (intptr_t)_archived_subgraph_info_records; 245 } 246 247 char* HeapShared::read_archived_subgraph_infos(char* buffer) { 248 Array<intptr_t>* records_header = (Array<intptr_t>*)buffer; 249 intptr_t* p = (intptr_t*)(records_header->data()); 250 size_t records_size = (size_t)(*p); 251 p ++; 252 _num_archived_subgraph_info_records = *p; 253 p ++; 254 _archived_subgraph_info_records = 255 (Array<ArchivedKlassSubGraphInfoRecord>*)(*p); 256 257 buffer = (char*)_archived_subgraph_info_records + records_size; 258 return buffer; 259 } 260 261 void HeapShared::initialize_from_archived_subgraph(Klass* k) { 262 if (!MetaspaceShared::open_archive_heap_region_mapped()) { 263 return; // nothing to do 264 } 265 266 if (_num_archived_subgraph_info_records == 0) { 267 return; // no subgraph info records 268 } 269 270 // Initialize from archived data. Currently only ArchivedModuleGraph 271 // has archived object subgraphs, which is used during VM initialization 272 // time when bootstraping the system modules. No lock is needed. 273 Thread* THREAD = Thread::current(); 274 for (int i = 0; i < _archived_subgraph_info_records->length(); i++) { 275 ArchivedKlassSubGraphInfoRecord* record = _archived_subgraph_info_records->adr_at(i); 276 if (record->klass() == k) { 277 int i; 278 // Found the archived subgraph info record for the requesting klass. 279 // Load/link/initialize the klasses of the objects in the subgraph. 280 // NULL class loader is used. 281 Array<Klass*>* klasses = record->subgraph_klasses(); 282 if (klasses != NULL) { 283 for (i = 0; i < klasses->length(); i++) { 284 Klass* obj_k = klasses->at(i); 285 Klass* resolved_k = SystemDictionary::resolve_or_null( 286 (obj_k)->name(), THREAD); 287 if (resolved_k != obj_k) { 288 return; 289 } 290 if ((obj_k)->is_instance_klass()) { 291 InstanceKlass* ik = InstanceKlass::cast(obj_k); 292 ik->initialize(THREAD); 293 } else if ((obj_k)->is_objArray_klass()) { 294 ObjArrayKlass* oak = ObjArrayKlass::cast(obj_k); 295 oak->initialize(THREAD); 296 } 297 } 298 } 299 300 if (HAS_PENDING_EXCEPTION) { 301 CLEAR_PENDING_EXCEPTION; 302 // None of the field value will be set if there was an exception. 303 // The java code will not see any of the archived objects in the 304 // subgraphs referenced from k in this case. 305 return; 306 } 307 308 // Load the subgraph entry fields from the record and store them back to 309 // the corresponding fields within the mirror. 310 oop m = k->java_mirror(); 311 Array<juint>* entry_field_records = record->entry_field_records(); 312 if (entry_field_records != NULL) { 313 int efr_len = entry_field_records->length(); 314 assert(efr_len % 2 == 0, "sanity"); 315 for (i = 0; i < efr_len;) { 316 int field_offset = entry_field_records->at(i); 317 // The object refereced by the field becomes 'known' by GC from this 318 // point. All objects in the subgraph reachable from the object are 319 // also 'known' by GC. 320 oop v = MetaspaceShared::materialize_archived_object( 321 CompressedOops::decode(entry_field_records->at(i+1))); 322 m->obj_field_put(field_offset, v); 323 i += 2; 324 } 325 } 326 327 // Done. Java code can see the archived sub-graphs referenced from k's 328 // mirror after this point. 329 return; 330 } 331 } 332 } 333 334 class WalkOopAndArchiveClosure: public BasicOopIterateClosure { 335 int _level; 336 KlassSubGraphInfo* _subgraph_info; 337 oop _orig_referencing_obj; 338 oop _archived_referencing_obj; 339 public: 340 WalkOopAndArchiveClosure(int level, KlassSubGraphInfo* subgraph_info, 341 oop orig, oop archived) : _level(level), 342 _subgraph_info(subgraph_info), 343 _orig_referencing_obj(orig), 344 _archived_referencing_obj(archived) {} 345 void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); } 346 void do_oop( oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); } 347 348 protected: 349 template <class T> void do_oop_work(T *p) { 350 oop obj = RawAccess<>::oop_load(p); 351 if (!CompressedOops::is_null(obj)) { 352 // A java.lang.Class instance can not be included in an archived 353 // object sub-graph. 354 if (java_lang_Class::is_instance(obj)) { 355 tty->print("Unknown java.lang.Class object is in the archived sub-graph\n"); 356 vm_exit(1); 357 } 358 359 LogTarget(Debug, cds, heap) log; 360 LogStream ls(log); 361 outputStream* out = &ls; 362 { 363 ResourceMark rm; 364 log.print("(%d) %s <--- referenced from: %s", 365 _level, obj->klass()->external_name(), 366 CompressedOops::is_null(_orig_referencing_obj) ? 367 "" : _orig_referencing_obj->klass()->external_name()); 368 obj->print_on(out); 369 } 370 371 if (MetaspaceShared::is_archive_object(obj)) { 372 // The current oop is an archived oop, nothing needs to be done 373 log.print("--- object is already archived ---"); 374 return; 375 } 376 377 size_t field_delta = pointer_delta( 378 p, _orig_referencing_obj, sizeof(char)); 379 T* new_p = (T*)(address(_archived_referencing_obj) + field_delta); 380 oop archived = MetaspaceShared::find_archived_heap_object(obj); 381 if (archived != NULL) { 382 // There is an archived copy existing, update reference to point 383 // to the archived copy 384 RawAccess<IS_NOT_NULL>::oop_store(new_p, archived); 385 log.print( 386 "--- found existing archived copy, store archived " PTR_FORMAT " in " PTR_FORMAT, 387 p2i(archived), p2i(new_p)); 388 return; 389 } 390 391 int l = _level + 1; 392 Thread* THREAD = Thread::current(); 393 // Archive the current oop before iterating through its references 394 archived = MetaspaceShared::archive_heap_object(obj, THREAD); 395 assert(MetaspaceShared::is_archive_object(archived), "must be archived"); 396 log.print("=== archiving oop " PTR_FORMAT " ==> " PTR_FORMAT, 397 p2i(obj), p2i(archived)); 398 399 // Following the references in the current oop and archive any 400 // encountered objects during the process 401 WalkOopAndArchiveClosure walker(l, _subgraph_info, obj, archived); 402 obj->oop_iterate(&walker); 403 404 // Update the reference in the archived copy of the referencing object 405 RawAccess<IS_NOT_NULL>::oop_store(new_p, archived); 406 log.print("=== store archived " PTR_FORMAT " in " PTR_FORMAT, 407 p2i(archived), p2i(new_p)); 408 409 // Add the klass to the list of classes that need to be loaded before 410 // module system initialization 411 Klass *orig_k = obj->klass(); 412 Klass *relocated_k = archived->klass(); 413 _subgraph_info->add_subgraph_object_klass(orig_k, relocated_k); 414 } 415 } 416 }; 417 418 // 419 // Start from the given static field in a java mirror and archive the 420 // complete sub-graph of java heap objects that are reached directly 421 // or indirectly from the starting object by following references. 422 // Currently, only ArchivedModuleGraph class instance (mirror) has archived 423 // object subgraphs. Sub-graph archiving restrictions (current): 424 // 425 // - All classes of objects in the archived sub-graph (including the 426 // entry class) must be boot class only. 427 // - No java.lang.Class instance (java mirror) can be included inside 428 // an archived sub-graph. Mirror can only be the sub-graph entry object. 429 // 430 // The Java heap object sub-graph archiving process (see 431 // WalkOopAndArchiveClosure): 432 // 433 // 1) Java object sub-graph archiving starts from a given static field 434 // within a Class instance (java mirror). If the static field is a 435 // refererence field and points to a non-null java object, proceed to 436 // the next step. 437 // 438 // 2) Archives the referenced java object. If an archived copy of the 439 // current object already exists, updates the pointer in the archived 440 // copy of the referencing object to point to the current archived object. 441 // Otherwise, proceed to the next step. 442 // 443 // 3) Follows all references within the current java object and recursively 444 // archive the sub-graph of objects starting from each reference. 445 // 446 // 4) Updates the pointer in the archived copy of referencing object to 447 // point to the current archived object. 448 // 449 // 5) The Klass of the current java object is added to the list of Klasses 450 // for loading and initialzing before any object in the archived graph can 451 // be accessed at runtime. 452 // 453 void HeapShared::archive_reachable_objects_from_static_field(Klass *k, 454 int field_offset, 455 BasicType field_type, 456 TRAPS) { 457 assert(DumpSharedSpaces, "dump time only"); 458 assert(k->is_instance_klass(), "sanity"); 459 assert(InstanceKlass::cast(k)->is_shared_boot_class(), 460 "must be boot class"); 461 462 oop m = k->java_mirror(); 463 oop archived_m = MetaspaceShared::find_archived_heap_object(m); 464 if (CompressedOops::is_null(archived_m)) { 465 return; 466 } 467 468 if (field_type == T_OBJECT) { 469 // obtain k's subGraph Info 470 KlassSubGraphInfo* subgraph_info = get_subgraph_info(k); 471 472 // get the object referenced by the field 473 oop f = m->obj_field(field_offset); 474 if (!CompressedOops::is_null(f)) { 475 LogTarget(Debug, cds, heap) log; 476 LogStream ls(log); 477 outputStream* out = &ls; 478 log.print("Start from: "); 479 f->print_on(out); 480 481 // get the archived copy of the field referenced object 482 oop af = MetaspaceShared::archive_heap_object(f, THREAD); 483 if (!MetaspaceShared::is_archive_object(f)) { 484 WalkOopAndArchiveClosure walker(1, subgraph_info, f, af); 485 f->oop_iterate(&walker); 486 } 487 488 // The field value is not preserved in the archived mirror. 489 // Record the field as a new subGraph entry point. The recorded 490 // information is restored from the archive at runtime. 491 subgraph_info->add_subgraph_entry_field(field_offset, af); 492 Klass *relocated_k = af->klass(); 493 Klass *orig_k = f->klass(); 494 subgraph_info->add_subgraph_object_klass(orig_k, relocated_k); 495 } else { 496 // The field contains null, we still need to record the entry point, 497 // so it can be restored at runtime. 498 subgraph_info->add_subgraph_entry_field(field_offset, NULL); 499 } 500 } else { 501 ShouldNotReachHere(); 502 } 503 } 504 505 #define do_module_object_graph(archive_object_graph_do) \ 506 archive_object_graph_do(SystemDictionary::ArchivedModuleGraph_klass(), jdk_internal_module_ArchivedModuleGraph::archivedSystemModules_offset(), T_OBJECT, CHECK); \ 507 archive_object_graph_do(SystemDictionary::ArchivedModuleGraph_klass(), jdk_internal_module_ArchivedModuleGraph::archivedModuleFinder_offset(), T_OBJECT, CHECK); \ 508 archive_object_graph_do(SystemDictionary::ArchivedModuleGraph_klass(), jdk_internal_module_ArchivedModuleGraph::archivedMainModule_offset(), T_OBJECT, CHECK); \ 509 archive_object_graph_do(SystemDictionary::ArchivedModuleGraph_klass(), jdk_internal_module_ArchivedModuleGraph::archivedConfiguration_offset(), T_OBJECT, CHECK); \ 510 archive_object_graph_do(SystemDictionary::ImmutableCollections_ListN_klass(), java_util_ImmutableCollections_ListN::EMPTY_LIST_offset(), T_OBJECT, CHECK); \ 511 archive_object_graph_do(SystemDictionary::ImmutableCollections_MapN_klass(), java_util_ImmutableCollections_MapN::EMPTY_MAP_offset(), T_OBJECT, CHECK); \ 512 archive_object_graph_do(SystemDictionary::ImmutableCollections_SetN_klass(), java_util_ImmutableCollections_SetN::EMPTY_SET_offset(), T_OBJECT, CHECK); \ 513 archive_object_graph_do(SystemDictionary::Configuration_klass(), java_lang_module_Configuration::EMPTY_CONFIGURATION_offset(), T_OBJECT, CHECK) 514 515 void HeapShared::archive_module_graph_objects(Thread* THREAD) { 516 do_module_object_graph(archive_reachable_objects_from_static_field); 517 } 518 #endif // INCLUDE_CDS_JAVA_HEAP