src/share/vm/gc_implementation/g1/concurrentG1RefineThread.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6930
570cb6369f17
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "gc_implementation/g1/concurrentG1Refine.hpp"
aoqi@0 27 #include "gc_implementation/g1/concurrentG1RefineThread.hpp"
aoqi@0 28 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
aoqi@0 29 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
aoqi@0 30 #include "memory/resourceArea.hpp"
aoqi@0 31 #include "runtime/handles.inline.hpp"
aoqi@0 32 #include "runtime/mutexLocker.hpp"
aoqi@0 33
aoqi@0 34 ConcurrentG1RefineThread::
aoqi@0 35 ConcurrentG1RefineThread(ConcurrentG1Refine* cg1r, ConcurrentG1RefineThread *next,
tschatzl@6930 36 CardTableEntryClosure* refine_closure,
aoqi@0 37 uint worker_id_offset, uint worker_id) :
aoqi@0 38 ConcurrentGCThread(),
tschatzl@6930 39 _refine_closure(refine_closure),
aoqi@0 40 _worker_id_offset(worker_id_offset),
aoqi@0 41 _worker_id(worker_id),
aoqi@0 42 _active(false),
aoqi@0 43 _next(next),
aoqi@0 44 _monitor(NULL),
aoqi@0 45 _cg1r(cg1r),
aoqi@0 46 _vtime_accum(0.0)
aoqi@0 47 {
aoqi@0 48
aoqi@0 49 // Each thread has its own monitor. The i-th thread is responsible for signalling
aoqi@0 50 // to thread i+1 if the number of buffers in the queue exceeds a threashold for this
aoqi@0 51 // thread. Monitors are also used to wake up the threads during termination.
aoqi@0 52 // The 0th worker in notified by mutator threads and has a special monitor.
aoqi@0 53 // The last worker is used for young gen rset size sampling.
aoqi@0 54 if (worker_id > 0) {
aoqi@0 55 _monitor = new Monitor(Mutex::nonleaf, "Refinement monitor", true);
aoqi@0 56 } else {
aoqi@0 57 _monitor = DirtyCardQ_CBL_mon;
aoqi@0 58 }
aoqi@0 59 initialize();
aoqi@0 60 create_and_start();
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 void ConcurrentG1RefineThread::initialize() {
aoqi@0 64 if (_worker_id < cg1r()->worker_thread_num()) {
aoqi@0 65 // Current thread activation threshold
aoqi@0 66 _threshold = MIN2<int>(cg1r()->thread_threshold_step() * (_worker_id + 1) + cg1r()->green_zone(),
aoqi@0 67 cg1r()->yellow_zone());
aoqi@0 68 // A thread deactivates once the number of buffer reached a deactivation threshold
aoqi@0 69 _deactivation_threshold = MAX2<int>(_threshold - cg1r()->thread_threshold_step(), cg1r()->green_zone());
aoqi@0 70 } else {
aoqi@0 71 set_active(true);
aoqi@0 72 }
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 void ConcurrentG1RefineThread::sample_young_list_rs_lengths() {
pliden@6906 76 SuspendibleThreadSetJoiner sts;
aoqi@0 77 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 78 G1CollectorPolicy* g1p = g1h->g1_policy();
aoqi@0 79 if (g1p->adaptive_young_list_length()) {
aoqi@0 80 int regions_visited = 0;
aoqi@0 81 g1h->young_list()->rs_length_sampling_init();
aoqi@0 82 while (g1h->young_list()->rs_length_sampling_more()) {
aoqi@0 83 g1h->young_list()->rs_length_sampling_next();
aoqi@0 84 ++regions_visited;
aoqi@0 85
aoqi@0 86 // we try to yield every time we visit 10 regions
aoqi@0 87 if (regions_visited == 10) {
pliden@6906 88 if (sts.should_yield()) {
pliden@6906 89 sts.yield();
aoqi@0 90 // we just abandon the iteration
aoqi@0 91 break;
aoqi@0 92 }
aoqi@0 93 regions_visited = 0;
aoqi@0 94 }
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 g1p->revise_young_list_target_length_if_necessary();
aoqi@0 98 }
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 void ConcurrentG1RefineThread::run_young_rs_sampling() {
aoqi@0 102 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 103 _vtime_start = os::elapsedVTime();
aoqi@0 104 while(!_should_terminate) {
aoqi@0 105 sample_young_list_rs_lengths();
aoqi@0 106
aoqi@0 107 if (os::supports_vtime()) {
aoqi@0 108 _vtime_accum = (os::elapsedVTime() - _vtime_start);
aoqi@0 109 } else {
aoqi@0 110 _vtime_accum = 0.0;
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 114 if (_should_terminate) {
aoqi@0 115 break;
aoqi@0 116 }
aoqi@0 117 _monitor->wait(Mutex::_no_safepoint_check_flag, G1ConcRefinementServiceIntervalMillis);
aoqi@0 118 }
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 void ConcurrentG1RefineThread::wait_for_completed_buffers() {
aoqi@0 122 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 123 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 124 while (!_should_terminate && !is_active()) {
aoqi@0 125 _monitor->wait(Mutex::_no_safepoint_check_flag);
aoqi@0 126 }
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 bool ConcurrentG1RefineThread::is_active() {
aoqi@0 130 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 131 return _worker_id > 0 ? _active : dcqs.process_completed_buffers();
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 void ConcurrentG1RefineThread::activate() {
aoqi@0 135 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 136 if (_worker_id > 0) {
aoqi@0 137 if (G1TraceConcRefinement) {
aoqi@0 138 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 139 gclog_or_tty->print_cr("G1-Refine-activated worker %d, on threshold %d, current %d",
aoqi@0 140 _worker_id, _threshold, (int)dcqs.completed_buffers_num());
aoqi@0 141 }
aoqi@0 142 set_active(true);
aoqi@0 143 } else {
aoqi@0 144 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 145 dcqs.set_process_completed(true);
aoqi@0 146 }
aoqi@0 147 _monitor->notify();
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 void ConcurrentG1RefineThread::deactivate() {
aoqi@0 151 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 152 if (_worker_id > 0) {
aoqi@0 153 if (G1TraceConcRefinement) {
aoqi@0 154 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 155 gclog_or_tty->print_cr("G1-Refine-deactivated worker %d, off threshold %d, current %d",
aoqi@0 156 _worker_id, _deactivation_threshold, (int)dcqs.completed_buffers_num());
aoqi@0 157 }
aoqi@0 158 set_active(false);
aoqi@0 159 } else {
aoqi@0 160 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 161 dcqs.set_process_completed(false);
aoqi@0 162 }
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 void ConcurrentG1RefineThread::run() {
aoqi@0 166 initialize_in_thread();
aoqi@0 167 wait_for_universe_init();
aoqi@0 168
aoqi@0 169 if (_worker_id >= cg1r()->worker_thread_num()) {
aoqi@0 170 run_young_rs_sampling();
aoqi@0 171 terminate();
aoqi@0 172 return;
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 _vtime_start = os::elapsedVTime();
aoqi@0 176 while (!_should_terminate) {
aoqi@0 177 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 178
aoqi@0 179 // Wait for work
aoqi@0 180 wait_for_completed_buffers();
aoqi@0 181
aoqi@0 182 if (_should_terminate) {
aoqi@0 183 break;
aoqi@0 184 }
aoqi@0 185
pliden@6906 186 {
pliden@6906 187 SuspendibleThreadSetJoiner sts;
aoqi@0 188
pliden@6906 189 do {
pliden@6906 190 int curr_buffer_num = (int)dcqs.completed_buffers_num();
pliden@6906 191 // If the number of the buffers falls down into the yellow zone,
pliden@6906 192 // that means that the transition period after the evacuation pause has ended.
pliden@6906 193 if (dcqs.completed_queue_padding() > 0 && curr_buffer_num <= cg1r()->yellow_zone()) {
pliden@6906 194 dcqs.set_completed_queue_padding(0);
pliden@6906 195 }
pliden@6906 196
pliden@6906 197 if (_worker_id > 0 && curr_buffer_num <= _deactivation_threshold) {
pliden@6906 198 // If the number of the buffer has fallen below our threshold
pliden@6906 199 // we should deactivate. The predecessor will reactivate this
pliden@6906 200 // thread should the number of the buffers cross the threshold again.
pliden@6906 201 deactivate();
pliden@6906 202 break;
pliden@6906 203 }
pliden@6906 204
pliden@6906 205 // Check if we need to activate the next thread.
pliden@6906 206 if (_next != NULL && !_next->is_active() && curr_buffer_num > _next->_threshold) {
pliden@6906 207 _next->activate();
pliden@6906 208 }
tschatzl@6930 209 } while (dcqs.apply_closure_to_completed_buffer(_refine_closure, _worker_id + _worker_id_offset, cg1r()->green_zone()));
pliden@6906 210
pliden@6906 211 // We can exit the loop above while being active if there was a yield request.
pliden@6906 212 if (is_active()) {
pliden@6906 213 deactivate();
aoqi@0 214 }
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 if (os::supports_vtime()) {
aoqi@0 218 _vtime_accum = (os::elapsedVTime() - _vtime_start);
aoqi@0 219 } else {
aoqi@0 220 _vtime_accum = 0.0;
aoqi@0 221 }
aoqi@0 222 }
aoqi@0 223 assert(_should_terminate, "just checking");
aoqi@0 224 terminate();
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 void ConcurrentG1RefineThread::stop() {
aoqi@0 228 // it is ok to take late safepoints here, if needed
aoqi@0 229 {
aoqi@0 230 MutexLockerEx mu(Terminator_lock);
aoqi@0 231 _should_terminate = true;
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 {
aoqi@0 235 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 236 _monitor->notify();
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 {
aoqi@0 240 MutexLockerEx mu(Terminator_lock);
aoqi@0 241 while (!_has_terminated) {
aoqi@0 242 Terminator_lock->wait();
aoqi@0 243 }
aoqi@0 244 }
aoqi@0 245 if (G1TraceConcRefinement) {
aoqi@0 246 gclog_or_tty->print_cr("G1-Refine-stop");
aoqi@0 247 }
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 void ConcurrentG1RefineThread::print() const {
aoqi@0 251 print_on(tty);
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 void ConcurrentG1RefineThread::print_on(outputStream* st) const {
aoqi@0 255 st->print("\"G1 Concurrent Refinement Thread#%d\" ", _worker_id);
aoqi@0 256 Thread::print_on(st);
aoqi@0 257 st->cr();
aoqi@0 258 }

mercurial