annotate src/share/vm/memory/metaspace.cpp @ 5428:85c1ca43713f

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