concurrentMark.hpp 47.6 KB
Newer Older
1
/*
2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
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.
22 23 24
 *
 */

25 26 27
#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_CONCURRENTMARK_HPP
#define SHARE_VM_GC_IMPLEMENTATION_G1_CONCURRENTMARK_HPP

28
#include "gc_implementation/g1/heapRegionSets.hpp"
29 30
#include "utilities/taskqueue.hpp"

31 32
class G1CollectedHeap;
class CMTask;
Z
zgu 已提交
33 34
typedef GenericTaskQueue<oop, mtGC>            CMTaskQueue;
typedef GenericTaskQueueSet<CMTaskQueue, mtGC> CMTaskQueueSet;
35

36 37 38 39 40 41 42 43 44
// Closure used by CM during concurrent reference discovery
// and reference processing (during remarking) to determine
// if a particular object is alive. It is primarily used
// to determine if referents of discovered reference objects
// are alive. An instance is also embedded into the
// reference processor as the _is_alive_non_header field
class G1CMIsAliveClosure: public BoolObjectClosure {
  G1CollectedHeap* _g1;
 public:
45
  G1CMIsAliveClosure(G1CollectedHeap* g1) : _g1(g1) { }
46 47 48 49 50 51 52

  void do_object(oop obj) {
    ShouldNotCallThis();
  }
  bool do_object_b(oop obj);
};

53 54 55
// A generic CM bit map.  This is essentially a wrapper around the BitMap
// class, with one bit per (1<<_shifter) HeapWords.

56
class CMBitMapRO VALUE_OBJ_CLASS_SPEC {
57 58 59 60 61 62 63 64 65
 protected:
  HeapWord* _bmStartWord;      // base address of range covered by map
  size_t    _bmWordSize;       // map size (in #HeapWords covered)
  const int _shifter;          // map to char or bit
  VirtualSpace _virtual_space; // underlying the bit map
  BitMap    _bm;               // the bit map itself

 public:
  // constructor
66
  CMBitMapRO(int shifter);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

  enum { do_yield = true };

  // inquiries
  HeapWord* startWord()   const { return _bmStartWord; }
  size_t    sizeInWords() const { return _bmWordSize;  }
  // the following is one past the last word in space
  HeapWord* endWord()     const { return _bmStartWord + _bmWordSize; }

  // read marks

  bool isMarked(HeapWord* addr) const {
    assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
           "outside underlying space?");
    return _bm.at(heapWordToOffset(addr));
  }

  // iteration
85 86
  inline bool iterate(BitMapClosure* cl, MemRegion mr);
  inline bool iterate(BitMapClosure* cl);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

  // Return the address corresponding to the next marked bit at or after
  // "addr", and before "limit", if "limit" is non-NULL.  If there is no
  // such bit, returns "limit" if that is non-NULL, or else "endWord()".
  HeapWord* getNextMarkedWordAddress(HeapWord* addr,
                                     HeapWord* limit = NULL) const;
  // Return the address corresponding to the next unmarked bit at or after
  // "addr", and before "limit", if "limit" is non-NULL.  If there is no
  // such bit, returns "limit" if that is non-NULL, or else "endWord()".
  HeapWord* getNextUnmarkedWordAddress(HeapWord* addr,
                                       HeapWord* limit = NULL) const;

  // conversion utilities
  // XXX Fix these so that offsets are size_t's...
  HeapWord* offsetToHeapWord(size_t offset) const {
    return _bmStartWord + (offset << _shifter);
  }
  size_t heapWordToOffset(HeapWord* addr) const {
    return pointer_delta(addr, _bmStartWord) >> _shifter;
  }
  int heapWordDiffToOffsetDiff(size_t diff) const;
  HeapWord* nextWord(HeapWord* addr) {
    return offsetToHeapWord(heapWordToOffset(addr) + 1);
  }

  // debugging
  NOT_PRODUCT(bool covers(ReservedSpace rs) const;)
};

class CMBitMap : public CMBitMapRO {

 public:
  // constructor
120 121 122 123 124
  CMBitMap(int shifter) :
    CMBitMapRO(shifter) {}

  // Allocates the back store for the marking bitmap
  bool allocate(ReservedSpace heap_rs);
125 126 127 128 129

  // write marks
  void mark(HeapWord* addr) {
    assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
           "outside underlying space?");
130
    _bm.set_bit(heapWordToOffset(addr));
131 132 133 134
  }
  void clear(HeapWord* addr) {
    assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
           "outside underlying space?");
135
    _bm.clear_bit(heapWordToOffset(addr));
136 137 138 139
  }
  bool parMark(HeapWord* addr) {
    assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
           "outside underlying space?");
140
    return _bm.par_set_bit(heapWordToOffset(addr));
141 142 143 144
  }
  bool parClear(HeapWord* addr) {
    assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),
           "outside underlying space?");
145
    return _bm.par_clear_bit(heapWordToOffset(addr));
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  }
  void markRange(MemRegion mr);
  void clearAll();
  void clearRange(MemRegion mr);

  // Starting at the bit corresponding to "addr" (inclusive), find the next
  // "1" bit, if any.  This bit starts some run of consecutive "1"'s; find
  // the end of this run (stopping at "end_addr").  Return the MemRegion
  // covering from the start of the region corresponding to the first bit
  // of the run to the end of the region corresponding to the last bit of
  // the run.  If there is no "1" bit at or after "addr", return an empty
  // MemRegion.
  MemRegion getAndClearMarkedRegion(HeapWord* addr, HeapWord* end_addr);
};

161
// Represents a marking stack used by ConcurrentMarking in the G1 collector.
162
class CMMarkStack VALUE_OBJ_CLASS_SPEC {
163
  VirtualSpace _virtual_space;   // Underlying backing store for actual stack
164
  ConcurrentMark* _cm;
165
  oop*   _base;        // bottom of stack
166 167 168 169
  jint _index;       // one more than last occupied index
  jint _capacity;    // max #elements
  jint _saved_index; // value of _index saved at start of GC
  NOT_PRODUCT(jint _max_depth;)   // max depth plumbed during run
170

171 172
  bool  _overflow;
  bool  _should_expand;
173 174 175 176 177 178 179
  DEBUG_ONLY(bool _drain_in_progress;)
  DEBUG_ONLY(bool _drain_in_progress_yields;)

