hotspot/src/share/vm/gc_implementation/g1/survRateGroup.cpp
author tonyp
Tue, 24 Aug 2010 17:24:33 -0400
changeset 7398 e4aa6d9bda09
parent 7397 5b173b4ca846
child 12117 23b52cd6ca73
permissions -rw-r--r--
6974966: G1: unnecessary direct-to-old allocations Summary: This change revamps the slow allocation path of G1. Improvements include the following: a) Allocations directly to old regions are now totally banned. G1 now only allows allocations out of young regions (with the only exception being humongous regions). b) The thread that allocates a new region (which is now guaranteed to be young) does not dirty all its cards. Each thread that successfully allocates out of a young region is now responsible for dirtying the cards that corresponding to the "block" that just got allocated. c) allocate_new_tlab() and mem_allocate() are now implemented differently and TLAB allocations are only done by allocate_new_tlab(). d) If a thread schedules an evacuation pause in order to satisfy an allocation request, it will perform the allocation at the end of the safepoint so that the thread that initiated the GC also gets "first pick" of any space made available by the GC. e) If a thread is unable to allocate a humongous object it will schedule an evacuation pause in case it reclaims enough regions so that the humongous allocation can be satisfied aftewards. f) The G1 policy is more careful to set the young list target length to be the survivor number +1. g) Lots of code tidy up, removal, refactoring to make future changes easier. Reviewed-by: johnc, ysr

/*
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
 * 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.
 *
 * 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.
 *
 */

#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"

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),
    _summary_surv_rates(NULL),
    _surv_rate(NULL),
    _accum_surv_rate_pred(NULL),
    _surv_rate_pred(NULL)
{
  reset();
  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();
}


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;
}


void
SurvRateGroup::start_adding_regions() {
  _setup_seq_num   = _stats_arrays_length;
  _region_num      = 0;
  _accum_surv_rate = 0.0;

#if 0
  gclog_or_tty->print_cr("[%s] start adding regions, seq num %d, length %d",
                         _name, _setup_seq_num, _region_num);
#endif // 0
}

void
SurvRateGroup::stop_adding_regions() {

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

  if (_region_num > _stats_arrays_length) {
    double* old_surv_rate = _surv_rate;
    double* old_accum_surv_rate_pred = _accum_surv_rate_pred;
    TruncatedSeq** old_surv_rate_pred = _surv_rate_pred;

    _surv_rate = NEW_C_HEAP_ARRAY(double, _region_num);
    if (_surv_rate == NULL) {
      vm_exit_out_of_memory(sizeof(double) * _region_num,
                            "Not enough space for surv rate array.");
    }
    _accum_surv_rate_pred = NEW_C_HEAP_ARRAY(double, _region_num);
    if (_accum_surv_rate_pred == NULL) {
      vm_exit_out_of_memory(sizeof(double) * _region_num,
                         "Not enough space for accum surv rate pred array.");
    }
    _surv_rate_pred = NEW_C_HEAP_ARRAY(TruncatedSeq*, _region_num);
    if (_surv_rate == NULL) {
      vm_exit_out_of_memory(sizeof(TruncatedSeq*) * _region_num,
                            "Not enough space for surv rate pred array.");
    }

    for (size_t i = 0; i < _stats_arrays_length; ++i)
      _surv_rate_pred[i] = old_surv_rate_pred[i];

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

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

    _stats_arrays_length = _region_num;

    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);
  }

  for (size_t i = 0; i < _stats_arrays_length; ++i)
    _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) {
    TruncatedSeq* seq = get_seq(_region_num+1);
    double surv_rate = _g1p->get_new_prediction(seq);
    ret += surv_rate;
  }

  return ret;
}

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

  ++_region_num;
  return (int) ++_all_regions_allocated;
}

void
SurvRateGroup::record_surviving_words(int age_in_group, size_t surv_words) {
  guarantee( 0 <= age_in_group && (size_t) age_in_group < _region_num,
             "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) {
  if (propagate && _region_num > 0) { // conservative
    double surv_rate = _surv_rate_pred[_region_num-1]->last();

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

    for (size_t i = _region_num; i < _stats_arrays_length; ++i) {
      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;
  for (size_t i = 0; i < _stats_arrays_length; ++i) {
    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() {
  gclog_or_tty->print_cr("Surv Rate Group: %s (%d entries)",
                _name, _region_num);
  for (size_t i = 0; i < _region_num; ++i) {
    gclog_or_tty->print_cr("    age %4d   surv rate %6.2lf %%   pred %6.2lf %%",
                  i, _surv_rate[i] * 100.0,
                  _g1p->get_new_prediction(_surv_rate_pred[i]) * 100.0);
  }
}

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