1/*
2 * Copyright (c) 2018, 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/parallel/adjoiningGenerationsForHeteroHeap.hpp"
27#include "gc/parallel/adjoiningVirtualSpaces.hpp"
28#include "gc/parallel/parallelArguments.hpp"
29#include "gc/parallel/parallelScavengeHeap.hpp"
30#include "gc/parallel/psFileBackedVirtualspace.hpp"
31#include "logging/log.hpp"
32#include "logging/logStream.hpp"
33#include "memory/resourceArea.hpp"
34#include "utilities/align.hpp"
35#include "utilities/ostream.hpp"
36
37// Create two virtual spaces (HeteroVirtualSpaces), low() on nv-dimm memory, high() on dram.
38// create ASPSOldGen and ASPSYoungGen the same way as in base class
39
40AdjoiningGenerationsForHeteroHeap::AdjoiningGenerationsForHeteroHeap(ReservedSpace old_young_rs) :
41 _total_size_limit(ParallelArguments::heap_max_size_bytes()) {
42 size_t init_old_byte_size = OldSize;
43 size_t min_old_byte_size = MinOldSize;
44 size_t max_old_byte_size = MaxOldSize;
45 size_t init_young_byte_size = NewSize;
46 size_t min_young_byte_size = MinNewSize;
47 size_t max_young_byte_size = MaxNewSize;
48 // create HeteroVirtualSpaces which is composed of non-overlapping virtual spaces.
49 HeteroVirtualSpaces* hetero_virtual_spaces = new HeteroVirtualSpaces(old_young_rs, min_old_byte_size,
50 min_young_byte_size, _total_size_limit);
51
52 assert(min_old_byte_size <= init_old_byte_size &&
53 init_old_byte_size <= max_old_byte_size, "Parameter check");
54 assert(min_young_byte_size <= init_young_byte_size &&
55 init_young_byte_size <= max_young_byte_size, "Parameter check");
56
57 assert(UseAdaptiveGCBoundary, "Should be used only when UseAdaptiveGCBoundary is true");
58
59 // Initialize the virtual spaces. Then pass a virtual space to each generation
60 // for initialization of the generation.
61
62 // Does the actual creation of the virtual spaces
63 hetero_virtual_spaces->initialize(max_old_byte_size, init_old_byte_size, init_young_byte_size);
64
65 _young_gen = new ASPSYoungGen(hetero_virtual_spaces->high(),
66 hetero_virtual_spaces->high()->committed_size() /* intial_size */,
67 min_young_byte_size,
68 hetero_virtual_spaces->max_young_size());
69
70 _old_gen = new ASPSOldGen(hetero_virtual_spaces->low(),
71 hetero_virtual_spaces->low()->committed_size() /* intial_size */,
72 min_old_byte_size,
73 hetero_virtual_spaces->max_old_size(), "old", 1);
74
75 young_gen()->initialize_work();
76 assert(young_gen()->reserved().byte_size() <= young_gen()->gen_size_limit(), "Consistency check");
77 assert(old_young_rs.size() >= young_gen()->gen_size_limit(), "Consistency check");
78
79 old_gen()->initialize_work("old", 1);
80 assert(old_gen()->reserved().byte_size() <= old_gen()->gen_size_limit(), "Consistency check");
81 assert(old_young_rs.size() >= old_gen()->gen_size_limit(), "Consistency check");
82
83 _virtual_spaces = hetero_virtual_spaces;
84}
85
86size_t AdjoiningGenerationsForHeteroHeap::required_reserved_memory() {
87 // This is the size that young gen can grow to, when AdaptiveGCBoundary is true.
88 size_t max_yg_size = ParallelArguments::heap_max_size_bytes() - MinOldSize;
89 // This is the size that old gen can grow to, when AdaptiveGCBoundary is true.
90 size_t max_old_size = ParallelArguments::heap_max_size_bytes() - MinNewSize;
91
92 return max_yg_size + max_old_size;
93}
94
95// We override this function since size of reservedspace here is more than heap size and
96// callers expect this function to return heap size.
97size_t AdjoiningGenerationsForHeteroHeap::reserved_byte_size() {
98 return total_size_limit();
99}
100
101AdjoiningGenerationsForHeteroHeap::HeteroVirtualSpaces::HeteroVirtualSpaces(ReservedSpace rs, size_t min_old_byte_size, size_t min_yg_byte_size, size_t max_total_size) :
102 AdjoiningVirtualSpaces(rs, min_old_byte_size, min_yg_byte_size, GenAlignment),
103 _max_total_size(max_total_size),
104 _min_old_byte_size(min_old_byte_size),
105 _min_young_byte_size(min_yg_byte_size),
106 _max_old_byte_size(_max_total_size - _min_young_byte_size),
107 _max_young_byte_size(_max_total_size - _min_old_byte_size) {
108}
109
110void AdjoiningGenerationsForHeteroHeap::HeteroVirtualSpaces::initialize(size_t initial_old_reserved_size, size_t init_old_byte_size,
111 size_t init_young_byte_size) {
112
113 // This is the reserved space exclusively for old generation.
114 ReservedSpace low_rs = _reserved_space.first_part(_max_old_byte_size, true);
115 // Intially we only assign 'initial_old_reserved_size' of the reserved space to old virtual space.
116 low_rs = low_rs.first_part(initial_old_reserved_size);
117
118 // This is the reserved space exclusively for young generation.
119 ReservedSpace high_rs = _reserved_space.last_part(_max_old_byte_size).first_part(_max_young_byte_size);
120
121 // Carve out 'initial_young_reserved_size' of reserved space.
122 size_t initial_young_reserved_size = _max_total_size - initial_old_reserved_size;
123 high_rs = high_rs.last_part(_max_young_byte_size - initial_young_reserved_size);
124
125 _low = new PSFileBackedVirtualSpace(low_rs, alignment(), AllocateOldGenAt);
126 if (!static_cast <PSFileBackedVirtualSpace*>(_low)->initialize()) {
127 vm_exit_during_initialization("Could not map space for old generation at given AllocateOldGenAt path");
128 }
129
130 if (!_low->expand_by(init_old_byte_size)) {
131 vm_exit_during_initialization("Could not reserve enough space for object heap");
132 }
133
134 _high = new PSVirtualSpaceHighToLow(high_rs, alignment());
135 if (!_high->expand_by(init_young_byte_size)) {
136 vm_exit_during_initialization("Could not reserve enough space for object heap");
137 }
138}
139
140// Since the virtual spaces are non-overlapping, there is no boundary as such.
141// We replicate the same behavior and maintain the same invariants as base class 'AdjoiningVirtualSpaces' by
142// increasing old generation size and decreasing young generation size by same amount.
143bool AdjoiningGenerationsForHeteroHeap::HeteroVirtualSpaces::adjust_boundary_up(size_t change_in_bytes) {
144 assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
145 DEBUG_ONLY(size_t total_size_before = young_vs()->reserved_size() + old_vs()->reserved_size());
146
147 size_t bytes_needed = change_in_bytes;
148 size_t uncommitted_in_old = MIN2(old_vs()->uncommitted_size(), bytes_needed);
149 bool old_expanded = false;
150
151 // 1. Try to expand old within its reserved space.
152 if (uncommitted_in_old != 0) {
153 if (!old_vs()->expand_by(uncommitted_in_old)) {
154 return false;
155 }
156 old_expanded = true;
157 bytes_needed -= uncommitted_in_old;
158 if (bytes_needed == 0) {
159 return true;
160 }
161 }
162
163 size_t bytes_to_add_in_old = 0;
164
165 // 2. Get uncommitted memory from Young virtualspace.
166 size_t young_uncommitted = MIN2(young_vs()->uncommitted_size(), bytes_needed);
167 if (young_uncommitted > 0) {
168 young_vs()->set_reserved(young_vs()->reserved_low_addr() + young_uncommitted,
169 young_vs()->reserved_high_addr(),
170 young_vs()->special());
171 bytes_needed -= young_uncommitted;
172 bytes_to_add_in_old = young_uncommitted;
173 }
174
175 // 3. Get committed memory from Young virtualspace
176 if (bytes_needed > 0) {
177 size_t shrink_size = align_down(bytes_needed, young_vs()->alignment());
178 bool ret = young_vs()->shrink_by(shrink_size);
179 assert(ret, "We should be able to shrink young space");
180 young_vs()->set_reserved(young_vs()->reserved_low_addr() + shrink_size,
181 young_vs()->reserved_high_addr(),
182 young_vs()->special());
183
184 bytes_to_add_in_old += shrink_size;
185 }
186
187 // 4. Increase size of old space
188 old_vs()->set_reserved(old_vs()->reserved_low_addr(),
189 old_vs()->reserved_high_addr() + bytes_to_add_in_old,
190 old_vs()->special());
191 if (!old_vs()->expand_by(bytes_to_add_in_old) && !old_expanded) {
192 return false;
193 }
194
195 DEBUG_ONLY(size_t total_size_after = young_vs()->reserved_size() + old_vs()->reserved_size());
196 assert(total_size_after == total_size_before, "should be equal");
197
198 return true;
199}
200
201// Read comment for adjust_boundary_up()
202// Increase young generation size and decrease old generation size by same amount.
203bool AdjoiningGenerationsForHeteroHeap::HeteroVirtualSpaces::adjust_boundary_down(size_t change_in_bytes) {
204 assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");
205 DEBUG_ONLY(size_t total_size_before = young_vs()->reserved_size() + old_vs()->reserved_size());
206
207 size_t bytes_needed = change_in_bytes;
208 size_t uncommitted_in_young = MIN2(young_vs()->uncommitted_size(), bytes_needed);
209 bool young_expanded = false;
210
211 // 1. Try to expand old within its reserved space.
212 if (uncommitted_in_young > 0) {
213 if (!young_vs()->expand_by(uncommitted_in_young)) {
214 return false;
215 }
216 young_expanded = true;
217 bytes_needed -= uncommitted_in_young;
218 if (bytes_needed == 0) {
219 return true;
220 }
221 }
222
223 size_t bytes_to_add_in_young = 0;
224
225 // 2. Get uncommitted memory from Old virtualspace.
226 size_t old_uncommitted = MIN2(old_vs()->uncommitted_size(), bytes_needed);
227 if (old_uncommitted > 0) {
228 old_vs()->set_reserved(old_vs()->reserved_low_addr(),
229 old_vs()->reserved_high_addr() - old_uncommitted,
230 old_vs()->special());
231 bytes_needed -= old_uncommitted;
232 bytes_to_add_in_young = old_uncommitted;
233 }
234
235 // 3. Get committed memory from Old virtualspace
236 if (bytes_needed > 0) {
237 size_t shrink_size = align_down(bytes_needed, old_vs()->alignment());
238 bool ret = old_vs()->shrink_by(shrink_size);
239 assert(ret, "We should be able to shrink young space");
240 old_vs()->set_reserved(old_vs()->reserved_low_addr(),
241 old_vs()->reserved_high_addr() - shrink_size,
242 old_vs()->special());
243
244 bytes_to_add_in_young += shrink_size;
245 }
246
247 assert(bytes_to_add_in_young <= change_in_bytes, "should not be more than requested size");
248 // 4. Increase size of young space
249 young_vs()->set_reserved(young_vs()->reserved_low_addr() - bytes_to_add_in_young,
250 young_vs()->reserved_high_addr(),
251 young_vs()->special());
252 if (!young_vs()->expand_by(bytes_to_add_in_young) && !young_expanded) {
253 return false;
254 }
255
256 DEBUG_ONLY(size_t total_size_after = young_vs()->reserved_size() + old_vs()->reserved_size());
257 assert(total_size_after == total_size_before, "should be equal");
258
259 return true;
260}
261
262