src/hotspot/share/memory/metaspace.cpp
changeset 59070 22ee476cc664
parent 59053 ba6c248cae19
child 59247 56bf71d64d51
equal deleted inserted replaced
59069:e0d59f0c2b7d 59070:22ee476cc664
   973 #define VIRTUALSPACEMULTIPLIER 2
   973 #define VIRTUALSPACEMULTIPLIER 2
   974 
   974 
   975 #ifdef _LP64
   975 #ifdef _LP64
   976 static const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
   976 static const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
   977 
   977 
   978 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
   978 void Metaspace::set_narrow_klass_base_and_shift(ReservedSpace metaspace_rs, address cds_base) {
   979   assert(!DumpSharedSpaces, "narrow_klass is set by MetaspaceShared class.");
   979   assert(!DumpSharedSpaces, "narrow_klass is set by MetaspaceShared class.");
   980   // Figure out the narrow_klass_base and the narrow_klass_shift.  The
   980   // Figure out the narrow_klass_base and the narrow_klass_shift.  The
   981   // narrow_klass_base is the lower of the metaspace base and the cds base
   981   // narrow_klass_base is the lower of the metaspace base and the cds base
   982   // (if cds is enabled).  The narrow_klass_shift depends on the distance
   982   // (if cds is enabled).  The narrow_klass_shift depends on the distance
   983   // between the lower base and higher address.
   983   // between the lower base and higher address.
   984   address lower_base;
   984   address lower_base = (address)metaspace_rs.base();
   985   address higher_address;
   985   address higher_address = (address)metaspace_rs.end();
   986 #if INCLUDE_CDS
   986   if (cds_base != NULL) {
   987   if (UseSharedSpaces) {
   987     assert(UseSharedSpaces, "must be");
   988     higher_address = MAX2((address)(cds_base + MetaspaceShared::core_spaces_size()),
   988     lower_base = MIN2(lower_base, cds_base);
   989                           (address)(metaspace_base + compressed_class_space_size()));
   989   } else {
   990     lower_base = MIN2(metaspace_base, cds_base);
       
   991   } else
       
   992 #endif
       
   993   {
       
   994     higher_address = metaspace_base + compressed_class_space_size();
       
   995     lower_base = metaspace_base;
       
   996 
       
   997     uint64_t klass_encoding_max = UnscaledClassSpaceMax << LogKlassAlignmentInBytes;
   990     uint64_t klass_encoding_max = UnscaledClassSpaceMax << LogKlassAlignmentInBytes;
   998     // If compressed class space fits in lower 32G, we don't need a base.
   991     // If compressed class space fits in lower 32G, we don't need a base.
   999     if (higher_address <= (address)klass_encoding_max) {
   992     if (higher_address <= (address)klass_encoding_max) {
  1000       lower_base = 0; // Effectively lower base is zero.
   993       lower_base = 0; // Effectively lower base is zero.
  1001     }
   994     }
  1016     CompressedKlassPointers::set_shift(LogKlassAlignmentInBytes);
  1009     CompressedKlassPointers::set_shift(LogKlassAlignmentInBytes);
  1017   }
  1010   }
  1018   AOTLoader::set_narrow_klass_shift();
  1011   AOTLoader::set_narrow_klass_shift();
  1019 }
  1012 }
  1020 
  1013 
  1021 #if INCLUDE_CDS
       
  1022 // Return TRUE if the specified metaspace_base and cds_base are close enough
       
  1023 // to work with compressed klass pointers.
       
  1024 bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) {
       
  1025   assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS");
       
  1026   assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
       
  1027   address lower_base = MIN2((address)metaspace_base, cds_base);
       
  1028   address higher_address = MAX2((address)(cds_base + MetaspaceShared::core_spaces_size()),
       
  1029                                 (address)(metaspace_base + compressed_class_space_size()));
       
  1030   return ((uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax);
       
  1031 }
       
  1032 #endif
       
  1033 
       
  1034 // Try to allocate the metaspace at the requested addr.
  1014 // Try to allocate the metaspace at the requested addr.
  1035 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) {
  1015 void Metaspace::allocate_metaspace_compressed_klass_ptrs(ReservedSpace metaspace_rs, char* requested_addr, address cds_base) {
  1036   assert(!DumpSharedSpaces, "compress klass space is allocated by MetaspaceShared class.");
  1016   assert(!DumpSharedSpaces, "compress klass space is allocated by MetaspaceShared class.");
  1037   assert(using_class_space(), "called improperly");
  1017   assert(using_class_space(), "called improperly");
  1038   assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
  1018   assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
  1039   assert(compressed_class_space_size() < KlassEncodingMetaspaceMax,
  1019   assert(compressed_class_space_size() < KlassEncodingMetaspaceMax,
  1040          "Metaspace size is too big");
  1020          "Metaspace size is too big");
  1043   assert_is_aligned(compressed_class_space_size(), _reserve_alignment);
  1023   assert_is_aligned(compressed_class_space_size(), _reserve_alignment);
  1044 
  1024 
  1045   // Don't use large pages for the class space.
  1025   // Don't use large pages for the class space.
  1046   bool large_pages = false;
  1026   bool large_pages = false;
  1047 
  1027 
       
  1028  if (metaspace_rs.is_reserved()) {
       
  1029    // CDS should have already reserved the space.
       
  1030    assert(requested_addr == NULL, "not used");
       
  1031    assert(cds_base != NULL, "CDS should have already reserved the memory space");
       
  1032  } else {
       
  1033    assert(cds_base == NULL, "must be");
  1048 #if !(defined(AARCH64) || defined(AIX))
  1034 #if !(defined(AARCH64) || defined(AIX))
  1049   ReservedSpace metaspace_rs = ReservedSpace(compressed_class_space_size(),
  1035   metaspace_rs = ReservedSpace(compressed_class_space_size(), _reserve_alignment,
  1050                                              _reserve_alignment,
  1036                                large_pages, requested_addr);
  1051                                              large_pages,
       
  1052                                              requested_addr);
       
  1053 #else // AARCH64
  1037 #else // AARCH64
  1054   ReservedSpace metaspace_rs;
       
  1055 
       
  1056   // Our compressed klass pointers may fit nicely into the lower 32
  1038   // Our compressed klass pointers may fit nicely into the lower 32
  1057   // bits.
  1039   // bits.
  1058   if ((uint64_t)requested_addr + compressed_class_space_size() < 4*G) {
  1040   if ((uint64_t)requested_addr + compressed_class_space_size() < 4*G) {
  1059     metaspace_rs = ReservedSpace(compressed_class_space_size(),
  1041     metaspace_rs = ReservedSpace(compressed_class_space_size(),
  1060                                  _reserve_alignment,
  1042                                  _reserve_alignment,
  1075       if (a == (char *)(32*G)) {
  1057       if (a == (char *)(32*G)) {
  1076         // Go faster from here on. Zero-based is no longer possible.
  1058         // Go faster from here on. Zero-based is no longer possible.
  1077         increment = 4*G;
  1059         increment = 4*G;
  1078       }
  1060       }
  1079 
  1061 
  1080 #if INCLUDE_CDS
       
  1081       if (UseSharedSpaces
       
  1082           && ! can_use_cds_with_metaspace_addr(a, cds_base)) {
       
  1083         // We failed to find an aligned base that will reach.  Fall
       
  1084         // back to using our requested addr.
       
  1085         metaspace_rs = ReservedSpace(compressed_class_space_size(),
       
  1086                                      _reserve_alignment,
       
  1087                                      large_pages,
       
  1088                                      requested_addr);
       
  1089         break;
       
  1090       }
       
  1091 #endif
       
  1092 
       
  1093       metaspace_rs = ReservedSpace(compressed_class_space_size(),
  1062       metaspace_rs = ReservedSpace(compressed_class_space_size(),
  1094                                    _reserve_alignment,
  1063                                    _reserve_alignment,
  1095                                    large_pages,
  1064                                    large_pages,
  1096                                    a);
  1065                                    a);
  1097       if (metaspace_rs.is_reserved())
  1066       if (metaspace_rs.is_reserved())
  1098         break;
  1067         break;
  1099     }
  1068     }
  1100   }
  1069   }
  1101 
       
  1102 #endif // AARCH64
  1070 #endif // AARCH64
       
  1071  }
  1103 
  1072 
  1104   if (!metaspace_rs.is_reserved()) {
  1073   if (!metaspace_rs.is_reserved()) {
  1105 #if INCLUDE_CDS
  1074     assert(cds_base == NULL, "CDS should have already reserved the memory space");
  1106     if (UseSharedSpaces) {
       
  1107       size_t increment = align_up(1*G, _reserve_alignment);
       
  1108 
       
  1109       // Keep trying to allocate the metaspace, increasing the requested_addr
       
  1110       // by 1GB each time, until we reach an address that will no longer allow
       
  1111       // use of CDS with compressed klass pointers.
       
  1112       char *addr = requested_addr;
       
  1113       while (!metaspace_rs.is_reserved() && (addr + increment > addr) &&
       
  1114              can_use_cds_with_metaspace_addr(addr + increment, cds_base)) {
       
  1115         addr = addr + increment;
       
  1116         metaspace_rs = ReservedSpace(compressed_class_space_size(),
       
  1117                                      _reserve_alignment, large_pages, addr);
       
  1118       }
       
  1119     }
       
  1120 #endif
       
  1121     // If no successful allocation then try to allocate the space anywhere.  If
  1075     // If no successful allocation then try to allocate the space anywhere.  If
  1122     // that fails then OOM doom.  At this point we cannot try allocating the
  1076     // that fails then OOM doom.  At this point we cannot try allocating the
  1123     // metaspace as if UseCompressedClassPointers is off because too much
  1077     // metaspace as if UseCompressedClassPointers is off because too much
  1124     // initialization has happened that depends on UseCompressedClassPointers.
  1078     // initialization has happened that depends on UseCompressedClassPointers.
  1125     // So, UseCompressedClassPointers cannot be turned off at this point.
  1079     // So, UseCompressedClassPointers cannot be turned off at this point.
       
  1080     metaspace_rs = ReservedSpace(compressed_class_space_size(),
       
  1081                                  _reserve_alignment, large_pages);
  1126     if (!metaspace_rs.is_reserved()) {
  1082     if (!metaspace_rs.is_reserved()) {
  1127       metaspace_rs = ReservedSpace(compressed_class_space_size(),
  1083       vm_exit_during_initialization(err_msg("Could not allocate metaspace: " SIZE_FORMAT " bytes",
  1128                                    _reserve_alignment, large_pages);
  1084                                             compressed_class_space_size()));
  1129       if (!metaspace_rs.is_reserved()) {
  1085     }
  1130         vm_exit_during_initialization(err_msg("Could not allocate metaspace: " SIZE_FORMAT " bytes",
  1086   }
  1131                                               compressed_class_space_size()));
  1087 
  1132       }
  1088   if (cds_base == NULL) {
  1133     }
  1089     // If we got here then the metaspace got allocated.
  1134   }
  1090     MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass);
  1135 
  1091   }
  1136   // If we got here then the metaspace got allocated.
  1092 
  1137   MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass);
  1093   set_narrow_klass_base_and_shift(metaspace_rs, cds_base);
  1138 
       
  1139 #if INCLUDE_CDS
       
  1140   // Verify that we can use shared spaces.  Otherwise, turn off CDS.
       
  1141   if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) {
       
  1142     FileMapInfo::stop_sharing_and_unmap(
       
  1143         "Could not allocate metaspace at a compatible address");
       
  1144   }
       
  1145 #endif
       
  1146   set_narrow_klass_base_and_shift((address)metaspace_rs.base(),
       
  1147                                   UseSharedSpaces ? (address)cds_base : 0);
       
  1148 
  1094 
  1149   initialize_class_space(metaspace_rs);
  1095   initialize_class_space(metaspace_rs);
  1150 
  1096 
  1151   LogTarget(Trace, gc, metaspace) lt;
  1097   LogTarget(Trace, gc, metaspace) lt;
  1152   if (lt.is_enabled()) {
  1098   if (lt.is_enabled()) {
  1245 }
  1191 }
  1246 
  1192 
  1247 void Metaspace::global_initialize() {
  1193 void Metaspace::global_initialize() {
  1248   MetaspaceGC::initialize();
  1194   MetaspaceGC::initialize();
  1249 
  1195 
       
  1196   bool class_space_inited = false;
  1250 #if INCLUDE_CDS
  1197 #if INCLUDE_CDS
  1251   if (DumpSharedSpaces) {
  1198   if (DumpSharedSpaces) {
  1252     MetaspaceShared::initialize_dumptime_shared_and_meta_spaces();
  1199     MetaspaceShared::initialize_dumptime_shared_and_meta_spaces();
       
  1200     class_space_inited = true;
  1253   } else if (UseSharedSpaces) {
  1201   } else if (UseSharedSpaces) {
  1254     // If any of the archived space fails to map, UseSharedSpaces
  1202     // If any of the archived space fails to map, UseSharedSpaces
  1255     // is reset to false. Fall through to the
  1203     // is reset to false.
  1256     // (!DumpSharedSpaces && !UseSharedSpaces) case to set up class
       
  1257     // metaspace.
       
  1258     MetaspaceShared::initialize_runtime_shared_and_meta_spaces();
  1204     MetaspaceShared::initialize_runtime_shared_and_meta_spaces();
       
  1205     class_space_inited = UseSharedSpaces;
  1259   }
  1206   }
  1260 
  1207 
  1261   if (DynamicDumpSharedSpaces && !UseSharedSpaces) {
  1208   if (DynamicDumpSharedSpaces && !UseSharedSpaces) {
  1262     vm_exit_during_initialization("DynamicDumpSharedSpaces is unsupported when base CDS archive is not loaded", NULL);
  1209     vm_exit_during_initialization("DynamicDumpSharedSpaces is unsupported when base CDS archive is not loaded", NULL);
  1263   }
  1210   }
  1264 
       
  1265   if (!DumpSharedSpaces && !UseSharedSpaces)
       
  1266 #endif // INCLUDE_CDS
  1211 #endif // INCLUDE_CDS
  1267   {
  1212 
  1268 #ifdef _LP64
  1213 #ifdef _LP64
  1269     if (using_class_space()) {
  1214   if (using_class_space() && !class_space_inited) {
  1270       char* base = (char*)align_up(CompressedOops::end(), _reserve_alignment);
  1215     char* base = (char*)align_up(CompressedOops::end(), _reserve_alignment);
  1271       allocate_metaspace_compressed_klass_ptrs(base, 0);
  1216     ReservedSpace dummy;
  1272     }
  1217     allocate_metaspace_compressed_klass_ptrs(dummy, base, 0);
  1273 #endif // _LP64
  1218   }
  1274   }
  1219 #endif
  1275 
  1220 
  1276   // Initialize these before initializing the VirtualSpaceList
  1221   // Initialize these before initializing the VirtualSpaceList
  1277   _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
  1222   _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
  1278   _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
  1223   _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
  1279   // Make the first class chunk bigger than a medium chunk so it's not put
  1224   // Make the first class chunk bigger than a medium chunk so it's not put