src/share/vm/memory/metaspace.cpp

Mon, 11 Dec 2017 02:39:24 -0800

author
mchinnathamb
date
Mon, 11 Dec 2017 02:39:24 -0800
changeset 9058
8c3e62bb99f3
parent 9057
b0f7174de2c5
child 9063
777ace6655eb
permissions
-rw-r--r--

8170395: Metaspace initialization queries the wrong chunk freelist
Reviewed-by: stuefe, stefank

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

mercurial