1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,229 @@ 1.4 +/* 1.5 + * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +class ConcurrentMarkSweepGeneration; 1.29 +class CMSCollector; 1.30 + 1.31 +// The Concurrent Mark Sweep GC Thread (could be several in the future). 1.32 +class ConcurrentMarkSweepThread: public ConcurrentGCThread { 1.33 + friend class VMStructs; 1.34 + friend class ConcurrentMarkSweepGeneration; // XXX should remove friendship 1.35 + friend class CMSCollector; 1.36 + public: 1.37 + virtual void run(); 1.38 + 1.39 + private: 1.40 + static ConcurrentMarkSweepThread* _cmst; 1.41 + static CMSCollector* _collector; 1.42 + static SurrogateLockerThread* _slt; 1.43 + static SurrogateLockerThread::SLT_msg_type _sltBuffer; 1.44 + static Monitor* _sltMonitor; 1.45 + 1.46 + ConcurrentMarkSweepThread* _next; 1.47 + 1.48 + static bool _should_terminate; 1.49 + 1.50 + enum CMS_flag_type { 1.51 + CMS_nil = NoBits, 1.52 + CMS_cms_wants_token = nth_bit(0), 1.53 + CMS_cms_has_token = nth_bit(1), 1.54 + CMS_vm_wants_token = nth_bit(2), 1.55 + CMS_vm_has_token = nth_bit(3) 1.56 + }; 1.57 + 1.58 + static int _CMS_flag; 1.59 + 1.60 + static bool CMS_flag_is_set(int b) { return (_CMS_flag & b) != 0; } 1.61 + static bool set_CMS_flag(int b) { return (_CMS_flag |= b) != 0; } 1.62 + static bool clear_CMS_flag(int b) { return (_CMS_flag &= ~b) != 0; } 1.63 + void sleepBeforeNextCycle(); 1.64 + 1.65 + // CMS thread should yield for a young gen collection, direct allocation, 1.66 + // and iCMS activity. 1.67 + static char _pad_1[64 - sizeof(jint)]; // prevent cache-line sharing 1.68 + static volatile jint _pending_yields; 1.69 + static volatile jint _pending_decrements; // decrements to _pending_yields 1.70 + static char _pad_2[64 - sizeof(jint)]; // prevent cache-line sharing 1.71 + 1.72 + // Tracing messages, enabled by CMSTraceThreadState. 1.73 + static inline void trace_state(const char* desc); 1.74 + 1.75 + static volatile bool _icms_enabled; // iCMS enabled? 1.76 + static volatile bool _should_run; // iCMS may run 1.77 + static volatile bool _should_stop; // iCMS should stop 1.78 + 1.79 + // debugging 1.80 + void verify_ok_to_terminate() const PRODUCT_RETURN; 1.81 + 1.82 + public: 1.83 + // Constructor 1.84 + ConcurrentMarkSweepThread(CMSCollector* collector); 1.85 + 1.86 + static void makeSurrogateLockerThread(TRAPS); 1.87 + static SurrogateLockerThread* slt() { return _slt; } 1.88 + 1.89 + // Tester 1.90 + bool is_ConcurrentGC_thread() const { return true; } 1.91 + 1.92 + static void threads_do(ThreadClosure* tc); 1.93 + 1.94 + // Printing 1.95 + void print_on(outputStream* st) const; 1.96 + void print() const { print_on(tty); } 1.97 + static void print_all_on(outputStream* st); 1.98 + static void print_all() { print_all_on(tty); } 1.99 + 1.100 + // Returns the CMS Thread 1.101 + static ConcurrentMarkSweepThread* cmst() { return _cmst; } 1.102 + static CMSCollector* collector() { return _collector; } 1.103 + 1.104 + // Create and start the CMS Thread, or stop it on shutdown 1.105 + static ConcurrentMarkSweepThread* start(CMSCollector* collector); 1.106 + static void stop(); 1.107 + static bool should_terminate() { return _should_terminate; } 1.108 + 1.109 + // Synchronization using CMS token 1.110 + static void synchronize(bool is_cms_thread); 1.111 + static void desynchronize(bool is_cms_thread); 1.112 + static bool vm_thread_has_cms_token() { 1.113 + return CMS_flag_is_set(CMS_vm_has_token); 1.114 + } 1.115 + static bool cms_thread_has_cms_token() { 1.116 + return CMS_flag_is_set(CMS_cms_has_token); 1.117 + } 1.118 + static bool vm_thread_wants_cms_token() { 1.119 + return CMS_flag_is_set(CMS_vm_wants_token); 1.120 + } 1.121 + static bool cms_thread_wants_cms_token() { 1.122 + return CMS_flag_is_set(CMS_cms_wants_token); 1.123 + } 1.124 + 1.125 + // Wait on CMS lock until the next synchronous GC 1.126 + // or given timeout, whichever is earlier. 1.127 + void wait_on_cms_lock(long t); // milliseconds 1.128 + 1.129 + // The CMS thread will yield during the work portion of it's cycle 1.130 + // only when requested to. Both synchronous and asychronous requests 1.131 + // are provided. A synchronous request is used for young gen 1.132 + // collections and direct allocations. The requesting thread increments 1.133 + // pending_yields at the beginning of an operation, and decrements it when 1.134 + // the operation is completed. The CMS thread yields when pending_yields 1.135 + // is positive. An asynchronous request is used by iCMS in the stop_icms() 1.136 + // operation. A single yield satisfies the outstanding asynch yield requests. 1.137 + // The requesting thread increments both pending_yields and pending_decrements. 1.138 + // After yielding, the CMS thread decrements both by the amount in 1.139 + // pending_decrements. 1.140 + // Note that, while "_pending_yields >= _pending_decrements" is an invariant, 1.141 + // we cannot easily test that invariant, since the counters are manipulated via 1.142 + // atomic instructions without explicit locking and we cannot read 1.143 + // the two counters atomically together: one suggestion is to 1.144 + // use (for example) 16-bit counters so as to be able to read the 1.145 + // two counters atomically even on 32-bit platforms. Notice that 1.146 + // the second assert in acknowledge_yield_request() does indeed 1.147 + // check a form of the above invariant, albeit indirectly. 1.148 + 1.149 + static void increment_pending_yields() { 1.150 + Atomic::inc(&_pending_yields); 1.151 + assert(_pending_yields >= 0, "can't be negative"); 1.152 + } 1.153 + static void decrement_pending_yields() { 1.154 + Atomic::dec(&_pending_yields); 1.155 + assert(_pending_yields >= 0, "can't be negative"); 1.156 + } 1.157 + static void asynchronous_yield_request() { 1.158 + increment_pending_yields(); 1.159 + Atomic::inc(&_pending_decrements); 1.160 + assert(_pending_decrements >= 0, "can't be negative"); 1.161 + } 1.162 + static void acknowledge_yield_request() { 1.163 + jint decrement = _pending_decrements; 1.164 + if (decrement > 0) { 1.165 + // Order important to preserve: _pending_yields >= _pending_decrements 1.166 + Atomic::add(-decrement, &_pending_decrements); 1.167 + Atomic::add(-decrement, &_pending_yields); 1.168 + assert(_pending_decrements >= 0, "can't be negative"); 1.169 + assert(_pending_yields >= 0, "can't be negative"); 1.170 + } 1.171 + } 1.172 + static bool should_yield() { return _pending_yields > 0; } 1.173 + 1.174 + // CMS incremental mode. 1.175 + static void start_icms(); // notify thread to start a quantum of work 1.176 + static void stop_icms(); // request thread to stop working 1.177 + void icms_wait(); // if asked to stop, wait until notified to start 1.178 + 1.179 + // Incremental mode is enabled globally by the flag CMSIncrementalMode. It 1.180 + // must also be enabled/disabled dynamically to allow foreground collections. 1.181 + static inline void enable_icms() { _icms_enabled = true; } 1.182 + static inline void disable_icms() { _icms_enabled = false; } 1.183 + static inline void set_icms_enabled(bool val) { _icms_enabled = val; } 1.184 + static inline bool icms_enabled() { return _icms_enabled; } 1.185 +}; 1.186 + 1.187 +inline void ConcurrentMarkSweepThread::trace_state(const char* desc) { 1.188 + if (CMSTraceThreadState) { 1.189 + char buf[128]; 1.190 + TimeStamp& ts = gclog_or_tty->time_stamp(); 1.191 + if (!ts.is_updated()) { 1.192 + ts.update(); 1.193 + } 1.194 + jio_snprintf(buf, sizeof(buf), " [%.3f: CMSThread %s] ", 1.195 + ts.seconds(), desc); 1.196 + buf[sizeof(buf) - 1] = '\0'; 1.197 + gclog_or_tty->print(buf); 1.198 + } 1.199 +} 1.200 + 1.201 +// For scoped increment/decrement of yield requests 1.202 +class CMSSynchronousYieldRequest: public StackObj { 1.203 + public: 1.204 + CMSSynchronousYieldRequest() { 1.205 + ConcurrentMarkSweepThread::increment_pending_yields(); 1.206 + } 1.207 + ~CMSSynchronousYieldRequest() { 1.208 + ConcurrentMarkSweepThread::decrement_pending_yields(); 1.209 + } 1.210 +}; 1.211 + 1.212 +// Used to emit a warning in case of unexpectedly excessive 1.213 +// looping (in "apparently endless loops") in CMS code. 1.214 +class CMSLoopCountWarn: public StackObj { 1.215 + private: 1.216 + const char* _src; 1.217 + const char* _msg; 1.218 + const intx _threshold; 1.219 + intx _ticks; 1.220 + 1.221 + public: 1.222 + inline CMSLoopCountWarn(const char* src, const char* msg, 1.223 + const intx threshold) : 1.224 + _src(src), _msg(msg), _threshold(threshold), _ticks(0) { } 1.225 + 1.226 + inline void tick() { 1.227 + _ticks++; 1.228 + if (CMSLoopWarn && _ticks % _threshold == 0) { 1.229 + warning("%s has looped %d times %s", _src, _ticks, _msg); 1.230 + } 1.231 + } 1.232 +};