1 #ifdef USE_PRAGMA_IDENT_SRC
2 #pragma ident "@(#)collectorPolicy.cpp 1.90 07/10/04 10:49:37 JVM"
3 #endif
4 /*
5 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
26 */
27
28 # include "incls/_precompiled.incl"
29 # include "incls/_collectorPolicy.cpp.incl"
30
31 // CollectorPolicy methods.
32
33 void CollectorPolicy::initialize_flags() {
34 if (PermSize > MaxPermSize) {
35 MaxPermSize = PermSize;
36 }
37 PermSize = align_size_down(PermSize, min_alignment());
38 MaxPermSize = align_size_up(MaxPermSize, max_alignment());
39
40 MinPermHeapExpansion = align_size_down(MinPermHeapExpansion, min_alignment());
41 MaxPermHeapExpansion = align_size_down(MaxPermHeapExpansion, min_alignment());
42
43 MinHeapDeltaBytes = align_size_up(MinHeapDeltaBytes, min_alignment());
44
45 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment());
46 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment());
47 SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment());
48
49 assert(PermSize % min_alignment() == 0, "permanent space alignment");
50 assert(MaxPermSize % max_alignment() == 0, "maximum permanent space alignment");
51 assert(SharedReadOnlySize % max_alignment() == 0, "read-only space alignment");
52 assert(SharedReadWriteSize % max_alignment() == 0, "read-write space alignment");
53 assert(SharedMiscDataSize % max_alignment() == 0, "misc-data space alignment");
54 if (PermSize < M) {
55 vm_exit_during_initialization("Too small initial permanent heap");
56 }
57 }
58
59 void CollectorPolicy::initialize_size_info() {
60 // User inputs from -mx and ms are aligned
61 _initial_heap_byte_size = align_size_up(Arguments::initial_heap_size(),
62 min_alignment());
63 _min_heap_byte_size = align_size_up(Arguments::min_heap_size(),
64 min_alignment());
65 _max_heap_byte_size = align_size_up(MaxHeapSize, max_alignment());
66
67 // Check validity of heap parameters from launcher
68 if (_initial_heap_byte_size == 0) {
69 _initial_heap_byte_size = NewSize + OldSize;
70 } else {
71 Universe::check_alignment(_initial_heap_byte_size, min_alignment(),
72 "initial heap");
73 }
74 if (_min_heap_byte_size == 0) {
75 _min_heap_byte_size = NewSize + OldSize;
76 } else {
77 Universe::check_alignment(_min_heap_byte_size, min_alignment(),
78 "initial heap");
79 }
80
81 // Check heap parameter properties
82 if (_initial_heap_byte_size < M) {
83 vm_exit_during_initialization("Too small initial heap");
84 }
85 // Check heap parameter properties
86 if (_min_heap_byte_size < M) {
87 vm_exit_during_initialization("Too small minimum heap");
88 }
89 if (_initial_heap_byte_size <= NewSize) {
90 // make sure there is at least some room in old space
91 vm_exit_during_initialization("Too small initial heap for new size specified");
92 }
93 if (_max_heap_byte_size < _min_heap_byte_size) {
94 vm_exit_during_initialization("Incompatible minimum and maximum heap sizes specified");
95 }
96 if (_initial_heap_byte_size < _min_heap_byte_size) {
97 vm_exit_during_initialization("Incompatible minimum and initial heap sizes specified");
98 }
99 if (_max_heap_byte_size < _initial_heap_byte_size) {
100 vm_exit_during_initialization("Incompatible initial and maximum heap sizes specified");
101 }
102 }
103
104 void CollectorPolicy::initialize_perm_generation(PermGen::Name pgnm) {
105 _permanent_generation =
106 new PermanentGenerationSpec(pgnm, PermSize, MaxPermSize,
107 SharedReadOnlySize,
108 SharedReadWriteSize,
109 SharedMiscDataSize,
110 SharedMiscCodeSize);
111 if (_permanent_generation == NULL) {
112 vm_exit_during_initialization("Unable to allocate gen spec");
113 }
114 }
115
116
117 GenRemSet* CollectorPolicy::create_rem_set(MemRegion whole_heap,
118 int max_covered_regions) {
119 switch (rem_set_name()) {
120 case GenRemSet::CardTable: {
121 if (barrier_set_name() != BarrierSet::CardTableModRef)
122 vm_exit_during_initialization("Mismatch between RS and BS.");
123 CardTableRS* res = new CardTableRS(whole_heap, max_covered_regions);
124 return res;
125 }
126 default:
127 guarantee(false, "unrecognized GenRemSet::Name");
128 return NULL;
129 }
130 }
131
132 // GenCollectorPolicy methods.
133
134 void GenCollectorPolicy::initialize_size_policy(size_t init_eden_size,
135 size_t init_promo_size,
136 size_t init_survivor_size) {
137 double max_gc_minor_pause_sec = ((double) MaxGCMinorPauseMillis)/1000.0;
138 _size_policy = new AdaptiveSizePolicy(init_eden_size,
139 init_promo_size,
140 init_survivor_size,
141 max_gc_minor_pause_sec,
142 GCTimeRatio);
143 }
144
145 size_t GenCollectorPolicy::compute_max_alignment() {
146 // The card marking array and the offset arrays for old generations are
147 // committed in os pages as well. Make sure they are entirely full (to
148 // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1
149 // byte entry and the os page size is 4096, the maximum heap size should
150 // be 512*4096 = 2MB aligned.
151 size_t alignment = GenRemSet::max_alignment_constraint(rem_set_name());
152
153 // Parallel GC does its own alignment of the generations to avoid requiring a
154 // large page (256M on some platforms) for the permanent generation. The
155 // other collectors should also be updated to do their own alignment and then
156 // this use of lcm() should be removed.
157 if (UseLargePages && !UseParallelGC) {
196 }
197
198 void TwoGenerationCollectorPolicy::initialize_flags() {
199 GenCollectorPolicy::initialize_flags();
200
201 OldSize = align_size_down(OldSize, min_alignment());
202 if (NewSize + OldSize > MaxHeapSize) {
203 MaxHeapSize = NewSize + OldSize;
204 }
205 MaxHeapSize = align_size_up(MaxHeapSize, max_alignment());
206
207 always_do_update_barrier = UseConcMarkSweepGC;
208 BlockOffsetArrayUseUnallocatedBlock =
209 BlockOffsetArrayUseUnallocatedBlock || ParallelGCThreads > 0;
210
211 // Check validity of heap flags
212 assert(OldSize % min_alignment() == 0, "old space alignment");
213 assert(MaxHeapSize % max_alignment() == 0, "maximum heap alignment");
214 }
215
216 void GenCollectorPolicy::initialize_size_info() {
217 CollectorPolicy::initialize_size_info();
218
219 // Minimum sizes of the generations may be different than
220 // the initial sizes.
221 if (!FLAG_IS_DEFAULT(NewSize)) {
222 _min_gen0_size = NewSize;
223 } else {
224 _min_gen0_size = align_size_down(_min_heap_byte_size / (NewRatio+1),
225 min_alignment());
226 // We bound the minimum size by NewSize below (since it historically
227 // would have been NewSize and because the NewRatio calculation could
228 // yield a size that is too small) and bound it by MaxNewSize above.
229 // This is not always best. The NewSize calculated by CMS (which has
230 // a fixed minimum of 16m) can sometimes be "too" large. Consider
231 // the case where -Xmx32m. The CMS calculated NewSize would be about
232 // half the entire heap which seems too large. But the counter
233 // example is seen when the client defaults for NewRatio are used.
234 // An initial young generation size of 640k was observed
235 // with -Xmx128m -XX:MaxNewSize=32m when NewSize was not used
236 // as a lower bound as with
237 // _min_gen0_size = MIN2(_min_gen0_size, MaxNewSize);
238 // and 640k seemed too small a young generation.
239 _min_gen0_size = MIN2(MAX2(_min_gen0_size, NewSize), MaxNewSize);
240 }
241
242 // Parameters are valid, compute area sizes.
243 size_t max_new_size = align_size_down(_max_heap_byte_size / (NewRatio+1),
244 min_alignment());
245 max_new_size = MIN2(MAX2(max_new_size, _min_gen0_size), MaxNewSize);
246
247 // desired_new_size is used to set the initial size. The
248 // initial size must be greater than the minimum size.
249 size_t desired_new_size =
250 align_size_down(_initial_heap_byte_size / (NewRatio+1),
251 min_alignment());
252
253 size_t new_size = MIN2(MAX2(desired_new_size, _min_gen0_size), max_new_size);
254
255 _initial_gen0_size = new_size;
256 _max_gen0_size = max_new_size;
257 }
258
259 void TwoGenerationCollectorPolicy::initialize_size_info() {
260 GenCollectorPolicy::initialize_size_info();
261
262 // Minimum sizes of the generations may be different than
263 // the initial sizes. An inconsistently is permitted here
264 // in the total size that can be specified explicitly by
265 // command line specification of OldSize and NewSize and
266 // also a command line specification of -Xms. Issue a warning
267 // but allow the values to pass.
268 if (!FLAG_IS_DEFAULT(OldSize)) {
269 _min_gen1_size = OldSize;
270 // The generation minimums and the overall heap mimimum should
271 // be within one heap alignment.
272 if ((_min_gen1_size + _min_gen0_size + max_alignment()) <
273 _min_heap_byte_size) {
274 warning("Inconsistency between minimum heap size and minimum "
275 "generation sizes: using min heap = " SIZE_FORMAT,
276 _min_heap_byte_size);
277 }
278 } else {
279 _min_gen1_size = _min_heap_byte_size - _min_gen0_size;
280 }
281
282 _initial_gen1_size = _initial_heap_byte_size - _initial_gen0_size;
283 _max_gen1_size = _max_heap_byte_size - _max_gen0_size;
284 }
285
286 HeapWord* GenCollectorPolicy::mem_allocate_work(size_t size,
287 bool is_tlab,
288 bool* gc_overhead_limit_was_exceeded) {
289 GenCollectedHeap *gch = GenCollectedHeap::heap();
290
291 debug_only(gch->check_for_valid_allocation_state());
292 assert(gch->no_gc_in_progress(), "Allocation during gc not allowed");
293 HeapWord* result = NULL;
294
295 // Loop until the allocation is satisified,
296 // or unsatisfied after GC.
297 for (int try_count = 1; /* return or throw */; try_count += 1) {
298 HandleMark hm; // discard any handles allocated in each iteration
299
300 // First allocation attempt is lock-free.
301 Generation *gen0 = gch->get_gen(0);
302 assert(gen0->supports_inline_contig_alloc(),
303 "Otherwise, must do alloc within heap lock");
|
1 #ifdef USE_PRAGMA_IDENT_SRC
2 #pragma ident "@(#)collectorPolicy.cpp 1.90 07/10/04 10:49:37 JVM"
3 #endif
4 /*
5 * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
26 */
27
28 # include "incls/_precompiled.incl"
29 # include "incls/_collectorPolicy.cpp.incl"
30
31 // CollectorPolicy methods.
32
33 void CollectorPolicy::initialize_flags() {
34 if (PermSize > MaxPermSize) {
35 MaxPermSize = PermSize;
36 }
37 PermSize = MAX2(min_alignment(), align_size_down_(PermSize, min_alignment()));
38 MaxPermSize = align_size_up(MaxPermSize, max_alignment());
39
40 MinPermHeapExpansion = MAX2(min_alignment(), align_size_down_(MinPermHeapExpansion, min_alignment()));
41 MaxPermHeapExpansion = MAX2(min_alignment(), align_size_down_(MaxPermHeapExpansion, min_alignment()));
42
43 MinHeapDeltaBytes = align_size_up(MinHeapDeltaBytes, min_alignment());
44
45 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment());
46 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment());
47 SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment());
48
49 assert(PermSize % min_alignment() == 0, "permanent space alignment");
50 assert(MaxPermSize % max_alignment() == 0, "maximum permanent space alignment");
51 assert(SharedReadOnlySize % max_alignment() == 0, "read-only space alignment");
52 assert(SharedReadWriteSize % max_alignment() == 0, "read-write space alignment");
53 assert(SharedMiscDataSize % max_alignment() == 0, "misc-data space alignment");
54 if (PermSize < M) {
55 vm_exit_during_initialization("Too small initial permanent heap");
56 }
57 }
58
59 void CollectorPolicy::initialize_size_info() {
60 // User inputs from -mx and ms are aligned
61 set_initial_heap_byte_size(Arguments::initial_heap_size());
62 if (initial_heap_byte_size() == 0) {
63 set_initial_heap_byte_size(NewSize + OldSize);
64 }
65 set_initial_heap_byte_size(align_size_up(_initial_heap_byte_size,
66 min_alignment()));
67
68 set_min_heap_byte_size(Arguments::min_heap_size());
69 if (min_heap_byte_size() == 0) {
70 set_min_heap_byte_size(NewSize + OldSize);
71 }
72 set_min_heap_byte_size(align_size_up(_min_heap_byte_size,
73 min_alignment()));
74
75 set_max_heap_byte_size(align_size_up(MaxHeapSize, max_alignment()));
76
77 // Check heap parameter properties
78 if (initial_heap_byte_size() < M) {
79 vm_exit_during_initialization("Too small initial heap");
80 }
81 // Check heap parameter properties
82 if (min_heap_byte_size() < M) {
83 vm_exit_during_initialization("Too small minimum heap");
84 }
85 if (initial_heap_byte_size() <= NewSize) {
86 // make sure there is at least some room in old space
87 vm_exit_during_initialization("Too small initial heap for new size specified");
88 }
89 if (max_heap_byte_size() < min_heap_byte_size()) {
90 vm_exit_during_initialization("Incompatible minimum and maximum heap sizes specified");
91 }
92 if (initial_heap_byte_size() < min_heap_byte_size()) {
93 vm_exit_during_initialization("Incompatible minimum and initial heap sizes specified");
94 }
95 if (max_heap_byte_size() < initial_heap_byte_size()) {
96 vm_exit_during_initialization("Incompatible initial and maximum heap sizes specified");
97 }
98
99 if (PrintGCDetails && Verbose) {
100 gclog_or_tty->print_cr("Minimum heap " SIZE_FORMAT " Initial heap "
101 SIZE_FORMAT " Maximum heap " SIZE_FORMAT,
102 min_heap_byte_size(), initial_heap_byte_size(), max_heap_byte_size());
103 }
104 }
105
106 void CollectorPolicy::initialize_perm_generation(PermGen::Name pgnm) {
107 _permanent_generation =
108 new PermanentGenerationSpec(pgnm, PermSize, MaxPermSize,
109 SharedReadOnlySize,
110 SharedReadWriteSize,
111 SharedMiscDataSize,
112 SharedMiscCodeSize);
113 if (_permanent_generation == NULL) {
114 vm_exit_during_initialization("Unable to allocate gen spec");
115 }
116 }
117
118
119 GenRemSet* CollectorPolicy::create_rem_set(MemRegion whole_heap,
120 int max_covered_regions) {
121 switch (rem_set_name()) {
122 case GenRemSet::CardTable: {
123 CardTableRS* res = new CardTableRS(whole_heap, max_covered_regions);
124 return res;
125 }
126 default:
127 guarantee(false, "unrecognized GenRemSet::Name");
128 return NULL;
129 }
130 }
131
132 // GenCollectorPolicy methods.
133
134 size_t GenCollectorPolicy::scale_by_NewRatio_aligned(size_t base_size) {
135 size_t x = base_size / (NewRatio+1);
136 size_t new_gen_size = x > min_alignment() ?
137 align_size_down(x, min_alignment()) :
138 min_alignment();
139 return new_gen_size;
140 }
141
142 size_t GenCollectorPolicy::bound_minus_alignment(size_t desired_size,
143 size_t maximum_size) {
144 size_t alignment = min_alignment();
145 size_t max_minus = maximum_size - alignment;
146 return desired_size < max_minus ? desired_size : max_minus;
147 }
148
149
150 void GenCollectorPolicy::initialize_size_policy(size_t init_eden_size,
151 size_t init_promo_size,
152 size_t init_survivor_size) {
153 const double max_gc_minor_pause_sec = ((double) MaxGCMinorPauseMillis)/1000.0;
154 _size_policy = new AdaptiveSizePolicy(init_eden_size,
155 init_promo_size,
156 init_survivor_size,
157 max_gc_minor_pause_sec,
158 GCTimeRatio);
159 }
160
161 size_t GenCollectorPolicy::compute_max_alignment() {
162 // The card marking array and the offset arrays for old generations are
163 // committed in os pages as well. Make sure they are entirely full (to
164 // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1
165 // byte entry and the os page size is 4096, the maximum heap size should
166 // be 512*4096 = 2MB aligned.
167 size_t alignment = GenRemSet::max_alignment_constraint(rem_set_name());
168
169 // Parallel GC does its own alignment of the generations to avoid requiring a
170 // large page (256M on some platforms) for the permanent generation. The
171 // other collectors should also be updated to do their own alignment and then
172 // this use of lcm() should be removed.
173 if (UseLargePages && !UseParallelGC) {
212 }
213
214 void TwoGenerationCollectorPolicy::initialize_flags() {
215 GenCollectorPolicy::initialize_flags();
216
217 OldSize = align_size_down(OldSize, min_alignment());
218 if (NewSize + OldSize > MaxHeapSize) {
219 MaxHeapSize = NewSize + OldSize;
220 }
221 MaxHeapSize = align_size_up(MaxHeapSize, max_alignment());
222
223 always_do_update_barrier = UseConcMarkSweepGC;
224 BlockOffsetArrayUseUnallocatedBlock =
225 BlockOffsetArrayUseUnallocatedBlock || ParallelGCThreads > 0;
226
227 // Check validity of heap flags
228 assert(OldSize % min_alignment() == 0, "old space alignment");
229 assert(MaxHeapSize % max_alignment() == 0, "maximum heap alignment");
230 }
231
232 // Values set on the command line win over any ergonomically
233 // set command line parameters.
234 // Ergonomic choice of parameters are done before this
235 // method is called. Values for command line parameters such as NewSize
236 // and MaxNewSize feed those ergonomic choices into this method.
237 // This method makes the final generation sizings consistent with
238 // themselves and with overall heap sizings.
239 // In the absence of explicitly set command line flags, policies
240 // such as the use of NewRatio are used to size the generation.
241 void GenCollectorPolicy::initialize_size_info() {
242 CollectorPolicy::initialize_size_info();
243
244 // min_alignment() is used for alignment within a generation.
245 // There is additional alignment done down stream for some
246 // collectors that sometimes causes unwanted rounding up of
247 // generations sizes.
248
249 // Determine maximum size of gen0
250
251 size_t max_new_size = 0;
252 if (FLAG_IS_CMDLINE(MaxNewSize)) {
253 if (MaxNewSize < min_alignment()) {
254 max_new_size = min_alignment();
255 } else if (MaxNewSize >= max_heap_byte_size()) {
256 max_new_size = align_size_down(max_heap_byte_size() - min_alignment(),
257 min_alignment());
258 warning("MaxNewSize (" SIZE_FORMAT "k) is equal to or "
259 "greater than the entire heap (" SIZE_FORMAT "k). A "
260 "new generation size of " SIZE_FORMAT "k will be used.",
261 MaxNewSize/K, max_heap_byte_size()/K, max_new_size/K);
262 } else {
263 max_new_size = align_size_down(MaxNewSize, min_alignment());
264 }
265
266 // The case for FLAG_IS_ERGO(MaxNewSize) could be treated
267 // specially at this point to just use an ergonomically set
268 // MaxNewSize to set max_new_size. For cases with small
269 // heaps such a policy often did not work because the MaxNewSize
270 // was larger than the entire heap. The interpretation given
271 // to ergonomically set flags is that the flags are set
272 // by different collectors for their own special needs but
273 // are not allowed to badly shape the heap. This allows the
274 // different collectors to decide what's best for themselves
275 // without having to factor in the overall heap shape. It
276 // can be the case in the future that the collectors would
277 // only make "wise" ergonomics choices and this policy could
278 // just accept those choices. The choices currently made are
279 // not always "wise".
280 } else {
281 max_new_size = scale_by_NewRatio_aligned(max_heap_byte_size());
282 // Bound the maximum size by NewSize below (since it historically
283 // would have been NewSize and because the NewRatio calculation could
284 // yield a size that is too small) and bound it by MaxNewSize above.
285 // Ergonomics plays here by previously calculating the desired
286 // NewSize and MaxNewSize.
287 max_new_size = MIN2(MAX2(max_new_size, NewSize), MaxNewSize);
288 }
289 assert(max_new_size > 0, "All paths should set max_new_size");
290
291 // Given the maximum gen0 size, determine the initial and
292 // minimum sizes.
293
294 if (max_heap_byte_size() == min_heap_byte_size()) {
295 // The maximum and minimum heap sizes are the same so
296 // the generations minimum and initial must be the
297 // same as its maximum.
298 set_min_gen0_size(max_new_size);
299 set_initial_gen0_size(max_new_size);
300 set_max_gen0_size(max_new_size);
301 } else {
302 size_t desired_new_size = 0;
303 if (!FLAG_IS_DEFAULT(NewSize)) {
304 // If NewSize is set ergonomically (for example by cms), it
305 // would make sense to use it. If it is used, also use it
306 // to set the initial size. Although there is no reason
307 // the minimum size and the initial size have to be the same,
308 // the current implementation gets into trouble during the calculation
309 // of the tenured generation sizes if they are different.
310 // Note that this makes the initial size and the minimum size
311 // generally small compared to the NewRatio calculation.
312 _min_gen0_size = NewSize;
313 desired_new_size = NewSize;
314 max_new_size = MAX2(max_new_size, NewSize);
315 } else {
316 // For the case where NewSize is the default, use NewRatio
317 // to size the minimum and initial generation sizes.
318 // Use the default NewSize as the floor for these values. If
319 // NewRatio is overly large, the resulting sizes can be too
320 // small.
321 _min_gen0_size = MAX2(scale_by_NewRatio_aligned(min_heap_byte_size()),
322 NewSize);
323 desired_new_size =
324 MAX2(scale_by_NewRatio_aligned(initial_heap_byte_size()),
325 NewSize);
326 }
327
328 assert(_min_gen0_size > 0, "Sanity check");
329 set_initial_gen0_size(desired_new_size);
330 set_max_gen0_size(max_new_size);
331
332 // At this point the desirable initial and minimum sizes have been
333 // determined without regard to the maximum sizes.
334
335 // Bound the sizes by the corresponding overall heap sizes.
336 set_min_gen0_size(
337 bound_minus_alignment(_min_gen0_size, min_heap_byte_size()));
338 set_initial_gen0_size(
339 bound_minus_alignment(_initial_gen0_size, initial_heap_byte_size()));
340 set_max_gen0_size(
341 bound_minus_alignment(_max_gen0_size, max_heap_byte_size()));
342
343 // At this point all three sizes have been checked against the
344 // maximum sizes but have not been checked for consistency
345 // among the three.
346
347 // Final check min <= initial <= max
348 set_min_gen0_size(MIN2(_min_gen0_size, _max_gen0_size));
349 set_initial_gen0_size(
350 MAX2(MIN2(_initial_gen0_size, _max_gen0_size), _min_gen0_size));
351 set_min_gen0_size(MIN2(_min_gen0_size, _initial_gen0_size));
352 }
353
354 if (PrintGCDetails && Verbose) {
355 gclog_or_tty->print_cr("Minimum gen0 " SIZE_FORMAT " Initial gen0 "
356 SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT,
357 min_gen0_size(), initial_gen0_size(), max_gen0_size());
358 }
359 }
360
361 // Call this method during the sizing of the gen1 to make
362 // adjustments to gen0 because of gen1 sizing policy. gen0 initially has
363 // the most freedom in sizing because it is done before the
364 // policy for gen1 is applied. Once gen1 policies have been applied,
365 // there may be conflicts in the shape of the heap and this method
366 // is used to make the needed adjustments. The application of the
367 // policies could be more sophisticated (iterative for example) but
368 // keeping it simple also seems a worthwhile goal.
369 bool TwoGenerationCollectorPolicy::adjust_gen0_sizes(size_t* gen0_size_ptr,
370 size_t* gen1_size_ptr,
371 size_t heap_size,
372 size_t min_gen0_size) {
373 bool result = false;
374 if ((*gen1_size_ptr + *gen0_size_ptr) > heap_size) {
375 if (((*gen0_size_ptr + OldSize) > heap_size) &&
376 (heap_size - min_gen0_size) >= min_alignment()) {
377 // Adjust gen0 down to accomodate OldSize
378 *gen0_size_ptr = heap_size - min_gen0_size;
379 *gen0_size_ptr =
380 MAX2((uintx)align_size_down(*gen0_size_ptr, min_alignment()),
381 min_alignment());
382 assert(*gen0_size_ptr > 0, "Min gen0 is too large");
383 result = true;
384 } else {
385 *gen1_size_ptr = heap_size - *gen0_size_ptr;
386 *gen1_size_ptr =
387 MAX2((uintx)align_size_down(*gen1_size_ptr, min_alignment()),
388 min_alignment());
389 }
390 }
391 return result;
392 }
393
394 // Minimum sizes of the generations may be different than
395 // the initial sizes. An inconsistently is permitted here
396 // in the total size that can be specified explicitly by
397 // command line specification of OldSize and NewSize and
398 // also a command line specification of -Xms. Issue a warning
399 // but allow the values to pass.
400
401 void TwoGenerationCollectorPolicy::initialize_size_info() {
402 GenCollectorPolicy::initialize_size_info();
403
404 // At this point the minimum, initial and maximum sizes
405 // of the overall heap and of gen0 have been determined.
406 // The maximum gen1 size can be determined from the maximum gen0
407 // and maximum heap size since not explicit flags exits
408 // for setting the gen1 maximum.
409 _max_gen1_size = max_heap_byte_size() - _max_gen0_size;
410 _max_gen1_size =
411 MAX2((uintx)align_size_down(_max_gen1_size, min_alignment()),
412 min_alignment());
413 // If no explicit command line flag has been set for the
414 // gen1 size, use what is left for gen1.
415 if (FLAG_IS_DEFAULT(OldSize) || FLAG_IS_ERGO(OldSize)) {
416 // The user has not specified any value or ergonomics
417 // has chosen a value (which may or may not be consistent
418 // with the overall heap size). In either case make
419 // the minimum, maximum and initial sizes consistent
420 // with the gen0 sizes and the overall heap sizes.
421 assert(min_heap_byte_size() > _min_gen0_size,
422 "gen0 has an unexpected minimum size");
423 set_min_gen1_size(min_heap_byte_size() - min_gen0_size());
424 set_min_gen1_size(
425 MAX2((uintx)align_size_down(_min_gen1_size, min_alignment()),
426 min_alignment()));
427 set_initial_gen1_size(initial_heap_byte_size() - initial_gen0_size());
428 set_initial_gen1_size(
429 MAX2((uintx)align_size_down(_initial_gen1_size, min_alignment()),
430 min_alignment()));
431
432 } else {
433 // It's been explicitly set on the command line. Use the
434 // OldSize and then determine the consequences.
435 set_min_gen1_size(OldSize);
436 set_initial_gen1_size(OldSize);
437
438 // If the user has explicitly set an OldSize that is inconsistent
439 // with other command line flags, issue a warning.
440 // The generation minimums and the overall heap mimimum should
441 // be within one heap alignment.
442 if ((_min_gen1_size + _min_gen0_size + min_alignment()) <
443 min_heap_byte_size()) {
444 warning("Inconsistency between minimum heap size and minimum "
445 "generation sizes: using minimum heap = " SIZE_FORMAT,
446 min_heap_byte_size());
447 }
448 if ((OldSize > _max_gen1_size)) {
449 warning("Inconsistency between maximum heap size and maximum "
450 "generation sizes: using maximum heap = " SIZE_FORMAT
451 " -XX:OldSize flag is being ignored",
452 max_heap_byte_size());
453 }
454 // If there is an inconsistency between the OldSize and the minimum and/or
455 // initial size of gen0, since OldSize was explicitly set, OldSize wins.
456 if (adjust_gen0_sizes(&_min_gen0_size, &_min_gen1_size,
457 min_heap_byte_size(), OldSize)) {
458 if (PrintGCDetails && Verbose) {
459 gclog_or_tty->print_cr("Minimum gen0 " SIZE_FORMAT " Initial gen0 "
460 SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT,
461 min_gen0_size(), initial_gen0_size(), max_gen0_size());
462 }
463 }
464 // Initial size
465 if (adjust_gen0_sizes(&_initial_gen0_size, &_initial_gen1_size,
466 initial_heap_byte_size(), OldSize)) {
467 if (PrintGCDetails && Verbose) {
468 gclog_or_tty->print_cr("Minimum gen0 " SIZE_FORMAT " Initial gen0 "
469 SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT,
470 min_gen0_size(), initial_gen0_size(), max_gen0_size());
471 }
472 }
473 }
474 // Enforce the maximum gen1 size.
475 set_min_gen1_size(MIN2(_min_gen1_size, _max_gen1_size));
476
477 // Check that min gen1 <= initial gen1 <= max gen1
478 set_initial_gen1_size(MAX2(_initial_gen1_size, _min_gen1_size));
479 set_initial_gen1_size(MIN2(_initial_gen1_size, _max_gen1_size));
480
481 if (PrintGCDetails && Verbose) {
482 gclog_or_tty->print_cr("Minimum gen1 " SIZE_FORMAT " Initial gen1 "
483 SIZE_FORMAT " Maximum gen1 " SIZE_FORMAT,
484 min_gen1_size(), initial_gen1_size(), max_gen1_size());
485 }
486 }
487
488 HeapWord* GenCollectorPolicy::mem_allocate_work(size_t size,
489 bool is_tlab,
490 bool* gc_overhead_limit_was_exceeded) {
491 GenCollectedHeap *gch = GenCollectedHeap::heap();
492
493 debug_only(gch->check_for_valid_allocation_state());
494 assert(gch->no_gc_in_progress(), "Allocation during gc not allowed");
495 HeapWord* result = NULL;
496
497 // Loop until the allocation is satisified,
498 // or unsatisfied after GC.
499 for (int try_count = 1; /* return or throw */; try_count += 1) {
500 HandleMark hm; // discard any handles allocated in each iteration
501
502 // First allocation attempt is lock-free.
503 Generation *gen0 = gch->get_gen(0);
504 assert(gen0->supports_inline_contig_alloc(),
505 "Otherwise, must do alloc within heap lock");
|