< prev index next >

src/hotspot/share/cds/archiveUtils.cpp

Print this page

 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 #include "cds/archiveBuilder.hpp"
 26 #include "cds/archiveHeapLoader.inline.hpp"
 27 #include "cds/archiveUtils.hpp"
 28 #include "cds/cdsConfig.hpp"
 29 #include "cds/classListParser.hpp"
 30 #include "cds/classListWriter.hpp"
 31 #include "cds/dynamicArchive.hpp"
 32 #include "cds/filemap.hpp"
 33 #include "cds/heapShared.hpp"
 34 #include "cds/lambdaProxyClassDictionary.hpp"
 35 #include "cds/metaspaceShared.hpp"

 36 #include "classfile/systemDictionaryShared.hpp"
 37 #include "classfile/vmClasses.hpp"
 38 #include "interpreter/bootstrapInfo.hpp"
 39 #include "memory/metaspaceUtils.hpp"
 40 #include "memory/resourceArea.hpp"
 41 #include "oops/compressedOops.inline.hpp"
 42 #include "oops/klass.inline.hpp"
 43 #include "runtime/arguments.hpp"
 44 #include "utilities/bitMap.inline.hpp"
 45 #include "utilities/debug.hpp"
 46 #include "utilities/formatBuffer.hpp"
 47 #include "utilities/globalDefinitions.hpp"
 48 #include "utilities/spinYield.hpp"
 49 
 50 CHeapBitMap* ArchivePtrMarker::_ptrmap = nullptr;
 51 CHeapBitMap* ArchivePtrMarker::_rw_ptrmap = nullptr;
 52 CHeapBitMap* ArchivePtrMarker::_ro_ptrmap = nullptr;

 53 VirtualSpace* ArchivePtrMarker::_vs;
 54 
 55 bool ArchivePtrMarker::_compacted;
 56 
 57 void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, VirtualSpace* vs) {
 58   assert(_ptrmap == nullptr, "initialize only once");
 59   assert(_rw_ptrmap == nullptr, "initialize only once");
 60   assert(_ro_ptrmap == nullptr, "initialize only once");

 61   _vs = vs;
 62   _compacted = false;
 63   _ptrmap = ptrmap;
 64 
 65   // Use this as initial guesstimate. We should need less space in the
 66   // archive, but if we're wrong the bitmap will be expanded automatically.
 67   size_t estimated_archive_size = MetaspaceGC::capacity_until_GC();
 68   // But set it smaller in debug builds so we always test the expansion code.
 69   // (Default archive is about 12MB).
 70   DEBUG_ONLY(estimated_archive_size = 6 * M);
 71 
 72   // We need one bit per pointer in the archive.
 73   _ptrmap->initialize(estimated_archive_size / sizeof(intptr_t));
 74 }
 75 
 76 void ArchivePtrMarker::initialize_rw_ro_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap) {
 77   address* buff_bottom = (address*)ArchiveBuilder::current()->buffer_bottom();
 78   address* rw_bottom   = (address*)ArchiveBuilder::current()->rw_region()->base();
 79   address* ro_bottom   = (address*)ArchiveBuilder::current()->ro_region()->base();

 80 
 81   // The bit in _ptrmap that cover the very first word in the rw/ro regions.
 82   size_t rw_start = rw_bottom - buff_bottom;
 83   size_t ro_start = ro_bottom - buff_bottom;

 84 
 85   // The number of bits used by the rw/ro ptrmaps. We might have lots of zero
 86   // bits at the bottom and top of rw/ro ptrmaps, but these zeros will be
 87   // removed by FileMapInfo::write_bitmap_region().
 88   size_t rw_size = ArchiveBuilder::current()->rw_region()->used() / sizeof(address);
 89   size_t ro_size = ArchiveBuilder::current()->ro_region()->used() / sizeof(address);

 90 
 91   // The last (exclusive) bit in _ptrmap that covers the rw/ro regions.
 92   // Note: _ptrmap is dynamically expanded only when an actual pointer is written, so
 93   // it may not be as large as we want.
 94   size_t rw_end = MIN2<size_t>(rw_start + rw_size, _ptrmap->size());
 95   size_t ro_end = MIN2<size_t>(ro_start + ro_size, _ptrmap->size());

 96 
 97   rw_ptrmap->initialize(rw_size);
 98   ro_ptrmap->initialize(ro_size);

 99 
100   for (size_t rw_bit = rw_start; rw_bit < rw_end; rw_bit++) {
101     rw_ptrmap->at_put(rw_bit - rw_start, _ptrmap->at(rw_bit));
102   }
103 
104   for(size_t ro_bit = ro_start; ro_bit < ro_end; ro_bit++) {
105     ro_ptrmap->at_put(ro_bit - ro_start, _ptrmap->at(ro_bit));
106   }
107 




108   _rw_ptrmap = rw_ptrmap;
109   _ro_ptrmap = ro_ptrmap;

110 }
111 
112 void ArchivePtrMarker::mark_pointer(address* ptr_loc) {
113   assert(_ptrmap != nullptr, "not initialized");
114   assert(!_compacted, "cannot mark anymore");
115 
116   if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) {
117     address value = *ptr_loc;
118     // We don't want any pointer that points to very bottom of the archive, otherwise when
119     // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer
120     // to nothing (null) vs a pointer to an objects that happens to be at the very bottom
121     // of the archive.
122     assert(value != (address)ptr_base(), "don't point to the bottom of the archive");
123 
124     if (value != nullptr) {
125       assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
126       size_t idx = ptr_loc - ptr_base();
127       if (_ptrmap->size() <= idx) {
128         _ptrmap->resize((idx + 1) * 2);
129       }

283     log_error(cds)(" required = %d", int(needed_bytes));
284   }
285 }
286 
287 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
288   _rs = rs;
289   _vs = vs;
290   // Start with 0 committed bytes. The memory will be committed as needed.
291   if (!_vs->initialize(*_rs, 0)) {
292     fatal("Unable to allocate memory for shared space");
293   }
294   _base = _top = _rs->base();
295   _end = _rs->end();
296 }
297 
298 void DumpRegion::pack(DumpRegion* next) {
299   if (!is_packed()) {
300     _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment());
301     _is_packed = true;
302   }
303   _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment());
304   _is_packed = true;
305   if (next != nullptr) {
306     next->_rs = _rs;
307     next->_vs = _vs;
308     next->_base = next->_top = this->_end;
309     next->_end = _rs->end();
310   }
311 }
312 
313 void WriteClosure::do_ptr(void** p) {
314   // Write ptr into the archive; ptr can be:
315   //   (a) null                 -> written as 0
316   //   (b) a "buffered" address -> written as is
317   //   (c) a "source"   address -> convert to "buffered" and write
318   // The common case is (c). E.g., when writing the vmClasses into the archive.
319   // We have (b) only when we don't have a corresponding source object. E.g.,
320   // the archived c++ vtable entries.
321   address ptr = *(address*)p;
322   if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
323     ptr = ArchiveBuilder::current()->get_buffered_addr(ptr);
324   }

