411 |
411 |
412 // byte_size is the size of the associated virtualspace. |
412 // byte_size is the size of the associated virtualspace. |
413 VirtualSpaceNode::VirtualSpaceNode(size_t bytes) : _top(NULL), _next(NULL), _rs(), _container_count(0) { |
413 VirtualSpaceNode::VirtualSpaceNode(size_t bytes) : _top(NULL), _next(NULL), _rs(), _container_count(0) { |
414 assert_is_size_aligned(bytes, Metaspace::reserve_alignment()); |
414 assert_is_size_aligned(bytes, Metaspace::reserve_alignment()); |
415 |
415 |
|
416 #if INCLUDE_CDS |
416 // This allocates memory with mmap. For DumpSharedspaces, try to reserve |
417 // This allocates memory with mmap. For DumpSharedspaces, try to reserve |
417 // configurable address, generally at the top of the Java heap so other |
418 // configurable address, generally at the top of the Java heap so other |
418 // memory addresses don't conflict. |
419 // memory addresses don't conflict. |
419 if (DumpSharedSpaces) { |
420 if (DumpSharedSpaces) { |
420 bool large_pages = false; // No large pages when dumping the CDS archive. |
421 bool large_pages = false; // No large pages when dumping the CDS archive. |
2937 // narrow_klass_base is the lower of the metaspace base and the cds base |
2940 // narrow_klass_base is the lower of the metaspace base and the cds base |
2938 // (if cds is enabled). The narrow_klass_shift depends on the distance |
2941 // (if cds is enabled). The narrow_klass_shift depends on the distance |
2939 // between the lower base and higher address. |
2942 // between the lower base and higher address. |
2940 address lower_base; |
2943 address lower_base; |
2941 address higher_address; |
2944 address higher_address; |
|
2945 #if INCLUDE_CDS |
2942 if (UseSharedSpaces) { |
2946 if (UseSharedSpaces) { |
2943 higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()), |
2947 higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()), |
2944 (address)(metaspace_base + compressed_class_space_size())); |
2948 (address)(metaspace_base + compressed_class_space_size())); |
2945 lower_base = MIN2(metaspace_base, cds_base); |
2949 lower_base = MIN2(metaspace_base, cds_base); |
2946 } else { |
2950 } else |
|
2951 #endif |
|
2952 { |
2947 higher_address = metaspace_base + compressed_class_space_size(); |
2953 higher_address = metaspace_base + compressed_class_space_size(); |
2948 lower_base = metaspace_base; |
2954 lower_base = metaspace_base; |
2949 |
2955 |
2950 uint64_t klass_encoding_max = UnscaledClassSpaceMax << LogKlassAlignmentInBytes; |
2956 uint64_t klass_encoding_max = UnscaledClassSpaceMax << LogKlassAlignmentInBytes; |
2951 // If compressed class space fits in lower 32G, we don't need a base. |
2957 // If compressed class space fits in lower 32G, we don't need a base. |
2962 assert(!UseSharedSpaces, "Cannot shift with UseSharedSpaces"); |
2968 assert(!UseSharedSpaces, "Cannot shift with UseSharedSpaces"); |
2963 Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes); |
2969 Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes); |
2964 } |
2970 } |
2965 } |
2971 } |
2966 |
2972 |
|
2973 #if INCLUDE_CDS |
2967 // Return TRUE if the specified metaspace_base and cds_base are close enough |
2974 // Return TRUE if the specified metaspace_base and cds_base are close enough |
2968 // to work with compressed klass pointers. |
2975 // to work with compressed klass pointers. |
2969 bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) { |
2976 bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) { |
2970 assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS"); |
2977 assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS"); |
2971 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs"); |
2978 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs"); |
2972 address lower_base = MIN2((address)metaspace_base, cds_base); |
2979 address lower_base = MIN2((address)metaspace_base, cds_base); |
2973 address higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()), |
2980 address higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()), |
2974 (address)(metaspace_base + compressed_class_space_size())); |
2981 (address)(metaspace_base + compressed_class_space_size())); |
2975 return ((uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax); |
2982 return ((uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax); |
2976 } |
2983 } |
|
2984 #endif |
2977 |
2985 |
2978 // Try to allocate the metaspace at the requested addr. |
2986 // Try to allocate the metaspace at the requested addr. |
2979 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) { |
2987 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) { |
2980 assert(using_class_space(), "called improperly"); |
2988 assert(using_class_space(), "called improperly"); |
2981 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs"); |
2989 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs"); |
2991 ReservedSpace metaspace_rs = ReservedSpace(compressed_class_space_size(), |
2999 ReservedSpace metaspace_rs = ReservedSpace(compressed_class_space_size(), |
2992 _reserve_alignment, |
3000 _reserve_alignment, |
2993 large_pages, |
3001 large_pages, |
2994 requested_addr, 0); |
3002 requested_addr, 0); |
2995 if (!metaspace_rs.is_reserved()) { |
3003 if (!metaspace_rs.is_reserved()) { |
|
3004 #if INCLUDE_CDS |
2996 if (UseSharedSpaces) { |
3005 if (UseSharedSpaces) { |
2997 size_t increment = align_size_up(1*G, _reserve_alignment); |
3006 size_t increment = align_size_up(1*G, _reserve_alignment); |
2998 |
3007 |
2999 // Keep trying to allocate the metaspace, increasing the requested_addr |
3008 // Keep trying to allocate the metaspace, increasing the requested_addr |
3000 // by 1GB each time, until we reach an address that will no longer allow |
3009 // by 1GB each time, until we reach an address that will no longer allow |
3005 addr = addr + increment; |
3014 addr = addr + increment; |
3006 metaspace_rs = ReservedSpace(compressed_class_space_size(), |
3015 metaspace_rs = ReservedSpace(compressed_class_space_size(), |
3007 _reserve_alignment, large_pages, addr, 0); |
3016 _reserve_alignment, large_pages, addr, 0); |
3008 } |
3017 } |
3009 } |
3018 } |
3010 |
3019 #endif |
3011 // If no successful allocation then try to allocate the space anywhere. If |
3020 // If no successful allocation then try to allocate the space anywhere. If |
3012 // that fails then OOM doom. At this point we cannot try allocating the |
3021 // that fails then OOM doom. At this point we cannot try allocating the |
3013 // metaspace as if UseCompressedClassPointers is off because too much |
3022 // metaspace as if UseCompressedClassPointers is off because too much |
3014 // initialization has happened that depends on UseCompressedClassPointers. |
3023 // initialization has happened that depends on UseCompressedClassPointers. |
3015 // So, UseCompressedClassPointers cannot be turned off at this point. |
3024 // So, UseCompressedClassPointers cannot be turned off at this point. |
3024 } |
3033 } |
3025 |
3034 |
3026 // If we got here then the metaspace got allocated. |
3035 // If we got here then the metaspace got allocated. |
3027 MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass); |
3036 MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass); |
3028 |
3037 |
|
3038 #if INCLUDE_CDS |
3029 // Verify that we can use shared spaces. Otherwise, turn off CDS. |
3039 // Verify that we can use shared spaces. Otherwise, turn off CDS. |
3030 if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) { |
3040 if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) { |
3031 FileMapInfo::stop_sharing_and_unmap( |
3041 FileMapInfo::stop_sharing_and_unmap( |
3032 "Could not allocate metaspace at a compatible address"); |
3042 "Could not allocate metaspace at a compatible address"); |
3033 } |
3043 } |
3034 |
3044 #endif |
3035 set_narrow_klass_base_and_shift((address)metaspace_rs.base(), |
3045 set_narrow_klass_base_and_shift((address)metaspace_rs.base(), |
3036 UseSharedSpaces ? (address)cds_base : 0); |
3046 UseSharedSpaces ? (address)cds_base : 0); |
3037 |
3047 |
3038 initialize_class_space(metaspace_rs); |
3048 initialize_class_space(metaspace_rs); |
3039 |
3049 |
3113 size_t cds_total = 0; |
3123 size_t cds_total = 0; |
3114 |
3124 |
3115 MetaspaceShared::set_max_alignment(max_alignment); |
3125 MetaspaceShared::set_max_alignment(max_alignment); |
3116 |
3126 |
3117 if (DumpSharedSpaces) { |
3127 if (DumpSharedSpaces) { |
|
3128 #if INCLUDE_CDS |
3118 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment); |
3129 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment); |
3119 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment); |
3130 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment); |
3120 SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment); |
3131 SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment); |
3121 SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment); |
3132 SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment); |
3122 |
3133 |
3150 gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT, |
3161 gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT, |
3151 _space_list->current_virtual_space()->bottom()); |
3162 _space_list->current_virtual_space()->bottom()); |
3152 } |
3163 } |
3153 |
3164 |
3154 Universe::set_narrow_klass_shift(0); |
3165 Universe::set_narrow_klass_shift(0); |
3155 #endif |
3166 #endif // _LP64 |
3156 |
3167 #endif // INCLUDE_CDS |
3157 } else { |
3168 } else { |
|
3169 #if INCLUDE_CDS |
3158 // If using shared space, open the file that contains the shared space |
3170 // If using shared space, open the file that contains the shared space |
3159 // and map in the memory before initializing the rest of metaspace (so |
3171 // and map in the memory before initializing the rest of metaspace (so |
3160 // the addresses don't conflict) |
3172 // the addresses don't conflict) |
3161 address cds_address = NULL; |
3173 address cds_address = NULL; |
3162 if (UseSharedSpaces) { |
3174 if (UseSharedSpaces) { |
3163 FileMapInfo* mapinfo = new FileMapInfo(); |
3175 FileMapInfo* mapinfo = new FileMapInfo(); |
3164 memset(mapinfo, 0, sizeof(FileMapInfo)); |
|
3165 |
3176 |
3166 // Open the shared archive file, read and validate the header. If |
3177 // Open the shared archive file, read and validate the header. If |
3167 // initialization fails, shared spaces [UseSharedSpaces] are |
3178 // initialization fails, shared spaces [UseSharedSpaces] are |
3168 // disabled and the file is closed. |
3179 // disabled and the file is closed. |
3169 // Map in spaces now also |
3180 // Map in spaces now also |
3170 if (mapinfo->initialize() && MetaspaceShared::map_shared_spaces(mapinfo)) { |
3181 if (mapinfo->initialize() && MetaspaceShared::map_shared_spaces(mapinfo)) { |
3171 FileMapInfo::set_current_info(mapinfo); |
|
3172 cds_total = FileMapInfo::shared_spaces_size(); |
3182 cds_total = FileMapInfo::shared_spaces_size(); |
3173 cds_address = (address)mapinfo->region_base(0); |
3183 cds_address = (address)mapinfo->region_base(0); |
3174 } else { |
3184 } else { |
3175 assert(!mapinfo->is_open() && !UseSharedSpaces, |
3185 assert(!mapinfo->is_open() && !UseSharedSpaces, |
3176 "archive file not closed or shared spaces not disabled."); |
3186 "archive file not closed or shared spaces not disabled."); |
3177 } |
3187 } |
3178 } |
3188 } |
3179 |
3189 #endif // INCLUDE_CDS |
3180 #ifdef _LP64 |
3190 #ifdef _LP64 |
3181 // If UseCompressedClassPointers is set then allocate the metaspace area |
3191 // If UseCompressedClassPointers is set then allocate the metaspace area |
3182 // above the heap and above the CDS area (if it exists). |
3192 // above the heap and above the CDS area (if it exists). |
3183 if (using_class_space()) { |
3193 if (using_class_space()) { |
3184 if (UseSharedSpaces) { |
3194 if (UseSharedSpaces) { |
|
3195 #if INCLUDE_CDS |
3185 char* cds_end = (char*)(cds_address + cds_total); |
3196 char* cds_end = (char*)(cds_address + cds_total); |
3186 cds_end = (char *)align_ptr_up(cds_end, _reserve_alignment); |
3197 cds_end = (char *)align_ptr_up(cds_end, _reserve_alignment); |
3187 allocate_metaspace_compressed_klass_ptrs(cds_end, cds_address); |
3198 allocate_metaspace_compressed_klass_ptrs(cds_end, cds_address); |
|
3199 #endif |
3188 } else { |
3200 } else { |
3189 char* base = (char*)align_ptr_up(Universe::heap()->reserved_region().end(), _reserve_alignment); |
3201 char* base = (char*)align_ptr_up(Universe::heap()->reserved_region().end(), _reserve_alignment); |
3190 allocate_metaspace_compressed_klass_ptrs(base, 0); |
3202 allocate_metaspace_compressed_klass_ptrs(base, 0); |
3191 } |
3203 } |
3192 } |
3204 } |
3193 #endif |
3205 #endif // _LP64 |
3194 |
3206 |
3195 // Initialize these before initializing the VirtualSpaceList |
3207 // Initialize these before initializing the VirtualSpaceList |
3196 _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord; |
3208 _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord; |
3197 _first_chunk_word_size = align_word_size_up(_first_chunk_word_size); |
3209 _first_chunk_word_size = align_word_size_up(_first_chunk_word_size); |
3198 // Make the first class chunk bigger than a medium chunk so it's not put |
3210 // Make the first class chunk bigger than a medium chunk so it's not put |
3378 |
3390 |
3379 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) { |
3391 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) { |
3380 assert(!SafepointSynchronize::is_at_safepoint() |
3392 assert(!SafepointSynchronize::is_at_safepoint() |
3381 || Thread::current()->is_VM_thread(), "should be the VM thread"); |
3393 || Thread::current()->is_VM_thread(), "should be the VM thread"); |
3382 |
3394 |
|
3395 if (DumpSharedSpaces && PrintSharedSpaces) { |
|
3396 record_deallocation(ptr, vsm()->get_raw_word_size(word_size)); |
|
3397 } |
|
3398 |
3383 MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag); |
3399 MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag); |
3384 |
3400 |
3385 if (word_size < TreeChunk<Metablock, FreeList<Metablock> >::min_size()) { |
3401 if (word_size < TreeChunk<Metablock, FreeList<Metablock> >::min_size()) { |
3386 // Dark matter. Too small for dictionary. |
3402 // Dark matter. Too small for dictionary. |
3387 #ifdef ASSERT |
3403 #ifdef ASSERT |
3415 Metaspace* space = read_only ? loader_data->ro_metaspace() : loader_data->rw_metaspace(); |
3431 Metaspace* space = read_only ? loader_data->ro_metaspace() : loader_data->rw_metaspace(); |
3416 MetaWord* result = space->allocate(word_size, NonClassType); |
3432 MetaWord* result = space->allocate(word_size, NonClassType); |
3417 if (result == NULL) { |
3433 if (result == NULL) { |
3418 report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite); |
3434 report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite); |
3419 } |
3435 } |
3420 |
3436 if (PrintSharedSpaces) { |
3421 space->record_allocation(result, type, space->vsm()->get_raw_word_size(word_size)); |
3437 space->record_allocation(result, type, space->vsm()->get_raw_word_size(word_size)); |
|
3438 } |
3422 |
3439 |
3423 // Zero initialize. |
3440 // Zero initialize. |
3424 Copy::fill_to_aligned_words((HeapWord*)result, word_size, 0); |
3441 Copy::fill_to_aligned_words((HeapWord*)result, word_size, 0); |
3425 |
3442 |
3426 return result; |
3443 return result; |
3515 } |
3532 } |
3516 |
3533 |
3517 void Metaspace::record_allocation(void* ptr, MetaspaceObj::Type type, size_t word_size) { |
3534 void Metaspace::record_allocation(void* ptr, MetaspaceObj::Type type, size_t word_size) { |
3518 assert(DumpSharedSpaces, "sanity"); |
3535 assert(DumpSharedSpaces, "sanity"); |
3519 |
3536 |
3520 AllocRecord *rec = new AllocRecord((address)ptr, type, (int)word_size * HeapWordSize); |
3537 int byte_size = (int)word_size * HeapWordSize; |
|
3538 AllocRecord *rec = new AllocRecord((address)ptr, type, byte_size); |
|
3539 |
3521 if (_alloc_record_head == NULL) { |
3540 if (_alloc_record_head == NULL) { |
3522 _alloc_record_head = _alloc_record_tail = rec; |
3541 _alloc_record_head = _alloc_record_tail = rec; |
3523 } else { |
3542 } else if (_alloc_record_tail->_ptr + _alloc_record_tail->_byte_size == (address)ptr) { |
3524 _alloc_record_tail->_next = rec; |
3543 _alloc_record_tail->_next = rec; |
3525 _alloc_record_tail = rec; |
3544 _alloc_record_tail = rec; |
3526 } |
3545 } else { |
|
3546 // slow linear search, but this doesn't happen that often, and only when dumping |
|
3547 for (AllocRecord *old = _alloc_record_head; old; old = old->_next) { |
|
3548 if (old->_ptr == ptr) { |
|
3549 assert(old->_type == MetaspaceObj::DeallocatedType, "sanity"); |
|
3550 int remain_bytes = old->_byte_size - byte_size; |
|
3551 assert(remain_bytes >= 0, "sanity"); |
|
3552 old->_type = type; |
|
3553 |
|
3554 if (remain_bytes == 0) { |
|
3555 delete(rec); |
|
3556 } else { |
|
3557 address remain_ptr = address(ptr) + byte_size; |
|
3558 rec->_ptr = remain_ptr; |
|
3559 rec->_byte_size = remain_bytes; |
|
3560 rec->_type = MetaspaceObj::DeallocatedType; |
|
3561 rec->_next = old->_next; |
|
3562 old->_byte_size = byte_size; |
|
3563 old->_next = rec; |
|
3564 } |
|
3565 return; |
|
3566 } |
|
3567 } |
|
3568 assert(0, "reallocating a freed pointer that was not recorded"); |
|
3569 } |
|
3570 } |
|
3571 |
|
3572 void Metaspace::record_deallocation(void* ptr, size_t word_size) { |
|
3573 assert(DumpSharedSpaces, "sanity"); |
|
3574 |
|
3575 for (AllocRecord *rec = _alloc_record_head; rec; rec = rec->_next) { |
|
3576 if (rec->_ptr == ptr) { |
|
3577 assert(rec->_byte_size == (int)word_size * HeapWordSize, "sanity"); |
|
3578 rec->_type = MetaspaceObj::DeallocatedType; |
|
3579 return; |
|
3580 } |
|
3581 } |
|
3582 |
|
3583 assert(0, "deallocating a pointer that was not recorded"); |
3527 } |
3584 } |
3528 |
3585 |
3529 void Metaspace::iterate(Metaspace::AllocRecordClosure *closure) { |
3586 void Metaspace::iterate(Metaspace::AllocRecordClosure *closure) { |
3530 assert(DumpSharedSpaces, "unimplemented for !DumpSharedSpaces"); |
3587 assert(DumpSharedSpaces, "unimplemented for !DumpSharedSpaces"); |
3531 |
3588 |