1 /*
  2  * Copyright (c) 2013, 2021, Red Hat, Inc. 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 
 25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHCONTROLTHREAD_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHCONTROLTHREAD_HPP
 27 
 28 #include "gc/shared/gcCause.hpp"
 29 #include "gc/shared/concurrentGCThread.hpp"
 30 #include "gc/shenandoah/shenandoahGC.hpp"
 31 #include "gc/shenandoah/shenandoahHeap.hpp"
 32 #include "gc/shenandoah/shenandoahPadding.hpp"
 33 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
 34 #include "runtime/task.hpp"
 35 #include "utilities/ostream.hpp"
 36 
 37 // Periodic task is useful for doing asynchronous things that do not require (heap) locks,
 38 // or synchronization with other parts of collector. These could run even when ShenandoahConcurrentThread
 39 // is busy driving the GC cycle.
 40 class ShenandoahPeriodicTask : public PeriodicTask {
 41 private:
 42   ShenandoahControlThread* _thread;
 43 public:
 44   ShenandoahPeriodicTask(ShenandoahControlThread* thread) :
 45           PeriodicTask(100), _thread(thread) {}
 46   virtual void task();
 47 };
 48 
 49 // Periodic task to notify blocked paced waiters.
 50 class ShenandoahPeriodicPacerNotify : public PeriodicTask {
 51 public:
 52   ShenandoahPeriodicPacerNotify() : PeriodicTask(PeriodicTask::min_interval) {}
 53   virtual void task();
 54 };
 55 
 56 class ShenandoahControlThread: public ConcurrentGCThread {
 57   friend class VMStructs;
 58 
 59 private:
 60   // While we could have a single lock for these, it may risk unblocking
 61   // GC waiters when alloc failure GC cycle finishes. We want instead
 62   // to make complete explicit cycle for demanding customers.
 63   Monitor _alloc_failure_waiters_lock;
 64   Monitor _gc_waiters_lock;
 65   Monitor _control_lock;
 66   Monitor _regulator_lock;
 67   ShenandoahPeriodicTask _periodic_task;
 68   ShenandoahPeriodicPacerNotify _periodic_pacer_notify_task;
 69 
 70 public:
 71   typedef enum {
 72     none,
 73     concurrent_normal,
 74     stw_degenerated,
 75     stw_full,
 76     marking_old
 77   } GCMode;
 78 
 79   void run_service();
 80   void stop_service();
 81 
 82   size_t get_gc_id();
 83 
 84 private:
 85   ShenandoahSharedFlag _allow_old_preemption;
 86   ShenandoahSharedFlag _preemption_requested;
 87   ShenandoahSharedFlag _gc_requested;
 88   ShenandoahSharedFlag _alloc_failure_gc;
 89   ShenandoahSharedFlag _graceful_shutdown;
 90   ShenandoahSharedFlag _do_counters_update;
 91   ShenandoahSharedFlag _force_counters_update;
 92   GCCause::Cause       _requested_gc_cause;
 93   GenerationMode       _requested_generation;
 94   ShenandoahGC::ShenandoahDegenPoint _degen_point;
 95   ShenandoahGeneration* _degen_generation;
 96 
 97   shenandoah_padding(0);
 98   volatile size_t _allocs_seen;
 99   shenandoah_padding(1);
100   volatile size_t _gc_id;
101   shenandoah_padding(2);
102   volatile GCMode _mode;
103   shenandoah_padding(3);
104 
105   bool check_cancellation_or_degen(ShenandoahGC::ShenandoahDegenPoint point);
106   void resume_concurrent_old_cycle(ShenandoahGeneration* generation, GCCause::Cause cause);
107   void service_concurrent_cycle(ShenandoahGeneration* generation, GCCause::Cause cause, bool reset_old_bitmap_specially);
108   void service_stw_full_cycle(GCCause::Cause cause);
109 
110   // Return true if degenerated cycle finishes normally.  Return false if the degenerated cycle transformed itself
111   // into a full GC.
112   bool service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahGC::ShenandoahDegenPoint point);
113   void service_uncommit(double shrink_before, size_t shrink_until);
114 
115   bool try_set_alloc_failure_gc();
116   void notify_alloc_failure_waiters();
117   bool is_alloc_failure_gc();
118 
119   void reset_gc_id();
120   void update_gc_id();
121 
122   void notify_gc_waiters();
123 
124   // Handle GC request.
125   // Blocks until GC is over.
126   void handle_requested_gc(GCCause::Cause cause);
127 
128   bool is_explicit_gc(GCCause::Cause cause) const;
129   bool is_implicit_gc(GCCause::Cause cause) const;
130 
131   bool preempt_old_marking(GenerationMode generation);
132 
133   bool check_soft_max_changed() const;
134 
135   void process_phase_timings(const ShenandoahHeap* heap);
136 
137 public:
138   // Constructor
139   ShenandoahControlThread();
140   ~ShenandoahControlThread();
141 
142   // Handle allocation failure from normal allocation.
143   // Blocks until memory is available.
144   void handle_alloc_failure(ShenandoahAllocRequest& req);
145 
146   // Handle allocation failure from evacuation path.
147   // Optionally blocks while collector is handling the failure.
148   void handle_alloc_failure_evac(size_t words);
149 
150   void request_gc(GCCause::Cause cause);
151   bool request_concurrent_gc(GenerationMode generation);
152 
153   void handle_counters_update();
154   void handle_force_counters_update();
155   void set_forced_counters_update(bool value);
156 
157   void notify_heap_changed();
158 
159   void pacing_notify_alloc(size_t words);
160 
161   void start();
162   void prepare_for_graceful_shutdown();
163   bool in_graceful_shutdown();
164 
165   const char* name() const { return "ShenandoahControlThread";}
166 
167   // Printing
168   void print_on(outputStream* st) const;
169   void print() const;
170 
171   void service_concurrent_normal_cycle(const ShenandoahHeap* heap,
172                                        const GenerationMode generation,
173                                        GCCause::Cause cause);
174 
175   void service_concurrent_old_cycle(const ShenandoahHeap* heap,
176                                     GCCause::Cause &cause);
177 
178   void set_gc_mode(GCMode new_mode);
179   GCMode gc_mode() {
180     return _mode;
181   }
182 
183  private:
184   static const char* gc_mode_name(GCMode mode);
185   void notify_control_thread();
186 };
187 
188 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCONTROLTHREAD_HPP