src/share/vm/memory/metaspace.cpp

Tue, 16 Feb 2016 21:42:29 +0000

author
poonam
date
Tue, 16 Feb 2016 21:42:29 +0000
changeset 8308
6acf14e730dd
parent 7867
57d4971ff1df
child 7994
04ff2f6cd0eb
child 9021
f23241cde362
permissions
-rw-r--r--

8072725: Provide more granular levels for GC verification
Summary: Add option VerifySubSet to selectively verify the memory sub-systems
Reviewed-by: kevinw, jmasa

coleenp@4037 1 /*
drchase@6680 2 * Copyright (c) 2011, 2014, 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 #include "precompiled.hpp"
coleenp@4037 25 #include "gc_interface/collectedHeap.hpp"
stefank@5771 26 #include "memory/allocation.hpp"
coleenp@4037 27 #include "memory/binaryTreeDictionary.hpp"
jmasa@4196 28 #include "memory/freeList.hpp"
coleenp@4037 29 #include "memory/collectorPolicy.hpp"
coleenp@4037 30 #include "memory/filemap.hpp"
coleenp@4037 31 #include "memory/freeList.hpp"
stefank@5863 32 #include "memory/gcLocker.hpp"
jmasa@4196 33 #include "memory/metachunk.hpp"
coleenp@4037 34 #include "memory/metaspace.hpp"
ehelin@6417 35 #include "memory/metaspaceGCThresholdUpdater.hpp"
coleenp@4037 36 #include "memory/metaspaceShared.hpp"
ehelin@6417 37 #include "memory/metaspaceTracer.hpp"
coleenp@4037 38 #include "memory/resourceArea.hpp"
coleenp@4037 39 #include "memory/universe.hpp"
stefank@5863 40 #include "runtime/atomic.inline.hpp"
coleenp@4037 41 #include "runtime/globals.hpp"
stefank@5863 42 #include "runtime/init.hpp"
hseigel@5528 43 #include "runtime/java.hpp"
coleenp@4037 44 #include "runtime/mutex.hpp"
goetz@6911 45 #include "runtime/orderAccess.inline.hpp"
coleenp@4037 46 #include "services/memTracker.hpp"
stefank@5864 47 #include "services/memoryService.hpp"
coleenp@4037 48 #include "utilities/copy.hpp"
coleenp@4037 49 #include "utilities/debug.hpp"
coleenp@4037 50
drchase@6680 51 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 52
goetz@6337 53 typedef BinaryTreeDictionary<Metablock, FreeList<Metablock> > BlockTreeDictionary;
goetz@6337 54 typedef BinaryTreeDictionary<Metachunk, FreeList<Metachunk> > ChunkTreeDictionary;
stefank@5941 55
stefank@5941 56 // Set this constant to enable slow integrity checking of the free chunk lists
mgerdin@4264 57 const bool metaspace_slow_verify = false;
mgerdin@4264 58
mgerdin@5699 59 size_t const allocation_from_dictionary_limit = 4 * K;
coleenp@4037 60
coleenp@4037 61 MetaWord* last_allocated = 0;
coleenp@4037 62
coleenp@6029 63 size_t Metaspace::_compressed_class_space_size;
ehelin@6417 64 const MetaspaceTracer* Metaspace::_tracer = NULL;
hseigel@5528 65
coleenp@4037 66 // Used in declarations in SpaceManager and ChunkManager
coleenp@4037 67 enum ChunkIndex {
jmasa@4382 68 ZeroIndex = 0,
jmasa@4382 69 SpecializedIndex = ZeroIndex,
jmasa@4382 70 SmallIndex = SpecializedIndex + 1,
jmasa@4382 71 MediumIndex = SmallIndex + 1,
jmasa@4382 72 HumongousIndex = MediumIndex + 1,
jmasa@4382 73 NumberOfFreeLists = 3,
jmasa@4382 74 NumberOfInUseLists = 4
jmasa@4382 75 };
jmasa@4382 76
jmasa@4382 77 enum ChunkSizes { // in words.
jmasa@4382 78 ClassSpecializedChunk = 128,
jmasa@4382 79 SpecializedChunk = 128,
jmasa@4382 80 ClassSmallChunk = 256,
jmasa@4382 81 SmallChunk = 512,
coleenp@5337 82 ClassMediumChunk = 4 * K,
mgerdin@6004 83 MediumChunk = 8 * K
coleenp@4037 84 };
coleenp@4037 85
coleenp@4037 86 static ChunkIndex next_chunk_index(ChunkIndex i) {
jmasa@4196 87 assert(i < NumberOfInUseLists, "Out of bound");
coleenp@4037 88 return (ChunkIndex) (i+1);
coleenp@4037 89 }
coleenp@4037 90
stefank@5863 91 volatile intptr_t MetaspaceGC::_capacity_until_GC = 0;
coleenp@4037 92 uint MetaspaceGC::_shrink_factor = 0;
coleenp@4037 93 bool MetaspaceGC::_should_concurrent_collect = false;
coleenp@4037 94
jmasa@4932 95 typedef class FreeList<Metachunk> ChunkList;
coleenp@4037 96
coleenp@4037 97 // Manages the global free lists of chunks.
stefank@5771 98 class ChunkManager : public CHeapObj<mtInternal> {
mgerdin@6004 99 friend class TestVirtualSpaceNodeTest;
coleenp@4037 100
coleenp@4037 101 // Free list of chunks of different sizes.
jmasa@5007 102 // SpecializedChunk
coleenp@4037 103 // SmallChunk
coleenp@4037 104 // MediumChunk
coleenp@4037 105 // HumongousChunk
jmasa@4196 106 ChunkList _free_chunks[NumberOfFreeLists];
jmasa@4196 107
jmasa@4196 108 // HumongousChunk
jmasa@4196 109 ChunkTreeDictionary _humongous_dictionary;
coleenp@4037 110
coleenp@4037 111 // ChunkManager in all lists of this type
coleenp@4037 112 size_t _free_chunks_total;
coleenp@4037 113 size_t _free_chunks_count;
coleenp@4037 114
coleenp@4037 115 void dec_free_chunks_total(size_t v) {
coleenp@4037 116 assert(_free_chunks_count > 0 &&
coleenp@4037 117 _free_chunks_total > 0,
coleenp@4037 118 "About to go negative");
coleenp@4037 119 Atomic::add_ptr(-1, &_free_chunks_count);
coleenp@4037 120 jlong minus_v = (jlong) - (jlong) v;
coleenp@4037 121 Atomic::add_ptr(minus_v, &_free_chunks_total);
coleenp@4037 122 }
coleenp@4037 123
coleenp@4037 124 // Debug support
coleenp@4037 125
coleenp@4037 126 size_t sum_free_chunks();
coleenp@4037 127 size_t sum_free_chunks_count();
coleenp@4037 128
coleenp@4037 129 void locked_verify_free_chunks_total();
mgerdin@4264 130 void slow_locked_verify_free_chunks_total() {
mgerdin@4264 131 if (metaspace_slow_verify) {
mgerdin@4264 132 locked_verify_free_chunks_total();
mgerdin@4264 133 }
mgerdin@4264 134 }
coleenp@4037 135 void locked_verify_free_chunks_count();
mgerdin@4264 136 void slow_locked_verify_free_chunks_count() {
mgerdin@4264 137 if (metaspace_slow_verify) {
mgerdin@4264 138 locked_verify_free_chunks_count();
mgerdin@4264 139 }
mgerdin@4264 140 }
coleenp@4037 141 void verify_free_chunks_count();
coleenp@4037 142
coleenp@4037 143 public:
coleenp@4037 144
stefank@5771 145 ChunkManager(size_t specialized_size, size_t small_size, size_t medium_size)
stefank@5771 146 : _free_chunks_total(0), _free_chunks_count(0) {
stefank@5771 147 _free_chunks[SpecializedIndex].set_size(specialized_size);
stefank@5771 148 _free_chunks[SmallIndex].set_size(small_size);
stefank@5771 149 _free_chunks[MediumIndex].set_size(medium_size);
stefank@5771 150 }
coleenp@4037 151
coleenp@4037 152 // add or delete (return) a chunk to the global freelist.
coleenp@4037 153 Metachunk* chunk_freelist_allocate(size_t word_size);
coleenp@4037 154
jmasa@4382 155 // Map a size to a list index assuming that there are lists
jmasa@4382 156 // for special, small, medium, and humongous chunks.
jmasa@4382 157 static ChunkIndex list_index(size_t size);
jmasa@4382 158
jmasa@5007 159 // Remove the chunk from its freelist. It is
jmasa@5007 160 // expected to be on one of the _free_chunks[] lists.
jmasa@5007 161 void remove_chunk(Metachunk* chunk);
jmasa@5007 162
jmasa@4932 163 // Add the simple linked list of chunks to the freelist of chunks
jmasa@4932 164 // of type index.
jmasa@4932 165 void return_chunks(ChunkIndex index, Metachunk* chunks);
jmasa@4932 166
coleenp@4037 167 // Total of the space in the free chunks list
ehelin@5703 168 size_t free_chunks_total_words();
ehelin@5703 169 size_t free_chunks_total_bytes();
coleenp@4037 170
coleenp@4037 171 // Number of chunks in the free chunks list
coleenp@4037 172 size_t free_chunks_count();
coleenp@4037 173
coleenp@4037 174 void inc_free_chunks_total(size_t v, size_t count = 1) {
coleenp@4037 175 Atomic::add_ptr(count, &_free_chunks_count);
coleenp@4037 176 Atomic::add_ptr(v, &_free_chunks_total);
coleenp@4037 177 }
jmasa@4196 178 ChunkTreeDictionary* humongous_dictionary() {
jmasa@4196 179 return &_humongous_dictionary;
jmasa@4196 180 }
coleenp@4037 181
coleenp@4037 182 ChunkList* free_chunks(ChunkIndex index);
coleenp@4037 183
coleenp@4037 184 // Returns the list for the given chunk word size.
coleenp@4037 185 ChunkList* find_free_chunks_list(size_t word_size);
coleenp@4037 186
stefank@5945 187 // Remove from a list by size. Selects list based on size of chunk.
coleenp@4037 188 Metachunk* free_chunks_get(size_t chunk_word_size);
coleenp@4037 189
ehelin@6420 190 #define index_bounds_check(index) \
ehelin@6420 191 assert(index == SpecializedIndex || \
ehelin@6420 192 index == SmallIndex || \
ehelin@6420 193 index == MediumIndex || \
ehelin@6420 194 index == HumongousIndex, err_msg("Bad index: %d", (int) index))
ehelin@6420 195
ehelin@6420 196 size_t num_free_chunks(ChunkIndex index) const {
ehelin@6420 197 index_bounds_check(index);
ehelin@6420 198
ehelin@6420 199 if (index == HumongousIndex) {
ehelin@6420 200 return _humongous_dictionary.total_free_blocks();
ehelin@6420 201 }
ehelin@6420 202
ehelin@6420 203 ssize_t count = _free_chunks[index].count();
ehelin@6420 204 return count == -1 ? 0 : (size_t) count;
ehelin@6420 205 }
ehelin@6420 206
ehelin@6420 207 size_t size_free_chunks_in_bytes(ChunkIndex index) const {
ehelin@6420 208 index_bounds_check(index);
ehelin@6420 209
ehelin@6420 210 size_t word_size = 0;
ehelin@6420 211 if (index == HumongousIndex) {
ehelin@6420 212 word_size = _humongous_dictionary.total_size();
ehelin@6420 213 } else {
ehelin@6420 214 const size_t size_per_chunk_in_words = _free_chunks[index].size();
ehelin@6420 215 word_size = size_per_chunk_in_words * num_free_chunks(index);
ehelin@6420 216 }
ehelin@6420 217
ehelin@6420 218 return word_size * BytesPerWord;
ehelin@6420 219 }
ehelin@6420 220
ehelin@6420 221 MetaspaceChunkFreeListSummary chunk_free_list_summary() const {
ehelin@6420 222 return MetaspaceChunkFreeListSummary(num_free_chunks(SpecializedIndex),
ehelin@6420 223 num_free_chunks(SmallIndex),
ehelin@6420 224 num_free_chunks(MediumIndex),
ehelin@6420 225 num_free_chunks(HumongousIndex),
ehelin@6420 226 size_free_chunks_in_bytes(SpecializedIndex),
ehelin@6420 227 size_free_chunks_in_bytes(SmallIndex),
ehelin@6420 228 size_free_chunks_in_bytes(MediumIndex),
ehelin@6420 229 size_free_chunks_in_bytes(HumongousIndex));
ehelin@6420 230 }
ehelin@6420 231
coleenp@4037 232 // Debug support
coleenp@4037 233 void verify();
mgerdin@4264 234 void slow_verify() {
mgerdin@4264 235 if (metaspace_slow_verify) {
mgerdin@4264 236 verify();
mgerdin@4264 237 }
mgerdin@4264 238 }
coleenp@4037 239 void locked_verify();
mgerdin@4264 240 void slow_locked_verify() {
mgerdin@4264 241 if (metaspace_slow_verify) {
mgerdin@4264 242 locked_verify();
mgerdin@4264 243 }
mgerdin@4264 244 }
coleenp@4037 245 void verify_free_chunks_total();
coleenp@4037 246
coleenp@4037 247 void locked_print_free_chunks(outputStream* st);
coleenp@4037 248 void locked_print_sum_free_chunks(outputStream* st);
jmasa@4196 249
stefank@5771 250 void print_on(outputStream* st) const;
coleenp@4037 251 };
coleenp@4037 252
coleenp@4037 253 // Used to manage the free list of Metablocks (a block corresponds
coleenp@4037 254 // to the allocation of a quantum of metadata).
coleenp@4037 255 class BlockFreelist VALUE_OBJ_CLASS_SPEC {
jmasa@4196 256 BlockTreeDictionary* _dictionary;
jmasa@4196 257
mgerdin@5699 258 // Only allocate and split from freelist if the size of the allocation
mgerdin@5699 259 // is at least 1/4th the size of the available block.
mgerdin@5699 260 const static int WasteMultiplier = 4;
mgerdin@5699 261
coleenp@4037 262 // Accessors
jmasa@4196 263 BlockTreeDictionary* dictionary() const { return _dictionary; }
coleenp@4037 264
coleenp@4037 265 public:
coleenp@4037 266 BlockFreelist();
coleenp@4037 267 ~BlockFreelist();
coleenp@4037 268
coleenp@4037 269 // Get and return a block to the free list
jmasa@4196 270 MetaWord* get_block(size_t word_size);
jmasa@4196 271 void return_block(MetaWord* p, size_t word_size);
jmasa@4196 272
jmasa@4196 273 size_t total_size() {
jmasa@4196 274 if (dictionary() == NULL) {
coleenp@4037 275 return 0;
jmasa@4196 276 } else {
jmasa@4196 277 return dictionary()->total_size();
coleenp@4037 278 }
jmasa@4196 279 }
coleenp@4037 280
coleenp@4037 281 void print_on(outputStream* st) const;
coleenp@4037 282 };
coleenp@4037 283
stefank@5941 284 // A VirtualSpaceList node.
coleenp@4037 285 class VirtualSpaceNode : public CHeapObj<mtClass> {
coleenp@4037 286 friend class VirtualSpaceList;
coleenp@4037 287
coleenp@4037 288 // Link to next VirtualSpaceNode
coleenp@4037 289 VirtualSpaceNode* _next;
coleenp@4037 290
coleenp@4037 291 // total in the VirtualSpace
coleenp@4037 292 MemRegion _reserved;
coleenp@4037 293 ReservedSpace _rs;
coleenp@4037 294 VirtualSpace _virtual_space;
coleenp@4037 295 MetaWord* _top;
jmasa@5007 296 // count of chunks contained in this VirtualSpace
jmasa@5007 297 uintx _container_count;
coleenp@4037 298
coleenp@4037 299 // Convenience functions to access the _virtual_space
coleenp@4037 300 char* low() const { return virtual_space()->low(); }
coleenp@4037 301 char* high() const { return virtual_space()->high(); }
coleenp@4037 302
jmasa@5007 303 // The first Metachunk will be allocated at the bottom of the
jmasa@5007 304 // VirtualSpace
jmasa@5007 305 Metachunk* first_chunk() { return (Metachunk*) bottom(); }
jmasa@5007 306
mgerdin@6004 307 // Committed but unused space in the virtual space
mgerdin@6004 308 size_t free_words_in_vs() const;
coleenp@4037 309 public:
coleenp@4037 310
coleenp@4037 311 VirtualSpaceNode(size_t byte_size);
jmasa@5007 312 VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {}
coleenp@4037 313 ~VirtualSpaceNode();
coleenp@4037 314
hseigel@5528 315 // Convenience functions for logical bottom and end
hseigel@5528 316 MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
hseigel@5528 317 MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
hseigel@5528 318
coleenp@6678 319 bool contains(const void* ptr) { return ptr >= low() && ptr < high(); }
coleenp@6678 320
stefank@5704 321 size_t reserved_words() const { return _virtual_space.reserved_size() / BytesPerWord; }
stefank@5704 322 size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; }
stefank@5704 323
stefank@5863 324 bool is_pre_committed() const { return _virtual_space.special(); }
stefank@5863 325
coleenp@4037 326 // address of next available space in _virtual_space;
coleenp@4037 327 // Accessors
coleenp@4037 328 VirtualSpaceNode* next() { return _next; }
coleenp@4037 329 void set_next(VirtualSpaceNode* v) { _next = v; }
coleenp@4037 330
coleenp@4037 331 void set_reserved(MemRegion const v) { _reserved = v; }
coleenp@4037 332 void set_top(MetaWord* v) { _top = v; }
coleenp@4037 333
coleenp@4037 334 // Accessors
coleenp@4037 335 MemRegion* reserved() { return &_reserved; }
coleenp@4037 336 VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
coleenp@4037 337
jmasa@5015 338 // Returns true if "word_size" is available in the VirtualSpace
stefank@6170 339 bool is_available(size_t word_size) { return word_size <= pointer_delta(end(), _top, sizeof(MetaWord)); }
coleenp@4037 340
coleenp@4037 341 MetaWord* top() const { return _top; }
coleenp@4037 342 void inc_top(size_t word_size) { _top += word_size; }
coleenp@4037 343
jmasa@5007 344 uintx container_count() { return _container_count; }
stefank@5771 345 void inc_container_count();
jmasa@5007 346 void dec_container_count();
jmasa@5007 347 #ifdef ASSERT
stefank@5771 348 uint container_count_slow();
jmasa@5007 349 void verify_container_count();
jmasa@5007 350 #endif
jmasa@5007 351
coleenp@4037 352 // used and capacity in this single entry in the list
coleenp@4037 353 size_t used_words_in_vs() const;
coleenp@4037 354 size_t capacity_words_in_vs() const;
coleenp@4037 355
coleenp@4037 356 bool initialize();
coleenp@4037 357
coleenp@4037 358 // get space from the virtual space
coleenp@4037 359 Metachunk* take_from_committed(size_t chunk_word_size);
coleenp@4037 360
coleenp@4037 361 // Allocate a chunk from the virtual space and return it.
coleenp@4037 362 Metachunk* get_chunk_vs(size_t chunk_word_size);
coleenp@4037 363
coleenp@4037 364 // Expands/shrinks the committed space in a virtual space. Delegates
coleenp@4037 365 // to Virtualspace
stefank@5863 366 bool expand_by(size_t min_words, size_t preferred_words);
coleenp@4037 367
jmasa@5007 368 // In preparation for deleting this node, remove all the chunks
jmasa@5007 369 // in the node from any freelist.
jmasa@5007 370 void purge(ChunkManager* chunk_manager);
jmasa@5007 371
mgerdin@6004 372 // If an allocation doesn't fit in the current node a new node is created.
mgerdin@6004 373 // Allocate chunks out of the remaining committed space in this node
mgerdin@6004 374 // to avoid wasting that memory.
mgerdin@6004 375 // This always adds up because all the chunk sizes are multiples of
mgerdin@6004 376 // the smallest chunk size.
mgerdin@6004 377 void retire(ChunkManager* chunk_manager);
mgerdin@6004 378
coleenp@4304 379 #ifdef ASSERT
coleenp@4037 380 // Debug support
coleenp@4037 381 void mangle();
coleenp@4304 382 #endif
coleenp@4037 383
coleenp@4037 384 void print_on(outputStream* st) const;
coleenp@4037 385 };
coleenp@4037 386
stefank@5863 387 #define assert_is_ptr_aligned(ptr, alignment) \
stefank@5863 388 assert(is_ptr_aligned(ptr, alignment), \
stefank@5863 389 err_msg(PTR_FORMAT " is not aligned to " \
stefank@5863 390 SIZE_FORMAT, ptr, alignment))
stefank@5863 391
stefank@5863 392 #define assert_is_size_aligned(size, alignment) \
stefank@5863 393 assert(is_size_aligned(size, alignment), \
stefank@5863 394 err_msg(SIZE_FORMAT " is not aligned to " \
stefank@5863 395 SIZE_FORMAT, size, alignment))
stefank@5863 396
stefank@5863 397
stefank@5863 398 // Decide if large pages should be committed when the memory is reserved.
stefank@5863 399 static bool should_commit_large_pages_when_reserving(size_t bytes) {
stefank@5863 400 if (UseLargePages && UseLargePagesInMetaspace && !os::can_commit_large_page_memory()) {
stefank@5863 401 size_t words = bytes / BytesPerWord;
stefank@5863 402 bool is_class = false; // We never reserve large pages for the class space.
stefank@5863 403 if (MetaspaceGC::can_expand(words, is_class) &&
stefank@5863 404 MetaspaceGC::allowed_expansion() >= words) {
stefank@5863 405 return true;
stefank@5863 406 }
stefank@5863 407 }
stefank@5863 408
stefank@5863 409 return false;
stefank@5863 410 }
stefank@5863 411
coleenp@4037 412 // byte_size is the size of the associated virtualspace.
stefank@5863 413 VirtualSpaceNode::VirtualSpaceNode(size_t bytes) : _top(NULL), _next(NULL), _rs(), _container_count(0) {
stefank@5863 414 assert_is_size_aligned(bytes, Metaspace::reserve_alignment());
zgu@4752 415
iklam@7089 416 #if INCLUDE_CDS
coleenp@4804 417 // This allocates memory with mmap. For DumpSharedspaces, try to reserve
coleenp@4804 418 // configurable address, generally at the top of the Java heap so other
coleenp@4804 419 // memory addresses don't conflict.
coleenp@4037 420 if (DumpSharedSpaces) {
stefank@5863 421 bool large_pages = false; // No large pages when dumping the CDS archive.
stefank@5863 422 char* shared_base = (char*)align_ptr_up((char*)SharedBaseAddress, Metaspace::reserve_alignment());
stefank@5863 423
stefank@5863 424 _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages, shared_base, 0);
coleenp@4037 425 if (_rs.is_reserved()) {
coleenp@4804 426 assert(shared_base == 0 || _rs.base() == shared_base, "should match");
coleenp@4037 427 } else {
coleenp@4804 428 // Get a mmap region anywhere if the SharedBaseAddress fails.
stefank@5863 429 _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages);
coleenp@4037 430 }
coleenp@4037 431 MetaspaceShared::set_shared_rs(&_rs);
iklam@7089 432 } else
iklam@7089 433 #endif
iklam@7089 434 {
stefank@5863 435 bool large_pages = should_commit_large_pages_when_reserving(bytes);
stefank@5863 436
stefank@5863 437 _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages);
coleenp@4037 438 }
coleenp@4037 439
stefank@5863 440 if (_rs.is_reserved()) {
stefank@5863 441 assert(_rs.base() != NULL, "Catch if we get a NULL address");
stefank@5863 442 assert(_rs.size() != 0, "Catch if we get a 0 size");
stefank@5863 443 assert_is_ptr_aligned(_rs.base(), Metaspace::reserve_alignment());
stefank@5863 444 assert_is_size_aligned(_rs.size(), Metaspace::reserve_alignment());
stefank@5863 445
stefank@5863 446 MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
stefank@5863 447 }
coleenp@4037 448 }
coleenp@4037 449
jmasa@5007 450 void VirtualSpaceNode::purge(ChunkManager* chunk_manager) {
jmasa@5007 451 Metachunk* chunk = first_chunk();
jmasa@5007 452 Metachunk* invalid_chunk = (Metachunk*) top();
jmasa@5007 453 while (chunk < invalid_chunk ) {
stefank@5941 454 assert(chunk->is_tagged_free(), "Should be tagged free");
stefank@5941 455 MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
stefank@5941 456 chunk_manager->remove_chunk(chunk);
stefank@5941 457 assert(chunk->next() == NULL &&
stefank@5941 458 chunk->prev() == NULL,
stefank@5941 459 "Was not removed from its list");
stefank@5941 460 chunk = (Metachunk*) next;
jmasa@5007 461 }
jmasa@5007 462 }
jmasa@5007 463
jmasa@5007 464 #ifdef ASSERT
jmasa@5007 465 uint VirtualSpaceNode::container_count_slow() {
jmasa@5007 466 uint count = 0;
jmasa@5007 467 Metachunk* chunk = first_chunk();
jmasa@5007 468 Metachunk* invalid_chunk = (Metachunk*) top();
jmasa@5007 469 while (chunk < invalid_chunk ) {
jmasa@5007 470 MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
jmasa@5007 471 // Don't count the chunks on the free lists. Those are
jmasa@5007 472 // still part of the VirtualSpaceNode but not currently
jmasa@5007 473 // counted.
stefank@5941 474 if (!chunk->is_tagged_free()) {
jmasa@5007 475 count++;
jmasa@5007 476 }
jmasa@5007 477 chunk = (Metachunk*) next;
jmasa@5007 478 }
jmasa@5007 479 return count;
jmasa@5007 480 }
jmasa@5007 481 #endif
jmasa@5007 482
coleenp@4037 483 // List of VirtualSpaces for metadata allocation.
coleenp@4037 484 class VirtualSpaceList : public CHeapObj<mtClass> {
coleenp@4037 485 friend class VirtualSpaceNode;
coleenp@4037 486
coleenp@4037 487 enum VirtualSpaceSizes {
coleenp@4037 488 VirtualSpaceSize = 256 * K
coleenp@4037 489 };
coleenp@4037 490
coleenp@4037 491 // Head of the list
coleenp@4037 492 VirtualSpaceNode* _virtual_space_list;
coleenp@4037 493 // virtual space currently being used for allocations
coleenp@4037 494 VirtualSpaceNode* _current_virtual_space;
coleenp@4037 495
stefank@5863 496 // Is this VirtualSpaceList used for the compressed class space
coleenp@4037 497 bool _is_class;
coleenp@4037 498
stefank@5704 499 // Sum of reserved and committed memory in the virtual spaces
stefank@5704 500 size_t _reserved_words;
stefank@5704 501 size_t _committed_words;
stefank@5704 502
stefank@5704 503 // Number of virtual spaces
coleenp@4037 504 size_t _virtual_space_count;
coleenp@4037 505
coleenp@4037 506 ~VirtualSpaceList();
coleenp@4037 507
coleenp@4037 508 VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
coleenp@4037 509
coleenp@4037 510 void set_virtual_space_list(VirtualSpaceNode* v) {
coleenp@4037 511 _virtual_space_list = v;
coleenp@4037 512 }
coleenp@4037 513 void set_current_virtual_space(VirtualSpaceNode* v) {
coleenp@4037 514 _current_virtual_space = v;
coleenp@4037 515 }
coleenp@4037 516
stefank@5704 517 void link_vs(VirtualSpaceNode* new_entry);
coleenp@4037 518
coleenp@4037 519 // Get another virtual space and add it to the list. This
coleenp@4037 520 // is typically prompted by a failed attempt to allocate a chunk
coleenp@4037 521 // and is typically followed by the allocation of a chunk.
stefank@5863 522 bool create_new_virtual_space(size_t vs_word_size);
coleenp@4037 523
mgerdin@6004 524 // Chunk up the unused committed space in the current
mgerdin@6004 525 // virtual space and add the chunks to the free list.
mgerdin@6004 526 void retire_current_virtual_space();
mgerdin@6004 527
coleenp@4037 528 public:
coleenp@4037 529 VirtualSpaceList(size_t word_size);
coleenp@4037 530 VirtualSpaceList(ReservedSpace rs);
coleenp@4037 531
jmasa@5015 532 size_t free_bytes();
jmasa@5015 533
jmasa@4382 534 Metachunk* get_new_chunk(size_t word_size,
jmasa@4382 535 size_t grow_chunks_by_words,
jmasa@4382 536 size_t medium_chunk_bunch);
jmasa@4382 537
stefank@5863 538 bool expand_node_by(VirtualSpaceNode* node,
stefank@5863 539 size_t min_words,
stefank@5863 540 size_t preferred_words);
stefank@5863 541
stefank@5863 542 bool expand_by(size_t min_words,
stefank@5863 543 size_t preferred_words);
coleenp@4037 544
coleenp@4037 545 VirtualSpaceNode* current_virtual_space() {
coleenp@4037 546 return _current_virtual_space;
coleenp@4037 547 }
coleenp@4037 548
coleenp@4037 549 bool is_class() const { return _is_class; }
coleenp@4037 550
stefank@5863 551 bool initialization_succeeded() { return _virtual_space_list != NULL; }
coleenp@4037 552
stefank@5704 553 size_t reserved_words() { return _reserved_words; }
stefank@5704 554 size_t reserved_bytes() { return reserved_words() * BytesPerWord; }
stefank@5704 555 size_t committed_words() { return _committed_words; }
stefank@5704 556 size_t committed_bytes() { return committed_words() * BytesPerWord; }
stefank@5704 557
stefank@5704 558 void inc_reserved_words(size_t v);
stefank@5704 559 void dec_reserved_words(size_t v);
stefank@5704 560 void inc_committed_words(size_t v);
stefank@5704 561 void dec_committed_words(size_t v);
jmasa@5007 562 void inc_virtual_space_count();
jmasa@5007 563 void dec_virtual_space_count();
jmasa@5007 564
coleenp@6678 565 bool contains(const void* ptr);
coleenp@6678 566
jmasa@5007 567 // Unlink empty VirtualSpaceNodes and free it.
stefank@5771 568 void purge(ChunkManager* chunk_manager);
coleenp@4037 569
coleenp@4037 570 void print_on(outputStream* st) const;
coleenp@4037 571
coleenp@4037 572 class VirtualSpaceListIterator : public StackObj {
coleenp@4037 573 VirtualSpaceNode* _virtual_spaces;
coleenp@4037 574 public:
coleenp@4037 575 VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
coleenp@4037 576 _virtual_spaces(virtual_spaces) {}
coleenp@4037 577
coleenp@4037 578 bool repeat() {
coleenp@4037 579 return _virtual_spaces != NULL;
coleenp@4037 580 }
coleenp@4037 581
coleenp@4037 582 VirtualSpaceNode* get_next() {
coleenp@4037 583 VirtualSpaceNode* result = _virtual_spaces;
coleenp@4037 584 if (_virtual_spaces != NULL) {
coleenp@4037 585 _virtual_spaces = _virtual_spaces->next();
coleenp@4037 586 }
coleenp@4037 587 return result;
coleenp@4037 588 }
coleenp@4037 589 };
coleenp@4037 590 };
coleenp@4037 591
coleenp@4037 592 class Metadebug : AllStatic {
coleenp@4037 593 // Debugging support for Metaspaces
coleenp@4037 594 static int _allocation_fail_alot_count;
coleenp@4037 595
coleenp@4037 596 public:
coleenp@4037 597
coleenp@4037 598 static void init_allocation_fail_alot_count();
coleenp@4037 599 #ifdef ASSERT
coleenp@4037 600 static bool test_metadata_failure();
coleenp@4037 601 #endif
coleenp@4037 602 };
coleenp@4037 603
coleenp@4037 604 int Metadebug::_allocation_fail_alot_count = 0;
coleenp@4037 605
coleenp@4037 606 // SpaceManager - used by Metaspace to handle allocations
coleenp@4037 607 class SpaceManager : public CHeapObj<mtClass> {
coleenp@4037 608 friend class Metaspace;
coleenp@4037 609 friend class Metadebug;
coleenp@4037 610
coleenp@4037 611 private:
jmasa@4382 612
coleenp@6305 613 // protects allocations
coleenp@4037 614 Mutex* const _lock;
coleenp@4037 615
jmasa@5162 616 // Type of metadata allocated.
jmasa@5162 617 Metaspace::MetadataType _mdtype;
jmasa@5162 618
coleenp@4037 619 // List of chunks in use by this SpaceManager. Allocations
coleenp@4037 620 // are done from the current chunk. The list is used for deallocating
coleenp@4037 621 // chunks when the SpaceManager is freed.
jmasa@4196 622 Metachunk* _chunks_in_use[NumberOfInUseLists];
coleenp@4037 623 Metachunk* _current_chunk;
coleenp@4037 624
jwilhelm@7867 625 // Number of small chunks to allocate to a manager
jwilhelm@7867 626 // If class space manager, small chunks are unlimited
coleenp@4037 627 static uint const _small_chunk_limit;
coleenp@4037 628
coleenp@4037 629 // Sum of all space in allocated chunks
jmasa@5015 630 size_t _allocated_blocks_words;
jmasa@5015 631
jmasa@5015 632 // Sum of all allocated chunks
jmasa@5015 633 size_t _allocated_chunks_words;
jmasa@5015 634 size_t _allocated_chunks_count;
coleenp@4037 635
coleenp@4037 636 // Free lists of blocks are per SpaceManager since they
coleenp@4037 637 // are assumed to be in chunks in use by the SpaceManager
coleenp@4037 638 // and all chunks in use by a SpaceManager are freed when
coleenp@4037 639 // the class loader using the SpaceManager is collected.
coleenp@4037 640 BlockFreelist _block_freelists;
coleenp@4037 641
coleenp@4037 642 // protects virtualspace and chunk expansions
coleenp@4037 643 static const char* _expand_lock_name;
coleenp@4037 644 static const int _expand_lock_rank;
coleenp@4037 645 static Mutex* const _expand_lock;
coleenp@4037 646
jmasa@4382 647 private:
coleenp@4037 648 // Accessors
coleenp@4037 649 Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
coleenp@6305 650 void set_chunks_in_use(ChunkIndex index, Metachunk* v) {
coleenp@6305 651 _chunks_in_use[index] = v;
coleenp@6305 652 }
coleenp@4037 653
coleenp@4037 654 BlockFreelist* block_freelists() const {
coleenp@4037 655 return (BlockFreelist*) &_block_freelists;
coleenp@4037 656 }
coleenp@4037 657
jmasa@5162 658 Metaspace::MetadataType mdtype() { return _mdtype; }
stefank@5771 659
stefank@5771 660 VirtualSpaceList* vs_list() const { return Metaspace::get_space_list(_mdtype); }
stefank@5771 661 ChunkManager* chunk_manager() const { return Metaspace::get_chunk_manager(_mdtype); }
coleenp@4037 662
coleenp@4037 663 Metachunk* current_chunk() const { return _current_chunk; }
coleenp@4037 664 void set_current_chunk(Metachunk* v) {
coleenp@4037 665 _current_chunk = v;
coleenp@4037 666 }
coleenp@4037 667
coleenp@4037 668 Metachunk* find_current_chunk(size_t word_size);
coleenp@4037 669
coleenp@4037 670 // Add chunk to the list of chunks in use
coleenp@4037 671 void add_chunk(Metachunk* v, bool make_current);
mgerdin@5699 672 void retire_current_chunk();
coleenp@4037 673
coleenp@4037 674 Mutex* lock() const { return _lock; }
coleenp@4037 675
jmasa@4382 676 const char* chunk_size_name(ChunkIndex index) const;
jmasa@4382 677
jmasa@4382 678 protected:
jmasa@4382 679 void initialize();
jmasa@4382 680
coleenp@4037 681 public:
jmasa@5162 682 SpaceManager(Metaspace::MetadataType mdtype,
stefank@5771 683 Mutex* lock);
coleenp@4037 684 ~SpaceManager();
coleenp@4037 685
jmasa@4382 686 enum ChunkMultiples {
jmasa@4382 687 MediumChunkMultiple = 4
coleenp@4037 688 };
coleenp@4037 689
stefank@5771 690 bool is_class() { return _mdtype == Metaspace::ClassType; }
stefank@5771 691
coleenp@4037 692 // Accessors
mgerdin@6004 693 size_t specialized_chunk_size() { return (size_t) is_class() ? ClassSpecializedChunk : SpecializedChunk; }
mgerdin@6004 694 size_t small_chunk_size() { return (size_t) is_class() ? ClassSmallChunk : SmallChunk; }
mgerdin@6004 695 size_t medium_chunk_size() { return (size_t) is_class() ? ClassMediumChunk : MediumChunk; }
mgerdin@6004 696 size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; }
mgerdin@6004 697
mgerdin@6004 698 size_t smallest_chunk_size() { return specialized_chunk_size(); }
jmasa@4382 699
jmasa@5015 700 size_t allocated_blocks_words() const { return _allocated_blocks_words; }
jmasa@5015 701 size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; }
jmasa@5015 702 size_t allocated_chunks_words() const { return _allocated_chunks_words; }
jmasa@5015 703 size_t allocated_chunks_count() const { return _allocated_chunks_count; }
jmasa@5015 704
jmasa@4382 705 bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
coleenp@4037 706
coleenp@4037 707 static Mutex* expand_lock() { return _expand_lock; }
coleenp@4037 708
jmasa@5015 709 // Increment the per Metaspace and global running sums for Metachunks
jmasa@5015 710 // by the given size. This is used when a Metachunk to added to
jmasa@5015 711 // the in-use list.
jmasa@5015 712 void inc_size_metrics(size_t words);
jmasa@5015 713 // Increment the per Metaspace and global running sums Metablocks by the given
jmasa@5015 714 // size. This is used when a Metablock is allocated.
jmasa@5015 715 void inc_used_metrics(size_t words);
jmasa@5015 716 // Delete the portion of the running sums for this SpaceManager. That is,
jmasa@5015 717 // the globals running sums for the Metachunks and Metablocks are
jmasa@5015 718 // decremented for all the Metachunks in-use by this SpaceManager.
jmasa@5015 719 void dec_total_from_size_metrics();
jmasa@5015 720
jmasa@4382 721 // Set the sizes for the initial chunks.
jmasa@4382 722 void get_initial_chunk_sizes(Metaspace::MetaspaceType type,
jmasa@4382 723 size_t* chunk_word_size,
jmasa@4382 724 size_t* class_chunk_word_size);
jmasa@4382 725
coleenp@4037 726 size_t sum_capacity_in_chunks_in_use() const;
coleenp@4037 727 size_t sum_used_in_chunks_in_use() const;
coleenp@4037 728 size_t sum_free_in_chunks_in_use() const;
coleenp@4037 729 size_t sum_waste_in_chunks_in_use() const;
coleenp@4037 730 size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const;
coleenp@4037 731
coleenp@4037 732 size_t sum_count_in_chunks_in_use();
coleenp@4037 733 size_t sum_count_in_chunks_in_use(ChunkIndex i);
coleenp@4037 734
jmasa@4382 735 Metachunk* get_new_chunk(size_t word_size, size_t grow_chunks_by_words);
jmasa@4382 736
coleenp@4037 737 // Block allocation and deallocation.
coleenp@4037 738 // Allocates a block from the current chunk
coleenp@4037 739 MetaWord* allocate(size_t word_size);
coleenp@4037 740
coleenp@4037 741 // Helper for allocations
jmasa@4196 742 MetaWord* allocate_work(size_t word_size);
coleenp@4037 743
coleenp@4037 744 // Returns a block to the per manager freelist
jmasa@4196 745 void deallocate(MetaWord* p, size_t word_size);
coleenp@4037 746
coleenp@4037 747 // Based on the allocation size and a minimum chunk size,
coleenp@4037 748 // returned chunk size (for expanding space for chunk allocation).
coleenp@4037 749 size_t calc_chunk_size(size_t allocation_word_size);
coleenp@4037 750
coleenp@4037 751 // Called when an allocation from the current chunk fails.
coleenp@4037 752 // Gets a new chunk (may require getting a new virtual space),
coleenp@4037 753 // and allocates from that chunk.
jmasa@4196 754 MetaWord* grow_and_allocate(size_t word_size);
coleenp@4037 755
stefank@5864 756 // Notify memory usage to MemoryService.
stefank@5864 757 void track_metaspace_memory_usage();
stefank@5864 758
coleenp@4037 759 // debugging support.
coleenp@4037 760
coleenp@4037 761 void dump(outputStream* const out) const;
coleenp@4037 762 void print_on(outputStream* st) const;
coleenp@4037 763 void locked_print_chunks_in_use_on(outputStream* st) const;
coleenp@4037 764
coleenp@4037 765 void verify();
jmasa@4327 766 void verify_chunk_size(Metachunk* chunk);
coleenp@4304 767 NOT_PRODUCT(void mangle_freed_chunks();)
coleenp@4037 768 #ifdef ASSERT
jmasa@5015 769 void verify_allocated_blocks_words();
coleenp@4037 770 #endif
iklam@5208 771
iklam@5208 772 size_t get_raw_word_size(size_t word_size) {
iklam@5208 773 size_t byte_size = word_size * BytesPerWord;
iklam@5208 774
stefank@5941 775 size_t raw_bytes_size = MAX2(byte_size, sizeof(Metablock));
stefank@5941 776 raw_bytes_size = align_size_up(raw_bytes_size, Metachunk::object_alignment());
stefank@5941 777
iklam@5208 778 size_t raw_word_size = raw_bytes_size / BytesPerWord;
iklam@5208 779 assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem");
iklam@5208 780
iklam@5208 781 return raw_word_size;
iklam@5208 782 }
coleenp@4037 783 };
coleenp@4037 784
coleenp@4037 785 uint const SpaceManager::_small_chunk_limit = 4;
coleenp@4037 786
coleenp@4037 787 const char* SpaceManager::_expand_lock_name =
coleenp@4037 788 "SpaceManager chunk allocation lock";
coleenp@4037 789 const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
coleenp@4037 790 Mutex* const SpaceManager::_expand_lock =
coleenp@4037 791 new Mutex(SpaceManager::_expand_lock_rank,
coleenp@4037 792 SpaceManager::_expand_lock_name,
coleenp@4037 793 Mutex::_allow_vm_block_flag);
coleenp@4037 794
jmasa@5007 795 void VirtualSpaceNode::inc_container_count() {
jmasa@5007 796 assert_lock_strong(SpaceManager::expand_lock());
jmasa@5007 797 _container_count++;
jmasa@5007 798 assert(_container_count == container_count_slow(),
jmasa@5007 799 err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
stefank@5771 800 " container_count_slow() " SIZE_FORMAT,
jmasa@5007 801 _container_count, container_count_slow()));
jmasa@5007 802 }
jmasa@5007 803
jmasa@5007 804 void VirtualSpaceNode::dec_container_count() {
jmasa@5007 805 assert_lock_strong(SpaceManager::expand_lock());
jmasa@5007 806 _container_count--;
jmasa@5007 807 }
jmasa@5007 808
jmasa@5007 809 #ifdef ASSERT
jmasa@5007 810 void VirtualSpaceNode::verify_container_count() {
jmasa@5007 811 assert(_container_count == container_count_slow(),
jmasa@5007 812 err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
stefank@5771 813 " container_count_slow() " SIZE_FORMAT, _container_count, container_count_slow()));
jmasa@5007 814 }
jmasa@5007 815 #endif
jmasa@5007 816
coleenp@4037 817 // BlockFreelist methods
coleenp@4037 818
coleenp@4037 819 BlockFreelist::BlockFreelist() : _dictionary(NULL) {}
coleenp@4037 820
coleenp@4037 821 BlockFreelist::~BlockFreelist() {
coleenp@4037 822 if (_dictionary != NULL) {
coleenp@4037 823 if (Verbose && TraceMetadataChunkAllocation) {
coleenp@4037 824 _dictionary->print_free_lists(gclog_or_tty);
coleenp@4037 825 }
coleenp@4037 826 delete _dictionary;
coleenp@4037 827 }
coleenp@4037 828 }
coleenp@4037 829
jmasa@4196 830 void BlockFreelist::return_block(MetaWord* p, size_t word_size) {
stefank@5941 831 Metablock* free_chunk = ::new (p) Metablock(word_size);
coleenp@4037 832 if (dictionary() == NULL) {
jmasa@4196 833 _dictionary = new BlockTreeDictionary();
coleenp@4037 834 }
jmasa@4196 835 dictionary()->return_chunk(free_chunk);
coleenp@4037 836 }
coleenp@4037 837
jmasa@4196 838 MetaWord* BlockFreelist::get_block(size_t word_size) {
coleenp@4037 839 if (dictionary() == NULL) {
coleenp@4037 840 return NULL;
coleenp@4037 841 }
coleenp@4037 842
goetz@6337 843 if (word_size < TreeChunk<Metablock, FreeList<Metablock> >::min_size()) {
jmasa@4196 844 // Dark matter. Too small for dictionary.
coleenp@4037 845 return NULL;
coleenp@4037 846 }
jmasa@4196 847
jmasa@4196 848 Metablock* free_block =
mgerdin@5699 849 dictionary()->get_chunk(word_size, FreeBlockDictionary<Metablock>::atLeast);
jmasa@4196 850 if (free_block == NULL) {
jmasa@4196 851 return NULL;
jmasa@4196 852 }
jmasa@4196 853
mgerdin@5699 854 const size_t block_size = free_block->size();
mgerdin@5699 855 if (block_size > WasteMultiplier * word_size) {
mgerdin@5699 856 return_block((MetaWord*)free_block, block_size);
mgerdin@5699 857 return NULL;
mgerdin@5699 858 }
mgerdin@5699 859
mgerdin@5699 860 MetaWord* new_block = (MetaWord*)free_block;
mgerdin@5699 861 assert(block_size >= word_size, "Incorrect size of block from freelist");
mgerdin@5699 862 const size_t unused = block_size - word_size;
goetz@6337 863 if (unused >= TreeChunk<Metablock, FreeList<Metablock> >::min_size()) {
mgerdin@5699 864 return_block(new_block + word_size, unused);
mgerdin@5699 865 }
mgerdin@5699 866
mgerdin@5699 867 return new_block;
coleenp@4037 868 }
coleenp@4037 869
coleenp@4037 870 void BlockFreelist::print_on(outputStream* st) const {
coleenp@4037 871 if (dictionary() == NULL) {
coleenp@4037 872 return;
coleenp@4037 873 }
coleenp@4037 874 dictionary()->print_free_lists(st);
coleenp@4037 875 }
coleenp@4037 876
coleenp@4037 877 // VirtualSpaceNode methods
coleenp@4037 878
coleenp@4037 879 VirtualSpaceNode::~VirtualSpaceNode() {
coleenp@4037 880 _rs.release();
jmasa@5007 881 #ifdef ASSERT
jmasa@5007 882 size_t word_size = sizeof(*this) / BytesPerWord;
jmasa@5007 883 Copy::fill_to_words((HeapWord*) this, word_size, 0xf1f1f1f1);
jmasa@5007 884 #endif
coleenp@4037 885 }
coleenp@4037 886
coleenp@4037 887 size_t VirtualSpaceNode::used_words_in_vs() const {
coleenp@4037 888 return pointer_delta(top(), bottom(), sizeof(MetaWord));
coleenp@4037 889 }
coleenp@4037 890
coleenp@4037 891 // Space committed in the VirtualSpace
coleenp@4037 892 size_t VirtualSpaceNode::capacity_words_in_vs() const {
coleenp@4037 893 return pointer_delta(end(), bottom(), sizeof(MetaWord));
coleenp@4037 894 }
coleenp@4037 895
jmasa@5015 896 size_t VirtualSpaceNode::free_words_in_vs() const {
jmasa@5015 897 return pointer_delta(end(), top(), sizeof(MetaWord));
jmasa@5015 898 }
coleenp@4037 899
coleenp@4037 900 // Allocates the chunk from the virtual space only.
coleenp@4037 901 // This interface is also used internally for debugging. Not all
coleenp@4037 902 // chunks removed here are necessarily used for allocation.
coleenp@4037 903 Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) {
coleenp@4037 904 // Bottom of the new chunk
coleenp@4037 905 MetaWord* chunk_limit = top();
coleenp@4037 906 assert(chunk_limit != NULL, "Not safe to call this method");
coleenp@4037 907
stefank@5863 908 // The virtual spaces are always expanded by the
stefank@5863 909 // commit granularity to enforce the following condition.
stefank@5863 910 // Without this the is_available check will not work correctly.
stefank@5863 911 assert(_virtual_space.committed_size() == _virtual_space.actual_committed_size(),
stefank@5863 912 "The committed memory doesn't match the expanded memory.");
stefank@5863 913
coleenp@4037 914 if (!is_available(chunk_word_size)) {
coleenp@4037 915 if (TraceMetadataChunkAllocation) {
stefank@5708 916 gclog_or_tty->print("VirtualSpaceNode::take_from_committed() not available %d words ", chunk_word_size);
coleenp@4037 917 // Dump some information about the virtual space that is nearly full
stefank@5708 918 print_on(gclog_or_tty);
coleenp@4037 919 }
coleenp@4037 920 return NULL;
coleenp@4037 921 }
coleenp@4037 922
coleenp@4037 923 // Take the space (bump top on the current virtual space).
coleenp@4037 924 inc_top(chunk_word_size);
coleenp@4037 925
jmasa@5007 926 // Initialize the chunk
jmasa@5007 927 Metachunk* result = ::new (chunk_limit) Metachunk(chunk_word_size, this);
coleenp@4037 928 return result;
coleenp@4037 929 }
coleenp@4037 930
coleenp@4037 931
coleenp@4037 932 // Expand the virtual space (commit more of the reserved space)
stefank@5863 933 bool VirtualSpaceNode::expand_by(size_t min_words, size_t preferred_words) {
stefank@5863 934 size_t min_bytes = min_words * BytesPerWord;
stefank@5863 935 size_t preferred_bytes = preferred_words * BytesPerWord;
stefank@5863 936
stefank@5863 937 size_t uncommitted = virtual_space()->reserved_size() - virtual_space()->actual_committed_size();
stefank@5863 938
stefank@5863 939 if (uncommitted < min_bytes) {
stefank@5863 940 return false;
coleenp@4037 941 }
stefank@5863 942
stefank@5863 943 size_t commit = MIN2(preferred_bytes, uncommitted);
stefank@5863 944 bool result = virtual_space()->expand_by(commit, false);
stefank@5863 945
stefank@5863 946 assert(result, "Failed to commit memory");
stefank@5863 947
coleenp@4037 948 return result;
coleenp@4037 949 }
coleenp@4037 950
coleenp@4037 951 Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) {
coleenp@4037 952 assert_lock_strong(SpaceManager::expand_lock());
jmasa@5007 953 Metachunk* result = take_from_committed(chunk_word_size);
jmasa@5007 954 if (result != NULL) {
jmasa@5007 955 inc_container_count();
jmasa@5007 956 }
jmasa@5007 957 return result;
coleenp@4037 958 }
coleenp@4037 959
coleenp@4037 960 bool VirtualSpaceNode::initialize() {
coleenp@4037 961
coleenp@4037 962 if (!_rs.is_reserved()) {
coleenp@4037 963 return false;
coleenp@4037 964 }
coleenp@4037 965
stefank@5863 966 // These are necessary restriction to make sure that the virtual space always
stefank@5863 967 // grows in steps of Metaspace::commit_alignment(). If both base and size are
stefank@5863 968 // aligned only the middle alignment of the VirtualSpace is used.
stefank@5863 969 assert_is_ptr_aligned(_rs.base(), Metaspace::commit_alignment());
stefank@5863 970 assert_is_size_aligned(_rs.size(), Metaspace::commit_alignment());
stefank@5863 971
stefank@5863 972 // ReservedSpaces marked as special will have the entire memory
stefank@5863 973 // pre-committed. Setting a committed size will make sure that
stefank@5863 974 // committed_size and actual_committed_size agrees.
stefank@5863 975 size_t pre_committed_size = _rs.special() ? _rs.size() : 0;
stefank@5863 976
stefank@5863 977 bool result = virtual_space()->initialize_with_granularity(_rs, pre_committed_size,
stefank@5863 978 Metaspace::commit_alignment());
coleenp@4037 979 if (result) {
stefank@5863 980 assert(virtual_space()->committed_size() == virtual_space()->actual_committed_size(),
stefank@5863 981 "Checking that the pre-committed memory was registered by the VirtualSpace");
stefank@5863 982
coleenp@4037 983 set_top((MetaWord*)virtual_space()->low());
coleenp@4037 984 set_reserved(MemRegion((HeapWord*)_rs.base(),
coleenp@4037 985 (HeapWord*)(_rs.base() + _rs.size())));
coleenp@4038 986
coleenp@4038 987 assert(reserved()->start() == (HeapWord*) _rs.base(),
coleenp@4038 988 err_msg("Reserved start was not set properly " PTR_FORMAT
coleenp@4038 989 " != " PTR_FORMAT, reserved()->start(), _rs.base()));
coleenp@4038 990 assert(reserved()->word_size() == _rs.size() / BytesPerWord,
coleenp@4038 991 err_msg("Reserved size was not set properly " SIZE_FORMAT
coleenp@4038 992 " != " SIZE_FORMAT, reserved()->word_size(),
coleenp@4038 993 _rs.size() / BytesPerWord));
coleenp@4037 994 }
coleenp@4037 995
coleenp@4037 996 return result;
coleenp@4037 997 }
coleenp@4037 998
coleenp@4037 999 void VirtualSpaceNode::print_on(outputStream* st) const {
coleenp@4037 1000 size_t used = used_words_in_vs();
coleenp@4037 1001 size_t capacity = capacity_words_in_vs();
coleenp@4037 1002 VirtualSpace* vs = virtual_space();
coleenp@4037 1003 st->print_cr(" space @ " PTR_FORMAT " " SIZE_FORMAT "K, %3d%% used "
coleenp@4037 1004 "[" PTR_FORMAT ", " PTR_FORMAT ", "
coleenp@4037 1005 PTR_FORMAT ", " PTR_FORMAT ")",
jmasa@4382 1006 vs, capacity / K,
jmasa@4382 1007 capacity == 0 ? 0 : used * 100 / capacity,
coleenp@4037 1008 bottom(), top(), end(),
coleenp@4037 1009 vs->high_boundary());
coleenp@4037 1010 }
coleenp@4037 1011
coleenp@4304 1012 #ifdef ASSERT
coleenp@4037 1013 void VirtualSpaceNode::mangle() {
coleenp@4037 1014 size_t word_size = capacity_words_in_vs();
coleenp@4037 1015 Copy::fill_to_words((HeapWord*) low(), word_size, 0xf1f1f1f1);
coleenp@4037 1016 }
coleenp@4304 1017 #endif // ASSERT
coleenp@4037 1018
coleenp@4037 1019 // VirtualSpaceList methods
coleenp@4037 1020 // Space allocated from the VirtualSpace
coleenp@4037 1021
coleenp@4037 1022 VirtualSpaceList::~VirtualSpaceList() {
coleenp@4037 1023 VirtualSpaceListIterator iter(virtual_space_list());
coleenp@4037 1024 while (iter.repeat()) {
coleenp@4037 1025 VirtualSpaceNode* vsl = iter.get_next();
coleenp@4037 1026 delete vsl;
coleenp@4037 1027 }
coleenp@4037 1028 }
coleenp@4037 1029
stefank@5704 1030 void VirtualSpaceList::inc_reserved_words(size_t v) {
jmasa@5007 1031 assert_lock_strong(SpaceManager::expand_lock());
stefank@5704 1032 _reserved_words = _reserved_words + v;
jmasa@5007 1033 }
stefank@5704 1034 void VirtualSpaceList::dec_reserved_words(size_t v) {
jmasa@5007 1035 assert_lock_strong(SpaceManager::expand_lock());
stefank@5704 1036 _reserved_words = _reserved_words - v;
stefank@5704 1037 }
stefank@5704 1038
stefank@5863 1039 #define assert_committed_below_limit() \
stefank@5863 1040 assert(MetaspaceAux::committed_bytes() <= MaxMetaspaceSize, \
stefank@5863 1041 err_msg("Too much committed memory. Committed: " SIZE_FORMAT \
stefank@5863 1042 " limit (MaxMetaspaceSize): " SIZE_FORMAT, \
stefank@5863 1043 MetaspaceAux::committed_bytes(), MaxMetaspaceSize));
stefank@5863 1044
stefank@5704 1045 void VirtualSpaceList::inc_committed_words(size_t v) {
stefank@5704 1046 assert_lock_strong(SpaceManager::expand_lock());
stefank@5704 1047 _committed_words = _committed_words + v;
stefank@5863 1048
stefank@5863 1049 assert_committed_below_limit();
stefank@5704 1050 }
stefank@5704 1051 void VirtualSpaceList::dec_committed_words(size_t v) {
stefank@5704 1052 assert_lock_strong(SpaceManager::expand_lock());
stefank@5704 1053 _committed_words = _committed_words - v;
stefank@5863 1054
stefank@5863 1055 assert_committed_below_limit();
jmasa@5007 1056 }
jmasa@5007 1057
jmasa@5007 1058 void VirtualSpaceList::inc_virtual_space_count() {
jmasa@5007 1059 assert_lock_strong(SpaceManager::expand_lock());
jmasa@5007 1060 _virtual_space_count++;
jmasa@5007 1061 }
jmasa@5007 1062 void VirtualSpaceList::dec_virtual_space_count() {
jmasa@5007 1063 assert_lock_strong(SpaceManager::expand_lock());
jmasa@5007 1064 _virtual_space_count--;
jmasa@5007 1065 }
jmasa@5007 1066
jmasa@5007 1067 void ChunkManager::remove_chunk(Metachunk* chunk) {
jmasa@5007 1068 size_t word_size = chunk->word_size();
jmasa@5007 1069 ChunkIndex index = list_index(word_size);
jmasa@5007 1070 if (index != HumongousIndex) {
jmasa@5007 1071 free_chunks(index)->remove_chunk(chunk);
jmasa@5007 1072 } else {
jmasa@5007 1073 humongous_dictionary()->remove_chunk(chunk);
jmasa@5007 1074 }
jmasa@5007 1075
jmasa@5007 1076 // Chunk is being removed from the chunks free list.
stefank@5941 1077 dec_free_chunks_total(chunk->word_size());
jmasa@5007 1078 }
jmasa@5007 1079
jmasa@5007 1080 // Walk the list of VirtualSpaceNodes and delete
jmasa@5007 1081 // nodes with a 0 container_count. Remove Metachunks in
jmasa@5007 1082 // the node from their respective freelists.
stefank@5771 1083 void VirtualSpaceList::purge(ChunkManager* chunk_manager) {
coleenp@6678 1084 assert(SafepointSynchronize::is_at_safepoint(), "must be called at safepoint for contains to work");
jmasa@5007 1085 assert_lock_strong(SpaceManager::expand_lock());
jmasa@5007 1086 // Don't use a VirtualSpaceListIterator because this
jmasa@5007 1087 // list is being changed and a straightforward use of an iterator is not safe.
jmasa@5007 1088 VirtualSpaceNode* purged_vsl = NULL;
jmasa@5007 1089 VirtualSpaceNode* prev_vsl = virtual_space_list();
jmasa@5007 1090 VirtualSpaceNode* next_vsl = prev_vsl;
jmasa@5007 1091 while (next_vsl != NULL) {
jmasa@5007 1092 VirtualSpaceNode* vsl = next_vsl;
jmasa@5007 1093 next_vsl = vsl->next();
jmasa@5007 1094 // Don't free the current virtual space since it will likely
jmasa@5007 1095 // be needed soon.
jmasa@5007 1096 if (vsl->container_count() == 0 && vsl != current_virtual_space()) {
jmasa@5007 1097 // Unlink it from the list
jmasa@5007 1098 if (prev_vsl == vsl) {
stefank@5863 1099 // This is the case of the current node being the first node.
stefank@5863 1100 assert(vsl == virtual_space_list(), "Expected to be the first node");
jmasa@5007 1101 set_virtual_space_list(vsl->next());
jmasa@5007 1102 } else {
jmasa@5007 1103 prev_vsl->set_next(vsl->next());
jmasa@5007 1104 }
jmasa@5007 1105
stefank@5771 1106 vsl->purge(chunk_manager);
stefank@5704 1107 dec_reserved_words(vsl->reserved_words());
stefank@5704 1108 dec_committed_words(vsl->committed_words());
jmasa@5007 1109 dec_virtual_space_count();
jmasa@5007 1110 purged_vsl = vsl;
jmasa@5007 1111 delete vsl;
jmasa@5007 1112 } else {
jmasa@5007 1113 prev_vsl = vsl;
jmasa@5007 1114 }
jmasa@5007 1115 }
jmasa@5007 1116 #ifdef ASSERT
jmasa@5007 1117 if (purged_vsl != NULL) {
coleenp@6678 1118 // List should be stable enough to use an iterator here.
coleenp@6678 1119 VirtualSpaceListIterator iter(virtual_space_list());
jmasa@5007 1120 while (iter.repeat()) {
jmasa@5007 1121 VirtualSpaceNode* vsl = iter.get_next();
jmasa@5007 1122 assert(vsl != purged_vsl, "Purge of vsl failed");
jmasa@5007 1123 }
jmasa@5007 1124 }
jmasa@5007 1125 #endif
jmasa@5007 1126 }
jmasa@5007 1127
coleenp@6678 1128
coleenp@6678 1129 // This function looks at the mmap regions in the metaspace without locking.
coleenp@6678 1130 // The chunks are added with store ordering and not deleted except for at
coleenp@6678 1131 // unloading time during a safepoint.
coleenp@6678 1132 bool VirtualSpaceList::contains(const void* ptr) {
coleenp@6678 1133 // List should be stable enough to use an iterator here because removing virtual
coleenp@6678 1134 // space nodes is only allowed at a safepoint.
coleenp@6678 1135 VirtualSpaceListIterator iter(virtual_space_list());
coleenp@6678 1136 while (iter.repeat()) {
coleenp@6678 1137 VirtualSpaceNode* vsn = iter.get_next();
coleenp@6678 1138 if (vsn->contains(ptr)) {
coleenp@6678 1139 return true;
coleenp@6678 1140 }
coleenp@6678 1141 }
coleenp@6678 1142 return false;
coleenp@6678 1143 }
coleenp@6678 1144
mgerdin@6004 1145 void VirtualSpaceList::retire_current_virtual_space() {
mgerdin@6004 1146 assert_lock_strong(SpaceManager::expand_lock());
mgerdin@6004 1147
mgerdin@6004 1148 VirtualSpaceNode* vsn = current_virtual_space();
mgerdin@6004 1149
mgerdin@6004 1150 ChunkManager* cm = is_class() ? Metaspace::chunk_manager_class() :
mgerdin@6004 1151 Metaspace::chunk_manager_metadata();
mgerdin@6004 1152
mgerdin@6004 1153 vsn->retire(cm);
mgerdin@6004 1154 }
mgerdin@6004 1155
mgerdin@6004 1156 void VirtualSpaceNode::retire(ChunkManager* chunk_manager) {
mgerdin@6004 1157 for (int i = (int)MediumIndex; i >= (int)ZeroIndex; --i) {
mgerdin@6004 1158 ChunkIndex index = (ChunkIndex)i;
mgerdin@6004 1159 size_t chunk_size = chunk_manager->free_chunks(index)->size();
mgerdin@6004 1160
mgerdin@6004 1161 while (free_words_in_vs() >= chunk_size) {
mgerdin@6004 1162 DEBUG_ONLY(verify_container_count();)
mgerdin@6004 1163 Metachunk* chunk = get_chunk_vs(chunk_size);
mgerdin@6004 1164 assert(chunk != NULL, "allocation should have been successful");
mgerdin@6004 1165
mgerdin@6004 1166 chunk_manager->return_chunks(index, chunk);
mgerdin@6004 1167 chunk_manager->inc_free_chunks_total(chunk_size);
mgerdin@6004 1168 DEBUG_ONLY(verify_container_count();)
mgerdin@6004 1169 }
mgerdin@6004 1170 }
mgerdin@6004 1171 assert(free_words_in_vs() == 0, "should be empty now");
mgerdin@6004 1172 }
mgerdin@6004 1173
stefank@5863 1174 VirtualSpaceList::VirtualSpaceList(size_t word_size) :
coleenp@4037 1175 _is_class(false),
coleenp@4037 1176 _virtual_space_list(NULL),
coleenp@4037 1177 _current_virtual_space(NULL),
stefank@5704 1178 _reserved_words(0),
stefank@5704 1179 _committed_words(0),
coleenp@4037 1180 _virtual_space_count(0) {
coleenp@4037 1181 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@4037 1182 Mutex::_no_safepoint_check_flag);
stefank@5863 1183 create_new_virtual_space(word_size);
coleenp@4037 1184 }
coleenp@4037 1185
coleenp@4037 1186 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
coleenp@4037 1187 _is_class(true),
coleenp@4037 1188 _virtual_space_list(NULL),
coleenp@4037 1189 _current_virtual_space(NULL),
stefank@5704 1190 _reserved_words(0),
stefank@5704 1191 _committed_words(0),
coleenp@4037 1192 _virtual_space_count(0) {
coleenp@4037 1193 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@4037 1194 Mutex::_no_safepoint_check_flag);
coleenp@4037 1195 VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs);
coleenp@4037 1196 bool succeeded = class_entry->initialize();
stefank@5863 1197 if (succeeded) {
stefank@5863 1198 link_vs(class_entry);
stefank@5863 1199 }
coleenp@4037 1200 }
coleenp@4037 1201
jmasa@5015 1202 size_t VirtualSpaceList::free_bytes() {
jmasa@5015 1203 return virtual_space_list()->free_words_in_vs() * BytesPerWord;
jmasa@5015 1204 }
jmasa@5015 1205
coleenp@4037 1206 // Allocate another meta virtual space and add it to the list.
stefank@5863 1207 bool VirtualSpaceList::create_new_virtual_space(size_t vs_word_size) {
coleenp@4037 1208 assert_lock_strong(SpaceManager::expand_lock());
stefank@5863 1209
stefank@5863 1210 if (is_class()) {
stefank@5863 1211 assert(false, "We currently don't support more than one VirtualSpace for"
stefank@5863 1212 " the compressed class space. The initialization of the"
stefank@5863 1213 " CCS uses another code path and should not hit this path.");
coleenp@4037 1214 return false;
coleenp@4037 1215 }
stefank@5863 1216
stefank@5863 1217 if (vs_word_size == 0) {
stefank@5863 1218 assert(false, "vs_word_size should always be at least _reserve_alignment large.");
stefank@5863 1219 return false;
stefank@5863 1220 }
stefank@5863 1221
coleenp@4037 1222 // Reserve the space
coleenp@4037 1223 size_t vs_byte_size = vs_word_size * BytesPerWord;
stefank@5863 1224 assert_is_size_aligned(vs_byte_size, Metaspace::reserve_alignment());
coleenp@4037 1225
coleenp@4037 1226 // Allocate the meta virtual space and initialize it.
coleenp@4037 1227 VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size);
coleenp@4037 1228 if (!new_entry->initialize()) {
coleenp@4037 1229 delete new_entry;
coleenp@4037 1230 return false;
coleenp@4037 1231 } else {
stefank@5863 1232 assert(new_entry->reserved_words() == vs_word_size,
stefank@5863 1233 "Reserved memory size differs from requested memory size");
coleenp@6678 1234 // ensure lock-free iteration sees fully initialized node
coleenp@6678 1235 OrderAccess::storestore();
stefank@5704 1236 link_vs(new_entry);
coleenp@4037 1237 return true;
coleenp@4037 1238 }
coleenp@4037 1239 }
coleenp@4037 1240
stefank@5704 1241 void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry) {
coleenp@4037 1242 if (virtual_space_list() == NULL) {
coleenp@4037 1243 set_virtual_space_list(new_entry);
coleenp@4037 1244 } else {
coleenp@4037 1245 current_virtual_space()->set_next(new_entry);
coleenp@4037 1246 }
coleenp@4037 1247 set_current_virtual_space(new_entry);
stefank@5704 1248 inc_reserved_words(new_entry->reserved_words());
stefank@5704 1249 inc_committed_words(new_entry->committed_words());
coleenp@4037 1250 inc_virtual_space_count();
coleenp@4037 1251 #ifdef ASSERT
coleenp@4037 1252 new_entry->mangle();
coleenp@4037 1253 #endif
coleenp@4037 1254 if (TraceMetavirtualspaceAllocation && Verbose) {
coleenp@4037 1255 VirtualSpaceNode* vsl = current_virtual_space();
stefank@5708 1256 vsl->print_on(gclog_or_tty);
coleenp@4037 1257 }
coleenp@4037 1258 }
coleenp@4037 1259
stefank@5863 1260 bool VirtualSpaceList::expand_node_by(VirtualSpaceNode* node,
stefank@5863 1261 size_t min_words,
stefank@5863 1262 size_t preferred_words) {
stefank@5704 1263 size_t before = node->committed_words();
stefank@5704 1264
stefank@5863 1265 bool result = node->expand_by(min_words, preferred_words);
stefank@5704 1266
stefank@5704 1267 size_t after = node->committed_words();
stefank@5704 1268
stefank@5704 1269 // after and before can be the same if the memory was pre-committed.
stefank@5863 1270 assert(after >= before, "Inconsistency");
stefank@5704 1271 inc_committed_words(after - before);
stefank@5704 1272
stefank@5704 1273 return result;
stefank@5704 1274 }
stefank@5704 1275
stefank@5863 1276 bool VirtualSpaceList::expand_by(size_t min_words, size_t preferred_words) {
stefank@5863 1277 assert_is_size_aligned(min_words, Metaspace::commit_alignment_words());
stefank@5863 1278 assert_is_size_aligned(preferred_words, Metaspace::commit_alignment_words());
stefank@5863 1279 assert(min_words <= preferred_words, "Invalid arguments");
stefank@5863 1280
stefank@5863 1281 if (!MetaspaceGC::can_expand(min_words, this->is_class())) {
stefank@5863 1282 return false;
stefank@5863 1283 }
stefank@5863 1284
stefank@5863 1285 size_t allowed_expansion_words = MetaspaceGC::allowed_expansion();
stefank@5863 1286 if (allowed_expansion_words < min_words) {
stefank@5863 1287 return false;
stefank@5863 1288 }
stefank@5863 1289
stefank@5863 1290 size_t max_expansion_words = MIN2(preferred_words, allowed_expansion_words);
stefank@5863 1291
stefank@5863 1292 // Commit more memory from the the current virtual space.
stefank@5863 1293 bool vs_expanded = expand_node_by(current_virtual_space(),
stefank@5863 1294 min_words,
stefank@5863 1295 max_expansion_words);
stefank@5863 1296 if (vs_expanded) {
stefank@5863 1297 return true;
stefank@5863 1298 }
mgerdin@6004 1299 retire_current_virtual_space();
stefank@5863 1300
stefank@5863 1301 // Get another virtual space.
stefank@5863 1302 size_t grow_vs_words = MAX2((size_t)VirtualSpaceSize, preferred_words);
stefank@5863 1303 grow_vs_words = align_size_up(grow_vs_words, Metaspace::reserve_alignment_words());
stefank@5863 1304
stefank@5863 1305 if (create_new_virtual_space(grow_vs_words)) {
stefank@5863 1306 if (current_virtual_space()->is_pre_committed()) {
stefank@5863 1307 // The memory was pre-committed, so we are done here.
stefank@5863 1308 assert(min_words <= current_virtual_space()->committed_words(),
stefank@5863 1309 "The new VirtualSpace was pre-committed, so it"
stefank@5863 1310 "should be large enough to fit the alloc request.");
stefank@5863 1311 return true;
stefank@5863 1312 }
stefank@5863 1313
stefank@5863 1314 return expand_node_by(current_virtual_space(),
stefank@5863 1315 min_words,
stefank@5863 1316 max_expansion_words);
stefank@5863 1317 }
stefank@5863 1318
stefank@5863 1319 return false;
stefank@5863 1320 }
stefank@5863 1321
coleenp@4037 1322 Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size,
jmasa@4382 1323 size_t grow_chunks_by_words,
jmasa@4382 1324 size_t medium_chunk_bunch) {
coleenp@4037 1325
stefank@5771 1326 // Allocate a chunk out of the current virtual space.
stefank@5771 1327 Metachunk* next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
coleenp@4037 1328
stefank@5863 1329 if (next != NULL) {
stefank@5863 1330 return next;
coleenp@4037 1331 }
coleenp@4037 1332
stefank@5863 1333 // The expand amount is currently only determined by the requested sizes
stefank@5863 1334 // and not how much committed memory is left in the current virtual space.
stefank@5863 1335
stefank@5863 1336 size_t min_word_size = align_size_up(grow_chunks_by_words, Metaspace::commit_alignment_words());
stefank@5863 1337 size_t preferred_word_size = align_size_up(medium_chunk_bunch, Metaspace::commit_alignment_words());
stefank@5863 1338 if (min_word_size >= preferred_word_size) {
stefank@5863 1339 // Can happen when humongous chunks are allocated.
stefank@5863 1340 preferred_word_size = min_word_size;
stefank@5863 1341 }
stefank@5863 1342
stefank@5863 1343 bool expanded = expand_by(min_word_size, preferred_word_size);
stefank@5863 1344 if (expanded) {
stefank@5863 1345 next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
stefank@5863 1346 assert(next != NULL, "The allocation was expected to succeed after the expansion");
stefank@5863 1347 }
stefank@5863 1348
stefank@5863 1349 return next;
jmasa@4382 1350 }
jmasa@4382 1351
coleenp@4037 1352 void VirtualSpaceList::print_on(outputStream* st) const {
coleenp@4037 1353 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@4037 1354 VirtualSpaceListIterator iter(virtual_space_list());
coleenp@4037 1355 while (iter.repeat()) {
coleenp@4037 1356 VirtualSpaceNode* node = iter.get_next();
coleenp@4037 1357 node->print_on(st);
coleenp@4037 1358 }
coleenp@4037 1359 }
coleenp@4037 1360 }
coleenp@4037 1361
coleenp@4037 1362 // MetaspaceGC methods
coleenp@4037 1363
coleenp@4037 1364 // VM_CollectForMetadataAllocation is the vm operation used to GC.
coleenp@4037 1365 // Within the VM operation after the GC the attempt to allocate the metadata
coleenp@4037 1366 // should succeed. If the GC did not free enough space for the metaspace
coleenp@4037 1367 // allocation, the HWM is increased so that another virtualspace will be
coleenp@4037 1368 // allocated for the metadata. With perm gen the increase in the perm
coleenp@4037 1369 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion. The
coleenp@4037 1370 // metaspace policy uses those as the small and large steps for the HWM.
coleenp@4037 1371 //
coleenp@4037 1372 // After the GC the compute_new_size() for MetaspaceGC is called to
coleenp@4037 1373 // resize the capacity of the metaspaces. The current implementation
jmasa@5015 1374 // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used
coleenp@4037 1375 // to resize the Java heap by some GC's. New flags can be implemented
jmasa@5015 1376 // if really needed. MinMetaspaceFreeRatio is used to calculate how much
coleenp@4037 1377 // free space is desirable in the metaspace capacity to decide how much
jmasa@4581 1378 // to increase the HWM. MaxMetaspaceFreeRatio is used to decide how much
coleenp@4037 1379 // free space is desirable in the metaspace capacity before decreasing
coleenp@4037 1380 // the HWM.
coleenp@4037 1381
coleenp@4037 1382 // Calculate the amount to increase the high water mark (HWM).
coleenp@4037 1383 // Increase by a minimum amount (MinMetaspaceExpansion) so that
coleenp@4037 1384 // another expansion is not requested too soon. If that is not
stefank@5863 1385 // enough to satisfy the allocation, increase by MaxMetaspaceExpansion.
stefank@5863 1386 // If that is still not enough, expand by the size of the allocation
stefank@5863 1387 // plus some.
stefank@5863 1388 size_t MetaspaceGC::delta_capacity_until_GC(size_t bytes) {
stefank@5863 1389 size_t min_delta = MinMetaspaceExpansion;
stefank@5863 1390 size_t max_delta = MaxMetaspaceExpansion;
stefank@5863 1391 size_t delta = align_size_up(bytes, Metaspace::commit_alignment());
stefank@5863 1392
stefank@5863 1393 if (delta <= min_delta) {
stefank@5863 1394 delta = min_delta;
stefank@5863 1395 } else if (delta <= max_delta) {
coleenp@4037 1396 // Don't want to hit the high water mark on the next
coleenp@4037 1397 // allocation so make the delta greater than just enough
coleenp@4037 1398 // for this allocation.
stefank@5863 1399 delta = max_delta;
stefank@5863 1400 } else {
stefank@5863 1401 // This allocation is large but the next ones are probably not
stefank@5863 1402 // so increase by the minimum.
stefank@5863 1403 delta = delta + min_delta;
coleenp@4037 1404 }
stefank@5863 1405
stefank@5863 1406 assert_is_size_aligned(delta, Metaspace::commit_alignment());
stefank@5863 1407
stefank@5863 1408 return delta;
coleenp@4037 1409 }
coleenp@4037 1410
stefank@5863 1411 size_t MetaspaceGC::capacity_until_GC() {
stefank@5863 1412 size_t value = (size_t)OrderAccess::load_ptr_acquire(&_capacity_until_GC);
stefank@5863 1413 assert(value >= MetaspaceSize, "Not initialied properly?");
stefank@5863 1414 return value;
stefank@5863 1415 }
stefank@5863 1416
ehelin@7254 1417 bool MetaspaceGC::inc_capacity_until_GC(size_t v, size_t* new_cap_until_GC, size_t* old_cap_until_GC) {
stefank@5863 1418 assert_is_size_aligned(v, Metaspace::commit_alignment());
stefank@5863 1419
ehelin@7254 1420 size_t capacity_until_GC = (size_t) _capacity_until_GC;
ehelin@7254 1421 size_t new_value = capacity_until_GC + v;
ehelin@7254 1422
ehelin@7254 1423 if (new_value < capacity_until_GC) {
ehelin@7254 1424 // The addition wrapped around, set new_value to aligned max value.
ehelin@7254 1425 new_value = align_size_down(max_uintx, Metaspace::commit_alignment());
ehelin@7254 1426 }
ehelin@7254 1427
ehelin@7254 1428 intptr_t expected = (intptr_t) capacity_until_GC;
ehelin@7254 1429 intptr_t actual = Atomic::cmpxchg_ptr((intptr_t) new_value, &_capacity_until_GC, expected);
ehelin@7254 1430
ehelin@7254 1431 if (expected != actual) {
ehelin@7254 1432 return false;
ehelin@7254 1433 }
ehelin@7254 1434
ehelin@7254 1435 if (new_cap_until_GC != NULL) {
ehelin@7254 1436 *new_cap_until_GC = new_value;
ehelin@7254 1437 }
ehelin@7254 1438 if (old_cap_until_GC != NULL) {
ehelin@7254 1439 *old_cap_until_GC = capacity_until_GC;
ehelin@7254 1440 }
ehelin@7254 1441 return true;
stefank@5863 1442 }
stefank@5863 1443
stefank@5863 1444 size_t MetaspaceGC::dec_capacity_until_GC(size_t v) {
stefank@5863 1445 assert_is_size_aligned(v, Metaspace::commit_alignment());
stefank@5863 1446
stefank@5863 1447 return (size_t)Atomic::add_ptr(-(intptr_t)v, &_capacity_until_GC);
stefank@5863 1448 }
stefank@5863 1449
ehelin@6722 1450 void MetaspaceGC::initialize() {
ehelin@6722 1451 // Set the high-water mark to MaxMetapaceSize during VM initializaton since
ehelin@6722 1452 // we can't do a GC during initialization.
ehelin@6722 1453 _capacity_until_GC = MaxMetaspaceSize;
ehelin@6722 1454 }
ehelin@6722 1455
ehelin@6722 1456 void MetaspaceGC::post_initialize() {
ehelin@6722 1457 // Reset the high-water mark once the VM initialization is done.
ehelin@6722 1458 _capacity_until_GC = MAX2(MetaspaceAux::committed_bytes(), MetaspaceSize);
ehelin@6722 1459 }
ehelin@6722 1460
stefank@5863 1461 bool MetaspaceGC::can_expand(size_t word_size, bool is_class) {
stefank@5863 1462 // Check if the compressed class space is full.
stefank@5863 1463 if (is_class && Metaspace::using_class_space()) {
stefank@5863 1464 size_t class_committed = MetaspaceAux::committed_bytes(Metaspace::ClassType);
stefank@5863 1465 if (class_committed + word_size * BytesPerWord > CompressedClassSpaceSize) {
coleenp@5337 1466 return false;
coleenp@5337 1467 }
mgerdin@4790 1468 }
coleenp@4037 1469
stefank@5863 1470 // Check if the user has imposed a limit on the metaspace memory.
stefank@5863 1471 size_t committed_bytes = MetaspaceAux::committed_bytes();
stefank@5863 1472 if (committed_bytes + word_size * BytesPerWord > MaxMetaspaceSize) {
stefank@5863 1473 return false;
coleenp@4037 1474 }
coleenp@4037 1475
stefank@5863 1476 return true;
stefank@5863 1477 }
stefank@5863 1478
stefank@5863 1479 size_t MetaspaceGC::allowed_expansion() {
stefank@5863 1480 size_t committed_bytes = MetaspaceAux::committed_bytes();
ehelin@6722 1481 size_t capacity_until_gc = capacity_until_GC();
ehelin@6722 1482
ehelin@6722 1483 assert(capacity_until_gc >= committed_bytes,
ehelin@6722 1484 err_msg("capacity_until_gc: " SIZE_FORMAT " < committed_bytes: " SIZE_FORMAT,
ehelin@6722 1485 capacity_until_gc, committed_bytes));
stefank@5863 1486
stefank@5863 1487 size_t left_until_max = MaxMetaspaceSize - committed_bytes;
stefank@5863 1488 size_t left_until_GC = capacity_until_gc - committed_bytes;
stefank@5863 1489 size_t left_to_commit = MIN2(left_until_GC, left_until_max);
stefank@5863 1490
stefank@5863 1491 return left_to_commit / BytesPerWord;
coleenp@4037 1492 }
coleenp@4037 1493
coleenp@4037 1494 void MetaspaceGC::compute_new_size() {
coleenp@4037 1495 assert(_shrink_factor <= 100, "invalid shrink factor");
coleenp@4037 1496 uint current_shrink_factor = _shrink_factor;
coleenp@4037 1497 _shrink_factor = 0;
coleenp@4037 1498
ehelin@6722 1499 // Using committed_bytes() for used_after_gc is an overestimation, since the
ehelin@6722 1500 // chunk free lists are included in committed_bytes() and the memory in an
ehelin@6722 1501 // un-fragmented chunk free list is available for future allocations.
ehelin@6722 1502 // However, if the chunk free lists becomes fragmented, then the memory may
ehelin@6722 1503 // not be available for future allocations and the memory is therefore "in use".
ehelin@6722 1504 // Including the chunk free lists in the definition of "in use" is therefore
ehelin@6722 1505 // necessary. Not including the chunk free lists can cause capacity_until_GC to
ehelin@6722 1506 // shrink below committed_bytes() and this has caused serious bugs in the past.
ehelin@6722 1507 const size_t used_after_gc = MetaspaceAux::committed_bytes();
jmasa@5015 1508 const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC();
coleenp@4037 1509
jmasa@4581 1510 const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0;
coleenp@4037 1511 const double maximum_used_percentage = 1.0 - minimum_free_percentage;
coleenp@4037 1512
coleenp@4037 1513 const double min_tmp = used_after_gc / maximum_used_percentage;
coleenp@4037 1514 size_t minimum_desired_capacity =
coleenp@4037 1515 (size_t)MIN2(min_tmp, double(max_uintx));
coleenp@4037 1516 // Don't shrink less than the initial generation size
coleenp@4037 1517 minimum_desired_capacity = MAX2(minimum_desired_capacity,
coleenp@4037 1518 MetaspaceSize);
coleenp@4037 1519
coleenp@4037 1520 if (PrintGCDetails && Verbose) {
coleenp@4037 1521 gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: ");
coleenp@4037 1522 gclog_or_tty->print_cr(" "
coleenp@4037 1523 " minimum_free_percentage: %6.2f"
coleenp@4037 1524 " maximum_used_percentage: %6.2f",
coleenp@4037 1525 minimum_free_percentage,
coleenp@4037 1526 maximum_used_percentage);
coleenp@4037 1527 gclog_or_tty->print_cr(" "
jmasa@5015 1528 " used_after_gc : %6.1fKB",
jmasa@5015 1529 used_after_gc / (double) K);
coleenp@4037 1530 }
coleenp@4037 1531
coleenp@4037 1532
jmasa@5015 1533 size_t shrink_bytes = 0;
coleenp@4037 1534 if (capacity_until_GC < minimum_desired_capacity) {
coleenp@4037 1535 // If we have less capacity below the metaspace HWM, then
coleenp@4037 1536 // increment the HWM.
coleenp@4037 1537 size_t expand_bytes = minimum_desired_capacity - capacity_until_GC;
stefank@5863 1538 expand_bytes = align_size_up(expand_bytes, Metaspace::commit_alignment());
coleenp@4037 1539 // Don't expand unless it's significant
coleenp@4037 1540 if (expand_bytes >= MinMetaspaceExpansion) {
ehelin@7254 1541 size_t new_capacity_until_GC = 0;
ehelin@7254 1542 bool succeeded = MetaspaceGC::inc_capacity_until_GC(expand_bytes, &new_capacity_until_GC);
ehelin@7254 1543 assert(succeeded, "Should always succesfully increment HWM when at safepoint");
ehelin@7254 1544
ehelin@6417 1545 Metaspace::tracer()->report_gc_threshold(capacity_until_GC,
ehelin@6417 1546 new_capacity_until_GC,
ehelin@6417 1547 MetaspaceGCThresholdUpdater::ComputeNewSize);
ehelin@6417 1548 if (PrintGCDetails && Verbose) {
ehelin@6417 1549 gclog_or_tty->print_cr(" expanding:"
ehelin@6417 1550 " minimum_desired_capacity: %6.1fKB"
ehelin@6417 1551 " expand_bytes: %6.1fKB"
ehelin@6417 1552 " MinMetaspaceExpansion: %6.1fKB"
ehelin@6417 1553 " new metaspace HWM: %6.1fKB",
ehelin@6417 1554 minimum_desired_capacity / (double) K,
ehelin@6417 1555 expand_bytes / (double) K,
ehelin@6417 1556 MinMetaspaceExpansion / (double) K,
ehelin@6417 1557 new_capacity_until_GC / (double) K);
ehelin@6417 1558 }
coleenp@4037 1559 }
coleenp@4037 1560 return;
coleenp@4037 1561 }
coleenp@4037 1562
coleenp@4037 1563 // No expansion, now see if we want to shrink
coleenp@4037 1564 // We would never want to shrink more than this
jmasa@5015 1565 size_t max_shrink_bytes = capacity_until_GC - minimum_desired_capacity;
jmasa@5015 1566 assert(max_shrink_bytes >= 0, err_msg("max_shrink_bytes " SIZE_FORMAT,
jmasa@5015 1567 max_shrink_bytes));
coleenp@4037 1568
coleenp@4037 1569 // Should shrinking be considered?
jmasa@4581 1570 if (MaxMetaspaceFreeRatio < 100) {
jmasa@4581 1571 const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0;
coleenp@4037 1572 const double minimum_used_percentage = 1.0 - maximum_free_percentage;
coleenp@4037 1573 const double max_tmp = used_after_gc / minimum_used_percentage;
coleenp@4037 1574 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
coleenp@4037 1575 maximum_desired_capacity = MAX2(maximum_desired_capacity,
coleenp@4037 1576 MetaspaceSize);
jmasa@5015 1577 if (PrintGCDetails && Verbose) {
coleenp@4037 1578 gclog_or_tty->print_cr(" "
coleenp@4037 1579 " maximum_free_percentage: %6.2f"
coleenp@4037 1580 " minimum_used_percentage: %6.2f",
coleenp@4037 1581 maximum_free_percentage,
coleenp@4037 1582 minimum_used_percentage);
coleenp@4037 1583 gclog_or_tty->print_cr(" "
jmasa@5015 1584 " minimum_desired_capacity: %6.1fKB"
jmasa@5015 1585 " maximum_desired_capacity: %6.1fKB",
coleenp@4037 1586 minimum_desired_capacity / (double) K,
coleenp@4037 1587 maximum_desired_capacity / (double) K);
coleenp@4037 1588 }
coleenp@4037 1589
coleenp@4037 1590 assert(minimum_desired_capacity <= maximum_desired_capacity,
coleenp@4037 1591 "sanity check");
coleenp@4037 1592
coleenp@4037 1593 if (capacity_until_GC > maximum_desired_capacity) {
coleenp@4037 1594 // Capacity too large, compute shrinking size
jmasa@5015 1595 shrink_bytes = capacity_until_GC - maximum_desired_capacity;
coleenp@4037 1596 // We don't want shrink all the way back to initSize if people call
coleenp@4037 1597 // System.gc(), because some programs do that between "phases" and then
coleenp@4037 1598 // we'd just have to grow the heap up again for the next phase. So we
coleenp@4037 1599 // damp the shrinking: 0% on the first call, 10% on the second call, 40%
coleenp@4037 1600 // on the third call, and 100% by the fourth call. But if we recompute
coleenp@4037 1601 // size without shrinking, it goes back to 0%.
jmasa@5015 1602 shrink_bytes = shrink_bytes / 100 * current_shrink_factor;
stefank@5863 1603
stefank@5863 1604 shrink_bytes = align_size_down(shrink_bytes, Metaspace::commit_alignment());
stefank@5863 1605
jmasa@5015 1606 assert(shrink_bytes <= max_shrink_bytes,
coleenp@4037 1607 err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT,
jmasa@5015 1608 shrink_bytes, max_shrink_bytes));
coleenp@4037 1609 if (current_shrink_factor == 0) {
coleenp@4037 1610 _shrink_factor = 10;
coleenp@4037 1611 } else {
coleenp@4037 1612 _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100);
coleenp@4037 1613 }
coleenp@4037 1614 if (PrintGCDetails && Verbose) {
coleenp@4037 1615 gclog_or_tty->print_cr(" "
coleenp@4037 1616 " shrinking:"
coleenp@4037 1617 " initSize: %.1fK"
coleenp@4037 1618 " maximum_desired_capacity: %.1fK",
coleenp@4037 1619 MetaspaceSize / (double) K,
coleenp@4037 1620 maximum_desired_capacity / (double) K);
coleenp@4037 1621 gclog_or_tty->print_cr(" "
jmasa@5015 1622 " shrink_bytes: %.1fK"
coleenp@4037 1623 " current_shrink_factor: %d"
coleenp@4037 1624 " new shrink factor: %d"
coleenp@4037 1625 " MinMetaspaceExpansion: %.1fK",
jmasa@5015 1626 shrink_bytes / (double) K,
coleenp@4037 1627 current_shrink_factor,
coleenp@4037 1628 _shrink_factor,
coleenp@4037 1629 MinMetaspaceExpansion / (double) K);
coleenp@4037 1630 }
coleenp@4037 1631 }
coleenp@4037 1632 }
coleenp@4037 1633
coleenp@4037 1634 // Don't shrink unless it's significant
jmasa@5015 1635 if (shrink_bytes >= MinMetaspaceExpansion &&
jmasa@5015 1636 ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) {
ehelin@6417 1637 size_t new_capacity_until_GC = MetaspaceGC::dec_capacity_until_GC(shrink_bytes);
ehelin@6417 1638 Metaspace::tracer()->report_gc_threshold(capacity_until_GC,
ehelin@6417 1639 new_capacity_until_GC,
ehelin@6417 1640 MetaspaceGCThresholdUpdater::ComputeNewSize);
coleenp@4037 1641 }
coleenp@4037 1642 }
coleenp@4037 1643
coleenp@4037 1644 // Metadebug methods
coleenp@4037 1645
coleenp@4037 1646 void Metadebug::init_allocation_fail_alot_count() {
coleenp@4037 1647 if (MetadataAllocationFailALot) {
coleenp@4037 1648 _allocation_fail_alot_count =
coleenp@4037 1649 1+(long)((double)MetadataAllocationFailALotInterval*os::random()/(max_jint+1.0));
coleenp@4037 1650 }
coleenp@4037 1651 }
coleenp@4037 1652
coleenp@4037 1653 #ifdef ASSERT
coleenp@4037 1654 bool Metadebug::test_metadata_failure() {
coleenp@4037 1655 if (MetadataAllocationFailALot &&
coleenp@4037 1656 Threads::is_vm_complete()) {
coleenp@4037 1657 if (_allocation_fail_alot_count > 0) {
coleenp@4037 1658 _allocation_fail_alot_count--;
coleenp@4037 1659 } else {
coleenp@4037 1660 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@4037 1661 gclog_or_tty->print_cr("Metadata allocation failing for "
coleenp@4037 1662 "MetadataAllocationFailALot");
coleenp@4037 1663 }
coleenp@4037 1664 init_allocation_fail_alot_count();
coleenp@4037 1665 return true;
coleenp@4037 1666 }
coleenp@4037 1667 }
coleenp@4037 1668 return false;
coleenp@4037 1669 }
coleenp@4037 1670 #endif
coleenp@4037 1671
coleenp@4037 1672 // ChunkManager methods
coleenp@4037 1673
ehelin@5703 1674 size_t ChunkManager::free_chunks_total_words() {
coleenp@4037 1675 return _free_chunks_total;
coleenp@4037 1676 }
coleenp@4037 1677
ehelin@5703 1678 size_t ChunkManager::free_chunks_total_bytes() {
ehelin@5703 1679 return free_chunks_total_words() * BytesPerWord;
coleenp@4037 1680 }
coleenp@4037 1681
coleenp@4037 1682 size_t ChunkManager::free_chunks_count() {
coleenp@4037 1683 #ifdef ASSERT
coleenp@4037 1684 if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
coleenp@4037 1685 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@4037 1686 Mutex::_no_safepoint_check_flag);
coleenp@4037 1687 // This lock is only needed in debug because the verification
coleenp@4037 1688 // of the _free_chunks_totals walks the list of free chunks
mgerdin@4264 1689 slow_locked_verify_free_chunks_count();
coleenp@4037 1690 }
coleenp@4037 1691 #endif
mgerdin@4264 1692 return _free_chunks_count;
coleenp@4037 1693 }
coleenp@4037 1694
coleenp@4037 1695 void ChunkManager::locked_verify_free_chunks_total() {
coleenp@4037 1696 assert_lock_strong(SpaceManager::expand_lock());
coleenp@4037 1697 assert(sum_free_chunks() == _free_chunks_total,
coleenp@4037 1698 err_msg("_free_chunks_total " SIZE_FORMAT " is not the"
coleenp@4037 1699 " same as sum " SIZE_FORMAT, _free_chunks_total,
coleenp@4037 1700 sum_free_chunks()));
coleenp@4037 1701 }
coleenp@4037 1702
coleenp@4037 1703 void ChunkManager::verify_free_chunks_total() {
coleenp@4037 1704 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@4037 1705 Mutex::_no_safepoint_check_flag);
coleenp@4037 1706 locked_verify_free_chunks_total();
coleenp@4037 1707 }
coleenp@4037 1708
coleenp@4037 1709 void ChunkManager::locked_verify_free_chunks_count() {
coleenp@4037 1710 assert_lock_strong(SpaceManager::expand_lock());
coleenp@4037 1711 assert(sum_free_chunks_count() == _free_chunks_count,
coleenp@4037 1712 err_msg("_free_chunks_count " SIZE_FORMAT " is not the"
coleenp@4037 1713 " same as sum " SIZE_FORMAT, _free_chunks_count,
coleenp@4037 1714 sum_free_chunks_count()));
coleenp@4037 1715 }
coleenp@4037 1716
coleenp@4037 1717 void ChunkManager::verify_free_chunks_count() {
coleenp@4037 1718 #ifdef ASSERT
coleenp@4037 1719 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@4037 1720 Mutex::_no_safepoint_check_flag);
coleenp@4037 1721 locked_verify_free_chunks_count();
coleenp@4037 1722 #endif
coleenp@4037 1723 }
coleenp@4037 1724
coleenp@4037 1725 void ChunkManager::verify() {
mgerdin@4264 1726 MutexLockerEx cl(SpaceManager::expand_lock(),
mgerdin@4264 1727 Mutex::_no_safepoint_check_flag);
mgerdin@4264 1728 locked_verify();
coleenp@4037 1729 }
coleenp@4037 1730
coleenp@4037 1731 void ChunkManager::locked_verify() {
jmasa@4196 1732 locked_verify_free_chunks_count();
coleenp@4037 1733 locked_verify_free_chunks_total();
coleenp@4037 1734 }
coleenp@4037 1735
coleenp@4037 1736 void ChunkManager::locked_print_free_chunks(outputStream* st) {
coleenp@4037 1737 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4382 1738 st->print_cr("Free chunk total " SIZE_FORMAT " count " SIZE_FORMAT,
coleenp@4037 1739 _free_chunks_total, _free_chunks_count);
coleenp@4037 1740 }
coleenp@4037 1741
coleenp@4037 1742 void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
coleenp@4037 1743 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4382 1744 st->print_cr("Sum free chunk total " SIZE_FORMAT " count " SIZE_FORMAT,
coleenp@4037 1745 sum_free_chunks(), sum_free_chunks_count());
coleenp@4037 1746 }
coleenp@4037 1747 ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
coleenp@4037 1748 return &_free_chunks[index];
coleenp@4037 1749 }
coleenp@4037 1750
coleenp@4037 1751 // These methods that sum the free chunk lists are used in printing
coleenp@4037 1752 // methods that are used in product builds.
coleenp@4037 1753 size_t ChunkManager::sum_free_chunks() {
coleenp@4037 1754 assert_lock_strong(SpaceManager::expand_lock());
coleenp@4037 1755 size_t result = 0;
jmasa@4382 1756 for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
coleenp@4037 1757 ChunkList* list = free_chunks(i);
coleenp@4037 1758
coleenp@4037 1759 if (list == NULL) {
coleenp@4037 1760 continue;
coleenp@4037 1761 }
coleenp@4037 1762
jmasa@4932 1763 result = result + list->count() * list->size();
coleenp@4037 1764 }
jmasa@4196 1765 result = result + humongous_dictionary()->total_size();
coleenp@4037 1766 return result;
coleenp@4037 1767 }
coleenp@4037 1768
coleenp@4037 1769 size_t ChunkManager::sum_free_chunks_count() {
coleenp@4037 1770 assert_lock_strong(SpaceManager::expand_lock());
coleenp@4037 1771 size_t count = 0;
jmasa@4382 1772 for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
coleenp@4037 1773 ChunkList* list = free_chunks(i);
coleenp@4037 1774 if (list == NULL) {
coleenp@4037 1775 continue;
coleenp@4037 1776 }
jmasa@4932 1777 count = count + list->count();
coleenp@4037 1778 }
jmasa@4196 1779 count = count + humongous_dictionary()->total_free_blocks();
coleenp@4037 1780 return count;
coleenp@4037 1781 }
coleenp@4037 1782
coleenp@4037 1783 ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) {
jmasa@4382 1784 ChunkIndex index = list_index(word_size);
jmasa@4382 1785 assert(index < HumongousIndex, "No humongous list");
jmasa@4382 1786 return free_chunks(index);
coleenp@4037 1787 }
coleenp@4037 1788
coleenp@4037 1789 Metachunk* ChunkManager::free_chunks_get(size_t word_size) {
coleenp@4037 1790 assert_lock_strong(SpaceManager::expand_lock());
coleenp@4037 1791
mgerdin@4264 1792 slow_locked_verify();
jmasa@4196 1793
jmasa@4196 1794 Metachunk* chunk = NULL;
jmasa@4382 1795 if (list_index(word_size) != HumongousIndex) {
jmasa@4196 1796 ChunkList* free_list = find_free_chunks_list(word_size);
jmasa@4196 1797 assert(free_list != NULL, "Sanity check");
jmasa@4196 1798
jmasa@4196 1799 chunk = free_list->head();
jmasa@4196 1800
jmasa@4196 1801 if (chunk == NULL) {
jmasa@4196 1802 return NULL;
jmasa@4196 1803 }
jmasa@4196 1804
coleenp@4037 1805 // Remove the chunk as the head of the list.
jmasa@4932 1806 free_list->remove_chunk(chunk);
jmasa@4382 1807
coleenp@4037 1808 if (TraceMetadataChunkAllocation && Verbose) {
stefank@5708 1809 gclog_or_tty->print_cr("ChunkManager::free_chunks_get: free_list "
stefank@5708 1810 PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT,
stefank@5708 1811 free_list, chunk, chunk->word_size());
coleenp@4037 1812 }
coleenp@4037 1813 } else {
jmasa@4196 1814 chunk = humongous_dictionary()->get_chunk(
jmasa@4196 1815 word_size,
jmasa@4196 1816 FreeBlockDictionary<Metachunk>::atLeast);
jmasa@4196 1817
stefank@5863 1818 if (chunk == NULL) {
jmasa@4382 1819 return NULL;
coleenp@4037 1820 }
stefank@5863 1821
stefank@5863 1822 if (TraceMetadataHumongousAllocation) {
stefank@5863 1823 size_t waste = chunk->word_size() - word_size;
stefank@5863 1824 gclog_or_tty->print_cr("Free list allocate humongous chunk size "
stefank@5863 1825 SIZE_FORMAT " for requested size " SIZE_FORMAT
stefank@5863 1826 " waste " SIZE_FORMAT,
stefank@5863 1827 chunk->word_size(), word_size, waste);
stefank@5863 1828 }
coleenp@4037 1829 }
jmasa@4382 1830
stefank@5863 1831 // Chunk is being removed from the chunks free list.
stefank@5941 1832 dec_free_chunks_total(chunk->word_size());
stefank@5863 1833
jmasa@4382 1834 // Remove it from the links to this freelist
jmasa@4382 1835 chunk->set_next(NULL);
jmasa@4382 1836 chunk->set_prev(NULL);
jmasa@5007 1837 #ifdef ASSERT
jmasa@5007 1838 // Chunk is no longer on any freelist. Setting to false make container_count_slow()
jmasa@5007 1839 // work.
stefank@5941 1840 chunk->set_is_tagged_free(false);
jmasa@5007 1841 #endif
stefank@5771 1842 chunk->container()->inc_container_count();
stefank@5771 1843
mgerdin@4264 1844 slow_locked_verify();
coleenp@4037 1845 return chunk;
coleenp@4037 1846 }
coleenp@4037 1847
coleenp@4037 1848 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
coleenp@4037 1849 assert_lock_strong(SpaceManager::expand_lock());
mgerdin@4264 1850 slow_locked_verify();
coleenp@4037 1851
coleenp@4037 1852 // Take from the beginning of the list
coleenp@4037 1853 Metachunk* chunk = free_chunks_get(word_size);
coleenp@4037 1854 if (chunk == NULL) {
coleenp@4037 1855 return NULL;
coleenp@4037 1856 }
coleenp@4037 1857
jmasa@4382 1858 assert((word_size <= chunk->word_size()) ||
jmasa@4382 1859 list_index(chunk->word_size() == HumongousIndex),
jmasa@4382 1860 "Non-humongous variable sized chunk");
coleenp@4037 1861 if (TraceMetadataChunkAllocation) {
jmasa@4382 1862 size_t list_count;
jmasa@4382 1863 if (list_index(word_size) < HumongousIndex) {
jmasa@4382 1864 ChunkList* list = find_free_chunks_list(word_size);
jmasa@4932 1865 list_count = list->count();
jmasa@4382 1866 } else {
jmasa@4382 1867 list_count = humongous_dictionary()->total_count();
jmasa@4382 1868 }
stefank@5708 1869 gclog_or_tty->print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk "
stefank@5708 1870 PTR_FORMAT " size " SIZE_FORMAT " count " SIZE_FORMAT " ",
stefank@5708 1871 this, chunk, chunk->word_size(), list_count);
stefank@5708 1872 locked_print_free_chunks(gclog_or_tty);
coleenp@4037 1873 }
coleenp@4037 1874
coleenp@4037 1875 return chunk;
coleenp@4037 1876 }
coleenp@4037 1877
stefank@5771 1878 void ChunkManager::print_on(outputStream* out) const {
jmasa@4196 1879 if (PrintFLSStatistics != 0) {
stefank@5771 1880 const_cast<ChunkManager *>(this)->humongous_dictionary()->report_statistics();
jmasa@4196 1881 }
jmasa@4196 1882 }
jmasa@4196 1883
coleenp@4037 1884 // SpaceManager methods
coleenp@4037 1885
jmasa@4382 1886 void SpaceManager::get_initial_chunk_sizes(Metaspace::MetaspaceType type,
jmasa@4382 1887 size_t* chunk_word_size,
jmasa@4382 1888 size_t* class_chunk_word_size) {
jmasa@4382 1889 switch (type) {
jmasa@4382 1890 case Metaspace::BootMetaspaceType:
jmasa@4382 1891 *chunk_word_size = Metaspace::first_chunk_word_size();
jmasa@4382 1892 *class_chunk_word_size = Metaspace::first_class_chunk_word_size();
jmasa@4382 1893 break;
jmasa@4382 1894 case Metaspace::ROMetaspaceType:
jmasa@4382 1895 *chunk_word_size = SharedReadOnlySize / wordSize;
jmasa@4382 1896 *class_chunk_word_size = ClassSpecializedChunk;
jmasa@4382 1897 break;
jmasa@4382 1898 case Metaspace::ReadWriteMetaspaceType:
jmasa@4382 1899 *chunk_word_size = SharedReadWriteSize / wordSize;
jmasa@4382 1900 *class_chunk_word_size = ClassSpecializedChunk;
jmasa@4382 1901 break;
jmasa@4382 1902 case Metaspace::AnonymousMetaspaceType:
jmasa@4382 1903 case Metaspace::ReflectionMetaspaceType:
jmasa@4382 1904 *chunk_word_size = SpecializedChunk;
jmasa@4382 1905 *class_chunk_word_size = ClassSpecializedChunk;
jmasa@4382 1906 break;
jmasa@4382 1907 default:
jmasa@4382 1908 *chunk_word_size = SmallChunk;
jmasa@4382 1909 *class_chunk_word_size = ClassSmallChunk;
jmasa@4382 1910 break;
jmasa@4382 1911 }
mikael@4548 1912 assert(*chunk_word_size != 0 && *class_chunk_word_size != 0,
jmasa@4382 1913 err_msg("Initial chunks sizes bad: data " SIZE_FORMAT
jmasa@4382 1914 " class " SIZE_FORMAT,
mikael@4548 1915 *chunk_word_size, *class_chunk_word_size));
jmasa@4382 1916 }
jmasa@4382 1917
coleenp@4037 1918 size_t SpaceManager::sum_free_in_chunks_in_use() const {
coleenp@4037 1919 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 1920 size_t free = 0;
jmasa@4382 1921 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@4037 1922 Metachunk* chunk = chunks_in_use(i);
coleenp@4037 1923 while (chunk != NULL) {
coleenp@4037 1924 free += chunk->free_word_size();
coleenp@4037 1925 chunk = chunk->next();
coleenp@4037 1926 }
coleenp@4037 1927 }
coleenp@4037 1928 return free;
coleenp@4037 1929 }
coleenp@4037 1930
coleenp@4037 1931 size_t SpaceManager::sum_waste_in_chunks_in_use() const {
coleenp@4037 1932 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 1933 size_t result = 0;
jmasa@4382 1934 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@4037 1935 result += sum_waste_in_chunks_in_use(i);
coleenp@4037 1936 }
jmasa@4196 1937
coleenp@4037 1938 return result;
coleenp@4037 1939 }
coleenp@4037 1940
coleenp@4037 1941 size_t SpaceManager::sum_waste_in_chunks_in_use(ChunkIndex index) const {
coleenp@4037 1942 size_t result = 0;
coleenp@4037 1943 Metachunk* chunk = chunks_in_use(index);
coleenp@4037 1944 // Count the free space in all the chunk but not the
coleenp@4037 1945 // current chunk from which allocations are still being done.
coleenp@5337 1946 while (chunk != NULL) {
coleenp@5337 1947 if (chunk != current_chunk()) {
jmasa@4196 1948 result += chunk->free_word_size();
coleenp@4037 1949 }
coleenp@5337 1950 chunk = chunk->next();
coleenp@4037 1951 }
coleenp@4037 1952 return result;
coleenp@4037 1953 }
coleenp@4037 1954
coleenp@4037 1955 size_t SpaceManager::sum_capacity_in_chunks_in_use() const {
jmasa@5015 1956 // For CMS use "allocated_chunks_words()" which does not need the
jmasa@5015 1957 // Metaspace lock. For the other collectors sum over the
jmasa@5015 1958 // lists. Use both methods as a check that "allocated_chunks_words()"
jmasa@5015 1959 // is correct. That is, sum_capacity_in_chunks() is too expensive
jmasa@5015 1960 // to use in the product and allocated_chunks_words() should be used
jmasa@5015 1961 // but allow for checking that allocated_chunks_words() returns the same
jmasa@5015 1962 // value as sum_capacity_in_chunks_in_use() which is the definitive
jmasa@5015 1963 // answer.
jmasa@5015 1964 if (UseConcMarkSweepGC) {
jmasa@5015 1965 return allocated_chunks_words();
jmasa@5015 1966 } else {
jmasa@5015 1967 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
jmasa@5015 1968 size_t sum = 0;
jmasa@5015 1969 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
jmasa@5015 1970 Metachunk* chunk = chunks_in_use(i);
jmasa@5015 1971 while (chunk != NULL) {
stefank@5941 1972 sum += chunk->word_size();
jmasa@5015 1973 chunk = chunk->next();
jmasa@5015 1974 }
coleenp@4037 1975 }
jmasa@5015 1976 return sum;
coleenp@4037 1977 }
coleenp@4037 1978 }
coleenp@4037 1979
coleenp@4037 1980 size_t SpaceManager::sum_count_in_chunks_in_use() {
coleenp@4037 1981 size_t count = 0;
jmasa@4382 1982 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@4037 1983 count = count + sum_count_in_chunks_in_use(i);
coleenp@4037 1984 }
jmasa@4196 1985
coleenp@4037 1986 return count;
coleenp@4037 1987 }
coleenp@4037 1988
coleenp@4037 1989 size_t SpaceManager::sum_count_in_chunks_in_use(ChunkIndex i) {
coleenp@4037 1990 size_t count = 0;
coleenp@4037 1991 Metachunk* chunk = chunks_in_use(i);
coleenp@4037 1992 while (chunk != NULL) {
coleenp@4037 1993 count++;
coleenp@4037 1994 chunk = chunk->next();
coleenp@4037 1995 }
coleenp@4037 1996 return count;
coleenp@4037 1997 }
coleenp@4037 1998
coleenp@4037 1999
coleenp@4037 2000 size_t SpaceManager::sum_used_in_chunks_in_use() const {
coleenp@4037 2001 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 2002 size_t used = 0;
jmasa@4382 2003 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@4037 2004 Metachunk* chunk = chunks_in_use(i);
coleenp@4037 2005 while (chunk != NULL) {
coleenp@4037 2006 used += chunk->used_word_size();
coleenp@4037 2007 chunk = chunk->next();
coleenp@4037 2008 }
coleenp@4037 2009 }
coleenp@4037 2010 return used;
coleenp@4037 2011 }
coleenp@4037 2012
coleenp@4037 2013 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
coleenp@4037 2014
jmasa@4382 2015 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
jmasa@4382 2016 Metachunk* chunk = chunks_in_use(i);
jmasa@4382 2017 st->print("SpaceManager: %s " PTR_FORMAT,
jmasa@4382 2018 chunk_size_name(i), chunk);
jmasa@4382 2019 if (chunk != NULL) {
jmasa@4382 2020 st->print_cr(" free " SIZE_FORMAT,
jmasa@4382 2021 chunk->free_word_size());
jmasa@4382 2022 } else {
drchase@6680 2023 st->cr();
jmasa@4382 2024 }
jmasa@4382 2025 }
coleenp@4037 2026
stefank@5771 2027 chunk_manager()->locked_print_free_chunks(st);
stefank@5771 2028 chunk_manager()->locked_print_sum_free_chunks(st);
coleenp@4037 2029 }
coleenp@4037 2030
coleenp@4037 2031 size_t SpaceManager::calc_chunk_size(size_t word_size) {
coleenp@4037 2032
coleenp@4037 2033 // Decide between a small chunk and a medium chunk. Up to
jwilhelm@7867 2034 // _small_chunk_limit small chunks can be allocated but
jwilhelm@7867 2035 // once a medium chunk has been allocated, no more small
jwilhelm@7867 2036 // chunks will be allocated.
coleenp@4037 2037 size_t chunk_word_size;
coleenp@4037 2038 if (chunks_in_use(MediumIndex) == NULL &&
coleenp@5337 2039 sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit) {
jmasa@4382 2040 chunk_word_size = (size_t) small_chunk_size();
jmasa@4382 2041 if (word_size + Metachunk::overhead() > small_chunk_size()) {
jmasa@4382 2042 chunk_word_size = medium_chunk_size();
coleenp@4037 2043 }
coleenp@4037 2044 } else {
jmasa@4382 2045 chunk_word_size = medium_chunk_size();
coleenp@4037 2046 }
coleenp@4037 2047
mgerdin@6004 2048 // Might still need a humongous chunk. Enforce
mgerdin@6004 2049 // humongous allocations sizes to be aligned up to
mgerdin@6004 2050 // the smallest chunk size.
jmasa@4382 2051 size_t if_humongous_sized_chunk =
jmasa@4382 2052 align_size_up(word_size + Metachunk::overhead(),
mgerdin@6004 2053 smallest_chunk_size());
coleenp@4037 2054 chunk_word_size =
jmasa@4382 2055 MAX2((size_t) chunk_word_size, if_humongous_sized_chunk);
jmasa@4382 2056
jmasa@4382 2057 assert(!SpaceManager::is_humongous(word_size) ||
jmasa@4382 2058 chunk_word_size == if_humongous_sized_chunk,
jmasa@4382 2059 err_msg("Size calculation is wrong, word_size " SIZE_FORMAT
jmasa@4382 2060 " chunk_word_size " SIZE_FORMAT,
jmasa@4382 2061 word_size, chunk_word_size));
coleenp@4037 2062 if (TraceMetadataHumongousAllocation &&
coleenp@4037 2063 SpaceManager::is_humongous(word_size)) {
coleenp@4037 2064 gclog_or_tty->print_cr("Metadata humongous allocation:");
coleenp@4037 2065 gclog_or_tty->print_cr(" word_size " PTR_FORMAT, word_size);
coleenp@4037 2066 gclog_or_tty->print_cr(" chunk_word_size " PTR_FORMAT,
coleenp@4037 2067 chunk_word_size);
jmasa@4196 2068 gclog_or_tty->print_cr(" chunk overhead " PTR_FORMAT,
coleenp@4037 2069 Metachunk::overhead());
coleenp@4037 2070 }
coleenp@4037 2071 return chunk_word_size;
coleenp@4037 2072 }
coleenp@4037 2073
stefank@5864 2074 void SpaceManager::track_metaspace_memory_usage() {
stefank@5864 2075 if (is_init_completed()) {
stefank@5864 2076 if (is_class()) {
stefank@5864 2077 MemoryService::track_compressed_class_memory_usage();
stefank@5864 2078 }
stefank@5864 2079 MemoryService::track_metaspace_memory_usage();
stefank@5864 2080 }
stefank@5864 2081 }
stefank@5864 2082
jmasa@4196 2083 MetaWord* SpaceManager::grow_and_allocate(size_t word_size) {
coleenp@4037 2084 assert(vs_list()->current_virtual_space() != NULL,
coleenp@4037 2085 "Should have been set");
coleenp@4037 2086 assert(current_chunk() == NULL ||
coleenp@4037 2087 current_chunk()->allocate(word_size) == NULL,
coleenp@4037 2088 "Don't need to expand");
coleenp@4037 2089 MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 2090
coleenp@4037 2091 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@4382 2092 size_t words_left = 0;
jmasa@4382 2093 size_t words_used = 0;
jmasa@4382 2094 if (current_chunk() != NULL) {
jmasa@4382 2095 words_left = current_chunk()->free_word_size();
jmasa@4382 2096 words_used = current_chunk()->used_word_size();
jmasa@4382 2097 }
coleenp@4037 2098 gclog_or_tty->print_cr("SpaceManager::grow_and_allocate for " SIZE_FORMAT
jmasa@4382 2099 " words " SIZE_FORMAT " words used " SIZE_FORMAT
jmasa@4382 2100 " words left",
jmasa@4382 2101 word_size, words_used, words_left);
coleenp@4037 2102 }
coleenp@4037 2103
jwilhelm@7867 2104 // Get another chunk out of the virtual space
coleenp@4037 2105 size_t grow_chunks_by_words = calc_chunk_size(word_size);
jmasa@4382 2106 Metachunk* next = get_new_chunk(word_size, grow_chunks_by_words);
coleenp@4037 2107
stefank@5863 2108 MetaWord* mem = NULL;
stefank@5863 2109
coleenp@4037 2110 // If a chunk was available, add it to the in-use chunk list
coleenp@4037 2111 // and do an allocation from it.
coleenp@4037 2112 if (next != NULL) {
coleenp@4037 2113 // Add to this manager's list of chunks in use.
coleenp@4037 2114 add_chunk(next, false);
stefank@5863 2115 mem = next->allocate(word_size);
coleenp@4037 2116 }
stefank@5863 2117
stefank@5864 2118 // Track metaspace memory usage statistic.
stefank@5864 2119 track_metaspace_memory_usage();
stefank@5864 2120
stefank@5863 2121 return mem;
coleenp@4037 2122 }
coleenp@4037 2123
coleenp@4037 2124 void SpaceManager::print_on(outputStream* st) const {
coleenp@4037 2125
jmasa@4382 2126 for (ChunkIndex i = ZeroIndex;
jmasa@4196 2127 i < NumberOfInUseLists ;
coleenp@4037 2128 i = next_chunk_index(i) ) {
coleenp@4037 2129 st->print_cr(" chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT,
coleenp@4037 2130 chunks_in_use(i),
coleenp@4037 2131 chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size());
coleenp@4037 2132 }
coleenp@4037 2133 st->print_cr(" waste: Small " SIZE_FORMAT " Medium " SIZE_FORMAT
coleenp@4037 2134 " Humongous " SIZE_FORMAT,
coleenp@4037 2135 sum_waste_in_chunks_in_use(SmallIndex),
coleenp@4037 2136 sum_waste_in_chunks_in_use(MediumIndex),
coleenp@4037 2137 sum_waste_in_chunks_in_use(HumongousIndex));
jmasa@4196 2138 // block free lists
jmasa@4196 2139 if (block_freelists() != NULL) {
jmasa@4196 2140 st->print_cr("total in block free lists " SIZE_FORMAT,
jmasa@4196 2141 block_freelists()->total_size());
jmasa@4196 2142 }
coleenp@4037 2143 }
coleenp@4037 2144
jmasa@5162 2145 SpaceManager::SpaceManager(Metaspace::MetadataType mdtype,
stefank@5771 2146 Mutex* lock) :
jmasa@5162 2147 _mdtype(mdtype),
jmasa@5015 2148 _allocated_blocks_words(0),
jmasa@5015 2149 _allocated_chunks_words(0),
jmasa@5015 2150 _allocated_chunks_count(0),
jmasa@4382 2151 _lock(lock)
jmasa@4382 2152 {
jmasa@4382 2153 initialize();
jmasa@4382 2154 }
jmasa@4382 2155
jmasa@5015 2156 void SpaceManager::inc_size_metrics(size_t words) {
jmasa@5015 2157 assert_lock_strong(SpaceManager::expand_lock());
jmasa@5015 2158 // Total of allocated Metachunks and allocated Metachunks count
jmasa@5015 2159 // for each SpaceManager
jmasa@5015 2160 _allocated_chunks_words = _allocated_chunks_words + words;
jmasa@5015 2161 _allocated_chunks_count++;
jmasa@5015 2162 // Global total of capacity in allocated Metachunks
jmasa@5162 2163 MetaspaceAux::inc_capacity(mdtype(), words);
jmasa@5015 2164 // Global total of allocated Metablocks.
jmasa@5015 2165 // used_words_slow() includes the overhead in each
jmasa@5015 2166 // Metachunk so include it in the used when the
jmasa@5015 2167 // Metachunk is first added (so only added once per
jmasa@5015 2168 // Metachunk).
jmasa@5162 2169 MetaspaceAux::inc_used(mdtype(), Metachunk::overhead());
jmasa@5015 2170 }
jmasa@5015 2171
jmasa@5015 2172 void SpaceManager::inc_used_metrics(size_t words) {
jmasa@5015 2173 // Add to the per SpaceManager total
jmasa@5015 2174 Atomic::add_ptr(words, &_allocated_blocks_words);
jmasa@5015 2175 // Add to the global total
jmasa@5162 2176 MetaspaceAux::inc_used(mdtype(), words);
jmasa@5015 2177 }
jmasa@5015 2178
jmasa@5015 2179 void SpaceManager::dec_total_from_size_metrics() {
jmasa@5162 2180 MetaspaceAux::dec_capacity(mdtype(), allocated_chunks_words());
jmasa@5162 2181 MetaspaceAux::dec_used(mdtype(), allocated_blocks_words());
jmasa@5015 2182 // Also deduct the overhead per Metachunk
jmasa@5162 2183 MetaspaceAux::dec_used(mdtype(), allocated_chunks_count() * Metachunk::overhead());
jmasa@5015 2184 }
jmasa@5015 2185
jmasa@4382 2186 void SpaceManager::initialize() {
coleenp@4037 2187 Metadebug::init_allocation_fail_alot_count();
jmasa@4382 2188 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@4037 2189 _chunks_in_use[i] = NULL;
coleenp@4037 2190 }
coleenp@4037 2191 _current_chunk = NULL;
coleenp@4037 2192 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@4037 2193 gclog_or_tty->print_cr("SpaceManager(): " PTR_FORMAT, this);
coleenp@4037 2194 }
coleenp@4037 2195 }
coleenp@4037 2196
jmasa@4932 2197 void ChunkManager::return_chunks(ChunkIndex index, Metachunk* chunks) {
jmasa@4932 2198 if (chunks == NULL) {
jmasa@4932 2199 return;
jmasa@4932 2200 }
jmasa@4932 2201 ChunkList* list = free_chunks(index);
jmasa@4932 2202 assert(list->size() == chunks->word_size(), "Mismatch in chunk sizes");
jmasa@4932 2203 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4932 2204 Metachunk* cur = chunks;
jmasa@4932 2205
jmasa@5007 2206 // This returns chunks one at a time. If a new
jmasa@4932 2207 // class List can be created that is a base class
jmasa@4932 2208 // of FreeList then something like FreeList::prepend()
jmasa@4932 2209 // can be used in place of this loop
jmasa@4932 2210 while (cur != NULL) {
jmasa@5007 2211 assert(cur->container() != NULL, "Container should have been set");
jmasa@5007 2212 cur->container()->dec_container_count();
jmasa@4932 2213 // Capture the next link before it is changed
jmasa@4932 2214 // by the call to return_chunk_at_head();
jmasa@4932 2215 Metachunk* next = cur->next();
stefank@5941 2216 DEBUG_ONLY(cur->set_is_tagged_free(true);)
jmasa@4932 2217 list->return_chunk_at_head(cur);
jmasa@4932 2218 cur = next;
jmasa@4932 2219 }
jmasa@4932 2220 }
jmasa@4932 2221
coleenp@4037 2222 SpaceManager::~SpaceManager() {
mgerdin@4784 2223 // This call this->_lock which can't be done while holding expand_lock()
jmasa@5015 2224 assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(),
jmasa@5015 2225 err_msg("sum_capacity_in_chunks_in_use() " SIZE_FORMAT
jmasa@5015 2226 " allocated_chunks_words() " SIZE_FORMAT,
jmasa@5015 2227 sum_capacity_in_chunks_in_use(), allocated_chunks_words()));
mgerdin@4784 2228
coleenp@4037 2229 MutexLockerEx fcl(SpaceManager::expand_lock(),
coleenp@4037 2230 Mutex::_no_safepoint_check_flag);
coleenp@4037 2231
stefank@5771 2232 chunk_manager()->slow_locked_verify();
coleenp@4037 2233
jmasa@5015 2234 dec_total_from_size_metrics();
jmasa@5015 2235
coleenp@4037 2236 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@4037 2237 gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this);
coleenp@4037 2238 locked_print_chunks_in_use_on(gclog_or_tty);
coleenp@4037 2239 }
coleenp@4037 2240
jmasa@5007 2241 // Do not mangle freed Metachunks. The chunk size inside Metachunks
jmasa@5007 2242 // is during the freeing of a VirtualSpaceNodes.
coleenp@4304 2243
coleenp@4037 2244 // Have to update before the chunks_in_use lists are emptied
coleenp@4037 2245 // below.
stefank@5771 2246 chunk_manager()->inc_free_chunks_total(allocated_chunks_words(),
stefank@5771 2247 sum_count_in_chunks_in_use());
coleenp@4037 2248
coleenp@4037 2249 // Add all the chunks in use by this space manager
coleenp@4037 2250 // to the global list of free chunks.
coleenp@4037 2251
jmasa@4382 2252 // Follow each list of chunks-in-use and add them to the
jmasa@4382 2253 // free lists. Each list is NULL terminated.
jmasa@4382 2254
jmasa@4382 2255 for (ChunkIndex i = ZeroIndex; i < HumongousIndex; i = next_chunk_index(i)) {
jmasa@4382 2256 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@4382 2257 gclog_or_tty->print_cr("returned %d %s chunks to freelist",
jmasa@4382 2258 sum_count_in_chunks_in_use(i),
jmasa@4382 2259 chunk_size_name(i));
jmasa@4382 2260 }
jmasa@4382 2261 Metachunk* chunks = chunks_in_use(i);
stefank@5771 2262 chunk_manager()->return_chunks(i, chunks);
jmasa@4382 2263 set_chunks_in_use(i, NULL);
jmasa@4382 2264 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@4382 2265 gclog_or_tty->print_cr("updated freelist count %d %s",
stefank@5771 2266 chunk_manager()->free_chunks(i)->count(),
jmasa@4382 2267 chunk_size_name(i));
jmasa@4382 2268 }
jmasa@4382 2269 assert(i != HumongousIndex, "Humongous chunks are handled explicitly later");
coleenp@4037 2270 }
coleenp@4037 2271
jmasa@4382 2272 // The medium chunk case may be optimized by passing the head and
jmasa@4382 2273 // tail of the medium chunk list to add_at_head(). The tail is often
jmasa@4382 2274 // the current chunk but there are probably exceptions.
jmasa@4382 2275
coleenp@4037 2276 // Humongous chunks
jmasa@4382 2277 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@4382 2278 gclog_or_tty->print_cr("returned %d %s humongous chunks to dictionary",
jmasa@4382 2279 sum_count_in_chunks_in_use(HumongousIndex),
jmasa@4382 2280 chunk_size_name(HumongousIndex));
jmasa@4382 2281 gclog_or_tty->print("Humongous chunk dictionary: ");
jmasa@4382 2282 }
coleenp@4037 2283 // Humongous chunks are never the current chunk.
coleenp@4037 2284 Metachunk* humongous_chunks = chunks_in_use(HumongousIndex);
coleenp@4037 2285
jmasa@4196 2286 while (humongous_chunks != NULL) {
jmasa@4196 2287 #ifdef ASSERT
stefank@5941 2288 humongous_chunks->set_is_tagged_free(true);
jmasa@4196 2289 #endif
jmasa@4382 2290 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@4382 2291 gclog_or_tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ",
jmasa@4382 2292 humongous_chunks,
jmasa@4382 2293 humongous_chunks->word_size());
jmasa@4382 2294 }
jmasa@4382 2295 assert(humongous_chunks->word_size() == (size_t)
jmasa@4382 2296 align_size_up(humongous_chunks->word_size(),
mgerdin@6004 2297 smallest_chunk_size()),
jmasa@4382 2298 err_msg("Humongous chunk size is wrong: word size " SIZE_FORMAT
mikael@4548 2299 " granularity %d",
mgerdin@6004 2300 humongous_chunks->word_size(), smallest_chunk_size()));
jmasa@4196 2301 Metachunk* next_humongous_chunks = humongous_chunks->next();
jmasa@5007 2302 humongous_chunks->container()->dec_container_count();
stefank@5771 2303 chunk_manager()->humongous_dictionary()->return_chunk(humongous_chunks);
jmasa@4196 2304 humongous_chunks = next_humongous_chunks;
coleenp@4037 2305 }
jmasa@4382 2306 if (TraceMetadataChunkAllocation && Verbose) {
drchase@6680 2307 gclog_or_tty->cr();
jmasa@4382 2308 gclog_or_tty->print_cr("updated dictionary count %d %s",
stefank@5771 2309 chunk_manager()->humongous_dictionary()->total_count(),
jmasa@4382 2310 chunk_size_name(HumongousIndex));
jmasa@4382 2311 }
stefank@5771 2312 chunk_manager()->slow_locked_verify();
coleenp@4037 2313 }
coleenp@4037 2314
jmasa@4382 2315 const char* SpaceManager::chunk_size_name(ChunkIndex index) const {
jmasa@4382 2316 switch (index) {
jmasa@4382 2317 case SpecializedIndex:
jmasa@4382 2318 return "Specialized";
jmasa@4382 2319 case SmallIndex:
jmasa@4382 2320 return "Small";
jmasa@4382 2321 case MediumIndex:
jmasa@4382 2322 return "Medium";
jmasa@4382 2323 case HumongousIndex:
jmasa@4382 2324 return "Humongous";
jmasa@4382 2325 default:
jmasa@4382 2326 return NULL;
jmasa@4382 2327 }
jmasa@4382 2328 }
jmasa@4382 2329
jmasa@4382 2330 ChunkIndex ChunkManager::list_index(size_t size) {
jmasa@4382 2331 switch (size) {
jmasa@4382 2332 case SpecializedChunk:
jmasa@4382 2333 assert(SpecializedChunk == ClassSpecializedChunk,
jmasa@4382 2334 "Need branch for ClassSpecializedChunk");
jmasa@4382 2335 return SpecializedIndex;
jmasa@4382 2336 case SmallChunk:
jmasa@4382 2337 case ClassSmallChunk:
jmasa@4382 2338 return SmallIndex;
jmasa@4382 2339 case MediumChunk:
jmasa@4382 2340 case ClassMediumChunk:
jmasa@4382 2341 return MediumIndex;
jmasa@4382 2342 default:
jmasa@4383 2343 assert(size > MediumChunk || size > ClassMediumChunk,
jmasa@4382 2344 "Not a humongous chunk");
jmasa@4382 2345 return HumongousIndex;
jmasa@4382 2346 }
jmasa@4382 2347 }
jmasa@4382 2348
jmasa@4196 2349 void SpaceManager::deallocate(MetaWord* p, size_t word_size) {
coleenp@4037 2350 assert_lock_strong(_lock);
fparain@5452 2351 size_t raw_word_size = get_raw_word_size(word_size);
goetz@6337 2352 size_t min_size = TreeChunk<Metablock, FreeList<Metablock> >::min_size();
fparain@5452 2353 assert(raw_word_size >= min_size,
hseigel@5528 2354 err_msg("Should not deallocate dark matter " SIZE_FORMAT "<" SIZE_FORMAT, word_size, min_size));
fparain@5452 2355 block_freelists()->return_block(p, raw_word_size);
coleenp@4037 2356 }
coleenp@4037 2357
coleenp@4037 2358 // Adds a chunk to the list of chunks in use.
coleenp@4037 2359 void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) {
coleenp@4037 2360
coleenp@4037 2361 assert(new_chunk != NULL, "Should not be NULL");
coleenp@4037 2362 assert(new_chunk->next() == NULL, "Should not be on a list");
coleenp@4037 2363
coleenp@4037 2364 new_chunk->reset_empty();
coleenp@4037 2365
coleenp@4037 2366 // Find the correct list and and set the current
coleenp@4037 2367 // chunk for that list.
jmasa@4382 2368 ChunkIndex index = ChunkManager::list_index(new_chunk->word_size());
jmasa@4382 2369
jmasa@4382 2370 if (index != HumongousIndex) {
mgerdin@5699 2371 retire_current_chunk();
coleenp@4037 2372 set_current_chunk(new_chunk);
jmasa@4382 2373 new_chunk->set_next(chunks_in_use(index));
jmasa@4382 2374 set_chunks_in_use(index, new_chunk);
jmasa@4382 2375 } else {
coleenp@4037 2376 // For null class loader data and DumpSharedSpaces, the first chunk isn't
coleenp@4037 2377 // small, so small will be null. Link this first chunk as the current
coleenp@4037 2378 // chunk.
coleenp@4037 2379 if (make_current) {
coleenp@4037 2380 // Set as the current chunk but otherwise treat as a humongous chunk.
coleenp@4037 2381 set_current_chunk(new_chunk);
coleenp@4037 2382 }
coleenp@4037 2383 // Link at head. The _current_chunk only points to a humongous chunk for
coleenp@4037 2384 // the null class loader metaspace (class and data virtual space managers)
coleenp@4037 2385 // any humongous chunks so will not point to the tail
coleenp@4037 2386 // of the humongous chunks list.
coleenp@4037 2387 new_chunk->set_next(chunks_in_use(HumongousIndex));
coleenp@4037 2388 set_chunks_in_use(HumongousIndex, new_chunk);
coleenp@4037 2389
jmasa@4383 2390 assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency");
coleenp@4037 2391 }
coleenp@4037 2392
jmasa@5015 2393 // Add to the running sum of capacity
jmasa@5015 2394 inc_size_metrics(new_chunk->word_size());
jmasa@5015 2395
coleenp@4037 2396 assert(new_chunk->is_empty(), "Not ready for reuse");
coleenp@4037 2397 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@4037 2398 gclog_or_tty->print("SpaceManager::add_chunk: %d) ",
coleenp@4037 2399 sum_count_in_chunks_in_use());
coleenp@4037 2400 new_chunk->print_on(gclog_or_tty);
stefank@5771 2401 chunk_manager()->locked_print_free_chunks(gclog_or_tty);
coleenp@4037 2402 }
coleenp@4037 2403 }
coleenp@4037 2404
mgerdin@5699 2405 void SpaceManager::retire_current_chunk() {
mgerdin@5699 2406 if (current_chunk() != NULL) {
mgerdin@5699 2407 size_t remaining_words = current_chunk()->free_word_size();
goetz@6337 2408 if (remaining_words >= TreeChunk<Metablock, FreeList<Metablock> >::min_size()) {
mgerdin@5699 2409 block_freelists()->return_block(current_chunk()->allocate(remaining_words), remaining_words);
mgerdin@5699 2410 inc_used_metrics(remaining_words);
mgerdin@5699 2411 }
mgerdin@5699 2412 }
mgerdin@5699 2413 }
mgerdin@5699 2414
jmasa@4382 2415 Metachunk* SpaceManager::get_new_chunk(size_t word_size,
jmasa@4382 2416 size_t grow_chunks_by_words) {
stefank@5771 2417 // Get a chunk from the chunk freelist
stefank@5771 2418 Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words);
stefank@5771 2419
stefank@5771 2420 if (next == NULL) {
stefank@5771 2421 next = vs_list()->get_new_chunk(word_size,
stefank@5771 2422 grow_chunks_by_words,
stefank@5771 2423 medium_chunk_bunch());
stefank@5771 2424 }
jmasa@4382 2425
stefank@5707 2426 if (TraceMetadataHumongousAllocation && next != NULL &&
jmasa@4382 2427 SpaceManager::is_humongous(next->word_size())) {
stefank@5707 2428 gclog_or_tty->print_cr(" new humongous chunk word size "
stefank@5707 2429 PTR_FORMAT, next->word_size());
jmasa@4382 2430 }
jmasa@4382 2431
jmasa@4382 2432 return next;
jmasa@4382 2433 }
jmasa@4382 2434
coleenp@4037 2435 MetaWord* SpaceManager::allocate(size_t word_size) {
coleenp@4037 2436 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 2437
iklam@5208 2438 size_t raw_word_size = get_raw_word_size(word_size);
coleenp@4037 2439 BlockFreelist* fl = block_freelists();
jmasa@4196 2440 MetaWord* p = NULL;
coleenp@4037 2441 // Allocation from the dictionary is expensive in the sense that
coleenp@4037 2442 // the dictionary has to be searched for a size. Don't allocate
coleenp@4037 2443 // from the dictionary until it starts to get fat. Is this
coleenp@4037 2444 // a reasonable policy? Maybe an skinny dictionary is fast enough
coleenp@4037 2445 // for allocations. Do some profiling. JJJ
jmasa@4196 2446 if (fl->total_size() > allocation_from_dictionary_limit) {
jmasa@4196 2447 p = fl->get_block(raw_word_size);
coleenp@4037 2448 }
jmasa@4196 2449 if (p == NULL) {
jmasa@4196 2450 p = allocate_work(raw_word_size);
coleenp@4037 2451 }
coleenp@4037 2452
jmasa@4196 2453 return p;
coleenp@4037 2454 }
coleenp@4037 2455
coleenp@4037 2456 // Returns the address of spaced allocated for "word_size".
coleenp@4037 2457 // This methods does not know about blocks (Metablocks)
jmasa@4196 2458 MetaWord* SpaceManager::allocate_work(size_t word_size) {
coleenp@4037 2459 assert_lock_strong(_lock);
coleenp@4037 2460 #ifdef ASSERT
coleenp@4037 2461 if (Metadebug::test_metadata_failure()) {
coleenp@4037 2462 return NULL;
coleenp@4037 2463 }
coleenp@4037 2464 #endif
coleenp@4037 2465 // Is there space in the current chunk?
jmasa@4196 2466 MetaWord* result = NULL;
coleenp@4037 2467
coleenp@4037 2468 // For DumpSharedSpaces, only allocate out of the current chunk which is
coleenp@4037 2469 // never null because we gave it the size we wanted. Caller reports out
coleenp@4037 2470 // of memory if this returns null.
coleenp@4037 2471 if (DumpSharedSpaces) {
coleenp@4037 2472 assert(current_chunk() != NULL, "should never happen");
jmasa@5015 2473 inc_used_metrics(word_size);
coleenp@4037 2474 return current_chunk()->allocate(word_size); // caller handles null result
coleenp@4037 2475 }
stefank@5863 2476
coleenp@4037 2477 if (current_chunk() != NULL) {
coleenp@4037 2478 result = current_chunk()->allocate(word_size);
coleenp@4037 2479 }
coleenp@4037 2480
coleenp@4037 2481 if (result == NULL) {
coleenp@4037 2482 result = grow_and_allocate(word_size);
coleenp@4037 2483 }
stefank@5863 2484
stefank@5863 2485 if (result != NULL) {
jmasa@5015 2486 inc_used_metrics(word_size);
jmasa@4196 2487 assert(result != (MetaWord*) chunks_in_use(MediumIndex),
jmasa@4196 2488 "Head of the list is being allocated");
coleenp@4037 2489 }
coleenp@4037 2490
coleenp@4037 2491 return result;
coleenp@4037 2492 }
coleenp@4037 2493
coleenp@4037 2494 void SpaceManager::verify() {
coleenp@4037 2495 // If there are blocks in the dictionary, then
coleenp@4037 2496 // verfication of chunks does not work since
coleenp@4037 2497 // being in the dictionary alters a chunk.
jmasa@4196 2498 if (block_freelists()->total_size() == 0) {
jmasa@4382 2499 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@4037 2500 Metachunk* curr = chunks_in_use(i);
coleenp@4037 2501 while (curr != NULL) {
coleenp@4037 2502 curr->verify();
jmasa@4327 2503 verify_chunk_size(curr);
coleenp@4037 2504 curr = curr->next();
coleenp@4037 2505 }
coleenp@4037 2506 }
coleenp@4037 2507 }
coleenp@4037 2508 }
coleenp@4037 2509
jmasa@4327 2510 void SpaceManager::verify_chunk_size(Metachunk* chunk) {
jmasa@4327 2511 assert(is_humongous(chunk->word_size()) ||
jmasa@4382 2512 chunk->word_size() == medium_chunk_size() ||
jmasa@4382 2513 chunk->word_size() == small_chunk_size() ||
jmasa@4382 2514 chunk->word_size() == specialized_chunk_size(),
jmasa@4327 2515 "Chunk size is wrong");
jmasa@4327 2516 return;
jmasa@4327 2517 }
jmasa@4327 2518
coleenp@4037 2519 #ifdef ASSERT
jmasa@5015 2520 void SpaceManager::verify_allocated_blocks_words() {
coleenp@4037 2521 // Verification is only guaranteed at a safepoint.
jmasa@5015 2522 assert(SafepointSynchronize::is_at_safepoint() || !Universe::is_fully_initialized(),
jmasa@5015 2523 "Verification can fail if the applications is running");
jmasa@5015 2524 assert(allocated_blocks_words() == sum_used_in_chunks_in_use(),
mikael@4548 2525 err_msg("allocation total is not consistent " SIZE_FORMAT
mikael@4548 2526 " vs " SIZE_FORMAT,
jmasa@5015 2527 allocated_blocks_words(), sum_used_in_chunks_in_use()));
coleenp@4037 2528 }
coleenp@4037 2529
coleenp@4037 2530 #endif
coleenp@4037 2531
coleenp@4037 2532 void SpaceManager::dump(outputStream* const out) const {
coleenp@4037 2533 size_t curr_total = 0;
coleenp@4037 2534 size_t waste = 0;
coleenp@4037 2535 uint i = 0;
coleenp@4037 2536 size_t used = 0;
coleenp@4037 2537 size_t capacity = 0;
coleenp@4037 2538
coleenp@4037 2539 // Add up statistics for all chunks in this SpaceManager.
jmasa@4382 2540 for (ChunkIndex index = ZeroIndex;
jmasa@4196 2541 index < NumberOfInUseLists;
coleenp@4037 2542 index = next_chunk_index(index)) {
coleenp@4037 2543 for (Metachunk* curr = chunks_in_use(index);
coleenp@4037 2544 curr != NULL;
coleenp@4037 2545 curr = curr->next()) {
coleenp@4037 2546 out->print("%d) ", i++);
coleenp@4037 2547 curr->print_on(out);
coleenp@4037 2548 curr_total += curr->word_size();
coleenp@4037 2549 used += curr->used_word_size();
stefank@5941 2550 capacity += curr->word_size();
coleenp@4037 2551 waste += curr->free_word_size() + curr->overhead();;
coleenp@4037 2552 }
coleenp@4037 2553 }
coleenp@4037 2554
stefank@5707 2555 if (TraceMetadataChunkAllocation && Verbose) {
stefank@5707 2556 block_freelists()->print_on(out);
stefank@5707 2557 }
stefank@5707 2558
jmasa@4382 2559 size_t free = current_chunk() == NULL ? 0 : current_chunk()->free_word_size();
coleenp@4037 2560 // Free space isn't wasted.
coleenp@4037 2561 waste -= free;
coleenp@4037 2562
coleenp@4037 2563 out->print_cr("total of all chunks " SIZE_FORMAT " used " SIZE_FORMAT
coleenp@4037 2564 " free " SIZE_FORMAT " capacity " SIZE_FORMAT
coleenp@4037 2565 " waste " SIZE_FORMAT, curr_total, used, free, capacity, waste);
coleenp@4037 2566 }
coleenp@4037 2567
coleenp@4304 2568 #ifndef PRODUCT
coleenp@4037 2569 void SpaceManager::mangle_freed_chunks() {
jmasa@4382 2570 for (ChunkIndex index = ZeroIndex;
jmasa@4196 2571 index < NumberOfInUseLists;
coleenp@4037 2572 index = next_chunk_index(index)) {
coleenp@4037 2573 for (Metachunk* curr = chunks_in_use(index);
coleenp@4037 2574 curr != NULL;
coleenp@4037 2575 curr = curr->next()) {
coleenp@4037 2576 curr->mangle();
coleenp@4037 2577 }
coleenp@4037 2578 }
coleenp@4037 2579 }
coleenp@4304 2580 #endif // PRODUCT
coleenp@4037 2581
coleenp@4037 2582 // MetaspaceAux
coleenp@4037 2583
jmasa@5015 2584
ehelin@6609 2585 size_t MetaspaceAux::_capacity_words[] = {0, 0};
ehelin@6609 2586 size_t MetaspaceAux::_used_words[] = {0, 0};
jmasa@5015 2587
ehelin@5531 2588 size_t MetaspaceAux::free_bytes(Metaspace::MetadataType mdtype) {
ehelin@5531 2589 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
ehelin@5531 2590 return list == NULL ? 0 : list->free_bytes();
ehelin@5531 2591 }
ehelin@5531 2592
jmasa@5015 2593 size_t MetaspaceAux::free_bytes() {
ehelin@5531 2594 return free_bytes(Metaspace::ClassType) + free_bytes(Metaspace::NonClassType);
jmasa@5015 2595 }
jmasa@5015 2596
jmasa@5162 2597 void MetaspaceAux::dec_capacity(Metaspace::MetadataType mdtype, size_t words) {
jmasa@5015 2598 assert_lock_strong(SpaceManager::expand_lock());
ehelin@6609 2599 assert(words <= capacity_words(mdtype),
jmasa@5015 2600 err_msg("About to decrement below 0: words " SIZE_FORMAT
ehelin@6609 2601 " is greater than _capacity_words[%u] " SIZE_FORMAT,
ehelin@6609 2602 words, mdtype, capacity_words(mdtype)));
ehelin@6609 2603 _capacity_words[mdtype] -= words;
jmasa@5015 2604 }
jmasa@5015 2605
jmasa@5162 2606 void MetaspaceAux::inc_capacity(Metaspace::MetadataType mdtype, size_t words) {
jmasa@5015 2607 assert_lock_strong(SpaceManager::expand_lock());
jmasa@5015 2608 // Needs to be atomic
ehelin@6609 2609 _capacity_words[mdtype] += words;
jmasa@5015 2610 }
jmasa@5015 2611
jmasa@5162 2612 void MetaspaceAux::dec_used(Metaspace::MetadataType mdtype, size_t words) {
ehelin@6609 2613 assert(words <= used_words(mdtype),
jmasa@5015 2614 err_msg("About to decrement below 0: words " SIZE_FORMAT
ehelin@6609 2615 " is greater than _used_words[%u] " SIZE_FORMAT,
ehelin@6609 2616 words, mdtype, used_words(mdtype)));
jmasa@5015 2617 // For CMS deallocation of the Metaspaces occurs during the
jmasa@5015 2618 // sweep which is a concurrent phase. Protection by the expand_lock()
jmasa@5015 2619 // is not enough since allocation is on a per Metaspace basis
jmasa@5015 2620 // and protected by the Metaspace lock.
jmasa@5015 2621 jlong minus_words = (jlong) - (jlong) words;
ehelin@6609 2622 Atomic::add_ptr(minus_words, &_used_words[mdtype]);
jmasa@5015 2623 }
jmasa@5015 2624
jmasa@5162 2625 void MetaspaceAux::inc_used(Metaspace::MetadataType mdtype, size_t words) {
ehelin@6609 2626 // _used_words tracks allocations for
jmasa@5015 2627 // each piece of metadata. Those allocations are
jmasa@5015 2628 // generally done concurrently by different application
jmasa@5015 2629 // threads so must be done atomically.
ehelin@6609 2630 Atomic::add_ptr(words, &_used_words[mdtype]);
jmasa@5015 2631 }
jmasa@5015 2632
jmasa@5015 2633 size_t MetaspaceAux::used_bytes_slow(Metaspace::MetadataType mdtype) {
jmasa@4042 2634 size_t used = 0;
jmasa@4042 2635 ClassLoaderDataGraphMetaspaceIterator iter;
jmasa@4042 2636 while (iter.repeat()) {
jmasa@4042 2637 Metaspace* msp = iter.get_next();
jmasa@5015 2638 // Sum allocated_blocks_words for each metaspace
jmasa@4042 2639 if (msp != NULL) {
jmasa@5015 2640 used += msp->used_words_slow(mdtype);
jmasa@4042 2641 }
jmasa@4042 2642 }
jmasa@4042 2643 return used * BytesPerWord;
jmasa@4042 2644 }
jmasa@4042 2645
ehelin@5703 2646 size_t MetaspaceAux::free_bytes_slow(Metaspace::MetadataType mdtype) {
coleenp@4037 2647 size_t free = 0;
coleenp@4037 2648 ClassLoaderDataGraphMetaspaceIterator iter;
coleenp@4037 2649 while (iter.repeat()) {
coleenp@4037 2650 Metaspace* msp = iter.get_next();
coleenp@4037 2651 if (msp != NULL) {
ehelin@5703 2652 free += msp->free_words_slow(mdtype);
coleenp@4037 2653 }
coleenp@4037 2654 }
coleenp@4037 2655 return free * BytesPerWord;
coleenp@4037 2656 }
coleenp@4037 2657
jmasa@5015 2658 size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) {
hseigel@5528 2659 if ((mdtype == Metaspace::ClassType) && !Metaspace::using_class_space()) {
hseigel@5528 2660 return 0;
hseigel@5528 2661 }
jmasa@5015 2662 // Don't count the space in the freelists. That space will be
jmasa@5015 2663 // added to the capacity calculation as needed.
jmasa@5015 2664 size_t capacity = 0;
coleenp@4037 2665 ClassLoaderDataGraphMetaspaceIterator iter;
coleenp@4037 2666 while (iter.repeat()) {
coleenp@4037 2667 Metaspace* msp = iter.get_next();
coleenp@4037 2668 if (msp != NULL) {
jmasa@5015 2669 capacity += msp->capacity_words_slow(mdtype);
coleenp@4037 2670 }
coleenp@4037 2671 }
coleenp@4037 2672 return capacity * BytesPerWord;
coleenp@4037 2673 }
coleenp@4037 2674
ehelin@5703 2675 size_t MetaspaceAux::capacity_bytes_slow() {
ehelin@5703 2676 #ifdef PRODUCT
ehelin@6609 2677 // Use capacity_bytes() in PRODUCT instead of this function.
ehelin@5703 2678 guarantee(false, "Should not call capacity_bytes_slow() in the PRODUCT");
ehelin@5703 2679 #endif
ehelin@5703 2680 size_t class_capacity = capacity_bytes_slow(Metaspace::ClassType);
ehelin@5703 2681 size_t non_class_capacity = capacity_bytes_slow(Metaspace::NonClassType);
ehelin@6609 2682 assert(capacity_bytes() == class_capacity + non_class_capacity,
ehelin@6609 2683 err_msg("bad accounting: capacity_bytes() " SIZE_FORMAT
ehelin@5703 2684 " class_capacity + non_class_capacity " SIZE_FORMAT
ehelin@5703 2685 " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT,
ehelin@6609 2686 capacity_bytes(), class_capacity + non_class_capacity,
ehelin@5703 2687 class_capacity, non_class_capacity));
ehelin@5703 2688
ehelin@5703 2689 return class_capacity + non_class_capacity;
ehelin@5703 2690 }
ehelin@5703 2691
ehelin@5703 2692 size_t MetaspaceAux::reserved_bytes(Metaspace::MetadataType mdtype) {
ehelin@5531 2693 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
stefank@5704 2694 return list == NULL ? 0 : list->reserved_bytes();
stefank@5704 2695 }
stefank@5704 2696
stefank@5704 2697 size_t MetaspaceAux::committed_bytes(Metaspace::MetadataType mdtype) {
stefank@5704 2698 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
stefank@5704 2699 return list == NULL ? 0 : list->committed_bytes();
coleenp@4037 2700 }
coleenp@4037 2701
ehelin@5703 2702 size_t MetaspaceAux::min_chunk_size_words() { return Metaspace::first_chunk_word_size(); }
ehelin@5703 2703
ehelin@5703 2704 size_t MetaspaceAux::free_chunks_total_words(Metaspace::MetadataType mdtype) {
stefank@5771 2705 ChunkManager* chunk_manager = Metaspace::get_chunk_manager(mdtype);
stefank@5771 2706 if (chunk_manager == NULL) {
hseigel@5528 2707 return 0;
hseigel@5528 2708 }
stefank@5771 2709 chunk_manager->slow_verify();
stefank@5771 2710 return chunk_manager->free_chunks_total_words();
coleenp@4037 2711 }
coleenp@4037 2712
ehelin@5703 2713 size_t MetaspaceAux::free_chunks_total_bytes(Metaspace::MetadataType mdtype) {
ehelin@5703 2714 return free_chunks_total_words(mdtype) * BytesPerWord;
coleenp@4037 2715 }
coleenp@4037 2716
ehelin@5703 2717 size_t MetaspaceAux::free_chunks_total_words() {
ehelin@5703 2718 return free_chunks_total_words(Metaspace::ClassType) +
ehelin@5703 2719 free_chunks_total_words(Metaspace::NonClassType);
jmasa@5015 2720 }
jmasa@5015 2721
ehelin@5703 2722 size_t MetaspaceAux::free_chunks_total_bytes() {
ehelin@5703 2723 return free_chunks_total_words() * BytesPerWord;
jmasa@5015 2724 }
jmasa@5015 2725
ehelin@6420 2726 bool MetaspaceAux::has_chunk_free_list(Metaspace::MetadataType mdtype) {
ehelin@6420 2727 return Metaspace::get_chunk_manager(mdtype) != NULL;
ehelin@6420 2728 }
ehelin@6420 2729
ehelin@6420 2730 MetaspaceChunkFreeListSummary MetaspaceAux::chunk_free_list_summary(Metaspace::MetadataType mdtype) {
ehelin@6420 2731 if (!has_chunk_free_list(mdtype)) {
ehelin@6420 2732 return MetaspaceChunkFreeListSummary();
ehelin@6420 2733 }
ehelin@6420 2734
ehelin@6420 2735 const ChunkManager* cm = Metaspace::get_chunk_manager(mdtype);
ehelin@6420 2736 return cm->chunk_free_list_summary();
ehelin@6420 2737 }
ehelin@6420 2738
coleenp@4037 2739 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) {
coleenp@4037 2740 gclog_or_tty->print(", [Metaspace:");
coleenp@4037 2741 if (PrintGCDetails && Verbose) {
coleenp@4037 2742 gclog_or_tty->print(" " SIZE_FORMAT
coleenp@4037 2743 "->" SIZE_FORMAT
jmasa@5015 2744 "(" SIZE_FORMAT ")",
coleenp@4037 2745 prev_metadata_used,
ehelin@6609 2746 used_bytes(),
ehelin@5703 2747 reserved_bytes());
coleenp@4037 2748 } else {
coleenp@4037 2749 gclog_or_tty->print(" " SIZE_FORMAT "K"
coleenp@4037 2750 "->" SIZE_FORMAT "K"
jmasa@5015 2751 "(" SIZE_FORMAT "K)",
ehelin@5703 2752 prev_metadata_used/K,
ehelin@6609 2753 used_bytes()/K,
ehelin@5703 2754 reserved_bytes()/K);
coleenp@4037 2755 }
coleenp@4037 2756
coleenp@4037 2757 gclog_or_tty->print("]");
coleenp@4037 2758 }
coleenp@4037 2759
coleenp@4037 2760 // This is printed when PrintGCDetails
coleenp@4037 2761 void MetaspaceAux::print_on(outputStream* out) {
coleenp@4037 2762 Metaspace::MetadataType nct = Metaspace::NonClassType;
coleenp@4037 2763
stefank@5863 2764 out->print_cr(" Metaspace "
stefank@5863 2765 "used " SIZE_FORMAT "K, "
stefank@5863 2766 "capacity " SIZE_FORMAT "K, "
stefank@5863 2767 "committed " SIZE_FORMAT "K, "
stefank@5863 2768 "reserved " SIZE_FORMAT "K",
ehelin@6609 2769 used_bytes()/K,
ehelin@6609 2770 capacity_bytes()/K,
stefank@5863 2771 committed_bytes()/K,
stefank@5863 2772 reserved_bytes()/K);
stefank@5863 2773
hseigel@5528 2774 if (Metaspace::using_class_space()) {
hseigel@5528 2775 Metaspace::MetadataType ct = Metaspace::ClassType;
hseigel@5528 2776 out->print_cr(" class space "
stefank@5863 2777 "used " SIZE_FORMAT "K, "
stefank@5863 2778 "capacity " SIZE_FORMAT "K, "
stefank@5863 2779 "committed " SIZE_FORMAT "K, "
stefank@5863 2780 "reserved " SIZE_FORMAT "K",
ehelin@6609 2781 used_bytes(ct)/K,
ehelin@6609 2782 capacity_bytes(ct)/K,
stefank@5863 2783 committed_bytes(ct)/K,
ehelin@5703 2784 reserved_bytes(ct)/K);
hseigel@5528 2785 }
coleenp@4037 2786 }
coleenp@4037 2787
coleenp@4037 2788 // Print information for class space and data space separately.
coleenp@4037 2789 // This is almost the same as above.
coleenp@4037 2790 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
ehelin@5703 2791 size_t free_chunks_capacity_bytes = free_chunks_total_bytes(mdtype);
jmasa@5015 2792 size_t capacity_bytes = capacity_bytes_slow(mdtype);
jmasa@5015 2793 size_t used_bytes = used_bytes_slow(mdtype);
ehelin@5703 2794 size_t free_bytes = free_bytes_slow(mdtype);
coleenp@4037 2795 size_t used_and_free = used_bytes + free_bytes +
coleenp@4037 2796 free_chunks_capacity_bytes;
coleenp@4037 2797 out->print_cr(" Chunk accounting: used in chunks " SIZE_FORMAT
coleenp@4037 2798 "K + unused in chunks " SIZE_FORMAT "K + "
coleenp@4037 2799 " capacity in free chunks " SIZE_FORMAT "K = " SIZE_FORMAT
coleenp@4037 2800 "K capacity in allocated chunks " SIZE_FORMAT "K",
coleenp@4037 2801 used_bytes / K,
coleenp@4037 2802 free_bytes / K,
coleenp@4037 2803 free_chunks_capacity_bytes / K,
coleenp@4037 2804 used_and_free / K,
coleenp@4037 2805 capacity_bytes / K);
mgerdin@4738 2806 // Accounting can only be correct if we got the values during a safepoint
mgerdin@4738 2807 assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong");
coleenp@4037 2808 }
coleenp@4037 2809
hseigel@5528 2810 // Print total fragmentation for class metaspaces
hseigel@5528 2811 void MetaspaceAux::print_class_waste(outputStream* out) {
hseigel@5528 2812 assert(Metaspace::using_class_space(), "class metaspace not used");
hseigel@5528 2813 size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0;
hseigel@5528 2814 size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0;
hseigel@5528 2815 ClassLoaderDataGraphMetaspaceIterator iter;
hseigel@5528 2816 while (iter.repeat()) {
hseigel@5528 2817 Metaspace* msp = iter.get_next();
hseigel@5528 2818 if (msp != NULL) {
hseigel@5528 2819 cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
hseigel@5528 2820 cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
hseigel@5528 2821 cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex);
hseigel@5528 2822 cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex);
hseigel@5528 2823 cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex);
hseigel@5528 2824 cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex);
hseigel@5528 2825 cls_humongous_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex);
hseigel@5528 2826 }
hseigel@5528 2827 }
hseigel@5528 2828 out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
hseigel@5528 2829 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
hseigel@5528 2830 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
hseigel@5528 2831 "large count " SIZE_FORMAT,
hseigel@5528 2832 cls_specialized_count, cls_specialized_waste,
hseigel@5528 2833 cls_small_count, cls_small_waste,
hseigel@5528 2834 cls_medium_count, cls_medium_waste, cls_humongous_count);
hseigel@5528 2835 }
hseigel@5528 2836
hseigel@5528 2837 // Print total fragmentation for data and class metaspaces separately
coleenp@4037 2838 void MetaspaceAux::print_waste(outputStream* out) {
coleenp@5337 2839 size_t specialized_waste = 0, small_waste = 0, medium_waste = 0;
coleenp@5337 2840 size_t specialized_count = 0, small_count = 0, medium_count = 0, humongous_count = 0;
coleenp@4037 2841
coleenp@4037 2842 ClassLoaderDataGraphMetaspaceIterator iter;
coleenp@4037 2843 while (iter.repeat()) {
coleenp@4037 2844 Metaspace* msp = iter.get_next();
coleenp@4037 2845 if (msp != NULL) {
jmasa@4382 2846 specialized_waste += msp->vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
jmasa@4382 2847 specialized_count += msp->vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
coleenp@4037 2848 small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex);
jmasa@4382 2849 small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex);
coleenp@4037 2850 medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex);
jmasa@4382 2851 medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex);
coleenp@5337 2852 humongous_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex);
coleenp@4037 2853 }
coleenp@4037 2854 }
coleenp@4037 2855 out->print_cr("Total fragmentation waste (words) doesn't count free space");
jmasa@4382 2856 out->print_cr(" data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
jmasa@4382 2857 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
coleenp@5337 2858 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
coleenp@5337 2859 "large count " SIZE_FORMAT,
jmasa@4382 2860 specialized_count, specialized_waste, small_count,
coleenp@5337 2861 small_waste, medium_count, medium_waste, humongous_count);
hseigel@5528 2862 if (Metaspace::using_class_space()) {
hseigel@5528 2863 print_class_waste(out);
hseigel@5528 2864 }
coleenp@4037 2865 }
coleenp@4037 2866
coleenp@4037 2867 // Dump global metaspace things from the end of ClassLoaderDataGraph
coleenp@4037 2868 void MetaspaceAux::dump(outputStream* out) {
coleenp@4037 2869 out->print_cr("All Metaspace:");
coleenp@4037 2870 out->print("data space: "); print_on(out, Metaspace::NonClassType);
coleenp@4037 2871 out->print("class space: "); print_on(out, Metaspace::ClassType);
coleenp@4037 2872 print_waste(out);
coleenp@4037 2873 }
coleenp@4037 2874
mgerdin@4264 2875 void MetaspaceAux::verify_free_chunks() {
stefank@5771 2876 Metaspace::chunk_manager_metadata()->verify();
hseigel@5528 2877 if (Metaspace::using_class_space()) {
stefank@5771 2878 Metaspace::chunk_manager_class()->verify();
hseigel@5528 2879 }
mgerdin@4264 2880 }
mgerdin@4264 2881
jmasa@5015 2882 void MetaspaceAux::verify_capacity() {
jmasa@5015 2883 #ifdef ASSERT
ehelin@6609 2884 size_t running_sum_capacity_bytes = capacity_bytes();
jmasa@5162 2885 // For purposes of the running sum of capacity, verify against capacity
jmasa@5015 2886 size_t capacity_in_use_bytes = capacity_bytes_slow();
jmasa@5015 2887 assert(running_sum_capacity_bytes == capacity_in_use_bytes,
ehelin@6609 2888 err_msg("capacity_words() * BytesPerWord " SIZE_FORMAT
jmasa@5015 2889 " capacity_bytes_slow()" SIZE_FORMAT,
jmasa@5015 2890 running_sum_capacity_bytes, capacity_in_use_bytes));
jmasa@5162 2891 for (Metaspace::MetadataType i = Metaspace::ClassType;
jmasa@5162 2892 i < Metaspace:: MetadataTypeCount;
jmasa@5162 2893 i = (Metaspace::MetadataType)(i + 1)) {
jmasa@5162 2894 size_t capacity_in_use_bytes = capacity_bytes_slow(i);
ehelin@6609 2895 assert(capacity_bytes(i) == capacity_in_use_bytes,
ehelin@6609 2896 err_msg("capacity_bytes(%u) " SIZE_FORMAT
jmasa@5162 2897 " capacity_bytes_slow(%u)" SIZE_FORMAT,
ehelin@6609 2898 i, capacity_bytes(i), i, capacity_in_use_bytes));
jmasa@5162 2899 }
jmasa@5015 2900 #endif
jmasa@5015 2901 }
jmasa@5015 2902
jmasa@5015 2903 void MetaspaceAux::verify_used() {
jmasa@5015 2904 #ifdef ASSERT
ehelin@6609 2905 size_t running_sum_used_bytes = used_bytes();
jmasa@5162 2906 // For purposes of the running sum of used, verify against used
jmasa@5015 2907 size_t used_in_use_bytes = used_bytes_slow();
ehelin@6609 2908 assert(used_bytes() == used_in_use_bytes,
ehelin@6609 2909 err_msg("used_bytes() " SIZE_FORMAT
jmasa@5162 2910 " used_bytes_slow()" SIZE_FORMAT,
ehelin@6609 2911 used_bytes(), used_in_use_bytes));
jmasa@5162 2912 for (Metaspace::MetadataType i = Metaspace::ClassType;
jmasa@5162 2913 i < Metaspace:: MetadataTypeCount;
jmasa@5162 2914 i = (Metaspace::MetadataType)(i + 1)) {
jmasa@5162 2915 size_t used_in_use_bytes = used_bytes_slow(i);
ehelin@6609 2916 assert(used_bytes(i) == used_in_use_bytes,
ehelin@6609 2917 err_msg("used_bytes(%u) " SIZE_FORMAT
jmasa@5162 2918 " used_bytes_slow(%u)" SIZE_FORMAT,
ehelin@6609 2919 i, used_bytes(i), i, used_in_use_bytes));
jmasa@5162 2920 }
jmasa@5015 2921 #endif
jmasa@5015 2922 }
jmasa@5015 2923
jmasa@5015 2924 void MetaspaceAux::verify_metrics() {
jmasa@5015 2925 verify_capacity();
jmasa@5015 2926 verify_used();
jmasa@5015 2927 }
jmasa@5015 2928
jmasa@5015 2929
coleenp@4037 2930 // Metaspace methods
coleenp@4037 2931
coleenp@4037 2932 size_t Metaspace::_first_chunk_word_size = 0;
jmasa@4382 2933 size_t Metaspace::_first_class_chunk_word_size = 0;
jmasa@4382 2934
stefank@5863 2935 size_t Metaspace::_commit_alignment = 0;
stefank@5863 2936 size_t Metaspace::_reserve_alignment = 0;
stefank@5863 2937
jmasa@4382 2938 Metaspace::Metaspace(Mutex* lock, MetaspaceType type) {
jmasa@4382 2939 initialize(lock, type);
coleenp@4037 2940 }
coleenp@4037 2941
coleenp@4037 2942 Metaspace::~Metaspace() {
coleenp@4037 2943 delete _vsm;
hseigel@5528 2944 if (using_class_space()) {
hseigel@5528 2945 delete _class_vsm;
hseigel@5528 2946 }
coleenp@4037 2947 }
coleenp@4037 2948
coleenp@4037 2949 VirtualSpaceList* Metaspace::_space_list = NULL;
coleenp@4037 2950 VirtualSpaceList* Metaspace::_class_space_list = NULL;
coleenp@4037 2951
stefank@5771 2952 ChunkManager* Metaspace::_chunk_manager_metadata = NULL;
stefank@5771 2953 ChunkManager* Metaspace::_chunk_manager_class = NULL;
stefank@5771 2954
coleenp@4037 2955 #define VIRTUALSPACEMULTIPLIER 2
coleenp@4037 2956
hseigel@5528 2957 #ifdef _LP64
coleenp@6029 2958 static const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1);
coleenp@6029 2959
hseigel@5528 2960 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
hseigel@5528 2961 // Figure out the narrow_klass_base and the narrow_klass_shift. The
hseigel@5528 2962 // narrow_klass_base is the lower of the metaspace base and the cds base
hseigel@5528 2963 // (if cds is enabled). The narrow_klass_shift depends on the distance
hseigel@5528 2964 // between the lower base and higher address.
hseigel@5528 2965 address lower_base;
hseigel@5528 2966 address higher_address;
iklam@7089 2967 #if INCLUDE_CDS
hseigel@5528 2968 if (UseSharedSpaces) {
hseigel@5528 2969 higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
coleenp@6029 2970 (address)(metaspace_base + compressed_class_space_size()));
hseigel@5528 2971 lower_base = MIN2(metaspace_base, cds_base);
iklam@7089 2972 } else
iklam@7089 2973 #endif
iklam@7089 2974 {
coleenp@6029 2975 higher_address = metaspace_base + compressed_class_space_size();
hseigel@5528 2976 lower_base = metaspace_base;
coleenp@6029 2977
coleenp@6029 2978 uint64_t klass_encoding_max = UnscaledClassSpaceMax << LogKlassAlignmentInBytes;
coleenp@6029 2979 // If compressed class space fits in lower 32G, we don't need a base.
coleenp@6029 2980 if (higher_address <= (address)klass_encoding_max) {
coleenp@6029 2981 lower_base = 0; // effectively lower base is zero.
coleenp@6029 2982 }
hseigel@5528 2983 }
coleenp@6029 2984
hseigel@5528 2985 Universe::set_narrow_klass_base(lower_base);
coleenp@6029 2986
coleenp@6062 2987 if ((uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax) {
hseigel@5528 2988 Universe::set_narrow_klass_shift(0);
hseigel@5528 2989 } else {
hseigel@5528 2990 assert(!UseSharedSpaces, "Cannot shift with UseSharedSpaces");
hseigel@5528 2991 Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
hseigel@5528 2992 }
hseigel@5528 2993 }
hseigel@5528 2994
iklam@7089 2995 #if INCLUDE_CDS
hseigel@5528 2996 // Return TRUE if the specified metaspace_base and cds_base are close enough
hseigel@5528 2997 // to work with compressed klass pointers.
hseigel@5528 2998 bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) {
hseigel@5528 2999 assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS");
ehelin@5694 3000 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
hseigel@5528 3001 address lower_base = MIN2((address)metaspace_base, cds_base);
hseigel@5528 3002 address higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
coleenp@6029 3003 (address)(metaspace_base + compressed_class_space_size()));
coleenp@6062 3004 return ((uint64_t)(higher_address - lower_base) <= UnscaledClassSpaceMax);
hseigel@5528 3005 }
iklam@7089 3006 #endif
hseigel@5528 3007
hseigel@5528 3008 // Try to allocate the metaspace at the requested addr.
hseigel@5528 3009 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) {
hseigel@5528 3010 assert(using_class_space(), "called improperly");
ehelin@5694 3011 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
coleenp@6029 3012 assert(compressed_class_space_size() < KlassEncodingMetaspaceMax,
hseigel@5528 3013 "Metaspace size is too big");
coleenp@6029 3014 assert_is_ptr_aligned(requested_addr, _reserve_alignment);
coleenp@6029 3015 assert_is_ptr_aligned(cds_base, _reserve_alignment);
coleenp@6029 3016 assert_is_size_aligned(compressed_class_space_size(), _reserve_alignment);
stefank@5863 3017
stefank@5863 3018 // Don't use large pages for the class space.
stefank@5863 3019 bool large_pages = false;
hseigel@5528 3020
coleenp@6029 3021 ReservedSpace metaspace_rs = ReservedSpace(compressed_class_space_size(),
stefank@5863 3022 _reserve_alignment,
stefank@5863 3023 large_pages,
stefank@5863 3024 requested_addr, 0);
hseigel@5528 3025 if (!metaspace_rs.is_reserved()) {
iklam@7089 3026 #if INCLUDE_CDS
hseigel@5528 3027 if (UseSharedSpaces) {
stefank@5863 3028 size_t increment = align_size_up(1*G, _reserve_alignment);
stefank@5863 3029
hseigel@5528 3030 // Keep trying to allocate the metaspace, increasing the requested_addr
hseigel@5528 3031 // by 1GB each time, until we reach an address that will no longer allow
hseigel@5528 3032 // use of CDS with compressed klass pointers.
hseigel@5528 3033 char *addr = requested_addr;
stefank@5863 3034 while (!metaspace_rs.is_reserved() && (addr + increment > addr) &&
stefank@5863 3035 can_use_cds_with_metaspace_addr(addr + increment, cds_base)) {
stefank@5863 3036 addr = addr + increment;
coleenp@6029 3037 metaspace_rs = ReservedSpace(compressed_class_space_size(),
stefank@5863 3038 _reserve_alignment, large_pages, addr, 0);
hseigel@5528 3039 }
hseigel@5528 3040 }
iklam@7089 3041 #endif
hseigel@5528 3042 // If no successful allocation then try to allocate the space anywhere. If
hseigel@5528 3043 // that fails then OOM doom. At this point we cannot try allocating the
ehelin@5694 3044 // metaspace as if UseCompressedClassPointers is off because too much
ehelin@5694 3045 // initialization has happened that depends on UseCompressedClassPointers.
ehelin@5694 3046 // So, UseCompressedClassPointers cannot be turned off at this point.
hseigel@5528 3047 if (!metaspace_rs.is_reserved()) {
coleenp@6029 3048 metaspace_rs = ReservedSpace(compressed_class_space_size(),
stefank@5863 3049 _reserve_alignment, large_pages);
hseigel@5528 3050 if (!metaspace_rs.is_reserved()) {
hseigel@5528 3051 vm_exit_during_initialization(err_msg("Could not allocate metaspace: %d bytes",
coleenp@6029 3052 compressed_class_space_size()));
hseigel@5528 3053 }
hseigel@5528 3054 }
hseigel@5528 3055 }
hseigel@5528 3056
hseigel@5528 3057 // If we got here then the metaspace got allocated.
hseigel@5528 3058 MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass);
hseigel@5528 3059
iklam@7089 3060 #if INCLUDE_CDS
hseigel@5528 3061 // Verify that we can use shared spaces. Otherwise, turn off CDS.
hseigel@5528 3062 if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) {
hseigel@5528 3063 FileMapInfo::stop_sharing_and_unmap(
hseigel@5528 3064 "Could not allocate metaspace at a compatible address");
hseigel@5528 3065 }
iklam@7089 3066 #endif
hseigel@5528 3067 set_narrow_klass_base_and_shift((address)metaspace_rs.base(),
hseigel@5528 3068 UseSharedSpaces ? (address)cds_base : 0);
hseigel@5528 3069
hseigel@5528 3070 initialize_class_space(metaspace_rs);
hseigel@5528 3071
hseigel@5528 3072 if (PrintCompressedOopsMode || (PrintMiscellaneous && Verbose)) {
hseigel@5528 3073 gclog_or_tty->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: " SIZE_FORMAT,
hseigel@5528 3074 Universe::narrow_klass_base(), Universe::narrow_klass_shift());
coleenp@6029 3075 gclog_or_tty->print_cr("Compressed class space size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT,
coleenp@6029 3076 compressed_class_space_size(), metaspace_rs.base(), requested_addr);
hseigel@5528 3077 }
hseigel@5528 3078 }
hseigel@5528 3079
ehelin@5694 3080 // For UseCompressedClassPointers the class space is reserved above the top of
hseigel@5528 3081 // the Java heap. The argument passed in is at the base of the compressed space.
hseigel@5528 3082 void Metaspace::initialize_class_space(ReservedSpace rs) {
hseigel@5528 3083 // The reserved space size may be bigger because of alignment, esp with UseLargePages
ehelin@5694 3084 assert(rs.size() >= CompressedClassSpaceSize,
ehelin@5694 3085 err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), CompressedClassSpaceSize));
hseigel@5528 3086 assert(using_class_space(), "Must be using class space");
hseigel@5528 3087 _class_space_list = new VirtualSpaceList(rs);
stefank@5771 3088 _chunk_manager_class = new ChunkManager(SpecializedChunk, ClassSmallChunk, ClassMediumChunk);
stefank@5863 3089
stefank@5863 3090 if (!_class_space_list->initialization_succeeded()) {
stefank@5863 3091 vm_exit_during_initialization("Failed to setup compressed class space virtual space list.");
stefank@5863 3092 }
hseigel@5528 3093 }
hseigel@5528 3094
hseigel@5528 3095 #endif
hseigel@5528 3096
stefank@5863 3097 void Metaspace::ergo_initialize() {
stefank@5863 3098 if (DumpSharedSpaces) {
stefank@5863 3099 // Using large pages when dumping the shared archive is currently not implemented.
stefank@5863 3100 FLAG_SET_ERGO(bool, UseLargePagesInMetaspace, false);
stefank@5863 3101 }
stefank@5863 3102
stefank@5863 3103 size_t page_size = os::vm_page_size();
stefank@5863 3104 if (UseLargePages && UseLargePagesInMetaspace) {
stefank@5863 3105 page_size = os::large_page_size();
stefank@5863 3106 }
stefank@5863 3107
stefank@5863 3108 _commit_alignment = page_size;
stefank@5863 3109 _reserve_alignment = MAX2(page_size, (size_t)os::vm_allocation_granularity());
stefank@5863 3110
stefank@5863 3111 // Do not use FLAG_SET_ERGO to update MaxMetaspaceSize, since this will
stefank@5863 3112 // override if MaxMetaspaceSize was set on the command line or not.
stefank@5863 3113 // This information is needed later to conform to the specification of the
stefank@5863 3114 // java.lang.management.MemoryUsage API.
stefank@5863 3115 //
stefank@5863 3116 // Ideally, we would be able to set the default value of MaxMetaspaceSize in
stefank@5863 3117 // globals.hpp to the aligned value, but this is not possible, since the
stefank@5863 3118 // alignment depends on other flags being parsed.
jwilhelm@6083 3119 MaxMetaspaceSize = align_size_down_bounded(MaxMetaspaceSize, _reserve_alignment);
stefank@5863 3120
stefank@5863 3121 if (MetaspaceSize > MaxMetaspaceSize) {
stefank@5863 3122 MetaspaceSize = MaxMetaspaceSize;
stefank@5863 3123 }
stefank@5863 3124
jwilhelm@6083 3125 MetaspaceSize = align_size_down_bounded(MetaspaceSize, _commit_alignment);
stefank@5863 3126
stefank@5863 3127 assert(MetaspaceSize <= MaxMetaspaceSize, "MetaspaceSize should be limited by MaxMetaspaceSize");
stefank@5863 3128
stefank@5863 3129 if (MetaspaceSize < 256*K) {
stefank@5863 3130 vm_exit_during_initialization("Too small initial Metaspace size");
stefank@5863 3131 }
stefank@5863 3132
jwilhelm@6083 3133 MinMetaspaceExpansion = align_size_down_bounded(MinMetaspaceExpansion, _commit_alignment);
jwilhelm@6083 3134 MaxMetaspaceExpansion = align_size_down_bounded(MaxMetaspaceExpansion, _commit_alignment);
jwilhelm@6083 3135
jwilhelm@6083 3136 CompressedClassSpaceSize = align_size_down_bounded(CompressedClassSpaceSize, _reserve_alignment);
coleenp@6029 3137 set_compressed_class_space_size(CompressedClassSpaceSize);
stefank@5863 3138 }
stefank@5863 3139
coleenp@4037 3140 void Metaspace::global_initialize() {
ehelin@6722 3141 MetaspaceGC::initialize();
ehelin@6722 3142
coleenp@4037 3143 // Initialize the alignment for shared spaces.
minqi@7297 3144 int max_alignment = os::vm_allocation_granularity();
hseigel@5528 3145 size_t cds_total = 0;
hseigel@5528 3146
coleenp@4037 3147 MetaspaceShared::set_max_alignment(max_alignment);
coleenp@4037 3148
coleenp@4037 3149 if (DumpSharedSpaces) {
iklam@7089 3150 #if INCLUDE_CDS
ccheung@7103 3151 MetaspaceShared::estimate_regions_size();
ccheung@7103 3152
stefank@5863 3153 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment);
coleenp@4037 3154 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment);
stefank@5863 3155 SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment);
stefank@5863 3156 SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment);
coleenp@4037 3157
ccheung@7300 3158 // the min_misc_code_size estimate is based on MetaspaceShared::generate_vtable_methods()
ccheung@7300 3159 uintx min_misc_code_size = align_size_up(
ccheung@7300 3160 (MetaspaceShared::num_virtuals * MetaspaceShared::vtbl_list_size) *
ccheung@7300 3161 (sizeof(void*) + MetaspaceShared::vtbl_method_size) + MetaspaceShared::vtbl_common_code_size,
ccheung@7300 3162 max_alignment);
ccheung@7300 3163
ccheung@7300 3164 if (SharedMiscCodeSize < min_misc_code_size) {
ccheung@7300 3165 report_out_of_shared_space(SharedMiscCode);
ccheung@7300 3166 }
ccheung@7300 3167
coleenp@4037 3168 // Initialize with the sum of the shared space sizes. The read-only
coleenp@4037 3169 // and read write metaspace chunks will be allocated out of this and the
coleenp@4037 3170 // remainder is the misc code and data chunks.
hseigel@5528 3171 cds_total = FileMapInfo::shared_spaces_size();
stefank@5863 3172 cds_total = align_size_up(cds_total, _reserve_alignment);
hseigel@5528 3173 _space_list = new VirtualSpaceList(cds_total/wordSize);
stefank@5771 3174 _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk);
hseigel@5528 3175
stefank@5863 3176 if (!_space_list->initialization_succeeded()) {
stefank@5863 3177 vm_exit_during_initialization("Unable to dump shared archive.", NULL);
stefank@5863 3178 }
stefank@5863 3179
hseigel@5528 3180 #ifdef _LP64
coleenp@6029 3181 if (cds_total + compressed_class_space_size() > UnscaledClassSpaceMax) {
stefank@5863 3182 vm_exit_during_initialization("Unable to dump shared archive.",
stefank@5863 3183 err_msg("Size of archive (" SIZE_FORMAT ") + compressed class space ("
stefank@5863 3184 SIZE_FORMAT ") == total (" SIZE_FORMAT ") is larger than compressed "
coleenp@6029 3185 "klass limit: " SIZE_FORMAT, cds_total, compressed_class_space_size(),
coleenp@6029 3186 cds_total + compressed_class_space_size(), UnscaledClassSpaceMax));
stefank@5863 3187 }
stefank@5863 3188
hseigel@5528 3189 // Set the compressed klass pointer base so that decoding of these pointers works
hseigel@5528 3190 // properly when creating the shared archive.
ehelin@5694 3191 assert(UseCompressedOops && UseCompressedClassPointers,
ehelin@5694 3192 "UseCompressedOops and UseCompressedClassPointers must be set");
hseigel@5528 3193 Universe::set_narrow_klass_base((address)_space_list->current_virtual_space()->bottom());
hseigel@5528 3194 if (TraceMetavirtualspaceAllocation && Verbose) {
hseigel@5528 3195 gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT,
hseigel@5528 3196 _space_list->current_virtual_space()->bottom());
hseigel@5528 3197 }
hseigel@5528 3198
hseigel@5528 3199 Universe::set_narrow_klass_shift(0);
iklam@7089 3200 #endif // _LP64
iklam@7089 3201 #endif // INCLUDE_CDS
coleenp@4037 3202 } else {
iklam@7089 3203 #if INCLUDE_CDS
coleenp@4037 3204 // If using shared space, open the file that contains the shared space
coleenp@4037 3205 // and map in the memory before initializing the rest of metaspace (so
coleenp@4037 3206 // the addresses don't conflict)
hseigel@5528 3207 address cds_address = NULL;
coleenp@4037 3208 if (UseSharedSpaces) {
coleenp@4037 3209 FileMapInfo* mapinfo = new FileMapInfo();
coleenp@4037 3210
coleenp@4037 3211 // Open the shared archive file, read and validate the header. If
coleenp@4037 3212 // initialization fails, shared spaces [UseSharedSpaces] are
coleenp@4037 3213 // disabled and the file is closed.
coleenp@4037 3214 // Map in spaces now also
coleenp@4037 3215 if (mapinfo->initialize() && MetaspaceShared::map_shared_spaces(mapinfo)) {
stefank@5863 3216 cds_total = FileMapInfo::shared_spaces_size();
stefank@5863 3217 cds_address = (address)mapinfo->region_base(0);
coleenp@4037 3218 } else {
coleenp@4037 3219 assert(!mapinfo->is_open() && !UseSharedSpaces,
coleenp@4037 3220 "archive file not closed or shared spaces not disabled.");
coleenp@4037 3221 }
coleenp@4037 3222 }
iklam@7089 3223 #endif // INCLUDE_CDS
hseigel@5528 3224 #ifdef _LP64
ehelin@5694 3225 // If UseCompressedClassPointers is set then allocate the metaspace area
hseigel@5528 3226 // above the heap and above the CDS area (if it exists).
hseigel@5528 3227 if (using_class_space()) {
hseigel@5528 3228 if (UseSharedSpaces) {
iklam@7089 3229 #if INCLUDE_CDS
stefank@5863 3230 char* cds_end = (char*)(cds_address + cds_total);
stefank@5863 3231 cds_end = (char *)align_ptr_up(cds_end, _reserve_alignment);
stefank@5863 3232 allocate_metaspace_compressed_klass_ptrs(cds_end, cds_address);
iklam@7089 3233 #endif
hseigel@5528 3234 } else {
coleenp@6029 3235 char* base = (char*)align_ptr_up(Universe::heap()->reserved_region().end(), _reserve_alignment);
coleenp@6029 3236 allocate_metaspace_compressed_klass_ptrs(base, 0);
hseigel@5528 3237 }
hseigel@5528 3238 }
iklam@7089 3239 #endif // _LP64
hseigel@5528 3240
jmasa@4382 3241 // Initialize these before initializing the VirtualSpaceList
coleenp@4037 3242 _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
jmasa@4382 3243 _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
jmasa@4382 3244 // Make the first class chunk bigger than a medium chunk so it's not put
jmasa@4382 3245 // on the medium chunk list. The next chunk will be small and progress
jmasa@4382 3246 // from there. This size calculated by -version.
jmasa@4382 3247 _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6,
ehelin@5694 3248 (CompressedClassSpaceSize/BytesPerWord)*2);
jmasa@4382 3249 _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size);
coleenp@4037 3250 // Arbitrarily set the initial virtual space to a multiple
coleenp@4037 3251 // of the boot class loader size.
stefank@5863 3252 size_t word_size = VIRTUALSPACEMULTIPLIER * _first_chunk_word_size;
stefank@5863 3253 word_size = align_size_up(word_size, Metaspace::reserve_alignment_words());
stefank@5863 3254
coleenp@4037 3255 // Initialize the list of virtual spaces.
coleenp@4037 3256 _space_list = new VirtualSpaceList(word_size);
stefank@5771 3257 _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk);
stefank@5863 3258
stefank@5863 3259 if (!_space_list->initialization_succeeded()) {
stefank@5863 3260 vm_exit_during_initialization("Unable to setup metadata virtual space list.", NULL);
stefank@5863 3261 }
coleenp@4037 3262 }
stefank@5863 3263
ehelin@6417 3264 _tracer = new MetaspaceTracer();
coleenp@4037 3265 }
coleenp@4037 3266
ehelin@6722 3267 void Metaspace::post_initialize() {
ehelin@6722 3268 MetaspaceGC::post_initialize();
ehelin@6722 3269 }
ehelin@6722 3270
stefank@5771 3271 Metachunk* Metaspace::get_initialization_chunk(MetadataType mdtype,
stefank@5771 3272 size_t chunk_word_size,
stefank@5771 3273 size_t chunk_bunch) {
stefank@5771 3274 // Get a chunk from the chunk freelist
stefank@5771 3275 Metachunk* chunk = get_chunk_manager(mdtype)->chunk_freelist_allocate(chunk_word_size);
stefank@5771 3276 if (chunk != NULL) {
stefank@5771 3277 return chunk;
stefank@5771 3278 }
stefank@5771 3279
stefank@5863 3280 return get_space_list(mdtype)->get_new_chunk(chunk_word_size, chunk_word_size, chunk_bunch);
stefank@5771 3281 }
stefank@5771 3282
hseigel@5528 3283 void Metaspace::initialize(Mutex* lock, MetaspaceType type) {
coleenp@4037 3284
coleenp@4037 3285 assert(space_list() != NULL,
coleenp@4037 3286 "Metadata VirtualSpaceList has not been initialized");
stefank@5771 3287 assert(chunk_manager_metadata() != NULL,
stefank@5771 3288 "Metadata ChunkManager has not been initialized");
stefank@5771 3289
stefank@5771 3290 _vsm = new SpaceManager(NonClassType, lock);
coleenp@4037 3291 if (_vsm == NULL) {
coleenp@4037 3292 return;
coleenp@4037 3293 }
jmasa@4382 3294 size_t word_size;
jmasa@4382 3295 size_t class_word_size;
hseigel@5528 3296 vsm()->get_initial_chunk_sizes(type, &word_size, &class_word_size);
hseigel@5528 3297
hseigel@5528 3298 if (using_class_space()) {
stefank@5771 3299 assert(class_space_list() != NULL,
stefank@5771 3300 "Class VirtualSpaceList has not been initialized");
stefank@5771 3301 assert(chunk_manager_class() != NULL,
stefank@5771 3302 "Class ChunkManager has not been initialized");
hseigel@5528 3303
hseigel@5528 3304 // Allocate SpaceManager for classes.
stefank@5771 3305 _class_vsm = new SpaceManager(ClassType, lock);
hseigel@5528 3306 if (_class_vsm == NULL) {
hseigel@5528 3307 return;
hseigel@5528 3308 }
coleenp@4037 3309 }
coleenp@4037 3310
coleenp@4037 3311 MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 3312
coleenp@4037 3313 // Allocate chunk for metadata objects
stefank@5771 3314 Metachunk* new_chunk = get_initialization_chunk(NonClassType,
stefank@5771 3315 word_size,
stefank@5771 3316 vsm()->medium_chunk_bunch());
coleenp@4037 3317 assert(!DumpSharedSpaces || new_chunk != NULL, "should have enough space for both chunks");
coleenp@4037 3318 if (new_chunk != NULL) {
coleenp@4037 3319 // Add to this manager's list of chunks in use and current_chunk().
coleenp@4037 3320 vsm()->add_chunk(new_chunk, true);
coleenp@4037 3321 }
coleenp@4037 3322
coleenp@4037 3323 // Allocate chunk for class metadata objects
hseigel@5528 3324 if (using_class_space()) {
stefank@5771 3325 Metachunk* class_chunk = get_initialization_chunk(ClassType,
stefank@5771 3326 class_word_size,
stefank@5771 3327 class_vsm()->medium_chunk_bunch());
hseigel@5528 3328 if (class_chunk != NULL) {
hseigel@5528 3329 class_vsm()->add_chunk(class_chunk, true);
hseigel@5528 3330 }
coleenp@4037 3331 }
iklam@5208 3332
iklam@5208 3333 _alloc_record_head = NULL;
iklam@5208 3334 _alloc_record_tail = NULL;
coleenp@4037 3335 }
coleenp@4037 3336
jmasa@4382 3337 size_t Metaspace::align_word_size_up(size_t word_size) {
jmasa@4382 3338 size_t byte_size = word_size * wordSize;
jmasa@4382 3339 return ReservedSpace::allocation_align_size_up(byte_size) / wordSize;
jmasa@4382 3340 }
jmasa@4382 3341
coleenp@4037 3342 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) {
coleenp@4037 3343 // DumpSharedSpaces doesn't use class metadata area (yet)
ehelin@5694 3344 // Also, don't use class_vsm() unless UseCompressedClassPointers is true.
mgerdin@5808 3345 if (is_class_space_allocation(mdtype)) {
jmasa@4196 3346 return class_vsm()->allocate(word_size);
coleenp@4037 3347 } else {
jmasa@4196 3348 return vsm()->allocate(word_size);
coleenp@4037 3349 }
coleenp@4037 3350 }
coleenp@4037 3351
jmasa@4064 3352 MetaWord* Metaspace::expand_and_allocate(size_t word_size, MetadataType mdtype) {
stefank@5863 3353 size_t delta_bytes = MetaspaceGC::delta_capacity_until_GC(word_size * BytesPerWord);
stefank@5863 3354 assert(delta_bytes > 0, "Must be");
stefank@5863 3355
ehelin@7254 3356 size_t before = 0;
ehelin@7254 3357 size_t after = 0;
ehelin@7254 3358 MetaWord* res;
ehelin@7254 3359 bool incremented;
ehelin@7254 3360
ehelin@7254 3361 // Each thread increments the HWM at most once. Even if the thread fails to increment
ehelin@7254 3362 // the HWM, an allocation is still attempted. This is because another thread must then
ehelin@7254 3363 // have incremented the HWM and therefore the allocation might still succeed.
ehelin@7254 3364 do {
ehelin@7254 3365 incremented = MetaspaceGC::inc_capacity_until_GC(delta_bytes, &after, &before);
ehelin@7254 3366 res = allocate(word_size, mdtype);
ehelin@7254 3367 } while (!incremented && res == NULL);
ehelin@7254 3368
ehelin@7254 3369 if (incremented) {
ehelin@7254 3370 tracer()->report_gc_threshold(before, after,
ehelin@7254 3371 MetaspaceGCThresholdUpdater::ExpandAndAllocate);
ehelin@7254 3372 if (PrintGCDetails && Verbose) {
ehelin@7254 3373 gclog_or_tty->print_cr("Increase capacity to GC from " SIZE_FORMAT
ehelin@7254 3374 " to " SIZE_FORMAT, before, after);
ehelin@7254 3375 }
jmasa@4064 3376 }
jmasa@4196 3377
ehelin@7254 3378 return res;
jmasa@4064 3379 }
jmasa@4064 3380
coleenp@4037 3381 // Space allocated in the Metaspace. This may
coleenp@4037 3382 // be across several metadata virtual spaces.
coleenp@4037 3383 char* Metaspace::bottom() const {
coleenp@4037 3384 assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces");
coleenp@4037 3385 return (char*)vsm()->current_chunk()->bottom();
coleenp@4037 3386 }
coleenp@4037 3387
jmasa@5015 3388 size_t Metaspace::used_words_slow(MetadataType mdtype) const {
hseigel@5528 3389 if (mdtype == ClassType) {
hseigel@5528 3390 return using_class_space() ? class_vsm()->sum_used_in_chunks_in_use() : 0;
hseigel@5528 3391 } else {
hseigel@5528 3392 return vsm()->sum_used_in_chunks_in_use(); // includes overhead!
hseigel@5528 3393 }
coleenp@4037 3394 }
coleenp@4037 3395
ehelin@5703 3396 size_t Metaspace::free_words_slow(MetadataType mdtype) const {
hseigel@5528 3397 if (mdtype == ClassType) {
hseigel@5528 3398 return using_class_space() ? class_vsm()->sum_free_in_chunks_in_use() : 0;
hseigel@5528 3399 } else {
hseigel@5528 3400 return vsm()->sum_free_in_chunks_in_use();
hseigel@5528 3401 }
coleenp@4037 3402 }
coleenp@4037 3403
coleenp@4037 3404 // Space capacity in the Metaspace. It includes
coleenp@4037 3405 // space in the list of chunks from which allocations
coleenp@4037 3406 // have been made. Don't include space in the global freelist and
coleenp@4037 3407 // in the space available in the dictionary which
coleenp@4037 3408 // is already counted in some chunk.
jmasa@5015 3409 size_t Metaspace::capacity_words_slow(MetadataType mdtype) const {
hseigel@5528 3410 if (mdtype == ClassType) {
hseigel@5528 3411 return using_class_space() ? class_vsm()->sum_capacity_in_chunks_in_use() : 0;
hseigel@5528 3412 } else {
hseigel@5528 3413 return vsm()->sum_capacity_in_chunks_in_use();
hseigel@5528 3414 }
coleenp@4037 3415 }
coleenp@4037 3416
jmasa@5015 3417 size_t Metaspace::used_bytes_slow(MetadataType mdtype) const {
jmasa@5015 3418 return used_words_slow(mdtype) * BytesPerWord;
jmasa@5015 3419 }
jmasa@5015 3420
jmasa@5015 3421 size_t Metaspace::capacity_bytes_slow(MetadataType mdtype) const {
jmasa@5015 3422 return capacity_words_slow(mdtype) * BytesPerWord;
jmasa@5015 3423 }
jmasa@5015 3424
coleenp@4037 3425 void Metaspace::deallocate(MetaWord* ptr, size_t word_size, bool is_class) {
coleenp@4037 3426 if (SafepointSynchronize::is_at_safepoint()) {
iklam@7089 3427 if (DumpSharedSpaces && PrintSharedSpaces) {
iklam@7089 3428 record_deallocation(ptr, vsm()->get_raw_word_size(word_size));
iklam@7089 3429 }
iklam@7089 3430
coleenp@4037 3431 assert(Thread::current()->is_VM_thread(), "should be the VM thread");
jmasa@4196 3432 // Don't take Heap_lock
mgerdin@5023 3433 MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag);
goetz@6337 3434 if (word_size < TreeChunk<Metablock, FreeList<Metablock> >::min_size()) {
jmasa@4196 3435 // Dark matter. Too small for dictionary.
jmasa@4196 3436 #ifdef ASSERT
jmasa@4196 3437 Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
jmasa@4196 3438 #endif
jmasa@4196 3439 return;
jmasa@4196 3440 }
hseigel@5528 3441 if (is_class && using_class_space()) {
hseigel@5528 3442 class_vsm()->deallocate(ptr, word_size);
coleenp@4037 3443 } else {
jmasa@4196 3444 vsm()->deallocate(ptr, word_size);
coleenp@4037 3445 }
coleenp@4037 3446 } else {
mgerdin@5023 3447 MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag);
coleenp@4037 3448
goetz@6337 3449 if (word_size < TreeChunk<Metablock, FreeList<Metablock> >::min_size()) {
jmasa@4196 3450 // Dark matter. Too small for dictionary.
jmasa@4196 3451 #ifdef ASSERT
jmasa@4196 3452 Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
jmasa@4196 3453 #endif
jmasa@4196 3454 return;
jmasa@4196 3455 }
hseigel@5528 3456 if (is_class && using_class_space()) {
jmasa@4196 3457 class_vsm()->deallocate(ptr, word_size);
coleenp@4037 3458 } else {
jmasa@4196 3459 vsm()->deallocate(ptr, word_size);
coleenp@4037 3460 }
coleenp@4037 3461 }
coleenp@4037 3462 }
coleenp@4037 3463
stefank@5863 3464
stefank@5941 3465 MetaWord* Metaspace::allocate(ClassLoaderData* loader_data, size_t word_size,
iklam@5208 3466 bool read_only, MetaspaceObj::Type type, TRAPS) {
coleenp@4037 3467 if (HAS_PENDING_EXCEPTION) {
coleenp@4037 3468 assert(false, "Should not allocate with exception pending");
coleenp@4037 3469 return NULL; // caller does a CHECK_NULL too
coleenp@4037 3470 }
coleenp@4037 3471
coleenp@4037 3472 assert(loader_data != NULL, "Should never pass around a NULL loader_data. "
coleenp@4037 3473 "ClassLoaderData::the_null_class_loader_data() should have been used.");
stefank@5863 3474
coleenp@4037 3475 // Allocate in metaspaces without taking out a lock, because it deadlocks
coleenp@4037 3476 // with the SymbolTable_lock. Dumping is single threaded for now. We'll have
coleenp@4037 3477 // to revisit this for application class data sharing.
coleenp@4037 3478 if (DumpSharedSpaces) {
iklam@5208 3479 assert(type > MetaspaceObj::UnknownType && type < MetaspaceObj::_number_of_types, "sanity");
iklam@5208 3480 Metaspace* space = read_only ? loader_data->ro_metaspace() : loader_data->rw_metaspace();
stefank@5863 3481 MetaWord* result = space->allocate(word_size, NonClassType);
coleenp@4037 3482 if (result == NULL) {
coleenp@4037 3483 report_out_of_shared_space(read_only ? SharedReadOnly : SharedReadWrite);
coleenp@4037 3484 }
iklam@7089 3485 if (PrintSharedSpaces) {
iklam@7089 3486 space->record_allocation(result, type, space->vsm()->get_raw_word_size(word_size));
iklam@7089 3487 }
stefank@5941 3488
stefank@5941 3489 // Zero initialize.
stefank@5941 3490 Copy::fill_to_aligned_words((HeapWord*)result, word_size, 0);
stefank@5941 3491
stefank@5941 3492 return result;
coleenp@4037 3493 }
coleenp@4037 3494
stefank@5863 3495 MetadataType mdtype = (type == MetaspaceObj::ClassType) ? ClassType : NonClassType;
stefank@5863 3496
stefank@5863 3497 // Try to allocate metadata.
stefank@5863 3498 MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
coleenp@4037 3499
coleenp@4037 3500 if (result == NULL) {
ehelin@6418 3501 tracer()->report_metaspace_allocation_failure(loader_data, word_size, type, mdtype);
ehelin@6418 3502
stefank@5863 3503 // Allocation failed.
stefank@5863 3504 if (is_init_completed()) {
stefank@5863 3505 // Only start a GC if the bootstrapping has completed.
stefank@5863 3506
stefank@5863 3507 // Try to clean out some memory and retry.
stefank@5863 3508 result = Universe::heap()->collector_policy()->satisfy_failed_metadata_allocation(
stefank@5863 3509 loader_data, word_size, mdtype);
coleenp@4037 3510 }
coleenp@4037 3511 }
stefank@5863 3512
stefank@5863 3513 if (result == NULL) {
jwilhelm@7867 3514 report_metadata_oome(loader_data, word_size, type, mdtype, CHECK_NULL);
stefank@5863 3515 }
stefank@5863 3516
stefank@5941 3517 // Zero initialize.
stefank@5941 3518 Copy::fill_to_aligned_words((HeapWord*)result, word_size, 0);
stefank@5941 3519
stefank@5941 3520 return result;
coleenp@4037 3521 }
coleenp@4037 3522
hseigel@6027 3523 size_t Metaspace::class_chunk_size(size_t word_size) {
hseigel@6027 3524 assert(using_class_space(), "Has to use class space");
hseigel@6027 3525 return class_vsm()->calc_chunk_size(word_size);
hseigel@6027 3526 }
hseigel@6027 3527
ehelin@6419 3528 void Metaspace::report_metadata_oome(ClassLoaderData* loader_data, size_t word_size, MetaspaceObj::Type type, MetadataType mdtype, TRAPS) {
ehelin@6419 3529 tracer()->report_metadata_oom(loader_data, word_size, type, mdtype);
ehelin@6419 3530
stefank@5863 3531 // If result is still null, we are out of memory.
stefank@5863 3532 if (Verbose && TraceMetadataChunkAllocation) {
stefank@5863 3533 gclog_or_tty->print_cr("Metaspace allocation failed for size "
stefank@5863 3534 SIZE_FORMAT, word_size);
stefank@5863 3535 if (loader_data->metaspace_or_null() != NULL) {
stefank@5863 3536 loader_data->dump(gclog_or_tty);
stefank@5863 3537 }
stefank@5863 3538 MetaspaceAux::dump(gclog_or_tty);
stefank@5863 3539 }
stefank@5863 3540
hseigel@6027 3541 bool out_of_compressed_class_space = false;
hseigel@6027 3542 if (is_class_space_allocation(mdtype)) {
hseigel@6027 3543 Metaspace* metaspace = loader_data->metaspace_non_null();
hseigel@6027 3544 out_of_compressed_class_space =
hseigel@6027 3545 MetaspaceAux::committed_bytes(Metaspace::ClassType) +
hseigel@6027 3546 (metaspace->class_chunk_size(word_size) * BytesPerWord) >
hseigel@6027 3547 CompressedClassSpaceSize;
hseigel@6027 3548 }
hseigel@6027 3549
stefank@5863 3550 // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
hseigel@6027 3551 const char* space_string = out_of_compressed_class_space ?
hseigel@6027 3552 "Compressed class space" : "Metaspace";
hseigel@6027 3553
stefank@5863 3554 report_java_out_of_memory(space_string);
stefank@5863 3555
stefank@5863 3556 if (JvmtiExport::should_post_resource_exhausted()) {
stefank@5863 3557 JvmtiExport::post_resource_exhausted(
stefank@5863 3558 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
stefank@5863 3559 space_string);
stefank@5863 3560 }
stefank@5863 3561
stefank@5863 3562 if (!is_init_completed()) {
stefank@5863 3563 vm_exit_during_initialization("OutOfMemoryError", space_string);
stefank@5863 3564 }
stefank@5863 3565
hseigel@6027 3566 if (out_of_compressed_class_space) {
stefank@5863 3567 THROW_OOP(Universe::out_of_memory_error_class_metaspace());
stefank@5863 3568 } else {
stefank@5863 3569 THROW_OOP(Universe::out_of_memory_error_metaspace());
stefank@5863 3570 }
stefank@5863 3571 }
stefank@5863 3572
ehelin@6418 3573 const char* Metaspace::metadata_type_name(Metaspace::MetadataType mdtype) {
ehelin@6418 3574 switch (mdtype) {
ehelin@6418 3575 case Metaspace::ClassType: return "Class";
ehelin@6418 3576 case Metaspace::NonClassType: return "Metadata";
ehelin@6418 3577 default:
ehelin@6418 3578 assert(false, err_msg("Got bad mdtype: %d", (int) mdtype));
ehelin@6418 3579 return NULL;
ehelin@6418 3580 }
ehelin@6418 3581 }
ehelin@6418 3582
iklam@5208 3583 void Metaspace::record_allocation(void* ptr, MetaspaceObj::Type type, size_t word_size) {
iklam@5208 3584 assert(DumpSharedSpaces, "sanity");
iklam@5208 3585
iklam@7089 3586 int byte_size = (int)word_size * HeapWordSize;
iklam@7089 3587 AllocRecord *rec = new AllocRecord((address)ptr, type, byte_size);
iklam@7089 3588
iklam@5208 3589 if (_alloc_record_head == NULL) {
iklam@5208 3590 _alloc_record_head = _alloc_record_tail = rec;
iklam@7089 3591 } else if (_alloc_record_tail->_ptr + _alloc_record_tail->_byte_size == (address)ptr) {
iklam@5208 3592 _alloc_record_tail->_next = rec;
iklam@5208 3593 _alloc_record_tail = rec;
iklam@7089 3594 } else {
iklam@7089 3595 // slow linear search, but this doesn't happen that often, and only when dumping
iklam@7089 3596 for (AllocRecord *old = _alloc_record_head; old; old = old->_next) {
iklam@7089 3597 if (old->_ptr == ptr) {
iklam@7089 3598 assert(old->_type == MetaspaceObj::DeallocatedType, "sanity");
iklam@7089 3599 int remain_bytes = old->_byte_size - byte_size;
iklam@7089 3600 assert(remain_bytes >= 0, "sanity");
iklam@7089 3601 old->_type = type;
iklam@7089 3602
iklam@7089 3603 if (remain_bytes == 0) {
iklam@7089 3604 delete(rec);
iklam@7089 3605 } else {
iklam@7089 3606 address remain_ptr = address(ptr) + byte_size;
iklam@7089 3607 rec->_ptr = remain_ptr;
iklam@7089 3608 rec->_byte_size = remain_bytes;
iklam@7089 3609 rec->_type = MetaspaceObj::DeallocatedType;
iklam@7089 3610 rec->_next = old->_next;
iklam@7089 3611 old->_byte_size = byte_size;
iklam@7089 3612 old->_next = rec;
iklam@7089 3613 }
iklam@7089 3614 return;
iklam@7089 3615 }
iklam@7089 3616 }
iklam@7089 3617 assert(0, "reallocating a freed pointer that was not recorded");
iklam@5208 3618 }
iklam@5208 3619 }
iklam@5208 3620
iklam@7089 3621 void Metaspace::record_deallocation(void* ptr, size_t word_size) {
iklam@7089 3622 assert(DumpSharedSpaces, "sanity");
iklam@7089 3623
iklam@7089 3624 for (AllocRecord *rec = _alloc_record_head; rec; rec = rec->_next) {
iklam@7089 3625 if (rec->_ptr == ptr) {
iklam@7089 3626 assert(rec->_byte_size == (int)word_size * HeapWordSize, "sanity");
iklam@7089 3627 rec->_type = MetaspaceObj::DeallocatedType;
iklam@7089 3628 return;
iklam@7089 3629 }
iklam@7089 3630 }
iklam@7089 3631
iklam@7089 3632 assert(0, "deallocating a pointer that was not recorded");
iklam@7089 3633 }
iklam@7089 3634
iklam@5208 3635 void Metaspace::iterate(Metaspace::AllocRecordClosure *closure) {
iklam@5208 3636 assert(DumpSharedSpaces, "unimplemented for !DumpSharedSpaces");
iklam@5208 3637
iklam@5208 3638 address last_addr = (address)bottom();
iklam@5208 3639
iklam@5208 3640 for (AllocRecord *rec = _alloc_record_head; rec; rec = rec->_next) {
iklam@5208 3641 address ptr = rec->_ptr;
iklam@5208 3642 if (last_addr < ptr) {
iklam@5208 3643 closure->doit(last_addr, MetaspaceObj::UnknownType, ptr - last_addr);
iklam@5208 3644 }
iklam@5208 3645 closure->doit(ptr, rec->_type, rec->_byte_size);
iklam@5208 3646 last_addr = ptr + rec->_byte_size;
iklam@5208 3647 }
iklam@5208 3648
iklam@5208 3649 address top = ((address)bottom()) + used_bytes_slow(Metaspace::NonClassType);
iklam@5208 3650 if (last_addr < top) {
iklam@5208 3651 closure->doit(last_addr, MetaspaceObj::UnknownType, top - last_addr);
iklam@5208 3652 }
iklam@5208 3653 }
iklam@5208 3654
stefank@5771 3655 void Metaspace::purge(MetadataType mdtype) {
stefank@5771 3656 get_space_list(mdtype)->purge(get_chunk_manager(mdtype));
stefank@5771 3657 }
stefank@5771 3658
jmasa@5007 3659 void Metaspace::purge() {
jmasa@5007 3660 MutexLockerEx cl(SpaceManager::expand_lock(),
jmasa@5007 3661 Mutex::_no_safepoint_check_flag);
stefank@5771 3662 purge(NonClassType);
hseigel@5528 3663 if (using_class_space()) {
stefank@5771 3664 purge(ClassType);
hseigel@5528 3665 }
jmasa@5007 3666 }
jmasa@5007 3667
coleenp@4037 3668 void Metaspace::print_on(outputStream* out) const {
coleenp@4037 3669 // Print both class virtual space counts and metaspace.
coleenp@4037 3670 if (Verbose) {
hseigel@5528 3671 vsm()->print_on(out);
hseigel@5528 3672 if (using_class_space()) {
coleenp@4037 3673 class_vsm()->print_on(out);
hseigel@5528 3674 }
coleenp@4037 3675 }
coleenp@4037 3676 }
coleenp@4037 3677
coleenp@6305 3678 bool Metaspace::contains(const void* ptr) {
coleenp@6678 3679 if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(ptr)) {
coleenp@6678 3680 return true;
coleenp@4037 3681 }
coleenp@6678 3682
coleenp@6678 3683 if (using_class_space() && get_space_list(ClassType)->contains(ptr)) {
coleenp@6678 3684 return true;
coleenp@6678 3685 }
coleenp@6678 3686
coleenp@6678 3687 return get_space_list(NonClassType)->contains(ptr);
coleenp@4037 3688 }
coleenp@4037 3689
coleenp@4037 3690 void Metaspace::verify() {
coleenp@4037 3691 vsm()->verify();
hseigel@5528 3692 if (using_class_space()) {
hseigel@5528 3693 class_vsm()->verify();
hseigel@5528 3694 }
coleenp@4037 3695 }
coleenp@4037 3696
coleenp@4037 3697 void Metaspace::dump(outputStream* const out) const {
coleenp@4037 3698 out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm());
coleenp@4037 3699 vsm()->dump(out);
hseigel@5528 3700 if (using_class_space()) {
hseigel@5528 3701 out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm());
hseigel@5528 3702 class_vsm()->dump(out);
hseigel@5528 3703 }
coleenp@4037 3704 }
stefank@5704 3705
stefank@5704 3706 /////////////// Unit tests ///////////////
stefank@5704 3707
stefank@5704 3708 #ifndef PRODUCT
stefank@5704 3709
brutisso@5774 3710 class TestMetaspaceAuxTest : AllStatic {
stefank@5704 3711 public:
stefank@5704 3712 static void test_reserved() {
stefank@5704 3713 size_t reserved = MetaspaceAux::reserved_bytes();
stefank@5704 3714
stefank@5704 3715 assert(reserved > 0, "assert");
stefank@5704 3716
stefank@5704 3717 size_t committed = MetaspaceAux::committed_bytes();
stefank@5704 3718 assert(committed <= reserved, "assert");
stefank@5704 3719
stefank@5704 3720 size_t reserved_metadata = MetaspaceAux::reserved_bytes(Metaspace::NonClassType);
stefank@5704 3721 assert(reserved_metadata > 0, "assert");
stefank@5704 3722 assert(reserved_metadata <= reserved, "assert");
stefank@5704 3723
stefank@5704 3724 if (UseCompressedClassPointers) {
stefank@5704 3725 size_t reserved_class = MetaspaceAux::reserved_bytes(Metaspace::ClassType);
stefank@5704 3726 assert(reserved_class > 0, "assert");
stefank@5704 3727 assert(reserved_class < reserved, "assert");
stefank@5704 3728 }
stefank@5704 3729 }
stefank@5704 3730
stefank@5704 3731 static void test_committed() {
stefank@5704 3732 size_t committed = MetaspaceAux::committed_bytes();
stefank@5704 3733
stefank@5704 3734 assert(committed > 0, "assert");
stefank@5704 3735
stefank@5704 3736 size_t reserved = MetaspaceAux::reserved_bytes();
stefank@5704 3737 assert(committed <= reserved, "assert");
stefank@5704 3738
stefank@5704 3739 size_t committed_metadata = MetaspaceAux::committed_bytes(Metaspace::NonClassType);
stefank@5704 3740 assert(committed_metadata > 0, "assert");
stefank@5704 3741 assert(committed_metadata <= committed, "assert");
stefank@5704 3742
stefank@5704 3743 if (UseCompressedClassPointers) {
stefank@5704 3744 size_t committed_class = MetaspaceAux::committed_bytes(Metaspace::ClassType);
stefank@5704 3745 assert(committed_class > 0, "assert");
stefank@5704 3746 assert(committed_class < committed, "assert");
stefank@5704 3747 }
stefank@5704 3748 }
stefank@5704 3749
brutisso@5774 3750 static void test_virtual_space_list_large_chunk() {
brutisso@5774 3751 VirtualSpaceList* vs_list = new VirtualSpaceList(os::vm_allocation_granularity());
brutisso@5774 3752 MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
brutisso@5774 3753 // A size larger than VirtualSpaceSize (256k) and add one page to make it _not_ be
brutisso@5774 3754 // vm_allocation_granularity aligned on Windows.
brutisso@5774 3755 size_t large_size = (size_t)(2*256*K + (os::vm_page_size()/BytesPerWord));
brutisso@5774 3756 large_size += (os::vm_page_size()/BytesPerWord);
brutisso@5774 3757 vs_list->get_new_chunk(large_size, large_size, 0);
brutisso@5774 3758 }
brutisso@5774 3759
stefank@5704 3760 static void test() {
stefank@5704 3761 test_reserved();
stefank@5704 3762 test_committed();
brutisso@5774 3763 test_virtual_space_list_large_chunk();
stefank@5704 3764 }
stefank@5704 3765 };
stefank@5704 3766
brutisso@5774 3767 void TestMetaspaceAux_test() {
brutisso@5774 3768 TestMetaspaceAuxTest::test();
stefank@5704 3769 }
stefank@5704 3770
mgerdin@6004 3771 class TestVirtualSpaceNodeTest {
mgerdin@6004 3772 static void chunk_up(size_t words_left, size_t& num_medium_chunks,
mgerdin@6004 3773 size_t& num_small_chunks,
mgerdin@6004 3774 size_t& num_specialized_chunks) {
mgerdin@6004 3775 num_medium_chunks = words_left / MediumChunk;
mgerdin@6004 3776 words_left = words_left % MediumChunk;
mgerdin@6004 3777
mgerdin@6004 3778 num_small_chunks = words_left / SmallChunk;
mgerdin@6004 3779 words_left = words_left % SmallChunk;
mgerdin@6004 3780 // how many specialized chunks can we get?
mgerdin@6004 3781 num_specialized_chunks = words_left / SpecializedChunk;
mgerdin@6004 3782 assert(words_left % SpecializedChunk == 0, "should be nothing left");
mgerdin@6004 3783 }
mgerdin@6004 3784
mgerdin@6004 3785 public:
mgerdin@6004 3786 static void test() {
mgerdin@6004 3787 MutexLockerEx ml(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
mgerdin@6004 3788 const size_t vsn_test_size_words = MediumChunk * 4;
mgerdin@6004 3789 const size_t vsn_test_size_bytes = vsn_test_size_words * BytesPerWord;
mgerdin@6004 3790
mgerdin@6004 3791 // The chunk sizes must be multiples of eachother, or this will fail
mgerdin@6004 3792 STATIC_ASSERT(MediumChunk % SmallChunk == 0);
mgerdin@6004 3793 STATIC_ASSERT(SmallChunk % SpecializedChunk == 0);
mgerdin@6004 3794
mgerdin@6004 3795 { // No committed memory in VSN
mgerdin@6004 3796 ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk);
mgerdin@6004 3797 VirtualSpaceNode vsn(vsn_test_size_bytes);
mgerdin@6004 3798 vsn.initialize();
mgerdin@6004 3799 vsn.retire(&cm);
mgerdin@6004 3800 assert(cm.sum_free_chunks_count() == 0, "did not commit any memory in the VSN");
mgerdin@6004 3801 }
mgerdin@6004 3802
mgerdin@6004 3803 { // All of VSN is committed, half is used by chunks
mgerdin@6004 3804 ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk);
mgerdin@6004 3805 VirtualSpaceNode vsn(vsn_test_size_bytes);
mgerdin@6004 3806 vsn.initialize();
mgerdin@6004 3807 vsn.expand_by(vsn_test_size_words, vsn_test_size_words);
mgerdin@6004 3808 vsn.get_chunk_vs(MediumChunk);
mgerdin@6004 3809 vsn.get_chunk_vs(MediumChunk);
mgerdin@6004 3810 vsn.retire(&cm);
mgerdin@6004 3811 assert(cm.sum_free_chunks_count() == 2, "should have been memory left for 2 medium chunks");
mgerdin@6004 3812 assert(cm.sum_free_chunks() == 2*MediumChunk, "sizes should add up");
mgerdin@6004 3813 }
mgerdin@6004 3814
mgerdin@6004 3815 { // 4 pages of VSN is committed, some is used by chunks
mgerdin@6004 3816 ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk);
mgerdin@6004 3817 VirtualSpaceNode vsn(vsn_test_size_bytes);
mgerdin@6004 3818 const size_t page_chunks = 4 * (size_t)os::vm_page_size() / BytesPerWord;
mgerdin@6004 3819 assert(page_chunks < MediumChunk, "Test expects medium chunks to be at least 4*page_size");
mgerdin@6004 3820 vsn.initialize();
mgerdin@6004 3821 vsn.expand_by(page_chunks, page_chunks);
mgerdin@6004 3822 vsn.get_chunk_vs(SmallChunk);
mgerdin@6004 3823 vsn.get_chunk_vs(SpecializedChunk);
mgerdin@6004 3824 vsn.retire(&cm);
mgerdin@6004 3825
mgerdin@6004 3826 // committed - used = words left to retire
mgerdin@6004 3827 const size_t words_left = page_chunks - SmallChunk - SpecializedChunk;
mgerdin@6004 3828
mgerdin@6004 3829 size_t num_medium_chunks, num_small_chunks, num_spec_chunks;
mgerdin@6004 3830 chunk_up(words_left, num_medium_chunks, num_small_chunks, num_spec_chunks);
mgerdin@6004 3831
mgerdin@6004 3832 assert(num_medium_chunks == 0, "should not get any medium chunks");
mgerdin@6004 3833 assert(cm.sum_free_chunks_count() == (num_small_chunks + num_spec_chunks), "should be space for 3 chunks");
mgerdin@6004 3834 assert(cm.sum_free_chunks() == words_left, "sizes should add up");
mgerdin@6004 3835 }
mgerdin@6004 3836
mgerdin@6004 3837 { // Half of VSN is committed, a humongous chunk is used
mgerdin@6004 3838 ChunkManager cm(SpecializedChunk, SmallChunk, MediumChunk);
mgerdin@6004 3839 VirtualSpaceNode vsn(vsn_test_size_bytes);
mgerdin@6004 3840 vsn.initialize();
mgerdin@6004 3841 vsn.expand_by(MediumChunk * 2, MediumChunk * 2);
mgerdin@6004 3842 vsn.get_chunk_vs(MediumChunk + SpecializedChunk); // Humongous chunks will be aligned up to MediumChunk + SpecializedChunk
mgerdin@6004 3843 vsn.retire(&cm);
mgerdin@6004 3844
mgerdin@6004 3845 const size_t words_left = MediumChunk * 2 - (MediumChunk + SpecializedChunk);
mgerdin@6004 3846 size_t num_medium_chunks, num_small_chunks, num_spec_chunks;
mgerdin@6004 3847 chunk_up(words_left, num_medium_chunks, num_small_chunks, num_spec_chunks);
mgerdin@6004 3848
mgerdin@6004 3849 assert(num_medium_chunks == 0, "should not get any medium chunks");
mgerdin@6004 3850 assert(cm.sum_free_chunks_count() == (num_small_chunks + num_spec_chunks), "should be space for 3 chunks");
mgerdin@6004 3851 assert(cm.sum_free_chunks() == words_left, "sizes should add up");
mgerdin@6004 3852 }
mgerdin@6004 3853
mgerdin@6004 3854 }
stefank@6170 3855
stefank@6170 3856 #define assert_is_available_positive(word_size) \
stefank@6170 3857 assert(vsn.is_available(word_size), \
stefank@6170 3858 err_msg(#word_size ": " PTR_FORMAT " bytes were not available in " \
stefank@6170 3859 "VirtualSpaceNode [" PTR_FORMAT ", " PTR_FORMAT ")", \
stefank@6170 3860 (uintptr_t)(word_size * BytesPerWord), vsn.bottom(), vsn.end()));
stefank@6170 3861
stefank@6170 3862 #define assert_is_available_negative(word_size) \
stefank@6170 3863 assert(!vsn.is_available(word_size), \
stefank@6170 3864 err_msg(#word_size ": " PTR_FORMAT " bytes should not be available in " \
stefank@6170 3865 "VirtualSpaceNode [" PTR_FORMAT ", " PTR_FORMAT ")", \
stefank@6170 3866 (uintptr_t)(word_size * BytesPerWord), vsn.bottom(), vsn.end()));
stefank@6170 3867
stefank@6170 3868 static void test_is_available_positive() {
stefank@6170 3869 // Reserve some memory.
stefank@6170 3870 VirtualSpaceNode vsn(os::vm_allocation_granularity());
stefank@6170 3871 assert(vsn.initialize(), "Failed to setup VirtualSpaceNode");
stefank@6170 3872
stefank@6170 3873 // Commit some memory.
stefank@6170 3874 size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord;
stefank@6170 3875 bool expanded = vsn.expand_by(commit_word_size, commit_word_size);
stefank@6170 3876 assert(expanded, "Failed to commit");
stefank@6170 3877
stefank@6170 3878 // Check that is_available accepts the committed size.
stefank@6170 3879 assert_is_available_positive(commit_word_size);
stefank@6170 3880
stefank@6170 3881 // Check that is_available accepts half the committed size.
stefank@6170 3882 size_t expand_word_size = commit_word_size / 2;
stefank@6170 3883 assert_is_available_positive(expand_word_size);
stefank@6170 3884 }
stefank@6170 3885
stefank@6170 3886 static void test_is_available_negative() {
stefank@6170 3887 // Reserve some memory.
stefank@6170 3888 VirtualSpaceNode vsn(os::vm_allocation_granularity());
stefank@6170 3889 assert(vsn.initialize(), "Failed to setup VirtualSpaceNode");
stefank@6170 3890
stefank@6170 3891 // Commit some memory.
stefank@6170 3892 size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord;
stefank@6170 3893 bool expanded = vsn.expand_by(commit_word_size, commit_word_size);
stefank@6170 3894 assert(expanded, "Failed to commit");
stefank@6170 3895
stefank@6170 3896 // Check that is_available doesn't accept a too large size.
stefank@6170 3897 size_t two_times_commit_word_size = commit_word_size * 2;
stefank@6170 3898 assert_is_available_negative(two_times_commit_word_size);
stefank@6170 3899 }
stefank@6170 3900
stefank@6170 3901 static void test_is_available_overflow() {
stefank@6170 3902 // Reserve some memory.
stefank@6170 3903 VirtualSpaceNode vsn(os::vm_allocation_granularity());
stefank@6170 3904 assert(vsn.initialize(), "Failed to setup VirtualSpaceNode");
stefank@6170 3905
stefank@6170 3906 // Commit some memory.
stefank@6170 3907 size_t commit_word_size = os::vm_allocation_granularity() / BytesPerWord;
stefank@6170 3908 bool expanded = vsn.expand_by(commit_word_size, commit_word_size);
stefank@6170 3909 assert(expanded, "Failed to commit");
stefank@6170 3910
stefank@6170 3911 // Calculate a size that will overflow the virtual space size.
stefank@6170 3912 void* virtual_space_max = (void*)(uintptr_t)-1;
stefank@6170 3913 size_t bottom_to_max = pointer_delta(virtual_space_max, vsn.bottom(), 1);
stefank@6170 3914 size_t overflow_size = bottom_to_max + BytesPerWord;
stefank@6170 3915 size_t overflow_word_size = overflow_size / BytesPerWord;
stefank@6170 3916
stefank@6170 3917 // Check that is_available can handle the overflow.
stefank@6170 3918 assert_is_available_negative(overflow_word_size);
stefank@6170 3919 }
stefank@6170 3920
stefank@6170 3921 static void test_is_available() {
stefank@6170 3922 TestVirtualSpaceNodeTest::test_is_available_positive();
stefank@6170 3923 TestVirtualSpaceNodeTest::test_is_available_negative();
stefank@6170 3924 TestVirtualSpaceNodeTest::test_is_available_overflow();
stefank@6170 3925 }
mgerdin@6004 3926 };
mgerdin@6004 3927
mgerdin@6004 3928 void TestVirtualSpaceNode_test() {
mgerdin@6004 3929 TestVirtualSpaceNodeTest::test();
stefank@6170 3930 TestVirtualSpaceNodeTest::test_is_available();
mgerdin@6004 3931 }
stefank@5704 3932 #endif

mercurial