psScavenge.inline.hpp revision 9727:f944761a3ce3
1274116Sdteske/*
2274116Sdteske * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
3274116Sdteske * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4274116Sdteske *
5274116Sdteske * This code is free software; you can redistribute it and/or modify it
6274116Sdteske * under the terms of the GNU General Public License version 2 only, as
7274116Sdteske * published by the Free Software Foundation.
8274116Sdteske *
9274116Sdteske * This code is distributed in the hope that it will be useful, but WITHOUT
10274116Sdteske * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11274116Sdteske * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12274116Sdteske * version 2 for more details (a copy is included in the LICENSE file that
13274116Sdteske * accompanied this code).
14274116Sdteske *
15274116Sdteske * You should have received a copy of the GNU General Public License version
16274116Sdteske * 2 along with this work; if not, write to the Free Software Foundation,
17274116Sdteske * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18274116Sdteske *
19274116Sdteske * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20274116Sdteske * or visit www.oracle.com if you need additional information or have any
21274116Sdteske * questions.
22274116Sdteske *
23274116Sdteske */
24274116Sdteske
25274116Sdteske#ifndef SHARE_VM_GC_PARALLEL_PSSCAVENGE_INLINE_HPP
26274116Sdteske#define SHARE_VM_GC_PARALLEL_PSSCAVENGE_INLINE_HPP
27274116Sdteske
28274116Sdteske#include "gc/parallel/cardTableExtension.hpp"
29274116Sdteske#include "gc/parallel/parallelScavengeHeap.hpp"
30274116Sdteske#include "gc/parallel/psPromotionManager.inline.hpp"
31274116Sdteske#include "gc/parallel/psScavenge.hpp"
32274116Sdteske#include "logging/log.hpp"
33274116Sdteske#include "memory/iterator.hpp"
34274116Sdteske#include "utilities/globalDefinitions.hpp"
35274116Sdteske
36274116Sdteskeinline void PSScavenge::save_to_space_top_before_gc() {
37274116Sdteske  ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
38274116Sdteske  _to_space_top_before_gc = heap->young_gen()->to_space()->top();
39274116Sdteske}
40274116Sdteske
41274116Sdtesketemplate <class T> inline bool PSScavenge::should_scavenge(T* p) {
42274116Sdteske  T heap_oop = oopDesc::load_heap_oop(p);
43274116Sdteske  return PSScavenge::is_obj_in_young(heap_oop);
44274116Sdteske}
45274116Sdteske
46274116Sdtesketemplate <class T>
47274116Sdteskeinline bool PSScavenge::should_scavenge(T* p, MutableSpace* to_space) {
48274116Sdteske  if (should_scavenge(p)) {
49274116Sdteske    oop obj = oopDesc::load_decode_heap_oop_not_null(p);
50274116Sdteske    // Skip objects copied to to_space since the scavenge started.
51275040Sdteske    HeapWord* const addr = (HeapWord*)obj;
52275040Sdteske    return addr < to_space_top_before_gc() || addr >= to_space->end();
53275040Sdteske  }
54274116Sdteske  return false;
55274116Sdteske}
56274116Sdteske
57template <class T>
58inline bool PSScavenge::should_scavenge(T* p, bool check_to_space) {
59  if (check_to_space) {
60    ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
61    return should_scavenge(p, heap->young_gen()->to_space());
62  }
63  return should_scavenge(p);
64}
65
66template<bool promote_immediately>
67class PSRootsClosure: public OopClosure {
68 private:
69  PSPromotionManager* _promotion_manager;
70
71 protected:
72  template <class T> void do_oop_work(T *p) {
73    if (PSScavenge::should_scavenge(p)) {
74      // We never card mark roots, maybe call a func without test?
75      _promotion_manager->copy_and_push_safe_barrier<T, promote_immediately>(p);
76    }
77  }
78 public:
79  PSRootsClosure(PSPromotionManager* pm) : _promotion_manager(pm) { }
80  void do_oop(oop* p)       { PSRootsClosure::do_oop_work(p); }
81  void do_oop(narrowOop* p) { PSRootsClosure::do_oop_work(p); }
82};
83
84typedef PSRootsClosure</*promote_immediately=*/false> PSScavengeRootsClosure;
85typedef PSRootsClosure</*promote_immediately=*/true> PSPromoteRootsClosure;
86
87// Scavenges a single oop in a Klass.
88class PSScavengeFromKlassClosure: public OopClosure {
89 private:
90  PSPromotionManager* _pm;
91  // Used to redirty a scanned klass if it has oops
92  // pointing to the young generation after being scanned.
93  Klass*             _scanned_klass;
94 public:
95  PSScavengeFromKlassClosure(PSPromotionManager* pm) : _pm(pm), _scanned_klass(NULL) { }
96  void do_oop(narrowOop* p) { ShouldNotReachHere(); }
97  void do_oop(oop* p)       {
98    ParallelScavengeHeap* psh = ParallelScavengeHeap::heap();
99    assert(!psh->is_in_reserved(p), "GC barrier needed");
100    if (PSScavenge::should_scavenge(p)) {
101      assert(PSScavenge::should_scavenge(p, true), "revisiting object?");
102
103      oop o = *p;
104      oop new_obj;
105      if (o->is_forwarded()) {
106        new_obj = o->forwardee();
107      } else {
108        new_obj = _pm->copy_to_survivor_space</*promote_immediately=*/false>(o);
109      }
110      oopDesc::encode_store_heap_oop_not_null(p, new_obj);
111
112      if (PSScavenge::is_obj_in_young(new_obj)) {
113        do_klass_barrier();
114      }
115    }
116  }
117
118  void set_scanned_klass(Klass* klass) {
119    assert(_scanned_klass == NULL || klass == NULL, "Should always only handling one klass at a time");
120    _scanned_klass = klass;
121  }
122
123 private:
124  void do_klass_barrier() {
125    assert(_scanned_klass != NULL, "Should not be called without having a scanned klass");
126    _scanned_klass->record_modified_oops();
127  }
128
129};
130
131// Scavenges the oop in a Klass.
132class PSScavengeKlassClosure: public KlassClosure {
133 private:
134  PSScavengeFromKlassClosure _oop_closure;
135 protected:
136 public:
137  PSScavengeKlassClosure(PSPromotionManager* pm) : _oop_closure(pm) { }
138  void do_klass(Klass* klass) {
139    // If the klass has not been dirtied we know that there's
140    // no references into  the young gen and we can skip it.
141
142    NOT_PRODUCT(ResourceMark rm);
143    log_develop_trace(gc, scavenge)("PSScavengeKlassClosure::do_klass " PTR_FORMAT ", %s, dirty: %s",
144                                    p2i(klass),
145                                    klass->external_name(),
146                                    klass->has_modified_oops() ? "true" : "false");
147
148    if (klass->has_modified_oops()) {
149      // Clean the klass since we're going to scavenge all the metadata.
150      klass->clear_modified_oops();
151
152      // Setup the promotion manager to redirty this klass
153      // if references are left in the young gen.
154      _oop_closure.set_scanned_klass(klass);
155
156      klass->oops_do(&_oop_closure);
157
158      _oop_closure.set_scanned_klass(NULL);
159    }
160  }
161};
162
163#endif // SHARE_VM_GC_PARALLEL_PSSCAVENGE_INLINE_HPP
164