1 /*
   2  * Copyright (c) 2001, 2010, Oracle and/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 #include "precompiled.hpp"
  26 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  27 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
  28 #include "gc_implementation/g1/heapRegion.hpp"
  29 #include "gc_implementation/g1/survRateGroup.hpp"
  30 #include "memory/allocation.hpp"
  31 
  32 SurvRateGroup::SurvRateGroup(G1CollectorPolicy* g1p,
  33                              const char* name,
  34                              size_t summary_surv_rates_len) :
  35     _g1p(g1p), _name(name),
  36     _summary_surv_rates_len(summary_surv_rates_len),
  37     _summary_surv_rates_max_len(0),
  38     _summary_surv_rates(NULL),
  39     _surv_rate(NULL),
  40     _accum_surv_rate_pred(NULL),
  41     _surv_rate_pred(NULL)
  42 {
  43   reset();
  44   if (summary_surv_rates_len > 0) {
  45     size_t length = summary_surv_rates_len;
  46       _summary_surv_rates = NEW_C_HEAP_ARRAY(NumberSeq*, length);
  47     if (_summary_surv_rates == NULL) {
  48       vm_exit_out_of_memory(sizeof(NumberSeq*) * length,
  49                             "Not enough space for surv rate summary");
  50     }
  51     for (size_t i = 0; i < length; ++i)
  52       _summary_surv_rates[i] = new NumberSeq();
  53   }
  54 
  55   start_adding_regions();
  56 }
  57 
  58 
  59 void SurvRateGroup::reset()
  60 {
  61   _all_regions_allocated = 0;
  62   _setup_seq_num         = 0;
  63   _stats_arrays_length   = 0;
  64   _accum_surv_rate       = 0.0;
  65   _last_pred             = 0.0;
  66   // the following will set up the arrays with length 1
  67   _region_num            = 1;
  68   stop_adding_regions();
  69   guarantee( _stats_arrays_length == 1, "invariant" );
  70   guarantee( _surv_rate_pred[0] != NULL, "invariant" );
  71   _surv_rate_pred[0]->add(0.4);
  72   all_surviving_words_recorded(false);
  73   _region_num = 0;
  74 }
  75 
  76 
  77 void
  78 SurvRateGroup::start_adding_regions() {
  79   _setup_seq_num   = _stats_arrays_length;
  80   _region_num      = 0;
  81   _accum_surv_rate = 0.0;
  82 
  83 #if 0
  84   gclog_or_tty->print_cr("[%s] start adding regions, seq num %d, length %d",
  85                          _name, _setup_seq_num, _region_num);
  86 #endif // 0
  87 }
  88 
  89 void
  90 SurvRateGroup::stop_adding_regions() {
  91 
  92 #if 0
  93   gclog_or_tty->print_cr("[%s] stop adding regions, length %d", _name, _region_num);
  94 #endif // 0
  95 
  96   if (_region_num > _stats_arrays_length) {
  97     double* old_surv_rate = _surv_rate;
  98     double* old_accum_surv_rate_pred = _accum_surv_rate_pred;
  99     TruncatedSeq** old_surv_rate_pred = _surv_rate_pred;
 100 
 101     _surv_rate = NEW_C_HEAP_ARRAY(double, _region_num);
 102     if (_surv_rate == NULL) {
 103       vm_exit_out_of_memory(sizeof(double) * _region_num,
 104                             "Not enough space for surv rate array.");
 105     }
 106     _accum_surv_rate_pred = NEW_C_HEAP_ARRAY(double, _region_num);
 107     if (_accum_surv_rate_pred == NULL) {
 108       vm_exit_out_of_memory(sizeof(double) * _region_num,
 109                          "Not enough space for accum surv rate pred array.");
 110     }
 111     _surv_rate_pred = NEW_C_HEAP_ARRAY(TruncatedSeq*, _region_num);
 112     if (_surv_rate == NULL) {
 113       vm_exit_out_of_memory(sizeof(TruncatedSeq*) * _region_num,
 114                             "Not enough space for surv rate pred array.");
 115     }
 116 
 117     for (size_t i = 0; i < _stats_arrays_length; ++i)
 118       _surv_rate_pred[i] = old_surv_rate_pred[i];
 119 
 120 #if 0
 121     gclog_or_tty->print_cr("[%s] stop adding regions, new seqs %d to %d",
 122                   _name, _array_length, _region_num - 1);
 123 #endif // 0
 124 
 125     for (size_t i = _stats_arrays_length; i < _region_num; ++i) {
 126       _surv_rate_pred[i] = new TruncatedSeq(10);
 127       // _surv_rate_pred[i]->add(last_pred);
 128     }
 129 
 130     _stats_arrays_length = _region_num;
 131 
 132     if (old_surv_rate != NULL)
 133       FREE_C_HEAP_ARRAY(double, old_surv_rate);
 134     if (old_accum_surv_rate_pred != NULL)
 135       FREE_C_HEAP_ARRAY(double, old_accum_surv_rate_pred);
 136     if (old_surv_rate_pred != NULL)
 137       FREE_C_HEAP_ARRAY(NumberSeq*, old_surv_rate_pred);
 138   }
 139 
 140   for (size_t i = 0; i < _stats_arrays_length; ++i)
 141     _surv_rate[i] = 0.0;
 142 }
 143 
 144 double
 145 SurvRateGroup::accum_surv_rate(size_t adjustment) {
 146   // we might relax this one in the future...
 147   guarantee( adjustment == 0 || adjustment == 1, "pre-condition" );
 148 
 149   double ret = _accum_surv_rate;
 150   if (adjustment > 0) {
 151     TruncatedSeq* seq = get_seq(_region_num+1);
 152     double surv_rate = _g1p->get_new_prediction(seq);
 153     ret += surv_rate;
 154   }
 155 
 156   return ret;
 157 }
 158 
 159 int
 160 SurvRateGroup::next_age_index() {
 161   TruncatedSeq* seq = get_seq(_region_num);
 162   double surv_rate = _g1p->get_new_prediction(seq);
 163   _accum_surv_rate += surv_rate;
 164 
 165   ++_region_num;
 166   return (int) ++_all_regions_allocated;
 167 }
 168 
 169 void
 170 SurvRateGroup::record_surviving_words(int age_in_group, size_t surv_words) {
 171   guarantee( 0 <= age_in_group && (size_t) age_in_group < _region_num,
 172              "pre-condition" );
 173   guarantee( _surv_rate[age_in_group] <= 0.00001,
 174              "should only update each slot once" );
 175 
 176   double surv_rate = (double) surv_words / (double) HeapRegion::GrainWords;
 177   _surv_rate[age_in_group] = surv_rate;
 178   _surv_rate_pred[age_in_group]->add(surv_rate);
 179   if ((size_t)age_in_group < _summary_surv_rates_len) {
 180     _summary_surv_rates[age_in_group]->add(surv_rate);
 181     if ((size_t)(age_in_group+1) > _summary_surv_rates_max_len)
 182       _summary_surv_rates_max_len = age_in_group+1;
 183   }
 184 }
 185 
 186 void
 187 SurvRateGroup::all_surviving_words_recorded(bool propagate) {
 188   if (propagate && _region_num > 0) { // conservative
 189     double surv_rate = _surv_rate_pred[_region_num-1]->last();
 190 
 191 #if 0
 192     gclog_or_tty->print_cr("propagating %1.2lf from %d to %d",
 193                   surv_rate, _curr_length, _array_length - 1);
 194 #endif // 0
 195 
 196     for (size_t i = _region_num; i < _stats_arrays_length; ++i) {
 197       guarantee( _surv_rate[i] <= 0.00001,
 198                  "the slot should not have been updated" );
 199       _surv_rate_pred[i]->add(surv_rate);
 200     }
 201   }
 202 
 203   double accum = 0.0;
 204   double pred = 0.0;
 205   for (size_t i = 0; i < _stats_arrays_length; ++i) {
 206     pred = _g1p->get_new_prediction(_surv_rate_pred[i]);
 207     if (pred > 1.0) pred = 1.0;
 208     accum += pred;
 209     _accum_surv_rate_pred[i] = accum;
 210     // gclog_or_tty->print_cr("age %3d, accum %10.2lf", i, accum);
 211   }
 212   _last_pred = pred;
 213 }
 214 
 215 #ifndef PRODUCT
 216 void
 217 SurvRateGroup::print() {
 218   gclog_or_tty->print_cr("Surv Rate Group: %s (%d entries)",
 219                 _name, _region_num);
 220   for (size_t i = 0; i < _region_num; ++i) {
 221     gclog_or_tty->print_cr("    age %4d   surv rate %6.2lf %%   pred %6.2lf %%",
 222                   i, _surv_rate[i] * 100.0,
 223                   _g1p->get_new_prediction(_surv_rate_pred[i]) * 100.0);
 224   }
 225 }
 226 
 227 void
 228 SurvRateGroup::print_surv_rate_summary() {
 229   size_t length = _summary_surv_rates_max_len;
 230   if (length == 0)
 231     return;
 232 
 233   gclog_or_tty->print_cr("");
 234   gclog_or_tty->print_cr("%s Rate Summary (for up to age %d)", _name, length-1);
 235   gclog_or_tty->print_cr("      age range     survival rate (avg)      samples (avg)");
 236   gclog_or_tty->print_cr("  ---------------------------------------------------------");
 237 
 238   size_t index = 0;
 239   size_t limit = MIN2((int) length, 10);
 240   while (index < limit) {
 241     gclog_or_tty->print_cr("           %4d                 %6.2lf%%             %6.2lf",
 242                   index, _summary_surv_rates[index]->avg() * 100.0,
 243                   (double) _summary_surv_rates[index]->num());
 244     ++index;
 245   }
 246 
 247   gclog_or_tty->print_cr("  ---------------------------------------------------------");
 248 
 249   int num = 0;
 250   double sum = 0.0;
 251   int samples = 0;
 252   while (index < length) {
 253     ++num;
 254     sum += _summary_surv_rates[index]->avg() * 100.0;
 255     samples += _summary_surv_rates[index]->num();
 256     ++index;
 257 
 258     if (index == length || num % 10 == 0) {
 259       gclog_or_tty->print_cr("   %4d .. %4d                 %6.2lf%%             %6.2lf",
 260                     (index-1) / 10 * 10, index-1, sum / (double) num,
 261                     (double) samples / (double) num);
 262       sum = 0.0;
 263       num = 0;
 264       samples = 0;
 265     }
 266   }
 267 
 268   gclog_or_tty->print_cr("  ---------------------------------------------------------");
 269 }
 270 #endif // PRODUCT