 public:
  CMMarkStack(ConcurrentMark* cm);
  ~CMMarkStack();

180 181 182 183 184 185 186
#ifndef PRODUCT
  jint max_depth() const {
    return _max_depth;
  }
#endif

  bool allocate(size_t capacity);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

  oop pop() {
    if (!isEmpty()) {
      return _base[--_index] ;
    }
    return NULL;
  }

  // If overflow happens, don't do the push, and record the overflow.
  // *Requires* that "ptr" is already marked.
  void push(oop ptr) {
    if (isFull()) {
      // Record overflow.
      _overflow = true;
      return;
    } else {
      _base[_index++] = ptr;
      NOT_PRODUCT(_max_depth = MAX2(_max_depth, _index));
    }
  }
  // Non-block impl.  Note: concurrency is allowed only with other
  // "par_push" operations, not with "pop" or "drain".  We would need
  // parallel versions of them if such concurrency was desired.
  void par_push(oop ptr);

  // Pushes the first "n" elements of "ptr_arr" on the stack.
  // Non-block impl.  Note: concurrency is allowed only with other
  // "par_adjoin_arr" or "push" operations, not with "pop" or "drain".
  void par_adjoin_arr(oop* ptr_arr, int n);

  // Pushes the first "n" elements of "ptr_arr" on the stack.
  // Locking impl: concurrency is allowed only with
  // "par_push_arr" and/or "par_pop_arr" operations, which use the same
  // locking strategy.
  void par_push_arr(oop* ptr_arr, int n);

  // If returns false, the array was empty.  Otherwise, removes up to "max"
  // elements from the stack, and transfers them to "ptr_arr" in an
  // unspecified order.  The actual number transferred is given in "n" ("n
  // == 0" is deliberately redundant with the return value.)  Locking impl:
  // concurrency is allowed only with "par_push_arr" and/or "par_pop_arr"
  // operations, which use the same locking strategy.
  bool par_pop_arr(oop* ptr_arr, int max, int* n);

  // Drain the mark stack, applying the given closure to all fields of
  // objects on the stack.  (That is, continue until the stack is empty,
  // even if closure applications add entries to the stack.)  The "bm"
  // argument, if non-null, may be used to verify that only marked objects
  // are on the mark stack.  If "yield_after" is "true", then the
  // concurrent marker performing the drain offers to yield after
  // processing each object.  If a yield occurs, stops the drain operation
  // and returns false.  Otherwise, returns true.
  template<class OopClosureClass>
  bool drain(OopClosureClass* cl, CMBitMap* bm, bool yield_after = false);

  bool isEmpty()    { return _index == 0; }
  bool isFull()     { return _index == _capacity; }
244
  int  maxElems()   { return _capacity; }
245 246 247 248

  bool overflow() { return _overflow; }
  void clear_overflow() { _overflow = false; }

249 250 251 252 253 254
  bool should_expand() const { return _should_expand; }
  void set_should_expand();

  // Expand the stack, typically in response to an overflow condition
  void expand();

255 256 257 258
  int  size() { return _index; }

  void setEmpty()   { _index = 0; clear_overflow(); }

259 260 261 262 263 264
  // Record the current index.
  void note_start_of_gc();

  // Make sure that we have not added any entries to the stack during GC.
  void note_end_of_gc();

265 266 267 268 269
  // iterate over the oops in the mark stack, up to the bound recorded via
  // the call above.
  void oops_do(OopClosure* f);
};

270 271 272 273 274 275 276 277 278 279 280 281 282
class ForceOverflowSettings VALUE_OBJ_CLASS_SPEC {
private:
#ifndef PRODUCT
  uintx _num_remaining;
  bool _force;
#endif // !defined(PRODUCT)

public:
  void init() PRODUCT_RETURN;
  void update() PRODUCT_RETURN;
  bool should_force() PRODUCT_RETURN_( return false; );
};

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
// this will enable a variety of different statistics per GC task
#define _MARKING_STATS_       0
// this will enable the higher verbose levels
#define _MARKING_VERBOSE_     0

#if _MARKING_STATS_
#define statsOnly(statement)  \
do {                          \
  statement ;                 \
} while (0)
#else // _MARKING_STATS_
#define statsOnly(statement)  \
do {                          \
} while (0)
#endif // _MARKING_STATS_

typedef enum {
  no_verbose  = 0,   // verbose turned off
  stats_verbose,     // only prints stats at the end of marking
  low_verbose,       // low verbose, mostly per region and per major event
  medium_verbose,    // a bit more detailed than low
  high_verbose       // per object verbose
} CMVerboseLevel;

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
class YoungList;

// Root Regions are regions that are not empty at the beginning of a
// marking cycle and which we might collect during an evacuation pause
// while the cycle is active. Given that, during evacuation pauses, we
// do not copy objects that are explicitly marked, what we have to do
// for the root regions is to scan them and mark all objects reachable
// from them. According to the SATB assumptions, we only need to visit
// each object once during marking. So, as long as we finish this scan
// before the next evacuation pause, we can copy the objects from the
// root regions without having to mark them or do anything else to them.
//
// Currently, we only support root region scanning once (at the start
// of the marking cycle) and the root regions are all the survivor
// regions populated during the initial-mark pause.
class CMRootRegions VALUE_OBJ_CLASS_SPEC {
private:
  YoungList*           _young_list;
  ConcurrentMark*      _cm;

  volatile bool        _scan_in_progress;
  volatile bool        _should_abort;
  HeapRegion* volatile _next_survivor;

public:
  CMRootRegions();
  // We actually do most of the initialization in this method.
  void init(G1CollectedHeap* g1h, ConcurrentMark* cm);

