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/shenandoah/shenandoahHeap.hpp" 28 #include "gc/shenandoah/shenandoahSharedVariables.hpp" 29 #include "gc/shared/concurrentGCThread.hpp" 30 #include "gc/shared/gcCause.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "runtime/task.hpp" 33 34 // Periodic task is useful for doing asynchronous things that do not require (heap) locks, 35 // or synchronization with other parts of collector. These could run even when ShenandoahConcurrentThread 36 // is busy driving the GC cycle. 37 class ShenandoahPeriodicTask : public PeriodicTask { 38 private: 39 ShenandoahConcurrentThread* _thread; 40 public: 41 ShenandoahPeriodicTask(ShenandoahConcurrentThread* thread) : 42 PeriodicTask(100), _thread(thread) {} 43 virtual void task(); 44 }; 45 46 class ShenandoahConcurrentThread: public ConcurrentGCThread { 47 friend class VMStructs; 48 49 private: 50 typedef enum { 51 none, 52 concurrent_traversal, 53 concurrent_normal, 54 stw_degenerated, 55 stw_full, 56 } GCMode; 57 58 // While we could have a single lock for these, it may risk unblocking 59 // explicit GC waiters when alloc failure GC cycle finishes. We want instead 60 // to make complete explicit cycle for for demanding customers. 61 Monitor _alloc_failure_waiters_lock; 62 Monitor _explicit_gc_waiters_lock; 63 ShenandoahPeriodicTask _periodic_task; 64 65 public: 66 void run_service(); 67 void stop_service(); 68 69 private: 70 ShenandoahSharedFlag _explicit_gc; 71 ShenandoahSharedFlag _alloc_failure_gc; 72 ShenandoahSharedFlag _graceful_shutdown; 73 ShenandoahSharedFlag _heap_changed; 74 ShenandoahSharedFlag _do_counters_update; 75 ShenandoahSharedFlag _force_counters_update; 76 volatile size_t _allocs_seen; 77 GCCause::Cause _explicit_gc_cause; 78 ShenandoahHeap::ShenandoahDegenPoint _degen_point; 79 80 bool check_cancellation_or_degen(ShenandoahHeap::ShenandoahDegenPoint point); 81 void service_concurrent_normal_cycle(GCCause::Cause cause); 82 void service_stw_full_cycle(GCCause::Cause cause); 83 void service_stw_degenerated_cycle(GCCause::Cause cause, ShenandoahHeap::ShenandoahDegenPoint point); 84 void service_concurrent_traversal_cycle(GCCause::Cause cause); 85 86 bool try_set_alloc_failure_gc(); 87 void notify_alloc_failure_waiters(); 88 bool is_alloc_failure_gc(); 89 90 void notify_explicit_gc_waiters(); 91 92 public: 93 // Constructor 94 ShenandoahConcurrentThread(); 95 ~ShenandoahConcurrentThread(); 96 97 // Handle allocation failure from normal allocation. 98 // Blocks until memory is available. 99 void handle_alloc_failure(size_t words); 100 101 // Handle allocation failure from evacuation path. 102 // Optionally blocks while collector is handling the failure. 103 void handle_alloc_failure_evac(size_t words); 104 105 // Handle explicit GC request. 106 // Blocks until GC is over. 107 void handle_explicit_gc(GCCause::Cause cause); 108 109 void handle_counters_update(); 110 void handle_force_counters_update(); 111 void set_forced_counters_update(bool value); 112 113 void notify_heap_changed(); 114 115 void pacing_notify_alloc(size_t words); 116 117 void start(); 118 void prepare_for_graceful_shutdown(); 119 bool in_graceful_shutdown(); 120 121 char* name() const { return (char*)"ShenandoahConcurrentThread";} 122 123 // Printing 124 void print_on(outputStream* st) const; 125 void print() const; 126 }; 127 128 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHCONCURRENTTHREAD_HPP