ysr@777: /* stefank@6969: * Copyright (c) 2001, 2014, 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@6969: #include "memory/iterator.hpp" stefank@6969: #include "oops/oopsHierarchy.hpp" stefank@2314: #include "runtime/os.hpp" stefank@6969: #include "utilities/debug.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: stefank@6969: class BufferingOopClosure: public OopClosure { stefank@6969: friend class TestBufferingOopClosure; stefank@6969: protected: stefank@6969: static const size_t BufferLength = 1024; ysr@777: stefank@6969: // We need to know if the buffered addresses contain oops or narrowOops. stefank@6969: // We can't tag the addresses the way StarTask does, because we need to stefank@6969: // be able to handle unaligned addresses coming from oops embedded in code. stefank@6969: // stefank@6969: // The addresses for the full-sized oops are filled in from the bottom, stefank@6969: // while the addresses for the narrowOops are filled in from the top. stefank@6969: OopOrNarrowOopStar _buffer[BufferLength]; stefank@6969: OopOrNarrowOopStar* _oop_top; stefank@6969: OopOrNarrowOopStar* _narrowOop_bottom; ysr@777: ysr@1280: OopClosure* _oc; ysr@1280: double _closure_app_seconds; ysr@777: stefank@6969: stefank@6969: bool is_buffer_empty() { stefank@6969: return _oop_top == _buffer && _narrowOop_bottom == (_buffer + BufferLength - 1); stefank@6969: } stefank@6969: stefank@6969: bool is_buffer_full() { stefank@6969: return _narrowOop_bottom < _oop_top; stefank@6969: } stefank@6969: stefank@6969: // Process addresses containing full-sized oops. stefank@6969: void process_oops() { stefank@6969: for (OopOrNarrowOopStar* curr = _buffer; curr < _oop_top; ++curr) { stefank@6969: _oc->do_oop((oop*)(*curr)); stefank@6969: } stefank@6969: _oop_top = _buffer; stefank@6969: } stefank@6969: stefank@6969: // Process addresses containing narrow oops. stefank@6969: void process_narrowOops() { stefank@6969: for (OopOrNarrowOopStar* curr = _buffer + BufferLength - 1; curr > _narrowOop_bottom; --curr) { stefank@6969: _oc->do_oop((narrowOop*)(*curr)); stefank@6969: } stefank@6969: _narrowOop_bottom = _buffer + BufferLength - 1; stefank@6969: } stefank@6969: stefank@6969: // Apply the closure to all oops and clear the buffer. stefank@6969: // Accumulate the time it took. stefank@6969: void process_buffer() { ysr@777: double start = os::elapsedTime(); stefank@6969: stefank@6969: process_oops(); stefank@6969: process_narrowOops(); stefank@6969: ysr@777: _closure_app_seconds += (os::elapsedTime() - start); ysr@777: } ysr@777: stefank@6969: void process_buffer_if_full() { stefank@6969: if (is_buffer_full()) { ysr@777: process_buffer(); ysr@777: } stefank@6969: } stefank@6969: stefank@6969: void add_narrowOop(narrowOop* p) { stefank@6969: assert(!is_buffer_full(), "Buffer should not be full"); stefank@6969: *_narrowOop_bottom = (OopOrNarrowOopStar)p; stefank@6969: _narrowOop_bottom--; stefank@6969: } stefank@6969: stefank@6969: void add_oop(oop* p) { stefank@6969: assert(!is_buffer_full(), "Buffer should not be full"); stefank@6969: *_oop_top = (OopOrNarrowOopStar)p; stefank@6969: _oop_top++; ysr@777: } ysr@1280: ysr@1280: public: stefank@6969: virtual void do_oop(narrowOop* p) { stefank@6969: process_buffer_if_full(); stefank@6969: add_narrowOop(p); stefank@6969: } ysr@1280: stefank@6969: virtual void do_oop(oop* p) { stefank@6969: process_buffer_if_full(); stefank@6969: add_oop(p); stefank@6969: } stefank@6969: stefank@6969: void done() { stefank@6969: if (!is_buffer_empty()) { ysr@777: process_buffer(); ysr@777: } ysr@777: } stefank@6969: stefank@6969: double closure_app_seconds() { ysr@777: return _closure_app_seconds; ysr@777: } stefank@6969: stefank@6969: BufferingOopClosure(OopClosure *oc) : ysr@777: _oc(oc), stefank@6969: _oop_top(_buffer), stefank@6969: _narrowOop_bottom(_buffer + BufferLength - 1), ysr@777: _closure_app_seconds(0.0) { } ysr@777: }; ysr@777: stefank@2314: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_BUFFERINGOOPCLOSURE_HPP