  // Reset the claiming / scanning of the root regions.
  void prepare_for_scan();

  // Forces get_next() to return NULL so that the iteration aborts early.
  void abort() { _should_abort = true; }

  // Return true if the CM thread are actively scanning root regions,
  // false otherwise.
  bool scan_in_progress() { return _scan_in_progress; }

  // Claim the next root region to scan atomically, or return NULL if
  // all have been claimed.
  HeapRegion* claim_next();

  // Flag that we're done with root region scanning and notify anyone
  // who's waiting on it. If aborted is false, assume that all regions
  // have been claimed.
  void scan_finished();

  // If CM threads are still scanning root regions, wait until they
  // are done. Return true if we had to wait, false otherwise.
  bool wait_until_scan_finished();
};
359 360 361

class ConcurrentMarkThread;

Z
zgu 已提交
362
class ConcurrentMark: public CHeapObj<mtGC> {
363
  friend class CMMarkStack;
364 365 366 367 368 369 370 371
  friend class ConcurrentMarkThread;
  friend class CMTask;
  friend class CMBitMapClosure;
  friend class CMGlobalObjectClosure;
  friend class CMRemarkTask;
  friend class CMConcurrentMarkingTask;
  friend class G1ParNoteEndTask;
  friend class CalcLiveObjectsClosure;
372 373
  friend class G1CMRefProcTaskProxy;
  friend class G1CMRefProcTaskExecutor;
374 375
  friend class G1CMKeepAliveAndDrainClosure;
  friend class G1CMDrainMarkingStackClosure;
376 377 378 379

protected:
  ConcurrentMarkThread* _cmThread;   // the thread doing the work
  G1CollectedHeap*      _g1h;        // the heap.
380
  uint                  _parallel_marking_threads; // the number of marking
381
                                                   // threads we're use
382
  uint                  _max_parallel_marking_threads; // max number of marking
383
                                                   // threads we'll ever use
384 385 386 387 388 389 390 391 392 393
  double                _sleep_factor; // how much we have to sleep, with
                                       // respect to the work we just did, to
                                       // meet the marking overhead goal
  double                _marking_task_overhead; // marking target overhead for
                                                // a single task

  // same as the two above, but for the cleanup task
  double                _cleanup_sleep_factor;
  double                _cleanup_task_overhead;

394
  FreeRegionList        _cleanup_list;
395

396
  // Concurrent marking support structures
397 398 399 400 401 402 403 404 405 406 407 408
  CMBitMap                _markBitMap1;
  CMBitMap                _markBitMap2;
  CMBitMapRO*             _prevMarkBitMap; // completed mark bitmap
  CMBitMap*               _nextMarkBitMap; // under-construction mark bitmap

  BitMap                  _region_bm;
  BitMap                  _card_bm;

  // Heap bounds
  HeapWord*               _heap_start;
  HeapWord*               _heap_end;

409 410 411
  // Root region tracking and claiming.
  CMRootRegions           _root_regions;

412 413 414 415 416 417 418
  // For gray objects
  CMMarkStack             _markStack; // Grey objects behind global finger.
  HeapWord* volatile      _finger;  // the global finger, region aligned,
                                    // always points to the end of the
                                    // last claimed region

  // marking tasks
419
  uint                    _max_worker_id;// maximum worker id
420
  uint                    _active_tasks; // task num currently active
421
  CMTask**                _tasks;        // task queue array (max_worker_id len)
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  CMTaskQueueSet*         _task_queues;  // task queue set
  ParallelTaskTerminator  _terminator;   // for termination

  // Two sync barriers that are used to synchronise tasks when an
  // overflow occurs. The algorithm is the following. All tasks enter
  // the first one to ensure that they have all stopped manipulating
  // the global data structures. After they exit it, they re-initialise
  // their data structures and task 0 re-initialises the global data
  // structures. Then, they enter the second sync barrier. This
  // ensure, that no task starts doing work before all data
  // structures (local and global) have been re-initialised. When they
  // exit it, they are free to start working again.
  WorkGangBarrierSync     _first_overflow_barrier_sync;
  WorkGangBarrierSync     _second_overflow_barrier_sync;

  // this is set by any task, when an overflow on the global data
  // structures is detected.
  volatile bool           _has_overflown;
  // true: marking is concurrent, false: we're in remark
  volatile bool           _concurrent;
  // set at the end of a Full GC so that marking aborts
  volatile bool           _has_aborted;
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
  // used when remark aborts due to an overflow to indicate that
  // another concurrent marking phase should start
  volatile bool           _restart_for_overflow;

  // This is true from the very start of concurrent marking until the
  // point when all the tasks complete their work. It is really used
  // to determine the points between the end of concurrent marking and
  // time of remark.
  volatile bool           _concurrent_marking_in_progress;

  // verbose level
  CMVerboseLevel          _verbose_level;

  // All of these times are in ms.
  NumberSeq _init_times;
  NumberSeq _remark_times;
  NumberSeq   _remark_mark_times;
  NumberSeq   _remark_weak_ref_times;
  NumberSeq _cleanup_times;
  double    _total_counting_time;
  double    _total_rs_scrub_time;

  double*   _accum_task_vtime;   // accumulated task vtime

469
  FlexibleWorkGang* _parallel_workers;
470

471 472 473
  ForceOverflowSettings _force_overflow_conc;
  ForceOverflowSettings _force_overflow_stw;

474 475 476 477 478 479 480 481
  void weakRefsWork(bool clear_all_soft_refs);

  void swapMarkBitMaps();

