src/share/vm/memory/metaspace.cpp

Thu, 21 Aug 2014 13:57:51 -0700

author
iklam
date
Thu, 21 Aug 2014 13:57:51 -0700
changeset 7089
6e0cb14ce59b
parent 6911
ce8f6bb717c9
child 7103
622c6e0ad4d6
permissions
-rw-r--r--

8046070: Class Data Sharing clean up and refactoring
Summary: Cleaned up CDS to be more configurable, maintainable and extensible
Reviewed-by: dholmes, coleenp, acorn, mchung

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

mercurial