1 /* 2 * Copyright (c) 2020, 2022, 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 #ifndef SHARE_CDS_ARCHIVEBUILDER_HPP 26 #define SHARE_CDS_ARCHIVEBUILDER_HPP 27 28 #include "cds/archiveUtils.hpp" 29 #include "cds/dumpAllocStats.hpp" 30 #include "memory/metaspaceClosure.hpp" 31 #include "oops/array.hpp" 32 #include "oops/klass.hpp" 33 #include "runtime/os.hpp" 34 #include "utilities/bitMap.hpp" 35 #include "utilities/growableArray.hpp" 36 #include "utilities/resizeableResourceHash.hpp" 37 #include "utilities/resourceHash.hpp" 38 39 struct ArchiveHeapBitmapInfo; 40 class CHeapBitMap; 41 class FileMapInfo; 42 class Klass; 43 class MemRegion; 44 class Symbol; 45 46 // Metaspace::allocate() requires that all blocks must be aligned with KlassAlignmentInBytes. 47 // We enforce the same alignment rule in blocks allocated from the shared space. 48 const int SharedSpaceObjectAlignment = KlassAlignmentInBytes; 49 50 // Overview of CDS archive creation (for both static and dynamic dump): 51 // 52 // [1] Load all classes (static dump: from the classlist, dynamic dump: as part of app execution) 53 // [2] Allocate "output buffer" 54 // [3] Copy contents of the 2 "core" regions (rw/ro) into the output buffer. 55 // - allocate the cpp vtables in rw (static dump only) 56 // - memcpy the MetaspaceObjs into rw/ro: 57 // dump_rw_region(); 58 // dump_ro_region(); 59 // - fix all the pointers in the MetaspaceObjs to point to the copies 60 // relocate_metaspaceobj_embedded_pointers() 61 // [4] Copy symbol table, dictionary, etc, into the ro region 62 // [5] Relocate all the pointers in rw/ro, so that the archive can be mapped to 63 // the "requested" location without runtime relocation. See relocate_to_requested() 64 // 65 // "source" vs "buffered" vs "requested" 66 // 67 // The ArchiveBuilder deals with three types of addresses. 68 // 69 // "source": These are the addresses of objects created in step [1] above. They are the actual 70 // InstanceKlass*, Method*, etc, of the Java classes that are loaded for executing 71 // Java bytecodes in the JVM process that's dumping the CDS archive. 72 // 73 // It may be necessary to contiue Java execution after ArchiveBuilder is finished. 74 // Therefore, we don't modify any of the "source" objects. 75 // 76 // "buffered": The "source" objects that are deemed archivable are copied into a temporary buffer. 77 // Objects in the buffer are modified in steps [2, 3, 4] (e.g., unshareable info is 78 // removed, pointers are relocated, etc) to prepare them to be loaded at runtime. 79 // 80 // "requested": These are the addreses where the "buffered" objects should be loaded at runtime. 81 // When the "buffered" objects are written into the archive file, their addresses 82 // are adjusted in step [5] such that the lowest of these objects would be mapped 83 // at SharedBaseAddress. 84 // 85 // Translation between "source" and "buffered" addresses is done with two hashtables: 86 // _src_obj_table : "source" -> "buffered" 87 // _buffered_to_src_table : "buffered" -> "source" 88 // 89 // Translation between "buffered" and "requested" addresses is done with a simple shift: 90 // buffered_address + _buffer_to_requested_delta == requested_address 91 // 92 class ArchiveBuilder : public StackObj { 93 protected: 94 DumpRegion* _current_dump_space; 95 address _buffer_bottom; // for writing the contents of rw/ro regions 96 address _last_verified_top; 97 int _num_dump_regions_used; 98 size_t _other_region_used_bytes; 99 100 // These are the addresses where we will request the static and dynamic archives to be 101 // mapped at run time. If the request fails (due to ASLR), we will map the archives at 102 // os-selected addresses. 103 address _requested_static_archive_bottom; // This is determined solely by the value of 104 // SharedBaseAddress during -Xshare:dump. 105 address _requested_static_archive_top; 106 address _requested_dynamic_archive_bottom; // Used only during dynamic dump. It's placed 107 // immediately above _requested_static_archive_top. 108 address _requested_dynamic_archive_top; 109 110 // (Used only during dynamic dump) where the static archive is actually mapped. This 111 // may be different than _requested_static_archive_{bottom,top} due to ASLR 112 address _mapped_static_archive_bottom; 113 address _mapped_static_archive_top; 114 115 intx _buffer_to_requested_delta; 116 117 DumpRegion* current_dump_space() const { return _current_dump_space; } 118 119 public: 120 enum FollowMode { 121 make_a_copy, point_to_it, set_to_null 122 }; 123 124 private: 125 class SpecialRefInfo { 126 // We have a "special pointer" of the given _type at _field_offset of _src_obj. 127 // See MetaspaceClosure::push_special(). 128 MetaspaceClosure::SpecialRef _type; 129 address _src_obj; 130 size_t _field_offset; 131 DEBUG_ONLY(size_t _src_obj_size_in_bytes;) 132 133 public: 134 SpecialRefInfo() {} 135 SpecialRefInfo(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset, size_t src_obj_size_in_bytes) 136 : _type(type), _src_obj(src_obj), _field_offset(field_offset) { 137 DEBUG_ONLY(_src_obj_size_in_bytes = src_obj_size_in_bytes); 138 } 139 140 MetaspaceClosure::SpecialRef type() const { return _type; } 141 address src_obj() const { return _src_obj; } 142 size_t field_offset() const { return _field_offset; } 143 144 DEBUG_ONLY(size_t src_obj_size_in_bytes() const { return _src_obj_size_in_bytes; }) 145 }; 146 147 class SourceObjInfo { 148 MetaspaceClosure::Ref* _ref; // The object that's copied into the buffer 149 uintx _ptrmap_start; // The bit-offset of the start of this object (inclusive) 150 uintx _ptrmap_end; // The bit-offset of the end of this object (exclusive) 151 bool _read_only; 152 FollowMode _follow_mode; 153 int _size_in_bytes; 154 MetaspaceObj::Type _msotype; 155 address _source_addr; // The value of the source object (_ref->obj()) when this 156 // SourceObjInfo was created. Note that _ref->obj() may change 157 // later if _ref is relocated. 158 address _buffered_addr; // The copy of _ref->obj() insider the buffer. 159 public: 160 SourceObjInfo(MetaspaceClosure::Ref* ref, bool read_only, FollowMode follow_mode) : 161 _ref(ref), _ptrmap_start(0), _ptrmap_end(0), _read_only(read_only), _follow_mode(follow_mode), 162 _size_in_bytes(ref->size() * BytesPerWord), _msotype(ref->msotype()), 163 _source_addr(ref->obj()) { 164 if (follow_mode == point_to_it) { 165 _buffered_addr = ref->obj(); 166 } else { 167 _buffered_addr = NULL; 168 } 169 } 170 171 bool should_copy() const { return _follow_mode == make_a_copy; } 172 MetaspaceClosure::Ref* ref() const { return _ref; } 173 void set_buffered_addr(address addr) { 174 assert(should_copy(), "must be"); 175 assert(_buffered_addr == NULL, "cannot be copied twice"); 176 assert(addr != NULL, "must be a valid copy"); 177 _buffered_addr = addr; 178 } 179 void set_ptrmap_start(uintx v) { _ptrmap_start = v; } 180 void set_ptrmap_end(uintx v) { _ptrmap_end = v; } 181 uintx ptrmap_start() const { return _ptrmap_start; } // inclusive 182 uintx ptrmap_end() const { return _ptrmap_end; } // exclusive 183 bool read_only() const { return _read_only; } 184 int size_in_bytes() const { return _size_in_bytes; } 185 address source_addr() const { return _source_addr; } 186 address buffered_addr() const { return _buffered_addr; } 187 MetaspaceObj::Type msotype() const { return _msotype; } 188 189 // convenience accessor 190 address obj() const { return ref()->obj(); } 191 }; 192 193 class SourceObjList { 194 uintx _total_bytes; 195 GrowableArray<SourceObjInfo*>* _objs; // Source objects to be archived 196 CHeapBitMap _ptrmap; // Marks the addresses of the pointer fields 197 // in the source objects 198 public: 199 SourceObjList(); 200 ~SourceObjList(); 201 202 GrowableArray<SourceObjInfo*>* objs() const { return _objs; } 203 204 void append(MetaspaceClosure::Ref* enclosing_ref, SourceObjInfo* src_info); 205 void remember_embedded_pointer(SourceObjInfo* pointing_obj, MetaspaceClosure::Ref* ref); 206 void relocate(int i, ArchiveBuilder* builder); 207 208 // convenience accessor 209 SourceObjInfo* at(int i) const { return objs()->at(i); } 210 }; 211 212 class SrcObjTableCleaner { 213 public: 214 bool do_entry(address key, const SourceObjInfo& value) { 215 delete value.ref(); 216 return true; 217 } 218 }; 219 220 class CDSMapLogger; 221 222 static const int INITIAL_TABLE_SIZE = 15889; 223 static const int MAX_TABLE_SIZE = 1000000; 224 225 ReservedSpace _shared_rs; 226 VirtualSpace _shared_vs; 227 228 DumpRegion _rw_region; 229 DumpRegion _ro_region; 230 CHeapBitMap _ptrmap; // bitmap used by ArchivePtrMarker 231 232 SourceObjList _rw_src_objs; // objs to put in rw region 233 SourceObjList _ro_src_objs; // objs to put in ro region 234 ResizeableResourceHashtable<address, SourceObjInfo, AnyObj::C_HEAP, mtClassShared> _src_obj_table; 235 ResizeableResourceHashtable<address, address, AnyObj::C_HEAP, mtClassShared> _buffered_to_src_table; 236 GrowableArray<Klass*>* _klasses; 237 GrowableArray<Symbol*>* _symbols; 238 GrowableArray<SpecialRefInfo>* _special_refs; 239 240 // statistics 241 DumpAllocStats _alloc_stats; 242 size_t _total_closed_heap_region_size; 243 size_t _total_open_heap_region_size; 244 245 void print_region_stats(FileMapInfo *map_info, 246 GrowableArray<MemRegion>* closed_heap_regions, 247 GrowableArray<MemRegion>* open_heap_regions); 248 void print_bitmap_region_stats(size_t size, size_t total_size); 249 void print_heap_region_stats(GrowableArray<MemRegion>* regions, 250 const char *name, size_t total_size); 251 252 // For global access. 253 static ArchiveBuilder* _current; 254 255 public: 256 // Use this when you allocate space outside of ArchiveBuilder::dump_{rw,ro}_region. 257 // These are usually for misc tables that are allocated in the RO space. 258 class OtherROAllocMark { 259 char* _oldtop; 260 public: 261 OtherROAllocMark() { 262 _oldtop = _current->_ro_region.top(); 263 } 264 ~OtherROAllocMark(); 265 }; 266 267 private: 268 bool is_dumping_full_module_graph(); 269 FollowMode get_follow_mode(MetaspaceClosure::Ref *ref); 270 271 void iterate_sorted_roots(MetaspaceClosure* it, bool is_relocating_pointers); 272 void sort_symbols_and_fix_hash(); 273 void sort_klasses(); 274 static int compare_symbols_by_address(Symbol** a, Symbol** b); 275 static int compare_klass_by_name(Klass** a, Klass** b); 276 277 void make_shallow_copies(DumpRegion *dump_region, const SourceObjList* src_objs); 278 void make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info); 279 280 void update_special_refs(); 281 void relocate_embedded_pointers(SourceObjList* src_objs); 282 283 bool is_excluded(Klass* k); 284 void clean_up_src_obj_table(); 285 286 protected: 287 virtual void iterate_roots(MetaspaceClosure* it, bool is_relocating_pointers) = 0; 288 289 // Conservative estimate for number of bytes needed for: 290 size_t _estimated_metaspaceobj_bytes; // all archived MetaspaceObj's. 291 size_t _estimated_hashtable_bytes; // symbol table and dictionaries 292 293 static const int _total_dump_regions = 2; 294 295 size_t estimate_archive_size(); 296 297 void start_dump_space(DumpRegion* next); 298 void verify_estimate_size(size_t estimate, const char* which); 299 300 public: 301 address reserve_buffer(); 302 303 address buffer_bottom() const { return _buffer_bottom; } 304 address buffer_top() const { return (address)current_dump_space()->top(); } 305 address requested_static_archive_bottom() const { return _requested_static_archive_bottom; } 306 address mapped_static_archive_bottom() const { return _mapped_static_archive_bottom; } 307 intx buffer_to_requested_delta() const { return _buffer_to_requested_delta; } 308 309 bool is_in_buffer_space(address p) const { 310 return (buffer_bottom() <= p && p < buffer_top()); 311 } 312 313 template <typename T> bool is_in_requested_static_archive(T p) const { 314 return _requested_static_archive_bottom <= (address)p && (address)p < _requested_static_archive_top; 315 } 316 317 template <typename T> bool is_in_mapped_static_archive(T p) const { 318 return _mapped_static_archive_bottom <= (address)p && (address)p < _mapped_static_archive_top; 319 } 320 321 template <typename T> bool is_in_buffer_space(T obj) const { 322 return is_in_buffer_space(address(obj)); 323 } 324 325 template <typename T> T to_requested(T obj) const { 326 assert(is_in_buffer_space(obj), "must be"); 327 return (T)(address(obj) + _buffer_to_requested_delta); 328 } 329 330 static intx get_buffer_to_requested_delta() { 331 return current()->buffer_to_requested_delta(); 332 } 333 334 public: 335 static const uintx MAX_SHARED_DELTA = 0x7FFFFFFF; 336 337 // The address p points to an object inside the output buffer. When the archive is mapped 338 // at the requested address, what's the offset of this object from _requested_static_archive_bottom? 339 uintx buffer_to_offset(address p) const; 340 341 // Same as buffer_to_offset, except that the address p points to either (a) an object 342 // inside the output buffer, or (b), an object in the currently mapped static archive. 343 uintx any_to_offset(address p) const; 344 345 template <typename T> 346 u4 buffer_to_offset_u4(T p) const { 347 uintx offset = buffer_to_offset((address)p); 348 guarantee(offset <= MAX_SHARED_DELTA, "must be 32-bit offset " INTPTR_FORMAT, offset); 349 return (u4)offset; 350 } 351 352 template <typename T> 353 u4 any_to_offset_u4(T p) const { 354 uintx offset = any_to_offset((address)p); 355 guarantee(offset <= MAX_SHARED_DELTA, "must be 32-bit offset " INTPTR_FORMAT, offset); 356 return (u4)offset; 357 } 358 359 static void assert_is_vm_thread() PRODUCT_RETURN; 360 361 public: 362 ArchiveBuilder(); 363 ~ArchiveBuilder(); 364 365 void gather_klasses_and_symbols(); 366 void gather_source_objs(); 367 bool gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only); 368 bool gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref, bool read_only); 369 void add_special_ref(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset, size_t src_obj_size_in_bytes) { 370 _special_refs->append(SpecialRefInfo(type, src_obj, field_offset, src_obj_size_in_bytes)); 371 } 372 void remember_embedded_pointer_in_copied_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref); 373 374 DumpRegion* rw_region() { return &_rw_region; } 375 DumpRegion* ro_region() { return &_ro_region; } 376 377 static char* rw_region_alloc(size_t num_bytes) { 378 return current()->rw_region()->allocate(num_bytes); 379 } 380 static char* ro_region_alloc(size_t num_bytes) { 381 return current()->ro_region()->allocate(num_bytes); 382 } 383 384 template <typename T> 385 static Array<T>* new_ro_array(int length) { 386 size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T)); 387 Array<T>* array = (Array<T>*)ro_region_alloc(byte_size); 388 array->initialize(length); 389 return array; 390 } 391 392 template <typename T> 393 static Array<T>* new_rw_array(int length) { 394 size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T)); 395 Array<T>* array = (Array<T>*)rw_region_alloc(byte_size); 396 array->initialize(length); 397 return array; 398 } 399 400 template <typename T> 401 static size_t ro_array_bytesize(int length) { 402 size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T)); 403 return align_up(byte_size, SharedSpaceObjectAlignment); 404 } 405 406 void dump_rw_metadata(); 407 void dump_ro_metadata(); 408 void relocate_metaspaceobj_embedded_pointers(); 409 void relocate_roots(); 410 void relocate_vm_classes(); 411 void make_klasses_shareable(); 412 void relocate_to_requested(); 413 void write_archive(FileMapInfo* mapinfo, 414 GrowableArray<MemRegion>* closed_heap_regions, 415 GrowableArray<MemRegion>* open_heap_regions, 416 GrowableArray<ArchiveHeapBitmapInfo>* closed_heap_oopmaps, 417 GrowableArray<ArchiveHeapBitmapInfo>* open_heap_oopmaps); 418 void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, 419 bool read_only, bool allow_exec); 420 421 address get_buffered_addr(address src_addr) const; 422 address get_source_addr(address buffered_addr) const; 423 template <typename T> T get_source_addr(T buffered_addr) const { 424 return (T)get_source_addr((address)buffered_addr); 425 } 426 427 // All klasses and symbols that will be copied into the archive 428 GrowableArray<Klass*>* klasses() const { return _klasses; } 429 GrowableArray<Symbol*>* symbols() const { return _symbols; } 430 431 static bool is_active() { 432 return (_current != NULL); 433 } 434 435 static ArchiveBuilder* current() { 436 assert_is_vm_thread(); 437 assert(_current != NULL, "ArchiveBuilder must be active"); 438 return _current; 439 } 440 441 static DumpAllocStats* alloc_stats() { 442 return &(current()->_alloc_stats); 443 } 444 445 static CompactHashtableStats* symbol_stats() { 446 return alloc_stats()->symbol_stats(); 447 } 448 449 static CompactHashtableStats* string_stats() { 450 return alloc_stats()->string_stats(); 451 } 452 453 void relocate_klass_ptr_of_oop(oop o); 454 455 static Klass* get_buffered_klass(Klass* src_klass) { 456 Klass* klass = (Klass*)current()->get_buffered_addr((address)src_klass); 457 assert(klass != NULL && klass->is_klass(), "must be"); 458 return klass; 459 } 460 461 static Symbol* get_buffered_symbol(Symbol* src_symbol) { 462 return (Symbol*)current()->get_buffered_addr((address)src_symbol); 463 } 464 465 void print_stats(); 466 void report_out_of_space(const char* name, size_t needed_bytes); 467 }; 468 469 #endif // SHARE_CDS_ARCHIVEBUILDER_HPP