collectedHeap.hpp 26.5 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28
#ifndef SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_HPP
#define SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_HPP

#include "gc_interface/gcCause.hpp"
29
#include "gc_interface/allocTracer.hpp"
S
sla 已提交
30
#include "gc_implementation/shared/gcWhen.hpp"
31 32 33 34 35
#include "memory/allocation.hpp"
#include "memory/barrierSet.hpp"
#include "runtime/handles.hpp"
#include "runtime/perfData.hpp"
#include "runtime/safepoint.hpp"
36
#include "utilities/events.hpp"
37

D
duke 已提交
38 39 40 41 42 43
// A "CollectedHeap" is an implementation of a java heap for HotSpot.  This
// is an abstract class: there may be many different kinds of heaps.  This
// class defines the functions that a heap must implement, and contains
// infrastructure common to all heaps.

class AdaptiveSizePolicy;
S
sla 已提交
44
class BarrierSet;
45
class CollectorPolicy;
S
sla 已提交
46 47 48 49 50 51 52
class GCHeapSummary;
class GCTimer;
class GCTracer;
class MetaspaceSummary;
class Thread;
class ThreadClosure;
class VirtualSpaceSummary;
J
johnc 已提交
53
class nmethod;
D
duke 已提交
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
class GCMessage : public FormatBuffer<1024> {
 public:
  bool is_before;

 public:
  GCMessage() {}
};

class GCHeapLog : public EventLogBase<GCMessage> {
 private:
  void log_heap(bool before);

 public:
  GCHeapLog() : EventLogBase<GCMessage>("GC Heap History") {}

  void log_heap_before() {
    log_heap(true);
  }
  void log_heap_after() {
    log_heap(false);
  }
};

D
duke 已提交
78 79 80 81 82 83 84
//
// CollectedHeap
//   SharedHeap
//     GenCollectedHeap
//     G1CollectedHeap
//   ParallelScavengeHeap
//
Z
zgu 已提交
85
class CollectedHeap : public CHeapObj<mtInternal> {
D
duke 已提交
86 87 88 89 90 91 92
  friend class VMStructs;
  friend class IsGCActiveMark; // Block structured external access to _is_gc_active

#ifdef ASSERT
  static int       _fire_out_of_memory_count;
#endif

93 94 95
  // Used for filler objects (static, but initialized in ctor).
  static size_t _filler_array_max_size;

96 97
  GCHeapLog* _gc_heap_log;

98 99 100
  // Used in support of ReduceInitialCardMarks; only consulted if COMPILER2 is being used
  bool _defer_initial_card_mark;

D
duke 已提交
101 102 103 104
 protected:
  MemRegion _reserved;
  BarrierSet* _barrier_set;
  bool _is_gc_active;
105
  uint _n_par_threads;
106

D
duke 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  unsigned int _total_collections;          // ... started
  unsigned int _total_full_collections;     // ... started
  NOT_PRODUCT(volatile size_t _promotion_failure_alot_count;)
  NOT_PRODUCT(volatile size_t _promotion_failure_alot_gc_number;)

  // Reason for current garbage collection.  Should be set to
  // a value reflecting no collection between collections.
  GCCause::Cause _gc_cause;
  GCCause::Cause _gc_lastcause;
  PerfStringVariable* _perf_gc_cause;
  PerfStringVariable* _perf_gc_lastcause;

  // Constructor
  CollectedHeap();

122 123 124 125 126 127 128
  // Do common initializations that must follow instance construction,
  // for example, those needing virtual calls.
  // This code could perhaps be moved into initialize() but would
  // be slightly more awkward because we want the latter to be a
  // pure virtual.
  void pre_initialize();

129
  // Create a new tlab. All TLAB allocations must go through this.
D
duke 已提交
130 131 132 133 134 135 136 137 138
  virtual HeapWord* allocate_new_tlab(size_t size);

  // Accumulate statistics on all tlabs.
  virtual void accumulate_statistics_all_tlabs();

  // Reinitialize tlabs before resuming mutators.
  virtual void resize_all_tlabs();