  // It resets the global marking data structures, as well as the
  // task local ones; should be called during initial mark.
  void reset();

482 483 484 485
  // Resets all the marking data structures. Called when we have to restart
  // marking or when marking completes (via set_non_marking_state below).
  void reset_marking_state(bool clear_overflow = true);

486 487 488 489
  // We do this after we're done with marking so that the marking data
  // structures are initialised to a sensible and predictable state.
  void set_non_marking_state();

490 491 492 493
  // It should be called to indicate which phase we're in (concurrent
  // mark or remark) and how many threads are currently active.
  void set_phase(uint active_tasks, bool concurrent);

494 495 496
  // prints all gathered CM-related statistics
  void print_stats();

497 498 499 500
  bool cleanup_list_is_empty() {
    return _cleanup_list.is_empty();
  }

501
  // accessor methods
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
  uint parallel_marking_threads() const     { return _parallel_marking_threads; }
  uint max_parallel_marking_threads() const { return _max_parallel_marking_threads;}
  double sleep_factor()                     { return _sleep_factor; }
  double marking_task_overhead()            { return _marking_task_overhead;}
  double cleanup_sleep_factor()             { return _cleanup_sleep_factor; }
  double cleanup_task_overhead()            { return _cleanup_task_overhead;}

  bool use_parallel_marking_threads() const {
    assert(parallel_marking_threads() <=
           max_parallel_marking_threads(), "sanity");
    assert((_parallel_workers == NULL && parallel_marking_threads() == 0) ||
           parallel_marking_threads() > 0,
           "parallel workers not set up correctly");
    return _parallel_workers != NULL;
  }

  HeapWord*               finger()          { return _finger;   }
  bool                    concurrent()      { return _concurrent; }
  uint                    active_tasks()    { return _active_tasks; }
  ParallelTaskTerminator* terminator()      { return &_terminator; }
522 523

  // It claims the next available region to be scanned by a marking
524 525
  // task/thread. It might return NULL if the next region is empty or
  // we have run out of regions. In the latter case, out_of_regions()
526
  // determines whether we've really run out of regions or the task
527
  // should call claim_region() again. This might seem a bit
528 529 530 531 532 533 534 535 536
  // awkward. Originally, the code was written so that claim_region()
  // either successfully returned with a non-empty region or there
  // were no more regions to be claimed. The problem with this was
  // that, in certain circumstances, it iterated over large chunks of
  // the heap finding only empty regions and, while it was working, it
  // was preventing the calling task to call its regular clock
  // method. So, this way, each task will spend very little time in
  // claim_region() and is allowed to call the regular clock method
  // frequently.
537
  HeapRegion* claim_region(uint worker_id);
538 539 540 541 542 543

  // It determines whether we've run out of regions to scan.
  bool        out_of_regions() { return _finger == _heap_end; }

  // Returns the task with the given id
  CMTask* task(int id) {
544 545
    assert(0 <= id && id < (int) _active_tasks,
           "task id not within active bounds");
546 547 548 549 550
    return _tasks[id];
  }

  // Returns the task queue with the given id
  CMTaskQueue* task_queue(int id) {
551 552
    assert(0 <= id && id < (int) _active_tasks,
           "task queue id not within active bounds");
553 554 555 556 557 558 559
    return (CMTaskQueue*) _task_queues->queue(id);
  }

  // Returns the task queue set
  CMTaskQueueSet* task_queues()  { return _task_queues; }

  // Access / manipulation of the overflow flag which is set to
560
  // indicate that the global stack has overflown
561 562 563
  bool has_overflown()           { return _has_overflown; }
  void set_has_overflown()       { _has_overflown = true; }
  void clear_has_overflown()     { _has_overflown = false; }
564
  bool restart_for_overflow()    { return _restart_for_overflow; }
565 566 567 568

  bool has_aborted()             { return _has_aborted; }

  // Methods to enter the two overflow sync barriers
569 570
  void enter_first_sync_barrier(uint worker_id);
  void enter_second_sync_barrier(uint worker_id);
571

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
  ForceOverflowSettings* force_overflow_conc() {
    return &_force_overflow_conc;
  }

  ForceOverflowSettings* force_overflow_stw() {
    return &_force_overflow_stw;
  }

  ForceOverflowSettings* force_overflow() {
    if (concurrent()) {
      return force_overflow_conc();
    } else {
      return force_overflow_stw();
    }
  }

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
  // Live Data Counting data structures...
  // These data structures are initialized at the start of
  // marking. They are written to while marking is active.
  // They are aggregated during remark; the aggregated values
  // are then used to populate the _region_bm, _card_bm, and
  // the total live bytes, which are then subsequently updated
  // during cleanup.

  // An array of bitmaps (one bit map per task). Each bitmap
  // is used to record the cards spanned by the live objects
  // marked by that task/worker.
  BitMap*  _count_card_bitmaps;

  // Used to record the number of marked live bytes
  // (for each region, by worker thread).
  size_t** _count_marked_bytes;

  // Card index of the bottom of the G1 heap. Used for biasing indices into
  // the card bitmaps.
  intptr_t _heap_bottom_card_num;

609 610 611
  // Set to true when initialization is complete
  bool _completed_initialization;

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
public:
  // Manipulation of the global mark stack.
  // Notice that the first mark_stack_push is CAS-based, whereas the
  // two below are Mutex-based. This is OK since the first one is only
  // called during evacuation pauses and doesn't compete with the
  // other two (which are called by the marking tasks during
  // concurrent marking or remark).
  bool mark_stack_push(oop p) {
    _markStack.par_push(p);
    if (_markStack.overflow()) {
      set_has_overflown();
      return false;
    }
    return true;
  }
  bool mark_stack_push(oop* arr, int n) {
    _markStack.par_push_arr(arr, n);
    if (_markStack.overflow()) {
      set_has_overflown();
      return false;
    }
    return true;
  }
  void mark_stack_pop(oop* arr, int max, int* n) {
    _markStack.par_pop_arr(arr, max, n);
  }
638
  size_t mark_stack_size()                { return _markStack.size(); }
639
  size_t partial_mark_stack_size_target() { return _markStack.maxElems()/3; }
640 641
  bool mark_stack_overflow()              { return _markStack.overflow(); }
  bool mark_stack_empty()                 { return _markStack.isEmpty(); }
642

643 644
  CMRootRegions* root_regions() { return &_root_regions; }

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
  bool concurrent_marking_in_progress() {
    return _concurrent_marking_in_progress;
  }
  void set_concurrent_marking_in_progress() {
    _concurrent_marking_in_progress = true;
  }
  void clear_concurrent_marking_in_progress() {
    _concurrent_marking_in_progress = false;
  }

