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