  // Allocate from the current thread's TLAB, with broken-out slow path.
S
sla 已提交
139 140
  inline static HeapWord* allocate_from_tlab(KlassHandle klass, Thread* thread, size_t size);
  static HeapWord* allocate_from_tlab_slow(KlassHandle klass, Thread* thread, size_t size);
D
duke 已提交
141 142 143

  // Allocate an uninitialized block of the given size, or returns NULL if
  // this is impossible.
S
sla 已提交
144
  inline static HeapWord* common_mem_allocate_noinit(KlassHandle klass, size_t size, TRAPS);
D
duke 已提交
145 146 147

  // Like allocate_init, but the block returned by a successful allocation
  // is guaranteed initialized to zeros.
S
sla 已提交
148
  inline static HeapWord* common_mem_allocate_init(KlassHandle klass, size_t size, TRAPS);
D
duke 已提交
149 150

  // Helper functions for (VM) allocation.
151
  inline static void post_allocation_setup_common(KlassHandle klass, HeapWord* obj);
D
duke 已提交
152
  inline static void post_allocation_setup_no_klass_install(KlassHandle klass,
153
                                                            HeapWord* objPtr);
D
duke 已提交
154

155
  inline static void post_allocation_setup_obj(KlassHandle klass, HeapWord* obj, int size);
D
duke 已提交
156 157

  inline static void post_allocation_setup_array(KlassHandle klass,
158
                                                 HeapWord* obj, int length);
D
duke 已提交
159 160 161 162

  // Clears an allocated object.
  inline static void init_obj(HeapWord* obj, size_t size);

163 164 165 166 167
  // Filler object utilities.
  static inline size_t filler_array_hdr_size();
  static inline size_t filler_array_min_size();

  DEBUG_ONLY(static void fill_args_check(HeapWord* start, size_t words);)
J
johnc 已提交
168
  DEBUG_ONLY(static void zap_filler_array(HeapWord* start, size_t words, bool zap = true);)
169 170 171

  // Fill with a single array; caller must ensure filler_array_min_size() <=
  // words <= filler_array_max_size().
J
johnc 已提交
172
  static inline void fill_with_array(HeapWord* start, size_t words, bool zap = true);
173 174

  // Fill with a single object (either an int array or a java.lang.Object).
J
johnc 已提交
175
  static inline void fill_with_object_impl(HeapWord* start, size_t words, bool zap = true);
176

S
sla 已提交
177 178
  virtual void trace_heap(GCWhen::Type when, GCTracer* tracer);

D
duke 已提交
179 180 181 182 183
  // Verification functions
  virtual void check_for_bad_heap_word_value(HeapWord* addr, size_t size)
    PRODUCT_RETURN;
  virtual void check_for_non_bad_heap_word_value(HeapWord* addr, size_t size)
    PRODUCT_RETURN;
184
  debug_only(static void check_for_valid_allocation_state();)
D
duke 已提交
185 186 187 188 189 190 191 192 193 194

 public:
  enum Name {
    Abstract,
    SharedHeap,
    GenCollectedHeap,
    ParallelScavengeHeap,
    G1CollectedHeap
  };

195 196 197 198
  static inline size_t filler_array_max_size() {
    return _filler_array_max_size;
  }

D
duke 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211
  virtual CollectedHeap::Name kind() const { return CollectedHeap::Abstract; }

  /**
   * Returns JNI error code JNI_ENOMEM if memory could not be allocated,
   * and JNI_OK on success.
   */
  virtual jint initialize() = 0;

  // In many heaps, there will be a need to perform some initialization activities
  // after the Universe is fully formed, but before general heap allocation is allowed.
  // This is the correct place to place such initialization methods.
  virtual void post_initialize() = 0;

212 213 214
  // Stop any onging concurrent work and prepare for exit.
  virtual void stop() {}

D
duke 已提交
215
  MemRegion reserved_region() const { return _reserved; }
216
  address base() const { return (address)reserved_region().start(); }
D
duke 已提交
217 218 219 220 221 222 223 224 225 226 227 228

  virtual size_t capacity() const = 0;
  virtual size_t used() const = 0;

  // Return "true" if the part of the heap that allocates Java
  // objects has reached the maximal committed limit that it can
  // reach, without a garbage collection.
  virtual bool is_maximal_no_gc() const = 0;

