23 */ 24 25 #ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP 26 #define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP 27 28 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.hpp" 29 #include "gc_implementation/shared/concurrentGCThread.hpp" 30 #ifdef TARGET_OS_FAMILY_linux 31 # include "thread_linux.inline.hpp" 32 #endif 33 #ifdef TARGET_OS_FAMILY_solaris 34 # include "thread_solaris.inline.hpp" 35 #endif 36 #ifdef TARGET_OS_FAMILY_windows 37 # include "thread_windows.inline.hpp" 38 #endif 39 40 class ConcurrentMarkSweepGeneration; 41 class CMSCollector; 42 43 // The Concurrent Mark Sweep GC Thread (could be several in the future). 44 class ConcurrentMarkSweepThread: public ConcurrentGCThread { 45 friend class VMStructs; 46 friend class ConcurrentMarkSweepGeneration; // XXX should remove friendship 47 friend class CMSCollector; 48 public: 49 virtual void run(); 50 51 private: 52 static ConcurrentMarkSweepThread* _cmst; 53 static CMSCollector* _collector; 54 static SurrogateLockerThread* _slt; 55 static SurrogateLockerThread::SLT_msg_type _sltBuffer; 56 static Monitor* _sltMonitor; 57 58 ConcurrentMarkSweepThread* _next; 59 60 static bool _should_terminate; 61 62 enum CMS_flag_type { 63 CMS_nil = NoBits, 64 CMS_cms_wants_token = nth_bit(0), 65 CMS_cms_has_token = nth_bit(1), 66 CMS_vm_wants_token = nth_bit(2), 67 CMS_vm_has_token = nth_bit(3) 68 }; 69 70 static int _CMS_flag; 71 72 static bool CMS_flag_is_set(int b) { return (_CMS_flag & b) != 0; } 73 static bool set_CMS_flag(int b) { return (_CMS_flag |= b) != 0; } 74 static bool clear_CMS_flag(int b) { return (_CMS_flag &= ~b) != 0; } 75 void sleepBeforeNextCycle(); 76 77 // CMS thread should yield for a young gen collection, direct allocation, 78 // and iCMS activity. 79 static char _pad_1[64 - sizeof(jint)]; // prevent cache-line sharing 80 static volatile jint _pending_yields; 81 static volatile jint _pending_decrements; // decrements to _pending_yields 82 static char _pad_2[64 - sizeof(jint)]; // prevent cache-line sharing 83 84 // Tracing messages, enabled by CMSTraceThreadState. 85 static inline void trace_state(const char* desc); 86 87 static volatile bool _icms_enabled; // iCMS enabled? 88 static volatile bool _should_run; // iCMS may run 89 static volatile bool _should_stop; // iCMS should stop 90 91 // debugging 92 void verify_ok_to_terminate() const PRODUCT_RETURN; 93 94 public: 95 // Constructor 96 ConcurrentMarkSweepThread(CMSCollector* collector); 97 98 static void makeSurrogateLockerThread(TRAPS); 99 static SurrogateLockerThread* slt() { return _slt; } 100 101 // Tester 102 bool is_ConcurrentGC_thread() const { return true; } 103 104 static void threads_do(ThreadClosure* tc); 105 106 // Printing 107 void print_on(outputStream* st) const; 197 static void acknowledge_yield_request() { 198 jint decrement = _pending_decrements; 199 if (decrement > 0) { 200 assert(CMSIncrementalMode, "Currently only used w/iCMS"); 201 // Order important to preserve: _pending_yields >= _pending_decrements 202 Atomic::add(-decrement, &_pending_decrements); 203 Atomic::add(-decrement, &_pending_yields); 204 assert(_pending_decrements >= 0, "can't be negative"); 205 assert(_pending_yields >= 0, "can't be negative"); 206 } 207 } 208 static bool should_yield() { return _pending_yields > 0; } 209 210 // CMS incremental mode. 211 static void start_icms(); // notify thread to start a quantum of work 212 static void stop_icms(); // request thread to stop working 213 void icms_wait(); // if asked to stop, wait until notified to start 214 215 // Incremental mode is enabled globally by the flag CMSIncrementalMode. It 216 // must also be enabled/disabled dynamically to allow foreground collections. 217 static inline void enable_icms() { _icms_enabled = true; } 218 static inline void disable_icms() { _icms_enabled = false; } 219 static inline void set_icms_enabled(bool val) { _icms_enabled = val; } 220 static inline bool icms_enabled() { return _icms_enabled; } 221 }; 222 223 inline void ConcurrentMarkSweepThread::trace_state(const char* desc) { 224 if (CMSTraceThreadState) { 225 char buf[128]; 226 TimeStamp& ts = gclog_or_tty->time_stamp(); 227 if (!ts.is_updated()) { 228 ts.update(); 229 } 230 jio_snprintf(buf, sizeof(buf), " [%.3f: CMSThread %s] ", 231 ts.seconds(), desc); 232 buf[sizeof(buf) - 1] = '\0'; 233 gclog_or_tty->print(buf); 234 } 235 } 236 237 // For scoped increment/decrement of (synchronous) yield requests 238 class CMSSynchronousYieldRequest: public StackObj { 239 public: 240 CMSSynchronousYieldRequest() { | 23 */ 24 25 #ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP 26 #define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP 27 28 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.hpp" 29 #include "gc_implementation/shared/concurrentGCThread.hpp" 30 #ifdef TARGET_OS_FAMILY_linux 31 # include "thread_linux.inline.hpp" 32 #endif 33 #ifdef TARGET_OS_FAMILY_solaris 34 # include "thread_solaris.inline.hpp" 35 #endif 36 #ifdef TARGET_OS_FAMILY_windows 37 # include "thread_windows.inline.hpp" 38 #endif 39 40 class ConcurrentMarkSweepGeneration; 41 class CMSCollector; 42 43 // The Concurrent Mark Sweep GC Thread 44 class ConcurrentMarkSweepThread: public ConcurrentGCThread { 45 friend class VMStructs; 46 friend class ConcurrentMarkSweepGeneration; // XXX should remove friendship 47 friend class CMSCollector; 48 public: 49 virtual void run(); 50 51 private: 52 static ConcurrentMarkSweepThread* _cmst; 53 static CMSCollector* _collector; 54 static SurrogateLockerThread* _slt; 55 static SurrogateLockerThread::SLT_msg_type _sltBuffer; 56 static Monitor* _sltMonitor; 57 58 static bool _should_terminate; 59 60 enum CMS_flag_type { 61 CMS_nil = NoBits, 62 CMS_cms_wants_token = nth_bit(0), 63 CMS_cms_has_token = nth_bit(1), 64 CMS_vm_wants_token = nth_bit(2), 65 CMS_vm_has_token = nth_bit(3) 66 }; 67 68 static int _CMS_flag; 69 70 static bool CMS_flag_is_set(int b) { return (_CMS_flag & b) != 0; } 71 static bool set_CMS_flag(int b) { return (_CMS_flag |= b) != 0; } 72 static bool clear_CMS_flag(int b) { return (_CMS_flag &= ~b) != 0; } 73 void sleepBeforeNextCycle(); 74 75 // CMS thread should yield for a young gen collection, direct allocation, 76 // and iCMS activity. 77 static char _pad_1[64 - sizeof(jint)]; // prevent cache-line sharing 78 static volatile jint _pending_yields; 79 static volatile jint _pending_decrements; // decrements to _pending_yields 80 static char _pad_2[64 - sizeof(jint)]; // prevent cache-line sharing 81 82 // Tracing messages, enabled by CMSTraceThreadState. 83 static inline void trace_state(const char* desc); 84 85 static volatile int _icms_disabled; // a counter to track #iCMS disable & enable 86 static volatile bool _should_run; // iCMS may run 87 static volatile bool _should_stop; // iCMS should stop 88 89 // debugging 90 void verify_ok_to_terminate() const PRODUCT_RETURN; 91 92 public: 93 // Constructor 94 ConcurrentMarkSweepThread(CMSCollector* collector); 95 96 static void makeSurrogateLockerThread(TRAPS); 97 static SurrogateLockerThread* slt() { return _slt; } 98 99 // Tester 100 bool is_ConcurrentGC_thread() const { return true; } 101 102 static void threads_do(ThreadClosure* tc); 103 104 // Printing 105 void print_on(outputStream* st) const; 195 static void acknowledge_yield_request() { 196 jint decrement = _pending_decrements; 197 if (decrement > 0) { 198 assert(CMSIncrementalMode, "Currently only used w/iCMS"); 199 // Order important to preserve: _pending_yields >= _pending_decrements 200 Atomic::add(-decrement, &_pending_decrements); 201 Atomic::add(-decrement, &_pending_yields); 202 assert(_pending_decrements >= 0, "can't be negative"); 203 assert(_pending_yields >= 0, "can't be negative"); 204 } 205 } 206 static bool should_yield() { return _pending_yields > 0; } 207 208 // CMS incremental mode. 209 static void start_icms(); // notify thread to start a quantum of work 210 static void stop_icms(); // request thread to stop working 211 void icms_wait(); // if asked to stop, wait until notified to start 212 213 // Incremental mode is enabled globally by the flag CMSIncrementalMode. It 214 // must also be enabled/disabled dynamically to allow foreground collections. 215 #define ICMS_ENABLING_ASSERT \ 216 assert((CMSIncrementalMode && _icms_disabled >= 0) || \ 217 (!CMSIncrementalMode && _icms_disabled <= 0), "Error") 218 219 static inline void enable_icms() { 220 ICMS_ENABLING_ASSERT; 221 Atomic::dec(&_icms_disabled); 222 } 223 static inline void disable_icms() { 224 ICMS_ENABLING_ASSERT; 225 Atomic::inc(&_icms_disabled); 226 } 227 static inline bool icms_is_disabled() { 228 ICMS_ENABLING_ASSERT; 229 return _icms_disabled > 0; 230 } 231 static inline bool icms_is_enabled() { 232 return !icms_is_disabled(); 233 } 234 }; 235 236 inline void ConcurrentMarkSweepThread::trace_state(const char* desc) { 237 if (CMSTraceThreadState) { 238 char buf[128]; 239 TimeStamp& ts = gclog_or_tty->time_stamp(); 240 if (!ts.is_updated()) { 241 ts.update(); 242 } 243 jio_snprintf(buf, sizeof(buf), " [%.3f: CMSThread %s] ", 244 ts.seconds(), desc); 245 buf[sizeof(buf) - 1] = '\0'; 246 gclog_or_tty->print(buf); 247 } 248 } 249 250 // For scoped increment/decrement of (synchronous) yield requests 251 class CMSSynchronousYieldRequest: public StackObj { 252 public: 253 CMSSynchronousYieldRequest() { |