src/share/vm/memory/metaspace.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial