src/share/vm/memory/metaspace.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7867
57d4971ff1df
parent 7535
7ae4e26cb1e0
child 9041
95a08233f46c
permissions
-rw-r--r--

merge

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

mercurial