  void update_accum_task_vtime(int i, double vtime) {
    _accum_task_vtime[i] += vtime;
  }

  double all_task_accum_vtime() {
    double ret = 0.0;
661
    for (uint i = 0; i < _max_worker_id; ++i)
662 663 664 665 666
      ret += _accum_task_vtime[i];
    return ret;
  }

  // Attempts to steal an object from the task queues of other tasks
667 668
  bool try_stealing(uint worker_id, int* hash_seed, oop& obj) {
    return _task_queues->steal(worker_id, hash_seed, obj);
669 670
  }

671
  ConcurrentMark(G1CollectedHeap* g1h, ReservedSpace heap_rs);
672
  ~ConcurrentMark();
673

674 675 676 677 678
  ConcurrentMarkThread* cmThread() { return _cmThread; }

  CMBitMapRO* prevMarkBitMap() const { return _prevMarkBitMap; }
  CMBitMap*   nextMarkBitMap() const { return _nextMarkBitMap; }

679 680 681
  // Returns the number of GC threads to be used in a concurrent
  // phase based on the number of GC threads being used in a STW
  // phase.
682
  uint scale_parallel_threads(uint n_par_threads);
683 684

  // Calculates the number of GC threads to be used in a concurrent phase.
685
  uint calc_parallel_marking_threads();
686

687 688 689 690
  // The following three are interaction between CM and
  // G1CollectedHeap

  // This notifies CM that a root during initial-mark needs to be
691 692 693 694 695 696 697 698 699 700 701
  // grayed. It is MT-safe. word_size is the size of the object in
  // words. It is passed explicitly as sometimes we cannot calculate
  // it from the given object because it might be in an inconsistent
  // state (e.g., in to-space and being copied). So the caller is
  // responsible for dealing with this issue (e.g., get the size from
  // the from-space image when the to-space image might be
  // inconsistent) and always passing the size. hr is the region that
  // contains the object and it's passed optionally from callers who
  // might already have it (no point in recalculating it).
  inline void grayRoot(oop obj, size_t word_size,
                       uint worker_id, HeapRegion* hr = NULL);
702

703 704 705 706
  // It iterates over the heap and for each object it comes across it
  // will dump the contents of its reference fields, as well as
  // liveness information for the object and its referents. The dump
  // will be written to a file with the following name:
707 708 709 710 711 712 713 714
  // G1PrintReachableBaseFile + "." + str.
  // vo decides whether the prev (vo == UsePrevMarking), the next
  // (vo == UseNextMarking) marking information, or the mark word
  // (vo == UseMarkWord) will be used to determine the liveness of
  // each object / referent.
  // If all is true, all objects in the heap will be dumped, otherwise
  // only the live ones. In the dump the following symbols / breviations
  // are used:
715 716 717 718 719 720 721
  //   M : an explicitly live object (its bitmap bit is set)
  //   > : an implicitly live object (over tams)
  //   O : an object outside the G1 heap (typically: in the perm gen)
  //   NOT : a reference field whose referent is not live
  //   AND MARKED : indicates that an object is both explicitly and
  //   implicitly live (it should be one or the other, not both)
  void print_reachable(const char* str,
722
                       VerifyOption vo, bool all) PRODUCT_RETURN;
723 724 725 726 727 728 729 730 731 732 733 734 735

  // Clear the next marking bitmap (will be called concurrently).
  void clearNextBitmap();

  // These two do the work that needs to be done before and after the
  // initial root checkpoint. Since this checkpoint can be done at two
  // different points (i.e. an explicit pause or piggy-backed on a
  // young collection), then it's nice to be able to easily share the
  // pre/post code. It might be the case that we can put everything in
  // the post method. TP
  void checkpointRootsInitialPre();
  void checkpointRootsInitialPost();

736 737 738 739 740 741 742
  // Scan all the root regions and mark everything reachable from
  // them.
  void scanRootRegions();

  // Scan a single root region and mark everything reachable from it.
  void scanRootRegion(HeapRegion* hr, uint worker_id);

743 744 745 746 747 748 749 750 751 752
  // Do concurrent phase of marking, to a tentative transitive closure.
  void markFromRoots();

  void checkpointRootsFinal(bool clear_all_soft_refs);
  void checkpointRootsFinalWork();
  void cleanup();
  void completeCleanup();

  // Mark in the previous bitmap.  NB: this is usually read-only, so use
  // this carefully!
753
  inline void markPrev(oop p);
754

755 756 757 758 759 760 761 762 763 764 765
  // Clears marks for all objects in the given range, for the prev,
  // next, or both bitmaps.  NB: the previous bitmap is usually
  // read-only, so use this carefully!
  void clearRangePrevBitmap(MemRegion mr);
  void clearRangeNextBitmap(MemRegion mr);
  void clearRangeBothBitmaps(MemRegion mr);

  // Notify data structures that a GC has started.
  void note_start_of_gc() {
    _markStack.note_start_of_gc();
  }
766

767 768 769
  // Notify data structures that a GC is finished.
  void note_end_of_gc() {
    _markStack.note_end_of_gc();
770
  }
771 772 773 774 775 776 777 778 779 780 781

  // Verify that there are no CSet oops on the stacks (taskqueues /
  // global mark stack), enqueued SATB buffers, per-thread SATB
  // buffers, and fingers (global / per-task). The boolean parameters
  // decide which of the above data structures to verify. If marking
  // is not in progress, it's a no-op.
  void verify_no_cset_oops(bool verify_stacks,
                           bool verify_enqueued_buffers,
                           bool verify_thread_buffers,
                           bool verify_fingers) PRODUCT_RETURN;

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
  // It is called at the end of an evacuation pause during marking so
  // that CM is notified of where the new end of the heap is. It
  // doesn't do anything if concurrent_marking_in_progress() is false,
  // unless the force parameter is true.
  void update_g1_committed(bool force = false);

