src/share/vm/memory/metaspace.cpp

Fri, 16 Aug 2013 13:22:32 +0200

author
stefank
date
Fri, 16 Aug 2013 13:22:32 +0200
changeset 5578
4c84d351cca9
parent 5531
1a8fb39bdbc4
child 5694
7944aba7ba41
permissions
-rw-r--r--

8007074: SIGSEGV at ParMarkBitMap::verify_clear()
Summary: Replace the broken large pages implementation on Linux. New flag: -XX:+UseTransparentHugePages - Linux specific flag to turn on transparent huge page hinting with madvise(..., MAP_HUGETLB). Changed behavior: -XX:+UseLargePages - tries to use -XX:+UseTransparentHugePages before trying other large pages implementations (on Linux). Changed behavior: -XX:+UseHugeTLBFS - Use upfront allocation of Large Pages instead of using the broken implementation to dynamically committing large pages. Changed behavior: -XX:LargePageSizeInBytes - Turned off the ability to use this flag on Linux and provides warning to user if set to a value different than the OS chosen large page size. Changed behavior: Setting no large page size - Now defaults to use -XX:UseTransparentHugePages if the OS supports it. Previously, -XX:+UseHugeTLBFS was chosen if the OS was configured to use large pages.
Reviewed-by: tschatzl, dcubed, brutisso

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

mercurial