src/share/vm/memory/metaspace.cpp

Fri, 21 Feb 2014 10:01:20 +0100

author
stefank
date
Fri, 21 Feb 2014 10:01:20 +0100
changeset 6973
4af19b914f53
parent 6911
ce8f6bb717c9
child 7089
6e0cb14ce59b
permissions
-rw-r--r--

8035393: Use CLDClosure instead of CLDToOopClosure in frame::oops_interpreted_do
Reviewed-by: tschatzl, coleenp

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

mercurial