  // Support for java.lang.Runtime.maxMemory():  return the maximum amount of
  // memory that the vm could make available for storing 'normal' java objects.
  // This is based on the reserved address space, but should not include space
229 230
  // that the vm uses internally for bookkeeping or temporary storage
  // (e.g., in the case of the young gen, one of the survivor
D
duke 已提交
231 232 233 234 235 236 237 238 239 240 241 242
  // spaces).
  virtual size_t max_capacity() const = 0;

  // Returns "TRUE" if "p" points into the reserved area of the heap.
  bool is_in_reserved(const void* p) const {
    return _reserved.contains(p);
  }

  bool is_in_reserved_or_null(const void* p) const {
    return p == NULL || is_in_reserved(p);
  }

S
stefank 已提交
243 244
  // Returns "TRUE" iff "p" points into the committed areas of the heap.
  // Since this method can be expensive in general, we restrict its
D
duke 已提交
245 246 247 248 249 250 251
  // use to assertion checking only.
  virtual bool is_in(const void* p) const = 0;

  bool is_in_or_null(const void* p) const {
    return p == NULL || is_in(p);
  }

252 253 254 255 256 257 258 259 260
  bool is_in_place(Metadata** p) {
    return !Universe::heap()->is_in(p);
  }
  bool is_in_place(oop* p) { return Universe::heap()->is_in(p); }
  bool is_in_place(narrowOop* p) {
    oop o = oopDesc::load_decode_heap_oop_not_null(p);
    return Universe::heap()->is_in((const void*)o);
  }

D
duke 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
  // Let's define some terms: a "closed" subset of a heap is one that
  //
  // 1) contains all currently-allocated objects, and
  //
  // 2) is closed under reference: no object in the closed subset
  //    references one outside the closed subset.
  //
  // Membership in a heap's closed subset is useful for assertions.
  // Clearly, the entire heap is a closed subset, so the default
  // implementation is to use "is_in_reserved".  But this may not be too
  // liberal to perform useful checking.  Also, the "is_in" predicate
  // defines a closed subset, but may be too expensive, since "is_in"
  // verifies that its argument points to an object head.  The
  // "closed_subset" method allows a heap to define an intermediate
  // predicate, allowing more precise checking than "is_in_reserved" at
  // lower cost than "is_in."

  // One important case is a heap composed of disjoint contiguous spaces,
  // such as the Garbage-First collector.  Such heaps have a convenient
  // closed subset consisting of the allocated portions of those
  // contiguous spaces.

  // Return "TRUE" iff the given pointer points into the heap's defined
  // closed subset (which defaults to the entire heap).
  virtual bool is_in_closed_subset(const void* p) const {
    return is_in_reserved(p);
  }

  bool is_in_closed_subset_or_null(const void* p) const {
    return p == NULL || is_in_closed_subset(p);
  }

293 294 295 296 297 298
#ifdef ASSERT
  // Returns true if "p" is in the part of the
  // heap being collected.
  virtual bool is_in_partial_collection(const void *p) = 0;
#endif

299 300
  // An object is scavengable if its location may move during a scavenge.
  // (A scavenge is a GC which is not a full GC.)
301
  virtual bool is_scavengable(const void *p) = 0;
302

D
duke 已提交
303 304 305 306 307 308 309 310 311 312
  void set_gc_cause(GCCause::Cause v) {
     if (UsePerfData) {
       _gc_lastcause = _gc_cause;
       _perf_gc_lastcause->set_value(GCCause::to_string(_gc_lastcause));
       _perf_gc_cause->set_value(GCCause::to_string(v));
     }
    _gc_cause = v;
  }
  GCCause::Cause gc_cause() { return _gc_cause; }

313
  // Number of threads currently working on GC tasks.
314
  uint n_par_threads() { return _n_par_threads; }
315 316

  // May be overridden to set additional parallelism.
317
  virtual void set_par_threads(uint t) { _n_par_threads = t; };
318

D
duke 已提交
319 320 321
  // General obj/array allocation facilities.
  inline static oop obj_allocate(KlassHandle klass, int size, TRAPS);
  inline static oop array_allocate(KlassHandle klass, int size, int length, TRAPS);
322
  inline static oop array_allocate_nozero(KlassHandle klass, int size, int length, TRAPS);
323 324
 private:
  inline static void check_array_size(int size, int length, TRAPS);
D
duke 已提交
325

326
 public:
327 328 329 330 331 332 333 334 335
  // Implicit Jfr inline methods.
  static void trace_slow_allocation(KlassHandle klass, oop obj, size_t alloc_size, Thread* thread) {
    AllocTracer::send_slow_allocation_event(klass, obj, alloc_size, thread);
  }

  static void trace_allocation_outside_tlab(KlassHandle klass, HeapWord* obj, size_t alloc_size, Thread* thread) {
    AllocTracer::send_allocation_outside_tlab_event(klass, obj, alloc_size, thread);
  }

336 337
  inline static void post_allocation_install_obj_klass(KlassHandle klass,
                                                       oop obj);
D
duke 已提交
338 339 340

  // Raw memory allocation facilities
  // The obj and array allocate methods are covers for these methods.
341
  // mem_allocate() should never be
342
  // called to allocate TLABs, only individual objects.
D
duke 已提交
343 344 345
  virtual HeapWord* mem_allocate(size_t size,
                                 bool* gc_overhead_limit_was_exceeded) = 0;

346 347 348 349 350 351 352 353 354 355 356
  // Utilities for turning raw memory into filler objects.
  //
  // min_fill_size() is the smallest region that can be filled.
  // fill_with_objects() can fill arbitrary-sized regions of the heap using
  // multiple objects.  fill_with_object() is for regions known to be smaller
  // than the largest array of integers; it uses a single object to fill the
  // region and has slightly less overhead.
  static size_t min_fill_size() {
    return size_t(align_object_size(oopDesc::header_size()));
  }

J
johnc 已提交
357
  static void fill_with_objects(HeapWord* start, size_t words, bool zap = true);
358

J
johnc 已提交
359 360 361
  static void fill_with_object(HeapWord* start, size_t words, bool zap = true);
  static void fill_with_object(MemRegion region, bool zap = true) {
    fill_with_object(region.start(), region.word_size(), zap);
362
  }
J
johnc 已提交
363 364
  static void fill_with_object(HeapWord* start, HeapWord* end, bool zap = true) {
    fill_with_object(start, pointer_delta(end, start), zap);
365 366
  }

367 368 369 370 371 372
  // Return the address "addr" aligned by "alignment_in_bytes" if such
  // an address is below "end".  Return NULL otherwise.
  inline static HeapWord* align_allocation_or_fail(HeapWord* addr,
                                                   HeapWord* end,
                                                   unsigned short alignment_in_bytes);

D
duke 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
  // Some heaps may offer a contiguous region for shared non-blocking
  // allocation, via inlined code (by exporting the address of the top and
  // end fields defining the extent of the contiguous allocation region.)

  // This function returns "true" iff the heap supports this kind of
  // allocation.  (Default is "no".)
  virtual bool supports_inline_contig_alloc() const {
    return false;
  }
  // These functions return the addresses of the fields that define the
  // boundaries of the contiguous allocation area.  (These fields should be
  // physically near to one another.)
  virtual HeapWord** top_addr() const {
    guarantee(false, "inline contiguous allocation not supported");
    return NULL;
  }
  virtual HeapWord** end_addr() const {
    guarantee(false, "inline contiguous allocation not supported");
    return NULL;
  }

  // Some heaps may be in an unparseable state at certain times between
  // collections. This may be necessary for efficient implementation of
  // certain allocation-related activities. Calling this function before
  // attempting to parse a heap ensures that the heap is in a parsable
  // state (provided other concurrent activity does not introduce
  // unparsability). It is normally expected, therefore, that this
  // method is invoked with the world stopped.
  // NOTE: if you override this method, make sure you call
  // super::ensure_parsability so that the non-generational
  // part of the work gets done. See implementation of
  // CollectedHeap::ensure_parsability and, for instance,
  // that of GenCollectedHeap::ensure_parsability().
  // The argument "retire_tlabs" controls whether existing TLABs
  // are merely filled or also retired, thus preventing further
  // allocation from them and necessitating allocation of new TLABs.
  virtual void ensure_parsability(bool retire_tlabs);

