collectorPolicy.cpp revision 13249:a2753984d2c1
1/*
2 * Copyright (c) 2001, 2016, 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/shared/adaptiveSizePolicy.hpp"
27#include "gc/shared/cardTableRS.hpp"
28#include "gc/shared/collectorPolicy.hpp"
29#include "gc/shared/gcLocker.inline.hpp"
30#include "gc/shared/gcPolicyCounters.hpp"
31#include "gc/shared/genCollectedHeap.hpp"
32#include "gc/shared/generationSpec.hpp"
33#include "gc/shared/space.hpp"
34#include "gc/shared/vmGCOperations.hpp"
35#include "logging/log.hpp"
36#include "memory/universe.hpp"
37#include "runtime/arguments.hpp"
38#include "runtime/globals_extension.hpp"
39#include "runtime/handles.inline.hpp"
40#include "runtime/java.hpp"
41#include "runtime/thread.inline.hpp"
42#include "runtime/vmThread.hpp"
43#include "utilities/align.hpp"
44#include "utilities/macros.hpp"
45
46// CollectorPolicy methods
47
48CollectorPolicy::CollectorPolicy() :
49    _space_alignment(0),
50    _heap_alignment(0),
51    _initial_heap_byte_size(InitialHeapSize),
52    _max_heap_byte_size(MaxHeapSize),
53    _min_heap_byte_size(Arguments::min_heap_size()),
54    _size_policy(NULL),
55    _should_clear_all_soft_refs(false),
56    _all_soft_refs_clear(false)
57{}
58
59#ifdef ASSERT
60void CollectorPolicy::assert_flags() {
61  assert(InitialHeapSize <= MaxHeapSize, "Ergonomics decided on incompatible initial and maximum heap sizes");
62  assert(InitialHeapSize % _heap_alignment == 0, "InitialHeapSize alignment");
63  assert(MaxHeapSize % _heap_alignment == 0, "MaxHeapSize alignment");
64}
65
66void CollectorPolicy::assert_size_info() {
67  assert(InitialHeapSize == _initial_heap_byte_size, "Discrepancy between InitialHeapSize flag and local storage");
68  assert(MaxHeapSize == _max_heap_byte_size, "Discrepancy between MaxHeapSize flag and local storage");
69  assert(_max_heap_byte_size >= _min_heap_byte_size, "Ergonomics decided on incompatible minimum and maximum heap sizes");
70  assert(_initial_heap_byte_size >= _min_heap_byte_size, "Ergonomics decided on incompatible initial and minimum heap sizes");
71  assert(_max_heap_byte_size >= _initial_heap_byte_size, "Ergonomics decided on incompatible initial and maximum heap sizes");
72  assert(_min_heap_byte_size % _heap_alignment == 0, "min_heap_byte_size alignment");
73  assert(_initial_heap_byte_size % _heap_alignment == 0, "initial_heap_byte_size alignment");
74  assert(_max_heap_byte_size % _heap_alignment == 0, "max_heap_byte_size alignment");
75}
76#endif // ASSERT
77
78void CollectorPolicy::initialize_flags() {
79  assert(_space_alignment != 0, "Space alignment not set up properly");
80  assert(_heap_alignment != 0, "Heap alignment not set up properly");
81  assert(_heap_alignment >= _space_alignment,
82         "heap_alignment: " SIZE_FORMAT " less than space_alignment: " SIZE_FORMAT,
83         _heap_alignment, _space_alignment);
84  assert(_heap_alignment % _space_alignment == 0,
85         "heap_alignment: " SIZE_FORMAT " not aligned by space_alignment: " SIZE_FORMAT,
86         _heap_alignment, _space_alignment);
87
88  if (FLAG_IS_CMDLINE(MaxHeapSize)) {
89    if (FLAG_IS_CMDLINE(InitialHeapSize) && InitialHeapSize > MaxHeapSize) {
90      vm_exit_during_initialization("Initial heap size set to a larger value than the maximum heap size");
91    }
92    if (_min_heap_byte_size != 0 && MaxHeapSize < _min_heap_byte_size) {
93      vm_exit_during_initialization("Incompatible minimum and maximum heap sizes specified");
94    }
95  }
96
97  // Check heap parameter properties
98  if (MaxHeapSize < 2 * M) {
99    vm_exit_during_initialization("Too small maximum heap");
100  }
101  if (InitialHeapSize < M) {
102    vm_exit_during_initialization("Too small initial heap");
103  }
104  if (_min_heap_byte_size < M) {
105    vm_exit_during_initialization("Too small minimum heap");
106  }
107
108  // User inputs from -Xmx and -Xms must be aligned
109  _min_heap_byte_size = align_up(_min_heap_byte_size, _heap_alignment);
110  size_t aligned_initial_heap_size = align_up(InitialHeapSize, _heap_alignment);
111  size_t aligned_max_heap_size = align_up(MaxHeapSize, _heap_alignment);
112
113  // Write back to flags if the values changed
114  if (aligned_initial_heap_size != InitialHeapSize) {
115    FLAG_SET_ERGO(size_t, InitialHeapSize, aligned_initial_heap_size);
116  }
117  if (aligned_max_heap_size != MaxHeapSize) {
118    FLAG_SET_ERGO(size_t, MaxHeapSize, aligned_max_heap_size);
119  }
120
121  if (FLAG_IS_CMDLINE(InitialHeapSize) && _min_heap_byte_size != 0 &&
122      InitialHeapSize < _min_heap_byte_size) {
123    vm_exit_during_initialization("Incompatible minimum and initial heap sizes specified");
124  }
125  if (!FLAG_IS_DEFAULT(InitialHeapSize) && InitialHeapSize > MaxHeapSize) {
126    FLAG_SET_ERGO(size_t, MaxHeapSize, InitialHeapSize);
127  } else if (!FLAG_IS_DEFAULT(MaxHeapSize) && InitialHeapSize > MaxHeapSize) {
128    FLAG_SET_ERGO(size_t, InitialHeapSize, MaxHeapSize);
129    if (InitialHeapSize < _min_heap_byte_size) {
130      _min_heap_byte_size = InitialHeapSize;
131    }
132  }
133
134  _initial_heap_byte_size = InitialHeapSize;
135  _max_heap_byte_size = MaxHeapSize;
136
137  FLAG_SET_ERGO(size_t, MinHeapDeltaBytes, align_up(MinHeapDeltaBytes, _space_alignment));
138
139  DEBUG_ONLY(CollectorPolicy::assert_flags();)
140}
141
142void CollectorPolicy::initialize_size_info() {
143  log_debug(gc, heap)("Minimum heap " SIZE_FORMAT "  Initial heap " SIZE_FORMAT "  Maximum heap " SIZE_FORMAT,
144                      _min_heap_byte_size, _initial_heap_byte_size, _max_heap_byte_size);
145
146  DEBUG_ONLY(CollectorPolicy::assert_size_info();)
147}
148
149bool CollectorPolicy::use_should_clear_all_soft_refs(bool v) {
150  bool result = _should_clear_all_soft_refs;
151  set_should_clear_all_soft_refs(false);
152  return result;
153}
154
155CardTableRS* CollectorPolicy::create_rem_set(MemRegion whole_heap) {
156  return new CardTableRS(whole_heap);
157}
158
159void CollectorPolicy::cleared_all_soft_refs() {
160  // If near gc overhear limit, continue to clear SoftRefs.  SoftRefs may
161  // have been cleared in the last collection but if the gc overhear
162  // limit continues to be near, SoftRefs should still be cleared.
163  if (size_policy() != NULL) {
164    _should_clear_all_soft_refs = size_policy()->gc_overhead_limit_near();
165  }
166  _all_soft_refs_clear = true;
167}
168
169size_t CollectorPolicy::compute_heap_alignment() {
170  // The card marking array and the offset arrays for old generations are
171  // committed in os pages as well. Make sure they are entirely full (to
172  // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1
173  // byte entry and the os page size is 4096, the maximum heap size should
174  // be 512*4096 = 2MB aligned.
175
176  size_t alignment = CardTableRS::ct_max_alignment_constraint();
177
178  if (UseLargePages) {
179      // In presence of large pages we have to make sure that our
180      // alignment is large page aware.
181      alignment = lcm(os::large_page_size(), alignment);
182  }
183
184  return alignment;
185}
186
187// GenCollectorPolicy methods
188
189GenCollectorPolicy::GenCollectorPolicy() :
190    _min_young_size(0),
191    _initial_young_size(0),
192    _max_young_size(0),
193    _min_old_size(0),
194    _initial_old_size(0),
195    _max_old_size(0),
196    _gen_alignment(0),
197    _young_gen_spec(NULL),
198    _old_gen_spec(NULL)
199{}
200
201size_t GenCollectorPolicy::scale_by_NewRatio_aligned(size_t base_size) {
202  return align_down_bounded(base_size / (NewRatio + 1), _gen_alignment);
203}
204
205size_t GenCollectorPolicy::bound_minus_alignment(size_t desired_size,
206                                                 size_t maximum_size) {
207  size_t max_minus = maximum_size - _gen_alignment;
208  return desired_size < max_minus ? desired_size : max_minus;
209}
210
211
212void GenCollectorPolicy::initialize_size_policy(size_t init_eden_size,
213                                                size_t init_promo_size,
214                                                size_t init_survivor_size) {
215  const double max_gc_pause_sec = ((double) MaxGCPauseMillis) / 1000.0;
216  _size_policy = new AdaptiveSizePolicy(init_eden_size,
217                                        init_promo_size,
218                                        init_survivor_size,
219                                        max_gc_pause_sec,
220                                        GCTimeRatio);
221}
222
223size_t GenCollectorPolicy::young_gen_size_lower_bound() {
224  // The young generation must be aligned and have room for eden + two survivors
225  return align_up(3 * _space_alignment, _gen_alignment);
226}
227
228size_t GenCollectorPolicy::old_gen_size_lower_bound() {
229  return align_up(_space_alignment, _gen_alignment);
230}
231
232#ifdef ASSERT
233void GenCollectorPolicy::assert_flags() {
234  CollectorPolicy::assert_flags();
235  assert(NewSize >= _min_young_size, "Ergonomics decided on a too small young gen size");
236  assert(NewSize <= MaxNewSize, "Ergonomics decided on incompatible initial and maximum young gen sizes");
237  assert(FLAG_IS_DEFAULT(MaxNewSize) || MaxNewSize < MaxHeapSize, "Ergonomics decided on incompatible maximum young gen and heap sizes");
238  assert(NewSize % _gen_alignment == 0, "NewSize alignment");
239  assert(FLAG_IS_DEFAULT(MaxNewSize) || MaxNewSize % _gen_alignment == 0, "MaxNewSize alignment");
240  assert(OldSize + NewSize <= MaxHeapSize, "Ergonomics decided on incompatible generation and heap sizes");
241  assert(OldSize % _gen_alignment == 0, "OldSize alignment");
242}
243
244void GenCollectorPolicy::assert_size_info() {
245  CollectorPolicy::assert_size_info();
246  // GenCollectorPolicy::initialize_size_info may update the MaxNewSize
247  assert(MaxNewSize < MaxHeapSize, "Ergonomics decided on incompatible maximum young and heap sizes");
248  assert(NewSize == _initial_young_size, "Discrepancy between NewSize flag and local storage");
249  assert(MaxNewSize == _max_young_size, "Discrepancy between MaxNewSize flag and local storage");
250  assert(OldSize == _initial_old_size, "Discrepancy between OldSize flag and local storage");
251  assert(_min_young_size <= _initial_young_size, "Ergonomics decided on incompatible minimum and initial young gen sizes");
252  assert(_initial_young_size <= _max_young_size, "Ergonomics decided on incompatible initial and maximum young gen sizes");
253  assert(_min_young_size % _gen_alignment == 0, "_min_young_size alignment");
254  assert(_initial_young_size % _gen_alignment == 0, "_initial_young_size alignment");
255  assert(_max_young_size % _gen_alignment == 0, "_max_young_size alignment");
256  assert(_min_young_size <= bound_minus_alignment(_min_young_size, _min_heap_byte_size),
257      "Ergonomics made minimum young generation larger than minimum heap");
258  assert(_initial_young_size <=  bound_minus_alignment(_initial_young_size, _initial_heap_byte_size),
259      "Ergonomics made initial young generation larger than initial heap");
260  assert(_max_young_size <= bound_minus_alignment(_max_young_size, _max_heap_byte_size),
261      "Ergonomics made maximum young generation lager than maximum heap");
262  assert(_min_old_size <= _initial_old_size, "Ergonomics decided on incompatible minimum and initial old gen sizes");
263  assert(_initial_old_size <= _max_old_size, "Ergonomics decided on incompatible initial and maximum old gen sizes");
264  assert(_max_old_size % _gen_alignment == 0, "_max_old_size alignment");
265  assert(_initial_old_size % _gen_alignment == 0, "_initial_old_size alignment");
266  assert(_max_heap_byte_size <= (_max_young_size + _max_old_size), "Total maximum heap sizes must be sum of generation maximum sizes");
267  assert(_min_young_size + _min_old_size <= _min_heap_byte_size, "Minimum generation sizes exceed minimum heap size");
268  assert(_initial_young_size + _initial_old_size == _initial_heap_byte_size, "Initial generation sizes should match initial heap size");
269  assert(_max_young_size + _max_old_size == _max_heap_byte_size, "Maximum generation sizes should match maximum heap size");
270}
271#endif // ASSERT
272
273void GenCollectorPolicy::initialize_flags() {
274  CollectorPolicy::initialize_flags();
275
276  assert(_gen_alignment != 0, "Generation alignment not set up properly");
277  assert(_heap_alignment >= _gen_alignment,
278         "heap_alignment: " SIZE_FORMAT " less than gen_alignment: " SIZE_FORMAT,
279         _heap_alignment, _gen_alignment);
280  assert(_gen_alignment % _space_alignment == 0,
281         "gen_alignment: " SIZE_FORMAT " not aligned by space_alignment: " SIZE_FORMAT,
282         _gen_alignment, _space_alignment);
283  assert(_heap_alignment % _gen_alignment == 0,
284         "heap_alignment: " SIZE_FORMAT " not aligned by gen_alignment: " SIZE_FORMAT,
285         _heap_alignment, _gen_alignment);
286
287  // All generational heaps have a young gen; handle those flags here
288
289  // Make sure the heap is large enough for two generations
290  size_t smallest_new_size = young_gen_size_lower_bound();
291  size_t smallest_heap_size = align_up(smallest_new_size + old_gen_size_lower_bound(),
292                                           _heap_alignment);
293  if (MaxHeapSize < smallest_heap_size) {
294    FLAG_SET_ERGO(size_t, MaxHeapSize, smallest_heap_size);
295    _max_heap_byte_size = MaxHeapSize;
296  }
297  // If needed, synchronize _min_heap_byte size and _initial_heap_byte_size
298  if (_min_heap_byte_size < smallest_heap_size) {
299    _min_heap_byte_size = smallest_heap_size;
300    if (InitialHeapSize < _min_heap_byte_size) {
301      FLAG_SET_ERGO(size_t, InitialHeapSize, smallest_heap_size);
302      _initial_heap_byte_size = smallest_heap_size;
303    }
304  }
305
306  // Make sure NewSize allows an old generation to fit even if set on the command line
307  if (FLAG_IS_CMDLINE(NewSize) && NewSize >= _initial_heap_byte_size) {
308    log_warning(gc, ergo)("NewSize was set larger than initial heap size, will use initial heap size.");
309    FLAG_SET_ERGO(size_t, NewSize, bound_minus_alignment(NewSize, _initial_heap_byte_size));
310  }
311
312  // Now take the actual NewSize into account. We will silently increase NewSize
313  // if the user specified a smaller or unaligned value.
314  size_t bounded_new_size = bound_minus_alignment(NewSize, MaxHeapSize);
315  bounded_new_size = MAX2(smallest_new_size, align_down(bounded_new_size, _gen_alignment));
316  if (bounded_new_size != NewSize) {
317    FLAG_SET_ERGO(size_t, NewSize, bounded_new_size);
318  }
319  _min_young_size = smallest_new_size;
320  _initial_young_size = NewSize;
321
322  if (!FLAG_IS_DEFAULT(MaxNewSize)) {
323    if (MaxNewSize >= MaxHeapSize) {
324      // Make sure there is room for an old generation
325      size_t smaller_max_new_size = MaxHeapSize - _gen_alignment;
326      if (FLAG_IS_CMDLINE(MaxNewSize)) {
327        log_warning(gc, ergo)("MaxNewSize (" SIZE_FORMAT "k) is equal to or greater than the entire "
328                              "heap (" SIZE_FORMAT "k).  A new max generation size of " SIZE_FORMAT "k will be used.",
329                              MaxNewSize/K, MaxHeapSize/K, smaller_max_new_size/K);
330      }
331      FLAG_SET_ERGO(size_t, MaxNewSize, smaller_max_new_size);
332      if (NewSize > MaxNewSize) {
333        FLAG_SET_ERGO(size_t, NewSize, MaxNewSize);
334        _initial_young_size = NewSize;
335      }
336    } else if (MaxNewSize < _initial_young_size) {
337      FLAG_SET_ERGO(size_t, MaxNewSize, _initial_young_size);
338    } else if (!is_aligned(MaxNewSize, _gen_alignment)) {
339      FLAG_SET_ERGO(size_t, MaxNewSize, align_down(MaxNewSize, _gen_alignment));
340    }
341    _max_young_size = MaxNewSize;
342  }
343
344  if (NewSize > MaxNewSize) {
345    // At this point this should only happen if the user specifies a large NewSize and/or
346    // a small (but not too small) MaxNewSize.
347    if (FLAG_IS_CMDLINE(MaxNewSize)) {
348      log_warning(gc, ergo)("NewSize (" SIZE_FORMAT "k) is greater than the MaxNewSize (" SIZE_FORMAT "k). "
349                            "A new max generation size of " SIZE_FORMAT "k will be used.",
350                            NewSize/K, MaxNewSize/K, NewSize/K);
351    }
352    FLAG_SET_ERGO(size_t, MaxNewSize, NewSize);
353    _max_young_size = MaxNewSize;
354  }
355
356  if (SurvivorRatio < 1 || NewRatio < 1) {
357    vm_exit_during_initialization("Invalid young gen ratio specified");
358  }
359
360  if (OldSize < old_gen_size_lower_bound()) {
361    FLAG_SET_ERGO(size_t, OldSize, old_gen_size_lower_bound());
362  }
363  if (!is_aligned(OldSize, _gen_alignment)) {
364    FLAG_SET_ERGO(size_t, OldSize, align_down(OldSize, _gen_alignment));
365  }
366
367  if (FLAG_IS_CMDLINE(OldSize) && FLAG_IS_DEFAULT(MaxHeapSize)) {
368    // NewRatio will be used later to set the young generation size so we use
369    // it to calculate how big the heap should be based on the requested OldSize
370    // and NewRatio.
371    assert(NewRatio > 0, "NewRatio should have been set up earlier");
372    size_t calculated_heapsize = (OldSize / NewRatio) * (NewRatio + 1);
373
374    calculated_heapsize = align_up(calculated_heapsize, _heap_alignment);
375    FLAG_SET_ERGO(size_t, MaxHeapSize, calculated_heapsize);
376    _max_heap_byte_size = MaxHeapSize;
377    FLAG_SET_ERGO(size_t, InitialHeapSize, calculated_heapsize);
378    _initial_heap_byte_size = InitialHeapSize;
379  }
380
381  // Adjust NewSize and OldSize or MaxHeapSize to match each other
382  if (NewSize + OldSize > MaxHeapSize) {
383    if (FLAG_IS_CMDLINE(MaxHeapSize)) {
384      // Somebody has set a maximum heap size with the intention that we should not
385      // exceed it. Adjust New/OldSize as necessary.
386      size_t calculated_size = NewSize + OldSize;
387      double shrink_factor = (double) MaxHeapSize / calculated_size;
388      size_t smaller_new_size = align_down((size_t)(NewSize * shrink_factor), _gen_alignment);
389      FLAG_SET_ERGO(size_t, NewSize, MAX2(young_gen_size_lower_bound(), smaller_new_size));
390      _initial_young_size = NewSize;
391
392      // OldSize is already aligned because above we aligned MaxHeapSize to
393      // _heap_alignment, and we just made sure that NewSize is aligned to
394      // _gen_alignment. In initialize_flags() we verified that _heap_alignment
395      // is a multiple of _gen_alignment.
396      FLAG_SET_ERGO(size_t, OldSize, MaxHeapSize - NewSize);
397    } else {
398      FLAG_SET_ERGO(size_t, MaxHeapSize, align_up(NewSize + OldSize, _heap_alignment));
399      _max_heap_byte_size = MaxHeapSize;
400    }
401  }
402
403  // Update NewSize, if possible, to avoid sizing the young gen too small when only
404  // OldSize is set on the command line.
405  if (FLAG_IS_CMDLINE(OldSize) && !FLAG_IS_CMDLINE(NewSize)) {
406    if (OldSize < _initial_heap_byte_size) {
407      size_t new_size = _initial_heap_byte_size - OldSize;
408      // Need to compare against the flag value for max since _max_young_size
409      // might not have been set yet.
410      if (new_size >= _min_young_size && new_size <= MaxNewSize) {
411        FLAG_SET_ERGO(size_t, NewSize, new_size);
412        _initial_young_size = NewSize;
413      }
414    }
415  }
416
417  always_do_update_barrier = UseConcMarkSweepGC;
418
419  DEBUG_ONLY(GenCollectorPolicy::assert_flags();)
420}
421
422// Values set on the command line win over any ergonomically
423// set command line parameters.
424// Ergonomic choice of parameters are done before this
425// method is called.  Values for command line parameters such as NewSize
426// and MaxNewSize feed those ergonomic choices into this method.
427// This method makes the final generation sizings consistent with
428// themselves and with overall heap sizings.
429// In the absence of explicitly set command line flags, policies
430// such as the use of NewRatio are used to size the generation.
431
432// Minimum sizes of the generations may be different than
433// the initial sizes.  An inconsistency is permitted here
434// in the total size that can be specified explicitly by
435// command line specification of OldSize and NewSize and
436// also a command line specification of -Xms.  Issue a warning
437// but allow the values to pass.
438void GenCollectorPolicy::initialize_size_info() {
439  CollectorPolicy::initialize_size_info();
440
441  _initial_young_size = NewSize;
442  _max_young_size = MaxNewSize;
443  _initial_old_size = OldSize;
444
445  // Determine maximum size of the young generation.
446
447  if (FLAG_IS_DEFAULT(MaxNewSize)) {
448    _max_young_size = scale_by_NewRatio_aligned(_max_heap_byte_size);
449    // Bound the maximum size by NewSize below (since it historically
450    // would have been NewSize and because the NewRatio calculation could
451    // yield a size that is too small) and bound it by MaxNewSize above.
452    // Ergonomics plays here by previously calculating the desired
453    // NewSize and MaxNewSize.
454    _max_young_size = MIN2(MAX2(_max_young_size, _initial_young_size), MaxNewSize);
455  }
456
457  // Given the maximum young size, determine the initial and
458  // minimum young sizes.
459
460  if (_max_heap_byte_size == _initial_heap_byte_size) {
461    // The maximum and initial heap sizes are the same so the generation's
462    // initial size must be the same as it maximum size. Use NewSize as the
463    // size if set on command line.
464    _max_young_size = FLAG_IS_CMDLINE(NewSize) ? NewSize : _max_young_size;
465    _initial_young_size = _max_young_size;
466
467    // Also update the minimum size if min == initial == max.
468    if (_max_heap_byte_size == _min_heap_byte_size) {
469      _min_young_size = _max_young_size;
470    }
471  } else {
472    if (FLAG_IS_CMDLINE(NewSize)) {
473      // If NewSize is set on the command line, we should use it as
474      // the initial size, but make sure it is within the heap bounds.
475      _initial_young_size =
476        MIN2(_max_young_size, bound_minus_alignment(NewSize, _initial_heap_byte_size));
477      _min_young_size = bound_minus_alignment(_initial_young_size, _min_heap_byte_size);
478    } else {
479      // For the case where NewSize is not set on the command line, use
480      // NewRatio to size the initial generation size. Use the current
481      // NewSize as the floor, because if NewRatio is overly large, the resulting
482      // size can be too small.
483      _initial_young_size =
484        MIN2(_max_young_size, MAX2(scale_by_NewRatio_aligned(_initial_heap_byte_size), NewSize));
485    }
486  }
487
488  log_trace(gc, heap)("1: Minimum young " SIZE_FORMAT "  Initial young " SIZE_FORMAT "  Maximum young " SIZE_FORMAT,
489                      _min_young_size, _initial_young_size, _max_young_size);
490
491  // At this point the minimum, initial and maximum sizes
492  // of the overall heap and of the young generation have been determined.
493  // The maximum old size can be determined from the maximum young
494  // and maximum heap size since no explicit flags exist
495  // for setting the old generation maximum.
496  _max_old_size = MAX2(_max_heap_byte_size - _max_young_size, _gen_alignment);
497
498  // If no explicit command line flag has been set for the
499  // old generation size, use what is left.
500  if (!FLAG_IS_CMDLINE(OldSize)) {
501    // The user has not specified any value but the ergonomics
502    // may have chosen a value (which may or may not be consistent
503    // with the overall heap size).  In either case make
504    // the minimum, maximum and initial sizes consistent
505    // with the young sizes and the overall heap sizes.
506    _min_old_size = _gen_alignment;
507    _initial_old_size = MIN2(_max_old_size, MAX2(_initial_heap_byte_size - _initial_young_size, _min_old_size));
508    // _max_old_size has already been made consistent above.
509  } else {
510    // OldSize has been explicitly set on the command line. Use it
511    // for the initial size but make sure the minimum allow a young
512    // generation to fit as well.
513    // If the user has explicitly set an OldSize that is inconsistent
514    // with other command line flags, issue a warning.
515    // The generation minimums and the overall heap minimum should
516    // be within one generation alignment.
517    if (_initial_old_size > _max_old_size) {
518      log_warning(gc, ergo)("Inconsistency between maximum heap size and maximum "
519                            "generation sizes: using maximum heap = " SIZE_FORMAT
520                            ", -XX:OldSize flag is being ignored",
521                            _max_heap_byte_size);
522      _initial_old_size = _max_old_size;
523    }
524
525    _min_old_size = MIN2(_initial_old_size, _min_heap_byte_size - _min_young_size);
526  }
527
528  // The initial generation sizes should match the initial heap size,
529  // if not issue a warning and resize the generations. This behavior
530  // differs from JDK8 where the generation sizes have higher priority
531  // than the initial heap size.
532  if ((_initial_old_size + _initial_young_size) != _initial_heap_byte_size) {
533    log_warning(gc, ergo)("Inconsistency between generation sizes and heap size, resizing "
534                          "the generations to fit the heap.");
535
536    size_t desired_young_size = _initial_heap_byte_size - _initial_old_size;
537    if (_initial_heap_byte_size < _initial_old_size) {
538      // Old want all memory, use minimum for young and rest for old
539      _initial_young_size = _min_young_size;
540      _initial_old_size = _initial_heap_byte_size - _min_young_size;
541    } else if (desired_young_size > _max_young_size) {
542      // Need to increase both young and old generation
543      _initial_young_size = _max_young_size;
544      _initial_old_size = _initial_heap_byte_size - _max_young_size;
545    } else if (desired_young_size < _min_young_size) {
546      // Need to decrease both young and old generation
547      _initial_young_size = _min_young_size;
548      _initial_old_size = _initial_heap_byte_size - _min_young_size;
549    } else {
550      // The young generation boundaries allow us to only update the
551      // young generation.
552      _initial_young_size = desired_young_size;
553    }
554
555    log_trace(gc, heap)("2: Minimum young " SIZE_FORMAT "  Initial young " SIZE_FORMAT "  Maximum young " SIZE_FORMAT,
556                    _min_young_size, _initial_young_size, _max_young_size);
557  }
558
559  // Write back to flags if necessary.
560  if (NewSize != _initial_young_size) {
561    FLAG_SET_ERGO(size_t, NewSize, _initial_young_size);
562  }
563
564  if (MaxNewSize != _max_young_size) {
565    FLAG_SET_ERGO(size_t, MaxNewSize, _max_young_size);
566  }
567
568  if (OldSize != _initial_old_size) {
569    FLAG_SET_ERGO(size_t, OldSize, _initial_old_size);
570  }
571
572  log_trace(gc, heap)("Minimum old " SIZE_FORMAT "  Initial old " SIZE_FORMAT "  Maximum old " SIZE_FORMAT,
573                  _min_old_size, _initial_old_size, _max_old_size);
574
575  DEBUG_ONLY(GenCollectorPolicy::assert_size_info();)
576}
577
578HeapWord* GenCollectorPolicy::mem_allocate_work(size_t size,
579                                        bool is_tlab,
580                                        bool* gc_overhead_limit_was_exceeded) {
581  GenCollectedHeap *gch = GenCollectedHeap::heap();
582
583  debug_only(gch->check_for_valid_allocation_state());
584  assert(gch->no_gc_in_progress(), "Allocation during gc not allowed");
585
586  // In general gc_overhead_limit_was_exceeded should be false so
587  // set it so here and reset it to true only if the gc time
588  // limit is being exceeded as checked below.
589  *gc_overhead_limit_was_exceeded = false;
590
591  HeapWord* result = NULL;
592
593  // Loop until the allocation is satisfied, or unsatisfied after GC.
594  for (uint try_count = 1, gclocker_stalled_count = 0; /* return or throw */; try_count += 1) {
595    HandleMark hm; // Discard any handles allocated in each iteration.
596
597    // First allocation attempt is lock-free.
598    Generation *young = gch->young_gen();
599    assert(young->supports_inline_contig_alloc(),
600      "Otherwise, must do alloc within heap lock");
601    if (young->should_allocate(size, is_tlab)) {
602      result = young->par_allocate(size, is_tlab);
603      if (result != NULL) {
604        assert(gch->is_in_reserved(result), "result not in heap");
605        return result;
606      }
607    }
608    uint gc_count_before;  // Read inside the Heap_lock locked region.
609    {
610      MutexLocker ml(Heap_lock);
611      log_trace(gc, alloc)("GenCollectorPolicy::mem_allocate_work: attempting locked slow path allocation");
612      // Note that only large objects get a shot at being
613      // allocated in later generations.
614      bool first_only = ! should_try_older_generation_allocation(size);
615
616      result = gch->attempt_allocation(size, is_tlab, first_only);
617      if (result != NULL) {
618        assert(gch->is_in_reserved(result), "result not in heap");
619        return result;
620      }
621
622      if (GCLocker::is_active_and_needs_gc()) {
623        if (is_tlab) {
624          return NULL;  // Caller will retry allocating individual object.
625        }
626        if (!gch->is_maximal_no_gc()) {
627          // Try and expand heap to satisfy request.
628          result = expand_heap_and_allocate(size, is_tlab);
629          // Result could be null if we are out of space.
630          if (result != NULL) {
631            return result;
632          }
633        }
634
635        if (gclocker_stalled_count > GCLockerRetryAllocationCount) {
636          return NULL; // We didn't get to do a GC and we didn't get any memory.
637        }
638
639        // If this thread is not in a jni critical section, we stall
640        // the requestor until the critical section has cleared and
641        // GC allowed. When the critical section clears, a GC is
642        // initiated by the last thread exiting the critical section; so
643        // we retry the allocation sequence from the beginning of the loop,
644        // rather than causing more, now probably unnecessary, GC attempts.
645        JavaThread* jthr = JavaThread::current();
646        if (!jthr->in_critical()) {
647          MutexUnlocker mul(Heap_lock);
648          // Wait for JNI critical section to be exited
649          GCLocker::stall_until_clear();
650          gclocker_stalled_count += 1;
651          continue;
652        } else {
653          if (CheckJNICalls) {
654            fatal("Possible deadlock due to allocating while"
655                  " in jni critical section");
656          }
657          return NULL;
658        }
659      }
660
661      // Read the gc count while the heap lock is held.
662      gc_count_before = gch->total_collections();
663    }
664
665    VM_GenCollectForAllocation op(size, is_tlab, gc_count_before);
666    VMThread::execute(&op);
667    if (op.prologue_succeeded()) {
668      result = op.result();
669      if (op.gc_locked()) {
670         assert(result == NULL, "must be NULL if gc_locked() is true");
671         continue;  // Retry and/or stall as necessary.
672      }
673
674      // Allocation has failed and a collection
675      // has been done.  If the gc time limit was exceeded the
676      // this time, return NULL so that an out-of-memory
677      // will be thrown.  Clear gc_overhead_limit_exceeded
678      // so that the overhead exceeded does not persist.
679
680      const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded();
681      const bool softrefs_clear = all_soft_refs_clear();
682
683      if (limit_exceeded && softrefs_clear) {
684        *gc_overhead_limit_was_exceeded = true;
685        size_policy()->set_gc_overhead_limit_exceeded(false);
686        if (op.result() != NULL) {
687          CollectedHeap::fill_with_object(op.result(), size);
688        }
689        return NULL;
690      }
691      assert(result == NULL || gch->is_in_reserved(result),
692             "result not in heap");
693      return result;
694    }
695
696    // Give a warning if we seem to be looping forever.
697    if ((QueuedAllocationWarningCount > 0) &&
698        (try_count % QueuedAllocationWarningCount == 0)) {
699          log_warning(gc, ergo)("GenCollectorPolicy::mem_allocate_work retries %d times,"
700                                " size=" SIZE_FORMAT " %s", try_count, size, is_tlab ? "(TLAB)" : "");
701    }
702  }
703}
704
705HeapWord* GenCollectorPolicy::expand_heap_and_allocate(size_t size,
706                                                       bool   is_tlab) {
707  GenCollectedHeap *gch = GenCollectedHeap::heap();
708  HeapWord* result = NULL;
709  Generation *old = gch->old_gen();
710  if (old->should_allocate(size, is_tlab)) {
711    result = old->expand_and_allocate(size, is_tlab);
712  }
713  if (result == NULL) {
714    Generation *young = gch->young_gen();
715    if (young->should_allocate(size, is_tlab)) {
716      result = young->expand_and_allocate(size, is_tlab);
717    }
718  }
719  assert(result == NULL || gch->is_in_reserved(result), "result not in heap");
720  return result;
721}
722
723HeapWord* GenCollectorPolicy::satisfy_failed_allocation(size_t size,
724                                                        bool   is_tlab) {
725  GenCollectedHeap *gch = GenCollectedHeap::heap();
726  GCCauseSetter x(gch, GCCause::_allocation_failure);
727  HeapWord* result = NULL;
728
729  assert(size != 0, "Precondition violated");
730  if (GCLocker::is_active_and_needs_gc()) {
731    // GC locker is active; instead of a collection we will attempt
732    // to expand the heap, if there's room for expansion.
733    if (!gch->is_maximal_no_gc()) {
734      result = expand_heap_and_allocate(size, is_tlab);
735    }
736    return result;   // Could be null if we are out of space.
737  } else if (!gch->incremental_collection_will_fail(false /* don't consult_young */)) {
738    // Do an incremental collection.
739    gch->do_collection(false,                     // full
740                       false,                     // clear_all_soft_refs
741                       size,                      // size
742                       is_tlab,                   // is_tlab
743                       GenCollectedHeap::OldGen); // max_generation
744  } else {
745    log_trace(gc)(" :: Trying full because partial may fail :: ");
746    // Try a full collection; see delta for bug id 6266275
747    // for the original code and why this has been simplified
748    // with from-space allocation criteria modified and
749    // such allocation moved out of the safepoint path.
750    gch->do_collection(true,                      // full
751                       false,                     // clear_all_soft_refs
752                       size,                      // size
753                       is_tlab,                   // is_tlab
754                       GenCollectedHeap::OldGen); // max_generation
755  }
756
757  result = gch->attempt_allocation(size, is_tlab, false /*first_only*/);
758
759  if (result != NULL) {
760    assert(gch->is_in_reserved(result), "result not in heap");
761    return result;
762  }
763
764  // OK, collection failed, try expansion.
765  result = expand_heap_and_allocate(size, is_tlab);
766  if (result != NULL) {
767    return result;
768  }
769
770  // If we reach this point, we're really out of memory. Try every trick
771  // we can to reclaim memory. Force collection of soft references. Force
772  // a complete compaction of the heap. Any additional methods for finding
773  // free memory should be here, especially if they are expensive. If this
774  // attempt fails, an OOM exception will be thrown.
775  {
776    UIntFlagSetting flag_change(MarkSweepAlwaysCompactCount, 1); // Make sure the heap is fully compacted
777
778    gch->do_collection(true,                      // full
779                       true,                      // clear_all_soft_refs
780                       size,                      // size
781                       is_tlab,                   // is_tlab
782                       GenCollectedHeap::OldGen); // max_generation
783  }
784
785  result = gch->attempt_allocation(size, is_tlab, false /* first_only */);
786  if (result != NULL) {
787    assert(gch->is_in_reserved(result), "result not in heap");
788    return result;
789  }
790
791  assert(!should_clear_all_soft_refs(),
792    "Flag should have been handled and cleared prior to this point");
793
794  // What else?  We might try synchronous finalization later.  If the total
795  // space available is large enough for the allocation, then a more
796  // complete compaction phase than we've tried so far might be
797  // appropriate.
798  return NULL;
799}
800
801MetaWord* CollectorPolicy::satisfy_failed_metadata_allocation(
802                                                 ClassLoaderData* loader_data,
803                                                 size_t word_size,
804                                                 Metaspace::MetadataType mdtype) {
805  uint loop_count = 0;
806  uint gc_count = 0;
807  uint full_gc_count = 0;
808
809  assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock");
810
811  do {
812    MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
813    if (result != NULL) {
814      return result;
815    }
816
817    if (GCLocker::is_active_and_needs_gc()) {
818      // If the GCLocker is active, just expand and allocate.
819      // If that does not succeed, wait if this thread is not
820      // in a critical section itself.
821      result =
822        loader_data->metaspace_non_null()->expand_and_allocate(word_size,
823                                                               mdtype);
824      if (result != NULL) {
825        return result;
826      }
827      JavaThread* jthr = JavaThread::current();
828      if (!jthr->in_critical()) {
829        // Wait for JNI critical section to be exited
830        GCLocker::stall_until_clear();
831        // The GC invoked by the last thread leaving the critical
832        // section will be a young collection and a full collection
833        // is (currently) needed for unloading classes so continue
834        // to the next iteration to get a full GC.
835        continue;
836      } else {
837        if (CheckJNICalls) {
838          fatal("Possible deadlock due to allocating while"
839                " in jni critical section");
840        }
841        return NULL;
842      }
843    }
844
845    {  // Need lock to get self consistent gc_count's
846      MutexLocker ml(Heap_lock);
847      gc_count      = Universe::heap()->total_collections();
848      full_gc_count = Universe::heap()->total_full_collections();
849    }
850
851    // Generate a VM operation
852    VM_CollectForMetadataAllocation op(loader_data,
853                                       word_size,
854                                       mdtype,
855                                       gc_count,
856                                       full_gc_count,
857                                       GCCause::_metadata_GC_threshold);
858    VMThread::execute(&op);
859
860    // If GC was locked out, try again. Check before checking success because the
861    // prologue could have succeeded and the GC still have been locked out.
862    if (op.gc_locked()) {
863      continue;
864    }
865
866    if (op.prologue_succeeded()) {
867      return op.result();
868    }
869    loop_count++;
870    if ((QueuedAllocationWarningCount > 0) &&
871        (loop_count % QueuedAllocationWarningCount == 0)) {
872      log_warning(gc, ergo)("satisfy_failed_metadata_allocation() retries %d times,"
873                            " size=" SIZE_FORMAT, loop_count, word_size);
874    }
875  } while (true);  // Until a GC is done
876}
877
878// Return true if any of the following is true:
879// . the allocation won't fit into the current young gen heap
880// . gc locker is occupied (jni critical section)
881// . heap memory is tight -- the most recent previous collection
882//   was a full collection because a partial collection (would
883//   have) failed and is likely to fail again
884bool GenCollectorPolicy::should_try_older_generation_allocation(
885        size_t word_size) const {
886  GenCollectedHeap* gch = GenCollectedHeap::heap();
887  size_t young_capacity = gch->young_gen()->capacity_before_gc();
888  return    (word_size > heap_word_size(young_capacity))
889         || GCLocker::is_active_and_needs_gc()
890         || gch->incremental_collection_failed();
891}
892
893
894//
895// MarkSweepPolicy methods
896//
897
898void MarkSweepPolicy::initialize_alignments() {
899  _space_alignment = _gen_alignment = (size_t)Generation::GenGrain;
900  _heap_alignment = compute_heap_alignment();
901}
902
903void MarkSweepPolicy::initialize_generations() {
904  _young_gen_spec = new GenerationSpec(Generation::DefNew, _initial_young_size, _max_young_size, _gen_alignment);
905  _old_gen_spec   = new GenerationSpec(Generation::MarkSweepCompact, _initial_old_size, _max_old_size, _gen_alignment);
906}
907
908void MarkSweepPolicy::initialize_gc_policy_counters() {
909  // Initialize the policy counters - 2 collectors, 3 generations.
910  _gc_policy_counters = new GCPolicyCounters("Copy:MSC", 2, 3);
911}
912
913