coleenp@4037: /* coleenp@4712: * Copyright (c) 2011, 2013, 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" hseigel@5528: #include "runtime/java.hpp" coleenp@4037: #include "runtime/mutex.hpp" coleenp@4295: #include "runtime/orderAccess.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: // 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; mgerdin@5699: size_t const allocation_from_dictionary_limit = 4 * K; coleenp@4037: coleenp@4037: MetaWord* last_allocated = 0; coleenp@4037: hseigel@5528: size_t Metaspace::_class_metaspace_size; hseigel@5528: coleenp@4037: // Used in declarations in SpaceManager and ChunkManager coleenp@4037: enum ChunkIndex { jmasa@4382: ZeroIndex = 0, jmasa@4382: SpecializedIndex = ZeroIndex, jmasa@4382: SmallIndex = SpecializedIndex + 1, jmasa@4382: MediumIndex = SmallIndex + 1, jmasa@4382: HumongousIndex = MediumIndex + 1, jmasa@4382: NumberOfFreeLists = 3, jmasa@4382: NumberOfInUseLists = 4 jmasa@4382: }; jmasa@4382: jmasa@4382: enum ChunkSizes { // in words. jmasa@4382: ClassSpecializedChunk = 128, jmasa@4382: SpecializedChunk = 128, jmasa@4382: ClassSmallChunk = 256, jmasa@4382: SmallChunk = 512, coleenp@5337: ClassMediumChunk = 4 * K, jmasa@4382: MediumChunk = 8 * K, jmasa@4382: HumongousChunkGranularity = 8 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: jmasa@4932: typedef class FreeList ChunkList; 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. jmasa@5007: // SpecializedChunk coleenp@4037: // SmallChunk coleenp@4037: // MediumChunk coleenp@4037: // HumongousChunk jmasa@4196: ChunkList _free_chunks[NumberOfFreeLists]; jmasa@4196: jmasa@4382: 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: jmasa@4382: // Map a size to a list index assuming that there are lists jmasa@4382: // for special, small, medium, and humongous chunks. jmasa@4382: static ChunkIndex list_index(size_t size); jmasa@4382: jmasa@5007: // Remove the chunk from its freelist. It is jmasa@5007: // expected to be on one of the _free_chunks[] lists. jmasa@5007: void remove_chunk(Metachunk* chunk); jmasa@5007: jmasa@4932: // Add the simple linked list of chunks to the freelist of chunks jmasa@4932: // of type index. jmasa@4932: void return_chunks(ChunkIndex index, Metachunk* chunks); jmasa@4932: coleenp@4037: // Total of the space in the free chunks list ehelin@5703: size_t free_chunks_total_words(); ehelin@5703: size_t free_chunks_total_bytes(); coleenp@4037: coleenp@4037: // Number of chunks in the free chunks list coleenp@4037: size_t free_chunks_count(); coleenp@4037: coleenp@4037: void inc_free_chunks_total(size_t v, size_t count = 1) { coleenp@4037: Atomic::add_ptr(count, &_free_chunks_count); coleenp@4037: Atomic::add_ptr(v, &_free_chunks_total); coleenp@4037: } jmasa@4196: ChunkTreeDictionary* humongous_dictionary() { jmasa@4196: return &_humongous_dictionary; jmasa@4196: } coleenp@4037: coleenp@4037: ChunkList* free_chunks(ChunkIndex index); coleenp@4037: coleenp@4037: // Returns the list for the given chunk word size. coleenp@4037: ChunkList* find_free_chunks_list(size_t word_size); coleenp@4037: 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: // 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: mgerdin@5699: // Only allocate and split from freelist if the size of the allocation mgerdin@5699: // is at least 1/4th the size of the available block. mgerdin@5699: const static int WasteMultiplier = 4; mgerdin@5699: coleenp@4037: // Accessors jmasa@4196: BlockTreeDictionary* dictionary() const { return _dictionary; } coleenp@4037: coleenp@4037: public: coleenp@4037: BlockFreelist(); coleenp@4037: ~BlockFreelist(); coleenp@4037: coleenp@4037: // Get and return a block to the free list jmasa@4196: MetaWord* get_block(size_t word_size); jmasa@4196: void return_block(MetaWord* p, size_t word_size); jmasa@4196: jmasa@4196: size_t total_size() { jmasa@4196: if (dictionary() == NULL) { coleenp@4037: return 0; jmasa@4196: } else { jmasa@4196: return dictionary()->total_size(); coleenp@4037: } jmasa@4196: } coleenp@4037: coleenp@4037: void print_on(outputStream* st) const; coleenp@4037: }; coleenp@4037: coleenp@4037: class VirtualSpaceNode : public CHeapObj { coleenp@4037: friend class VirtualSpaceList; coleenp@4037: coleenp@4037: // Link to next VirtualSpaceNode coleenp@4037: VirtualSpaceNode* _next; coleenp@4037: coleenp@4037: // total in the VirtualSpace coleenp@4037: MemRegion _reserved; coleenp@4037: ReservedSpace _rs; coleenp@4037: VirtualSpace _virtual_space; coleenp@4037: MetaWord* _top; jmasa@5007: // count of chunks contained in this VirtualSpace jmasa@5007: uintx _container_count; coleenp@4037: coleenp@4037: // Convenience functions to access the _virtual_space coleenp@4037: char* low() const { return virtual_space()->low(); } coleenp@4037: char* high() const { return virtual_space()->high(); } coleenp@4037: jmasa@5007: // The first Metachunk will be allocated at the bottom of the jmasa@5007: // VirtualSpace jmasa@5007: Metachunk* first_chunk() { return (Metachunk*) bottom(); } jmasa@5007: jmasa@5007: void inc_container_count(); jmasa@5007: #ifdef ASSERT jmasa@5007: uint container_count_slow(); jmasa@5007: #endif jmasa@5007: coleenp@4037: public: coleenp@4037: coleenp@4037: VirtualSpaceNode(size_t byte_size); jmasa@5007: VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {} coleenp@4037: ~VirtualSpaceNode(); coleenp@4037: hseigel@5528: // Convenience functions for logical bottom and end hseigel@5528: MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); } hseigel@5528: MetaWord* end() const { return (MetaWord*) _virtual_space.high(); } hseigel@5528: stefank@5704: size_t reserved_words() const { return _virtual_space.reserved_size() / BytesPerWord; } stefank@5704: size_t expanded_words() const { return _virtual_space.committed_size() / BytesPerWord; } stefank@5704: size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; } stefank@5704: coleenp@4037: // address of next available space in _virtual_space; coleenp@4037: // Accessors coleenp@4037: VirtualSpaceNode* next() { return _next; } coleenp@4037: void set_next(VirtualSpaceNode* v) { _next = v; } coleenp@4037: coleenp@4037: void set_reserved(MemRegion const v) { _reserved = v; } coleenp@4037: void set_top(MetaWord* v) { _top = v; } coleenp@4037: coleenp@4037: // Accessors coleenp@4037: MemRegion* reserved() { return &_reserved; } coleenp@4037: VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; } coleenp@4037: jmasa@5015: // Returns true if "word_size" is available in the VirtualSpace 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: jmasa@5007: uintx container_count() { return _container_count; } jmasa@5007: void dec_container_count(); jmasa@5007: #ifdef ASSERT jmasa@5007: void verify_container_count(); jmasa@5007: #endif jmasa@5007: coleenp@4037: // used and capacity in this single entry in the list coleenp@4037: size_t used_words_in_vs() const; coleenp@4037: size_t capacity_words_in_vs() const; jmasa@5015: size_t free_words_in_vs() const; coleenp@4037: coleenp@4037: bool initialize(); coleenp@4037: coleenp@4037: // get space from the virtual space coleenp@4037: Metachunk* take_from_committed(size_t chunk_word_size); coleenp@4037: coleenp@4037: // Allocate a chunk from the virtual space and return it. coleenp@4037: Metachunk* get_chunk_vs(size_t chunk_word_size); coleenp@4037: coleenp@4037: // Expands/shrinks the committed space in a virtual space. Delegates coleenp@4037: // to Virtualspace coleenp@4037: bool expand_by(size_t words, bool pre_touch = false); coleenp@4037: jmasa@5007: // In preparation for deleting this node, remove all the chunks jmasa@5007: // in the node from any freelist. jmasa@5007: void purge(ChunkManager* chunk_manager); jmasa@5007: coleenp@4304: #ifdef ASSERT coleenp@4037: // Debug support coleenp@4037: void mangle(); coleenp@4304: #endif coleenp@4037: coleenp@4037: void print_on(outputStream* st) const; coleenp@4037: }; coleenp@4037: coleenp@4037: // byte_size is the size of the associated virtualspace. stefank@5578: VirtualSpaceNode::VirtualSpaceNode(size_t byte_size) : _top(NULL), _next(NULL), _rs(), _container_count(0) { zgu@4752: // align up to vm allocation granularity zgu@4752: byte_size = align_size_up(byte_size, os::vm_allocation_granularity()); zgu@4752: coleenp@4804: // This allocates memory with mmap. For DumpSharedspaces, try to reserve coleenp@4804: // configurable address, generally at the top of the Java heap so other coleenp@4804: // memory addresses don't conflict. coleenp@4037: if (DumpSharedSpaces) { coleenp@4804: char* shared_base = (char*)SharedBaseAddress; coleenp@4037: _rs = ReservedSpace(byte_size, 0, false, shared_base, 0); coleenp@4037: if (_rs.is_reserved()) { coleenp@4804: assert(shared_base == 0 || _rs.base() == shared_base, "should match"); coleenp@4037: } else { coleenp@4804: // Get a mmap region anywhere if the SharedBaseAddress fails. 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: jmasa@5007: void VirtualSpaceNode::purge(ChunkManager* chunk_manager) { jmasa@5007: Metachunk* chunk = first_chunk(); jmasa@5007: Metachunk* invalid_chunk = (Metachunk*) top(); jmasa@5007: while (chunk < invalid_chunk ) { jmasa@5007: assert(chunk->is_free(), "Should be marked free"); jmasa@5007: MetaWord* next = ((MetaWord*)chunk) + chunk->word_size(); jmasa@5007: chunk_manager->remove_chunk(chunk); jmasa@5007: assert(chunk->next() == NULL && jmasa@5007: chunk->prev() == NULL, jmasa@5007: "Was not removed from its list"); jmasa@5007: chunk = (Metachunk*) next; jmasa@5007: } jmasa@5007: } jmasa@5007: jmasa@5007: #ifdef ASSERT jmasa@5007: uint VirtualSpaceNode::container_count_slow() { jmasa@5007: uint count = 0; jmasa@5007: Metachunk* chunk = first_chunk(); jmasa@5007: Metachunk* invalid_chunk = (Metachunk*) top(); jmasa@5007: while (chunk < invalid_chunk ) { jmasa@5007: MetaWord* next = ((MetaWord*)chunk) + chunk->word_size(); jmasa@5007: // Don't count the chunks on the free lists. Those are jmasa@5007: // still part of the VirtualSpaceNode but not currently jmasa@5007: // counted. jmasa@5007: if (!chunk->is_free()) { jmasa@5007: count++; jmasa@5007: } jmasa@5007: chunk = (Metachunk*) next; jmasa@5007: } jmasa@5007: return count; jmasa@5007: } jmasa@5007: #endif jmasa@5007: coleenp@4037: // List of VirtualSpaces for metadata allocation. coleenp@4037: // 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; ehelin@5694: bool can_grow() const { return !is_class() || !UseCompressedClassPointers; } coleenp@4037: stefank@5704: // Sum of reserved and committed memory in the virtual spaces stefank@5704: size_t _reserved_words; stefank@5704: size_t _committed_words; stefank@5704: stefank@5704: // Number of virtual spaces coleenp@4037: size_t _virtual_space_count; coleenp@4037: coleenp@4037: ~VirtualSpaceList(); coleenp@4037: coleenp@4037: VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; } coleenp@4037: coleenp@4037: void set_virtual_space_list(VirtualSpaceNode* v) { coleenp@4037: _virtual_space_list = v; coleenp@4037: } coleenp@4037: void set_current_virtual_space(VirtualSpaceNode* v) { coleenp@4037: _current_virtual_space = v; coleenp@4037: } coleenp@4037: stefank@5704: void link_vs(VirtualSpaceNode* new_entry); coleenp@4037: coleenp@4037: // Get another virtual space and add it to the list. This coleenp@4037: // is typically prompted by a failed attempt to allocate a chunk coleenp@4037: // and is typically followed by the allocation of a chunk. 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: jmasa@5015: size_t free_bytes(); jmasa@5015: jmasa@4382: Metachunk* get_new_chunk(size_t word_size, jmasa@4382: size_t grow_chunks_by_words, jmasa@4382: size_t medium_chunk_bunch); jmasa@4382: stefank@5704: bool expand_by(VirtualSpaceNode* node, size_t word_size, bool pre_touch = false); stefank@5704: jmasa@4382: // Get the first chunk for a Metaspace. Used for jmasa@4382: // special cases such as the boot class loader, reflection jmasa@4382: // class loader and anonymous class loader. jmasa@4382: Metachunk* get_initialization_chunk(size_t word_size, size_t chunk_bunch); 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: stefank@5704: size_t reserved_words() { return _reserved_words; } stefank@5704: size_t reserved_bytes() { return reserved_words() * BytesPerWord; } stefank@5704: size_t committed_words() { return _committed_words; } stefank@5704: size_t committed_bytes() { return committed_words() * BytesPerWord; } stefank@5704: stefank@5704: void inc_reserved_words(size_t v); stefank@5704: void dec_reserved_words(size_t v); stefank@5704: void inc_committed_words(size_t v); stefank@5704: void dec_committed_words(size_t v); jmasa@5007: void inc_virtual_space_count(); jmasa@5007: void dec_virtual_space_count(); jmasa@5007: jmasa@5007: // Unlink empty VirtualSpaceNodes and free it. jmasa@5007: void purge(); 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: jmasa@4382: coleenp@4037: // protects allocations and contains. coleenp@4037: Mutex* const _lock; coleenp@4037: jmasa@5162: // Type of metadata allocated. jmasa@5162: Metaspace::MetadataType _mdtype; jmasa@5162: jmasa@4382: // Chunk related size jmasa@4382: size_t _medium_chunk_bunch; jmasa@4382: 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: coleenp@4037: // Sum of all space in allocated chunks jmasa@5015: size_t _allocated_blocks_words; jmasa@5015: jmasa@5015: // Sum of all allocated chunks jmasa@5015: size_t _allocated_chunks_words; jmasa@5015: size_t _allocated_chunks_count; coleenp@4037: coleenp@4037: // Free lists of blocks are per SpaceManager since they coleenp@4037: // are assumed to be in chunks in use by the SpaceManager coleenp@4037: // and all chunks in use by a SpaceManager are freed when coleenp@4037: // the class loader using the SpaceManager is collected. coleenp@4037: BlockFreelist _block_freelists; coleenp@4037: coleenp@4037: // protects virtualspace and chunk expansions coleenp@4037: static const char* _expand_lock_name; coleenp@4037: static const int _expand_lock_rank; coleenp@4037: static Mutex* const _expand_lock; coleenp@4037: jmasa@4382: private: coleenp@4037: // Accessors coleenp@4037: Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; } coleenp@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: jmasa@5162: Metaspace::MetadataType mdtype() { return _mdtype; } 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); mgerdin@5699: void retire_current_chunk(); coleenp@4037: coleenp@4037: Mutex* lock() const { return _lock; } coleenp@4037: jmasa@4382: const char* chunk_size_name(ChunkIndex index) const; jmasa@4382: jmasa@4382: protected: jmasa@4382: void initialize(); jmasa@4382: coleenp@4037: public: jmasa@5162: SpaceManager(Metaspace::MetadataType mdtype, jmasa@5162: Mutex* lock, jmasa@4382: VirtualSpaceList* vs_list); coleenp@4037: ~SpaceManager(); coleenp@4037: jmasa@4382: enum ChunkMultiples { jmasa@4382: MediumChunkMultiple = 4 coleenp@4037: }; coleenp@4037: coleenp@4037: // Accessors jmasa@4382: size_t specialized_chunk_size() { return SpecializedChunk; } jmasa@4382: size_t small_chunk_size() { return (size_t) vs_list()->is_class() ? ClassSmallChunk : SmallChunk; } jmasa@4382: size_t medium_chunk_size() { return (size_t) vs_list()->is_class() ? ClassMediumChunk : MediumChunk; } jmasa@4382: size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; } jmasa@4382: jmasa@5015: size_t allocated_blocks_words() const { return _allocated_blocks_words; } jmasa@5015: size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; } jmasa@5015: size_t allocated_chunks_words() const { return _allocated_chunks_words; } jmasa@5015: size_t allocated_chunks_count() const { return _allocated_chunks_count; } jmasa@5015: jmasa@4382: bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); } coleenp@4037: coleenp@4037: static Mutex* expand_lock() { return _expand_lock; } coleenp@4037: jmasa@5015: // Increment the per Metaspace and global running sums for Metachunks jmasa@5015: // by the given size. This is used when a Metachunk to added to jmasa@5015: // the in-use list. jmasa@5015: void inc_size_metrics(size_t words); jmasa@5015: // Increment the per Metaspace and global running sums Metablocks by the given jmasa@5015: // size. This is used when a Metablock is allocated. jmasa@5015: void inc_used_metrics(size_t words); jmasa@5015: // Delete the portion of the running sums for this SpaceManager. That is, jmasa@5015: // the globals running sums for the Metachunks and Metablocks are jmasa@5015: // decremented for all the Metachunks in-use by this SpaceManager. jmasa@5015: void dec_total_from_size_metrics(); jmasa@5015: jmasa@4382: // Set the sizes for the initial chunks. jmasa@4382: void get_initial_chunk_sizes(Metaspace::MetaspaceType type, jmasa@4382: size_t* chunk_word_size, jmasa@4382: size_t* class_chunk_word_size); jmasa@4382: coleenp@4037: size_t sum_capacity_in_chunks_in_use() const; coleenp@4037: size_t sum_used_in_chunks_in_use() const; coleenp@4037: size_t sum_free_in_chunks_in_use() const; coleenp@4037: size_t sum_waste_in_chunks_in_use() const; coleenp@4037: size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const; coleenp@4037: coleenp@4037: size_t sum_count_in_chunks_in_use(); coleenp@4037: size_t sum_count_in_chunks_in_use(ChunkIndex i); coleenp@4037: jmasa@4382: Metachunk* get_new_chunk(size_t word_size, size_t grow_chunks_by_words); jmasa@4382: coleenp@4037: // Block allocation and deallocation. coleenp@4037: // Allocates a block from the current chunk coleenp@4037: MetaWord* allocate(size_t word_size); coleenp@4037: coleenp@4037: // Helper for allocations jmasa@4196: MetaWord* allocate_work(size_t word_size); coleenp@4037: coleenp@4037: // Returns a block to the per manager freelist jmasa@4196: void deallocate(MetaWord* p, size_t word_size); coleenp@4037: coleenp@4037: // Based on the allocation size and a minimum chunk size, coleenp@4037: // returned chunk size (for expanding space for chunk allocation). coleenp@4037: size_t calc_chunk_size(size_t allocation_word_size); coleenp@4037: coleenp@4037: // Called when an allocation from the current chunk fails. coleenp@4037: // Gets a new chunk (may require getting a new virtual space), coleenp@4037: // and allocates from that chunk. jmasa@4196: MetaWord* grow_and_allocate(size_t word_size); coleenp@4037: coleenp@4037: // debugging support. coleenp@4037: coleenp@4037: void dump(outputStream* const out) const; coleenp@4037: void print_on(outputStream* st) const; coleenp@4037: void locked_print_chunks_in_use_on(outputStream* st) const; coleenp@4037: coleenp@4037: void verify(); jmasa@4327: void verify_chunk_size(Metachunk* chunk); coleenp@4304: NOT_PRODUCT(void mangle_freed_chunks();) coleenp@4037: #ifdef ASSERT jmasa@5015: void verify_allocated_blocks_words(); coleenp@4037: #endif iklam@5208: iklam@5208: size_t get_raw_word_size(size_t word_size) { iklam@5208: // If only the dictionary is going to be used (i.e., no iklam@5208: // indexed free list), then there is a minimum size requirement. iklam@5208: // MinChunkSize is a placeholder for the real minimum size JJJ iklam@5208: size_t byte_size = word_size * BytesPerWord; iklam@5208: stefank@5705: size_t raw_bytes_size = MAX2(byte_size, iklam@5208: Metablock::min_block_byte_size()); iklam@5208: raw_bytes_size = ARENA_ALIGN(raw_bytes_size); iklam@5208: size_t raw_word_size = raw_bytes_size / BytesPerWord; iklam@5208: assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem"); iklam@5208: iklam@5208: return raw_word_size; iklam@5208: } coleenp@4037: }; coleenp@4037: coleenp@4037: uint const SpaceManager::_small_chunk_limit = 4; coleenp@4037: coleenp@4037: const char* SpaceManager::_expand_lock_name = coleenp@4037: "SpaceManager chunk allocation lock"; coleenp@4037: const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1; coleenp@4037: Mutex* const SpaceManager::_expand_lock = coleenp@4037: new Mutex(SpaceManager::_expand_lock_rank, coleenp@4037: SpaceManager::_expand_lock_name, coleenp@4037: Mutex::_allow_vm_block_flag); coleenp@4037: jmasa@5007: void VirtualSpaceNode::inc_container_count() { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: _container_count++; jmasa@5007: assert(_container_count == container_count_slow(), jmasa@5007: err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT jmasa@5007: "container_count_slow() " SIZE_FORMAT, jmasa@5007: _container_count, container_count_slow())); jmasa@5007: } jmasa@5007: jmasa@5007: void VirtualSpaceNode::dec_container_count() { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: _container_count--; jmasa@5007: } jmasa@5007: jmasa@5007: #ifdef ASSERT jmasa@5007: void VirtualSpaceNode::verify_container_count() { jmasa@5007: assert(_container_count == container_count_slow(), jmasa@5007: err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT jmasa@5007: "container_count_slow() " SIZE_FORMAT, _container_count, container_count_slow())); jmasa@5007: } jmasa@5007: #endif jmasa@5007: coleenp@4037: // BlockFreelist methods coleenp@4037: coleenp@4037: BlockFreelist::BlockFreelist() : _dictionary(NULL) {} coleenp@4037: coleenp@4037: BlockFreelist::~BlockFreelist() { coleenp@4037: if (_dictionary != NULL) { coleenp@4037: if (Verbose && TraceMetadataChunkAllocation) { coleenp@4037: _dictionary->print_free_lists(gclog_or_tty); coleenp@4037: } coleenp@4037: delete _dictionary; coleenp@4037: } coleenp@4037: } coleenp@4037: jmasa@4196: 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 = mgerdin@5699: dictionary()->get_chunk(word_size, FreeBlockDictionary::atLeast); jmasa@4196: if (free_block == NULL) { jmasa@4196: return NULL; jmasa@4196: } jmasa@4196: mgerdin@5699: const size_t block_size = free_block->size(); mgerdin@5699: if (block_size > WasteMultiplier * word_size) { mgerdin@5699: return_block((MetaWord*)free_block, block_size); mgerdin@5699: return NULL; mgerdin@5699: } mgerdin@5699: mgerdin@5699: MetaWord* new_block = (MetaWord*)free_block; mgerdin@5699: assert(block_size >= word_size, "Incorrect size of block from freelist"); mgerdin@5699: const size_t unused = block_size - word_size; mgerdin@5699: if (unused >= TreeChunk::min_size()) { mgerdin@5699: return_block(new_block + word_size, unused); mgerdin@5699: } mgerdin@5699: mgerdin@5699: return new_block; coleenp@4037: } coleenp@4037: coleenp@4037: void BlockFreelist::print_on(outputStream* st) const { coleenp@4037: if (dictionary() == NULL) { coleenp@4037: return; coleenp@4037: } coleenp@4037: dictionary()->print_free_lists(st); coleenp@4037: } coleenp@4037: coleenp@4037: // VirtualSpaceNode methods coleenp@4037: coleenp@4037: VirtualSpaceNode::~VirtualSpaceNode() { coleenp@4037: _rs.release(); jmasa@5007: #ifdef ASSERT jmasa@5007: size_t word_size = sizeof(*this) / BytesPerWord; jmasa@5007: Copy::fill_to_words((HeapWord*) this, word_size, 0xf1f1f1f1); jmasa@5007: #endif coleenp@4037: } coleenp@4037: coleenp@4037: size_t VirtualSpaceNode::used_words_in_vs() const { coleenp@4037: return pointer_delta(top(), bottom(), sizeof(MetaWord)); coleenp@4037: } coleenp@4037: coleenp@4037: // Space committed in the VirtualSpace coleenp@4037: size_t VirtualSpaceNode::capacity_words_in_vs() const { coleenp@4037: return pointer_delta(end(), bottom(), sizeof(MetaWord)); coleenp@4037: } coleenp@4037: jmasa@5015: size_t VirtualSpaceNode::free_words_in_vs() const { jmasa@5015: return pointer_delta(end(), top(), sizeof(MetaWord)); jmasa@5015: } coleenp@4037: coleenp@4037: // Allocates the chunk from the virtual space only. coleenp@4037: // This interface is also used internally for debugging. Not all coleenp@4037: // chunks removed here are necessarily used for allocation. coleenp@4037: Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) { coleenp@4037: // Bottom of the new chunk coleenp@4037: MetaWord* chunk_limit = top(); coleenp@4037: assert(chunk_limit != NULL, "Not safe to call this method"); coleenp@4037: coleenp@4037: if (!is_available(chunk_word_size)) { coleenp@4037: if (TraceMetadataChunkAllocation) { stefank@5708: gclog_or_tty->print("VirtualSpaceNode::take_from_committed() not available %d words ", chunk_word_size); coleenp@4037: // Dump some information about the virtual space that is nearly full stefank@5708: print_on(gclog_or_tty); coleenp@4037: } coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: coleenp@4037: // Take the space (bump top on the current virtual space). coleenp@4037: inc_top(chunk_word_size); coleenp@4037: jmasa@5007: // Initialize the chunk jmasa@5007: Metachunk* result = ::new (chunk_limit) Metachunk(chunk_word_size, this); coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: // Expand the virtual space (commit more of the reserved space) 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); stefank@5708: virtual_space()->print_on(gclog_or_tty); coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: Metachunk* result = take_from_committed(chunk_word_size); jmasa@5007: if (result != NULL) { jmasa@5007: inc_container_count(); jmasa@5007: } jmasa@5007: return result; coleenp@4037: } coleenp@4037: coleenp@4037: bool VirtualSpaceNode::initialize() { coleenp@4037: coleenp@4037: if (!_rs.is_reserved()) { coleenp@4037: return false; coleenp@4037: } coleenp@4037: jmasa@4382: // An allocation out of this Virtualspace that is larger jmasa@4382: // than an initial commit size can waste that initial committed jmasa@4382: // space. jmasa@4382: size_t committed_byte_size = 0; 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 ")", jmasa@4382: vs, capacity / K, jmasa@4382: capacity == 0 ? 0 : used * 100 / capacity, coleenp@4037: bottom(), top(), end(), coleenp@4037: vs->high_boundary()); coleenp@4037: } coleenp@4037: coleenp@4304: #ifdef ASSERT coleenp@4037: void VirtualSpaceNode::mangle() { coleenp@4037: size_t word_size = capacity_words_in_vs(); coleenp@4037: Copy::fill_to_words((HeapWord*) low(), word_size, 0xf1f1f1f1); coleenp@4037: } coleenp@4304: #endif // ASSERT coleenp@4037: coleenp@4037: // VirtualSpaceList methods coleenp@4037: // Space allocated from the VirtualSpace coleenp@4037: coleenp@4037: VirtualSpaceList::~VirtualSpaceList() { coleenp@4037: VirtualSpaceListIterator iter(virtual_space_list()); coleenp@4037: while (iter.repeat()) { coleenp@4037: VirtualSpaceNode* vsl = iter.get_next(); coleenp@4037: delete vsl; coleenp@4037: } coleenp@4037: } coleenp@4037: stefank@5704: void VirtualSpaceList::inc_reserved_words(size_t v) { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); stefank@5704: _reserved_words = _reserved_words + v; jmasa@5007: } stefank@5704: void VirtualSpaceList::dec_reserved_words(size_t v) { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); stefank@5704: _reserved_words = _reserved_words - v; stefank@5704: } stefank@5704: stefank@5704: void VirtualSpaceList::inc_committed_words(size_t v) { stefank@5704: assert_lock_strong(SpaceManager::expand_lock()); stefank@5704: _committed_words = _committed_words + v; stefank@5704: } stefank@5704: void VirtualSpaceList::dec_committed_words(size_t v) { stefank@5704: assert_lock_strong(SpaceManager::expand_lock()); stefank@5704: _committed_words = _committed_words - v; jmasa@5007: } jmasa@5007: jmasa@5007: void VirtualSpaceList::inc_virtual_space_count() { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: _virtual_space_count++; jmasa@5007: } jmasa@5007: void VirtualSpaceList::dec_virtual_space_count() { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: _virtual_space_count--; jmasa@5007: } jmasa@5007: jmasa@5007: void ChunkManager::remove_chunk(Metachunk* chunk) { jmasa@5007: size_t word_size = chunk->word_size(); jmasa@5007: ChunkIndex index = list_index(word_size); jmasa@5007: if (index != HumongousIndex) { jmasa@5007: free_chunks(index)->remove_chunk(chunk); jmasa@5007: } else { jmasa@5007: humongous_dictionary()->remove_chunk(chunk); jmasa@5007: } jmasa@5007: jmasa@5007: // Chunk is being removed from the chunks free list. jmasa@5007: dec_free_chunks_total(chunk->capacity_word_size()); jmasa@5007: } jmasa@5007: jmasa@5007: // Walk the list of VirtualSpaceNodes and delete jmasa@5007: // nodes with a 0 container_count. Remove Metachunks in jmasa@5007: // the node from their respective freelists. jmasa@5007: void VirtualSpaceList::purge() { jmasa@5007: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5007: // Don't use a VirtualSpaceListIterator because this jmasa@5007: // list is being changed and a straightforward use of an iterator is not safe. jmasa@5007: VirtualSpaceNode* purged_vsl = NULL; jmasa@5007: VirtualSpaceNode* prev_vsl = virtual_space_list(); jmasa@5007: VirtualSpaceNode* next_vsl = prev_vsl; jmasa@5007: while (next_vsl != NULL) { jmasa@5007: VirtualSpaceNode* vsl = next_vsl; jmasa@5007: next_vsl = vsl->next(); jmasa@5007: // Don't free the current virtual space since it will likely jmasa@5007: // be needed soon. jmasa@5007: if (vsl->container_count() == 0 && vsl != current_virtual_space()) { jmasa@5007: // Unlink it from the list jmasa@5007: if (prev_vsl == vsl) { jmasa@5007: // This is the case of the current note being the first note. jmasa@5007: assert(vsl == virtual_space_list(), "Expected to be the first note"); jmasa@5007: set_virtual_space_list(vsl->next()); jmasa@5007: } else { jmasa@5007: prev_vsl->set_next(vsl->next()); jmasa@5007: } jmasa@5007: jmasa@5007: vsl->purge(chunk_manager()); stefank@5704: dec_reserved_words(vsl->reserved_words()); stefank@5704: dec_committed_words(vsl->committed_words()); jmasa@5007: dec_virtual_space_count(); jmasa@5007: purged_vsl = vsl; jmasa@5007: delete vsl; jmasa@5007: } else { jmasa@5007: prev_vsl = vsl; jmasa@5007: } jmasa@5007: } jmasa@5007: #ifdef ASSERT jmasa@5007: if (purged_vsl != NULL) { jmasa@5007: // List should be stable enough to use an iterator here. jmasa@5007: VirtualSpaceListIterator iter(virtual_space_list()); jmasa@5007: while (iter.repeat()) { jmasa@5007: VirtualSpaceNode* vsl = iter.get_next(); jmasa@5007: assert(vsl != purged_vsl, "Purge of vsl failed"); jmasa@5007: } jmasa@5007: } jmasa@5007: #endif jmasa@5007: } jmasa@5007: coleenp@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: } ehelin@5703: assert(allocated_by_vs >= chunk_manager()->free_chunks_total_words(), coleenp@4037: err_msg("Total in free chunks " SIZE_FORMAT coleenp@4037: " greater than total from virtual_spaces " SIZE_FORMAT, ehelin@5703: allocated_by_vs, chunk_manager()->free_chunks_total_words())); coleenp@4037: size_t used = ehelin@5703: allocated_by_vs - chunk_manager()->free_chunks_total_words(); 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), stefank@5704: _reserved_words(0), stefank@5704: _committed_words(0), coleenp@4037: _virtual_space_count(0) { coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: bool initialization_succeeded = grow_vs(word_size); coleenp@4037: jmasa@4932: _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk); jmasa@4932: _chunk_manager.free_chunks(SmallIndex)->set_size(SmallChunk); jmasa@4932: _chunk_manager.free_chunks(MediumIndex)->set_size(MediumChunk); 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), stefank@5704: _reserved_words(0), stefank@5704: _committed_words(0), coleenp@4037: _virtual_space_count(0) { coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs); coleenp@4037: bool succeeded = class_entry->initialize(); jmasa@4932: _chunk_manager.free_chunks(SpecializedIndex)->set_size(SpecializedChunk); jmasa@4932: _chunk_manager.free_chunks(SmallIndex)->set_size(ClassSmallChunk); jmasa@4932: _chunk_manager.free_chunks(MediumIndex)->set_size(ClassMediumChunk); coleenp@4037: assert(succeeded, " VirtualSpaceList initialization should not fail"); stefank@5704: link_vs(class_entry); coleenp@4037: } coleenp@4037: jmasa@5015: size_t VirtualSpaceList::free_bytes() { jmasa@5015: return virtual_space_list()->free_words_in_vs() * BytesPerWord; jmasa@5015: } jmasa@5015: coleenp@4037: // Allocate another meta virtual space and add it to the list. 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 { stefank@5704: assert(new_entry->reserved_words() == vs_word_size, "Must be"); coleenp@4295: // ensure lock-free iteration sees fully initialized node coleenp@4295: OrderAccess::storestore(); stefank@5704: link_vs(new_entry); coleenp@4037: return true; coleenp@4037: } coleenp@4037: } coleenp@4037: stefank@5704: void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry) { coleenp@4037: if (virtual_space_list() == NULL) { coleenp@4037: set_virtual_space_list(new_entry); coleenp@4037: } else { coleenp@4037: current_virtual_space()->set_next(new_entry); coleenp@4037: } coleenp@4037: set_current_virtual_space(new_entry); stefank@5704: inc_reserved_words(new_entry->reserved_words()); stefank@5704: inc_committed_words(new_entry->committed_words()); coleenp@4037: inc_virtual_space_count(); coleenp@4037: #ifdef ASSERT coleenp@4037: new_entry->mangle(); coleenp@4037: #endif coleenp@4037: if (TraceMetavirtualspaceAllocation && Verbose) { coleenp@4037: VirtualSpaceNode* vsl = current_virtual_space(); stefank@5708: vsl->print_on(gclog_or_tty); coleenp@4037: } coleenp@4037: } coleenp@4037: stefank@5704: bool VirtualSpaceList::expand_by(VirtualSpaceNode* node, size_t word_size, bool pre_touch) { stefank@5704: size_t before = node->committed_words(); stefank@5704: stefank@5704: bool result = node->expand_by(word_size, pre_touch); stefank@5704: stefank@5704: size_t after = node->committed_words(); stefank@5704: stefank@5704: // after and before can be the same if the memory was pre-committed. stefank@5704: assert(after >= before, "Must be"); stefank@5704: inc_committed_words(after - before); stefank@5704: stefank@5704: return result; stefank@5704: } stefank@5704: coleenp@4037: Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size, jmasa@4382: size_t grow_chunks_by_words, jmasa@4382: size_t medium_chunk_bunch) { coleenp@4037: coleenp@4037: // Get a chunk from the chunk freelist coleenp@4037: Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words); coleenp@4037: jmasa@5007: if (next != NULL) { jmasa@5007: next->container()->inc_container_count(); jmasa@5007: } else { jmasa@5007: // Allocate a chunk out of the current virtual space. 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. jmasa@4382: size_t expand_vs_by_words = MAX2(medium_chunk_bunch, jmasa@4382: 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 = stefank@5704: expand_by(current_virtual_space(), aligned_expand_vs_by_words); 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. stefank@5704: assert(current_virtual_space()->expanded_words() == 0, stefank@5704: "New virtuals space nodes should not have expanded"); stefank@5704: stefank@5704: size_t grow_chunks_by_words_aligned = align_size_up(grow_chunks_by_words, stefank@5704: page_size_words); stefank@5704: // We probably want to expand by aligned_expand_vs_by_words here. stefank@5704: expand_by(current_virtual_space(), grow_chunks_by_words_aligned); stefank@5704: next = current_virtual_space()->get_chunk_vs(grow_chunks_by_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: jmasa@4382: assert(next == NULL || (next->next() == NULL && next->prev() == NULL), jmasa@4382: "New chunk is still on some list"); coleenp@4037: return next; coleenp@4037: } coleenp@4037: jmasa@4382: Metachunk* VirtualSpaceList::get_initialization_chunk(size_t chunk_word_size, jmasa@4382: size_t chunk_bunch) { jmasa@4382: // Get a chunk from the chunk freelist jmasa@4382: Metachunk* new_chunk = get_new_chunk(chunk_word_size, jmasa@4382: chunk_word_size, jmasa@4382: chunk_bunch); jmasa@4382: return new_chunk; jmasa@4382: } jmasa@4382: coleenp@4037: void VirtualSpaceList::print_on(outputStream* st) const { coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: VirtualSpaceListIterator iter(virtual_space_list()); coleenp@4037: while (iter.repeat()) { coleenp@4037: VirtualSpaceNode* node = iter.get_next(); coleenp@4037: node->print_on(st); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: 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: coleenp@4037: coleenp@4037: // MetaspaceGC methods coleenp@4037: coleenp@4037: // VM_CollectForMetadataAllocation is the vm operation used to GC. coleenp@4037: // Within the VM operation after the GC the attempt to allocate the metadata coleenp@4037: // should succeed. If the GC did not free enough space for the metaspace coleenp@4037: // allocation, the HWM is increased so that another virtualspace will be coleenp@4037: // allocated for the metadata. With perm gen the increase in the perm coleenp@4037: // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion. The coleenp@4037: // metaspace policy uses those as the small and large steps for the HWM. coleenp@4037: // coleenp@4037: // After the GC the compute_new_size() for MetaspaceGC is called to coleenp@4037: // resize the capacity of the metaspaces. The current implementation jmasa@5015: // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used coleenp@4037: // to resize the Java heap by some GC's. New flags can be implemented jmasa@5015: // if really needed. MinMetaspaceFreeRatio is used to calculate how much coleenp@4037: // free space is desirable in the metaspace capacity to decide how much jmasa@4581: // to increase the HWM. MaxMetaspaceFreeRatio is used to decide how much coleenp@4037: // free space is desirable in the metaspace capacity before decreasing coleenp@4037: // the HWM. coleenp@4037: coleenp@4037: // Calculate the amount to increase the high water mark (HWM). coleenp@4037: // Increase by a minimum amount (MinMetaspaceExpansion) so that coleenp@4037: // another expansion is not requested too soon. If that is not 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) { jmasa@5015: mgerdin@4790: // If the user wants a limit, impose one. coleenp@5337: // The reason for someone using this flag is to limit reserved space. So coleenp@5337: // for non-class virtual space, compare against virtual spaces that are reserved. coleenp@5337: // For class virtual space, we only compare against the committed space, not coleenp@5337: // reserved space, because this is a larger space prereserved for compressed coleenp@5337: // class pointers. coleenp@5337: if (!FLAG_IS_DEFAULT(MaxMetaspaceSize)) { stefank@5704: size_t real_allocated = Metaspace::space_list()->reserved_words() + coleenp@5337: MetaspaceAux::allocated_capacity_bytes(Metaspace::ClassType); coleenp@5337: if (real_allocated >= MaxMetaspaceSize) { coleenp@5337: return false; coleenp@5337: } mgerdin@4790: } coleenp@4037: coleenp@4037: // Class virtual space should always be expanded. Call GC for the other coleenp@4037: // metadata virtual space. hseigel@5528: if (Metaspace::using_class_space() && hseigel@5528: (vsl == Metaspace::class_space_list())) return true; coleenp@4037: coleenp@4037: // If this is part of an allocation after a GC, expand coleenp@4037: // unconditionally. jmasa@5015: if (MetaspaceGC::expand_after_GC()) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: jmasa@5015: 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@5337: size_t committed_capacity_bytes = MetaspaceAux::allocated_capacity_bytes(); coleenp@5337: size_t metaspace_size_bytes = MetaspaceSize; jmasa@5015: if (committed_capacity_bytes < metaspace_size_bytes || coleenp@4037: capacity_until_GC() == 0) { jmasa@5015: set_capacity_until_GC(metaspace_size_bytes); coleenp@4037: return true; coleenp@4037: } else { jmasa@5015: if (committed_capacity_bytes < 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 jmasa@5015: " allocated_capacity_bytes " SIZE_FORMAT, coleenp@4037: word_size, coleenp@4037: capacity_until_GC(), jmasa@5015: MetaspaceAux::allocated_capacity_bytes()); coleenp@4037: } coleenp@4037: return false; coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: jmasa@5015: 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: jmasa@5015: // Until a faster way of calculating the "used" quantity is implemented, jmasa@5015: // use "capacity". jmasa@5015: const size_t used_after_gc = MetaspaceAux::allocated_capacity_bytes(); jmasa@5015: const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC(); coleenp@4037: jmasa@4581: const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0; coleenp@4037: const double maximum_used_percentage = 1.0 - minimum_free_percentage; coleenp@4037: coleenp@4037: const double min_tmp = used_after_gc / maximum_used_percentage; coleenp@4037: size_t minimum_desired_capacity = coleenp@4037: (size_t)MIN2(min_tmp, double(max_uintx)); coleenp@4037: // Don't shrink less than the initial generation size coleenp@4037: minimum_desired_capacity = MAX2(minimum_desired_capacity, coleenp@4037: MetaspaceSize); coleenp@4037: coleenp@4037: if (PrintGCDetails && Verbose) { coleenp@4037: gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: "); coleenp@4037: gclog_or_tty->print_cr(" " coleenp@4037: " minimum_free_percentage: %6.2f" coleenp@4037: " maximum_used_percentage: %6.2f", coleenp@4037: minimum_free_percentage, coleenp@4037: maximum_used_percentage); coleenp@4037: gclog_or_tty->print_cr(" " jmasa@5015: " used_after_gc : %6.1fKB", jmasa@5015: used_after_gc / (double) K); coleenp@4037: } coleenp@4037: coleenp@4037: jmasa@5015: size_t shrink_bytes = 0; coleenp@4037: if (capacity_until_GC < minimum_desired_capacity) { coleenp@4037: // If we have less capacity below the metaspace HWM, then coleenp@4037: // increment the HWM. coleenp@4037: size_t expand_bytes = minimum_desired_capacity - capacity_until_GC; coleenp@4037: // Don't expand unless it's significant coleenp@4037: if (expand_bytes >= MinMetaspaceExpansion) { jmasa@5015: MetaspaceGC::set_capacity_until_GC(capacity_until_GC + expand_bytes); coleenp@4037: } coleenp@4037: if (PrintGCDetails && Verbose) { jmasa@5015: size_t new_capacity_until_GC = capacity_until_GC; coleenp@4037: gclog_or_tty->print_cr(" expanding:" jmasa@5015: " minimum_desired_capacity: %6.1fKB" jmasa@5015: " expand_bytes: %6.1fKB" jmasa@5015: " MinMetaspaceExpansion: %6.1fKB" jmasa@5015: " new metaspace HWM: %6.1fKB", 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: // We would never want to shrink more than this jmasa@5015: size_t max_shrink_bytes = capacity_until_GC - minimum_desired_capacity; jmasa@5015: assert(max_shrink_bytes >= 0, err_msg("max_shrink_bytes " SIZE_FORMAT, jmasa@5015: max_shrink_bytes)); coleenp@4037: coleenp@4037: // Should shrinking be considered? jmasa@4581: if (MaxMetaspaceFreeRatio < 100) { jmasa@4581: const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0; coleenp@4037: const double minimum_used_percentage = 1.0 - maximum_free_percentage; coleenp@4037: const double max_tmp = used_after_gc / minimum_used_percentage; coleenp@4037: size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); coleenp@4037: maximum_desired_capacity = MAX2(maximum_desired_capacity, coleenp@4037: MetaspaceSize); jmasa@5015: if (PrintGCDetails && Verbose) { coleenp@4037: gclog_or_tty->print_cr(" " coleenp@4037: " maximum_free_percentage: %6.2f" coleenp@4037: " minimum_used_percentage: %6.2f", coleenp@4037: maximum_free_percentage, coleenp@4037: minimum_used_percentage); coleenp@4037: gclog_or_tty->print_cr(" " jmasa@5015: " minimum_desired_capacity: %6.1fKB" jmasa@5015: " maximum_desired_capacity: %6.1fKB", coleenp@4037: minimum_desired_capacity / (double) K, coleenp@4037: maximum_desired_capacity / (double) K); coleenp@4037: } coleenp@4037: coleenp@4037: assert(minimum_desired_capacity <= maximum_desired_capacity, coleenp@4037: "sanity check"); coleenp@4037: coleenp@4037: if (capacity_until_GC > maximum_desired_capacity) { coleenp@4037: // Capacity too large, compute shrinking size jmasa@5015: shrink_bytes = capacity_until_GC - maximum_desired_capacity; coleenp@4037: // We don't want shrink all the way back to initSize if people call coleenp@4037: // System.gc(), because some programs do that between "phases" and then coleenp@4037: // we'd just have to grow the heap up again for the next phase. So we coleenp@4037: // damp the shrinking: 0% on the first call, 10% on the second call, 40% coleenp@4037: // on the third call, and 100% by the fourth call. But if we recompute coleenp@4037: // size without shrinking, it goes back to 0%. jmasa@5015: shrink_bytes = shrink_bytes / 100 * current_shrink_factor; jmasa@5015: assert(shrink_bytes <= max_shrink_bytes, coleenp@4037: err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT, jmasa@5015: shrink_bytes, max_shrink_bytes)); coleenp@4037: if (current_shrink_factor == 0) { coleenp@4037: _shrink_factor = 10; coleenp@4037: } else { coleenp@4037: _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100); coleenp@4037: } coleenp@4037: if (PrintGCDetails && Verbose) { coleenp@4037: gclog_or_tty->print_cr(" " coleenp@4037: " shrinking:" coleenp@4037: " initSize: %.1fK" coleenp@4037: " maximum_desired_capacity: %.1fK", coleenp@4037: MetaspaceSize / (double) K, coleenp@4037: maximum_desired_capacity / (double) K); coleenp@4037: gclog_or_tty->print_cr(" " jmasa@5015: " shrink_bytes: %.1fK" coleenp@4037: " current_shrink_factor: %d" coleenp@4037: " new shrink factor: %d" coleenp@4037: " MinMetaspaceExpansion: %.1fK", jmasa@5015: shrink_bytes / (double) K, coleenp@4037: current_shrink_factor, coleenp@4037: _shrink_factor, coleenp@4037: MinMetaspaceExpansion / (double) K); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Don't shrink unless it's significant jmasa@5015: if (shrink_bytes >= MinMetaspaceExpansion && jmasa@5015: ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) { jmasa@5015: MetaspaceGC::set_capacity_until_GC(capacity_until_GC - shrink_bytes); 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", ehelin@5703: vsl->chunk_manager()->free_chunks_total_words(), 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: // ChunkManager methods coleenp@4037: ehelin@5703: size_t ChunkManager::free_chunks_total_words() { coleenp@4037: return _free_chunks_total; coleenp@4037: } coleenp@4037: ehelin@5703: size_t ChunkManager::free_chunks_total_bytes() { ehelin@5703: return free_chunks_total_words() * BytesPerWord; coleenp@4037: } coleenp@4037: coleenp@4037: size_t ChunkManager::free_chunks_count() { coleenp@4037: #ifdef ASSERT coleenp@4037: if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) { coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: // This lock is only needed in debug because the verification coleenp@4037: // of the _free_chunks_totals walks the list of free chunks mgerdin@4264: slow_locked_verify_free_chunks_count(); coleenp@4037: } coleenp@4037: #endif mgerdin@4264: return _free_chunks_count; coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_verify_free_chunks_total() { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: assert(sum_free_chunks() == _free_chunks_total, coleenp@4037: err_msg("_free_chunks_total " SIZE_FORMAT " is not the" coleenp@4037: " same as sum " SIZE_FORMAT, _free_chunks_total, coleenp@4037: sum_free_chunks())); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::verify_free_chunks_total() { coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: locked_verify_free_chunks_total(); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_verify_free_chunks_count() { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: assert(sum_free_chunks_count() == _free_chunks_count, coleenp@4037: err_msg("_free_chunks_count " SIZE_FORMAT " is not the" coleenp@4037: " same as sum " SIZE_FORMAT, _free_chunks_count, coleenp@4037: sum_free_chunks_count())); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::verify_free_chunks_count() { coleenp@4037: #ifdef ASSERT coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: locked_verify_free_chunks_count(); coleenp@4037: #endif coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::verify() { mgerdin@4264: MutexLockerEx cl(SpaceManager::expand_lock(), mgerdin@4264: Mutex::_no_safepoint_check_flag); mgerdin@4264: locked_verify(); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_verify() { jmasa@4196: locked_verify_free_chunks_count(); coleenp@4037: locked_verify_free_chunks_total(); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_print_free_chunks(outputStream* st) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); jmasa@4382: st->print_cr("Free chunk total " SIZE_FORMAT " count " SIZE_FORMAT, coleenp@4037: _free_chunks_total, _free_chunks_count); coleenp@4037: } coleenp@4037: coleenp@4037: void ChunkManager::locked_print_sum_free_chunks(outputStream* st) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); jmasa@4382: st->print_cr("Sum free chunk total " SIZE_FORMAT " count " SIZE_FORMAT, coleenp@4037: sum_free_chunks(), sum_free_chunks_count()); coleenp@4037: } coleenp@4037: ChunkList* ChunkManager::free_chunks(ChunkIndex index) { coleenp@4037: return &_free_chunks[index]; coleenp@4037: } coleenp@4037: coleenp@4037: // These methods that sum the free chunk lists are used in printing coleenp@4037: // methods that are used in product builds. coleenp@4037: size_t ChunkManager::sum_free_chunks() { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: size_t result = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) { coleenp@4037: ChunkList* list = free_chunks(i); coleenp@4037: coleenp@4037: if (list == NULL) { coleenp@4037: continue; coleenp@4037: } coleenp@4037: jmasa@4932: result = result + list->count() * list->size(); coleenp@4037: } jmasa@4196: result = result + humongous_dictionary()->total_size(); coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: size_t ChunkManager::sum_free_chunks_count() { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); coleenp@4037: size_t count = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) { coleenp@4037: ChunkList* list = free_chunks(i); coleenp@4037: if (list == NULL) { coleenp@4037: continue; coleenp@4037: } jmasa@4932: count = count + list->count(); coleenp@4037: } jmasa@4196: count = count + humongous_dictionary()->total_free_blocks(); coleenp@4037: return count; coleenp@4037: } coleenp@4037: coleenp@4037: ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) { jmasa@4382: ChunkIndex index = list_index(word_size); jmasa@4382: assert(index < HumongousIndex, "No humongous list"); jmasa@4382: return free_chunks(index); coleenp@4037: } coleenp@4037: coleenp@4037: 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) { stefank@5708: gclog_or_tty->print_cr("ChunkManager::chunk_freelist_deallocate: chunk " stefank@5708: PTR_FORMAT " size " SIZE_FORMAT, stefank@5708: 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@4382: if (list_index(word_size) != HumongousIndex) { jmasa@4196: ChunkList* free_list = find_free_chunks_list(word_size); jmasa@4196: assert(free_list != NULL, "Sanity check"); jmasa@4196: jmasa@4196: chunk = free_list->head(); jmasa@4196: 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. jmasa@4932: free_list->remove_chunk(chunk); jmasa@4382: jmasa@4382: // Chunk is being removed from the chunks free list. jmasa@4196: dec_free_chunks_total(chunk->capacity_word_size()); coleenp@4037: coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { stefank@5708: gclog_or_tty->print_cr("ChunkManager::free_chunks_get: free_list " stefank@5708: PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT, stefank@5708: free_list, chunk, chunk->word_size()); coleenp@4037: } coleenp@4037: } else { jmasa@4196: chunk = humongous_dictionary()->get_chunk( jmasa@4196: word_size, jmasa@4196: FreeBlockDictionary::atLeast); jmasa@4196: jmasa@4196: if (chunk != NULL) { jmasa@4196: if (TraceMetadataHumongousAllocation) { jmasa@4196: size_t waste = chunk->word_size() - word_size; stefank@5708: gclog_or_tty->print_cr("Free list allocate humongous chunk size " stefank@5708: SIZE_FORMAT " for requested size " SIZE_FORMAT stefank@5708: " waste " SIZE_FORMAT, stefank@5708: 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@4382: } else { jmasa@4382: return NULL; coleenp@4037: } coleenp@4037: } jmasa@4382: jmasa@4382: // Remove it from the links to this freelist jmasa@4382: chunk->set_next(NULL); jmasa@4382: chunk->set_prev(NULL); jmasa@5007: #ifdef ASSERT jmasa@5007: // Chunk is no longer on any freelist. Setting to false make container_count_slow() jmasa@5007: // work. jmasa@5007: chunk->set_is_free(false); jmasa@5007: #endif mgerdin@4264: slow_locked_verify(); coleenp@4037: return chunk; coleenp@4037: } coleenp@4037: coleenp@4037: Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) { coleenp@4037: assert_lock_strong(SpaceManager::expand_lock()); mgerdin@4264: slow_locked_verify(); coleenp@4037: coleenp@4037: // Take from the beginning of the list coleenp@4037: Metachunk* chunk = free_chunks_get(word_size); coleenp@4037: if (chunk == NULL) { coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: jmasa@4382: assert((word_size <= chunk->word_size()) || jmasa@4382: list_index(chunk->word_size() == HumongousIndex), jmasa@4382: "Non-humongous variable sized chunk"); coleenp@4037: if (TraceMetadataChunkAllocation) { jmasa@4382: size_t list_count; jmasa@4382: if (list_index(word_size) < HumongousIndex) { jmasa@4382: ChunkList* list = find_free_chunks_list(word_size); jmasa@4932: list_count = list->count(); jmasa@4382: } else { jmasa@4382: list_count = humongous_dictionary()->total_count(); jmasa@4382: } stefank@5708: gclog_or_tty->print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk " stefank@5708: PTR_FORMAT " size " SIZE_FORMAT " count " SIZE_FORMAT " ", stefank@5708: this, chunk, chunk->word_size(), list_count); stefank@5708: locked_print_free_chunks(gclog_or_tty); coleenp@4037: } coleenp@4037: coleenp@4037: return chunk; coleenp@4037: } coleenp@4037: 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: jmasa@4382: void SpaceManager::get_initial_chunk_sizes(Metaspace::MetaspaceType type, jmasa@4382: size_t* chunk_word_size, jmasa@4382: size_t* class_chunk_word_size) { jmasa@4382: switch (type) { jmasa@4382: case Metaspace::BootMetaspaceType: jmasa@4382: *chunk_word_size = Metaspace::first_chunk_word_size(); jmasa@4382: *class_chunk_word_size = Metaspace::first_class_chunk_word_size(); jmasa@4382: break; jmasa@4382: case Metaspace::ROMetaspaceType: jmasa@4382: *chunk_word_size = SharedReadOnlySize / wordSize; jmasa@4382: *class_chunk_word_size = ClassSpecializedChunk; jmasa@4382: break; jmasa@4382: case Metaspace::ReadWriteMetaspaceType: jmasa@4382: *chunk_word_size = SharedReadWriteSize / wordSize; jmasa@4382: *class_chunk_word_size = ClassSpecializedChunk; jmasa@4382: break; jmasa@4382: case Metaspace::AnonymousMetaspaceType: jmasa@4382: case Metaspace::ReflectionMetaspaceType: jmasa@4382: *chunk_word_size = SpecializedChunk; jmasa@4382: *class_chunk_word_size = ClassSpecializedChunk; jmasa@4382: break; jmasa@4382: default: jmasa@4382: *chunk_word_size = SmallChunk; jmasa@4382: *class_chunk_word_size = ClassSmallChunk; jmasa@4382: break; jmasa@4382: } mikael@4548: assert(*chunk_word_size != 0 && *class_chunk_word_size != 0, jmasa@4382: err_msg("Initial chunks sizes bad: data " SIZE_FORMAT jmasa@4382: " class " SIZE_FORMAT, mikael@4548: *chunk_word_size, *class_chunk_word_size)); jmasa@4382: } jmasa@4382: coleenp@4037: size_t SpaceManager::sum_free_in_chunks_in_use() const { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: size_t free = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: Metachunk* chunk = chunks_in_use(i); coleenp@4037: while (chunk != NULL) { coleenp@4037: free += chunk->free_word_size(); coleenp@4037: chunk = chunk->next(); coleenp@4037: } coleenp@4037: } coleenp@4037: return free; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_waste_in_chunks_in_use() const { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: size_t result = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: result += sum_waste_in_chunks_in_use(i); coleenp@4037: } jmasa@4196: coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_waste_in_chunks_in_use(ChunkIndex index) const { coleenp@4037: size_t result = 0; coleenp@4037: Metachunk* chunk = chunks_in_use(index); coleenp@4037: // Count the free space in all the chunk but not the coleenp@4037: // current chunk from which allocations are still being done. coleenp@5337: while (chunk != NULL) { coleenp@5337: if (chunk != current_chunk()) { jmasa@4196: result += chunk->free_word_size(); coleenp@4037: } coleenp@5337: chunk = chunk->next(); coleenp@4037: } coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_capacity_in_chunks_in_use() const { jmasa@5015: // For CMS use "allocated_chunks_words()" which does not need the jmasa@5015: // Metaspace lock. For the other collectors sum over the jmasa@5015: // lists. Use both methods as a check that "allocated_chunks_words()" jmasa@5015: // is correct. That is, sum_capacity_in_chunks() is too expensive jmasa@5015: // to use in the product and allocated_chunks_words() should be used jmasa@5015: // but allow for checking that allocated_chunks_words() returns the same jmasa@5015: // value as sum_capacity_in_chunks_in_use() which is the definitive jmasa@5015: // answer. jmasa@5015: if (UseConcMarkSweepGC) { jmasa@5015: return allocated_chunks_words(); jmasa@5015: } else { jmasa@5015: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); jmasa@5015: size_t sum = 0; jmasa@5015: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { jmasa@5015: Metachunk* chunk = chunks_in_use(i); jmasa@5015: while (chunk != NULL) { jmasa@5015: sum += chunk->capacity_word_size(); jmasa@5015: chunk = chunk->next(); jmasa@5015: } coleenp@4037: } jmasa@5015: return sum; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_count_in_chunks_in_use() { coleenp@4037: size_t count = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: count = count + sum_count_in_chunks_in_use(i); coleenp@4037: } jmasa@4196: coleenp@4037: return count; coleenp@4037: } coleenp@4037: coleenp@4037: size_t SpaceManager::sum_count_in_chunks_in_use(ChunkIndex i) { coleenp@4037: size_t count = 0; coleenp@4037: Metachunk* chunk = chunks_in_use(i); coleenp@4037: while (chunk != NULL) { coleenp@4037: count++; coleenp@4037: chunk = chunk->next(); coleenp@4037: } coleenp@4037: return count; coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: size_t SpaceManager::sum_used_in_chunks_in_use() const { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: size_t used = 0; jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: Metachunk* chunk = chunks_in_use(i); coleenp@4037: while (chunk != NULL) { coleenp@4037: used += chunk->used_word_size(); coleenp@4037: chunk = chunk->next(); coleenp@4037: } coleenp@4037: } coleenp@4037: return used; coleenp@4037: } coleenp@4037: coleenp@4037: void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const { coleenp@4037: jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { jmasa@4382: Metachunk* chunk = chunks_in_use(i); jmasa@4382: st->print("SpaceManager: %s " PTR_FORMAT, jmasa@4382: chunk_size_name(i), chunk); jmasa@4382: if (chunk != NULL) { jmasa@4382: st->print_cr(" free " SIZE_FORMAT, jmasa@4382: chunk->free_word_size()); jmasa@4382: } else { jmasa@4382: st->print_cr(""); jmasa@4382: } jmasa@4382: } 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@5337: sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit) { jmasa@4382: chunk_word_size = (size_t) small_chunk_size(); jmasa@4382: if (word_size + Metachunk::overhead() > small_chunk_size()) { jmasa@4382: chunk_word_size = medium_chunk_size(); coleenp@4037: } coleenp@4037: } else { jmasa@4382: chunk_word_size = medium_chunk_size(); coleenp@4037: } coleenp@4037: jmasa@4382: // Might still need a humongous chunk. Enforce an jmasa@4382: // eight word granularity to facilitate reuse (some jmasa@4382: // wastage but better chance of reuse). jmasa@4382: size_t if_humongous_sized_chunk = jmasa@4382: align_size_up(word_size + Metachunk::overhead(), jmasa@4382: HumongousChunkGranularity); coleenp@4037: chunk_word_size = jmasa@4382: MAX2((size_t) chunk_word_size, if_humongous_sized_chunk); jmasa@4382: jmasa@4382: assert(!SpaceManager::is_humongous(word_size) || jmasa@4382: chunk_word_size == if_humongous_sized_chunk, jmasa@4382: err_msg("Size calculation is wrong, word_size " SIZE_FORMAT jmasa@4382: " chunk_word_size " SIZE_FORMAT, jmasa@4382: word_size, chunk_word_size)); coleenp@4037: if (TraceMetadataHumongousAllocation && coleenp@4037: SpaceManager::is_humongous(word_size)) { coleenp@4037: gclog_or_tty->print_cr("Metadata humongous allocation:"); coleenp@4037: gclog_or_tty->print_cr(" word_size " PTR_FORMAT, word_size); coleenp@4037: gclog_or_tty->print_cr(" chunk_word_size " PTR_FORMAT, coleenp@4037: chunk_word_size); jmasa@4196: gclog_or_tty->print_cr(" chunk overhead " PTR_FORMAT, coleenp@4037: Metachunk::overhead()); coleenp@4037: } coleenp@4037: return chunk_word_size; coleenp@4037: } coleenp@4037: jmasa@4196: MetaWord* SpaceManager::grow_and_allocate(size_t word_size) { coleenp@4037: assert(vs_list()->current_virtual_space() != NULL, coleenp@4037: "Should have been set"); coleenp@4037: assert(current_chunk() == NULL || coleenp@4037: current_chunk()->allocate(word_size) == NULL, coleenp@4037: "Don't need to expand"); coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: size_t words_left = 0; jmasa@4382: size_t words_used = 0; jmasa@4382: if (current_chunk() != NULL) { jmasa@4382: words_left = current_chunk()->free_word_size(); jmasa@4382: words_used = current_chunk()->used_word_size(); jmasa@4382: } coleenp@4037: gclog_or_tty->print_cr("SpaceManager::grow_and_allocate for " SIZE_FORMAT jmasa@4382: " words " SIZE_FORMAT " words used " SIZE_FORMAT jmasa@4382: " words left", jmasa@4382: word_size, words_used, words_left); coleenp@4037: } coleenp@4037: coleenp@4037: // Get another chunk out of the virtual space coleenp@4037: size_t grow_chunks_by_words = calc_chunk_size(word_size); jmasa@4382: Metachunk* next = get_new_chunk(word_size, grow_chunks_by_words); coleenp@4037: 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: jmasa@4382: for (ChunkIndex i = ZeroIndex; jmasa@4196: i < NumberOfInUseLists ; coleenp@4037: i = next_chunk_index(i) ) { coleenp@4037: st->print_cr(" chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT, coleenp@4037: chunks_in_use(i), coleenp@4037: chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size()); coleenp@4037: } coleenp@4037: st->print_cr(" waste: Small " SIZE_FORMAT " Medium " SIZE_FORMAT coleenp@4037: " Humongous " SIZE_FORMAT, coleenp@4037: sum_waste_in_chunks_in_use(SmallIndex), coleenp@4037: sum_waste_in_chunks_in_use(MediumIndex), coleenp@4037: sum_waste_in_chunks_in_use(HumongousIndex)); jmasa@4196: // block free lists jmasa@4196: if (block_freelists() != NULL) { jmasa@4196: st->print_cr("total in block free lists " SIZE_FORMAT, jmasa@4196: block_freelists()->total_size()); jmasa@4196: } coleenp@4037: } coleenp@4037: jmasa@5162: SpaceManager::SpaceManager(Metaspace::MetadataType mdtype, jmasa@5162: Mutex* lock, jmasa@4382: VirtualSpaceList* vs_list) : coleenp@4037: _vs_list(vs_list), jmasa@5162: _mdtype(mdtype), jmasa@5015: _allocated_blocks_words(0), jmasa@5015: _allocated_chunks_words(0), jmasa@5015: _allocated_chunks_count(0), jmasa@4382: _lock(lock) jmasa@4382: { jmasa@4382: initialize(); jmasa@4382: } jmasa@4382: jmasa@5015: void SpaceManager::inc_size_metrics(size_t words) { jmasa@5015: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5015: // Total of allocated Metachunks and allocated Metachunks count jmasa@5015: // for each SpaceManager jmasa@5015: _allocated_chunks_words = _allocated_chunks_words + words; jmasa@5015: _allocated_chunks_count++; jmasa@5015: // Global total of capacity in allocated Metachunks jmasa@5162: MetaspaceAux::inc_capacity(mdtype(), words); jmasa@5015: // Global total of allocated Metablocks. jmasa@5015: // used_words_slow() includes the overhead in each jmasa@5015: // Metachunk so include it in the used when the jmasa@5015: // Metachunk is first added (so only added once per jmasa@5015: // Metachunk). jmasa@5162: MetaspaceAux::inc_used(mdtype(), Metachunk::overhead()); jmasa@5015: } jmasa@5015: jmasa@5015: void SpaceManager::inc_used_metrics(size_t words) { jmasa@5015: // Add to the per SpaceManager total jmasa@5015: Atomic::add_ptr(words, &_allocated_blocks_words); jmasa@5015: // Add to the global total jmasa@5162: MetaspaceAux::inc_used(mdtype(), words); jmasa@5015: } jmasa@5015: jmasa@5015: void SpaceManager::dec_total_from_size_metrics() { jmasa@5162: MetaspaceAux::dec_capacity(mdtype(), allocated_chunks_words()); jmasa@5162: MetaspaceAux::dec_used(mdtype(), allocated_blocks_words()); jmasa@5015: // Also deduct the overhead per Metachunk jmasa@5162: MetaspaceAux::dec_used(mdtype(), allocated_chunks_count() * Metachunk::overhead()); jmasa@5015: } jmasa@5015: jmasa@4382: void SpaceManager::initialize() { coleenp@4037: Metadebug::init_allocation_fail_alot_count(); jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: _chunks_in_use[i] = NULL; coleenp@4037: } coleenp@4037: _current_chunk = NULL; coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print_cr("SpaceManager(): " PTR_FORMAT, this); coleenp@4037: } coleenp@4037: } coleenp@4037: jmasa@4932: void ChunkManager::return_chunks(ChunkIndex index, Metachunk* chunks) { jmasa@4932: if (chunks == NULL) { jmasa@4932: return; jmasa@4932: } jmasa@4932: ChunkList* list = free_chunks(index); jmasa@4932: assert(list->size() == chunks->word_size(), "Mismatch in chunk sizes"); jmasa@4932: assert_lock_strong(SpaceManager::expand_lock()); jmasa@4932: Metachunk* cur = chunks; jmasa@4932: jmasa@5007: // This returns chunks one at a time. If a new jmasa@4932: // class List can be created that is a base class jmasa@4932: // of FreeList then something like FreeList::prepend() jmasa@4932: // can be used in place of this loop jmasa@4932: while (cur != NULL) { jmasa@5007: assert(cur->container() != NULL, "Container should have been set"); jmasa@5007: cur->container()->dec_container_count(); jmasa@4932: // Capture the next link before it is changed jmasa@4932: // by the call to return_chunk_at_head(); jmasa@4932: Metachunk* next = cur->next(); jmasa@4932: cur->set_is_free(true); jmasa@4932: list->return_chunk_at_head(cur); jmasa@4932: cur = next; jmasa@4932: } jmasa@4932: } jmasa@4932: coleenp@4037: SpaceManager::~SpaceManager() { mgerdin@4784: // This call this->_lock which can't be done while holding expand_lock() jmasa@5015: assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(), jmasa@5015: err_msg("sum_capacity_in_chunks_in_use() " SIZE_FORMAT jmasa@5015: " allocated_chunks_words() " SIZE_FORMAT, jmasa@5015: sum_capacity_in_chunks_in_use(), allocated_chunks_words())); mgerdin@4784: coleenp@4037: MutexLockerEx fcl(SpaceManager::expand_lock(), coleenp@4037: Mutex::_no_safepoint_check_flag); coleenp@4037: coleenp@4037: ChunkManager* chunk_manager = vs_list()->chunk_manager(); coleenp@4037: mgerdin@4264: chunk_manager->slow_locked_verify(); coleenp@4037: jmasa@5015: dec_total_from_size_metrics(); jmasa@5015: coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this); coleenp@4037: locked_print_chunks_in_use_on(gclog_or_tty); coleenp@4037: } coleenp@4037: jmasa@5007: // Do not mangle freed Metachunks. The chunk size inside Metachunks jmasa@5007: // is during the freeing of a VirtualSpaceNodes. coleenp@4304: coleenp@4037: // Have to update before the chunks_in_use lists are emptied coleenp@4037: // below. jmasa@5015: chunk_manager->inc_free_chunks_total(allocated_chunks_words(), 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: jmasa@4382: // Follow each list of chunks-in-use and add them to the jmasa@4382: // free lists. Each list is NULL terminated. jmasa@4382: jmasa@4382: for (ChunkIndex i = ZeroIndex; i < HumongousIndex; i = next_chunk_index(i)) { jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: gclog_or_tty->print_cr("returned %d %s chunks to freelist", jmasa@4382: sum_count_in_chunks_in_use(i), jmasa@4382: chunk_size_name(i)); jmasa@4382: } jmasa@4382: Metachunk* chunks = chunks_in_use(i); jmasa@4932: chunk_manager->return_chunks(i, chunks); jmasa@4382: set_chunks_in_use(i, NULL); jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: gclog_or_tty->print_cr("updated freelist count %d %s", jmasa@4932: chunk_manager->free_chunks(i)->count(), jmasa@4382: chunk_size_name(i)); jmasa@4382: } jmasa@4382: assert(i != HumongousIndex, "Humongous chunks are handled explicitly later"); coleenp@4037: } coleenp@4037: jmasa@4382: // The medium chunk case may be optimized by passing the head and jmasa@4382: // tail of the medium chunk list to add_at_head(). The tail is often jmasa@4382: // the current chunk but there are probably exceptions. jmasa@4382: coleenp@4037: // Humongous chunks jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: gclog_or_tty->print_cr("returned %d %s humongous chunks to dictionary", jmasa@4382: sum_count_in_chunks_in_use(HumongousIndex), jmasa@4382: chunk_size_name(HumongousIndex)); jmasa@4382: gclog_or_tty->print("Humongous chunk dictionary: "); jmasa@4382: } coleenp@4037: // Humongous chunks are never the current chunk. coleenp@4037: Metachunk* humongous_chunks = chunks_in_use(HumongousIndex); coleenp@4037: jmasa@4196: while (humongous_chunks != NULL) { jmasa@4196: #ifdef ASSERT jmasa@4196: humongous_chunks->set_is_free(true); jmasa@4196: #endif jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: gclog_or_tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ", jmasa@4382: humongous_chunks, jmasa@4382: humongous_chunks->word_size()); jmasa@4382: } jmasa@4382: assert(humongous_chunks->word_size() == (size_t) jmasa@4382: align_size_up(humongous_chunks->word_size(), jmasa@4382: HumongousChunkGranularity), jmasa@4382: err_msg("Humongous chunk size is wrong: word size " SIZE_FORMAT mikael@4548: " granularity %d", jmasa@4382: humongous_chunks->word_size(), HumongousChunkGranularity)); jmasa@4196: Metachunk* next_humongous_chunks = humongous_chunks->next(); jmasa@5007: humongous_chunks->container()->dec_container_count(); jmasa@4196: chunk_manager->humongous_dictionary()->return_chunk(humongous_chunks); jmasa@4196: humongous_chunks = next_humongous_chunks; coleenp@4037: } jmasa@4382: if (TraceMetadataChunkAllocation && Verbose) { jmasa@4382: gclog_or_tty->print_cr(""); jmasa@4382: gclog_or_tty->print_cr("updated dictionary count %d %s", jmasa@4382: chunk_manager->humongous_dictionary()->total_count(), jmasa@4382: chunk_size_name(HumongousIndex)); jmasa@4382: } mgerdin@4264: chunk_manager->slow_locked_verify(); coleenp@4037: } coleenp@4037: jmasa@4382: const char* SpaceManager::chunk_size_name(ChunkIndex index) const { jmasa@4382: switch (index) { jmasa@4382: case SpecializedIndex: jmasa@4382: return "Specialized"; jmasa@4382: case SmallIndex: jmasa@4382: return "Small"; jmasa@4382: case MediumIndex: jmasa@4382: return "Medium"; jmasa@4382: case HumongousIndex: jmasa@4382: return "Humongous"; jmasa@4382: default: jmasa@4382: return NULL; jmasa@4382: } jmasa@4382: } jmasa@4382: jmasa@4382: ChunkIndex ChunkManager::list_index(size_t size) { jmasa@4382: switch (size) { jmasa@4382: case SpecializedChunk: jmasa@4382: assert(SpecializedChunk == ClassSpecializedChunk, jmasa@4382: "Need branch for ClassSpecializedChunk"); jmasa@4382: return SpecializedIndex; jmasa@4382: case SmallChunk: jmasa@4382: case ClassSmallChunk: jmasa@4382: return SmallIndex; jmasa@4382: case MediumChunk: jmasa@4382: case ClassMediumChunk: jmasa@4382: return MediumIndex; jmasa@4382: default: jmasa@4383: assert(size > MediumChunk || size > ClassMediumChunk, jmasa@4382: "Not a humongous chunk"); jmasa@4382: return HumongousIndex; jmasa@4382: } jmasa@4382: } jmasa@4382: jmasa@4196: void SpaceManager::deallocate(MetaWord* p, size_t word_size) { coleenp@4037: assert_lock_strong(_lock); fparain@5452: size_t raw_word_size = get_raw_word_size(word_size); jmasa@4196: size_t min_size = TreeChunk::min_size(); fparain@5452: assert(raw_word_size >= min_size, hseigel@5528: err_msg("Should not deallocate dark matter " SIZE_FORMAT "<" SIZE_FORMAT, word_size, min_size)); fparain@5452: block_freelists()->return_block(p, raw_word_size); coleenp@4037: } coleenp@4037: coleenp@4037: // Adds a chunk to the list of chunks in use. coleenp@4037: void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) { coleenp@4037: coleenp@4037: assert(new_chunk != NULL, "Should not be NULL"); coleenp@4037: assert(new_chunk->next() == NULL, "Should not be on a list"); coleenp@4037: coleenp@4037: new_chunk->reset_empty(); coleenp@4037: coleenp@4037: // Find the correct list and and set the current coleenp@4037: // chunk for that list. jmasa@4382: ChunkIndex index = ChunkManager::list_index(new_chunk->word_size()); jmasa@4382: jmasa@4382: if (index != HumongousIndex) { mgerdin@5699: retire_current_chunk(); coleenp@4037: set_current_chunk(new_chunk); jmasa@4382: new_chunk->set_next(chunks_in_use(index)); jmasa@4382: set_chunks_in_use(index, new_chunk); jmasa@4382: } else { coleenp@4037: // For null class loader data and DumpSharedSpaces, the first chunk isn't coleenp@4037: // small, so small will be null. Link this first chunk as the current coleenp@4037: // chunk. coleenp@4037: if (make_current) { coleenp@4037: // Set as the current chunk but otherwise treat as a humongous chunk. coleenp@4037: set_current_chunk(new_chunk); coleenp@4037: } coleenp@4037: // Link at head. The _current_chunk only points to a humongous chunk for coleenp@4037: // the null class loader metaspace (class and data virtual space managers) coleenp@4037: // any humongous chunks so will not point to the tail coleenp@4037: // of the humongous chunks list. coleenp@4037: new_chunk->set_next(chunks_in_use(HumongousIndex)); coleenp@4037: set_chunks_in_use(HumongousIndex, new_chunk); coleenp@4037: jmasa@4383: assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency"); coleenp@4037: } coleenp@4037: jmasa@5015: // Add to the running sum of capacity jmasa@5015: inc_size_metrics(new_chunk->word_size()); jmasa@5015: coleenp@4037: assert(new_chunk->is_empty(), "Not ready for reuse"); coleenp@4037: if (TraceMetadataChunkAllocation && Verbose) { coleenp@4037: gclog_or_tty->print("SpaceManager::add_chunk: %d) ", coleenp@4037: sum_count_in_chunks_in_use()); coleenp@4037: new_chunk->print_on(gclog_or_tty); jmasa@5015: if (vs_list() != NULL) { stefank@5708: vs_list()->chunk_manager()->locked_print_free_chunks(gclog_or_tty); jmasa@5015: } coleenp@4037: } coleenp@4037: } coleenp@4037: mgerdin@5699: void SpaceManager::retire_current_chunk() { mgerdin@5699: if (current_chunk() != NULL) { mgerdin@5699: size_t remaining_words = current_chunk()->free_word_size(); mgerdin@5699: if (remaining_words >= TreeChunk::min_size()) { mgerdin@5699: block_freelists()->return_block(current_chunk()->allocate(remaining_words), remaining_words); mgerdin@5699: inc_used_metrics(remaining_words); mgerdin@5699: } mgerdin@5699: } mgerdin@5699: } mgerdin@5699: jmasa@4382: Metachunk* SpaceManager::get_new_chunk(size_t word_size, jmasa@4382: size_t grow_chunks_by_words) { jmasa@4382: jmasa@4382: Metachunk* next = vs_list()->get_new_chunk(word_size, jmasa@4382: grow_chunks_by_words, jmasa@4382: medium_chunk_bunch()); jmasa@4382: stefank@5707: if (TraceMetadataHumongousAllocation && next != NULL && jmasa@4382: SpaceManager::is_humongous(next->word_size())) { stefank@5707: gclog_or_tty->print_cr(" new humongous chunk word size " stefank@5707: PTR_FORMAT, next->word_size()); jmasa@4382: } jmasa@4382: jmasa@4382: return next; jmasa@4382: } jmasa@4382: coleenp@4037: MetaWord* SpaceManager::allocate(size_t word_size) { coleenp@4037: MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: iklam@5208: size_t raw_word_size = get_raw_word_size(word_size); coleenp@4037: BlockFreelist* fl = block_freelists(); jmasa@4196: MetaWord* p = NULL; coleenp@4037: // Allocation from the dictionary is expensive in the sense that coleenp@4037: // the dictionary has to be searched for a size. Don't allocate coleenp@4037: // from the dictionary until it starts to get fat. Is this coleenp@4037: // a reasonable policy? Maybe an skinny dictionary is fast enough coleenp@4037: // for allocations. Do some profiling. JJJ jmasa@4196: if (fl->total_size() > allocation_from_dictionary_limit) { jmasa@4196: p = fl->get_block(raw_word_size); coleenp@4037: } jmasa@4196: if (p == NULL) { jmasa@4196: p = allocate_work(raw_word_size); coleenp@4037: } coleenp@4037: 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"); jmasa@5015: inc_used_metrics(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: } hseigel@5528: if (result != 0) { jmasa@5015: inc_used_metrics(word_size); jmasa@4196: assert(result != (MetaWord*) chunks_in_use(MediumIndex), jmasa@4196: "Head of the list is being allocated"); coleenp@4037: } coleenp@4037: coleenp@4037: return result; coleenp@4037: } coleenp@4037: coleenp@4037: void SpaceManager::verify() { coleenp@4037: // If there are blocks in the dictionary, then coleenp@4037: // verfication of chunks does not work since coleenp@4037: // being in the dictionary alters a chunk. jmasa@4196: if (block_freelists()->total_size() == 0) { jmasa@4382: for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) { coleenp@4037: Metachunk* curr = chunks_in_use(i); coleenp@4037: while (curr != NULL) { coleenp@4037: curr->verify(); jmasa@4327: verify_chunk_size(curr); coleenp@4037: curr = curr->next(); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: jmasa@4327: void SpaceManager::verify_chunk_size(Metachunk* chunk) { jmasa@4327: assert(is_humongous(chunk->word_size()) || jmasa@4382: chunk->word_size() == medium_chunk_size() || jmasa@4382: chunk->word_size() == small_chunk_size() || jmasa@4382: chunk->word_size() == specialized_chunk_size(), jmasa@4327: "Chunk size is wrong"); jmasa@4327: return; jmasa@4327: } jmasa@4327: coleenp@4037: #ifdef ASSERT jmasa@5015: void SpaceManager::verify_allocated_blocks_words() { coleenp@4037: // Verification is only guaranteed at a safepoint. jmasa@5015: assert(SafepointSynchronize::is_at_safepoint() || !Universe::is_fully_initialized(), jmasa@5015: "Verification can fail if the applications is running"); jmasa@5015: assert(allocated_blocks_words() == sum_used_in_chunks_in_use(), mikael@4548: err_msg("allocation total is not consistent " SIZE_FORMAT mikael@4548: " vs " SIZE_FORMAT, jmasa@5015: allocated_blocks_words(), sum_used_in_chunks_in_use())); coleenp@4037: } coleenp@4037: coleenp@4037: #endif coleenp@4037: coleenp@4037: void SpaceManager::dump(outputStream* const out) const { coleenp@4037: size_t curr_total = 0; coleenp@4037: size_t waste = 0; coleenp@4037: uint i = 0; coleenp@4037: size_t used = 0; coleenp@4037: size_t capacity = 0; coleenp@4037: coleenp@4037: // Add up statistics for all chunks in this SpaceManager. jmasa@4382: for (ChunkIndex index = ZeroIndex; jmasa@4196: index < NumberOfInUseLists; coleenp@4037: index = next_chunk_index(index)) { coleenp@4037: for (Metachunk* curr = chunks_in_use(index); coleenp@4037: curr != NULL; coleenp@4037: curr = curr->next()) { coleenp@4037: out->print("%d) ", i++); coleenp@4037: curr->print_on(out); coleenp@4037: curr_total += curr->word_size(); coleenp@4037: used += curr->used_word_size(); coleenp@4037: capacity += curr->capacity_word_size(); coleenp@4037: waste += curr->free_word_size() + curr->overhead();; coleenp@4037: } coleenp@4037: } coleenp@4037: stefank@5707: if (TraceMetadataChunkAllocation && Verbose) { stefank@5707: block_freelists()->print_on(out); stefank@5707: } stefank@5707: jmasa@4382: size_t free = current_chunk() == NULL ? 0 : current_chunk()->free_word_size(); coleenp@4037: // Free space isn't wasted. coleenp@4037: waste -= free; coleenp@4037: coleenp@4037: out->print_cr("total of all chunks " SIZE_FORMAT " used " SIZE_FORMAT coleenp@4037: " free " SIZE_FORMAT " capacity " SIZE_FORMAT coleenp@4037: " waste " SIZE_FORMAT, curr_total, used, free, capacity, waste); coleenp@4037: } coleenp@4037: coleenp@4304: #ifndef PRODUCT coleenp@4037: void SpaceManager::mangle_freed_chunks() { jmasa@4382: for (ChunkIndex index = ZeroIndex; jmasa@4196: index < NumberOfInUseLists; coleenp@4037: index = next_chunk_index(index)) { coleenp@4037: for (Metachunk* curr = chunks_in_use(index); coleenp@4037: curr != NULL; coleenp@4037: curr = curr->next()) { coleenp@4037: curr->mangle(); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4304: #endif // PRODUCT coleenp@4037: coleenp@4037: // MetaspaceAux coleenp@4037: jmasa@5015: jmasa@5162: size_t MetaspaceAux::_allocated_capacity_words[] = {0, 0}; jmasa@5162: size_t MetaspaceAux::_allocated_used_words[] = {0, 0}; jmasa@5015: ehelin@5531: size_t MetaspaceAux::free_bytes(Metaspace::MetadataType mdtype) { ehelin@5531: VirtualSpaceList* list = Metaspace::get_space_list(mdtype); ehelin@5531: return list == NULL ? 0 : list->free_bytes(); ehelin@5531: } ehelin@5531: jmasa@5015: size_t MetaspaceAux::free_bytes() { ehelin@5531: return free_bytes(Metaspace::ClassType) + free_bytes(Metaspace::NonClassType); jmasa@5015: } jmasa@5015: jmasa@5162: void MetaspaceAux::dec_capacity(Metaspace::MetadataType mdtype, size_t words) { jmasa@5015: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5162: assert(words <= allocated_capacity_words(mdtype), jmasa@5015: err_msg("About to decrement below 0: words " SIZE_FORMAT jmasa@5162: " is greater than _allocated_capacity_words[%u] " SIZE_FORMAT, jmasa@5162: words, mdtype, allocated_capacity_words(mdtype))); jmasa@5162: _allocated_capacity_words[mdtype] -= words; jmasa@5015: } jmasa@5015: jmasa@5162: void MetaspaceAux::inc_capacity(Metaspace::MetadataType mdtype, size_t words) { jmasa@5015: assert_lock_strong(SpaceManager::expand_lock()); jmasa@5015: // Needs to be atomic jmasa@5162: _allocated_capacity_words[mdtype] += words; jmasa@5015: } jmasa@5015: jmasa@5162: void MetaspaceAux::dec_used(Metaspace::MetadataType mdtype, size_t words) { jmasa@5162: assert(words <= allocated_used_words(mdtype), jmasa@5015: err_msg("About to decrement below 0: words " SIZE_FORMAT jmasa@5162: " is greater than _allocated_used_words[%u] " SIZE_FORMAT, jmasa@5162: words, mdtype, allocated_used_words(mdtype))); jmasa@5015: // For CMS deallocation of the Metaspaces occurs during the jmasa@5015: // sweep which is a concurrent phase. Protection by the expand_lock() jmasa@5015: // is not enough since allocation is on a per Metaspace basis jmasa@5015: // and protected by the Metaspace lock. jmasa@5015: jlong minus_words = (jlong) - (jlong) words; jmasa@5162: Atomic::add_ptr(minus_words, &_allocated_used_words[mdtype]); jmasa@5015: } jmasa@5015: jmasa@5162: void MetaspaceAux::inc_used(Metaspace::MetadataType mdtype, size_t words) { jmasa@5015: // _allocated_used_words tracks allocations for jmasa@5015: // each piece of metadata. Those allocations are jmasa@5015: // generally done concurrently by different application jmasa@5015: // threads so must be done atomically. jmasa@5162: Atomic::add_ptr(words, &_allocated_used_words[mdtype]); jmasa@5015: } jmasa@5015: jmasa@5015: size_t MetaspaceAux::used_bytes_slow(Metaspace::MetadataType mdtype) { jmasa@4042: size_t used = 0; jmasa@4042: ClassLoaderDataGraphMetaspaceIterator iter; jmasa@4042: while (iter.repeat()) { jmasa@4042: Metaspace* msp = iter.get_next(); jmasa@5015: // Sum allocated_blocks_words for each metaspace jmasa@4042: if (msp != NULL) { jmasa@5015: used += msp->used_words_slow(mdtype); jmasa@4042: } jmasa@4042: } jmasa@4042: return used * BytesPerWord; jmasa@4042: } jmasa@4042: ehelin@5703: size_t MetaspaceAux::free_bytes_slow(Metaspace::MetadataType mdtype) { coleenp@4037: size_t free = 0; coleenp@4037: ClassLoaderDataGraphMetaspaceIterator iter; coleenp@4037: while (iter.repeat()) { coleenp@4037: Metaspace* msp = iter.get_next(); coleenp@4037: if (msp != NULL) { ehelin@5703: free += msp->free_words_slow(mdtype); coleenp@4037: } coleenp@4037: } coleenp@4037: return free * BytesPerWord; coleenp@4037: } coleenp@4037: jmasa@5015: size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) { hseigel@5528: if ((mdtype == Metaspace::ClassType) && !Metaspace::using_class_space()) { hseigel@5528: return 0; hseigel@5528: } jmasa@5015: // Don't count the space in the freelists. That space will be jmasa@5015: // added to the capacity calculation as needed. jmasa@5015: size_t capacity = 0; coleenp@4037: ClassLoaderDataGraphMetaspaceIterator iter; coleenp@4037: while (iter.repeat()) { coleenp@4037: Metaspace* msp = iter.get_next(); coleenp@4037: if (msp != NULL) { jmasa@5015: capacity += msp->capacity_words_slow(mdtype); coleenp@4037: } coleenp@4037: } coleenp@4037: return capacity * BytesPerWord; coleenp@4037: } coleenp@4037: ehelin@5703: size_t MetaspaceAux::capacity_bytes_slow() { ehelin@5703: #ifdef PRODUCT ehelin@5703: // Use allocated_capacity_bytes() in PRODUCT instead of this function. ehelin@5703: guarantee(false, "Should not call capacity_bytes_slow() in the PRODUCT"); ehelin@5703: #endif ehelin@5703: size_t class_capacity = capacity_bytes_slow(Metaspace::ClassType); ehelin@5703: size_t non_class_capacity = capacity_bytes_slow(Metaspace::NonClassType); ehelin@5703: assert(allocated_capacity_bytes() == class_capacity + non_class_capacity, ehelin@5703: err_msg("bad accounting: allocated_capacity_bytes() " SIZE_FORMAT ehelin@5703: " class_capacity + non_class_capacity " SIZE_FORMAT ehelin@5703: " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT, ehelin@5703: allocated_capacity_bytes(), class_capacity + non_class_capacity, ehelin@5703: class_capacity, non_class_capacity)); ehelin@5703: ehelin@5703: return class_capacity + non_class_capacity; ehelin@5703: } ehelin@5703: ehelin@5703: size_t MetaspaceAux::reserved_bytes(Metaspace::MetadataType mdtype) { ehelin@5531: VirtualSpaceList* list = Metaspace::get_space_list(mdtype); stefank@5704: return list == NULL ? 0 : list->reserved_bytes(); stefank@5704: } stefank@5704: stefank@5704: size_t MetaspaceAux::committed_bytes(Metaspace::MetadataType mdtype) { stefank@5704: VirtualSpaceList* list = Metaspace::get_space_list(mdtype); stefank@5704: return list == NULL ? 0 : list->committed_bytes(); coleenp@4037: } coleenp@4037: ehelin@5703: size_t MetaspaceAux::min_chunk_size_words() { return Metaspace::first_chunk_word_size(); } ehelin@5703: ehelin@5703: size_t MetaspaceAux::free_chunks_total_words(Metaspace::MetadataType mdtype) { ehelin@5531: VirtualSpaceList* list = Metaspace::get_space_list(mdtype); ehelin@5531: if (list == NULL) { hseigel@5528: return 0; hseigel@5528: } ehelin@5531: ChunkManager* chunk = list->chunk_manager(); mgerdin@4264: chunk->slow_verify(); ehelin@5703: return chunk->free_chunks_total_words(); coleenp@4037: } coleenp@4037: ehelin@5703: size_t MetaspaceAux::free_chunks_total_bytes(Metaspace::MetadataType mdtype) { ehelin@5703: return free_chunks_total_words(mdtype) * BytesPerWord; coleenp@4037: } coleenp@4037: ehelin@5703: size_t MetaspaceAux::free_chunks_total_words() { ehelin@5703: return free_chunks_total_words(Metaspace::ClassType) + ehelin@5703: free_chunks_total_words(Metaspace::NonClassType); jmasa@5015: } jmasa@5015: ehelin@5703: size_t MetaspaceAux::free_chunks_total_bytes() { ehelin@5703: return free_chunks_total_words() * BytesPerWord; jmasa@5015: } jmasa@5015: coleenp@4037: void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) { coleenp@4037: gclog_or_tty->print(", [Metaspace:"); coleenp@4037: if (PrintGCDetails && Verbose) { coleenp@4037: gclog_or_tty->print(" " SIZE_FORMAT coleenp@4037: "->" SIZE_FORMAT jmasa@5015: "(" SIZE_FORMAT ")", coleenp@4037: prev_metadata_used, jmasa@5310: allocated_used_bytes(), ehelin@5703: reserved_bytes()); coleenp@4037: } else { coleenp@4037: gclog_or_tty->print(" " SIZE_FORMAT "K" coleenp@4037: "->" SIZE_FORMAT "K" jmasa@5015: "(" SIZE_FORMAT "K)", ehelin@5703: prev_metadata_used/K, ehelin@5703: allocated_used_bytes()/K, ehelin@5703: reserved_bytes()/K); coleenp@4037: } coleenp@4037: coleenp@4037: gclog_or_tty->print("]"); coleenp@4037: } coleenp@4037: coleenp@4037: // This is printed when PrintGCDetails coleenp@4037: void MetaspaceAux::print_on(outputStream* out) { coleenp@4037: Metaspace::MetadataType nct = Metaspace::NonClassType; coleenp@4037: coleenp@4037: out->print_cr(" Metaspace total " coleenp@4037: SIZE_FORMAT "K, used " SIZE_FORMAT "K," coleenp@4037: " reserved " SIZE_FORMAT "K", ehelin@5703: allocated_capacity_bytes()/K, allocated_used_bytes()/K, reserved_bytes()/K); jmasa@5162: jmasa@5162: out->print_cr(" data space " jmasa@5162: SIZE_FORMAT "K, used " SIZE_FORMAT "K," jmasa@5162: " reserved " SIZE_FORMAT "K", jmasa@5162: allocated_capacity_bytes(nct)/K, jmasa@5162: allocated_used_bytes(nct)/K, ehelin@5703: reserved_bytes(nct)/K); hseigel@5528: if (Metaspace::using_class_space()) { hseigel@5528: Metaspace::MetadataType ct = Metaspace::ClassType; hseigel@5528: out->print_cr(" class space " hseigel@5528: SIZE_FORMAT "K, used " SIZE_FORMAT "K," hseigel@5528: " reserved " SIZE_FORMAT "K", hseigel@5528: allocated_capacity_bytes(ct)/K, hseigel@5528: allocated_used_bytes(ct)/K, ehelin@5703: reserved_bytes(ct)/K); hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: // Print information for class space and data space separately. coleenp@4037: // This is almost the same as above. coleenp@4037: void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) { ehelin@5703: size_t free_chunks_capacity_bytes = free_chunks_total_bytes(mdtype); jmasa@5015: size_t capacity_bytes = capacity_bytes_slow(mdtype); jmasa@5015: size_t used_bytes = used_bytes_slow(mdtype); ehelin@5703: size_t free_bytes = free_bytes_slow(mdtype); coleenp@4037: size_t used_and_free = used_bytes + free_bytes + coleenp@4037: free_chunks_capacity_bytes; coleenp@4037: out->print_cr(" Chunk accounting: used in chunks " SIZE_FORMAT coleenp@4037: "K + unused in chunks " SIZE_FORMAT "K + " coleenp@4037: " capacity in free chunks " SIZE_FORMAT "K = " SIZE_FORMAT coleenp@4037: "K capacity in allocated chunks " SIZE_FORMAT "K", coleenp@4037: used_bytes / K, coleenp@4037: free_bytes / K, coleenp@4037: free_chunks_capacity_bytes / K, coleenp@4037: used_and_free / K, coleenp@4037: capacity_bytes / K); mgerdin@4738: // Accounting can only be correct if we got the values during a safepoint mgerdin@4738: assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong"); coleenp@4037: } coleenp@4037: hseigel@5528: // Print total fragmentation for class metaspaces hseigel@5528: void MetaspaceAux::print_class_waste(outputStream* out) { hseigel@5528: assert(Metaspace::using_class_space(), "class metaspace not used"); hseigel@5528: size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0; hseigel@5528: size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0; hseigel@5528: ClassLoaderDataGraphMetaspaceIterator iter; hseigel@5528: while (iter.repeat()) { hseigel@5528: Metaspace* msp = iter.get_next(); hseigel@5528: if (msp != NULL) { hseigel@5528: cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex); hseigel@5528: cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex); hseigel@5528: cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex); hseigel@5528: cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex); hseigel@5528: cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex); hseigel@5528: cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex); hseigel@5528: cls_humongous_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex); hseigel@5528: } hseigel@5528: } hseigel@5528: out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", " hseigel@5528: SIZE_FORMAT " small(s) " SIZE_FORMAT ", " hseigel@5528: SIZE_FORMAT " medium(s) " SIZE_FORMAT ", " hseigel@5528: "large count " SIZE_FORMAT, hseigel@5528: cls_specialized_count, cls_specialized_waste, hseigel@5528: cls_small_count, cls_small_waste, hseigel@5528: cls_medium_count, cls_medium_waste, cls_humongous_count); hseigel@5528: } hseigel@5528: hseigel@5528: // Print total fragmentation for data and class metaspaces separately coleenp@4037: void MetaspaceAux::print_waste(outputStream* out) { coleenp@5337: size_t specialized_waste = 0, small_waste = 0, medium_waste = 0; coleenp@5337: size_t specialized_count = 0, small_count = 0, medium_count = 0, humongous_count = 0; coleenp@4037: coleenp@4037: ClassLoaderDataGraphMetaspaceIterator iter; coleenp@4037: while (iter.repeat()) { coleenp@4037: Metaspace* msp = iter.get_next(); coleenp@4037: if (msp != NULL) { jmasa@4382: specialized_waste += msp->vsm()->sum_waste_in_chunks_in_use(SpecializedIndex); jmasa@4382: specialized_count += msp->vsm()->sum_count_in_chunks_in_use(SpecializedIndex); coleenp@4037: small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex); jmasa@4382: small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex); coleenp@4037: medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex); jmasa@4382: medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex); coleenp@5337: humongous_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex); coleenp@4037: } coleenp@4037: } coleenp@4037: out->print_cr("Total fragmentation waste (words) doesn't count free space"); jmasa@4382: out->print_cr(" data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", " jmasa@4382: SIZE_FORMAT " small(s) " SIZE_FORMAT ", " coleenp@5337: SIZE_FORMAT " medium(s) " SIZE_FORMAT ", " coleenp@5337: "large count " SIZE_FORMAT, jmasa@4382: specialized_count, specialized_waste, small_count, coleenp@5337: small_waste, medium_count, medium_waste, humongous_count); hseigel@5528: if (Metaspace::using_class_space()) { hseigel@5528: print_class_waste(out); hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: // Dump global metaspace things from the end of ClassLoaderDataGraph coleenp@4037: void MetaspaceAux::dump(outputStream* out) { coleenp@4037: out->print_cr("All Metaspace:"); coleenp@4037: out->print("data space: "); print_on(out, Metaspace::NonClassType); coleenp@4037: out->print("class space: "); print_on(out, Metaspace::ClassType); coleenp@4037: print_waste(out); coleenp@4037: } coleenp@4037: mgerdin@4264: void MetaspaceAux::verify_free_chunks() { mgerdin@4264: Metaspace::space_list()->chunk_manager()->verify(); hseigel@5528: if (Metaspace::using_class_space()) { hseigel@5528: Metaspace::class_space_list()->chunk_manager()->verify(); hseigel@5528: } mgerdin@4264: } mgerdin@4264: jmasa@5015: void MetaspaceAux::verify_capacity() { jmasa@5015: #ifdef ASSERT jmasa@5015: size_t running_sum_capacity_bytes = allocated_capacity_bytes(); jmasa@5162: // For purposes of the running sum of capacity, verify against capacity jmasa@5015: size_t capacity_in_use_bytes = capacity_bytes_slow(); jmasa@5015: assert(running_sum_capacity_bytes == capacity_in_use_bytes, jmasa@5015: err_msg("allocated_capacity_words() * BytesPerWord " SIZE_FORMAT jmasa@5015: " capacity_bytes_slow()" SIZE_FORMAT, jmasa@5015: running_sum_capacity_bytes, capacity_in_use_bytes)); jmasa@5162: for (Metaspace::MetadataType i = Metaspace::ClassType; jmasa@5162: i < Metaspace:: MetadataTypeCount; jmasa@5162: i = (Metaspace::MetadataType)(i + 1)) { jmasa@5162: size_t capacity_in_use_bytes = capacity_bytes_slow(i); jmasa@5162: assert(allocated_capacity_bytes(i) == capacity_in_use_bytes, jmasa@5162: err_msg("allocated_capacity_bytes(%u) " SIZE_FORMAT jmasa@5162: " capacity_bytes_slow(%u)" SIZE_FORMAT, jmasa@5162: i, allocated_capacity_bytes(i), i, capacity_in_use_bytes)); jmasa@5162: } jmasa@5015: #endif jmasa@5015: } jmasa@5015: jmasa@5015: void MetaspaceAux::verify_used() { jmasa@5015: #ifdef ASSERT jmasa@5015: size_t running_sum_used_bytes = allocated_used_bytes(); jmasa@5162: // For purposes of the running sum of used, verify against used jmasa@5015: size_t used_in_use_bytes = used_bytes_slow(); jmasa@5015: assert(allocated_used_bytes() == used_in_use_bytes, jmasa@5015: err_msg("allocated_used_bytes() " SIZE_FORMAT jmasa@5162: " used_bytes_slow()" SIZE_FORMAT, jmasa@5015: allocated_used_bytes(), used_in_use_bytes)); jmasa@5162: for (Metaspace::MetadataType i = Metaspace::ClassType; jmasa@5162: i < Metaspace:: MetadataTypeCount; jmasa@5162: i = (Metaspace::MetadataType)(i + 1)) { jmasa@5162: size_t used_in_use_bytes = used_bytes_slow(i); jmasa@5162: assert(allocated_used_bytes(i) == used_in_use_bytes, jmasa@5162: err_msg("allocated_used_bytes(%u) " SIZE_FORMAT jmasa@5162: " used_bytes_slow(%u)" SIZE_FORMAT, jmasa@5162: i, allocated_used_bytes(i), i, used_in_use_bytes)); jmasa@5162: } jmasa@5015: #endif jmasa@5015: } jmasa@5015: jmasa@5015: void MetaspaceAux::verify_metrics() { jmasa@5015: verify_capacity(); jmasa@5015: verify_used(); jmasa@5015: } jmasa@5015: jmasa@5015: coleenp@4037: // Metaspace methods coleenp@4037: coleenp@4037: size_t Metaspace::_first_chunk_word_size = 0; jmasa@4382: size_t Metaspace::_first_class_chunk_word_size = 0; jmasa@4382: jmasa@4382: Metaspace::Metaspace(Mutex* lock, MetaspaceType type) { jmasa@4382: initialize(lock, type); coleenp@4037: } coleenp@4037: coleenp@4037: Metaspace::~Metaspace() { coleenp@4037: delete _vsm; hseigel@5528: if (using_class_space()) { hseigel@5528: delete _class_vsm; hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: VirtualSpaceList* Metaspace::_space_list = NULL; coleenp@4037: VirtualSpaceList* Metaspace::_class_space_list = NULL; coleenp@4037: coleenp@4037: #define VIRTUALSPACEMULTIPLIER 2 coleenp@4037: hseigel@5528: #ifdef _LP64 hseigel@5528: void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) { hseigel@5528: // Figure out the narrow_klass_base and the narrow_klass_shift. The hseigel@5528: // narrow_klass_base is the lower of the metaspace base and the cds base hseigel@5528: // (if cds is enabled). The narrow_klass_shift depends on the distance hseigel@5528: // between the lower base and higher address. hseigel@5528: address lower_base; hseigel@5528: address higher_address; hseigel@5528: if (UseSharedSpaces) { hseigel@5528: higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()), hseigel@5528: (address)(metaspace_base + class_metaspace_size())); hseigel@5528: lower_base = MIN2(metaspace_base, cds_base); hseigel@5528: } else { hseigel@5528: higher_address = metaspace_base + class_metaspace_size(); hseigel@5528: lower_base = metaspace_base; hseigel@5528: } hseigel@5528: Universe::set_narrow_klass_base(lower_base); hseigel@5528: if ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint) { hseigel@5528: Universe::set_narrow_klass_shift(0); hseigel@5528: } else { hseigel@5528: assert(!UseSharedSpaces, "Cannot shift with UseSharedSpaces"); hseigel@5528: Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes); hseigel@5528: } hseigel@5528: } hseigel@5528: hseigel@5528: // Return TRUE if the specified metaspace_base and cds_base are close enough hseigel@5528: // to work with compressed klass pointers. hseigel@5528: bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) { hseigel@5528: assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS"); ehelin@5694: assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs"); hseigel@5528: address lower_base = MIN2((address)metaspace_base, cds_base); hseigel@5528: address higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()), hseigel@5528: (address)(metaspace_base + class_metaspace_size())); hseigel@5528: return ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint); hseigel@5528: } hseigel@5528: hseigel@5528: // Try to allocate the metaspace at the requested addr. hseigel@5528: void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) { hseigel@5528: assert(using_class_space(), "called improperly"); ehelin@5694: assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs"); hseigel@5528: assert(class_metaspace_size() < KlassEncodingMetaspaceMax, hseigel@5528: "Metaspace size is too big"); hseigel@5528: hseigel@5528: ReservedSpace metaspace_rs = ReservedSpace(class_metaspace_size(), hseigel@5528: os::vm_allocation_granularity(), hseigel@5528: false, requested_addr, 0); hseigel@5528: if (!metaspace_rs.is_reserved()) { hseigel@5528: if (UseSharedSpaces) { hseigel@5528: // Keep trying to allocate the metaspace, increasing the requested_addr hseigel@5528: // by 1GB each time, until we reach an address that will no longer allow hseigel@5528: // use of CDS with compressed klass pointers. hseigel@5528: char *addr = requested_addr; hseigel@5528: while (!metaspace_rs.is_reserved() && (addr + 1*G > addr) && hseigel@5528: can_use_cds_with_metaspace_addr(addr + 1*G, cds_base)) { hseigel@5528: addr = addr + 1*G; hseigel@5528: metaspace_rs = ReservedSpace(class_metaspace_size(), hseigel@5528: os::vm_allocation_granularity(), false, addr, 0); hseigel@5528: } hseigel@5528: } hseigel@5528: hseigel@5528: // If no successful allocation then try to allocate the space anywhere. If hseigel@5528: // that fails then OOM doom. At this point we cannot try allocating the ehelin@5694: // metaspace as if UseCompressedClassPointers is off because too much ehelin@5694: // initialization has happened that depends on UseCompressedClassPointers. ehelin@5694: // So, UseCompressedClassPointers cannot be turned off at this point. hseigel@5528: if (!metaspace_rs.is_reserved()) { hseigel@5528: metaspace_rs = ReservedSpace(class_metaspace_size(), hseigel@5528: os::vm_allocation_granularity(), false); hseigel@5528: if (!metaspace_rs.is_reserved()) { hseigel@5528: vm_exit_during_initialization(err_msg("Could not allocate metaspace: %d bytes", hseigel@5528: class_metaspace_size())); hseigel@5528: } hseigel@5528: } hseigel@5528: } hseigel@5528: hseigel@5528: // If we got here then the metaspace got allocated. hseigel@5528: MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass); hseigel@5528: hseigel@5528: // Verify that we can use shared spaces. Otherwise, turn off CDS. hseigel@5528: if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) { hseigel@5528: FileMapInfo::stop_sharing_and_unmap( hseigel@5528: "Could not allocate metaspace at a compatible address"); hseigel@5528: } hseigel@5528: hseigel@5528: set_narrow_klass_base_and_shift((address)metaspace_rs.base(), hseigel@5528: UseSharedSpaces ? (address)cds_base : 0); hseigel@5528: hseigel@5528: initialize_class_space(metaspace_rs); hseigel@5528: hseigel@5528: if (PrintCompressedOopsMode || (PrintMiscellaneous && Verbose)) { hseigel@5528: gclog_or_tty->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: " SIZE_FORMAT, hseigel@5528: Universe::narrow_klass_base(), Universe::narrow_klass_shift()); hseigel@5528: gclog_or_tty->print_cr("Metaspace Size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT, hseigel@5528: class_metaspace_size(), metaspace_rs.base(), requested_addr); hseigel@5528: } hseigel@5528: } hseigel@5528: ehelin@5694: // For UseCompressedClassPointers the class space is reserved above the top of hseigel@5528: // the Java heap. The argument passed in is at the base of the compressed space. hseigel@5528: void Metaspace::initialize_class_space(ReservedSpace rs) { hseigel@5528: // The reserved space size may be bigger because of alignment, esp with UseLargePages ehelin@5694: assert(rs.size() >= CompressedClassSpaceSize, ehelin@5694: err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), CompressedClassSpaceSize)); hseigel@5528: assert(using_class_space(), "Must be using class space"); hseigel@5528: _class_space_list = new VirtualSpaceList(rs); hseigel@5528: } hseigel@5528: hseigel@5528: #endif hseigel@5528: coleenp@4037: void Metaspace::global_initialize() { coleenp@4037: // Initialize the alignment for shared spaces. coleenp@4037: int max_alignment = os::vm_page_size(); hseigel@5528: size_t cds_total = 0; hseigel@5528: ehelin@5694: set_class_metaspace_size(align_size_up(CompressedClassSpaceSize, hseigel@5528: os::vm_allocation_granularity())); hseigel@5528: 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. hseigel@5528: cds_total = FileMapInfo::shared_spaces_size(); hseigel@5528: _space_list = new VirtualSpaceList(cds_total/wordSize); hseigel@5528: hseigel@5528: #ifdef _LP64 hseigel@5528: // Set the compressed klass pointer base so that decoding of these pointers works hseigel@5528: // properly when creating the shared archive. ehelin@5694: assert(UseCompressedOops && UseCompressedClassPointers, ehelin@5694: "UseCompressedOops and UseCompressedClassPointers must be set"); hseigel@5528: Universe::set_narrow_klass_base((address)_space_list->current_virtual_space()->bottom()); hseigel@5528: if (TraceMetavirtualspaceAllocation && Verbose) { hseigel@5528: gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT, hseigel@5528: _space_list->current_virtual_space()->bottom()); hseigel@5528: } hseigel@5528: hseigel@5528: // Set the shift to zero. hseigel@5528: assert(class_metaspace_size() < (uint64_t)(max_juint) - cds_total, hseigel@5528: "CDS region is too large"); hseigel@5528: Universe::set_narrow_klass_shift(0); hseigel@5528: #endif hseigel@5528: 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) hseigel@5528: address cds_address = NULL; 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: } hseigel@5528: cds_total = FileMapInfo::shared_spaces_size(); hseigel@5528: cds_address = (address)mapinfo->region_base(0); coleenp@4037: } coleenp@4037: hseigel@5528: #ifdef _LP64 ehelin@5694: // If UseCompressedClassPointers is set then allocate the metaspace area hseigel@5528: // above the heap and above the CDS area (if it exists). hseigel@5528: if (using_class_space()) { hseigel@5528: if (UseSharedSpaces) { hseigel@5528: allocate_metaspace_compressed_klass_ptrs((char *)(cds_address + cds_total), cds_address); hseigel@5528: } else { hseigel@5528: allocate_metaspace_compressed_klass_ptrs((char *)CompressedKlassPointersBase, 0); hseigel@5528: } hseigel@5528: } hseigel@5528: #endif hseigel@5528: jmasa@4382: // Initialize these before initializing the VirtualSpaceList coleenp@4037: _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord; jmasa@4382: _first_chunk_word_size = align_word_size_up(_first_chunk_word_size); jmasa@4382: // Make the first class chunk bigger than a medium chunk so it's not put jmasa@4382: // on the medium chunk list. The next chunk will be small and progress jmasa@4382: // from there. This size calculated by -version. jmasa@4382: _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6, ehelin@5694: (CompressedClassSpaceSize/BytesPerWord)*2); jmasa@4382: _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size); coleenp@4037: // Arbitrarily set the initial virtual space to a multiple coleenp@4037: // of the boot class loader size. jmasa@4382: size_t word_size = VIRTUALSPACEMULTIPLIER * 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: hseigel@5528: void Metaspace::initialize(Mutex* lock, MetaspaceType type) { coleenp@4037: coleenp@4037: assert(space_list() != NULL, coleenp@4037: "Metadata VirtualSpaceList has not been initialized"); coleenp@4037: hseigel@5528: _vsm = new SpaceManager(NonClassType, lock, space_list()); coleenp@4037: if (_vsm == NULL) { coleenp@4037: return; coleenp@4037: } jmasa@4382: size_t word_size; jmasa@4382: size_t class_word_size; hseigel@5528: vsm()->get_initial_chunk_sizes(type, &word_size, &class_word_size); hseigel@5528: hseigel@5528: if (using_class_space()) { hseigel@5528: assert(class_space_list() != NULL, hseigel@5528: "Class VirtualSpaceList has not been initialized"); hseigel@5528: hseigel@5528: // Allocate SpaceManager for classes. hseigel@5528: _class_vsm = new SpaceManager(ClassType, lock, class_space_list()); hseigel@5528: if (_class_vsm == NULL) { hseigel@5528: return; hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: coleenp@4037: // Allocate chunk for metadata objects coleenp@4037: Metachunk* new_chunk = jmasa@4382: space_list()->get_initialization_chunk(word_size, jmasa@4382: vsm()->medium_chunk_bunch()); coleenp@4037: assert(!DumpSharedSpaces || new_chunk != NULL, "should have enough space for both chunks"); coleenp@4037: if (new_chunk != NULL) { coleenp@4037: // Add to this manager's list of chunks in use and current_chunk(). coleenp@4037: vsm()->add_chunk(new_chunk, true); coleenp@4037: } coleenp@4037: coleenp@4037: // Allocate chunk for class metadata objects hseigel@5528: if (using_class_space()) { hseigel@5528: Metachunk* class_chunk = hseigel@5528: class_space_list()->get_initialization_chunk(class_word_size, hseigel@5528: class_vsm()->medium_chunk_bunch()); hseigel@5528: if (class_chunk != NULL) { hseigel@5528: class_vsm()->add_chunk(class_chunk, true); hseigel@5528: } coleenp@4037: } iklam@5208: iklam@5208: _alloc_record_head = NULL; iklam@5208: _alloc_record_tail = NULL; coleenp@4037: } coleenp@4037: jmasa@4382: size_t Metaspace::align_word_size_up(size_t word_size) { jmasa@4382: size_t byte_size = word_size * wordSize; jmasa@4382: return ReservedSpace::allocation_align_size_up(byte_size) / wordSize; jmasa@4382: } jmasa@4382: coleenp@4037: MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) { coleenp@4037: // DumpSharedSpaces doesn't use class metadata area (yet) ehelin@5694: // Also, don't use class_vsm() unless UseCompressedClassPointers is true. hseigel@5528: if (mdtype == ClassType && using_class_space()) { 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@5015: size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size) * BytesPerWord; jmasa@5015: MetaspaceGC::inc_capacity_until_GC(delta_bytes); 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: jmasa@5015: size_t Metaspace::used_words_slow(MetadataType mdtype) const { hseigel@5528: if (mdtype == ClassType) { hseigel@5528: return using_class_space() ? class_vsm()->sum_used_in_chunks_in_use() : 0; hseigel@5528: } else { hseigel@5528: return vsm()->sum_used_in_chunks_in_use(); // includes overhead! hseigel@5528: } coleenp@4037: } coleenp@4037: ehelin@5703: size_t Metaspace::free_words_slow(MetadataType mdtype) const { hseigel@5528: if (mdtype == ClassType) { hseigel@5528: return using_class_space() ? class_vsm()->sum_free_in_chunks_in_use() : 0; hseigel@5528: } else { hseigel@5528: return vsm()->sum_free_in_chunks_in_use(); hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: // Space capacity in the Metaspace. It includes coleenp@4037: // space in the list of chunks from which allocations coleenp@4037: // have been made. Don't include space in the global freelist and coleenp@4037: // in the space available in the dictionary which coleenp@4037: // is already counted in some chunk. jmasa@5015: size_t Metaspace::capacity_words_slow(MetadataType mdtype) const { hseigel@5528: if (mdtype == ClassType) { hseigel@5528: return using_class_space() ? class_vsm()->sum_capacity_in_chunks_in_use() : 0; hseigel@5528: } else { hseigel@5528: return vsm()->sum_capacity_in_chunks_in_use(); hseigel@5528: } coleenp@4037: } coleenp@4037: jmasa@5015: size_t Metaspace::used_bytes_slow(MetadataType mdtype) const { jmasa@5015: return used_words_slow(mdtype) * BytesPerWord; jmasa@5015: } jmasa@5015: jmasa@5015: size_t Metaspace::capacity_bytes_slow(MetadataType mdtype) const { jmasa@5015: return capacity_words_slow(mdtype) * BytesPerWord; jmasa@5015: } jmasa@5015: coleenp@4037: void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) { coleenp@4037: if (SafepointSynchronize::is_at_safepoint()) { coleenp@4037: assert(Thread::current()->is_VM_thread(), "should be the VM thread"); jmasa@4196: // Don't take Heap_lock mgerdin@5023: MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag); 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: } hseigel@5528: if (is_class && using_class_space()) { hseigel@5528: class_vsm()->deallocate(ptr, word_size); coleenp@4037: } else { jmasa@4196: vsm()->deallocate(ptr, word_size); coleenp@4037: } coleenp@4037: } else { mgerdin@5023: MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: 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: } hseigel@5528: if (is_class && using_class_space()) { jmasa@4196: class_vsm()->deallocate(ptr, word_size); coleenp@4037: } else { jmasa@4196: vsm()->deallocate(ptr, word_size); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: jmasa@4196: Metablock* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size, iklam@5208: bool read_only, MetaspaceObj::Type type, TRAPS) { coleenp@4037: if (HAS_PENDING_EXCEPTION) { coleenp@4037: assert(false, "Should not allocate with exception pending"); coleenp@4037: return NULL; // caller does a CHECK_NULL too coleenp@4037: } coleenp@4037: iklam@5208: MetadataType mdtype = (type == MetaspaceObj::ClassType) ? ClassType : NonClassType; iklam@5208: 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) { iklam@5208: assert(type > MetaspaceObj::UnknownType && type < MetaspaceObj::_number_of_types, "sanity"); iklam@5208: Metaspace* space = read_only ? loader_data->ro_metaspace() : loader_data->rw_metaspace(); iklam@5208: result = space->allocate(word_size, NonClassType); coleenp@4037: if (result == NULL) { coleenp@4037: report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite); iklam@5208: } else { iklam@5208: space->record_allocation(result, type, space->vsm()->get_raw_word_size(word_size)); 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) { jmasa@4382: if (Verbose && TraceMetadataChunkAllocation) { jmasa@4382: gclog_or_tty->print_cr("Metaspace allocation failed for size " jmasa@4382: SIZE_FORMAT, word_size); coleenp@5337: if (loader_data->metaspace_or_null() != NULL) loader_data->dump(gclog_or_tty); jmasa@4382: MetaspaceAux::dump(gclog_or_tty); jmasa@4382: } coleenp@4037: // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support ehelin@5694: const char* space_string = (mdtype == ClassType) ? "Compressed class space" : coleenp@5337: "Metadata space"; coleenp@5337: report_java_out_of_memory(space_string); coleenp@4037: coleenp@4037: if (JvmtiExport::should_post_resource_exhausted()) { coleenp@4037: JvmtiExport::post_resource_exhausted( coleenp@4037: JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR, coleenp@5337: space_string); coleenp@4037: } coleenp@5337: if (mdtype == ClassType) { coleenp@5337: THROW_OOP_0(Universe::out_of_memory_error_class_metaspace()); coleenp@5337: } else { coleenp@5337: THROW_OOP_0(Universe::out_of_memory_error_metaspace()); coleenp@5337: } coleenp@4037: } coleenp@4037: } jmasa@4196: return Metablock::initialize(result, word_size); coleenp@4037: } coleenp@4037: iklam@5208: void Metaspace::record_allocation(void* ptr, MetaspaceObj::Type type, size_t word_size) { iklam@5208: assert(DumpSharedSpaces, "sanity"); iklam@5208: iklam@5208: AllocRecord *rec = new AllocRecord((address)ptr, type, (int)word_size * HeapWordSize); iklam@5208: if (_alloc_record_head == NULL) { iklam@5208: _alloc_record_head = _alloc_record_tail = rec; iklam@5208: } else { iklam@5208: _alloc_record_tail->_next = rec; iklam@5208: _alloc_record_tail = rec; iklam@5208: } iklam@5208: } iklam@5208: iklam@5208: void Metaspace::iterate(Metaspace::AllocRecordClosure *closure) { iklam@5208: assert(DumpSharedSpaces, "unimplemented for !DumpSharedSpaces"); iklam@5208: iklam@5208: address last_addr = (address)bottom(); iklam@5208: iklam@5208: for (AllocRecord *rec = _alloc_record_head; rec; rec = rec->_next) { iklam@5208: address ptr = rec->_ptr; iklam@5208: if (last_addr < ptr) { iklam@5208: closure->doit(last_addr, MetaspaceObj::UnknownType, ptr - last_addr); iklam@5208: } iklam@5208: closure->doit(ptr, rec->_type, rec->_byte_size); iklam@5208: last_addr = ptr + rec->_byte_size; iklam@5208: } iklam@5208: iklam@5208: address top = ((address)bottom()) + used_bytes_slow(Metaspace::NonClassType); iklam@5208: if (last_addr < top) { iklam@5208: closure->doit(last_addr, MetaspaceObj::UnknownType, top - last_addr); iklam@5208: } iklam@5208: } iklam@5208: jmasa@5007: void Metaspace::purge() { jmasa@5007: MutexLockerEx cl(SpaceManager::expand_lock(), jmasa@5007: Mutex::_no_safepoint_check_flag); jmasa@5007: space_list()->purge(); hseigel@5528: if (using_class_space()) { hseigel@5528: class_space_list()->purge(); hseigel@5528: } jmasa@5007: } jmasa@5007: coleenp@4037: void Metaspace::print_on(outputStream* out) const { coleenp@4037: // Print both class virtual space counts and metaspace. coleenp@4037: if (Verbose) { hseigel@5528: vsm()->print_on(out); hseigel@5528: if (using_class_space()) { coleenp@4037: class_vsm()->print_on(out); hseigel@5528: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4295: bool Metaspace::contains(const void * ptr) { coleenp@4037: if (MetaspaceShared::is_in_shared_space(ptr)) { coleenp@4037: return true; coleenp@4037: } coleenp@4295: // This is checked while unlocked. As long as the virtualspaces are added coleenp@4295: // at the end, the pointer will be in one of them. The virtual spaces coleenp@4295: // aren't deleted presently. When they are, some sort of locking might coleenp@4295: // be needed. Note, locking this can cause inversion problems with the coleenp@4295: // caller in MetaspaceObj::is_metadata() function. jmasa@5007: return space_list()->contains(ptr) || hseigel@5528: (using_class_space() && class_space_list()->contains(ptr)); coleenp@4037: } coleenp@4037: coleenp@4037: void Metaspace::verify() { coleenp@4037: vsm()->verify(); hseigel@5528: if (using_class_space()) { hseigel@5528: class_vsm()->verify(); hseigel@5528: } coleenp@4037: } coleenp@4037: coleenp@4037: void Metaspace::dump(outputStream* const out) const { coleenp@4037: out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm()); coleenp@4037: vsm()->dump(out); hseigel@5528: if (using_class_space()) { hseigel@5528: out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm()); hseigel@5528: class_vsm()->dump(out); hseigel@5528: } coleenp@4037: } stefank@5704: stefank@5704: /////////////// Unit tests /////////////// stefank@5704: stefank@5704: #ifndef PRODUCT stefank@5704: stefank@5704: class MetaspaceAuxTest : AllStatic { stefank@5704: public: stefank@5704: static void test_reserved() { stefank@5704: size_t reserved = MetaspaceAux::reserved_bytes(); stefank@5704: stefank@5704: assert(reserved > 0, "assert"); stefank@5704: stefank@5704: size_t committed = MetaspaceAux::committed_bytes(); stefank@5704: assert(committed <= reserved, "assert"); stefank@5704: stefank@5704: size_t reserved_metadata = MetaspaceAux::reserved_bytes(Metaspace::NonClassType); stefank@5704: assert(reserved_metadata > 0, "assert"); stefank@5704: assert(reserved_metadata <= reserved, "assert"); stefank@5704: stefank@5704: if (UseCompressedClassPointers) { stefank@5704: size_t reserved_class = MetaspaceAux::reserved_bytes(Metaspace::ClassType); stefank@5704: assert(reserved_class > 0, "assert"); stefank@5704: assert(reserved_class < reserved, "assert"); stefank@5704: } stefank@5704: } stefank@5704: stefank@5704: static void test_committed() { stefank@5704: size_t committed = MetaspaceAux::committed_bytes(); stefank@5704: stefank@5704: assert(committed > 0, "assert"); stefank@5704: stefank@5704: size_t reserved = MetaspaceAux::reserved_bytes(); stefank@5704: assert(committed <= reserved, "assert"); stefank@5704: stefank@5704: size_t committed_metadata = MetaspaceAux::committed_bytes(Metaspace::NonClassType); stefank@5704: assert(committed_metadata > 0, "assert"); stefank@5704: assert(committed_metadata <= committed, "assert"); stefank@5704: stefank@5704: if (UseCompressedClassPointers) { stefank@5704: size_t committed_class = MetaspaceAux::committed_bytes(Metaspace::ClassType); stefank@5704: assert(committed_class > 0, "assert"); stefank@5704: assert(committed_class < committed, "assert"); stefank@5704: } stefank@5704: } stefank@5704: stefank@5704: static void test() { stefank@5704: test_reserved(); stefank@5704: test_committed(); stefank@5704: } stefank@5704: }; stefank@5704: stefank@5704: void MetaspaceAux_test() { stefank@5704: MetaspaceAuxTest::test(); stefank@5704: } stefank@5704: stefank@5704: #endif