markSweep.cpp 6.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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.
 *
19 20 21
 * 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.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28
#include "precompiled.hpp"
#include "compiler/compileBroker.hpp"
#include "gc_implementation/shared/markSweep.inline.hpp"
#include "gc_interface/collectedHeap.inline.hpp"
29
#include "oops/methodData.hpp"
30 31
#include "oops/objArrayKlass.inline.hpp"
#include "oops/oop.inline.hpp"
D
duke 已提交
32

33
uint                    MarkSweep::_total_invocations = 0;
34

Z
zgu 已提交
35 36
Stack<oop, mtGC>              MarkSweep::_marking_stack;
Stack<ObjArrayTask, mtGC>     MarkSweep::_objarray_stack;
D
duke 已提交
37

Z
zgu 已提交
38 39
Stack<oop, mtGC>              MarkSweep::_preserved_oop_stack;
Stack<markOop, mtGC>          MarkSweep::_preserved_mark_stack;
D
duke 已提交
40 41 42 43 44
size_t                  MarkSweep::_preserved_count = 0;
size_t                  MarkSweep::_preserved_count_max = 0;
PreservedMark*          MarkSweep::_preserved_marks = NULL;
ReferenceProcessor*     MarkSweep::_ref_processor   = NULL;

45
MarkSweep::FollowRootClosure  MarkSweep::follow_root_closure;
46
CodeBlobToOopClosure MarkSweep::follow_code_root_closure(&MarkSweep::follow_root_closure, /*do_marking=*/ true);
D
duke 已提交
47

48 49
void MarkSweep::FollowRootClosure::do_oop(oop* p)       { follow_root(p); }
void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); }
D
duke 已提交
50 51

MarkSweep::MarkAndPushClosure MarkSweep::mark_and_push_closure;
52 53
MarkSweep::FollowKlassClosure MarkSweep::follow_klass_closure;
MarkSweep::AdjustKlassClosure MarkSweep::adjust_klass_closure;
D
duke 已提交
54

55
void MarkSweep::MarkAndPushClosure::do_oop(oop* p)       { mark_and_push(p); }
56
void MarkSweep::MarkAndPushClosure::do_oop(narrowOop* p) { mark_and_push(p); }
D
duke 已提交
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
void MarkSweep::FollowKlassClosure::do_klass(Klass* klass) {
  klass->oops_do(&MarkSweep::mark_and_push_closure);
}
void MarkSweep::AdjustKlassClosure::do_klass(Klass* klass) {
  klass->oops_do(&MarkSweep::adjust_pointer_closure);
}

void MarkSweep::follow_klass(Klass* klass) {
  ClassLoaderData* cld = klass->class_loader_data();
  // The actual processing of the klass is done when we
  // traverse the list of Klasses in the class loader data.
  MarkSweep::follow_class_loader(cld);
}

void MarkSweep::adjust_klass(Klass* klass) {
  ClassLoaderData* cld = klass->class_loader_data();
  // The actual processing of the klass is done when we
  // traverse the list of Klasses in the class loader data.
  MarkSweep::adjust_class_loader(cld);
}

void MarkSweep::follow_class_loader(ClassLoaderData* cld) {
  cld->oops_do(&MarkSweep::mark_and_push_closure, &MarkSweep::follow_klass_closure, true);
}

void MarkSweep::adjust_class_loader(ClassLoaderData* cld) {
84
  cld->oops_do(&MarkSweep::adjust_pointer_closure, &MarkSweep::adjust_klass_closure, true);
85 86 87
}


D
duke 已提交
88
void MarkSweep::follow_stack() {
89
  do {
90 91
    while (!_marking_stack.is_empty()) {
      oop obj = _marking_stack.pop();
92 93 94
      assert (obj->is_gc_marked(), "p must be marked");
      obj->follow_contents();
    }
95
    // Process ObjArrays one at a time to avoid marking stack bloat.
96 97
    if (!_objarray_stack.is_empty()) {
      ObjArrayTask task = _objarray_stack.pop();
98
      ObjArrayKlass* const k = (ObjArrayKlass*)task.obj()->klass();
99 100
      k->oop_follow_contents(task.obj(), task.index());
    }
101
  } while (!_marking_stack.is_empty() || !_objarray_stack.is_empty());
D
duke 已提交
102 103 104 105
}

MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure;

106
void MarkSweep::FollowStackClosure::do_void() { follow_stack(); }
D
duke 已提交
107

108 109 110
// We preserve the mark which should be replaced at the end and the location
// that it will go.  Note that the object that this markOop belongs to isn't
// currently at that address but it will be after phase4
D
duke 已提交
111
void MarkSweep::preserve_mark(oop obj, markOop mark) {
112 113 114 115
  // We try to store preserved marks in the to space of the new generation since
  // this is storage which should be available.  Most of the time this should be
  // sufficient space for the marks we need to preserve but if it isn't we fall
  // back to using Stacks to keep track of the overflow.
D
duke 已提交
116 117 118
  if (_preserved_count < _preserved_count_max) {
    _preserved_marks[_preserved_count++].init(obj, mark);
  } else {
119 120
    _preserved_mark_stack.push(mark);
    _preserved_oop_stack.push(obj);
D
duke 已提交
121 122 123
  }
}

124
MarkSweep::AdjustPointerClosure MarkSweep::adjust_pointer_closure;
D
duke 已提交
125

126 127
void MarkSweep::AdjustPointerClosure::do_oop(oop* p)       { adjust_pointer(p); }
void MarkSweep::AdjustPointerClosure::do_oop(narrowOop* p) { adjust_pointer(p); }
128

D
duke 已提交
129
void MarkSweep::adjust_marks() {
130
  assert( _preserved_oop_stack.size() == _preserved_mark_stack.size(),
D
duke 已提交
131 132 133 134 135 136 137 138
         "inconsistent preserved oop stacks");

  // adjust the oops we saved earlier
  for (size_t i = 0; i < _preserved_count; i++) {
    _preserved_marks[i].adjust_pointer();
  }

  // deal with the overflow stack
Z
zgu 已提交
139
  StackIterator<oop, mtGC> iter(_preserved_oop_stack);
140 141 142
  while (!iter.is_empty()) {
    oop* p = iter.next_addr();
    adjust_pointer(p);
D
duke 已提交
143 144 145 146
  }
}

void MarkSweep::restore_marks() {
147
  assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),
D
duke 已提交
148 149
         "inconsistent preserved oop stacks");
  if (PrintGC && Verbose) {
150 151
    gclog_or_tty->print_cr("Restoring %d marks",
                           _preserved_count + _preserved_oop_stack.size());
D
duke 已提交
152 153 154 155 156 157 158 159
  }

  // restore the marks we saved earlier
  for (size_t i = 0; i < _preserved_count; i++) {
    _preserved_marks[i].restore();
  }

  // deal with the overflow
160 161 162 163
  while (!_preserved_oop_stack.is_empty()) {
    oop obj       = _preserved_oop_stack.pop();
    markOop mark  = _preserved_mark_stack.pop();
    obj->set_mark(mark);
D
duke 已提交
164 165 166
  }
}

167
MarkSweep::IsAliveClosure   MarkSweep::is_alive;
D
duke 已提交
168

169
bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked(); }
D
duke 已提交
170 171 172

MarkSweep::KeepAliveClosure MarkSweep::keep_alive;

173 174 175
void MarkSweep::KeepAliveClosure::do_oop(oop* p)       { MarkSweep::KeepAliveClosure::do_oop_work(p); }
void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); }

D
duke 已提交
176 177 178 179 180 181 182 183 184 185
void marksweep_init() { /* empty */ }

#ifndef PRODUCT

void MarkSweep::trace(const char* msg) {
  if (TraceMarkSweep)
    gclog_or_tty->print("%s", msg);
}

#endif