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