Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.cpp
+++ new/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.cpp
1 1 /*
2 2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 *
23 23 */
24 24
25 25 #include "precompiled.hpp"
26 26 #include "classfile/systemDictionary.hpp"
27 27 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"
28 28 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
29 29 #include "memory/genCollectedHeap.hpp"
30 30 #include "oops/instanceRefKlass.hpp"
31 31 #include "oops/oop.inline.hpp"
32 32 #include "runtime/init.hpp"
33 33 #include "runtime/interfaceSupport.hpp"
34 34 #include "runtime/java.hpp"
35 35 #include "runtime/javaCalls.hpp"
36 36 #include "runtime/mutexLocker.hpp"
37 37 #include "runtime/os.hpp"
38 38 #include "runtime/vmThread.hpp"
39 39
40 40 // ======= Concurrent Mark Sweep Thread ========
41 41
42 42 // The CMS thread is created when Concurrent Mark Sweep is used in the
43 43 // older of two generations in a generational memory system.
↓ open down ↓ |
43 lines elided |
↑ open up ↑ |
44 44
45 45 ConcurrentMarkSweepThread*
46 46 ConcurrentMarkSweepThread::_cmst = NULL;
47 47 CMSCollector* ConcurrentMarkSweepThread::_collector = NULL;
48 48 bool ConcurrentMarkSweepThread::_should_terminate = false;
49 49 int ConcurrentMarkSweepThread::_CMS_flag = CMS_nil;
50 50
51 51 volatile jint ConcurrentMarkSweepThread::_pending_yields = 0;
52 52 volatile jint ConcurrentMarkSweepThread::_pending_decrements = 0;
53 53
54 -volatile bool ConcurrentMarkSweepThread::_icms_enabled = false;
54 +volatile jint ConcurrentMarkSweepThread::_icms_disabled = 0;
55 55 volatile bool ConcurrentMarkSweepThread::_should_run = false;
56 56 // When icms is enabled, the icms thread is stopped until explicitly
57 57 // started.
58 58 volatile bool ConcurrentMarkSweepThread::_should_stop = true;
59 59
60 60 SurrogateLockerThread*
61 61 ConcurrentMarkSweepThread::_slt = NULL;
62 62 SurrogateLockerThread::SLT_msg_type
63 63 ConcurrentMarkSweepThread::_sltBuffer = SurrogateLockerThread::empty;
64 64 Monitor*
65 65 ConcurrentMarkSweepThread::_sltMonitor = NULL;
66 66
67 67 ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector)
68 68 : ConcurrentGCThread() {
69 69 assert(UseConcMarkSweepGC, "UseConcMarkSweepGC should be set");
70 70 assert(_cmst == NULL, "CMS thread already created");
71 71 _cmst = this;
72 72 assert(_collector == NULL, "Collector already set");
73 73 _collector = collector;
74 74
75 75 set_name("Concurrent Mark-Sweep GC Thread");
76 76
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
77 77 if (os::create_thread(this, os::cgc_thread)) {
78 78 // XXX: need to set this to low priority
79 79 // unless "agressive mode" set; priority
80 80 // should be just less than that of VMThread.
81 81 os::set_priority(this, NearMaxPriority);
82 82 if (!DisableStartThread) {
83 83 os::start_thread(this);
84 84 }
85 85 }
86 86 _sltMonitor = SLT_lock;
87 - set_icms_enabled(CMSIncrementalMode);
87 + assert(!CMSIncrementalMode || icms_is_enabled(), "Error");
88 88 }
89 89
90 90 void ConcurrentMarkSweepThread::run() {
91 91 assert(this == cmst(), "just checking");
92 92
93 93 this->record_stack_base_and_size();
94 94 this->initialize_thread_local_storage();
95 95 this->set_active_handles(JNIHandleBlock::allocate_block());
96 96 // From this time Thread::current() should be working.
97 97 assert(this == Thread::current(), "just checking");
98 98 if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) {
99 99 warning("Couldn't bind CMS thread to processor %u", CPUForCMSThread);
100 100 }
101 101 // Wait until Universe::is_fully_initialized()
102 102 {
103 103 CMSLoopCountWarn loopX("CMS::run", "waiting for "
104 104 "Universe::is_fully_initialized()", 2);
105 105 MutexLockerEx x(CGC_lock, true);
106 106 set_CMS_flag(CMS_cms_wants_token);
107 107 // Wait until Universe is initialized and all initialization is completed.
108 108 while (!is_init_completed() && !Universe::is_fully_initialized() &&
109 109 !_should_terminate) {
110 110 CGC_lock->wait(true, 200);
111 111 loopX.tick();
112 112 }
113 113 // Wait until the surrogate locker thread that will do
114 114 // pending list locking on our behalf has been created.
115 115 // We cannot start the SLT thread ourselves since we need
116 116 // to be a JavaThread to do so.
117 117 CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2);
118 118 while (_slt == NULL && !_should_terminate) {
119 119 CGC_lock->wait(true, 200);
120 120 loopY.tick();
121 121 }
122 122 clear_CMS_flag(CMS_cms_wants_token);
123 123 }
124 124
125 125 while (!_should_terminate) {
126 126 sleepBeforeNextCycle();
127 127 if (_should_terminate) break;
128 128 _collector->collect_in_background(false); // !clear_all_soft_refs
129 129 }
130 130 assert(_should_terminate, "just checking");
131 131 // Check that the state of any protocol for synchronization
132 132 // between background (CMS) and foreground collector is "clean"
133 133 // (i.e. will not potentially block the foreground collector,
134 134 // requiring action by us).
135 135 verify_ok_to_terminate();
136 136 // Signal that it is terminated
137 137 {
138 138 MutexLockerEx mu(Terminator_lock,
139 139 Mutex::_no_safepoint_check_flag);
140 140 assert(_cmst == this, "Weird!");
141 141 _cmst = NULL;
142 142 Terminator_lock->notify();
143 143 }
144 144
145 145 // Thread destructor usually does this..
146 146 ThreadLocalStorage::set_thread(NULL);
147 147 }
148 148
149 149 #ifndef PRODUCT
150 150 void ConcurrentMarkSweepThread::verify_ok_to_terminate() const {
151 151 assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() ||
152 152 cms_thread_wants_cms_token()),
153 153 "Must renounce all worldly possessions and desires for nirvana");
154 154 _collector->verify_ok_to_terminate();
155 155 }
156 156 #endif
157 157
158 158 // create and start a new ConcurrentMarkSweep Thread for given CMS generation
159 159 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) {
160 160 if (!_should_terminate) {
161 161 assert(cmst() == NULL, "start() called twice?");
162 162 ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector);
163 163 assert(cmst() == th, "Where did the just-created CMS thread go?");
164 164 return th;
165 165 }
166 166 return NULL;
167 167 }
168 168
169 169 void ConcurrentMarkSweepThread::stop() {
170 170 if (CMSIncrementalMode) {
171 171 // Disable incremental mode and wake up the thread so it notices the change.
172 172 disable_icms();
173 173 start_icms();
174 174 }
175 175 // it is ok to take late safepoints here, if needed
176 176 {
177 177 MutexLockerEx x(Terminator_lock);
178 178 _should_terminate = true;
179 179 }
180 180 { // Now post a notify on CGC_lock so as to nudge
181 181 // CMS thread(s) that might be slumbering in
182 182 // sleepBeforeNextCycle.
183 183 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
184 184 CGC_lock->notify_all();
185 185 }
186 186 { // Now wait until (all) CMS thread(s) have exited
187 187 MutexLockerEx x(Terminator_lock);
188 188 while(cmst() != NULL) {
189 189 Terminator_lock->wait();
190 190 }
191 191 }
192 192 }
193 193
194 194 void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) {
195 195 assert(tc != NULL, "Null ThreadClosure");
196 196 if (_cmst != NULL) {
197 197 tc->do_thread(_cmst);
198 198 }
199 199 assert(Universe::is_fully_initialized(),
200 200 "Called too early, make sure heap is fully initialized");
201 201 if (_collector != NULL) {
202 202 AbstractWorkGang* gang = _collector->conc_workers();
203 203 if (gang != NULL) {
204 204 gang->threads_do(tc);
205 205 }
206 206 }
207 207 }
208 208
209 209 void ConcurrentMarkSweepThread::print_on(outputStream* st) const {
210 210 st->print("\"%s\" ", name());
211 211 Thread::print_on(st);
212 212 st->cr();
213 213 }
214 214
215 215 void ConcurrentMarkSweepThread::print_all_on(outputStream* st) {
216 216 if (_cmst != NULL) {
217 217 _cmst->print_on(st);
218 218 }
219 219 if (_collector != NULL) {
220 220 AbstractWorkGang* gang = _collector->conc_workers();
221 221 if (gang != NULL) {
222 222 gang->print_worker_threads_on(st);
223 223 }
224 224 }
225 225 }
226 226
227 227 void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) {
228 228 assert(UseConcMarkSweepGC, "just checking");
229 229
230 230 MutexLockerEx x(CGC_lock,
231 231 Mutex::_no_safepoint_check_flag);
232 232 if (!is_cms_thread) {
233 233 assert(Thread::current()->is_VM_thread(), "Not a VM thread");
234 234 CMSSynchronousYieldRequest yr;
235 235 while (CMS_flag_is_set(CMS_cms_has_token)) {
236 236 // indicate that we want to get the token
237 237 set_CMS_flag(CMS_vm_wants_token);
238 238 CGC_lock->wait(true);
239 239 }
240 240 // claim the token and proceed
241 241 clear_CMS_flag(CMS_vm_wants_token);
242 242 set_CMS_flag(CMS_vm_has_token);
243 243 } else {
244 244 assert(Thread::current()->is_ConcurrentGC_thread(),
245 245 "Not a CMS thread");
246 246 // The following barrier assumes there's only one CMS thread.
247 247 // This will need to be modified is there are more CMS threads than one.
248 248 while (CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token)) {
249 249 set_CMS_flag(CMS_cms_wants_token);
250 250 CGC_lock->wait(true);
251 251 }
252 252 // claim the token
253 253 clear_CMS_flag(CMS_cms_wants_token);
254 254 set_CMS_flag(CMS_cms_has_token);
255 255 }
256 256 }
257 257
258 258 void ConcurrentMarkSweepThread::desynchronize(bool is_cms_thread) {
259 259 assert(UseConcMarkSweepGC, "just checking");
260 260
261 261 MutexLockerEx x(CGC_lock,
262 262 Mutex::_no_safepoint_check_flag);
263 263 if (!is_cms_thread) {
264 264 assert(Thread::current()->is_VM_thread(), "Not a VM thread");
265 265 assert(CMS_flag_is_set(CMS_vm_has_token), "just checking");
266 266 clear_CMS_flag(CMS_vm_has_token);
267 267 if (CMS_flag_is_set(CMS_cms_wants_token)) {
268 268 // wake-up a waiting CMS thread
269 269 CGC_lock->notify();
270 270 }
271 271 assert(!CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token),
272 272 "Should have been cleared");
273 273 } else {
274 274 assert(Thread::current()->is_ConcurrentGC_thread(),
275 275 "Not a CMS thread");
276 276 assert(CMS_flag_is_set(CMS_cms_has_token), "just checking");
277 277 clear_CMS_flag(CMS_cms_has_token);
278 278 if (CMS_flag_is_set(CMS_vm_wants_token)) {
279 279 // wake-up a waiting VM thread
280 280 CGC_lock->notify();
281 281 }
282 282 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
283 283 "Should have been cleared");
284 284 }
285 285 }
286 286
287 287 // Wait until the next synchronous GC, a concurrent full gc request,
288 288 // or a timeout, whichever is earlier.
289 289 void ConcurrentMarkSweepThread::wait_on_cms_lock(long t_millis) {
290 290 MutexLockerEx x(CGC_lock,
291 291 Mutex::_no_safepoint_check_flag);
292 292 if (_should_terminate || _collector->_full_gc_requested) {
293 293 return;
294 294 }
295 295 set_CMS_flag(CMS_cms_wants_token); // to provoke notifies
296 296 CGC_lock->wait(Mutex::_no_safepoint_check_flag, t_millis);
297 297 clear_CMS_flag(CMS_cms_wants_token);
298 298 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
299 299 "Should not be set");
300 300 }
301 301
302 302 void ConcurrentMarkSweepThread::sleepBeforeNextCycle() {
303 303 while (!_should_terminate) {
304 304 if (CMSIncrementalMode) {
305 305 icms_wait();
306 306 return;
307 307 } else {
308 308 // Wait until the next synchronous GC, a concurrent full gc
309 309 // request or a timeout, whichever is earlier.
310 310 wait_on_cms_lock(CMSWaitDuration);
311 311 }
312 312 // Check if we should start a CMS collection cycle
313 313 if (_collector->shouldConcurrentCollect()) {
314 314 return;
315 315 }
316 316 // .. collection criterion not yet met, let's go back
317 317 // and wait some more
318 318 }
319 319 }
320 320
321 321 // Incremental CMS
322 322 void ConcurrentMarkSweepThread::start_icms() {
323 323 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
324 324 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
325 325 trace_state("start_icms");
326 326 _should_run = true;
327 327 iCMS_lock->notify_all();
328 328 }
329 329
330 330 void ConcurrentMarkSweepThread::stop_icms() {
331 331 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
332 332 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
333 333 if (!_should_stop) {
↓ open down ↓ |
236 lines elided |
↑ open up ↑ |
334 334 trace_state("stop_icms");
335 335 _should_stop = true;
336 336 _should_run = false;
337 337 asynchronous_yield_request();
338 338 iCMS_lock->notify_all();
339 339 }
340 340 }
341 341
342 342 void ConcurrentMarkSweepThread::icms_wait() {
343 343 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
344 - if (_should_stop && icms_enabled()) {
344 + if (_should_stop && icms_is_enabled()) {
345 345 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
346 346 trace_state("pause_icms");
347 347 _collector->stats().stop_cms_timer();
348 - while(!_should_run && icms_enabled()) {
348 + while(!_should_run && icms_is_enabled()) {
349 349 iCMS_lock->wait(Mutex::_no_safepoint_check_flag);
350 350 }
351 351 _collector->stats().start_cms_timer();
352 352 _should_stop = false;
353 353 trace_state("pause_icms end");
354 354 }
355 355 }
356 356
357 357 // Note: this method, although exported by the ConcurrentMarkSweepThread,
358 358 // which is a non-JavaThread, can only be called by a JavaThread.
359 359 // Currently this is done at vm creation time (post-vm-init) by the
360 360 // main/Primordial (Java)Thread.
361 361 // XXX Consider changing this in the future to allow the CMS thread
362 362 // itself to create this thread?
363 363 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) {
364 364 assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC");
365 365 assert(_slt == NULL, "SLT already created");
366 366 _slt = SurrogateLockerThread::make(THREAD);
367 367 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX