< prev index next >

src/hotspot/share/cds/archiveBuilder.hpp

Print this page

125   class SourceObjInfo {
126     uintx _ptrmap_start;     // The bit-offset of the start of this object (inclusive)
127     uintx _ptrmap_end;       // The bit-offset of the end   of this object (exclusive)
128     bool _read_only;
129     FollowMode _follow_mode;
130     int _size_in_bytes;
131     MetaspaceObj::Type _msotype;
132     address _source_addr;    // The source object to be copied.
133     address _buffered_addr;  // The copy of this object insider the buffer.
134   public:
135     SourceObjInfo(MetaspaceClosure::Ref* ref, bool read_only, FollowMode follow_mode) :
136       _ptrmap_start(0), _ptrmap_end(0), _read_only(read_only), _follow_mode(follow_mode),
137       _size_in_bytes(ref->size() * BytesPerWord), _msotype(ref->msotype()),
138       _source_addr(ref->obj()) {
139       if (follow_mode == point_to_it) {
140         _buffered_addr = ref->obj();
141       } else {
142         _buffered_addr = nullptr;
143       }
144     }




145 
146     // This constructor is only used for regenerated objects (created by LambdaFormInvokers, etc).
147     //   src = address of a Method or InstanceKlass that has been regenerated.
148     //   renegerated_obj_info = info for the regenerated version of src.
149     SourceObjInfo(address src, SourceObjInfo* renegerated_obj_info) :
150       _ptrmap_start(0), _ptrmap_end(0), _read_only(false),
151       _follow_mode(renegerated_obj_info->_follow_mode),
152       _size_in_bytes(0), _msotype(renegerated_obj_info->_msotype),
153       _source_addr(src),  _buffered_addr(renegerated_obj_info->_buffered_addr) {}
154 
155     bool should_copy() const { return _follow_mode == make_a_copy; }
156     void set_buffered_addr(address addr)  {
157       assert(should_copy(), "must be");
158       assert(_buffered_addr == nullptr, "cannot be copied twice");
159       assert(addr != nullptr, "must be a valid copy");
160       _buffered_addr = addr;
161     }
162     void set_ptrmap_start(uintx v) { _ptrmap_start = v;    }
163     void set_ptrmap_end(uintx v)   { _ptrmap_end = v;      }
164     uintx ptrmap_start()  const    { return _ptrmap_start; } // inclusive

187     GrowableArray<SourceObjInfo*>* objs() const { return _objs; }
188 
189     void append(SourceObjInfo* src_info);
190     void remember_embedded_pointer(SourceObjInfo* pointing_obj, MetaspaceClosure::Ref* ref);
191     void relocate(int i, ArchiveBuilder* builder);
192 
193     // convenience accessor
194     SourceObjInfo* at(int i) const { return objs()->at(i); }
195   };
196 
197   class CDSMapLogger;
198 
199   static const int INITIAL_TABLE_SIZE = 15889;
200   static const int MAX_TABLE_SIZE     = 1000000;
201 
202   ReservedSpace _shared_rs;
203   VirtualSpace _shared_vs;
204 
205   DumpRegion _rw_region;
206   DumpRegion _ro_region;

