src/share/vm/memory/metaspace.cpp

Wed, 14 Mar 2018 03:19:46 -0700

author
shshahma
date
Wed, 14 Mar 2018 03:19:46 -0700
changeset 9301
d47844b56aaf
parent 9064
ea0367ce6726
child 9448
73d689add964
child 9612
58ffe5f227a6
permissions
-rw-r--r--

8035074: hs_err improvement: Add time zone information in the hs_err file
8026335: hs_err improvement: Print exact compressed oops mode and the heap base value.
8026331: hs_err improvement: Print if we have seen any OutOfMemoryErrors or StackOverflowErrors
Summary: Add requested things to hs_err file.
Reviewed-by: dholmes

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

mercurial