annotate src/share/vm/memory/metaspace.cpp @ 5506:bdfbb1fb19ca

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