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 #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 and 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   void regulate_interleaved_cycles();
69   void regulate_concurrent_cycles();
70   void regulate_heap();
71 
72   bool start_old_cycle();
73   bool start_young_cycle();
74   bool start_global_cycle();
75 
76   ShenandoahSharedFlag _heap_changed;
77   ShenandoahControlThread* _control_thread;
78   ShenandoahHeuristics* _young_heuristics;
79   ShenandoahHeuristics* _old_heuristics;
80   ShenandoahHeuristics* _global_heuristics;
81 
82   int _sleep;
83   double _last_sleep_adjust_time;
84 
85   void regulator_sleep();
86 };
87 
88 
89 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHREGULATORTHREAD_HPP