Path: blob/master/src/hotspot/share/gc/g1/g1CollectionSet.hpp
40957 views
/*1* Copyright (c) 2016, 2019, 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_GC_G1_G1COLLECTIONSET_HPP25#define SHARE_GC_G1_G1COLLECTIONSET_HPP2627#include "utilities/debug.hpp"28#include "utilities/globalDefinitions.hpp"2930class G1CollectedHeap;31class G1CollectionSetCandidates;32class G1CollectorState;33class G1GCPhaseTimes;34class G1ParScanThreadStateSet;35class G1Policy;36class G1SurvivorRegions;37class HeapRegion;38class HeapRegionClaimer;39class HeapRegionClosure;4041// The collection set.42//43// The set of regions that are evacuated during an evacuation pause.44//45// At the end of a collection, before freeing the collection set, this set46// contains all regions that were evacuated during this collection:47//48// - survivor regions from the last collection (if any)49// - eden regions allocated by the mutator50// - old gen regions evacuated during mixed gc51//52// This set is built incrementally at mutator time as regions are retired, and53// if this had been a mixed gc, some additional (during gc) incrementally added54// old regions from the collection set candidates built during the concurrent55// cycle.56//57// A more detailed overview of how the collection set changes over time follows:58//59// 0) at the end of GC the survivor regions are added to this collection set.60// 1) the mutator incrementally adds eden regions as they retire61//62// ----- gc starts63//64// 2) prepare (finalize) young regions of the collection set for collection65// - relabel the survivors as eden66// - finish up the incremental building that happened at mutator time67//68// iff this is a young-only collection:69//70// a3) evacuate the current collection set in one "initial evacuation" phase71//72// iff this is a mixed collection:73//74// b3) calculate the set of old gen regions we may be able to collect in this75// collection from the list of collection set candidates.76// - one part is added to the current collection set77// - the remainder regions are labeled as optional, and NOT yet added to the78// collection set.79// b4) evacuate the current collection set in the "initial evacuation" phase80// b5) evacuate the optional regions in the "optional evacuation" phase. This is81// done in increments (or rounds).82// b5-1) add a few of the optional regions to the current collection set83// b5-2) evacuate only these newly added optional regions. For this mechanism we84// reuse the incremental collection set building infrastructure (used also at85// mutator time).86// b5-3) repeat from b5-1 until the policy determines we are done87//88// all collections89//90// 6) free the collection set (contains all regions now; empties collection set91// afterwards)92// 7) add survivors to this collection set93//94// ----- gc ends95//96// goto 1)97//98// Examples of how the collection set might look over time:99//100// Legend:101// S = survivor, E = eden, O = old.102// |xxxx| = increment (with increment markers), containing four regions103//104// |SSSS| ... after step 0), with four survivor regions105// |SSSSEE| ... at step 1), after retiring two eden regions106// |SSSSEEEE| ... after step 1), after retiring four eden regions107// |EEEEEEEE| ... after step 2)108//109// iff this is a young-only collection110//111// EEEEEEEE|| ... after step a3), after initial evacuation phase112// || ... after step 6)113// |SS| ... after step 7), with two survivor regions114//115// iff this is a mixed collection116//117// |EEEEEEEEOOOO| ... after step b3), added four regions to be118// evacuated in the "initial evacuation" phase119// EEEEEEEEOOOO|| ... after step b4), incremental part is empty120// after evacuation121// EEEEEEEEOOOO|OO| ... after step b5.1), added two regions to be122// evacuated in the first round of the123// "optional evacuation" phase124// EEEEEEEEOOOOOO|O| ... after step b5.1), added one region to be125// evacuated in the second round of the126// "optional evacuation" phase127// EEEEEEEEOOOOOOO|| ... after step b5), the complete collection set.128// || ... after step b6)129// |SSS| ... after step 7), with three survivor regions130//131class G1CollectionSet {132G1CollectedHeap* _g1h;133G1Policy* _policy;134135// All old gen collection set candidate regions for the current mixed phase.136G1CollectionSetCandidates* _candidates;137138uint _eden_region_length;139uint _survivor_region_length;140uint _old_region_length;141142// The actual collection set as a set of region indices.143// All entries in _collection_set_regions below _collection_set_cur_length are144// assumed to be part of the collection set.145// We assume that at any time there is at most only one writer and (one or more)146// concurrent readers. This means we are good with using storestore and loadload147// barriers on the writer and reader respectively only.148uint* _collection_set_regions;149volatile size_t _collection_set_cur_length;150size_t _collection_set_max_length;151152// When doing mixed collections we can add old regions to the collection set, which153// will be collected only if there is enough time. We call these optional regions.154// This member records the current number of regions that are of that type that155// correspond to the first x entries in the collection set candidates.156uint _num_optional_regions;157158// The number of bytes in the collection set before the pause. Set from159// the incrementally built collection set at the start of an evacuation160// pause, and updated as more regions are added to the collection set.161size_t _bytes_used_before;162163// The number of cards in the remembered set in the collection set. Set from164// the incrementally built collection set at the start of an evacuation165// pause, and updated as more regions are added to the collection set.166size_t _recorded_rs_length;167168enum CSetBuildType {169Active, // We are actively building the collection set170Inactive // We are not actively building the collection set171};172173CSetBuildType _inc_build_state;174size_t _inc_part_start;175176// Information about eden regions in the incremental collection set.177struct IncCollectionSetRegionStat {178// The predicted non-copy time that was added to the total incremental value179// for the collection set.180double _non_copy_time_ms;181// The remembered set length that was added to the total incremental value182// for the collection set.183size_t _rs_length;184185#ifdef ASSERT186// Resets members to "uninitialized" values.187void reset() { _rs_length = ~(size_t)0; _non_copy_time_ms = -1.0; }188#endif189};190191IncCollectionSetRegionStat* _inc_collection_set_stats;192// The associated information that is maintained while the incremental193// collection set is being built with *young* regions. Used to populate194// the recorded info for the evacuation pause.195196// The number of bytes in the incrementally built collection set.197// Used to set _collection_set_bytes_used_before at the start of198// an evacuation pause.199size_t _inc_bytes_used_before;200201// The RSet lengths recorded for regions in the CSet. It is updated202// by the thread that adds a new region to the CSet. We assume that203// only one thread can be allocating a new CSet region (currently,204// it does so after taking the Heap_lock) hence no need to205// synchronize updates to this field.206size_t _inc_recorded_rs_length;207208// A concurrent refinement thread periodically samples the young209// region RSets and needs to update _inc_recorded_rs_length as210// the RSets grow. Instead of having to synchronize updates to that211// field we accumulate them in this field and add it to212// _inc_recorded_rs_length_diff at the start of a GC.213size_t _inc_recorded_rs_length_diff;214215// The predicted elapsed time it will take to collect the regions in216// the CSet. This is updated by the thread that adds a new region to217// the CSet. See the comment for _inc_recorded_rs_length about218// MT-safety assumptions.219double _inc_predicted_non_copy_time_ms;220221// See the comment for _inc_recorded_rs_length_diff.222double _inc_predicted_non_copy_time_ms_diff;223224void set_recorded_rs_length(size_t rs_length);225226G1CollectorState* collector_state() const;227G1GCPhaseTimes* phase_times();228229void verify_young_cset_indices() const NOT_DEBUG_RETURN;230231double predict_region_non_copy_time_ms(HeapRegion* hr) const;232233// Update the incremental collection set information when adding a region.234void add_young_region_common(HeapRegion* hr);235236// Add old region "hr" to the collection set.237void add_old_region(HeapRegion* hr);238void free_optional_regions();239240// Add old region "hr" to optional collection set.241void add_optional_region(HeapRegion* hr);242243void move_candidates_to_collection_set(uint num_regions);244245// Finalize the young part of the initial collection set. Relabel survivor regions246// as Eden and calculate a prediction on how long the evacuation of all young regions247// will take.248double finalize_young_part(double target_pause_time_ms, G1SurvivorRegions* survivors);249// Perform any final calculations on the incremental collection set fields before we250// can use them.251void finalize_incremental_building();252253// Select the old regions of the initial collection set and determine how many optional254// regions we might be able to evacuate in this pause.255void finalize_old_part(double time_remaining_ms);256257// Iterate the part of the collection set given by the offset and length applying the given258// HeapRegionClosure. The worker_id will determine where in the part to start the iteration259// to allow for more efficient parallel iteration.260void iterate_part_from(HeapRegionClosure* cl,261HeapRegionClaimer* hr_claimer,262size_t offset,263size_t length,264uint worker_id,265uint total_workers) const;266public:267G1CollectionSet(G1CollectedHeap* g1h, G1Policy* policy);268~G1CollectionSet();269270// Initializes the collection set giving the maximum possible length of the collection set.271void initialize(uint max_region_length);272273void clear_candidates();274275void set_candidates(G1CollectionSetCandidates* candidates) {276assert(_candidates == NULL, "Trying to replace collection set candidates.");277_candidates = candidates;278}279G1CollectionSetCandidates* candidates() { return _candidates; }280281void init_region_lengths(uint eden_cset_region_length,282uint survivor_cset_region_length);283284uint region_length() const { return young_region_length() +285old_region_length(); }286uint young_region_length() const { return eden_region_length() +287survivor_region_length(); }288289uint eden_region_length() const { return _eden_region_length; }290uint survivor_region_length() const { return _survivor_region_length; }291uint old_region_length() const { return _old_region_length; }292uint optional_region_length() const { return _num_optional_regions; }293294// Reset the contents of the collection set.295void clear();296297// Incremental collection set support298299// Initialize incremental collection set info.300void start_incremental_building();301// Start a new collection set increment.302void update_incremental_marker() { _inc_build_state = Active; _inc_part_start = _collection_set_cur_length; }303// Stop adding regions to the current collection set increment.304void stop_incremental_building() { _inc_build_state = Inactive; }305306// Iterate over the current collection set increment applying the given HeapRegionClosure307// from a starting position determined by the given worker id.308void iterate_incremental_part_from(HeapRegionClosure* cl, HeapRegionClaimer* hr_claimer, uint worker_id, uint total_workers) const;309310// Returns the length of the current increment in number of regions.311size_t increment_length() const { return _collection_set_cur_length - _inc_part_start; }312// Returns the length of the whole current collection set in number of regions313size_t cur_length() const { return _collection_set_cur_length; }314315// Iterate over the entire collection set (all increments calculated so far), applying316// the given HeapRegionClosure on all of them.317void iterate(HeapRegionClosure* cl) const;318void par_iterate(HeapRegionClosure* cl,319HeapRegionClaimer* hr_claimer,320uint worker_id,321uint total_workers) const;322323void iterate_optional(HeapRegionClosure* cl) const;324325size_t recorded_rs_length() { return _recorded_rs_length; }326327size_t bytes_used_before() const {328return _bytes_used_before;329}330331void reset_bytes_used_before() {332_bytes_used_before = 0;333}334335// Finalize the initial collection set consisting of all young regions potentially a336// few old gen regions.337void finalize_initial_collection_set(double target_pause_time_ms, G1SurvivorRegions* survivor);338// Finalize the next collection set from the set of available optional old gen regions.339bool finalize_optional_for_evacuation(double remaining_pause_time);340// Abandon (clean up) optional collection set regions that were not evacuated in this341// pause.342void abandon_optional_collection_set(G1ParScanThreadStateSet* pss);343344// Update information about hr in the aggregated information for345// the incrementally built collection set.346void update_young_region_prediction(HeapRegion* hr, size_t new_rs_length);347348// Add eden region to the collection set.349void add_eden_region(HeapRegion* hr);350351// Add survivor region to the collection set.352void add_survivor_regions(HeapRegion* hr);353354#ifndef PRODUCT355bool verify_young_ages();356357void print(outputStream* st);358#endif // !PRODUCT359};360361#endif // SHARE_GC_G1_G1COLLECTIONSET_HPP362363364