  // Section on thread-local allocation buffers (TLABs)
  // If the heap supports thread-local allocation buffers, it should override
  // the following methods:
  // Returns "true" iff the heap supports thread-local allocation buffers.
  // The default is "no".
B
brutisso 已提交
416 417
  virtual bool supports_tlab_allocation() const = 0;

D
duke 已提交
418
  // The amount of space available for thread-local allocation buffers.
B
brutisso 已提交
419 420 421 422 423 424 425
  virtual size_t tlab_capacity(Thread *thr) const = 0;

  // The amount of used space for thread-local allocation buffers for the given thread.
  virtual size_t tlab_used(Thread *thr) const = 0;

  virtual size_t max_tlab_size() const;

D
duke 已提交
426 427 428 429 430 431 432
  // An estimate of the maximum allocation that could be performed
  // for thread-local allocation buffers without triggering any
  // collection or expansion activity.
  virtual size_t unsafe_max_tlab_alloc(Thread *thr) const {
    guarantee(false, "thread-local allocation buffers not supported");
    return 0;
  }
433

D
duke 已提交
434 435
  // Can a compiler initialize a new object without store barriers?
  // This permission only extends from the creation of a new object
436 437 438 439 440
  // via a TLAB up to the first subsequent safepoint. If such permission
  // is granted for this heap type, the compiler promises to call
  // defer_store_barrier() below on any slow path allocation of
  // a new object for which such initializing store barriers will
  // have been elided.
441 442
  virtual bool can_elide_tlab_store_barriers() const = 0;

D
duke 已提交
443 444 445 446 447
  // If a compiler is eliding store barriers for TLAB-allocated objects,
  // there is probably a corresponding slow path which can produce
  // an object allocated anywhere.  The compiler's runtime support
  // promises to call this function on such a slow-path-allocated
  // object before performing initializations that have elided
448
  // store barriers. Returns new_obj, or maybe a safer copy thereof.
449
  virtual oop new_store_pre_barrier(JavaThread* thread, oop new_obj);
450 451

  // Answers whether an initializing store to a new object currently
452
  // allocated at the given address doesn't need a store
453 454 455 456
  // barrier. Returns "true" if it doesn't need an initializing
  // store barrier; answers "false" if it does.
  virtual bool can_elide_initializing_store_barrier(oop new_obj) = 0;

457 458 459 460 461 462 463 464 465 466 467
  // If a compiler is eliding store barriers for TLAB-allocated objects,
  // we will be informed of a slow-path allocation by a call
  // to new_store_pre_barrier() above. Such a call precedes the
  // initialization of the object itself, and no post-store-barriers will
  // be issued. Some heap types require that the barrier strictly follows
  // the initializing stores. (This is currently implemented by deferring the
  // barrier until the next slow-path allocation or gc-related safepoint.)
  // This interface answers whether a particular heap type needs the card
  // mark to be thus strictly sequenced after the stores.
  virtual bool card_mark_must_follow_store() const = 0;

468 469 470 471
  // If the CollectedHeap was asked to defer a store barrier above,
  // this informs it to flush such a deferred store barrier to the
  // remembered set.
  virtual void flush_deferred_store_barrier(JavaThread* thread);
D
duke 已提交
472 473

  // Does this heap support heap inspection (+PrintClassHistogram?)
474
  virtual bool supports_heap_inspection() const = 0;
D
duke 已提交
475 476 477 478 479 480

  // Perform a collection of the heap; intended for use in implementing
  // "System.gc".  This probably implies as full a collection as the
  // "CollectedHeap" supports.
  virtual void collect(GCCause::Cause cause) = 0;

481 482 483
  // Perform a full collection
  virtual void do_full_collection(bool clear_all_soft_refs) = 0;

D
duke 已提交
484 485 486 487
  // This interface assumes that it's being called by the
  // vm thread. It collects the heap assuming that the
  // heap lock is already held and that we are executing in
  // the context of the vm thread.
488 489
  virtual void collect_as_vm_thread(GCCause::Cause cause);

D
duke 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
  // Returns the barrier set for this heap
  BarrierSet* barrier_set() { return _barrier_set; }

