annotate src/share/vm/memory/metaspace.cpp @ 5336:b960c9df4f11

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