src/share/vm/memory/metaspace.cpp

Sat, 23 Nov 2013 12:25:13 +0100

author
mgronlun
date
Sat, 23 Nov 2013 12:25:13 +0100
changeset 6131
86e6d691f2e1
parent 6093
11b116661830
child 6170
fa76dce60db7
permissions
-rw-r--r--

8028128: Add a type safe alternative for working with counter based data
Reviewed-by: dholmes, egahlin

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

mercurial