aboutsummaryrefslogtreecommitdiff
path: root/src/share/vm/gc_implementation/parallelScavenge/adjoiningGenerations.cpp
blob: ee9ce8b89d425f83ad592bd9f4332d8bda1de05b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"
#include "gc_implementation/parallelScavenge/adjoiningGenerations.hpp"
#include "gc_implementation/parallelScavenge/adjoiningVirtualSpaces.hpp"
#include "gc_implementation/parallelScavenge/generationSizer.hpp"
#include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"

// If boundary moving is being used, create the young gen and old
// gen with ASPSYoungGen and ASPSOldGen, respectively.  Revert to
// the old behavior otherwise (with PSYoungGen and PSOldGen).

AdjoiningGenerations::AdjoiningGenerations(ReservedSpace old_young_rs,
                                           GenerationSizer* policy,
                                           size_t alignment) :
  _virtual_spaces(old_young_rs, policy->min_gen1_size(),
                  policy->min_gen0_size(), alignment) {
  size_t init_low_byte_size = policy->initial_gen1_size();
  size_t min_low_byte_size = policy->min_gen1_size();
  size_t max_low_byte_size = policy->max_gen1_size();
  size_t init_high_byte_size = policy->initial_gen0_size();
  size_t min_high_byte_size = policy->min_gen0_size();
  size_t max_high_byte_size = policy->max_gen0_size();

  assert(min_low_byte_size <= init_low_byte_size &&
         init_low_byte_size <= max_low_byte_size, "Parameter check");
  assert(min_high_byte_size <= init_high_byte_size &&
         init_high_byte_size <= max_high_byte_size, "Parameter check");
  // Create the generations differently based on the option to
  // move the boundary.
  if (UseAdaptiveGCBoundary) {
    // Initialize the adjoining virtual spaces.  Then pass the
    // a virtual to each generation for initialization of the
    // generation.

    // Does the actual creation of the virtual spaces
    _virtual_spaces.initialize(max_low_byte_size,
                               init_low_byte_size,
                               init_high_byte_size);

    // Place the young gen at the high end.  Passes in the virtual space.
    _young_gen = new ASPSYoungGen(_virtual_spaces.high(),
                                  _virtual_spaces.high()->committed_size(),
                                  min_high_byte_size,
                                  _virtual_spaces.high_byte_size_limit());

    // Place the old gen at the low end. Passes in the virtual space.
    _old_gen = new ASPSOldGen(_virtual_spaces.low(),
                              _virtual_spaces.low()->committed_size(),
                              min_low_byte_size,
                              _virtual_spaces.low_byte_size_limit(),
                              "old", 1);

    young_gen()->initialize_work();
    assert(young_gen()->reserved().byte_size() <= young_gen()->gen_size_limit(),
     "Consistency check");
    assert(old_young_rs.size() >= young_gen()->gen_size_limit(),
     "Consistency check");

    old_gen()->initialize_work("old", 1);
    assert(old_gen()->reserved().byte_size() <= old_gen()->gen_size_limit(),
     "Consistency check");
    assert(old_young_rs.size() >= old_gen()->gen_size_limit(),
     "Consistency check");
  } else {

    // Layout the reserved space for the generations.
    ReservedSpace old_rs   =
      virtual_spaces()->reserved_space().first_part(max_low_byte_size);
    ReservedSpace heap_rs  =
      virtual_spaces()->reserved_space().last_part(max_low_byte_size);
    ReservedSpace young_rs = heap_rs.first_part(max_high_byte_size);
    assert(young_rs.size() == heap_rs.size(), "Didn't reserve all of the heap");

    // Create the generations.  Virtual spaces are not passed in.
    _young_gen = new PSYoungGen(init_high_byte_size,
                                min_high_byte_size,
                                max_high_byte_size);
    _old_gen = new PSOldGen(init_low_byte_size,
                            min_low_byte_size,
                            max_low_byte_size,
                            "old", 1);

    // The virtual spaces are created by the initialization of the gens.
    _young_gen->initialize(young_rs, alignment);
    assert(young_gen()->gen_size_limit() == young_rs.size(),
      "Consistency check");
    _old_gen->initialize(old_rs, alignment, "old", 1);
    assert(old_gen()->gen_size_limit() == old_rs.size(), "Consistency check");
  }
}

size_t AdjoiningGenerations::reserved_byte_size() {
  return virtual_spaces()->reserved_space().size();
}


