src/share/vm/memory/metaspace.hpp

Fri, 21 Mar 2014 10:28:07 +0100

author
ehelin
date
Fri, 21 Mar 2014 10:28:07 +0100
changeset 6419
5af31f70a866
parent 6418
bc7714614ad8
child 6420
9fdaa79b0c27
permissions
-rw-r--r--

8036701: Add trace event when a metaspace throws out of memory error
Reviewed-by: stefank, mgerdin

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

mercurial