coleenp@4037: /* coleenp@4037: * Copyright (c) 2011, 2012, 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" 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" jmasa@4196: #include "memory/metablock.hpp" jmasa@4196: #include "memory/metachunk.hpp" coleenp@4037: #include "memory/metaspace.hpp" coleenp@4037: #include "memory/metaspaceShared.hpp" coleenp@4037: #include "memory/resourceArea.hpp" coleenp@4037: #include "memory/universe.hpp" coleenp@4037: #include "runtime/globals.hpp" coleenp@4037: #include "runtime/mutex.hpp" coleenp@4037: #include "services/memTracker.hpp" coleenp@4037: #include "utilities/copy.hpp" coleenp@4037: #include "utilities/debug.hpp" coleenp@4037: jmasa@4196: typedef BinaryTreeDictionary BlockTreeDictionary; jmasa@4196: typedef BinaryTreeDictionary ChunkTreeDictionary; mgerdin@4264: // Define this macro to enable slow integrity checking of mgerdin@4264: // the free chunk lists mgerdin@4264: const bool metaspace_slow_verify = false; mgerdin@4264: coleenp@4037: coleenp@4037: // Parameters for stress mode testing coleenp@4037: const uint metadata_deallocate_a_lot_block = 10; coleenp@4037: const uint metadata_deallocate_a_lock_chunk = 3; coleenp@4037: size_t const allocation_from_dictionary_limit = 64 * K; coleenp@4037: const size_t metadata_chunk_initialize = 0xf7f7f7f7; coleenp@4037: const size_t metadata_deallocate = 0xf5f5f5f5; coleenp@4037: coleenp@4037: MetaWord* last_allocated = 0; coleenp@4037: coleenp@4037: // Used in declarations in SpaceManager and ChunkManager coleenp@4037: enum ChunkIndex { coleenp@4037: SmallIndex = 0, coleenp@4037: MediumIndex = 1, coleenp@4037: HumongousIndex = 2, jmasa@4196: NumberOfFreeLists = 2, jmasa@4196: NumberOfInUseLists = 3 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: coleenp@4037: // Originally _capacity_until_GC was set to MetaspaceSize here but coleenp@4037: // the default MetaspaceSize before argument processing was being coleenp@4037: // used which was not the desired value. See the code coleenp@4037: // in should_expand() to see how the initialization is handled coleenp@4037: // now. coleenp@4037: size_t MetaspaceGC::_capacity_until_GC = 0; coleenp@4037: bool MetaspaceGC::_expand_after_GC = false; coleenp@4037: uint MetaspaceGC::_shrink_factor = 0; coleenp@4037: bool MetaspaceGC::_should_concurrent_collect = false; coleenp@4037: coleenp@4037: // Blocks of space for metadata are allocated out of Metachunks. coleenp@4037: // coleenp@4037: // Metachunk are allocated out of MetadataVirtualspaces and once coleenp@4037: // allocated there is no explicit link between a Metachunk and coleenp@4037: // the MetadataVirtualspaces from which it was allocated. coleenp@4037: // coleenp@4037: // Each SpaceManager maintains a coleenp@4037: // list of the chunks it is using and the current chunk. The current coleenp@4037: // chunk is the chunk from which allocations are done. Space freed in coleenp@4037: // a chunk is placed on the free list of blocks (BlockFreelist) and coleenp@4037: // reused from there. coleenp@4037: // coleenp@4037: // Future modification coleenp@4037: // coleenp@4037: // The Metachunk can conceivable be replaced by the Chunk in coleenp@4037: // allocation.hpp. Note that the latter Chunk is the space for coleenp@4037: // allocation (allocations from the chunk are out of the space in coleenp@4037: // the Chunk after the header for the Chunk) where as Metachunks coleenp@4037: // point to space in a VirtualSpace. To replace Metachunks with coleenp@4037: // Chunks, change Chunks so that they can be allocated out of a VirtualSpace. jmasa@4196: size_t Metablock::_min_block_byte_size = sizeof(Metablock); coleenp@4037: #ifdef ASSERT jmasa@4196: size_t Metablock::_overhead = jmasa@4196: Chunk::aligned_overhead_size(sizeof(Metablock)) / BytesPerWord; jmasa@4196: #else jmasa@4196: size_t Metablock::_overhead = 0; coleenp@4037: #endif coleenp@4037: coleenp@4037: // Pointer to list of Metachunks. coleenp@4037: class ChunkList VALUE_OBJ_CLASS_SPEC { coleenp@4037: // List of free chunks coleenp@4037: Metachunk* _head; coleenp@4037: coleenp@4037: public: coleenp@4037: // Constructor coleenp@4037: ChunkList() : _head(NULL) {} coleenp@4037: coleenp@4037: // Accessors coleenp@4037: Metachunk* head() { return _head; } coleenp@4037: void set_head(Metachunk* v) { _head = v; } coleenp@4037: coleenp@4037: // Link at head of the list coleenp@4037: void add_at_head(Metachunk* head, Metachunk* tail); coleenp@4037: void add_at_head(Metachunk* head); coleenp@4037: coleenp@4037: size_t sum_list_size(); coleenp@4037: size_t sum_list_count(); coleenp@4037: size_t sum_list_capacity(); coleenp@4037: }; coleenp@4037: coleenp@4037: // Manages the global free lists of chunks. coleenp@4037: // Has three lists of free chunks, and a total size and coleenp@4037: // count that includes all three coleenp@4037: coleenp@4037: class ChunkManager VALUE_OBJ_CLASS_SPEC { coleenp@4037: coleenp@4037: // Free list of chunks of different sizes. 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: coleenp@4037: ChunkManager() : _free_chunks_total(0), _free_chunks_count(0) {} 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: void chunk_freelist_deallocate(Metachunk* chunk); coleenp@4037: coleenp@4037: // Total of the space in the free chunks list coleenp@4037: size_t free_chunks_total(); coleenp@4037: size_t free_chunks_total_in_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: } coleenp@4037: ChunkList* free_medium_chunks() { return &_free_chunks[1]; } coleenp@4037: ChunkList* free_small_chunks() { return &_free_chunks[0]; } 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: coleenp@4037: // Add and remove from a list by size. Selects coleenp@4037: // list based on size of chunk. coleenp@4037: void free_chunks_put(Metachunk* chuck); coleenp@4037: Metachunk* free_chunks_get(size_t chunk_word_size); coleenp@4037: 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: jmasa@4196: void print_on(outputStream* st); coleenp@4037: }; 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: static Metablock* initialize_free_chunk(MetaWord* p, size_t word_size); jmasa@4196: 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: 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; coleenp@4037: coleenp@4037: // Convenience functions for logical bottom and end coleenp@4037: MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); } coleenp@4037: MetaWord* end() const { return (MetaWord*) _virtual_space.high(); } 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: coleenp@4037: public: coleenp@4037: coleenp@4037: VirtualSpaceNode(size_t byte_size); coleenp@4037: VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs) {} coleenp@4037: ~VirtualSpaceNode(); coleenp@4037: 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: coleenp@4037: // Returns true if "word_size" is available in the virtual space coleenp@4037: bool is_available(size_t word_size) { return _top + word_size <= end(); } coleenp@4037: coleenp@4037: MetaWord* top() const { return _top; } coleenp@4037: void inc_top(size_t word_size) { _top += word_size; } coleenp@4037: 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: Metachunk* get_chunk_vs_with_expand(size_t chunk_word_size); coleenp@4037: coleenp@4037: // Expands/shrinks the committed space in a virtual space. Delegates coleenp@4037: // to Virtualspace coleenp@4037: bool expand_by(size_t words, bool pre_touch = false); coleenp@4037: bool shrink_by(size_t words); coleenp@4037: coleenp@4304: #ifdef ASSERT coleenp@4037: // Debug support coleenp@4037: static void verify_virtual_space_total(); coleenp@4037: static void verify_virtual_space_count(); coleenp@4037: void mangle(); coleenp@4304: #endif coleenp@4037: coleenp@4037: void print_on(outputStream* st) const; coleenp@4037: }; coleenp@4037: coleenp@4037: // byte_size is the size of the associated virtualspace. coleenp@4037: VirtualSpaceNode::VirtualSpaceNode(size_t byte_size) : _top(NULL), _next(NULL), _rs(0) { coleenp@4037: // This allocates memory with mmap. For DumpSharedspaces, allocate the coleenp@4037: // space at low memory so that other shared images don't conflict. coleenp@4037: // This is the same address as memory needed for UseCompressedOops but coleenp@4037: // compressed oops don't work with CDS (offsets in metadata are wrong), so coleenp@4037: // borrow the same address. coleenp@4037: if (DumpSharedSpaces) { coleenp@4037: char* shared_base = (char*)HeapBaseMinAddress; coleenp@4037: _rs = ReservedSpace(byte_size, 0, false, shared_base, 0); coleenp@4037: if (_rs.is_reserved()) { coleenp@4037: assert(_rs.base() == shared_base, "should match"); coleenp@4037: } else { coleenp@4037: // If we are dumping the heap, then allocate a wasted block of address coleenp@4037: // space in order to push the heap to a lower address. This extra coleenp@4037: // address range allows for other (or larger) libraries to be loaded coleenp@4037: // without them occupying the space required for the shared spaces. coleenp@4037: uintx reserved = 0; coleenp@4037: uintx block_size = 64*1024*1024; coleenp@4037: while (reserved < SharedDummyBlockSize) { coleenp@4037: char* dummy = os::reserve_memory(block_size); coleenp@4037: reserved += block_size; coleenp@4037: } coleenp@4037: _rs = ReservedSpace(byte_size); coleenp@4037: } coleenp@4037: MetaspaceShared::set_shared_rs(&_rs); coleenp@4037: } else { coleenp@4037: _rs = ReservedSpace(byte_size); coleenp@4037: } coleenp@4037: coleenp@4037: MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass); coleenp@4037: } coleenp@4037: coleenp@4037: // List of VirtualSpaces for metadata allocation. coleenp@4037: // It has a _next link for singly linked list and a MemRegion coleenp@4037: // for total space in the VirtualSpace. 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: // Global list of virtual spaces 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: // Free chunk list for all other metadata coleenp@4037: ChunkManager _chunk_manager; coleenp@4037: coleenp@4037: // Can this virtual list allocate >1 spaces? Also, used to determine coleenp@4037: // whether to allocate unlimited small chunks in this virtual space coleenp@4037: bool _is_class; coleenp@4037: bool can_grow() const { return !is_class() || !UseCompressedKlassPointers; } coleenp@4037: coleenp@4037: // Sum of space in all virtual spaces and number of virtual spaces coleenp@4037: size_t _virtual_space_total; 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: coleenp@4037: void link_vs(VirtualSpaceNode* new_entry, size_t vs_word_size); 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. coleenp@4037: bool grow_vs(size_t vs_word_size); coleenp@4037: coleenp@4037: public: coleenp@4037: VirtualSpaceList(size_t word_size); coleenp@4037: VirtualSpaceList(ReservedSpace rs); coleenp@4037: coleenp@4037: Metachunk* get_new_chunk(size_t word_size, size_t grow_chunks_by_words); coleenp@4037: coleenp@4037: VirtualSpaceNode* current_virtual_space() { coleenp@4037: return _current_virtual_space; coleenp@4037: } coleenp@4037: coleenp@4037: ChunkManager* chunk_manager() { return &_chunk_manager; } coleenp@4037: bool is_class() const { return _is_class; } coleenp@4037: coleenp@4037: // Allocate the first virtualspace. coleenp@4037: void initialize(size_t word_size); coleenp@4037: coleenp@4037: size_t virtual_space_total() { return _virtual_space_total; } coleenp@4037: void inc_virtual_space_total(size_t v) { coleenp@4037: Atomic::add_ptr(v, &_virtual_space_total); coleenp@4037: } coleenp@4037: coleenp@4037: size_t virtual_space_count() { return _virtual_space_count; } coleenp@4037: void inc_virtual_space_count() { coleenp@4037: Atomic::inc_ptr(&_virtual_space_count); coleenp@4037: } coleenp@4037: coleenp@4037: // Used and capacity in the entire list of virtual spaces. coleenp@4037: // These are global values shared by all Metaspaces coleenp@4037: size_t capacity_words_sum(); coleenp@4037: size_t capacity_bytes_sum() { return capacity_words_sum() * BytesPerWord; } coleenp@4037: size_t used_words_sum(); coleenp@4037: size_t used_bytes_sum() { return used_words_sum() * BytesPerWord; } coleenp@4037: coleenp@4037: bool contains(const void *ptr); 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 _deallocate_block_a_lot_count; coleenp@4037: static int _deallocate_chunk_a_lot_count; coleenp@4037: static int _allocation_fail_alot_count; coleenp@4037: coleenp@4037: public: coleenp@4037: static int deallocate_block_a_lot_count() { coleenp@4037: return _deallocate_block_a_lot_count; coleenp@4037: } coleenp@4037: static void set_deallocate_block_a_lot_count(int v) { coleenp@4037: _deallocate_block_a_lot_count = v; coleenp@4037: } coleenp@4037: static void inc_deallocate_block_a_lot_count() { coleenp@4037: _deallocate_block_a_lot_count++; coleenp@4037: } coleenp@4037: static int deallocate_chunk_a_lot_count() { coleenp@4037: return _deallocate_chunk_a_lot_count; coleenp@4037: } coleenp@4037: static void reset_deallocate_chunk_a_lot_count() { coleenp@4037: _deallocate_chunk_a_lot_count = 1; coleenp@4037: } coleenp@4037: static void inc_deallocate_chunk_a_lot_count() { coleenp@4037: _deallocate_chunk_a_lot_count++; coleenp@4037: } 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: static void deallocate_chunk_a_lot(SpaceManager* sm, coleenp@4037: size_t chunk_word_size); coleenp@4037: static void deallocate_block_a_lot(SpaceManager* sm, coleenp@4037: size_t chunk_word_size); coleenp@4037: coleenp@4037: }; coleenp@4037: coleenp@4037: int Metadebug::_deallocate_block_a_lot_count = 0; coleenp@4037: int Metadebug::_deallocate_chunk_a_lot_count = 0; 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: coleenp@4037: // protects allocations and contains. coleenp@4037: Mutex* const _lock; coleenp@4037: 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: coleenp@4037: // Virtual space where allocation comes from. coleenp@4037: VirtualSpaceList* _vs_list; coleenp@4037: coleenp@4037: // Number of small chunks to allocate to a manager coleenp@4037: // If class space manager, small chunks are unlimited coleenp@4037: static uint const _small_chunk_limit; coleenp@4037: bool has_small_chunk_limit() { return !vs_list()->is_class(); } coleenp@4037: coleenp@4037: // Sum of all space in allocated chunks coleenp@4037: size_t _allocation_total; 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: coleenp@4037: // Accessors coleenp@4037: Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; } coleenp@4037: void set_chunks_in_use(ChunkIndex index, Metachunk* v) { _chunks_in_use[index] = v; } coleenp@4037: coleenp@4037: BlockFreelist* block_freelists() const { coleenp@4037: return (BlockFreelist*) &_block_freelists; coleenp@4037: } coleenp@4037: coleenp@4037: VirtualSpaceList* vs_list() const { return _vs_list; } 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); coleenp@4037: coleenp@4037: Mutex* lock() const { return _lock; } coleenp@4037: coleenp@4037: public: coleenp@4037: SpaceManager(Mutex* lock, VirtualSpaceList* vs_list); coleenp@4037: ~SpaceManager(); coleenp@4037: coleenp@4037: enum ChunkSizes { // in words. coleenp@4037: SmallChunk = 512, coleenp@4037: MediumChunk = 8 * K, coleenp@4037: MediumChunkBunch = 4 * MediumChunk coleenp@4037: }; coleenp@4037: coleenp@4037: // Accessors coleenp@4037: size_t allocation_total() const { return _allocation_total; } coleenp@4037: void inc_allocation_total(size_t v) { Atomic::add_ptr(v, &_allocation_total); } coleenp@4037: static bool is_humongous(size_t word_size) { return word_size > MediumChunk; } coleenp@4037: coleenp@4037: static Mutex* expand_lock() { return _expand_lock; } coleenp@4037: 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: 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: 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(); coleenp@4304: NOT_PRODUCT(void mangle_freed_chunks();) coleenp@4037: #ifdef ASSERT coleenp@4037: void verify_allocation_total(); coleenp@4037: #endif coleenp@4037: }; coleenp@4037: coleenp@4037: uint const SpaceManager::_small_chunk_limit = 4; coleenp@4037: jmasa@4196: jmasa@4196: 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: coleenp@4037: size_t Metachunk::_overhead = coleenp@4037: Chunk::aligned_overhead_size(sizeof(Metachunk)) / BytesPerWord; coleenp@4037: coleenp@4037: // New blocks returned by the Metaspace are zero initialized. coleenp@4037: // We should fix the constructors to not assume this instead. coleenp@4037: Metablock* Metablock::initialize(MetaWord* p, size_t word_size) { jmasa@4196: if (p == NULL) { jmasa@4196: return NULL; jmasa@4196: } jmasa@4196: coleenp@4037: Metablock* result = (Metablock*) p; coleenp@4037: coleenp@4037: // Clear the memory coleenp@4037: Copy::fill_to_aligned_words((HeapWord*)result, word_size); coleenp@4037: #ifdef ASSERT coleenp@4037: result->set_word_size(word_size); coleenp@4037: #endif coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: // Metachunk methods coleenp@4037: coleenp@4037: Metachunk* Metachunk::initialize(MetaWord* ptr, size_t word_size) { coleenp@4037: // Set bottom, top, and end. Allow space for the Metachunk itself coleenp@4037: Metachunk* chunk = (Metachunk*) ptr; coleenp@4037: coleenp@4037: MetaWord* chunk_bottom = ptr + _overhead; coleenp@4037: chunk->set_bottom(ptr); coleenp@4037: chunk->set_top(chunk_bottom); coleenp@4037: MetaWord* chunk_end = ptr + word_size; coleenp@4037: assert(chunk_end > chunk_bottom, "Chunk must be too small"); coleenp@4037: chunk->set_end(chunk_end); coleenp@4037: chunk->set_next(NULL); coleenp@4037: chunk->set_word_size(word_size); coleenp@4037: #ifdef ASSERT coleenp@4037: size_t data_word_size = pointer_delta(chunk_end, chunk_bottom, sizeof(MetaWord)); coleenp@4037: Copy::fill_to_words((HeapWord*) chunk_bottom, data_word_size, metadata_chunk_initialize); coleenp@4037: #endif coleenp@4037: return chunk; coleenp@4037: } coleenp@4037: coleenp@4037: jmasa@4196: MetaWord* Metachunk::allocate(size_t word_size) { jmasa@4196: MetaWord* result = NULL; coleenp@4037: // If available, bump the pointer to allocate. coleenp@4037: if (free_word_size() >= word_size) { jmasa@4196: result = _top; coleenp@4037: _top = _top + word_size; coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: // _bottom points to the start of the chunk including the overhead. coleenp@4037: size_t Metachunk::used_word_size() { coleenp@4037: return pointer_delta(_top, _bottom, sizeof(MetaWord)); coleenp@4037: } coleenp@4037: coleenp@4037: size_t Metachunk::free_word_size() { coleenp@4037: return pointer_delta(_end, _top, sizeof(MetaWord)); coleenp@4037: } coleenp@4037: coleenp@4037: size_t Metachunk::capacity_word_size() { coleenp@4037: return pointer_delta(_end, _bottom, sizeof(MetaWord)); coleenp@4037: } coleenp@4037: coleenp@4037: void Metachunk::print_on(outputStream* st) const { coleenp@4037: st->print_cr("Metachunk:" coleenp@4037: " bottom " PTR_FORMAT " top " PTR_FORMAT coleenp@4037: " end " PTR_FORMAT " size " SIZE_FORMAT, coleenp@4037: bottom(), top(), end(), word_size()); coleenp@4037: } coleenp@4037: coleenp@4304: #ifndef PRODUCT jmasa@4196: void Metachunk::mangle() { jmasa@4196: // Mangle the payload of the chunk and not the links that jmasa@4196: // maintain list of chunks. jmasa@4196: HeapWord* start = (HeapWord*)(bottom() + overhead()); jmasa@4196: size_t word_size = capacity_word_size() - overhead(); jmasa@4196: Copy::fill_to_words(start, word_size, metadata_chunk_initialize); jmasa@4196: } coleenp@4304: #endif // PRODUCT coleenp@4037: coleenp@4037: void Metachunk::verify() { coleenp@4037: #ifdef ASSERT coleenp@4037: // Cannot walk through the blocks unless the blocks have coleenp@4037: // headers with sizes. jmasa@4196: assert(_bottom <= _top && jmasa@4196: _top <= _end, jmasa@4196: "Chunk has been smashed"); jmasa@4196: assert(SpaceManager::is_humongous(_word_size) || jmasa@4196: _word_size == SpaceManager::MediumChunk || jmasa@4196: _word_size == SpaceManager::SmallChunk, jmasa@4196: "Chunk size is wrong"); coleenp@4037: #endif coleenp@4037: return; coleenp@4037: } coleenp@4037: 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: Metablock* BlockFreelist::initialize_free_chunk(MetaWord* p, size_t word_size) { jmasa@4196: Metablock* block = (Metablock*) p; jmasa@4196: block->set_word_size(word_size); jmasa@4196: block->set_prev(NULL); jmasa@4196: block->set_next(NULL); jmasa@4196: coleenp@4037: return block; coleenp@4037: } coleenp@4037: jmasa@4196: void BlockFreelist::return_block(MetaWord* p, size_t word_size) { jmasa@4196: Metablock* free_chunk = initialize_free_chunk(p, 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: jmasa@4196: 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 = jmasa@4196: dictionary()->get_chunk(word_size, FreeBlockDictionary::exactly); jmasa@4196: if (free_block == NULL) { jmasa@4196: return NULL; jmasa@4196: } jmasa@4196: jmasa@4196: return (MetaWord*) free_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(); 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: 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: coleenp@4037: if (!is_available(chunk_word_size)) { coleenp@4037: if (TraceMetadataChunkAllocation) { coleenp@4037: 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 coleenp@4037: print_on(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: coleenp@4037: // Point the chunk at the space coleenp@4037: Metachunk* result = Metachunk::initialize(chunk_limit, chunk_word_size); coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: // Expand the virtual space (commit more of the reserved space) coleenp@4037: bool VirtualSpaceNode::expand_by(size_t words, bool pre_touch) { coleenp@4037: size_t bytes = words * BytesPerWord; coleenp@4037: bool result = virtual_space()->expand_by(bytes, pre_touch); coleenp@4037: if (TraceMetavirtualspaceAllocation && !result) { coleenp@4037: gclog_or_tty->print_cr("VirtualSpaceNode::expand_by() failed " coleenp@4037: "for byte size " SIZE_FORMAT, bytes); coleenp@4037: virtual_space()->print(); coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: // Shrink the virtual space (commit more of the reserved space) coleenp@4037: bool VirtualSpaceNode::shrink_by(size_t words) { coleenp@4037: size_t bytes = words * BytesPerWord; coleenp@4037: virtual_space()->shrink_by(bytes); coleenp@4037: return true; coleenp@4037: } coleenp@4037: coleenp@4037: // Add another chunk to the chunk list. coleenp@4037: coleenp@4037: Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: Metachunk* result = NULL; coleenp@4037: coleenp@4037: return take_from_committed(chunk_word_size); coleenp@4037: } coleenp@4037: coleenp@4037: Metachunk* VirtualSpaceNode::get_chunk_vs_with_expand(size_t chunk_word_size) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: coleenp@4037: Metachunk* new_chunk = get_chunk_vs(chunk_word_size); coleenp@4037: coleenp@4037: if (new_chunk == NULL) { coleenp@4037: // Only a small part of the virtualspace is committed when first coleenp@4037: // allocated so committing more here can be expected. coleenp@4037: size_t page_size_words = os::vm_page_size() / BytesPerWord; coleenp@4037: size_t aligned_expand_vs_by_words = align_size_up(chunk_word_size, coleenp@4037: page_size_words); coleenp@4037: expand_by(aligned_expand_vs_by_words, false); coleenp@4037: new_chunk = get_chunk_vs(chunk_word_size); coleenp@4037: } coleenp@4037: return new_chunk; 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: coleenp@4037: // Commit only 1 page instead of the whole reserved space _rs.size() coleenp@4037: size_t committed_byte_size = os::vm_page_size(); coleenp@4037: bool result = virtual_space()->initialize(_rs, committed_byte_size); coleenp@4037: if (result) { 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 ")", coleenp@4037: vs, capacity / K, 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: coleenp@4037: size_t VirtualSpaceList::used_words_sum() { coleenp@4037: size_t allocated_by_vs = 0; coleenp@4037: VirtualSpaceListIterator iter(virtual_space_list()); coleenp@4037: while (iter.repeat()) { coleenp@4037: VirtualSpaceNode* vsl = iter.get_next(); coleenp@4037: // Sum used region [bottom, top) in each virtualspace coleenp@4037: allocated_by_vs += vsl->used_words_in_vs(); coleenp@4037: } coleenp@4037: assert(allocated_by_vs >= chunk_manager()->free_chunks_total(), coleenp@4037: err_msg("Total in free chunks " SIZE_FORMAT coleenp@4037: " greater than total from virtual_spaces " SIZE_FORMAT, coleenp@4037: allocated_by_vs, chunk_manager()->free_chunks_total())); coleenp@4037: size_t used = coleenp@4037: allocated_by_vs - chunk_manager()->free_chunks_total(); coleenp@4037: return used; coleenp@4037: } coleenp@4037: coleenp@4037: // Space available in all MetadataVirtualspaces allocated coleenp@4037: // for metadata. This is the upper limit on the capacity coleenp@4037: // of chunks allocated out of all the MetadataVirtualspaces. coleenp@4037: size_t VirtualSpaceList::capacity_words_sum() { coleenp@4037: size_t capacity = 0; coleenp@4037: VirtualSpaceListIterator iter(virtual_space_list()); coleenp@4037: while (iter.repeat()) { coleenp@4037: VirtualSpaceNode* vsl = iter.get_next(); coleenp@4037: capacity += vsl->capacity_words_in_vs(); coleenp@4037: } coleenp@4037: return capacity; coleenp@4037: } coleenp@4037: coleenp@4037: VirtualSpaceList::VirtualSpaceList(size_t word_size ) : coleenp@4037: _is_class(false), coleenp@4037: _virtual_space_list(NULL), coleenp@4037: _current_virtual_space(NULL), coleenp@4037: _virtual_space_total(0), coleenp@4037: _virtual_space_count(0) { coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: bool initialization_succeeded = grow_vs(word_size); coleenp@4037: coleenp@4037: assert(initialization_succeeded, coleenp@4037: " VirtualSpaceList initialization should not fail"); 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), coleenp@4037: _virtual_space_total(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(); coleenp@4037: assert(succeeded, " VirtualSpaceList initialization should not fail"); coleenp@4037: link_vs(class_entry, rs.size()/BytesPerWord); coleenp@4037: } coleenp@4037: coleenp@4037: // Allocate another meta virtual space and add it to the list. coleenp@4037: bool VirtualSpaceList::grow_vs(size_t vs_word_size) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: if (vs_word_size == 0) { coleenp@4037: return false; coleenp@4037: } coleenp@4037: // Reserve the space coleenp@4037: size_t vs_byte_size = vs_word_size * BytesPerWord; coleenp@4037: assert(vs_byte_size % os::vm_page_size() == 0, "Not aligned"); 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 { coleenp@4037: link_vs(new_entry, vs_word_size); coleenp@4037: return true; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry, size_t vs_word_size) { 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); coleenp@4037: inc_virtual_space_total(vs_word_size); 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(); coleenp@4037: vsl->print_on(tty); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size, coleenp@4037: size_t grow_chunks_by_words) { coleenp@4037: coleenp@4037: // Get a chunk from the chunk freelist coleenp@4037: Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words); coleenp@4037: coleenp@4037: // Allocate a chunk out of the current virtual space. coleenp@4037: if (next == NULL) { coleenp@4037: next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words); coleenp@4037: } coleenp@4037: coleenp@4037: if (next == NULL) { coleenp@4037: // Not enough room in current virtual space. Try to commit coleenp@4037: // more space. coleenp@4037: size_t expand_vs_by_words = MAX2((size_t)SpaceManager::MediumChunkBunch, coleenp@4037: grow_chunks_by_words); coleenp@4037: size_t page_size_words = os::vm_page_size() / BytesPerWord; coleenp@4037: size_t aligned_expand_vs_by_words = align_size_up(expand_vs_by_words, coleenp@4037: page_size_words); coleenp@4037: bool vs_expanded = coleenp@4037: current_virtual_space()->expand_by(aligned_expand_vs_by_words, false); coleenp@4037: if (!vs_expanded) { coleenp@4037: // Should the capacity of the metaspaces be expanded for coleenp@4037: // this allocation? If it's the virtual space for classes and is coleenp@4037: // being used for CompressedHeaders, don't allocate a new virtualspace. coleenp@4037: if (can_grow() && MetaspaceGC::should_expand(this, word_size)) { coleenp@4037: // Get another virtual space. coleenp@4037: size_t grow_vs_words = coleenp@4037: MAX2((size_t)VirtualSpaceSize, aligned_expand_vs_by_words); coleenp@4037: if (grow_vs(grow_vs_words)) { coleenp@4037: // Got it. It's on the list now. Get a chunk from it. coleenp@4037: next = current_virtual_space()->get_chunk_vs_with_expand(grow_chunks_by_words); coleenp@4037: } coleenp@4037: if (TraceMetadataHumongousAllocation && SpaceManager::is_humongous(word_size)) { coleenp@4037: gclog_or_tty->print_cr(" aligned_expand_vs_by_words " PTR_FORMAT, coleenp@4037: aligned_expand_vs_by_words); coleenp@4037: gclog_or_tty->print_cr(" grow_vs_words " PTR_FORMAT, coleenp@4037: grow_vs_words); coleenp@4037: } coleenp@4037: } else { coleenp@4037: // Allocation will fail and induce a GC coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print_cr("VirtualSpaceList::get_new_chunk():" coleenp@4037: " Fail instead of expand the metaspace"); coleenp@4037: } coleenp@4037: } coleenp@4037: } else { coleenp@4037: // The virtual space expanded, get a new chunk coleenp@4037: next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words); coleenp@4037: assert(next != NULL, "Just expanded, should succeed"); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: return next; coleenp@4037: } coleenp@4037: 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: #ifndef PRODUCT coleenp@4037: bool VirtualSpaceList::contains(const void *ptr) { coleenp@4037: VirtualSpaceNode* list = virtual_space_list(); coleenp@4037: VirtualSpaceListIterator iter(list); coleenp@4037: while (iter.repeat()) { coleenp@4037: VirtualSpaceNode* node = iter.get_next(); coleenp@4037: if (node->reserved()->contains(ptr)) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: } coleenp@4037: return false; coleenp@4037: } coleenp@4037: #endif // PRODUCT 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 coleenp@4037: // is based on the flags MinHeapFreeRatio and MaxHeapFreeRatio used coleenp@4037: // to resize the Java heap by some GC's. New flags can be implemented coleenp@4037: // if really needed. MinHeapFreeRatio is used to calculate how much coleenp@4037: // free space is desirable in the metaspace capacity to decide how much coleenp@4037: // to increase the HWM. MaxHeapFreeRatio 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 coleenp@4037: // enough to satisfy the allocation (i.e. big enough for a word_size coleenp@4037: // allocation), increase by MaxMetaspaceExpansion. If that is still coleenp@4037: // not enough, expand by the size of the allocation (word_size) plus coleenp@4037: // some. coleenp@4037: size_t MetaspaceGC::delta_capacity_until_GC(size_t word_size) { coleenp@4037: size_t before_inc = MetaspaceGC::capacity_until_GC(); coleenp@4037: size_t min_delta_words = MinMetaspaceExpansion / BytesPerWord; coleenp@4037: size_t max_delta_words = MaxMetaspaceExpansion / BytesPerWord; coleenp@4037: size_t page_size_words = os::vm_page_size() / BytesPerWord; coleenp@4037: size_t size_delta_words = align_size_up(word_size, page_size_words); coleenp@4037: size_t delta_words = MAX2(size_delta_words, min_delta_words); coleenp@4037: if (delta_words > min_delta_words) { 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. coleenp@4037: delta_words = MAX2(delta_words, max_delta_words); coleenp@4037: if (delta_words > max_delta_words) { coleenp@4037: // This allocation is large but the next ones are probably not coleenp@4037: // so increase by the minimum. coleenp@4037: delta_words = delta_words + min_delta_words; coleenp@4037: } coleenp@4037: } coleenp@4037: return delta_words; coleenp@4037: } coleenp@4037: coleenp@4037: bool MetaspaceGC::should_expand(VirtualSpaceList* vsl, size_t word_size) { coleenp@4037: coleenp@4037: // Class virtual space should always be expanded. Call GC for the other coleenp@4037: // metadata virtual space. coleenp@4037: if (vsl == Metaspace::class_space_list()) return true; coleenp@4037: coleenp@4037: // If the user wants a limit, impose one. coleenp@4037: size_t max_metaspace_size_words = MaxMetaspaceSize / BytesPerWord; coleenp@4037: size_t metaspace_size_words = MetaspaceSize / BytesPerWord; coleenp@4037: if (!FLAG_IS_DEFAULT(MaxMetaspaceSize) && coleenp@4037: vsl->capacity_words_sum() >= max_metaspace_size_words) { coleenp@4037: return false; coleenp@4037: } coleenp@4037: coleenp@4037: // If this is part of an allocation after a GC, expand coleenp@4037: // unconditionally. coleenp@4037: if(MetaspaceGC::expand_after_GC()) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: coleenp@4037: // If the capacity is below the minimum capacity, allow the coleenp@4037: // expansion. Also set the high-water-mark (capacity_until_GC) coleenp@4037: // to that minimum capacity so that a GC will not be induced coleenp@4037: // until that minimum capacity is exceeded. coleenp@4037: if (vsl->capacity_words_sum() < metaspace_size_words || coleenp@4037: capacity_until_GC() == 0) { coleenp@4037: set_capacity_until_GC(metaspace_size_words); coleenp@4037: return true; coleenp@4037: } else { coleenp@4037: if (vsl->capacity_words_sum() < capacity_until_GC()) { coleenp@4037: return true; coleenp@4037: } else { coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print_cr(" allocation request size " SIZE_FORMAT coleenp@4037: " capacity_until_GC " SIZE_FORMAT coleenp@4037: " capacity_words_sum " SIZE_FORMAT coleenp@4037: " used_words_sum " SIZE_FORMAT coleenp@4037: " free chunks " SIZE_FORMAT coleenp@4037: " free chunks count %d", coleenp@4037: word_size, coleenp@4037: capacity_until_GC(), coleenp@4037: vsl->capacity_words_sum(), coleenp@4037: vsl->used_words_sum(), coleenp@4037: vsl->chunk_manager()->free_chunks_total(), coleenp@4037: vsl->chunk_manager()->free_chunks_count()); coleenp@4037: } coleenp@4037: return false; coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Variables are in bytes 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: coleenp@4037: VirtualSpaceList *vsl = Metaspace::space_list(); coleenp@4037: coleenp@4037: size_t capacity_after_gc = vsl->capacity_bytes_sum(); coleenp@4037: // Check to see if these two can be calculated without walking the CLDG coleenp@4037: size_t used_after_gc = vsl->used_bytes_sum(); coleenp@4037: size_t capacity_until_GC = vsl->capacity_bytes_sum(); coleenp@4037: size_t free_after_gc = capacity_until_GC - used_after_gc; coleenp@4037: coleenp@4037: const double minimum_free_percentage = MinHeapFreeRatio / 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: const double free_percentage = ((double)free_after_gc) / capacity_until_GC; 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: double d_free_after_gc = free_after_gc / (double) K; coleenp@4037: gclog_or_tty->print_cr(" " coleenp@4037: " free_after_gc : %6.1fK" coleenp@4037: " used_after_gc : %6.1fK" coleenp@4037: " capacity_after_gc : %6.1fK" coleenp@4037: " metaspace HWM : %6.1fK", coleenp@4037: free_after_gc / (double) K, coleenp@4037: used_after_gc / (double) K, coleenp@4037: capacity_after_gc / (double) K, coleenp@4037: capacity_until_GC / (double) K); coleenp@4037: gclog_or_tty->print_cr(" " coleenp@4037: " free_percentage: %6.2f", coleenp@4037: free_percentage); coleenp@4037: } coleenp@4037: coleenp@4037: 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; coleenp@4037: // Don't expand unless it's significant coleenp@4037: if (expand_bytes >= MinMetaspaceExpansion) { coleenp@4037: size_t expand_words = expand_bytes / BytesPerWord; coleenp@4037: MetaspaceGC::inc_capacity_until_GC(expand_words); coleenp@4037: } coleenp@4037: if (PrintGCDetails && Verbose) { coleenp@4037: size_t new_capacity_until_GC = MetaspaceGC::capacity_until_GC_in_bytes(); coleenp@4037: gclog_or_tty->print_cr(" expanding:" coleenp@4037: " minimum_desired_capacity: %6.1fK" coleenp@4037: " expand_words: %6.1fK" coleenp@4037: " MinMetaspaceExpansion: %6.1fK" coleenp@4037: " new metaspace HWM: %6.1fK", coleenp@4037: minimum_desired_capacity / (double) K, coleenp@4037: expand_bytes / (double) K, coleenp@4037: MinMetaspaceExpansion / (double) K, coleenp@4037: new_capacity_until_GC / (double) K); coleenp@4037: } coleenp@4037: return; coleenp@4037: } coleenp@4037: coleenp@4037: // No expansion, now see if we want to shrink coleenp@4037: size_t shrink_words = 0; coleenp@4037: // We would never want to shrink more than this coleenp@4037: size_t max_shrink_words = capacity_until_GC - minimum_desired_capacity; coleenp@4037: assert(max_shrink_words >= 0, err_msg("max_shrink_words " SIZE_FORMAT, coleenp@4037: max_shrink_words)); coleenp@4037: coleenp@4037: // Should shrinking be considered? coleenp@4037: if (MaxHeapFreeRatio < 100) { coleenp@4037: const double maximum_free_percentage = MaxHeapFreeRatio / 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); coleenp@4037: if (PrintGC && 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(" " coleenp@4037: " capacity_until_GC: %6.1fK" coleenp@4037: " minimum_desired_capacity: %6.1fK" coleenp@4037: " maximum_desired_capacity: %6.1fK", coleenp@4037: capacity_until_GC / (double) K, 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 coleenp@4037: shrink_words = 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%. coleenp@4037: shrink_words = shrink_words / 100 * current_shrink_factor; coleenp@4037: assert(shrink_words <= max_shrink_words, coleenp@4037: err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT, coleenp@4037: shrink_words, max_shrink_words)); 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(" " coleenp@4037: " shrink_words: %.1fK" coleenp@4037: " current_shrink_factor: %d" coleenp@4037: " new shrink factor: %d" coleenp@4037: " MinMetaspaceExpansion: %.1fK", coleenp@4037: shrink_words / (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: coleenp@4037: // Don't shrink unless it's significant coleenp@4037: if (shrink_words >= MinMetaspaceExpansion) { coleenp@4037: VirtualSpaceNode* csp = vsl->current_virtual_space(); coleenp@4037: size_t available_to_shrink = csp->capacity_words_in_vs() - coleenp@4037: csp->used_words_in_vs(); coleenp@4037: shrink_words = MIN2(shrink_words, available_to_shrink); coleenp@4037: csp->shrink_by(shrink_words); coleenp@4037: MetaspaceGC::dec_capacity_until_GC(shrink_words); coleenp@4037: if (PrintGCDetails && Verbose) { coleenp@4037: size_t new_capacity_until_GC = MetaspaceGC::capacity_until_GC_in_bytes(); coleenp@4037: gclog_or_tty->print_cr(" metaspace HWM: %.1fK", new_capacity_until_GC / (double) K); coleenp@4037: } coleenp@4037: } coleenp@4037: assert(vsl->used_bytes_sum() == used_after_gc && coleenp@4037: used_after_gc <= vsl->capacity_bytes_sum(), coleenp@4037: "sanity check"); coleenp@4037: coleenp@4037: } coleenp@4037: coleenp@4037: // Metadebug methods coleenp@4037: coleenp@4037: void Metadebug::deallocate_chunk_a_lot(SpaceManager* sm, coleenp@4037: size_t chunk_word_size){ coleenp@4037: #ifdef ASSERT coleenp@4037: VirtualSpaceList* vsl = sm->vs_list(); coleenp@4037: if (MetaDataDeallocateALot && coleenp@4037: Metadebug::deallocate_chunk_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) { coleenp@4037: Metadebug::reset_deallocate_chunk_a_lot_count(); coleenp@4037: for (uint i = 0; i < metadata_deallocate_a_lock_chunk; i++) { coleenp@4037: Metachunk* dummy_chunk = vsl->current_virtual_space()->take_from_committed(chunk_word_size); coleenp@4037: if (dummy_chunk == NULL) { coleenp@4037: break; coleenp@4037: } coleenp@4037: vsl->chunk_manager()->chunk_freelist_deallocate(dummy_chunk); coleenp@4037: coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print("Metadebug::deallocate_chunk_a_lot: %d) ", coleenp@4037: sm->sum_count_in_chunks_in_use()); coleenp@4037: dummy_chunk->print_on(gclog_or_tty); coleenp@4037: gclog_or_tty->print_cr(" Free chunks total %d count %d", coleenp@4037: vsl->chunk_manager()->free_chunks_total(), coleenp@4037: vsl->chunk_manager()->free_chunks_count()); coleenp@4037: } coleenp@4037: } coleenp@4037: } else { coleenp@4037: Metadebug::inc_deallocate_chunk_a_lot_count(); coleenp@4037: } coleenp@4037: #endif coleenp@4037: } coleenp@4037: coleenp@4037: void Metadebug::deallocate_block_a_lot(SpaceManager* sm, coleenp@4037: size_t raw_word_size){ coleenp@4037: #ifdef ASSERT coleenp@4037: if (MetaDataDeallocateALot && coleenp@4037: Metadebug::deallocate_block_a_lot_count() % MetaDataDeallocateALotInterval == 0 ) { coleenp@4037: Metadebug::set_deallocate_block_a_lot_count(0); coleenp@4037: for (uint i = 0; i < metadata_deallocate_a_lot_block; i++) { jmasa@4196: MetaWord* dummy_block = sm->allocate_work(raw_word_size); coleenp@4037: if (dummy_block == 0) { coleenp@4037: break; coleenp@4037: } jmasa@4196: sm->deallocate(dummy_block, raw_word_size); coleenp@4037: } coleenp@4037: } else { coleenp@4037: Metadebug::inc_deallocate_block_a_lot_count(); coleenp@4037: } coleenp@4037: #endif coleenp@4037: } 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: // ChunkList methods coleenp@4037: coleenp@4037: size_t ChunkList::sum_list_size() { coleenp@4037: size_t result = 0; coleenp@4037: Metachunk* cur = head(); coleenp@4037: while (cur != NULL) { coleenp@4037: result += cur->word_size(); coleenp@4037: cur = cur->next(); coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: size_t ChunkList::sum_list_count() { coleenp@4037: size_t result = 0; coleenp@4037: Metachunk* cur = head(); coleenp@4037: while (cur != NULL) { coleenp@4037: result++; coleenp@4037: cur = cur->next(); coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: size_t ChunkList::sum_list_capacity() { coleenp@4037: size_t result = 0; coleenp@4037: Metachunk* cur = head(); coleenp@4037: while (cur != NULL) { coleenp@4037: result += cur->capacity_word_size(); coleenp@4037: cur = cur->next(); coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkList::add_at_head(Metachunk* head, Metachunk* tail) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: assert(tail->next() == NULL, "Not the tail"); coleenp@4037: coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: tty->print("ChunkList::add_at_head: "); coleenp@4037: Metachunk* cur = head; coleenp@4037: while (cur != NULL) { coleenp@4037: tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ", cur, cur->word_size()); coleenp@4037: cur = cur->next(); coleenp@4037: } coleenp@4037: tty->print_cr(""); coleenp@4037: } coleenp@4037: coleenp@4037: if (tail != NULL) { coleenp@4037: tail->set_next(_head); coleenp@4037: } coleenp@4037: set_head(head); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkList::add_at_head(Metachunk* list) { coleenp@4037: if (list == NULL) { coleenp@4037: // Nothing to add coleenp@4037: return; coleenp@4037: } coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: Metachunk* head = list; coleenp@4037: Metachunk* tail = list; coleenp@4037: Metachunk* cur = head->next(); coleenp@4037: // Search for the tail since it is not passed. coleenp@4037: while (cur != NULL) { coleenp@4037: tail = cur; coleenp@4037: cur = cur->next(); coleenp@4037: } coleenp@4037: add_at_head(head, tail); coleenp@4037: } coleenp@4037: coleenp@4037: // ChunkManager methods coleenp@4037: coleenp@4037: // Verification of _free_chunks_total and _free_chunks_count does not coleenp@4037: // work with the CMS collector because its use of additional locks coleenp@4037: // complicate the mutex deadlock detection but it can still be useful coleenp@4037: // for detecting errors in the chunk accounting with other collectors. coleenp@4037: coleenp@4037: size_t ChunkManager::free_chunks_total() { 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); mgerdin@4264: slow_locked_verify_free_chunks_total(); coleenp@4037: } coleenp@4037: #endif coleenp@4037: return _free_chunks_total; coleenp@4037: } coleenp@4037: coleenp@4037: size_t ChunkManager::free_chunks_total_in_bytes() { coleenp@4037: return free_chunks_total() * 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()); coleenp@4037: st->print_cr("Free chunk total 0x%x count 0x%x", 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()); coleenp@4037: st->print_cr("Sum free chunk total 0x%x count 0x%x", 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; coleenp@4037: for (ChunkIndex i = SmallIndex; 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: coleenp@4037: result = result + list->sum_list_capacity(); 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; coleenp@4037: for (ChunkIndex i = SmallIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) { coleenp@4037: ChunkList* list = free_chunks(i); coleenp@4037: if (list == NULL) { coleenp@4037: continue; coleenp@4037: } coleenp@4037: count = count + list->sum_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) { coleenp@4037: switch (word_size) { coleenp@4037: case SpaceManager::SmallChunk : coleenp@4037: return &_free_chunks[0]; coleenp@4037: case SpaceManager::MediumChunk : coleenp@4037: return &_free_chunks[1]; coleenp@4037: default: coleenp@4037: assert(word_size > SpaceManager::MediumChunk, "List inconsistency"); coleenp@4037: return &_free_chunks[2]; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::free_chunks_put(Metachunk* chunk) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: ChunkList* free_list = find_free_chunks_list(chunk->word_size()); coleenp@4037: chunk->set_next(free_list->head()); coleenp@4037: free_list->set_head(chunk); coleenp@4037: // chunk is being returned to the chunk free list coleenp@4037: inc_free_chunks_total(chunk->capacity_word_size()); mgerdin@4264: slow_locked_verify(); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::chunk_freelist_deallocate(Metachunk* chunk) { coleenp@4037: // The deallocation of a chunk originates in the freelist coleenp@4037: // manangement code for a Metaspace and does not hold the coleenp@4037: // lock. coleenp@4037: assert(chunk != NULL, "Deallocating NULL"); mgerdin@4264: assert_lock_strong(SpaceManager::expand_lock()); mgerdin@4264: slow_locked_verify(); coleenp@4037: if (TraceMetadataChunkAllocation) { coleenp@4037: tty->print_cr("ChunkManager::chunk_freelist_deallocate: chunk " coleenp@4037: PTR_FORMAT " size " SIZE_FORMAT, coleenp@4037: chunk, chunk->word_size()); coleenp@4037: } coleenp@4037: free_chunks_put(chunk); 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@4196: if (!SpaceManager::is_humongous(word_size)) { 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: debug_only(Metachunk* debug_head = chunk;) 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. coleenp@4037: free_list->set_head(chunk->next()); coleenp@4037: chunk->set_next(NULL); jmasa@4196: // Chunk has been removed from the chunks free list. jmasa@4196: dec_free_chunks_total(chunk->capacity_word_size()); coleenp@4037: coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: tty->print_cr("ChunkManager::free_chunks_get: free_list " coleenp@4037: PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT, coleenp@4037: 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: jmasa@4196: if (chunk != NULL) { jmasa@4196: if (TraceMetadataHumongousAllocation) { jmasa@4196: size_t waste = chunk->word_size() - word_size; jmasa@4196: tty->print_cr("Free list allocate humongous chunk size " SIZE_FORMAT jmasa@4196: " for requested size " SIZE_FORMAT jmasa@4196: " waste " SIZE_FORMAT, jmasa@4196: chunk->word_size(), word_size, waste); coleenp@4037: } jmasa@4196: // Chunk is being removed from the chunks free list. jmasa@4196: dec_free_chunks_total(chunk->capacity_word_size()); jmasa@4196: #ifdef ASSERT jmasa@4196: chunk->set_is_free(false); jmasa@4196: #endif coleenp@4037: } coleenp@4037: } 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: coleenp@4037: assert(word_size <= chunk->word_size() || coleenp@4037: SpaceManager::is_humongous(chunk->word_size()), coleenp@4037: "Non-humongous variable sized chunk"); coleenp@4037: if (TraceMetadataChunkAllocation) { coleenp@4037: tty->print("ChunkManager::chunk_freelist_allocate: chunk " coleenp@4037: PTR_FORMAT " size " SIZE_FORMAT " ", coleenp@4037: chunk, chunk->word_size()); coleenp@4037: locked_print_free_chunks(tty); coleenp@4037: } coleenp@4037: coleenp@4037: return chunk; coleenp@4037: } coleenp@4037: jmasa@4196: void ChunkManager::print_on(outputStream* out) { jmasa@4196: if (PrintFLSStatistics != 0) { jmasa@4196: humongous_dictionary()->report_statistics(); jmasa@4196: } jmasa@4196: } jmasa@4196: coleenp@4037: // SpaceManager methods coleenp@4037: 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@4196: for (ChunkIndex i = SmallIndex; 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@4196: for (ChunkIndex i = SmallIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { jmasa@4196: jmasa@4196: 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: size_t count = 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@4037: if (chunk != NULL) { jmasa@4196: Metachunk* prev = chunk; jmasa@4196: while (chunk != NULL && chunk != current_chunk()) { jmasa@4196: result += chunk->free_word_size(); jmasa@4196: prev = chunk; coleenp@4037: chunk = chunk->next(); coleenp@4037: count++; coleenp@4037: } coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_capacity_in_chunks_in_use() const { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: size_t sum = 0; jmasa@4196: for (ChunkIndex i = SmallIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: Metachunk* chunk = chunks_in_use(i); coleenp@4037: while (chunk != NULL) { coleenp@4037: // Just changed this sum += chunk->capacity_word_size(); coleenp@4037: // sum += chunk->word_size() - Metachunk::overhead(); coleenp@4037: sum += chunk->capacity_word_size(); coleenp@4037: chunk = chunk->next(); coleenp@4037: } coleenp@4037: } coleenp@4037: return sum; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_count_in_chunks_in_use() { coleenp@4037: size_t count = 0; jmasa@4196: for (ChunkIndex i = SmallIndex; 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@4196: for (ChunkIndex i = SmallIndex; 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: coleenp@4037: Metachunk* small_chunk = chunks_in_use(SmallIndex); coleenp@4037: st->print_cr("SpaceManager: small chunk " PTR_FORMAT coleenp@4037: " free " SIZE_FORMAT, coleenp@4037: small_chunk, coleenp@4037: small_chunk->free_word_size()); coleenp@4037: coleenp@4037: Metachunk* medium_chunk = chunks_in_use(MediumIndex); coleenp@4037: st->print("medium chunk " PTR_FORMAT, medium_chunk); coleenp@4037: Metachunk* tail = current_chunk(); coleenp@4037: st->print_cr(" current chunk " PTR_FORMAT, tail); coleenp@4037: coleenp@4037: Metachunk* head = chunks_in_use(HumongousIndex); coleenp@4037: st->print_cr("humongous chunk " PTR_FORMAT, head); coleenp@4037: coleenp@4037: vs_list()->chunk_manager()->locked_print_free_chunks(st); coleenp@4037: vs_list()->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 coleenp@4037: // _small_chunk_limit small chunks can be allocated but coleenp@4037: // once a medium chunk has been allocated, no more small coleenp@4037: // chunks will be allocated. coleenp@4037: size_t chunk_word_size; coleenp@4037: if (chunks_in_use(MediumIndex) == NULL && coleenp@4037: (!has_small_chunk_limit() || coleenp@4037: sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit)) { coleenp@4037: chunk_word_size = (size_t) SpaceManager::SmallChunk; coleenp@4037: if (word_size + Metachunk::overhead() > SpaceManager::SmallChunk) { coleenp@4037: chunk_word_size = MediumChunk; coleenp@4037: } coleenp@4037: } else { coleenp@4037: chunk_word_size = MediumChunk; coleenp@4037: } coleenp@4037: coleenp@4037: // Might still need a humongous chunk coleenp@4037: chunk_word_size = coleenp@4037: MAX2((size_t) chunk_word_size, word_size + Metachunk::overhead()); coleenp@4037: 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: 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) { coleenp@4037: gclog_or_tty->print_cr("SpaceManager::grow_and_allocate for " SIZE_FORMAT coleenp@4037: " words " SIZE_FORMAT " space left", coleenp@4037: word_size, current_chunk() != NULL ? coleenp@4037: current_chunk()->free_word_size() : 0); coleenp@4037: } coleenp@4037: coleenp@4037: // Get another chunk out of the virtual space coleenp@4037: size_t grow_chunks_by_words = calc_chunk_size(word_size); coleenp@4037: Metachunk* next = vs_list()->get_new_chunk(word_size, grow_chunks_by_words); coleenp@4037: 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: Metadebug::deallocate_chunk_a_lot(this, grow_chunks_by_words); coleenp@4037: // Add to this manager's list of chunks in use. coleenp@4037: add_chunk(next, false); coleenp@4037: return next->allocate(word_size); coleenp@4037: } coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: coleenp@4037: void SpaceManager::print_on(outputStream* st) const { coleenp@4037: coleenp@4037: for (ChunkIndex i = SmallIndex; 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: coleenp@4037: SpaceManager::SpaceManager(Mutex* lock, VirtualSpaceList* vs_list) : coleenp@4037: _vs_list(vs_list), coleenp@4037: _allocation_total(0), coleenp@4037: _lock(lock) { coleenp@4037: Metadebug::init_allocation_fail_alot_count(); jmasa@4196: for (ChunkIndex i = SmallIndex; 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: coleenp@4037: SpaceManager::~SpaceManager() { coleenp@4037: MutexLockerEx fcl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: coleenp@4037: ChunkManager* chunk_manager = vs_list()->chunk_manager(); coleenp@4037: mgerdin@4264: chunk_manager->slow_locked_verify(); coleenp@4037: 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: coleenp@4304: // Mangle freed memory. coleenp@4304: NOT_PRODUCT(mangle_freed_chunks();) coleenp@4304: coleenp@4037: // Have to update before the chunks_in_use lists are emptied coleenp@4037: // below. coleenp@4037: chunk_manager->inc_free_chunks_total(sum_capacity_in_chunks_in_use(), coleenp@4037: 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: coleenp@4037: // Small chunks. There is one _current_chunk for each coleenp@4037: // Metaspace. It could point to a small or medium chunk. coleenp@4037: // Rather than determine which it is, follow the list of coleenp@4037: // small chunks to add them to the free list coleenp@4037: Metachunk* small_chunk = chunks_in_use(SmallIndex); coleenp@4037: chunk_manager->free_small_chunks()->add_at_head(small_chunk); coleenp@4037: set_chunks_in_use(SmallIndex, NULL); coleenp@4037: coleenp@4037: // After the small chunk are the medium chunks coleenp@4037: Metachunk* medium_chunk = chunks_in_use(MediumIndex); coleenp@4037: assert(medium_chunk == NULL || coleenp@4037: medium_chunk->word_size() == MediumChunk, coleenp@4037: "Chunk is on the wrong list"); coleenp@4037: coleenp@4037: if (medium_chunk != NULL) { coleenp@4037: Metachunk* head = medium_chunk; coleenp@4037: // If there is a medium chunk then the _current_chunk can only coleenp@4037: // point to the last medium chunk. coleenp@4037: Metachunk* tail = current_chunk(); coleenp@4037: chunk_manager->free_medium_chunks()->add_at_head(head, tail); coleenp@4037: set_chunks_in_use(MediumIndex, NULL); coleenp@4037: } coleenp@4037: coleenp@4037: // Humongous chunks 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 jmasa@4196: humongous_chunks->set_is_free(true); jmasa@4196: #endif jmasa@4196: Metachunk* next_humongous_chunks = humongous_chunks->next(); jmasa@4196: chunk_manager->humongous_dictionary()->return_chunk(humongous_chunks); jmasa@4196: humongous_chunks = next_humongous_chunks; coleenp@4037: } jmasa@4196: set_chunks_in_use(HumongousIndex, NULL); mgerdin@4264: chunk_manager->slow_locked_verify(); coleenp@4037: } coleenp@4037: jmasa@4196: void SpaceManager::deallocate(MetaWord* p, size_t word_size) { coleenp@4037: assert_lock_strong(_lock); jmasa@4196: size_t min_size = TreeChunk::min_size(); jmasa@4196: assert(word_size >= min_size, jmasa@4196: err_msg("Should not deallocate dark matter " SIZE_FORMAT, word_size)); jmasa@4196: block_freelists()->return_block(p, 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. coleenp@4037: switch (new_chunk->word_size()) { coleenp@4037: case SpaceManager::SmallChunk : coleenp@4037: if (chunks_in_use(SmallIndex) == NULL) { coleenp@4037: // First chunk to add to the list coleenp@4037: set_chunks_in_use(SmallIndex, new_chunk); coleenp@4037: } else { coleenp@4037: assert(current_chunk()->word_size() == SpaceManager::SmallChunk, coleenp@4037: err_msg( "Incorrect mix of sizes in chunk list " coleenp@4037: SIZE_FORMAT " new chunk " SIZE_FORMAT, coleenp@4037: current_chunk()->word_size(), new_chunk->word_size())); coleenp@4037: current_chunk()->set_next(new_chunk); coleenp@4037: } coleenp@4037: // Make current chunk coleenp@4037: set_current_chunk(new_chunk); coleenp@4037: break; coleenp@4037: case SpaceManager::MediumChunk : coleenp@4037: if (chunks_in_use(MediumIndex) == NULL) { coleenp@4037: // About to add the first medium chunk so teminate the coleenp@4037: // small chunk list. In general once medium chunks are coleenp@4037: // being added, we're past the need for small chunks. coleenp@4037: if (current_chunk() != NULL) { coleenp@4037: // Only a small chunk or the initial chunk could be coleenp@4037: // the current chunk if this is the first medium chunk. coleenp@4037: assert(current_chunk()->word_size() == SpaceManager::SmallChunk || coleenp@4037: chunks_in_use(SmallIndex) == NULL, coleenp@4037: err_msg("Should be a small chunk or initial chunk, current chunk " coleenp@4037: SIZE_FORMAT " new chunk " SIZE_FORMAT, coleenp@4037: current_chunk()->word_size(), new_chunk->word_size())); coleenp@4037: current_chunk()->set_next(NULL); coleenp@4037: } coleenp@4037: // First chunk to add to the list coleenp@4037: set_chunks_in_use(MediumIndex, new_chunk); coleenp@4037: coleenp@4037: } else { coleenp@4037: // As a minimum the first medium chunk added would coleenp@4037: // have become the _current_chunk coleenp@4037: // so the _current_chunk has to be non-NULL here coleenp@4037: // (although not necessarily still the first medium chunk). coleenp@4037: assert(current_chunk()->word_size() == SpaceManager::MediumChunk, coleenp@4037: "A medium chunk should the current chunk"); coleenp@4037: current_chunk()->set_next(new_chunk); coleenp@4037: } coleenp@4037: // Make current chunk coleenp@4037: set_current_chunk(new_chunk); coleenp@4037: break; coleenp@4037: default: { 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: coleenp@4037: assert(new_chunk->word_size() > MediumChunk, "List inconsistency"); coleenp@4037: } coleenp@4037: } coleenp@4037: 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); coleenp@4037: vs_list()->chunk_manager()->locked_print_free_chunks(tty); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: MetaWord* SpaceManager::allocate(size_t word_size) { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: coleenp@4037: // If only the dictionary is going to be used (i.e., no coleenp@4037: // indexed free list), then there is a minimum size requirement. coleenp@4037: // MinChunkSize is a placeholder for the real minimum size JJJ jmasa@4196: size_t byte_size = word_size * BytesPerWord; jmasa@4196: jmasa@4196: size_t byte_size_with_overhead = byte_size + Metablock::overhead(); jmasa@4196: jmasa@4196: size_t raw_bytes_size = MAX2(byte_size_with_overhead, jmasa@4196: Metablock::min_block_byte_size()); jmasa@4196: raw_bytes_size = ARENA_ALIGN(raw_bytes_size); coleenp@4037: size_t raw_word_size = raw_bytes_size / BytesPerWord; coleenp@4037: assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem"); coleenp@4037: 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: Metadebug::deallocate_block_a_lot(this, raw_word_size); 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"); coleenp@4037: inc_allocation_total(word_size); coleenp@4037: return current_chunk()->allocate(word_size); // caller handles null result coleenp@4037: } 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: } coleenp@4037: if (result > 0) { coleenp@4037: inc_allocation_total(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) { coleenp@4037: // Skip the small chunks because their next link points to coleenp@4037: // medium chunks. This is because the small chunk is the coleenp@4037: // current chunk (for allocations) until it is full and the coleenp@4037: // the addition of the next chunk does not NULL the next coleenp@4037: // like of the small chunk. jmasa@4196: for (ChunkIndex i = MediumIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: Metachunk* curr = chunks_in_use(i); coleenp@4037: while (curr != NULL) { coleenp@4037: curr->verify(); coleenp@4037: curr = curr->next(); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: #ifdef ASSERT coleenp@4037: void SpaceManager::verify_allocation_total() { coleenp@4037: #if 0 coleenp@4037: // Verification is only guaranteed at a safepoint. coleenp@4037: if (SafepointSynchronize::is_at_safepoint()) { coleenp@4037: gclog_or_tty->print_cr("Chunk " PTR_FORMAT " allocation_total " SIZE_FORMAT coleenp@4037: " sum_used_in_chunks_in_use " SIZE_FORMAT, coleenp@4037: this, coleenp@4037: allocation_total(), coleenp@4037: sum_used_in_chunks_in_use()); coleenp@4037: } coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: assert(allocation_total() == sum_used_in_chunks_in_use(), coleenp@4037: err_msg("allocation total is not consistent %d vs %d", coleenp@4037: allocation_total(), sum_used_in_chunks_in_use())); coleenp@4037: #endif 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. coleenp@4037: for (ChunkIndex index = SmallIndex; 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: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: block_freelists()->print_on(out); coleenp@4037: } coleenp@4037: curr_total += curr->word_size(); coleenp@4037: used += curr->used_word_size(); coleenp@4037: capacity += curr->capacity_word_size(); coleenp@4037: waste += curr->free_word_size() + curr->overhead();; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: size_t free = 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() { coleenp@4037: for (ChunkIndex index = SmallIndex; 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: // Try to detect incorrectly terminated small chunk coleenp@4037: // list. coleenp@4037: assert(index == MediumIndex || curr != chunks_in_use(MediumIndex), coleenp@4037: err_msg("Mangling medium chunks in small chunks? " coleenp@4037: "curr " PTR_FORMAT " medium list " PTR_FORMAT, coleenp@4037: curr, chunks_in_use(MediumIndex))); coleenp@4037: curr->mangle(); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4304: #endif // PRODUCT coleenp@4037: coleenp@4037: coleenp@4037: // MetaspaceAux coleenp@4037: coleenp@4304: size_t MetaspaceAux::used_in_bytes() { coleenp@4304: return (Metaspace::class_space_list()->used_words_sum() + coleenp@4304: Metaspace::space_list()->used_words_sum()) * BytesPerWord; coleenp@4304: } coleenp@4304: jmasa@4046: size_t MetaspaceAux::used_in_bytes(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@4042: // Sum allocation_total for each metaspace jmasa@4042: if (msp != NULL) { jmasa@4042: used += msp->used_words(mdtype); jmasa@4042: } jmasa@4042: } jmasa@4042: return used * BytesPerWord; jmasa@4042: } jmasa@4042: coleenp@4037: size_t MetaspaceAux::free_in_bytes(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) { coleenp@4037: free += msp->free_words(mdtype); coleenp@4037: } coleenp@4037: } coleenp@4037: return free * BytesPerWord; coleenp@4037: } coleenp@4037: coleenp@4037: // The total words available for metadata allocation. This coleenp@4037: // uses Metaspace capacity_words() which is the total words coleenp@4037: // in chunks allocated for a Metaspace. coleenp@4304: size_t MetaspaceAux::capacity_in_bytes() { coleenp@4304: return (Metaspace::class_space_list()->capacity_words_sum() + coleenp@4304: Metaspace::space_list()->capacity_words_sum()) * BytesPerWord; coleenp@4304: } coleenp@4304: coleenp@4037: size_t MetaspaceAux::capacity_in_bytes(Metaspace::MetadataType mdtype) { coleenp@4037: size_t capacity = free_chunks_total(mdtype); coleenp@4037: ClassLoaderDataGraphMetaspaceIterator iter; coleenp@4037: while (iter.repeat()) { coleenp@4037: Metaspace* msp = iter.get_next(); coleenp@4037: if (msp != NULL) { coleenp@4037: capacity += msp->capacity_words(mdtype); coleenp@4037: } coleenp@4037: } coleenp@4037: return capacity * BytesPerWord; coleenp@4037: } coleenp@4037: coleenp@4304: size_t MetaspaceAux::reserved_in_bytes() { coleenp@4304: return (Metaspace::class_space_list()->virtual_space_total() + coleenp@4304: Metaspace::space_list()->virtual_space_total()) * BytesPerWord; coleenp@4304: } coleenp@4304: coleenp@4037: size_t MetaspaceAux::reserved_in_bytes(Metaspace::MetadataType mdtype) { coleenp@4037: size_t reserved = (mdtype == Metaspace::ClassType) ? coleenp@4037: Metaspace::class_space_list()->virtual_space_total() : coleenp@4037: Metaspace::space_list()->virtual_space_total(); coleenp@4037: return reserved * BytesPerWord; coleenp@4037: } coleenp@4037: coleenp@4037: size_t MetaspaceAux::min_chunk_size() { return SpaceManager::MediumChunk; } coleenp@4037: coleenp@4037: size_t MetaspaceAux::free_chunks_total(Metaspace::MetadataType mdtype) { coleenp@4037: ChunkManager* chunk = (mdtype == Metaspace::ClassType) ? coleenp@4037: Metaspace::class_space_list()->chunk_manager() : coleenp@4037: Metaspace::space_list()->chunk_manager(); mgerdin@4264: chunk->slow_verify(); coleenp@4037: return chunk->free_chunks_total(); coleenp@4037: } coleenp@4037: coleenp@4037: size_t MetaspaceAux::free_chunks_total_in_bytes(Metaspace::MetadataType mdtype) { coleenp@4037: return free_chunks_total(mdtype) * BytesPerWord; coleenp@4037: } coleenp@4037: 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 coleenp@4037: "(" SIZE_FORMAT "/" SIZE_FORMAT ")", coleenp@4037: prev_metadata_used, coleenp@4037: used_in_bytes(), coleenp@4037: capacity_in_bytes(), coleenp@4037: reserved_in_bytes()); coleenp@4037: } else { coleenp@4037: gclog_or_tty->print(" " SIZE_FORMAT "K" coleenp@4037: "->" SIZE_FORMAT "K" coleenp@4037: "(" SIZE_FORMAT "K/" SIZE_FORMAT "K)", coleenp@4037: prev_metadata_used / K, coleenp@4037: used_in_bytes()/ K, coleenp@4037: capacity_in_bytes()/K, coleenp@4037: reserved_in_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 ct = Metaspace::ClassType; coleenp@4037: Metaspace::MetadataType nct = Metaspace::NonClassType; coleenp@4037: coleenp@4037: out->print_cr(" Metaspace total " coleenp@4037: SIZE_FORMAT "K, used " SIZE_FORMAT "K," coleenp@4037: " reserved " SIZE_FORMAT "K", jmasa@4046: capacity_in_bytes()/K, used_in_bytes()/K, reserved_in_bytes()/K); coleenp@4037: out->print_cr(" data space " coleenp@4037: SIZE_FORMAT "K, used " SIZE_FORMAT "K," coleenp@4037: " reserved " SIZE_FORMAT "K", jmasa@4046: capacity_in_bytes(nct)/K, used_in_bytes(nct)/K, reserved_in_bytes(nct)/K); coleenp@4037: out->print_cr(" class space " coleenp@4037: SIZE_FORMAT "K, used " SIZE_FORMAT "K," coleenp@4037: " reserved " SIZE_FORMAT "K", jmasa@4046: capacity_in_bytes(ct)/K, used_in_bytes(ct)/K, reserved_in_bytes(ct)/K); 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) { coleenp@4037: size_t free_chunks_capacity_bytes = free_chunks_total_in_bytes(mdtype); coleenp@4037: size_t capacity_bytes = capacity_in_bytes(mdtype); coleenp@4037: size_t used_bytes = used_in_bytes(mdtype); coleenp@4037: size_t free_bytes = free_in_bytes(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); coleenp@4037: assert(used_and_free == capacity_bytes, "Accounting is wrong"); coleenp@4037: } coleenp@4037: coleenp@4037: // Print total fragmentation for class and data metaspaces separately coleenp@4037: void MetaspaceAux::print_waste(outputStream* out) { coleenp@4037: coleenp@4037: size_t small_waste = 0, medium_waste = 0, large_waste = 0; coleenp@4037: size_t cls_small_waste = 0, cls_medium_waste = 0, cls_large_waste = 0; coleenp@4037: coleenp@4037: ClassLoaderDataGraphMetaspaceIterator iter; coleenp@4037: while (iter.repeat()) { coleenp@4037: Metaspace* msp = iter.get_next(); coleenp@4037: if (msp != NULL) { coleenp@4037: small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex); coleenp@4037: medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex); coleenp@4037: large_waste += msp->vsm()->sum_waste_in_chunks_in_use(HumongousIndex); coleenp@4037: coleenp@4037: cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex); coleenp@4037: cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex); coleenp@4037: cls_large_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(HumongousIndex); coleenp@4037: } coleenp@4037: } coleenp@4037: out->print_cr("Total fragmentation waste (words) doesn't count free space"); coleenp@4037: out->print(" data: small " SIZE_FORMAT " medium " SIZE_FORMAT, coleenp@4037: small_waste, medium_waste); coleenp@4037: out->print_cr(" class: small " SIZE_FORMAT, cls_small_waste); 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() { mgerdin@4264: Metaspace::space_list()->chunk_manager()->verify(); mgerdin@4264: Metaspace::class_space_list()->chunk_manager()->verify(); mgerdin@4264: } mgerdin@4264: coleenp@4037: // Metaspace methods coleenp@4037: coleenp@4037: size_t Metaspace::_first_chunk_word_size = 0; coleenp@4037: coleenp@4037: Metaspace::Metaspace(Mutex* lock, size_t word_size) { coleenp@4037: initialize(lock, word_size); coleenp@4037: } coleenp@4037: coleenp@4037: Metaspace::Metaspace(Mutex* lock) { coleenp@4037: initialize(lock); coleenp@4037: } coleenp@4037: coleenp@4037: Metaspace::~Metaspace() { coleenp@4037: delete _vsm; coleenp@4037: delete _class_vsm; coleenp@4037: } coleenp@4037: coleenp@4037: VirtualSpaceList* Metaspace::_space_list = NULL; coleenp@4037: VirtualSpaceList* Metaspace::_class_space_list = NULL; coleenp@4037: coleenp@4037: #define VIRTUALSPACEMULTIPLIER 2 coleenp@4037: coleenp@4037: void Metaspace::global_initialize() { coleenp@4037: // Initialize the alignment for shared spaces. coleenp@4037: int max_alignment = os::vm_page_size(); coleenp@4037: MetaspaceShared::set_max_alignment(max_alignment); coleenp@4037: coleenp@4037: if (DumpSharedSpaces) { coleenp@4037: SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment); coleenp@4037: SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment); coleenp@4037: SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment); coleenp@4037: SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment); coleenp@4037: 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. coleenp@4037: size_t total = align_size_up(SharedReadOnlySize + SharedReadWriteSize + coleenp@4037: SharedMiscDataSize + SharedMiscCodeSize, coleenp@4037: os::vm_allocation_granularity()); coleenp@4037: size_t word_size = total/wordSize; coleenp@4037: _space_list = new VirtualSpaceList(word_size); coleenp@4037: } else { 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) coleenp@4037: if (UseSharedSpaces) { coleenp@4037: FileMapInfo* mapinfo = new FileMapInfo(); coleenp@4037: memset(mapinfo, 0, sizeof(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)) { coleenp@4037: FileMapInfo::set_current_info(mapinfo); 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: } coleenp@4037: coleenp@4037: // Initialize this before initializing the VirtualSpaceList coleenp@4037: _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord; coleenp@4037: // Arbitrarily set the initial virtual space to a multiple coleenp@4037: // of the boot class loader size. coleenp@4037: size_t word_size = VIRTUALSPACEMULTIPLIER * Metaspace::first_chunk_word_size(); coleenp@4037: // Initialize the list of virtual spaces. coleenp@4037: _space_list = new VirtualSpaceList(word_size); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // For UseCompressedKlassPointers the class space is reserved as a piece of the coleenp@4037: // Java heap because the compression algorithm is the same for each. The coleenp@4037: // argument passed in is at the top of the compressed space coleenp@4037: void Metaspace::initialize_class_space(ReservedSpace rs) { coleenp@4037: // The reserved space size may be bigger because of alignment, esp with UseLargePages coleenp@4037: assert(rs.size() >= ClassMetaspaceSize, err_msg("%d != %d", rs.size(), ClassMetaspaceSize)); coleenp@4037: _class_space_list = new VirtualSpaceList(rs); coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: void Metaspace::initialize(Mutex* lock, size_t initial_size) { coleenp@4048: // Use SmallChunk size if not specified. If specified, use this size for coleenp@4048: // the data metaspace. coleenp@4037: size_t word_size; coleenp@4037: size_t class_word_size; coleenp@4037: if (initial_size == 0) { coleenp@4037: word_size = (size_t) SpaceManager::SmallChunk; coleenp@4048: class_word_size = (size_t) SpaceManager::SmallChunk; coleenp@4037: } else { coleenp@4037: word_size = initial_size; coleenp@4048: // Make the first class chunk bigger than a medium chunk so it's not put coleenp@4048: // on the medium chunk list. The next chunk will be small and progress coleenp@4048: // from there. This size calculated by -version. coleenp@4048: class_word_size = MIN2((size_t)SpaceManager::MediumChunk*5, coleenp@4048: (ClassMetaspaceSize/BytesPerWord)*2); coleenp@4037: } coleenp@4037: coleenp@4037: assert(space_list() != NULL, coleenp@4037: "Metadata VirtualSpaceList has not been initialized"); coleenp@4037: coleenp@4037: _vsm = new SpaceManager(lock, space_list()); coleenp@4037: if (_vsm == NULL) { coleenp@4037: return; coleenp@4037: } coleenp@4037: coleenp@4037: assert(class_space_list() != NULL, coleenp@4037: "Class VirtualSpaceList has not been initialized"); coleenp@4037: coleenp@4037: // Allocate SpaceManager for classes. coleenp@4037: _class_vsm = new SpaceManager(lock, class_space_list()); coleenp@4037: if (_class_vsm == NULL) { coleenp@4037: return; 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 coleenp@4037: Metachunk* new_chunk = coleenp@4037: space_list()->current_virtual_space()->get_chunk_vs_with_expand(word_size); 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 coleenp@4037: Metachunk* class_chunk = coleenp@4037: class_space_list()->current_virtual_space()->get_chunk_vs_with_expand(class_word_size); coleenp@4037: if (class_chunk != NULL) { coleenp@4037: class_vsm()->add_chunk(class_chunk, true); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) { coleenp@4037: // DumpSharedSpaces doesn't use class metadata area (yet) coleenp@4037: if (mdtype == ClassType && !DumpSharedSpaces) { 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) { jmasa@4064: MetaWord* result; jmasa@4064: MetaspaceGC::set_expand_after_GC(true); jmasa@4064: size_t before_inc = MetaspaceGC::capacity_until_GC(); jmasa@4064: size_t delta_words = MetaspaceGC::delta_capacity_until_GC(word_size); jmasa@4064: MetaspaceGC::inc_capacity_until_GC(delta_words); jmasa@4064: if (PrintGCDetails && Verbose) { jmasa@4064: gclog_or_tty->print_cr("Increase capacity to GC from " SIZE_FORMAT jmasa@4064: " to " SIZE_FORMAT, before_inc, MetaspaceGC::capacity_until_GC()); jmasa@4064: } jmasa@4196: jmasa@4064: result = allocate(word_size, mdtype); jmasa@4064: jmasa@4064: return result; 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: coleenp@4037: size_t Metaspace::used_words(MetadataType mdtype) const { coleenp@4037: // return vsm()->allocation_total(); coleenp@4037: return mdtype == ClassType ? class_vsm()->sum_used_in_chunks_in_use() : coleenp@4037: vsm()->sum_used_in_chunks_in_use(); // includes overhead! coleenp@4037: } coleenp@4037: coleenp@4037: size_t Metaspace::free_words(MetadataType mdtype) const { coleenp@4037: return mdtype == ClassType ? class_vsm()->sum_free_in_chunks_in_use() : coleenp@4037: vsm()->sum_free_in_chunks_in_use(); 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. coleenp@4037: size_t Metaspace::capacity_words(MetadataType mdtype) const { coleenp@4037: return mdtype == ClassType ? class_vsm()->sum_capacity_in_chunks_in_use() : coleenp@4037: vsm()->sum_capacity_in_chunks_in_use(); coleenp@4037: } coleenp@4037: coleenp@4037: void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) { coleenp@4037: if (SafepointSynchronize::is_at_safepoint()) { coleenp@4037: assert(Thread::current()->is_VM_thread(), "should be the VM thread"); jmasa@4196: // Don't take Heap_lock jmasa@4196: MutexLocker ml(vsm()->lock()); jmasa@4196: 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: } coleenp@4037: if (is_class) { jmasa@4196: class_vsm()->deallocate(ptr, word_size); coleenp@4037: } else { jmasa@4196: vsm()->deallocate(ptr, word_size); coleenp@4037: } coleenp@4037: } else { coleenp@4037: MutexLocker ml(vsm()->lock()); coleenp@4037: jmasa@4196: 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: } coleenp@4037: if (is_class) { 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: jmasa@4196: Metablock* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size, coleenp@4037: bool read_only, MetadataType mdtype, 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: // SSS: Should we align the allocations and make sure the sizes are aligned. coleenp@4037: MetaWord* result = NULL; 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."); 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) { coleenp@4037: if (read_only) { coleenp@4037: result = loader_data->ro_metaspace()->allocate(word_size, NonClassType); coleenp@4037: } else { coleenp@4037: result = loader_data->rw_metaspace()->allocate(word_size, NonClassType); coleenp@4037: } coleenp@4037: if (result == NULL) { coleenp@4037: report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite); coleenp@4037: } jmasa@4196: return Metablock::initialize(result, word_size); coleenp@4037: } coleenp@4037: coleenp@4037: result = loader_data->metaspace_non_null()->allocate(word_size, mdtype); coleenp@4037: coleenp@4037: if (result == NULL) { coleenp@4037: // Try to clean out some memory and retry. coleenp@4037: result = jmasa@4196: Universe::heap()->collector_policy()->satisfy_failed_metadata_allocation( coleenp@4037: loader_data, word_size, mdtype); coleenp@4037: coleenp@4037: // If result is still null, we are out of memory. coleenp@4037: if (result == NULL) { coleenp@4037: // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support coleenp@4037: report_java_out_of_memory("Metadata space"); coleenp@4037: coleenp@4037: if (JvmtiExport::should_post_resource_exhausted()) { coleenp@4037: JvmtiExport::post_resource_exhausted( coleenp@4037: JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR, coleenp@4037: "Metadata space"); coleenp@4037: } coleenp@4037: THROW_OOP_0(Universe::out_of_memory_error_perm_gen()); coleenp@4037: } coleenp@4037: } jmasa@4196: return Metablock::initialize(result, word_size); coleenp@4037: } coleenp@4037: coleenp@4037: void Metaspace::print_on(outputStream* out) const { coleenp@4037: // Print both class virtual space counts and metaspace. coleenp@4037: if (Verbose) { coleenp@4037: vsm()->print_on(out); coleenp@4037: class_vsm()->print_on(out); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: #ifndef PRODUCT coleenp@4037: bool Metaspace::contains(const void * ptr) const { coleenp@4037: if (MetaspaceShared::is_in_shared_space(ptr)) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: return space_list()->contains(ptr) || class_space_list()->contains(ptr); coleenp@4037: } coleenp@4037: #endif coleenp@4037: coleenp@4037: void Metaspace::verify() { coleenp@4037: vsm()->verify(); coleenp@4037: class_vsm()->verify(); coleenp@4037: } coleenp@4037: coleenp@4037: void Metaspace::dump(outputStream* const out) const { coleenp@4037: if (UseMallocOnly) { coleenp@4037: // Just print usage for now coleenp@4037: out->print_cr("usage %d", used_words(Metaspace::NonClassType)); coleenp@4037: } coleenp@4037: out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm()); coleenp@4037: vsm()->dump(out); coleenp@4037: out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm()); coleenp@4037: class_vsm()->dump(out); coleenp@4037: }