coleenp@4037: /* drchase@6680: * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. coleenp@4037: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. coleenp@4037: * coleenp@4037: * This code is free software; you can redistribute it and/or modify it coleenp@4037: * under the terms of the GNU General Public License version 2 only, as coleenp@4037: * published by the Free Software Foundation. coleenp@4037: * coleenp@4037: * This code is distributed in the hope that it will be useful, but WITHOUT coleenp@4037: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or coleenp@4037: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License coleenp@4037: * version 2 for more details (a copy is included in the LICENSE file that coleenp@4037: * accompanied this code). coleenp@4037: * coleenp@4037: * You should have received a copy of the GNU General Public License version coleenp@4037: * 2 along with this work; if not, write to the Free Software Foundation, coleenp@4037: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. coleenp@4037: * coleenp@4037: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA coleenp@4037: * or visit www.oracle.com if you need additional information or have any coleenp@4037: * questions. coleenp@4037: * coleenp@4037: */ coleenp@4037: #include "precompiled.hpp" coleenp@4037: #include "gc_interface/collectedHeap.hpp" stefank@5771: #include "memory/allocation.hpp" coleenp@4037: #include "memory/binaryTreeDictionary.hpp" jmasa@4196: #include "memory/freeList.hpp" coleenp@4037: #include "memory/collectorPolicy.hpp" coleenp@4037: #include "memory/filemap.hpp" coleenp@4037: #include "memory/freeList.hpp" stefank@5863: #include "memory/gcLocker.hpp" jmasa@4196: #include "memory/metachunk.hpp" coleenp@4037: #include "memory/metaspace.hpp" ehelin@6417: #include "memory/metaspaceGCThresholdUpdater.hpp" coleenp@4037: #include "memory/metaspaceShared.hpp" ehelin@6417: #include "memory/metaspaceTracer.hpp" coleenp@4037: #include "memory/resourceArea.hpp" coleenp@4037: #include "memory/universe.hpp" stefank@5863: #include "runtime/atomic.inline.hpp" coleenp@4037: #include "runtime/globals.hpp" stefank@5863: #include "runtime/init.hpp" hseigel@5528: #include "runtime/java.hpp" coleenp@4037: #include "runtime/mutex.hpp" goetz@6911: #include "runtime/orderAccess.inline.hpp" coleenp@4037: #include "services/memTracker.hpp" stefank@5864: #include "services/memoryService.hpp" coleenp@4037: #include "utilities/copy.hpp" coleenp@4037: #include "utilities/debug.hpp" coleenp@4037: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: goetz@6337: typedef BinaryTreeDictionary > BlockTreeDictionary; goetz@6337: typedef BinaryTreeDictionary > ChunkTreeDictionary; stefank@5941: stefank@5941: // Set this constant to enable slow integrity checking of the free chunk lists mgerdin@4264: const bool metaspace_slow_verify = false; mgerdin@4264: mgerdin@5699: size_t const allocation_from_dictionary_limit = 4 * K; coleenp@4037: coleenp@4037: MetaWord* last_allocated = 0; coleenp@4037: coleenp@6029: size_t Metaspace::_compressed_class_space_size; ehelin@6417: const MetaspaceTracer* Metaspace::_tracer = NULL; hseigel@5528: coleenp@4037: // Used in declarations in SpaceManager and ChunkManager coleenp@4037: enum ChunkIndex { jmasa@4382: ZeroIndex = 0, jmasa@4382: SpecializedIndex = ZeroIndex, jmasa@4382: SmallIndex = SpecializedIndex + 1, jmasa@4382: MediumIndex = SmallIndex + 1, jmasa@4382: HumongousIndex = MediumIndex + 1, jmasa@4382: NumberOfFreeLists = 3, jmasa@4382: NumberOfInUseLists = 4 jmasa@4382: }; jmasa@4382: jmasa@4382: enum ChunkSizes { // in words. jmasa@4382: ClassSpecializedChunk = 128, jmasa@4382: SpecializedChunk = 128, jmasa@4382: ClassSmallChunk = 256, jmasa@4382: SmallChunk = 512, coleenp@5337: ClassMediumChunk = 4 * K, mgerdin@6004: MediumChunk = 8 * K coleenp@4037: }; coleenp@4037: coleenp@4037: static ChunkIndex next_chunk_index(ChunkIndex i) { jmasa@4196: assert(i < NumberOfInUseLists, "Out of bound"); coleenp@4037: return (ChunkIndex) (i+1); coleenp@4037: } coleenp@4037: stefank@5863: volatile intptr_t MetaspaceGC::_capacity_until_GC = 0; coleenp@4037: uint MetaspaceGC::_shrink_factor = 0; coleenp@4037: bool MetaspaceGC::_should_concurrent_collect = false; coleenp@4037: jmasa@4932: typedef class FreeList ChunkList; coleenp@4037: coleenp@4037: // Manages the global free lists of chunks. stefank@5771: class ChunkManager : public CHeapObj { mgerdin@6004: friend class TestVirtualSpaceNodeTest; coleenp@4037: coleenp@4037: // Free list of chunks of different sizes. jmasa@5007: // SpecializedChunk coleenp@4037: // SmallChunk coleenp@4037: // MediumChunk coleenp@4037: // HumongousChunk jmasa@4196: ChunkList _free_chunks[NumberOfFreeLists]; jmasa@4196: jmasa@4196: // HumongousChunk jmasa@4196: ChunkTreeDictionary _humongous_dictionary; coleenp@4037: coleenp@4037: // ChunkManager in all lists of this type coleenp@4037: size_t _free_chunks_total; coleenp@4037: size_t _free_chunks_count; coleenp@4037: coleenp@4037: void dec_free_chunks_total(size_t v) { coleenp@4037: assert(_free_chunks_count > 0 && coleenp@4037: _free_chunks_total > 0, coleenp@4037: "About to go negative"); coleenp@4037: Atomic::add_ptr(-1, &_free_chunks_count); coleenp@4037: jlong minus_v = (jlong) - (jlong) v; coleenp@4037: Atomic::add_ptr(minus_v, &_free_chunks_total); coleenp@4037: } coleenp@4037: coleenp@4037: // Debug support coleenp@4037: coleenp@4037: size_t sum_free_chunks(); coleenp@4037: size_t sum_free_chunks_count(); coleenp@4037: coleenp@4037: void locked_verify_free_chunks_total(); mgerdin@4264: void slow_locked_verify_free_chunks_total() { mgerdin@4264: if (metaspace_slow_verify) { mgerdin@4264: locked_verify_free_chunks_total(); mgerdin@4264: } mgerdin@4264: } coleenp@4037: void locked_verify_free_chunks_count(); mgerdin@4264: void slow_locked_verify_free_chunks_count() { mgerdin@4264: if (metaspace_slow_verify) { mgerdin@4264: locked_verify_free_chunks_count(); mgerdin@4264: } mgerdin@4264: } coleenp@4037: void verify_free_chunks_count(); coleenp@4037: coleenp@4037: public: coleenp@4037: stefank@5771: ChunkManager(size_t specialized_size, size_t small_size, size_t medium_size) stefank@5771: : _free_chunks_total(0), _free_chunks_count(0) { stefank@5771: _free_chunks[SpecializedIndex].set_size(specialized_size); stefank@5771: _free_chunks[SmallIndex].set_size(small_size); stefank@5771: _free_chunks[MediumIndex].set_size(medium_size); stefank@5771: } coleenp@4037: coleenp@4037: // add or delete (return) a chunk to the global freelist. coleenp@4037: Metachunk* chunk_freelist_allocate(size_t word_size); coleenp@4037: jmasa@4382: // Map a size to a list index assuming that there are lists jmasa@4382: // for special, small, medium, and humongous chunks. jmasa@4382: static ChunkIndex list_index(size_t size); jmasa@4382: jmasa@5007: // Remove the chunk from its freelist. It is jmasa@5007: // expected to be on one of the _free_chunks[] lists. jmasa@5007: void remove_chunk(Metachunk* chunk); jmasa@5007: jmasa@4932: // Add the simple linked list of chunks to the freelist of chunks jmasa@4932: // of type index. jmasa@4932: void return_chunks(ChunkIndex index, Metachunk* chunks); jmasa@4932: coleenp@4037: // Total of the space in the free chunks list ehelin@5703: size_t free_chunks_total_words(); ehelin@5703: size_t free_chunks_total_bytes(); coleenp@4037: coleenp@4037: // Number of chunks in the free chunks list coleenp@4037: size_t free_chunks_count(); coleenp@4037: coleenp@4037: void inc_free_chunks_total(size_t v, size_t count = 1) { coleenp@4037: Atomic::add_ptr(count, &_free_chunks_count); coleenp@4037: Atomic::add_ptr(v, &_free_chunks_total); coleenp@4037: } jmasa@4196: ChunkTreeDictionary* humongous_dictionary() { jmasa@4196: return &_humongous_dictionary; jmasa@4196: } coleenp@4037: coleenp@4037: ChunkList* free_chunks(ChunkIndex index); coleenp@4037: coleenp@4037: // Returns the list for the given chunk word size. coleenp@4037: ChunkList* find_free_chunks_list(size_t word_size); coleenp@4037: stefank@5945: // Remove from a list by size. Selects list based on size of chunk. coleenp@4037: Metachunk* free_chunks_get(size_t chunk_word_size); coleenp@4037: ehelin@6420: #define index_bounds_check(index) \ ehelin@6420: assert(index == SpecializedIndex || \ ehelin@6420: index == SmallIndex || \ ehelin@6420: index == MediumIndex || \ ehelin@6420: index == HumongousIndex, err_msg("Bad index: %d", (int) index)) ehelin@6420: ehelin@6420: size_t num_free_chunks(ChunkIndex index) const { ehelin@6420: index_bounds_check(index); ehelin@6420: ehelin@6420: if (index == HumongousIndex) { ehelin@6420: return _humongous_dictionary.total_free_blocks(); ehelin@6420: } ehelin@6420: ehelin@6420: ssize_t count = _free_chunks[index].count(); ehelin@6420: return count == -1 ? 0 : (size_t) count; ehelin@6420: } ehelin@6420: ehelin@6420: size_t size_free_chunks_in_bytes(ChunkIndex index) const { ehelin@6420: index_bounds_check(index); ehelin@6420: ehelin@6420: size_t word_size = 0; ehelin@6420: if (index == HumongousIndex) { ehelin@6420: word_size = _humongous_dictionary.total_size(); ehelin@6420: } else { ehelin@6420: const size_t size_per_chunk_in_words = _free_chunks[index].size(); ehelin@6420: word_size = size_per_chunk_in_words * num_free_chunks(index); ehelin@6420: } ehelin@6420: ehelin@6420: return word_size * BytesPerWord; ehelin@6420: } ehelin@6420: ehelin@6420: MetaspaceChunkFreeListSummary chunk_free_list_summary() const { ehelin@6420: return MetaspaceChunkFreeListSummary(num_free_chunks(SpecializedIndex), ehelin@6420: num_free_chunks(SmallIndex), ehelin@6420: num_free_chunks(MediumIndex), ehelin@6420: num_free_chunks(HumongousIndex), ehelin@6420: size_free_chunks_in_bytes(SpecializedIndex), ehelin@6420: size_free_chunks_in_bytes(SmallIndex), ehelin@6420: size_free_chunks_in_bytes(MediumIndex), ehelin@6420: size_free_chunks_in_bytes(HumongousIndex)); ehelin@6420: } ehelin@6420: coleenp@4037: // Debug support coleenp@4037: void verify(); mgerdin@4264: void slow_verify() { mgerdin@4264: if (metaspace_slow_verify) { mgerdin@4264: verify(); mgerdin@4264: } mgerdin@4264: } coleenp@4037: void locked_verify(); mgerdin@4264: void slow_locked_verify() { mgerdin@4264: if (metaspace_slow_verify) { mgerdin@4264: locked_verify(); mgerdin@4264: } mgerdin@4264: } coleenp@4037: void verify_free_chunks_total(); coleenp@4037: coleenp@4037: void locked_print_free_chunks(outputStream* st); coleenp@4037: void locked_print_sum_free_chunks(outputStream* st); jmasa@4196: stefank@5771: void print_on(outputStream* st) const; coleenp@4037: }; coleenp@4037: coleenp@4037: // Used to manage the free list of Metablocks (a block corresponds coleenp@4037: // to the allocation of a quantum of metadata). coleenp@4037: class BlockFreelist VALUE_OBJ_CLASS_SPEC { jmasa@4196: BlockTreeDictionary* _dictionary; jmasa@4196: mgerdin@5699: // Only allocate and split from freelist if the size of the allocation mgerdin@5699: // is at least 1/4th the size of the available block. mgerdin@5699: const static int WasteMultiplier = 4; mgerdin@5699: coleenp@4037: // Accessors jmasa@4196: BlockTreeDictionary* dictionary() const { return _dictionary; } coleenp@4037: coleenp@4037: public: coleenp@4037: BlockFreelist(); coleenp@4037: ~BlockFreelist(); coleenp@4037: coleenp@4037: // Get and return a block to the free list jmasa@4196: MetaWord* get_block(size_t word_size); jmasa@4196: void return_block(MetaWord* p, size_t word_size); jmasa@4196: jmasa@4196: size_t total_size() { jmasa@4196: if (dictionary() == NULL) { coleenp@4037: return 0; jmasa@4196: } else { jmasa@4196: return dictionary()->total_size(); coleenp@4037: } jmasa@4196: } coleenp@4037: coleenp@4037: void print_on(outputStream* st) const; coleenp@4037: }; coleenp@4037: stefank@5941: // A VirtualSpaceList node. coleenp@4037: class VirtualSpaceNode : public CHeapObj { coleenp@4037: friend class VirtualSpaceList; coleenp@4037: coleenp@4037: // Link to next VirtualSpaceNode coleenp@4037: VirtualSpaceNode* _next; coleenp@4037: coleenp@4037: // total in the VirtualSpace coleenp@4037: MemRegion _reserved; coleenp@4037: ReservedSpace _rs; coleenp@4037: VirtualSpace _virtual_space; coleenp@4037: MetaWord* _top; jmasa@5007: // count of chunks contained in this VirtualSpace jmasa@5007: uintx _container_count; coleenp@4037: coleenp@4037: // Convenience functions to access the _virtual_space coleenp@4037: char* low() const { return virtual_space()->low(); } coleenp@4037: char* high() const { return virtual_space()->high(); } coleenp@4037: jmasa@5007: // The first Metachunk will be allocated at the bottom of the jmasa@5007: // VirtualSpace jmasa@5007: Metachunk* first_chunk() { return (Metachunk*) bottom(); } jmasa@5007: mgerdin@6004: // Committed but unused space in the virtual space mgerdin@6004: size_t free_words_in_vs() const; coleenp@4037: public: coleenp@4037: coleenp@4037: VirtualSpaceNode(size_t byte_size); jmasa@5007: VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {} coleenp@4037: ~VirtualSpaceNode(); coleenp@4037: hseigel@5528: // Convenience functions for logical bottom and end hseigel@5528: MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); } hseigel@5528: MetaWord* end() const { return (MetaWord*) _virtual_space.high(); } hseigel@5528: coleenp@6678: bool contains(const void* ptr) { return ptr >= low() && ptr < high(); } coleenp@6678: stefank@5704: size_t reserved_words() const { return _virtual_space.reserved_size() / BytesPerWord; } stefank@5704: size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; } stefank@5704: stefank@5863: bool is_pre_committed() const { return _virtual_space.special(); } stefank@5863: coleenp@4037: // address of next available space in _virtual_space; coleenp@4037: // Accessors coleenp@4037: VirtualSpaceNode* next() { return _next; } coleenp@4037: void set_next(VirtualSpaceNode* v) { _next = v; } coleenp@4037: coleenp@4037: void set_reserved(MemRegion const v) { _reserved = v; } coleenp@4037: void set_top(MetaWord* v) { _top = v; } coleenp@4037: coleenp@4037: // Accessors coleenp@4037: MemRegion* reserved() { return &_reserved; } coleenp@4037: VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; } coleenp@4037: jmasa@5015: // Returns true if "word_size" is available in the VirtualSpace stefank@6170: bool is_available(size_t word_size) { return word_size <= pointer_delta(end(), _top, sizeof(MetaWord)); } coleenp@4037: coleenp@4037: MetaWord* top() const { return _top; } coleenp@4037: void inc_top(size_t word_size) { _top += word_size; } coleenp@4037: jmasa@5007: uintx container_count() { return _container_count; } stefank@5771: void inc_container_count(); jmasa@5007: void dec_container_count(); jmasa@5007: #ifdef ASSERT stefank@5771: uint container_count_slow(); jmasa@5007: void verify_container_count(); jmasa@5007: #endif jmasa@5007: coleenp@4037: // used and capacity in this single entry in the list coleenp@4037: size_t used_words_in_vs() const; coleenp@4037: size_t capacity_words_in_vs() const; coleenp@4037: coleenp@4037: bool initialize(); coleenp@4037: coleenp@4037: // get space from the virtual space coleenp@4037: Metachunk* take_from_committed(size_t chunk_word_size); coleenp@4037: coleenp@4037: // Allocate a chunk from the virtual space and return it. coleenp@4037: Metachunk* get_chunk_vs(size_t chunk_word_size); coleenp@4037: coleenp@4037: // Expands/shrinks the committed space in a virtual space. Delegates coleenp@4037: // to Virtualspace stefank@5863: bool expand_by(size_t min_words, size_t preferred_words); coleenp@4037: jmasa@5007: // In preparation for deleting this node, remove all the chunks jmasa@5007: // in the node from any freelist. jmasa@5007: void purge(ChunkManager* chunk_manager); jmasa@5007: mgerdin@6004: // If an allocation doesn't fit in the current node a new node is created. mgerdin@6004: // Allocate chunks out of the remaining committed space in this node mgerdin@6004: // to avoid wasting that memory. mgerdin@6004: // This always adds up because all the chunk sizes are multiples of mgerdin@6004: // the smallest chunk size. mgerdin@6004: void retire(ChunkManager* chunk_manager); mgerdin@6004: coleenp@4304: #ifdef ASSERT coleenp@4037: // Debug support coleenp@4037: void mangle(); coleenp@4304: #endif coleenp@4037: coleenp@4037: void print_on(outputStream* st) const; coleenp@4037: }; coleenp@4037: stefank@5863: #define assert_is_ptr_aligned(ptr, alignment) \ stefank@5863: assert(is_ptr_aligned(ptr, alignment), \ stefank@5863: err_msg(PTR_FORMAT " is not aligned to " \ stefank@5863: SIZE_FORMAT, ptr, alignment)) stefank@5863: stefank@5863: #define assert_is_size_aligned(size, alignment) \ stefank@5863: assert(is_size_aligned(size, alignment), \ stefank@5863: err_msg(SIZE_FORMAT " is not aligned to " \ stefank@5863: SIZE_FORMAT, size, alignment)) stefank@5863: stefank@5863: stefank@5863: // Decide if large pages should be committed when the memory is reserved. stefank@5863: static bool should_commit_large_pages_when_reserving(size_t bytes) { stefank@5863: if (UseLargePages && UseLargePagesInMetaspace && !os::can_commit_large_page_memory()) { stefank@5863: size_t words = bytes / BytesPerWord; stefank@5863: bool is_class = false; // We never reserve large pages for the class space. stefank@5863: if (MetaspaceGC::can_expand(words, is_class) && stefank@5863: MetaspaceGC::allowed_expansion() >= words) { stefank@5863: return true; stefank@5863: } stefank@5863: } stefank@5863: stefank@5863: return false; stefank@5863: } stefank@5863: coleenp@4037: // byte_size is the size of the associated virtualspace. stefank@5863: VirtualSpaceNode::VirtualSpaceNode(size_t bytes) : _top(NULL), _next(NULL), _rs(), _container_count(0) { stefank@5863: assert_is_size_aligned(bytes, Metaspace::reserve_alignment()); zgu@4752: iklam@7089: #if INCLUDE_CDS coleenp@4804: // This allocates memory with mmap. For DumpSharedspaces, try to reserve coleenp@4804: // configurable address, generally at the top of the Java heap so other coleenp@4804: // memory addresses don't conflict. coleenp@4037: if (DumpSharedSpaces) { stefank@5863: bool large_pages = false; // No large pages when dumping the CDS archive. stefank@5863: char* shared_base = (char*)align_ptr_up((char*)SharedBaseAddress, Metaspace::reserve_alignment()); stefank@5863: stefank@5863: _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages, shared_base, 0); coleenp@4037: if (_rs.is_reserved()) { coleenp@4804: assert(shared_base == 0 || _rs.base() == shared_base, "should match"); coleenp@4037: } else { coleenp@4804: // Get a mmap region anywhere if the SharedBaseAddress fails. stefank@5863: _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages); coleenp@4037: } coleenp@4037: MetaspaceShared::set_shared_rs(&_rs); iklam@7089: } else iklam@7089: #endif iklam@7089: { stefank@5863: bool large_pages = should_commit_large_pages_when_reserving(bytes); stefank@5863: stefank@5863: _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages); coleenp@4037: } coleenp@4037: stefank@5863: if (_rs.is_reserved()) { stefank@5863: assert(_rs.base() != NULL, "Catch if we get a NULL address"); stefank@5863: assert(_rs.size() != 0, "Catch if we get a 0 size"); stefank@5863: assert_is_ptr_aligned(_rs.base(), Metaspace::reserve_alignment()); stefank@5863: assert_is_size_aligned(_rs.size(), Metaspace::reserve_alignment()); stefank@5863: stefank@5863: MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass); stefank@5863: } coleenp@4037: } coleenp@4037: jmasa@5007: void VirtualSpaceNode::purge(ChunkManager* chunk_manager) { jmasa@5007: Metachunk* chunk = first_chunk(); jmasa@5007: Metachunk* invalid_chunk = (Metachunk*) top(); jmasa@5007: while (chunk < invalid_chunk ) { stefank@5941: assert(chunk->is_tagged_free(), "Should be tagged free"); stefank@5941: MetaWord* next = ((MetaWord*)chunk) + chunk->word_size(); stefank@5941: chunk_manager->remove_chunk(chunk); stefank@5941: assert(chunk->next() == NULL && stefank@5941: chunk->prev() == NULL, stefank@5941: "Was not removed from its list"); stefank@5941: chunk = (Metachunk*) next; jmasa@5007: } jmasa@5007: } jmasa@5007: jmasa@5007: #ifdef ASSERT jmasa@5007: uint VirtualSpaceNode::container_count_slow() { jmasa@5007: uint count = 0; jmasa@5007: Metachunk* chunk = first_chunk(); jmasa@5007: Metachunk* invalid_chunk = (Metachunk*) top(); jmasa@5007: while (chunk < invalid_chunk ) { jmasa@5007: MetaWord* next = ((MetaWord*)chunk) + chunk->word_size(); jmasa@5007: // Don't count the chunks on the free lists. Those are jmasa@5007: // still part of the VirtualSpaceNode but not currently jmasa@5007: // counted. stefank@5941: if (!chunk->is_tagged_free()) { jmasa@5007: count++; jmasa@5007: } jmasa@5007: chunk = (Metachunk*) next; jmasa@5007: } jmasa@5007: return count; jmasa@5007: } jmasa@5007: #endif jmasa@5007: coleenp@4037: // List of VirtualSpaces for metadata allocation. coleenp@4037: class VirtualSpaceList : public CHeapObj { coleenp@4037: friend class VirtualSpaceNode; coleenp@4037: coleenp@4037: enum VirtualSpaceSizes { coleenp@4037: VirtualSpaceSize = 256 * K coleenp@4037: }; coleenp@4037: coleenp@4037: // Head of the list coleenp@4037: VirtualSpaceNode* _virtual_space_list; coleenp@4037: // virtual space currently being used for allocations coleenp@4037: VirtualSpaceNode* _current_virtual_space; coleenp@4037: stefank@5863: // Is this VirtualSpaceList used for the compressed class space coleenp@4037: bool _is_class; coleenp@4037: stefank@5704: // Sum of reserved and committed memory in the virtual spaces stefank@5704: size_t _reserved_words; stefank@5704: size_t _committed_words; stefank@5704: stefank@5704: // Number of virtual spaces coleenp@4037: size_t _virtual_space_count; coleenp@4037: coleenp@4037: ~VirtualSpaceList(); coleenp@4037: coleenp@4037: VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; } coleenp@4037: coleenp@4037: void set_virtual_space_list(VirtualSpaceNode* v) { coleenp@4037: _virtual_space_list = v; coleenp@4037: } coleenp@4037: void set_current_virtual_space(VirtualSpaceNode* v) { coleenp@4037: _current_virtual_space = v; coleenp@4037: } coleenp@4037: stefank@5704: void link_vs(VirtualSpaceNode* new_entry); coleenp@4037: coleenp@4037: // Get another virtual space and add it to the list. This coleenp@4037: // is typically prompted by a failed attempt to allocate a chunk coleenp@4037: // and is typically followed by the allocation of a chunk. stefank@5863: bool create_new_virtual_space(size_t vs_word_size); coleenp@4037: mgerdin@6004: // Chunk up the unused committed space in the current mgerdin@6004: // virtual space and add the chunks to the free list. mgerdin@6004: void retire_current_virtual_space(); mgerdin@6004: coleenp@4037: public: coleenp@4037: VirtualSpaceList(size_t word_size); coleenp@4037: VirtualSpaceList(ReservedSpace rs); coleenp@4037: jmasa@5015: size_t free_bytes(); jmasa@5015: jmasa@4382: Metachunk* get_new_chunk(size_t word_size, jmasa@4382: size_t grow_chunks_by_words, jmasa@4382: size_t medium_chunk_bunch); jmasa@4382: stefank@5863: bool expand_node_by(VirtualSpaceNode* node, stefank@5863: size_t min_words, stefank@5863: size_t preferred_words); stefank@5863: stefank@5863: bool expand_by(size_t min_words, stefank@5863: size_t preferred_words); coleenp@4037: coleenp@4037: VirtualSpaceNode* current_virtual_space() { coleenp@4037: return _current_virtual_space; coleenp@4037: } coleenp@4037: coleenp@4037: bool is_class() const { return _is_class; } coleenp@4037: stefank@5863: bool initialization_succeeded() { return _virtual_space_list != NULL; } coleenp@4037: stefank@5704: size_t reserved_words() { return _reserved_words; } stefank@5704: size_t reserved_bytes() { return reserved_words() * BytesPerWord; } stefank@5704: size_t committed_words() { return _committed_words; } stefank@5704: size_t committed_bytes() { return committed_words() * BytesPerWord; } stefank@5704: stefank@5704: void inc_reserved_words(size_t v); stefank@5704: void dec_reserved_words(size_t v); stefank@5704: void inc_committed_words(size_t v); stefank@5704: void dec_committed_words(size_t v); jmasa@5007: void inc_virtual_space_count(); jmasa@5007: void dec_virtual_space_count(); jmasa@5007: coleenp@6678: bool contains(const void* ptr); coleenp@6678: jmasa@5007: // Unlink empty VirtualSpaceNodes and free it. stefank@5771: void purge(ChunkManager* chunk_manager); coleenp@4037: coleenp@4037: void print_on(outputStream* st) const; coleenp@4037: coleenp@4037: class VirtualSpaceListIterator : public StackObj { coleenp@4037: VirtualSpaceNode* _virtual_spaces; coleenp@4037: public: coleenp@4037: VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) : coleenp@4037: _virtual_spaces(virtual_spaces) {} coleenp@4037: coleenp@4037: bool repeat() { coleenp@4037: return _virtual_spaces != NULL; coleenp@4037: } coleenp@4037: coleenp@4037: VirtualSpaceNode* get_next() { coleenp@4037: VirtualSpaceNode* result = _virtual_spaces; coleenp@4037: if (_virtual_spaces != NULL) { coleenp@4037: _virtual_spaces = _virtual_spaces->next(); coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: }; coleenp@4037: }; coleenp@4037: coleenp@4037: class Metadebug : AllStatic { coleenp@4037: // Debugging support for Metaspaces coleenp@4037: static int _allocation_fail_alot_count; coleenp@4037: coleenp@4037: public: coleenp@4037: coleenp@4037: static void init_allocation_fail_alot_count(); coleenp@4037: #ifdef ASSERT coleenp@4037: static bool test_metadata_failure(); coleenp@4037: #endif coleenp@4037: }; coleenp@4037: coleenp@4037: int Metadebug::_allocation_fail_alot_count = 0; coleenp@4037: coleenp@4037: // SpaceManager - used by Metaspace to handle allocations coleenp@4037: class SpaceManager : public CHeapObj { coleenp@4037: friend class Metaspace; coleenp@4037: friend class Metadebug; coleenp@4037: coleenp@4037: private: jmasa@4382: coleenp@6305: // protects allocations coleenp@4037: Mutex* const _lock; coleenp@4037: jmasa@5162: // Type of metadata allocated. jmasa@5162: Metaspace::MetadataType _mdtype; jmasa@5162: coleenp@4037: // List of chunks in use by this SpaceManager. Allocations coleenp@4037: // are done from the current chunk. The list is used for deallocating coleenp@4037: // chunks when the SpaceManager is freed. jmasa@4196: Metachunk* _chunks_in_use[NumberOfInUseLists]; coleenp@4037: Metachunk* _current_chunk; coleenp@4037: jwilhelm@7867: // Number of small chunks to allocate to a manager jwilhelm@7867: // If class space manager, small chunks are unlimited coleenp@4037: static uint const _small_chunk_limit; coleenp@4037: coleenp@4037: // Sum of all space in allocated chunks jmasa@5015: size_t _allocated_blocks_words; jmasa@5015: jmasa@5015: // Sum of all allocated chunks jmasa@5015: size_t _allocated_chunks_words; jmasa@5015: size_t _allocated_chunks_count; coleenp@4037: coleenp@4037: // Free lists of blocks are per SpaceManager since they coleenp@4037: // are assumed to be in chunks in use by the SpaceManager coleenp@4037: // and all chunks in use by a SpaceManager are freed when coleenp@4037: // the class loader using the SpaceManager is collected. coleenp@4037: BlockFreelist _block_freelists; coleenp@4037: coleenp@4037: // protects virtualspace and chunk expansions coleenp@4037: static const char* _expand_lock_name; coleenp@4037: static const int _expand_lock_rank; coleenp@4037: static Mutex* const _expand_lock; coleenp@4037: jmasa@4382: private: coleenp@4037: // Accessors coleenp@4037: Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; } coleenp@6305: void set_chunks_in_use(ChunkIndex index, Metachunk* v) { coleenp@6305: _chunks_in_use[index] = v; coleenp@6305: } coleenp@4037: coleenp@4037: BlockFreelist* block_freelists() const { coleenp@4037: return (BlockFreelist*) &_block_freelists; coleenp@4037: } coleenp@4037: jmasa@5162: Metaspace::MetadataType mdtype() { return _mdtype; } stefank@5771: stefank@5771: VirtualSpaceList* vs_list() const { return Metaspace::get_space_list(_mdtype); } stefank@5771: ChunkManager* chunk_manager() const { return Metaspace::get_chunk_manager(_mdtype); } coleenp@4037: coleenp@4037: Metachunk* current_chunk() const { return _current_chunk; } coleenp@4037: void set_current_chunk(Metachunk* v) { coleenp@4037: _current_chunk = v; coleenp@4037: } coleenp@4037: coleenp@4037: Metachunk* find_current_chunk(size_t word_size); coleenp@4037: coleenp@4037: // Add chunk to the list of chunks in use coleenp@4037: void add_chunk(Metachunk* v, bool make_current); mgerdin@5699: void retire_current_chunk(); coleenp@4037: coleenp@4037: Mutex* lock() const { return _lock; } coleenp@4037: jmasa@4382: const char* chunk_size_name(ChunkIndex index) const; jmasa@4382: jmasa@4382: protected: jmasa@4382: void initialize(); jmasa@4382: coleenp@4037: public: jmasa@5162: SpaceManager(Metaspace::MetadataType mdtype, stefank@5771: Mutex* lock); coleenp@4037: ~SpaceManager(); coleenp@4037: jmasa@4382: enum ChunkMultiples { jmasa@4382: MediumChunkMultiple = 4 coleenp@4037: }; coleenp@4037: stefank@5771: bool is_class() { return _mdtype == Metaspace::ClassType; } stefank@5771: coleenp@4037: // Accessors mgerdin@6004: size_t specialized_chunk_size() { return (size_t) is_class() ? ClassSpecializedChunk : SpecializedChunk; } mgerdin@6004: size_t small_chunk_size() { return (size_t) is_class() ? ClassSmallChunk : SmallChunk; } mgerdin@6004: size_t medium_chunk_size() { return (size_t) is_class() ? ClassMediumChunk : MediumChunk; } mgerdin@6004: size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; } mgerdin@6004: mgerdin@6004: size_t smallest_chunk_size() { return specialized_chunk_size(); } jmasa@4382: jmasa@5015: size_t allocated_blocks_words() const { return _allocated_blocks_words; } jmasa@5015: size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; } jmasa@5015: size_t allocated_chunks_words() const { return _allocated_chunks_words; } jmasa@5015: size_t allocated_chunks_count() const { return _allocated_chunks_count; } jmasa@5015: jmasa@4382: bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); } coleenp@4037: coleenp@4037: static Mutex* expand_lock() { return _expand_lock; } coleenp@4037: jmasa@5015: // Increment the per Metaspace and global running sums for Metachunks jmasa@5015: // by the given size. This is used when a Metachunk to added to jmasa@5015: // the in-use list. jmasa@5015: void inc_size_metrics(size_t words); jmasa@5015: // Increment the per Metaspace and global running sums Metablocks by the given jmasa@5015: // size. This is used when a Metablock is allocated. jmasa@5015: void inc_used_metrics(size_t words); jmasa@5015: // Delete the portion of the running sums for this SpaceManager. That is, jmasa@5015: // the globals running sums for the Metachunks and Metablocks are jmasa@5015: // decremented for all the Metachunks in-use by this SpaceManager. jmasa@5015: void dec_total_from_size_metrics(); jmasa@5015: jmasa@4382: // Set the sizes for the initial chunks. jmasa@4382: void get_initial_chunk_sizes(Metaspace::MetaspaceType type, jmasa@4382: size_t* chunk_word_size, jmasa@4382: size_t* class_chunk_word_size); jmasa@4382: coleenp@4037: size_t sum_capacity_in_chunks_in_use() const; coleenp@4037: size_t sum_used_in_chunks_in_use() const; coleenp@4037: size_t sum_free_in_chunks_in_use() const; coleenp@4037: size_t sum_waste_in_chunks_in_use() const; coleenp@4037: size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const; coleenp@4037: coleenp@4037: size_t sum_count_in_chunks_in_use(); coleenp@4037: size_t sum_count_in_chunks_in_use(ChunkIndex i); coleenp@4037: jmasa@4382: Metachunk* get_new_chunk(size_t word_size, size_t grow_chunks_by_words); jmasa@4382: coleenp@4037: // Block allocation and deallocation. coleenp@4037: // Allocates a block from the current chunk coleenp@4037: MetaWord* allocate(size_t word_size); coleenp@4037: coleenp@4037: // Helper for allocations jmasa@4196: MetaWord* allocate_work(size_t word_size); coleenp@4037: coleenp@4037: // Returns a block to the per manager freelist jmasa@4196: void deallocate(MetaWord* p, size_t word_size); coleenp@4037: coleenp@4037: // Based on the allocation size and a minimum chunk size, coleenp@4037: // returned chunk size (for expanding space for chunk allocation). coleenp@4037: size_t calc_chunk_size(size_t allocation_word_size); coleenp@4037: coleenp@4037: // Called when an allocation from the current chunk fails. coleenp@4037: // Gets a new chunk (may require getting a new virtual space), coleenp@4037: // and allocates from that chunk. jmasa@4196: MetaWord* grow_and_allocate(size_t word_size); coleenp@4037: stefank@5864: // Notify memory usage to MemoryService. stefank@5864: void track_metaspace_memory_usage(); stefank@5864: coleenp@4037: // debugging support. coleenp@4037: coleenp@4037: void dump(outputStream* const out) const; coleenp@4037: void print_on(outputStream* st) const; coleenp@4037: void locked_print_chunks_in_use_on(outputStream* st) const; coleenp@4037: coleenp@4037: void verify(); jmasa@4327: void verify_chunk_size(Metachunk* chunk); coleenp@4304: NOT_PRODUCT(void mangle_freed_chunks();) coleenp@4037: #ifdef ASSERT jmasa@5015: void verify_allocated_blocks_words(); coleenp@4037: #endif iklam@5208: iklam@5208: size_t get_raw_word_size(size_t word_size) { iklam@5208: size_t byte_size = word_size * BytesPerWord; iklam@5208: stefank@5941: size_t raw_bytes_size = MAX2(byte_size, sizeof(Metablock)); stefank@5941: raw_bytes_size = align_size_up(raw_bytes_size, Metachunk::object_alignment()); stefank@5941: iklam@5208: size_t raw_word_size = raw_bytes_size / BytesPerWord; iklam@5208: assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem"); iklam@5208: iklam@5208: return raw_word_size; iklam@5208: } coleenp@4037: }; coleenp@4037: coleenp@4037: uint const SpaceManager::_small_chunk_limit = 4; coleenp@4037: coleenp@4037: const char* SpaceManager::_expand_lock_name = coleenp@4037: "SpaceManager chunk allocation lock"; coleenp@4037: const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1; coleenp@4037: Mutex* const SpaceManager::_expand_lock = coleenp@4037: new Mutex(SpaceManager::_expand_lock_rank, coleenp@4037: SpaceManager::_expand_lock_name, coleenp@4037: Mutex::_allow_vm_block_flag); coleenp@4037: jmasa@5007: void VirtualSpaceNode::inc_container_count() { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: _container_count++; jmasa@5007: assert(_container_count == container_count_slow(), jmasa@5007: err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT stefank@5771: " container_count_slow() " SIZE_FORMAT, jmasa@5007: _container_count, container_count_slow())); jmasa@5007: } jmasa@5007: jmasa@5007: void VirtualSpaceNode::dec_container_count() { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: _container_count--; jmasa@5007: } jmasa@5007: jmasa@5007: #ifdef ASSERT jmasa@5007: void VirtualSpaceNode::verify_container_count() { jmasa@5007: assert(_container_count == container_count_slow(), jmasa@5007: err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT stefank@5771: " container_count_slow() " SIZE_FORMAT, _container_count, container_count_slow())); jmasa@5007: } jmasa@5007: #endif jmasa@5007: coleenp@4037: // BlockFreelist methods coleenp@4037: coleenp@4037: BlockFreelist::BlockFreelist() : _dictionary(NULL) {} coleenp@4037: coleenp@4037: BlockFreelist::~BlockFreelist() { coleenp@4037: if (_dictionary != NULL) { coleenp@4037: if (Verbose && TraceMetadataChunkAllocation) { coleenp@4037: _dictionary->print_free_lists(gclog_or_tty); coleenp@4037: } coleenp@4037: delete _dictionary; coleenp@4037: } coleenp@4037: } coleenp@4037: jmasa@4196: void BlockFreelist::return_block(MetaWord* p, size_t word_size) { stefank@5941: Metablock* free_chunk = ::new (p) Metablock(word_size); coleenp@4037: if (dictionary() == NULL) { jmasa@4196: _dictionary = new BlockTreeDictionary(); coleenp@4037: } jmasa@4196: dictionary()->return_chunk(free_chunk); coleenp@4037: } coleenp@4037: jmasa@4196: MetaWord* BlockFreelist::get_block(size_t word_size) { coleenp@4037: if (dictionary() == NULL) { coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: goetz@6337: if (word_size < TreeChunk >::min_size()) { jmasa@4196: // Dark matter. Too small for dictionary. coleenp@4037: return NULL; coleenp@4037: } jmasa@4196: jmasa@4196: Metablock* free_block = mgerdin@5699: dictionary()->get_chunk(word_size, FreeBlockDictionary::atLeast); jmasa@4196: if (free_block == NULL) { jmasa@4196: return NULL; jmasa@4196: } jmasa@4196: mgerdin@5699: const size_t block_size = free_block->size(); mgerdin@5699: if (block_size > WasteMultiplier * word_size) { mgerdin@5699: return_block((MetaWord*)free_block, block_size); mgerdin@5699: return NULL; mgerdin@5699: } mgerdin@5699: mgerdin@5699: MetaWord* new_block = (MetaWord*)free_block; mgerdin@5699: assert(block_size >= word_size, "Incorrect size of block from freelist"); mgerdin@5699: const size_t unused = block_size - word_size; goetz@6337: if (unused >= TreeChunk >::min_size()) { mgerdin@5699: return_block(new_block + word_size, unused); mgerdin@5699: } mgerdin@5699: mgerdin@5699: return new_block; coleenp@4037: } coleenp@4037: coleenp@4037: void BlockFreelist::print_on(outputStream* st) const { coleenp@4037: if (dictionary() == NULL) { coleenp@4037: return; coleenp@4037: } coleenp@4037: dictionary()->print_free_lists(st); coleenp@4037: } coleenp@4037: coleenp@4037: // VirtualSpaceNode methods coleenp@4037: coleenp@4037: VirtualSpaceNode::~VirtualSpaceNode() { coleenp@4037: _rs.release(); jmasa@5007: #ifdef ASSERT jmasa@5007: size_t word_size = sizeof(*this) / BytesPerWord; jmasa@5007: Copy::fill_to_words((HeapWord*) this, word_size, 0xf1f1f1f1); jmasa@5007: #endif coleenp@4037: } coleenp@4037: coleenp@4037: size_t VirtualSpaceNode::used_words_in_vs() const { coleenp@4037: return pointer_delta(top(), bottom(), sizeof(MetaWord)); coleenp@4037: } coleenp@4037: coleenp@4037: // Space committed in the VirtualSpace coleenp@4037: size_t VirtualSpaceNode::capacity_words_in_vs() const { coleenp@4037: return pointer_delta(end(), bottom(), sizeof(MetaWord)); coleenp@4037: } coleenp@4037: jmasa@5015: size_t VirtualSpaceNode::free_words_in_vs() const { jmasa@5015: return pointer_delta(end(), top(), sizeof(MetaWord)); jmasa@5015: } coleenp@4037: coleenp@4037: // Allocates the chunk from the virtual space only. coleenp@4037: // This interface is also used internally for debugging. Not all coleenp@4037: // chunks removed here are necessarily used for allocation. coleenp@4037: Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) { coleenp@4037: // Bottom of the new chunk coleenp@4037: MetaWord* chunk_limit = top(); coleenp@4037: assert(chunk_limit != NULL, "Not safe to call this method"); coleenp@4037: stefank@5863: // The virtual spaces are always expanded by the stefank@5863: // commit granularity to enforce the following condition. stefank@5863: // Without this the is_available check will not work correctly. stefank@5863: assert(_virtual_space.committed_size() == _virtual_space.actual_committed_size(), stefank@5863: "The committed memory doesn't match the expanded memory."); stefank@5863: coleenp@4037: if (!is_available(chunk_word_size)) { coleenp@4037: if (TraceMetadataChunkAllocation) { stefank@5708: gclog_or_tty->print("VirtualSpaceNode::take_from_committed() not available %d words ", chunk_word_size); coleenp@4037: // Dump some information about the virtual space that is nearly full stefank@5708: print_on(gclog_or_tty); coleenp@4037: } coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: coleenp@4037: // Take the space (bump top on the current virtual space). coleenp@4037: inc_top(chunk_word_size); coleenp@4037: jmasa@5007: // Initialize the chunk jmasa@5007: Metachunk* result = ::new (chunk_limit) Metachunk(chunk_word_size, this); coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: // Expand the virtual space (commit more of the reserved space) stefank@5863: bool VirtualSpaceNode::expand_by(size_t min_words, size_t preferred_words) { stefank@5863: size_t min_bytes = min_words * BytesPerWord; stefank@5863: size_t preferred_bytes = preferred_words * BytesPerWord; stefank@5863: stefank@5863: size_t uncommitted = virtual_space()->reserved_size() - virtual_space()->actual_committed_size(); stefank@5863: stefank@5863: if (uncommitted < min_bytes) { stefank@5863: return false; coleenp@4037: } stefank@5863: stefank@5863: size_t commit = MIN2(preferred_bytes, uncommitted); stefank@5863: bool result = virtual_space()->expand_by(commit, false); stefank@5863: stefank@5863: assert(result, "Failed to commit memory"); stefank@5863: coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: Metachunk* result = take_from_committed(chunk_word_size); jmasa@5007: if (result != NULL) { jmasa@5007: inc_container_count(); jmasa@5007: } jmasa@5007: return result; coleenp@4037: } coleenp@4037: coleenp@4037: bool VirtualSpaceNode::initialize() { coleenp@4037: coleenp@4037: if (!_rs.is_reserved()) { coleenp@4037: return false; coleenp@4037: } coleenp@4037: stefank@5863: // These are necessary restriction to make sure that the virtual space always stefank@5863: // grows in steps of Metaspace::commit_alignment(). If both base and size are stefank@5863: // aligned only the middle alignment of the VirtualSpace is used. stefank@5863: assert_is_ptr_aligned(_rs.base(), Metaspace::commit_alignment()); stefank@5863: assert_is_size_aligned(_rs.size(), Metaspace::commit_alignment()); stefank@5863: stefank@5863: // ReservedSpaces marked as special will have the entire memory stefank@5863: // pre-committed. Setting a committed size will make sure that stefank@5863: // committed_size and actual_committed_size agrees. stefank@5863: size_t pre_committed_size = _rs.special() ? _rs.size() : 0; stefank@5863: stefank@5863: bool result = virtual_space()->initialize_with_granularity(_rs, pre_committed_size, stefank@5863: Metaspace::commit_alignment()); coleenp@4037: if (result) { stefank@5863: assert(virtual_space()->committed_size() == virtual_space()->actual_committed_size(), stefank@5863: "Checking that the pre-committed memory was registered by the VirtualSpace"); stefank@5863: coleenp@4037: set_top((MetaWord*)virtual_space()->low()); coleenp@4037: set_reserved(MemRegion((HeapWord*)_rs.base(), coleenp@4037: (HeapWord*)(_rs.base() + _rs.size()))); coleenp@4038: coleenp@4038: assert(reserved()->start() == (HeapWord*) _rs.base(), coleenp@4038: err_msg("Reserved start was not set properly " PTR_FORMAT coleenp@4038: " != " PTR_FORMAT, reserved()->start(), _rs.base())); coleenp@4038: assert(reserved()->word_size() == _rs.size() / BytesPerWord, coleenp@4038: err_msg("Reserved size was not set properly " SIZE_FORMAT coleenp@4038: " != " SIZE_FORMAT, reserved()->word_size(), coleenp@4038: _rs.size() / BytesPerWord)); coleenp@4037: } coleenp@4037: coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: void VirtualSpaceNode::print_on(outputStream* st) const { coleenp@4037: size_t used = used_words_in_vs(); coleenp@4037: size_t capacity = capacity_words_in_vs(); coleenp@4037: VirtualSpace* vs = virtual_space(); coleenp@4037: st->print_cr(" space @ " PTR_FORMAT " " SIZE_FORMAT "K, %3d%% used " coleenp@4037: "[" PTR_FORMAT ", " PTR_FORMAT ", " coleenp@4037: PTR_FORMAT ", " PTR_FORMAT ")", jmasa@4382: vs, capacity / K, jmasa@4382: capacity == 0 ? 0 : used * 100 / capacity, coleenp@4037: bottom(), top(), end(), coleenp@4037: vs->high_boundary()); coleenp@4037: } coleenp@4037: coleenp@4304: #ifdef ASSERT coleenp@4037: void VirtualSpaceNode::mangle() { coleenp@4037: size_t word_size = capacity_words_in_vs(); coleenp@4037: Copy::fill_to_words((HeapWord*) low(), word_size, 0xf1f1f1f1); coleenp@4037: } coleenp@4304: #endif // ASSERT coleenp@4037: coleenp@4037: // VirtualSpaceList methods coleenp@4037: // Space allocated from the VirtualSpace coleenp@4037: coleenp@4037: VirtualSpaceList::~VirtualSpaceList() { coleenp@4037: VirtualSpaceListIterator iter(virtual_space_list()); coleenp@4037: while (iter.repeat()) { coleenp@4037: VirtualSpaceNode* vsl = iter.get_next(); coleenp@4037: delete vsl; coleenp@4037: } coleenp@4037: } coleenp@4037: stefank@5704: void VirtualSpaceList::inc_reserved_words(size_t v) { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); stefank@5704: _reserved_words = _reserved_words + v; jmasa@5007: } stefank@5704: void VirtualSpaceList::dec_reserved_words(size_t v) { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); stefank@5704: _reserved_words = _reserved_words - v; stefank@5704: } stefank@5704: stefank@5863: #define assert_committed_below_limit() \ stefank@5863: assert(MetaspaceAux::committed_bytes() <= MaxMetaspaceSize, \ stefank@5863: err_msg("Too much committed memory. Committed: " SIZE_FORMAT \ stefank@5863: " limit (MaxMetaspaceSize): " SIZE_FORMAT, \ stefank@5863: MetaspaceAux::committed_bytes(), MaxMetaspaceSize)); stefank@5863: stefank@5704: void VirtualSpaceList::inc_committed_words(size_t v) { stefank@5704: assert_lock_strong(SpaceManager::expand_lock()); stefank@5704: _committed_words = _committed_words + v; stefank@5863: stefank@5863: assert_committed_below_limit(); stefank@5704: } stefank@5704: void VirtualSpaceList::dec_committed_words(size_t v) { stefank@5704: assert_lock_strong(SpaceManager::expand_lock()); stefank@5704: _committed_words = _committed_words - v; stefank@5863: stefank@5863: assert_committed_below_limit(); jmasa@5007: } jmasa@5007: jmasa@5007: void VirtualSpaceList::inc_virtual_space_count() { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: _virtual_space_count++; jmasa@5007: } jmasa@5007: void VirtualSpaceList::dec_virtual_space_count() { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: _virtual_space_count--; jmasa@5007: } jmasa@5007: jmasa@5007: void ChunkManager::remove_chunk(Metachunk* chunk) { jmasa@5007: size_t word_size = chunk->word_size(); jmasa@5007: ChunkIndex index = list_index(word_size); jmasa@5007: if (index != HumongousIndex) { jmasa@5007: free_chunks(index)->remove_chunk(chunk); jmasa@5007: } else { jmasa@5007: humongous_dictionary()->remove_chunk(chunk); jmasa@5007: } jmasa@5007: jmasa@5007: // Chunk is being removed from the chunks free list. stefank@5941: dec_free_chunks_total(chunk->word_size()); jmasa@5007: } jmasa@5007: jmasa@5007: // Walk the list of VirtualSpaceNodes and delete jmasa@5007: // nodes with a 0 container_count. Remove Metachunks in jmasa@5007: // the node from their respective freelists. stefank@5771: void VirtualSpaceList::purge(ChunkManager* chunk_manager) { coleenp@6678: assert(SafepointSynchronize::is_at_safepoint(), "must be called at safepoint for contains to work"); jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: // Don't use a VirtualSpaceListIterator because this jmasa@5007: // list is being changed and a straightforward use of an iterator is not safe. jmasa@5007: VirtualSpaceNode* purged_vsl = NULL; jmasa@5007: VirtualSpaceNode* prev_vsl = virtual_space_list(); jmasa@5007: VirtualSpaceNode* next_vsl = prev_vsl; jmasa@5007: while (next_vsl != NULL) { jmasa@5007: VirtualSpaceNode* vsl = next_vsl; jmasa@5007: next_vsl = vsl->next(); jmasa@5007: // Don't free the current virtual space since it will likely jmasa@5007: // be needed soon. jmasa@5007: if (vsl->container_count() == 0 && vsl != current_virtual_space()) { jmasa@5007: // Unlink it from the list jmasa@5007: if (prev_vsl == vsl) { stefank@5863: // This is the case of the current node being the first node. stefank@5863: assert(vsl == virtual_space_list(), "Expected to be the first node"); jmasa@5007: set_virtual_space_list(vsl->next()); jmasa@5007: } else { jmasa@5007: prev_vsl->set_next(vsl->next()); jmasa@5007: } jmasa@5007: stefank@5771: vsl->purge(chunk_manager); stefank@5704: dec_reserved_words(vsl->reserved_words()); stefank@5704: dec_committed_words(vsl->committed_words()); jmasa@5007: dec_virtual_space_count(); jmasa@5007: purged_vsl = vsl; jmasa@5007: delete vsl; jmasa@5007: } else { jmasa@5007: prev_vsl = vsl; jmasa@5007: } jmasa@5007: } jmasa@5007: #ifdef ASSERT jmasa@5007: if (purged_vsl != NULL) { coleenp@6678: // List should be stable enough to use an iterator here. coleenp@6678: VirtualSpaceListIterator iter(virtual_space_list()); jmasa@5007: while (iter.repeat()) { jmasa@5007: VirtualSpaceNode* vsl = iter.get_next(); jmasa@5007: assert(vsl != purged_vsl, "Purge of vsl failed"); jmasa@5007: } jmasa@5007: } jmasa@5007: #endif jmasa@5007: } jmasa@5007: coleenp@6678: coleenp@6678: // This function looks at the mmap regions in the metaspace without locking. coleenp@6678: // The chunks are added with store ordering and not deleted except for at coleenp@6678: // unloading time during a safepoint. coleenp@6678: bool VirtualSpaceList::contains(const void* ptr) { coleenp@6678: // List should be stable enough to use an iterator here because removing virtual coleenp@6678: // space nodes is only allowed at a safepoint. coleenp@6678: VirtualSpaceListIterator iter(virtual_space_list()); coleenp@6678: while (iter.repeat()) { coleenp@6678: VirtualSpaceNode* vsn = iter.get_next(); coleenp@6678: if (vsn->contains(ptr)) { coleenp@6678: return true; coleenp@6678: } coleenp@6678: } coleenp@6678: return false; coleenp@6678: } coleenp@6678: mgerdin@6004: void VirtualSpaceList::retire_current_virtual_space() { mgerdin@6004: assert_lock_strong(SpaceManager::expand_lock()); mgerdin@6004: mgerdin@6004: VirtualSpaceNode* vsn = current_virtual_space(); mgerdin@6004: mgerdin@6004: ChunkManager* cm = is_class() ? Metaspace::chunk_manager_class() : mgerdin@6004: Metaspace::chunk_manager_metadata(); mgerdin@6004: mgerdin@6004: vsn->retire(cm); mgerdin@6004: } mgerdin@6004: mgerdin@6004: void VirtualSpaceNode::retire(ChunkManager* chunk_manager) { mgerdin@6004: for (int i = (int)MediumIndex; i >= (int)ZeroIndex; --i) { mgerdin@6004: ChunkIndex index = (ChunkIndex)i; mgerdin@6004: size_t chunk_size = chunk_manager->free_chunks(index)->size(); mgerdin@6004: mgerdin@6004: while (free_words_in_vs() >= chunk_size) { mgerdin@6004: DEBUG_ONLY(verify_container_count();) mgerdin@6004: Metachunk* chunk = get_chunk_vs(chunk_size); mgerdin@6004: assert(chunk != NULL, "allocation should have been successful"); mgerdin@6004: mgerdin@6004: chunk_manager->return_chunks(index, chunk); mgerdin@6004: chunk_manager->inc_free_chunks_total(chunk_size); mgerdin@6004: DEBUG_ONLY(verify_container_count();) mgerdin@6004: } mgerdin@6004: } mgerdin@6004: assert(free_words_in_vs() == 0, "should be empty now"); mgerdin@6004: } mgerdin@6004: stefank@5863: VirtualSpaceList::VirtualSpaceList(size_t word_size) : coleenp@4037: _is_class(false), coleenp@4037: _virtual_space_list(NULL), coleenp@4037: _current_virtual_space(NULL), stefank@5704: _reserved_words(0), stefank@5704: _committed_words(0), coleenp@4037: _virtual_space_count(0) { coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); stefank@5863: create_new_virtual_space(word_size); coleenp@4037: } coleenp@4037: coleenp@4037: VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) : coleenp@4037: _is_class(true), coleenp@4037: _virtual_space_list(NULL), coleenp@4037: _current_virtual_space(NULL), stefank@5704: _reserved_words(0), stefank@5704: _committed_words(0), coleenp@4037: _virtual_space_count(0) { coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs); coleenp@4037: bool succeeded = class_entry->initialize(); stefank@5863: if (succeeded) { stefank@5863: link_vs(class_entry); stefank@5863: } coleenp@4037: } coleenp@4037: jmasa@5015: size_t VirtualSpaceList::free_bytes() { jmasa@5015: return virtual_space_list()->free_words_in_vs() * BytesPerWord; jmasa@5015: } jmasa@5015: coleenp@4037: // Allocate another meta virtual space and add it to the list. stefank@5863: bool VirtualSpaceList::create_new_virtual_space(size_t vs_word_size) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); stefank@5863: stefank@5863: if (is_class()) { stefank@5863: assert(false, "We currently don't support more than one VirtualSpace for" stefank@5863: " the compressed class space. The initialization of the" stefank@5863: " CCS uses another code path and should not hit this path."); coleenp@4037: return false; coleenp@4037: } stefank@5863: stefank@5863: if (vs_word_size == 0) { stefank@5863: assert(false, "vs_word_size should always be at least _reserve_alignment large."); stefank@5863: return false; stefank@5863: } stefank@5863: coleenp@4037: // Reserve the space coleenp@4037: size_t vs_byte_size = vs_word_size * BytesPerWord; stefank@5863: assert_is_size_aligned(vs_byte_size, Metaspace::reserve_alignment()); coleenp@4037: coleenp@4037: // Allocate the meta virtual space and initialize it. coleenp@4037: VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size); coleenp@4037: if (!new_entry->initialize()) { coleenp@4037: delete new_entry; coleenp@4037: return false; coleenp@4037: } else { stefank@5863: assert(new_entry->reserved_words() == vs_word_size, stefank@5863: "Reserved memory size differs from requested memory size"); coleenp@6678: // ensure lock-free iteration sees fully initialized node coleenp@6678: OrderAccess::storestore(); stefank@5704: link_vs(new_entry); coleenp@4037: return true; coleenp@4037: } coleenp@4037: } coleenp@4037: stefank@5704: void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry) { coleenp@4037: if (virtual_space_list() == NULL) { coleenp@4037: set_virtual_space_list(new_entry); coleenp@4037: } else { coleenp@4037: current_virtual_space()->set_next(new_entry); coleenp@4037: } coleenp@4037: set_current_virtual_space(new_entry); stefank@5704: inc_reserved_words(new_entry->reserved_words()); stefank@5704: inc_committed_words(new_entry->committed_words()); coleenp@4037: inc_virtual_space_count(); coleenp@4037: #ifdef ASSERT coleenp@4037: new_entry->mangle(); coleenp@4037: #endif coleenp@4037: if (TraceMetavirtualspaceAllocation && Verbose) { coleenp@4037: VirtualSpaceNode* vsl = current_virtual_space(); stefank@5708: vsl->print_on(gclog_or_tty); coleenp@4037: } coleenp@4037: } coleenp@4037: stefank@5863: bool VirtualSpaceList::expand_node_by(VirtualSpaceNode* node, stefank@5863: size_t min_words, stefank@5863: size_t preferred_words) { stefank@5704: size_t before = node->committed_words(); stefank@5704: stefank@5863: bool result = node->expand_by(min_words, preferred_words); stefank@5704: stefank@5704: size_t after = node->committed_words(); stefank@5704: stefank@5704: // after and before can be the same if the memory was pre-committed. stefank@5863: assert(after >= before, "Inconsistency"); stefank@5704: inc_committed_words(after - before); stefank@5704: stefank@5704: return result; stefank@5704: } stefank@5704: stefank@5863: bool VirtualSpaceList::expand_by(size_t min_words, size_t preferred_words) { stefank@5863: assert_is_size_aligned(min_words, Metaspace::commit_alignment_words()); stefank@5863: assert_is_size_aligned(preferred_words, Metaspace::commit_alignment_words()); stefank@5863: assert(min_words <= preferred_words, "Invalid arguments"); stefank@5863: stefank@5863: if (!MetaspaceGC::can_expand(min_words, this->is_class())) { stefank@5863: return false; stefank@5863: } stefank@5863: stefank@5863: size_t allowed_expansion_words = MetaspaceGC::allowed_expansion(); stefank@5863: if (allowed_expansion_words < min_words) { stefank@5863: return false; stefank@5863: } stefank@5863: stefank@5863: size_t max_expansion_words = MIN2(preferred_words, allowed_expansion_words); stefank@5863: stefank@5863: // Commit more memory from the the current virtual space. stefank@5863: bool vs_expanded = expand_node_by(current_virtual_space(), stefank@5863: min_words, stefank@5863: max_expansion_words); stefank@5863: if (vs_expanded) { stefank@5863: return true; stefank@5863: } mgerdin@6004: retire_current_virtual_space(); stefank@5863: stefank@5863: // Get another virtual space. stefank@5863: size_t grow_vs_words = MAX2((size_t)VirtualSpaceSize, preferred_words); stefank@5863: grow_vs_words = align_size_up(grow_vs_words, Metaspace::reserve_alignment_words()); stefank@5863: stefank@5863: if (create_new_virtual_space(grow_vs_words)) { stefank@5863: if (current_virtual_space()->is_pre_committed()) { stefank@5863: // The memory was pre-committed, so we are done here. stefank@5863: assert(min_words <= current_virtual_space()->committed_words(), stefank@5863: "The new VirtualSpace was pre-committed, so it" stefank@5863: "should be large enough to fit the alloc request."); stefank@5863: return true; stefank@5863: } stefank@5863: stefank@5863: return expand_node_by(current_virtual_space(), stefank@5863: min_words, stefank@5863: max_expansion_words); stefank@5863: } stefank@5863: stefank@5863: return false; stefank@5863: } stefank@5863: coleenp@4037: Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size, jmasa@4382: size_t grow_chunks_by_words, jmasa@4382: size_t medium_chunk_bunch) { coleenp@4037: stefank@5771: // Allocate a chunk out of the current virtual space. stefank@5771: Metachunk* next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words); coleenp@4037: stefank@5863: if (next != NULL) { stefank@5863: return next; coleenp@4037: } coleenp@4037: stefank@5863: // The expand amount is currently only determined by the requested sizes stefank@5863: // and not how much committed memory is left in the current virtual space. stefank@5863: stefank@5863: size_t min_word_size = align_size_up(grow_chunks_by_words, Metaspace::commit_alignment_words()); stefank@5863: size_t preferred_word_size = align_size_up(medium_chunk_bunch, Metaspace::commit_alignment_words()); stefank@5863: if (min_word_size >= preferred_word_size) { stefank@5863: // Can happen when humongous chunks are allocated. stefank@5863: preferred_word_size = min_word_size; stefank@5863: } stefank@5863: stefank@5863: bool expanded = expand_by(min_word_size, preferred_word_size); stefank@5863: if (expanded) { stefank@5863: next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words); stefank@5863: assert(next != NULL, "The allocation was expected to succeed after the expansion"); stefank@5863: } stefank@5863: stefank@5863: return next; jmasa@4382: } jmasa@4382: coleenp@4037: void VirtualSpaceList::print_on(outputStream* st) const { coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: VirtualSpaceListIterator iter(virtual_space_list()); coleenp@4037: while (iter.repeat()) { coleenp@4037: VirtualSpaceNode* node = iter.get_next(); coleenp@4037: node->print_on(st); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // MetaspaceGC methods coleenp@4037: coleenp@4037: // VM_CollectForMetadataAllocation is the vm operation used to GC. coleenp@4037: // Within the VM operation after the GC the attempt to allocate the metadata coleenp@4037: // should succeed. If the GC did not free enough space for the metaspace coleenp@4037: // allocation, the HWM is increased so that another virtualspace will be coleenp@4037: // allocated for the metadata. With perm gen the increase in the perm coleenp@4037: // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion. The coleenp@4037: // metaspace policy uses those as the small and large steps for the HWM. coleenp@4037: // coleenp@4037: // After the GC the compute_new_size() for MetaspaceGC is called to coleenp@4037: // resize the capacity of the metaspaces. The current implementation jmasa@5015: // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used coleenp@4037: // to resize the Java heap by some GC's. New flags can be implemented jmasa@5015: // if really needed. MinMetaspaceFreeRatio is used to calculate how much coleenp@4037: // free space is desirable in the metaspace capacity to decide how much jmasa@4581: // to increase the HWM. MaxMetaspaceFreeRatio is used to decide how much coleenp@4037: // free space is desirable in the metaspace capacity before decreasing coleenp@4037: // the HWM. coleenp@4037: coleenp@4037: // Calculate the amount to increase the high water mark (HWM). coleenp@4037: // Increase by a minimum amount (MinMetaspaceExpansion) so that coleenp@4037: // another expansion is not requested too soon. If that is not stefank@5863: // enough to satisfy the allocation, increase by MaxMetaspaceExpansion. stefank@5863: // If that is still not enough, expand by the size of the allocation stefank@5863: // plus some. stefank@5863: size_t MetaspaceGC::delta_capacity_until_GC(size_t bytes) { stefank@5863: size_t min_delta = MinMetaspaceExpansion; stefank@5863: size_t max_delta = MaxMetaspaceExpansion; stefank@5863: size_t delta = align_size_up(bytes, Metaspace::commit_alignment()); stefank@5863: stefank@5863: if (delta <= min_delta) { stefank@5863: delta = min_delta; stefank@5863: } else if (delta <= max_delta) { coleenp@4037: // Don't want to hit the high water mark on the next coleenp@4037: // allocation so make the delta greater than just enough coleenp@4037: // for this allocation. stefank@5863: delta = max_delta; stefank@5863: } else { stefank@5863: // This allocation is large but the next ones are probably not stefank@5863: // so increase by the minimum. stefank@5863: delta = delta + min_delta; coleenp@4037: } stefank@5863: stefank@5863: assert_is_size_aligned(delta, Metaspace::commit_alignment()); stefank@5863: stefank@5863: return delta; coleenp@4037: } coleenp@4037: stefank@5863: size_t MetaspaceGC::capacity_until_GC() { stefank@5863: size_t value = (size_t)OrderAccess::load_ptr_acquire(&_capacity_until_GC); stefank@5863: assert(value >= MetaspaceSize, "Not initialied properly?"); stefank@5863: return value; stefank@5863: } stefank@5863: ehelin@7254: bool MetaspaceGC::inc_capacity_until_GC(size_t v, size_t* new_cap_until_GC, size_t* old_cap_until_GC) { stefank@5863: assert_is_size_aligned(v, Metaspace::commit_alignment()); stefank@5863: ehelin@7254: size_t capacity_until_GC = (size_t) _capacity_until_GC; ehelin@7254: size_t new_value = capacity_until_GC + v; ehelin@7254: ehelin@7254: if (new_value < capacity_until_GC) { ehelin@7254: // The addition wrapped around, set new_value to aligned max value. ehelin@7254: new_value = align_size_down(max_uintx, Metaspace::commit_alignment()); ehelin@7254: } ehelin@7254: ehelin@7254: intptr_t expected = (intptr_t) capacity_until_GC; ehelin@7254: intptr_t actual = Atomic::cmpxchg_ptr((intptr_t) new_value, &_capacity_until_GC, expected); ehelin@7254: ehelin@7254: if (expected != actual) { ehelin@7254: return false; ehelin@7254: } ehelin@7254: ehelin@7254: if (new_cap_until_GC != NULL) { ehelin@7254: *new_cap_until_GC = new_value; ehelin@7254: } ehelin@7254: if (old_cap_until_GC != NULL) { ehelin@7254: *old_cap_until_GC = capacity_until_GC; ehelin@7254: } ehelin@7254: return true; stefank@5863: } stefank@5863: stefank@5863: size_t MetaspaceGC::dec_capacity_until_GC(size_t v) { stefank@5863: assert_is_size_aligned(v, Metaspace::commit_alignment()); stefank@5863: stefank@5863: return (size_t)Atomic::add_ptr(-(intptr_t)v, &_capacity_until_GC); stefank@5863: } stefank@5863: ehelin@6722: void MetaspaceGC::initialize() { ehelin@6722: // Set the high-water mark to MaxMetapaceSize during VM initializaton since ehelin@6722: // we can't do a GC during initialization. ehelin@6722: _capacity_until_GC = MaxMetaspaceSize; ehelin@6722: } ehelin@6722: ehelin@6722: void MetaspaceGC::post_initialize() { ehelin@6722: // Reset the high-water mark once the VM initialization is done. ehelin@6722: _capacity_until_GC = MAX2(MetaspaceAux::committed_bytes(), MetaspaceSize); ehelin@6722: } ehelin@6722: stefank@5863: bool MetaspaceGC::can_expand(size_t word_size, bool is_class) { stefank@5863: // Check if the compressed class space is full. stefank@5863: if (is_class && Metaspace::using_class_space()) { stefank@5863: size_t class_committed = MetaspaceAux::committed_bytes(Metaspace::ClassType); stefank@5863: if (class_committed + word_size * BytesPerWord > CompressedClassSpaceSize) { coleenp@5337: return false; coleenp@5337: } mgerdin@4790: } coleenp@4037: stefank@5863: // Check if the user has imposed a limit on the metaspace memory. stefank@5863: size_t committed_bytes = MetaspaceAux::committed_bytes(); stefank@5863: if (committed_bytes + word_size * BytesPerWord > MaxMetaspaceSize) { stefank@5863: return false; coleenp@4037: } coleenp@4037: stefank@5863: return true; stefank@5863: } stefank@5863: stefank@5863: size_t MetaspaceGC::allowed_expansion() { stefank@5863: size_t committed_bytes = MetaspaceAux::committed_bytes(); ehelin@6722: size_t capacity_until_gc = capacity_until_GC(); ehelin@6722: ehelin@6722: assert(capacity_until_gc >= committed_bytes, ehelin@6722: err_msg("capacity_until_gc: " SIZE_FORMAT " < committed_bytes: " SIZE_FORMAT, ehelin@6722: capacity_until_gc, committed_bytes)); stefank@5863: stefank@5863: size_t left_until_max = MaxMetaspaceSize - committed_bytes; stefank@5863: size_t left_until_GC = capacity_until_gc - committed_bytes; stefank@5863: size_t left_to_commit = MIN2(left_until_GC, left_until_max); stefank@5863: stefank@5863: return left_to_commit / BytesPerWord; coleenp@4037: } coleenp@4037: coleenp@4037: void MetaspaceGC::compute_new_size() { coleenp@4037: assert(_shrink_factor <= 100, "invalid shrink factor"); coleenp@4037: uint current_shrink_factor = _shrink_factor; coleenp@4037: _shrink_factor = 0; coleenp@4037: ehelin@6722: // Using committed_bytes() for used_after_gc is an overestimation, since the ehelin@6722: // chunk free lists are included in committed_bytes() and the memory in an ehelin@6722: // un-fragmented chunk free list is available for future allocations. ehelin@6722: // However, if the chunk free lists becomes fragmented, then the memory may ehelin@6722: // not be available for future allocations and the memory is therefore "in use". ehelin@6722: // Including the chunk free lists in the definition of "in use" is therefore ehelin@6722: // necessary. Not including the chunk free lists can cause capacity_until_GC to ehelin@6722: // shrink below committed_bytes() and this has caused serious bugs in the past. ehelin@6722: const size_t used_after_gc = MetaspaceAux::committed_bytes(); jmasa@5015: const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC(); coleenp@4037: jmasa@4581: const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0; coleenp@4037: const double maximum_used_percentage = 1.0 - minimum_free_percentage; coleenp@4037: coleenp@4037: const double min_tmp = used_after_gc / maximum_used_percentage; coleenp@4037: size_t minimum_desired_capacity = coleenp@4037: (size_t)MIN2(min_tmp, double(max_uintx)); coleenp@4037: // Don't shrink less than the initial generation size coleenp@4037: minimum_desired_capacity = MAX2(minimum_desired_capacity, coleenp@4037: MetaspaceSize); coleenp@4037: coleenp@4037: if (PrintGCDetails && Verbose) { coleenp@4037: gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: "); coleenp@4037: gclog_or_tty->print_cr(" " coleenp@4037: " minimum_free_percentage: %6.2f" coleenp@4037: " maximum_used_percentage: %6.2f", coleenp@4037: minimum_free_percentage, coleenp@4037: maximum_used_percentage); coleenp@4037: gclog_or_tty->print_cr(" " jmasa@5015: " used_after_gc : %6.1fKB", jmasa@5015: used_after_gc / (double) K); coleenp@4037: } coleenp@4037: coleenp@4037: jmasa@5015: size_t shrink_bytes = 0; coleenp@4037: if (capacity_until_GC < minimum_desired_capacity) { coleenp@4037: // If we have less capacity below the metaspace HWM, then coleenp@4037: // increment the HWM. coleenp@4037: size_t expand_bytes = minimum_desired_capacity - capacity_until_GC; stefank@5863: expand_bytes = align_size_up(expand_bytes, Metaspace::commit_alignment()); coleenp@4037: // Don't expand unless it's significant coleenp@4037: if (expand_bytes >= MinMetaspaceExpansion) { ehelin@7254: size_t new_capacity_until_GC = 0; ehelin@7254: bool succeeded = MetaspaceGC::inc_capacity_until_GC(expand_bytes, &new_capacity_until_GC); ehelin@7254: assert(succeeded, "Should always succesfully increment HWM when at safepoint"); ehelin@7254: ehelin@6417: Metaspace::tracer()->report_gc_threshold(capacity_until_GC, ehelin@6417: new_capacity_until_GC, ehelin@6417: MetaspaceGCThresholdUpdater::ComputeNewSize); ehelin@6417: if (PrintGCDetails && Verbose) { ehelin@6417: gclog_or_tty->print_cr(" expanding:" ehelin@6417: " minimum_desired_capacity: %6.1fKB" ehelin@6417: " expand_bytes: %6.1fKB" ehelin@6417: " MinMetaspaceExpansion: %6.1fKB" ehelin@6417: " new metaspace HWM: %6.1fKB", ehelin@6417: minimum_desired_capacity / (double) K, ehelin@6417: expand_bytes / (double) K, ehelin@6417: MinMetaspaceExpansion / (double) K, ehelin@6417: new_capacity_until_GC / (double) K); ehelin@6417: } coleenp@4037: } coleenp@4037: return; coleenp@4037: } coleenp@4037: coleenp@4037: // No expansion, now see if we want to shrink coleenp@4037: // We would never want to shrink more than this jmasa@5015: size_t max_shrink_bytes = capacity_until_GC - minimum_desired_capacity; jmasa@5015: assert(max_shrink_bytes >= 0, err_msg("max_shrink_bytes " SIZE_FORMAT, jmasa@5015: max_shrink_bytes)); coleenp@4037: coleenp@4037: // Should shrinking be considered? jmasa@4581: if (MaxMetaspaceFreeRatio < 100) { jmasa@4581: const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0; coleenp@4037: const double minimum_used_percentage = 1.0 - maximum_free_percentage; coleenp@4037: const double max_tmp = used_after_gc / minimum_used_percentage; coleenp@4037: size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); coleenp@4037: maximum_desired_capacity = MAX2(maximum_desired_capacity, coleenp@4037: MetaspaceSize); jmasa@5015: if (PrintGCDetails && Verbose) { coleenp@4037: gclog_or_tty->print_cr(" " coleenp@4037: " maximum_free_percentage: %6.2f" coleenp@4037: " minimum_used_percentage: %6.2f", coleenp@4037: maximum_free_percentage, coleenp@4037: minimum_used_percentage); coleenp@4037: gclog_or_tty->print_cr(" " jmasa@5015: " minimum_desired_capacity: %6.1fKB" jmasa@5015: " maximum_desired_capacity: %6.1fKB", coleenp@4037: minimum_desired_capacity / (double) K, coleenp@4037: maximum_desired_capacity / (double) K); coleenp@4037: } coleenp@4037: coleenp@4037: assert(minimum_desired_capacity <= maximum_desired_capacity, coleenp@4037: "sanity check"); coleenp@4037: coleenp@4037: if (capacity_until_GC > maximum_desired_capacity) { coleenp@4037: // Capacity too large, compute shrinking size jmasa@5015: shrink_bytes = capacity_until_GC - maximum_desired_capacity; coleenp@4037: // We don't want shrink all the way back to initSize if people call coleenp@4037: // System.gc(), because some programs do that between "phases" and then coleenp@4037: // we'd just have to grow the heap up again for the next phase. So we coleenp@4037: // damp the shrinking: 0% on the first call, 10% on the second call, 40% coleenp@4037: // on the third call, and 100% by the fourth call. But if we recompute coleenp@4037: // size without shrinking, it goes back to 0%. jmasa@5015: shrink_bytes = shrink_bytes / 100 * current_shrink_factor; stefank@5863: stefank@5863: shrink_bytes = align_size_down(shrink_bytes, Metaspace::commit_alignment()); stefank@5863: jmasa@5015: assert(shrink_bytes <= max_shrink_bytes, coleenp@4037: err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT, jmasa@5015: shrink_bytes, max_shrink_bytes)); coleenp@4037: if (current_shrink_factor == 0) { coleenp@4037: _shrink_factor = 10; coleenp@4037: } else { coleenp@4037: _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100); coleenp@4037: } coleenp@4037: if (PrintGCDetails && Verbose) { coleenp@4037: gclog_or_tty->print_cr(" " coleenp@4037: " shrinking:" coleenp@4037: " initSize: %.1fK" coleenp@4037: " maximum_desired_capacity: %.1fK", coleenp@4037: MetaspaceSize / (double) K, coleenp@4037: maximum_desired_capacity / (double) K); coleenp@4037: gclog_or_tty->print_cr(" " jmasa@5015: " shrink_bytes: %.1fK" coleenp@4037: " current_shrink_factor: %d" coleenp@4037: " new shrink factor: %d" coleenp@4037: " MinMetaspaceExpansion: %.1fK", jmasa@5015: shrink_bytes / (double) K, coleenp@4037: current_shrink_factor, coleenp@4037: _shrink_factor, coleenp@4037: MinMetaspaceExpansion / (double) K); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Don't shrink unless it's significant jmasa@5015: if (shrink_bytes >= MinMetaspaceExpansion && jmasa@5015: ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) { ehelin@6417: size_t new_capacity_until_GC = MetaspaceGC::dec_capacity_until_GC(shrink_bytes); ehelin@6417: Metaspace::tracer()->report_gc_threshold(capacity_until_GC, ehelin@6417: new_capacity_until_GC, ehelin@6417: MetaspaceGCThresholdUpdater::ComputeNewSize); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Metadebug methods coleenp@4037: coleenp@4037: void Metadebug::init_allocation_fail_alot_count() { coleenp@4037: if (MetadataAllocationFailALot) { coleenp@4037: _allocation_fail_alot_count = coleenp@4037: 1+(long)((double)MetadataAllocationFailALotInterval*os::random()/(max_jint+1.0)); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: #ifdef ASSERT coleenp@4037: bool Metadebug::test_metadata_failure() { coleenp@4037: if (MetadataAllocationFailALot && coleenp@4037: Threads::is_vm_complete()) { coleenp@4037: if (_allocation_fail_alot_count > 0) { coleenp@4037: _allocation_fail_alot_count--; coleenp@4037: } else { coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print_cr("Metadata allocation failing for " coleenp@4037: "MetadataAllocationFailALot"); coleenp@4037: } coleenp@4037: init_allocation_fail_alot_count(); coleenp@4037: return true; coleenp@4037: } coleenp@4037: } coleenp@4037: return false; coleenp@4037: } coleenp@4037: #endif coleenp@4037: coleenp@4037: // ChunkManager methods coleenp@4037: ehelin@5703: size_t ChunkManager::free_chunks_total_words() { coleenp@4037: return _free_chunks_total; coleenp@4037: } coleenp@4037: ehelin@5703: size_t ChunkManager::free_chunks_total_bytes() { ehelin@5703: return free_chunks_total_words() * BytesPerWord; coleenp@4037: } coleenp@4037: coleenp@4037: size_t ChunkManager::free_chunks_count() { coleenp@4037: #ifdef ASSERT coleenp@4037: if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) { coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: // This lock is only needed in debug because the verification coleenp@4037: // of the _free_chunks_totals walks the list of free chunks mgerdin@4264: slow_locked_verify_free_chunks_count(); coleenp@4037: } coleenp@4037: #endif mgerdin@4264: return _free_chunks_count; coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_verify_free_chunks_total() { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: assert(sum_free_chunks() == _free_chunks_total, coleenp@4037: err_msg("_free_chunks_total " SIZE_FORMAT " is not the" coleenp@4037: " same as sum " SIZE_FORMAT, _free_chunks_total, coleenp@4037: sum_free_chunks())); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::verify_free_chunks_total() { coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: locked_verify_free_chunks_total(); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_verify_free_chunks_count() { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: assert(sum_free_chunks_count() == _free_chunks_count, coleenp@4037: err_msg("_free_chunks_count " SIZE_FORMAT " is not the" coleenp@4037: " same as sum " SIZE_FORMAT, _free_chunks_count, coleenp@4037: sum_free_chunks_count())); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::verify_free_chunks_count() { coleenp@4037: #ifdef ASSERT coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: locked_verify_free_chunks_count(); coleenp@4037: #endif coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::verify() { mgerdin@4264: MutexLockerEx cl(SpaceManager::expand_lock(), mgerdin@4264: Mutex::_no_safepoint_check_flag); mgerdin@4264: locked_verify(); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_verify() { jmasa@4196: locked_verify_free_chunks_count(); coleenp@4037: locked_verify_free_chunks_total(); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_print_free_chunks(outputStream* st) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); jmasa@4382: st->print_cr("Free chunk total " SIZE_FORMAT " count " SIZE_FORMAT, coleenp@4037: _free_chunks_total, _free_chunks_count); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_print_sum_free_chunks(outputStream* st) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); jmasa@4382: st->print_cr("Sum free chunk total " SIZE_FORMAT " count " SIZE_FORMAT, coleenp@4037: sum_free_chunks(), sum_free_chunks_count()); coleenp@4037: } coleenp@4037: ChunkList* ChunkManager::free_chunks(ChunkIndex index) { coleenp@4037: return &_free_chunks[index]; coleenp@4037: } coleenp@4037: coleenp@4037: // These methods that sum the free chunk lists are used in printing coleenp@4037: // methods that are used in product builds. coleenp@4037: size_t ChunkManager::sum_free_chunks() { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: size_t result = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) { coleenp@4037: ChunkList* list = free_chunks(i); coleenp@4037: coleenp@4037: if (list == NULL) { coleenp@4037: continue; coleenp@4037: } coleenp@4037: jmasa@4932: result = result + list->count() * list->size(); coleenp@4037: } jmasa@4196: result = result + humongous_dictionary()->total_size(); coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: size_t ChunkManager::sum_free_chunks_count() { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: size_t count = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) { coleenp@4037: ChunkList* list = free_chunks(i); coleenp@4037: if (list == NULL) { coleenp@4037: continue; coleenp@4037: } jmasa@4932: count = count + list->count(); coleenp@4037: } jmasa@4196: count = count + humongous_dictionary()->total_free_blocks(); coleenp@4037: return count; coleenp@4037: } coleenp@4037: coleenp@4037: ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) { jmasa@4382: ChunkIndex index = list_index(word_size); jmasa@4382: assert(index < HumongousIndex, "No humongous list"); jmasa@4382: return free_chunks(index); coleenp@4037: } coleenp@4037: coleenp@4037: Metachunk* ChunkManager::free_chunks_get(size_t word_size) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: mgerdin@4264: slow_locked_verify(); jmasa@4196: jmasa@4196: Metachunk* chunk = NULL; jmasa@4382: if (list_index(word_size) != HumongousIndex) { jmasa@4196: ChunkList* free_list = find_free_chunks_list(word_size); jmasa@4196: assert(free_list != NULL, "Sanity check"); jmasa@4196: jmasa@4196: chunk = free_list->head(); jmasa@4196: jmasa@4196: if (chunk == NULL) { jmasa@4196: return NULL; jmasa@4196: } jmasa@4196: coleenp@4037: // Remove the chunk as the head of the list. jmasa@4932: free_list->remove_chunk(chunk); jmasa@4382: coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { stefank@5708: gclog_or_tty->print_cr("ChunkManager::free_chunks_get: free_list " stefank@5708: PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT, stefank@5708: free_list, chunk, chunk->word_size()); coleenp@4037: } coleenp@4037: } else { jmasa@4196: chunk = humongous_dictionary()->get_chunk( jmasa@4196: word_size, jmasa@4196: FreeBlockDictionary::atLeast); jmasa@4196: stefank@5863: if (chunk == NULL) { jmasa@4382: return NULL; coleenp@4037: } stefank@5863: stefank@5863: if (TraceMetadataHumongousAllocation) { stefank@5863: size_t waste = chunk->word_size() - word_size; stefank@5863: gclog_or_tty->print_cr("Free list allocate humongous chunk size " stefank@5863: SIZE_FORMAT " for requested size " SIZE_FORMAT stefank@5863: " waste " SIZE_FORMAT, stefank@5863: chunk->word_size(), word_size, waste); stefank@5863: } coleenp@4037: } jmasa@4382: stefank@5863: // Chunk is being removed from the chunks free list. stefank@5941: dec_free_chunks_total(chunk->word_size()); stefank@5863: jmasa@4382: // Remove it from the links to this freelist jmasa@4382: chunk->set_next(NULL); jmasa@4382: chunk->set_prev(NULL); jmasa@5007: #ifdef ASSERT jmasa@5007: // Chunk is no longer on any freelist. Setting to false make container_count_slow() jmasa@5007: // work. stefank@5941: chunk->set_is_tagged_free(false); jmasa@5007: #endif stefank@5771: chunk->container()->inc_container_count(); stefank@5771: mgerdin@4264: slow_locked_verify(); coleenp@4037: return chunk; coleenp@4037: } coleenp@4037: coleenp@4037: Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); mgerdin@4264: slow_locked_verify(); coleenp@4037: coleenp@4037: // Take from the beginning of the list coleenp@4037: Metachunk* chunk = free_chunks_get(word_size); coleenp@4037: if (chunk == NULL) { coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: jmasa@4382: assert((word_size <= chunk->word_size()) || jmasa@4382: list_index(chunk->word_size() == HumongousIndex), jmasa@4382: "Non-humongous variable sized chunk"); coleenp@4037: if (TraceMetadataChunkAllocation) { jmasa@4382: size_t list_count; jmasa@4382: if (list_index(word_size) < HumongousIndex) { jmasa@4382: ChunkList* list = find_free_chunks_list(word_size); jmasa@4932: list_count = list->count(); jmasa@4382: } else { jmasa@4382: list_count = humongous_dictionary()->total_count(); jmasa@4382: } stefank@5708: gclog_or_tty->print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk " stefank@5708: PTR_FORMAT " size " SIZE_FORMAT " count " SIZE_FORMAT " ", stefank@5708: this, chunk, chunk->word_size(), list_count); stefank@5708: locked_print_free_chunks(gclog_or_tty); coleenp@4037: } coleenp@4037: coleenp@4037: return chunk; coleenp@4037: } coleenp@4037: stefank@5771: void ChunkManager::print_on(outputStream* out) const { jmasa@4196: if (PrintFLSStatistics != 0) { stefank@5771: const_cast(this)->humongous_dictionary()->report_statistics(); jmasa@4196: } jmasa@4196: } jmasa@4196: coleenp@4037: // SpaceManager methods coleenp@4037: jmasa@4382: void SpaceManager::get_initial_chunk_sizes(Metaspace::MetaspaceType type, jmasa@4382: size_t* chunk_word_size, jmasa@4382: size_t* class_chunk_word_size) { jmasa@4382: switch (type) { jmasa@4382: case Metaspace::BootMetaspaceType: jmasa@4382: *chunk_word_size = Metaspace::first_chunk_word_size(); jmasa@4382: *class_chunk_word_size = Metaspace::first_class_chunk_word_size(); jmasa@4382: break; jmasa@4382: case Metaspace::ROMetaspaceType: jmasa@4382: *chunk_word_size = SharedReadOnlySize / wordSize; jmasa@4382: *class_chunk_word_size = ClassSpecializedChunk; jmasa@4382: break; jmasa@4382: case Metaspace::ReadWriteMetaspaceType: jmasa@4382: *chunk_word_size = SharedReadWriteSize / wordSize; jmasa@4382: *class_chunk_word_size = ClassSpecializedChunk; jmasa@4382: break; jmasa@4382: case Metaspace::AnonymousMetaspaceType: jmasa@4382: case Metaspace::ReflectionMetaspaceType: jmasa@4382: *chunk_word_size = SpecializedChunk; jmasa@4382: *class_chunk_word_size = ClassSpecializedChunk; jmasa@4382: break; jmasa@4382: default: jmasa@4382: *chunk_word_size = SmallChunk; jmasa@4382: *class_chunk_word_size = ClassSmallChunk; jmasa@4382: break; jmasa@4382: } mikael@4548: assert(*chunk_word_size != 0 && *class_chunk_word_size != 0, jmasa@4382: err_msg("Initial chunks sizes bad: data " SIZE_FORMAT jmasa@4382: " class " SIZE_FORMAT, mikael@4548: *chunk_word_size, *class_chunk_word_size)); jmasa@4382: } jmasa@4382: coleenp@4037: size_t SpaceManager::sum_free_in_chunks_in_use() const { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: size_t free = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: Metachunk* chunk = chunks_in_use(i); coleenp@4037: while (chunk != NULL) { coleenp@4037: free += chunk->free_word_size(); coleenp@4037: chunk = chunk->next(); coleenp@4037: } coleenp@4037: } coleenp@4037: return free; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_waste_in_chunks_in_use() const { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: size_t result = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: result += sum_waste_in_chunks_in_use(i); coleenp@4037: } jmasa@4196: coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_waste_in_chunks_in_use(ChunkIndex index) const { coleenp@4037: size_t result = 0; coleenp@4037: Metachunk* chunk = chunks_in_use(index); coleenp@4037: // Count the free space in all the chunk but not the coleenp@4037: // current chunk from which allocations are still being done. coleenp@5337: while (chunk != NULL) { coleenp@5337: if (chunk != current_chunk()) { jmasa@4196: result += chunk->free_word_size(); coleenp@4037: } coleenp@5337: chunk = chunk->next(); coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_capacity_in_chunks_in_use() const { jmasa@5015: // For CMS use "allocated_chunks_words()" which does not need the jmasa@5015: // Metaspace lock. For the other collectors sum over the jmasa@5015: // lists. Use both methods as a check that "allocated_chunks_words()" jmasa@5015: // is correct. That is, sum_capacity_in_chunks() is too expensive jmasa@5015: // to use in the product and allocated_chunks_words() should be used jmasa@5015: // but allow for checking that allocated_chunks_words() returns the same jmasa@5015: // value as sum_capacity_in_chunks_in_use() which is the definitive jmasa@5015: // answer. jmasa@5015: if (UseConcMarkSweepGC) { jmasa@5015: return allocated_chunks_words(); jmasa@5015: } else { jmasa@5015: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); jmasa@5015: size_t sum = 0; jmasa@5015: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { jmasa@5015: Metachunk* chunk = chunks_in_use(i); jmasa@5015: while (chunk != NULL) { stefank@5941: sum += chunk->word_size(); jmasa@5015: chunk = chunk->next(); jmasa@5015: } coleenp@4037: } jmasa@5015: return sum; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_count_in_chunks_in_use() { coleenp@4037: size_t count = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: count = count + sum_count_in_chunks_in_use(i); coleenp@4037: } jmasa@4196: coleenp@4037: return count; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_count_in_chunks_in_use(ChunkIndex i) { coleenp@4037: size_t count = 0; coleenp@4037: Metachunk* chunk = chunks_in_use(i); coleenp@4037: while (chunk != NULL) { coleenp@4037: count++; coleenp@4037: chunk = chunk->next(); coleenp@4037: } coleenp@4037: return count; coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: size_t SpaceManager::sum_used_in_chunks_in_use() const { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: size_t used = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: Metachunk* chunk = chunks_in_use(i); coleenp@4037: while (chunk != NULL) { coleenp@4037: used += chunk->used_word_size(); coleenp@4037: chunk = chunk->next(); coleenp@4037: } coleenp@4037: } coleenp@4037: return used; coleenp@4037: } coleenp@4037: coleenp@4037: void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const { coleenp@4037: jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { jmasa@4382: Metachunk* chunk = chunks_in_use(i); jmasa@4382: st->print("SpaceManager: %s " PTR_FORMAT, jmasa@4382: chunk_size_name(i), chunk); jmasa@4382: if (chunk != NULL) { jmasa@4382: st->print_cr(" free " SIZE_FORMAT, jmasa@4382: chunk->free_word_size()); jmasa@4382: } else { drchase@6680: st->cr(); jmasa@4382: } jmasa@4382: } coleenp@4037: stefank@5771: chunk_manager()->locked_print_free_chunks(st); stefank@5771: chunk_manager()->locked_print_sum_free_chunks(st); coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::calc_chunk_size(size_t word_size) { coleenp@4037: coleenp@4037: // Decide between a small chunk and a medium chunk. Up to jwilhelm@7867: // _small_chunk_limit small chunks can be allocated but jwilhelm@7867: // once a medium chunk has been allocated, no more small jwilhelm@7867: // chunks will be allocated. coleenp@4037: size_t chunk_word_size; coleenp@4037: if (chunks_in_use(MediumIndex) == NULL && coleenp@5337: sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit) { jmasa@4382: chunk_word_size = (size_t) small_chunk_size(); jmasa@4382: if (word_size + Metachunk::overhead() > small_chunk_size()) { jmasa@4382: chunk_word_size = medium_chunk_size(); coleenp@4037: } coleenp@4037: } else { jmasa@4382: chunk_word_size = medium_chunk_size(); coleenp@4037: } coleenp@4037: mgerdin@6004: // Might still need a humongous chunk. Enforce mgerdin@6004: // humongous allocations sizes to be aligned up to mgerdin@6004: // the smallest chunk size. jmasa@4382: size_t if_humongous_sized_chunk = jmasa@4382: align_size_up(word_size + Metachunk::overhead(), mgerdin@6004: smallest_chunk_size()); coleenp@4037: chunk_word_size = jmasa@4382: MAX2((size_t) chunk_word_size, if_humongous_sized_chunk); jmasa@4382: jmasa@4382: assert(!SpaceManager::is_humongous(word_size) || jmasa@4382: chunk_word_size == if_humongous_sized_chunk, jmasa@4382: err_msg("Size calculation is wrong, word_size " SIZE_FORMAT jmasa@4382: " chunk_word_size " SIZE_FORMAT, jmasa@4382: word_size, chunk_word_size)); coleenp@4037: if (TraceMetadataHumongousAllocation && coleenp@4037: SpaceManager::is_humongous(word_size)) { coleenp@4037: gclog_or_tty->print_cr("Metadata humongous allocation:"); coleenp@4037: gclog_or_tty->print_cr(" word_size " PTR_FORMAT, word_size); coleenp@4037: gclog_or_tty->print_cr(" chunk_word_size " PTR_FORMAT, coleenp@4037: chunk_word_size); jmasa@4196: gclog_or_tty->print_cr(" chunk overhead " PTR_FORMAT, coleenp@4037: Metachunk::overhead()); coleenp@4037: } coleenp@4037: return chunk_word_size; coleenp@4037: } coleenp@4037: stefank@5864: void SpaceManager::track_metaspace_memory_usage() { stefank@5864: if (is_init_completed()) { stefank@5864: if (is_class()) { stefank@5864: MemoryService::track_compressed_class_memory_usage(); stefank@5864: } stefank@5864: MemoryService::track_metaspace_memory_usage(); stefank@5864: } stefank@5864: } stefank@5864: jmasa@4196: MetaWord* SpaceManager::grow_and_allocate(size_t word_size) { coleenp@4037: assert(vs_list()->current_virtual_space() != NULL, coleenp@4037: "Should have been set"); coleenp@4037: assert(current_chunk() == NULL || coleenp@4037: current_chunk()->allocate(word_size) == NULL, coleenp@4037: "Don't need to expand"); coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: size_t words_left = 0; jmasa@4382: size_t words_used = 0; jmasa@4382: if (current_chunk() != NULL) { jmasa@4382: words_left = current_chunk()->free_word_size(); jmasa@4382: words_used = current_chunk()->used_word_size(); jmasa@4382: } coleenp@4037: gclog_or_tty->print_cr("SpaceManager::grow_and_allocate for " SIZE_FORMAT jmasa@4382: " words " SIZE_FORMAT " words used " SIZE_FORMAT jmasa@4382: " words left", jmasa@4382: word_size, words_used, words_left); coleenp@4037: } coleenp@4037: jwilhelm@7867: // Get another chunk out of the virtual space coleenp@4037: size_t grow_chunks_by_words = calc_chunk_size(word_size); jmasa@4382: Metachunk* next = get_new_chunk(word_size, grow_chunks_by_words); coleenp@4037: stefank@5863: MetaWord* mem = NULL; stefank@5863: coleenp@4037: // If a chunk was available, add it to the in-use chunk list coleenp@4037: // and do an allocation from it. coleenp@4037: if (next != NULL) { coleenp@4037: // Add to this manager's list of chunks in use. coleenp@4037: add_chunk(next, false); stefank@5863: mem = next->allocate(word_size); coleenp@4037: } stefank@5863: stefank@5864: // Track metaspace memory usage statistic. stefank@5864: track_metaspace_memory_usage(); stefank@5864: stefank@5863: return mem; coleenp@4037: } coleenp@4037: coleenp@4037: void SpaceManager::print_on(outputStream* st) const { coleenp@4037: jmasa@4382: for (ChunkIndex i = ZeroIndex; jmasa@4196: i < NumberOfInUseLists ; coleenp@4037: i = next_chunk_index(i) ) { coleenp@4037: st->print_cr(" chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT, coleenp@4037: chunks_in_use(i), coleenp@4037: chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size()); coleenp@4037: } coleenp@4037: st->print_cr(" waste: Small " SIZE_FORMAT " Medium " SIZE_FORMAT coleenp@4037: " Humongous " SIZE_FORMAT, coleenp@4037: sum_waste_in_chunks_in_use(SmallIndex), coleenp@4037: sum_waste_in_chunks_in_use(MediumIndex), coleenp@4037: sum_waste_in_chunks_in_use(HumongousIndex)); jmasa@4196: // block free lists jmasa@4196: if (block_freelists() != NULL) { jmasa@4196: st->print_cr("total in block free lists " SIZE_FORMAT, jmasa@4196: block_freelists()->total_size()); jmasa@4196: } coleenp@4037: } coleenp@4037: jmasa@5162: SpaceManager::SpaceManager(Metaspace::MetadataType mdtype, stefank@5771: Mutex* lock) : jmasa@5162: _mdtype(mdtype), jmasa@5015: _allocated_blocks_words(0), jmasa@5015: _allocated_chunks_words(0), jmasa@5015: _allocated_chunks_count(0), jmasa@4382: _lock(lock) jmasa@4382: { jmasa@4382: initialize(); jmasa@4382: } jmasa@4382: jmasa@5015: void SpaceManager::inc_size_metrics(size_t words) { jmasa@5015: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5015: // Total of allocated Metachunks and allocated Metachunks count jmasa@5015: // for each SpaceManager jmasa@5015: _allocated_chunks_words = _allocated_chunks_words + words; jmasa@5015: _allocated_chunks_count++; jmasa@5015: // Global total of capacity in allocated Metachunks jmasa@5162: MetaspaceAux::inc_capacity(mdtype(), words); jmasa@5015: // Global total of allocated Metablocks. jmasa@5015: // used_words_slow() includes the overhead in each jmasa@5015: // Metachunk so include it in the used when the jmasa@5015: // Metachunk is first added (so only added once per jmasa@5015: // Metachunk). jmasa@5162: MetaspaceAux::inc_used(mdtype(), Metachunk::overhead()); jmasa@5015: } jmasa@5015: jmasa@5015: void SpaceManager::inc_used_metrics(size_t words) { jmasa@5015: // Add to the per SpaceManager total jmasa@5015: Atomic::add_ptr(words, &_allocated_blocks_words); jmasa@5015: // Add to the global total jmasa@5162: MetaspaceAux::inc_used(mdtype(), words); jmasa@5015: } jmasa@5015: jmasa@5015: void SpaceManager::dec_total_from_size_metrics() { jmasa@5162: MetaspaceAux::dec_capacity(mdtype(), allocated_chunks_words()); jmasa@5162: MetaspaceAux::dec_used(mdtype(), allocated_blocks_words()); jmasa@5015: // Also deduct the overhead per Metachunk jmasa@5162: MetaspaceAux::dec_used(mdtype(), allocated_chunks_count() * Metachunk::overhead()); jmasa@5015: } jmasa@5015: jmasa@4382: void SpaceManager::initialize() { coleenp@4037: Metadebug::init_allocation_fail_alot_count(); jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: _chunks_in_use[i] = NULL; coleenp@4037: } coleenp@4037: _current_chunk = NULL; coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print_cr("SpaceManager(): " PTR_FORMAT, this); coleenp@4037: } coleenp@4037: } coleenp@4037: jmasa@4932: void ChunkManager::return_chunks(ChunkIndex index, Metachunk* chunks) { jmasa@4932: if (chunks == NULL) { jmasa@4932: return; jmasa@4932: } jmasa@4932: ChunkList* list = free_chunks(index); jmasa@4932: assert(list->size() == chunks->word_size(), "Mismatch in chunk sizes"); jmasa@4932: assert_lock_strong(SpaceManager::expand_lock()); jmasa@4932: Metachunk* cur = chunks; jmasa@4932: jmasa@5007: // This returns chunks one at a time. If a new jmasa@4932: // class List can be created that is a base class jmasa@4932: // of FreeList then something like FreeList::prepend() jmasa@4932: // can be used in place of this loop jmasa@4932: while (cur != NULL) { jmasa@5007: assert(cur->container() != NULL, "Container should have been set"); jmasa@5007: cur->container()->dec_container_count(); jmasa@4932: // Capture the next link before it is changed jmasa@4932: // by the call to return_chunk_at_head(); jmasa@4932: Metachunk* next = cur->next(); stefank@5941: DEBUG_ONLY(cur->set_is_tagged_free(true);) jmasa@4932: list->return_chunk_at_head(cur); jmasa@4932: cur = next; jmasa@4932: } jmasa@4932: } jmasa@4932: coleenp@4037: SpaceManager::~SpaceManager() { mgerdin@4784: // This call this->_lock which can't be done while holding expand_lock() jmasa@5015: assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(), jmasa@5015: err_msg("sum_capacity_in_chunks_in_use() " SIZE_FORMAT jmasa@5015: " allocated_chunks_words() " SIZE_FORMAT, jmasa@5015: sum_capacity_in_chunks_in_use(), allocated_chunks_words())); mgerdin@4784: coleenp@4037: MutexLockerEx fcl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: stefank@5771: chunk_manager()->slow_locked_verify(); coleenp@4037: jmasa@5015: dec_total_from_size_metrics(); jmasa@5015: coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this); coleenp@4037: locked_print_chunks_in_use_on(gclog_or_tty); coleenp@4037: } coleenp@4037: jmasa@5007: // Do not mangle freed Metachunks. The chunk size inside Metachunks jmasa@5007: // is during the freeing of a VirtualSpaceNodes. coleenp@4304: coleenp@4037: // Have to update before the chunks_in_use lists are emptied coleenp@4037: // below. stefank@5771: chunk_manager()->inc_free_chunks_total(allocated_chunks_words(), stefank@5771: sum_count_in_chunks_in_use()); coleenp@4037: coleenp@4037: // Add all the chunks in use by this space manager coleenp@4037: // to the global list of free chunks. coleenp@4037: jmasa@4382: // Follow each list of chunks-in-use and add them to the jmasa@4382: // free lists. Each list is NULL terminated. jmasa@4382: jmasa@4382: for (ChunkIndex i = ZeroIndex; i < HumongousIndex; i = next_chunk_index(i)) { jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: gclog_or_tty->print_cr("returned %d %s chunks to freelist", jmasa@4382: sum_count_in_chunks_in_use(i), jmasa@4382: chunk_size_name(i)); jmasa@4382: } jmasa@4382: Metachunk* chunks = chunks_in_use(i); stefank@5771: chunk_manager()->return_chunks(i, chunks); jmasa@4382: set_chunks_in_use(i, NULL); jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: gclog_or_tty->print_cr("updated freelist count %d %s", stefank@5771: chunk_manager()->free_chunks(i)->count(), jmasa@4382: chunk_size_name(i)); jmasa@4382: } jmasa@4382: assert(i != HumongousIndex, "Humongous chunks are handled explicitly later"); coleenp@4037: } coleenp@4037: jmasa@4382: // The medium chunk case may be optimized by passing the head and jmasa@4382: // tail of the medium chunk list to add_at_head(). The tail is often jmasa@4382: // the current chunk but there are probably exceptions. jmasa@4382: coleenp@4037: // Humongous chunks jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: gclog_or_tty->print_cr("returned %d %s humongous chunks to dictionary", jmasa@4382: sum_count_in_chunks_in_use(HumongousIndex), jmasa@4382: chunk_size_name(HumongousIndex)); jmasa@4382: gclog_or_tty->print("Humongous chunk dictionary: "); jmasa@4382: } coleenp@4037: // Humongous chunks are never the current chunk. coleenp@4037: Metachunk* humongous_chunks = chunks_in_use(HumongousIndex); coleenp@4037: jmasa@4196: while (humongous_chunks != NULL) { jmasa@4196: #ifdef ASSERT stefank@5941: humongous_chunks->set_is_tagged_free(true); jmasa@4196: #endif jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: gclog_or_tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ", jmasa@4382: humongous_chunks, jmasa@4382: humongous_chunks->word_size()); jmasa@4382: } jmasa@4382: assert(humongous_chunks->word_size() == (size_t) jmasa@4382: align_size_up(humongous_chunks->word_size(), mgerdin@6004: smallest_chunk_size()), jmasa@4382: err_msg("Humongous chunk size is wrong: word size " SIZE_FORMAT mikael@4548: " granularity %d", mgerdin@6004: humongous_chunks->word_size(), smallest_chunk_size())); jmasa@4196: Metachunk* next_humongous_chunks = humongous_chunks->next(); jmasa@5007: humongous_chunks->container()->dec_container_count(); stefank@5771: chunk_manager()->humongous_dictionary()->return_chunk(humongous_chunks); jmasa@4196: humongous_chunks = next_humongous_chunks; coleenp@4037: } jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { drchase@6680: gclog_or_tty->cr(); jmasa@4382: gclog_or_tty->print_cr("updated dictionary count %d %s", stefank@5771: chunk_manager()->humongous_dictionary()->total_count(), jmasa@4382: chunk_size_name(HumongousIndex)); jmasa@4382: } stefank@5771: chunk_manager()->slow_locked_verify(); coleenp@4037: } coleenp@4037: jmasa@4382: const char* SpaceManager::chunk_size_name(ChunkIndex index) const { jmasa@4382: switch (index) { jmasa@4382: case SpecializedIndex: jmasa@4382: return "Specialized"; jmasa@4382: case SmallIndex: jmasa@4382: return "Small"; jmasa@4382: case MediumIndex: jmasa@4382: return "Medium"; jmasa@4382: case HumongousIndex: jmasa@4382: return "Humongous"; jmasa@4382: default: jmasa@4382: return NULL; jmasa@4382: } jmasa@4382: } jmasa@4382: jmasa@4382: ChunkIndex ChunkManager::list_index(size_t size) { jmasa@4382: switch (size) { jmasa@4382: case SpecializedChunk: jmasa@4382: assert(SpecializedChunk == ClassSpecializedChunk, jmasa@4382: "Need branch for ClassSpecializedChunk"); jmasa@4382: return SpecializedIndex; jmasa@4382: case SmallChunk: jmasa@4382: case ClassSmallChunk: jmasa@4382: return SmallIndex; jmasa@4382: case MediumChunk: jmasa@4382: case ClassMediumChunk: jmasa@4382: return MediumIndex; jmasa@4382: default: jmasa@4383: assert(size > MediumChunk || size > ClassMediumChunk, jmasa@4382: "Not a humongous chunk"); jmasa@4382: return HumongousIndex; jmasa@4382: } jmasa@4382: } jmasa@4382: jmasa@4196: void SpaceManager::deallocate(MetaWord* p, size_t word_size) { coleenp@4037: assert_lock_strong(_lock); fparain@5452: size_t raw_word_size = get_raw_word_size(word_size); goetz@6337: size_t min_size = TreeChunk >::min_size(); fparain@5452: assert(raw_word_size >= min_size, hseigel@5528: err_msg("Should not deallocate dark matter " SIZE_FORMAT "<" SIZE_FORMAT, word_size, min_size)); fparain@5452: block_freelists()->return_block(p, raw_word_size); coleenp@4037: } coleenp@4037: coleenp@4037: // Adds a chunk to the list of chunks in use. coleenp@4037: void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) { coleenp@4037: coleenp@4037: assert(new_chunk != NULL, "Should not be NULL"); coleenp@4037: assert(new_chunk->next() == NULL, "Should not be on a list"); coleenp@4037: coleenp@4037: new_chunk->reset_empty(); coleenp@4037: coleenp@4037: // Find the correct list and and set the current coleenp@4037: // chunk for that list. jmasa@4382: ChunkIndex index = ChunkManager::list_index(new_chunk->word_size()); jmasa@4382: jmasa@4382: if (index != HumongousIndex) { mgerdin@5699: retire_current_chunk(); coleenp@4037: set_current_chunk(new_chunk); jmasa@4382: new_chunk->set_next(chunks_in_use(index)); jmasa@4382: set_chunks_in_use(index, new_chunk); jmasa@4382: } else { coleenp@4037: // For null class loader data and DumpSharedSpaces, the first chunk isn't coleenp@4037: // small, so small will be null. Link this first chunk as the current coleenp@4037: // chunk. coleenp@4037: if (make_current) { coleenp@4037: // Set as the current chunk but otherwise treat as a humongous chunk. coleenp@4037: set_current_chunk(new_chunk); coleenp@4037: } coleenp@4037: // Link at head. The _current_chunk only points to a humongous chunk for coleenp@4037: // the null class loader metaspace (class and data virtual space managers) coleenp@4037: // any humongous chunks so will not point to the tail coleenp@4037: // of the humongous chunks list. coleenp@4037: new_chunk->set_next(chunks_in_use(HumongousIndex)); coleenp@4037: set_chunks_in_use(HumongousIndex, new_chunk); coleenp@4037: jmasa@4383: assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency"); coleenp@4037: } coleenp@4037: jmasa@5015: // Add to the running sum of capacity jmasa@5015: inc_size_metrics(new_chunk->word_size()); jmasa@5015: coleenp@4037: assert(new_chunk->is_empty(), "Not ready for reuse"); coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print("SpaceManager::add_chunk: %d) ", coleenp@4037: sum_count_in_chunks_in_use()); coleenp@4037: new_chunk->print_on(gclog_or_tty); stefank@5771: chunk_manager()->locked_print_free_chunks(gclog_or_tty); coleenp@4037: } coleenp@4037: } coleenp@4037: mgerdin@5699: void SpaceManager::retire_current_chunk() { mgerdin@5699: if (current_chunk() != NULL) { mgerdin@5699: size_t remaining_words = current_chunk()->free_word_size(); goetz@6337: if (remaining_words >= TreeChunk >::min_size()) { mgerdin@5699: block_freelists()->return_block(current_chunk()->allocate(remaining_words), remaining_words); mgerdin@5699: inc_used_metrics(remaining_words); mgerdin@5699: } mgerdin@5699: } mgerdin@5699: } mgerdin@5699: jmasa@4382: Metachunk* SpaceManager::get_new_chunk(size_t word_size, jmasa@4382: size_t grow_chunks_by_words) { stefank@5771: // Get a chunk from the chunk freelist stefank@5771: Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words); stefank@5771: stefank@5771: if (next == NULL) { stefank@5771: next = vs_list()->get_new_chunk(word_size, stefank@5771: grow_chunks_by_words, stefank@5771: medium_chunk_bunch()); stefank@5771: } jmasa@4382: stefank@5707: if (TraceMetadataHumongousAllocation && next != NULL && jmasa@4382: SpaceManager::is_humongous(next->word_size())) { stefank@5707: gclog_or_tty->print_cr(" new humongous chunk word size " stefank@5707: PTR_FORMAT, next->word_size()); jmasa@4382: } jmasa@4382: jmasa@4382: return next; jmasa@4382: } jmasa@4382: coleenp@4037: MetaWord* SpaceManager::allocate(size_t word_size) { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: iklam@5208: size_t raw_word_size = get_raw_word_size(word_size); coleenp@4037: BlockFreelist* fl = block_freelists(); jmasa@4196: MetaWord* p = NULL; coleenp@4037: // Allocation from the dictionary is expensive in the sense that coleenp@4037: // the dictionary has to be searched for a size. Don't allocate coleenp@4037: // from the dictionary until it starts to get fat. Is this coleenp@4037: // a reasonable policy? Maybe an skinny dictionary is fast enough coleenp@4037: // for allocations. Do some profiling. JJJ jmasa@4196: if (fl->total_size() > allocation_from_dictionary_limit) { jmasa@4196: p = fl->get_block(raw_word_size); coleenp@4037: } jmasa@4196: if (p == NULL) { jmasa@4196: p = allocate_work(raw_word_size); coleenp@4037: } coleenp@4037: jmasa@4196: return p; coleenp@4037: } coleenp@4037: coleenp@4037: // Returns the address of spaced allocated for "word_size". coleenp@4037: // This methods does not know about blocks (Metablocks) jmasa@4196: MetaWord* SpaceManager::allocate_work(size_t word_size) { coleenp@4037: assert_lock_strong(_lock); coleenp@4037: #ifdef ASSERT coleenp@4037: if (Metadebug::test_metadata_failure()) { coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: #endif coleenp@4037: // Is there space in the current chunk? jmasa@4196: MetaWord* result = NULL; coleenp@4037: coleenp@4037: // For DumpSharedSpaces, only allocate out of the current chunk which is coleenp@4037: // never null because we gave it the size we wanted. Caller reports out coleenp@4037: // of memory if this returns null. coleenp@4037: if (DumpSharedSpaces) { coleenp@4037: assert(current_chunk() != NULL, "should never happen"); jmasa@5015: inc_used_metrics(word_size); coleenp@4037: return current_chunk()->allocate(word_size); // caller handles null result coleenp@4037: } stefank@5863: coleenp@4037: if (current_chunk() != NULL) { coleenp@4037: result = current_chunk()->allocate(word_size); coleenp@4037: } coleenp@4037: coleenp@4037: if (result == NULL) { coleenp@4037: result = grow_and_allocate(word_size); coleenp@4037: } stefank@5863: stefank@5863: if (result != NULL) { jmasa@5015: inc_used_metrics(word_size); jmasa@4196: assert(result != (MetaWord*) chunks_in_use(MediumIndex), jmasa@4196: "Head of the list is being allocated"); coleenp@4037: } coleenp@4037: coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: void SpaceManager::verify() { coleenp@4037: // If there are blocks in the dictionary, then coleenp@4037: // verfication of chunks does not work since coleenp@4037: // being in the dictionary alters a chunk. jmasa@4196: if (block_freelists()->total_size() == 0) { jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: Metachunk* curr = chunks_in_use(i); coleenp@4037: while (curr != NULL) { coleenp@4037: curr->verify(); jmasa@4327: verify_chunk_size(curr); coleenp@4037: curr = curr->next(); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: jmasa@4327: void SpaceManager::verify_chunk_size(Metachunk* chunk) { jmasa@4327: assert(is_humongous(chunk->word_size()) || jmasa@4382: chunk->word_size() == medium_chunk_size() || jmasa@4382: chunk->word_size() == small_chunk_size() || jmasa@4382: chunk->word_size() == specialized_chunk_size(), jmasa@4327: "Chunk size is wrong"); jmasa@4327: return; jmasa@4327: } jmasa@4327: coleenp@4037: #ifdef ASSERT jmasa@5015: void SpaceManager::verify_allocated_blocks_words() { coleenp@4037: // Verification is only guaranteed at a safepoint. jmasa@5015: assert(SafepointSynchronize::is_at_safepoint() || !Universe::is_fully_initialized(), jmasa@5015: "Verification can fail if the applications is running"); jmasa@5015: assert(allocated_blocks_words() == sum_used_in_chunks_in_use(), mikael@4548: err_msg("allocation total is not consistent " SIZE_FORMAT mikael@4548: " vs " SIZE_FORMAT, jmasa@5015: allocated_blocks_words(), sum_used_in_chunks_in_use())); coleenp@4037: } coleenp@4037: coleenp@4037: #endif coleenp@4037: coleenp@4037: void SpaceManager::dump(outputStream* const out) const { coleenp@4037: size_t curr_total = 0; coleenp@4037: size_t waste = 0; coleenp@4037: uint i = 0; coleenp@4037: size_t used = 0; coleenp@4037: size_t capacity = 0; coleenp@4037: coleenp@4037: // Add up statistics for all chunks in this SpaceManager. jmasa@4382: for (ChunkIndex index = ZeroIndex; jmasa@4196: index < NumberOfInUseLists; coleenp@4037: index = next_chunk_index(index)) { coleenp@4037: for (Metachunk* curr = chunks_in_use(index); coleenp@4037: curr != NULL; coleenp@4037: curr = curr->next()) { coleenp@4037: out->print("%d) ", i++); coleenp@4037: curr->print_on(out); coleenp@4037: curr_total += curr->word_size(); coleenp@4037: used += curr->used_word_size(); stefank@5941: capacity += curr->word_size(); coleenp@4037: waste += curr->free_word_size() + curr->overhead();; coleenp@4037: } coleenp@4037: } coleenp@4037: stefank@5707: if (TraceMetadataChunkAllocation && Verbose) { stefank@5707: block_freelists()->print_on(out); stefank@5707: } stefank@5707: jmasa@4382: size_t free = current_chunk() == NULL ? 0 : current_chunk()->free_word_size(); coleenp@4037: // Free space isn't wasted. coleenp@4037: waste -= free; coleenp@4037: coleenp@4037: out->print_cr("total of all chunks " SIZE_FORMAT " used " SIZE_FORMAT coleenp@4037: " free " SIZE_FORMAT " capacity " SIZE_FORMAT coleenp@4037: " waste " SIZE_FORMAT, curr_total, used, free, capacity, waste); coleenp@4037: } coleenp@4037: coleenp@4304: #ifndef PRODUCT coleenp@4037: void SpaceManager::mangle_freed_chunks() { jmasa@4382: for (ChunkIndex index = ZeroIndex; jmasa@4196: index < NumberOfInUseLists; coleenp@4037: index = next_chunk_index(index)) { coleenp@4037: for (Metachunk* curr = chunks_in_use(index); coleenp@4037: curr != NULL; coleenp@4037: curr = curr->next()) { coleenp@4037: curr->mangle(); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4304: #endif // PRODUCT coleenp@4037: coleenp@4037: // MetaspaceAux coleenp@4037: jmasa@5015: ehelin@6609: size_t MetaspaceAux::_capacity_words[] = {0, 0}; ehelin@6609: size_t MetaspaceAux::_used_words[] = {0, 0}; jmasa@5015: ehelin@5531: size_t MetaspaceAux::free_bytes(Metaspace::MetadataType mdtype) { ehelin@5531: VirtualSpaceList* list = Metaspace::get_space_list(mdtype); ehelin@5531: return list == NULL ? 0 : list->free_bytes(); ehelin@5531: } ehelin@5531: jmasa@5015: size_t MetaspaceAux::free_bytes() { ehelin@5531: return free_bytes(Metaspace::ClassType) + free_bytes(Metaspace::NonClassType); jmasa@5015: } jmasa@5015: jmasa@5162: void MetaspaceAux::dec_capacity(Metaspace::MetadataType mdtype, size_t words) { jmasa@5015: assert_lock_strong(SpaceManager::expand_lock()); ehelin@6609: assert(words <= capacity_words(mdtype), jmasa@5015: err_msg("About to decrement below 0: words " SIZE_FORMAT ehelin@6609: " is greater than _capacity_words[%u] " SIZE_FORMAT, ehelin@6609: words, mdtype, capacity_words(mdtype))); ehelin@6609: _capacity_words[mdtype] -= words; jmasa@5015: } jmasa@5015: jmasa@5162: void MetaspaceAux::inc_capacity(Metaspace::MetadataType mdtype, size_t words) { jmasa@5015: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5015: // Needs to be atomic ehelin@6609: _capacity_words[mdtype] += words; jmasa@5015: } jmasa@5015: jmasa@5162: void MetaspaceAux::dec_used(Metaspace::MetadataType mdtype, size_t words) { ehelin@6609: assert(words <= used_words(mdtype), jmasa@5015: err_msg("About to decrement below 0: words " SIZE_FORMAT ehelin@6609: " is greater than _used_words[%u] " SIZE_FORMAT, ehelin@6609: words, mdtype, used_words(mdtype))); jmasa@5015: // For CMS deallocation of the Metaspaces occurs during the jmasa@5015: // sweep which is a concurrent phase. Protection by the expand_lock() jmasa@5015: // is not enough since allocation is on a per Metaspace basis jmasa@5015: // and protected by the Metaspace lock. jmasa@5015: jlong minus_words = (jlong) - (jlong) words; ehelin@6609: Atomic::add_ptr(minus_words, &_used_words[mdtype]); jmasa@5015: } jmasa@5015: jmasa@5162: void MetaspaceAux::inc_used(Metaspace::MetadataType mdtype, size_t words) { ehelin@6609: // _used_words tracks allocations for jmasa@5015: // each piece of metadata. Those allocations are jmasa@5015: // generally done concurrently by different application jmasa@5015: // threads so must be done atomically. ehelin@6609: Atomic::add_ptr(words, &_used_words[mdtype]); jmasa@5015: } jmasa@5015: jmasa@5015: size_t MetaspaceAux::used_bytes_slow(Metaspace::MetadataType mdtype) { jmasa@4042: size_t used = 0; jmasa@4042: ClassLoaderDataGraphMetaspaceIterator iter; jmasa@4042: while (iter.repeat()) { jmasa@4042: Metaspace* msp = iter.get_next(); jmasa@5015: // Sum allocated_blocks_words for each metaspace jmasa@4042: if (msp != NULL) { jmasa@5015: used += msp->used_words_slow(mdtype); jmasa@4042: } jmasa@4042: } jmasa@4042: return used * BytesPerWord; jmasa@4042: } jmasa@4042: ehelin@5703: size_t MetaspaceAux::free_bytes_slow(Metaspace::MetadataType mdtype) { coleenp@4037: size_t free = 0; coleenp@4037: ClassLoaderDataGraphMetaspaceIterator iter; coleenp@4037: while (iter.repeat()) { coleenp@4037: Metaspace* msp = iter.get_next(); coleenp@4037: if (msp != NULL) { ehelin@5703: free += msp->free_words_slow(mdtype); coleenp@4037: } coleenp@4037: } coleenp@4037: return free * BytesPerWord; coleenp@4037: } coleenp@4037: jmasa@5015: size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) { hseigel@5528: if ((mdtype == Metaspace::ClassType) && !Metaspace::using_class_space()) { hseigel@5528: return 0; hseigel@5528: } jmasa@5015: // Don't count the space in the freelists. That space will be jmasa@5015: // added to the capacity calculation as needed. jmasa@5015: size_t capacity = 0; coleenp@4037: ClassLoaderDataGraphMetaspaceIterator iter; coleenp@4037: while (iter.repeat()) { coleenp@4037: Metaspace* msp = iter.get_next(); coleenp@4037: if (msp != NULL) { jmasa@5015: capacity += msp->capacity_words_slow(mdtype); coleenp@4037: } coleenp@4037: } coleenp@4037: return capacity * BytesPerWord; coleenp@4037: } coleenp@4037: ehelin@5703: size_t MetaspaceAux::capacity_bytes_slow() { ehelin@5703: #ifdef PRODUCT ehelin@6609: // Use capacity_bytes() in PRODUCT instead of this function. ehelin@5703: guarantee(false, "Should not call capacity_bytes_slow() in the PRODUCT"); ehelin@5703: #endif ehelin@5703: size_t class_capacity = capacity_bytes_slow(Metaspace::ClassType); ehelin@5703: size_t non_class_capacity = capacity_bytes_slow(Metaspace::NonClassType); ehelin@6609: assert(capacity_bytes() == class_capacity + non_class_capacity, ehelin@6609: err_msg("bad accounting: capacity_bytes() " SIZE_FORMAT ehelin@5703: " class_capacity + non_class_capacity " SIZE_FORMAT ehelin@5703: " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT, ehelin@6609: capacity_bytes(), class_capacity + non_class_capacity, ehelin@5703: class_capacity, non_class_capacity)); ehelin@5703: ehelin@5703: return class_capacity + non_class_capacity; ehelin@5703: } ehelin@5703: ehelin@5703: size_t MetaspaceAux::reserved_bytes(Metaspace::MetadataType mdtype) { ehelin@5531: VirtualSpaceList* list = Metaspace::get_space_list(mdtype); stefank@5704: return list == NULL ? 0 : list->reserved_bytes(); stefank@5704: } stefank@5704: stefank@5704: size_t MetaspaceAux::committed_bytes(Metaspace::MetadataType mdtype) { stefank@5704: VirtualSpaceList* list = Metaspace::get_space_list(mdtype); stefank@5704: return list == NULL ? 0 : list->committed_bytes(); coleenp@4037: } coleenp@4037: ehelin@5703: size_t MetaspaceAux::min_chunk_size_words() { return Metaspace::first_chunk_word_size(); } ehelin@5703: ehelin@5703: size_t MetaspaceAux::free_chunks_total_words(Metaspace::MetadataType mdtype) { stefank@5771: ChunkManager* chunk_manager = Metaspace::get_chunk_manager(mdtype); stefank@5771: if (chunk_manager == NULL) { hseigel@5528: return 0; hseigel@5528: } stefank@5771: chunk_manager->slow_verify(); stefank@5771: return chunk_manager->free_chunks_total_words(); coleenp@4037: } coleenp@4037: ehelin@5703: size_t MetaspaceAux::free_chunks_total_bytes(Metaspace::MetadataType mdtype) { ehelin@5703: return free_chunks_total_words(mdtype) * BytesPerWord; coleenp@4037: } coleenp@4037: ehelin@5703: size_t MetaspaceAux::free_chunks_total_words() { ehelin@5703: return free_chunks_total_words(Metaspace::ClassType) + ehelin@5703: free_chunks_total_words(Metaspace::NonClassType); jmasa@5015: } jmasa@5015: ehelin@5703: size_t MetaspaceAux::free_chunks_total_bytes() { ehelin@5703: return free_chunks_total_words() * BytesPerWord; jmasa@5015: } jmasa@5015: ehelin@6420: bool MetaspaceAux::has_chunk_free_list(Metaspace::MetadataType mdtype) { ehelin@6420: return Metaspace::get_chunk_manager(mdtype) != NULL; ehelin@6420: } ehelin@6420: ehelin@6420: MetaspaceChunkFreeListSummary MetaspaceAux::chunk_free_list_summary(Metaspace::MetadataType mdtype) { ehelin@6420: if (!has_chunk_free_list(mdtype)) { ehelin@6420: return MetaspaceChunkFreeListSummary(); ehelin@6420: } ehelin@6420: ehelin@6420: const ChunkManager* cm = Metaspace::get_chunk_manager(mdtype); ehelin@6420: return cm->chunk_free_list_summary(); ehelin@6420: } ehelin@6420: coleenp@4037: void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) { coleenp@4037: gclog_or_tty->print(", [Metaspace:"); coleenp@4037: if (PrintGCDetails && Verbose) { coleenp@4037: gclog_or_tty->print(" " SIZE_FORMAT coleenp@4037: "->" SIZE_FORMAT jmasa@5015: "(" SIZE_FORMAT ")", coleenp@4037: prev_metadata_used, ehelin@6609: used_bytes(), ehelin@5703: reserved_bytes()); coleenp@4037: } else { coleenp@4037: gclog_or_tty->print(" " SIZE_FORMAT "K" coleenp@4037: "->" SIZE_FORMAT "K" jmasa@5015: "(" SIZE_FORMAT "K)", ehelin@5703: prev_metadata_used/K, ehelin@6609: used_bytes()/K, ehelin@5703: reserved_bytes()/K); coleenp@4037: } coleenp@4037: coleenp@4037: gclog_or_tty->print("]"); coleenp@4037: } coleenp@4037: coleenp@4037: // This is printed when PrintGCDetails coleenp@4037: void MetaspaceAux::print_on(outputStream* out) { coleenp@4037: Metaspace::MetadataType nct = Metaspace::NonClassType; coleenp@4037: stefank@5863: out->print_cr(" Metaspace " stefank@5863: "used " SIZE_FORMAT "K, " stefank@5863: "capacity " SIZE_FORMAT "K, " stefank@5863: "committed " SIZE_FORMAT "K, " stefank@5863: "reserved " SIZE_FORMAT "K", ehelin@6609: used_bytes()/K, ehelin@6609: capacity_bytes()/K, stefank@5863: committed_bytes()/K, stefank@5863: reserved_bytes()/K); stefank@5863: hseigel@5528: if (Metaspace::using_class_space()) { hseigel@5528: Metaspace::MetadataType ct = Metaspace::ClassType; hseigel@5528: out->print_cr(" class space " stefank@5863: "used " SIZE_FORMAT "K, " stefank@5863: "capacity " SIZE_FORMAT "K, " stefank@5863: "committed " SIZE_FORMAT "K, " stefank@5863: "reserved " SIZE_FORMAT "K", ehelin@6609: used_bytes(ct)/K, ehelin@6609: capacity_bytes(ct)/K, stefank@5863: committed_bytes(ct)/K, ehelin@5703: reserved_bytes(ct)/K); hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: // Print information for class space and data space separately. coleenp@4037: // This is almost the same as above. coleenp@4037: void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) { ehelin@5703: size_t free_chunks_capacity_bytes = free_chunks_total_bytes(mdtype); jmasa@5015: size_t capacity_bytes = capacity_bytes_slow(mdtype); jmasa@5015: size_t used_bytes = used_bytes_slow(mdtype); ehelin@5703: size_t free_bytes = free_bytes_slow(mdtype); coleenp@4037: size_t used_and_free = used_bytes + free_bytes + coleenp@4037: free_chunks_capacity_bytes; coleenp@4037: out->print_cr(" Chunk accounting: used in chunks " SIZE_FORMAT coleenp@4037: "K + unused in chunks " SIZE_FORMAT "K + " coleenp@4037: " capacity in free chunks " SIZE_FORMAT "K = " SIZE_FORMAT coleenp@4037: "K capacity in allocated chunks " SIZE_FORMAT "K", coleenp@4037: used_bytes / K, coleenp@4037: free_bytes / K, coleenp@4037: free_chunks_capacity_bytes / K, coleenp@4037: used_and_free / K, coleenp@4037: capacity_bytes / K); mgerdin@4738: // Accounting can only be correct if we got the values during a safepoint mgerdin@4738: assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong"); coleenp@4037: } coleenp@4037: hseigel@5528: // Print total fragmentation for class metaspaces hseigel@5528: void MetaspaceAux::print_class_waste(outputStream* out) { hseigel@5528: assert(Metaspace::using_class_space(), "class metaspace not used"); hseigel@5528: size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0; hseigel@5528: size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0; hseigel@5528: ClassLoaderDataGraphMetaspaceIterator iter; hseigel@5528: while (iter.repeat()) { hseigel@5528: Metaspace* msp = iter.get_next(); hseigel@5528: if (msp != NULL) { hseigel@5528: cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex); hseigel@5528: cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex); hseigel@5528: cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex); hseigel@5528: cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex); hseigel@5528: cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex); hseigel@5528: cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex); hseigel@5528: cls_humongous_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex); hseigel@5528: } hseigel@5528: } hseigel@5528: out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", " hseigel@5528: SIZE_FORMAT " small(s) " SIZE_FORMAT ", " hseigel@5528: SIZE_FORMAT " medium(s) " SIZE_FORMAT ", " hseigel@5528: "large count " SIZE_FORMAT, hseigel@5528: cls_specialized_count, cls_specialized_waste, hseigel@5528: cls_small_count, cls_small_waste, hseigel@5528: cls_medium_count, cls_medium_waste, cls_humongous_count); hseigel@5528: } hseigel@5528: hseigel@5528: // Print total fragmentation for data and class metaspaces separately coleenp@4037: void MetaspaceAux::print_waste(outputStream* out) { coleenp@5337: size_t specialized_waste = 0, small_waste = 0, medium_waste = 0; coleenp@5337: size_t specialized_count = 0, small_count = 0, medium_count = 0, humongous_count = 0; coleenp@4037: coleenp@4037: ClassLoaderDataGraphMetaspaceIterator iter; coleenp@4037: while (iter.repeat()) { coleenp@4037: Metaspace* msp = iter.get_next(); coleenp@4037: if (msp != NULL) { jmasa@4382: specialized_waste += msp->vsm()->sum_waste_in_chunks_in_use(SpecializedIndex); jmasa@4382: specialized_count += msp->vsm()->sum_count_in_chunks_in_use(SpecializedIndex); coleenp@4037: small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex); jmasa@4382: small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex); coleenp@4037: medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex); jmasa@4382: medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex); coleenp@5337: humongous_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex); coleenp@4037: } coleenp@4037: } coleenp@4037: out->print_cr("Total fragmentation waste (words) doesn't count free space"); jmasa@4382: out->print_cr(" data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", " jmasa@4382: SIZE_FORMAT " small(s) " SIZE_FORMAT ", " coleenp@5337: SIZE_FORMAT " medium(s) " SIZE_FORMAT ", " coleenp@5337: "large count " SIZE_FORMAT, jmasa@4382: specialized_count, specialized_waste, small_count, coleenp@5337: small_waste, medium_count, medium_waste, humongous_count); hseigel@5528: if (Metaspace::using_class_space()) { hseigel@5528: print_class_waste(out); hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: // Dump global metaspace things from the end of ClassLoaderDataGraph coleenp@4037: void MetaspaceAux::dump(outputStream* out) { coleenp@4037: out->print_cr("All Metaspace:"); coleenp@4037: out->print("data space: "); print_on(out, Metaspace::NonClassType); coleenp@4037: out->print("class space: "); print_on(out, Metaspace::ClassType); coleenp@4037: print_waste(out); coleenp@4037: } coleenp@4037: mgerdin@4264: void MetaspaceAux::verify_free_chunks() { stefank@5771: Metaspace::chunk_manager_metadata()->verify(); hseigel@5528: if (Metaspace::using_class_space()) { stefank@5771: Metaspace::chunk_manager_class()->verify(); hseigel@5528: } mgerdin@4264: } mgerdin@4264: jmasa@5015: void MetaspaceAux::verify_capacity() { jmasa@5015: #ifdef ASSERT ehelin@6609: size_t running_sum_capacity_bytes = capacity_bytes(); jmasa@5162: // For purposes of the running sum of capacity, verify against capacity jmasa@5015: size_t capacity_in_use_bytes = capacity_bytes_slow(); jmasa@5015: assert(running_sum_capacity_bytes == capacity_in_use_bytes, ehelin@6609: err_msg("capacity_words() * BytesPerWord " SIZE_FORMAT jmasa@5015: " capacity_bytes_slow()" SIZE_FORMAT, jmasa@5015: running_sum_capacity_bytes, capacity_in_use_bytes)); jmasa@5162: for (Metaspace::MetadataType i = Metaspace::ClassType; jmasa@5162: i < Metaspace:: MetadataTypeCount; jmasa@5162: i = (Metaspace::MetadataType)(i + 1)) { jmasa@5162: size_t capacity_in_use_bytes = capacity_bytes_slow(i); ehelin@6609: assert(capacity_bytes(i) == capacity_in_use_bytes, ehelin@6609: err_msg("capacity_bytes(%u) " SIZE_FORMAT jmasa@5162: " capacity_bytes_slow(%u)" SIZE_FORMAT, ehelin@6609: i, capacity_bytes(i), i, capacity_in_use_bytes)); jmasa@5162: } jmasa@5015: #endif jmasa@5015: } jmasa@5015: jmasa@5015: void MetaspaceAux::verify_used() { jmasa@5015: #ifdef ASSERT ehelin@6609: size_t running_sum_used_bytes = used_bytes(); jmasa@5162: // For purposes of the running sum of used, verify against used jmasa@5015: size_t used_in_use_bytes = used_bytes_slow(); ehelin@6609: assert(used_bytes() == used_in_use_bytes, ehelin@6609: err_msg("used_bytes() " SIZE_FORMAT jmasa@5162: " used_bytes_slow()" SIZE_FORMAT, ehelin@6609: used_bytes(), used_in_use_bytes)); jmasa@5162: for (Metaspace::MetadataType i = Metaspace::ClassType; jmasa@5162: i < Metaspace:: MetadataTypeCount; jmasa@5162: i = (Metaspace::MetadataType)(i + 1)) { jmasa@5162: size_t used_in_use_bytes = used_bytes_slow(i); ehelin@6609: assert(used_bytes(i) == used_in_use_bytes, ehelin@6609: err_msg("used_bytes(%u) " SIZE_FORMAT jmasa@5162: " used_bytes_slow(%u)" SIZE_FORMAT, ehelin@6609: i, used_bytes(i), i, used_in_use_bytes)); jmasa@5162: } jmasa@5015: #endif jmasa@5015: } jmasa@5015: jmasa@5015: void MetaspaceAux::verify_metrics() { jmasa@5015: verify_capacity(); jmasa@5015: verify_used(); jmasa@5015: } jmasa@5015: jmasa@5015: coleenp@4037: // Metaspace methods coleenp@4037: coleenp@4037: size_t Metaspace::_first_chunk_word_size = 0; jmasa@4382: size_t Metaspace::_first_class_chunk_word_size = 0; jmasa@4382: stefank@5863: size_t Metaspace::_commit_alignment = 0; stefank@5863: size_t Metaspace::_reserve_alignment = 0; stefank@5863: jmasa@4382: Metaspace::Metaspace(Mutex* lock, MetaspaceType type) { jmasa@4382: initialize(lock, type); coleenp@4037: } coleenp@4037: coleenp@4037: Metaspace::~Metaspace() { coleenp@4037: delete _vsm; hseigel@5528: if (using_class_space()) { hseigel@5528: delete _class_vsm; hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: VirtualSpaceList* Metaspace::_space_list = NULL; coleenp@4037: VirtualSpaceList* Metaspace::_class_space_list = NULL; coleenp@4037: stefank@5771: ChunkManager* Metaspace::_chunk_manager_metadata = NULL; stefank@5771: ChunkManager* Metaspace::_chunk_manager_class = NULL; stefank@5771: coleenp@4037: #define VIRTUALSPACEMULTIPLIER 2 coleenp@4037: hseigel@5528: #ifdef _LP64 coleenp@6029: static const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1); coleenp@6029: hseigel@5528: void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) { hseigel@5528: // Figure out the narrow_klass_base and the narrow_klass_shift. The hseigel@5528: // narrow_klass_base is the lower of the metaspace base and the cds base hseigel@5528: // (if cds is enabled). The narrow_klass_shift depends on the distance hseigel@5528: // between the lower base and higher address. hseigel@5528: address lower_base; hseigel@5528: address higher_address; iklam@7089: #if INCLUDE_CDS hseigel@5528: if (UseSharedSpaces) { hseigel@5528: higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()), coleenp@6029: (address)(metaspace_base + compressed_class_space_size())); hseigel@5528: lower_base = MIN2(metaspace_base, cds_base); iklam@7089: } else iklam@7089: #endif iklam@7089: { coleenp@6029: higher_address = metaspace_base + compressed_class_space_size(); hseigel@5528: lower_base = metaspace_base; coleenp@6029: coleenp@6029: uint64_t klass_encoding_max = UnscaledClassSpaceMax << LogKlassAlignmentInBytes; coleenp@6029: // If compressed class space fits in lower 32G, we don't need a base. coleenp@6029: if (higher_address <= (address)klass_encoding_max) { coleenp@6029: lower_base = 0; // effectively lower base is zero. coleenp@6029: } hseigel@5528: } coleenp@6029: hseigel@5528: Universe::set_narrow_klass_base(lower_base); coleenp@6029: coleenp@6062: if ((uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax) { hseigel@5528: Universe::set_narrow_klass_shift(0); hseigel@5528: } else { hseigel@5528: assert(!UseSharedSpaces, "Cannot shift with UseSharedSpaces"); hseigel@5528: Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes); hseigel@5528: } hseigel@5528: } hseigel@5528: iklam@7089: #if INCLUDE_CDS hseigel@5528: // Return TRUE if the specified metaspace_base and cds_base are close enough hseigel@5528: // to work with compressed klass pointers. hseigel@5528: bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) { hseigel@5528: assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS"); ehelin@5694: assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs"); hseigel@5528: address lower_base = MIN2((address)metaspace_base, cds_base); hseigel@5528: address higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()), coleenp@6029: (address)(metaspace_base + compressed_class_space_size())); coleenp@6062: return ((uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax); hseigel@5528: } iklam@7089: #endif hseigel@5528: hseigel@5528: // Try to allocate the metaspace at the requested addr. hseigel@5528: void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) { hseigel@5528: assert(using_class_space(), "called improperly"); ehelin@5694: assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs"); coleenp@6029: assert(compressed_class_space_size() < KlassEncodingMetaspaceMax, hseigel@5528: "Metaspace size is too big"); coleenp@6029: assert_is_ptr_aligned(requested_addr, _reserve_alignment); coleenp@6029: assert_is_ptr_aligned(cds_base, _reserve_alignment); coleenp@6029: assert_is_size_aligned(compressed_class_space_size(), _reserve_alignment); stefank@5863: stefank@5863: // Don't use large pages for the class space. stefank@5863: bool large_pages = false; hseigel@5528: coleenp@6029: ReservedSpace metaspace_rs = ReservedSpace(compressed_class_space_size(), stefank@5863: _reserve_alignment, stefank@5863: large_pages, stefank@5863: requested_addr, 0); hseigel@5528: if (!metaspace_rs.is_reserved()) { iklam@7089: #if INCLUDE_CDS hseigel@5528: if (UseSharedSpaces) { stefank@5863: size_t increment = align_size_up(1*G, _reserve_alignment); stefank@5863: hseigel@5528: // Keep trying to allocate the metaspace, increasing the requested_addr hseigel@5528: // by 1GB each time, until we reach an address that will no longer allow hseigel@5528: // use of CDS with compressed klass pointers. hseigel@5528: char *addr = requested_addr; stefank@5863: while (!metaspace_rs.is_reserved() && (addr + increment > addr) && stefank@5863: can_use_cds_with_metaspace_addr(addr + increment, cds_base)) { stefank@5863: addr = addr + increment; coleenp@6029: metaspace_rs = ReservedSpace(compressed_class_space_size(), stefank@5863: _reserve_alignment, large_pages, addr, 0); hseigel@5528: } hseigel@5528: } iklam@7089: #endif hseigel@5528: // If no successful allocation then try to allocate the space anywhere. If hseigel@5528: // that fails then OOM doom. At this point we cannot try allocating the ehelin@5694: // metaspace as if UseCompressedClassPointers is off because too much ehelin@5694: // initialization has happened that depends on UseCompressedClassPointers. ehelin@5694: // So, UseCompressedClassPointers cannot be turned off at this point. hseigel@5528: if (!metaspace_rs.is_reserved()) { coleenp@6029: metaspace_rs = ReservedSpace(compressed_class_space_size(), stefank@5863: _reserve_alignment, large_pages); hseigel@5528: if (!metaspace_rs.is_reserved()) { hseigel@5528: vm_exit_during_initialization(err_msg("Could not allocate metaspace: %d bytes", coleenp@6029: compressed_class_space_size())); hseigel@5528: } hseigel@5528: } hseigel@5528: } hseigel@5528: hseigel@5528: // If we got here then the metaspace got allocated. hseigel@5528: MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass); hseigel@5528: iklam@7089: #if INCLUDE_CDS hseigel@5528: // Verify that we can use shared spaces. Otherwise, turn off CDS. hseigel@5528: if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) { hseigel@5528: FileMapInfo::stop_sharing_and_unmap( hseigel@5528: "Could not allocate metaspace at a compatible address"); hseigel@5528: } iklam@7089: #endif hseigel@5528: set_narrow_klass_base_and_shift((address)metaspace_rs.base(), hseigel@5528: UseSharedSpaces ? (address)cds_base : 0); hseigel@5528: hseigel@5528: initialize_class_space(metaspace_rs); hseigel@5528: hseigel@5528: if (PrintCompressedOopsMode || (PrintMiscellaneous && Verbose)) { hseigel@5528: gclog_or_tty->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: " SIZE_FORMAT, hseigel@5528: Universe::narrow_klass_base(), Universe::narrow_klass_shift()); coleenp@6029: gclog_or_tty->print_cr("Compressed class space size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT, coleenp@6029: compressed_class_space_size(), metaspace_rs.base(), requested_addr); hseigel@5528: } hseigel@5528: } hseigel@5528: ehelin@5694: // For UseCompressedClassPointers the class space is reserved above the top of hseigel@5528: // the Java heap. The argument passed in is at the base of the compressed space. hseigel@5528: void Metaspace::initialize_class_space(ReservedSpace rs) { hseigel@5528: // The reserved space size may be bigger because of alignment, esp with UseLargePages ehelin@5694: assert(rs.size() >= CompressedClassSpaceSize, ehelin@5694: err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), CompressedClassSpaceSize)); hseigel@5528: assert(using_class_space(), "Must be using class space"); hseigel@5528: _class_space_list = new VirtualSpaceList(rs); stefank@5771: _chunk_manager_class = new ChunkManager(SpecializedChunk, ClassSmallChunk, ClassMediumChunk); stefank@5863: stefank@5863: if (!_class_space_list->initialization_succeeded()) { stefank@5863: vm_exit_during_initialization("Failed to setup compressed class space virtual space list."); stefank@5863: } hseigel@5528: } hseigel@5528: hseigel@5528: #endif hseigel@5528: stefank@5863: void Metaspace::ergo_initialize() { stefank@5863: if (DumpSharedSpaces) { stefank@5863: // Using large pages when dumping the shared archive is currently not implemented. stefank@5863: FLAG_SET_ERGO(bool, UseLargePagesInMetaspace, false); stefank@5863: } stefank@5863: stefank@5863: size_t page_size = os::vm_page_size(); stefank@5863: if (UseLargePages && UseLargePagesInMetaspace) { stefank@5863: page_size = os::large_page_size(); stefank@5863: } stefank@5863: stefank@5863: _commit_alignment = page_size; stefank@5863: _reserve_alignment = MAX2(page_size, (size_t)os::vm_allocation_granularity()); stefank@5863: stefank@5863: // Do not use FLAG_SET_ERGO to update MaxMetaspaceSize, since this will stefank@5863: // override if MaxMetaspaceSize was set on the command line or not. stefank@5863: // This information is needed later to conform to the specification of the stefank@5863: // java.lang.management.MemoryUsage API. stefank@5863: // stefank@5863: // Ideally, we would be able to set the default value of MaxMetaspaceSize in stefank@5863: // globals.hpp to the aligned value, but this is not possible, since the stefank@5863: // alignment depends on other flags being parsed. jwilhelm@6083: MaxMetaspaceSize = align_size_down_bounded(MaxMetaspaceSize, _reserve_alignment); stefank@5863: stefank@5863: if (MetaspaceSize > MaxMetaspaceSize) { stefank@5863: MetaspaceSize = MaxMetaspaceSize; stefank@5863: } stefank@5863: jwilhelm@6083: MetaspaceSize = align_size_down_bounded(MetaspaceSize, _commit_alignment); stefank@5863: stefank@5863: assert(MetaspaceSize <= MaxMetaspaceSize, "MetaspaceSize should be limited by MaxMetaspaceSize"); stefank@5863: stefank@5863: if (MetaspaceSize < 256*K) { stefank@5863: vm_exit_during_initialization("Too small initial Metaspace size"); stefank@5863: } stefank@5863: jwilhelm@6083: MinMetaspaceExpansion = align_size_down_bounded(MinMetaspaceExpansion, _commit_alignment); jwilhelm@6083: MaxMetaspaceExpansion = align_size_down_bounded(MaxMetaspaceExpansion, _commit_alignment); jwilhelm@6083: jwilhelm@6083: CompressedClassSpaceSize = align_size_down_bounded(CompressedClassSpaceSize, _reserve_alignment); coleenp@6029: set_compressed_class_space_size(CompressedClassSpaceSize); stefank@5863: } stefank@5863: coleenp@4037: void Metaspace::global_initialize() { ehelin@6722: MetaspaceGC::initialize(); ehelin@6722: coleenp@4037: // Initialize the alignment for shared spaces. minqi@7297: int max_alignment = os::vm_allocation_granularity(); hseigel@5528: size_t cds_total = 0; hseigel@5528: coleenp@4037: MetaspaceShared::set_max_alignment(max_alignment); coleenp@4037: coleenp@4037: if (DumpSharedSpaces) { iklam@7089: #if INCLUDE_CDS ccheung@7103: MetaspaceShared::estimate_regions_size(); ccheung@7103: stefank@5863: SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment); coleenp@4037: SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment); stefank@5863: SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment); stefank@5863: SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment); coleenp@4037: ccheung@7300: // the min_misc_code_size estimate is based on MetaspaceShared::generate_vtable_methods() ccheung@7300: uintx min_misc_code_size = align_size_up( ccheung@7300: (MetaspaceShared::num_virtuals * MetaspaceShared::vtbl_list_size) * ccheung@7300: (sizeof(void*) + MetaspaceShared::vtbl_method_size) + MetaspaceShared::vtbl_common_code_size, ccheung@7300: max_alignment); ccheung@7300: ccheung@7300: if (SharedMiscCodeSize < min_misc_code_size) { ccheung@7300: report_out_of_shared_space(SharedMiscCode); ccheung@7300: } ccheung@7300: coleenp@4037: // Initialize with the sum of the shared space sizes. The read-only coleenp@4037: // and read write metaspace chunks will be allocated out of this and the coleenp@4037: // remainder is the misc code and data chunks. hseigel@5528: cds_total = FileMapInfo::shared_spaces_size(); stefank@5863: cds_total = align_size_up(cds_total, _reserve_alignment); hseigel@5528: _space_list = new VirtualSpaceList(cds_total/wordSize); stefank@5771: _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk); hseigel@5528: stefank@5863: if (!_space_list->initialization_succeeded()) { stefank@5863: vm_exit_during_initialization("Unable to dump shared archive.", NULL); stefank@5863: } stefank@5863: hseigel@5528: #ifdef _LP64 coleenp@6029: if (cds_total + compressed_class_space_size() > UnscaledClassSpaceMax) { stefank@5863: vm_exit_during_initialization("Unable to dump shared archive.", stefank@5863: err_msg("Size of archive (" SIZE_FORMAT ") + compressed class space (" stefank@5863: SIZE_FORMAT ") == total (" SIZE_FORMAT ") is larger than compressed " coleenp@6029: "klass limit: " SIZE_FORMAT, cds_total, compressed_class_space_size(), coleenp@6029: cds_total + compressed_class_space_size(), UnscaledClassSpaceMax)); stefank@5863: } stefank@5863: hseigel@5528: // Set the compressed klass pointer base so that decoding of these pointers works hseigel@5528: // properly when creating the shared archive. ehelin@5694: assert(UseCompressedOops && UseCompressedClassPointers, ehelin@5694: "UseCompressedOops and UseCompressedClassPointers must be set"); hseigel@5528: Universe::set_narrow_klass_base((address)_space_list->current_virtual_space()->bottom()); hseigel@5528: if (TraceMetavirtualspaceAllocation && Verbose) { hseigel@5528: gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT, hseigel@5528: _space_list->current_virtual_space()->bottom()); hseigel@5528: } hseigel@5528: hseigel@5528: Universe::set_narrow_klass_shift(0); iklam@7089: #endif // _LP64 iklam@7089: #endif // INCLUDE_CDS coleenp@4037: } else { iklam@7089: #if INCLUDE_CDS coleenp@4037: // If using shared space, open the file that contains the shared space coleenp@4037: // and map in the memory before initializing the rest of metaspace (so coleenp@4037: // the addresses don't conflict) hseigel@5528: address cds_address = NULL; coleenp@4037: if (UseSharedSpaces) { coleenp@4037: FileMapInfo* mapinfo = new FileMapInfo(); coleenp@4037: coleenp@4037: // Open the shared archive file, read and validate the header. If coleenp@4037: // initialization fails, shared spaces [UseSharedSpaces] are coleenp@4037: // disabled and the file is closed. coleenp@4037: // Map in spaces now also coleenp@4037: if (mapinfo->initialize() && MetaspaceShared::map_shared_spaces(mapinfo)) { stefank@5863: cds_total = FileMapInfo::shared_spaces_size(); stefank@5863: cds_address = (address)mapinfo->region_base(0); coleenp@4037: } else { coleenp@4037: assert(!mapinfo->is_open() && !UseSharedSpaces, coleenp@4037: "archive file not closed or shared spaces not disabled."); coleenp@4037: } coleenp@4037: } iklam@7089: #endif // INCLUDE_CDS hseigel@5528: #ifdef _LP64 ehelin@5694: // If UseCompressedClassPointers is set then allocate the metaspace area hseigel@5528: // above the heap and above the CDS area (if it exists). hseigel@5528: if (using_class_space()) { hseigel@5528: if (UseSharedSpaces) { iklam@7089: #if INCLUDE_CDS stefank@5863: char* cds_end = (char*)(cds_address + cds_total); stefank@5863: cds_end = (char *)align_ptr_up(cds_end, _reserve_alignment); stefank@5863: allocate_metaspace_compressed_klass_ptrs(cds_end, cds_address); iklam@7089: #endif hseigel@5528: } else { coleenp@6029: char* base = (char*)align_ptr_up(Universe::heap()->reserved_region().end(), _reserve_alignment); coleenp@6029: allocate_metaspace_compressed_klass_ptrs(base, 0); hseigel@5528: } hseigel@5528: } iklam@7089: #endif // _LP64 hseigel@5528: jmasa@4382: // Initialize these before initializing the VirtualSpaceList coleenp@4037: _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord; jmasa@4382: _first_chunk_word_size = align_word_size_up(_first_chunk_word_size); jmasa@4382: // Make the first class chunk bigger than a medium chunk so it's not put jmasa@4382: // on the medium chunk list. The next chunk will be small and progress jmasa@4382: // from there. This size calculated by -version. jmasa@4382: _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6, ehelin@5694: (CompressedClassSpaceSize/BytesPerWord)*2); jmasa@4382: _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size); coleenp@4037: // Arbitrarily set the initial virtual space to a multiple coleenp@4037: // of the boot class loader size. stefank@5863: size_t word_size = VIRTUALSPACEMULTIPLIER * _first_chunk_word_size; stefank@5863: word_size = align_size_up(word_size, Metaspace::reserve_alignment_words()); stefank@5863: coleenp@4037: // Initialize the list of virtual spaces. coleenp@4037: _space_list = new VirtualSpaceList(word_size); stefank@5771: _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk); stefank@5863: stefank@5863: if (!_space_list->initialization_succeeded()) { stefank@5863: vm_exit_during_initialization("Unable to setup metadata virtual space list.", NULL); stefank@5863: } coleenp@4037: } stefank@5863: ehelin@6417: _tracer = new MetaspaceTracer(); coleenp@4037: } coleenp@4037: ehelin@6722: void Metaspace::post_initialize() { ehelin@6722: MetaspaceGC::post_initialize(); ehelin@6722: } ehelin@6722: stefank@5771: Metachunk* Metaspace::get_initialization_chunk(MetadataType mdtype, stefank@5771: size_t chunk_word_size, stefank@5771: size_t chunk_bunch) { stefank@5771: // Get a chunk from the chunk freelist stefank@5771: Metachunk* chunk = get_chunk_manager(mdtype)->chunk_freelist_allocate(chunk_word_size); stefank@5771: if (chunk != NULL) { stefank@5771: return chunk; stefank@5771: } stefank@5771: stefank@5863: return get_space_list(mdtype)->get_new_chunk(chunk_word_size, chunk_word_size, chunk_bunch); stefank@5771: } stefank@5771: hseigel@5528: void Metaspace::initialize(Mutex* lock, MetaspaceType type) { coleenp@4037: coleenp@4037: assert(space_list() != NULL, coleenp@4037: "Metadata VirtualSpaceList has not been initialized"); stefank@5771: assert(chunk_manager_metadata() != NULL, stefank@5771: "Metadata ChunkManager has not been initialized"); stefank@5771: stefank@5771: _vsm = new SpaceManager(NonClassType, lock); coleenp@4037: if (_vsm == NULL) { coleenp@4037: return; coleenp@4037: } jmasa@4382: size_t word_size; jmasa@4382: size_t class_word_size; hseigel@5528: vsm()->get_initial_chunk_sizes(type, &word_size, &class_word_size); hseigel@5528: hseigel@5528: if (using_class_space()) { stefank@5771: assert(class_space_list() != NULL, stefank@5771: "Class VirtualSpaceList has not been initialized"); stefank@5771: assert(chunk_manager_class() != NULL, stefank@5771: "Class ChunkManager has not been initialized"); hseigel@5528: hseigel@5528: // Allocate SpaceManager for classes. stefank@5771: _class_vsm = new SpaceManager(ClassType, lock); hseigel@5528: if (_class_vsm == NULL) { hseigel@5528: return; hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: coleenp@4037: // Allocate chunk for metadata objects stefank@5771: Metachunk* new_chunk = get_initialization_chunk(NonClassType, stefank@5771: word_size, stefank@5771: vsm()->medium_chunk_bunch()); coleenp@4037: assert(!DumpSharedSpaces || new_chunk != NULL, "should have enough space for both chunks"); coleenp@4037: if (new_chunk != NULL) { coleenp@4037: // Add to this manager's list of chunks in use and current_chunk(). coleenp@4037: vsm()->add_chunk(new_chunk, true); coleenp@4037: } coleenp@4037: coleenp@4037: // Allocate chunk for class metadata objects hseigel@5528: if (using_class_space()) { stefank@5771: Metachunk* class_chunk = get_initialization_chunk(ClassType, stefank@5771: class_word_size, stefank@5771: class_vsm()->medium_chunk_bunch()); hseigel@5528: if (class_chunk != NULL) { hseigel@5528: class_vsm()->add_chunk(class_chunk, true); hseigel@5528: } coleenp@4037: } iklam@5208: iklam@5208: _alloc_record_head = NULL; iklam@5208: _alloc_record_tail = NULL; coleenp@4037: } coleenp@4037: jmasa@4382: size_t Metaspace::align_word_size_up(size_t word_size) { jmasa@4382: size_t byte_size = word_size * wordSize; jmasa@4382: return ReservedSpace::allocation_align_size_up(byte_size) / wordSize; jmasa@4382: } jmasa@4382: coleenp@4037: MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) { coleenp@4037: // DumpSharedSpaces doesn't use class metadata area (yet) ehelin@5694: // Also, don't use class_vsm() unless UseCompressedClassPointers is true. mgerdin@5808: if (is_class_space_allocation(mdtype)) { jmasa@4196: return class_vsm()->allocate(word_size); coleenp@4037: } else { jmasa@4196: return vsm()->allocate(word_size); coleenp@4037: } coleenp@4037: } coleenp@4037: jmasa@4064: MetaWord* Metaspace::expand_and_allocate(size_t word_size, MetadataType mdtype) { stefank@5863: size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size * BytesPerWord); stefank@5863: assert(delta_bytes > 0, "Must be"); stefank@5863: ehelin@7254: size_t before = 0; ehelin@7254: size_t after = 0; ehelin@7254: MetaWord* res; ehelin@7254: bool incremented; ehelin@7254: ehelin@7254: // Each thread increments the HWM at most once. Even if the thread fails to increment ehelin@7254: // the HWM, an allocation is still attempted. This is because another thread must then ehelin@7254: // have incremented the HWM and therefore the allocation might still succeed. ehelin@7254: do { ehelin@7254: incremented = MetaspaceGC::inc_capacity_until_GC(delta_bytes, &after, &before); ehelin@7254: res = allocate(word_size, mdtype); ehelin@7254: } while (!incremented && res == NULL); ehelin@7254: ehelin@7254: if (incremented) { ehelin@7254: tracer()->report_gc_threshold(before, after, ehelin@7254: MetaspaceGCThresholdUpdater::ExpandAndAllocate); ehelin@7254: if (PrintGCDetails && Verbose) { ehelin@7254: gclog_or_tty->print_cr("Increase capacity to GC from " SIZE_FORMAT ehelin@7254: " to " SIZE_FORMAT, before, after); ehelin@7254: } jmasa@4064: } jmasa@4196: ehelin@7254: return res; jmasa@4064: } jmasa@4064: coleenp@4037: // Space allocated in the Metaspace. This may coleenp@4037: // be across several metadata virtual spaces. coleenp@4037: char* Metaspace::bottom() const { coleenp@4037: assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces"); coleenp@4037: return (char*)vsm()->current_chunk()->bottom(); coleenp@4037: } coleenp@4037: jmasa@5015: size_t Metaspace::used_words_slow(MetadataType mdtype) const { hseigel@5528: if (mdtype == ClassType) { hseigel@5528: return using_class_space() ? class_vsm()->sum_used_in_chunks_in_use() : 0; hseigel@5528: } else { hseigel@5528: return vsm()->sum_used_in_chunks_in_use(); // includes overhead! hseigel@5528: } coleenp@4037: } coleenp@4037: ehelin@5703: size_t Metaspace::free_words_slow(MetadataType mdtype) const { hseigel@5528: if (mdtype == ClassType) { hseigel@5528: return using_class_space() ? class_vsm()->sum_free_in_chunks_in_use() : 0; hseigel@5528: } else { hseigel@5528: return vsm()->sum_free_in_chunks_in_use(); hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: // Space capacity in the Metaspace. It includes coleenp@4037: // space in the list of chunks from which allocations coleenp@4037: // have been made. Don't include space in the global freelist and coleenp@4037: // in the space available in the dictionary which coleenp@4037: // is already counted in some chunk. jmasa@5015: size_t Metaspace::capacity_words_slow(MetadataType mdtype) const { hseigel@5528: if (mdtype == ClassType) { hseigel@5528: return using_class_space() ? class_vsm()->sum_capacity_in_chunks_in_use() : 0; hseigel@5528: } else { hseigel@5528: return vsm()->sum_capacity_in_chunks_in_use(); hseigel@5528: } coleenp@4037: } coleenp@4037: jmasa@5015: size_t Metaspace::used_bytes_slow(MetadataType mdtype) const { jmasa@5015: return used_words_slow(mdtype) * BytesPerWord; jmasa@5015: } jmasa@5015: jmasa@5015: size_t Metaspace::capacity_bytes_slow(MetadataType mdtype) const { jmasa@5015: return capacity_words_slow(mdtype) * BytesPerWord; jmasa@5015: } jmasa@5015: coleenp@4037: void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) { coleenp@4037: if (SafepointSynchronize::is_at_safepoint()) { iklam@7089: if (DumpSharedSpaces && PrintSharedSpaces) { iklam@7089: record_deallocation(ptr, vsm()->get_raw_word_size(word_size)); iklam@7089: } iklam@7089: coleenp@4037: assert(Thread::current()->is_VM_thread(), "should be the VM thread"); jmasa@4196: // Don't take Heap_lock mgerdin@5023: MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag); goetz@6337: if (word_size < TreeChunk >::min_size()) { jmasa@4196: // Dark matter. Too small for dictionary. jmasa@4196: #ifdef ASSERT jmasa@4196: Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5); jmasa@4196: #endif jmasa@4196: return; jmasa@4196: } hseigel@5528: if (is_class && using_class_space()) { hseigel@5528: class_vsm()->deallocate(ptr, word_size); coleenp@4037: } else { jmasa@4196: vsm()->deallocate(ptr, word_size); coleenp@4037: } coleenp@4037: } else { mgerdin@5023: MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: goetz@6337: if (word_size < TreeChunk >::min_size()) { jmasa@4196: // Dark matter. Too small for dictionary. jmasa@4196: #ifdef ASSERT jmasa@4196: Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5); jmasa@4196: #endif jmasa@4196: return; jmasa@4196: } hseigel@5528: if (is_class && using_class_space()) { jmasa@4196: class_vsm()->deallocate(ptr, word_size); coleenp@4037: } else { jmasa@4196: vsm()->deallocate(ptr, word_size); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: stefank@5863: stefank@5941: MetaWord* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size, iklam@5208: bool read_only, MetaspaceObj::Type type, TRAPS) { coleenp@4037: if (HAS_PENDING_EXCEPTION) { coleenp@4037: assert(false, "Should not allocate with exception pending"); coleenp@4037: return NULL; // caller does a CHECK_NULL too coleenp@4037: } coleenp@4037: coleenp@4037: assert(loader_data != NULL, "Should never pass around a NULL loader_data. " coleenp@4037: "ClassLoaderData::the_null_class_loader_data() should have been used."); stefank@5863: coleenp@4037: // Allocate in metaspaces without taking out a lock, because it deadlocks coleenp@4037: // with the SymbolTable_lock. Dumping is single threaded for now. We'll have coleenp@4037: // to revisit this for application class data sharing. coleenp@4037: if (DumpSharedSpaces) { iklam@5208: assert(type > MetaspaceObj::UnknownType && type < MetaspaceObj::_number_of_types, "sanity"); iklam@5208: Metaspace* space = read_only ? loader_data->ro_metaspace() : loader_data->rw_metaspace(); stefank@5863: MetaWord* result = space->allocate(word_size, NonClassType); coleenp@4037: if (result == NULL) { coleenp@4037: report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite); coleenp@4037: } iklam@7089: if (PrintSharedSpaces) { iklam@7089: space->record_allocation(result, type, space->vsm()->get_raw_word_size(word_size)); iklam@7089: } stefank@5941: stefank@5941: // Zero initialize. stefank@5941: Copy::fill_to_aligned_words((HeapWord*)result, word_size, 0); stefank@5941: stefank@5941: return result; coleenp@4037: } coleenp@4037: stefank@5863: MetadataType mdtype = (type == MetaspaceObj::ClassType) ? ClassType : NonClassType; stefank@5863: stefank@5863: // Try to allocate metadata. stefank@5863: MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype); coleenp@4037: coleenp@4037: if (result == NULL) { ehelin@6418: tracer()->report_metaspace_allocation_failure(loader_data, word_size, type, mdtype); ehelin@6418: stefank@5863: // Allocation failed. stefank@5863: if (is_init_completed()) { stefank@5863: // Only start a GC if the bootstrapping has completed. stefank@5863: stefank@5863: // Try to clean out some memory and retry. stefank@5863: result = Universe::heap()->collector_policy()->satisfy_failed_metadata_allocation( stefank@5863: loader_data, word_size, mdtype); coleenp@4037: } coleenp@4037: } stefank@5863: stefank@5863: if (result == NULL) { jwilhelm@7867: report_metadata_oome(loader_data, word_size, type, mdtype, CHECK_NULL); stefank@5863: } stefank@5863: stefank@5941: // Zero initialize. stefank@5941: Copy::fill_to_aligned_words((HeapWord*)result, word_size, 0); stefank@5941: stefank@5941: return result; coleenp@4037: } coleenp@4037: hseigel@6027: size_t Metaspace::class_chunk_size(size_t word_size) { hseigel@6027: assert(using_class_space(), "Has to use class space"); hseigel@6027: return class_vsm()->calc_chunk_size(word_size); hseigel@6027: } hseigel@6027: ehelin@6419: void Metaspace::report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, MetaspaceObj::Type type, MetadataType mdtype, TRAPS) { ehelin@6419: tracer()->report_metadata_oom(loader_data, word_size, type, mdtype); ehelin@6419: stefank@5863: // If result is still null, we are out of memory. stefank@5863: if (Verbose && TraceMetadataChunkAllocation) { stefank@5863: gclog_or_tty->print_cr("Metaspace allocation failed for size " stefank@5863: SIZE_FORMAT, word_size); stefank@5863: if (loader_data->metaspace_or_null() != NULL) { stefank@5863: loader_data->dump(gclog_or_tty); stefank@5863: } stefank@5863: MetaspaceAux::dump(gclog_or_tty); stefank@5863: } stefank@5863: hseigel@6027: bool out_of_compressed_class_space = false; hseigel@6027: if (is_class_space_allocation(mdtype)) { hseigel@6027: Metaspace* metaspace = loader_data->metaspace_non_null(); hseigel@6027: out_of_compressed_class_space = hseigel@6027: MetaspaceAux::committed_bytes(Metaspace::ClassType) + hseigel@6027: (metaspace->class_chunk_size(word_size) * BytesPerWord) > hseigel@6027: CompressedClassSpaceSize; hseigel@6027: } hseigel@6027: stefank@5863: // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support hseigel@6027: const char* space_string = out_of_compressed_class_space ? hseigel@6027: "Compressed class space" : "Metaspace"; hseigel@6027: stefank@5863: report_java_out_of_memory(space_string); stefank@5863: stefank@5863: if (JvmtiExport::should_post_resource_exhausted()) { stefank@5863: JvmtiExport::post_resource_exhausted( stefank@5863: JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR, stefank@5863: space_string); stefank@5863: } stefank@5863: stefank@5863: if (!is_init_completed()) { stefank@5863: vm_exit_during_initialization("OutOfMemoryError", space_string); stefank@5863: } stefank@5863: hseigel@6027: if (out_of_compressed_class_space) { stefank@5863: THROW_OOP(Universe::out_of_memory_error_class_metaspace()); stefank@5863: } else { stefank@5863: THROW_OOP(Universe::out_of_memory_error_metaspace()); stefank@5863: } stefank@5863: } stefank@5863: ehelin@6418: const char* Metaspace::metadata_type_name(Metaspace::MetadataType mdtype) { ehelin@6418: switch (mdtype) { ehelin@6418: case Metaspace::ClassType: return "Class"; ehelin@6418: case Metaspace::NonClassType: return "Metadata"; ehelin@6418: default: ehelin@6418: assert(false, err_msg("Got bad mdtype: %d", (int) mdtype)); ehelin@6418: return NULL; ehelin@6418: } ehelin@6418: } ehelin@6418: iklam@5208: void Metaspace::record_allocation(void* ptr, MetaspaceObj::Type type, size_t word_size) { iklam@5208: assert(DumpSharedSpaces, "sanity"); iklam@5208: iklam@7089: int byte_size = (int)word_size * HeapWordSize; iklam@7089: AllocRecord *rec = new AllocRecord((address)ptr, type, byte_size); iklam@7089: iklam@5208: if (_alloc_record_head == NULL) { iklam@5208: _alloc_record_head = _alloc_record_tail = rec; iklam@7089: } else if (_alloc_record_tail->_ptr + _alloc_record_tail->_byte_size == (address)ptr) { iklam@5208: _alloc_record_tail->_next = rec; iklam@5208: _alloc_record_tail = rec; iklam@7089: } else { iklam@7089: // slow linear search, but this doesn't happen that often, and only when dumping iklam@7089: for (AllocRecord *old = _alloc_record_head; old; old = old->_next) { iklam@7089: if (old->_ptr == ptr) { iklam@7089: assert(old->_type == MetaspaceObj::DeallocatedType, "sanity"); iklam@7089: int remain_bytes = old->_byte_size - byte_size; iklam@7089: assert(remain_bytes >= 0, "sanity"); iklam@7089: old->_type = type; iklam@7089: iklam@7089: if (remain_bytes == 0) { iklam@7089: delete(rec); iklam@7089: } else { iklam@7089: address remain_ptr = address(ptr) + byte_size; iklam@7089: rec->_ptr = remain_ptr; iklam@7089: rec->_byte_size = remain_bytes; iklam@7089: rec->_type = MetaspaceObj::DeallocatedType; iklam@7089: rec->_next = old->_next; iklam@7089: old->_byte_size = byte_size; iklam@7089: old->_next = rec; iklam@7089: } iklam@7089: return; iklam@7089: } iklam@7089: } iklam@7089: assert(0, "reallocating a freed pointer that was not recorded"); iklam@5208: } iklam@5208: } iklam@5208: iklam@7089: void Metaspace::record_deallocation(void* ptr, size_t word_size) { iklam@7089: assert(DumpSharedSpaces, "sanity"); iklam@7089: iklam@7089: for (AllocRecord *rec = _alloc_record_head; rec; rec = rec->_next) { iklam@7089: if (rec->_ptr == ptr) { iklam@7089: assert(rec->_byte_size == (int)word_size * HeapWordSize, "sanity"); iklam@7089: rec->_type = MetaspaceObj::DeallocatedType; iklam@7089: return; iklam@7089: } iklam@7089: } iklam@7089: iklam@7089: assert(0, "deallocating a pointer that was not recorded"); iklam@7089: } iklam@7089: iklam@5208: void Metaspace::iterate(Metaspace::AllocRecordClosure *closure) { iklam@5208: assert(DumpSharedSpaces, "unimplemented for !DumpSharedSpaces"); iklam@5208: iklam@5208: address last_addr = (address)bottom(); iklam@5208: iklam@5208: for (AllocRecord *rec = _alloc_record_head; rec; rec = rec->_next) { iklam@5208: address ptr = rec->_ptr; iklam@5208: if (last_addr < ptr) { iklam@5208: closure->doit(last_addr, MetaspaceObj::UnknownType, ptr - last_addr); iklam@5208: } iklam@5208: closure->doit(ptr, rec->_type, rec->_byte_size); iklam@5208: last_addr = ptr + rec->_byte_size; iklam@5208: } iklam@5208: iklam@5208: address top = ((address)bottom()) + used_bytes_slow(Metaspace::NonClassType); iklam@5208: if (last_addr < top) { iklam@5208: closure->doit(last_addr, MetaspaceObj::UnknownType, top - last_addr); iklam@5208: } iklam@5208: } iklam@5208: stefank@5771: void Metaspace::purge(MetadataType mdtype) { stefank@5771: get_space_list(mdtype)->purge(get_chunk_manager(mdtype)); stefank@5771: } stefank@5771: jmasa@5007: void Metaspace::purge() { jmasa@5007: MutexLockerEx cl(SpaceManager::expand_lock(), jmasa@5007: Mutex::_no_safepoint_check_flag); stefank@5771: purge(NonClassType); hseigel@5528: if (using_class_space()) { stefank@5771: purge(ClassType); hseigel@5528: } jmasa@5007: } jmasa@5007: coleenp@4037: void Metaspace::print_on(outputStream* out) const { coleenp@4037: // Print both class virtual space counts and metaspace. coleenp@4037: if (Verbose) { hseigel@5528: vsm()->print_on(out); hseigel@5528: if (using_class_space()) { coleenp@4037: class_vsm()->print_on(out); hseigel@5528: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@6305: bool Metaspace::contains(const void* ptr) { coleenp@6678: if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(ptr)) { coleenp@6678: return true; coleenp@4037: } coleenp@6678: coleenp@6678: if (using_class_space() && get_space_list(ClassType)->contains(ptr)) { coleenp@6678: return true; coleenp@6678: } coleenp@6678: coleenp@6678: return get_space_list(NonClassType)->contains(ptr); coleenp@4037: } coleenp@4037: coleenp@4037: void Metaspace::verify() { coleenp@4037: vsm()->verify(); hseigel@5528: if (using_class_space()) { hseigel@5528: class_vsm()->verify(); hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: void Metaspace::dump(outputStream* const out) const { coleenp@4037: out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm()); coleenp@4037: vsm()->dump(out); hseigel@5528: if (using_class_space()) { hseigel@5528: out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm()); hseigel@5528: class_vsm()->dump(out); hseigel@5528: } coleenp@4037: } stefank@5704: stefank@5704: /////////////// Unit tests /////////////// stefank@5704: stefank@5704: #ifndef PRODUCT stefank@5704: brutisso@5774: class TestMetaspaceAuxTest : AllStatic { stefank@5704: public: stefank@5704: static void test_reserved() { stefank@5704: size_t reserved = MetaspaceAux::reserved_bytes(); stefank@5704: stefank@5704: assert(reserved > 0, "assert"); stefank@5704: stefank@5704: size_t committed = MetaspaceAux::committed_bytes(); stefank@5704: assert(committed <= reserved, "assert"); stefank@5704: stefank@5704: size_t reserved_metadata = MetaspaceAux::reserved_bytes(Metaspace::NonClassType); stefank@5704: assert(reserved_metadata > 0, "assert"); stefank@5704: assert(reserved_metadata <= reserved, "assert"); stefank@5704: stefank@5704: if (UseCompressedClassPointers) { stefank@5704: size_t reserved_class = MetaspaceAux::reserved_bytes(Metaspace::ClassType); stefank@5704: assert(reserved_class > 0, "assert"); stefank@5704: assert(reserved_class < reserved, "assert"); stefank@5704: } stefank@5704: } stefank@5704: stefank@5704: static void test_committed() { stefank@5704: size_t committed = MetaspaceAux::committed_bytes(); stefank@5704: stefank@5704: assert(committed > 0, "assert"); stefank@5704: stefank@5704: size_t reserved = MetaspaceAux::reserved_bytes(); stefank@5704: assert(committed <= reserved, "assert"); stefank@5704: stefank@5704: size_t committed_metadata = MetaspaceAux::committed_bytes(Metaspace::NonClassType); stefank@5704: assert(committed_metadata > 0, "assert"); stefank@5704: assert(committed_metadata <= committed, "assert"); stefank@5704: stefank@5704: if (UseCompressedClassPointers) { stefank@5704: size_t committed_class = MetaspaceAux::committed_bytes(Metaspace::ClassType); stefank@5704: assert(committed_class > 0, "assert"); stefank@5704: assert(committed_class < committed, "assert"); stefank@5704: } stefank@5704: } stefank@5704: brutisso@5774: static void test_virtual_space_list_large_chunk() { brutisso@5774: VirtualSpaceList* vs_list = new VirtualSpaceList(os::vm_allocation_granularity()); brutisso@5774: MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag); brutisso@5774: // A size larger than VirtualSpaceSize (256k) and add one page to make it _not_ be brutisso@5774: // vm_allocation_granularity aligned on Windows. brutisso@5774: size_t large_size = (size_t)(2*256*K + (os::vm_page_size()/BytesPerWord)); brutisso@5774: large_size += (os::vm_page_size()/BytesPerWord); brutisso@5774: vs_list->get_new_chunk(large_size, large_size, 0); brutisso@5774: } brutisso@5774: stefank@5704: static void test() { stefank@5704: test_reserved(); stefank@5704: test_committed(); brutisso@5774: test_virtual_space_list_large_chunk(); stefank@5704: } stefank@5704: }; stefank@5704: brutisso@5774: void TestMetaspaceAux_test() { brutisso@5774: TestMetaspaceAuxTest::test(); stefank@5704: } stefank@5704: mgerdin@6004: class TestVirtualSpaceNodeTest { mgerdin@6004: static void chunk_up(size_t words_left, size_t& num_medium_chunks, mgerdin@6004: size_t& num_small_chunks, mgerdin@6004: size_t& num_specialized_chunks) { mgerdin@6004: num_medium_chunks = words_left / MediumChunk; mgerdin@6004: words_left = words_left % MediumChunk; mgerdin@6004: mgerdin@6004: num_small_chunks = words_left / SmallChunk; mgerdin@6004: words_left = words_left % SmallChunk; mgerdin@6004: // how many specialized chunks can we get? mgerdin@6004: num_specialized_chunks = words_left / SpecializedChunk; mgerdin@6004: assert(words_left % SpecializedChunk == 0, "should be nothing left"); mgerdin@6004: } mgerdin@6004: mgerdin@6004: public: mgerdin@6004: static void test() { mgerdin@6004: MutexLockerEx ml(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag); mgerdin@6004: const size_t vsn_test_size_words = MediumChunk * 4; mgerdin@6004: const size_t vsn_test_size_bytes = vsn_test_size_words * BytesPerWord; mgerdin@6004: mgerdin@6004: // The chunk sizes must be multiples of eachother, or this will fail mgerdin@6004: STATIC_ASSERT(MediumChunk % SmallChunk == 0); mgerdin@6004: STATIC_ASSERT(SmallChunk % SpecializedChunk == 0); mgerdin@6004: mgerdin@6004: { // No committed memory in VSN mgerdin@6004: ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk); mgerdin@6004: VirtualSpaceNode vsn(vsn_test_size_bytes); mgerdin@6004: vsn.initialize(); mgerdin@6004: vsn.retire(&cm); mgerdin@6004: assert(cm.sum_free_chunks_count() == 0, "did not commit any memory in the VSN"); mgerdin@6004: } mgerdin@6004: mgerdin@6004: { // All of VSN is committed, half is used by chunks mgerdin@6004: ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk); mgerdin@6004: VirtualSpaceNode vsn(vsn_test_size_bytes); mgerdin@6004: vsn.initialize(); mgerdin@6004: vsn.expand_by(vsn_test_size_words, vsn_test_size_words); mgerdin@6004: vsn.get_chunk_vs(MediumChunk); mgerdin@6004: vsn.get_chunk_vs(MediumChunk); mgerdin@6004: vsn.retire(&cm); mgerdin@6004: assert(cm.sum_free_chunks_count() == 2, "should have been memory left for 2 medium chunks"); mgerdin@6004: assert(cm.sum_free_chunks() == 2*MediumChunk, "sizes should add up"); mgerdin@6004: } mgerdin@6004: mgerdin@6004: { // 4 pages of VSN is committed, some is used by chunks mgerdin@6004: ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk); mgerdin@6004: VirtualSpaceNode vsn(vsn_test_size_bytes); mgerdin@6004: const size_t page_chunks = 4 * (size_t)os::vm_page_size() / BytesPerWord; mgerdin@6004: assert(page_chunks < MediumChunk, "Test expects medium chunks to be at least 4*page_size"); mgerdin@6004: vsn.initialize(); mgerdin@6004: vsn.expand_by(page_chunks, page_chunks); mgerdin@6004: vsn.get_chunk_vs(SmallChunk); mgerdin@6004: vsn.get_chunk_vs(SpecializedChunk); mgerdin@6004: vsn.retire(&cm); mgerdin@6004: mgerdin@6004: // committed - used = words left to retire mgerdin@6004: const size_t words_left = page_chunks - SmallChunk - SpecializedChunk; mgerdin@6004: mgerdin@6004: size_t num_medium_chunks, num_small_chunks, num_spec_chunks; mgerdin@6004: chunk_up(words_left, num_medium_chunks, num_small_chunks, num_spec_chunks); mgerdin@6004: mgerdin@6004: assert(num_medium_chunks == 0, "should not get any medium chunks"); mgerdin@6004: assert(cm.sum_free_chunks_count() == (num_small_chunks + num_spec_chunks), "should be space for 3 chunks"); mgerdin@6004: assert(cm.sum_free_chunks() == words_left, "sizes should add up"); mgerdin@6004: } mgerdin@6004: mgerdin@6004: { // Half of VSN is committed, a humongous chunk is used mgerdin@6004: ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk); mgerdin@6004: VirtualSpaceNode vsn(vsn_test_size_bytes); mgerdin@6004: vsn.initialize(); mgerdin@6004: vsn.expand_by(MediumChunk * 2, MediumChunk * 2); mgerdin@6004: vsn.get_chunk_vs(MediumChunk + SpecializedChunk); // Humongous chunks will be aligned up to MediumChunk + SpecializedChunk mgerdin@6004: vsn.retire(&cm); mgerdin@6004: mgerdin@6004: const size_t words_left = MediumChunk * 2 - (MediumChunk + SpecializedChunk); mgerdin@6004: size_t num_medium_chunks, num_small_chunks, num_spec_chunks; mgerdin@6004: chunk_up(words_left, num_medium_chunks, num_small_chunks, num_spec_chunks); mgerdin@6004: mgerdin@6004: assert(num_medium_chunks == 0, "should not get any medium chunks"); mgerdin@6004: assert(cm.sum_free_chunks_count() == (num_small_chunks + num_spec_chunks), "should be space for 3 chunks"); mgerdin@6004: assert(cm.sum_free_chunks() == words_left, "sizes should add up"); mgerdin@6004: } mgerdin@6004: mgerdin@6004: } stefank@6170: stefank@6170: #define assert_is_available_positive(word_size) \ stefank@6170: assert(vsn.is_available(word_size), \ stefank@6170: err_msg(#word_size ": " PTR_FORMAT " bytes were not available in " \ stefank@6170: "VirtualSpaceNode [" PTR_FORMAT ", " PTR_FORMAT ")", \ stefank@6170: (uintptr_t)(word_size * BytesPerWord), vsn.bottom(), vsn.end())); stefank@6170: stefank@6170: #define assert_is_available_negative(word_size) \ stefank@6170: assert(!vsn.is_available(word_size), \ stefank@6170: err_msg(#word_size ": " PTR_FORMAT " bytes should not be available in " \ stefank@6170: "VirtualSpaceNode [" PTR_FORMAT ", " PTR_FORMAT ")", \ stefank@6170: (uintptr_t)(word_size * BytesPerWord), vsn.bottom(), vsn.end())); stefank@6170: stefank@6170: static void test_is_available_positive() { stefank@6170: // Reserve some memory. stefank@6170: VirtualSpaceNode vsn(os::vm_allocation_granularity()); stefank@6170: assert(vsn.initialize(), "Failed to setup VirtualSpaceNode"); stefank@6170: stefank@6170: // Commit some memory. stefank@6170: size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord; stefank@6170: bool expanded = vsn.expand_by(commit_word_size, commit_word_size); stefank@6170: assert(expanded, "Failed to commit"); stefank@6170: stefank@6170: // Check that is_available accepts the committed size. stefank@6170: assert_is_available_positive(commit_word_size); stefank@6170: stefank@6170: // Check that is_available accepts half the committed size. stefank@6170: size_t expand_word_size = commit_word_size / 2; stefank@6170: assert_is_available_positive(expand_word_size); stefank@6170: } stefank@6170: stefank@6170: static void test_is_available_negative() { stefank@6170: // Reserve some memory. stefank@6170: VirtualSpaceNode vsn(os::vm_allocation_granularity()); stefank@6170: assert(vsn.initialize(), "Failed to setup VirtualSpaceNode"); stefank@6170: stefank@6170: // Commit some memory. stefank@6170: size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord; stefank@6170: bool expanded = vsn.expand_by(commit_word_size, commit_word_size); stefank@6170: assert(expanded, "Failed to commit"); stefank@6170: stefank@6170: // Check that is_available doesn't accept a too large size. stefank@6170: size_t two_times_commit_word_size = commit_word_size * 2; stefank@6170: assert_is_available_negative(two_times_commit_word_size); stefank@6170: } stefank@6170: stefank@6170: static void test_is_available_overflow() { stefank@6170: // Reserve some memory. stefank@6170: VirtualSpaceNode vsn(os::vm_allocation_granularity()); stefank@6170: assert(vsn.initialize(), "Failed to setup VirtualSpaceNode"); stefank@6170: stefank@6170: // Commit some memory. stefank@6170: size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord; stefank@6170: bool expanded = vsn.expand_by(commit_word_size, commit_word_size); stefank@6170: assert(expanded, "Failed to commit"); stefank@6170: stefank@6170: // Calculate a size that will overflow the virtual space size. stefank@6170: void* virtual_space_max = (void*)(uintptr_t)-1; stefank@6170: size_t bottom_to_max = pointer_delta(virtual_space_max, vsn.bottom(), 1); stefank@6170: size_t overflow_size = bottom_to_max + BytesPerWord; stefank@6170: size_t overflow_word_size = overflow_size / BytesPerWord; stefank@6170: stefank@6170: // Check that is_available can handle the overflow. stefank@6170: assert_is_available_negative(overflow_word_size); stefank@6170: } stefank@6170: stefank@6170: static void test_is_available() { stefank@6170: TestVirtualSpaceNodeTest::test_is_available_positive(); stefank@6170: TestVirtualSpaceNodeTest::test_is_available_negative(); stefank@6170: TestVirtualSpaceNodeTest::test_is_available_overflow(); stefank@6170: } mgerdin@6004: }; mgerdin@6004: mgerdin@6004: void TestVirtualSpaceNode_test() { mgerdin@6004: TestVirtualSpaceNodeTest::test(); stefank@6170: TestVirtualSpaceNodeTest::test_is_available(); mgerdin@6004: } stefank@5704: #endif