< prev index next >

src/hotspot/share/cds/archiveUtils.cpp

Print this page

 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/aotCompressedPointers.hpp"
 26 #include "cds/aotLogging.hpp"
 27 #include "cds/aotMetaspace.hpp"
 28 #include "cds/archiveBuilder.hpp"
 29 #include "cds/archiveUtils.hpp"
 30 #include "cds/cdsConfig.hpp"
 31 #include "cds/classListParser.hpp"
 32 #include "cds/classListWriter.hpp"
 33 #include "cds/dumpAllocStats.hpp"
 34 #include "cds/dynamicArchive.hpp"
 35 #include "cds/filemap.hpp"
 36 #include "cds/heapShared.hpp"
 37 #include "cds/lambdaProxyClassDictionary.hpp"

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

 56 VirtualSpace* ArchivePtrMarker::_vs;
 57 
 58 bool ArchivePtrMarker::_compacted;
 59 
 60 void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, VirtualSpace* vs) {
 61   assert(_ptrmap == nullptr, "initialize only once");
 62   assert(_rw_ptrmap == nullptr, "initialize only once");
 63   assert(_ro_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_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_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 
 84   // The bit in _ptrmap that cover the very first word in the rw/ro regions.
 85   size_t rw_start = rw_bottom - buff_bottom;
 86   size_t ro_start = ro_bottom - buff_bottom;

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

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

 99 
100   rw_ptrmap->initialize(rw_size);
101   ro_ptrmap->initialize(ro_size);

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




111   _rw_ptrmap = rw_ptrmap;
112   _ro_ptrmap = ro_ptrmap;

113 }
114 
115 void ArchivePtrMarker::mark_pointer(address* ptr_loc) {
116   assert(_ptrmap != nullptr, "not initialized");
117   assert(!_compacted, "cannot mark anymore");
118 
119   if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) {
120     address value = *ptr_loc;
121     if (value != nullptr) {
122       // We don't want any pointer that points to very bottom of the AOT metaspace, otherwise
123       // when AOTMetaspace::default_base_address()==0, we can't distinguish between a pointer
124       // to nothing (null) vs a pointer to an objects that happens to be at the very bottom
125       // of the AOT metaspace.
126       //
127       // This should never happen because the protection zone prevents any valid objects from
128       // being allocated at the bottom of the AOT metaspace.
129       assert(AOTMetaspace::protection_zone_size() > 0, "must be");
130       assert(ArchiveBuilder::current()->any_to_offset(value) > 0, "cannot point to bottom of AOT metaspace");
131 
132       assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");

451     aot_log_error(aot)(" required = %d", int(needed_bytes));
452   }
453 }
454 
455 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
456   _rs = rs;
457   _vs = vs;
458   // Start with 0 committed bytes. The memory will be committed as needed.
459   if (!_vs->initialize(*_rs, 0)) {
460     fatal("Unable to allocate memory for shared space");
461   }
462   _base = _top = _rs->base();
463   _end = _rs->end();
464 }
465 
466 void DumpRegion::pack(DumpRegion* next) {
467   if (!is_packed()) {
468     _end = (char*)align_up(_top, AOTMetaspace::core_region_alignment());
469     _is_packed = true;
470   }
471   _end = (char*)align_up(_top, AOTMetaspace::core_region_alignment());
472   _is_packed = true;
473   if (next != nullptr) {
474     next->_rs = _rs;
475     next->_vs = _vs;
476     next->_base = next->_top = this->_end;
477     next->_end = _rs->end();
478   }
479 }
480 
481 void WriteClosure::do_ptr(void** p) {
482   address ptr = *(address*)p;
483   AOTCompressedPointers::narrowPtr narrowp = AOTCompressedPointers::encode(ptr);
484   _dump_region->append_intptr_t(checked_cast<intptr_t>(narrowp), false);
485 }
486 
487 void ReadClosure::do_ptr(void** p) {
488   assert(*p == nullptr, "initializing previous initialized pointer.");
489   u4 narrowp = checked_cast<u4>(nextPtr());
490   *p = AOTCompressedPointers::decode<void*>(cast_from_u4(narrowp), _base_address);
491 }
492 

518     if (LambdaProxyClassDictionary::is_supported_invokedynamic(bootstrap_specifier)) {
519       const constantPoolHandle& pool = bootstrap_specifier->pool();
520       if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
521         // Currently lambda proxy classes are supported only for the built-in loaders.
522         ResourceMark rm(THREAD);
523         int pool_index = bootstrap_specifier->bss_index();
524         ClassListWriter w;
525         w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
526         CDSIndyInfo cii;
527         ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
528         GrowableArray<const char*>* indy_items = cii.items();
529         for (int i = 0; i < indy_items->length(); i++) {
530           w.stream()->print(" %s", indy_items->at(i));
531         }
532         w.stream()->cr();
533       }
534     }
535   }
536 }
537 


















































