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 
29 class ShenandoahHeuristics;
30 class ShenandoahGenerationalControlThread;
31 
32 /*
33  * The purpose of this class (and thread) is to allow us to continue
34  * to evaluate heuristics during a garbage collection. This is necessary
35  * to allow young generation collections to interrupt an old generation
36  * collection which is in-progress. This puts heuristic triggers on the
37  * same footing as other gc requests (alloc failure, System.gc, etc.).
38  * However, this regulator does not block after submitting a gc request.
39  *
40  * We could use a PeriodicTask for this, but this thread will sleep longer
41  * when the allocation rate is lower and PeriodicTasks cannot adjust their
42  * sleep time.
43  */
44 class ShenandoahRegulatorThread: public ConcurrentGCThread {
45   friend class VMStructs;
46 
47  public:
48   explicit ShenandoahRegulatorThread(ShenandoahGenerationalControlThread* control_thread);
49 
50  protected:
51   void run_service() override;
52   void stop_service() override;
53 
54  private:
55   // When mode is generational
56   void regulate_young_and_old_cycles();
57   // When mode is generational, but ShenandoahAllowOldMarkingPreemption is false
58   void regulate_young_and_global_cycles();
59 
60   // These return true if a cycle was started.
61   bool start_old_cycle();
62   bool start_young_cycle();
63   bool start_global_cycle();
64 
65   // The generational mode can only unload classes in a global cycle. The regulator
66   // thread itself will trigger a global cycle if metaspace is out of memory.
67   bool should_start_metaspace_gc();
68 
69   // Regulator will sleep longer when the allocation rate is lower.
70   void regulator_sleep();
71 
72   // Provides instrumentation to track how long it takes to acknowledge a request.
73   bool request_concurrent_gc(ShenandoahGenerationType generation);
74 
75   ShenandoahGenerationalControlThread* _control_thread;
76   ShenandoahHeuristics* _young_heuristics;
77   ShenandoahHeuristics* _old_heuristics;
78   ShenandoahHeuristics* _global_heuristics;
79 
80   uint _sleep;
81   double _last_sleep_adjust_time;
82 };
83 
84 
85 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHREGULATORTHREAD_HPP