src/share/vm/memory/metaspace.cpp

Thu, 29 Nov 2012 16:50:29 -0500

author
coleenp
date
Thu, 29 Nov 2012 16:50:29 -0500
changeset 4304
90273fc0a981
parent 4264
6bc207d87e5d
child 4306
5fafdef522c6
permissions
-rw-r--r--

8000662: NPG: nashorn ant clean test262 out-of-memory with Java heap
Summary: Add ClassLoaderData object for each anonymous class with metaspaces to allocate in.
Reviewed-by: twisti, jrose, stefank

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

mercurial