Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/shared/markSweep.cpp
38920 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "compiler/compileBroker.hpp"26#include "gc_implementation/shared/gcTimer.hpp"27#include "gc_implementation/shared/gcTrace.hpp"28#include "gc_implementation/shared/markSweep.inline.hpp"29#include "gc_interface/collectedHeap.inline.hpp"30#include "oops/methodData.hpp"31#include "oops/objArrayKlass.inline.hpp"32#include "oops/oop.inline.hpp"3334PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC3536uint MarkSweep::_total_invocations = 0;3738Stack<oop, mtGC> MarkSweep::_marking_stack;39Stack<ObjArrayTask, mtGC> MarkSweep::_objarray_stack;4041Stack<oop, mtGC> MarkSweep::_preserved_oop_stack;42Stack<markOop, mtGC> MarkSweep::_preserved_mark_stack;43size_t MarkSweep::_preserved_count = 0;44size_t MarkSweep::_preserved_count_max = 0;45PreservedMark* MarkSweep::_preserved_marks = NULL;46ReferenceProcessor* MarkSweep::_ref_processor = NULL;47STWGCTimer* MarkSweep::_gc_timer = NULL;48SerialOldTracer* MarkSweep::_gc_tracer = NULL;4950MarkSweep::FollowRootClosure MarkSweep::follow_root_closure;5152void MarkSweep::FollowRootClosure::do_oop(oop* p) { follow_root(p); }53void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); }5455MarkSweep::MarkAndPushClosure MarkSweep::mark_and_push_closure;56CLDToOopClosure MarkSweep::follow_cld_closure(&mark_and_push_closure);57CLDToOopClosure MarkSweep::adjust_cld_closure(&adjust_pointer_closure);5859void MarkSweep::MarkAndPushClosure::do_oop(oop* p) { mark_and_push(p); }60void MarkSweep::MarkAndPushClosure::do_oop(narrowOop* p) { mark_and_push(p); }6162void MarkSweep::follow_class_loader(ClassLoaderData* cld) {63MarkSweep::follow_cld_closure.do_cld(cld);64}6566void MarkSweep::follow_stack() {67do {68while (!_marking_stack.is_empty()) {69oop obj = _marking_stack.pop();70assert (obj->is_gc_marked(), "p must be marked");71obj->follow_contents();72}73// Process ObjArrays one at a time to avoid marking stack bloat.74if (!_objarray_stack.is_empty()) {75ObjArrayTask task = _objarray_stack.pop();76ObjArrayKlass* k = (ObjArrayKlass*)task.obj()->klass();77k->oop_follow_contents(task.obj(), task.index());78}79} while (!_marking_stack.is_empty() || !_objarray_stack.is_empty());80}8182MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure;8384void MarkSweep::FollowStackClosure::do_void() { follow_stack(); }8586// We preserve the mark which should be replaced at the end and the location87// that it will go. Note that the object that this markOop belongs to isn't88// currently at that address but it will be after phase489void MarkSweep::preserve_mark(oop obj, markOop mark) {90// We try to store preserved marks in the to space of the new generation since91// this is storage which should be available. Most of the time this should be92// sufficient space for the marks we need to preserve but if it isn't we fall93// back to using Stacks to keep track of the overflow.94if (_preserved_count < _preserved_count_max) {95_preserved_marks[_preserved_count++].init(obj, mark);96} else {97_preserved_mark_stack.push(mark);98_preserved_oop_stack.push(obj);99}100}101102MarkSweep::AdjustPointerClosure MarkSweep::adjust_pointer_closure;103104void MarkSweep::AdjustPointerClosure::do_oop(oop* p) { adjust_pointer(p); }105void MarkSweep::AdjustPointerClosure::do_oop(narrowOop* p) { adjust_pointer(p); }106107void MarkSweep::adjust_marks() {108assert( _preserved_oop_stack.size() == _preserved_mark_stack.size(),109"inconsistent preserved oop stacks");110111// adjust the oops we saved earlier112for (size_t i = 0; i < _preserved_count; i++) {113_preserved_marks[i].adjust_pointer();114}115116// deal with the overflow stack117StackIterator<oop, mtGC> iter(_preserved_oop_stack);118while (!iter.is_empty()) {119oop* p = iter.next_addr();120adjust_pointer(p);121}122}123124void MarkSweep::restore_marks() {125assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),126"inconsistent preserved oop stacks");127if (PrintGC && Verbose) {128gclog_or_tty->print_cr("Restoring %d marks",129_preserved_count + _preserved_oop_stack.size());130}131132// restore the marks we saved earlier133for (size_t i = 0; i < _preserved_count; i++) {134_preserved_marks[i].restore();135}136137// deal with the overflow138while (!_preserved_oop_stack.is_empty()) {139oop obj = _preserved_oop_stack.pop();140markOop mark = _preserved_mark_stack.pop();141obj->set_mark(mark);142}143}144145MarkSweep::IsAliveClosure MarkSweep::is_alive;146147bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked(); }148149MarkSweep::KeepAliveClosure MarkSweep::keep_alive;150151void MarkSweep::KeepAliveClosure::do_oop(oop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); }152void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); }153154void marksweep_init() {155MarkSweep::_gc_timer = new (ResourceObj::C_HEAP, mtGC) STWGCTimer();156MarkSweep::_gc_tracer = new (ResourceObj::C_HEAP, mtGC) SerialOldTracer();157}158159#ifndef PRODUCT160161void MarkSweep::trace(const char* msg) {162if (TraceMarkSweep)163gclog_or_tty->print("%s", msg);164}165166#endif167168169