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   // Mortality rate of a cohort, given its population in
111   // previous and current epochs
112   double mortality_rate(size_t prev_pop, size_t cur_pop);
113 
114   // Update the tenuring threshold, calling
115   // compute_tenuring_threshold to calculate the new
116   // value
117   void update_tenuring_threshold();
118 
119   // This uses the data in the ShenandoahAgeCensus object's _global_age_table and the
120   // current _epoch to compute a new tenuring threshold, which will be remembered
121   // until the next invocation of compute_tenuring_threshold.
122   uint compute_tenuring_threshold();
123 
124  public:
125   enum {
126     MAX_COHORTS = AgeTable::table_size,    // = markWord::max_age + 1
127     MAX_SNAPSHOTS = MAX_COHORTS            // May change in the future
128   };
129 
130   ShenandoahAgeCensus();
131 
132   // Return the local age table (population vector) for worker_id.
133   // Only used in the case of (ShenandoahGenerationalAdaptiveTenuring && !ShenandoahGenerationalCensusAtEvac)
134   AgeTable* get_local_age_table(uint worker_id) {
135     return (AgeTable*) _local_age_table[worker_id];
136   }
137 
138   // Update the local age table for worker_id by size for
139   // given obj_age, region_age, and region_youth
140   CENSUS_NOISE(void add(uint obj_age, uint region_age, uint region_youth, size_t size, uint worker_id);)
141   NO_CENSUS_NOISE(void add(uint obj_age, uint region_age, size_t size, uint worker_id);)
142 
143 #ifdef SHENANDOAH_CENSUS_NOISE
144   // Update the local skip table for worker_id by size
145   void add_skipped(size_t size, uint worker_id);
146   // Update the local aged region volume table for worker_id by size
147   void add_aged(size_t size, uint worker_id);
148   // Update the local clamped object volume table for worker_id by size
149   void add_clamped(size_t size, uint worker_id);
150   // Update the local (rejuvenated) object volume (retrograde age) for worker_id by size
151   void add_young(size_t size, uint worker_id);
152 #endif // SHENANDOAH_CENSUS_NOISE
153 
154   // Update to a new epoch, creating a slot for new census.
155   void prepare_for_census_update();
156 
157   // Update the census data, and compute the new tenuring threshold.
158   // age0_pop is the population of Cohort 0 that may have been missed in
159   // the regular census.
160   void update_census(size_t age0_pop, AgeTable* pv1 = nullptr, AgeTable* pv2 = nullptr);
161 
162   // Return the most recently computed tenuring threshold
163   uint tenuring_threshold() const { return _tenuring_threshold[_epoch]; }
164 
165   // Return the tenuring threshold computed for the previous epoch
166   uint previous_tenuring_threshold() const {
167     assert(_epoch < MAX_SNAPSHOTS, "Error");
168     uint prev = _epoch - 1;
169     if (prev >= MAX_SNAPSHOTS) {
170       // _epoch is 0
171       prev = MAX_SNAPSHOTS - 1;
172     }
173     return _tenuring_threshold[prev];
174   }
175 
176   // Reset the epoch, clearing accumulated census history
177   void reset_global();
178   // Reset any partial census information
179   void reset_local();
180 
181 #ifndef PRODUCT
182   // Check whether census information is clear
183   bool is_clear_global();
184   bool is_clear_local();
185 #endif // !PRODUCT
186 
187   // Print the age census information
188   void print();
189 };
190 
191 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHAGECENSUS_HPP