1 /* 2 * Copyright Amazon.com Inc. 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 #include "precompiled.hpp" 25 26 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp" 27 #include "gc/shenandoah/mode/shenandoahMode.hpp" 28 #include "gc/shenandoah/shenandoahAsserts.hpp" 29 #include "gc/shenandoah/shenandoahGenerationalControlThread.hpp" 30 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 31 #include "gc/shenandoah/shenandoahOldGeneration.hpp" 32 #include "gc/shenandoah/shenandoahRegulatorThread.hpp" 33 #include "gc/shenandoah/shenandoahYoungGeneration.hpp" 34 #include "logging/log.hpp" 35 36 ShenandoahRegulatorThread::ShenandoahRegulatorThread(ShenandoahGenerationalControlThread* control_thread) : 37 ConcurrentGCThread(), 38 _control_thread(control_thread), 39 _sleep(ShenandoahControlIntervalMin), 40 _last_sleep_adjust_time(os::elapsedTime()) { 41 shenandoah_assert_generational(); 42 ShenandoahHeap* heap = ShenandoahHeap::heap(); 43 _old_heuristics = heap->old_generation()->heuristics(); 44 _young_heuristics = heap->young_generation()->heuristics(); 45 _global_heuristics = heap->global_generation()->heuristics(); 46 47 set_name("Shenandoah Regulator Thread"); 48 create_and_start(); 49 } 50 51 void ShenandoahRegulatorThread::run_service() { 52 if (ShenandoahAllowOldMarkingPreemption) { 53 regulate_young_and_old_cycles(); 54 } else { 55 regulate_young_and_global_cycles(); 56 } 57 58 log_debug(gc)("%s: Done.", name()); 59 } 60 61 void ShenandoahRegulatorThread::regulate_young_and_old_cycles() { 62 while (!should_terminate()) { 63 ShenandoahGenerationalControlThread::GCMode mode = _control_thread->gc_mode(); 64 if (mode == ShenandoahGenerationalControlThread::none) { 65 if (should_start_metaspace_gc()) { 66 if (request_concurrent_gc(GLOBAL)) { 67 // Some of vmTestbase/metaspace tests depend on following line to count GC cycles 68 _global_heuristics->log_trigger("%s", GCCause::to_string(GCCause::_metadata_GC_threshold)); 69 } 70 } else { 71 if (_young_heuristics->should_start_gc()) { 72 // Give the old generation a chance to run. The old generation cycle 73 // begins with a 'bootstrap' cycle that will also collect young. 74 if (start_old_cycle()) { 75 log_debug(gc)("Heuristics request for old collection accepted"); 76 } else if (request_concurrent_gc(YOUNG)) { 77 log_debug(gc)("Heuristics request for young collection accepted"); 78 } 79 } 80 } 81 } else if (mode == ShenandoahGenerationalControlThread::servicing_old) { 82 if (start_young_cycle()) { 83 log_debug(gc)("Heuristics request to interrupt old for young collection accepted"); 84 } 85 } 86 87 regulator_sleep(); 88 } 89 } 90 91 92 void ShenandoahRegulatorThread::regulate_young_and_global_cycles() { 93 while (!should_terminate()) { 94 if (_control_thread->gc_mode() == ShenandoahGenerationalControlThread::none) { 95 if (start_global_cycle()) { 96 log_debug(gc)("Heuristics request for global collection accepted."); 97 } else if (start_young_cycle()) { 98 log_debug(gc)("Heuristics request for young collection accepted."); 99 } 100 } 101 102 regulator_sleep(); 103 } 104 } 105 106 void ShenandoahRegulatorThread::regulator_sleep() { 107 // Wait before performing the next action. If allocation happened during this wait, 108 // we exit sooner, to let heuristics re-evaluate new conditions. If we are at idle, 109 // back off exponentially. 110 double current = os::elapsedTime(); 111 112 if (ShenandoahHeap::heap()->has_changed()) { 113 _sleep = ShenandoahControlIntervalMin; 114 } else if ((current - _last_sleep_adjust_time) * 1000 > ShenandoahControlIntervalAdjustPeriod){ 115 _sleep = MIN2<uint>(ShenandoahControlIntervalMax, MAX2(1u, _sleep * 2)); 116 _last_sleep_adjust_time = current; 117 } 118 119 os::naked_short_sleep(_sleep); 120 if (LogTarget(Debug, gc, thread)::is_enabled()) { 121 double elapsed = os::elapsedTime() - current; 122 double hiccup = elapsed - double(_sleep); 123 if (hiccup > 0.001) { 124 log_debug(gc, thread)("Regulator hiccup time: %.3fs", hiccup); 125 } 126 } 127 } 128 129 bool ShenandoahRegulatorThread::start_old_cycle() { 130 return _old_heuristics->should_start_gc() && request_concurrent_gc(OLD); 131 } 132 133 bool ShenandoahRegulatorThread::start_young_cycle() { 134 return _young_heuristics->should_start_gc() && request_concurrent_gc(YOUNG); 135 } 136 137 bool ShenandoahRegulatorThread::start_global_cycle() { 138 return _global_heuristics->should_start_gc() && request_concurrent_gc(GLOBAL); 139 } 140 141 bool ShenandoahRegulatorThread::request_concurrent_gc(ShenandoahGenerationType generation) { 142 double now = os::elapsedTime(); 143 bool accepted = _control_thread->request_concurrent_gc(generation); 144 if (LogTarget(Debug, gc, thread)::is_enabled() && accepted) { 145 double wait_time = os::elapsedTime() - now; 146 if (wait_time > 0.001) { 147 log_debug(gc, thread)("Regulator waited %.3fs for control thread to acknowledge request.", wait_time); 148 } 149 } 150 return accepted; 151 } 152 153 void ShenandoahRegulatorThread::stop_service() { 154 log_debug(gc)("%s: Stop requested.", name()); 155 } 156 157 bool ShenandoahRegulatorThread::should_start_metaspace_gc() { 158 // The generational mode can, at present, only unload classes during a global 159 // cycle. For this reason, we treat an oom in metaspace as a _trigger_ for a 160 // global cycle. But, we check other prerequisites before starting a gc that won't 161 // unload anything. 162 return ClassUnloadingWithConcurrentMark 163 && _global_heuristics->can_unload_classes() 164 && _global_heuristics->has_metaspace_oom(); 165 }