coleenp@4037: /* coleenp@4037: * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. coleenp@4037: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. coleenp@4037: * coleenp@4037: * This code is free software; you can redistribute it and/or modify it coleenp@4037: * under the terms of the GNU General Public License version 2 only, as coleenp@4037: * published by the Free Software Foundation. coleenp@4037: * coleenp@4037: * This code is distributed in the hope that it will be useful, but WITHOUT coleenp@4037: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or coleenp@4037: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License coleenp@4037: * version 2 for more details (a copy is included in the LICENSE file that coleenp@4037: * accompanied this code). coleenp@4037: * coleenp@4037: * You should have received a copy of the GNU General Public License version coleenp@4037: * 2 along with this work; if not, write to the Free Software Foundation, coleenp@4037: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. coleenp@4037: * coleenp@4037: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA coleenp@4037: * or visit www.oracle.com if you need additional information or have any coleenp@4037: * questions. coleenp@4037: * coleenp@4037: */ coleenp@4037: #ifndef SHARE_VM_MEMORY_METASPACE_HPP coleenp@4037: #define SHARE_VM_MEMORY_METASPACE_HPP coleenp@4037: coleenp@4037: #include "memory/allocation.hpp" coleenp@4037: #include "memory/memRegion.hpp" coleenp@4037: #include "runtime/virtualspace.hpp" coleenp@4037: #include "utilities/exceptions.hpp" coleenp@4037: coleenp@4037: // Metaspace coleenp@4037: // coleenp@4037: // Metaspaces are Arenas for the VM's metadata. coleenp@4037: // They are allocated one per class loader object, and one for the null coleenp@4037: // bootstrap class loader coleenp@4037: // Eventually for bootstrap loader we'll have a read-only section and read-write coleenp@4037: // to write for DumpSharedSpaces and read for UseSharedSpaces coleenp@4037: // coleenp@4037: // block X ---+ +-------------------+ coleenp@4037: // | | Virtualspace | coleenp@4037: // | | | coleenp@4037: // | | | coleenp@4037: // | |-------------------| coleenp@4037: // | || Chunk | coleenp@4037: // | || | coleenp@4037: // | ||---------- | coleenp@4037: // +------>||| block 0 | | coleenp@4037: // ||---------- | coleenp@4037: // ||| block 1 | | coleenp@4037: // ||---------- | coleenp@4037: // || | coleenp@4037: // |-------------------| coleenp@4037: // | | coleenp@4037: // | | coleenp@4037: // +-------------------+ coleenp@4037: // coleenp@4037: coleenp@4037: class ClassLoaderData; jmasa@4196: class Metablock; coleenp@4037: class MetaWord; coleenp@4037: class Mutex; coleenp@4037: class outputStream; coleenp@4037: class SpaceManager; coleenp@4037: coleenp@4037: // Metaspaces each have a SpaceManager and allocations coleenp@4037: // are done by the SpaceManager. Allocations are done coleenp@4037: // out of the current Metachunk. When the current Metachunk coleenp@4037: // is exhausted, the SpaceManager gets a new one from coleenp@4037: // the current VirtualSpace. When the VirtualSpace is exhausted coleenp@4037: // the SpaceManager gets a new one. The SpaceManager coleenp@4037: // also manages freelists of available Chunks. coleenp@4037: // coleenp@4037: // Currently the space manager maintains the list of coleenp@4037: // virtual spaces and the list of chunks in use. Its coleenp@4037: // allocate() method returns a block for use as a coleenp@4037: // quantum of metadata. coleenp@4037: coleenp@4037: class VirtualSpaceList; coleenp@4037: coleenp@4037: class Metaspace : public CHeapObj { coleenp@4037: friend class VMStructs; coleenp@4037: friend class SpaceManager; coleenp@4037: friend class VM_CollectForMetadataAllocation; coleenp@4037: friend class MetaspaceGC; coleenp@4037: friend class MetaspaceAux; coleenp@4037: coleenp@4037: public: coleenp@4037: enum MetadataType {ClassType, NonClassType}; jmasa@4382: enum MetaspaceType { jmasa@4382: StandardMetaspaceType, jmasa@4382: BootMetaspaceType, jmasa@4382: ROMetaspaceType, jmasa@4382: ReadWriteMetaspaceType, jmasa@4382: AnonymousMetaspaceType, jmasa@4382: ReflectionMetaspaceType jmasa@4382: }; coleenp@4037: coleenp@4037: private: jmasa@4382: void initialize(Mutex* lock, MetaspaceType type); jmasa@4382: jmasa@4382: // Align up the word size to the allocation word size jmasa@4382: static size_t align_word_size_up(size_t); coleenp@4037: coleenp@4037: static size_t _first_chunk_word_size; jmasa@4382: static size_t _first_class_chunk_word_size; coleenp@4037: coleenp@4037: SpaceManager* _vsm; coleenp@4037: SpaceManager* vsm() const { return _vsm; } coleenp@4037: coleenp@4037: SpaceManager* _class_vsm; coleenp@4037: SpaceManager* class_vsm() const { return _class_vsm; } coleenp@4037: jmasa@5015: // Allocate space for metadata of type mdtype. This is space jmasa@5015: // within a Metachunk and is used by jmasa@5015: // allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS) jmasa@5015: // which returns a Metablock. coleenp@4037: MetaWord* allocate(size_t word_size, MetadataType mdtype); coleenp@4037: coleenp@4037: // Virtual Space lists for both classes and other metadata coleenp@4037: static VirtualSpaceList* _space_list; coleenp@4037: static VirtualSpaceList* _class_space_list; coleenp@4037: coleenp@4037: static VirtualSpaceList* space_list() { return _space_list; } coleenp@4037: static VirtualSpaceList* class_space_list() { return _class_space_list; } coleenp@4037: coleenp@4037: public: coleenp@4037: jmasa@4382: Metaspace(Mutex* lock, MetaspaceType type); coleenp@4037: ~Metaspace(); coleenp@4037: coleenp@4037: // Initialize globals for Metaspace coleenp@4037: static void global_initialize(); coleenp@4037: static void initialize_class_space(ReservedSpace rs); coleenp@4037: coleenp@4037: static size_t first_chunk_word_size() { return _first_chunk_word_size; } jmasa@4382: static size_t first_class_chunk_word_size() { return _first_class_chunk_word_size; } coleenp@4037: coleenp@4037: char* bottom() const; jmasa@5015: size_t used_words_slow(MetadataType mdtype) const; coleenp@4037: size_t free_words(MetadataType mdtype) const; jmasa@5015: size_t capacity_words_slow(MetadataType mdtype) const; coleenp@4037: size_t waste_words(MetadataType mdtype) const; coleenp@4037: jmasa@5015: size_t used_bytes_slow(MetadataType mdtype) const; jmasa@5015: size_t capacity_bytes_slow(MetadataType mdtype) const; jmasa@5015: jmasa@4196: static Metablock* allocate(ClassLoaderData* loader_data, size_t size, coleenp@4037: bool read_only, MetadataType mdtype, TRAPS); jmasa@4064: void deallocate(MetaWord* ptr, size_t byte_size, bool is_class); coleenp@4037: jmasa@4064: MetaWord* expand_and_allocate(size_t size, jmasa@4064: MetadataType mdtype); coleenp@4037: roland@4159: static bool is_initialized() { return _class_space_list != NULL; } roland@4159: coleenp@4295: static bool contains(const void *ptr); coleenp@4037: void dump(outputStream* const out) const; coleenp@4037: jmasa@5007: // Free empty virtualspaces jmasa@5007: static void purge(); jmasa@5007: coleenp@4037: void print_on(outputStream* st) const; coleenp@4037: // Debugging support coleenp@4037: void verify(); coleenp@4037: }; coleenp@4037: coleenp@4037: class MetaspaceAux : AllStatic { coleenp@4037: coleenp@4037: // Statistics for class space and data space in metaspace. jmasa@5015: jmasa@5015: // These methods iterate over the classloader data graph jmasa@5015: // for the given Metaspace type. These are slow. jmasa@5015: static size_t used_bytes_slow(Metaspace::MetadataType mdtype); coleenp@4037: static size_t free_in_bytes(Metaspace::MetadataType mdtype); jmasa@5015: static size_t capacity_bytes_slow(Metaspace::MetadataType mdtype); jmasa@5015: jmasa@5015: // Iterates over the virtual space list. coleenp@4037: static size_t reserved_in_bytes(Metaspace::MetadataType mdtype); coleenp@4037: ehelin@4827: static size_t free_chunks_total(Metaspace::MetadataType mdtype); ehelin@4827: static size_t free_chunks_total_in_bytes(Metaspace::MetadataType mdtype); ehelin@4827: ehelin@4827: public: jmasa@5015: // Running sum of space in all Metachunks that has been jmasa@5015: // allocated to a Metaspace. This is used instead of jmasa@5015: // iterating over all the classloaders jmasa@5015: static size_t _allocated_capacity_words; jmasa@5015: // Running sum of space in all Metachunks that have jmasa@5015: // are being used for metadata. jmasa@5015: static size_t _allocated_used_words; jmasa@5015: jmasa@5015: public: jmasa@5015: // Decrement and increment _allocated_capacity_words jmasa@5015: static void dec_capacity(size_t words); jmasa@5015: static void inc_capacity(size_t words); jmasa@5015: jmasa@5015: // Decrement and increment _allocated_used_words jmasa@5015: static void dec_used(size_t words); jmasa@5015: static void inc_used(size_t words); jmasa@5015: jmasa@5015: // Total of space allocated to metadata in all Metaspaces. jmasa@5015: // This sums the space used in each Metachunk by jmasa@5015: // iterating over the classloader data graph jmasa@5015: static size_t used_bytes_slow() { jmasa@5015: return used_bytes_slow(Metaspace::ClassType) + jmasa@5015: used_bytes_slow(Metaspace::NonClassType); stefank@4371: } coleenp@4037: jmasa@5015: // Used by MetaspaceCounters jmasa@5015: static size_t free_chunks_total(); jmasa@5015: static size_t free_chunks_total_in_bytes(); jmasa@5015: jmasa@5015: static size_t allocated_capacity_words() { jmasa@5015: return _allocated_capacity_words; jmasa@5015: } jmasa@5015: static size_t allocated_capacity_bytes() { jmasa@5015: return _allocated_capacity_words * BytesPerWord; jmasa@5015: } jmasa@5015: jmasa@5015: static size_t allocated_used_words() { jmasa@5015: return _allocated_used_words; jmasa@5015: } jmasa@5015: static size_t allocated_used_bytes() { jmasa@5015: return _allocated_used_words * BytesPerWord; jmasa@5015: } jmasa@5015: jmasa@5015: static size_t free_bytes(); jmasa@5015: jmasa@5015: // Total capacity in all Metaspaces jmasa@5015: static size_t capacity_bytes_slow() { jmasa@5015: #ifdef PRODUCT jmasa@5015: // Use allocated_capacity_bytes() in PRODUCT instead of this function. jmasa@5015: guarantee(false, "Should not call capacity_bytes_slow() in the PRODUCT"); jmasa@5015: #endif jmasa@5015: size_t class_capacity = capacity_bytes_slow(Metaspace::ClassType); jmasa@5015: size_t non_class_capacity = capacity_bytes_slow(Metaspace::NonClassType); jmasa@5015: assert(allocated_capacity_bytes() == class_capacity + non_class_capacity, jmasa@5015: err_msg("bad accounting: allocated_capacity_bytes() " SIZE_FORMAT jmasa@5015: " class_capacity + non_class_capacity " SIZE_FORMAT jmasa@5015: " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT, jmasa@5015: allocated_capacity_bytes(), class_capacity + non_class_capacity, jmasa@5015: class_capacity, non_class_capacity)); jmasa@5015: jmasa@5015: return class_capacity + non_class_capacity; stefank@4371: } coleenp@4037: coleenp@4037: // Total space reserved in all Metaspaces stefank@4371: static size_t reserved_in_bytes() { stefank@4371: return reserved_in_bytes(Metaspace::ClassType) + stefank@4371: reserved_in_bytes(Metaspace::NonClassType); stefank@4371: } coleenp@4037: coleenp@4037: static size_t min_chunk_size(); coleenp@4037: coleenp@4037: // Print change in used metadata. coleenp@4037: static void print_metaspace_change(size_t prev_metadata_used); coleenp@4037: static void print_on(outputStream * out); coleenp@4037: static void print_on(outputStream * out, Metaspace::MetadataType mdtype); coleenp@4037: coleenp@4037: static void print_waste(outputStream* out); coleenp@4037: static void dump(outputStream* out); mgerdin@4264: static void verify_free_chunks(); jmasa@5015: // Checks that the values returned by allocated_capacity_bytes() and jmasa@5015: // capacity_bytes_slow() are the same. jmasa@5015: static void verify_capacity(); jmasa@5015: static void verify_used(); jmasa@5015: static void verify_metrics(); coleenp@4037: }; coleenp@4037: coleenp@4037: // Metaspace are deallocated when their class loader are GC'ed. coleenp@4037: // This class implements a policy for inducing GC's to recover coleenp@4037: // Metaspaces. coleenp@4037: coleenp@4037: class MetaspaceGC : AllStatic { coleenp@4037: coleenp@4037: // The current high-water-mark for inducing a GC. When coleenp@4037: // the capacity of all space in the virtual lists reaches this value, coleenp@4037: // a GC is induced and the value is increased. This should be changed coleenp@4037: // to the space actually used for allocations to avoid affects of coleenp@4037: // fragmentation losses to partially used chunks. Size is in words. coleenp@4037: static size_t _capacity_until_GC; coleenp@4037: coleenp@4037: // After a GC is done any allocation that fails should try to expand coleenp@4037: // the capacity of the Metaspaces. This flag is set during attempts coleenp@4037: // to allocate in the VMGCOperation that does the GC. coleenp@4037: static bool _expand_after_GC; coleenp@4037: coleenp@4037: // For a CMS collection, signal that a concurrent collection should coleenp@4037: // be started. coleenp@4037: static bool _should_concurrent_collect; coleenp@4037: coleenp@4037: static uint _shrink_factor; coleenp@4037: coleenp@4037: static void set_capacity_until_GC(size_t v) { _capacity_until_GC = v; } coleenp@4037: coleenp@4037: static size_t shrink_factor() { return _shrink_factor; } coleenp@4037: void set_shrink_factor(uint v) { _shrink_factor = v; } coleenp@4037: coleenp@4037: public: coleenp@4037: coleenp@4037: static size_t capacity_until_GC() { return _capacity_until_GC; } coleenp@4037: static void inc_capacity_until_GC(size_t v) { _capacity_until_GC += v; } coleenp@4037: static void dec_capacity_until_GC(size_t v) { coleenp@4037: _capacity_until_GC = _capacity_until_GC > v ? _capacity_until_GC - v : 0; coleenp@4037: } coleenp@4037: static bool expand_after_GC() { return _expand_after_GC; } coleenp@4037: static void set_expand_after_GC(bool v) { _expand_after_GC = v; } coleenp@4037: coleenp@4037: static bool should_concurrent_collect() { return _should_concurrent_collect; } coleenp@4037: static void set_should_concurrent_collect(bool v) { coleenp@4037: _should_concurrent_collect = v; coleenp@4037: } coleenp@4037: coleenp@4037: // The amount to increase the high-water-mark (_capacity_until_GC) coleenp@4037: static size_t delta_capacity_until_GC(size_t word_size); coleenp@4037: coleenp@4037: // It is expected that this will be called when the current capacity coleenp@4037: // has been used and a GC should be considered. coleenp@4037: static bool should_expand(VirtualSpaceList* vsl, size_t word_size); coleenp@4037: coleenp@4037: // Calculate the new high-water mark at which to induce coleenp@4037: // a GC. coleenp@4037: static void compute_new_size(); coleenp@4037: }; coleenp@4037: coleenp@4037: #endif // SHARE_VM_MEMORY_METASPACE_HPP