src/share/vm/memory/metaspace.cpp

Wed, 14 Oct 2020 17:44:48 +0800

author
aoqi
date
Wed, 14 Oct 2020 17:44:48 +0800
changeset 9931
fd44df5e3bc3
parent 9637
eef07cd490d4
parent 9905
6c179587bf5b
permissions
-rw-r--r--

Merge

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

mercurial