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/compressedOops.hpp"
33 #include "oops/oopHandle.hpp"
34 #include "utilities/bitMap.hpp"
35 #include "utilities/exceptions.hpp"
36 #include "utilities/growableArray.hpp"
37 #include "utilities/hashTable.hpp"
38 #include "utilities/macros.hpp"
39
40 class MemRegion;
41
42 #if INCLUDE_CDS_JAVA_HEAP
43 class DumpedInternedStrings :
44 public ResizeableHashTable<oop, bool,
45 AnyObj::C_HEAP,
46 mtClassShared,
47 HeapShared::string_oop_hash>
48 {
49 public:
50 DumpedInternedStrings(unsigned size, unsigned max_size) :
51 ResizeableHashTable<oop, bool,
52 AnyObj::C_HEAP,
53 mtClassShared,
54 HeapShared::string_oop_hash>(size, max_size) {}
55 };
56
57 class AOTMappedHeapWriter : AllStatic {
58 friend class HeapShared;
59 friend class AOTMappedHeapLoader;
60 // AOTMappedHeapWriter manipulates three types of addresses:
61 //
62 // "source" vs "buffered" vs "requested"
63 //
64 // (Note: the design and convention is the same as for the archiving of Metaspace objects.
65 // See archiveBuilder.hpp.)
66 //
67 // - "source objects" are regular Java objects allocated during the execution
68 // of "java -Xshare:dump". They can be used as regular oops.
69 //
70 // Between HeapShared::start_scanning_for_oops() and HeapShared::end_scanning_for_oops(),
71 // we recursively search for the oops that need to be stored into the CDS archive.
72 // These are entered into HeapShared::archived_object_cache().
73 //
74 // - "buffered objects" are copies of the "source objects", and are stored in into
75 // AOTMappedHeapWriter::_buffer, which is a GrowableArray that sits outside of
76 // the valid heap range. Therefore we avoid using the addresses of these copies
114 struct NativePointerInfo {
115 oop _src_obj;
116 int _field_offset;
117 };
118
119 static bool _is_writing_deterministic_heap;
120 static GrowableArrayCHeap<u1, mtClassShared>* _buffer;
121
122 // The number of bytes that have written into _buffer (may be smaller than _buffer->length()).
123 static size_t _buffer_used;
124
125 // The heap root segments information.
126 static HeapRootSegments _heap_root_segments;
127
128 // The address range of the requested location of the archived heap objects.
129 static address _requested_bottom; // The requested address of the lowest archived heap object
130 static address _requested_top; // The exclusive end of the highest archived heap object
131
132 static GrowableArrayCHeap<NativePointerInfo, mtClassShared>* _native_pointers;
133 static GrowableArrayCHeap<oop, mtClassShared>* _source_objs;
134 static DumpedInternedStrings *_dumped_interned_strings;
135
136 // We sort _source_objs_order to minimize the number of bits in ptrmap and oopmap.
137 // See comments near the body of AOTMappedHeapWriter::compare_objs_by_oop_fields().
138 // The objects will be written in the order of:
139 //_source_objs->at(_source_objs_order->at(0)._index)
140 // source_objs->at(_source_objs_order->at(1)._index)
141 // source_objs->at(_source_objs_order->at(2)._index)
142 // ...
143 struct HeapObjOrder {
144 int _index; // The location of this object in _source_objs
145 int _rank; // A lower rank means the object will be written at a lower location.
146 };
147 static GrowableArrayCHeap<HeapObjOrder, mtClassShared>* _source_objs_order;
148
149 typedef ResizeableHashTable<size_t, OopHandle,
150 AnyObj::C_HEAP,
151 mtClassShared> BufferOffsetToSourceObjectTable;
152 static BufferOffsetToSourceObjectTable* _buffer_offset_to_source_obj_table;
153
154 static void allocate_buffer();
173
174 // The exclusive end of the last object that was copied into the buffer.
175 static address buffer_top() {
176 return buffer_bottom() + _buffer_used;
177 }
178
179 static bool in_buffer(address buffered_addr) {
180 return (buffer_bottom() <= buffered_addr) && (buffered_addr < buffer_top());
181 }
182
183 static size_t buffered_address_to_offset(address buffered_addr) {
184 assert(in_buffer(buffered_addr), "sanity");
185 return buffered_addr - buffer_bottom();
186 }
187
188 static void root_segment_at_put(objArrayOop segment, int index, oop root);
189 static objArrayOop allocate_root_segment(size_t offset, int element_count);
190 static void copy_roots_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots);
191 static void copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots);
192 static size_t copy_one_source_obj_to_buffer(oop src_obj);
193
194 static void maybe_fill_gc_region_gap(size_t required_byte_size);
195 static size_t filler_array_byte_size(int length);
196 static int filler_array_length(size_t fill_bytes);
197 static HeapWord* init_filler_array_at_buffer_top(int array_length, size_t fill_bytes);
198
199 static void set_requested_address_range(ArchiveMappedHeapInfo* info);
200 static void mark_native_pointers(oop orig_obj);
201 static void relocate_embedded_oops(GrowableArrayCHeap<oop, mtClassShared>* roots, ArchiveMappedHeapInfo* info);
202 static void compute_ptrmap(ArchiveMappedHeapInfo *info);
203 static bool is_in_requested_range(oop o);
204 static oop requested_obj_from_buffer_offset(size_t offset);
205
206 static oop load_oop_from_buffer(oop* buffered_addr);
207 static oop load_oop_from_buffer(narrowOop* buffered_addr);
208 inline static void store_oop_in_buffer(oop* buffered_addr, oop requested_obj);
209 inline static void store_oop_in_buffer(narrowOop* buffered_addr, oop requested_obj);
210
211 template <typename T> static oop load_source_oop_from_buffer(T* buffered_addr);
212 template <typename T> static void store_requested_oop_in_buffer(T* buffered_addr, oop request_oop);
213
214 template <typename T> static T* requested_addr_to_buffered_addr(T* p);
215 template <typename T> static void relocate_field_in_buffer(T* field_addr_in_buffer, oop source_referent, CHeapBitMap* oopmap);
216 template <typename T> static void mark_oop_pointer(T* buffered_addr, CHeapBitMap* oopmap);
217
218 static void update_header_for_requested_obj(oop requested_obj, oop src_obj, Klass* src_klass);
219
220 static int compare_objs_by_oop_fields(HeapObjOrder* a, HeapObjOrder* b);
221 static void sort_source_objs();
222
223 public:
224 static void init() NOT_CDS_JAVA_HEAP_RETURN;
225 static void delete_tables_with_raw_oops();
226 static void add_source_obj(oop src_obj);
227 static bool is_too_large_to_archive(size_t size);
228 static bool is_too_large_to_archive(oop obj);
229 static bool is_string_too_large_to_archive(oop string);
230 static bool is_dumped_interned_string(oop o);
231 static void add_to_dumped_interned_strings(oop string);
232 static void write(GrowableArrayCHeap<oop, mtClassShared>*, ArchiveMappedHeapInfo* heap_info);
233 static address requested_address(); // requested address of the lowest achived heap object
234 static size_t get_filler_size_at(address buffered_addr);
235
236 static void mark_native_pointer(oop src_obj, int offset);
237 static oop source_obj_to_requested_obj(oop src_obj);
238 static oop buffered_addr_to_source_obj(address buffered_addr);
239 static address buffered_addr_to_requested_addr(address buffered_addr);
240 static Klass* real_klass_of_buffered_oop(address buffered_addr);
241 static size_t size_of_buffered_oop(address buffered_addr);
242
243 static AOTMapLogger::OopDataIterator* oop_iterator(ArchiveMappedHeapInfo* heap_info);
244 };
245 #endif // INCLUDE_CDS_JAVA_HEAP
246 #endif // SHARE_CDS_AOTMAPPEDHEAPWRITER_HPP
|
1 /*
2 * Copyright (c) 2024, 2026, 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/compressedOops.hpp"
33 #include "oops/oopHandle.hpp"
34 #include "utilities/bitMap.hpp"
35 #include "utilities/exceptions.hpp"
36 #include "utilities/growableArray.hpp"
37 #include "utilities/hashTable.hpp"
38 #include "utilities/macros.hpp"
39
40 class MemRegion;
41
42 #if INCLUDE_CDS_JAVA_HEAP
43 class AOTMappedHeapWriter : AllStatic {
44 friend class HeapShared;
45 friend class AOTMappedHeapLoader;
46 // AOTMappedHeapWriter manipulates three types of addresses:
47 //
48 // "source" vs "buffered" vs "requested"
49 //
50 // (Note: the design and convention is the same as for the archiving of Metaspace objects.
51 // See archiveBuilder.hpp.)
52 //
53 // - "source objects" are regular Java objects allocated during the execution
54 // of "java -Xshare:dump". They can be used as regular oops.
55 //
56 // Between HeapShared::start_scanning_for_oops() and HeapShared::end_scanning_for_oops(),
57 // we recursively search for the oops that need to be stored into the CDS archive.
58 // These are entered into HeapShared::archived_object_cache().
59 //
60 // - "buffered objects" are copies of the "source objects", and are stored in into
61 // AOTMappedHeapWriter::_buffer, which is a GrowableArray that sits outside of
62 // the valid heap range. Therefore we avoid using the addresses of these copies
100 struct NativePointerInfo {
101 oop _src_obj;
102 int _field_offset;
103 };
104
105 static bool _is_writing_deterministic_heap;
106 static GrowableArrayCHeap<u1, mtClassShared>* _buffer;
107
108 // The number of bytes that have written into _buffer (may be smaller than _buffer->length()).
109 static size_t _buffer_used;
110
111 // The heap root segments information.
112 static HeapRootSegments _heap_root_segments;
113
114 // The address range of the requested location of the archived heap objects.
115 static address _requested_bottom; // The requested address of the lowest archived heap object
116 static address _requested_top; // The exclusive end of the highest archived heap object
117
118 static GrowableArrayCHeap<NativePointerInfo, mtClassShared>* _native_pointers;
119 static GrowableArrayCHeap<oop, mtClassShared>* _source_objs;
120
121 // We sort _source_objs_order to minimize the number of bits in ptrmap and oopmap.
122 // See comments near the body of AOTMappedHeapWriter::compare_objs_by_oop_fields().
123 // The objects will be written in the order of:
124 //_source_objs->at(_source_objs_order->at(0)._index)
125 // source_objs->at(_source_objs_order->at(1)._index)
126 // source_objs->at(_source_objs_order->at(2)._index)
127 // ...
128 struct HeapObjOrder {
129 int _index; // The location of this object in _source_objs
130 int _rank; // A lower rank means the object will be written at a lower location.
131 };
132 static GrowableArrayCHeap<HeapObjOrder, mtClassShared>* _source_objs_order;
133
134 typedef ResizeableHashTable<size_t, OopHandle,
135 AnyObj::C_HEAP,
136 mtClassShared> BufferOffsetToSourceObjectTable;
137 static BufferOffsetToSourceObjectTable* _buffer_offset_to_source_obj_table;
138
139 static void allocate_buffer();
158
159 // The exclusive end of the last object that was copied into the buffer.
160 static address buffer_top() {
161 return buffer_bottom() + _buffer_used;
162 }
163
164 static bool in_buffer(address buffered_addr) {
165 return (buffer_bottom() <= buffered_addr) && (buffered_addr < buffer_top());
166 }
167
168 static size_t buffered_address_to_offset(address buffered_addr) {
169 assert(in_buffer(buffered_addr), "sanity");
170 return buffered_addr - buffer_bottom();
171 }
172
173 static void root_segment_at_put(objArrayOop segment, int index, oop root);
174 static objArrayOop allocate_root_segment(size_t offset, int element_count);
175 static void copy_roots_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots);
176 static void copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots);
177 static size_t copy_one_source_obj_to_buffer(oop src_obj);
178 static void update_stats(oop src_obj);
179
180 static void maybe_fill_gc_region_gap(size_t required_byte_size);
181 static size_t filler_array_byte_size(int length);
182 static int filler_array_length(size_t fill_bytes);
183 static HeapWord* init_filler_array_at_buffer_top(int array_length, size_t fill_bytes);
184
185 static void set_requested_address_range(ArchiveMappedHeapInfo* info);
186 static void mark_native_pointers(oop orig_obj);
187 static void relocate_embedded_oops(GrowableArrayCHeap<oop, mtClassShared>* roots, ArchiveMappedHeapInfo* info);
188 static void compute_ptrmap(ArchiveMappedHeapInfo *info);
189 static bool is_in_requested_range(oop o);
190 static oop requested_obj_from_buffer_offset(size_t offset);
191
192 static oop load_oop_from_buffer(oop* buffered_addr);
193 static oop load_oop_from_buffer(narrowOop* buffered_addr);
194 inline static void store_oop_in_buffer(oop* buffered_addr, oop requested_obj);
195 inline static void store_oop_in_buffer(narrowOop* buffered_addr, oop requested_obj);
196
197 template <typename T> static oop load_source_oop_from_buffer(T* buffered_addr);
198 template <typename T> static void store_requested_oop_in_buffer(T* buffered_addr, oop request_oop);
199
200 template <typename T> static T* requested_addr_to_buffered_addr(T* p);
201 template <typename T> static void relocate_field_in_buffer(T* field_addr_in_buffer, oop source_referent, CHeapBitMap* oopmap);
202 template <typename T> static void mark_oop_pointer(T* buffered_addr, CHeapBitMap* oopmap);
203
204 static void update_header_for_requested_obj(oop requested_obj, oop src_obj, Klass* src_klass);
205
206 static int compare_objs_by_oop_fields(HeapObjOrder* a, HeapObjOrder* b);
207 static void sort_source_objs();
208
209 public:
210 static void init() NOT_CDS_JAVA_HEAP_RETURN;
211 static void delete_tables_with_raw_oops();
212 static void add_source_obj(oop src_obj);
213 static bool is_too_large_to_archive(size_t size);
214 static bool is_too_large_to_archive(oop obj);
215 static bool is_string_too_large_to_archive(oop string);
216 static void write(GrowableArrayCHeap<oop, mtClassShared>*, ArchiveMappedHeapInfo* heap_info);
217 static address requested_address(); // requested address of the lowest achived heap object
218 static size_t get_filler_size_at(address buffered_addr);
219
220 static void mark_native_pointer(oop src_obj, int offset);
221 static oop source_obj_to_requested_obj(oop src_obj);
222 static oop buffered_addr_to_source_obj(address buffered_addr);
223 static address buffered_addr_to_requested_addr(address buffered_addr);
224 static Klass* real_klass_of_buffered_oop(address buffered_addr);
225 static size_t size_of_buffered_oop(address buffered_addr);
226
227 static AOTMapLogger::OopDataIterator* oop_iterator(ArchiveMappedHeapInfo* heap_info);
228 };
229 #endif // INCLUDE_CDS_JAVA_HEAP
230 #endif // SHARE_CDS_AOTMAPPEDHEAPWRITER_HPP
|