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