annotate src/share/vm/memory/metaspace.cpp @ 4572:c23dbf0e8ab7

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