src/share/vm/memory/metaspace.cpp

Mon, 31 Mar 2014 17:09:38 +0200

author
ehelin
date
Mon, 31 Mar 2014 17:09:38 +0200
changeset 6609
270d7cb38f40
parent 6420
9fdaa79b0c27
child 6678
7384f6a12fc1
permissions
-rw-r--r--

8038934: Remove prefix allocated_ from methods and variables in Metaspace
Reviewed-by: jmasa, coleenp

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

mercurial