1 /*
   2  * Copyright (c) 2013, 2015, Red Hat, Inc. and/or its affiliates.
   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_implementation/shared/concurrentGCThread.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahHeap.hpp"
  29 #include "gc_implementation/shenandoah/shenandoahSharedVariables.hpp"
  30 #include "gc_interface/gcCause.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "runtime/task.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   // explicit 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 _explicit_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 _explicit_gc;
  83   ShenandoahSharedFlag _alloc_failure_gc;
  84   ShenandoahSharedFlag _graceful_shutdown;
  85   ShenandoahSharedFlag _heap_changed;
  86   ShenandoahSharedFlag _do_counters_update;
  87   ShenandoahSharedFlag _force_counters_update;
  88   volatile intptr_t _allocs_seen;
  89   GCCause::Cause _explicit_gc_cause;
  90   ShenandoahHeap::ShenandoahDegenPoint _degen_point;
  91 
  92   bool check_cancellation_or_degen(ShenandoahHeap::ShenandoahDegenPoint point);
  93   void service_concurrent_normal_cycle(GCCause::Cause cause);
  94   void service_stw_full_cycle(GCCause::Cause cause);
  95   void service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahHeap::ShenandoahDegenPoint point);
  96   void service_uncommit(double shrink_before);
  97 
  98   bool try_set_alloc_failure_gc();
  99   void notify_alloc_failure_waiters();
 100   bool is_alloc_failure_gc();
 101 
 102   void notify_explicit_gc_waiters();
 103 
 104 public:
 105   // Constructor
 106   ShenandoahControlThread();
 107   ~ShenandoahControlThread();
 108 
 109   static void makeSurrogateLockerThread(TRAPS);
 110   static SurrogateLockerThread* slt() { return _slt; }
 111 
 112   // Handle allocation failure from normal allocation.
 113   // Blocks until memory is available.
 114   void handle_alloc_failure(size_t words);
 115 
 116   // Handle allocation failure from evacuation path.
 117   // Optionally blocks while collector is handling the failure.
 118   void handle_alloc_failure_evac(size_t words);
 119 
 120   // Handle explicit GC request.
 121   // Blocks until GC is over.
 122   void handle_explicit_gc(GCCause::Cause cause);
 123 
 124   void handle_counters_update();
 125   void handle_force_counters_update();
 126   void set_forced_counters_update(bool value);
 127 
 128   void notify_heap_changed();
 129 
 130   void pacing_notify_alloc(size_t words);
 131 
 132   void start();
 133   void prepare_for_graceful_shutdown();
 134   bool in_graceful_shutdown();
 135 
 136   char* name() const { return (char*)"ShenandoahConcurrentThread";}
 137 
 138   // Printing
 139   void print_on(outputStream* st) const;
 140   void print() const;
 141 
 142 };
 143 
 144 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTTHREAD_HPP