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