1 /*
   2  * Copyright (c) 2013, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTTHREAD_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTTHREAD_HPP
  26 
  27 #include "gc_interface/gcCause.hpp"
  28 #include "gc_implementation/shared/concurrentGCThread.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahHeap.hpp"
  30 #include "gc_implementation/shenandoah/shenandoahSharedVariables.hpp"
  31 #include "runtime/task.hpp"
  32 #include "utilities/ostream.hpp"
  33 
  34 class ShenandoahControlThread;
  35 
  36 // Periodic task is useful for doing asynchronous things that do not require (heap) locks,
  37 // or synchronization with other parts of collector. These could run even when ShenandoahConcurrentThread
  38 // is busy driving the GC cycle.
  39 class ShenandoahPeriodicTask : public PeriodicTask {
  40 private:
  41   ShenandoahControlThread* _thread;
  42 public:
  43   ShenandoahPeriodicTask(ShenandoahControlThread* thread) :
  44           PeriodicTask(100), _thread(thread) {}
  45   virtual void task();
  46 };
  47 
  48 // Periodic task to flush SATB buffers periodically.
  49 class ShenandoahPeriodicSATBFlushTask : public PeriodicTask {
  50 public:
  51   ShenandoahPeriodicSATBFlushTask() : PeriodicTask(ShenandoahSATBBufferFlushInterval) {}
  52   virtual void task();
  53 };
  54 
  55 class ShenandoahControlThread: public ConcurrentGCThread {
  56   friend class VMStructs;
  57 
  58 private:
  59   typedef enum {
  60     none,
  61     concurrent_normal,
  62     stw_degenerated,
  63     stw_full,
  64   } GCMode;
  65 
  66   // While we could have a single lock for these, it may risk unblocking
  67   // GC waiters when alloc failure GC cycle finishes. We want instead
  68   // to make complete explicit cycle for for demanding customers.
  69   Monitor _alloc_failure_waiters_lock;
  70   Monitor _gc_waiters_lock;
  71   ShenandoahPeriodicTask _periodic_task;
  72   ShenandoahPeriodicSATBFlushTask _periodic_satb_flush_task;
  73 
  74  private:
  75   static SurrogateLockerThread* _slt;
  76 
  77 public:
  78   void run();
  79   void stop();
  80 
  81 private:
  82   ShenandoahSharedFlag _gc_requested;
  83   ShenandoahSharedFlag _alloc_failure_gc;
  84   ShenandoahSharedFlag _graceful_shutdown;
  85   ShenandoahSharedFlag _heap_changed;
  86   ShenandoahSharedFlag _do_counters_update;
  87   ShenandoahSharedFlag _force_counters_update;
  88   GCCause::Cause       _requested_gc_cause;
  89   ShenandoahHeap::ShenandoahDegenPoint _degen_point;
  90 
  91   char _pad0[DEFAULT_CACHE_LINE_SIZE];
  92   volatile intptr_t _allocs_seen;
  93   char _pad1[DEFAULT_CACHE_LINE_SIZE];
  94 
  95   bool check_cancellation_or_degen(ShenandoahHeap::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, ShenandoahHeap::ShenandoahDegenPoint point);
  99   void service_uncommit(double shrink_before);
 100 
 101   bool try_set_alloc_failure_gc();
 102   void notify_alloc_failure_waiters();
 103   bool is_alloc_failure_gc();
 104 
 105   void notify_gc_waiters();
 106 
 107   // Handle GC request.
 108   // Blocks until GC is over.
 109   void handle_requested_gc(GCCause::Cause cause);
 110 
 111   bool is_explicit_gc(GCCause::Cause cause) const;
 112 public:
 113   // Constructor
 114   ShenandoahControlThread();
 115   ~ShenandoahControlThread();
 116 
 117   static void makeSurrogateLockerThread(TRAPS);
 118   static SurrogateLockerThread* slt() { return _slt; }
 119 
 120   // Handle allocation failure from normal allocation.
 121   // Blocks until memory is available.
 122   void handle_alloc_failure(size_t words);
 123 
 124   // Handle allocation failure from evacuation path.
 125   // Optionally blocks while collector is handling the failure.
 126   void handle_alloc_failure_evac(size_t words);
 127 
 128   void request_gc(GCCause::Cause cause);
 129 
 130   void handle_counters_update();
 131   void handle_force_counters_update();
 132   void set_forced_counters_update(bool value);
 133 
 134   void notify_heap_changed();
 135 
 136   void pacing_notify_alloc(size_t words);
 137 
 138   void start();
 139   void prepare_for_graceful_shutdown();
 140   bool in_graceful_shutdown();
 141 
 142   char* name() const { return (char*)"ShenandoahConcurrentThread";}
 143 
 144   // Printing
 145   void print_on(outputStream* st) const;
 146   void print() const;
 147 
 148 };
 149 
 150 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTTHREAD_HPP