survRateGroup.cpp 8.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25 26 27 28 29 30
#include "precompiled.hpp"
#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
#include "gc_implementation/g1/g1CollectorPolicy.hpp"
#include "gc_implementation/g1/heapRegion.hpp"
#include "gc_implementation/g1/survRateGroup.hpp"
#include "memory/allocation.hpp"
31 32 33 34 35 36 37

SurvRateGroup::SurvRateGroup(G1CollectorPolicy* g1p,
                             const char* name,
                             size_t summary_surv_rates_len) :
    _g1p(g1p), _name(name),
    _summary_surv_rates_len(summary_surv_rates_len),
    _summary_surv_rates_max_len(0),
38 39 40 41 42 43
    _summary_surv_rates(NULL),
    _surv_rate(NULL),
    _accum_surv_rate_pred(NULL),
    _surv_rate_pred(NULL)
{
  reset();
44 45 46 47 48 49 50 51 52 53 54 55 56 57
  if (summary_surv_rates_len > 0) {
    size_t length = summary_surv_rates_len;
      _summary_surv_rates = NEW_C_HEAP_ARRAY(NumberSeq*, length);
    if (_summary_surv_rates == NULL) {
      vm_exit_out_of_memory(sizeof(NumberSeq*) * length,
                            "Not enough space for surv rate summary");
    }
    for (size_t i = 0; i < length; ++i)
      _summary_surv_rates[i] = new NumberSeq();
  }

  start_adding_regions();
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

void SurvRateGroup::reset()
{
  _all_regions_allocated = 0;
  _setup_seq_num         = 0;
  _stats_arrays_length   = 0;
  _accum_surv_rate       = 0.0;
  _last_pred             = 0.0;
  // the following will set up the arrays with length 1
  _region_num            = 1;
  stop_adding_regions();
  guarantee( _stats_arrays_length == 1, "invariant" );
  guarantee( _surv_rate_pred[0] != NULL, "invariant" );
  _surv_rate_pred[0]->add(0.4);
  all_surviving_words_recorded(false);
  _region_num = 0;
}


77 78
void
SurvRateGroup::start_adding_regions() {
79
  _setup_seq_num   = _stats_arrays_length;
80
  _region_num      = 0;
81 82 83
  _accum_surv_rate = 0.0;

#if 0
84 85
  gclog_or_tty->print_cr("[%s] start adding regions, seq num %d, length %d",
                         _name, _setup_seq_num, _region_num);
86 87 88 89 90 91 92
#endif // 0
}

void
SurvRateGroup::stop_adding_regions() {

#if 0
93
  gclog_or_tty->print_cr("[%s] stop adding regions, length %d", _name, _region_num);
94 95
#endif // 0

96
  if (_region_num > _stats_arrays_length) {
97 98 99 100
    double* old_surv_rate = _surv_rate;
    double* old_accum_surv_rate_pred = _accum_surv_rate_pred;
    TruncatedSeq** old_surv_rate_pred = _surv_rate_pred;

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 105
                            "Not enough space for surv rate array.");
    }
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 110
                         "Not enough space for accum surv rate pred array.");
    }
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 115 116
                            "Not enough space for surv rate pred array.");
    }

117
    for (size_t i = 0; i < _stats_arrays_length; ++i)
118 119 120
      _surv_rate_pred[i] = old_surv_rate_pred[i];

#if 0
121 122
    gclog_or_tty->print_cr("[%s] stop adding regions, new seqs %d to %d",
                  _name, _array_length, _region_num - 1);
123 124
#endif // 0

125
    for (size_t i = _stats_arrays_length; i < _region_num; ++i) {
126 127 128 129
      _surv_rate_pred[i] = new TruncatedSeq(10);
      // _surv_rate_pred[i]->add(last_pred);
    }

130
    _stats_arrays_length = _region_num;
131 132 133 134 135 136 137 138 139

    if (old_surv_rate != NULL)
      FREE_C_HEAP_ARRAY(double, old_surv_rate);
    if (old_accum_surv_rate_pred != NULL)
      FREE_C_HEAP_ARRAY(double, old_accum_surv_rate_pred);
    if (old_surv_rate_pred != NULL)
      FREE_C_HEAP_ARRAY(NumberSeq*, old_surv_rate_pred);
  }

140
  for (size_t i = 0; i < _stats_arrays_length; ++i)
141 142 143 144 145 146 147 148 149 150
    _surv_rate[i] = 0.0;
}