207   CHeapBitMap _ptrmap;    // bitmap used by ArchivePtrMarker
208 
209   SourceObjList _rw_src_objs;                 // objs to put in rw region
210   SourceObjList _ro_src_objs;                 // objs to put in ro region
211   ResizeableResourceHashtable<address, SourceObjInfo, AnyObj::C_HEAP, mtClassShared> _src_obj_table;
212   ResizeableResourceHashtable<address, address, AnyObj::C_HEAP, mtClassShared> _buffered_to_src_table;
213   GrowableArray<Klass*>* _klasses;
214   GrowableArray<Symbol*>* _symbols;
215 
216   // statistics
217   DumpAllocStats _alloc_stats;
218   size_t _total_heap_region_size;
219 
220   void print_region_stats(FileMapInfo *map_info, ArchiveHeapInfo* heap_info);
221   void print_bitmap_region_stats(size_t size, size_t total_size);
222   void print_heap_region_stats(ArchiveHeapInfo* heap_info, size_t total_size);
223 
224   // For global access.
225   static ArchiveBuilder* _current;
226 
227 public:
228   // Use this when you allocate space outside of ArchiveBuilder::dump_{rw,ro}_region.
229   // These are usually for misc tables that are allocated in the RO space.
230   class OtherROAllocMark {
231     char* _oldtop;
232   public:
233     OtherROAllocMark() {
234       _oldtop = _current->_ro_region.top();
235     }
236     ~OtherROAllocMark();
237   };
238 
239 private:
240   FollowMode get_follow_mode(MetaspaceClosure::Ref *ref);
241 
242   void iterate_sorted_roots(MetaspaceClosure* it);
243   void sort_klasses();
244   static int compare_symbols_by_address(Symbol** a, Symbol** b);
245   static int compare_klass_by_name(Klass** a, Klass** b);

246 
247   void make_shallow_copies(DumpRegion *dump_region, const SourceObjList* src_objs);
248   void make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info);
249 
250   void relocate_embedded_pointers(SourceObjList* src_objs);
251 
252   bool is_excluded(Klass* k);
253   void clean_up_src_obj_table();
254 
255 protected:
256   virtual void iterate_roots(MetaspaceClosure* it) = 0;
257 
258   // Conservative estimate for number of bytes needed for:
259   size_t _estimated_metaspaceobj_bytes;   // all archived MetaspaceObj's.
260   size_t _estimated_hashtable_bytes;     // symbol table and dictionaries
261 
262   static const int _total_dump_regions = 2;
263 
264   size_t estimate_archive_size();
265 
266   void start_dump_space(DumpRegion* next);
267   void verify_estimate_size(size_t estimate, const char* which);
268 
269 public:
270   address reserve_buffer();
271 
272   address buffer_bottom()                    const { return _buffer_bottom;                       }
273   address buffer_top()                       const { return (address)current_dump_space()->top(); }
274   address requested_static_archive_bottom()  const { return  _requested_static_archive_bottom;    }
275   address mapped_static_archive_bottom()     const { return  _mapped_static_archive_bottom;       }
276   intx buffer_to_requested_delta()           const { return _buffer_to_requested_delta;           }
277 
278   bool is_in_buffer_space(address p) const {



279     return (buffer_bottom() <= p && p < buffer_top());
280   }
281 
282   template <typename T> bool is_in_requested_static_archive(T p) const {
283     return _requested_static_archive_bottom <= (address)p && (address)p < _requested_static_archive_top;
284   }
285 
286   template <typename T> bool is_in_mapped_static_archive(T p) const {
287     return _mapped_static_archive_bottom <= (address)p && (address)p < _mapped_static_archive_top;
288   }
289 
290   template <typename T> bool is_in_buffer_space(T obj) const {
291     return is_in_buffer_space(address(obj));
292   }
293 
294   template <typename T> T to_requested(T obj) const {
295     assert(is_in_buffer_space(obj), "must be");
296     return (T)(address(obj) + _buffer_to_requested_delta);
297   }
298 

311   // The address p points to an object inside the output buffer. When the archive is mapped
312   // at the requested address, what's the offset of this object from _requested_static_archive_bottom?
313   uintx buffer_to_offset(address p) const;
314 
315   // Same as buffer_to_offset, except that the address p points to either (a) an object
316   // inside the output buffer, or (b), an object in the currently mapped static archive.
317   uintx any_to_offset(address p) const;
318 
319   template <typename T>
320   u4 buffer_to_offset_u4(T p) const {
321     uintx offset = buffer_to_offset((address)p);
322     return to_offset_u4(offset);
323   }
324 
325   template <typename T>
326   u4 any_to_offset_u4(T p) const {
327     uintx offset = any_to_offset((address)p);
328     return to_offset_u4(offset);
329   }
330 
331   static void assert_is_vm_thread() PRODUCT_RETURN;
332 
333 public:
334   ArchiveBuilder();
335   ~ArchiveBuilder();
336 
337   void gather_klasses_and_symbols();
338   void gather_source_objs();
339   bool gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only);
340   bool gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only);
341   void remember_embedded_pointer_in_enclosing_obj(MetaspaceClosure::Ref* ref);
342   static void serialize_dynamic_archivable_items(SerializeClosure* soc);
343 
344   DumpRegion* rw_region() { return &_rw_region; }
345   DumpRegion* ro_region() { return &_ro_region; }




