Thu, 23 May 2013 12:44:18 +0100
Merge
1 /*
2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 #ifndef SHARE_VM_MEMORY_METASPACE_HPP
25 #define SHARE_VM_MEMORY_METASPACE_HPP
27 #include "memory/allocation.hpp"
28 #include "memory/memRegion.hpp"
29 #include "runtime/virtualspace.hpp"
30 #include "utilities/exceptions.hpp"
32 // Metaspace
33 //
34 // Metaspaces are Arenas for the VM's metadata.
35 // They are allocated one per class loader object, and one for the null
36 // bootstrap class loader
37 // Eventually for bootstrap loader we'll have a read-only section and read-write
38 // to write for DumpSharedSpaces and read for UseSharedSpaces
39 //
40 // block X ---+ +-------------------+
41 // | | Virtualspace |
42 // | | |
43 // | | |
44 // | |-------------------|
45 // | || Chunk |
46 // | || |
47 // | ||---------- |
48 // +------>||| block 0 | |
49 // ||---------- |
50 // ||| block 1 | |
51 // ||---------- |
52 // || |
53 // |-------------------|
54 // | |
55 // | |
56 // +-------------------+
57 //
59 class ClassLoaderData;
60 class Metablock;
61 class MetaWord;
62 class Mutex;
63 class outputStream;
64 class SpaceManager;
66 // Metaspaces each have a SpaceManager and allocations
67 // are done by the SpaceManager. Allocations are done
68 // out of the current Metachunk. When the current Metachunk
69 // is exhausted, the SpaceManager gets a new one from
70 // the current VirtualSpace. When the VirtualSpace is exhausted
71 // the SpaceManager gets a new one. The SpaceManager
72 // also manages freelists of available Chunks.
73 //
74 // Currently the space manager maintains the list of
75 // virtual spaces and the list of chunks in use. Its
76 // allocate() method returns a block for use as a
77 // quantum of metadata.
79 class VirtualSpaceList;
81 class Metaspace : public CHeapObj<mtClass> {
82 friend class VMStructs;
83 friend class SpaceManager;
84 friend class VM_CollectForMetadataAllocation;
85 friend class MetaspaceGC;
86 friend class MetaspaceAux;
88 public:
89 enum MetadataType {ClassType, NonClassType};
90 enum MetaspaceType {
91 StandardMetaspaceType,
92 BootMetaspaceType,
93 ROMetaspaceType,
94 ReadWriteMetaspaceType,
95 AnonymousMetaspaceType,
96 ReflectionMetaspaceType
97 };
99 private:
100 void initialize(Mutex* lock, MetaspaceType type);
102 // Align up the word size to the allocation word size
103 static size_t align_word_size_up(size_t);
105 static size_t _first_chunk_word_size;
106 static size_t _first_class_chunk_word_size;
108 SpaceManager* _vsm;
109 SpaceManager* vsm() const { return _vsm; }
111 SpaceManager* _class_vsm;
112 SpaceManager* class_vsm() const { return _class_vsm; }
114 // Allocate space for metadata of type mdtype. This is space
115 // within a Metachunk and is used by
116 // allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
117 // which returns a Metablock.
118 MetaWord* allocate(size_t word_size, MetadataType mdtype);
120 // Virtual Space lists for both classes and other metadata
121 static VirtualSpaceList* _space_list;
122 static VirtualSpaceList* _class_space_list;
124 static VirtualSpaceList* space_list() { return _space_list; }
125 static VirtualSpaceList* class_space_list() { return _class_space_list; }
127 public:
129 Metaspace(Mutex* lock, MetaspaceType type);
130 ~Metaspace();
132 // Initialize globals for Metaspace
133 static void global_initialize();
134 static void initialize_class_space(ReservedSpace rs);
136 static size_t first_chunk_word_size() { return _first_chunk_word_size; }
137 static size_t first_class_chunk_word_size() { return _first_class_chunk_word_size; }
139 char* bottom() const;
140 size_t used_words_slow(MetadataType mdtype) const;
141 size_t free_words(MetadataType mdtype) const;
142 size_t capacity_words_slow(MetadataType mdtype) const;
143 size_t waste_words(MetadataType mdtype) const;
145 size_t used_bytes_slow(MetadataType mdtype) const;
146 size_t capacity_bytes_slow(MetadataType mdtype) const;
148 static Metablock* allocate(ClassLoaderData* loader_data, size_t size,
149 bool read_only, MetadataType mdtype, TRAPS);
150 void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
152 MetaWord* expand_and_allocate(size_t size,
153 MetadataType mdtype);
155 static bool is_initialized() { return _class_space_list != NULL; }
157 static bool contains(const void *ptr);
158 void dump(outputStream* const out) const;
160 // Free empty virtualspaces
161 static void purge();
163 void print_on(outputStream* st) const;
164 // Debugging support
165 void verify();
166 };
168 class MetaspaceAux : AllStatic {
170 // Statistics for class space and data space in metaspace.
172 // These methods iterate over the classloader data graph
173 // for the given Metaspace type. These are slow.
174 static size_t used_bytes_slow(Metaspace::MetadataType mdtype);
175 static size_t free_in_bytes(Metaspace::MetadataType mdtype);
176 static size_t capacity_bytes_slow(Metaspace::MetadataType mdtype);
178 // Iterates over the virtual space list.
179 static size_t reserved_in_bytes(Metaspace::MetadataType mdtype);
181 static size_t free_chunks_total(Metaspace::MetadataType mdtype);
182 static size_t free_chunks_total_in_bytes(Metaspace::MetadataType mdtype);
184 public:
185 // Running sum of space in all Metachunks that has been
186 // allocated to a Metaspace. This is used instead of
187 // iterating over all the classloaders
188 static size_t _allocated_capacity_words;
189 // Running sum of space in all Metachunks that have
190 // are being used for metadata.
191 static size_t _allocated_used_words;
193 public:
194 // Decrement and increment _allocated_capacity_words
195 static void dec_capacity(size_t words);
196 static void inc_capacity(size_t words);
198 // Decrement and increment _allocated_used_words
199 static void dec_used(size_t words);
200 static void inc_used(size_t words);
202 // Total of space allocated to metadata in all Metaspaces.
203 // This sums the space used in each Metachunk by
204 // iterating over the classloader data graph
205 static size_t used_bytes_slow() {
206 return used_bytes_slow(Metaspace::ClassType) +
207 used_bytes_slow(Metaspace::NonClassType);
208 }
210 // Used by MetaspaceCounters
211 static size_t free_chunks_total();
212 static size_t free_chunks_total_in_bytes();
214 static size_t allocated_capacity_words() {
215 return _allocated_capacity_words;
216 }
217 static size_t allocated_capacity_bytes() {
218 return _allocated_capacity_words * BytesPerWord;
219 }
221 static size_t allocated_used_words() {
222 return _allocated_used_words;
223 }
224 static size_t allocated_used_bytes() {
225 return _allocated_used_words * BytesPerWord;
226 }
228 static size_t free_bytes();
230 // Total capacity in all Metaspaces
231 static size_t capacity_bytes_slow() {
232 #ifdef PRODUCT
233 // Use allocated_capacity_bytes() in PRODUCT instead of this function.
234 guarantee(false, "Should not call capacity_bytes_slow() in the PRODUCT");
235 #endif
236 size_t class_capacity = capacity_bytes_slow(Metaspace::ClassType);
237 size_t non_class_capacity = capacity_bytes_slow(Metaspace::NonClassType);
238 assert(allocated_capacity_bytes() == class_capacity + non_class_capacity,
239 err_msg("bad accounting: allocated_capacity_bytes() " SIZE_FORMAT
240 " class_capacity + non_class_capacity " SIZE_FORMAT
241 " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT,
242 allocated_capacity_bytes(), class_capacity + non_class_capacity,
243 class_capacity, non_class_capacity));
245 return class_capacity + non_class_capacity;
246 }
248 // Total space reserved in all Metaspaces
249 static size_t reserved_in_bytes() {
250 return reserved_in_bytes(Metaspace::ClassType) +
251 reserved_in_bytes(Metaspace::NonClassType);
252 }
254 static size_t min_chunk_size();
256 // Print change in used metadata.
257 static void print_metaspace_change(size_t prev_metadata_used);
258 static void print_on(outputStream * out);
259 static void print_on(outputStream * out, Metaspace::MetadataType mdtype);
261 static void print_waste(outputStream* out);
262 static void dump(outputStream* out);
263 static void verify_free_chunks();
264 // Checks that the values returned by allocated_capacity_bytes() and
265 // capacity_bytes_slow() are the same.
266 static void verify_capacity();
267 static void verify_used();
268 static void verify_metrics();
269 };
271 // Metaspace are deallocated when their class loader are GC'ed.
272 // This class implements a policy for inducing GC's to recover
273 // Metaspaces.
275 class MetaspaceGC : AllStatic {
277 // The current high-water-mark for inducing a GC. When
278 // the capacity of all space in the virtual lists reaches this value,
279 // a GC is induced and the value is increased. This should be changed
280 // to the space actually used for allocations to avoid affects of
281 // fragmentation losses to partially used chunks. Size is in words.
282 static size_t _capacity_until_GC;
284 // After a GC is done any allocation that fails should try to expand
285 // the capacity of the Metaspaces. This flag is set during attempts
286 // to allocate in the VMGCOperation that does the GC.
287 static bool _expand_after_GC;
289 // For a CMS collection, signal that a concurrent collection should
290 // be started.
291 static bool _should_concurrent_collect;
293 static uint _shrink_factor;
295 static void set_capacity_until_GC(size_t v) { _capacity_until_GC = v; }
297 static size_t shrink_factor() { return _shrink_factor; }
298 void set_shrink_factor(uint v) { _shrink_factor = v; }
300 public:
302 static size_t capacity_until_GC() { return _capacity_until_GC; }
303 static void inc_capacity_until_GC(size_t v) { _capacity_until_GC += v; }
304 static void dec_capacity_until_GC(size_t v) {
305 _capacity_until_GC = _capacity_until_GC > v ? _capacity_until_GC - v : 0;
306 }
307 static bool expand_after_GC() { return _expand_after_GC; }
308 static void set_expand_after_GC(bool v) { _expand_after_GC = v; }
310 static bool should_concurrent_collect() { return _should_concurrent_collect; }
311 static void set_should_concurrent_collect(bool v) {
312 _should_concurrent_collect = v;
313 }
315 // The amount to increase the high-water-mark (_capacity_until_GC)
316 static size_t delta_capacity_until_GC(size_t word_size);
318 // It is expected that this will be called when the current capacity
319 // has been used and a GC should be considered.
320 static bool should_expand(VirtualSpaceList* vsl, size_t word_size);
322 // Calculate the new high-water mark at which to induce
323 // a GC.
324 static void compute_new_size();
325 };
327 #endif // SHARE_VM_MEMORY_METASPACE_HPP