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