src/share/vm/memory/metaspace.cpp

Thu, 26 Sep 2013 12:18:21 +0200

author
tschatzl
date
Thu, 26 Sep 2013 12:18:21 +0200
changeset 5775
461159cd7a91
parent 5774
03f493ce3a71
child 5808
bc918fd1e584
permissions
-rw-r--r--

Merge

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

mercurial