1 /*
2 * Copyright (c) 2019, 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_ARCHIVEUTILS_HPP
26 #define SHARE_CDS_ARCHIVEUTILS_HPP
27
28 #include "cds/cds_globals.hpp"
29 #include "cds/serializeClosure.hpp"
30 #include "logging/log.hpp"
31 #include "memory/metaspace.hpp"
32 #include "memory/virtualspace.hpp"
33 #include "runtime/nonJavaThread.hpp"
34 #include "runtime/semaphore.hpp"
35 #include "utilities/bitMap.hpp"
36 #include "utilities/exceptions.hpp"
37 #include "utilities/macros.hpp"
38
39 class BootstrapInfo;
40 class ReservedSpace;
41 class VirtualSpace;
42
43 template<class E> class Array;
44 template<class E> class GrowableArray;
45
46 // ArchivePtrMarker is used to mark the location of pointers embedded in a CDS archive. E.g., when an
47 // InstanceKlass k is dumped, we mark the location of the k->_name pointer by effectively calling
48 // mark_pointer(/*ptr_loc=*/&k->_name). It's required that (_prt_base <= ptr_loc < _ptr_end). _ptr_base is
49 // fixed, but _ptr_end can be expanded as more objects are dumped.
50 class ArchivePtrMarker : AllStatic {
51 static CHeapBitMap* _ptrmap;
52 static CHeapBitMap* _rw_ptrmap;
53 static CHeapBitMap* _ro_ptrmap;
54 static CHeapBitMap* _ac_ptrmap;
55 static VirtualSpace* _vs;
56
57 // Once _ptrmap is compacted, we don't allow bit marking anymore. This is to
58 // avoid unintentional copy operations after the bitmap has been finalized and written.
59 static bool _compacted;
60
61 static address* ptr_base() { return (address*)_vs->low(); } // committed lower bound (inclusive)
62 static address* ptr_end() { return (address*)_vs->high(); } // committed upper bound (exclusive)
63
64 public:
65 static void initialize(CHeapBitMap* ptrmap, VirtualSpace* vs);
66 static void initialize_rw_ro_ac_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, CHeapBitMap* ac_ptrmap);
67 static void mark_pointer(address* ptr_loc);
68 static void clear_pointer(address* ptr_loc);
69 static void compact(address relocatable_base, address relocatable_end);
70 static void compact(size_t max_non_null_offset);
71
72 template <typename T>
73 static void mark_pointer(T* ptr_loc) {
74 mark_pointer((address*)ptr_loc);
75 }
76
77 template <typename T>
78 static void set_and_mark_pointer(T* ptr_loc, T ptr_value) {
79 *ptr_loc = ptr_value;
80 mark_pointer(ptr_loc);
81 }
82
83 static CHeapBitMap* ptrmap() {
84 return _ptrmap;
85 }
86
87 static CHeapBitMap* rw_ptrmap() {
88 return _rw_ptrmap;
89 }
90
91 static CHeapBitMap* ro_ptrmap() {
92 return _ro_ptrmap;
93 }
94
95 static CHeapBitMap* ac_ptrmap() {
96 return _ac_ptrmap;
97 }
98
99 static void reset_map_and_vs() {
100 _ptrmap = nullptr;
101 _rw_ptrmap = nullptr;
102 _ro_ptrmap = nullptr;
103 _ac_ptrmap = nullptr;
104 _vs = nullptr;
105 }
106 };
107
108 // SharedDataRelocator is used to shift pointers in the CDS archive.
109 //
110 // The CDS archive is basically a contiguous block of memory (divided into several regions)
111 // that contains multiple objects. The objects may contain direct pointers that point to other objects
112 // within the archive (e.g., InstanceKlass::_name points to a Symbol in the archive). During dumping, we
113 // built a bitmap that marks the locations of all these pointers (using ArchivePtrMarker, see comments above).
114 //
115 // The contents of the archive assumes that it's mapped at the default SharedBaseAddress (e.g. 0x800000000).
116 // If the archive ends up being mapped at a different address (e.g. 0x810000000), SharedDataRelocator
117 // is used to shift each marked pointer by a delta (0x10000000 in this example), so that it points to
118 // the actually mapped location of the target object.
119 class SharedDataRelocator: public BitMapClosure {
120 // for all (address** p), where (is_marked(p) && _patch_base <= p && p < _patch_end) { *p += delta; }
121
122 // Patch all pointers within this region that are marked.
123 address* _patch_base;
124 address* _patch_end;
125
126 // Before patching, all pointers must point to this region.
127 address _valid_old_base;
128 address _valid_old_end;
129
130 // After patching, all pointers must point to this region.
131 address _valid_new_base;
132 address _valid_new_end;
133
134 // How much to relocate for each pointer.
135 intx _delta;
136
137 public:
138 SharedDataRelocator(address* patch_base, address* patch_end,
139 address valid_old_base, address valid_old_end,
140 address valid_new_base, address valid_new_end, intx delta) :
141 _patch_base(patch_base), _patch_end(patch_end),
142 _valid_old_base(valid_old_base), _valid_old_end(valid_old_end),
143 _valid_new_base(valid_new_base), _valid_new_end(valid_new_end),
144 _delta(delta) {
145 log_debug(aot, reloc)("SharedDataRelocator::_patch_base = " PTR_FORMAT, p2i(_patch_base));
146 log_debug(aot, reloc)("SharedDataRelocator::_patch_end = " PTR_FORMAT, p2i(_patch_end));
147 log_debug(aot, reloc)("SharedDataRelocator::_valid_old_base = " PTR_FORMAT, p2i(_valid_old_base));
148 log_debug(aot, reloc)("SharedDataRelocator::_valid_old_end = " PTR_FORMAT, p2i(_valid_old_end));
149 log_debug(aot, reloc)("SharedDataRelocator::_valid_new_base = " PTR_FORMAT, p2i(_valid_new_base));
150 log_debug(aot, reloc)("SharedDataRelocator::_valid_new_end = " PTR_FORMAT, p2i(_valid_new_end));
151 }
152
153 bool do_bit(size_t offset);
154 };
155
156 class DumpRegion {
157 private:
158 const char* _name;
159 char* _base;
160 char* _top;
161 char* _end;
162 bool _is_packed;
163 ReservedSpace* _rs;
164 VirtualSpace* _vs;
165
166 void commit_to(char* newtop);
167
168 public:
169 DumpRegion(const char* name)
170 : _name(name), _base(nullptr), _top(nullptr), _end(nullptr),
171 _is_packed(false),
172 _rs(nullptr), _vs(nullptr) {}
173
174 char* expand_top_to(char* newtop);
175 char* allocate(size_t num_bytes, size_t alignment = 0);
176
177 void append_intptr_t(intptr_t n, bool need_to_mark = false) NOT_CDS_RETURN;
178
179 char* base() const { return _base; }
180 char* top() const { return _top; }
181 char* end() const { return _end; }
182 size_t reserved() const { return _end - _base; }
183 size_t used() const { return _top - _base; }
184 bool is_packed() const { return _is_packed; }
185 bool is_allocatable() const {
186 return !is_packed() && _base != nullptr;
187 }
188 bool is_empty() const { return _base == _top; }
189
190 void print(size_t total_bytes) const;
191 void print_out_of_space_msg(const char* failing_region, size_t needed_bytes);
192
193 void init(ReservedSpace* rs, VirtualSpace* vs);
194
195 void pack(DumpRegion* next = nullptr);
196
197 bool contains(char* p) {
198 return base() <= p && p < top();
199 }
200 };
201
202 // Closure for serializing initialization data out to a data area to be
203 // written to the shared file.
204
205 class WriteClosure : public SerializeClosure {
206 private:
207 DumpRegion* _dump_region;
208
209 public:
210 WriteClosure(DumpRegion* r) {
211 _dump_region = r;
212 }
213
214 void do_ptr(void** p);
215
216 void do_u4(u4* p) {
217 _dump_region->append_intptr_t((intptr_t)(*p));
218 }
219
220 void do_int(int* p) {
221 _dump_region->append_intptr_t((intptr_t)(*p));
222 }
223
224 void do_bool(bool *p) {
225 _dump_region->append_intptr_t((intptr_t)(*p));
226 }
227
228 void do_tag(int tag) {
229 _dump_region->append_intptr_t((intptr_t)tag);
230 }
231
232 char* region_top() {
233 return _dump_region->top();
234 }
235
236 bool reading() const { return false; }
237 };
238
239 // Closure for serializing initialization data in from a data area
240 // (ptr_array) read from the shared file.
241
242 class ReadClosure : public SerializeClosure {
243 private:
244 intptr_t** _ptr_array;
245 address _base_address;
246 inline intptr_t nextPtr() {
247 return *(*_ptr_array)++;
248 }
249
250 public:
251 ReadClosure(intptr_t** ptr_array, address base_address) :
252 _ptr_array(ptr_array), _base_address(base_address) {}
253
254 void do_ptr(void** p);
255 void do_u4(u4* p);
256 void do_int(int* p);
257 void do_bool(bool *p);
258 void do_tag(int tag);
259 bool reading() const { return true; }
260 char* region_top() { return nullptr; }
261 };
262
263 class ArchiveUtils {
264 template <typename T> static Array<T>* archive_non_ptr_array(GrowableArray<T>* tmp_array);
265 template <typename T> static Array<T>* archive_ptr_array(GrowableArray<T>* tmp_array);
266
267 public:
268 static void log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) NOT_CDS_RETURN;
269 static bool has_aot_initialized_mirror(InstanceKlass* src_ik);
270
271 template <typename T, ENABLE_IF(!std::is_pointer<T>::value)>
272 static Array<T>* archive_array(GrowableArray<T>* tmp_array) {
273 return archive_non_ptr_array(tmp_array);
274 }
275
276 template <typename T, ENABLE_IF(std::is_pointer<T>::value)>
277 static Array<T>* archive_array(GrowableArray<T>* tmp_array) {
278 return archive_ptr_array(tmp_array);
279 }
280
281 static const char* builtin_loader_name_or_null(oop loader); // "boot", "platform", "app", or nullptr
282 static const char* builtin_loader_name(oop loader); // "boot", "platform", or "app". Asserts if not a built-in-loader.
283
284 static bool builtin_loader_from_type(const char* loader_type, oop* value_ret);
285 static oop builtin_loader_from_type(int loader_type);
286 };
287
288 class HeapRootSegments {
289 private:
290 size_t _base_offset;
291 size_t _count;
292 int _roots_count;
293 size_t _max_size_in_bytes;
294 int _max_size_in_elems;
295
296 public:
297 size_t base_offset() { return _base_offset; }
298 size_t count() { return _count; }
299 int roots_count() { return _roots_count; }
300 size_t max_size_in_bytes() { return _max_size_in_bytes; }
301 int max_size_in_elems() { return _max_size_in_elems; }
302
303 size_t size_in_bytes(size_t seg_idx);
304 int size_in_elems(size_t seg_idx);
305 size_t segment_offset(size_t seg_idx);
306
307 // Trivial copy assignments are allowed to copy the entire object representation.
308 // We also inline this class into archive header. Therefore, it is important to make
309 // sure any gaps in object representation are initialized to zeroes. This is why
310 // constructors memset before doing field assignments.
311 HeapRootSegments() {
312 memset(this, 0, sizeof(*this));
313 }
314 HeapRootSegments(size_t base_offset, int roots_count, int max_size_in_bytes, int max_size_in_elems) {
315 memset(this, 0, sizeof(*this));
316 _base_offset = base_offset;
317 _count = (roots_count + max_size_in_elems - 1) / max_size_in_elems;
318 _roots_count = roots_count;
319 _max_size_in_bytes = max_size_in_bytes;
320 _max_size_in_elems = max_size_in_elems;
321 }
322
323 // This class is trivially copyable and assignable.
324 HeapRootSegments(const HeapRootSegments&) = default;
325 HeapRootSegments& operator=(const HeapRootSegments&) = default;
326 };
327
328 class ArchiveWorkers;
329
330 // A task to be worked on by worker threads
331 class ArchiveWorkerTask : public CHeapObj<mtInternal> {
332 friend class ArchiveWorkers;
333 private:
334 const char* _name;
335 int _max_chunks;
336 volatile int _chunk;
337
338 void run();
339
340 void configure_max_chunks(int max_chunks);
341
342 public:
343 ArchiveWorkerTask(const char* name) :
344 _name(name), _max_chunks(0), _chunk(0) {}
345 const char* name() const { return _name; }
346 virtual void work(int chunk, int max_chunks) = 0;
347 };
348
349 class ArchiveWorkerThread : public NamedThread {
350 friend class ArchiveWorkers;
351 private:
352 ArchiveWorkers* const _pool;
353
354 void post_run() override;
355
356 public:
357 ArchiveWorkerThread(ArchiveWorkers* pool);
358 const char* type_name() const override { return "Archive Worker Thread"; }
359 void run() override;
360 };
361
362 // Special archive workers. The goal for this implementation is to startup fast,
363 // distribute spiky workloads efficiently, and shutdown immediately after use.
364 // This makes the implementation quite different from the normal GC worker pool.
365 class ArchiveWorkers : public StackObj {
366 friend class ArchiveWorkerThread;
367 private:
368 // Target number of chunks per worker. This should be large enough to even
369 // out work imbalance, and small enough to keep bookkeeping overheads low.
370 static constexpr int CHUNKS_PER_WORKER = 4;
371 static int max_workers();
372
373 Semaphore _end_semaphore;
374
375 int _num_workers;
376 int _started_workers;
377 int _finish_tokens;
378
379 typedef enum { UNUSED, WORKING, SHUTDOWN } State;
380 volatile State _state;
381
382 ArchiveWorkerTask* _task;
383
384 void run_as_worker();
385 void start_worker_if_needed();
386
387 void run_task_single(ArchiveWorkerTask* task);
388 void run_task_multi(ArchiveWorkerTask* task);
389
390 bool is_parallel();
391
392 public:
393 ArchiveWorkers();
394 ~ArchiveWorkers();
395 void run_task(ArchiveWorkerTask* task);
396 };
397
398 #endif // SHARE_CDS_ARCHIVEUTILS_HPP