364     if (LambdaProxyClassDictionary::is_supported_invokedynamic(bootstrap_specifier)) {
365       const constantPoolHandle& pool = bootstrap_specifier->pool();
366       if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
367         // Currently lambda proxy classes are supported only for the built-in loaders.
368         ResourceMark rm(THREAD);
369         int pool_index = bootstrap_specifier->bss_index();
370         ClassListWriter w;
371         w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
372         CDSIndyInfo cii;
373         ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
374         GrowableArray<const char*>* indy_items = cii.items();
375         for (int i = 0; i < indy_items->length(); i++) {
376           w.stream()->print(" %s", indy_items->at(i));
377         }
378         w.stream()->cr();
379       }
380     }
381   }
382 }
383 


















































384 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) {
385   if (SystemDictionaryShared::is_excluded_class(src_ik)) {
386     assert(!ArchiveBuilder::current()->has_been_buffered(src_ik), "sanity");
387     return false;
388   }
389   return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror();
390 }
391 
392 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) {
393   assert(seg_idx < _count, "In range");
394   return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize;
395 }
396 
397 int HeapRootSegments::size_in_elems(size_t seg_idx) {
398   assert(seg_idx < _count, "In range");
399   if (seg_idx != _count - 1) {
400     return _max_size_in_elems;
401   } else {
402     // Last slice, leftover
403     return _roots_count % _max_size_in_elems;

 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 #include "cds/archiveBuilder.hpp"
 26 #include "cds/archiveHeapLoader.inline.hpp"
 27 #include "cds/archiveUtils.hpp"
 28 #include "cds/cdsConfig.hpp"
 29 #include "cds/classListParser.hpp"
 30 #include "cds/classListWriter.hpp"
 31 #include "cds/dynamicArchive.hpp"
 32 #include "cds/filemap.hpp"
 33 #include "cds/heapShared.hpp"
 34 #include "cds/lambdaProxyClassDictionary.hpp"
 35 #include "cds/metaspaceShared.hpp"
 36 #include "classfile/classLoader.hpp"
 37 #include "classfile/systemDictionaryShared.hpp"
 38 #include "classfile/vmClasses.hpp"
 39 #include "interpreter/bootstrapInfo.hpp"
 40 #include "memory/metaspaceUtils.hpp"
 41 #include "memory/resourceArea.hpp"
 42 #include "oops/compressedOops.inline.hpp"
 43 #include "oops/klass.inline.hpp"
 44 #include "runtime/arguments.hpp"
 45 #include "utilities/bitMap.inline.hpp"
 46 #include "utilities/debug.hpp"
 47 #include "utilities/formatBuffer.hpp"
 48 #include "utilities/globalDefinitions.hpp"
 49 #include "utilities/spinYield.hpp"
 50 
 51 CHeapBitMap* ArchivePtrMarker::_ptrmap = nullptr;
 52 CHeapBitMap* ArchivePtrMarker::_rw_ptrmap = nullptr;
 53 CHeapBitMap* ArchivePtrMarker::_ro_ptrmap = nullptr;
 54 CHeapBitMap* ArchivePtrMarker::_ac_ptrmap = nullptr;
 55 VirtualSpace* ArchivePtrMarker::_vs;
 56 
 57 bool ArchivePtrMarker::_compacted;
 58 
 59 void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, VirtualSpace* vs) {
 60   assert(_ptrmap == nullptr, "initialize only once");
 61   assert(_rw_ptrmap == nullptr, "initialize only once");
 62   assert(_ro_ptrmap == nullptr, "initialize only once");
 63   assert(_ac_ptrmap == nullptr, "initialize only once");
 64   _vs = vs;
 65   _compacted = false;
 66   _ptrmap = ptrmap;
 67 
 68   // Use this as initial guesstimate. We should need less space in the
 69   // archive, but if we're wrong the bitmap will be expanded automatically.
 70   size_t estimated_archive_size = MetaspaceGC::capacity_until_GC();
 71   // But set it smaller in debug builds so we always test the expansion code.
 72   // (Default archive is about 12MB).
 73   DEBUG_ONLY(estimated_archive_size = 6 * M);
 74 
 75   // We need one bit per pointer in the archive.
 76   _ptrmap->initialize(estimated_archive_size / sizeof(intptr_t));
 77 }
 78 
 79 void ArchivePtrMarker::initialize_rw_ro_ac_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, CHeapBitMap* ac_ptrmap) {
 80   address* buff_bottom = (address*)ArchiveBuilder::current()->buffer_bottom();
 81   address* rw_bottom   = (address*)ArchiveBuilder::current()->rw_region()->base();
 82   address* ro_bottom   = (address*)ArchiveBuilder::current()->ro_region()->base();
 83   address* ac_bottom   = (address*)ArchiveBuilder::current()->ac_region()->base();
 84 
 85   // The bit in _ptrmap that cover the very first word in the rw/ro/ac regions.
 86   size_t rw_start = rw_bottom - buff_bottom;
 87   size_t ro_start = ro_bottom - buff_bottom;
 88   size_t ac_start = ac_bottom - buff_bottom;
 89 
 90   // The number of bits used by the rw/ro ptrmaps. We might have lots of zero
 91   // bits at the bottom and top of rw/ro ptrmaps, but these zeros will be
 92   // removed by FileMapInfo::write_bitmap_region().
 93   size_t rw_size = ArchiveBuilder::current()->rw_region()->used() / sizeof(address);
 94   size_t ro_size = ArchiveBuilder::current()->ro_region()->used() / sizeof(address);
 95   size_t ac_size = ArchiveBuilder::current()->ac_region()->used() / sizeof(address);
 96 
 97   // The last (exclusive) bit in _ptrmap that covers the rw/ro regions.
 98   // Note: _ptrmap is dynamically expanded only when an actual pointer is written, so
 99   // it may not be as large as we want.
100   size_t rw_end = MIN2<size_t>(rw_start + rw_size, _ptrmap->size());
101   size_t ro_end = MIN2<size_t>(ro_start + ro_size, _ptrmap->size());
102   size_t ac_end = MIN2<size_t>(ac_start + ac_size, _ptrmap->size());
103 
104   rw_ptrmap->initialize(rw_size);
105   ro_ptrmap->initialize(ro_size);
106   ac_ptrmap->initialize(ac_size);
107 
108   for (size_t rw_bit = rw_start; rw_bit < rw_end; rw_bit++) {
109     rw_ptrmap->at_put(rw_bit - rw_start, _ptrmap->at(rw_bit));
110   }
111 
112   for(size_t ro_bit = ro_start; ro_bit < ro_end; ro_bit++) {
113     ro_ptrmap->at_put(ro_bit - ro_start, _ptrmap->at(ro_bit));
114   }
115 
116   for (size_t ac_bit = ac_start; ac_bit < ac_end; ac_bit++) {
117     ac_ptrmap->at_put(ac_bit - ac_start, _ptrmap->at(ac_bit));
118   }
119 
120   _rw_ptrmap = rw_ptrmap;
121   _ro_ptrmap = ro_ptrmap;
122   _ac_ptrmap = ac_ptrmap;
123 }
124 
125 void ArchivePtrMarker::mark_pointer(address* ptr_loc) {
126   assert(_ptrmap != nullptr, "not initialized");
127   assert(!_compacted, "cannot mark anymore");
128 
129   if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) {
130     address value = *ptr_loc;
131     // We don't want any pointer that points to very bottom of the archive, otherwise when
132     // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer
133     // to nothing (null) vs a pointer to an objects that happens to be at the very bottom
134     // of the archive.
135     assert(value != (address)ptr_base(), "don't point to the bottom of the archive");
136 
137     if (value != nullptr) {
138       assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
139       size_t idx = ptr_loc - ptr_base();
140       if (_ptrmap->size() <= idx) {
141         _ptrmap->resize((idx + 1) * 2);
142       }

296     log_error(cds)(" required = %d", int(needed_bytes));
297   }
298 }
299 
300 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
301   _rs = rs;
302   _vs = vs;
303   // Start with 0 committed bytes. The memory will be committed as needed.
304   if (!_vs->initialize(*_rs, 0)) {
305     fatal("Unable to allocate memory for shared space");
306   }
307   _base = _top = _rs->base();
308   _end = _rs->end();
309 }
310 
311 void DumpRegion::pack(DumpRegion* next) {
312   if (!is_packed()) {
313     _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment());
314     _is_packed = true;
315   }


