1 /*
2 * Copyright (c) 2024, 2025, 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_AOTMAPPEDHEAPWRITER_HPP
26 #define SHARE_CDS_AOTMAPPEDHEAPWRITER_HPP
27
28 #include "cds/aotMapLogger.hpp"
29 #include "cds/heapShared.hpp"
30 #include "memory/allocation.hpp"
31 #include "memory/allStatic.hpp"
32 #include "oops/oopHandle.hpp"
33 #include "utilities/bitMap.hpp"
34 #include "utilities/exceptions.hpp"
35 #include "utilities/growableArray.hpp"
36 #include "utilities/hashTable.hpp"
37 #include "utilities/macros.hpp"
38
39 class MemRegion;
40
41 #if INCLUDE_CDS_JAVA_HEAP
42 class DumpedInternedStrings :
43 public ResizeableHashTable<oop, bool,
44 AnyObj::C_HEAP,
45 mtClassShared,
46 HeapShared::string_oop_hash>
47 {
48 public:
49 DumpedInternedStrings(unsigned size, unsigned max_size) :
50 ResizeableHashTable<oop, bool,
51 AnyObj::C_HEAP,
52 mtClassShared,
53 HeapShared::string_oop_hash>(size, max_size) {}
54 };
55
56 class AOTMappedHeapWriter : AllStatic {
57 friend class HeapShared;
58 friend class AOTMappedHeapLoader;
59 // AOTMappedHeapWriter manipulates three types of addresses:
60 //
61 // "source" vs "buffered" vs "requested"
62 //
63 // (Note: the design and convention is the same as for the archiving of Metaspace objects.
64 // See archiveBuilder.hpp.)
65 //
66 // - "source objects" are regular Java objects allocated during the execution
67 // of "java -Xshare:dump". They can be used as regular oops.
68 //
69 // Between HeapShared::start_scanning_for_oops() and HeapShared::end_scanning_for_oops(),
70 // we recursively search for the oops that need to be stored into the CDS archive.
71 // These are entered into HeapShared::archived_object_cache().
72 //
73 // - "buffered objects" are copies of the "source objects", and are stored in into
74 // ArchiveHeapWriter::_buffer, which is a GrowableArray that sits outside of
75 // the valid heap range. Therefore we avoid using the addresses of these copies
76 // as oops. They are usually called "buffered_addr" in the code (of the type "address").
77 //
78 // The buffered objects are stored contiguously, possibly with interleaving fillers
79 // to make sure no objects span across boundaries of MIN_GC_REGION_ALIGNMENT.
80 //
81 // - Each archived object has a "requested address" -- at run time, if the object
82 // can be mapped at this address, we can avoid relocation.
83 //
84 // The requested address is implemented differently depending on UseCompressedOops:
85 //
86 // UseCompressedOops == true:
87 // The archived objects are stored assuming that the runtime COOPS compression
88 // scheme is exactly the same as in dump time (or else a more expensive runtime relocation
89 // would be needed.)
90 //
91 // At dump time, we assume that the runtime heap range is exactly the same as
92 // in dump time. The requested addresses of the archived objects are chosen such that
93 // they would occupy the top end of a G1 heap (TBD when dumping is supported by other
94 // collectors. See JDK-8298614).
95 //
96 // UseCompressedOops == false:
97 // At runtime, the heap range is usually picked (randomly) by the OS, so we will almost always
98 // need to perform relocation. Hence, the goal of the "requested address" is to ensure that
99 // the contents of the archived objects are deterministic. I.e., the oop fields of archived
100 // objects will always point to deterministic addresses.
101 //
102 // For G1, the archived heap is written such that the lowest archived object is placed
103 // at NOCOOPS_REQUESTED_BASE. (TBD after JDK-8298614).
104 // ----------------------------------------------------------------------
105
106 public:
107 static const intptr_t NOCOOPS_REQUESTED_BASE = 0x10000000;
108
109 // The minimum region size of all collectors that are supported by CDS.
110 // G1 heap region size can never be smaller than 1M.
111 // Shenandoah heap region size can never be smaller than 256K.
112 static constexpr int MIN_GC_REGION_ALIGNMENT = 256 * K;
113
114 static const int INITIAL_TABLE_SIZE = 15889; // prime number
115 static const int MAX_TABLE_SIZE = 1000000;
116
117 private:
118 class EmbeddedOopRelocator;
119 struct NativePointerInfo {
120 oop _src_obj;
121 int _field_offset;
122 };
123
124 static GrowableArrayCHeap<u1, mtClassShared>* _buffer;
125
126 // The number of bytes that have written into _buffer (may be smaller than _buffer->length()).
127 static size_t _buffer_used;
128
129 // The heap root segments information.
130 static HeapRootSegments _heap_root_segments;
131
132 // The address range of the requested location of the archived heap objects.
133 static address _requested_bottom;
134 static address _requested_top;
135
136 static GrowableArrayCHeap<NativePointerInfo, mtClassShared>* _native_pointers;
137 static GrowableArrayCHeap<oop, mtClassShared>* _source_objs;
138 static DumpedInternedStrings *_dumped_interned_strings;
139
140 // We sort _source_objs_order to minimize the number of bits in ptrmap and oopmap.
141 // See comments near the body of ArchiveHeapWriter::compare_objs_by_oop_fields().
142 // The objects will be written in the order of:
143 //_source_objs->at(_source_objs_order->at(0)._index)
144 // source_objs->at(_source_objs_order->at(1)._index)
145 // source_objs->at(_source_objs_order->at(2)._index)
146 // ...
147 struct HeapObjOrder {
148 int _index; // The location of this object in _source_objs
149 int _rank; // A lower rank means the object will be written at a lower location.
150 };
151 static GrowableArrayCHeap<HeapObjOrder, mtClassShared>* _source_objs_order;
152
153 typedef ResizeableHashTable<size_t, OopHandle,
154 AnyObj::C_HEAP,
155 mtClassShared> BufferOffsetToSourceObjectTable;
156 static BufferOffsetToSourceObjectTable* _buffer_offset_to_source_obj_table;
157
158 static void allocate_buffer();
159 static void ensure_buffer_space(size_t min_bytes);
160
161 // Both Java bytearray and GrowableArraty use int indices and lengths. Do a safe typecast with range check
162 static int to_array_index(size_t i) {
163 assert(i <= (size_t)max_jint, "must be");
164 return (int)i;
165 }
166 static int to_array_length(size_t n) {
167 return to_array_index(n);
168 }
169
170 template <typename T> static T offset_to_buffered_address(size_t offset) {
171 return (T)(_buffer->adr_at(to_array_index(offset)));
172 }
173
174 static address buffer_bottom() {
175 return offset_to_buffered_address<address>(0);
176 }
177
178 // The exclusive end of the last object that was copied into the buffer.
179 static address buffer_top() {
180 return buffer_bottom() + _buffer_used;
181 }
182
183 static bool in_buffer(address buffered_addr) {
184 return (buffer_bottom() <= buffered_addr) && (buffered_addr < buffer_top());
185 }
186
187 static size_t buffered_address_to_offset(address buffered_addr) {
188 assert(in_buffer(buffered_addr), "sanity");
189 return buffered_addr - buffer_bottom();
190 }
191
192 static void root_segment_at_put(objArrayOop segment, int index, oop root);
193 static objArrayOop allocate_root_segment(size_t offset, int element_count);
194 static void copy_roots_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots);
195 static void copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots);
196 static size_t copy_one_source_obj_to_buffer(oop src_obj);
197
198 static void maybe_fill_gc_region_gap(size_t required_byte_size);
199 static size_t filler_array_byte_size(int length);
200 static int filler_array_length(size_t fill_bytes);
201 static HeapWord* init_filler_array_at_buffer_top(int array_length, size_t fill_bytes);
202
203 static void set_requested_address(ArchiveMappedHeapInfo* info);
204 static void mark_native_pointers(oop orig_obj);
205 static void relocate_embedded_oops(GrowableArrayCHeap<oop, mtClassShared>* roots, ArchiveMappedHeapInfo* info);
206 static void compute_ptrmap(ArchiveMappedHeapInfo *info);
207 static bool is_in_requested_range(oop o);
208 static oop requested_obj_from_buffer_offset(size_t offset);
209
210 static oop load_oop_from_buffer(oop* buffered_addr);
211 static oop load_oop_from_buffer(narrowOop* buffered_addr);
212 inline static void store_oop_in_buffer(oop* buffered_addr, oop requested_obj);
213 inline static void store_oop_in_buffer(narrowOop* buffered_addr, oop requested_obj);
214
215 template <typename T> static oop load_source_oop_from_buffer(T* buffered_addr);
216 template <typename T> static void store_requested_oop_in_buffer(T* buffered_addr, oop request_oop);
217
218 template <typename T> static T* requested_addr_to_buffered_addr(T* p);
219 template <typename T> static void relocate_field_in_buffer(T* field_addr_in_buffer, oop source_referent, CHeapBitMap* oopmap);
220 template <typename T> static void mark_oop_pointer(T* buffered_addr, CHeapBitMap* oopmap);
221
222 static void update_header_for_requested_obj(oop requested_obj, oop src_obj, Klass* src_klass);
223
224 static int compare_objs_by_oop_fields(HeapObjOrder* a, HeapObjOrder* b);
225 static void sort_source_objs();
226
227 public:
228 static void init() NOT_CDS_JAVA_HEAP_RETURN;
229 static void delete_tables_with_raw_oops();
230 static void add_source_obj(oop src_obj);
231 static bool is_too_large_to_archive(size_t size);
232 static bool is_too_large_to_archive(oop obj);
233 static bool is_string_too_large_to_archive(oop string);
234 static bool is_dumped_interned_string(oop o);
235 static void add_to_dumped_interned_strings(oop string);
236 static void write(GrowableArrayCHeap<oop, mtClassShared>*, ArchiveMappedHeapInfo* heap_info);
237 static address requested_address(); // requested address of the lowest achived heap object
238 static size_t get_filler_size_at(address buffered_addr);
239
240 static void mark_native_pointer(oop src_obj, int offset);
241 static oop source_obj_to_requested_obj(oop src_obj);
242 static oop buffered_addr_to_source_obj(address buffered_addr);
243 static address buffered_addr_to_requested_addr(address buffered_addr);
244 static Klass* real_klass_of_buffered_oop(address buffered_addr);
245 static size_t size_of_buffered_oop(address buffered_addr);
246
247 static AOTMapLogger::OopDataIterator* oop_iterator(ArchiveMappedHeapInfo* heap_info);
248 };
249 #endif // INCLUDE_CDS_JAVA_HEAP
250 #endif // SHARE_CDS_AOTMAPPEDHEAPWRITER_HPP