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 
105   size_t _skipped;                   // net size of objects encountered, but skipped during census,
106                                      // because their age was indeterminate
107 #endif // SHENANDOAH_CENSUS_NOISE
108 
109 #ifndef PRODUCT
110   size_t _counted;                   // net size of objects counted in census
111   size_t _total;                     // net size of objects encountered (counted or skipped) in census
112 #endif
113 
114   uint _epoch;                       // Current epoch (modulo max age)
115   uint *_tenuring_threshold;         // An array of the last N tenuring threshold values we
116                                      // computed.
117 
118   // Mortality rate of a cohort, given its population in
119   // previous and current epochs
120   double mortality_rate(size_t prev_pop, size_t cur_pop);
121 
122   // Update to a new epoch, creating a slot for new census.
123   void prepare_for_census_update();
124 
125   // Update the tenuring threshold, calling
126   // compute_tenuring_threshold() to calculate the new
127   // value
128   void update_tenuring_threshold();
129 
130   // Use _global_age_table and the current _epoch to compute a new tenuring
131   // threshold, which will be remembered until the next invocation of
132   // compute_tenuring_threshold.
133   uint compute_tenuring_threshold();
134 
135   // Return the tenuring threshold computed for the previous epoch
136   uint previous_tenuring_threshold() const {
137     assert(_epoch < MAX_SNAPSHOTS, "Error");
138     uint prev = _epoch - 1;
139     if (prev >= MAX_SNAPSHOTS) {
140       // _epoch is 0
141       assert(_epoch == 0, "Error");
142       prev = MAX_SNAPSHOTS - 1;
143     }
144     return _tenuring_threshold[prev];
145   }
146 
147 #ifndef PRODUCT
148   // Return the sum of size of objects of all ages recorded in the
149   // census at snapshot indexed by snap.
150   size_t get_all_ages(uint snap);
151 
152   // Return the size of all objects that were encountered, but skipped,
153   // during the census, because their age was indeterminate.
154   size_t get_skipped(uint snap);
155 
156   // Update the total size of objects counted or skipped at the census for
157   // the most recent epoch.
158   void update_total();
159 #endif // !PRODUCT
160 
161  public:
162   enum {
163     MAX_COHORTS = AgeTable::table_size,    // = markWord::max_age + 1
164     MAX_SNAPSHOTS = MAX_COHORTS            // May change in the future
165   };
166 
167   ShenandoahAgeCensus();
168 
169   // Return the local age table (population vector) for worker_id.
170   // Only used in the case of (ShenandoahGenerationalAdaptiveTenuring && !ShenandoahGenerationalCensusAtEvac)
171   AgeTable* get_local_age_table(uint worker_id) {
172     return (AgeTable*) _local_age_table[worker_id];
173   }
174 
175   // Update the local age table for worker_id by size for
176   // given obj_age, region_age, and region_youth
177   CENSUS_NOISE(void add(uint obj_age, uint region_age, uint region_youth, size_t size, uint worker_id);)
178   NO_CENSUS_NOISE(void add(uint obj_age, uint region_age, size_t size, uint worker_id);)
179 
180 #ifdef SHENANDOAH_CENSUS_NOISE
181   // Update the local skip table for worker_id by size
182   void add_skipped(size_t size, uint worker_id);
183   // Update the local aged region volume table for worker_id by size
184   void add_aged(size_t size, uint worker_id);
185   // Update the local clamped object volume table for worker_id by size
186   void add_clamped(size_t size, uint worker_id);
187   // Update the local (rejuvenated) object volume (retrograde age) for worker_id by size
188   void add_young(size_t size, uint worker_id);
189 #endif // SHENANDOAH_CENSUS_NOISE
190 
191   // Update the census data, and compute the new tenuring threshold.
192   // This method should be called at the end of each marking (or optionally
193   // evacuation) cycle to update the tenuring threshold to be used in
194   // the next cycle.
195   // age0_pop is the population of Cohort 0 that may have been missed in
196   // the regular census during the marking cycle, corresponding to objects
197   // allocated when the concurrent marking was in progress.
198   // Optional parameters, pv1 and pv2 are population vectors that together
199   // provide object census data (only) for the case when
200   // ShenandoahGenerationalCensusAtEvac. In this case, the age0_pop
201   // is 0, because the evacuated objects have all had their ages incremented.
202   void update_census(size_t age0_pop, AgeTable* pv1 = nullptr, AgeTable* pv2 = nullptr);
203 
204   // Return the most recently computed tenuring threshold
205   uint tenuring_threshold() const { return _tenuring_threshold[_epoch]; }
206 
207   // Reset the epoch, clearing accumulated census history
208   // Note: this isn't currently used, but reserved for planned
209   // future usage.
210   void reset_global();
211 
212   // Reset any (potentially partial) census information in worker-local age tables
213   void reset_local();
214 
215 #ifndef PRODUCT
216   // Check whether census information is clear
217   bool is_clear_global();
218   bool is_clear_local();
219 
220   // Return the net size of objects encountered (counted or skipped) in census
221   // at most recent epoch.
222   size_t get_total() { return _total; }
223 #endif // !PRODUCT
224 
225   // Print the age census information
226   void print();
227 };
228 
229 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHAGECENSUS_HPP