src/share/vm/memory/metaspace.cpp

Thu, 17 Jan 2013 19:04:48 -0800

author
jmasa
date
Thu, 17 Jan 2013 19:04:48 -0800
changeset 4457
59a58e20dc60
parent 4383
1de1b145f6bc
child 4548
d9058e388631
permissions
-rw-r--r--

8006537: Assert when dumping archive with default methods
Reviewed-by: coleenp

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

mercurial