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