1/*
2 * Copyright (c) 2015, 2017, 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 "logging/log.hpp"
29#include "memory/allocation.inline.hpp"
30
31void G1EvacStats::log_plab_allocation() {
32 PLABStats::log_plab_allocation();
33 log_debug(gc, plab)("%s other allocation: "
34 "region end waste: " SIZE_FORMAT "B, "
35 "regions filled: %u, "
36 "direct allocated: " SIZE_FORMAT "B, "
37 "failure used: " SIZE_FORMAT "B, "
38 "failure wasted: " SIZE_FORMAT "B",
39 _description,
40 _region_end_waste * HeapWordSize,
41 _regions_filled,
42 _direct_allocated * HeapWordSize,
43 _failure_used * HeapWordSize,
44 _failure_waste * HeapWordSize);
45}
46
47size_t G1EvacStats::compute_desired_plab_sz() {
48 // The size of the PLAB caps the amount of space that can be wasted at the
49 // end of the collection. In the worst case the last PLAB could be completely
50 // empty.
51 // This allows us to calculate the new PLAB size to achieve the
52 // TargetPLABWastePct given the latest memory usage and that the last buffer
53 // will be G1LastPLABAverageOccupancy full.
54 //
55 // E.g. assume that if in the current GC 100 words were allocated and a
56 // TargetPLABWastePct of 10 had been set.
57 //
58 // So we could waste up to 10 words to meet that percentage. Given that we
59 // also assume that that buffer is typically half-full, the new desired PLAB
60 // size is set to 20 words.
61 //
62 // The amount of allocation performed should be independent of the number of
63 // threads, so should the maximum waste we can spend in total. So if
64 // we used n threads to allocate, each of them can spend maximum waste/n words in
65 // a first rough approximation. The number of threads only comes into play later
66 // when actually retrieving the actual desired PLAB size.
67 //
68 // After calculating this optimal PLAB size the algorithm applies the usual
69 // exponential decaying average over this value to guess the next PLAB size.
70 //
71 // We account region end waste fully to PLAB allocation (in the calculation of
72 // what we consider as "used_for_waste_calculation" below). This is not
73 // completely fair, but is a conservative assumption because PLABs may be sized
74 // flexibly while we cannot adjust inline allocations.
75 // Allocation during GC will try to minimize region end waste so this impact
76 // should be minimal.
77 //
78 // We need to cover overflow when calculating the amount of space actually used
79 // by objects in PLABs when subtracting the region end waste.
80 // Region end waste may be higher than actual allocation. This may occur if many
81 // threads do not allocate anything but a few rather large objects. In this
82 // degenerate case the PLAB size would simply quickly tend to minimum PLAB size,
83 // which is an okay reaction.
84 size_t const used_for_waste_calculation = used() > _region_end_waste ? used() - _region_end_waste : 0;
85
86 size_t const total_waste_allowed = used_for_waste_calculation * TargetPLABWastePct;
87 size_t const cur_plab_sz = (size_t)((double)total_waste_allowed / G1LastPLABAverageOccupancy);
88 return cur_plab_sz;
89}
90
91G1EvacStats::G1EvacStats(const char* description, size_t desired_plab_sz_, unsigned wt) :
92 PLABStats(description, desired_plab_sz_, wt),
93 _region_end_waste(0),
94 _regions_filled(0),
95 _direct_allocated(0),
96 _failure_used(0),
97 _failure_waste(0) {
98}
99
100
101G1EvacStats::~G1EvacStats() { }
102