1 /* 2 * Copyright (c) 2013, 2015, Red Hat, Inc. and/or its affiliates. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "gc/shared/gcTraceTime.inline.hpp" 25 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp" 26 #include "gc/shenandoah/shenandoahConcurrentThread.hpp" 27 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 29 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp" 30 #include "gc/shenandoah/vm_operations_shenandoah.hpp" 31 #include "memory/iterator.hpp" 32 #include "memory/universe.hpp" 33 #include "runtime/vmThread.hpp" 34 35 ShenandoahConcurrentThread::ShenandoahConcurrentThread() : 36 ConcurrentGCThread(), 37 _full_gc_lock(Mutex::leaf, "ShenandoahFullGC_lock", true, Monitor::_safepoint_check_always), 38 _do_full_gc(false), 39 _graceful_shutdown(0) 40 { 41 create_and_start(); 42 } 43 44 ShenandoahConcurrentThread::~ShenandoahConcurrentThread() { 45 // This is here so that super is called. 46 } 47 48 void ShenandoahConcurrentThread::run_service() { 49 ShenandoahHeap* heap = ShenandoahHeap::heap(); 50 51 while (!should_terminate()) { 52 if (in_graceful_shutdown()) { 53 break; 54 } else if (is_full_gc()) { 55 service_fullgc_cycle(); 56 } else if (heap->shenandoahPolicy()->should_start_concurrent_mark(heap->used(), heap->capacity())) { 57 service_normal_cycle(); 58 if (heap->is_evacuation_in_progress()) { 59 heap->set_evacuation_in_progress_concurrently(false); 60 } 61 } else { 62 Thread::current()->_ParkEvent->park(10); 63 } 64 heap->monitoring_support()->update_counters(); 65 66 // Make sure the _do_full_gc flag changes are seen. 67 OrderAccess::storeload(); 68 } 69 70 // Wait for the actual stop(), can't leave run_service() earlier. 71 while (!should_terminate()) { 72 Thread::current()->_ParkEvent->park(10); 73 } 74 } 75 76 void ShenandoahConcurrentThread::service_normal_cycle() { 77 if (check_cancellation()) return; 78 79 ShenandoahHeap* heap = ShenandoahHeap::heap(); 80 81 GCTimer* gc_timer = heap->gc_timer(); 82 83 gc_timer->register_gc_start(); 84 85 heap->shenandoahPolicy()->increase_cycle_counter(); 86 87 GCIdMark gc_id_mark; 88 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters()); 89 TraceMemoryManagerStats tmms(false, GCCause::_no_cause_specified); 90 91 // Start initial mark under STW: 92 { 93 // Workers are setup by VM_ShenandoahInitMark 94 TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters()); 95 VM_ShenandoahInitMark initMark; 96 heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross); 97 heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::init_mark_gross); 98 VMThread::execute(&initMark); 99 heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::init_mark_gross); 100 heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross); 101 } 102 103 if (check_cancellation()) return; 104 105 // Continue concurrent mark: 106 { 107 // Setup workers for concurrent marking phase 108 WorkGang* workers = heap->workers(); 109 uint n_workers = ShenandoahCollectorPolicy::calc_workers_for_conc_marking(workers->active_workers(), 110 Threads::number_of_non_daemon_threads()); 111 ShenandoahWorkerScope scope(workers, n_workers); 112 113 GCTraceTime(Info, gc) time("Concurrent marking", gc_timer, GCCause::_no_gc, true); 114 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters()); 115 ShenandoahHeap::heap()->concurrentMark()->mark_from_roots(); 116 } 117 118 // Possibly hand over remaining marking work to final-mark phase. 119 bool clear_full_gc = false; 120 if (heap->cancelled_concgc()) { 121 heap->shenandoahPolicy()->record_cm_cancelled(); 122 if (_full_gc_cause == GCCause::_allocation_failure && 123 heap->shenandoahPolicy()->handover_cancelled_marking()) { 124 heap->set_cancelled_concgc(false); 125 clear_full_gc = true; 126 heap->shenandoahPolicy()->record_cm_degenerated(); 127 } else { 128 heap->gc_timer()->register_gc_end(); 129 return; 130 } 131 } else { 132 heap->shenandoahPolicy()->record_cm_success(); 133 } 134 135 // Proceed to complete marking under STW, and start evacuation: 136 { 137 // Workers are setup by VM_ShenandoahStartEvacuation 138 TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters()); 139 VM_ShenandoahStartEvacuation finishMark; 140 heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross); 141 heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::final_mark_gross); 142 VMThread::execute(&finishMark); 143 heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::final_mark_gross); 144 heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross); 145 } 146 147 if (check_cancellation()) return; 148 149 // If we handed off remaining marking work above, we need to kick off waiting Java threads 150 if (clear_full_gc) { 151 reset_full_gc(); 152 } 153 154 // Continue concurrent evacuation: 155 { 156 // Setup workers for concurrent evacuation phase 157 WorkGang* workers = heap->workers(); 158 uint n_workers = ShenandoahCollectorPolicy::calc_workers_for_conc_evacuation(workers->active_workers(), 159 Threads::number_of_non_daemon_threads()); 160 ShenandoahWorkerScope scope(workers, n_workers); 161 162 GCTraceTime(Info, gc) time("Concurrent evacuation ", gc_timer, GCCause::_no_gc, true); 163 TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters()); 164 heap->do_evacuation(); 165 } 166 167 // Prepare for the next normal cycle: 168 if (check_cancellation()) return; 169 170 { 171 GCTraceTime(Info, gc) time("Concurrent reset bitmaps", gc_timer, GCCause::_no_gc); 172 heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::reset_bitmaps); 173 WorkGang* workers = heap->workers(); 174 ShenandoahPushWorkerScope scope(workers, heap->max_workers()); 175 heap->reset_next_mark_bitmap(workers); 176 heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::reset_bitmaps); 177 } 178 179 gc_timer->register_gc_end(); 180 } 181 182 bool ShenandoahConcurrentThread::check_cancellation() { 183 ShenandoahHeap* heap = ShenandoahHeap::heap(); 184 if (heap->cancelled_concgc()) { 185 assert (is_full_gc() || in_graceful_shutdown(), "Cancel GC either for Full GC, or gracefully exiting"); 186 heap->gc_timer()->register_gc_end(); 187 return true; 188 } 189 return false; 190 } 191 192 193 void ShenandoahConcurrentThread::stop_service() { 194 // Nothing to do here. 195 } 196 197 void ShenandoahConcurrentThread::service_fullgc_cycle() { 198 GCIdMark gc_id_mark; 199 ShenandoahHeap* heap = ShenandoahHeap::heap(); 200 201 { 202 if (_full_gc_cause == GCCause::_allocation_failure) { 203 heap->shenandoahPolicy()->record_allocation_failure_gc(); 204 } else { 205 heap->shenandoahPolicy()->record_user_requested_gc(); 206 } 207 208 TraceCollectorStats tcs(heap->monitoring_support()->full_collection_counters()); 209 TraceMemoryManagerStats tmms(true, _full_gc_cause); 210 VM_ShenandoahFullGC full_gc(_full_gc_cause); 211 VMThread::execute(&full_gc); 212 } 213 214 reset_full_gc(); 215 } 216 217 void ShenandoahConcurrentThread::do_full_gc(GCCause::Cause cause) { 218 assert(Thread::current()->is_Java_thread(), "expect Java thread here"); 219 220 if (try_set_full_gc()) { 221 _full_gc_cause = cause; 222 223 // Now that full GC is scheduled, we can abort everything else 224 ShenandoahHeap::heap()->cancel_concgc(cause); 225 } else { 226 if (_full_gc_cause != cause) { 227 log_info(gc)("Full GC is already pending with cause: %s; new cause is %s", 228 GCCause::to_string(_full_gc_cause), 229 GCCause::to_string(cause)); 230 } 231 } 232 233 MonitorLockerEx ml(&_full_gc_lock); 234 while (is_full_gc()) { 235 ml.wait(); 236 } 237 assert(!is_full_gc(), "expect full GC to have completed"); 238 } 239 240 void ShenandoahConcurrentThread::reset_full_gc() { 241 OrderAccess::release_store_fence(&_do_full_gc, 0); 242 MonitorLockerEx ml(&_full_gc_lock); 243 ml.notify_all(); 244 } 245 246 bool ShenandoahConcurrentThread::try_set_full_gc() { 247 jbyte old = Atomic::cmpxchg(1, &_do_full_gc, 0); 248 return old == 0; // success 249 } 250 251 bool ShenandoahConcurrentThread::is_full_gc() { 252 return OrderAccess::load_acquire(&_do_full_gc) == 1; 253 } 254 255 void ShenandoahConcurrentThread::print() const { 256 print_on(tty); 257 } 258 259 void ShenandoahConcurrentThread::print_on(outputStream* st) const { 260 st->print("Shenandoah Concurrent Thread"); 261 Thread::print_on(st); 262 st->cr(); 263 } 264 265 void ShenandoahConcurrentThread::sleepBeforeNextCycle() { 266 assert(false, "Wake up in the GC thread that never sleeps :-)"); 267 } 268 269 void ShenandoahConcurrentThread::start() { 270 create_and_start(); 271 } 272 273 void ShenandoahConcurrentThread::prepare_for_graceful_shutdown() { 274 OrderAccess::release_store_fence(&_graceful_shutdown, 1); 275 } 276 277 bool ShenandoahConcurrentThread::in_graceful_shutdown() { 278 return OrderAccess::load_acquire(&_graceful_shutdown) == 1; 279 }