538 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) {
539   if (!ArchiveBuilder::current()->has_been_archived(src_ik)) {
540     return false;
541   }
542   return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror();
543 }
544 
545 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) {
546   assert(seg_idx < _count, "In range");
547   return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize;
548 }
549 
550 int HeapRootSegments::size_in_elems(size_t seg_idx) {
551   assert(seg_idx < _count, "In range");
552   if (seg_idx != _count - 1) {
553     return _max_size_in_elems;
554   } else {
555     // Last slice, leftover
556     return _roots_count % _max_size_in_elems;
557   }

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

464     aot_log_error(aot)(" required = %d", int(needed_bytes));
465   }
466 }
467 
468 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
469   _rs = rs;
470   _vs = vs;
471   // Start with 0 committed bytes. The memory will be committed as needed.
472   if (!_vs->initialize(*_rs, 0)) {
473     fatal("Unable to allocate memory for shared space");
474   }
475   _base = _top = _rs->base();
476   _end = _rs->end();
477 }
478 
479 void DumpRegion::pack(DumpRegion* next) {
480   if (!is_packed()) {
481     _end = (char*)align_up(_top, AOTMetaspace::core_region_alignment());
482     _is_packed = true;
483   }


484   if (next != nullptr) {
485     next->_rs = _rs;
486     next->_vs = _vs;
487     next->_base = next->_top = this->_end;
488     next->_end = _rs->end();
489   }
490 }
491 
492 void WriteClosure::do_ptr(void** p) {
493   address ptr = *(address*)p;
494   AOTCompressedPointers::narrowPtr narrowp = AOTCompressedPointers::encode(ptr);
495   _dump_region->append_intptr_t(checked_cast<intptr_t>(narrowp), false);
496 }
497 
498 void ReadClosure::do_ptr(void** p) {
499   assert(*p == nullptr, "initializing previous initialized pointer.");
500   u4 narrowp = checked_cast<u4>(nextPtr());
501   *p = AOTCompressedPointers::decode<void*>(cast_from_u4(narrowp), _base_address);
502 }
503 

529     if (LambdaProxyClassDictionary::is_supported_invokedynamic(bootstrap_specifier)) {
530       const constantPoolHandle& pool = bootstrap_specifier->pool();
531       if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
532         // Currently lambda proxy classes are supported only for the built-in loaders.
533         ResourceMark rm(THREAD);
534         int pool_index = bootstrap_specifier->bss_index();
535         ClassListWriter w;
536         w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
537         CDSIndyInfo cii;
538         ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
539         GrowableArray<const char*>* indy_items = cii.items();
540         for (int i = 0; i < indy_items->length(); i++) {
541           w.stream()->print(" %s", indy_items->at(i));
542         }
543         w.stream()->cr();
544       }
545     }
546   }
547 }
548 
549 
550 // "boot", "platform", "app" or nullptr
551 const char* ArchiveUtils::builtin_loader_name_or_null(oop loader) {
552   if (loader == nullptr) {
553     return "boot";
554   } else if (loader == SystemDictionary::java_platform_loader()) {
555     return "platform";
556   } else if (loader == SystemDictionary::java_system_loader()) {
557     return "app";
558   } else {
559     return nullptr;
560   }
561 }
562 
563 // "boot", "platform", "app". Asserts if not a built-in-loader
564 const char* ArchiveUtils::builtin_loader_name(oop loader) {
565   const char* name = builtin_loader_name_or_null(loader);
566   assert(name != nullptr, "must be a built-in loader");
567   return name;
568 }
569 
570 bool ArchiveUtils::builtin_loader_from_type(const char* loader_type, oop* value_ret) {
571   if (strcmp(loader_type, "boot") == 0) {
572     *value_ret = nullptr;
573     return true;
574   } else if (strcmp(loader_type, "platform") == 0) {
575     *value_ret = SystemDictionary::java_platform_loader();
576     return true;
577   } else if (strcmp(loader_type, "app") == 0) {
578     *value_ret = SystemDictionary::java_system_loader();
579     return true;
580   } else {
581     DEBUG_ONLY(*value_ret = cast_to_oop((void*)badOopVal));
582     return false;
583   }
584 }
585 
586 oop ArchiveUtils::builtin_loader_from_type(int loader_type) {
587   if (loader_type == ClassLoader::BOOT_LOADER) {
588     return nullptr;
589   } else if (loader_type == ClassLoader::PLATFORM_LOADER)  {
590     return SystemDictionary::java_platform_loader();
591   } else if (loader_type == ClassLoader::APP_LOADER) {
592     return SystemDictionary::java_system_loader();
593   } else {
594     ShouldNotReachHere();
595     return nullptr;
596   }
597 }
598 
599 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) {
600   if (!ArchiveBuilder::current()->has_been_archived(src_ik)) {
601     return false;
602   }
603   return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror();
604 }
605 
606 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) {
607   assert(seg_idx < _count, "In range");
608   return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize;
609 }
610 
611 int HeapRootSegments::size_in_elems(size_t seg_idx) {
612   assert(seg_idx < _count, "In range");
613   if (seg_idx != _count - 1) {
614     return _max_size_in_elems;
615   } else {
616     // Last slice, leftover
617     return _roots_count % _max_size_in_elems;
618   }
< prev index next >