  // Returns "true" iff there is a stop-world GC in progress.  (I assume
  // that it should answer "false" for the concurrent part of a concurrent
  // collector -- dld).
  bool is_gc_active() const { return _is_gc_active; }

  // Total number of GC collections (started)
  unsigned int total_collections() const { return _total_collections; }
  unsigned int total_full_collections() const { return _total_full_collections;}

  // Increment total number of GC collections (started)
  // Should be protected but used by PSMarkSweep - cleanup for 1.4.2
  void increment_total_collections(bool full = false) {
    _total_collections++;
    if (full) {
      increment_total_full_collections();
    }
  }

  void increment_total_full_collections() { _total_full_collections++; }

  // Return the AdaptiveSizePolicy for the heap.
  virtual AdaptiveSizePolicy* size_policy() = 0;

516 517 518
  // Return the CollectorPolicy for the heap
  virtual CollectorPolicy* collector_policy() const = 0;

519 520
  void oop_iterate_no_header(OopClosure* cl);

D
duke 已提交
521
  // Iterate over all the ref-containing fields of all objects, calling
522 523
  // "cl.do_oop" on each.
  virtual void oop_iterate(ExtendedOopClosure* cl) = 0;
D
duke 已提交
524 525 526

  // Iterate over all objects, calling "cl.do_object" on each.
  virtual void object_iterate(ObjectClosure* cl) = 0;
527 528 529 530

  // Similar to object_iterate() except iterates only
  // over live objects.
  virtual void safe_object_iterate(ObjectClosure* cl) = 0;
D
duke 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

  // NOTE! There is no requirement that a collector implement these
  // functions.
  //
  // A CollectedHeap is divided into a dense sequence of "blocks"; that is,
  // each address in the (reserved) heap is a member of exactly
  // one block.  The defining characteristic of a block is that it is
  // possible to find its size, and thus to progress forward to the next
  // block.  (Blocks may be of different sizes.)  Thus, blocks may
  // represent Java objects, or they might be free blocks in a
  // free-list-based heap (or subheap), as long as the two kinds are
  // distinguishable and the size of each is determinable.

  // Returns the address of the start of the "block" that contains the
  // address "addr".  We say "blocks" instead of "object" since some heaps
  // may not pack objects densely; a chunk may either be an object or a
  // non-object.
  virtual HeapWord* block_start(const void* addr) const = 0;

  // Requires "addr" to be the start of a chunk, and returns its size.
  // "addr + size" is required to be the start of a new chunk, or the end
  // of the active area of the heap.
  virtual size_t block_size(const HeapWord* addr) const = 0;

  // Requires "addr" to be the start of a block, and returns "TRUE" iff
  // the block is an object.
  virtual bool block_is_obj(const HeapWord* addr) const = 0;

  // Returns the longest time (in ms) that has elapsed since the last
  // time that any part of the heap was examined by a garbage collection.
  virtual jlong millis_since_last_gc() = 0;

  // Perform any cleanup actions necessary before allowing a verification.
  virtual void prepare_for_verify() = 0;

566
  // Generate any dumps preceding or following a full gc
S
sla 已提交
567 568 569 570 571 572 573
  void pre_full_gc_dump(GCTimer* timer);
  void post_full_gc_dump(GCTimer* timer);

  VirtualSpaceSummary create_heap_space_summary();
  GCHeapSummary create_heap_summary();

  MetaspaceSummary create_metaspace_summary();
574

575
  // Print heap information on the given outputStream.
D
duke 已提交
576
  virtual void print_on(outputStream* st) const = 0;
577 578 579 580 581
  // The default behavior is to call print_on() on tty.
  virtual void print() const {
    print_on(tty);
  }
  // Print more detailed heap information on the given
S
sla 已提交
582
  // outputStream. The default behavior is to call print_on(). It is
583 584 585 586 587
  // up to each subclass to override it and add any additional output
  // it needs.
  virtual void print_extended_on(outputStream* st) const {
    print_on(st);
  }
D
duke 已提交
588

589 590 591 592 593 594 595 596
  virtual void print_on_error(outputStream* st) const {
    st->print_cr("Heap:");
    print_extended_on(st);
    st->cr();

    _barrier_set->print_on(st);
  }

D
duke 已提交
597 598 599
  // Print all GC threads (other than the VM thread)
  // used by this heap.
  virtual void print_gc_threads_on(outputStream* st) const = 0;
600 601 602 603
  // The default behavior is to call print_gc_threads_on() on tty.
  void print_gc_threads() {
    print_gc_threads_on(tty);
  }
D
duke 已提交
604 605 606 607 608 609 610
  // Iterator for all GC threads (other than VM thread)
  virtual void gc_threads_do(ThreadClosure* tc) const = 0;