double
SurvRateGroup::accum_surv_rate(size_t adjustment) {
  // we might relax this one in the future...
  guarantee( adjustment == 0 || adjustment == 1, "pre-condition" );

  double ret = _accum_surv_rate;
  if (adjustment > 0) {
151
    TruncatedSeq* seq = get_seq(_region_num+1);
152 153 154 155 156 157 158 159 160
    double surv_rate = _g1p->get_new_prediction(seq);
    ret += surv_rate;
  }

  return ret;
}

int
SurvRateGroup::next_age_index() {
161
  TruncatedSeq* seq = get_seq(_region_num);
162 163 164
  double surv_rate = _g1p->get_new_prediction(seq);
  _accum_surv_rate += surv_rate;

165
  ++_region_num;
166 167 168 169 170
  return (int) ++_all_regions_allocated;
}

void
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
             "pre-condition" );
  guarantee( _surv_rate[age_in_group] <= 0.00001,
             "should only update each slot once" );

  double surv_rate = (double) surv_words / (double) HeapRegion::GrainWords;
  _surv_rate[age_in_group] = surv_rate;
  _surv_rate_pred[age_in_group]->add(surv_rate);
  if ((size_t)age_in_group < _summary_surv_rates_len) {
    _summary_surv_rates[age_in_group]->add(surv_rate);
    if ((size_t)(age_in_group+1) > _summary_surv_rates_max_len)
      _summary_surv_rates_max_len = age_in_group+1;
  }
}

void
SurvRateGroup::all_surviving_words_recorded(bool propagate) {
188 189
  if (propagate && _region_num > 0) { // conservative
    double surv_rate = _surv_rate_pred[_region_num-1]->last();
190 191 192 193 194 195

#if 0
    gclog_or_tty->print_cr("propagating %1.2lf from %d to %d",
                  surv_rate, _curr_length, _array_length - 1);
#endif // 0

196
    for (size_t i = _region_num; i < _stats_arrays_length; ++i) {
197 198 199 200 201 202 203 204
      guarantee( _surv_rate[i] <= 0.00001,
                 "the slot should not have been updated" );
      _surv_rate_pred[i]->add(surv_rate);
    }
  }

  double accum = 0.0;
  double pred = 0.0;
205
  for (size_t i = 0; i < _stats_arrays_length; ++i) {
206 207 208 209 210 211 212 213 214 215 216 217
    pred = _g1p->get_new_prediction(_surv_rate_pred[i]);
    if (pred > 1.0) pred = 1.0;
    accum += pred;
    _accum_surv_rate_pred[i] = accum;
    // gclog_or_tty->print_cr("age %3d, accum %10.2lf", i, accum);
  }
  _last_pred = pred;
}

#ifndef PRODUCT
void
SurvRateGroup::print() {
218 219
  gclog_or_tty->print_cr("Surv Rate Group: %s (%d entries)",
                _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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  }
}

void
SurvRateGroup::print_surv_rate_summary() {
  size_t length = _summary_surv_rates_max_len;
  if (length == 0)
    return;

  gclog_or_tty->print_cr("");
  gclog_or_tty->print_cr("%s Rate Summary (for up to age %d)", _name, length-1);
  gclog_or_tty->print_cr("      age range     survival rate (avg)      samples (avg)");
  gclog_or_tty->print_cr("  ---------------------------------------------------------");

  size_t index = 0;
  size_t limit = MIN2((int) length, 10);
  while (index < limit) {
    gclog_or_tty->print_cr("           %4d                 %6.2lf%%             %6.2lf",
                  index, _summary_surv_rates[index]->avg() * 100.0,
                  (double) _summary_surv_rates[index]->num());
    ++index;
  }

  gclog_or_tty->print_cr("  ---------------------------------------------------------");

  int num = 0;
  double sum = 0.0;
  int samples = 0;
  while (index < length) {
    ++num;
    sum += _summary_surv_rates[index]->avg() * 100.0;
    samples += _summary_surv_rates[index]->num();
    ++index;

    if (index == length || num % 10 == 0) {
      gclog_or_tty->print_cr("   %4d .. %4d                 %6.2lf%%             %6.2lf",
                    (index-1) / 10 * 10, index-1, sum / (double) num,
                    (double) samples / (double) num);
      sum = 0.0;
      num = 0;
      samples = 0;
    }
  }

  gclog_or_tty->print_cr("  ---------------------------------------------------------");
}
#endif // PRODUCT