src/share/vm/memory/metaspace.cpp

Fri, 20 Sep 2013 10:53:28 +0200

author
stefank
date
Fri, 20 Sep 2013 10:53:28 +0200
changeset 5769
2c022e432e10
parent 5708
8c5e6482cbfc
child 5770
9361de86a50f
permissions
-rw-r--r--

8024974: Incorrect use of GC_locker::is_active()
Summary: SymbolTable and StringTable can make calls to GC_locker::is_active() outside a safepoint. This isn't safe because the GC_locker active state (lock count) is only updated at a safepoint and only remains valid as long as _needs_gc is true. However, outside a safepoint_needs_gc can change to false at any time, which makes it impossible to do a correct call to is_active() in that context. In this case these calls can just be removed since the input argument to basic_add() should never be on the heap and so there's no need to check the GC_locker state. This change also adjusts the assert() in is_active() to makes sure all calls to this function are always done under a safepoint.
Reviewed-by: brutisso, dcubed
Contributed-by: per.liden@oracle.com

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

mercurial