// Make checks on the current sizes of the generations and
// the contraints on the sizes of the generations.  Push
// up the boundary within the contraints.  A partial
// push can occur.
void AdjoiningGenerations::request_old_gen_expansion(size_t expand_in_bytes) {
  assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");

  assert_lock_strong(ExpandHeap_lock);
  assert_locked_or_safepoint(Heap_lock);

  // These sizes limit the amount the boundaries can move.  Effectively,
  // the generation says how much it is willing to yield to the other
  // generation.
  const size_t young_gen_available = young_gen()->available_for_contraction();
  const size_t old_gen_available = old_gen()->available_for_expansion();
  const size_t alignment = virtual_spaces()->alignment();
  size_t change_in_bytes = MIN3(young_gen_available,
                                old_gen_available,
                                align_size_up_(expand_in_bytes, alignment));

  if (change_in_bytes == 0) {
    return;
  }

  if (TraceAdaptiveGCBoundary) {
    gclog_or_tty->print_cr("Before expansion of old gen with boundary move");
    gclog_or_tty->print_cr("  Requested change: 0x%x  Attempted change: 0x%x",
      expand_in_bytes, change_in_bytes);
    if (!PrintHeapAtGC) {
      Universe::print_on(gclog_or_tty);
    }
    gclog_or_tty->print_cr("  PSOldGen max size: " SIZE_FORMAT "K",
      old_gen()->max_gen_size()/K);
  }

  // Move the boundary between the generations up (smaller young gen).
  if (virtual_spaces()->adjust_boundary_up(change_in_bytes)) {
    young_gen()->reset_after_change();
    old_gen()->reset_after_change();
  }

  // The total reserved for the generations should match the sum
  // of the two even if the boundary is moving.
  assert(reserved_byte_size() ==
         old_gen()->max_gen_size() + young_gen()->max_size(),
         "Space is missing");
  young_gen()->space_invariants();
  old_gen()->space_invariants();

  if (TraceAdaptiveGCBoundary) {
    gclog_or_tty->print_cr("After expansion of old gen with boundary move");
    if (!PrintHeapAtGC) {
      Universe::print_on(gclog_or_tty);
    }
    gclog_or_tty->print_cr("  PSOldGen max size: " SIZE_FORMAT "K",
      old_gen()->max_gen_size()/K);
  }
}

// See comments on request_old_gen_expansion()
bool AdjoiningGenerations::request_young_gen_expansion(size_t expand_in_bytes) {
  assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");

  // If eden is not empty, the boundary can be moved but no advantage
  // can be made of the move since eden cannot be moved.
  if (!young_gen()->eden_space()->is_empty()) {
    return false;
  }


  bool result = false;
  const size_t young_gen_available = young_gen()->available_for_expansion();
  const size_t old_gen_available = old_gen()->available_for_contraction();
  const size_t alignment = virtual_spaces()->alignment();
  size_t change_in_bytes = MIN3(young_gen_available,
                                old_gen_available,
                                align_size_up_(expand_in_bytes, alignment));

  if (change_in_bytes == 0) {
    return false;
  }

  if (TraceAdaptiveGCBoundary) {
    gclog_or_tty->print_cr("Before expansion of young gen with boundary move");
    gclog_or_tty->print_cr("  Requested change: 0x%x  Attempted change: 0x%x",
      expand_in_bytes, change_in_bytes);
    if (!PrintHeapAtGC) {
      Universe::print_on(gclog_or_tty);
    }
    gclog_or_tty->print_cr("  PSYoungGen max size: " SIZE_FORMAT "K",
      young_gen()->max_size()/K);
  }

  // Move the boundary between the generations down (smaller old gen).
  MutexLocker x(ExpandHeap_lock);
  if (virtual_spaces()->adjust_boundary_down(change_in_bytes)) {
    young_gen()->reset_after_change();
    old_gen()->reset_after_change();
    result = true;
  }

  // The total reserved for the generations should match the sum
  // of the two even if the boundary is moving.
  assert(reserved_byte_size() ==
         old_gen()->max_gen_size() + young_gen()->max_size(),
         "Space is missing");
  young_gen()->space_invariants();
  old_gen()->space_invariants();

  if (TraceAdaptiveGCBoundary) {
    gclog_or_tty->print_cr("After expansion of young gen with boundary move");
    if (!PrintHeapAtGC) {
      Universe::print_on(gclog_or_tty);
    }
    gclog_or_tty->print_cr("  PSYoungGen max size: " SIZE_FORMAT "K",
      young_gen()->max_size()/K);
  }

  return result;
}

// Additional space is needed in the old generation.  Try to move the boundary
// up to meet the need.  Moves boundary up only
void AdjoiningGenerations::adjust_boundary_for_old_gen_needs(
  size_t desired_free_space) {
  assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");

  // Stress testing.
  if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 1) {
    MutexLocker x(ExpandHeap_lock);
    request_old_gen_expansion(virtual_spaces()->alignment() * 3 / 2);
  }

  // Expand only if the entire generation is already committed.
  if (old_gen()->virtual_space()->uncommitted_size() == 0) {
    if (old_gen()->free_in_bytes() < desired_free_space) {
      MutexLocker x(ExpandHeap_lock);
      request_old_gen_expansion(desired_free_space);
    }
  }
}

// See comment on adjust_boundary_for_old_gen_needss().
// Adjust boundary down only.
void AdjoiningGenerations::adjust_boundary_for_young_gen_needs(size_t eden_size,
    size_t survivor_size) {

  assert(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary, "runtime check");

  // Stress testing.
  if (PSAdaptiveSizePolicyResizeVirtualSpaceAlot == 0) {
    request_young_gen_expansion(virtual_spaces()->alignment() * 3 / 2);
    eden_size = young_gen()->eden_space()->capacity_in_bytes();
  }

  // Expand only if the entire generation is already committed.
  if (young_gen()->virtual_space()->uncommitted_size() == 0) {
    size_t desired_size = eden_size + 2 * survivor_size;
    const size_t committed = young_gen()->virtual_space()->committed_size();
    if (desired_size > committed) {
      request_young_gen_expansion(desired_size - committed);
    }
  }
}