  // Print any relevant tracing info that flags imply.
  // Default implementation does nothing.
  virtual void print_tracing_info() const = 0;

S
sla 已提交
611 612 613
  void print_heap_before_gc();
  void print_heap_after_gc();

J
johnc 已提交
614 615 616 617 618
  // Registering and unregistering an nmethod (compiled code) with the heap.
  // Override with specific mechanism for each specialized heap type.
  virtual void register_nmethod(nmethod* nm);
  virtual void unregister_nmethod(nmethod* nm);

S
sla 已提交
619 620
  void trace_heap_before_gc(GCTracer* gc_tracer);
  void trace_heap_after_gc(GCTracer* gc_tracer);
621

D
duke 已提交
622
  // Heap verification
623
  virtual void verify(bool silent, VerifyOption option) = 0;
D
duke 已提交
624 625 626 627 628 629 630 631 632 633

  // Non product verification and debugging.
#ifndef PRODUCT
  // Support for PromotionFailureALot.  Return true if it's time to cause a
  // promotion failure.  The no-argument version uses
  // this->_promotion_failure_alot_count as the counter.
  inline bool promotion_should_fail(volatile size_t* count);
  inline bool promotion_should_fail();

  // Reset the PromotionFailureALot counters.  Should be called at the end of a
S
sla 已提交
634
  // GC in which promotion failure occurred.
D
duke 已提交
635 636 637 638 639 640 641 642 643
  inline void reset_promotion_should_fail(volatile size_t* count);
  inline void reset_promotion_should_fail();
#endif  // #ifndef PRODUCT

#ifdef ASSERT
  static int fired_fake_oom() {
    return (CIFireOOMAt > 1 && _fire_out_of_memory_count >= CIFireOOMAt);
  }
#endif
644 645 646 647 648 649 650 651

 public:
  // This is a convenience method that is used in cases where
  // the actual number of GC worker threads is not pertinent but
  // only whether there more than 0.  Use of this method helps
  // reduce the occurrence of ParallelGCThreads to uses where the
  // actual number may be germane.
  static bool use_parallel_gc_threads() { return ParallelGCThreads > 0; }
S
stefank 已提交
652

653 654 655 656
  // Copy the current allocation context statistics for the specified contexts.
  // For each context in contexts, set the corresponding entries in the totals
  // and accuracy arrays to the current values held by the statistics.  Each
  // array should be of length len.
657 658
  // Returns true if there are more stats available.
  virtual bool copy_allocation_context_stats(const jint* contexts,
659 660
                                             jlong* totals,
                                             jbyte* accuracy,
661 662 663
                                             jint len) {
    return false;
  }
664

S
stefank 已提交
665 666 667
  /////////////// Unit tests ///////////////

  NOT_PRODUCT(static void test_is_in();)
D
duke 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
};

// Class to set and reset the GC cause for a CollectedHeap.

class GCCauseSetter : StackObj {
  CollectedHeap* _heap;
  GCCause::Cause _previous_cause;
 public:
  GCCauseSetter(CollectedHeap* heap, GCCause::Cause cause) {
    assert(SafepointSynchronize::is_at_safepoint(),
           "This method manipulates heap state without locking");
    _heap = heap;
    _previous_cause = _heap->gc_cause();
    _heap->set_gc_cause(cause);
  }

  ~GCCauseSetter() {
    assert(SafepointSynchronize::is_at_safepoint(),
          "This method manipulates heap state without locking");
    _heap->set_gc_cause(_previous_cause);
  }
};
690 691

#endif // SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_HPP