annotate src/share/vm/memory/metaspace.cpp @ 5510:94c0343b1887

8026715: Remove the MetaDataDeallocateALot develop flag Reviewed-by: coleenp, mgerdin
author stefank
date Thu, 17 Oct 2013 08:42:41 +0200
parents bdfbb1fb19ca
children 1d1ea10fe09f a6177f601c64
rev   line source
coleenp@3602 1 /*
coleenp@4277 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
coleenp@3602 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
coleenp@3602 4 *
coleenp@3602 5 * This code is free software; you can redistribute it and/or modify it
coleenp@3602 6 * under the terms of the GNU General Public License version 2 only, as
coleenp@3602 7 * published by the Free Software Foundation.
coleenp@3602 8 *
coleenp@3602 9 * This code is distributed in the hope that it will be useful, but WITHOUT
coleenp@3602 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
coleenp@3602 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
coleenp@3602 12 * version 2 for more details (a copy is included in the LICENSE file that
coleenp@3602 13 * accompanied this code).
coleenp@3602 14 *
coleenp@3602 15 * You should have received a copy of the GNU General Public License version
coleenp@3602 16 * 2 along with this work; if not, write to the Free Software Foundation,
coleenp@3602 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
coleenp@3602 18 *
coleenp@3602 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
coleenp@3602 20 * or visit www.oracle.com if you need additional information or have any
coleenp@3602 21 * questions.
coleenp@3602 22 *
coleenp@3602 23 */
coleenp@3602 24 #include "precompiled.hpp"
coleenp@3602 25 #include "gc_interface/collectedHeap.hpp"
stefank@5336 26 #include "memory/allocation.hpp"
coleenp@3602 27 #include "memory/binaryTreeDictionary.hpp"
jmasa@3761 28 #include "memory/freeList.hpp"
coleenp@3602 29 #include "memory/collectorPolicy.hpp"
coleenp@3602 30 #include "memory/filemap.hpp"
coleenp@3602 31 #include "memory/freeList.hpp"
stefank@5428 32 #include "memory/gcLocker.hpp"
jmasa@3761 33 #include "memory/metachunk.hpp"
coleenp@3602 34 #include "memory/metaspace.hpp"
coleenp@3602 35 #include "memory/metaspaceShared.hpp"
coleenp@3602 36 #include "memory/resourceArea.hpp"
coleenp@3602 37 #include "memory/universe.hpp"
stefank@5428 38 #include "runtime/atomic.inline.hpp"
coleenp@3602 39 #include "runtime/globals.hpp"
stefank@5428 40 #include "runtime/init.hpp"
hseigel@5093 41 #include "runtime/java.hpp"
coleenp@3602 42 #include "runtime/mutex.hpp"
coleenp@3860 43 #include "runtime/orderAccess.hpp"
coleenp@3602 44 #include "services/memTracker.hpp"
stefank@5429 45 #include "services/memoryService.hpp"
coleenp@3602 46 #include "utilities/copy.hpp"
coleenp@3602 47 #include "utilities/debug.hpp"
coleenp@3602 48
jmasa@3761 49 typedef BinaryTreeDictionary<Metablock, FreeList> BlockTreeDictionary;
jmasa@3761 50 typedef BinaryTreeDictionary<Metachunk, FreeList> ChunkTreeDictionary;
stefank@5506 51
stefank@5506 52 // Set this constant to enable slow integrity checking of the free chunk lists
mgerdin@3829 53 const bool metaspace_slow_verify = false;
mgerdin@3829 54
mgerdin@5264 55 size_t const allocation_from_dictionary_limit = 4 * K;
coleenp@3602 56
coleenp@3602 57 MetaWord* last_allocated = 0;
coleenp@3602 58
hseigel@5093 59 size_t Metaspace::_class_metaspace_size;
hseigel@5093 60
coleenp@3602 61 // Used in declarations in SpaceManager and ChunkManager
coleenp@3602 62 enum ChunkIndex {
jmasa@3947 63 ZeroIndex = 0,
jmasa@3947 64 SpecializedIndex = ZeroIndex,
jmasa@3947 65 SmallIndex = SpecializedIndex + 1,
jmasa@3947 66 MediumIndex = SmallIndex + 1,
jmasa@3947 67 HumongousIndex = MediumIndex + 1,
jmasa@3947 68 NumberOfFreeLists = 3,
jmasa@3947 69 NumberOfInUseLists = 4
jmasa@3947 70 };
jmasa@3947 71
jmasa@3947 72 enum ChunkSizes { // in words.
jmasa@3947 73 ClassSpecializedChunk = 128,
jmasa@3947 74 SpecializedChunk = 128,
jmasa@3947 75 ClassSmallChunk = 256,
jmasa@3947 76 SmallChunk = 512,
coleenp@4902 77 ClassMediumChunk = 4 * K,
jmasa@3947 78 MediumChunk = 8 * K,
jmasa@3947 79 HumongousChunkGranularity = 8
coleenp@3602 80 };
coleenp@3602 81
coleenp@3602 82 static ChunkIndex next_chunk_index(ChunkIndex i) {
jmasa@3761 83 assert(i < NumberOfInUseLists, "Out of bound");
coleenp@3602 84 return (ChunkIndex) (i+1);
coleenp@3602 85 }
coleenp@3602 86
stefank@5428 87 volatile intptr_t MetaspaceGC::_capacity_until_GC = 0;
coleenp@3602 88 uint MetaspaceGC::_shrink_factor = 0;
coleenp@3602 89 bool MetaspaceGC::_should_concurrent_collect = false;
coleenp@3602 90
jmasa@4497 91 typedef class FreeList<Metachunk> ChunkList;
coleenp@3602 92
coleenp@3602 93 // Manages the global free lists of chunks.
stefank@5336 94 class ChunkManager : public CHeapObj<mtInternal> {
coleenp@3602 95
coleenp@3602 96 // Free list of chunks of different sizes.
jmasa@4572 97 // SpecializedChunk
coleenp@3602 98 // SmallChunk
coleenp@3602 99 // MediumChunk
coleenp@3602 100 // HumongousChunk
jmasa@3761 101 ChunkList _free_chunks[NumberOfFreeLists];
jmasa@3761 102
jmasa@3761 103 // HumongousChunk
jmasa@3761 104 ChunkTreeDictionary _humongous_dictionary;
coleenp@3602 105
coleenp@3602 106 // ChunkManager in all lists of this type
coleenp@3602 107 size_t _free_chunks_total;
coleenp@3602 108 size_t _free_chunks_count;
coleenp@3602 109
coleenp@3602 110 void dec_free_chunks_total(size_t v) {
coleenp@3602 111 assert(_free_chunks_count > 0 &&
coleenp@3602 112 _free_chunks_total > 0,
coleenp@3602 113 "About to go negative");
coleenp@3602 114 Atomic::add_ptr(-1, &_free_chunks_count);
coleenp@3602 115 jlong minus_v = (jlong) - (jlong) v;
coleenp@3602 116 Atomic::add_ptr(minus_v, &_free_chunks_total);
coleenp@3602 117 }
coleenp@3602 118
coleenp@3602 119 // Debug support
coleenp@3602 120
coleenp@3602 121 size_t sum_free_chunks();
coleenp@3602 122 size_t sum_free_chunks_count();
coleenp@3602 123
coleenp@3602 124 void locked_verify_free_chunks_total();
mgerdin@3829 125 void slow_locked_verify_free_chunks_total() {
mgerdin@3829 126 if (metaspace_slow_verify) {
mgerdin@3829 127 locked_verify_free_chunks_total();
mgerdin@3829 128 }
mgerdin@3829 129 }
coleenp@3602 130 void locked_verify_free_chunks_count();
mgerdin@3829 131 void slow_locked_verify_free_chunks_count() {
mgerdin@3829 132 if (metaspace_slow_verify) {
mgerdin@3829 133 locked_verify_free_chunks_count();
mgerdin@3829 134 }
mgerdin@3829 135 }
coleenp@3602 136 void verify_free_chunks_count();
coleenp@3602 137
coleenp@3602 138 public:
coleenp@3602 139
stefank@5336 140 ChunkManager(size_t specialized_size, size_t small_size, size_t medium_size)
stefank@5336 141 : _free_chunks_total(0), _free_chunks_count(0) {
stefank@5336 142 _free_chunks[SpecializedIndex].set_size(specialized_size);
stefank@5336 143 _free_chunks[SmallIndex].set_size(small_size);
stefank@5336 144 _free_chunks[MediumIndex].set_size(medium_size);
stefank@5336 145 }
coleenp@3602 146
coleenp@3602 147 // add or delete (return) a chunk to the global freelist.
coleenp@3602 148 Metachunk* chunk_freelist_allocate(size_t word_size);
coleenp@3602 149
jmasa@3947 150 // Map a size to a list index assuming that there are lists
jmasa@3947 151 // for special, small, medium, and humongous chunks.
jmasa@3947 152 static ChunkIndex list_index(size_t size);
jmasa@3947 153
jmasa@4572 154 // Remove the chunk from its freelist. It is
jmasa@4572 155 // expected to be on one of the _free_chunks[] lists.
jmasa@4572 156 void remove_chunk(Metachunk* chunk);
jmasa@4572 157
jmasa@4497 158 // Add the simple linked list of chunks to the freelist of chunks
jmasa@4497 159 // of type index.
jmasa@4497 160 void return_chunks(ChunkIndex index, Metachunk* chunks);
jmasa@4497 161
coleenp@3602 162 // Total of the space in the free chunks list
ehelin@5268 163 size_t free_chunks_total_words();
ehelin@5268 164 size_t free_chunks_total_bytes();
coleenp@3602 165
coleenp@3602 166 // Number of chunks in the free chunks list
coleenp@3602 167 size_t free_chunks_count();
coleenp@3602 168
coleenp@3602 169 void inc_free_chunks_total(size_t v, size_t count = 1) {
coleenp@3602 170 Atomic::add_ptr(count, &_free_chunks_count);
coleenp@3602 171 Atomic::add_ptr(v, &_free_chunks_total);
coleenp@3602 172 }
jmasa@3761 173 ChunkTreeDictionary* humongous_dictionary() {
jmasa@3761 174 return &_humongous_dictionary;
jmasa@3761 175 }
coleenp@3602 176
coleenp@3602 177 ChunkList* free_chunks(ChunkIndex index);
coleenp@3602 178
coleenp@3602 179 // Returns the list for the given chunk word size.
coleenp@3602 180 ChunkList* find_free_chunks_list(size_t word_size);
coleenp@3602 181
stefank@5510 182 // Remove from a list by size. Selects list based on size of chunk.
coleenp@3602 183 Metachunk* free_chunks_get(size_t chunk_word_size);
coleenp@3602 184
coleenp@3602 185 // Debug support
coleenp@3602 186 void verify();
mgerdin@3829 187 void slow_verify() {
mgerdin@3829 188 if (metaspace_slow_verify) {
mgerdin@3829 189 verify();
mgerdin@3829 190 }
mgerdin@3829 191 }
coleenp@3602 192 void locked_verify();
mgerdin@3829 193 void slow_locked_verify() {
mgerdin@3829 194 if (metaspace_slow_verify) {
mgerdin@3829 195 locked_verify();
mgerdin@3829 196 }
mgerdin@3829 197 }
coleenp@3602 198 void verify_free_chunks_total();
coleenp@3602 199
coleenp@3602 200 void locked_print_free_chunks(outputStream* st);
coleenp@3602 201 void locked_print_sum_free_chunks(outputStream* st);
jmasa@3761 202
stefank@5336 203 void print_on(outputStream* st) const;
coleenp@3602 204 };
coleenp@3602 205
coleenp@3602 206 // Used to manage the free list of Metablocks (a block corresponds
coleenp@3602 207 // to the allocation of a quantum of metadata).
coleenp@3602 208 class BlockFreelist VALUE_OBJ_CLASS_SPEC {
jmasa@3761 209 BlockTreeDictionary* _dictionary;
jmasa@3761 210
mgerdin@5264 211 // Only allocate and split from freelist if the size of the allocation
mgerdin@5264 212 // is at least 1/4th the size of the available block.
mgerdin@5264 213 const static int WasteMultiplier = 4;
mgerdin@5264 214
coleenp@3602 215 // Accessors
jmasa@3761 216 BlockTreeDictionary* dictionary() const { return _dictionary; }
coleenp@3602 217
coleenp@3602 218 public:
coleenp@3602 219 BlockFreelist();
coleenp@3602 220 ~BlockFreelist();
coleenp@3602 221
coleenp@3602 222 // Get and return a block to the free list
jmasa@3761 223 MetaWord* get_block(size_t word_size);
jmasa@3761 224 void return_block(MetaWord* p, size_t word_size);
jmasa@3761 225
jmasa@3761 226 size_t total_size() {
jmasa@3761 227 if (dictionary() == NULL) {
coleenp@3602 228 return 0;
jmasa@3761 229 } else {
jmasa@3761 230 return dictionary()->total_size();
coleenp@3602 231 }
jmasa@3761 232 }
coleenp@3602 233
coleenp@3602 234 void print_on(outputStream* st) const;
coleenp@3602 235 };
coleenp@3602 236
stefank@5506 237 // A VirtualSpaceList node.
coleenp@3602 238 class VirtualSpaceNode : public CHeapObj<mtClass> {
coleenp@3602 239 friend class VirtualSpaceList;
coleenp@3602 240
coleenp@3602 241 // Link to next VirtualSpaceNode
coleenp@3602 242 VirtualSpaceNode* _next;
coleenp@3602 243
coleenp@3602 244 // total in the VirtualSpace
coleenp@3602 245 MemRegion _reserved;
coleenp@3602 246 ReservedSpace _rs;
coleenp@3602 247 VirtualSpace _virtual_space;
coleenp@3602 248 MetaWord* _top;
jmasa@4572 249 // count of chunks contained in this VirtualSpace
jmasa@4572 250 uintx _container_count;
coleenp@3602 251
coleenp@3602 252 // Convenience functions to access the _virtual_space
coleenp@3602 253 char* low() const { return virtual_space()->low(); }
coleenp@3602 254 char* high() const { return virtual_space()->high(); }
coleenp@3602 255
jmasa@4572 256 // The first Metachunk will be allocated at the bottom of the
jmasa@4572 257 // VirtualSpace
jmasa@4572 258 Metachunk* first_chunk() { return (Metachunk*) bottom(); }
jmasa@4572 259
coleenp@3602 260 public:
coleenp@3602 261
coleenp@3602 262 VirtualSpaceNode(size_t byte_size);
jmasa@4572 263 VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {}
coleenp@3602 264 ~VirtualSpaceNode();
coleenp@3602 265
hseigel@5093 266 // Convenience functions for logical bottom and end
hseigel@5093 267 MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
hseigel@5093 268 MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
hseigel@5093 269
stefank@5269 270 size_t reserved_words() const { return _virtual_space.reserved_size() / BytesPerWord; }
stefank@5269 271 size_t committed_words() const { return _virtual_space.actual_committed_size() / BytesPerWord; }
stefank@5269 272
stefank@5428 273 bool is_pre_committed() const { return _virtual_space.special(); }
stefank@5428 274
coleenp@3602 275 // address of next available space in _virtual_space;
coleenp@3602 276 // Accessors
coleenp@3602 277 VirtualSpaceNode* next() { return _next; }
coleenp@3602 278 void set_next(VirtualSpaceNode* v) { _next = v; }
coleenp@3602 279
coleenp@3602 280 void set_reserved(MemRegion const v) { _reserved = v; }
coleenp@3602 281 void set_top(MetaWord* v) { _top = v; }
coleenp@3602 282
coleenp@3602 283 // Accessors
coleenp@3602 284 MemRegion* reserved() { return &_reserved; }
coleenp@3602 285 VirtualSpace* virtual_space() const { return (VirtualSpace*) &_virtual_space; }
coleenp@3602 286
jmasa@4580 287 // Returns true if "word_size" is available in the VirtualSpace
coleenp@3602 288 bool is_available(size_t word_size) { return _top + word_size <= end(); }
coleenp@3602 289
coleenp@3602 290 MetaWord* top() const { return _top; }
coleenp@3602 291 void inc_top(size_t word_size) { _top += word_size; }
coleenp@3602 292
jmasa@4572 293 uintx container_count() { return _container_count; }
stefank@5336 294 void inc_container_count();
jmasa@4572 295 void dec_container_count();
jmasa@4572 296 #ifdef ASSERT
stefank@5336 297 uint container_count_slow();
jmasa@4572 298 void verify_container_count();
jmasa@4572 299 #endif
jmasa@4572 300
coleenp@3602 301 // used and capacity in this single entry in the list
coleenp@3602 302 size_t used_words_in_vs() const;
coleenp@3602 303 size_t capacity_words_in_vs() const;
jmasa@4580 304 size_t free_words_in_vs() const;
coleenp@3602 305
coleenp@3602 306 bool initialize();
coleenp@3602 307
coleenp@3602 308 // get space from the virtual space
coleenp@3602 309 Metachunk* take_from_committed(size_t chunk_word_size);
coleenp@3602 310
coleenp@3602 311 // Allocate a chunk from the virtual space and return it.
coleenp@3602 312 Metachunk* get_chunk_vs(size_t chunk_word_size);
coleenp@3602 313
coleenp@3602 314 // Expands/shrinks the committed space in a virtual space. Delegates
coleenp@3602 315 // to Virtualspace
stefank@5428 316 bool expand_by(size_t min_words, size_t preferred_words);
coleenp@3602 317
jmasa@4572 318 // In preparation for deleting this node, remove all the chunks
jmasa@4572 319 // in the node from any freelist.
jmasa@4572 320 void purge(ChunkManager* chunk_manager);
jmasa@4572 321
coleenp@3869 322 #ifdef ASSERT
coleenp@3602 323 // Debug support
coleenp@3602 324 void mangle();
coleenp@3869 325 #endif
coleenp@3602 326
coleenp@3602 327 void print_on(outputStream* st) const;
coleenp@3602 328 };
coleenp@3602 329
stefank@5428 330 #define assert_is_ptr_aligned(ptr, alignment) \
stefank@5428 331 assert(is_ptr_aligned(ptr, alignment), \
stefank@5428 332 err_msg(PTR_FORMAT " is not aligned to " \
stefank@5428 333 SIZE_FORMAT, ptr, alignment))
stefank@5428 334
stefank@5428 335 #define assert_is_size_aligned(size, alignment) \
stefank@5428 336 assert(is_size_aligned(size, alignment), \
stefank@5428 337 err_msg(SIZE_FORMAT " is not aligned to " \
stefank@5428 338 SIZE_FORMAT, size, alignment))
stefank@5428 339
stefank@5428 340
stefank@5428 341 // Decide if large pages should be committed when the memory is reserved.
stefank@5428 342 static bool should_commit_large_pages_when_reserving(size_t bytes) {
stefank@5428 343 if (UseLargePages && UseLargePagesInMetaspace && !os::can_commit_large_page_memory()) {
stefank@5428 344 size_t words = bytes / BytesPerWord;
stefank@5428 345 bool is_class = false; // We never reserve large pages for the class space.
stefank@5428 346 if (MetaspaceGC::can_expand(words, is_class) &&
stefank@5428 347 MetaspaceGC::allowed_expansion() >= words) {
stefank@5428 348 return true;
stefank@5428 349 }
stefank@5428 350 }
stefank@5428 351
stefank@5428 352 return false;
stefank@5428 353 }
stefank@5428 354
coleenp@3602 355 // byte_size is the size of the associated virtualspace.
stefank@5428 356 VirtualSpaceNode::VirtualSpaceNode(size_t bytes) : _top(NULL), _next(NULL), _rs(), _container_count(0) {
stefank@5428 357 assert_is_size_aligned(bytes, Metaspace::reserve_alignment());
zgu@4317 358
coleenp@4369 359 // This allocates memory with mmap. For DumpSharedspaces, try to reserve
coleenp@4369 360 // configurable address, generally at the top of the Java heap so other
coleenp@4369 361 // memory addresses don't conflict.
coleenp@3602 362 if (DumpSharedSpaces) {
stefank@5428 363 bool large_pages = false; // No large pages when dumping the CDS archive.
stefank@5428 364 char* shared_base = (char*)align_ptr_up((char*)SharedBaseAddress, Metaspace::reserve_alignment());
stefank@5428 365
stefank@5428 366 _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages, shared_base, 0);
coleenp@3602 367 if (_rs.is_reserved()) {
coleenp@4369 368 assert(shared_base == 0 || _rs.base() == shared_base, "should match");
coleenp@3602 369 } else {
coleenp@4369 370 // Get a mmap region anywhere if the SharedBaseAddress fails.
stefank@5428 371 _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages);
coleenp@3602 372 }
coleenp@3602 373 MetaspaceShared::set_shared_rs(&_rs);
coleenp@3602 374 } else {
stefank@5428 375 bool large_pages = should_commit_large_pages_when_reserving(bytes);
stefank@5428 376
stefank@5428 377 _rs = ReservedSpace(bytes, Metaspace::reserve_alignment(), large_pages);
coleenp@3602 378 }
coleenp@3602 379
stefank@5428 380 if (_rs.is_reserved()) {
stefank@5428 381 assert(_rs.base() != NULL, "Catch if we get a NULL address");
stefank@5428 382 assert(_rs.size() != 0, "Catch if we get a 0 size");
stefank@5428 383 assert_is_ptr_aligned(_rs.base(), Metaspace::reserve_alignment());
stefank@5428 384 assert_is_size_aligned(_rs.size(), Metaspace::reserve_alignment());
stefank@5428 385
stefank@5428 386 MemTracker::record_virtual_memory_type((address)_rs.base(), mtClass);
stefank@5428 387 }
coleenp@3602 388 }
coleenp@3602 389
jmasa@4572 390 void VirtualSpaceNode::purge(ChunkManager* chunk_manager) {
jmasa@4572 391 Metachunk* chunk = first_chunk();
jmasa@4572 392 Metachunk* invalid_chunk = (Metachunk*) top();
jmasa@4572 393 while (chunk < invalid_chunk ) {
stefank@5506 394 assert(chunk->is_tagged_free(), "Should be tagged free");
stefank@5506 395 MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
stefank@5506 396 chunk_manager->remove_chunk(chunk);
stefank@5506 397 assert(chunk->next() == NULL &&
stefank@5506 398 chunk->prev() == NULL,
stefank@5506 399 "Was not removed from its list");
stefank@5506 400 chunk = (Metachunk*) next;
jmasa@4572 401 }
jmasa@4572 402 }
jmasa@4572 403
jmasa@4572 404 #ifdef ASSERT
jmasa@4572 405 uint VirtualSpaceNode::container_count_slow() {
jmasa@4572 406 uint count = 0;
jmasa@4572 407 Metachunk* chunk = first_chunk();
jmasa@4572 408 Metachunk* invalid_chunk = (Metachunk*) top();
jmasa@4572 409 while (chunk < invalid_chunk ) {
jmasa@4572 410 MetaWord* next = ((MetaWord*)chunk) + chunk->word_size();
jmasa@4572 411 // Don't count the chunks on the free lists. Those are
jmasa@4572 412 // still part of the VirtualSpaceNode but not currently
jmasa@4572 413 // counted.
stefank@5506 414 if (!chunk->is_tagged_free()) {
jmasa@4572 415 count++;
jmasa@4572 416 }
jmasa@4572 417 chunk = (Metachunk*) next;
jmasa@4572 418 }
jmasa@4572 419 return count;
jmasa@4572 420 }
jmasa@4572 421 #endif
jmasa@4572 422
coleenp@3602 423 // List of VirtualSpaces for metadata allocation.
coleenp@3602 424 class VirtualSpaceList : public CHeapObj<mtClass> {
coleenp@3602 425 friend class VirtualSpaceNode;
coleenp@3602 426
coleenp@3602 427 enum VirtualSpaceSizes {
coleenp@3602 428 VirtualSpaceSize = 256 * K
coleenp@3602 429 };
coleenp@3602 430
coleenp@3602 431 // Head of the list
coleenp@3602 432 VirtualSpaceNode* _virtual_space_list;
coleenp@3602 433 // virtual space currently being used for allocations
coleenp@3602 434 VirtualSpaceNode* _current_virtual_space;
coleenp@3602 435
stefank@5428 436 // Is this VirtualSpaceList used for the compressed class space
coleenp@3602 437 bool _is_class;
coleenp@3602 438
stefank@5269 439 // Sum of reserved and committed memory in the virtual spaces
stefank@5269 440 size_t _reserved_words;
stefank@5269 441 size_t _committed_words;
stefank@5269 442
stefank@5269 443 // Number of virtual spaces
coleenp@3602 444 size_t _virtual_space_count;
coleenp@3602 445
coleenp@3602 446 ~VirtualSpaceList();
coleenp@3602 447
coleenp@3602 448 VirtualSpaceNode* virtual_space_list() const { return _virtual_space_list; }
coleenp@3602 449
coleenp@3602 450 void set_virtual_space_list(VirtualSpaceNode* v) {
coleenp@3602 451 _virtual_space_list = v;
coleenp@3602 452 }
coleenp@3602 453 void set_current_virtual_space(VirtualSpaceNode* v) {
coleenp@3602 454 _current_virtual_space = v;
coleenp@3602 455 }
coleenp@3602 456
stefank@5269 457 void link_vs(VirtualSpaceNode* new_entry);
coleenp@3602 458
coleenp@3602 459 // Get another virtual space and add it to the list. This
coleenp@3602 460 // is typically prompted by a failed attempt to allocate a chunk
coleenp@3602 461 // and is typically followed by the allocation of a chunk.
stefank@5428 462 bool create_new_virtual_space(size_t vs_word_size);
coleenp@3602 463
coleenp@3602 464 public:
coleenp@3602 465 VirtualSpaceList(size_t word_size);
coleenp@3602 466 VirtualSpaceList(ReservedSpace rs);
coleenp@3602 467
jmasa@4580 468 size_t free_bytes();
jmasa@4580 469
jmasa@3947 470 Metachunk* get_new_chunk(size_t word_size,
jmasa@3947 471 size_t grow_chunks_by_words,
jmasa@3947 472 size_t medium_chunk_bunch);
jmasa@3947 473
stefank@5428 474 bool expand_node_by(VirtualSpaceNode* node,
stefank@5428 475 size_t min_words,
stefank@5428 476 size_t preferred_words);
stefank@5428 477
stefank@5428 478 bool expand_by(size_t min_words,
stefank@5428 479 size_t preferred_words);
coleenp@3602 480
coleenp@3602 481 VirtualSpaceNode* current_virtual_space() {
coleenp@3602 482 return _current_virtual_space;
coleenp@3602 483 }
coleenp@3602 484
coleenp@3602 485 bool is_class() const { return _is_class; }
coleenp@3602 486
stefank@5428 487 bool initialization_succeeded() { return _virtual_space_list != NULL; }
coleenp@3602 488
stefank@5269 489 size_t reserved_words() { return _reserved_words; }
stefank@5269 490 size_t reserved_bytes() { return reserved_words() * BytesPerWord; }
stefank@5269 491 size_t committed_words() { return _committed_words; }
stefank@5269 492 size_t committed_bytes() { return committed_words() * BytesPerWord; }
stefank@5269 493
stefank@5269 494 void inc_reserved_words(size_t v);
stefank@5269 495 void dec_reserved_words(size_t v);
stefank@5269 496 void inc_committed_words(size_t v);
stefank@5269 497 void dec_committed_words(size_t v);
jmasa@4572 498 void inc_virtual_space_count();
jmasa@4572 499 void dec_virtual_space_count();
jmasa@4572 500
jmasa@4572 501 // Unlink empty VirtualSpaceNodes and free it.
stefank@5336 502 void purge(ChunkManager* chunk_manager);
coleenp@3602 503
coleenp@3602 504 bool contains(const void *ptr);
coleenp@3602 505
coleenp@3602 506 void print_on(outputStream* st) const;
coleenp@3602 507
coleenp@3602 508 class VirtualSpaceListIterator : public StackObj {
coleenp@3602 509 VirtualSpaceNode* _virtual_spaces;
coleenp@3602 510 public:
coleenp@3602 511 VirtualSpaceListIterator(VirtualSpaceNode* virtual_spaces) :
coleenp@3602 512 _virtual_spaces(virtual_spaces) {}
coleenp@3602 513
coleenp@3602 514 bool repeat() {
coleenp@3602 515 return _virtual_spaces != NULL;
coleenp@3602 516 }
coleenp@3602 517
coleenp@3602 518 VirtualSpaceNode* get_next() {
coleenp@3602 519 VirtualSpaceNode* result = _virtual_spaces;
coleenp@3602 520 if (_virtual_spaces != NULL) {
coleenp@3602 521 _virtual_spaces = _virtual_spaces->next();
coleenp@3602 522 }
coleenp@3602 523 return result;
coleenp@3602 524 }
coleenp@3602 525 };
coleenp@3602 526 };
coleenp@3602 527
coleenp@3602 528 class Metadebug : AllStatic {
coleenp@3602 529 // Debugging support for Metaspaces
coleenp@3602 530 static int _allocation_fail_alot_count;
coleenp@3602 531
coleenp@3602 532 public:
coleenp@3602 533
coleenp@3602 534 static void init_allocation_fail_alot_count();
coleenp@3602 535 #ifdef ASSERT
coleenp@3602 536 static bool test_metadata_failure();
coleenp@3602 537 #endif
coleenp@3602 538 };
coleenp@3602 539
coleenp@3602 540 int Metadebug::_allocation_fail_alot_count = 0;
coleenp@3602 541
coleenp@3602 542 // SpaceManager - used by Metaspace to handle allocations
coleenp@3602 543 class SpaceManager : public CHeapObj<mtClass> {
coleenp@3602 544 friend class Metaspace;
coleenp@3602 545 friend class Metadebug;
coleenp@3602 546
coleenp@3602 547 private:
jmasa@3947 548
coleenp@3602 549 // protects allocations and contains.
coleenp@3602 550 Mutex* const _lock;
coleenp@3602 551
jmasa@4727 552 // Type of metadata allocated.
jmasa@4727 553 Metaspace::MetadataType _mdtype;
jmasa@4727 554
coleenp@3602 555 // List of chunks in use by this SpaceManager. Allocations
coleenp@3602 556 // are done from the current chunk. The list is used for deallocating
coleenp@3602 557 // chunks when the SpaceManager is freed.
jmasa@3761 558 Metachunk* _chunks_in_use[NumberOfInUseLists];
coleenp@3602 559 Metachunk* _current_chunk;
coleenp@3602 560
coleenp@3602 561 // Number of small chunks to allocate to a manager
coleenp@3602 562 // If class space manager, small chunks are unlimited
coleenp@3602 563 static uint const _small_chunk_limit;
coleenp@3602 564
coleenp@3602 565 // Sum of all space in allocated chunks
jmasa@4580 566 size_t _allocated_blocks_words;
jmasa@4580 567
jmasa@4580 568 // Sum of all allocated chunks
jmasa@4580 569 size_t _allocated_chunks_words;
jmasa@4580 570 size_t _allocated_chunks_count;
coleenp@3602 571
coleenp@3602 572 // Free lists of blocks are per SpaceManager since they
coleenp@3602 573 // are assumed to be in chunks in use by the SpaceManager
coleenp@3602 574 // and all chunks in use by a SpaceManager are freed when
coleenp@3602 575 // the class loader using the SpaceManager is collected.
coleenp@3602 576 BlockFreelist _block_freelists;
coleenp@3602 577
coleenp@3602 578 // protects virtualspace and chunk expansions
coleenp@3602 579 static const char* _expand_lock_name;
coleenp@3602 580 static const int _expand_lock_rank;
coleenp@3602 581 static Mutex* const _expand_lock;
coleenp@3602 582
jmasa@3947 583 private:
coleenp@3602 584 // Accessors
coleenp@3602 585 Metachunk* chunks_in_use(ChunkIndex index) const { return _chunks_in_use[index]; }
coleenp@3602 586 void set_chunks_in_use(ChunkIndex index, Metachunk* v) { _chunks_in_use[index] = v; }
coleenp@3602 587
coleenp@3602 588 BlockFreelist* block_freelists() const {
coleenp@3602 589 return (BlockFreelist*) &_block_freelists;
coleenp@3602 590 }
coleenp@3602 591
jmasa@4727 592 Metaspace::MetadataType mdtype() { return _mdtype; }
stefank@5336 593
stefank@5336 594 VirtualSpaceList* vs_list() const { return Metaspace::get_space_list(_mdtype); }
stefank@5336 595 ChunkManager* chunk_manager() const { return Metaspace::get_chunk_manager(_mdtype); }
coleenp@3602 596
coleenp@3602 597 Metachunk* current_chunk() const { return _current_chunk; }
coleenp@3602 598 void set_current_chunk(Metachunk* v) {
coleenp@3602 599 _current_chunk = v;
coleenp@3602 600 }
coleenp@3602 601
coleenp@3602 602 Metachunk* find_current_chunk(size_t word_size);
coleenp@3602 603
coleenp@3602 604 // Add chunk to the list of chunks in use
coleenp@3602 605 void add_chunk(Metachunk* v, bool make_current);
mgerdin@5264 606 void retire_current_chunk();
coleenp@3602 607
coleenp@3602 608 Mutex* lock() const { return _lock; }
coleenp@3602 609
jmasa@3947 610 const char* chunk_size_name(ChunkIndex index) const;
jmasa@3947 611
jmasa@3947 612 protected:
jmasa@3947 613 void initialize();
jmasa@3947 614
coleenp@3602 615 public:
jmasa@4727 616 SpaceManager(Metaspace::MetadataType mdtype,
stefank@5336 617 Mutex* lock);
coleenp@3602 618 ~SpaceManager();
coleenp@3602 619
jmasa@3947 620 enum ChunkMultiples {
jmasa@3947 621 MediumChunkMultiple = 4
coleenp@3602 622 };
coleenp@3602 623
stefank@5336 624 bool is_class() { return _mdtype == Metaspace::ClassType; }
stefank@5336 625
coleenp@3602 626 // Accessors
jmasa@3947 627 size_t specialized_chunk_size() { return SpecializedChunk; }
stefank@5336 628 size_t small_chunk_size() { return (size_t) is_class() ? ClassSmallChunk : SmallChunk; }
stefank@5336 629 size_t medium_chunk_size() { return (size_t) is_class() ? ClassMediumChunk : MediumChunk; }
jmasa@3947 630 size_t medium_chunk_bunch() { return medium_chunk_size() * MediumChunkMultiple; }
jmasa@3947 631
jmasa@4580 632 size_t allocated_blocks_words() const { return _allocated_blocks_words; }
jmasa@4580 633 size_t allocated_blocks_bytes() const { return _allocated_blocks_words * BytesPerWord; }
jmasa@4580 634 size_t allocated_chunks_words() const { return _allocated_chunks_words; }
jmasa@4580 635 size_t allocated_chunks_count() const { return _allocated_chunks_count; }
jmasa@4580 636
jmasa@3947 637 bool is_humongous(size_t word_size) { return word_size > medium_chunk_size(); }
coleenp@3602 638
coleenp@3602 639 static Mutex* expand_lock() { return _expand_lock; }
coleenp@3602 640
jmasa@4580 641 // Increment the per Metaspace and global running sums for Metachunks
jmasa@4580 642 // by the given size. This is used when a Metachunk to added to
jmasa@4580 643 // the in-use list.
jmasa@4580 644 void inc_size_metrics(size_t words);
jmasa@4580 645 // Increment the per Metaspace and global running sums Metablocks by the given
jmasa@4580 646 // size. This is used when a Metablock is allocated.
jmasa@4580 647 void inc_used_metrics(size_t words);
jmasa@4580 648 // Delete the portion of the running sums for this SpaceManager. That is,
jmasa@4580 649 // the globals running sums for the Metachunks and Metablocks are
jmasa@4580 650 // decremented for all the Metachunks in-use by this SpaceManager.
jmasa@4580 651 void dec_total_from_size_metrics();
jmasa@4580 652
jmasa@3947 653 // Set the sizes for the initial chunks.
jmasa@3947 654 void get_initial_chunk_sizes(Metaspace::MetaspaceType type,
jmasa@3947 655 size_t* chunk_word_size,
jmasa@3947 656 size_t* class_chunk_word_size);
jmasa@3947 657
coleenp@3602 658 size_t sum_capacity_in_chunks_in_use() const;
coleenp@3602 659 size_t sum_used_in_chunks_in_use() const;
coleenp@3602 660 size_t sum_free_in_chunks_in_use() const;
coleenp@3602 661 size_t sum_waste_in_chunks_in_use() const;
coleenp@3602 662 size_t sum_waste_in_chunks_in_use(ChunkIndex index ) const;
coleenp@3602 663
coleenp@3602 664 size_t sum_count_in_chunks_in_use();
coleenp@3602 665 size_t sum_count_in_chunks_in_use(ChunkIndex i);
coleenp@3602 666
jmasa@3947 667 Metachunk* get_new_chunk(size_t word_size, size_t grow_chunks_by_words);
jmasa@3947 668
coleenp@3602 669 // Block allocation and deallocation.
coleenp@3602 670 // Allocates a block from the current chunk
coleenp@3602 671 MetaWord* allocate(size_t word_size);
coleenp@3602 672
coleenp@3602 673 // Helper for allocations
jmasa@3761 674 MetaWord* allocate_work(size_t word_size);
coleenp@3602 675
coleenp@3602 676 // Returns a block to the per manager freelist
jmasa@3761 677 void deallocate(MetaWord* p, size_t word_size);
coleenp@3602 678
coleenp@3602 679 // Based on the allocation size and a minimum chunk size,
coleenp@3602 680 // returned chunk size (for expanding space for chunk allocation).
coleenp@3602 681 size_t calc_chunk_size(size_t allocation_word_size);
coleenp@3602 682
coleenp@3602 683 // Called when an allocation from the current chunk fails.
coleenp@3602 684 // Gets a new chunk (may require getting a new virtual space),
coleenp@3602 685 // and allocates from that chunk.
jmasa@3761 686 MetaWord* grow_and_allocate(size_t word_size);
coleenp@3602 687
stefank@5429 688 // Notify memory usage to MemoryService.
stefank@5429 689 void track_metaspace_memory_usage();
stefank@5429 690
coleenp@3602 691 // debugging support.
coleenp@3602 692
coleenp@3602 693 void dump(outputStream* const out) const;
coleenp@3602 694 void print_on(outputStream* st) const;
coleenp@3602 695 void locked_print_chunks_in_use_on(outputStream* st) const;
coleenp@3602 696
coleenp@3602 697 void verify();
jmasa@3892 698 void verify_chunk_size(Metachunk* chunk);
coleenp@3869 699 NOT_PRODUCT(void mangle_freed_chunks();)
coleenp@3602 700 #ifdef ASSERT
jmasa@4580 701 void verify_allocated_blocks_words();
coleenp@3602 702 #endif
iklam@4773 703
iklam@4773 704 size_t get_raw_word_size(size_t word_size) {
iklam@4773 705 size_t byte_size = word_size * BytesPerWord;
iklam@4773 706
stefank@5506 707 size_t raw_bytes_size = MAX2(byte_size, sizeof(Metablock));
stefank@5506 708 raw_bytes_size = align_size_up(raw_bytes_size, Metachunk::object_alignment());
stefank@5506 709
iklam@4773 710 size_t raw_word_size = raw_bytes_size / BytesPerWord;
iklam@4773 711 assert(raw_word_size * BytesPerWord == raw_bytes_size, "Size problem");
iklam@4773 712
iklam@4773 713 return raw_word_size;
iklam@4773 714 }
coleenp@3602 715 };
coleenp@3602 716
coleenp@3602 717 uint const SpaceManager::_small_chunk_limit = 4;
coleenp@3602 718
coleenp@3602 719 const char* SpaceManager::_expand_lock_name =
coleenp@3602 720 "SpaceManager chunk allocation lock";
coleenp@3602 721 const int SpaceManager::_expand_lock_rank = Monitor::leaf - 1;
coleenp@3602 722 Mutex* const SpaceManager::_expand_lock =
coleenp@3602 723 new Mutex(SpaceManager::_expand_lock_rank,
coleenp@3602 724 SpaceManager::_expand_lock_name,
coleenp@3602 725 Mutex::_allow_vm_block_flag);
coleenp@3602 726
jmasa@4572 727 void VirtualSpaceNode::inc_container_count() {
jmasa@4572 728 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4572 729 _container_count++;
jmasa@4572 730 assert(_container_count == container_count_slow(),
jmasa@4572 731 err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
stefank@5336 732 " container_count_slow() " SIZE_FORMAT,
jmasa@4572 733 _container_count, container_count_slow()));
jmasa@4572 734 }
jmasa@4572 735
jmasa@4572 736 void VirtualSpaceNode::dec_container_count() {
jmasa@4572 737 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4572 738 _container_count--;
jmasa@4572 739 }
jmasa@4572 740
jmasa@4572 741 #ifdef ASSERT
jmasa@4572 742 void VirtualSpaceNode::verify_container_count() {
jmasa@4572 743 assert(_container_count == container_count_slow(),
jmasa@4572 744 err_msg("Inconsistency in countainer_count _container_count " SIZE_FORMAT
stefank@5336 745 " container_count_slow() " SIZE_FORMAT, _container_count, container_count_slow()));
jmasa@4572 746 }
jmasa@4572 747 #endif
jmasa@4572 748
coleenp@3602 749 // BlockFreelist methods
coleenp@3602 750
coleenp@3602 751 BlockFreelist::BlockFreelist() : _dictionary(NULL) {}
coleenp@3602 752
coleenp@3602 753 BlockFreelist::~BlockFreelist() {
coleenp@3602 754 if (_dictionary != NULL) {
coleenp@3602 755 if (Verbose && TraceMetadataChunkAllocation) {
coleenp@3602 756 _dictionary->print_free_lists(gclog_or_tty);
coleenp@3602 757 }
coleenp@3602 758 delete _dictionary;
coleenp@3602 759 }
coleenp@3602 760 }
coleenp@3602 761
jmasa@3761 762 void BlockFreelist::return_block(MetaWord* p, size_t word_size) {
stefank@5506 763 Metablock* free_chunk = ::new (p) Metablock(word_size);
coleenp@3602 764 if (dictionary() == NULL) {
jmasa@3761 765 _dictionary = new BlockTreeDictionary();
coleenp@3602 766 }
jmasa@3761 767 dictionary()->return_chunk(free_chunk);
coleenp@3602 768 }
coleenp@3602 769
jmasa@3761 770 MetaWord* BlockFreelist::get_block(size_t word_size) {
coleenp@3602 771 if (dictionary() == NULL) {
coleenp@3602 772 return NULL;
coleenp@3602 773 }
coleenp@3602 774
jmasa@3761 775 if (word_size < TreeChunk<Metablock, FreeList>::min_size()) {
jmasa@3761 776 // Dark matter. Too small for dictionary.
coleenp@3602 777 return NULL;
coleenp@3602 778 }
jmasa@3761 779
jmasa@3761 780 Metablock* free_block =
mgerdin@5264 781 dictionary()->get_chunk(word_size, FreeBlockDictionary<Metablock>::atLeast);
jmasa@3761 782 if (free_block == NULL) {
jmasa@3761 783 return NULL;
jmasa@3761 784 }
jmasa@3761 785
mgerdin@5264 786 const size_t block_size = free_block->size();
mgerdin@5264 787 if (block_size > WasteMultiplier * word_size) {
mgerdin@5264 788 return_block((MetaWord*)free_block, block_size);
mgerdin@5264 789 return NULL;
mgerdin@5264 790 }
mgerdin@5264 791
mgerdin@5264 792 MetaWord* new_block = (MetaWord*)free_block;
mgerdin@5264 793 assert(block_size >= word_size, "Incorrect size of block from freelist");
mgerdin@5264 794 const size_t unused = block_size - word_size;
mgerdin@5264 795 if (unused >= TreeChunk<Metablock, FreeList>::min_size()) {
mgerdin@5264 796 return_block(new_block + word_size, unused);
mgerdin@5264 797 }
mgerdin@5264 798
mgerdin@5264 799 return new_block;
coleenp@3602 800 }
coleenp@3602 801
coleenp@3602 802 void BlockFreelist::print_on(outputStream* st) const {
coleenp@3602 803 if (dictionary() == NULL) {
coleenp@3602 804 return;
coleenp@3602 805 }
coleenp@3602 806 dictionary()->print_free_lists(st);
coleenp@3602 807 }
coleenp@3602 808
coleenp@3602 809 // VirtualSpaceNode methods
coleenp@3602 810
coleenp@3602 811 VirtualSpaceNode::~VirtualSpaceNode() {
coleenp@3602 812 _rs.release();
jmasa@4572 813 #ifdef ASSERT
jmasa@4572 814 size_t word_size = sizeof(*this) / BytesPerWord;
jmasa@4572 815 Copy::fill_to_words((HeapWord*) this, word_size, 0xf1f1f1f1);
jmasa@4572 816 #endif
coleenp@3602 817 }
coleenp@3602 818
coleenp@3602 819 size_t VirtualSpaceNode::used_words_in_vs() const {
coleenp@3602 820 return pointer_delta(top(), bottom(), sizeof(MetaWord));
coleenp@3602 821 }
coleenp@3602 822
coleenp@3602 823 // Space committed in the VirtualSpace
coleenp@3602 824 size_t VirtualSpaceNode::capacity_words_in_vs() const {
coleenp@3602 825 return pointer_delta(end(), bottom(), sizeof(MetaWord));
coleenp@3602 826 }
coleenp@3602 827
jmasa@4580 828 size_t VirtualSpaceNode::free_words_in_vs() const {
jmasa@4580 829 return pointer_delta(end(), top(), sizeof(MetaWord));
jmasa@4580 830 }
coleenp@3602 831
coleenp@3602 832 // Allocates the chunk from the virtual space only.
coleenp@3602 833 // This interface is also used internally for debugging. Not all
coleenp@3602 834 // chunks removed here are necessarily used for allocation.
coleenp@3602 835 Metachunk* VirtualSpaceNode::take_from_committed(size_t chunk_word_size) {
coleenp@3602 836 // Bottom of the new chunk
coleenp@3602 837 MetaWord* chunk_limit = top();
coleenp@3602 838 assert(chunk_limit != NULL, "Not safe to call this method");
coleenp@3602 839
stefank@5428 840 // The virtual spaces are always expanded by the
stefank@5428 841 // commit granularity to enforce the following condition.
stefank@5428 842 // Without this the is_available check will not work correctly.
stefank@5428 843 assert(_virtual_space.committed_size() == _virtual_space.actual_committed_size(),
stefank@5428 844 "The committed memory doesn't match the expanded memory.");
stefank@5428 845
coleenp@3602 846 if (!is_available(chunk_word_size)) {
coleenp@3602 847 if (TraceMetadataChunkAllocation) {
stefank@5273 848 gclog_or_tty->print("VirtualSpaceNode::take_from_committed() not available %d words ", chunk_word_size);
coleenp@3602 849 // Dump some information about the virtual space that is nearly full
stefank@5273 850 print_on(gclog_or_tty);
coleenp@3602 851 }
coleenp@3602 852 return NULL;
coleenp@3602 853 }
coleenp@3602 854
coleenp@3602 855 // Take the space (bump top on the current virtual space).
coleenp@3602 856 inc_top(chunk_word_size);
coleenp@3602 857
jmasa@4572 858 // Initialize the chunk
jmasa@4572 859 Metachunk* result = ::new (chunk_limit) Metachunk(chunk_word_size, this);
coleenp@3602 860 return result;
coleenp@3602 861 }
coleenp@3602 862
coleenp@3602 863
coleenp@3602 864 // Expand the virtual space (commit more of the reserved space)
stefank@5428 865 bool VirtualSpaceNode::expand_by(size_t min_words, size_t preferred_words) {
stefank@5428 866 size_t min_bytes = min_words * BytesPerWord;
stefank@5428 867 size_t preferred_bytes = preferred_words * BytesPerWord;
stefank@5428 868
stefank@5428 869 size_t uncommitted = virtual_space()->reserved_size() - virtual_space()->actual_committed_size();
stefank@5428 870
stefank@5428 871 if (uncommitted < min_bytes) {
stefank@5428 872 return false;
coleenp@3602 873 }
stefank@5428 874
stefank@5428 875 size_t commit = MIN2(preferred_bytes, uncommitted);
stefank@5428 876 bool result = virtual_space()->expand_by(commit, false);
stefank@5428 877
stefank@5428 878 assert(result, "Failed to commit memory");
stefank@5428 879
coleenp@3602 880 return result;
coleenp@3602 881 }
coleenp@3602 882
coleenp@3602 883 Metachunk* VirtualSpaceNode::get_chunk_vs(size_t chunk_word_size) {
coleenp@3602 884 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4572 885 Metachunk* result = take_from_committed(chunk_word_size);
jmasa@4572 886 if (result != NULL) {
jmasa@4572 887 inc_container_count();
jmasa@4572 888 }
jmasa@4572 889 return result;
coleenp@3602 890 }
coleenp@3602 891
coleenp@3602 892 bool VirtualSpaceNode::initialize() {
coleenp@3602 893
coleenp@3602 894 if (!_rs.is_reserved()) {
coleenp@3602 895 return false;
coleenp@3602 896 }
coleenp@3602 897
stefank@5428 898 // These are necessary restriction to make sure that the virtual space always
stefank@5428 899 // grows in steps of Metaspace::commit_alignment(). If both base and size are
stefank@5428 900 // aligned only the middle alignment of the VirtualSpace is used.
stefank@5428 901 assert_is_ptr_aligned(_rs.base(), Metaspace::commit_alignment());
stefank@5428 902 assert_is_size_aligned(_rs.size(), Metaspace::commit_alignment());
stefank@5428 903
stefank@5428 904 // ReservedSpaces marked as special will have the entire memory
stefank@5428 905 // pre-committed. Setting a committed size will make sure that
stefank@5428 906 // committed_size and actual_committed_size agrees.
stefank@5428 907 size_t pre_committed_size = _rs.special() ? _rs.size() : 0;
stefank@5428 908
stefank@5428 909 bool result = virtual_space()->initialize_with_granularity(_rs, pre_committed_size,
stefank@5428 910 Metaspace::commit_alignment());
coleenp@3602 911 if (result) {
stefank@5428 912 assert(virtual_space()->committed_size() == virtual_space()->actual_committed_size(),
stefank@5428 913 "Checking that the pre-committed memory was registered by the VirtualSpace");
stefank@5428 914
coleenp@3602 915 set_top((MetaWord*)virtual_space()->low());
coleenp@3602 916 set_reserved(MemRegion((HeapWord*)_rs.base(),
coleenp@3602 917 (HeapWord*)(_rs.base() + _rs.size())));
coleenp@3603 918
coleenp@3603 919 assert(reserved()->start() == (HeapWord*) _rs.base(),
coleenp@3603 920 err_msg("Reserved start was not set properly " PTR_FORMAT
coleenp@3603 921 " != " PTR_FORMAT, reserved()->start(), _rs.base()));
coleenp@3603 922 assert(reserved()->word_size() == _rs.size() / BytesPerWord,
coleenp@3603 923 err_msg("Reserved size was not set properly " SIZE_FORMAT
coleenp@3603 924 " != " SIZE_FORMAT, reserved()->word_size(),
coleenp@3603 925 _rs.size() / BytesPerWord));
coleenp@3602 926 }
coleenp@3602 927
coleenp@3602 928 return result;
coleenp@3602 929 }
coleenp@3602 930
coleenp@3602 931 void VirtualSpaceNode::print_on(outputStream* st) const {
coleenp@3602 932 size_t used = used_words_in_vs();
coleenp@3602 933 size_t capacity = capacity_words_in_vs();
coleenp@3602 934 VirtualSpace* vs = virtual_space();
coleenp@3602 935 st->print_cr(" space @ " PTR_FORMAT " " SIZE_FORMAT "K, %3d%% used "
coleenp@3602 936 "[" PTR_FORMAT ", " PTR_FORMAT ", "
coleenp@3602 937 PTR_FORMAT ", " PTR_FORMAT ")",
jmasa@3947 938 vs, capacity / K,
jmasa@3947 939 capacity == 0 ? 0 : used * 100 / capacity,
coleenp@3602 940 bottom(), top(), end(),
coleenp@3602 941 vs->high_boundary());
coleenp@3602 942 }
coleenp@3602 943
coleenp@3869 944 #ifdef ASSERT
coleenp@3602 945 void VirtualSpaceNode::mangle() {
coleenp@3602 946 size_t word_size = capacity_words_in_vs();
coleenp@3602 947 Copy::fill_to_words((HeapWord*) low(), word_size, 0xf1f1f1f1);
coleenp@3602 948 }
coleenp@3869 949 #endif // ASSERT
coleenp@3602 950
coleenp@3602 951 // VirtualSpaceList methods
coleenp@3602 952 // Space allocated from the VirtualSpace
coleenp@3602 953
coleenp@3602 954 VirtualSpaceList::~VirtualSpaceList() {
coleenp@3602 955 VirtualSpaceListIterator iter(virtual_space_list());
coleenp@3602 956 while (iter.repeat()) {
coleenp@3602 957 VirtualSpaceNode* vsl = iter.get_next();
coleenp@3602 958 delete vsl;
coleenp@3602 959 }
coleenp@3602 960 }
coleenp@3602 961
stefank@5269 962 void VirtualSpaceList::inc_reserved_words(size_t v) {
jmasa@4572 963 assert_lock_strong(SpaceManager::expand_lock());
stefank@5269 964 _reserved_words = _reserved_words + v;
jmasa@4572 965 }
stefank@5269 966 void VirtualSpaceList::dec_reserved_words(size_t v) {
jmasa@4572 967 assert_lock_strong(SpaceManager::expand_lock());
stefank@5269 968 _reserved_words = _reserved_words - v;
stefank@5269 969 }
stefank@5269 970
stefank@5428 971 #define assert_committed_below_limit() \
stefank@5428 972 assert(MetaspaceAux::committed_bytes() <= MaxMetaspaceSize, \
stefank@5428 973 err_msg("Too much committed memory. Committed: " SIZE_FORMAT \
stefank@5428 974 " limit (MaxMetaspaceSize): " SIZE_FORMAT, \
stefank@5428 975 MetaspaceAux::committed_bytes(), MaxMetaspaceSize));
stefank@5428 976
stefank@5269 977 void VirtualSpaceList::inc_committed_words(size_t v) {
stefank@5269 978 assert_lock_strong(SpaceManager::expand_lock());
stefank@5269 979 _committed_words = _committed_words + v;
stefank@5428 980
stefank@5428 981 assert_committed_below_limit();
stefank@5269 982 }
stefank@5269 983 void VirtualSpaceList::dec_committed_words(size_t v) {
stefank@5269 984 assert_lock_strong(SpaceManager::expand_lock());
stefank@5269 985 _committed_words = _committed_words - v;
stefank@5428 986
stefank@5428 987 assert_committed_below_limit();
jmasa@4572 988 }
jmasa@4572 989
jmasa@4572 990 void VirtualSpaceList::inc_virtual_space_count() {
jmasa@4572 991 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4572 992 _virtual_space_count++;
jmasa@4572 993 }
jmasa@4572 994 void VirtualSpaceList::dec_virtual_space_count() {
jmasa@4572 995 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4572 996 _virtual_space_count--;
jmasa@4572 997 }
jmasa@4572 998
jmasa@4572 999 void ChunkManager::remove_chunk(Metachunk* chunk) {
jmasa@4572 1000 size_t word_size = chunk->word_size();
jmasa@4572 1001 ChunkIndex index = list_index(word_size);
jmasa@4572 1002 if (index != HumongousIndex) {
jmasa@4572 1003 free_chunks(index)->remove_chunk(chunk);
jmasa@4572 1004 } else {
jmasa@4572 1005 humongous_dictionary()->remove_chunk(chunk);
jmasa@4572 1006 }
jmasa@4572 1007
jmasa@4572 1008 // Chunk is being removed from the chunks free list.
stefank@5506 1009 dec_free_chunks_total(chunk->word_size());
jmasa@4572 1010 }
jmasa@4572 1011
jmasa@4572 1012 // Walk the list of VirtualSpaceNodes and delete
jmasa@4572 1013 // nodes with a 0 container_count. Remove Metachunks in
jmasa@4572 1014 // the node from their respective freelists.
stefank@5336 1015 void VirtualSpaceList::purge(ChunkManager* chunk_manager) {
jmasa@4572 1016 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4572 1017 // Don't use a VirtualSpaceListIterator because this
jmasa@4572 1018 // list is being changed and a straightforward use of an iterator is not safe.
jmasa@4572 1019 VirtualSpaceNode* purged_vsl = NULL;
jmasa@4572 1020 VirtualSpaceNode* prev_vsl = virtual_space_list();
jmasa@4572 1021 VirtualSpaceNode* next_vsl = prev_vsl;
jmasa@4572 1022 while (next_vsl != NULL) {
jmasa@4572 1023 VirtualSpaceNode* vsl = next_vsl;
jmasa@4572 1024 next_vsl = vsl->next();
jmasa@4572 1025 // Don't free the current virtual space since it will likely
jmasa@4572 1026 // be needed soon.
jmasa@4572 1027 if (vsl->container_count() == 0 && vsl != current_virtual_space()) {
jmasa@4572 1028 // Unlink it from the list
jmasa@4572 1029 if (prev_vsl == vsl) {
stefank@5428 1030 // This is the case of the current node being the first node.
stefank@5428 1031 assert(vsl == virtual_space_list(), "Expected to be the first node");
jmasa@4572 1032 set_virtual_space_list(vsl->next());
jmasa@4572 1033 } else {
jmasa@4572 1034 prev_vsl->set_next(vsl->next());
jmasa@4572 1035 }
jmasa@4572 1036
stefank@5336 1037 vsl->purge(chunk_manager);
stefank@5269 1038 dec_reserved_words(vsl->reserved_words());
stefank@5269 1039 dec_committed_words(vsl->committed_words());
jmasa@4572 1040 dec_virtual_space_count();
jmasa@4572 1041 purged_vsl = vsl;
jmasa@4572 1042 delete vsl;
jmasa@4572 1043 } else {
jmasa@4572 1044 prev_vsl = vsl;
jmasa@4572 1045 }
jmasa@4572 1046 }
jmasa@4572 1047 #ifdef ASSERT
jmasa@4572 1048 if (purged_vsl != NULL) {
jmasa@4572 1049 // List should be stable enough to use an iterator here.
jmasa@4572 1050 VirtualSpaceListIterator iter(virtual_space_list());
jmasa@4572 1051 while (iter.repeat()) {
jmasa@4572 1052 VirtualSpaceNode* vsl = iter.get_next();
jmasa@4572 1053 assert(vsl != purged_vsl, "Purge of vsl failed");
jmasa@4572 1054 }
jmasa@4572 1055 }
jmasa@4572 1056 #endif
jmasa@4572 1057 }
jmasa@4572 1058
stefank@5428 1059 VirtualSpaceList::VirtualSpaceList(size_t word_size) :
coleenp@3602 1060 _is_class(false),
coleenp@3602 1061 _virtual_space_list(NULL),
coleenp@3602 1062 _current_virtual_space(NULL),
stefank@5269 1063 _reserved_words(0),
stefank@5269 1064 _committed_words(0),
coleenp@3602 1065 _virtual_space_count(0) {
coleenp@3602 1066 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@3602 1067 Mutex::_no_safepoint_check_flag);
stefank@5428 1068 create_new_virtual_space(word_size);
coleenp@3602 1069 }
coleenp@3602 1070
coleenp@3602 1071 VirtualSpaceList::VirtualSpaceList(ReservedSpace rs) :
coleenp@3602 1072 _is_class(true),
coleenp@3602 1073 _virtual_space_list(NULL),
coleenp@3602 1074 _current_virtual_space(NULL),
stefank@5269 1075 _reserved_words(0),
stefank@5269 1076 _committed_words(0),
coleenp@3602 1077 _virtual_space_count(0) {
coleenp@3602 1078 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@3602 1079 Mutex::_no_safepoint_check_flag);
coleenp@3602 1080 VirtualSpaceNode* class_entry = new VirtualSpaceNode(rs);
coleenp@3602 1081 bool succeeded = class_entry->initialize();
stefank@5428 1082 if (succeeded) {
stefank@5428 1083 link_vs(class_entry);
stefank@5428 1084 }
coleenp@3602 1085 }
coleenp@3602 1086
jmasa@4580 1087 size_t VirtualSpaceList::free_bytes() {
jmasa@4580 1088 return virtual_space_list()->free_words_in_vs() * BytesPerWord;
jmasa@4580 1089 }
jmasa@4580 1090
coleenp@3602 1091 // Allocate another meta virtual space and add it to the list.
stefank@5428 1092 bool VirtualSpaceList::create_new_virtual_space(size_t vs_word_size) {
coleenp@3602 1093 assert_lock_strong(SpaceManager::expand_lock());
stefank@5428 1094
stefank@5428 1095 if (is_class()) {
stefank@5428 1096 assert(false, "We currently don't support more than one VirtualSpace for"
stefank@5428 1097 " the compressed class space. The initialization of the"
stefank@5428 1098 " CCS uses another code path and should not hit this path.");
coleenp@3602 1099 return false;
coleenp@3602 1100 }
stefank@5428 1101
stefank@5428 1102 if (vs_word_size == 0) {
stefank@5428 1103 assert(false, "vs_word_size should always be at least _reserve_alignment large.");
stefank@5428 1104 return false;
stefank@5428 1105 }
stefank@5428 1106
coleenp@3602 1107 // Reserve the space
coleenp@3602 1108 size_t vs_byte_size = vs_word_size * BytesPerWord;
stefank@5428 1109 assert_is_size_aligned(vs_byte_size, Metaspace::reserve_alignment());
coleenp@3602 1110
coleenp@3602 1111 // Allocate the meta virtual space and initialize it.
coleenp@3602 1112 VirtualSpaceNode* new_entry = new VirtualSpaceNode(vs_byte_size);
coleenp@3602 1113 if (!new_entry->initialize()) {
coleenp@3602 1114 delete new_entry;
coleenp@3602 1115 return false;
coleenp@3602 1116 } else {
stefank@5428 1117 assert(new_entry->reserved_words() == vs_word_size,
stefank@5428 1118 "Reserved memory size differs from requested memory size");
coleenp@3860 1119 // ensure lock-free iteration sees fully initialized node
coleenp@3860 1120 OrderAccess::storestore();
stefank@5269 1121 link_vs(new_entry);
coleenp@3602 1122 return true;
coleenp@3602 1123 }
coleenp@3602 1124 }
coleenp@3602 1125
stefank@5269 1126 void VirtualSpaceList::link_vs(VirtualSpaceNode* new_entry) {
coleenp@3602 1127 if (virtual_space_list() == NULL) {
coleenp@3602 1128 set_virtual_space_list(new_entry);
coleenp@3602 1129 } else {
coleenp@3602 1130 current_virtual_space()->set_next(new_entry);
coleenp@3602 1131 }
coleenp@3602 1132 set_current_virtual_space(new_entry);
stefank@5269 1133 inc_reserved_words(new_entry->reserved_words());
stefank@5269 1134 inc_committed_words(new_entry->committed_words());
coleenp@3602 1135 inc_virtual_space_count();
coleenp@3602 1136 #ifdef ASSERT
coleenp@3602 1137 new_entry->mangle();
coleenp@3602 1138 #endif
coleenp@3602 1139 if (TraceMetavirtualspaceAllocation && Verbose) {
coleenp@3602 1140 VirtualSpaceNode* vsl = current_virtual_space();
stefank@5273 1141 vsl->print_on(gclog_or_tty);
coleenp@3602 1142 }
coleenp@3602 1143 }
coleenp@3602 1144
stefank@5428 1145 bool VirtualSpaceList::expand_node_by(VirtualSpaceNode* node,
stefank@5428 1146 size_t min_words,
stefank@5428 1147 size_t preferred_words) {
stefank@5269 1148 size_t before = node->committed_words();
stefank@5269 1149
stefank@5428 1150 bool result = node->expand_by(min_words, preferred_words);
stefank@5269 1151
stefank@5269 1152 size_t after = node->committed_words();
stefank@5269 1153
stefank@5269 1154 // after and before can be the same if the memory was pre-committed.
stefank@5428 1155 assert(after >= before, "Inconsistency");
stefank@5269 1156 inc_committed_words(after - before);
stefank@5269 1157
stefank@5269 1158 return result;
stefank@5269 1159 }
stefank@5269 1160
stefank@5428 1161 bool VirtualSpaceList::expand_by(size_t min_words, size_t preferred_words) {
stefank@5428 1162 assert_is_size_aligned(min_words, Metaspace::commit_alignment_words());
stefank@5428 1163 assert_is_size_aligned(preferred_words, Metaspace::commit_alignment_words());
stefank@5428 1164 assert(min_words <= preferred_words, "Invalid arguments");
stefank@5428 1165
stefank@5428 1166 if (!MetaspaceGC::can_expand(min_words, this->is_class())) {
stefank@5428 1167 return false;
stefank@5428 1168 }
stefank@5428 1169
stefank@5428 1170 size_t allowed_expansion_words = MetaspaceGC::allowed_expansion();
stefank@5428 1171 if (allowed_expansion_words < min_words) {
stefank@5428 1172 return false;
stefank@5428 1173 }
stefank@5428 1174
stefank@5428 1175 size_t max_expansion_words = MIN2(preferred_words, allowed_expansion_words);
stefank@5428 1176
stefank@5428 1177 // Commit more memory from the the current virtual space.
stefank@5428 1178 bool vs_expanded = expand_node_by(current_virtual_space(),
stefank@5428 1179 min_words,
stefank@5428 1180 max_expansion_words);
stefank@5428 1181 if (vs_expanded) {
stefank@5428 1182 return true;
stefank@5428 1183 }
stefank@5428 1184
stefank@5428 1185 // Get another virtual space.
stefank@5428 1186 size_t grow_vs_words = MAX2((size_t)VirtualSpaceSize, preferred_words);
stefank@5428 1187 grow_vs_words = align_size_up(grow_vs_words, Metaspace::reserve_alignment_words());
stefank@5428 1188
stefank@5428 1189 if (create_new_virtual_space(grow_vs_words)) {
stefank@5428 1190 if (current_virtual_space()->is_pre_committed()) {
stefank@5428 1191 // The memory was pre-committed, so we are done here.
stefank@5428 1192 assert(min_words <= current_virtual_space()->committed_words(),
stefank@5428 1193 "The new VirtualSpace was pre-committed, so it"
stefank@5428 1194 "should be large enough to fit the alloc request.");
stefank@5428 1195 return true;
stefank@5428 1196 }
stefank@5428 1197
stefank@5428 1198 return expand_node_by(current_virtual_space(),
stefank@5428 1199 min_words,
stefank@5428 1200 max_expansion_words);
stefank@5428 1201 }
stefank@5428 1202
stefank@5428 1203 return false;
stefank@5428 1204 }
stefank@5428 1205
coleenp@3602 1206 Metachunk* VirtualSpaceList::get_new_chunk(size_t word_size,
jmasa@3947 1207 size_t grow_chunks_by_words,
jmasa@3947 1208 size_t medium_chunk_bunch) {
coleenp@3602 1209
stefank@5336 1210 // Allocate a chunk out of the current virtual space.
stefank@5336 1211 Metachunk* next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
coleenp@3602 1212
stefank@5428 1213 if (next != NULL) {
stefank@5428 1214 return next;
coleenp@3602 1215 }
coleenp@3602 1216
stefank@5428 1217 // The expand amount is currently only determined by the requested sizes
stefank@5428 1218 // and not how much committed memory is left in the current virtual space.
stefank@5428 1219
stefank@5428 1220 size_t min_word_size = align_size_up(grow_chunks_by_words, Metaspace::commit_alignment_words());
stefank@5428 1221 size_t preferred_word_size = align_size_up(medium_chunk_bunch, Metaspace::commit_alignment_words());
stefank@5428 1222 if (min_word_size >= preferred_word_size) {
stefank@5428 1223 // Can happen when humongous chunks are allocated.
stefank@5428 1224 preferred_word_size = min_word_size;
stefank@5428 1225 }
stefank@5428 1226
stefank@5428 1227 bool expanded = expand_by(min_word_size, preferred_word_size);
stefank@5428 1228 if (expanded) {
stefank@5428 1229 next = current_virtual_space()->get_chunk_vs(grow_chunks_by_words);
stefank@5428 1230 assert(next != NULL, "The allocation was expected to succeed after the expansion");
stefank@5428 1231 }
stefank@5428 1232
stefank@5428 1233 return next;
jmasa@3947 1234 }
jmasa@3947 1235
coleenp@3602 1236 void VirtualSpaceList::print_on(outputStream* st) const {
coleenp@3602 1237 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@3602 1238 VirtualSpaceListIterator iter(virtual_space_list());
coleenp@3602 1239 while (iter.repeat()) {
coleenp@3602 1240 VirtualSpaceNode* node = iter.get_next();
coleenp@3602 1241 node->print_on(st);
coleenp@3602 1242 }
coleenp@3602 1243 }
coleenp@3602 1244 }
coleenp@3602 1245
coleenp@3602 1246 bool VirtualSpaceList::contains(const void *ptr) {
coleenp@3602 1247 VirtualSpaceNode* list = virtual_space_list();
coleenp@3602 1248 VirtualSpaceListIterator iter(list);
coleenp@3602 1249 while (iter.repeat()) {
coleenp@3602 1250 VirtualSpaceNode* node = iter.get_next();
coleenp@3602 1251 if (node->reserved()->contains(ptr)) {
coleenp@3602 1252 return true;
coleenp@3602 1253 }
coleenp@3602 1254 }
coleenp@3602 1255 return false;
coleenp@3602 1256 }
coleenp@3602 1257
coleenp@3602 1258
coleenp@3602 1259 // MetaspaceGC methods
coleenp@3602 1260
coleenp@3602 1261 // VM_CollectForMetadataAllocation is the vm operation used to GC.
coleenp@3602 1262 // Within the VM operation after the GC the attempt to allocate the metadata
coleenp@3602 1263 // should succeed. If the GC did not free enough space for the metaspace
coleenp@3602 1264 // allocation, the HWM is increased so that another virtualspace will be
coleenp@3602 1265 // allocated for the metadata. With perm gen the increase in the perm
coleenp@3602 1266 // gen had bounds, MinMetaspaceExpansion and MaxMetaspaceExpansion. The
coleenp@3602 1267 // metaspace policy uses those as the small and large steps for the HWM.
coleenp@3602 1268 //
coleenp@3602 1269 // After the GC the compute_new_size() for MetaspaceGC is called to
coleenp@3602 1270 // resize the capacity of the metaspaces. The current implementation
jmasa@4580 1271 // is based on the flags MinMetaspaceFreeRatio and MaxMetaspaceFreeRatio used
coleenp@3602 1272 // to resize the Java heap by some GC's. New flags can be implemented
jmasa@4580 1273 // if really needed. MinMetaspaceFreeRatio is used to calculate how much
coleenp@3602 1274 // free space is desirable in the metaspace capacity to decide how much
jmasa@4146 1275 // to increase the HWM. MaxMetaspaceFreeRatio is used to decide how much
coleenp@3602 1276 // free space is desirable in the metaspace capacity before decreasing
coleenp@3602 1277 // the HWM.
coleenp@3602 1278
coleenp@3602 1279 // Calculate the amount to increase the high water mark (HWM).
coleenp@3602 1280 // Increase by a minimum amount (MinMetaspaceExpansion) so that
coleenp@3602 1281 // another expansion is not requested too soon. If that is not
stefank@5428 1282 // enough to satisfy the allocation, increase by MaxMetaspaceExpansion.
stefank@5428 1283 // If that is still not enough, expand by the size of the allocation
stefank@5428 1284 // plus some.
stefank@5428 1285 size_t MetaspaceGC::delta_capacity_until_GC(size_t bytes) {
stefank@5428 1286 size_t min_delta = MinMetaspaceExpansion;
stefank@5428 1287 size_t max_delta = MaxMetaspaceExpansion;
stefank@5428 1288 size_t delta = align_size_up(bytes, Metaspace::commit_alignment());
stefank@5428 1289
stefank@5428 1290 if (delta <= min_delta) {
stefank@5428 1291 delta = min_delta;
stefank@5428 1292 } else if (delta <= max_delta) {
coleenp@3602 1293 // Don't want to hit the high water mark on the next
coleenp@3602 1294 // allocation so make the delta greater than just enough
coleenp@3602 1295 // for this allocation.
stefank@5428 1296 delta = max_delta;
stefank@5428 1297 } else {
stefank@5428 1298 // This allocation is large but the next ones are probably not
stefank@5428 1299 // so increase by the minimum.
stefank@5428 1300 delta = delta + min_delta;
coleenp@3602 1301 }
stefank@5428 1302
stefank@5428 1303 assert_is_size_aligned(delta, Metaspace::commit_alignment());
stefank@5428 1304
stefank@5428 1305 return delta;
coleenp@3602 1306 }
coleenp@3602 1307
stefank@5428 1308 size_t MetaspaceGC::capacity_until_GC() {
stefank@5428 1309 size_t value = (size_t)OrderAccess::load_ptr_acquire(&_capacity_until_GC);
stefank@5428 1310 assert(value >= MetaspaceSize, "Not initialied properly?");
stefank@5428 1311 return value;
stefank@5428 1312 }
stefank@5428 1313
stefank@5428 1314 size_t MetaspaceGC::inc_capacity_until_GC(size_t v) {
stefank@5428 1315 assert_is_size_aligned(v, Metaspace::commit_alignment());
stefank@5428 1316
stefank@5428 1317 return (size_t)Atomic::add_ptr(v, &_capacity_until_GC);
stefank@5428 1318 }
stefank@5428 1319
stefank@5428 1320 size_t MetaspaceGC::dec_capacity_until_GC(size_t v) {
stefank@5428 1321 assert_is_size_aligned(v, Metaspace::commit_alignment());
stefank@5428 1322
stefank@5428 1323 return (size_t)Atomic::add_ptr(-(intptr_t)v, &_capacity_until_GC);
stefank@5428 1324 }
stefank@5428 1325
stefank@5428 1326 bool MetaspaceGC::can_expand(size_t word_size, bool is_class) {
stefank@5428 1327 // Check if the compressed class space is full.
stefank@5428 1328 if (is_class && Metaspace::using_class_space()) {
stefank@5428 1329 size_t class_committed = MetaspaceAux::committed_bytes(Metaspace::ClassType);
stefank@5428 1330 if (class_committed + word_size * BytesPerWord > CompressedClassSpaceSize) {
coleenp@4902 1331 return false;
coleenp@4902 1332 }
mgerdin@4355 1333 }
coleenp@3602 1334
stefank@5428 1335 // Check if the user has imposed a limit on the metaspace memory.
stefank@5428 1336 size_t committed_bytes = MetaspaceAux::committed_bytes();
stefank@5428 1337 if (committed_bytes + word_size * BytesPerWord > MaxMetaspaceSize) {
stefank@5428 1338 return false;
coleenp@3602 1339 }
coleenp@3602 1340
stefank@5428 1341 return true;
stefank@5428 1342 }
stefank@5428 1343
stefank@5428 1344 size_t MetaspaceGC::allowed_expansion() {
stefank@5428 1345 size_t committed_bytes = MetaspaceAux::committed_bytes();
stefank@5428 1346
stefank@5428 1347 size_t left_until_max = MaxMetaspaceSize - committed_bytes;
stefank@5428 1348
stefank@5428 1349 // Always grant expansion if we are initiating the JVM,
stefank@5428 1350 // or if the GC_locker is preventing GCs.
stefank@5428 1351 if (!is_init_completed() || GC_locker::is_active_and_needs_gc()) {
stefank@5428 1352 return left_until_max / BytesPerWord;
coleenp@3602 1353 }
stefank@5428 1354
stefank@5428 1355 size_t capacity_until_gc = capacity_until_GC();
stefank@5428 1356
stefank@5428 1357 if (capacity_until_gc <= committed_bytes) {
stefank@5428 1358 return 0;
stefank@5428 1359 }
stefank@5428 1360
stefank@5428 1361 size_t left_until_GC = capacity_until_gc - committed_bytes;
stefank@5428 1362 size_t left_to_commit = MIN2(left_until_GC, left_until_max);
stefank@5428 1363
stefank@5428 1364 return left_to_commit / BytesPerWord;
coleenp@3602 1365 }
coleenp@3602 1366
coleenp@3602 1367 void MetaspaceGC::compute_new_size() {
coleenp@3602 1368 assert(_shrink_factor <= 100, "invalid shrink factor");
coleenp@3602 1369 uint current_shrink_factor = _shrink_factor;
coleenp@3602 1370 _shrink_factor = 0;
coleenp@3602 1371
jmasa@4580 1372 const size_t used_after_gc = MetaspaceAux::allocated_capacity_bytes();
jmasa@4580 1373 const size_t capacity_until_GC = MetaspaceGC::capacity_until_GC();
coleenp@3602 1374
jmasa@4146 1375 const double minimum_free_percentage = MinMetaspaceFreeRatio / 100.0;
coleenp@3602 1376 const double maximum_used_percentage = 1.0 - minimum_free_percentage;
coleenp@3602 1377
coleenp@3602 1378 const double min_tmp = used_after_gc / maximum_used_percentage;
coleenp@3602 1379 size_t minimum_desired_capacity =
coleenp@3602 1380 (size_t)MIN2(min_tmp, double(max_uintx));
coleenp@3602 1381 // Don't shrink less than the initial generation size
coleenp@3602 1382 minimum_desired_capacity = MAX2(minimum_desired_capacity,
coleenp@3602 1383 MetaspaceSize);
coleenp@3602 1384
coleenp@3602 1385 if (PrintGCDetails && Verbose) {
coleenp@3602 1386 gclog_or_tty->print_cr("\nMetaspaceGC::compute_new_size: ");
coleenp@3602 1387 gclog_or_tty->print_cr(" "
coleenp@3602 1388 " minimum_free_percentage: %6.2f"
coleenp@3602 1389 " maximum_used_percentage: %6.2f",
coleenp@3602 1390 minimum_free_percentage,
coleenp@3602 1391 maximum_used_percentage);
coleenp@3602 1392 gclog_or_tty->print_cr(" "
jmasa@4580 1393 " used_after_gc : %6.1fKB",
jmasa@4580 1394 used_after_gc / (double) K);
coleenp@3602 1395 }
coleenp@3602 1396
coleenp@3602 1397
jmasa@4580 1398 size_t shrink_bytes = 0;
coleenp@3602 1399 if (capacity_until_GC < minimum_desired_capacity) {
coleenp@3602 1400 // If we have less capacity below the metaspace HWM, then
coleenp@3602 1401 // increment the HWM.
coleenp@3602 1402 size_t expand_bytes = minimum_desired_capacity - capacity_until_GC;
stefank@5428 1403 expand_bytes = align_size_up(expand_bytes, Metaspace::commit_alignment());
coleenp@3602 1404 // Don't expand unless it's significant
coleenp@3602 1405 if (expand_bytes >= MinMetaspaceExpansion) {
stefank@5428 1406 MetaspaceGC::inc_capacity_until_GC(expand_bytes);
coleenp@3602 1407 }
coleenp@3602 1408 if (PrintGCDetails && Verbose) {
jmasa@4580 1409 size_t new_capacity_until_GC = capacity_until_GC;
coleenp@3602 1410 gclog_or_tty->print_cr(" expanding:"
jmasa@4580 1411 " minimum_desired_capacity: %6.1fKB"
jmasa@4580 1412 " expand_bytes: %6.1fKB"
jmasa@4580 1413 " MinMetaspaceExpansion: %6.1fKB"
jmasa@4580 1414 " new metaspace HWM: %6.1fKB",
coleenp@3602 1415 minimum_desired_capacity / (double) K,
coleenp@3602 1416 expand_bytes / (double) K,
coleenp@3602 1417 MinMetaspaceExpansion / (double) K,
coleenp@3602 1418 new_capacity_until_GC / (double) K);
coleenp@3602 1419 }
coleenp@3602 1420 return;
coleenp@3602 1421 }
coleenp@3602 1422
coleenp@3602 1423 // No expansion, now see if we want to shrink
coleenp@3602 1424 // We would never want to shrink more than this
jmasa@4580 1425 size_t max_shrink_bytes = capacity_until_GC - minimum_desired_capacity;
jmasa@4580 1426 assert(max_shrink_bytes >= 0, err_msg("max_shrink_bytes " SIZE_FORMAT,
jmasa@4580 1427 max_shrink_bytes));
coleenp@3602 1428
coleenp@3602 1429 // Should shrinking be considered?
jmasa@4146 1430 if (MaxMetaspaceFreeRatio < 100) {
jmasa@4146 1431 const double maximum_free_percentage = MaxMetaspaceFreeRatio / 100.0;
coleenp@3602 1432 const double minimum_used_percentage = 1.0 - maximum_free_percentage;
coleenp@3602 1433 const double max_tmp = used_after_gc / minimum_used_percentage;
coleenp@3602 1434 size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
coleenp@3602 1435 maximum_desired_capacity = MAX2(maximum_desired_capacity,
coleenp@3602 1436 MetaspaceSize);
jmasa@4580 1437 if (PrintGCDetails && Verbose) {
coleenp@3602 1438 gclog_or_tty->print_cr(" "
coleenp@3602 1439 " maximum_free_percentage: %6.2f"
coleenp@3602 1440 " minimum_used_percentage: %6.2f",
coleenp@3602 1441 maximum_free_percentage,
coleenp@3602 1442 minimum_used_percentage);
coleenp@3602 1443 gclog_or_tty->print_cr(" "
jmasa@4580 1444 " minimum_desired_capacity: %6.1fKB"
jmasa@4580 1445 " maximum_desired_capacity: %6.1fKB",
coleenp@3602 1446 minimum_desired_capacity / (double) K,
coleenp@3602 1447 maximum_desired_capacity / (double) K);
coleenp@3602 1448 }
coleenp@3602 1449
coleenp@3602 1450 assert(minimum_desired_capacity <= maximum_desired_capacity,
coleenp@3602 1451 "sanity check");
coleenp@3602 1452
coleenp@3602 1453 if (capacity_until_GC > maximum_desired_capacity) {
coleenp@3602 1454 // Capacity too large, compute shrinking size
jmasa@4580 1455 shrink_bytes = capacity_until_GC - maximum_desired_capacity;
coleenp@3602 1456 // We don't want shrink all the way back to initSize if people call
coleenp@3602 1457 // System.gc(), because some programs do that between "phases" and then
coleenp@3602 1458 // we'd just have to grow the heap up again for the next phase. So we
coleenp@3602 1459 // damp the shrinking: 0% on the first call, 10% on the second call, 40%
coleenp@3602 1460 // on the third call, and 100% by the fourth call. But if we recompute
coleenp@3602 1461 // size without shrinking, it goes back to 0%.
jmasa@4580 1462 shrink_bytes = shrink_bytes / 100 * current_shrink_factor;
stefank@5428 1463
stefank@5428 1464 shrink_bytes = align_size_down(shrink_bytes, Metaspace::commit_alignment());
stefank@5428 1465
jmasa@4580 1466 assert(shrink_bytes <= max_shrink_bytes,
coleenp@3602 1467 err_msg("invalid shrink size " SIZE_FORMAT " not <= " SIZE_FORMAT,
jmasa@4580 1468 shrink_bytes, max_shrink_bytes));
coleenp@3602 1469 if (current_shrink_factor == 0) {
coleenp@3602 1470 _shrink_factor = 10;
coleenp@3602 1471 } else {
coleenp@3602 1472 _shrink_factor = MIN2(current_shrink_factor * 4, (uint) 100);
coleenp@3602 1473 }
coleenp@3602 1474 if (PrintGCDetails && Verbose) {
coleenp@3602 1475 gclog_or_tty->print_cr(" "
coleenp@3602 1476 " shrinking:"
coleenp@3602 1477 " initSize: %.1fK"
coleenp@3602 1478 " maximum_desired_capacity: %.1fK",
coleenp@3602 1479 MetaspaceSize / (double) K,
coleenp@3602 1480 maximum_desired_capacity / (double) K);
coleenp@3602 1481 gclog_or_tty->print_cr(" "
jmasa@4580 1482 " shrink_bytes: %.1fK"
coleenp@3602 1483 " current_shrink_factor: %d"
coleenp@3602 1484 " new shrink factor: %d"
coleenp@3602 1485 " MinMetaspaceExpansion: %.1fK",
jmasa@4580 1486 shrink_bytes / (double) K,
coleenp@3602 1487 current_shrink_factor,
coleenp@3602 1488 _shrink_factor,
coleenp@3602 1489 MinMetaspaceExpansion / (double) K);
coleenp@3602 1490 }
coleenp@3602 1491 }
coleenp@3602 1492 }
coleenp@3602 1493
coleenp@3602 1494 // Don't shrink unless it's significant
jmasa@4580 1495 if (shrink_bytes >= MinMetaspaceExpansion &&
jmasa@4580 1496 ((capacity_until_GC - shrink_bytes) >= MetaspaceSize)) {
stefank@5428 1497 MetaspaceGC::dec_capacity_until_GC(shrink_bytes);
coleenp@3602 1498 }
coleenp@3602 1499 }
coleenp@3602 1500
coleenp@3602 1501 // Metadebug methods
coleenp@3602 1502
coleenp@3602 1503 void Metadebug::init_allocation_fail_alot_count() {
coleenp@3602 1504 if (MetadataAllocationFailALot) {
coleenp@3602 1505 _allocation_fail_alot_count =
coleenp@3602 1506 1+(long)((double)MetadataAllocationFailALotInterval*os::random()/(max_jint+1.0));
coleenp@3602 1507 }
coleenp@3602 1508 }
coleenp@3602 1509
coleenp@3602 1510 #ifdef ASSERT
coleenp@3602 1511 bool Metadebug::test_metadata_failure() {
coleenp@3602 1512 if (MetadataAllocationFailALot &&
coleenp@3602 1513 Threads::is_vm_complete()) {
coleenp@3602 1514 if (_allocation_fail_alot_count > 0) {
coleenp@3602 1515 _allocation_fail_alot_count--;
coleenp@3602 1516 } else {
coleenp@3602 1517 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@3602 1518 gclog_or_tty->print_cr("Metadata allocation failing for "
coleenp@3602 1519 "MetadataAllocationFailALot");
coleenp@3602 1520 }
coleenp@3602 1521 init_allocation_fail_alot_count();
coleenp@3602 1522 return true;
coleenp@3602 1523 }
coleenp@3602 1524 }
coleenp@3602 1525 return false;
coleenp@3602 1526 }
coleenp@3602 1527 #endif
coleenp@3602 1528
coleenp@3602 1529 // ChunkManager methods
coleenp@3602 1530
ehelin@5268 1531 size_t ChunkManager::free_chunks_total_words() {
coleenp@3602 1532 return _free_chunks_total;
coleenp@3602 1533 }
coleenp@3602 1534
ehelin@5268 1535 size_t ChunkManager::free_chunks_total_bytes() {
ehelin@5268 1536 return free_chunks_total_words() * BytesPerWord;
coleenp@3602 1537 }
coleenp@3602 1538
coleenp@3602 1539 size_t ChunkManager::free_chunks_count() {
coleenp@3602 1540 #ifdef ASSERT
coleenp@3602 1541 if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
coleenp@3602 1542 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@3602 1543 Mutex::_no_safepoint_check_flag);
coleenp@3602 1544 // This lock is only needed in debug because the verification
coleenp@3602 1545 // of the _free_chunks_totals walks the list of free chunks
mgerdin@3829 1546 slow_locked_verify_free_chunks_count();
coleenp@3602 1547 }
coleenp@3602 1548 #endif
mgerdin@3829 1549 return _free_chunks_count;
coleenp@3602 1550 }
coleenp@3602 1551
coleenp@3602 1552 void ChunkManager::locked_verify_free_chunks_total() {
coleenp@3602 1553 assert_lock_strong(SpaceManager::expand_lock());
coleenp@3602 1554 assert(sum_free_chunks() == _free_chunks_total,
coleenp@3602 1555 err_msg("_free_chunks_total " SIZE_FORMAT " is not the"
coleenp@3602 1556 " same as sum " SIZE_FORMAT, _free_chunks_total,
coleenp@3602 1557 sum_free_chunks()));
coleenp@3602 1558 }
coleenp@3602 1559
coleenp@3602 1560 void ChunkManager::verify_free_chunks_total() {
coleenp@3602 1561 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@3602 1562 Mutex::_no_safepoint_check_flag);
coleenp@3602 1563 locked_verify_free_chunks_total();
coleenp@3602 1564 }
coleenp@3602 1565
coleenp@3602 1566 void ChunkManager::locked_verify_free_chunks_count() {
coleenp@3602 1567 assert_lock_strong(SpaceManager::expand_lock());
coleenp@3602 1568 assert(sum_free_chunks_count() == _free_chunks_count,
coleenp@3602 1569 err_msg("_free_chunks_count " SIZE_FORMAT " is not the"
coleenp@3602 1570 " same as sum " SIZE_FORMAT, _free_chunks_count,
coleenp@3602 1571 sum_free_chunks_count()));
coleenp@3602 1572 }
coleenp@3602 1573
coleenp@3602 1574 void ChunkManager::verify_free_chunks_count() {
coleenp@3602 1575 #ifdef ASSERT
coleenp@3602 1576 MutexLockerEx cl(SpaceManager::expand_lock(),
coleenp@3602 1577 Mutex::_no_safepoint_check_flag);
coleenp@3602 1578 locked_verify_free_chunks_count();
coleenp@3602 1579 #endif
coleenp@3602 1580 }
coleenp@3602 1581
coleenp@3602 1582 void ChunkManager::verify() {
mgerdin@3829 1583 MutexLockerEx cl(SpaceManager::expand_lock(),
mgerdin@3829 1584 Mutex::_no_safepoint_check_flag);
mgerdin@3829 1585 locked_verify();
coleenp@3602 1586 }
coleenp@3602 1587
coleenp@3602 1588 void ChunkManager::locked_verify() {
jmasa@3761 1589 locked_verify_free_chunks_count();
coleenp@3602 1590 locked_verify_free_chunks_total();
coleenp@3602 1591 }
coleenp@3602 1592
coleenp@3602 1593 void ChunkManager::locked_print_free_chunks(outputStream* st) {
coleenp@3602 1594 assert_lock_strong(SpaceManager::expand_lock());
jmasa@3947 1595 st->print_cr("Free chunk total " SIZE_FORMAT " count " SIZE_FORMAT,
coleenp@3602 1596 _free_chunks_total, _free_chunks_count);
coleenp@3602 1597 }
coleenp@3602 1598
coleenp@3602 1599 void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
coleenp@3602 1600 assert_lock_strong(SpaceManager::expand_lock());
jmasa@3947 1601 st->print_cr("Sum free chunk total " SIZE_FORMAT " count " SIZE_FORMAT,
coleenp@3602 1602 sum_free_chunks(), sum_free_chunks_count());
coleenp@3602 1603 }
coleenp@3602 1604 ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
coleenp@3602 1605 return &_free_chunks[index];
coleenp@3602 1606 }
coleenp@3602 1607
coleenp@3602 1608 // These methods that sum the free chunk lists are used in printing
coleenp@3602 1609 // methods that are used in product builds.
coleenp@3602 1610 size_t ChunkManager::sum_free_chunks() {
coleenp@3602 1611 assert_lock_strong(SpaceManager::expand_lock());
coleenp@3602 1612 size_t result = 0;
jmasa@3947 1613 for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
coleenp@3602 1614 ChunkList* list = free_chunks(i);
coleenp@3602 1615
coleenp@3602 1616 if (list == NULL) {
coleenp@3602 1617 continue;
coleenp@3602 1618 }
coleenp@3602 1619
jmasa@4497 1620 result = result + list->count() * list->size();
coleenp@3602 1621 }
jmasa@3761 1622 result = result + humongous_dictionary()->total_size();
coleenp@3602 1623 return result;
coleenp@3602 1624 }
coleenp@3602 1625
coleenp@3602 1626 size_t ChunkManager::sum_free_chunks_count() {
coleenp@3602 1627 assert_lock_strong(SpaceManager::expand_lock());
coleenp@3602 1628 size_t count = 0;
jmasa@3947 1629 for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
coleenp@3602 1630 ChunkList* list = free_chunks(i);
coleenp@3602 1631 if (list == NULL) {
coleenp@3602 1632 continue;
coleenp@3602 1633 }
jmasa@4497 1634 count = count + list->count();
coleenp@3602 1635 }
jmasa@3761 1636 count = count + humongous_dictionary()->total_free_blocks();
coleenp@3602 1637 return count;
coleenp@3602 1638 }
coleenp@3602 1639
coleenp@3602 1640 ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) {
jmasa@3947 1641 ChunkIndex index = list_index(word_size);
jmasa@3947 1642 assert(index < HumongousIndex, "No humongous list");
jmasa@3947 1643 return free_chunks(index);
coleenp@3602 1644 }
coleenp@3602 1645
coleenp@3602 1646 Metachunk* ChunkManager::free_chunks_get(size_t word_size) {
coleenp@3602 1647 assert_lock_strong(SpaceManager::expand_lock());
coleenp@3602 1648
mgerdin@3829 1649 slow_locked_verify();
jmasa@3761 1650
jmasa@3761 1651 Metachunk* chunk = NULL;
jmasa@3947 1652 if (list_index(word_size) != HumongousIndex) {
jmasa@3761 1653 ChunkList* free_list = find_free_chunks_list(word_size);
jmasa@3761 1654 assert(free_list != NULL, "Sanity check");
jmasa@3761 1655
jmasa@3761 1656 chunk = free_list->head();
jmasa@3761 1657
jmasa@3761 1658 if (chunk == NULL) {
jmasa@3761 1659 return NULL;
jmasa@3761 1660 }
jmasa@3761 1661
coleenp@3602 1662 // Remove the chunk as the head of the list.
jmasa@4497 1663 free_list->remove_chunk(chunk);
jmasa@3947 1664
coleenp@3602 1665 if (TraceMetadataChunkAllocation && Verbose) {
stefank@5273 1666 gclog_or_tty->print_cr("ChunkManager::free_chunks_get: free_list "
stefank@5273 1667 PTR_FORMAT " head " PTR_FORMAT " size " SIZE_FORMAT,
stefank@5273 1668 free_list, chunk, chunk->word_size());
coleenp@3602 1669 }
coleenp@3602 1670 } else {
jmasa@3761 1671 chunk = humongous_dictionary()->get_chunk(
jmasa@3761 1672 word_size,
jmasa@3761 1673 FreeBlockDictionary<Metachunk>::atLeast);
jmasa@3761 1674
stefank@5428 1675 if (chunk == NULL) {
jmasa@3947 1676 return NULL;
coleenp@3602 1677 }
stefank@5428 1678
stefank@5428 1679 if (TraceMetadataHumongousAllocation) {
stefank@5428 1680 size_t waste = chunk->word_size() - word_size;
stefank@5428 1681 gclog_or_tty->print_cr("Free list allocate humongous chunk size "
stefank@5428 1682 SIZE_FORMAT " for requested size " SIZE_FORMAT
stefank@5428 1683 " waste " SIZE_FORMAT,
stefank@5428 1684 chunk->word_size(), word_size, waste);
stefank@5428 1685 }
coleenp@3602 1686 }
jmasa@3947 1687
stefank@5428 1688 // Chunk is being removed from the chunks free list.
stefank@5506 1689 dec_free_chunks_total(chunk->word_size());
stefank@5428 1690
jmasa@3947 1691 // Remove it from the links to this freelist
jmasa@3947 1692 chunk->set_next(NULL);
jmasa@3947 1693 chunk->set_prev(NULL);
jmasa@4572 1694 #ifdef ASSERT
jmasa@4572 1695 // Chunk is no longer on any freelist. Setting to false make container_count_slow()
jmasa@4572 1696 // work.
stefank@5506 1697 chunk->set_is_tagged_free(false);
jmasa@4572 1698 #endif
stefank@5336 1699 chunk->container()->inc_container_count();
stefank@5336 1700
mgerdin@3829 1701 slow_locked_verify();
coleenp@3602 1702 return chunk;
coleenp@3602 1703 }
coleenp@3602 1704
coleenp@3602 1705 Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
coleenp@3602 1706 assert_lock_strong(SpaceManager::expand_lock());
mgerdin@3829 1707 slow_locked_verify();
coleenp@3602 1708
coleenp@3602 1709 // Take from the beginning of the list
coleenp@3602 1710 Metachunk* chunk = free_chunks_get(word_size);
coleenp@3602 1711 if (chunk == NULL) {
coleenp@3602 1712 return NULL;
coleenp@3602 1713 }
coleenp@3602 1714
jmasa@3947 1715 assert((word_size <= chunk->word_size()) ||
jmasa@3947 1716 list_index(chunk->word_size() == HumongousIndex),
jmasa@3947 1717 "Non-humongous variable sized chunk");
coleenp@3602 1718 if (TraceMetadataChunkAllocation) {
jmasa@3947 1719 size_t list_count;
jmasa@3947 1720 if (list_index(word_size) < HumongousIndex) {
jmasa@3947 1721 ChunkList* list = find_free_chunks_list(word_size);
jmasa@4497 1722 list_count = list->count();
jmasa@3947 1723 } else {
jmasa@3947 1724 list_count = humongous_dictionary()->total_count();
jmasa@3947 1725 }
stefank@5273 1726 gclog_or_tty->print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk "
stefank@5273 1727 PTR_FORMAT " size " SIZE_FORMAT " count " SIZE_FORMAT " ",
stefank@5273 1728 this, chunk, chunk->word_size(), list_count);
stefank@5273 1729 locked_print_free_chunks(gclog_or_tty);
coleenp@3602 1730 }
coleenp@3602 1731
coleenp@3602 1732 return chunk;
coleenp@3602 1733 }
coleenp@3602 1734
stefank@5336 1735 void ChunkManager::print_on(outputStream* out) const {
jmasa@3761 1736 if (PrintFLSStatistics != 0) {
stefank@5336 1737 const_cast<ChunkManager *>(this)->humongous_dictionary()->report_statistics();
jmasa@3761 1738 }
jmasa@3761 1739 }
jmasa@3761 1740
coleenp@3602 1741 // SpaceManager methods
coleenp@3602 1742
jmasa@3947 1743 void SpaceManager::get_initial_chunk_sizes(Metaspace::MetaspaceType type,
jmasa@3947 1744 size_t* chunk_word_size,
jmasa@3947 1745 size_t* class_chunk_word_size) {
jmasa@3947 1746 switch (type) {
jmasa@3947 1747 case Metaspace::BootMetaspaceType:
jmasa@3947 1748 *chunk_word_size = Metaspace::first_chunk_word_size();
jmasa@3947 1749 *class_chunk_word_size = Metaspace::first_class_chunk_word_size();
jmasa@3947 1750 break;
jmasa@3947 1751 case Metaspace::ROMetaspaceType:
jmasa@3947 1752 *chunk_word_size = SharedReadOnlySize / wordSize;
jmasa@3947 1753 *class_chunk_word_size = ClassSpecializedChunk;
jmasa@3947 1754 break;
jmasa@3947 1755 case Metaspace::ReadWriteMetaspaceType:
jmasa@3947 1756 *chunk_word_size = SharedReadWriteSize / wordSize;
jmasa@3947 1757 *class_chunk_word_size = ClassSpecializedChunk;
jmasa@3947 1758 break;
jmasa@3947 1759 case Metaspace::AnonymousMetaspaceType:
jmasa@3947 1760 case Metaspace::ReflectionMetaspaceType:
jmasa@3947 1761 *chunk_word_size = SpecializedChunk;
jmasa@3947 1762 *class_chunk_word_size = ClassSpecializedChunk;
jmasa@3947 1763 break;
jmasa@3947 1764 default:
jmasa@3947 1765 *chunk_word_size = SmallChunk;
jmasa@3947 1766 *class_chunk_word_size = ClassSmallChunk;
jmasa@3947 1767 break;
jmasa@3947 1768 }
mikael@4113 1769 assert(*chunk_word_size != 0 && *class_chunk_word_size != 0,
jmasa@3947 1770 err_msg("Initial chunks sizes bad: data " SIZE_FORMAT
jmasa@3947 1771 " class " SIZE_FORMAT,
mikael@4113 1772 *chunk_word_size, *class_chunk_word_size));
jmasa@3947 1773 }
jmasa@3947 1774
coleenp@3602 1775 size_t SpaceManager::sum_free_in_chunks_in_use() const {
coleenp@3602 1776 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
coleenp@3602 1777 size_t free = 0;
jmasa@3947 1778 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@3602 1779 Metachunk* chunk = chunks_in_use(i);
coleenp@3602 1780 while (chunk != NULL) {
coleenp@3602 1781 free += chunk->free_word_size();
coleenp@3602 1782 chunk = chunk->next();
coleenp@3602 1783 }
coleenp@3602 1784 }
coleenp@3602 1785 return free;
coleenp@3602 1786 }
coleenp@3602 1787
coleenp@3602 1788 size_t SpaceManager::sum_waste_in_chunks_in_use() const {
coleenp@3602 1789 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
coleenp@3602 1790 size_t result = 0;
jmasa@3947 1791 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@3602 1792 result += sum_waste_in_chunks_in_use(i);
coleenp@3602 1793 }
jmasa@3761 1794
coleenp@3602 1795 return result;
coleenp@3602 1796 }
coleenp@3602 1797
coleenp@3602 1798 size_t SpaceManager::sum_waste_in_chunks_in_use(ChunkIndex index) const {
coleenp@3602 1799 size_t result = 0;
coleenp@3602 1800 Metachunk* chunk = chunks_in_use(index);
coleenp@3602 1801 // Count the free space in all the chunk but not the
coleenp@3602 1802 // current chunk from which allocations are still being done.
coleenp@4902 1803 while (chunk != NULL) {
coleenp@4902 1804 if (chunk != current_chunk()) {
jmasa@3761 1805 result += chunk->free_word_size();
coleenp@3602 1806 }
coleenp@4902 1807 chunk = chunk->next();
coleenp@3602 1808 }
coleenp@3602 1809 return result;
coleenp@3602 1810 }
coleenp@3602 1811
coleenp@3602 1812 size_t SpaceManager::sum_capacity_in_chunks_in_use() const {
jmasa@4580 1813 // For CMS use "allocated_chunks_words()" which does not need the
jmasa@4580 1814 // Metaspace lock. For the other collectors sum over the
jmasa@4580 1815 // lists. Use both methods as a check that "allocated_chunks_words()"
jmasa@4580 1816 // is correct. That is, sum_capacity_in_chunks() is too expensive
jmasa@4580 1817 // to use in the product and allocated_chunks_words() should be used
jmasa@4580 1818 // but allow for checking that allocated_chunks_words() returns the same
jmasa@4580 1819 // value as sum_capacity_in_chunks_in_use() which is the definitive
jmasa@4580 1820 // answer.
jmasa@4580 1821 if (UseConcMarkSweepGC) {
jmasa@4580 1822 return allocated_chunks_words();
jmasa@4580 1823 } else {
jmasa@4580 1824 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
jmasa@4580 1825 size_t sum = 0;
jmasa@4580 1826 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
jmasa@4580 1827 Metachunk* chunk = chunks_in_use(i);
jmasa@4580 1828 while (chunk != NULL) {
stefank@5506 1829 sum += chunk->word_size();
jmasa@4580 1830 chunk = chunk->next();
jmasa@4580 1831 }
coleenp@3602 1832 }
jmasa@4580 1833 return sum;
coleenp@3602 1834 }
coleenp@3602 1835 }
coleenp@3602 1836
coleenp@3602 1837 size_t SpaceManager::sum_count_in_chunks_in_use() {
coleenp@3602 1838 size_t count = 0;
jmasa@3947 1839 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@3602 1840 count = count + sum_count_in_chunks_in_use(i);
coleenp@3602 1841 }
jmasa@3761 1842
coleenp@3602 1843 return count;
coleenp@3602 1844 }
coleenp@3602 1845
coleenp@3602 1846 size_t SpaceManager::sum_count_in_chunks_in_use(ChunkIndex i) {
coleenp@3602 1847 size_t count = 0;
coleenp@3602 1848 Metachunk* chunk = chunks_in_use(i);
coleenp@3602 1849 while (chunk != NULL) {
coleenp@3602 1850 count++;
coleenp@3602 1851 chunk = chunk->next();
coleenp@3602 1852 }
coleenp@3602 1853 return count;
coleenp@3602 1854 }
coleenp@3602 1855
coleenp@3602 1856
coleenp@3602 1857 size_t SpaceManager::sum_used_in_chunks_in_use() const {
coleenp@3602 1858 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
coleenp@3602 1859 size_t used = 0;
jmasa@3947 1860 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@3602 1861 Metachunk* chunk = chunks_in_use(i);
coleenp@3602 1862 while (chunk != NULL) {
coleenp@3602 1863 used += chunk->used_word_size();
coleenp@3602 1864 chunk = chunk->next();
coleenp@3602 1865 }
coleenp@3602 1866 }
coleenp@3602 1867 return used;
coleenp@3602 1868 }
coleenp@3602 1869
coleenp@3602 1870 void SpaceManager::locked_print_chunks_in_use_on(outputStream* st) const {
coleenp@3602 1871
jmasa@3947 1872 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
jmasa@3947 1873 Metachunk* chunk = chunks_in_use(i);
jmasa@3947 1874 st->print("SpaceManager: %s " PTR_FORMAT,
jmasa@3947 1875 chunk_size_name(i), chunk);
jmasa@3947 1876 if (chunk != NULL) {
jmasa@3947 1877 st->print_cr(" free " SIZE_FORMAT,
jmasa@3947 1878 chunk->free_word_size());
jmasa@3947 1879 } else {
jmasa@3947 1880 st->print_cr("");
jmasa@3947 1881 }
jmasa@3947 1882 }
coleenp@3602 1883
stefank@5336 1884 chunk_manager()->locked_print_free_chunks(st);
stefank@5336 1885 chunk_manager()->locked_print_sum_free_chunks(st);
coleenp@3602 1886 }
coleenp@3602 1887
coleenp@3602 1888 size_t SpaceManager::calc_chunk_size(size_t word_size) {
coleenp@3602 1889
coleenp@3602 1890 // Decide between a small chunk and a medium chunk. Up to
coleenp@3602 1891 // _small_chunk_limit small chunks can be allocated but
coleenp@3602 1892 // once a medium chunk has been allocated, no more small
coleenp@3602 1893 // chunks will be allocated.
coleenp@3602 1894 size_t chunk_word_size;
coleenp@3602 1895 if (chunks_in_use(MediumIndex) == NULL &&
coleenp@4902 1896 sum_count_in_chunks_in_use(SmallIndex) < _small_chunk_limit) {
jmasa@3947 1897 chunk_word_size = (size_t) small_chunk_size();
jmasa@3947 1898 if (word_size + Metachunk::overhead() > small_chunk_size()) {
jmasa@3947 1899 chunk_word_size = medium_chunk_size();
coleenp@3602 1900 }
coleenp@3602 1901 } else {
jmasa@3947 1902 chunk_word_size = medium_chunk_size();
coleenp@3602 1903 }
coleenp@3602 1904
jmasa@3947 1905 // Might still need a humongous chunk. Enforce an
jmasa@3947 1906 // eight word granularity to facilitate reuse (some
jmasa@3947 1907 // wastage but better chance of reuse).
jmasa@3947 1908 size_t if_humongous_sized_chunk =
jmasa@3947 1909 align_size_up(word_size + Metachunk::overhead(),
jmasa@3947 1910 HumongousChunkGranularity);
coleenp@3602 1911 chunk_word_size =
jmasa@3947 1912 MAX2((size_t) chunk_word_size, if_humongous_sized_chunk);
jmasa@3947 1913
jmasa@3947 1914 assert(!SpaceManager::is_humongous(word_size) ||
jmasa@3947 1915 chunk_word_size == if_humongous_sized_chunk,
jmasa@3947 1916 err_msg("Size calculation is wrong, word_size " SIZE_FORMAT
jmasa@3947 1917 " chunk_word_size " SIZE_FORMAT,
jmasa@3947 1918 word_size, chunk_word_size));
coleenp@3602 1919 if (TraceMetadataHumongousAllocation &&
coleenp@3602 1920 SpaceManager::is_humongous(word_size)) {
coleenp@3602 1921 gclog_or_tty->print_cr("Metadata humongous allocation:");
coleenp@3602 1922 gclog_or_tty->print_cr(" word_size " PTR_FORMAT, word_size);
coleenp@3602 1923 gclog_or_tty->print_cr(" chunk_word_size " PTR_FORMAT,
coleenp@3602 1924 chunk_word_size);
jmasa@3761 1925 gclog_or_tty->print_cr(" chunk overhead " PTR_FORMAT,
coleenp@3602 1926 Metachunk::overhead());
coleenp@3602 1927 }
coleenp@3602 1928 return chunk_word_size;
coleenp@3602 1929 }
coleenp@3602 1930
stefank@5429 1931 void SpaceManager::track_metaspace_memory_usage() {
stefank@5429 1932 if (is_init_completed()) {
stefank@5429 1933 if (is_class()) {
stefank@5429 1934 MemoryService::track_compressed_class_memory_usage();
stefank@5429 1935 }
stefank@5429 1936 MemoryService::track_metaspace_memory_usage();
stefank@5429 1937 }
stefank@5429 1938 }
stefank@5429 1939
jmasa@3761 1940 MetaWord* SpaceManager::grow_and_allocate(size_t word_size) {
coleenp@3602 1941 assert(vs_list()->current_virtual_space() != NULL,
coleenp@3602 1942 "Should have been set");
coleenp@3602 1943 assert(current_chunk() == NULL ||
coleenp@3602 1944 current_chunk()->allocate(word_size) == NULL,
coleenp@3602 1945 "Don't need to expand");
coleenp@3602 1946 MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
coleenp@3602 1947
coleenp@3602 1948 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@3947 1949 size_t words_left = 0;
jmasa@3947 1950 size_t words_used = 0;
jmasa@3947 1951 if (current_chunk() != NULL) {
jmasa@3947 1952 words_left = current_chunk()->free_word_size();
jmasa@3947 1953 words_used = current_chunk()->used_word_size();
jmasa@3947 1954 }
coleenp@3602 1955 gclog_or_tty->print_cr("SpaceManager::grow_and_allocate for " SIZE_FORMAT
jmasa@3947 1956 " words " SIZE_FORMAT " words used " SIZE_FORMAT
jmasa@3947 1957 " words left",
jmasa@3947 1958 word_size, words_used, words_left);
coleenp@3602 1959 }
coleenp@3602 1960
coleenp@3602 1961 // Get another chunk out of the virtual space
coleenp@3602 1962 size_t grow_chunks_by_words = calc_chunk_size(word_size);
jmasa@3947 1963 Metachunk* next = get_new_chunk(word_size, grow_chunks_by_words);
coleenp@3602 1964
stefank@5428 1965 MetaWord* mem = NULL;
stefank@5428 1966
coleenp@3602 1967 // If a chunk was available, add it to the in-use chunk list
coleenp@3602 1968 // and do an allocation from it.
coleenp@3602 1969 if (next != NULL) {
coleenp@3602 1970 // Add to this manager's list of chunks in use.
coleenp@3602 1971 add_chunk(next, false);
stefank@5428 1972 mem = next->allocate(word_size);
coleenp@3602 1973 }
stefank@5428 1974
stefank@5429 1975 // Track metaspace memory usage statistic.
stefank@5429 1976 track_metaspace_memory_usage();
stefank@5429 1977
stefank@5428 1978 return mem;
coleenp@3602 1979 }
coleenp@3602 1980
coleenp@3602 1981 void SpaceManager::print_on(outputStream* st) const {
coleenp@3602 1982
jmasa@3947 1983 for (ChunkIndex i = ZeroIndex;
jmasa@3761 1984 i < NumberOfInUseLists ;
coleenp@3602 1985 i = next_chunk_index(i) ) {
coleenp@3602 1986 st->print_cr(" chunks_in_use " PTR_FORMAT " chunk size " PTR_FORMAT,
coleenp@3602 1987 chunks_in_use(i),
coleenp@3602 1988 chunks_in_use(i) == NULL ? 0 : chunks_in_use(i)->word_size());
coleenp@3602 1989 }
coleenp@3602 1990 st->print_cr(" waste: Small " SIZE_FORMAT " Medium " SIZE_FORMAT
coleenp@3602 1991 " Humongous " SIZE_FORMAT,
coleenp@3602 1992 sum_waste_in_chunks_in_use(SmallIndex),
coleenp@3602 1993 sum_waste_in_chunks_in_use(MediumIndex),
coleenp@3602 1994 sum_waste_in_chunks_in_use(HumongousIndex));
jmasa@3761 1995 // block free lists
jmasa@3761 1996 if (block_freelists() != NULL) {
jmasa@3761 1997 st->print_cr("total in block free lists " SIZE_FORMAT,
jmasa@3761 1998 block_freelists()->total_size());
jmasa@3761 1999 }
coleenp@3602 2000 }
coleenp@3602 2001
jmasa@4727 2002 SpaceManager::SpaceManager(Metaspace::MetadataType mdtype,
stefank@5336 2003 Mutex* lock) :
jmasa@4727 2004 _mdtype(mdtype),
jmasa@4580 2005 _allocated_blocks_words(0),
jmasa@4580 2006 _allocated_chunks_words(0),
jmasa@4580 2007 _allocated_chunks_count(0),
jmasa@3947 2008 _lock(lock)
jmasa@3947 2009 {
jmasa@3947 2010 initialize();
jmasa@3947 2011 }
jmasa@3947 2012
jmasa@4580 2013 void SpaceManager::inc_size_metrics(size_t words) {
jmasa@4580 2014 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4580 2015 // Total of allocated Metachunks and allocated Metachunks count
jmasa@4580 2016 // for each SpaceManager
jmasa@4580 2017 _allocated_chunks_words = _allocated_chunks_words + words;
jmasa@4580 2018 _allocated_chunks_count++;
jmasa@4580 2019 // Global total of capacity in allocated Metachunks
jmasa@4727 2020 MetaspaceAux::inc_capacity(mdtype(), words);
jmasa@4580 2021 // Global total of allocated Metablocks.
jmasa@4580 2022 // used_words_slow() includes the overhead in each
jmasa@4580 2023 // Metachunk so include it in the used when the
jmasa@4580 2024 // Metachunk is first added (so only added once per
jmasa@4580 2025 // Metachunk).
jmasa@4727 2026 MetaspaceAux::inc_used(mdtype(), Metachunk::overhead());
jmasa@4580 2027 }
jmasa@4580 2028
jmasa@4580 2029 void SpaceManager::inc_used_metrics(size_t words) {
jmasa@4580 2030 // Add to the per SpaceManager total
jmasa@4580 2031 Atomic::add_ptr(words, &_allocated_blocks_words);
jmasa@4580 2032 // Add to the global total
jmasa@4727 2033 MetaspaceAux::inc_used(mdtype(), words);
jmasa@4580 2034 }
jmasa@4580 2035
jmasa@4580 2036 void SpaceManager::dec_total_from_size_metrics() {
jmasa@4727 2037 MetaspaceAux::dec_capacity(mdtype(), allocated_chunks_words());
jmasa@4727 2038 MetaspaceAux::dec_used(mdtype(), allocated_blocks_words());
jmasa@4580 2039 // Also deduct the overhead per Metachunk
jmasa@4727 2040 MetaspaceAux::dec_used(mdtype(), allocated_chunks_count() * Metachunk::overhead());
jmasa@4580 2041 }
jmasa@4580 2042
jmasa@3947 2043 void SpaceManager::initialize() {
coleenp@3602 2044 Metadebug::init_allocation_fail_alot_count();
jmasa@3947 2045 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@3602 2046 _chunks_in_use[i] = NULL;
coleenp@3602 2047 }
coleenp@3602 2048 _current_chunk = NULL;
coleenp@3602 2049 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@3602 2050 gclog_or_tty->print_cr("SpaceManager(): " PTR_FORMAT, this);
coleenp@3602 2051 }
coleenp@3602 2052 }
coleenp@3602 2053
jmasa@4497 2054 void ChunkManager::return_chunks(ChunkIndex index, Metachunk* chunks) {
jmasa@4497 2055 if (chunks == NULL) {
jmasa@4497 2056 return;
jmasa@4497 2057 }
jmasa@4497 2058 ChunkList* list = free_chunks(index);
jmasa@4497 2059 assert(list->size() == chunks->word_size(), "Mismatch in chunk sizes");
jmasa@4497 2060 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4497 2061 Metachunk* cur = chunks;
jmasa@4497 2062
jmasa@4572 2063 // This returns chunks one at a time. If a new
jmasa@4497 2064 // class List can be created that is a base class
jmasa@4497 2065 // of FreeList then something like FreeList::prepend()
jmasa@4497 2066 // can be used in place of this loop
jmasa@4497 2067 while (cur != NULL) {
jmasa@4572 2068 assert(cur->container() != NULL, "Container should have been set");
jmasa@4572 2069 cur->container()->dec_container_count();
jmasa@4497 2070 // Capture the next link before it is changed
jmasa@4497 2071 // by the call to return_chunk_at_head();
jmasa@4497 2072 Metachunk* next = cur->next();
stefank@5506 2073 DEBUG_ONLY(cur->set_is_tagged_free(true);)
jmasa@4497 2074 list->return_chunk_at_head(cur);
jmasa@4497 2075 cur = next;
jmasa@4497 2076 }
jmasa@4497 2077 }
jmasa@4497 2078
coleenp@3602 2079 SpaceManager::~SpaceManager() {
mgerdin@4349 2080 // This call this->_lock which can't be done while holding expand_lock()
jmasa@4580 2081 assert(sum_capacity_in_chunks_in_use() == allocated_chunks_words(),
jmasa@4580 2082 err_msg("sum_capacity_in_chunks_in_use() " SIZE_FORMAT
jmasa@4580 2083 " allocated_chunks_words() " SIZE_FORMAT,
jmasa@4580 2084 sum_capacity_in_chunks_in_use(), allocated_chunks_words()));
mgerdin@4349 2085
coleenp@3602 2086 MutexLockerEx fcl(SpaceManager::expand_lock(),
coleenp@3602 2087 Mutex::_no_safepoint_check_flag);
coleenp@3602 2088
stefank@5336 2089 chunk_manager()->slow_locked_verify();
coleenp@3602 2090
jmasa@4580 2091 dec_total_from_size_metrics();
jmasa@4580 2092
coleenp@3602 2093 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@3602 2094 gclog_or_tty->print_cr("~SpaceManager(): " PTR_FORMAT, this);
coleenp@3602 2095 locked_print_chunks_in_use_on(gclog_or_tty);
coleenp@3602 2096 }
coleenp@3602 2097
jmasa@4572 2098 // Do not mangle freed Metachunks. The chunk size inside Metachunks
jmasa@4572 2099 // is during the freeing of a VirtualSpaceNodes.
coleenp@3869 2100
coleenp@3602 2101 // Have to update before the chunks_in_use lists are emptied
coleenp@3602 2102 // below.
stefank@5336 2103 chunk_manager()->inc_free_chunks_total(allocated_chunks_words(),
stefank@5336 2104 sum_count_in_chunks_in_use());
coleenp@3602 2105
coleenp@3602 2106 // Add all the chunks in use by this space manager
coleenp@3602 2107 // to the global list of free chunks.
coleenp@3602 2108
jmasa@3947 2109 // Follow each list of chunks-in-use and add them to the
jmasa@3947 2110 // free lists. Each list is NULL terminated.
jmasa@3947 2111
jmasa@3947 2112 for (ChunkIndex i = ZeroIndex; i < HumongousIndex; i = next_chunk_index(i)) {
jmasa@3947 2113 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@3947 2114 gclog_or_tty->print_cr("returned %d %s chunks to freelist",
jmasa@3947 2115 sum_count_in_chunks_in_use(i),
jmasa@3947 2116 chunk_size_name(i));
jmasa@3947 2117 }
jmasa@3947 2118 Metachunk* chunks = chunks_in_use(i);
stefank@5336 2119 chunk_manager()->return_chunks(i, chunks);
jmasa@3947 2120 set_chunks_in_use(i, NULL);
jmasa@3947 2121 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@3947 2122 gclog_or_tty->print_cr("updated freelist count %d %s",
stefank@5336 2123 chunk_manager()->free_chunks(i)->count(),
jmasa@3947 2124 chunk_size_name(i));
jmasa@3947 2125 }
jmasa@3947 2126 assert(i != HumongousIndex, "Humongous chunks are handled explicitly later");
coleenp@3602 2127 }
coleenp@3602 2128
jmasa@3947 2129 // The medium chunk case may be optimized by passing the head and
jmasa@3947 2130 // tail of the medium chunk list to add_at_head(). The tail is often
jmasa@3947 2131 // the current chunk but there are probably exceptions.
jmasa@3947 2132
coleenp@3602 2133 // Humongous chunks
jmasa@3947 2134 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@3947 2135 gclog_or_tty->print_cr("returned %d %s humongous chunks to dictionary",
jmasa@3947 2136 sum_count_in_chunks_in_use(HumongousIndex),
jmasa@3947 2137 chunk_size_name(HumongousIndex));
jmasa@3947 2138 gclog_or_tty->print("Humongous chunk dictionary: ");
jmasa@3947 2139 }
coleenp@3602 2140 // Humongous chunks are never the current chunk.
coleenp@3602 2141 Metachunk* humongous_chunks = chunks_in_use(HumongousIndex);
coleenp@3602 2142
jmasa@3761 2143 while (humongous_chunks != NULL) {
jmasa@3761 2144 #ifdef ASSERT
stefank@5506 2145 humongous_chunks->set_is_tagged_free(true);
jmasa@3761 2146 #endif
jmasa@3947 2147 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@3947 2148 gclog_or_tty->print(PTR_FORMAT " (" SIZE_FORMAT ") ",
jmasa@3947 2149 humongous_chunks,
jmasa@3947 2150 humongous_chunks->word_size());
jmasa@3947 2151 }
jmasa@3947 2152 assert(humongous_chunks->word_size() == (size_t)
jmasa@3947 2153 align_size_up(humongous_chunks->word_size(),
jmasa@3947 2154 HumongousChunkGranularity),
jmasa@3947 2155 err_msg("Humongous chunk size is wrong: word size " SIZE_FORMAT
mikael@4113 2156 " granularity %d",
jmasa@3947 2157 humongous_chunks->word_size(), HumongousChunkGranularity));
jmasa@3761 2158 Metachunk* next_humongous_chunks = humongous_chunks->next();
jmasa@4572 2159 humongous_chunks->container()->dec_container_count();
stefank@5336 2160 chunk_manager()->humongous_dictionary()->return_chunk(humongous_chunks);
jmasa@3761 2161 humongous_chunks = next_humongous_chunks;
coleenp@3602 2162 }
jmasa@3947 2163 if (TraceMetadataChunkAllocation && Verbose) {
jmasa@3947 2164 gclog_or_tty->print_cr("");
jmasa@3947 2165 gclog_or_tty->print_cr("updated dictionary count %d %s",
stefank@5336 2166 chunk_manager()->humongous_dictionary()->total_count(),
jmasa@3947 2167 chunk_size_name(HumongousIndex));
jmasa@3947 2168 }
stefank@5336 2169 chunk_manager()->slow_locked_verify();
coleenp@3602 2170 }
coleenp@3602 2171
jmasa@3947 2172 const char* SpaceManager::chunk_size_name(ChunkIndex index) const {
jmasa@3947 2173 switch (index) {
jmasa@3947 2174 case SpecializedIndex:
jmasa@3947 2175 return "Specialized";
jmasa@3947 2176 case SmallIndex:
jmasa@3947 2177 return "Small";
jmasa@3947 2178 case MediumIndex:
jmasa@3947 2179 return "Medium";
jmasa@3947 2180 case HumongousIndex:
jmasa@3947 2181 return "Humongous";
jmasa@3947 2182 default:
jmasa@3947 2183 return NULL;
jmasa@3947 2184 }
jmasa@3947 2185 }
jmasa@3947 2186
jmasa@3947 2187 ChunkIndex ChunkManager::list_index(size_t size) {
jmasa@3947 2188 switch (size) {
jmasa@3947 2189 case SpecializedChunk:
jmasa@3947 2190 assert(SpecializedChunk == ClassSpecializedChunk,
jmasa@3947 2191 "Need branch for ClassSpecializedChunk");
jmasa@3947 2192 return SpecializedIndex;
jmasa@3947 2193 case SmallChunk:
jmasa@3947 2194 case ClassSmallChunk:
jmasa@3947 2195 return SmallIndex;
jmasa@3947 2196 case MediumChunk:
jmasa@3947 2197 case ClassMediumChunk:
jmasa@3947 2198 return MediumIndex;
jmasa@3947 2199 default:
jmasa@3948 2200 assert(size > MediumChunk || size > ClassMediumChunk,
jmasa@3947 2201 "Not a humongous chunk");
jmasa@3947 2202 return HumongousIndex;
jmasa@3947 2203 }
jmasa@3947 2204 }
jmasa@3947 2205
jmasa@3761 2206 void SpaceManager::deallocate(MetaWord* p, size_t word_size) {
coleenp@3602 2207 assert_lock_strong(_lock);
fparain@5017 2208 size_t raw_word_size = get_raw_word_size(word_size);
jmasa@3761 2209 size_t min_size = TreeChunk<Metablock, FreeList>::min_size();
fparain@5017 2210 assert(raw_word_size >= min_size,
hseigel@5093 2211 err_msg("Should not deallocate dark matter " SIZE_FORMAT "<" SIZE_FORMAT, word_size, min_size));
fparain@5017 2212 block_freelists()->return_block(p, raw_word_size);
coleenp@3602 2213 }
coleenp@3602 2214
coleenp@3602 2215 // Adds a chunk to the list of chunks in use.
coleenp@3602 2216 void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) {
coleenp@3602 2217
coleenp@3602 2218 assert(new_chunk != NULL, "Should not be NULL");
coleenp@3602 2219 assert(new_chunk->next() == NULL, "Should not be on a list");
coleenp@3602 2220
coleenp@3602 2221 new_chunk->reset_empty();
coleenp@3602 2222
coleenp@3602 2223 // Find the correct list and and set the current
coleenp@3602 2224 // chunk for that list.
jmasa@3947 2225 ChunkIndex index = ChunkManager::list_index(new_chunk->word_size());
jmasa@3947 2226
jmasa@3947 2227 if (index != HumongousIndex) {
mgerdin@5264 2228 retire_current_chunk();
coleenp@3602 2229 set_current_chunk(new_chunk);
jmasa@3947 2230 new_chunk->set_next(chunks_in_use(index));
jmasa@3947 2231 set_chunks_in_use(index, new_chunk);
jmasa@3947 2232 } else {
coleenp@3602 2233 // For null class loader data and DumpSharedSpaces, the first chunk isn't
coleenp@3602 2234 // small, so small will be null. Link this first chunk as the current
coleenp@3602 2235 // chunk.
coleenp@3602 2236 if (make_current) {
coleenp@3602 2237 // Set as the current chunk but otherwise treat as a humongous chunk.
coleenp@3602 2238 set_current_chunk(new_chunk);
coleenp@3602 2239 }
coleenp@3602 2240 // Link at head. The _current_chunk only points to a humongous chunk for
coleenp@3602 2241 // the null class loader metaspace (class and data virtual space managers)
coleenp@3602 2242 // any humongous chunks so will not point to the tail
coleenp@3602 2243 // of the humongous chunks list.
coleenp@3602 2244 new_chunk->set_next(chunks_in_use(HumongousIndex));
coleenp@3602 2245 set_chunks_in_use(HumongousIndex, new_chunk);
coleenp@3602 2246
jmasa@3948 2247 assert(new_chunk->word_size() > medium_chunk_size(), "List inconsistency");
coleenp@3602 2248 }
coleenp@3602 2249
jmasa@4580 2250 // Add to the running sum of capacity
jmasa@4580 2251 inc_size_metrics(new_chunk->word_size());
jmasa@4580 2252
coleenp@3602 2253 assert(new_chunk->is_empty(), "Not ready for reuse");
coleenp@3602 2254 if (TraceMetadataChunkAllocation && Verbose) {
coleenp@3602 2255 gclog_or_tty->print("SpaceManager::add_chunk: %d) ",
coleenp@3602 2256 sum_count_in_chunks_in_use());
coleenp@3602 2257 new_chunk->print_on(gclog_or_tty);
stefank@5336 2258 chunk_manager()->locked_print_free_chunks(gclog_or_tty);
coleenp@3602 2259 }
coleenp@3602 2260 }
coleenp@3602 2261
mgerdin@5264 2262 void SpaceManager::retire_current_chunk() {
mgerdin@5264 2263 if (current_chunk() != NULL) {
mgerdin@5264 2264 size_t remaining_words = current_chunk()->free_word_size();
mgerdin@5264 2265 if (remaining_words >= TreeChunk<Metablock, FreeList>::min_size()) {
mgerdin@5264 2266 block_freelists()->return_block(current_chunk()->allocate(remaining_words), remaining_words);
mgerdin@5264 2267 inc_used_metrics(remaining_words);
mgerdin@5264 2268 }
mgerdin@5264 2269 }
mgerdin@5264 2270 }
mgerdin@5264 2271
jmasa@3947 2272 Metachunk* SpaceManager::get_new_chunk(size_t word_size,
jmasa@3947 2273 size_t grow_chunks_by_words) {
stefank@5336 2274 // Get a chunk from the chunk freelist
stefank@5336 2275 Metachunk* next = chunk_manager()->chunk_freelist_allocate(grow_chunks_by_words);
stefank@5336 2276
stefank@5336 2277 if (next == NULL) {
stefank@5336 2278 next = vs_list()->get_new_chunk(word_size,
stefank@5336 2279 grow_chunks_by_words,
stefank@5336 2280 medium_chunk_bunch());
stefank@5336 2281 }
jmasa@3947 2282
stefank@5272 2283 if (TraceMetadataHumongousAllocation && next != NULL &&
jmasa@3947 2284 SpaceManager::is_humongous(next->word_size())) {
stefank@5272 2285 gclog_or_tty->print_cr(" new humongous chunk word size "
stefank@5272 2286 PTR_FORMAT, next->word_size());
jmasa@3947 2287 }
jmasa@3947 2288
jmasa@3947 2289 return next;
jmasa@3947 2290 }
jmasa@3947 2291
coleenp@3602 2292 MetaWord* SpaceManager::allocate(size_t word_size) {
coleenp@3602 2293 MutexLockerEx cl(lock(), Mutex::_no_safepoint_check_flag);
coleenp@3602 2294
iklam@4773 2295 size_t raw_word_size = get_raw_word_size(word_size);
coleenp@3602 2296 BlockFreelist* fl = block_freelists();
jmasa@3761 2297 MetaWord* p = NULL;
coleenp@3602 2298 // Allocation from the dictionary is expensive in the sense that
coleenp@3602 2299 // the dictionary has to be searched for a size. Don't allocate
coleenp@3602 2300 // from the dictionary until it starts to get fat. Is this
coleenp@3602 2301 // a reasonable policy? Maybe an skinny dictionary is fast enough
coleenp@3602 2302 // for allocations. Do some profiling. JJJ
jmasa@3761 2303 if (fl->total_size() > allocation_from_dictionary_limit) {
jmasa@3761 2304 p = fl->get_block(raw_word_size);
coleenp@3602 2305 }
jmasa@3761 2306 if (p == NULL) {
jmasa@3761 2307 p = allocate_work(raw_word_size);
coleenp@3602 2308 }
coleenp@3602 2309
jmasa@3761 2310 return p;
coleenp@3602 2311 }
coleenp@3602 2312
coleenp@3602 2313 // Returns the address of spaced allocated for "word_size".
coleenp@3602 2314 // This methods does not know about blocks (Metablocks)
jmasa@3761 2315 MetaWord* SpaceManager::allocate_work(size_t word_size) {
coleenp@3602 2316 assert_lock_strong(_lock);
coleenp@3602 2317 #ifdef ASSERT
coleenp@3602 2318 if (Metadebug::test_metadata_failure()) {
coleenp@3602 2319 return NULL;
coleenp@3602 2320 }
coleenp@3602 2321 #endif
coleenp@3602 2322 // Is there space in the current chunk?
jmasa@3761 2323 MetaWord* result = NULL;
coleenp@3602 2324
coleenp@3602 2325 // For DumpSharedSpaces, only allocate out of the current chunk which is
coleenp@3602 2326 // never null because we gave it the size we wanted. Caller reports out
coleenp@3602 2327 // of memory if this returns null.
coleenp@3602 2328 if (DumpSharedSpaces) {
coleenp@3602 2329 assert(current_chunk() != NULL, "should never happen");
jmasa@4580 2330 inc_used_metrics(word_size);
coleenp@3602 2331 return current_chunk()->allocate(word_size); // caller handles null result
coleenp@3602 2332 }
stefank@5428 2333
coleenp@3602 2334 if (current_chunk() != NULL) {
coleenp@3602 2335 result = current_chunk()->allocate(word_size);
coleenp@3602 2336 }
coleenp@3602 2337
coleenp@3602 2338 if (result == NULL) {
coleenp@3602 2339 result = grow_and_allocate(word_size);
coleenp@3602 2340 }
stefank@5428 2341
stefank@5428 2342 if (result != NULL) {
jmasa@4580 2343 inc_used_metrics(word_size);
jmasa@3761 2344 assert(result != (MetaWord*) chunks_in_use(MediumIndex),
jmasa@3761 2345 "Head of the list is being allocated");
coleenp@3602 2346 }
coleenp@3602 2347
coleenp@3602 2348 return result;
coleenp@3602 2349 }
coleenp@3602 2350
coleenp@3602 2351 void SpaceManager::verify() {
coleenp@3602 2352 // If there are blocks in the dictionary, then
coleenp@3602 2353 // verfication of chunks does not work since
coleenp@3602 2354 // being in the dictionary alters a chunk.
jmasa@3761 2355 if (block_freelists()->total_size() == 0) {
jmasa@3947 2356 for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
coleenp@3602 2357 Metachunk* curr = chunks_in_use(i);
coleenp@3602 2358 while (curr != NULL) {
coleenp@3602 2359 curr->verify();
jmasa@3892 2360 verify_chunk_size(curr);
coleenp@3602 2361 curr = curr->next();
coleenp@3602 2362 }
coleenp@3602 2363 }
coleenp@3602 2364 }
coleenp@3602 2365 }
coleenp@3602 2366
jmasa@3892 2367 void SpaceManager::verify_chunk_size(Metachunk* chunk) {
jmasa@3892 2368 assert(is_humongous(chunk->word_size()) ||
jmasa@3947 2369 chunk->word_size() == medium_chunk_size() ||
jmasa@3947 2370 chunk->word_size() == small_chunk_size() ||
jmasa@3947 2371 chunk->word_size() == specialized_chunk_size(),
jmasa@3892 2372 "Chunk size is wrong");
jmasa@3892 2373 return;
jmasa@3892 2374 }
jmasa@3892 2375
coleenp@3602 2376 #ifdef ASSERT
jmasa@4580 2377 void SpaceManager::verify_allocated_blocks_words() {
coleenp@3602 2378 // Verification is only guaranteed at a safepoint.
jmasa@4580 2379 assert(SafepointSynchronize::is_at_safepoint() || !Universe::is_fully_initialized(),
jmasa@4580 2380 "Verification can fail if the applications is running");
jmasa@4580 2381 assert(allocated_blocks_words() == sum_used_in_chunks_in_use(),
mikael@4113 2382 err_msg("allocation total is not consistent " SIZE_FORMAT
mikael@4113 2383 " vs " SIZE_FORMAT,
jmasa@4580 2384 allocated_blocks_words(), sum_used_in_chunks_in_use()));
coleenp@3602 2385 }
coleenp@3602 2386
coleenp@3602 2387 #endif
coleenp@3602 2388
coleenp@3602 2389 void SpaceManager::dump(outputStream* const out) const {
coleenp@3602 2390 size_t curr_total = 0;
coleenp@3602 2391 size_t waste = 0;
coleenp@3602 2392 uint i = 0;
coleenp@3602 2393 size_t used = 0;
coleenp@3602 2394 size_t capacity = 0;
coleenp@3602 2395
coleenp@3602 2396 // Add up statistics for all chunks in this SpaceManager.
jmasa@3947 2397 for (ChunkIndex index = ZeroIndex;
jmasa@3761 2398 index < NumberOfInUseLists;
coleenp@3602 2399 index = next_chunk_index(index)) {
coleenp@3602 2400 for (Metachunk* curr = chunks_in_use(index);
coleenp@3602 2401 curr != NULL;
coleenp@3602 2402 curr = curr->next()) {
coleenp@3602 2403 out->print("%d) ", i++);
coleenp@3602 2404 curr->print_on(out);
coleenp@3602 2405 curr_total += curr->word_size();
coleenp@3602 2406 used += curr->used_word_size();
stefank@5506 2407 capacity += curr->word_size();
coleenp@3602 2408 waste += curr->free_word_size() + curr->overhead();;
coleenp@3602 2409 }
coleenp@3602 2410 }
coleenp@3602 2411
stefank@5272 2412 if (TraceMetadataChunkAllocation && Verbose) {
stefank@5272 2413 block_freelists()->print_on(out);
stefank@5272 2414 }
stefank@5272 2415
jmasa@3947 2416 size_t free = current_chunk() == NULL ? 0 : current_chunk()->free_word_size();
coleenp@3602 2417 // Free space isn't wasted.
coleenp@3602 2418 waste -= free;
coleenp@3602 2419
coleenp@3602 2420 out->print_cr("total of all chunks " SIZE_FORMAT " used " SIZE_FORMAT
coleenp@3602 2421 " free " SIZE_FORMAT " capacity " SIZE_FORMAT
coleenp@3602 2422 " waste " SIZE_FORMAT, curr_total, used, free, capacity, waste);
coleenp@3602 2423 }
coleenp@3602 2424
coleenp@3869 2425 #ifndef PRODUCT
coleenp@3602 2426 void SpaceManager::mangle_freed_chunks() {
jmasa@3947 2427 for (ChunkIndex index = ZeroIndex;
jmasa@3761 2428 index < NumberOfInUseLists;
coleenp@3602 2429 index = next_chunk_index(index)) {
coleenp@3602 2430 for (Metachunk* curr = chunks_in_use(index);
coleenp@3602 2431 curr != NULL;
coleenp@3602 2432 curr = curr->next()) {
coleenp@3602 2433 curr->mangle();
coleenp@3602 2434 }
coleenp@3602 2435 }
coleenp@3602 2436 }
coleenp@3869 2437 #endif // PRODUCT
coleenp@3602 2438
coleenp@3602 2439 // MetaspaceAux
coleenp@3602 2440
jmasa@4580 2441
jmasa@4727 2442 size_t MetaspaceAux::_allocated_capacity_words[] = {0, 0};
jmasa@4727 2443 size_t MetaspaceAux::_allocated_used_words[] = {0, 0};
jmasa@4580 2444
ehelin@5096 2445 size_t MetaspaceAux::free_bytes(Metaspace::MetadataType mdtype) {
ehelin@5096 2446 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
ehelin@5096 2447 return list == NULL ? 0 : list->free_bytes();
ehelin@5096 2448 }
ehelin@5096 2449
jmasa@4580 2450 size_t MetaspaceAux::free_bytes() {
ehelin@5096 2451 return free_bytes(Metaspace::ClassType) + free_bytes(Metaspace::NonClassType);
jmasa@4580 2452 }
jmasa@4580 2453
jmasa@4727 2454 void MetaspaceAux::dec_capacity(Metaspace::MetadataType mdtype, size_t words) {
jmasa@4580 2455 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4727 2456 assert(words <= allocated_capacity_words(mdtype),
jmasa@4580 2457 err_msg("About to decrement below 0: words " SIZE_FORMAT
jmasa@4727 2458 " is greater than _allocated_capacity_words[%u] " SIZE_FORMAT,
jmasa@4727 2459 words, mdtype, allocated_capacity_words(mdtype)));
jmasa@4727 2460 _allocated_capacity_words[mdtype] -= words;
jmasa@4580 2461 }
jmasa@4580 2462
jmasa@4727 2463 void MetaspaceAux::inc_capacity(Metaspace::MetadataType mdtype, size_t words) {
jmasa@4580 2464 assert_lock_strong(SpaceManager::expand_lock());
jmasa@4580 2465 // Needs to be atomic
jmasa@4727 2466 _allocated_capacity_words[mdtype] += words;
jmasa@4580 2467 }
jmasa@4580 2468
jmasa@4727 2469 void MetaspaceAux::dec_used(Metaspace::MetadataType mdtype, size_t words) {
jmasa@4727 2470 assert(words <= allocated_used_words(mdtype),
jmasa@4580 2471 err_msg("About to decrement below 0: words " SIZE_FORMAT
jmasa@4727 2472 " is greater than _allocated_used_words[%u] " SIZE_FORMAT,
jmasa@4727 2473 words, mdtype, allocated_used_words(mdtype)));
jmasa@4580 2474 // For CMS deallocation of the Metaspaces occurs during the
jmasa@4580 2475 // sweep which is a concurrent phase. Protection by the expand_lock()
jmasa@4580 2476 // is not enough since allocation is on a per Metaspace basis
jmasa@4580 2477 // and protected by the Metaspace lock.
jmasa@4580 2478 jlong minus_words = (jlong) - (jlong) words;
jmasa@4727 2479 Atomic::add_ptr(minus_words, &_allocated_used_words[mdtype]);
jmasa@4580 2480 }
jmasa@4580 2481
jmasa@4727 2482 void MetaspaceAux::inc_used(Metaspace::MetadataType mdtype, size_t words) {
jmasa@4580 2483 // _allocated_used_words tracks allocations for
jmasa@4580 2484 // each piece of metadata. Those allocations are
jmasa@4580 2485 // generally done concurrently by different application
jmasa@4580 2486 // threads so must be done atomically.
jmasa@4727 2487 Atomic::add_ptr(words, &_allocated_used_words[mdtype]);
jmasa@4580 2488 }
jmasa@4580 2489
jmasa@4580 2490 size_t MetaspaceAux::used_bytes_slow(Metaspace::MetadataType mdtype) {
jmasa@3607 2491 size_t used = 0;
jmasa@3607 2492 ClassLoaderDataGraphMetaspaceIterator iter;
jmasa@3607 2493 while (iter.repeat()) {
jmasa@3607 2494 Metaspace* msp = iter.get_next();
jmasa@4580 2495 // Sum allocated_blocks_words for each metaspace
jmasa@3607 2496 if (msp != NULL) {
jmasa@4580 2497 used += msp->used_words_slow(mdtype);
jmasa@3607 2498 }
jmasa@3607 2499 }
jmasa@3607 2500 return used * BytesPerWord;
jmasa@3607 2501 }
jmasa@3607 2502
ehelin@5268 2503 size_t MetaspaceAux::free_bytes_slow(Metaspace::MetadataType mdtype) {
coleenp@3602 2504 size_t free = 0;
coleenp@3602 2505 ClassLoaderDataGraphMetaspaceIterator iter;
coleenp@3602 2506 while (iter.repeat()) {
coleenp@3602 2507 Metaspace* msp = iter.get_next();
coleenp@3602 2508 if (msp != NULL) {
ehelin@5268 2509 free += msp->free_words_slow(mdtype);
coleenp@3602 2510 }
coleenp@3602 2511 }
coleenp@3602 2512 return free * BytesPerWord;
coleenp@3602 2513 }
coleenp@3602 2514
jmasa@4580 2515 size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) {
hseigel@5093 2516 if ((mdtype == Metaspace::ClassType) && !Metaspace::using_class_space()) {
hseigel@5093 2517 return 0;
hseigel@5093 2518 }
jmasa@4580 2519 // Don't count the space in the freelists. That space will be
jmasa@4580 2520 // added to the capacity calculation as needed.
jmasa@4580 2521 size_t capacity = 0;
coleenp@3602 2522 ClassLoaderDataGraphMetaspaceIterator iter;
coleenp@3602 2523 while (iter.repeat()) {
coleenp@3602 2524 Metaspace* msp = iter.get_next();
coleenp@3602 2525 if (msp != NULL) {
jmasa@4580 2526 capacity += msp->capacity_words_slow(mdtype);
coleenp@3602 2527 }
coleenp@3602 2528 }
coleenp@3602 2529 return capacity * BytesPerWord;
coleenp@3602 2530 }
coleenp@3602 2531
ehelin@5268 2532 size_t MetaspaceAux::capacity_bytes_slow() {
ehelin@5268 2533 #ifdef PRODUCT
ehelin@5268 2534 // Use allocated_capacity_bytes() in PRODUCT instead of this function.
ehelin@5268 2535 guarantee(false, "Should not call capacity_bytes_slow() in the PRODUCT");
ehelin@5268 2536 #endif
ehelin@5268 2537 size_t class_capacity = capacity_bytes_slow(Metaspace::ClassType);
ehelin@5268 2538 size_t non_class_capacity = capacity_bytes_slow(Metaspace::NonClassType);
ehelin@5268 2539 assert(allocated_capacity_bytes() == class_capacity + non_class_capacity,
ehelin@5268 2540 err_msg("bad accounting: allocated_capacity_bytes() " SIZE_FORMAT
ehelin@5268 2541 " class_capacity + non_class_capacity " SIZE_FORMAT
ehelin@5268 2542 " class_capacity " SIZE_FORMAT " non_class_capacity " SIZE_FORMAT,
ehelin@5268 2543 allocated_capacity_bytes(), class_capacity + non_class_capacity,
ehelin@5268 2544 class_capacity, non_class_capacity));
ehelin@5268 2545
ehelin@5268 2546 return class_capacity + non_class_capacity;
ehelin@5268 2547 }
ehelin@5268 2548
ehelin@5268 2549 size_t MetaspaceAux::reserved_bytes(Metaspace::MetadataType mdtype) {
ehelin@5096 2550 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
stefank@5269 2551 return list == NULL ? 0 : list->reserved_bytes();
stefank@5269 2552 }
stefank@5269 2553
stefank@5269 2554 size_t MetaspaceAux::committed_bytes(Metaspace::MetadataType mdtype) {
stefank@5269 2555 VirtualSpaceList* list = Metaspace::get_space_list(mdtype);
stefank@5269 2556 return list == NULL ? 0 : list->committed_bytes();
coleenp@3602 2557 }
coleenp@3602 2558
ehelin@5268 2559 size_t MetaspaceAux::min_chunk_size_words() { return Metaspace::first_chunk_word_size(); }
ehelin@5268 2560
ehelin@5268 2561 size_t MetaspaceAux::free_chunks_total_words(Metaspace::MetadataType mdtype) {
stefank@5336 2562 ChunkManager* chunk_manager = Metaspace::get_chunk_manager(mdtype);
stefank@5336 2563 if (chunk_manager == NULL) {
hseigel@5093 2564 return 0;
hseigel@5093 2565 }
stefank@5336 2566 chunk_manager->slow_verify();
stefank@5336 2567 return chunk_manager->free_chunks_total_words();
coleenp@3602 2568 }
coleenp@3602 2569
ehelin@5268 2570 size_t MetaspaceAux::free_chunks_total_bytes(Metaspace::MetadataType mdtype) {
ehelin@5268 2571 return free_chunks_total_words(mdtype) * BytesPerWord;
coleenp@3602 2572 }
coleenp@3602 2573
ehelin@5268 2574 size_t MetaspaceAux::free_chunks_total_words() {
ehelin@5268 2575 return free_chunks_total_words(Metaspace::ClassType) +
ehelin@5268 2576 free_chunks_total_words(Metaspace::NonClassType);
jmasa@4580 2577 }
jmasa@4580 2578
ehelin@5268 2579 size_t MetaspaceAux::free_chunks_total_bytes() {
ehelin@5268 2580 return free_chunks_total_words() * BytesPerWord;
jmasa@4580 2581 }
jmasa@4580 2582
coleenp@3602 2583 void MetaspaceAux::print_metaspace_change(size_t prev_metadata_used) {
coleenp@3602 2584 gclog_or_tty->print(", [Metaspace:");
coleenp@3602 2585 if (PrintGCDetails && Verbose) {
coleenp@3602 2586 gclog_or_tty->print(" " SIZE_FORMAT
coleenp@3602 2587 "->" SIZE_FORMAT
jmasa@4580 2588 "(" SIZE_FORMAT ")",
coleenp@3602 2589 prev_metadata_used,
jmasa@4875 2590 allocated_used_bytes(),
ehelin@5268 2591 reserved_bytes());
coleenp@3602 2592 } else {
coleenp@3602 2593 gclog_or_tty->print(" " SIZE_FORMAT "K"
coleenp@3602 2594 "->" SIZE_FORMAT "K"
jmasa@4580 2595 "(" SIZE_FORMAT "K)",
ehelin@5268 2596 prev_metadata_used/K,
ehelin@5268 2597 allocated_used_bytes()/K,
ehelin@5268 2598 reserved_bytes()/K);
coleenp@3602 2599 }
coleenp@3602 2600
coleenp@3602 2601 gclog_or_tty->print("]");
coleenp@3602 2602 }
coleenp@3602 2603
coleenp@3602 2604 // This is printed when PrintGCDetails
coleenp@3602 2605 void MetaspaceAux::print_on(outputStream* out) {
coleenp@3602 2606 Metaspace::MetadataType nct = Metaspace::NonClassType;
coleenp@3602 2607
stefank@5428 2608 out->print_cr(" Metaspace "
stefank@5428 2609 "used " SIZE_FORMAT "K, "
stefank@5428 2610 "capacity " SIZE_FORMAT "K, "
stefank@5428 2611 "committed " SIZE_FORMAT "K, "
stefank@5428 2612 "reserved " SIZE_FORMAT "K",
stefank@5428 2613 allocated_used_bytes()/K,
stefank@5428 2614 allocated_capacity_bytes()/K,
stefank@5428 2615 committed_bytes()/K,
stefank@5428 2616 reserved_bytes()/K);
stefank@5428 2617
hseigel@5093 2618 if (Metaspace::using_class_space()) {
hseigel@5093 2619 Metaspace::MetadataType ct = Metaspace::ClassType;
hseigel@5093 2620 out->print_cr(" class space "
stefank@5428 2621 "used " SIZE_FORMAT "K, "
stefank@5428 2622 "capacity " SIZE_FORMAT "K, "
stefank@5428 2623 "committed " SIZE_FORMAT "K, "
stefank@5428 2624 "reserved " SIZE_FORMAT "K",
stefank@5428 2625 allocated_used_bytes(ct)/K,
hseigel@5093 2626 allocated_capacity_bytes(ct)/K,
stefank@5428 2627 committed_bytes(ct)/K,
ehelin@5268 2628 reserved_bytes(ct)/K);
hseigel@5093 2629 }
coleenp@3602 2630 }
coleenp@3602 2631
coleenp@3602 2632 // Print information for class space and data space separately.
coleenp@3602 2633 // This is almost the same as above.
coleenp@3602 2634 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
ehelin@5268 2635 size_t free_chunks_capacity_bytes = free_chunks_total_bytes(mdtype);
jmasa@4580 2636 size_t capacity_bytes = capacity_bytes_slow(mdtype);
jmasa@4580 2637 size_t used_bytes = used_bytes_slow(mdtype);
ehelin@5268 2638 size_t free_bytes = free_bytes_slow(mdtype);
coleenp@3602 2639 size_t used_and_free = used_bytes + free_bytes +
coleenp@3602 2640 free_chunks_capacity_bytes;
coleenp@3602 2641 out->print_cr(" Chunk accounting: used in chunks " SIZE_FORMAT
coleenp@3602 2642 "K + unused in chunks " SIZE_FORMAT "K + "
coleenp@3602 2643 " capacity in free chunks " SIZE_FORMAT "K = " SIZE_FORMAT
coleenp@3602 2644 "K capacity in allocated chunks " SIZE_FORMAT "K",
coleenp@3602 2645 used_bytes / K,
coleenp@3602 2646 free_bytes / K,
coleenp@3602 2647 free_chunks_capacity_bytes / K,
coleenp@3602 2648 used_and_free / K,
coleenp@3602 2649 capacity_bytes / K);
mgerdin@4303 2650 // Accounting can only be correct if we got the values during a safepoint
mgerdin@4303 2651 assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong");
coleenp@3602 2652 }
coleenp@3602 2653
hseigel@5093 2654 // Print total fragmentation for class metaspaces
hseigel@5093 2655 void MetaspaceAux::print_class_waste(outputStream* out) {
hseigel@5093 2656 assert(Metaspace::using_class_space(), "class metaspace not used");
hseigel@5093 2657 size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0;
hseigel@5093 2658 size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0;
hseigel@5093 2659 ClassLoaderDataGraphMetaspaceIterator iter;
hseigel@5093 2660 while (iter.repeat()) {
hseigel@5093 2661 Metaspace* msp = iter.get_next();
hseigel@5093 2662 if (msp != NULL) {
hseigel@5093 2663 cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
hseigel@5093 2664 cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
hseigel@5093 2665 cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex);
hseigel@5093 2666 cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex);
hseigel@5093 2667 cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex);
hseigel@5093 2668 cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex);
hseigel@5093 2669 cls_humongous_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex);
hseigel@5093 2670 }
hseigel@5093 2671 }
hseigel@5093 2672 out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
hseigel@5093 2673 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
hseigel@5093 2674 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
hseigel@5093 2675 "large count " SIZE_FORMAT,
hseigel@5093 2676 cls_specialized_count, cls_specialized_waste,
hseigel@5093 2677 cls_small_count, cls_small_waste,
hseigel@5093 2678 cls_medium_count, cls_medium_waste, cls_humongous_count);
hseigel@5093 2679 }
hseigel@5093 2680
hseigel@5093 2681 // Print total fragmentation for data and class metaspaces separately
coleenp@3602 2682 void MetaspaceAux::print_waste(outputStream* out) {
coleenp@4902 2683 size_t specialized_waste = 0, small_waste = 0, medium_waste = 0;
coleenp@4902 2684 size_t specialized_count = 0, small_count = 0, medium_count = 0, humongous_count = 0;
coleenp@3602 2685
coleenp@3602 2686 ClassLoaderDataGraphMetaspaceIterator iter;
coleenp@3602 2687 while (iter.repeat()) {
coleenp@3602 2688 Metaspace* msp = iter.get_next();
coleenp@3602 2689 if (msp != NULL) {
jmasa@3947 2690 specialized_waste += msp->vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
jmasa@3947 2691 specialized_count += msp->vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
coleenp@3602 2692 small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex);
jmasa@3947 2693 small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex);
coleenp@3602 2694 medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex);
jmasa@3947 2695 medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex);
coleenp@4902 2696 humongous_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex);
coleenp@3602 2697 }
coleenp@3602 2698 }
coleenp@3602 2699 out->print_cr("Total fragmentation waste (words) doesn't count free space");
jmasa@3947 2700 out->print_cr(" data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
jmasa@3947 2701 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
coleenp@4902 2702 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
coleenp@4902 2703 "large count " SIZE_FORMAT,
jmasa@3947 2704 specialized_count, specialized_waste, small_count,
coleenp@4902 2705 small_waste, medium_count, medium_waste, humongous_count);
hseigel@5093 2706 if (Metaspace::using_class_space()) {
hseigel@5093 2707 print_class_waste(out);
hseigel@5093 2708 }
coleenp@3602 2709 }
coleenp@3602 2710
coleenp@3602 2711 // Dump global metaspace things from the end of ClassLoaderDataGraph
coleenp@3602 2712 void MetaspaceAux::dump(outputStream* out) {
coleenp@3602 2713 out->print_cr("All Metaspace:");
coleenp@3602 2714 out->print("data space: "); print_on(out, Metaspace::NonClassType);
coleenp@3602 2715 out->print("class space: "); print_on(out, Metaspace::ClassType);
coleenp@3602 2716 print_waste(out);
coleenp@3602 2717 }
coleenp@3602 2718
mgerdin@3829 2719 void MetaspaceAux::verify_free_chunks() {
stefank@5336 2720 Metaspace::chunk_manager_metadata()->verify();
hseigel@5093 2721 if (Metaspace::using_class_space()) {
stefank@5336 2722 Metaspace::chunk_manager_class()->verify();
hseigel@5093 2723 }
mgerdin@3829 2724 }
mgerdin@3829 2725
jmasa@4580 2726 void MetaspaceAux::verify_capacity() {
jmasa@4580 2727 #ifdef ASSERT
jmasa@4580 2728 size_t running_sum_capacity_bytes = allocated_capacity_bytes();
jmasa@4727 2729 // For purposes of the running sum of capacity, verify against capacity
jmasa@4580 2730 size_t capacity_in_use_bytes = capacity_bytes_slow();
jmasa@4580 2731 assert(running_sum_capacity_bytes == capacity_in_use_bytes,
jmasa@4580 2732 err_msg("allocated_capacity_words() * BytesPerWord " SIZE_FORMAT
jmasa@4580 2733 " capacity_bytes_slow()" SIZE_FORMAT,
jmasa@4580 2734 running_sum_capacity_bytes, capacity_in_use_bytes));
jmasa@4727 2735 for (Metaspace::MetadataType i = Metaspace::ClassType;
jmasa@4727 2736 i < Metaspace:: MetadataTypeCount;
jmasa@4727 2737 i = (Metaspace::MetadataType)(i + 1)) {
jmasa@4727 2738 size_t capacity_in_use_bytes = capacity_bytes_slow(i);
jmasa@4727 2739 assert(allocated_capacity_bytes(i) == capacity_in_use_bytes,
jmasa@4727 2740 err_msg("allocated_capacity_bytes(%u) " SIZE_FORMAT
jmasa@4727 2741 " capacity_bytes_slow(%u)" SIZE_FORMAT,
jmasa@4727 2742 i, allocated_capacity_bytes(i), i, capacity_in_use_bytes));
jmasa@4727 2743 }
jmasa@4580 2744 #endif
jmasa@4580 2745 }
jmasa@4580 2746
jmasa@4580 2747 void MetaspaceAux::verify_used() {
jmasa@4580 2748 #ifdef ASSERT
jmasa@4580 2749 size_t running_sum_used_bytes = allocated_used_bytes();
jmasa@4727 2750 // For purposes of the running sum of used, verify against used
jmasa@4580 2751 size_t used_in_use_bytes = used_bytes_slow();
jmasa@4580 2752 assert(allocated_used_bytes() == used_in_use_bytes,
jmasa@4580 2753 err_msg("allocated_used_bytes() " SIZE_FORMAT
jmasa@4727 2754 " used_bytes_slow()" SIZE_FORMAT,
jmasa@4580 2755 allocated_used_bytes(), used_in_use_bytes));
jmasa@4727 2756 for (Metaspace::MetadataType i = Metaspace::ClassType;
jmasa@4727 2757 i < Metaspace:: MetadataTypeCount;
jmasa@4727 2758 i = (Metaspace::MetadataType)(i + 1)) {
jmasa@4727 2759 size_t used_in_use_bytes = used_bytes_slow(i);
jmasa@4727 2760 assert(allocated_used_bytes(i) == used_in_use_bytes,
jmasa@4727 2761 err_msg("allocated_used_bytes(%u) " SIZE_FORMAT
jmasa@4727 2762 " used_bytes_slow(%u)" SIZE_FORMAT,
jmasa@4727 2763 i, allocated_used_bytes(i), i, used_in_use_bytes));
jmasa@4727 2764 }
jmasa@4580 2765 #endif
jmasa@4580 2766 }
jmasa@4580 2767
jmasa@4580 2768 void MetaspaceAux::verify_metrics() {
jmasa@4580 2769 verify_capacity();
jmasa@4580 2770 verify_used();
jmasa@4580 2771 }
jmasa@4580 2772
jmasa@4580 2773
coleenp@3602 2774 // Metaspace methods
coleenp@3602 2775
coleenp@3602 2776 size_t Metaspace::_first_chunk_word_size = 0;
jmasa@3947 2777 size_t Metaspace::_first_class_chunk_word_size = 0;
jmasa@3947 2778
stefank@5428 2779 size_t Metaspace::_commit_alignment = 0;
stefank@5428 2780 size_t Metaspace::_reserve_alignment = 0;
stefank@5428 2781
jmasa@3947 2782 Metaspace::Metaspace(Mutex* lock, MetaspaceType type) {
jmasa@3947 2783 initialize(lock, type);
coleenp@3602 2784 }
coleenp@3602 2785
coleenp@3602 2786 Metaspace::~Metaspace() {
coleenp@3602 2787 delete _vsm;
hseigel@5093 2788 if (using_class_space()) {
hseigel@5093 2789 delete _class_vsm;
hseigel@5093 2790 }
coleenp@3602 2791 }
coleenp@3602 2792
coleenp@3602 2793 VirtualSpaceList* Metaspace::_space_list = NULL;
coleenp@3602 2794 VirtualSpaceList* Metaspace::_class_space_list = NULL;
coleenp@3602 2795
stefank@5336 2796 ChunkManager* Metaspace::_chunk_manager_metadata = NULL;
stefank@5336 2797 ChunkManager* Metaspace::_chunk_manager_class = NULL;
stefank@5336 2798
coleenp@3602 2799 #define VIRTUALSPACEMULTIPLIER 2
coleenp@3602 2800
hseigel@5093 2801 #ifdef _LP64
hseigel@5093 2802 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
hseigel@5093 2803 // Figure out the narrow_klass_base and the narrow_klass_shift. The
hseigel@5093 2804 // narrow_klass_base is the lower of the metaspace base and the cds base
hseigel@5093 2805 // (if cds is enabled). The narrow_klass_shift depends on the distance
hseigel@5093 2806 // between the lower base and higher address.
hseigel@5093 2807 address lower_base;
hseigel@5093 2808 address higher_address;
hseigel@5093 2809 if (UseSharedSpaces) {
hseigel@5093 2810 higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
hseigel@5093 2811 (address)(metaspace_base + class_metaspace_size()));
hseigel@5093 2812 lower_base = MIN2(metaspace_base, cds_base);
hseigel@5093 2813 } else {
hseigel@5093 2814 higher_address = metaspace_base + class_metaspace_size();
hseigel@5093 2815 lower_base = metaspace_base;
hseigel@5093 2816 }
hseigel@5093 2817 Universe::set_narrow_klass_base(lower_base);
hseigel@5093 2818 if ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint) {
hseigel@5093 2819 Universe::set_narrow_klass_shift(0);
hseigel@5093 2820 } else {
hseigel@5093 2821 assert(!UseSharedSpaces, "Cannot shift with UseSharedSpaces");
hseigel@5093 2822 Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
hseigel@5093 2823 }
hseigel@5093 2824 }
hseigel@5093 2825
hseigel@5093 2826 // Return TRUE if the specified metaspace_base and cds_base are close enough
hseigel@5093 2827 // to work with compressed klass pointers.
hseigel@5093 2828 bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) {
hseigel@5093 2829 assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS");
ehelin@5259 2830 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
hseigel@5093 2831 address lower_base = MIN2((address)metaspace_base, cds_base);
hseigel@5093 2832 address higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
hseigel@5093 2833 (address)(metaspace_base + class_metaspace_size()));
hseigel@5093 2834 return ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint);
hseigel@5093 2835 }
hseigel@5093 2836
hseigel@5093 2837 // Try to allocate the metaspace at the requested addr.
hseigel@5093 2838 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) {
hseigel@5093 2839 assert(using_class_space(), "called improperly");
ehelin@5259 2840 assert(UseCompressedClassPointers, "Only use with CompressedKlassPtrs");
hseigel@5093 2841 assert(class_metaspace_size() < KlassEncodingMetaspaceMax,
hseigel@5093 2842 "Metaspace size is too big");
stefank@5428 2843 assert_is_ptr_aligned(requested_addr, _reserve_alignment);
stefank@5428 2844 assert_is_ptr_aligned(cds_base, _reserve_alignment);
stefank@5428 2845 assert_is_size_aligned(class_metaspace_size(), _reserve_alignment);
stefank@5428 2846
stefank@5428 2847 // Don't use large pages for the class space.
stefank@5428 2848 bool large_pages = false;
hseigel@5093 2849
hseigel@5093 2850 ReservedSpace metaspace_rs = ReservedSpace(class_metaspace_size(),
stefank@5428 2851 _reserve_alignment,
stefank@5428 2852 large_pages,
stefank@5428 2853 requested_addr, 0);
hseigel@5093 2854 if (!metaspace_rs.is_reserved()) {
hseigel@5093 2855 if (UseSharedSpaces) {
stefank@5428 2856 size_t increment = align_size_up(1*G, _reserve_alignment);
stefank@5428 2857
hseigel@5093 2858 // Keep trying to allocate the metaspace, increasing the requested_addr
hseigel@5093 2859 // by 1GB each time, until we reach an address that will no longer allow
hseigel@5093 2860 // use of CDS with compressed klass pointers.
hseigel@5093 2861 char *addr = requested_addr;
stefank@5428 2862 while (!metaspace_rs.is_reserved() && (addr + increment > addr) &&
stefank@5428 2863 can_use_cds_with_metaspace_addr(addr + increment, cds_base)) {
stefank@5428 2864 addr = addr + increment;
hseigel@5093 2865 metaspace_rs = ReservedSpace(class_metaspace_size(),
stefank@5428 2866 _reserve_alignment, large_pages, addr, 0);
hseigel@5093 2867 }
hseigel@5093 2868 }
hseigel@5093 2869
hseigel@5093 2870 // If no successful allocation then try to allocate the space anywhere. If
hseigel@5093 2871 // that fails then OOM doom. At this point we cannot try allocating the
ehelin@5259 2872 // metaspace as if UseCompressedClassPointers is off because too much
ehelin@5259 2873 // initialization has happened that depends on UseCompressedClassPointers.
ehelin@5259 2874 // So, UseCompressedClassPointers cannot be turned off at this point.
hseigel@5093 2875 if (!metaspace_rs.is_reserved()) {
hseigel@5093 2876 metaspace_rs = ReservedSpace(class_metaspace_size(),
stefank@5428 2877 _reserve_alignment, large_pages);
hseigel@5093 2878 if (!metaspace_rs.is_reserved()) {
hseigel@5093 2879 vm_exit_during_initialization(err_msg("Could not allocate metaspace: %d bytes",
hseigel@5093 2880 class_metaspace_size()));
hseigel@5093 2881 }
hseigel@5093 2882 }
hseigel@5093 2883 }
hseigel@5093 2884
hseigel@5093 2885 // If we got here then the metaspace got allocated.
hseigel@5093 2886 MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass);
hseigel@5093 2887
hseigel@5093 2888 // Verify that we can use shared spaces. Otherwise, turn off CDS.
hseigel@5093 2889 if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) {
hseigel@5093 2890 FileMapInfo::stop_sharing_and_unmap(
hseigel@5093 2891 "Could not allocate metaspace at a compatible address");
hseigel@5093 2892 }
hseigel@5093 2893
hseigel@5093 2894 set_narrow_klass_base_and_shift((address)metaspace_rs.base(),
hseigel@5093 2895 UseSharedSpaces ? (address)cds_base : 0);
hseigel@5093 2896
hseigel@5093 2897 initialize_class_space(metaspace_rs);
hseigel@5093 2898
hseigel@5093 2899 if (PrintCompressedOopsMode || (PrintMiscellaneous && Verbose)) {
hseigel@5093 2900 gclog_or_tty->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: " SIZE_FORMAT,
hseigel@5093 2901 Universe::narrow_klass_base(), Universe::narrow_klass_shift());
hseigel@5093 2902 gclog_or_tty->print_cr("Metaspace Size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT,
hseigel@5093 2903 class_metaspace_size(), metaspace_rs.base(), requested_addr);
hseigel@5093 2904 }
hseigel@5093 2905 }
hseigel@5093 2906
ehelin@5259 2907 // For UseCompressedClassPointers the class space is reserved above the top of
hseigel@5093 2908 // the Java heap. The argument passed in is at the base of the compressed space.
hseigel@5093 2909 void Metaspace::initialize_class_space(ReservedSpace rs) {
hseigel@5093 2910 // The reserved space size may be bigger because of alignment, esp with UseLargePages
ehelin@5259 2911 assert(rs.size() >= CompressedClassSpaceSize,
ehelin@5259 2912 err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), CompressedClassSpaceSize));
hseigel@5093 2913 assert(using_class_space(), "Must be using class space");
hseigel@5093 2914 _class_space_list = new VirtualSpaceList(rs);
stefank@5336 2915 _chunk_manager_class = new ChunkManager(SpecializedChunk, ClassSmallChunk, ClassMediumChunk);
stefank@5428 2916
stefank@5428 2917 if (!_class_space_list->initialization_succeeded()) {
stefank@5428 2918 vm_exit_during_initialization("Failed to setup compressed class space virtual space list.");
stefank@5428 2919 }
hseigel@5093 2920 }
hseigel@5093 2921
hseigel@5093 2922 #endif
hseigel@5093 2923
stefank@5428 2924 // Align down. If the aligning result in 0, return 'alignment'.
stefank@5428 2925 static size_t restricted_align_down(size_t size, size_t alignment) {
stefank@5428 2926 return MAX2(alignment, align_size_down_(size, alignment));
stefank@5428 2927 }
stefank@5428 2928
stefank@5428 2929 void Metaspace::ergo_initialize() {
stefank@5428 2930 if (DumpSharedSpaces) {
stefank@5428 2931 // Using large pages when dumping the shared archive is currently not implemented.
stefank@5428 2932 FLAG_SET_ERGO(bool, UseLargePagesInMetaspace, false);
stefank@5428 2933 }
stefank@5428 2934
stefank@5428 2935 size_t page_size = os::vm_page_size();
stefank@5428 2936 if (UseLargePages && UseLargePagesInMetaspace) {
stefank@5428 2937 page_size = os::large_page_size();
stefank@5428 2938 }
stefank@5428 2939
stefank@5428 2940 _commit_alignment = page_size;
stefank@5428 2941 _reserve_alignment = MAX2(page_size, (size_t)os::vm_allocation_granularity());
stefank@5428 2942
stefank@5428 2943 // Do not use FLAG_SET_ERGO to update MaxMetaspaceSize, since this will
stefank@5428 2944 // override if MaxMetaspaceSize was set on the command line or not.
stefank@5428 2945 // This information is needed later to conform to the specification of the
stefank@5428 2946 // java.lang.management.MemoryUsage API.
stefank@5428 2947 //
stefank@5428 2948 // Ideally, we would be able to set the default value of MaxMetaspaceSize in
stefank@5428 2949 // globals.hpp to the aligned value, but this is not possible, since the
stefank@5428 2950 // alignment depends on other flags being parsed.
stefank@5428 2951 MaxMetaspaceSize = restricted_align_down(MaxMetaspaceSize, _reserve_alignment);
stefank@5428 2952
stefank@5428 2953 if (MetaspaceSize > MaxMetaspaceSize) {
stefank@5428 2954 MetaspaceSize = MaxMetaspaceSize;
stefank@5428 2955 }
stefank@5428 2956
stefank@5428 2957 MetaspaceSize = restricted_align_down(MetaspaceSize, _commit_alignment);
stefank@5428 2958
stefank@5428 2959 assert(MetaspaceSize <= MaxMetaspaceSize, "MetaspaceSize should be limited by MaxMetaspaceSize");
stefank@5428 2960
stefank@5428 2961 if (MetaspaceSize < 256*K) {
stefank@5428 2962 vm_exit_during_initialization("Too small initial Metaspace size");
stefank@5428 2963 }
stefank@5428 2964
stefank@5428 2965 MinMetaspaceExpansion = restricted_align_down(MinMetaspaceExpansion, _commit_alignment);
stefank@5428 2966 MaxMetaspaceExpansion = restricted_align_down(MaxMetaspaceExpansion, _commit_alignment);
stefank@5428 2967
stefank@5428 2968 CompressedClassSpaceSize = restricted_align_down(CompressedClassSpaceSize, _reserve_alignment);
stefank@5428 2969 set_class_metaspace_size(CompressedClassSpaceSize);
stefank@5428 2970 }
stefank@5428 2971
coleenp@3602 2972 void Metaspace::global_initialize() {
coleenp@3602 2973 // Initialize the alignment for shared spaces.
coleenp@3602 2974 int max_alignment = os::vm_page_size();
hseigel@5093 2975 size_t cds_total = 0;
hseigel@5093 2976
coleenp@3602 2977 MetaspaceShared::set_max_alignment(max_alignment);
coleenp@3602 2978
coleenp@3602 2979 if (DumpSharedSpaces) {
stefank@5428 2980 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment);
coleenp@3602 2981 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment);
stefank@5428 2982 SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment);
stefank@5428 2983 SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment);
coleenp@3602 2984
coleenp@3602 2985 // Initialize with the sum of the shared space sizes. The read-only
coleenp@3602 2986 // and read write metaspace chunks will be allocated out of this and the
coleenp@3602 2987 // remainder is the misc code and data chunks.
hseigel@5093 2988 cds_total = FileMapInfo::shared_spaces_size();
stefank@5428 2989 cds_total = align_size_up(cds_total, _reserve_alignment);
hseigel@5093 2990 _space_list = new VirtualSpaceList(cds_total/wordSize);
stefank@5336 2991 _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk);
hseigel@5093 2992
stefank@5428 2993 if (!_space_list->initialization_succeeded()) {
stefank@5428 2994 vm_exit_during_initialization("Unable to dump shared archive.", NULL);
stefank@5428 2995 }
stefank@5428 2996
hseigel@5093 2997 #ifdef _LP64
stefank@5428 2998 if (cds_total + class_metaspace_size() > (uint64_t)max_juint) {
stefank@5428 2999 vm_exit_during_initialization("Unable to dump shared archive.",
stefank@5428 3000 err_msg("Size of archive (" SIZE_FORMAT ") + compressed class space ("
stefank@5428 3001 SIZE_FORMAT ") == total (" SIZE_FORMAT ") is larger than compressed "
stefank@5428 3002 "klass limit: " SIZE_FORMAT, cds_total, class_metaspace_size(),
stefank@5428 3003 cds_total + class_metaspace_size(), (size_t)max_juint));
stefank@5428 3004 }
stefank@5428 3005
hseigel@5093 3006 // Set the compressed klass pointer base so that decoding of these pointers works
hseigel@5093 3007 // properly when creating the shared archive.
ehelin@5259 3008 assert(UseCompressedOops && UseCompressedClassPointers,
ehelin@5259 3009 "UseCompressedOops and UseCompressedClassPointers must be set");
hseigel@5093 3010 Universe::set_narrow_klass_base((address)_space_list->current_virtual_space()->bottom());
hseigel@5093 3011 if (TraceMetavirtualspaceAllocation && Verbose) {
hseigel@5093 3012 gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT,
hseigel@5093 3013 _space_list->current_virtual_space()->bottom());
hseigel@5093 3014 }
hseigel@5093 3015
hseigel@5093 3016 Universe::set_narrow_klass_shift(0);
hseigel@5093 3017 #endif
hseigel@5093 3018
coleenp@3602 3019 } else {
coleenp@3602 3020 // If using shared space, open the file that contains the shared space
coleenp@3602 3021 // and map in the memory before initializing the rest of metaspace (so
coleenp@3602 3022 // the addresses don't conflict)
hseigel@5093 3023 address cds_address = NULL;
coleenp@3602 3024 if (UseSharedSpaces) {
coleenp@3602 3025 FileMapInfo* mapinfo = new FileMapInfo();
coleenp@3602 3026 memset(mapinfo, 0, sizeof(FileMapInfo));
coleenp@3602 3027
coleenp@3602 3028 // Open the shared archive file, read and validate the header. If
coleenp@3602 3029 // initialization fails, shared spaces [UseSharedSpaces] are
coleenp@3602 3030 // disabled and the file is closed.
coleenp@3602 3031 // Map in spaces now also
coleenp@3602 3032 if (mapinfo->initialize() && MetaspaceShared::map_shared_spaces(mapinfo)) {
coleenp@3602 3033 FileMapInfo::set_current_info(mapinfo);
stefank@5428 3034 cds_total = FileMapInfo::shared_spaces_size();
stefank@5428 3035 cds_address = (address)mapinfo->region_base(0);
coleenp@3602 3036 } else {
coleenp@3602 3037 assert(!mapinfo->is_open() && !UseSharedSpaces,
coleenp@3602 3038 "archive file not closed or shared spaces not disabled.");
coleenp@3602 3039 }
coleenp@3602 3040 }
coleenp@3602 3041
hseigel@5093 3042 #ifdef _LP64
ehelin@5259 3043 // If UseCompressedClassPointers is set then allocate the metaspace area
hseigel@5093 3044 // above the heap and above the CDS area (if it exists).
hseigel@5093 3045 if (using_class_space()) {
hseigel@5093 3046 if (UseSharedSpaces) {
stefank@5428 3047 char* cds_end = (char*)(cds_address + cds_total);
stefank@5428 3048 cds_end = (char *)align_ptr_up(cds_end, _reserve_alignment);
stefank@5428 3049 allocate_metaspace_compressed_klass_ptrs(cds_end, cds_address);
hseigel@5093 3050 } else {
hseigel@5093 3051 allocate_metaspace_compressed_klass_ptrs((char *)CompressedKlassPointersBase, 0);
hseigel@5093 3052 }
hseigel@5093 3053 }
hseigel@5093 3054 #endif
hseigel@5093 3055
jmasa@3947 3056 // Initialize these before initializing the VirtualSpaceList
coleenp@3602 3057 _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
jmasa@3947 3058 _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
jmasa@3947 3059 // Make the first class chunk bigger than a medium chunk so it's not put
jmasa@3947 3060 // on the medium chunk list. The next chunk will be small and progress
jmasa@3947 3061 // from there. This size calculated by -version.
jmasa@3947 3062 _first_class_chunk_word_size = MIN2((size_t)MediumChunk*6,
ehelin@5259 3063 (CompressedClassSpaceSize/BytesPerWord)*2);
jmasa@3947 3064 _first_class_chunk_word_size = align_word_size_up(_first_class_chunk_word_size);
coleenp@3602 3065 // Arbitrarily set the initial virtual space to a multiple
coleenp@3602 3066 // of the boot class loader size.
stefank@5428 3067 size_t word_size = VIRTUALSPACEMULTIPLIER * _first_chunk_word_size;
stefank@5428 3068 word_size = align_size_up(word_size, Metaspace::reserve_alignment_words());
stefank@5428 3069
coleenp@3602 3070 // Initialize the list of virtual spaces.
coleenp@3602 3071 _space_list = new VirtualSpaceList(word_size);
stefank@5336 3072 _chunk_manager_metadata = new ChunkManager(SpecializedChunk, SmallChunk, MediumChunk);
stefank@5428 3073
stefank@5428 3074 if (!_space_list->initialization_succeeded()) {
stefank@5428 3075 vm_exit_during_initialization("Unable to setup metadata virtual space list.", NULL);
stefank@5428 3076 }
coleenp@3602 3077 }
stefank@5428 3078
stefank@5428 3079 MetaspaceGC::initialize();
coleenp@3602 3080 }
coleenp@3602 3081
stefank@5336 3082 Metachunk* Metaspace::get_initialization_chunk(MetadataType mdtype,
stefank@5336 3083 size_t chunk_word_size,
stefank@5336 3084 size_t chunk_bunch) {
stefank@5336 3085 // Get a chunk from the chunk freelist
stefank@5336 3086 Metachunk* chunk = get_chunk_manager(mdtype)->chunk_freelist_allocate(chunk_word_size);
stefank@5336 3087 if (chunk != NULL) {
stefank@5336 3088 return chunk;
stefank@5336 3089 }
stefank@5336