annotate src/share/vm/memory/metaspace.cpp @ 5335:9361de86a50f

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