1 /*
  2  * Copyright (c) 2021, Amazon.com, Inc. and/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/shenandoahGeneration.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.inline.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 != NULL ? nullable->heuristics() : NULL;
 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_concurrent_cycles();
 57     } else {
 58       regulate_interleaved_cycles();
 59     }
 60   } else {
 61     regulate_heap();
 62   }
 63 
 64   log_info(gc)("%s: Done.", name());
 65 }
 66 
 67 void ShenandoahRegulatorThread::regulate_concurrent_cycles() {
 68   assert(_young_heuristics != NULL, "Need young heuristics.");
 69   assert(_old_heuristics != NULL, "Need old heuristics.");
 70 
 71   while (!should_terminate()) {
 72     ShenandoahControlThread::GCMode mode = _control_thread->gc_mode();
 73     if (mode == ShenandoahControlThread::none) {
 74       if (start_old_cycle()) {
 75         log_info(gc)("Heuristics request for old collection accepted");
 76       } else if (start_young_cycle()) {
 77         log_info(gc)("Heuristics request for young collection accepted");
 78       }
 79     } else if (mode == ShenandoahControlThread::marking_old) {
 80       if (start_young_cycle()) {
 81         log_info(gc)("Heuristics request for young collection accepted");
 82       }
 83     }
 84 
 85     regulator_sleep();
 86   }
 87 }
 88 
 89 void ShenandoahRegulatorThread::regulate_interleaved_cycles() {
 90   assert(_young_heuristics != NULL, "Need young heuristics.");
 91   assert(_global_heuristics != NULL, "Need global heuristics.");
 92 
 93   while (!should_terminate()) {
 94     if (_control_thread->gc_mode() == ShenandoahControlThread::none) {
 95       if (start_global_cycle()) {
 96         log_info(gc)("Heuristics request for global collection accepted.");
 97       } else if (start_young_cycle()) {
 98         log_info(gc)("Heuristics request for young collection accepted.");
 99       }
100     }
101 
102     regulator_sleep();
103   }
104 }
105 
106 void ShenandoahRegulatorThread::regulate_heap() {
107   assert(_global_heuristics != NULL, "Need global heuristics.");
108 
109   while (!should_terminate()) {
110     if (_control_thread->gc_mode() == ShenandoahControlThread::none) {
111       if (start_global_cycle()) {
112         log_info(gc)("Heuristics request for global collection accepted.");
113       }
114     }
115 
116     regulator_sleep();
117   }
118 }
119 
120 void ShenandoahRegulatorThread::regulator_sleep() {
121   // Wait before performing the next action. If allocation happened during this wait,
122   // we exit sooner, to let heuristics re-evaluate new conditions. If we are at idle,
123   // back off exponentially.
124   double current = os::elapsedTime();
125 
126   if (_heap_changed.try_unset()) {
127     _sleep = ShenandoahControlIntervalMin;
128   } else if ((current - _last_sleep_adjust_time) * 1000 > ShenandoahControlIntervalAdjustPeriod){
129     _sleep = MIN2<int>(ShenandoahControlIntervalMax, MAX2(1, _sleep * 2));
130     _last_sleep_adjust_time = current;
131   }
132 
133   os::naked_short_sleep(_sleep);
134 }
135 
136 bool ShenandoahRegulatorThread::start_old_cycle() {
137   return _old_heuristics->should_start_gc() && _control_thread->request_concurrent_gc(OLD);
138 }
139 
140 bool ShenandoahRegulatorThread::start_young_cycle() {
141   return _young_heuristics->should_start_gc() && _control_thread->request_concurrent_gc(YOUNG);
142 }
143 
144 bool ShenandoahRegulatorThread::start_global_cycle() {
145   return _global_heuristics->should_start_gc() && _control_thread->request_concurrent_gc(GLOBAL);
146 }
147 
148 void ShenandoahRegulatorThread::stop_service() {
149   log_info(gc)("%s: Stop requested.", name());
150 }
151