src/share/vm/memory/metaspace.cpp

Mon, 12 Aug 2013 17:24:54 -0400

author
coleenp
date
Mon, 12 Aug 2013 17:24:54 -0400
changeset 5508
85147f28faba
parent 5452
83ca9dc4564d
child 5528
740e263c80c6
permissions
-rw-r--r--

8009728: nsk/jvmti/AttachOnDemand/attach030 crashes on Win32
Summary: ActiveMethodOopsCache was used to keep track of old versions of some methods that are cached in Universe but is buggy with permgen removal and not needed anymore
Reviewed-by: sspitsyn, dcubed, mseledtsov

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

mercurial