Tue, 18 Sep 2012 14:15:06 -0700
7197557: NPG: nsk/sysdict/vm/stress/chain/chain004 hangs intermittently
Reviewed-by: johnc, ysr
coleenp@4037 | 1 | /* |
coleenp@4037 | 2 | * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
coleenp@4037 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
coleenp@4037 | 4 | * |
coleenp@4037 | 5 | * This code is free software; you can redistribute it and/or modify it |
coleenp@4037 | 6 | * under the terms of the GNU General Public License version 2 only, as |
coleenp@4037 | 7 | * published by the Free Software Foundation. |
coleenp@4037 | 8 | * |
coleenp@4037 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
coleenp@4037 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
coleenp@4037 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
coleenp@4037 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
coleenp@4037 | 13 | * accompanied this code). |
coleenp@4037 | 14 | * |
coleenp@4037 | 15 | * You should have received a copy of the GNU General Public License version |
coleenp@4037 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
coleenp@4037 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
coleenp@4037 | 18 | * |
coleenp@4037 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
coleenp@4037 | 20 | * or visit www.oracle.com if you need additional information or have any |
coleenp@4037 | 21 | * questions. |
coleenp@4037 | 22 | * |
coleenp@4037 | 23 | */ |
coleenp@4037 | 24 | #ifndef SHARE_VM_MEMORY_METASPACE_HPP |
coleenp@4037 | 25 | #define SHARE_VM_MEMORY_METASPACE_HPP |
coleenp@4037 | 26 | |
coleenp@4037 | 27 | #include "memory/allocation.hpp" |
coleenp@4037 | 28 | #include "memory/memRegion.hpp" |
coleenp@4037 | 29 | #include "runtime/virtualspace.hpp" |
coleenp@4037 | 30 | #include "utilities/exceptions.hpp" |
coleenp@4037 | 31 | |
coleenp@4037 | 32 | // Metaspace |
coleenp@4037 | 33 | // |
coleenp@4037 | 34 | // Metaspaces are Arenas for the VM's metadata. |
coleenp@4037 | 35 | // They are allocated one per class loader object, and one for the null |
coleenp@4037 | 36 | // bootstrap class loader |
coleenp@4037 | 37 | // Eventually for bootstrap loader we'll have a read-only section and read-write |
coleenp@4037 | 38 | // to write for DumpSharedSpaces and read for UseSharedSpaces |
coleenp@4037 | 39 | // |
coleenp@4037 | 40 | // block X ---+ +-------------------+ |
coleenp@4037 | 41 | // | | Virtualspace | |
coleenp@4037 | 42 | // | | | |
coleenp@4037 | 43 | // | | | |
coleenp@4037 | 44 | // | |-------------------| |
coleenp@4037 | 45 | // | || Chunk | |
coleenp@4037 | 46 | // | || | |
coleenp@4037 | 47 | // | ||---------- | |
coleenp@4037 | 48 | // +------>||| block 0 | | |
coleenp@4037 | 49 | // ||---------- | |
coleenp@4037 | 50 | // ||| block 1 | | |
coleenp@4037 | 51 | // ||---------- | |
coleenp@4037 | 52 | // || | |
coleenp@4037 | 53 | // |-------------------| |
coleenp@4037 | 54 | // | | |
coleenp@4037 | 55 | // | | |
coleenp@4037 | 56 | // +-------------------+ |
coleenp@4037 | 57 | // |
coleenp@4037 | 58 | |
coleenp@4037 | 59 | class ClassLoaderData; |
coleenp@4037 | 60 | class MetaWord; |
coleenp@4037 | 61 | class Mutex; |
coleenp@4037 | 62 | class outputStream; |
coleenp@4037 | 63 | class FreeChunk; |
coleenp@4037 | 64 | template <class Chunk_t> class FreeList; |
coleenp@4037 | 65 | template <class Chunk_t> class BinaryTreeDictionary; |
coleenp@4037 | 66 | class SpaceManager; |
coleenp@4037 | 67 | |
coleenp@4037 | 68 | // Metaspaces each have a SpaceManager and allocations |
coleenp@4037 | 69 | // are done by the SpaceManager. Allocations are done |
coleenp@4037 | 70 | // out of the current Metachunk. When the current Metachunk |
coleenp@4037 | 71 | // is exhausted, the SpaceManager gets a new one from |
coleenp@4037 | 72 | // the current VirtualSpace. When the VirtualSpace is exhausted |
coleenp@4037 | 73 | // the SpaceManager gets a new one. The SpaceManager |
coleenp@4037 | 74 | // also manages freelists of available Chunks. |
coleenp@4037 | 75 | // |
coleenp@4037 | 76 | // Currently the space manager maintains the list of |
coleenp@4037 | 77 | // virtual spaces and the list of chunks in use. Its |
coleenp@4037 | 78 | // allocate() method returns a block for use as a |
coleenp@4037 | 79 | // quantum of metadata. |
coleenp@4037 | 80 | |
coleenp@4037 | 81 | class VirtualSpaceList; |
coleenp@4037 | 82 | |
coleenp@4037 | 83 | class Metaspace : public CHeapObj<mtClass> { |
coleenp@4037 | 84 | friend class VMStructs; |
coleenp@4037 | 85 | friend class SpaceManager; |
coleenp@4037 | 86 | friend class VM_CollectForMetadataAllocation; |
coleenp@4037 | 87 | friend class MetaspaceGC; |
coleenp@4037 | 88 | friend class MetaspaceAux; |
coleenp@4037 | 89 | |
coleenp@4037 | 90 | public: |
coleenp@4037 | 91 | enum MetadataType {ClassType, NonClassType}; |
coleenp@4037 | 92 | |
coleenp@4037 | 93 | private: |
coleenp@4037 | 94 | void initialize(Mutex* lock, size_t initial_size = 0); |
coleenp@4037 | 95 | |
coleenp@4037 | 96 | static size_t _first_chunk_word_size; |
coleenp@4037 | 97 | |
coleenp@4037 | 98 | SpaceManager* _vsm; |
coleenp@4037 | 99 | SpaceManager* vsm() const { return _vsm; } |
coleenp@4037 | 100 | |
coleenp@4037 | 101 | SpaceManager* _class_vsm; |
coleenp@4037 | 102 | SpaceManager* class_vsm() const { return _class_vsm; } |
coleenp@4037 | 103 | |
coleenp@4037 | 104 | MetaWord* allocate(size_t word_size, MetadataType mdtype); |
coleenp@4037 | 105 | |
coleenp@4037 | 106 | // Virtual Space lists for both classes and other metadata |
coleenp@4037 | 107 | static VirtualSpaceList* _space_list; |
coleenp@4037 | 108 | static VirtualSpaceList* _class_space_list; |
coleenp@4037 | 109 | |
coleenp@4037 | 110 | static VirtualSpaceList* space_list() { return _space_list; } |
coleenp@4037 | 111 | static VirtualSpaceList* class_space_list() { return _class_space_list; } |
coleenp@4037 | 112 | |
coleenp@4037 | 113 | public: |
coleenp@4037 | 114 | |
coleenp@4037 | 115 | Metaspace(Mutex* lock, size_t initial_size); |
coleenp@4037 | 116 | Metaspace(Mutex* lock); |
coleenp@4037 | 117 | ~Metaspace(); |
coleenp@4037 | 118 | |
coleenp@4037 | 119 | // Initialize globals for Metaspace |
coleenp@4037 | 120 | static void global_initialize(); |
coleenp@4037 | 121 | static void initialize_class_space(ReservedSpace rs); |
coleenp@4037 | 122 | |
coleenp@4037 | 123 | static size_t first_chunk_word_size() { return _first_chunk_word_size; } |
coleenp@4037 | 124 | |
coleenp@4037 | 125 | char* bottom() const; |
coleenp@4037 | 126 | size_t used_words(MetadataType mdtype) const; |
coleenp@4037 | 127 | size_t free_words(MetadataType mdtype) const; |
coleenp@4037 | 128 | size_t capacity_words(MetadataType mdtype) const; |
coleenp@4037 | 129 | size_t waste_words(MetadataType mdtype) const; |
coleenp@4037 | 130 | |
coleenp@4037 | 131 | static MetaWord* allocate(ClassLoaderData* loader_data, size_t size, |
coleenp@4037 | 132 | bool read_only, MetadataType mdtype, TRAPS); |
jmasa@4064 | 133 | void deallocate(MetaWord* ptr, size_t byte_size, bool is_class); |
coleenp@4037 | 134 | |
jmasa@4064 | 135 | MetaWord* expand_and_allocate(size_t size, |
jmasa@4064 | 136 | MetadataType mdtype); |
coleenp@4037 | 137 | |
coleenp@4037 | 138 | #ifndef PRODUCT |
coleenp@4037 | 139 | bool contains(const void *ptr) const; |
coleenp@4037 | 140 | bool contains_class(const void *ptr) const; |
coleenp@4037 | 141 | #endif |
coleenp@4037 | 142 | |
coleenp@4037 | 143 | void dump(outputStream* const out) const; |
coleenp@4037 | 144 | |
coleenp@4037 | 145 | void print_on(outputStream* st) const; |
coleenp@4037 | 146 | // Debugging support |
coleenp@4037 | 147 | void verify(); |
coleenp@4037 | 148 | }; |
coleenp@4037 | 149 | |
coleenp@4037 | 150 | class MetaspaceAux : AllStatic { |
coleenp@4037 | 151 | |
coleenp@4037 | 152 | // Statistics for class space and data space in metaspace. |
coleenp@4037 | 153 | static size_t used_in_bytes(Metaspace::MetadataType mdtype); |
coleenp@4037 | 154 | static size_t free_in_bytes(Metaspace::MetadataType mdtype); |
coleenp@4037 | 155 | static size_t capacity_in_bytes(Metaspace::MetadataType mdtype); |
coleenp@4037 | 156 | static size_t reserved_in_bytes(Metaspace::MetadataType mdtype); |
coleenp@4037 | 157 | |
coleenp@4037 | 158 | static size_t free_chunks_total(Metaspace::MetadataType mdtype); |
coleenp@4037 | 159 | static size_t free_chunks_total_in_bytes(Metaspace::MetadataType mdtype); |
coleenp@4037 | 160 | |
coleenp@4037 | 161 | public: |
coleenp@4037 | 162 | // Total of space allocated to metadata in all Metaspaces |
coleenp@4037 | 163 | static size_t used_in_bytes() { |
coleenp@4037 | 164 | return used_in_bytes(Metaspace::ClassType) + |
coleenp@4037 | 165 | used_in_bytes(Metaspace::NonClassType); |
coleenp@4037 | 166 | } |
coleenp@4037 | 167 | |
coleenp@4037 | 168 | // Total of available space in all Metaspaces |
coleenp@4037 | 169 | // Total of capacity allocated to all Metaspaces. This includes |
coleenp@4037 | 170 | // space in Metachunks not yet allocated and in the Metachunk |
coleenp@4037 | 171 | // freelist. |
coleenp@4037 | 172 | static size_t capacity_in_bytes() { |
coleenp@4037 | 173 | return capacity_in_bytes(Metaspace::ClassType) + |
coleenp@4037 | 174 | capacity_in_bytes(Metaspace::NonClassType); |
coleenp@4037 | 175 | } |
coleenp@4037 | 176 | |
coleenp@4037 | 177 | // Total space reserved in all Metaspaces |
coleenp@4037 | 178 | static size_t reserved_in_bytes() { |
coleenp@4037 | 179 | return reserved_in_bytes(Metaspace::ClassType) + |
coleenp@4037 | 180 | reserved_in_bytes(Metaspace::NonClassType); |
coleenp@4037 | 181 | } |
coleenp@4037 | 182 | |
coleenp@4037 | 183 | static size_t min_chunk_size(); |
coleenp@4037 | 184 | |
coleenp@4037 | 185 | // Print change in used metadata. |
coleenp@4037 | 186 | static void print_metaspace_change(size_t prev_metadata_used); |
coleenp@4037 | 187 | static void print_on(outputStream * out); |
coleenp@4037 | 188 | static void print_on(outputStream * out, Metaspace::MetadataType mdtype); |
coleenp@4037 | 189 | |
coleenp@4037 | 190 | static void print_waste(outputStream* out); |
coleenp@4037 | 191 | static void dump(outputStream* out); |
coleenp@4037 | 192 | }; |
coleenp@4037 | 193 | |
coleenp@4037 | 194 | // Metaspace are deallocated when their class loader are GC'ed. |
coleenp@4037 | 195 | // This class implements a policy for inducing GC's to recover |
coleenp@4037 | 196 | // Metaspaces. |
coleenp@4037 | 197 | |
coleenp@4037 | 198 | class MetaspaceGC : AllStatic { |
coleenp@4037 | 199 | |
coleenp@4037 | 200 | // The current high-water-mark for inducing a GC. When |
coleenp@4037 | 201 | // the capacity of all space in the virtual lists reaches this value, |
coleenp@4037 | 202 | // a GC is induced and the value is increased. This should be changed |
coleenp@4037 | 203 | // to the space actually used for allocations to avoid affects of |
coleenp@4037 | 204 | // fragmentation losses to partially used chunks. Size is in words. |
coleenp@4037 | 205 | static size_t _capacity_until_GC; |
coleenp@4037 | 206 | |
coleenp@4037 | 207 | // After a GC is done any allocation that fails should try to expand |
coleenp@4037 | 208 | // the capacity of the Metaspaces. This flag is set during attempts |
coleenp@4037 | 209 | // to allocate in the VMGCOperation that does the GC. |
coleenp@4037 | 210 | static bool _expand_after_GC; |
coleenp@4037 | 211 | |
coleenp@4037 | 212 | // For a CMS collection, signal that a concurrent collection should |
coleenp@4037 | 213 | // be started. |
coleenp@4037 | 214 | static bool _should_concurrent_collect; |
coleenp@4037 | 215 | |
coleenp@4037 | 216 | static uint _shrink_factor; |
coleenp@4037 | 217 | |
coleenp@4037 | 218 | static void set_capacity_until_GC(size_t v) { _capacity_until_GC = v; } |
coleenp@4037 | 219 | |
coleenp@4037 | 220 | static size_t shrink_factor() { return _shrink_factor; } |
coleenp@4037 | 221 | void set_shrink_factor(uint v) { _shrink_factor = v; } |
coleenp@4037 | 222 | |
coleenp@4037 | 223 | public: |
coleenp@4037 | 224 | |
coleenp@4037 | 225 | static size_t capacity_until_GC() { return _capacity_until_GC; } |
coleenp@4037 | 226 | static size_t capacity_until_GC_in_bytes() { return _capacity_until_GC * BytesPerWord; } |
coleenp@4037 | 227 | static void inc_capacity_until_GC(size_t v) { _capacity_until_GC += v; } |
coleenp@4037 | 228 | static void dec_capacity_until_GC(size_t v) { |
coleenp@4037 | 229 | _capacity_until_GC = _capacity_until_GC > v ? _capacity_until_GC - v : 0; |
coleenp@4037 | 230 | } |
coleenp@4037 | 231 | static bool expand_after_GC() { return _expand_after_GC; } |
coleenp@4037 | 232 | static void set_expand_after_GC(bool v) { _expand_after_GC = v; } |
coleenp@4037 | 233 | |
coleenp@4037 | 234 | static bool should_concurrent_collect() { return _should_concurrent_collect; } |
coleenp@4037 | 235 | static void set_should_concurrent_collect(bool v) { |
coleenp@4037 | 236 | _should_concurrent_collect = v; |
coleenp@4037 | 237 | } |
coleenp@4037 | 238 | |
coleenp@4037 | 239 | // The amount to increase the high-water-mark (_capacity_until_GC) |
coleenp@4037 | 240 | static size_t delta_capacity_until_GC(size_t word_size); |
coleenp@4037 | 241 | |
coleenp@4037 | 242 | // It is expected that this will be called when the current capacity |
coleenp@4037 | 243 | // has been used and a GC should be considered. |
coleenp@4037 | 244 | static bool should_expand(VirtualSpaceList* vsl, size_t word_size); |
coleenp@4037 | 245 | |
coleenp@4037 | 246 | // Calculate the new high-water mark at which to induce |
coleenp@4037 | 247 | // a GC. |
coleenp@4037 | 248 | static void compute_new_size(); |
coleenp@4037 | 249 | }; |
coleenp@4037 | 250 | |
coleenp@4037 | 251 | #endif // SHARE_VM_MEMORY_METASPACE_HPP |