346 
347   static char* rw_region_alloc(size_t num_bytes) {
348     return current()->rw_region()->allocate(num_bytes);
349   }
350   static char* ro_region_alloc(size_t num_bytes) {
351     return current()->ro_region()->allocate(num_bytes);
352   }



353 
354   template <typename T>
355   static Array<T>* new_ro_array(int length) {
356     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
357     Array<T>* array = (Array<T>*)ro_region_alloc(byte_size);
358     array->initialize(length);
359     return array;
360   }
361 
362   template <typename T>
363   static Array<T>* new_rw_array(int length) {
364     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
365     Array<T>* array = (Array<T>*)rw_region_alloc(byte_size);
366     array->initialize(length);
367     return array;
368   }
369 
370   template <typename T>
371   static size_t ro_array_bytesize(int length) {
372     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
373     return align_up(byte_size, SharedSpaceObjectAlignment);
374   }
375 
376   char* ro_strdup(const char* s);
377 
378   void dump_rw_metadata();
379   void dump_ro_metadata();
380   void relocate_metaspaceobj_embedded_pointers();
381   void record_regenerated_object(address orig_src_obj, address regen_src_obj);
382   void make_klasses_shareable();
383   void relocate_to_requested();
384   void write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info);
385   void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region,
386                     bool read_only,  bool allow_exec);
387 
388   void write_pointer_in_buffer(address* ptr_location, address src_addr);
389   template <typename T> void write_pointer_in_buffer(T* ptr_location, T src_addr) {
390     write_pointer_in_buffer((address*)ptr_location, (address)src_addr);
391   }
392 






