1 /*
   2  * Copyright (c) 2015, 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/g1/g1EvacStats.hpp"
  27 #include "gc/shared/gcId.hpp"
  28 #include "trace/tracing.hpp"
  29 
  30 void G1EvacStats::adjust_desired_plab_sz() {
  31   if (PrintPLAB) {
  32     gclog_or_tty->print(" (allocated = " SIZE_FORMAT " wasted = " SIZE_FORMAT " "
  33                         "unused = " SIZE_FORMAT " used = " SIZE_FORMAT " "
  34                         "undo_waste = " SIZE_FORMAT " region_end_waste = " SIZE_FORMAT " "
  35                         "regions filled = %u direct_allocated = " SIZE_FORMAT " "
  36                         "failure_used = " SIZE_FORMAT " failure_waste = " SIZE_FORMAT ") ",
  37                         _allocated, _wasted, _unused, used(), _undo_wasted, _region_end_waste,
  38                         _regions_filled, _direct_allocated, _failure_used, _failure_waste);
  39   }
  40 
  41   if (ResizePLAB) {
  42 
  43     assert(is_object_aligned(max_size()) && min_size() <= max_size(),
  44            "PLAB clipping computation may be incorrect");
  45 
  46     if (_allocated == 0) {
  47       assert((_unused == 0),
  48              err_msg("Inconsistency in PLAB stats: "
  49                      "_allocated: "SIZE_FORMAT", "
  50                      "_wasted: "SIZE_FORMAT", "
  51                      "_region_end_waste: "SIZE_FORMAT", "
  52                      "_unused: "SIZE_FORMAT", "
  53                      "_used  : "SIZE_FORMAT,
  54                      _allocated, _wasted, _region_end_waste, _unused, used()));
  55       _allocated = 1;
  56     }
  57     // Calculate the new PLAB size as the amount of space that could be wasted to
  58     // keep TargetPLABWastePct given latest memory usage and that the last buffer
  59     // will be G1ExpectedAveragePLABOccupancyPercent full.
  60     //
  61     // E.g. assume that if we recently used 100 words and a TargetPLABWastePct of 10.
  62     // If we had one thread, we could waste up to 10 words to meet that percentage.
  63     // Given that we also assume that that buffer is typically half-full, the new
  64     // desired PLAB size is 20 words.
  65     //
  66     // We account region end waste fully to PLAB allocation (in the calculation of
  67     // what we consider as "used" below). This is not completely fair, but is a
  68     // conservative assumption because PLABs may be sized flexibly while we cannot
  69     // adjust inline allocations.
  70     
  71     // We need to cover overflow when calculating the amount of space actually used
  72     // by objects in PLABs when subtracting the region end waste.
  73     // This is a possible situation if many threads do not allocate anything but a
  74     // few rather large objects. In this degenerate case the PLAB size would simply quickly
  75     // tend to minimum PLAB size, which is an okay reaction.
  76     // Allocation during GC will try to minimize region end waste.
  77     size_t const waste_used_for_calculation = used() > _region_end_waste ? used() - _region_end_waste : 0;
  78 
  79     // Use fixed point calculation in the following calculation.
  80     size_t const total_waste_allowed = waste_used_for_calculation * TargetPLABWastePct;
  81     size_t const cur_plab_sz = total_waste_allowed / G1ExpectedAveragePLABOccupancyPercent;
  82     // Take historical weighted average
  83     _filter.sample(cur_plab_sz);
  84     // Clip from above and below, and align to object boundary
  85     size_t plab_sz;
  86     plab_sz = MAX2(min_size(), (size_t)_filter.average());
  87     plab_sz = MIN2(max_size(), plab_sz);
  88     plab_sz = align_object_size(plab_sz);
  89     // Latch the result
  90     _desired_net_plab_sz = plab_sz;
  91     if (PrintPLAB) {
  92       gclog_or_tty->print_cr(" (plab_sz = " SIZE_FORMAT " desired_plab_sz = " SIZE_FORMAT ") ", cur_plab_sz, plab_sz);
  93     }
  94   }
  95   // Clear accumulators for next round.
  96   reset();
  97 }
  98