annotate src/share/vm/memory/metaspace.cpp @ 5093:740e263c80c6

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