316   if (next != nullptr) {
317     next->_rs = _rs;
318     next->_vs = _vs;
319     next->_base = next->_top = this->_end;
320     next->_end = _rs->end();
321   }
322 }
323 
324 void WriteClosure::do_ptr(void** p) {
325   // Write ptr into the archive; ptr can be:
326   //   (a) null                 -> written as 0
327   //   (b) a "buffered" address -> written as is
328   //   (c) a "source"   address -> convert to "buffered" and write
329   // The common case is (c). E.g., when writing the vmClasses into the archive.
330   // We have (b) only when we don't have a corresponding source object. E.g.,
331   // the archived c++ vtable entries.
332   address ptr = *(address*)p;
333   if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
334     ptr = ArchiveBuilder::current()->get_buffered_addr(ptr);
335   }

375     if (LambdaProxyClassDictionary::is_supported_invokedynamic(bootstrap_specifier)) {
376       const constantPoolHandle& pool = bootstrap_specifier->pool();
377       if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
378         // Currently lambda proxy classes are supported only for the built-in loaders.
379         ResourceMark rm(THREAD);
380         int pool_index = bootstrap_specifier->bss_index();
381         ClassListWriter w;
382         w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
383         CDSIndyInfo cii;
384         ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
385         GrowableArray<const char*>* indy_items = cii.items();
386         for (int i = 0; i < indy_items->length(); i++) {
387           w.stream()->print(" %s", indy_items->at(i));
388         }
389         w.stream()->cr();
390       }
391     }
392   }
393 }
394 
395 
396 // "boot", "platform", "app" or nullptr
397 const char* ArchiveUtils::builtin_loader_name_or_null(oop loader) {
398   if (loader == nullptr) {
399     return "boot";
400   } else if (loader == SystemDictionary::java_platform_loader()) {
401     return "platform";
402   } else if (loader == SystemDictionary::java_system_loader()) {
403     return "app";
404   } else {
405     return nullptr;
406   }
407 }
408 
409 // "boot", "platform", "app". Asserts if not a built-in-loader
410 const char* ArchiveUtils::builtin_loader_name(oop loader) {
411   const char* name = builtin_loader_name_or_null(loader);
412   assert(name != nullptr, "must be a built-in loader");
413   return name;
414 }
415 
416 bool ArchiveUtils::builtin_loader_from_type(const char* loader_type, oop* value_ret) {
417   if (strcmp(loader_type, "boot") == 0) {
418     *value_ret = nullptr;
419     return true;
420   } else if (strcmp(loader_type, "platform") == 0) {
421     *value_ret = SystemDictionary::java_platform_loader();
422     return true;
423   } else if (strcmp(loader_type, "app") == 0) {
424     *value_ret = SystemDictionary::java_system_loader();
425     return true;
426   } else {
427     DEBUG_ONLY(*value_ret = cast_to_oop((void*)badOopVal));
428     return false;
429   }
430 }
431 
432 oop ArchiveUtils::builtin_loader_from_type(int loader_type) {
433   if (loader_type == ClassLoader::BOOT_LOADER) {
434     return nullptr;
435   } else if (loader_type == ClassLoader::PLATFORM_LOADER)  {
436     return SystemDictionary::java_platform_loader();
437   } else if (loader_type == ClassLoader::APP_LOADER) {
438     return SystemDictionary::java_system_loader();
439   } else {
440     ShouldNotReachHere();
441     return nullptr;
442   }
443 }
444 
445 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) {
446   if (SystemDictionaryShared::is_excluded_class(src_ik)) {
447     assert(!ArchiveBuilder::current()->has_been_buffered(src_ik), "sanity");
448     return false;
449   }
450   return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror();
451 }
452 
453 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) {
454   assert(seg_idx < _count, "In range");
455   return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize;
456 }
457 
458 int HeapRootSegments::size_in_elems(size_t seg_idx) {
459   assert(seg_idx < _count, "In range");
460   if (seg_idx != _count - 1) {
461     return _max_size_in_elems;
462   } else {
463     // Last slice, leftover
464     return _roots_count % _max_size_in_elems;
< prev index next >