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_traversal,
  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   ShenandoahPeriodicSATBFlushTask _periodic_satb_flush_task;
  74 
  75  private:
  76   static SurrogateLockerThread* _slt;
  77 
  78 public:
  79   void run();
  80   void stop();
  81 
  82 private:
  83   ShenandoahSharedFlag _gc_requested;
  84   ShenandoahSharedFlag _alloc_failure_gc;
  85   ShenandoahSharedFlag _graceful_shutdown;
  86   ShenandoahSharedFlag _heap_changed;
  87   ShenandoahSharedFlag _do_counters_update;
  88   ShenandoahSharedFlag _force_counters_update;
  89   GCCause::Cause       _requested_gc_cause;
  90   ShenandoahHeap::ShenandoahDegenPoint _degen_point;
  91 
  92   char _pad0[DEFAULT_CACHE_LINE_SIZE];
  93   volatile intptr_t _allocs_seen;
  94   char _pad1[DEFAULT_CACHE_LINE_SIZE];
  95 
  96   bool check_cancellation_or_degen(ShenandoahHeap::ShenandoahDegenPoint point);
  97   void service_concurrent_normal_cycle(GCCause::Cause cause);
  98   void service_stw_full_cycle(GCCause::Cause cause);
  99   void service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahHeap::ShenandoahDegenPoint point);
 100   void service_concurrent_traversal_cycle(GCCause::Cause cause);
 101   void service_uncommit(double shrink_before);
 102 
 103   bool try_set_alloc_failure_gc();
 104   void notify_alloc_failure_waiters();
 105   bool is_alloc_failure_gc();
 106 
 107   void notify_gc_waiters();
 108 
 109   // Handle GC request.
 110   // Blocks until GC is over.
 111   void handle_requested_gc(GCCause::Cause cause);
 112 
 113   bool is_explicit_gc(GCCause::Cause cause) const;
 114 public:
 115   // Constructor
 116   ShenandoahControlThread();
 117   ~ShenandoahControlThread();
 118 
 119   static void makeSurrogateLockerThread(TRAPS);
 120   static SurrogateLockerThread* slt() { return _slt; }
 121 
 122   // Handle allocation failure from normal allocation.
 123   // Blocks until memory is available.
 124   void handle_alloc_failure(size_t words);
 125 
 126   // Handle allocation failure from evacuation path.
 127   // Optionally blocks while collector is handling the failure.
 128   void handle_alloc_failure_evac(size_t words);
 129 
 130   void request_gc(GCCause::Cause cause);
 131 
 132   void handle_counters_update();
 133   void handle_force_counters_update();
 134   void set_forced_counters_update(bool value);
 135 
 136   void notify_heap_changed();
 137 
 138   void pacing_notify_alloc(size_t words);
 139 
 140   void start();
 141   void prepare_for_graceful_shutdown();
 142   bool in_graceful_shutdown();
 143 
 144   char* name() const { return (char*)"ShenandoahConcurrentThread";}
 145 
 146   // Printing
 147   void print_on(outputStream* st) const;
 148   void print() const;
 149 
 150 };
 151 
 152 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTTHREAD_HPP