ysr@777: /* trims@1907: * 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: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/g1/concurrentG1Refine.hpp" stefank@2314: #include "gc_implementation/g1/concurrentG1RefineThread.hpp" stefank@2314: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" stefank@2314: #include "gc_implementation/g1/g1CollectorPolicy.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/mutexLocker.hpp" ysr@777: ysr@777: ConcurrentG1RefineThread:: iveresov@1230: ConcurrentG1RefineThread(ConcurrentG1Refine* cg1r, ConcurrentG1RefineThread *next, iveresov@1230: int worker_id_offset, int worker_id) : ysr@777: ConcurrentGCThread(), iveresov@1230: _worker_id_offset(worker_id_offset), iveresov@1229: _worker_id(worker_id), iveresov@1229: _active(false), iveresov@1229: _next(next), iveresov@1546: _monitor(NULL), ysr@777: _cg1r(cg1r), iveresov@1546: _vtime_accum(0.0) ysr@777: { iveresov@1546: iveresov@1546: // Each thread has its own monitor. The i-th thread is responsible for signalling iveresov@1546: // to thread i+1 if the number of buffers in the queue exceeds a threashold for this iveresov@1546: // thread. Monitors are also used to wake up the threads during termination. iveresov@1546: // The 0th worker in notified by mutator threads and has a special monitor. iveresov@1546: // The last worker is used for young gen rset size sampling. iveresov@1546: if (worker_id > 0) { iveresov@1546: _monitor = new Monitor(Mutex::nonleaf, "Refinement monitor", true); iveresov@1546: } else { iveresov@1546: _monitor = DirtyCardQ_CBL_mon; iveresov@1546: } iveresov@1546: initialize(); ysr@777: create_and_start(); ysr@777: } ysr@777: iveresov@1546: void ConcurrentG1RefineThread::initialize() { iveresov@1546: if (_worker_id < cg1r()->worker_thread_num()) { iveresov@1546: // Current thread activation threshold iveresov@1546: _threshold = MIN2(cg1r()->thread_threshold_step() * (_worker_id + 1) + cg1r()->green_zone(), iveresov@1546: cg1r()->yellow_zone()); iveresov@1546: // A thread deactivates once the number of buffer reached a deactivation threshold iveresov@1546: _deactivation_threshold = MAX2(_threshold - cg1r()->thread_threshold_step(), cg1r()->green_zone()); iveresov@1546: } else { iveresov@1546: set_active(true); iveresov@1546: } iveresov@1546: } iveresov@1546: ysr@777: void ConcurrentG1RefineThread::sample_young_list_rs_lengths() { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: G1CollectorPolicy* g1p = g1h->g1_policy(); ysr@777: if (g1p->adaptive_young_list_length()) { ysr@777: int regions_visited = 0; johnc@1829: g1h->young_list()->rs_length_sampling_init(); johnc@1829: while (g1h->young_list()->rs_length_sampling_more()) { johnc@1829: g1h->young_list()->rs_length_sampling_next(); ysr@777: ++regions_visited; ysr@777: ysr@777: // we try to yield every time we visit 10 regions ysr@777: if (regions_visited == 10) { ysr@777: if (_sts.should_yield()) { ysr@777: _sts.yield("G1 refine"); ysr@777: // we just abandon the iteration ysr@777: break; ysr@777: } ysr@777: regions_visited = 0; ysr@777: } ysr@777: } ysr@777: tonyp@3119: g1p->revise_young_list_target_length_if_necessary(); ysr@777: } ysr@777: } ysr@777: iveresov@1546: void ConcurrentG1RefineThread::run_young_rs_sampling() { iveresov@1546: DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); iveresov@1546: _vtime_start = os::elapsedVTime(); iveresov@1546: while(!_should_terminate) { iveresov@1546: _sts.join(); iveresov@1546: sample_young_list_rs_lengths(); iveresov@1546: _sts.leave(); iveresov@1546: iveresov@1546: if (os::supports_vtime()) { iveresov@1546: _vtime_accum = (os::elapsedVTime() - _vtime_start); iveresov@1546: } else { iveresov@1546: _vtime_accum = 0.0; iveresov@1546: } iveresov@1546: iveresov@1546: MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag); iveresov@1546: if (_should_terminate) { iveresov@1546: break; iveresov@1546: } tonyp@1717: _monitor->wait(Mutex::_no_safepoint_check_flag, G1ConcRefinementServiceIntervalMillis); iveresov@1546: } iveresov@1546: } iveresov@1546: iveresov@1546: void ConcurrentG1RefineThread::wait_for_completed_buffers() { iveresov@1546: DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); iveresov@1546: MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag); iveresov@1546: while (!_should_terminate && !is_active()) { iveresov@1546: _monitor->wait(Mutex::_no_safepoint_check_flag); iveresov@1546: } iveresov@1546: } iveresov@1546: iveresov@1546: bool ConcurrentG1RefineThread::is_active() { iveresov@1546: DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); iveresov@1546: return _worker_id > 0 ? _active : dcqs.process_completed_buffers(); iveresov@1546: } iveresov@1546: iveresov@1546: void ConcurrentG1RefineThread::activate() { iveresov@1546: MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag); iveresov@1546: if (_worker_id > 0) { tonyp@1717: if (G1TraceConcRefinement) { iveresov@1546: DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); iveresov@1546: gclog_or_tty->print_cr("G1-Refine-activated worker %d, on threshold %d, current %d", iveresov@1546: _worker_id, _threshold, (int)dcqs.completed_buffers_num()); iveresov@1546: } iveresov@1546: set_active(true); iveresov@1546: } else { iveresov@1546: DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); iveresov@1546: dcqs.set_process_completed(true); iveresov@1546: } iveresov@1546: _monitor->notify(); iveresov@1546: } iveresov@1546: iveresov@1546: void ConcurrentG1RefineThread::deactivate() { iveresov@1546: MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag); iveresov@1546: if (_worker_id > 0) { tonyp@1717: if (G1TraceConcRefinement) { iveresov@1546: DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); iveresov@1546: gclog_or_tty->print_cr("G1-Refine-deactivated worker %d, off threshold %d, current %d", iveresov@1546: _worker_id, _deactivation_threshold, (int)dcqs.completed_buffers_num()); iveresov@1546: } iveresov@1546: set_active(false); iveresov@1546: } else { iveresov@1546: DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); iveresov@1546: dcqs.set_process_completed(false); iveresov@1546: } iveresov@1546: } iveresov@1546: ysr@777: void ConcurrentG1RefineThread::run() { ysr@777: initialize_in_thread(); ysr@777: wait_for_universe_init(); ysr@777: iveresov@1546: if (_worker_id >= cg1r()->worker_thread_num()) { iveresov@1546: run_young_rs_sampling(); iveresov@1546: terminate(); johnc@1829: return; iveresov@1546: } iveresov@1546: iveresov@1546: _vtime_start = os::elapsedVTime(); ysr@777: while (!_should_terminate) { iveresov@1229: DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); iveresov@1546: iveresov@1546: // Wait for work iveresov@1546: wait_for_completed_buffers(); iveresov@1546: iveresov@1546: if (_should_terminate) { iveresov@1546: break; iveresov@1229: } iveresov@1229: iveresov@1546: _sts.join(); iveresov@1229: iveresov@1546: do { iveresov@1546: int curr_buffer_num = (int)dcqs.completed_buffers_num(); iveresov@1546: // If the number of the buffers falls down into the yellow zone, iveresov@1546: // that means that the transition period after the evacuation pause has ended. iveresov@1546: if (dcqs.completed_queue_padding() > 0 && curr_buffer_num <= cg1r()->yellow_zone()) { iveresov@1546: dcqs.set_completed_queue_padding(0); iveresov@1546: } iveresov@1229: iveresov@1546: if (_worker_id > 0 && curr_buffer_num <= _deactivation_threshold) { iveresov@1229: // If the number of the buffer has fallen below our threshold iveresov@1229: // we should deactivate. The predecessor will reactivate this iveresov@1229: // thread should the number of the buffers cross the threshold again. iveresov@1229: deactivate(); iveresov@1229: break; iveresov@1229: } iveresov@1229: iveresov@1229: // Check if we need to activate the next thread. iveresov@1546: if (_next != NULL && !_next->is_active() && curr_buffer_num > _next->_threshold) { iveresov@1229: _next->activate(); iveresov@1229: } iveresov@1546: } while (dcqs.apply_closure_to_completed_buffer(_worker_id + _worker_id_offset, cg1r()->green_zone())); iveresov@1229: iveresov@1546: // We can exit the loop above while being active if there was a yield request. iveresov@1546: if (is_active()) { iveresov@1546: deactivate(); iveresov@1229: } iveresov@1546: ysr@777: _sts.leave(); iveresov@1229: ysr@777: if (os::supports_vtime()) { ysr@777: _vtime_accum = (os::elapsedVTime() - _vtime_start); ysr@777: } else { ysr@777: _vtime_accum = 0.0; ysr@777: } ysr@777: } ysr@777: assert(_should_terminate, "just checking"); ysr@777: terminate(); ysr@777: } ysr@777: ysr@777: ysr@777: void ConcurrentG1RefineThread::yield() { tonyp@1717: if (G1TraceConcRefinement) { tonyp@1717: gclog_or_tty->print_cr("G1-Refine-yield"); tonyp@1717: } ysr@777: _sts.yield("G1 refine"); tonyp@1717: if (G1TraceConcRefinement) { tonyp@1717: gclog_or_tty->print_cr("G1-Refine-yield-end"); tonyp@1717: } ysr@777: } ysr@777: ysr@777: void ConcurrentG1RefineThread::stop() { ysr@777: // it is ok to take late safepoints here, if needed ysr@777: { ysr@777: MutexLockerEx mu(Terminator_lock); ysr@777: _should_terminate = true; ysr@777: } ysr@777: ysr@777: { iveresov@1546: MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag); iveresov@1546: _monitor->notify(); ysr@777: } ysr@777: ysr@777: { ysr@777: MutexLockerEx mu(Terminator_lock); ysr@777: while (!_has_terminated) { ysr@777: Terminator_lock->wait(); ysr@777: } ysr@777: } tonyp@1717: if (G1TraceConcRefinement) { tonyp@1717: gclog_or_tty->print_cr("G1-Refine-stop"); tonyp@1717: } ysr@777: } ysr@777: tonyp@1454: void ConcurrentG1RefineThread::print() const { tonyp@1454: print_on(tty); ysr@777: } tonyp@1454: tonyp@1454: void ConcurrentG1RefineThread::print_on(outputStream* st) const { tonyp@1454: st->print("\"G1 Concurrent Refinement Thread#%d\" ", _worker_id); tonyp@1454: Thread::print_on(st); tonyp@1454: st->cr(); tonyp@1454: }