src/share/vm/memory/metaspace.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6722
e204777ac770
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24 #ifndef SHARE_VM_MEMORY_METASPACE_HPP
aoqi@0 25 #define SHARE_VM_MEMORY_METASPACE_HPP
aoqi@0 26
aoqi@0 27 #include "memory/allocation.hpp"
aoqi@0 28 #include "memory/memRegion.hpp"
aoqi@0 29 #include "memory/metaspaceChunkFreeListSummary.hpp"
aoqi@0 30 #include "runtime/virtualspace.hpp"
aoqi@0 31 #include "utilities/exceptions.hpp"
aoqi@0 32
aoqi@0 33 // Metaspace
aoqi@0 34 //
aoqi@0 35 // Metaspaces are Arenas for the VM's metadata.
aoqi@0 36 // They are allocated one per class loader object, and one for the null
aoqi@0 37 // bootstrap class loader
aoqi@0 38 // Eventually for bootstrap loader we'll have a read-only section and read-write
aoqi@0 39 // to write for DumpSharedSpaces and read for UseSharedSpaces
aoqi@0 40 //
aoqi@0 41 // block X ---+ +-------------------+
aoqi@0 42 // | | Virtualspace |
aoqi@0 43 // | | |
aoqi@0 44 // | | |
aoqi@0 45 // | |-------------------|
aoqi@0 46 // | || Chunk |
aoqi@0 47 // | || |
aoqi@0 48 // | ||---------- |
aoqi@0 49 // +------>||| block 0 | |
aoqi@0 50 // ||---------- |
aoqi@0 51 // ||| block 1 | |
aoqi@0 52 // ||---------- |
aoqi@0 53 // || |
aoqi@0 54 // |-------------------|
aoqi@0 55 // | |
aoqi@0 56 // | |
aoqi@0 57 // +-------------------+
aoqi@0 58 //
aoqi@0 59
aoqi@0 60 class ChunkManager;
aoqi@0 61 class ClassLoaderData;
aoqi@0 62 class Metablock;
aoqi@0 63 class Metachunk;
aoqi@0 64 class MetaspaceTracer;
aoqi@0 65 class MetaWord;
aoqi@0 66 class Mutex;
aoqi@0 67 class outputStream;
aoqi@0 68 class SpaceManager;
aoqi@0 69 class VirtualSpaceList;
aoqi@0 70
aoqi@0 71 // Metaspaces each have a SpaceManager and allocations
aoqi@0 72 // are done by the SpaceManager. Allocations are done
aoqi@0 73 // out of the current Metachunk. When the current Metachunk
aoqi@0 74 // is exhausted, the SpaceManager gets a new one from
aoqi@0 75 // the current VirtualSpace. When the VirtualSpace is exhausted
aoqi@0 76 // the SpaceManager gets a new one. The SpaceManager
aoqi@0 77 // also manages freelists of available Chunks.
aoqi@0 78 //
aoqi@0 79 // Currently the space manager maintains the list of
aoqi@0 80 // virtual spaces and the list of chunks in use. Its
aoqi@0 81 // allocate() method returns a block for use as a
aoqi@0 82 // quantum of metadata.
aoqi@0 83
aoqi@0 84 class Metaspace : public CHeapObj<mtClass> {
aoqi@0 85 friend class VMStructs;
aoqi@0 86 friend class SpaceManager;
aoqi@0 87 friend class VM_CollectForMetadataAllocation;
aoqi@0 88 friend class MetaspaceGC;
aoqi@0 89 friend class MetaspaceAux;
aoqi@0 90
aoqi@0 91 public:
aoqi@0 92 enum MetadataType {
aoqi@0 93 ClassType,
aoqi@0 94 NonClassType,
aoqi@0 95 MetadataTypeCount
aoqi@0 96 };
aoqi@0 97 enum MetaspaceType {
aoqi@0 98 StandardMetaspaceType,
aoqi@0 99 BootMetaspaceType,
aoqi@0 100 ROMetaspaceType,
aoqi@0 101 ReadWriteMetaspaceType,
aoqi@0 102 AnonymousMetaspaceType,
aoqi@0 103 ReflectionMetaspaceType
aoqi@0 104 };
aoqi@0 105
aoqi@0 106 private:
aoqi@0 107 void initialize(Mutex* lock, MetaspaceType type);
aoqi@0 108
aoqi@0 109 // Get the first chunk for a Metaspace. Used for
aoqi@0 110 // special cases such as the boot class loader, reflection
aoqi@0 111 // class loader and anonymous class loader.
aoqi@0 112 Metachunk* get_initialization_chunk(MetadataType mdtype,
aoqi@0 113 size_t chunk_word_size,
aoqi@0 114 size_t chunk_bunch);
aoqi@0 115
aoqi@0 116 // Align up the word size to the allocation word size
aoqi@0 117 static size_t align_word_size_up(size_t);
aoqi@0 118
aoqi@0 119 // Aligned size of the metaspace.
aoqi@0 120 static size_t _compressed_class_space_size;
aoqi@0 121
aoqi@0 122 static size_t compressed_class_space_size() {
aoqi@0 123 return _compressed_class_space_size;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 static void set_compressed_class_space_size(size_t size) {
aoqi@0 127 _compressed_class_space_size = size;
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 static size_t _first_chunk_word_size;
aoqi@0 131 static size_t _first_class_chunk_word_size;
aoqi@0 132
aoqi@0 133 static size_t _commit_alignment;
aoqi@0 134 static size_t _reserve_alignment;
aoqi@0 135
aoqi@0 136 SpaceManager* _vsm;
aoqi@0 137 SpaceManager* vsm() const { return _vsm; }
aoqi@0 138
aoqi@0 139 SpaceManager* _class_vsm;
aoqi@0 140 SpaceManager* class_vsm() const { return _class_vsm; }
aoqi@0 141
aoqi@0 142 // Allocate space for metadata of type mdtype. This is space
aoqi@0 143 // within a Metachunk and is used by
aoqi@0 144 // allocate(ClassLoaderData*, size_t, bool, MetadataType, TRAPS)
aoqi@0 145 MetaWord* allocate(size_t word_size, MetadataType mdtype);
aoqi@0 146
aoqi@0 147 // Virtual Space lists for both classes and other metadata
aoqi@0 148 static VirtualSpaceList* _space_list;
aoqi@0 149 static VirtualSpaceList* _class_space_list;
aoqi@0 150
aoqi@0 151 static ChunkManager* _chunk_manager_metadata;
aoqi@0 152 static ChunkManager* _chunk_manager_class;
aoqi@0 153
aoqi@0 154 static const MetaspaceTracer* _tracer;
aoqi@0 155
aoqi@0 156 public:
aoqi@0 157 static VirtualSpaceList* space_list() { return _space_list; }
aoqi@0 158 static VirtualSpaceList* class_space_list() { return _class_space_list; }
aoqi@0 159 static VirtualSpaceList* get_space_list(MetadataType mdtype) {
aoqi@0 160 assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
aoqi@0 161 return mdtype == ClassType ? class_space_list() : space_list();
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 static ChunkManager* chunk_manager_metadata() { return _chunk_manager_metadata; }
aoqi@0 165 static ChunkManager* chunk_manager_class() { return _chunk_manager_class; }
aoqi@0 166 static ChunkManager* get_chunk_manager(MetadataType mdtype) {
aoqi@0 167 assert(mdtype != MetadataTypeCount, "MetadaTypeCount can't be used as mdtype");
aoqi@0 168 return mdtype == ClassType ? chunk_manager_class() : chunk_manager_metadata();
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 static const MetaspaceTracer* tracer() { return _tracer; }
aoqi@0 172
aoqi@0 173 private:
aoqi@0 174 // This is used by DumpSharedSpaces only, where only _vsm is used. So we will
aoqi@0 175 // maintain a single list for now.
aoqi@0 176 void record_allocation(void* ptr, MetaspaceObj::Type type, size_t word_size);
aoqi@0 177
aoqi@0 178 #ifdef _LP64
aoqi@0 179 static void set_narrow_klass_base_and_shift(address metaspace_base, address cds_base);
aoqi@0 180
aoqi@0 181 // Returns true if can use CDS with metaspace allocated as specified address.
aoqi@0 182 static bool can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base);
aoqi@0 183
aoqi@0 184 static void allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base);
aoqi@0 185
aoqi@0 186 static void initialize_class_space(ReservedSpace rs);
aoqi@0 187 #endif
aoqi@0 188
aoqi@0 189 class AllocRecord : public CHeapObj<mtClass> {
aoqi@0 190 public:
aoqi@0 191 AllocRecord(address ptr, MetaspaceObj::Type type, int byte_size)
aoqi@0 192 : _next(NULL), _ptr(ptr), _type(type), _byte_size(byte_size) {}
aoqi@0 193 AllocRecord *_next;
aoqi@0 194 address _ptr;
aoqi@0 195 MetaspaceObj::Type _type;
aoqi@0 196 int _byte_size;
aoqi@0 197 };
aoqi@0 198
aoqi@0 199 AllocRecord * _alloc_record_head;
aoqi@0 200 AllocRecord * _alloc_record_tail;
aoqi@0 201
aoqi@0 202 size_t class_chunk_size(size_t word_size);
aoqi@0 203
aoqi@0 204 public:
aoqi@0 205
aoqi@0 206 Metaspace(Mutex* lock, MetaspaceType type);
aoqi@0 207 ~Metaspace();
aoqi@0 208
aoqi@0 209 static void ergo_initialize();
aoqi@0 210 static void global_initialize();
aoqi@0 211 static void post_initialize();
aoqi@0 212
aoqi@0 213 static size_t first_chunk_word_size() { return _first_chunk_word_size; }
aoqi@0 214 static size_t first_class_chunk_word_size() { return _first_class_chunk_word_size; }
aoqi@0 215
aoqi@0 216 static size_t reserve_alignment() { return _reserve_alignment; }
aoqi@0 217 static size_t reserve_alignment_words() { return _reserve_alignment / BytesPerWord; }
aoqi@0 218 static size_t commit_alignment() { return _commit_alignment; }
aoqi@0 219 static size_t commit_alignment_words() { return _commit_alignment / BytesPerWord; }
aoqi@0 220
aoqi@0 221 char* bottom() const;
aoqi@0 222 size_t used_words_slow(MetadataType mdtype) const;
aoqi@0 223 size_t free_words_slow(MetadataType mdtype) const;
aoqi@0 224 size_t capacity_words_slow(MetadataType mdtype) const;
aoqi@0 225
aoqi@0 226 size_t used_bytes_slow(MetadataType mdtype) const;
aoqi@0 227 size_t capacity_bytes_slow(MetadataType mdtype) const;
aoqi@0 228
aoqi@0 229 static MetaWord* allocate(ClassLoaderData* loader_data, size_t word_size,
aoqi@0 230 bool read_only, MetaspaceObj::Type type, TRAPS);
aoqi@0 231 void deallocate(MetaWord* ptr, size_t byte_size, bool is_class);
aoqi@0 232
aoqi@0 233 MetaWord* expand_and_allocate(size_t size,
aoqi@0 234 MetadataType mdtype);
aoqi@0 235
aoqi@0 236 static bool contains(const void* ptr);
aoqi@0 237
aoqi@0 238 void dump(outputStream* const out) const;
aoqi@0 239
aoqi@0 240 // Free empty virtualspaces
aoqi@0 241 static void purge(MetadataType mdtype);
aoqi@0 242 static void purge();
aoqi@0 243
aoqi@0 244 static void report_metadata_oome(ClassLoaderData* loader_data, size_t word_size,
aoqi@0 245 MetaspaceObj::Type type, MetadataType mdtype, TRAPS);
aoqi@0 246
aoqi@0 247 static const char* metadata_type_name(Metaspace::MetadataType mdtype);
aoqi@0 248
aoqi@0 249 void print_on(outputStream* st) const;
aoqi@0 250 // Debugging support
aoqi@0 251 void verify();
aoqi@0 252
aoqi@0 253 class AllocRecordClosure : public StackObj {
aoqi@0 254 public:
aoqi@0 255 virtual void doit(address ptr, MetaspaceObj::Type type, int byte_size) = 0;
aoqi@0 256 };
aoqi@0 257
aoqi@0 258 void iterate(AllocRecordClosure *closure);
aoqi@0 259
aoqi@0 260 // Return TRUE only if UseCompressedClassPointers is True and DumpSharedSpaces is False.
aoqi@0 261 static bool using_class_space() {
aoqi@0 262 return NOT_LP64(false) LP64_ONLY(UseCompressedClassPointers && !DumpSharedSpaces);
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 static bool is_class_space_allocation(MetadataType mdType) {
aoqi@0 266 return mdType == ClassType && using_class_space();
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 };
aoqi@0 270
aoqi@0 271 class MetaspaceAux : AllStatic {
aoqi@0 272 static size_t free_chunks_total_words(Metaspace::MetadataType mdtype);
aoqi@0 273
aoqi@0 274 // These methods iterate over the classloader data graph
aoqi@0 275 // for the given Metaspace type. These are slow.
aoqi@0 276 static size_t used_bytes_slow(Metaspace::MetadataType mdtype);
aoqi@0 277 static size_t free_bytes_slow(Metaspace::MetadataType mdtype);
aoqi@0 278 static size_t capacity_bytes_slow(Metaspace::MetadataType mdtype);
aoqi@0 279 static size_t capacity_bytes_slow();
aoqi@0 280
aoqi@0 281 // Running sum of space in all Metachunks that has been
aoqi@0 282 // allocated to a Metaspace. This is used instead of
aoqi@0 283 // iterating over all the classloaders. One for each
aoqi@0 284 // type of Metadata
aoqi@0 285 static size_t _capacity_words[Metaspace:: MetadataTypeCount];
aoqi@0 286 // Running sum of space in all Metachunks that
aoqi@0 287 // are being used for metadata. One for each
aoqi@0 288 // type of Metadata.
aoqi@0 289 static size_t _used_words[Metaspace:: MetadataTypeCount];
aoqi@0 290
aoqi@0 291 public:
aoqi@0 292 // Decrement and increment _allocated_capacity_words
aoqi@0 293 static void dec_capacity(Metaspace::MetadataType type, size_t words);
aoqi@0 294 static void inc_capacity(Metaspace::MetadataType type, size_t words);
aoqi@0 295
aoqi@0 296 // Decrement and increment _allocated_used_words
aoqi@0 297 static void dec_used(Metaspace::MetadataType type, size_t words);
aoqi@0 298 static void inc_used(Metaspace::MetadataType type, size_t words);
aoqi@0 299
aoqi@0 300 // Total of space allocated to metadata in all Metaspaces.
aoqi@0 301 // This sums the space used in each Metachunk by
aoqi@0 302 // iterating over the classloader data graph
aoqi@0 303 static size_t used_bytes_slow() {
aoqi@0 304 return used_bytes_slow(Metaspace::ClassType) +
aoqi@0 305 used_bytes_slow(Metaspace::NonClassType);
aoqi@0 306 }
aoqi@0 307
aoqi@0 308 // Used by MetaspaceCounters
aoqi@0 309 static size_t free_chunks_total_words();
aoqi@0 310 static size_t free_chunks_total_bytes();
aoqi@0 311 static size_t free_chunks_total_bytes(Metaspace::MetadataType mdtype);
aoqi@0 312
aoqi@0 313 static size_t capacity_words(Metaspace::MetadataType mdtype) {
aoqi@0 314 return _capacity_words[mdtype];
aoqi@0 315 }
aoqi@0 316 static size_t capacity_words() {
aoqi@0 317 return capacity_words(Metaspace::NonClassType) +
aoqi@0 318 capacity_words(Metaspace::ClassType);
aoqi@0 319 }
aoqi@0 320 static size_t capacity_bytes(Metaspace::MetadataType mdtype) {
aoqi@0 321 return capacity_words(mdtype) * BytesPerWord;
aoqi@0 322 }
aoqi@0 323 static size_t capacity_bytes() {
aoqi@0 324 return capacity_words() * BytesPerWord;
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 static size_t used_words(Metaspace::MetadataType mdtype) {
aoqi@0 328 return _used_words[mdtype];
aoqi@0 329 }
aoqi@0 330 static size_t used_words() {
aoqi@0 331 return used_words(Metaspace::NonClassType) +
aoqi@0 332 used_words(Metaspace::ClassType);
aoqi@0 333 }
aoqi@0 334 static size_t used_bytes(Metaspace::MetadataType mdtype) {
aoqi@0 335 return used_words(mdtype) * BytesPerWord;
aoqi@0 336 }
aoqi@0 337 static size_t used_bytes() {
aoqi@0 338 return used_words() * BytesPerWord;
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 static size_t free_bytes();
aoqi@0 342 static size_t free_bytes(Metaspace::MetadataType mdtype);
aoqi@0 343
aoqi@0 344 static size_t reserved_bytes(Metaspace::MetadataType mdtype);
aoqi@0 345 static size_t reserved_bytes() {
aoqi@0 346 return reserved_bytes(Metaspace::ClassType) +
aoqi@0 347 reserved_bytes(Metaspace::NonClassType);
aoqi@0 348 }
aoqi@0 349
aoqi@0 350 static size_t committed_bytes(Metaspace::MetadataType mdtype);
aoqi@0 351 static size_t committed_bytes() {
aoqi@0 352 return committed_bytes(Metaspace::ClassType) +
aoqi@0 353 committed_bytes(Metaspace::NonClassType);
aoqi@0 354 }
aoqi@0 355
aoqi@0 356 static size_t min_chunk_size_words();
aoqi@0 357 static size_t min_chunk_size_bytes() {
aoqi@0 358 return min_chunk_size_words() * BytesPerWord;
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 static bool has_chunk_free_list(Metaspace::MetadataType mdtype);
aoqi@0 362 static MetaspaceChunkFreeListSummary chunk_free_list_summary(Metaspace::MetadataType mdtype);
aoqi@0 363
aoqi@0 364 // Print change in used metadata.
aoqi@0 365 static void print_metaspace_change(size_t prev_metadata_used);
aoqi@0 366 static void print_on(outputStream * out);
aoqi@0 367 static void print_on(outputStream * out, Metaspace::MetadataType mdtype);
aoqi@0 368
aoqi@0 369 static void print_class_waste(outputStream* out);
aoqi@0 370 static void print_waste(outputStream* out);
aoqi@0 371 static void dump(outputStream* out);
aoqi@0 372 static void verify_free_chunks();
aoqi@0 373 // Checks that the values returned by allocated_capacity_bytes() and
aoqi@0 374 // capacity_bytes_slow() are the same.
aoqi@0 375 static void verify_capacity();
aoqi@0 376 static void verify_used();
aoqi@0 377 static void verify_metrics();
aoqi@0 378 };
aoqi@0 379
aoqi@0 380 // Metaspace are deallocated when their class loader are GC'ed.
aoqi@0 381 // This class implements a policy for inducing GC's to recover
aoqi@0 382 // Metaspaces.
aoqi@0 383
aoqi@0 384 class MetaspaceGC : AllStatic {
aoqi@0 385
aoqi@0 386 // The current high-water-mark for inducing a GC.
aoqi@0 387 // When committed memory of all metaspaces reaches this value,
aoqi@0 388 // a GC is induced and the value is increased. Size is in bytes.
aoqi@0 389 static volatile intptr_t _capacity_until_GC;
aoqi@0 390
aoqi@0 391 // For a CMS collection, signal that a concurrent collection should
aoqi@0 392 // be started.
aoqi@0 393 static bool _should_concurrent_collect;
aoqi@0 394
aoqi@0 395 static uint _shrink_factor;
aoqi@0 396
aoqi@0 397 static size_t shrink_factor() { return _shrink_factor; }
aoqi@0 398 void set_shrink_factor(uint v) { _shrink_factor = v; }
aoqi@0 399
aoqi@0 400 public:
aoqi@0 401
aoqi@0 402 static void initialize();
aoqi@0 403 static void post_initialize();
aoqi@0 404
aoqi@0 405 static size_t capacity_until_GC();
aoqi@0 406 static size_t inc_capacity_until_GC(size_t v);
aoqi@0 407 static size_t dec_capacity_until_GC(size_t v);
aoqi@0 408
aoqi@0 409 static bool should_concurrent_collect() { return _should_concurrent_collect; }
aoqi@0 410 static void set_should_concurrent_collect(bool v) {
aoqi@0 411 _should_concurrent_collect = v;
aoqi@0 412 }
aoqi@0 413
aoqi@0 414 // The amount to increase the high-water-mark (_capacity_until_GC)
aoqi@0 415 static size_t delta_capacity_until_GC(size_t bytes);
aoqi@0 416
aoqi@0 417 // Tells if we have can expand metaspace without hitting set limits.
aoqi@0 418 static bool can_expand(size_t words, bool is_class);
aoqi@0 419
aoqi@0 420 // Returns amount that we can expand without hitting a GC,
aoqi@0 421 // measured in words.
aoqi@0 422 static size_t allowed_expansion();
aoqi@0 423
aoqi@0 424 // Calculate the new high-water mark at which to induce
aoqi@0 425 // a GC.
aoqi@0 426 static void compute_new_size();
aoqi@0 427 };
aoqi@0 428
aoqi@0 429 #endif // SHARE_VM_MEMORY_METASPACE_HPP

mercurial