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 log_debug(gc)("Heuristics request for global (unload classes) accepted."); 68 } 69 } else { 70 if (_young_heuristics->should_start_gc()) { 71 // Give the old generation a chance to run. The old generation cycle 72 // begins with a 'bootstrap' cycle that will also collect young. 73 if (start_old_cycle()) { 74 log_debug(gc)("Heuristics request for old collection accepted"); 75 } else if (request_concurrent_gc(YOUNG)) { 76 log_debug(gc)("Heuristics request for young collection accepted"); 77 } 78 } 79 } 80 } else if (mode == ShenandoahGenerationalControlThread::servicing_old) { 81 if (start_young_cycle()) { 82 log_debug(gc)("Heuristics request to interrupt old for young collection accepted"); 83 } 84 } 85 86 regulator_sleep(); 87 } 88 } 89 90 91 void ShenandoahRegulatorThread::regulate_young_and_global_cycles() { 92 while (!should_terminate()) { 93 if (_control_thread->gc_mode() == ShenandoahGenerationalControlThread::none) { 94 if (start_global_cycle()) { 95 log_debug(gc)("Heuristics request for global collection accepted."); 96 } else if (start_young_cycle()) { 97 log_debug(gc)("Heuristics request for young collection accepted."); 98 } 99 } 100 101 regulator_sleep(); 102 } 103 } 104 105 void ShenandoahRegulatorThread::regulator_sleep() { 106 // Wait before performing the next action. If allocation happened during this wait, 107 // we exit sooner, to let heuristics re-evaluate new conditions. If we are at idle, 108 // back off exponentially. 109 double current = os::elapsedTime(); 110 111 if (ShenandoahHeap::heap()->has_changed()) { 112 _sleep = ShenandoahControlIntervalMin; 113 } else if ((current - _last_sleep_adjust_time) * 1000 > ShenandoahControlIntervalAdjustPeriod){ 114 _sleep = MIN2<uint>(ShenandoahControlIntervalMax, MAX2(1u, _sleep * 2)); 115 _last_sleep_adjust_time = current; 116 } 117 118 os::naked_short_sleep(_sleep); 119 if (LogTarget(Debug, gc, thread)::is_enabled()) { 120 double elapsed = os::elapsedTime() - current; 121 double hiccup = elapsed - double(_sleep); 122 if (hiccup > 0.001) { 123 log_debug(gc, thread)("Regulator hiccup time: %.3fs", hiccup); 124 } 125 } 126 } 127 128 bool ShenandoahRegulatorThread::start_old_cycle() { 129 return _old_heuristics->should_start_gc() && request_concurrent_gc(OLD); 130 } 131 132 bool ShenandoahRegulatorThread::start_young_cycle() { 133 return _young_heuristics->should_start_gc() && request_concurrent_gc(YOUNG); 134 } 135 136 bool ShenandoahRegulatorThread::start_global_cycle() { 137 return _global_heuristics->should_start_gc() && request_concurrent_gc(GLOBAL); 138 } 139 140 bool ShenandoahRegulatorThread::request_concurrent_gc(ShenandoahGenerationType generation) { 141 double now = os::elapsedTime(); 142 bool accepted = _control_thread->request_concurrent_gc(generation); 143 if (LogTarget(Debug, gc, thread)::is_enabled() && accepted) { 144 double wait_time = os::elapsedTime() - now; 145 if (wait_time > 0.001) { 146 log_debug(gc, thread)("Regulator waited %.3fs for control thread to acknowledge request.", wait_time); 147 } 148 } 149 return accepted; 150 } 151 152 void ShenandoahRegulatorThread::stop_service() { 153 log_debug(gc)("%s: Stop requested.", name()); 154 } 155 156 bool ShenandoahRegulatorThread::should_start_metaspace_gc() { 157 // The generational mode can, at present, only unload classes during a global 158 // cycle. For this reason, we treat an oom in metaspace as a _trigger_ for a 159 // global cycle. But, we check other prerequisites before starting a gc that won't 160 // unload anything. 161 return ClassUnloadingWithConcurrentMark 162 && _global_heuristics->can_unload_classes() 163 && _global_heuristics->has_metaspace_oom(); 164 }