  bool isMarked(oop p) const {
    assert(p != NULL && p->is_oop(), "expected an oop");
    HeapWord* addr = (HeapWord*)p;
    assert(addr >= _nextMarkBitMap->startWord() ||
           addr < _nextMarkBitMap->endWord(), "in a region");

    return _nextMarkBitMap->isMarked(addr);
  }

  inline bool not_yet_marked(oop p) const;

  // XXX Debug code
  bool containing_card_is_marked(void* p);
  bool containing_cards_are_marked(void* start, void* last);

  bool isPrevMarked(oop p) const {
    assert(p != NULL && p->is_oop(), "expected an oop");
    HeapWord* addr = (HeapWord*)p;
    assert(addr >= _prevMarkBitMap->startWord() ||
           addr < _prevMarkBitMap->endWord(), "in a region");

    return _prevMarkBitMap->isMarked(addr);
  }

812
  inline bool do_yield_check(uint worker_i = 0);
813 814 815 816 817 818 819 820 821 822
  inline bool should_yield();

  // Called to abort the marking cycle after a Full GC takes palce.
  void abort();

  // This prints the global/local fingers. It is used for debugging.
  NOT_PRODUCT(void print_finger();)

  void print_summary_info();

T
tonyp 已提交
823 824
  void print_worker_threads_on(outputStream* st) const;

825 826 827
  // The following indicate whether a given verbose level has been
  // set. Notice that anything above stats is conditional to
  // _MARKING_VERBOSE_ having been set to 1
828 829 830 831 832 833 834 835 836 837 838 839
  bool verbose_stats() {
    return _verbose_level >= stats_verbose;
  }
  bool verbose_low() {
    return _MARKING_VERBOSE_ && _verbose_level >= low_verbose;
  }
  bool verbose_medium() {
    return _MARKING_VERBOSE_ && _verbose_level >= medium_verbose;
  }
  bool verbose_high() {
    return _MARKING_VERBOSE_ && _verbose_level >= high_verbose;
  }
840

841 842 843 844 845 846 847 848
  // Liveness counting

  // Utility routine to set an exclusive range of cards on the given
  // card liveness bitmap
  inline void set_card_bitmap_range(BitMap* card_bm,
                                    BitMap::idx_t start_idx,
                                    BitMap::idx_t end_idx,
                                    bool is_par);
849 850 851 852 853 854 855 856 857

  // Returns the card number of the bottom of the G1 heap.
  // Used in biasing indices into accounting card bitmaps.
  intptr_t heap_bottom_card_num() const {
    return _heap_bottom_card_num;
  }

  // Returns the card bitmap for a given task or worker id.
  BitMap* count_card_bitmap_for(uint worker_id) {
858
    assert(0 <= worker_id && worker_id < _max_worker_id, "oob");
859 860 861 862 863 864 865 866 867
    assert(_count_card_bitmaps != NULL, "uninitialized");
    BitMap* task_card_bm = &_count_card_bitmaps[worker_id];
    assert(task_card_bm->size() == _card_bm.size(), "size mismatch");
    return task_card_bm;
  }

  // Returns the array containing the marked bytes for each region,
  // for the given worker or task id.
  size_t* count_marked_bytes_array_for(uint worker_id) {
868
    assert(0 <= worker_id && worker_id < _max_worker_id, "oob");
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
    assert(_count_marked_bytes != NULL, "uninitialized");
    size_t* marked_bytes_array = _count_marked_bytes[worker_id];
    assert(marked_bytes_array != NULL, "uninitialized");
    return marked_bytes_array;
  }

  // Returns the index in the liveness accounting card table bitmap
  // for the given address
  inline BitMap::idx_t card_bitmap_index_for(HeapWord* addr);

  // Counts the size of the given memory region in the the given
  // marked_bytes array slot for the given HeapRegion.
  // Sets the bits in the given card bitmap that are associated with the
  // cards that are spanned by the memory region.
  inline void count_region(MemRegion mr, HeapRegion* hr,
                           size_t* marked_bytes_array,
                           BitMap* task_card_bm);

887 888 889 890
  // Counts the given memory region in the task/worker counting
  // data structures for the given worker id.
  inline void count_region(MemRegion mr, HeapRegion* hr, uint worker_id);

891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
  // Counts the given memory region in the task/worker counting
  // data structures for the given worker id.
  inline void count_region(MemRegion mr, uint worker_id);

  // Counts the given object in the given task/worker counting
  // data structures.
  inline void count_object(oop obj, HeapRegion* hr,
                           size_t* marked_bytes_array,
                           BitMap* task_card_bm);

  // Counts the given object in the task/worker counting data
  // structures for the given worker id.
  inline void count_object(oop obj, HeapRegion* hr, uint worker_id);

  // Attempts to mark the given object and, if successful, counts
  // the object in the given task/worker counting structures.
  inline bool par_mark_and_count(oop obj, HeapRegion* hr,
                                 size_t* marked_bytes_array,
                                 BitMap* task_card_bm);

911 912 913 914 915 916
  // Attempts to mark the given object and, if successful, counts
  // the object in the task/worker counting structures for the
  // given worker id.
  inline bool par_mark_and_count(oop obj, size_t word_size,
                                 HeapRegion* hr, uint worker_id);

917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
  // Attempts to mark the given object and, if successful, counts
  // the object in the task/worker counting structures for the
  // given worker id.
  inline bool par_mark_and_count(oop obj, HeapRegion* hr, uint worker_id);

  // Similar to the above routine but we don't know the heap region that
  // contains the object to be marked/counted, which this routine looks up.
  inline bool par_mark_and_count(oop obj, uint worker_id);

  // Similar to the above routine but there are times when we cannot
  // safely calculate the size of obj due to races and we, therefore,
  // pass the size in as a parameter. It is the caller's reponsibility
  // to ensure that the size passed in for obj is valid.
  inline bool par_mark_and_count(oop obj, size_t word_size, uint worker_id);

