g1EvacStats.cpp revision 9056:dc9930a04ab0
12926Sphk/*
22926Sphk * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
32926Sphk * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
493150Sphk *
52926Sphk * This code is free software; you can redistribute it and/or modify it
62926Sphk * under the terms of the GNU General Public License version 2 only, as
72926Sphk * published by the Free Software Foundation.
82926Sphk *
950479Speter * This code is distributed in the hope that it will be useful, but WITHOUT
102926Sphk * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
112926Sphk * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
122926Sphk * version 2 for more details (a copy is included in the LICENSE file that
132886Sphk * accompanied this code).
142926Sphk *
152886Sphk * You should have received a copy of the GNU General Public License version
162886Sphk * 2 along with this work; if not, write to the Free Software Foundation,
174816Sphk * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
182886Sphk *
192886Sphk * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202886Sphk * or visit www.oracle.com if you need additional information or have any
2115456Sphk * questions.
2215456Sphk *
2315456Sphk */
2415456Sphk
2529526Scharnier#include "precompiled.hpp"
2615456Sphk#include "gc/g1/g1EvacStats.hpp"
2715456Sphk#include "gc/shared/gcId.hpp"
2815456Sphk#include "trace/tracing.hpp"
2915456Sphk
3015456Sphkvoid G1EvacStats::adjust_desired_plab_sz() {
3115456Sphk  if (PrintPLAB) {
322886Sphk    gclog_or_tty->print(" (allocated = " SIZE_FORMAT " wasted = " SIZE_FORMAT " "
332886Sphk                        "unused = " SIZE_FORMAT " used = " SIZE_FORMAT " "
342886Sphk                        "undo_waste = " SIZE_FORMAT " region_end_waste = " SIZE_FORMAT " "
352886Sphk                        "regions filled = %u direct_allocated = " SIZE_FORMAT " "
362886Sphk                        "failure_used = " SIZE_FORMAT " failure_waste = " SIZE_FORMAT ") ",
372886Sphk                        _allocated, _wasted, _unused, used(), _undo_wasted, _region_end_waste,
382886Sphk                        _regions_filled, _direct_allocated, _failure_used, _failure_waste);
392886Sphk  }
402886Sphk
419491Sphk  if (ResizePLAB) {
4217946Sphk
4315456Sphk    assert(is_object_aligned(max_size()) && min_size() <= max_size(),
4417946Sphk           "PLAB clipping computation may be incorrect");
458857Srgrimes
462886Sphk    if (_allocated == 0) {
472886Sphk      assert((_unused == 0),
482886Sphk             "Inconsistency in PLAB stats: "
492886Sphk             "_allocated: " SIZE_FORMAT ", "
502886Sphk             "_wasted: " SIZE_FORMAT ", "
512886Sphk             "_region_end_waste: " SIZE_FORMAT ", "
522886Sphk             "_unused: " SIZE_FORMAT ", "
532886Sphk             "_used  : " SIZE_FORMAT,
542886Sphk             _allocated, _wasted, _region_end_waste, _unused, used());
552886Sphk      _allocated = 1;
5615456Sphk    }
5715456Sphk    // The size of the PLAB caps the amount of space that can be wasted at the
5815456Sphk    // end of the collection. In the worst case the last PLAB could be completely
5915456Sphk    // empty.
6015456Sphk    // This allows us to calculate the new PLAB size to achieve the
6115456Sphk    // TargetPLABWastePct given the latest memory usage and that the last buffer
6215456Sphk    // will be G1LastPLABAverageOccupancy full.
6315456Sphk    //
6415456Sphk    // E.g. assume that if in the current GC 100 words were allocated and a
6515456Sphk    // TargetPLABWastePct of 10 had been set.
6615456Sphk    //
6715456Sphk    // So we could waste up to 10 words to meet that percentage. Given that we
6815456Sphk    // also assume that that buffer is typically half-full, the new desired PLAB
6915456Sphk    // size is set to 20 words.
7015456Sphk    //
7115456Sphk    // The amount of allocation performed should be independent of the number of
7215456Sphk    // threads, so should the maximum waste we can spend in total. So if
7315456Sphk    // we used n threads to allocate, each of them can spend maximum waste/n words in
7415456Sphk    // a first rough approximation. The number of threads only comes into play later
7515456Sphk    // when actually retrieving the actual desired PLAB size.
7615456Sphk    //
7715456Sphk    // After calculating this optimal PLAB size the algorithm applies the usual
7815456Sphk    // exponential decaying average over this value to guess the next PLAB size.
7915456Sphk    //
8015456Sphk    // We account region end waste fully to PLAB allocation (in the calculation of
8115456Sphk    // what we consider as "used_for_waste_calculation" below). This is not
8215456Sphk    // completely fair, but is a conservative assumption because PLABs may be sized
8315456Sphk    // flexibly while we cannot adjust inline allocations.
8415456Sphk    // Allocation during GC will try to minimize region end waste so this impact
8515456Sphk    // should be minimal.
8615456Sphk    //
8715456Sphk    // We need to cover overflow when calculating the amount of space actually used
8815456Sphk    // by objects in PLABs when subtracting the region end waste.
8915456Sphk    // Region end waste may be higher than actual allocation. This may occur if many
9015456Sphk    // threads do not allocate anything but a few rather large objects. In this
9115456Sphk    // degenerate case the PLAB size would simply quickly tend to minimum PLAB size,
9215456Sphk    // which is an okay reaction.
9315456Sphk    size_t const used_for_waste_calculation = used() > _region_end_waste ? used() - _region_end_waste : 0;
9415456Sphk
9515456Sphk    size_t const total_waste_allowed = used_for_waste_calculation * TargetPLABWastePct;
9615456Sphk    size_t const cur_plab_sz = (size_t)((double)total_waste_allowed / G1LastPLABAverageOccupancy);
9715456Sphk    // Take historical weighted average
9815456Sphk    _filter.sample(cur_plab_sz);
9915456Sphk    // Clip from above and below, and align to object boundary
10015456Sphk    size_t plab_sz;
10115456Sphk    plab_sz = MAX2(min_size(), (size_t)_filter.average());
10215456Sphk    plab_sz = MIN2(max_size(), plab_sz);
10315456Sphk    plab_sz = align_object_size(plab_sz);
10415456Sphk    // Latch the result
10515456Sphk    _desired_net_plab_sz = plab_sz;
10615456Sphk    if (PrintPLAB) {
10715456Sphk      gclog_or_tty->print_cr(" (plab_sz = " SIZE_FORMAT " desired_plab_sz = " SIZE_FORMAT ") ", cur_plab_sz, plab_sz);
10815456Sphk    }
1092886Sphk  }
11013917Sphk  // Clear accumulators for next round.
11113917Sphk  reset();
11213917Sphk}
11313917Sphk
11413917Sphk