annotate src/share/vm/memory/metaspace.cpp @ 3613:11fb740ce98f

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