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