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

Mon, 03 Aug 2009 12:59:30 -0700

author
johnc
date
Mon, 03 Aug 2009 12:59:30 -0700
changeset 1324
15c5903cf9e1
parent 1320
7f807f55161a
child 1371
e1fdf4fd34dc
permissions
-rw-r--r--

6865703: G1: Parallelize hot card cache cleanup
Summary: Have the GC worker threads clear the hot card cache in parallel by having each worker thread claim a chunk of the card cache and process the cards in that chunk. The size of the chunks that each thread will claim is determined at VM initialization from the size of the card cache and the number of worker threads.
Reviewed-by: jmasa, tonyp

ysr@777 1 /*
xdono@1279 2 * Copyright 2001-2009 Sun Microsystems, Inc. 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 *
ysr@777 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
ysr@777 20 * CA 95054 USA or visit www.sun.com if you need additional information or
ysr@777 21 * have any questions.
ysr@777 22 *
ysr@777 23 */
ysr@777 24
ysr@777 25 #include "incls/_precompiled.incl"
ysr@777 26 #include "incls/_concurrentG1RefineThread.cpp.incl"
ysr@777 27
ysr@777 28 // ======= Concurrent Mark Thread ========
ysr@777 29
ysr@777 30 // The CM thread is created when the G1 garbage collector is used
ysr@777 31
ysr@777 32 ConcurrentG1RefineThread::
iveresov@1230 33 ConcurrentG1RefineThread(ConcurrentG1Refine* cg1r, ConcurrentG1RefineThread *next,
iveresov@1230 34 int worker_id_offset, int worker_id) :
ysr@777 35 ConcurrentGCThread(),
iveresov@1230 36 _worker_id_offset(worker_id_offset),
iveresov@1229 37 _worker_id(worker_id),
iveresov@1229 38 _active(false),
iveresov@1229 39 _next(next),
ysr@777 40 _cg1r(cg1r),
ysr@777 41 _vtime_accum(0.0),
ysr@777 42 _co_tracker(G1CRGroup),
ysr@777 43 _interval_ms(5.0)
ysr@777 44 {
ysr@777 45 create_and_start();
ysr@777 46 }
ysr@777 47
ysr@777 48 void ConcurrentG1RefineThread::sample_young_list_rs_lengths() {
ysr@777 49 G1CollectedHeap* g1h = G1CollectedHeap::heap();
ysr@777 50 G1CollectorPolicy* g1p = g1h->g1_policy();
ysr@777 51 if (g1p->adaptive_young_list_length()) {
ysr@777 52 int regions_visited = 0;
ysr@777 53
ysr@777 54 g1h->young_list_rs_length_sampling_init();
ysr@777 55 while (g1h->young_list_rs_length_sampling_more()) {
ysr@777 56 g1h->young_list_rs_length_sampling_next();
ysr@777 57 ++regions_visited;
ysr@777 58
ysr@777 59 // we try to yield every time we visit 10 regions
ysr@777 60 if (regions_visited == 10) {
ysr@777 61 if (_sts.should_yield()) {
ysr@777 62 _sts.yield("G1 refine");
ysr@777 63 // we just abandon the iteration
ysr@777 64 break;
ysr@777 65 }
ysr@777 66 regions_visited = 0;
ysr@777 67 }
ysr@777 68 }
ysr@777 69
ysr@777 70 g1p->check_prediction_validity();
ysr@777 71 }
ysr@777 72 }
ysr@777 73
ysr@777 74 void ConcurrentG1RefineThread::run() {
ysr@777 75 initialize_in_thread();
ysr@777 76 _vtime_start = os::elapsedVTime();
ysr@777 77 wait_for_universe_init();
ysr@777 78
ysr@777 79 _co_tracker.enable();
ysr@777 80 _co_tracker.start();
ysr@777 81
ysr@777 82 while (!_should_terminate) {
iveresov@1229 83 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
iveresov@1229 84 // Wait for completed log buffers to exist.
iveresov@1229 85 {
iveresov@1229 86 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
iveresov@1229 87 while (((_worker_id == 0 && !dcqs.process_completed_buffers()) ||
iveresov@1229 88 (_worker_id > 0 && !is_active())) &&
iveresov@1229 89 !_should_terminate) {
iveresov@1229 90 DirtyCardQ_CBL_mon->wait(Mutex::_no_safepoint_check_flag);
iveresov@1229 91 }
iveresov@1229 92 }
iveresov@1229 93
iveresov@1229 94 if (_should_terminate) {
iveresov@1229 95 return;
iveresov@1229 96 }
iveresov@1229 97
iveresov@1229 98 // Now we take them off (this doesn't hold locks while it applies
iveresov@1229 99 // closures.) (If we did a full collection, then we'll do a full
iveresov@1229 100 // traversal.
iveresov@1229 101 _sts.join();
iveresov@1229 102 int n_logs = 0;
iveresov@1229 103 int lower_limit = 0;
iveresov@1229 104 double start_vtime_sec; // only used when G1SmoothConcRefine is on
iveresov@1229 105 int prev_buffer_num; // only used when G1SmoothConcRefine is on
iveresov@1229 106 // This thread activation threshold
tonyp@1318 107 int threshold = G1UpdateBufferQueueProcessingThreshold * _worker_id;
iveresov@1229 108 // Next thread activation threshold
tonyp@1318 109 int next_threshold = threshold + G1UpdateBufferQueueProcessingThreshold;
tonyp@1318 110 int deactivation_threshold = MAX2<int>(threshold - G1UpdateBufferQueueProcessingThreshold / 2, 0);
iveresov@1229 111
iveresov@1229 112 if (G1SmoothConcRefine) {
iveresov@1229 113 lower_limit = 0;
iveresov@1229 114 start_vtime_sec = os::elapsedVTime();
iveresov@1229 115 prev_buffer_num = (int) dcqs.completed_buffers_num();
ysr@777 116 } else {
tonyp@1318 117 lower_limit = G1UpdateBufferQueueProcessingThreshold / 4; // For now.
ysr@777 118 }
iveresov@1230 119 while (dcqs.apply_closure_to_completed_buffer(_worker_id + _worker_id_offset, lower_limit)) {
iveresov@1229 120 double end_vtime_sec;
iveresov@1229 121 double elapsed_vtime_sec;
iveresov@1229 122 int elapsed_vtime_ms;
iveresov@1229 123 int curr_buffer_num = (int) dcqs.completed_buffers_num();
iveresov@1229 124
iveresov@1229 125 if (G1SmoothConcRefine) {
iveresov@1229 126 end_vtime_sec = os::elapsedVTime();
iveresov@1229 127 elapsed_vtime_sec = end_vtime_sec - start_vtime_sec;
iveresov@1229 128 elapsed_vtime_ms = (int) (elapsed_vtime_sec * 1000.0);
iveresov@1229 129
iveresov@1229 130 if (curr_buffer_num > prev_buffer_num ||
iveresov@1229 131 curr_buffer_num > next_threshold) {
iveresov@1229 132 decreaseInterval(elapsed_vtime_ms);
iveresov@1229 133 } else if (curr_buffer_num < prev_buffer_num) {
iveresov@1229 134 increaseInterval(elapsed_vtime_ms);
iveresov@1229 135 }
iveresov@1229 136 }
iveresov@1229 137 if (_worker_id == 0) {
iveresov@1229 138 sample_young_list_rs_lengths();
iveresov@1229 139 } else if (curr_buffer_num < deactivation_threshold) {
iveresov@1229 140 // If the number of the buffer has fallen below our threshold
iveresov@1229 141 // we should deactivate. The predecessor will reactivate this
iveresov@1229 142 // thread should the number of the buffers cross the threshold again.
iveresov@1229 143 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
iveresov@1229 144 deactivate();
iveresov@1229 145 if (G1TraceConcurrentRefinement) {
iveresov@1229 146 gclog_or_tty->print_cr("G1-Refine-deactivated worker %d", _worker_id);
iveresov@1229 147 }
iveresov@1229 148 break;
iveresov@1229 149 }
iveresov@1229 150 _co_tracker.update(false);
iveresov@1229 151
iveresov@1229 152 // Check if we need to activate the next thread.
iveresov@1229 153 if (curr_buffer_num > next_threshold && _next != NULL && !_next->is_active()) {
iveresov@1229 154 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
iveresov@1229 155 _next->activate();
iveresov@1229 156 DirtyCardQ_CBL_mon->notify_all();
iveresov@1229 157 if (G1TraceConcurrentRefinement) {
iveresov@1229 158 gclog_or_tty->print_cr("G1-Refine-activated worker %d", _next->_worker_id);
iveresov@1229 159 }
iveresov@1229 160 }
iveresov@1229 161
iveresov@1229 162 if (G1SmoothConcRefine) {
iveresov@1229 163 prev_buffer_num = curr_buffer_num;
iveresov@1229 164 _sts.leave();
iveresov@1229 165 os::sleep(Thread::current(), (jlong) _interval_ms, false);
iveresov@1229 166 _sts.join();
iveresov@1229 167 start_vtime_sec = os::elapsedVTime();
iveresov@1229 168 }
iveresov@1229 169 n_logs++;
iveresov@1229 170 }
iveresov@1229 171 _co_tracker.update(false);
ysr@777 172 _sts.leave();
iveresov@1229 173
ysr@777 174 if (os::supports_vtime()) {
ysr@777 175 _vtime_accum = (os::elapsedVTime() - _vtime_start);
ysr@777 176 } else {
ysr@777 177 _vtime_accum = 0.0;
ysr@777 178 }
ysr@777 179 }
ysr@777 180 _sts.join();
ysr@777 181 _co_tracker.update(true);
ysr@777 182 _sts.leave();
ysr@777 183 assert(_should_terminate, "just checking");
ysr@777 184
ysr@777 185 terminate();
ysr@777 186 }
ysr@777 187
ysr@777 188
ysr@777 189 void ConcurrentG1RefineThread::yield() {
johnc@1186 190 if (G1TraceConcurrentRefinement) gclog_or_tty->print_cr("G1-Refine-yield");
ysr@777 191 _sts.yield("G1 refine");
johnc@1186 192 if (G1TraceConcurrentRefinement) gclog_or_tty->print_cr("G1-Refine-yield-end");
ysr@777 193 }
ysr@777 194
ysr@777 195 void ConcurrentG1RefineThread::stop() {
ysr@777 196 // it is ok to take late safepoints here, if needed
ysr@777 197 {
ysr@777 198 MutexLockerEx mu(Terminator_lock);
ysr@777 199 _should_terminate = true;
ysr@777 200 }
ysr@777 201
ysr@777 202 {
ysr@777 203 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
ysr@777 204 DirtyCardQ_CBL_mon->notify_all();
ysr@777 205 }
ysr@777 206
ysr@777 207 {
ysr@777 208 MutexLockerEx mu(Terminator_lock);
ysr@777 209 while (!_has_terminated) {
ysr@777 210 Terminator_lock->wait();
ysr@777 211 }
ysr@777 212 }
johnc@1186 213 if (G1TraceConcurrentRefinement) gclog_or_tty->print_cr("G1-Refine-stop");
ysr@777 214 }
ysr@777 215
ysr@777 216 void ConcurrentG1RefineThread::print() {
ysr@777 217 gclog_or_tty->print("\"Concurrent G1 Refinement Thread\" ");
ysr@777 218 Thread::print();
ysr@777 219 gclog_or_tty->cr();
ysr@777 220 }

mercurial