393   address get_buffered_addr(address src_addr) const;
394   template <typename T> T get_buffered_addr(T src_addr) const {
395     return (T)get_buffered_addr((address)src_addr);
396   }
397 
398   address get_source_addr(address buffered_addr) const;
399   template <typename T> T get_source_addr(T buffered_addr) const {
400     return (T)get_source_addr((address)buffered_addr);
401   }
402 
403   // All klasses and symbols that will be copied into the archive
404   GrowableArray<Klass*>*  klasses() const { return _klasses; }
405   GrowableArray<Symbol*>* symbols() const { return _symbols; }
406 
407   static bool is_active() {
408     return (_current != nullptr);

409   }
410 
411   static ArchiveBuilder* current() {
412     assert_is_vm_thread();
413     assert(_current != nullptr, "ArchiveBuilder must be active");
414     return _current;
415   }
416 
417   static DumpAllocStats* alloc_stats() {
418     return &(current()->_alloc_stats);
419   }
420 
421   static CompactHashtableStats* symbol_stats() {
422     return alloc_stats()->symbol_stats();
423   }
424 
425   static CompactHashtableStats* string_stats() {
426     return alloc_stats()->string_stats();
427   }
428 
429   narrowKlass get_requested_narrow_klass(Klass* k);
430 
431   static Klass* get_buffered_klass(Klass* src_klass) {
432     Klass* klass = (Klass*)current()->get_buffered_addr((address)src_klass);

125   class SourceObjInfo {
126     uintx _ptrmap_start;     // The bit-offset of the start of this object (inclusive)
127     uintx _ptrmap_end;       // The bit-offset of the end   of this object (exclusive)
128     bool _read_only;
129     FollowMode _follow_mode;
130     int _size_in_bytes;
131     MetaspaceObj::Type _msotype;
132     address _source_addr;    // The source object to be copied.
133     address _buffered_addr;  // The copy of this object insider the buffer.
134   public:
135     SourceObjInfo(MetaspaceClosure::Ref* ref, bool read_only, FollowMode follow_mode) :
136       _ptrmap_start(0), _ptrmap_end(0), _read_only(read_only), _follow_mode(follow_mode),
137       _size_in_bytes(ref->size() * BytesPerWord), _msotype(ref->msotype()),
138       _source_addr(ref->obj()) {
139       if (follow_mode == point_to_it) {
140         _buffered_addr = ref->obj();
141       } else {
142         _buffered_addr = nullptr;
143       }
144     }
145     SourceObjInfo(address src, address buf) {
146       _source_addr = src;
147       _buffered_addr = buf;
148     }
149 
150     // This constructor is only used for regenerated objects (created by LambdaFormInvokers, etc).
151     //   src = address of a Method or InstanceKlass that has been regenerated.
152     //   renegerated_obj_info = info for the regenerated version of src.
153     SourceObjInfo(address src, SourceObjInfo* renegerated_obj_info) :
154       _ptrmap_start(0), _ptrmap_end(0), _read_only(false),
155       _follow_mode(renegerated_obj_info->_follow_mode),
156       _size_in_bytes(0), _msotype(renegerated_obj_info->_msotype),
157       _source_addr(src),  _buffered_addr(renegerated_obj_info->_buffered_addr) {}
158 
159     bool should_copy() const { return _follow_mode == make_a_copy; }
160     void set_buffered_addr(address addr)  {
161       assert(should_copy(), "must be");
162       assert(_buffered_addr == nullptr, "cannot be copied twice");
163       assert(addr != nullptr, "must be a valid copy");
164       _buffered_addr = addr;
165     }
166     void set_ptrmap_start(uintx v) { _ptrmap_start = v;    }
167     void set_ptrmap_end(uintx v)   { _ptrmap_end = v;      }
168     uintx ptrmap_start()  const    { return _ptrmap_start; } // inclusive

191     GrowableArray<SourceObjInfo*>* objs() const { return _objs; }
192 
193     void append(SourceObjInfo* src_info);
194     void remember_embedded_pointer(SourceObjInfo* pointing_obj, MetaspaceClosure::Ref* ref);
195     void relocate(int i, ArchiveBuilder* builder);
196 
197     // convenience accessor
198     SourceObjInfo* at(int i) const { return objs()->at(i); }
199   };
200 
201   class CDSMapLogger;
202 
203   static const int INITIAL_TABLE_SIZE = 15889;
204   static const int MAX_TABLE_SIZE     = 1000000;
205 
206   ReservedSpace _shared_rs;
207   VirtualSpace _shared_vs;
208 
209   DumpRegion _rw_region;
210   DumpRegion _ro_region;
211   DumpRegion _cc_region;
212   CHeapBitMap _ptrmap;    // bitmap used by ArchivePtrMarker
213 
214   SourceObjList _rw_src_objs;                 // objs to put in rw region
215   SourceObjList _ro_src_objs;                 // objs to put in ro region
216   ResizeableResourceHashtable<address, SourceObjInfo, AnyObj::C_HEAP, mtClassShared> _src_obj_table;
217   ResizeableResourceHashtable<address, address, AnyObj::C_HEAP, mtClassShared> _buffered_to_src_table;
218   GrowableArray<Klass*>* _klasses;
219   GrowableArray<Symbol*>* _symbols;
220 
221   // statistics
222   DumpAllocStats _alloc_stats;
223   size_t _total_heap_region_size;
224 
225   void print_region_stats(FileMapInfo *map_info, ArchiveHeapInfo* heap_info);
226   void print_bitmap_region_stats(size_t size, size_t total_size);
227   void print_heap_region_stats(ArchiveHeapInfo* heap_info, size_t total_size);
228 
229   // For global access.
230   static ArchiveBuilder* _current;
231 
232 public:
233   // Use this when you allocate space outside of ArchiveBuilder::dump_{rw,ro}_region.
234   // These are usually for misc tables that are allocated in the RO space.
235   class OtherROAllocMark {
236     char* _oldtop;
237   public:
238     OtherROAllocMark() {
239       _oldtop = _current->_ro_region.top();
240     }
241     ~OtherROAllocMark();
242   };
243 
244 private:
245   FollowMode get_follow_mode(MetaspaceClosure::Ref *ref);
246 
247   void iterate_sorted_roots(MetaspaceClosure* it);
248   void sort_klasses();
249   static int compare_symbols_by_address(Symbol** a, Symbol** b);
250   static int compare_klass_by_name(Klass** a, Klass** b);
251   void update_hidden_class_loader_type(InstanceKlass* ik);
252 
253   void make_shallow_copies(DumpRegion *dump_region, const SourceObjList* src_objs);
254   void make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info);
255 
256   void relocate_embedded_pointers(SourceObjList* src_objs);
257 
258   bool is_excluded(Klass* k);
259   void clean_up_src_obj_table();
260 
261 protected:
262   virtual void iterate_roots(MetaspaceClosure* it) = 0;
263 
264   // Conservative estimate for number of bytes needed for:
265   size_t _estimated_metaspaceobj_bytes;   // all archived MetaspaceObj's.
266   size_t _estimated_hashtable_bytes;     // symbol table and dictionaries
267 
268   static const int _total_dump_regions = 2;
269 
270   size_t estimate_archive_size();
271 
272   void start_dump_space(DumpRegion* next);
273   void verify_estimate_size(size_t estimate, const char* which);
274 
275 public:
276   address reserve_buffer();
277 
278   address buffer_bottom()                    const { return _buffer_bottom;                       }
279   address buffer_top()                       const { return (address)current_dump_space()->top(); }
280   address requested_static_archive_bottom()  const { return  _requested_static_archive_bottom;    }
281   address mapped_static_archive_bottom()     const { return  _mapped_static_archive_bottom;       }
282   intx buffer_to_requested_delta()           const { return _buffer_to_requested_delta;           }
283 
284   bool is_in_buffer_space(address p) const {
285     if (current_dump_space() == nullptr) {
286       return false;
287     }
288     return (buffer_bottom() <= p && p < buffer_top());
289   }
290 
291   template <typename T> bool is_in_requested_static_archive(T p) const {
292     return _requested_static_archive_bottom <= (address)p && (address)p < _requested_static_archive_top;
293   }
294 
295   template <typename T> bool is_in_mapped_static_archive(T p) const {
296     return _mapped_static_archive_bottom <= (address)p && (address)p < _mapped_static_archive_top;
297   }
298 
299   template <typename T> bool is_in_buffer_space(T obj) const {
300     return is_in_buffer_space(address(obj));
301   }
302 
303   template <typename T> T to_requested(T obj) const {
304     assert(is_in_buffer_space(obj), "must be");
305     return (T)(address(obj) + _buffer_to_requested_delta);
306   }
307 

