src/share/vm/memory/metaspace.cpp

Wed, 11 Sep 2013 09:37:14 +0200

author
mgerdin
date
Wed, 11 Sep 2013 09:37:14 +0200
changeset 5699
24e87613ee58
parent 5694
7944aba7ba41
child 5703
d6c266999345
permissions
-rw-r--r--

8009561: NPG: Metaspace fragmentation when retiring a Metachunk
Summary: Use best-fit block-splitting freelist allocation from the block freelist.
Reviewed-by: jmasa, stefank

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

mercurial