Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/gc_implementation/concurrentMarkSweep/vmCMSOperations.cpp
+++ new/src/share/vm/gc_implementation/concurrentMarkSweep/vmCMSOperations.cpp
1 1 /*
2 2 * Copyright (c) 2005, 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 "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"
27 27 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
28 28 #include "gc_implementation/concurrentMarkSweep/vmCMSOperations.hpp"
29 29 #include "gc_implementation/shared/isGCActiveMark.hpp"
30 30 #include "memory/gcLocker.inline.hpp"
31 31 #include "runtime/interfaceSupport.hpp"
32 32 #include "utilities/dtrace.hpp"
33 33 HS_DTRACE_PROBE_DECL(hs_private, cms__initmark__begin);
34 34 HS_DTRACE_PROBE_DECL(hs_private, cms__initmark__end);
35 35
36 36 HS_DTRACE_PROBE_DECL(hs_private, cms__remark__begin);
37 37 HS_DTRACE_PROBE_DECL(hs_private, cms__remark__end);
38 38
39 39 //////////////////////////////////////////////////////////
40 40 // Methods in abstract class VM_CMS_Operation
41 41 //////////////////////////////////////////////////////////
42 42 void VM_CMS_Operation::acquire_pending_list_lock() {
43 43 // The caller may block while communicating
44 44 // with the SLT thread in order to acquire/release the PLL.
45 45 ConcurrentMarkSweepThread::slt()->
46 46 manipulatePLL(SurrogateLockerThread::acquirePLL);
47 47 }
48 48
49 49 void VM_CMS_Operation::release_and_notify_pending_list_lock() {
50 50 // The caller may block while communicating
51 51 // with the SLT thread in order to acquire/release the PLL.
52 52 ConcurrentMarkSweepThread::slt()->
53 53 manipulatePLL(SurrogateLockerThread::releaseAndNotifyPLL);
54 54 }
55 55
56 56 void VM_CMS_Operation::verify_before_gc() {
57 57 if (VerifyBeforeGC &&
58 58 GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
59 59 HandleMark hm;
60 60 FreelistLocker x(_collector);
61 61 MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag);
62 62 Universe::heap()->prepare_for_verify();
63 63 Universe::verify(true);
64 64 }
65 65 }
66 66
67 67 void VM_CMS_Operation::verify_after_gc() {
68 68 if (VerifyAfterGC &&
69 69 GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
70 70 HandleMark hm;
71 71 FreelistLocker x(_collector);
72 72 MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag);
73 73 Universe::verify(true);
74 74 }
75 75 }
76 76
77 77 bool VM_CMS_Operation::lost_race() const {
78 78 if (CMSCollector::abstract_state() == CMSCollector::Idling) {
79 79 // We lost a race to a foreground collection
80 80 // -- there's nothing to do
81 81 return true;
82 82 }
83 83 assert(CMSCollector::abstract_state() == legal_state(),
84 84 "Inconsistent collector state?");
85 85 return false;
86 86 }
87 87
88 88 bool VM_CMS_Operation::doit_prologue() {
89 89 assert(Thread::current()->is_ConcurrentGC_thread(), "just checking");
90 90 assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock");
91 91 assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
92 92 "Possible deadlock");
93 93
94 94 if (needs_pll()) {
95 95 acquire_pending_list_lock();
96 96 }
97 97 // Get the Heap_lock after the pending_list_lock.
98 98 Heap_lock->lock();
99 99 if (lost_race()) {
100 100 assert(_prologue_succeeded == false, "Initialized in c'tor");
101 101 Heap_lock->unlock();
102 102 if (needs_pll()) {
103 103 release_and_notify_pending_list_lock();
104 104 }
105 105 } else {
106 106 _prologue_succeeded = true;
107 107 }
108 108 return _prologue_succeeded;
109 109 }
110 110
111 111 void VM_CMS_Operation::doit_epilogue() {
112 112 assert(Thread::current()->is_ConcurrentGC_thread(), "just checking");
113 113 assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock");
114 114 assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
115 115 "Possible deadlock");
116 116
117 117 // Release the Heap_lock first.
118 118 Heap_lock->unlock();
119 119 if (needs_pll()) {
120 120 release_and_notify_pending_list_lock();
121 121 }
122 122 }
123 123
124 124 //////////////////////////////////////////////////////////
125 125 // Methods in class VM_CMS_Initial_Mark
126 126 //////////////////////////////////////////////////////////
127 127 void VM_CMS_Initial_Mark::doit() {
128 128 if (lost_race()) {
129 129 // Nothing to do.
130 130 return;
131 131 }
132 132 HS_DTRACE_PROBE(hs_private, cms__initmark__begin);
133 133
134 134 GenCollectedHeap* gch = GenCollectedHeap::heap();
135 135 GCCauseSetter gccs(gch, GCCause::_cms_initial_mark);
136 136
137 137 VM_CMS_Operation::verify_before_gc();
138 138
139 139 IsGCActiveMark x; // stop-world GC active
140 140 _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsInitial);
141 141
142 142 VM_CMS_Operation::verify_after_gc();
143 143 HS_DTRACE_PROBE(hs_private, cms__initmark__end);
144 144 }
145 145
146 146 //////////////////////////////////////////////////////////
147 147 // Methods in class VM_CMS_Final_Remark_Operation
148 148 //////////////////////////////////////////////////////////
149 149 void VM_CMS_Final_Remark::doit() {
150 150 if (lost_race()) {
151 151 // Nothing to do.
152 152 return;
153 153 }
154 154 HS_DTRACE_PROBE(hs_private, cms__remark__begin);
155 155
156 156 GenCollectedHeap* gch = GenCollectedHeap::heap();
157 157 GCCauseSetter gccs(gch, GCCause::_cms_final_remark);
158 158
159 159 VM_CMS_Operation::verify_before_gc();
160 160
161 161 IsGCActiveMark x; // stop-world GC active
162 162 _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsFinal);
163 163
164 164 VM_CMS_Operation::verify_after_gc();
165 165 HS_DTRACE_PROBE(hs_private, cms__remark__end);
166 166 }
167 167
168 168 // VM operation to invoke a concurrent collection of a
169 169 // GenCollectedHeap heap.
170 170 void VM_GenCollectFullConcurrent::doit() {
171 171 assert(Thread::current()->is_VM_thread(), "Should be VM thread");
172 172 assert(GCLockerInvokesConcurrent || ExplicitGCInvokesConcurrent, "Unexpected");
173 173
174 174 GenCollectedHeap* gch = GenCollectedHeap::heap();
175 175 if (_gc_count_before == gch->total_collections()) {
176 176 // The "full" of do_full_collection call below "forces"
177 177 // a collection; the second arg, 0, below ensures that
178 178 // only the young gen is collected. XXX In the future,
179 179 // we'll probably need to have something in this interface
180 180 // to say do this only if we are sure we will not bail
181 181 // out to a full collection in this attempt, but that's
182 182 // for the future.
183 183 assert(SafepointSynchronize::is_at_safepoint(),
184 184 "We can only be executing this arm of if at a safepoint");
↓ open down ↓ |
184 lines elided |
↑ open up ↑ |
185 185 GCCauseSetter gccs(gch, _gc_cause);
186 186 gch->do_full_collection(gch->must_clear_all_soft_refs(),
187 187 0 /* collect only youngest gen */);
188 188 } // Else no need for a foreground young gc
189 189 assert((_gc_count_before < gch->total_collections()) ||
190 190 (GC_locker::is_active() /* gc may have been skipped */
191 191 && (_gc_count_before == gch->total_collections())),
192 192 "total_collections() should be monotonically increasing");
193 193
194 194 MutexLockerEx x(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
195 + assert(_full_gc_count_before <= gch->total_full_collections(), "Error");
195 196 if (gch->total_full_collections() == _full_gc_count_before) {
196 - // Disable iCMS until the full collection is done.
197 + // Disable iCMS until the full collection is done, and
198 + // remember that we did so.
197 199 CMSCollector::disable_icms();
200 + _disabled_icms = true;
198 201 // In case CMS thread was in icms_wait(), wake it up.
199 202 CMSCollector::start_icms();
200 203 // Nudge the CMS thread to start a concurrent collection.
201 204 CMSCollector::request_full_gc(_full_gc_count_before);
202 205 } else {
206 + assert(_full_gc_count_before < gch->total_full_collections(), "Error");
203 207 FullGCCount_lock->notify_all(); // Inform the Java thread its work is done
204 208 }
205 209 }
206 210
207 211 bool VM_GenCollectFullConcurrent::evaluate_at_safepoint() const {
208 212 Thread* thr = Thread::current();
209 213 assert(thr != NULL, "Unexpected tid");
210 214 if (!thr->is_Java_thread()) {
211 215 assert(thr->is_VM_thread(), "Expected to be evaluated by VM thread");
212 216 GenCollectedHeap* gch = GenCollectedHeap::heap();
213 217 if (_gc_count_before != gch->total_collections()) {
214 218 // No need to do a young gc, we'll just nudge the CMS thread
215 219 // in the doit() method above, to be executed soon.
216 220 assert(_gc_count_before < gch->total_collections(),
217 221 "total_collections() should be monotnically increasing");
218 222 return false; // no need for foreground young gc
219 223 }
220 224 }
221 225 return true; // may still need foreground young gc
222 226 }
223 227
224 228
225 229 void VM_GenCollectFullConcurrent::doit_epilogue() {
226 230 Thread* thr = Thread::current();
227 231 assert(thr->is_Java_thread(), "just checking");
228 232 JavaThread* jt = (JavaThread*)thr;
229 233 // Release the Heap_lock first.
230 234 Heap_lock->unlock();
231 235 release_and_notify_pending_list_lock();
232 236
233 237 // It is fine to test whether completed collections has
234 238 // exceeded our request count without locking because
235 239 // the completion count is monotonically increasing;
236 240 // this will break for very long-running apps when the
237 241 // count overflows and wraps around. XXX fix me !!!
238 242 // e.g. at the rate of 1 full gc per ms, this could
239 243 // overflow in about 1000 years.
240 244 GenCollectedHeap* gch = GenCollectedHeap::heap();
241 245 if (_gc_cause != GCCause::_gc_locker &&
242 246 gch->total_full_collections_completed() <= _full_gc_count_before) {
243 247 // maybe we should change the condition to test _gc_cause ==
244 248 // GCCause::_java_lang_system_gc, instead of
245 249 // _gc_cause != GCCause::_gc_locker
246 250 assert(_gc_cause == GCCause::_java_lang_system_gc,
247 251 "the only way to get here if this was a System.gc()-induced GC");
248 252 assert(ExplicitGCInvokesConcurrent, "Error");
249 253 // Now, wait for witnessing concurrent gc cycle to complete,
250 254 // but do so in native mode, because we want to lock the
251 255 // FullGCEvent_lock, which may be needed by the VM thread
↓ open down ↓ |
39 lines elided |
↑ open up ↑ |
252 256 // or by the CMS thread, so we do not want to be suspended
253 257 // while holding that lock.
254 258 ThreadToNativeFromVM native(jt);
255 259 MutexLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
256 260 // Either a concurrent or a stop-world full gc is sufficient
257 261 // witness to our request.
258 262 while (gch->total_full_collections_completed() <= _full_gc_count_before) {
259 263 FullGCCount_lock->wait(Mutex::_no_safepoint_check_flag);
260 264 }
261 265 }
262 - // Enable iCMS back.
263 - CMSCollector::enable_icms();
266 + // Enable iCMS back if we disabled it earlier.
267 + if (_disabled_icms) {
268 + CMSCollector::enable_icms();
269 + }
264 270 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX