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 #include "precompiled.hpp"
 25 
 26 #include "gc/shenandoah/shenandoahAgeCensus.hpp"
 27 #include "unittest.hpp"
 28 
 29 class ShenandoahAgeCensusTest : public ::testing::Test {
 30 protected:
 31   static constexpr size_t MinimumPopulationSize = 4*K;
 32   static constexpr size_t InitialPopulationSize = MinimumPopulationSize * 1000;
 33 
 34   size_t _cohorts_count = ShenandoahAgeCensus::MAX_COHORTS;
 35   double _mortality_rates[ShenandoahAgeCensus::MAX_COHORTS];
 36   size_t _cohort_populations[ShenandoahAgeCensus::MAX_COHORTS];
 37 
 38   ShenandoahAgeCensusTest()
 39   : _mortality_rates{0.9, 0.7, 0.5, 0.3, 0.09, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
 40   {
 41     build_cohort_populations(_mortality_rates, _cohort_populations, _cohorts_count);
 42   }
 43 
 44   static void add_population(ShenandoahAgeCensus& census, const uint age, const size_t population_words) {
 45     CENSUS_NOISE(census.add(age, 0, 0, population_words, 0));
 46     NO_CENSUS_NOISE(census.add(age, 0, population_words, 0));
 47   }
 48 
 49   void update(ShenandoahAgeCensus& census, size_t cohorts) const {
 50     for (uint i = 1; i < cohorts; i++) {
 51       add_population(census, i, _cohort_populations[i]);
 52     }
 53     census.update_census(_cohort_populations[0]);
 54   }
 55 
 56   void update(ShenandoahAgeCensus& census) const {
 57     update(census, _cohorts_count);
 58   }
 59 
 60   size_t get_total_population_older_than(const size_t min_cohort_age) const {
 61     size_t total = 0;
 62     for (size_t i = 0; i < _cohorts_count; i++) {
 63       if (i >= min_cohort_age) {
 64         total += _cohort_populations[i];
 65       }
 66     }
 67     return total;
 68   }
 69 
 70   void promote_all_tenurable(const size_t tenuring_threshold) {
 71     for (size_t i = 0; i < _cohorts_count; i++) {
 72       if (i > tenuring_threshold) {
 73         _cohort_populations[i] = 0;
 74       }
 75     }
 76   }
 77 
 78   static void build_cohort_populations(const double mortality_rates[], size_t cohort_populations[], const size_t cohorts) {
 79     cohort_populations[0] = InitialPopulationSize;
 80     for (size_t i = 1; i < cohorts; i++) {
 81       cohort_populations[i] = cohort_populations[i - 1] * (1.0 - mortality_rates[i - 1]);
 82     }
 83   }
 84 };
 85 
 86 TEST_F(ShenandoahAgeCensusTest, initialize) {
 87   const ShenandoahAgeCensus census(1);
 88   EXPECT_EQ(census.tenuring_threshold(), ShenandoahAgeCensus::MAX_COHORTS);
 89 }
 90 
 91 TEST_F(ShenandoahAgeCensusTest, ignore_small_populations) {
 92   // Small populations are ignored so we do not return early before reaching the youngest cohort.
 93   ShenandoahAgeCensus census(1);
 94   add_population(census,1, 32);
 95   add_population(census,1, 32);
 96   census.update_census(64);
 97   EXPECT_EQ(1u, census.tenuring_threshold());
 98 }
 99 
100 TEST_F(ShenandoahAgeCensusTest, find_high_mortality_rate) {
101   ShenandoahAgeCensus census(1);
102 
103   // Initial threshold, no data
104   EXPECT_EQ(16u, census.tenuring_threshold());
105 
106   // Provide population data for 1st cohort. Previous epoch has no population data so our
107   // algorithm skips over all cohorts, leaving tenuring threshold at 1.
108   update(census, 1);
109   EXPECT_EQ(1u, census.tenuring_threshold());
110 
111   // Mortality rate of 1st cohort at age 1 is 0.9, we don't want to promote here. Move threshold to 2.
112   update(census, 2);
113   EXPECT_EQ(2u, census.tenuring_threshold());
114 
115   // Mortality rate of 1st cohort at age 2 is 0.7, we don't want to promote here. Move threshold to 3.
116   update(census, 3);
117   EXPECT_EQ(3u, census.tenuring_threshold());
118 
119   // Mortality rate of 1st cohort at age 3 is 0.5, we don't want to promote here. Move threshold to 4.
120   update(census, 4);
121   EXPECT_EQ(4u, census.tenuring_threshold());
122 
123   // Mortality rate of 1st cohort at age 4 is 0.3, we don't want to promote here. Move threshold to 5.
124   update(census, 5);
125   EXPECT_EQ(5u, census.tenuring_threshold());
126 
127   // Mortality rate of 1st cohort at age 5 is 0.09, this is less than the mortality rate threshold. It
128   // is okay to tenure objects older than 5 now. Keep threshold at 5.
129   update(census, 6);
130   EXPECT_EQ(5u, census.tenuring_threshold());
131 
132   // Mortality rate at this age is 0. Keep tenuring threshold at 5.
133   update(census, 7);
134   EXPECT_EQ(5u, census.tenuring_threshold());
135 }
136 
137 TEST_F(ShenandoahAgeCensusTest, ignore_mortality_caused_by_promotions) {
138   ShenandoahAgeCensus census(1);
139 
140   // Simulate a sequence of censuses with the same mortality rate. Each one will see a
141   // mortality rate above the tenuring threshold and raise the tenuring threshold by one.
142   update(census, 1);
143   update(census, 2);
144   update(census, 3);
145   update(census, 4);
146   update(census, 5);
147 
148   EXPECT_EQ(5u, census.tenuring_threshold());
149 
150   // Simulate the effect of promoting all objects above the tenuring threshold
151   // out of the young generation. This will look like a very high (100%) mortality
152   // rate for these cohorts. However, we do _not_ want to raise the threshold in
153   // this case because these objects haven't really "died", they have just been
154   // tenured.
155   promote_all_tenurable(census.tenuring_threshold());
156   update(census);
157 
158   // We want this to stay at 5 - the mortality in 1st cohort at age 6 was caused by expected promotions.
159   EXPECT_EQ(5u, census.tenuring_threshold());
160 }