ysr@777: /* stefank@2314: * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_BUFFERINGOOPCLOSURE_HPP stefank@2314: #define SHARE_VM_GC_IMPLEMENTATION_G1_BUFFERINGOOPCLOSURE_HPP stefank@2314: stefank@2314: #include "memory/genOopClosures.hpp" stefank@2314: #include "memory/generation.hpp" stefank@2314: #include "runtime/os.hpp" stefank@2314: #include "utilities/taskqueue.hpp" stefank@2314: ysr@777: // A BufferingOops closure tries to separate out the cost of finding roots ysr@777: // from the cost of applying closures to them. It maintains an array of ysr@777: // ref-containing locations. Until the array is full, applying the closure ysr@777: // to an oop* merely records that location in the array. Since this ysr@777: // closure app cost is small, an elapsed timer can approximately attribute ysr@777: // all of this cost to the cost of finding the roots. When the array fills ysr@777: // up, the wrapped closure is applied to all elements, keeping track of ysr@777: // this elapsed time of this process, and leaving the array empty. ysr@777: // The caller must be sure to call "done" to process any unprocessed ysr@777: // buffered entriess. ysr@777: ysr@777: class Generation; ysr@777: class HeapRegion; ysr@777: ysr@777: class BufferingOopClosure: public OopClosure { ysr@777: protected: ysr@777: enum PrivateConstants { ysr@777: BufferLength = 1024 ysr@777: }; ysr@777: ysr@1280: StarTask _buffer[BufferLength]; ysr@1280: StarTask* _buffer_top; ysr@1280: StarTask* _buffer_curr; ysr@777: ysr@1280: OopClosure* _oc; ysr@1280: double _closure_app_seconds; ysr@777: ysr@777: void process_buffer () { ysr@777: double start = os::elapsedTime(); ysr@1280: for (StarTask* curr = _buffer; curr < _buffer_curr; ++curr) { ysr@1280: if (curr->is_narrow()) { ysr@1280: assert(UseCompressedOops, "Error"); ysr@1280: _oc->do_oop((narrowOop*)(*curr)); ysr@1280: } else { ysr@1280: _oc->do_oop((oop*)(*curr)); ysr@1280: } ysr@777: } ysr@777: _buffer_curr = _buffer; ysr@777: _closure_app_seconds += (os::elapsedTime() - start); ysr@777: } ysr@777: ysr@1280: template inline void do_oop_work(T* p) { ysr@777: if (_buffer_curr == _buffer_top) { ysr@777: process_buffer(); ysr@777: } ysr@1280: StarTask new_ref(p); ysr@1280: *_buffer_curr = new_ref; ysr@777: ++_buffer_curr; ysr@777: } ysr@1280: ysr@1280: public: ysr@1280: virtual void do_oop(narrowOop* p) { do_oop_work(p); } ysr@1280: virtual void do_oop(oop* p) { do_oop_work(p); } ysr@1280: ysr@777: void done () { ysr@777: if (_buffer_curr > _buffer) { ysr@777: process_buffer(); ysr@777: } ysr@777: } ysr@777: double closure_app_seconds () { ysr@777: return _closure_app_seconds; ysr@777: } ysr@777: BufferingOopClosure (OopClosure *oc) : ysr@777: _oc(oc), ysr@777: _buffer_curr(_buffer), _buffer_top(_buffer + BufferLength), ysr@777: _closure_app_seconds(0.0) { } ysr@777: }; ysr@777: ysr@777: class BufferingOopsInGenClosure: public OopsInGenClosure { ysr@777: BufferingOopClosure _boc; ysr@777: OopsInGenClosure* _oc; ysr@1280: protected: ysr@1280: template inline void do_oop_work(T* p) { ysr@1280: assert(generation()->is_in_reserved((void*)p), "Must be in!"); ysr@1280: _boc.do_oop(p); ysr@1280: } ysr@1280: public: ysr@777: BufferingOopsInGenClosure(OopsInGenClosure *oc) : ysr@777: _boc(oc), _oc(oc) {} ysr@777: ysr@1280: virtual void do_oop(narrowOop* p) { do_oop_work(p); } ysr@1280: virtual void do_oop(oop* p) { do_oop_work(p); } ysr@777: ysr@777: void done() { ysr@777: _boc.done(); ysr@777: } ysr@777: ysr@777: double closure_app_seconds () { ysr@777: return _boc.closure_app_seconds(); ysr@777: } ysr@777: ysr@777: void set_generation(Generation* gen) { ysr@777: OopsInGenClosure::set_generation(gen); ysr@777: _oc->set_generation(gen); ysr@777: } ysr@777: ysr@777: void reset_generation() { ysr@777: // Make sure we finish the current work with the current generation. ysr@777: _boc.done(); ysr@777: OopsInGenClosure::reset_generation(); ysr@777: _oc->reset_generation(); ysr@777: } ysr@777: ysr@777: }; ysr@777: ysr@777: ysr@777: class BufferingOopsInHeapRegionClosure: public OopsInHeapRegionClosure { ysr@777: private: ysr@777: enum PrivateConstants { ysr@777: BufferLength = 1024 ysr@777: }; ysr@777: ysr@1280: StarTask _buffer[BufferLength]; ysr@1280: StarTask* _buffer_top; ysr@1280: StarTask* _buffer_curr; ysr@777: ysr@1280: HeapRegion* _hr_buffer[BufferLength]; ysr@1280: HeapRegion** _hr_curr; ysr@777: ysr@1280: OopsInHeapRegionClosure* _oc; ysr@777: double _closure_app_seconds; ysr@777: ysr@777: void process_buffer () { ysr@777: ysr@777: assert((_hr_curr - _hr_buffer) == (_buffer_curr - _buffer), ysr@777: "the two lengths should be the same"); ysr@777: ysr@777: double start = os::elapsedTime(); ysr@1280: HeapRegion** hr_curr = _hr_buffer; ysr@1280: HeapRegion* hr_prev = NULL; ysr@1280: for (StarTask* curr = _buffer; curr < _buffer_curr; ++curr) { ysr@1280: HeapRegion* region = *hr_curr; ysr@777: if (region != hr_prev) { ysr@777: _oc->set_region(region); ysr@777: hr_prev = region; ysr@777: } ysr@1280: if (curr->is_narrow()) { ysr@1280: assert(UseCompressedOops, "Error"); ysr@1280: _oc->do_oop((narrowOop*)(*curr)); ysr@1280: } else { ysr@1280: _oc->do_oop((oop*)(*curr)); ysr@1280: } ysr@777: ++hr_curr; ysr@777: } ysr@777: _buffer_curr = _buffer; ysr@777: _hr_curr = _hr_buffer; ysr@777: _closure_app_seconds += (os::elapsedTime() - start); ysr@777: } ysr@777: ysr@777: public: ysr@1280: virtual void do_oop(narrowOop* p) { do_oop_work(p); } ysr@1280: virtual void do_oop( oop* p) { do_oop_work(p); } ysr@777: ysr@1280: template void do_oop_work(T* p) { ysr@777: if (_buffer_curr == _buffer_top) { ysr@777: assert(_hr_curr > _hr_buffer, "_hr_curr should be consistent with _buffer_curr"); ysr@777: process_buffer(); ysr@777: } ysr@1280: StarTask new_ref(p); ysr@1280: *_buffer_curr = new_ref; ysr@777: ++_buffer_curr; ysr@777: *_hr_curr = _from; ysr@777: ++_hr_curr; ysr@777: } ysr@777: void done () { ysr@777: if (_buffer_curr > _buffer) { ysr@777: assert(_hr_curr > _hr_buffer, "_hr_curr should be consistent with _buffer_curr"); ysr@777: process_buffer(); ysr@777: } ysr@777: } ysr@777: double closure_app_seconds () { ysr@777: return _closure_app_seconds; ysr@777: } ysr@777: BufferingOopsInHeapRegionClosure (OopsInHeapRegionClosure *oc) : ysr@777: _oc(oc), ysr@777: _buffer_curr(_buffer), _buffer_top(_buffer + BufferLength), ysr@777: _hr_curr(_hr_buffer), ysr@777: _closure_app_seconds(0.0) { } ysr@777: }; stefank@2314: stefank@2314: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_BUFFERINGOOPCLOSURE_HPP