320   // The address p points to an object inside the output buffer. When the archive is mapped
321   // at the requested address, what's the offset of this object from _requested_static_archive_bottom?
322   uintx buffer_to_offset(address p) const;
323 
324   // Same as buffer_to_offset, except that the address p points to either (a) an object
325   // inside the output buffer, or (b), an object in the currently mapped static archive.
326   uintx any_to_offset(address p) const;
327 
328   template <typename T>
329   u4 buffer_to_offset_u4(T p) const {
330     uintx offset = buffer_to_offset((address)p);
331     return to_offset_u4(offset);
332   }
333 
334   template <typename T>
335   u4 any_to_offset_u4(T p) const {
336     uintx offset = any_to_offset((address)p);
337     return to_offset_u4(offset);
338   }
339 


340 public:
341   ArchiveBuilder();
342   ~ArchiveBuilder();
343 
344   void gather_klasses_and_symbols();
345   void gather_source_objs();
346   bool gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only);
347   bool gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only);
348   void remember_embedded_pointer_in_enclosing_obj(MetaspaceClosure::Ref* ref);
349   static void serialize_dynamic_archivable_items(SerializeClosure* soc);
350 
351   DumpRegion* rw_region() { return &_rw_region; }
352   DumpRegion* ro_region() { return &_ro_region; }
353   DumpRegion* cc_region() { return &_cc_region; }
354 
355   void start_cc_region();
356   void end_cc_region();
357 
358   static char* rw_region_alloc(size_t num_bytes) {
359     return current()->rw_region()->allocate(num_bytes);
360   }
361   static char* ro_region_alloc(size_t num_bytes) {
362     return current()->ro_region()->allocate(num_bytes);
363   }
364   static char* cc_region_alloc(size_t num_bytes) {
365     return current()->cc_region()->allocate(num_bytes);
366   }
367 
368   template <typename T>
369   static Array<T>* new_ro_array(int length) {
370     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
371     Array<T>* array = (Array<T>*)ro_region_alloc(byte_size);
372     array->initialize(length);
373     return array;
374   }
375 
376   template <typename T>
377   static Array<T>* new_rw_array(int length) {
378     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
379     Array<T>* array = (Array<T>*)rw_region_alloc(byte_size);
380     array->initialize(length);
381     return array;
382   }
383 
384   template <typename T>
385   static size_t ro_array_bytesize(int length) {
386     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
387     return align_up(byte_size, SharedSpaceObjectAlignment);
388   }
389 
390   char* ro_strdup(const char* s);
391 
392   void dump_rw_metadata();
393   void dump_ro_metadata();
394   void relocate_metaspaceobj_embedded_pointers();
395   void record_regenerated_object(address orig_src_obj, address regen_src_obj);
396   void make_klasses_shareable();
397   void relocate_to_requested();
398   void write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info);
399   void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region,
400                     bool read_only,  bool allow_exec);
401 
402   void write_pointer_in_buffer(address* ptr_location, address src_addr);
403   template <typename T> void write_pointer_in_buffer(T* ptr_location, T src_addr) {
404     write_pointer_in_buffer((address*)ptr_location, (address)src_addr);
405   }
406 
407   void mark_and_relocate_to_buffered_addr(address* ptr_location);
408   template <typename T> void mark_and_relocate_to_buffered_addr(T ptr_location) {
409     mark_and_relocate_to_buffered_addr((address*)ptr_location);
410   }
411 
412   bool has_been_archived(address src_addr) const;
413   address get_buffered_addr(address src_addr) const;
414   template <typename T> T get_buffered_addr(T src_addr) const {
415     return (T)get_buffered_addr((address)src_addr);
416   }
417 
418   address get_source_addr(address buffered_addr) const;
419   template <typename T> T get_source_addr(T buffered_addr) const {
420     return (T)get_source_addr((address)buffered_addr);
421   }
422 
423   // All klasses and symbols that will be copied into the archive
424   GrowableArray<Klass*>*  klasses() const { return _klasses; }
425   GrowableArray<Symbol*>* symbols() const { return _symbols; }
426 
427   static bool is_active() {
428     CDS_ONLY(return (_current != nullptr));
429     NOT_CDS(return false;)
430   }
431 
432   static ArchiveBuilder* current() {

433     assert(_current != nullptr, "ArchiveBuilder must be active");
434     return _current;
435   }
436 
437   static DumpAllocStats* alloc_stats() {
438     return &(current()->_alloc_stats);
439   }
440 
441   static CompactHashtableStats* symbol_stats() {
442     return alloc_stats()->symbol_stats();
443   }
444 
445   static CompactHashtableStats* string_stats() {
446     return alloc_stats()->string_stats();
447   }
448 
449   narrowKlass get_requested_narrow_klass(Klass* k);
450 
451   static Klass* get_buffered_klass(Klass* src_klass) {
452     Klass* klass = (Klass*)current()->get_buffered_addr((address)src_klass);
< prev index next >