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 ShenandoahHeap;
30 class ShenandoahHeuristics;
31 class ShenandoahGeneration;
32 class ShenandoahGenerationalControlThread;
33 class ShenandoahOldHeuristics;
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(ShenandoahGenerationalControlThread* control_thread);
52 
53  protected:
54   void run_service() override;
55   void stop_service() override;
56 
57  private:
58   // When mode is generational
59   void regulate_young_and_old_cycles();
60   // When mode is generational, but ShenandoahAllowOldMarkingPreemption is false
61   void regulate_young_and_global_cycles();
62 
63   // These return true if a cycle was started.
64   bool start_old_cycle() const;
65   bool start_young_cycle() const;
66   bool start_global_cycle() const;
67   bool resume_old_cycle();
68 
69   // The generational mode can only unload classes in a global cycle. The regulator
70   // thread itself will trigger a global cycle if metaspace is out of memory.
71   bool should_start_metaspace_gc();
72 
73   // Regulator will sleep longer when the allocation rate is lower.
74   void regulator_sleep();
75 
76   // Provides instrumentation to track how long it takes to acknowledge a request.
77   bool request_concurrent_gc(ShenandoahGeneration* generation) const;
78 
79   ShenandoahHeap* _heap;
80   ShenandoahGenerationalControlThread* _control_thread;
81   ShenandoahHeuristics* _young_heuristics;
82   ShenandoahOldHeuristics* _old_heuristics;
83   ShenandoahHeuristics* _global_heuristics;
84 
85   uint _sleep;
86   double _last_sleep_adjust_time;
87 };
88 
89 
90 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHREGULATORTHREAD_HPP