annotate src/share/vm/memory/metaspace.cpp @ 5429:a6414751d537

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