psMarkSweepDecorator.cpp revision 8413:92457dfb91bd
1/*
2 * Copyright (c) 2001, 2015, 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 "classfile/systemDictionary.hpp"
27#include "gc/parallel/objectStartArray.hpp"
28#include "gc/parallel/parallelScavengeHeap.hpp"
29#include "gc/parallel/psMarkSweep.hpp"
30#include "gc/parallel/psMarkSweepDecorator.hpp"
31#include "gc/serial/markSweep.inline.hpp"
32#include "gc/shared/liveRange.hpp"
33#include "gc/shared/spaceDecorator.hpp"
34#include "oops/oop.inline.hpp"
35#include "runtime/prefetch.inline.hpp"
36
37PSMarkSweepDecorator* PSMarkSweepDecorator::_destination_decorator = NULL;
38
39
40void PSMarkSweepDecorator::set_destination_decorator_tenured() {
41  ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
42  _destination_decorator = heap->old_gen()->object_mark_sweep();
43}
44
45void PSMarkSweepDecorator::advance_destination_decorator() {
46  ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
47
48  assert(_destination_decorator != NULL, "Sanity");
49
50  PSMarkSweepDecorator* first = heap->old_gen()->object_mark_sweep();
51  PSMarkSweepDecorator* second = heap->young_gen()->eden_mark_sweep();
52  PSMarkSweepDecorator* third = heap->young_gen()->from_mark_sweep();
53  PSMarkSweepDecorator* fourth = heap->young_gen()->to_mark_sweep();
54
55  if ( _destination_decorator == first ) {
56    _destination_decorator = second;
57  } else if ( _destination_decorator == second ) {
58    _destination_decorator = third;
59  } else if ( _destination_decorator == third ) {
60    _destination_decorator = fourth;
61  } else {
62    fatal("PSMarkSweep attempting to advance past last compaction area");
63  }
64}
65
66PSMarkSweepDecorator* PSMarkSweepDecorator::destination_decorator() {
67  assert(_destination_decorator != NULL, "Sanity");
68
69  return _destination_decorator;
70}
71
72// FIX ME FIX ME FIX ME FIX ME!!!!!!!!!
73// The object forwarding code is duplicated. Factor this out!!!!!
74//
75// This method "precompacts" objects inside its space to dest. It places forwarding
76// pointers into markOops for use by adjust_pointers. If "dest" should overflow, we
77// finish by compacting into our own space.
78
79void PSMarkSweepDecorator::precompact() {
80  // Reset our own compact top.
81  set_compaction_top(space()->bottom());
82
83  /* We allow some amount of garbage towards the bottom of the space, so
84   * we don't start compacting before there is a significant gain to be made.
85   * Occasionally, we want to ensure a full compaction, which is determined
86   * by the MarkSweepAlwaysCompactCount parameter. This is a significant
87   * performance improvement!
88   */
89  bool skip_dead = ((PSMarkSweep::total_invocations() % MarkSweepAlwaysCompactCount) != 0);
90
91  size_t allowed_deadspace = 0;
92  if (skip_dead) {
93    const size_t ratio = allowed_dead_ratio();
94    allowed_deadspace = space()->capacity_in_words() * ratio / 100;
95  }
96
97  // Fetch the current destination decorator
98  PSMarkSweepDecorator* dest = destination_decorator();
99  ObjectStartArray* start_array = dest->start_array();
100
101  HeapWord* compact_top = dest->compaction_top();
102  HeapWord* compact_end = dest->space()->end();
103
104  HeapWord* q = space()->bottom();
105  HeapWord* t = space()->top();
106
107  HeapWord*  end_of_live= q;    /* One byte beyond the last byte of the last
108                                   live object. */
109  HeapWord*  first_dead = space()->end(); /* The first dead object. */
110  LiveRange* liveRange  = NULL; /* The current live range, recorded in the
111                                   first header of preceding free area. */
112  _first_dead = first_dead;
113
114  const intx interval = PrefetchScanIntervalInBytes;
115
116  while (q < t) {
117    assert(oop(q)->mark()->is_marked() || oop(q)->mark()->is_unlocked() ||
118           oop(q)->mark()->has_bias_pattern(),
119           "these are the only valid states during a mark sweep");
120    if (oop(q)->is_gc_marked()) {
121      /* prefetch beyond q */
122      Prefetch::write(q, interval);
123      size_t size = oop(q)->size();
124
125      size_t compaction_max_size = pointer_delta(compact_end, compact_top);
126
127      // This should only happen if a space in the young gen overflows the
128      // old gen. If that should happen, we null out the start_array, because
129      // the young spaces are not covered by one.
130      while(size > compaction_max_size) {
131        // First record the last compact_top
132        dest->set_compaction_top(compact_top);
133
134        // Advance to the next compaction decorator
135        advance_destination_decorator();
136        dest = destination_decorator();
137
138        // Update compaction info
139        start_array = dest->start_array();
140        compact_top = dest->compaction_top();
141        compact_end = dest->space()->end();
142        assert(compact_top == dest->space()->bottom(), "Advanced to space already in use");
143        assert(compact_end > compact_top, "Must always be space remaining");
144        compaction_max_size =
145          pointer_delta(compact_end, compact_top);
146      }
147
148      // store the forwarding pointer into the mark word
149      if (q != compact_top) {
150        oop(q)->forward_to(oop(compact_top));
151        assert(oop(q)->is_gc_marked(), "encoding the pointer should preserve the mark");
152      } else {
153        // if the object isn't moving we can just set the mark to the default
154        // mark and handle it specially later on.
155        oop(q)->init_mark();
156        assert(oop(q)->forwardee() == NULL, "should be forwarded to NULL");
157      }
158
159      // Update object start array
160      if (start_array) {
161        start_array->allocate_block(compact_top);
162      }
163
164      compact_top += size;
165      assert(compact_top <= dest->space()->end(),
166        "Exceeding space in destination");
167
168      q += size;
169      end_of_live = q;
170    } else {
171      /* run over all the contiguous dead objects */
172      HeapWord* end = q;
173      do {
174        /* prefetch beyond end */
175        Prefetch::write(end, interval);
176        end += oop(end)->size();
177      } while (end < t && (!oop(end)->is_gc_marked()));
178
179      /* see if we might want to pretend this object is alive so that
180       * we don't have to compact quite as often.
181       */
182      if (allowed_deadspace > 0 && q == compact_top) {
183        size_t sz = pointer_delta(end, q);
184        if (insert_deadspace(allowed_deadspace, q, sz)) {
185          size_t compaction_max_size = pointer_delta(compact_end, compact_top);
186
187          // This should only happen if a space in the young gen overflows the
188          // old gen. If that should happen, we null out the start_array, because
189          // the young spaces are not covered by one.
190          while (sz > compaction_max_size) {
191            // First record the last compact_top
192            dest->set_compaction_top(compact_top);
193
194            // Advance to the next compaction decorator
195            advance_destination_decorator();
196            dest = destination_decorator();
197
198            // Update compaction info
199            start_array = dest->start_array();
200            compact_top = dest->compaction_top();
201            compact_end = dest->space()->end();
202            assert(compact_top == dest->space()->bottom(), "Advanced to space already in use");
203            assert(compact_end > compact_top, "Must always be space remaining");
204            compaction_max_size =
205              pointer_delta(compact_end, compact_top);
206          }
207
208          // store the forwarding pointer into the mark word
209          if (q != compact_top) {
210            oop(q)->forward_to(oop(compact_top));
211            assert(oop(q)->is_gc_marked(), "encoding the pointer should preserve the mark");
212          } else {
213            // if the object isn't moving we can just set the mark to the default
214            // mark and handle it specially later on.
215            oop(q)->init_mark();
216            assert(oop(q)->forwardee() == NULL, "should be forwarded to NULL");
217          }
218
219          // Update object start array
220          if (start_array) {
221            start_array->allocate_block(compact_top);
222          }
223
224          compact_top += sz;
225          assert(compact_top <= dest->space()->end(),
226            "Exceeding space in destination");
227
228          q = end;
229          end_of_live = end;
230          continue;
231        }
232      }
233
234      /* for the previous LiveRange, record the end of the live objects. */
235      if (liveRange) {
236        liveRange->set_end(q);
237      }
238
239      /* record the current LiveRange object.
240       * liveRange->start() is overlaid on the mark word.
241       */
242      liveRange = (LiveRange*)q;
243      liveRange->set_start(end);
244      liveRange->set_end(end);
245
246      /* see if this is the first dead region. */
247      if (q < first_dead) {
248        first_dead = q;
249      }
250
251      /* move on to the next object */
252      q = end;
253    }
254  }
255
256  assert(q == t, "just checking");
257  if (liveRange != NULL) {
258    liveRange->set_end(q);
259  }
260  _end_of_live = end_of_live;
261  if (end_of_live < first_dead) {
262    first_dead = end_of_live;
263  }
264  _first_dead = first_dead;
265
266  // Update compaction top
267  dest->set_compaction_top(compact_top);
268}
269
270bool PSMarkSweepDecorator::insert_deadspace(size_t& allowed_deadspace_words,
271                                            HeapWord* q, size_t deadlength) {
272  if (allowed_deadspace_words >= deadlength) {
273    allowed_deadspace_words -= deadlength;
274    CollectedHeap::fill_with_object(q, deadlength);
275    oop(q)->set_mark(oop(q)->mark()->set_marked());
276    assert((int) deadlength == oop(q)->size(), "bad filler object size");
277    // Recall that we required "q == compaction_top".
278    return true;
279  } else {
280    allowed_deadspace_words = 0;
281    return false;
282  }
283}
284
285void PSMarkSweepDecorator::adjust_pointers() {
286  // adjust all the interior pointers to point at the new locations of objects
287  // Used by MarkSweep::mark_sweep_phase3()
288
289  HeapWord* q = space()->bottom();
290  HeapWord* t = _end_of_live;  // Established by "prepare_for_compaction".
291
292  assert(_first_dead <= _end_of_live, "Stands to reason, no?");
293
294  if (q < t && _first_dead > q &&
295      !oop(q)->is_gc_marked()) {
296    // we have a chunk of the space which hasn't moved and we've
297    // reinitialized the mark word during the previous pass, so we can't
298    // use is_gc_marked for the traversal.
299    HeapWord* end = _first_dead;
300
301    while (q < end) {
302      // point all the oops to the new location
303      size_t size = MarkSweep::adjust_pointers(oop(q));
304      q += size;
305    }
306
307    if (_first_dead == t) {
308      q = t;
309    } else {
310      // $$$ This is funky.  Using this to read the previously written
311      // LiveRange.  See also use below.
312      q = (HeapWord*)oop(_first_dead)->mark()->decode_pointer();
313    }
314  }
315  const intx interval = PrefetchScanIntervalInBytes;
316
317  debug_only(HeapWord* prev_q = NULL);
318  while (q < t) {
319    // prefetch beyond q
320    Prefetch::write(q, interval);
321    if (oop(q)->is_gc_marked()) {
322      // q is alive
323      // point all the oops to the new location
324      size_t size = MarkSweep::adjust_pointers(oop(q));
325      debug_only(prev_q = q);
326      q += size;
327    } else {
328      // q is not a live object, so its mark should point at the next
329      // live object
330      debug_only(prev_q = q);
331      q = (HeapWord*) oop(q)->mark()->decode_pointer();
332      assert(q > prev_q, "we should be moving forward through memory");
333    }
334  }
335
336  assert(q == t, "just checking");
337}
338
339void PSMarkSweepDecorator::compact(bool mangle_free_space ) {
340  // Copy all live objects to their new location
341  // Used by MarkSweep::mark_sweep_phase4()
342
343  HeapWord*       q = space()->bottom();
344  HeapWord* const t = _end_of_live;
345  debug_only(HeapWord* prev_q = NULL);
346
347  if (q < t && _first_dead > q &&
348      !oop(q)->is_gc_marked()) {
349#ifdef ASSERT
350    // we have a chunk of the space which hasn't moved and we've reinitialized the
351    // mark word during the previous pass, so we can't use is_gc_marked for the
352    // traversal.
353    HeapWord* const end = _first_dead;
354
355    while (q < end) {
356      size_t size = oop(q)->size();
357      assert(!oop(q)->is_gc_marked(), "should be unmarked (special dense prefix handling)");
358      debug_only(prev_q = q);
359      q += size;
360    }
361#endif
362
363    if (_first_dead == t) {
364      q = t;
365    } else {
366      // $$$ Funky
367      q = (HeapWord*) oop(_first_dead)->mark()->decode_pointer();
368    }
369  }
370
371  const intx scan_interval = PrefetchScanIntervalInBytes;
372  const intx copy_interval = PrefetchCopyIntervalInBytes;
373
374  while (q < t) {
375    if (!oop(q)->is_gc_marked()) {
376      // mark is pointer to next marked oop
377      debug_only(prev_q = q);
378      q = (HeapWord*) oop(q)->mark()->decode_pointer();
379      assert(q > prev_q, "we should be moving forward through memory");
380    } else {
381      // prefetch beyond q
382      Prefetch::read(q, scan_interval);
383
384      // size and destination
385      size_t size = oop(q)->size();
386      HeapWord* compaction_top = (HeapWord*)oop(q)->forwardee();
387
388      // prefetch beyond compaction_top
389      Prefetch::write(compaction_top, copy_interval);
390
391      // copy object and reinit its mark
392      assert(q != compaction_top, "everything in this pass should be moving");
393      Copy::aligned_conjoint_words(q, compaction_top, size);
394      oop(compaction_top)->init_mark();
395      assert(oop(compaction_top)->klass() != NULL, "should have a class");
396
397      debug_only(prev_q = q);
398      q += size;
399    }
400  }
401
402  assert(compaction_top() >= space()->bottom() && compaction_top() <= space()->end(),
403         "should point inside space");
404  space()->set_top(compaction_top());
405
406  if (mangle_free_space) {
407    space()->mangle_unused_area();
408  }
409}
410