annotate src/share/vm/memory/metaspace.cpp @ 4902:de2d15ce3d4a

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