1 /* 2 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHAGECENSUS_HPP 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHAGECENSUS_HPP 27 28 #include "gc/shared/ageTable.hpp" 29 30 #ifndef PRODUCT 31 // Enable noise instrumentation 32 #define SHENANDOAH_CENSUS_NOISE 1 33 #endif // PRODUCT 34 35 #ifdef SHENANDOAH_CENSUS_NOISE 36 37 #define CENSUS_NOISE(x) x 38 #define NO_CENSUS_NOISE(x) 39 40 struct ShenandoahNoiseStats { 41 size_t skipped; // Volume of objects skipped 42 size_t aged; // Volume of objects from aged regions 43 size_t clamped; // Volume of objects whose ages were clamped 44 size_t young; // Volume of (rejuvenated) objects of retrograde age 45 46 ShenandoahNoiseStats() { 47 clear(); 48 } 49 50 void clear() { 51 skipped = 0; 52 aged = 0; 53 clamped = 0; 54 young = 0; 55 } 56 57 #ifndef PRODUCT 58 bool is_clear() { 59 return (skipped + aged + clamped + young) == 0; 60 } 61 #endif // !PRODUCT 62 63 void merge(ShenandoahNoiseStats& other) { 64 skipped += other.skipped; 65 aged += other.aged; 66 clamped += other.clamped; 67 young += other.young; 68 } 69 70 void print(size_t total); 71 }; 72 #else // SHENANDOAH_CENSUS_NOISE 73 #define CENSUS_NOISE(x) 74 #define NO_CENSUS_NOISE(x) x 75 #endif // SHENANDOAH_CENSUS_NOISE 76 77 // A class for tracking a sequence of cohort population vectors (or, 78 // interchangeably, age tables) for up to C=MAX_COHORTS age cohorts, where a cohort 79 // represents the set of objects allocated during a specific inter-GC epoch. 80 // Epochs are demarcated by GC cycles, with those surviving a cycle aging by 81 // an epoch. The census tracks the historical variation of cohort demographics 82 // across N=MAX_SNAPSHOTS recent epochs. Since there are at most C age cohorts in 83 // the population, we need only track at most N=C epochal snapshots to track a 84 // maximal longitudinal demographics of every object's longitudinal cohort in 85 // the young generation. The _global_age_table is thus, currently, a C x N (row-major) 86 // matrix, with C=16, and, for now N=C=16, currently. 87 // In theory, we might decide to track even longer (N=MAX_SNAPSHOTS) demographic 88 // histories, but that isn't the case today. In particular, the current tenuring 89 // threshold algorithm uses only 2 most recent snapshots, with the remaining 90 // MAX_SNAPSHOTS-2=14 reserved for research purposes. 91 // 92 // In addition, this class also maintains per worker population vectors into which 93 // census for the current minor GC is accumulated (during marking or, optionally, during 94 // evacuation). These are cleared after each marking (resectively, evacuation) cycle, 95 // once the per-worker data is consolidated into the appropriate population vector 96 // per minor collection. The _local_age_table is thus C x N, for N GC workers. 97 class ShenandoahAgeCensus: public CHeapObj<mtGC> { 98 AgeTable** _global_age_table; // Global age table used for adapting tenuring threshold, one per snapshot 99 AgeTable** _local_age_table; // Local scratch age tables to track object ages, one per worker 100 101 #ifdef SHENANDOAH_CENSUS_NOISE 102 ShenandoahNoiseStats* _global_noise; // Noise stats, one per snapshot 103 ShenandoahNoiseStats* _local_noise; // Local scratch table for noise stats, one per worker 104 #endif // SHENANDOAH_CENSUS_NOISE 105 106 uint _epoch; // Current epoch (modulo max age) 107 uint *_tenuring_threshold; // An array of the last N tenuring threshold values we 108 // computed. 109 110 // A private work method invoked by the public compute_tenuring_threshold() method. 111 // This uses the data in the ShenandoahAgeCensus object's _global_age_table and the 112 // current _epoch to compute a new tenuring threshold, which will be remembered 113 // until the next invocation of compute_tenuring_threshold. 114 uint compute_tenuring_threshold_work(); 115 116 // Mortality rate of a cohort, given its population in 117 // previous and current epochs 118 double mortality_rate(size_t prev_pop, size_t cur_pop); 119 120 // Update the tenuring threshold, calling 121 // compute_tenuring_threshold to calculate the new 122 // value 123 void update_tenuring_threshold(); 124 uint compute_tenuring_threshold(); 125 126 public: 127 enum { 128 MAX_COHORTS = AgeTable::table_size, // = markWord::max_age + 1 129 MAX_SNAPSHOTS = MAX_COHORTS // May change in the future 130 }; 131 132 ShenandoahAgeCensus(); 133 134 // Return the local age table (population vector) for worker_id. 135 // Only used in the case of (ShenandoahGenerationalAdaptiveTenuring && !ShenandoahGenerationalCensusAtEvac) 136 AgeTable* get_local_age_table(uint worker_id) { 137 return (AgeTable*) _local_age_table[worker_id]; 138 } 139 140 // Update the local age table for worker_id by size for 141 // given obj_age, region_age, and region_youth 142 CENSUS_NOISE(void add(uint obj_age, uint region_age, uint region_youth, size_t size, uint worker_id);) 143 NO_CENSUS_NOISE(void add(uint obj_age, uint region_age, size_t size, uint worker_id);) 144 145 #ifdef SHENANDOAH_CENSUS_NOISE 146 // Update the local skip table for worker_id by size 147 void add_skipped(size_t size, uint worker_id); 148 // Update the local aged region volume table for worker_id by size 149 void add_aged(size_t size, uint worker_id); 150 // Update the local clamped object volume table for worker_id by size 151 void add_clamped(size_t size, uint worker_id); 152 // Update the local (rejuvenated) object volume (retrograde age) for worker_id by size 153 void add_young(size_t size, uint worker_id); 154 #endif // SHENANDOAH_CENSUS_NOISE 155 156 // Update to a new epoch, creating a slot for new census. 157 void prepare_for_census_update(); 158 159 // Update the census data, and compute the new tenuring threshold. 160 // age0_pop is the population of Cohort 0 that may have been missed in 161 // the regular census. 162 void update_census(size_t age0_pop, AgeTable* pv1 = nullptr, AgeTable* pv2 = nullptr); 163 164 // Return the most recently computed tenuring threshold 165 uint tenuring_threshold() const { return _tenuring_threshold[_epoch]; } 166 167 // Return the tenuring threshold computed for the previous epoch 168 uint previous_tenuring_threshold() const { 169 assert(_epoch < MAX_SNAPSHOTS, "Error"); 170 uint prev = _epoch - 1; 171 if (prev >= MAX_SNAPSHOTS) { 172 // _epoch is 0 173 prev = MAX_SNAPSHOTS - 1; 174 } 175 return _tenuring_threshold[prev]; 176 } 177 178 // Reset the epoch, clearing accumulated census history 179 void reset_global(); 180 // Reset any partial census information 181 void reset_local(); 182 183 // Check whether census information is clear 184 bool is_clear_global(); 185 bool is_clear_local(); 186 187 // Print the age census information 188 void print(); 189 }; 190 191 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHAGECENSUS_HPP