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 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHREGULATORTHREAD_HPP
 25 #define SHARE_GC_SHENANDOAH_SHENANDOAHREGULATORTHREAD_HPP
 26 
 27 #include "gc/shared/concurrentGCThread.hpp"
 28 #include "gc/shared/gcCause.hpp"
 29 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 30 #include "runtime/mutex.hpp"
 31 
 32 class ShenandoahHeuristics;
 33 class ShenandoahControlThread;
 34 
 35 /*
 36  * The purpose of this class (and thread) is to allow us to continue
 37  * to evaluate heuristics during a garbage collection. This is necessary
 38  * to allow young generation collections to interrupt an old generation
 39  * collection which is in-progress. This puts heuristic triggers on the
 40  * same footing as other gc requests (alloc failure, System.gc, etc.).
 41  * However, this regulator does not block after submitting a gc request.
 42  *
 43  * We could use a PeriodicTask for this, but this thread will sleep longer
 44  * when the allocation rate is lower and PeriodicTasks cannot adjust their
 45  * sleep time.
 46  */
 47 class ShenandoahRegulatorThread: public ConcurrentGCThread {
 48   friend class VMStructs;
 49 
 50  public:
 51   explicit ShenandoahRegulatorThread(ShenandoahControlThread* control_thread);
 52 
 53   const char* name() const { return "ShenandoahRegulatorThread";}
 54 
 55   // This is called from allocation path, and thus should be fast.
 56   void notify_heap_changed() {
 57     // Notify that something had changed.
 58     if (_heap_changed.is_unset()) {
 59       _heap_changed.set();
 60     }
 61   }
 62 
 63  protected:
 64   void run_service();
 65   void stop_service();
 66 
 67  private:
 68   // When mode is generational
 69   void regulate_young_and_old_cycles();
 70   // When mode is generational, but ShenandoahAllowOldMarkingPreemption is false
 71   void regulate_young_and_global_cycles();
 72   // Default behavior for other modes (single generation).
 73   void regulate_global_cycles();
 74 
 75   // These return true if a cycle was started.
 76   bool start_old_cycle();
 77   bool start_young_cycle();
 78   bool start_global_cycle();
 79 
 80   // The generational mode can only unload classes in a global cycle. The regulator
 81   // thread itself will trigger a global cycle if metaspace is out of memory.
 82   bool should_start_metaspace_gc();
 83 
 84   // Regulator will sleep longer when the allocation rate is lower.
 85   void regulator_sleep();
 86 
 87   // Provides instrumentation to track how long it takes to acknowledge a request.
 88   bool request_concurrent_gc(ShenandoahGenerationType generation);
 89 
 90   ShenandoahSharedFlag _heap_changed;
 91   ShenandoahControlThread* _control_thread;
 92   ShenandoahHeuristics* _young_heuristics;
 93   ShenandoahHeuristics* _old_heuristics;
 94   ShenandoahHeuristics* _global_heuristics;
 95 
 96   int _sleep;
 97   double _last_sleep_adjust_time;
 98 };
 99 
100 
101 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHREGULATORTHREAD_HPP