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

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6552
8847586c9037
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
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,
aoqi@0 36 uint worker_id_offset, uint worker_id) :
aoqi@0 37 ConcurrentGCThread(),
aoqi@0 38 _worker_id_offset(worker_id_offset),
aoqi@0 39 _worker_id(worker_id),
aoqi@0 40 _active(false),
aoqi@0 41 _next(next),
aoqi@0 42 _monitor(NULL),
aoqi@0 43 _cg1r(cg1r),
aoqi@0 44 _vtime_accum(0.0)
aoqi@0 45 {
aoqi@0 46
aoqi@0 47 // Each thread has its own monitor. The i-th thread is responsible for signalling
aoqi@0 48 // to thread i+1 if the number of buffers in the queue exceeds a threashold for this
aoqi@0 49 // thread. Monitors are also used to wake up the threads during termination.
aoqi@0 50 // The 0th worker in notified by mutator threads and has a special monitor.
aoqi@0 51 // The last worker is used for young gen rset size sampling.
aoqi@0 52 if (worker_id > 0) {
aoqi@0 53 _monitor = new Monitor(Mutex::nonleaf, "Refinement monitor", true);
aoqi@0 54 } else {
aoqi@0 55 _monitor = DirtyCardQ_CBL_mon;
aoqi@0 56 }
aoqi@0 57 initialize();
aoqi@0 58 create_and_start();
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 void ConcurrentG1RefineThread::initialize() {
aoqi@0 62 if (_worker_id < cg1r()->worker_thread_num()) {
aoqi@0 63 // Current thread activation threshold
aoqi@0 64 _threshold = MIN2<int>(cg1r()->thread_threshold_step() * (_worker_id + 1) + cg1r()->green_zone(),
aoqi@0 65 cg1r()->yellow_zone());
aoqi@0 66 // A thread deactivates once the number of buffer reached a deactivation threshold
aoqi@0 67 _deactivation_threshold = MAX2<int>(_threshold - cg1r()->thread_threshold_step(), cg1r()->green_zone());
aoqi@0 68 } else {
aoqi@0 69 set_active(true);
aoqi@0 70 }
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 void ConcurrentG1RefineThread::sample_young_list_rs_lengths() {
aoqi@0 74 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 75 G1CollectorPolicy* g1p = g1h->g1_policy();
aoqi@0 76 if (g1p->adaptive_young_list_length()) {
aoqi@0 77 int regions_visited = 0;
aoqi@0 78 g1h->young_list()->rs_length_sampling_init();
aoqi@0 79 while (g1h->young_list()->rs_length_sampling_more()) {
aoqi@0 80 g1h->young_list()->rs_length_sampling_next();
aoqi@0 81 ++regions_visited;
aoqi@0 82
aoqi@0 83 // we try to yield every time we visit 10 regions
aoqi@0 84 if (regions_visited == 10) {
aoqi@0 85 if (_sts.should_yield()) {
aoqi@0 86 _sts.yield("G1 refine");
aoqi@0 87 // we just abandon the iteration
aoqi@0 88 break;
aoqi@0 89 }
aoqi@0 90 regions_visited = 0;
aoqi@0 91 }
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 g1p->revise_young_list_target_length_if_necessary();
aoqi@0 95 }
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 void ConcurrentG1RefineThread::run_young_rs_sampling() {
aoqi@0 99 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 100 _vtime_start = os::elapsedVTime();
aoqi@0 101 while(!_should_terminate) {
aoqi@0 102 _sts.join();
aoqi@0 103 sample_young_list_rs_lengths();
aoqi@0 104 _sts.leave();
aoqi@0 105
aoqi@0 106 if (os::supports_vtime()) {
aoqi@0 107 _vtime_accum = (os::elapsedVTime() - _vtime_start);
aoqi@0 108 } else {
aoqi@0 109 _vtime_accum = 0.0;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 113 if (_should_terminate) {
aoqi@0 114 break;
aoqi@0 115 }
aoqi@0 116 _monitor->wait(Mutex::_no_safepoint_check_flag, G1ConcRefinementServiceIntervalMillis);
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 void ConcurrentG1RefineThread::wait_for_completed_buffers() {
aoqi@0 121 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 122 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 123 while (!_should_terminate && !is_active()) {
aoqi@0 124 _monitor->wait(Mutex::_no_safepoint_check_flag);
aoqi@0 125 }
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 bool ConcurrentG1RefineThread::is_active() {
aoqi@0 129 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 130 return _worker_id > 0 ? _active : dcqs.process_completed_buffers();
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 void ConcurrentG1RefineThread::activate() {
aoqi@0 134 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 135 if (_worker_id > 0) {
aoqi@0 136 if (G1TraceConcRefinement) {
aoqi@0 137 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 138 gclog_or_tty->print_cr("G1-Refine-activated worker %d, on threshold %d, current %d",
aoqi@0 139 _worker_id, _threshold, (int)dcqs.completed_buffers_num());
aoqi@0 140 }
aoqi@0 141 set_active(true);
aoqi@0 142 } else {
aoqi@0 143 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 144 dcqs.set_process_completed(true);
aoqi@0 145 }
aoqi@0 146 _monitor->notify();
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 void ConcurrentG1RefineThread::deactivate() {
aoqi@0 150 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 151 if (_worker_id > 0) {
aoqi@0 152 if (G1TraceConcRefinement) {
aoqi@0 153 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 154 gclog_or_tty->print_cr("G1-Refine-deactivated worker %d, off threshold %d, current %d",
aoqi@0 155 _worker_id, _deactivation_threshold, (int)dcqs.completed_buffers_num());
aoqi@0 156 }
aoqi@0 157 set_active(false);
aoqi@0 158 } else {
aoqi@0 159 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 160 dcqs.set_process_completed(false);
aoqi@0 161 }
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 void ConcurrentG1RefineThread::run() {
aoqi@0 165 initialize_in_thread();
aoqi@0 166 wait_for_universe_init();
aoqi@0 167
aoqi@0 168 if (_worker_id >= cg1r()->worker_thread_num()) {
aoqi@0 169 run_young_rs_sampling();
aoqi@0 170 terminate();
aoqi@0 171 return;
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 _vtime_start = os::elapsedVTime();
aoqi@0 175 while (!_should_terminate) {
aoqi@0 176 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
aoqi@0 177
aoqi@0 178 // Wait for work
aoqi@0 179 wait_for_completed_buffers();
aoqi@0 180
aoqi@0 181 if (_should_terminate) {
aoqi@0 182 break;
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 _sts.join();
aoqi@0 186
aoqi@0 187 do {
aoqi@0 188 int curr_buffer_num = (int)dcqs.completed_buffers_num();
aoqi@0 189 // If the number of the buffers falls down into the yellow zone,
aoqi@0 190 // that means that the transition period after the evacuation pause has ended.
aoqi@0 191 if (dcqs.completed_queue_padding() > 0 && curr_buffer_num <= cg1r()->yellow_zone()) {
aoqi@0 192 dcqs.set_completed_queue_padding(0);
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 if (_worker_id > 0 && curr_buffer_num <= _deactivation_threshold) {
aoqi@0 196 // If the number of the buffer has fallen below our threshold
aoqi@0 197 // we should deactivate. The predecessor will reactivate this
aoqi@0 198 // thread should the number of the buffers cross the threshold again.
aoqi@0 199 deactivate();
aoqi@0 200 break;
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 // Check if we need to activate the next thread.
aoqi@0 204 if (_next != NULL && !_next->is_active() && curr_buffer_num > _next->_threshold) {
aoqi@0 205 _next->activate();
aoqi@0 206 }
aoqi@0 207 } while (dcqs.apply_closure_to_completed_buffer(_worker_id + _worker_id_offset, cg1r()->green_zone()));
aoqi@0 208
aoqi@0 209 // We can exit the loop above while being active if there was a yield request.
aoqi@0 210 if (is_active()) {
aoqi@0 211 deactivate();
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 _sts.leave();
aoqi@0 215
aoqi@0 216 if (os::supports_vtime()) {
aoqi@0 217 _vtime_accum = (os::elapsedVTime() - _vtime_start);
aoqi@0 218 } else {
aoqi@0 219 _vtime_accum = 0.0;
aoqi@0 220 }
aoqi@0 221 }
aoqi@0 222 assert(_should_terminate, "just checking");
aoqi@0 223 terminate();
aoqi@0 224 }
aoqi@0 225
aoqi@0 226
aoqi@0 227 void ConcurrentG1RefineThread::yield() {
aoqi@0 228 if (G1TraceConcRefinement) {
aoqi@0 229 gclog_or_tty->print_cr("G1-Refine-yield");
aoqi@0 230 }
aoqi@0 231 _sts.yield("G1 refine");
aoqi@0 232 if (G1TraceConcRefinement) {
aoqi@0 233 gclog_or_tty->print_cr("G1-Refine-yield-end");
aoqi@0 234 }
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 void ConcurrentG1RefineThread::stop() {
aoqi@0 238 // it is ok to take late safepoints here, if needed
aoqi@0 239 {
aoqi@0 240 MutexLockerEx mu(Terminator_lock);
aoqi@0 241 _should_terminate = true;
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 {
aoqi@0 245 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
aoqi@0 246 _monitor->notify();
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 {
aoqi@0 250 MutexLockerEx mu(Terminator_lock);
aoqi@0 251 while (!_has_terminated) {
aoqi@0 252 Terminator_lock->wait();
aoqi@0 253 }
aoqi@0 254 }
aoqi@0 255 if (G1TraceConcRefinement) {
aoqi@0 256 gclog_or_tty->print_cr("G1-Refine-stop");
aoqi@0 257 }
aoqi@0 258 }
aoqi@0 259
aoqi@0 260 void ConcurrentG1RefineThread::print() const {
aoqi@0 261 print_on(tty);
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 void ConcurrentG1RefineThread::print_on(outputStream* st) const {
aoqi@0 265 st->print("\"G1 Concurrent Refinement Thread#%d\" ", _worker_id);
aoqi@0 266 Thread::print_on(st);
aoqi@0 267 st->cr();
aoqi@0 268 }

mercurial