src/share/vm/memory/metaspace.cpp

Fri, 21 Mar 2014 10:17:47 +0100

author
ehelin
date
Fri, 21 Mar 2014 10:17:47 +0100
changeset 6417
daef39043d2c
parent 6337
ab36007d6358
child 6418
bc7714614ad8
permissions
-rw-r--r--

8036698: Add trace event for updates to metaspace gc threshold
Reviewed-by: stefank, mgerdin

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

mercurial