src/share/vm/memory/metaspace.cpp

Wed, 01 May 2013 14:11:01 +0100

author
chegar
date
Wed, 01 May 2013 14:11:01 +0100
changeset 5246
4b52137b07c9
parent 4932
df254344edf1
child 5007
c23dbf0e8ab7
permissions
-rw-r--r--

Merge

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

mercurial