annotate src/share/vm/memory/metaspace.cpp @ 3860:59c790074993

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