  // Unconditionally mark the given object, and unconditinally count
  // the object in the counting structures for worker id 0.
  // Should *not* be called from parallel code.
  inline bool mark_and_count(oop obj, HeapRegion* hr);

  // Similar to the above routine but we don't know the heap region that
  // contains the object to be marked/counted, which this routine looks up.
  // Should *not* be called from parallel code.
  inline bool mark_and_count(oop obj);

942 943 944 945 946
  // Returns true if initialization was successfully completed.
  bool completed_initialization() const {
    return _completed_initialization;
  }

947 948 949 950 951 952 953 954 955 956 957 958 959
protected:
  // Clear all the per-task bitmaps and arrays used to store the
  // counting data.
  void clear_all_count_data();

  // Aggregates the counting data for each worker/task
  // that was constructed while marking. Also sets
  // the amount of marked bytes for each region and
  // the top at concurrent mark count.
  void aggregate_count_data();

  // Verification routine
  void verify_count_data();
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
};

// A class representing a marking task.
class CMTask : public TerminatorTerminator {
private:
  enum PrivateConstants {
    // the regular clock call is called once the scanned words reaches
    // this limit
    words_scanned_period          = 12*1024,
    // the regular clock call is called once the number of visited
    // references reaches this limit
    refs_reached_period           = 384,
    // initial value for the hash seed, used in the work stealing code
    init_hash_seed                = 17,
    // how many entries will be transferred between global stack and
    // local queues
    global_stack_transfer_size    = 16
  };

979
  uint                        _worker_id;
980 981 982 983 984
  G1CollectedHeap*            _g1h;
  ConcurrentMark*             _cm;
  CMBitMap*                   _nextMarkBitMap;
  // the task queue of this task
  CMTaskQueue*                _task_queue;
985
private:
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
  // the task queue set---needed for stealing
  CMTaskQueueSet*             _task_queues;
  // indicates whether the task has been claimed---this is only  for
  // debugging purposes
  bool                        _claimed;

  // number of calls to this task
  int                         _calls;

  // when the virtual timer reaches this time, the marking step should
  // exit
  double                      _time_target_ms;
  // the start time of the current marking step
  double                      _start_time_ms;

  // the oop closure used for iterations over oops
1002
  G1CMOopClosure*             _cm_oop_closure;
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

  // the region this task is scanning, NULL if we're not scanning any
  HeapRegion*                 _curr_region;
  // the local finger of this task, NULL if we're not scanning a region
  HeapWord*                   _finger;
  // limit of the region this task is scanning, NULL if we're not scanning one
  HeapWord*                   _region_limit;

  // the number of words this task has scanned
  size_t                      _words_scanned;
  // When _words_scanned reaches this limit, the regular clock is
  // called. Notice that this might be decreased under certain
  // circumstances (i.e. when we believe that we did an expensive
  // operation).
  size_t                      _words_scanned_limit;
  // the initial value of _words_scanned_limit (i.e. what it was
  // before it was decreased).
  size_t                      _real_words_scanned_limit;

  // the number of references this task has visited
  size_t                      _refs_reached;
  // When _refs_reached reaches this limit, the regular clock is
  // called. Notice this this might be decreased under certain
  // circumstances (i.e. when we believe that we did an expensive
  // operation).
  size_t                      _refs_reached_limit;
  // the initial value of _refs_reached_limit (i.e. what it was before
  // it was decreased).
  size_t                      _real_refs_reached_limit;

  // used by the work stealing stuff
  int                         _hash_seed;
  // if this is true, then the task has aborted for some reason
  bool                        _has_aborted;
  // set when the task aborts because it has met its time quota
1038
  bool                        _has_timed_out;
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
  // true when we're draining SATB buffers; this avoids the task
  // aborting due to SATB buffers being available (as we're already
  // dealing with them)
  bool                        _draining_satb_buffers;

  // number sequence of past step times
  NumberSeq                   _step_times_ms;
  // elapsed time of this task
  double                      _elapsed_time_ms;
  // termination time of this task
  double                      _termination_time_ms;
  // when this task got into the termination protocol
  double                      _termination_start_time_ms;

  // true when the task is during a concurrent phase, false when it is
  // in the remark phase (so, in the latter case, we do not have to
  // check all the things that we have to check during the concurrent
  // phase, i.e. SATB buffer availability...)
  bool                        _concurrent;

  TruncatedSeq                _marking_step_diffs_ms;

1061 1062 1063 1064 1065 1066
  // Counting data structures. Embedding the task's marked_bytes_array
  // and card bitmap into the actual task saves having to go through
  // the ConcurrentMark object.
  size_t*                     _marked_bytes_array;
  BitMap*                     _card_bm;

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
  // LOTS of statistics related with this task
#if _MARKING_STATS_
  NumberSeq                   _all_clock_intervals_ms;
  double                      _interval_start_time_ms;

  int                         _aborted;
  int                         _aborted_overflow;
  int                         _aborted_cm_aborted;
  int                         _aborted_yield;
  int                         _aborted_timed_out;
  int                         _aborted_satb;
  int                         _aborted_termination;

  int                         _steal_attempts;
  int                         _steals;

  int                         _clock_due_to_marking;
  int                         _clock_due_to_scanning;

  int                         _local_pushes;
  int                         _local_pops;
  int                         _local_max_size;
  int                         _objs_scanned;

  int                         _global_pushes;
  int                         _global_pops;
  int                         _global_max_size;

  int                         _global_transfers_to;
  int                         _global_transfers_from;

  int                         _regions_claimed;
  int                         _objs_found_on_bitmap;

  int                         _satb_buffers_processed;
#endif // _MARKING_STATS_

  // it updates the local fields after this task has claimed
  // a new region to scan
  void setup_for_region(HeapRegion* hr);
  // it brings up-to-date the limit of the region
  void update_region_limit();

