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_SHENANDOAHSCHEDULERTHREAD_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHSCHEDULERTHREAD_HPP
  26 
  27 #include "gc/shared/gcCause.hpp"
  28 #include "gc/shared/concurrentGCThread.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.hpp"
  30 #include "gc/shenandoah/shenandoahPadding.hpp"
  31 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
  32 #include "runtime/task.hpp"
  33 #include "utilities/ostream.hpp"
  34 
  35 // Periodic task is useful for doing asynchronous things that do not require (heap) locks,
  36 // or synchronization with other parts of collector. These could run even when ShenandoahConcurrentThread
  37 // is busy driving the GC cycle.
  38 class ShenandoahPeriodicTask : public PeriodicTask {
  39 private:
  40   ShenandoahControlThread* _thread;
  41 public:
  42   ShenandoahPeriodicTask(ShenandoahControlThread* thread) :
  43           PeriodicTask(100), _thread(thread) {}
  44   virtual void task();
  45 };
  46 
  47 // Periodic task to flush SATB buffers periodically.
  48 class ShenandoahPeriodicSATBFlushTask : public PeriodicTask {
  49 public:
  50   ShenandoahPeriodicSATBFlushTask() : PeriodicTask(ShenandoahSATBBufferFlushInterval) {}
  51   virtual void task();
  52 };
  53 
  54 class ShenandoahControlThread: public ConcurrentGCThread {
  55   friend class VMStructs;
  56 
  57 private:
  58   typedef enum {
  59     none,
  60     concurrent_normal,
  61     stw_degenerated,
  62     stw_full
  63   } GCMode;
  64 
  65   // While we could have a single lock for these, it may risk unblocking
  66   // GC waiters when alloc failure GC cycle finishes. We want instead
  67   // to make complete explicit cycle for for demanding customers.
  68   Monitor _alloc_failure_waiters_lock;
  69   Monitor _gc_waiters_lock;
  70   ShenandoahPeriodicTask _periodic_task;
  71   ShenandoahPeriodicSATBFlushTask _periodic_satb_flush_task;
  72 
  73 public:
  74   void run_service();
  75   void stop_service();
  76 
  77 private:
  78   ShenandoahSharedFlag _gc_requested;
  79   ShenandoahSharedFlag _alloc_failure_gc;
  80   ShenandoahSharedFlag _graceful_shutdown;
  81   ShenandoahSharedFlag _heap_changed;
  82   ShenandoahSharedFlag _do_counters_update;
  83   ShenandoahSharedFlag _force_counters_update;
  84   GCCause::Cause       _requested_gc_cause;
  85   ShenandoahHeap::ShenandoahDegenPoint _degen_point;
  86 
  87   shenandoah_padding(0);
  88   volatile size_t _allocs_seen;
  89   shenandoah_padding(1);
  90   volatile size_t _gc_id;
  91   shenandoah_padding(2);
  92 
  93   bool check_cancellation_or_degen(ShenandoahHeap::ShenandoahDegenPoint point);
  94   void service_concurrent_normal_cycle(GCCause::Cause cause);
  95   void service_stw_full_cycle(GCCause::Cause cause);
  96   void service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahHeap::ShenandoahDegenPoint point);
  97   void service_uncommit(double shrink_before);
  98 
  99   bool try_set_alloc_failure_gc();
 100   void notify_alloc_failure_waiters();
 101   bool is_alloc_failure_gc();
 102 
 103   void reset_gc_id();
 104   void update_gc_id();
 105   size_t get_gc_id();
 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   // Handle allocation failure from normal allocation.
 120   // Blocks until memory is available.
 121   void handle_alloc_failure(ShenandoahAllocRequest& req);
 122 
 123   // Handle allocation failure from evacuation path.
 124   // Optionally blocks while collector is handling the failure.
 125   void handle_alloc_failure_evac(size_t words);
 126 
 127   void request_gc(GCCause::Cause cause);
 128 
 129   void handle_counters_update();
 130   void handle_force_counters_update();
 131   void set_forced_counters_update(bool value);
 132 
 133   void notify_heap_changed();
 134 
 135   void pacing_notify_alloc(size_t words);
 136 
 137   void start();
 138   void prepare_for_graceful_shutdown();
 139   bool in_graceful_shutdown();
 140 
 141   char* name() const { return (char*)"ShenandoahControlThread";}
 142 
 143   // Printing
 144   void print_on(outputStream* st) const;
 145   void print() const;
 146 };
 147 
 148 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHSCHEDULERTHREAD_HPP