Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/concurrentMark.hpp
38920 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_CONCURRENTMARK_HPP25#define SHARE_VM_GC_IMPLEMENTATION_G1_CONCURRENTMARK_HPP2627#include "classfile/javaClasses.hpp"28#include "gc_implementation/g1/g1ConcurrentMarkObjArrayProcessor.hpp"29#include "gc_implementation/g1/heapRegionSet.hpp"30#include "gc_implementation/g1/g1RegionToSpaceMapper.hpp"31#include "gc_implementation/shared/gcId.hpp"32#include "utilities/taskqueue.hpp"3334class G1CollectedHeap;35class CMBitMap;36class CMTask;37typedef GenericTaskQueue<oop, mtGC> CMTaskQueue;38typedef GenericTaskQueueSet<CMTaskQueue, mtGC> CMTaskQueueSet;3940// Closure used by CM during concurrent reference discovery41// and reference processing (during remarking) to determine42// if a particular object is alive. It is primarily used43// to determine if referents of discovered reference objects44// are alive. An instance is also embedded into the45// reference processor as the _is_alive_non_header field46class G1CMIsAliveClosure: public BoolObjectClosure {47G1CollectedHeap* _g1;48public:49G1CMIsAliveClosure(G1CollectedHeap* g1) : _g1(g1) { }5051bool do_object_b(oop obj);52};5354// A generic CM bit map. This is essentially a wrapper around the BitMap55// class, with one bit per (1<<_shifter) HeapWords.5657class CMBitMapRO VALUE_OBJ_CLASS_SPEC {58protected:59HeapWord* _bmStartWord; // base address of range covered by map60size_t _bmWordSize; // map size (in #HeapWords covered)61const int _shifter; // map to char or bit62BitMap _bm; // the bit map itself6364public:65// constructor66CMBitMapRO(int shifter);6768enum { do_yield = true };6970// inquiries71HeapWord* startWord() const { return _bmStartWord; }72size_t sizeInWords() const { return _bmWordSize; }73// the following is one past the last word in space74HeapWord* endWord() const { return _bmStartWord + _bmWordSize; }7576// read marks7778bool isMarked(HeapWord* addr) const {79assert(_bmStartWord <= addr && addr < (_bmStartWord + _bmWordSize),80"outside underlying space?");81return _bm.at(heapWordToOffset(addr));82}8384// iteration85inline bool iterate(BitMapClosure* cl, MemRegion mr);86inline bool iterate(BitMapClosure* cl);8788// Return the address corresponding to the next marked bit at or after89// "addr", and before "limit", if "limit" is non-NULL. If there is no90// such bit, returns "limit" if that is non-NULL, or else "endWord()".91HeapWord* getNextMarkedWordAddress(const HeapWord* addr,92const HeapWord* limit = NULL) const;93// Return the address corresponding to the next unmarked bit at or after94// "addr", and before "limit", if "limit" is non-NULL. If there is no95// such bit, returns "limit" if that is non-NULL, or else "endWord()".96HeapWord* getNextUnmarkedWordAddress(const HeapWord* addr,97const HeapWord* limit = NULL) const;9899// conversion utilities100HeapWord* offsetToHeapWord(size_t offset) const {101return _bmStartWord + (offset << _shifter);102}103size_t heapWordToOffset(const HeapWord* addr) const {104return pointer_delta(addr, _bmStartWord) >> _shifter;105}106int heapWordDiffToOffsetDiff(size_t diff) const;107108// The argument addr should be the start address of a valid object109HeapWord* nextObject(HeapWord* addr) {110oop obj = (oop) addr;111HeapWord* res = addr + obj->size();112assert(offsetToHeapWord(heapWordToOffset(res)) == res, "sanity");113return res;114}115116void print_on_error(outputStream* st, const char* prefix) const;117118// debugging119NOT_PRODUCT(bool covers(MemRegion rs) const;)120};121122class CMBitMapMappingChangedListener : public G1MappingChangedListener {123private:124CMBitMap* _bm;125public:126CMBitMapMappingChangedListener() : _bm(NULL) {}127128void set_bitmap(CMBitMap* bm) { _bm = bm; }129130virtual void on_commit(uint start_idx, size_t num_regions, bool zero_filled);131};132133class CMBitMap : public CMBitMapRO {134private:135CMBitMapMappingChangedListener _listener;136137public:138static size_t compute_size(size_t heap_size);139// Returns the amount of bytes on the heap between two marks in the bitmap.140static size_t mark_distance();141142CMBitMap() : CMBitMapRO(LogMinObjAlignment), _listener() { _listener.set_bitmap(this); }143144// Initializes the underlying BitMap to cover the given area.145void initialize(MemRegion heap, G1RegionToSpaceMapper* storage);146147// Write marks.148inline void mark(HeapWord* addr);149inline void clear(HeapWord* addr);150inline bool parMark(HeapWord* addr);151inline bool parClear(HeapWord* addr);152153void markRange(MemRegion mr);154void clearRange(MemRegion mr);155156// Starting at the bit corresponding to "addr" (inclusive), find the next157// "1" bit, if any. This bit starts some run of consecutive "1"'s; find158// the end of this run (stopping at "end_addr"). Return the MemRegion159// covering from the start of the region corresponding to the first bit160// of the run to the end of the region corresponding to the last bit of161// the run. If there is no "1" bit at or after "addr", return an empty162// MemRegion.163MemRegion getAndClearMarkedRegion(HeapWord* addr, HeapWord* end_addr);164165// Clear the whole mark bitmap.166void clearAll();167};168169// Represents a marking stack used by ConcurrentMarking in the G1 collector.170class CMMarkStack VALUE_OBJ_CLASS_SPEC {171VirtualSpace _virtual_space; // Underlying backing store for actual stack172ConcurrentMark* _cm;173oop* _base; // bottom of stack174jint _index; // one more than last occupied index175jint _capacity; // max #elements176jint _saved_index; // value of _index saved at start of GC177NOT_PRODUCT(jint _max_depth;) // max depth plumbed during run178179bool _overflow;180bool _should_expand;181DEBUG_ONLY(bool _drain_in_progress;)182DEBUG_ONLY(bool _drain_in_progress_yields;)183184public:185CMMarkStack(ConcurrentMark* cm);186~CMMarkStack();187188#ifndef PRODUCT189jint max_depth() const {190return _max_depth;191}192#endif193194bool allocate(size_t capacity);195196oop pop() {197if (!isEmpty()) {198return _base[--_index] ;199}200return NULL;201}202203// If overflow happens, don't do the push, and record the overflow.204// *Requires* that "ptr" is already marked.205void push(oop ptr) {206if (isFull()) {207// Record overflow.208_overflow = true;209return;210} else {211_base[_index++] = ptr;212NOT_PRODUCT(_max_depth = MAX2(_max_depth, _index));213}214}215// Non-block impl. Note: concurrency is allowed only with other216// "par_push" operations, not with "pop" or "drain". We would need217// parallel versions of them if such concurrency was desired.218void par_push(oop ptr);219220// Pushes the first "n" elements of "ptr_arr" on the stack.221// Non-block impl. Note: concurrency is allowed only with other222// "par_adjoin_arr" or "push" operations, not with "pop" or "drain".223void par_adjoin_arr(oop* ptr_arr, int n);224225// Pushes the first "n" elements of "ptr_arr" on the stack.226// Locking impl: concurrency is allowed only with227// "par_push_arr" and/or "par_pop_arr" operations, which use the same228// locking strategy.229void par_push_arr(oop* ptr_arr, int n);230231// If returns false, the array was empty. Otherwise, removes up to "max"232// elements from the stack, and transfers them to "ptr_arr" in an233// unspecified order. The actual number transferred is given in "n" ("n234// == 0" is deliberately redundant with the return value.) Locking impl:235// concurrency is allowed only with "par_push_arr" and/or "par_pop_arr"236// operations, which use the same locking strategy.237bool par_pop_arr(oop* ptr_arr, int max, int* n);238239// Drain the mark stack, applying the given closure to all fields of240// objects on the stack. (That is, continue until the stack is empty,241// even if closure applications add entries to the stack.) The "bm"242// argument, if non-null, may be used to verify that only marked objects243// are on the mark stack. If "yield_after" is "true", then the244// concurrent marker performing the drain offers to yield after245// processing each object. If a yield occurs, stops the drain operation246// and returns false. Otherwise, returns true.247template<class OopClosureClass>248bool drain(OopClosureClass* cl, CMBitMap* bm, bool yield_after = false);249250bool isEmpty() { return _index == 0; }251bool isFull() { return _index == _capacity; }252int maxElems() { return _capacity; }253254bool overflow() { return _overflow; }255void clear_overflow() { _overflow = false; }256257bool should_expand() const { return _should_expand; }258void set_should_expand();259260// Expand the stack, typically in response to an overflow condition261void expand();262263int size() { return _index; }264265void setEmpty() { _index = 0; clear_overflow(); }266267// Record the current index.268void note_start_of_gc();269270// Make sure that we have not added any entries to the stack during GC.271void note_end_of_gc();272273// iterate over the oops in the mark stack, up to the bound recorded via274// the call above.275void oops_do(OopClosure* f);276};277278class ForceOverflowSettings VALUE_OBJ_CLASS_SPEC {279private:280#ifndef PRODUCT281uintx _num_remaining;282bool _force;283#endif // !defined(PRODUCT)284285public:286void init() PRODUCT_RETURN;287void update() PRODUCT_RETURN;288bool should_force() PRODUCT_RETURN_( return false; );289};290291// this will enable a variety of different statistics per GC task292#define _MARKING_STATS_ 0293// this will enable the higher verbose levels294#define _MARKING_VERBOSE_ 0295296#if _MARKING_STATS_297#define statsOnly(statement) \298do { \299statement ; \300} while (0)301#else // _MARKING_STATS_302#define statsOnly(statement) \303do { \304} while (0)305#endif // _MARKING_STATS_306307typedef enum {308no_verbose = 0, // verbose turned off309stats_verbose, // only prints stats at the end of marking310low_verbose, // low verbose, mostly per region and per major event311medium_verbose, // a bit more detailed than low312high_verbose // per object verbose313} CMVerboseLevel;314315class YoungList;316317// Root Regions are regions that are not empty at the beginning of a318// marking cycle and which we might collect during an evacuation pause319// while the cycle is active. Given that, during evacuation pauses, we320// do not copy objects that are explicitly marked, what we have to do321// for the root regions is to scan them and mark all objects reachable322// from them. According to the SATB assumptions, we only need to visit323// each object once during marking. So, as long as we finish this scan324// before the next evacuation pause, we can copy the objects from the325// root regions without having to mark them or do anything else to them.326//327// Currently, we only support root region scanning once (at the start328// of the marking cycle) and the root regions are all the survivor329// regions populated during the initial-mark pause.330class CMRootRegions VALUE_OBJ_CLASS_SPEC {331private:332YoungList* _young_list;333ConcurrentMark* _cm;334335volatile bool _scan_in_progress;336volatile bool _should_abort;337HeapRegion* volatile _next_survivor;338339public:340CMRootRegions();341// We actually do most of the initialization in this method.342void init(G1CollectedHeap* g1h, ConcurrentMark* cm);343344// Reset the claiming / scanning of the root regions.345void prepare_for_scan();346347// Forces get_next() to return NULL so that the iteration aborts early.348void abort() { _should_abort = true; }349350// Return true if the CM thread are actively scanning root regions,351// false otherwise.352bool scan_in_progress() { return _scan_in_progress; }353354// Claim the next root region to scan atomically, or return NULL if355// all have been claimed.356HeapRegion* claim_next();357358// Flag that we're done with root region scanning and notify anyone359// who's waiting on it. If aborted is false, assume that all regions360// have been claimed.361void scan_finished();362363// If CM threads are still scanning root regions, wait until they364// are done. Return true if we had to wait, false otherwise.365bool wait_until_scan_finished();366};367368class ConcurrentMarkThread;369370class ConcurrentMark: public CHeapObj<mtGC> {371friend class CMMarkStack;372friend class ConcurrentMarkThread;373friend class CMTask;374friend class CMBitMapClosure;375friend class CMGlobalObjectClosure;376friend class CMRemarkTask;377friend class CMConcurrentMarkingTask;378friend class G1ParNoteEndTask;379friend class CalcLiveObjectsClosure;380friend class G1CMRefProcTaskProxy;381friend class G1CMRefProcTaskExecutor;382friend class G1CMKeepAliveAndDrainClosure;383friend class G1CMDrainMarkingStackClosure;384385protected:386ConcurrentMarkThread* _cmThread; // the thread doing the work387G1CollectedHeap* _g1h; // the heap.388uint _parallel_marking_threads; // the number of marking389// threads we're use390uint _max_parallel_marking_threads; // max number of marking391// threads we'll ever use392double _sleep_factor; // how much we have to sleep, with393// respect to the work we just did, to394// meet the marking overhead goal395double _marking_task_overhead; // marking target overhead for396// a single task397398// same as the two above, but for the cleanup task399double _cleanup_sleep_factor;400double _cleanup_task_overhead;401402FreeRegionList _cleanup_list;403404// Concurrent marking support structures405CMBitMap _markBitMap1;406CMBitMap _markBitMap2;407CMBitMapRO* _prevMarkBitMap; // completed mark bitmap408CMBitMap* _nextMarkBitMap; // under-construction mark bitmap409410BitMap _region_bm;411BitMap _card_bm;412413// Heap bounds414HeapWord* _heap_start;415HeapWord* _heap_end;416417// Root region tracking and claiming.418CMRootRegions _root_regions;419420// For gray objects421CMMarkStack _markStack; // Grey objects behind global finger.422HeapWord* volatile _finger; // the global finger, region aligned,423// always points to the end of the424// last claimed region425426// marking tasks427uint _max_worker_id;// maximum worker id428uint _active_tasks; // task num currently active429CMTask** _tasks; // task queue array (max_worker_id len)430CMTaskQueueSet* _task_queues; // task queue set431ParallelTaskTerminator _terminator; // for termination432433// Two sync barriers that are used to synchronise tasks when an434// overflow occurs. The algorithm is the following. All tasks enter435// the first one to ensure that they have all stopped manipulating436// the global data structures. After they exit it, they re-initialise437// their data structures and task 0 re-initialises the global data438// structures. Then, they enter the second sync barrier. This439// ensure, that no task starts doing work before all data440// structures (local and global) have been re-initialised. When they441// exit it, they are free to start working again.442WorkGangBarrierSync _first_overflow_barrier_sync;443WorkGangBarrierSync _second_overflow_barrier_sync;444445// this is set by any task, when an overflow on the global data446// structures is detected.447volatile bool _has_overflown;448// true: marking is concurrent, false: we're in remark449volatile bool _concurrent;450// set at the end of a Full GC so that marking aborts451volatile bool _has_aborted;452GCId _aborted_gc_id;453454// used when remark aborts due to an overflow to indicate that455// another concurrent marking phase should start456volatile bool _restart_for_overflow;457458// This is true from the very start of concurrent marking until the459// point when all the tasks complete their work. It is really used460// to determine the points between the end of concurrent marking and461// time of remark.462volatile bool _concurrent_marking_in_progress;463464// verbose level465CMVerboseLevel _verbose_level;466467// All of these times are in ms.468NumberSeq _init_times;469NumberSeq _remark_times;470NumberSeq _remark_mark_times;471NumberSeq _remark_weak_ref_times;472NumberSeq _cleanup_times;473double _total_counting_time;474double _total_rs_scrub_time;475476double* _accum_task_vtime; // accumulated task vtime477478FlexibleWorkGang* _parallel_workers;479480ForceOverflowSettings _force_overflow_conc;481ForceOverflowSettings _force_overflow_stw;482483void weakRefsWorkParallelPart(BoolObjectClosure* is_alive, bool purged_classes);484void weakRefsWork(bool clear_all_soft_refs);485486void swapMarkBitMaps();487488// It resets the global marking data structures, as well as the489// task local ones; should be called during initial mark.490void reset();491492// Resets all the marking data structures. Called when we have to restart493// marking or when marking completes (via set_non_marking_state below).494void reset_marking_state(bool clear_overflow = true);495496// We do this after we're done with marking so that the marking data497// structures are initialised to a sensible and predictable state.498void set_non_marking_state();499500// Called to indicate how many threads are currently active.501void set_concurrency(uint active_tasks);502503// It should be called to indicate which phase we're in (concurrent504// mark or remark) and how many threads are currently active.505void set_concurrency_and_phase(uint active_tasks, bool concurrent);506507// prints all gathered CM-related statistics508void print_stats();509510bool cleanup_list_is_empty() {511return _cleanup_list.is_empty();512}513514// accessor methods515uint parallel_marking_threads() const { return _parallel_marking_threads; }516uint max_parallel_marking_threads() const { return _max_parallel_marking_threads;}517double sleep_factor() { return _sleep_factor; }518double marking_task_overhead() { return _marking_task_overhead;}519double cleanup_sleep_factor() { return _cleanup_sleep_factor; }520double cleanup_task_overhead() { return _cleanup_task_overhead;}521522bool use_parallel_marking_threads() const {523assert(parallel_marking_threads() <=524max_parallel_marking_threads(), "sanity");525assert((_parallel_workers == NULL && parallel_marking_threads() == 0) ||526parallel_marking_threads() > 0,527"parallel workers not set up correctly");528return _parallel_workers != NULL;529}530531HeapWord* finger() { return _finger; }532bool concurrent() { return _concurrent; }533uint active_tasks() { return _active_tasks; }534ParallelTaskTerminator* terminator() { return &_terminator; }535536// It claims the next available region to be scanned by a marking537// task/thread. It might return NULL if the next region is empty or538// we have run out of regions. In the latter case, out_of_regions()539// determines whether we've really run out of regions or the task540// should call claim_region() again. This might seem a bit541// awkward. Originally, the code was written so that claim_region()542// either successfully returned with a non-empty region or there543// were no more regions to be claimed. The problem with this was544// that, in certain circumstances, it iterated over large chunks of545// the heap finding only empty regions and, while it was working, it546// was preventing the calling task to call its regular clock547// method. So, this way, each task will spend very little time in548// claim_region() and is allowed to call the regular clock method549// frequently.550HeapRegion* claim_region(uint worker_id);551552// It determines whether we've run out of regions to scan. Note that553// the finger can point past the heap end in case the heap was expanded554// to satisfy an allocation without doing a GC. This is fine, because all555// objects in those regions will be considered live anyway because of556// SATB guarantees (i.e. their TAMS will be equal to bottom).557bool out_of_regions() { return _finger >= _heap_end; }558559// Returns the task with the given id560CMTask* task(int id) {561assert(0 <= id && id < (int) _active_tasks,562"task id not within active bounds");563return _tasks[id];564}565566// Returns the task queue with the given id567CMTaskQueue* task_queue(int id) {568assert(0 <= id && id < (int) _active_tasks,569"task queue id not within active bounds");570return (CMTaskQueue*) _task_queues->queue(id);571}572573// Returns the task queue set574CMTaskQueueSet* task_queues() { return _task_queues; }575576// Access / manipulation of the overflow flag which is set to577// indicate that the global stack has overflown578bool has_overflown() { return _has_overflown; }579void set_has_overflown() { _has_overflown = true; }580void clear_has_overflown() { _has_overflown = false; }581bool restart_for_overflow() { return _restart_for_overflow; }582583// Methods to enter the two overflow sync barriers584void enter_first_sync_barrier(uint worker_id);585void enter_second_sync_barrier(uint worker_id);586587ForceOverflowSettings* force_overflow_conc() {588return &_force_overflow_conc;589}590591ForceOverflowSettings* force_overflow_stw() {592return &_force_overflow_stw;593}594595ForceOverflowSettings* force_overflow() {596if (concurrent()) {597return force_overflow_conc();598} else {599return force_overflow_stw();600}601}602603// Live Data Counting data structures...604// These data structures are initialized at the start of605// marking. They are written to while marking is active.606// They are aggregated during remark; the aggregated values607// are then used to populate the _region_bm, _card_bm, and608// the total live bytes, which are then subsequently updated609// during cleanup.610611// An array of bitmaps (one bit map per task). Each bitmap612// is used to record the cards spanned by the live objects613// marked by that task/worker.614BitMap* _count_card_bitmaps;615616// Used to record the number of marked live bytes617// (for each region, by worker thread).618size_t** _count_marked_bytes;619620// Card index of the bottom of the G1 heap. Used for biasing indices into621// the card bitmaps.622intptr_t _heap_bottom_card_num;623624// Set to true when initialization is complete625bool _completed_initialization;626627public:628// Manipulation of the global mark stack.629// Notice that the first mark_stack_push is CAS-based, whereas the630// two below are Mutex-based. This is OK since the first one is only631// called during evacuation pauses and doesn't compete with the632// other two (which are called by the marking tasks during633// concurrent marking or remark).634bool mark_stack_push(oop p) {635_markStack.par_push(p);636if (_markStack.overflow()) {637set_has_overflown();638return false;639}640return true;641}642bool mark_stack_push(oop* arr, int n) {643_markStack.par_push_arr(arr, n);644if (_markStack.overflow()) {645set_has_overflown();646return false;647}648return true;649}650void mark_stack_pop(oop* arr, int max, int* n) {651_markStack.par_pop_arr(arr, max, n);652}653size_t mark_stack_size() { return _markStack.size(); }654size_t partial_mark_stack_size_target() { return _markStack.maxElems()/3; }655bool mark_stack_overflow() { return _markStack.overflow(); }656bool mark_stack_empty() { return _markStack.isEmpty(); }657658CMRootRegions* root_regions() { return &_root_regions; }659660bool concurrent_marking_in_progress() {661return _concurrent_marking_in_progress;662}663void set_concurrent_marking_in_progress() {664_concurrent_marking_in_progress = true;665}666void clear_concurrent_marking_in_progress() {667_concurrent_marking_in_progress = false;668}669670void update_accum_task_vtime(int i, double vtime) {671_accum_task_vtime[i] += vtime;672}673674double all_task_accum_vtime() {675double ret = 0.0;676for (uint i = 0; i < _max_worker_id; ++i)677ret += _accum_task_vtime[i];678return ret;679}680681// Attempts to steal an object from the task queues of other tasks682bool try_stealing(uint worker_id, int* hash_seed, oop& obj) {683return _task_queues->steal(worker_id, hash_seed, obj);684}685686ConcurrentMark(G1CollectedHeap* g1h,687G1RegionToSpaceMapper* prev_bitmap_storage,688G1RegionToSpaceMapper* next_bitmap_storage);689~ConcurrentMark();690691ConcurrentMarkThread* cmThread() { return _cmThread; }692693CMBitMapRO* prevMarkBitMap() const { return _prevMarkBitMap; }694CMBitMap* nextMarkBitMap() const { return _nextMarkBitMap; }695696// Returns the number of GC threads to be used in a concurrent697// phase based on the number of GC threads being used in a STW698// phase.699uint scale_parallel_threads(uint n_par_threads);700701// Calculates the number of GC threads to be used in a concurrent phase.702uint calc_parallel_marking_threads();703704// The following three are interaction between CM and705// G1CollectedHeap706707// This notifies CM that a root during initial-mark needs to be708// grayed. It is MT-safe. word_size is the size of the object in709// words. It is passed explicitly as sometimes we cannot calculate710// it from the given object because it might be in an inconsistent711// state (e.g., in to-space and being copied). So the caller is712// responsible for dealing with this issue (e.g., get the size from713// the from-space image when the to-space image might be714// inconsistent) and always passing the size. hr is the region that715// contains the object and it's passed optionally from callers who716// might already have it (no point in recalculating it).717inline void grayRoot(oop obj,718size_t word_size,719uint worker_id,720HeapRegion* hr = NULL);721722// It iterates over the heap and for each object it comes across it723// will dump the contents of its reference fields, as well as724// liveness information for the object and its referents. The dump725// will be written to a file with the following name:726// G1PrintReachableBaseFile + "." + str.727// vo decides whether the prev (vo == UsePrevMarking), the next728// (vo == UseNextMarking) marking information, or the mark word729// (vo == UseMarkWord) will be used to determine the liveness of730// each object / referent.731// If all is true, all objects in the heap will be dumped, otherwise732// only the live ones. In the dump the following symbols / breviations733// are used:734// M : an explicitly live object (its bitmap bit is set)735// > : an implicitly live object (over tams)736// O : an object outside the G1 heap (typically: in the perm gen)737// NOT : a reference field whose referent is not live738// AND MARKED : indicates that an object is both explicitly and739// implicitly live (it should be one or the other, not both)740void print_reachable(const char* str,741VerifyOption vo,742bool all) PRODUCT_RETURN;743744// Clear the next marking bitmap (will be called concurrently).745void clearNextBitmap();746747// Return whether the next mark bitmap has no marks set. To be used for assertions748// only. Will not yield to pause requests.749bool nextMarkBitmapIsClear();750751// These two do the work that needs to be done before and after the752// initial root checkpoint. Since this checkpoint can be done at two753// different points (i.e. an explicit pause or piggy-backed on a754// young collection), then it's nice to be able to easily share the755// pre/post code. It might be the case that we can put everything in756// the post method. TP757void checkpointRootsInitialPre();758void checkpointRootsInitialPost();759760// Scan all the root regions and mark everything reachable from761// them.762void scanRootRegions();763764// Scan a single root region and mark everything reachable from it.765void scanRootRegion(HeapRegion* hr, uint worker_id);766767// Do concurrent phase of marking, to a tentative transitive closure.768void markFromRoots();769770void checkpointRootsFinal(bool clear_all_soft_refs);771void checkpointRootsFinalWork();772void cleanup();773void completeCleanup();774775// Mark in the previous bitmap. NB: this is usually read-only, so use776// this carefully!777inline void markPrev(oop p);778779// Clears marks for all objects in the given range, for the prev or780// next bitmaps. NB: the previous bitmap is usually781// read-only, so use this carefully!782void clearRangePrevBitmap(MemRegion mr);783void clearRangeNextBitmap(MemRegion mr);784785// Notify data structures that a GC has started.786void note_start_of_gc() {787_markStack.note_start_of_gc();788}789790// Notify data structures that a GC is finished.791void note_end_of_gc() {792_markStack.note_end_of_gc();793}794795// Verify that there are no CSet oops on the stacks (taskqueues /796// global mark stack) and fingers (global / per-task).797// If marking is not in progress, it's a no-op.798void verify_no_cset_oops() PRODUCT_RETURN;799800bool isPrevMarked(oop p) const {801assert(p != NULL && p->is_oop(), "expected an oop");802HeapWord* addr = (HeapWord*)p;803assert(addr >= _prevMarkBitMap->startWord() ||804addr < _prevMarkBitMap->endWord(), "in a region");805806return _prevMarkBitMap->isMarked(addr);807}808809inline bool do_yield_check(uint worker_i = 0);810811// Called to abort the marking cycle after a Full GC takes palce.812void abort();813814bool has_aborted() { return _has_aborted; }815816const GCId& concurrent_gc_id();817818// This prints the global/local fingers. It is used for debugging.819NOT_PRODUCT(void print_finger();)820821void print_summary_info();822823void print_worker_threads_on(outputStream* st) const;824825void print_on_error(outputStream* st) const;826827// The following indicate whether a given verbose level has been828// set. Notice that anything above stats is conditional to829// _MARKING_VERBOSE_ having been set to 1830bool verbose_stats() {831return _verbose_level >= stats_verbose;832}833bool verbose_low() {834return _MARKING_VERBOSE_ && _verbose_level >= low_verbose;835}836bool verbose_medium() {837return _MARKING_VERBOSE_ && _verbose_level >= medium_verbose;838}839bool verbose_high() {840return _MARKING_VERBOSE_ && _verbose_level >= high_verbose;841}842843// Liveness counting844845// Utility routine to set an exclusive range of cards on the given846// card liveness bitmap847inline void set_card_bitmap_range(BitMap* card_bm,848BitMap::idx_t start_idx,849BitMap::idx_t end_idx,850bool is_par);851852// Returns the card number of the bottom of the G1 heap.853// Used in biasing indices into accounting card bitmaps.854intptr_t heap_bottom_card_num() const {855return _heap_bottom_card_num;856}857858// Returns the card bitmap for a given task or worker id.859BitMap* count_card_bitmap_for(uint worker_id) {860assert(0 <= worker_id && worker_id < _max_worker_id, "oob");861assert(_count_card_bitmaps != NULL, "uninitialized");862BitMap* task_card_bm = &_count_card_bitmaps[worker_id];863assert(task_card_bm->size() == _card_bm.size(), "size mismatch");864return task_card_bm;865}866867// Returns the array containing the marked bytes for each region,868// for the given worker or task id.869size_t* count_marked_bytes_array_for(uint worker_id) {870assert(0 <= worker_id && worker_id < _max_worker_id, "oob");871assert(_count_marked_bytes != NULL, "uninitialized");872size_t* marked_bytes_array = _count_marked_bytes[worker_id];873assert(marked_bytes_array != NULL, "uninitialized");874return marked_bytes_array;875}876877// Returns the index in the liveness accounting card table bitmap878// for the given address879inline BitMap::idx_t card_bitmap_index_for(HeapWord* addr);880881// Counts the size of the given memory region in the the given882// marked_bytes array slot for the given HeapRegion.883// Sets the bits in the given card bitmap that are associated with the884// cards that are spanned by the memory region.885inline void count_region(MemRegion mr,886HeapRegion* hr,887size_t* marked_bytes_array,888BitMap* task_card_bm);889890// Counts the given memory region in the task/worker counting891// data structures for the given worker id.892inline void count_region(MemRegion mr, HeapRegion* hr, uint worker_id);893894// Counts the given object in the given task/worker counting895// data structures.896inline void count_object(oop obj,897HeapRegion* hr,898size_t* marked_bytes_array,899BitMap* task_card_bm);900901// Attempts to mark the given object and, if successful, counts902// the object in the given task/worker counting structures.903inline bool par_mark_and_count(oop obj,904HeapRegion* hr,905size_t* marked_bytes_array,906BitMap* task_card_bm);907908// Attempts to mark the given object and, if successful, counts909// the object in the task/worker counting structures for the910// given worker id.911inline bool par_mark_and_count(oop obj,912size_t word_size,913HeapRegion* hr,914uint worker_id);915916// Returns true if initialization was successfully completed.917bool completed_initialization() const {918return _completed_initialization;919}920921protected:922// Clear all the per-task bitmaps and arrays used to store the923// counting data.924void clear_all_count_data();925926// Aggregates the counting data for each worker/task927// that was constructed while marking. Also sets928// the amount of marked bytes for each region and929// the top at concurrent mark count.930void aggregate_count_data();931932// Verification routine933void verify_count_data();934};935936// A class representing a marking task.937class CMTask : public TerminatorTerminator {938private:939enum PrivateConstants {940// the regular clock call is called once the scanned words reaches941// this limit942words_scanned_period = 12*1024,943// the regular clock call is called once the number of visited944// references reaches this limit945refs_reached_period = 1024,946// initial value for the hash seed, used in the work stealing code947init_hash_seed = 17,948// how many entries will be transferred between global stack and949// local queues950global_stack_transfer_size = 16951};952953G1CMObjArrayProcessor _objArray_processor;954955uint _worker_id;956G1CollectedHeap* _g1h;957ConcurrentMark* _cm;958CMBitMap* _nextMarkBitMap;959// the task queue of this task960CMTaskQueue* _task_queue;961private:962// the task queue set---needed for stealing963CMTaskQueueSet* _task_queues;964// indicates whether the task has been claimed---this is only for965// debugging purposes966bool _claimed;967968// number of calls to this task969int _calls;970971// when the virtual timer reaches this time, the marking step should972// exit973double _time_target_ms;974// the start time of the current marking step975double _start_time_ms;976977// the oop closure used for iterations over oops978G1CMOopClosure* _cm_oop_closure;979980// the region this task is scanning, NULL if we're not scanning any981HeapRegion* _curr_region;982// the local finger of this task, NULL if we're not scanning a region983HeapWord* _finger;984// limit of the region this task is scanning, NULL if we're not scanning one985HeapWord* _region_limit;986987// the number of words this task has scanned988size_t _words_scanned;989// When _words_scanned reaches this limit, the regular clock is990// called. Notice that this might be decreased under certain991// circumstances (i.e. when we believe that we did an expensive992// operation).993size_t _words_scanned_limit;994// the initial value of _words_scanned_limit (i.e. what it was995// before it was decreased).996size_t _real_words_scanned_limit;997998// the number of references this task has visited999size_t _refs_reached;1000// When _refs_reached reaches this limit, the regular clock is1001// called. Notice this this might be decreased under certain1002// circumstances (i.e. when we believe that we did an expensive1003// operation).1004size_t _refs_reached_limit;1005// the initial value of _refs_reached_limit (i.e. what it was before1006// it was decreased).1007size_t _real_refs_reached_limit;10081009// used by the work stealing stuff1010int _hash_seed;1011// if this is true, then the task has aborted for some reason1012bool _has_aborted;1013// set when the task aborts because it has met its time quota1014bool _has_timed_out;1015// true when we're draining SATB buffers; this avoids the task1016// aborting due to SATB buffers being available (as we're already1017// dealing with them)1018bool _draining_satb_buffers;10191020// number sequence of past step times1021NumberSeq _step_times_ms;1022// elapsed time of this task1023double _elapsed_time_ms;1024// termination time of this task1025double _termination_time_ms;1026// when this task got into the termination protocol1027double _termination_start_time_ms;10281029// true when the task is during a concurrent phase, false when it is1030// in the remark phase (so, in the latter case, we do not have to1031// check all the things that we have to check during the concurrent1032// phase, i.e. SATB buffer availability...)1033bool _concurrent;10341035TruncatedSeq _marking_step_diffs_ms;10361037// Counting data structures. Embedding the task's marked_bytes_array1038// and card bitmap into the actual task saves having to go through1039// the ConcurrentMark object.1040size_t* _marked_bytes_array;1041BitMap* _card_bm;10421043// LOTS of statistics related with this task1044#if _MARKING_STATS_1045NumberSeq _all_clock_intervals_ms;1046double _interval_start_time_ms;10471048int _aborted;1049int _aborted_overflow;1050int _aborted_cm_aborted;1051int _aborted_yield;1052int _aborted_timed_out;1053int _aborted_satb;1054int _aborted_termination;10551056int _steal_attempts;1057int _steals;10581059int _clock_due_to_marking;1060int _clock_due_to_scanning;10611062int _local_pushes;1063int _local_pops;1064int _local_max_size;1065int _objs_scanned;10661067int _global_pushes;1068int _global_pops;1069int _global_max_size;10701071int _global_transfers_to;1072int _global_transfers_from;10731074int _regions_claimed;1075int _objs_found_on_bitmap;10761077int _satb_buffers_processed;1078#endif // _MARKING_STATS_10791080// it updates the local fields after this task has claimed1081// a new region to scan1082void setup_for_region(HeapRegion* hr);1083// it brings up-to-date the limit of the region1084void update_region_limit();10851086// called when either the words scanned or the refs visited limit1087// has been reached1088void reached_limit();1089// recalculates the words scanned and refs visited limits1090void recalculate_limits();1091// decreases the words scanned and refs visited limits when we reach1092// an expensive operation1093void decrease_limits();1094// it checks whether the words scanned or refs visited reached their1095// respective limit and calls reached_limit() if they have1096void check_limits() {1097if (_words_scanned >= _words_scanned_limit ||1098_refs_reached >= _refs_reached_limit) {1099reached_limit();1100}1101}1102// this is supposed to be called regularly during a marking step as1103// it checks a bunch of conditions that might cause the marking step1104// to abort1105void regular_clock_call();1106bool concurrent() { return _concurrent; }11071108// Test whether obj might have already been passed over by the1109// mark bitmap scan, and so needs to be pushed onto the mark stack.1110bool is_below_finger(oop obj, HeapWord* global_finger) const;11111112template<bool scan> void process_grey_object(oop obj);11131114public:1115// Apply the closure on the given area of the objArray. Return the number of words1116// scanned.1117inline size_t scan_objArray(objArrayOop obj, MemRegion mr);1118// It resets the task; it should be called right at the beginning of1119// a marking phase.1120void reset(CMBitMap* _nextMarkBitMap);1121// it clears all the fields that correspond to a claimed region.1122void clear_region_fields();11231124void set_concurrent(bool concurrent) { _concurrent = concurrent; }11251126// The main method of this class which performs a marking step1127// trying not to exceed the given duration. However, it might exit1128// prematurely, according to some conditions (i.e. SATB buffers are1129// available for processing).1130void do_marking_step(double target_ms,1131bool do_termination,1132bool is_serial);11331134// These two calls start and stop the timer1135void record_start_time() {1136_elapsed_time_ms = os::elapsedTime() * 1000.0;1137}1138void record_end_time() {1139_elapsed_time_ms = os::elapsedTime() * 1000.0 - _elapsed_time_ms;1140}11411142// returns the worker ID associated with this task.1143uint worker_id() { return _worker_id; }11441145// From TerminatorTerminator. It determines whether this task should1146// exit the termination protocol after it's entered it.1147virtual bool should_exit_termination();11481149// Resets the local region fields after a task has finished scanning a1150// region; or when they have become stale as a result of the region1151// being evacuated.1152void giveup_current_region();11531154HeapWord* finger() { return _finger; }11551156bool has_aborted() { return _has_aborted; }1157void set_has_aborted() { _has_aborted = true; }1158void clear_has_aborted() { _has_aborted = false; }1159bool has_timed_out() { return _has_timed_out; }1160bool claimed() { return _claimed; }11611162void set_cm_oop_closure(G1CMOopClosure* cm_oop_closure);11631164// Increment the number of references this task has visited.1165void increment_refs_reached() { ++_refs_reached; }11661167// Grey the object by marking it. If not already marked, push it on1168// the local queue if below the finger.1169// Precondition: obj is in region.1170// Precondition: obj is below region's NTAMS.1171inline void make_reference_grey(oop obj, HeapRegion* region);11721173// Grey the object (by calling make_grey_reference) if required,1174// e.g. obj is below its containing region's NTAMS.1175// Precondition: obj is a valid heap object.1176inline void deal_with_reference(oop obj);11771178// It scans an object and visits its children.1179void scan_object(oop obj) { process_grey_object<true>(obj); }11801181// It pushes an object on the local queue.1182inline void push(oop obj);11831184// These two move entries to/from the global stack.1185void move_entries_to_global_stack();1186void get_entries_from_global_stack();11871188// It pops and scans objects from the local queue. If partially is1189// true, then it stops when the queue size is of a given limit. If1190// partially is false, then it stops when the queue is empty.1191void drain_local_queue(bool partially);1192// It moves entries from the global stack to the local queue and1193// drains the local queue. If partially is true, then it stops when1194// both the global stack and the local queue reach a given size. If1195// partially if false, it tries to empty them totally.1196void drain_global_stack(bool partially);1197// It keeps picking SATB buffers and processing them until no SATB1198// buffers are available.1199void drain_satb_buffers();12001201// moves the local finger to a new location1202inline void move_finger_to(HeapWord* new_finger) {1203assert(new_finger >= _finger && new_finger < _region_limit, "invariant");1204_finger = new_finger;1205}12061207CMTask(uint worker_id,1208ConcurrentMark *cm,1209size_t* marked_bytes,1210BitMap* card_bm,1211CMTaskQueue* task_queue,1212CMTaskQueueSet* task_queues);12131214// it prints statistics associated with this task1215void print_stats();12161217#if _MARKING_STATS_1218void increase_objs_found_on_bitmap() { ++_objs_found_on_bitmap; }1219#endif // _MARKING_STATS_1220};12211222// Class that's used to to print out per-region liveness1223// information. It's currently used at the end of marking and also1224// after we sort the old regions at the end of the cleanup operation.1225class G1PrintRegionLivenessInfoClosure: public HeapRegionClosure {1226private:1227outputStream* _out;12281229// Accumulators for these values.1230size_t _total_used_bytes;1231size_t _total_capacity_bytes;1232size_t _total_prev_live_bytes;1233size_t _total_next_live_bytes;12341235// These are set up when we come across a "stars humongous" region1236// (as this is where most of this information is stored, not in the1237// subsequent "continues humongous" regions). After that, for every1238// region in a given humongous region series we deduce the right1239// values for it by simply subtracting the appropriate amount from1240// these fields. All these values should reach 0 after we've visited1241// the last region in the series.1242size_t _hum_used_bytes;1243size_t _hum_capacity_bytes;1244size_t _hum_prev_live_bytes;1245size_t _hum_next_live_bytes;12461247// Accumulator for the remembered set size1248size_t _total_remset_bytes;12491250// Accumulator for strong code roots memory size1251size_t _total_strong_code_roots_bytes;12521253static double perc(size_t val, size_t total) {1254if (total == 0) {1255return 0.0;1256} else {1257return 100.0 * ((double) val / (double) total);1258}1259}12601261static double bytes_to_mb(size_t val) {1262return (double) val / (double) M;1263}12641265// See the .cpp file.1266size_t get_hum_bytes(size_t* hum_bytes);1267void get_hum_bytes(size_t* used_bytes, size_t* capacity_bytes,1268size_t* prev_live_bytes, size_t* next_live_bytes);12691270public:1271// The header and footer are printed in the constructor and1272// destructor respectively.1273G1PrintRegionLivenessInfoClosure(outputStream* out, const char* phase_name);1274virtual bool doHeapRegion(HeapRegion* r);1275~G1PrintRegionLivenessInfoClosure();1276};12771278#endif // SHARE_VM_GC_IMPLEMENTATION_G1_CONCURRENTMARK_HPP127912801281