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 class ShenandoahControlThread: public ConcurrentGCThread {
  49   friend class VMStructs;
  50 
  51 private:
  52   typedef enum {
  53     none,
  54     concurrent_normal,
  55     stw_degenerated,
  56     stw_full,
  57   } GCMode;
  58 
  59   // While we could have a single lock for these, it may risk unblocking
  60   // explicit GC waiters when alloc failure GC cycle finishes. We want instead
  61   // to make complete explicit cycle for for demanding customers.
  62   Monitor _alloc_failure_waiters_lock;
  63   Monitor _explicit_gc_waiters_lock;
  64   ShenandoahPeriodicTask _periodic_task;
  65 
  66  private:
  67   static SurrogateLockerThread* _slt;
  68 
  69 public:
  70   void run();
  71   void stop();
  72 
  73 private:
  74   ShenandoahSharedFlag _explicit_gc;
  75   ShenandoahSharedFlag _alloc_failure_gc;
  76   ShenandoahSharedFlag _graceful_shutdown;
  77   ShenandoahSharedFlag _heap_changed;
  78   ShenandoahSharedFlag _do_counters_update;
  79   ShenandoahSharedFlag _force_counters_update;
  80   volatile intptr_t _allocs_seen;
  81   GCCause::Cause _explicit_gc_cause;
  82   ShenandoahHeap::ShenandoahDegenPoint _degen_point;
  83 
  84   bool check_cancellation_or_degen(ShenandoahHeap::ShenandoahDegenPoint point);
  85   void service_concurrent_normal_cycle(GCCause::Cause cause);
  86   void service_stw_full_cycle(GCCause::Cause cause);
  87   void service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahHeap::ShenandoahDegenPoint point);
  88 
  89   bool try_set_alloc_failure_gc();
  90   void notify_alloc_failure_waiters();
  91   bool is_alloc_failure_gc();
  92 
  93   void notify_explicit_gc_waiters();
  94 
  95 public:
  96   // Constructor
  97   ShenandoahControlThread();
  98   ~ShenandoahControlThread();
  99 
 100   static void makeSurrogateLockerThread(TRAPS);
 101   static SurrogateLockerThread* slt() { return _slt; }
 102 
 103   // Handle allocation failure from normal allocation.
 104   // Blocks until memory is available.
 105   void handle_alloc_failure(size_t words);
 106 
 107   // Handle allocation failure from evacuation path.
 108   // Optionally blocks while collector is handling the failure.
 109   void handle_alloc_failure_evac(size_t words);
 110 
 111   // Handle explicit GC request.
 112   // Blocks until GC is over.
 113   void handle_explicit_gc(GCCause::Cause cause);
 114 
 115   void handle_counters_update();
 116   void handle_force_counters_update();
 117   void set_forced_counters_update(bool value);
 118 
 119   void notify_heap_changed();
 120 
 121   void pacing_notify_alloc(size_t words);
 122 
 123   void start();
 124   void prepare_for_graceful_shutdown();
 125   bool in_graceful_shutdown();
 126 
 127   char* name() const { return (char*)"ShenandoahConcurrentThread";}
 128 
 129   // Printing
 130   void print_on(outputStream* st) const;
 131   void print() const;
 132 
 133 };
 134 
 135 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTTHREAD_HPP