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   typedef enum {
 61     none,
 62     concurrent_normal,
 63     stw_degenerated,
 64     stw_full
 65   } GCMode;
 66 
 67   // While we could have a single lock for these, it may risk unblocking
 68   // GC waiters when alloc failure GC cycle finishes. We want instead
 69   // to make complete explicit cycle for for demanding customers.
 70   Monitor _alloc_failure_waiters_lock;
 71   Monitor _gc_waiters_lock;
 72   ShenandoahPeriodicTask _periodic_task;
 73   ShenandoahPeriodicPacerNotify _periodic_pacer_notify_task;
 74 
 75 public:
 76   void run_service();
 77   void stop_service();
 78 
 79 private:
 80   ShenandoahSharedFlag _gc_requested;
 81   ShenandoahSharedFlag _alloc_failure_gc;
 82   ShenandoahSharedFlag _graceful_shutdown;
 83   ShenandoahSharedFlag _heap_changed;
 84   ShenandoahSharedFlag _do_counters_update;
 85   ShenandoahSharedFlag _force_counters_update;
 86   GCCause::Cause       _requested_gc_cause;
 87   ShenandoahGC::ShenandoahDegenPoint _degen_point;
 88 
 89   shenandoah_padding(0);
 90   volatile size_t _allocs_seen;
 91   shenandoah_padding(1);
 92   volatile size_t _gc_id;
 93   shenandoah_padding(2);
 94 
 95   bool check_cancellation_or_degen(ShenandoahGC::ShenandoahDegenPoint point);
 96   void service_concurrent_normal_cycle(GCCause::Cause cause);
 97   void service_stw_full_cycle(GCCause::Cause cause);
 98   void service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahGC::ShenandoahDegenPoint point);
 99   void service_uncommit(double shrink_before, size_t shrink_until);
100 
101   bool try_set_alloc_failure_gc();
102   void notify_alloc_failure_waiters();
103   bool is_alloc_failure_gc();
104 
105   void reset_gc_id();
106   void update_gc_id();
107   size_t get_gc_id();
108 
109   void notify_gc_waiters();
110 
111   // Handle GC request.
112   // Blocks until GC is over.
113   void handle_requested_gc(GCCause::Cause cause);
114 
115   bool is_explicit_gc(GCCause::Cause cause) const;
116 
117   bool check_soft_max_changed() const;
118 
119 public:
120   // Constructor
121   ShenandoahControlThread();
122   ~ShenandoahControlThread();
123 
124   // Handle allocation failure from normal allocation.
125   // Blocks until memory is available.
126   void handle_alloc_failure(ShenandoahAllocRequest& req);
127 
128   // Handle allocation failure from evacuation path.
129   // Optionally blocks while collector is handling the failure.
130   void handle_alloc_failure_evac(size_t words);
131 
132   void request_gc(GCCause::Cause cause);
133 
134   void handle_counters_update();
135   void handle_force_counters_update();
136   void set_forced_counters_update(bool value);
137 
138   void notify_heap_changed();
139 
140   void pacing_notify_alloc(size_t words);
141 
142   void start();
143   void prepare_for_graceful_shutdown();
144   bool in_graceful_shutdown();
145 
146   const char* name() const { return "ShenandoahControlThread";}
147 
148   // Printing
149   void print_on(outputStream* st) const;
150   void print() const;
151 };
152 
153 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCONTROLTHREAD_HPP