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