  // called when either the words scanned or the refs visited limit
  // has been reached
  void reached_limit();
  // recalculates the words scanned and refs visited limits
  void recalculate_limits();
  // decreases the words scanned and refs visited limits when we reach
  // an expensive operation
  void decrease_limits();
  // it checks whether the words scanned or refs visited reached their
  // respective limit and calls reached_limit() if they have
  void check_limits() {
    if (_words_scanned >= _words_scanned_limit ||
1122
        _refs_reached >= _refs_reached_limit) {
1123
      reached_limit();
1124
    }
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
  }
  // this is supposed to be called regularly during a marking step as
  // it checks a bunch of conditions that might cause the marking step
  // to abort
  void regular_clock_call();
  bool concurrent() { return _concurrent; }

public:
  // It resets the task; it should be called right at the beginning of
  // a marking phase.
  void reset(CMBitMap* _nextMarkBitMap);
  // it clears all the fields that correspond to a claimed region.
  void clear_region_fields();

  void set_concurrent(bool concurrent) { _concurrent = concurrent; }

  // The main method of this class which performs a marking step
  // trying not to exceed the given duration. However, it might exit
  // prematurely, according to some conditions (i.e. SATB buffers are
  // available for processing).
1145
  void do_marking_step(double target_ms, bool do_stealing, bool do_termination);
1146 1147 1148 1149 1150 1151 1152 1153 1154

  // These two calls start and stop the timer
  void record_start_time() {
    _elapsed_time_ms = os::elapsedTime() * 1000.0;
  }
  void record_end_time() {
    _elapsed_time_ms = os::elapsedTime() * 1000.0 - _elapsed_time_ms;
  }

1155 1156
  // returns the worker ID associated with this task.
  uint worker_id() { return _worker_id; }
1157 1158 1159 1160 1161

  // From TerminatorTerminator. It determines whether this task should
  // exit the termination protocol after it's entered it.
  virtual bool should_exit_termination();

1162 1163 1164 1165 1166
  // Resets the local region fields after a task has finished scanning a
  // region; or when they have become stale as a result of the region
  // being evacuated.
  void giveup_current_region();

1167 1168 1169 1170 1171
  HeapWord* finger()            { return _finger; }

  bool has_aborted()            { return _has_aborted; }
  void set_has_aborted()        { _has_aborted = true; }
  void clear_has_aborted()      { _has_aborted = false; }
1172 1173
  bool has_timed_out()          { return _has_timed_out; }
  bool claimed()                { return _claimed; }
1174

1175
  void set_cm_oop_closure(G1CMOopClosure* cm_oop_closure);
1176 1177 1178

  // It grays the object by marking it and, if necessary, pushing it
  // on the local queue
1179
  inline void deal_with_reference(oop obj);
1180 1181

  // It scans an object and visits its children.
1182
  void scan_object(oop obj);
1183 1184

  // It pushes an object on the local queue.
1185
  inline void push(oop obj);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

  // These two move entries to/from the global stack.
  void move_entries_to_global_stack();
  void get_entries_from_global_stack();

  // It pops and scans objects from the local queue. If partially is
  // true, then it stops when the queue size is of a given limit. If
  // partially is false, then it stops when the queue is empty.
  void drain_local_queue(bool partially);
  // It moves entries from the global stack to the local queue and
  // drains the local queue. If partially is true, then it stops when
  // both the global stack and the local queue reach a given size. If
  // partially if false, it tries to empty them totally.
  void drain_global_stack(bool partially);
  // It keeps picking SATB buffers and processing them until no SATB
  // buffers are available.
  void drain_satb_buffers();
1203

1204 1205
  // moves the local finger to a new location
  inline void move_finger_to(HeapWord* new_finger) {
1206
    assert(new_finger >= _finger && new_finger < _region_limit, "invariant");
1207 1208 1209
    _finger = new_finger;
  }

1210
  CMTask(uint worker_id, ConcurrentMark *cm,
1211
         size_t* marked_bytes, BitMap* card_bm,
1212 1213 1214 1215 1216 1217 1218 1219 1220
         CMTaskQueue* task_queue, CMTaskQueueSet* task_queues);

  // it prints statistics associated with this task
  void print_stats();

#if _MARKING_STATS_
  void increase_objs_found_on_bitmap() { ++_objs_found_on_bitmap; }
#endif // _MARKING_STATS_
};
1221

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
// Class that's used to to print out per-region liveness
// information. It's currently used at the end of marking and also
// after we sort the old regions at the end of the cleanup operation.
class G1PrintRegionLivenessInfoClosure: public HeapRegionClosure {
private:
  outputStream* _out;

  // Accumulators for these values.
  size_t _total_used_bytes;
  size_t _total_capacity_bytes;
  size_t _total_prev_live_bytes;
  size_t _total_next_live_bytes;

  // These are set up when we come across a "stars humongous" region
  // (as this is where most of this information is stored, not in the
  // subsequent "continues humongous" regions). After that, for every
  // region in a given humongous region series we deduce the right
  // values for it by simply subtracting the appropriate amount from
  // these fields. All these values should reach 0 after we've visited
  // the last region in the series.
  size_t _hum_used_bytes;
  size_t _hum_capacity_bytes;
  size_t _hum_prev_live_bytes;
  size_t _hum_next_live_bytes;

  static double perc(size_t val, size_t total) {
    if (total == 0) {
      return 0.0;
    } else {
      return 100.0 * ((double) val / (double) total);
    }
  }

  static double bytes_to_mb(size_t val) {
    return (double) val / (double) M;
  }

  // See the .cpp file.
  size_t get_hum_bytes(size_t* hum_bytes);
  void get_hum_bytes(size_t* used_bytes, size_t* capacity_bytes,
                     size_t* prev_live_bytes, size_t* next_live_bytes);

public:
  // The header and footer are printed in the constructor and
  // destructor respectively.
  G1PrintRegionLivenessInfoClosure(outputStream* out, const char* phase_name);
  virtual bool doHeapRegion(HeapRegion* r);
  ~G1PrintRegionLivenessInfoClosure();
};

1272
#endif // SHARE_VM_GC_IMPLEMENTATION_G1_CONCURRENTMARK_HPP