Sat, 01 Dec 2007 00:00:00 +0000
Initial load
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | class ConcurrentMarkSweepGeneration; |
duke@435 | 26 | class CMSCollector; |
duke@435 | 27 | |
duke@435 | 28 | // The Concurrent Mark Sweep GC Thread (could be several in the future). |
duke@435 | 29 | class ConcurrentMarkSweepThread: public ConcurrentGCThread { |
duke@435 | 30 | friend class VMStructs; |
duke@435 | 31 | friend class ConcurrentMarkSweepGeneration; // XXX should remove friendship |
duke@435 | 32 | friend class CMSCollector; |
duke@435 | 33 | public: |
duke@435 | 34 | virtual void run(); |
duke@435 | 35 | |
duke@435 | 36 | private: |
duke@435 | 37 | static ConcurrentMarkSweepThread* _cmst; |
duke@435 | 38 | static CMSCollector* _collector; |
duke@435 | 39 | static SurrogateLockerThread* _slt; |
duke@435 | 40 | static SurrogateLockerThread::SLT_msg_type _sltBuffer; |
duke@435 | 41 | static Monitor* _sltMonitor; |
duke@435 | 42 | |
duke@435 | 43 | ConcurrentMarkSweepThread* _next; |
duke@435 | 44 | |
duke@435 | 45 | static bool _should_terminate; |
duke@435 | 46 | |
duke@435 | 47 | enum CMS_flag_type { |
duke@435 | 48 | CMS_nil = NoBits, |
duke@435 | 49 | CMS_cms_wants_token = nth_bit(0), |
duke@435 | 50 | CMS_cms_has_token = nth_bit(1), |
duke@435 | 51 | CMS_vm_wants_token = nth_bit(2), |
duke@435 | 52 | CMS_vm_has_token = nth_bit(3) |
duke@435 | 53 | }; |
duke@435 | 54 | |
duke@435 | 55 | static int _CMS_flag; |
duke@435 | 56 | |
duke@435 | 57 | static bool CMS_flag_is_set(int b) { return (_CMS_flag & b) != 0; } |
duke@435 | 58 | static bool set_CMS_flag(int b) { return (_CMS_flag |= b) != 0; } |
duke@435 | 59 | static bool clear_CMS_flag(int b) { return (_CMS_flag &= ~b) != 0; } |
duke@435 | 60 | void sleepBeforeNextCycle(); |
duke@435 | 61 | |
duke@435 | 62 | // CMS thread should yield for a young gen collection, direct allocation, |
duke@435 | 63 | // and iCMS activity. |
duke@435 | 64 | static char _pad_1[64 - sizeof(jint)]; // prevent cache-line sharing |
duke@435 | 65 | static volatile jint _pending_yields; |
duke@435 | 66 | static volatile jint _pending_decrements; // decrements to _pending_yields |
duke@435 | 67 | static char _pad_2[64 - sizeof(jint)]; // prevent cache-line sharing |
duke@435 | 68 | |
duke@435 | 69 | // Tracing messages, enabled by CMSTraceThreadState. |
duke@435 | 70 | static inline void trace_state(const char* desc); |
duke@435 | 71 | |
duke@435 | 72 | static volatile bool _icms_enabled; // iCMS enabled? |
duke@435 | 73 | static volatile bool _should_run; // iCMS may run |
duke@435 | 74 | static volatile bool _should_stop; // iCMS should stop |
duke@435 | 75 | |
duke@435 | 76 | // debugging |
duke@435 | 77 | void verify_ok_to_terminate() const PRODUCT_RETURN; |
duke@435 | 78 | |
duke@435 | 79 | public: |
duke@435 | 80 | // Constructor |
duke@435 | 81 | ConcurrentMarkSweepThread(CMSCollector* collector); |
duke@435 | 82 | |
duke@435 | 83 | static void makeSurrogateLockerThread(TRAPS); |
duke@435 | 84 | static SurrogateLockerThread* slt() { return _slt; } |
duke@435 | 85 | |
duke@435 | 86 | // Tester |
duke@435 | 87 | bool is_ConcurrentGC_thread() const { return true; } |
duke@435 | 88 | |
duke@435 | 89 | static void threads_do(ThreadClosure* tc); |
duke@435 | 90 | |
duke@435 | 91 | // Printing |
duke@435 | 92 | void print_on(outputStream* st) const; |
duke@435 | 93 | void print() const { print_on(tty); } |
duke@435 | 94 | static void print_all_on(outputStream* st); |
duke@435 | 95 | static void print_all() { print_all_on(tty); } |
duke@435 | 96 | |
duke@435 | 97 | // Returns the CMS Thread |
duke@435 | 98 | static ConcurrentMarkSweepThread* cmst() { return _cmst; } |
duke@435 | 99 | static CMSCollector* collector() { return _collector; } |
duke@435 | 100 | |
duke@435 | 101 | // Create and start the CMS Thread, or stop it on shutdown |
duke@435 | 102 | static ConcurrentMarkSweepThread* start(CMSCollector* collector); |
duke@435 | 103 | static void stop(); |
duke@435 | 104 | static bool should_terminate() { return _should_terminate; } |
duke@435 | 105 | |
duke@435 | 106 | // Synchronization using CMS token |
duke@435 | 107 | static void synchronize(bool is_cms_thread); |
duke@435 | 108 | static void desynchronize(bool is_cms_thread); |
duke@435 | 109 | static bool vm_thread_has_cms_token() { |
duke@435 | 110 | return CMS_flag_is_set(CMS_vm_has_token); |
duke@435 | 111 | } |
duke@435 | 112 | static bool cms_thread_has_cms_token() { |
duke@435 | 113 | return CMS_flag_is_set(CMS_cms_has_token); |
duke@435 | 114 | } |
duke@435 | 115 | static bool vm_thread_wants_cms_token() { |
duke@435 | 116 | return CMS_flag_is_set(CMS_vm_wants_token); |
duke@435 | 117 | } |
duke@435 | 118 | static bool cms_thread_wants_cms_token() { |
duke@435 | 119 | return CMS_flag_is_set(CMS_cms_wants_token); |
duke@435 | 120 | } |
duke@435 | 121 | |
duke@435 | 122 | // Wait on CMS lock until the next synchronous GC |
duke@435 | 123 | // or given timeout, whichever is earlier. |
duke@435 | 124 | void wait_on_cms_lock(long t); // milliseconds |
duke@435 | 125 | |
duke@435 | 126 | // The CMS thread will yield during the work portion of it's cycle |
duke@435 | 127 | // only when requested to. Both synchronous and asychronous requests |
duke@435 | 128 | // are provided. A synchronous request is used for young gen |
duke@435 | 129 | // collections and direct allocations. The requesting thread increments |
duke@435 | 130 | // pending_yields at the beginning of an operation, and decrements it when |
duke@435 | 131 | // the operation is completed. The CMS thread yields when pending_yields |
duke@435 | 132 | // is positive. An asynchronous request is used by iCMS in the stop_icms() |
duke@435 | 133 | // operation. A single yield satisfies the outstanding asynch yield requests. |
duke@435 | 134 | // The requesting thread increments both pending_yields and pending_decrements. |
duke@435 | 135 | // After yielding, the CMS thread decrements both by the amount in |
duke@435 | 136 | // pending_decrements. |
duke@435 | 137 | // Note that, while "_pending_yields >= _pending_decrements" is an invariant, |
duke@435 | 138 | // we cannot easily test that invariant, since the counters are manipulated via |
duke@435 | 139 | // atomic instructions without explicit locking and we cannot read |
duke@435 | 140 | // the two counters atomically together: one suggestion is to |
duke@435 | 141 | // use (for example) 16-bit counters so as to be able to read the |
duke@435 | 142 | // two counters atomically even on 32-bit platforms. Notice that |
duke@435 | 143 | // the second assert in acknowledge_yield_request() does indeed |
duke@435 | 144 | // check a form of the above invariant, albeit indirectly. |
duke@435 | 145 | |
duke@435 | 146 | static void increment_pending_yields() { |
duke@435 | 147 | Atomic::inc(&_pending_yields); |
duke@435 | 148 | assert(_pending_yields >= 0, "can't be negative"); |
duke@435 | 149 | } |
duke@435 | 150 | static void decrement_pending_yields() { |
duke@435 | 151 | Atomic::dec(&_pending_yields); |
duke@435 | 152 | assert(_pending_yields >= 0, "can't be negative"); |
duke@435 | 153 | } |
duke@435 | 154 | static void asynchronous_yield_request() { |
duke@435 | 155 | increment_pending_yields(); |
duke@435 | 156 | Atomic::inc(&_pending_decrements); |
duke@435 | 157 | assert(_pending_decrements >= 0, "can't be negative"); |
duke@435 | 158 | } |
duke@435 | 159 | static void acknowledge_yield_request() { |
duke@435 | 160 | jint decrement = _pending_decrements; |
duke@435 | 161 | if (decrement > 0) { |
duke@435 | 162 | // Order important to preserve: _pending_yields >= _pending_decrements |
duke@435 | 163 | Atomic::add(-decrement, &_pending_decrements); |
duke@435 | 164 | Atomic::add(-decrement, &_pending_yields); |
duke@435 | 165 | assert(_pending_decrements >= 0, "can't be negative"); |
duke@435 | 166 | assert(_pending_yields >= 0, "can't be negative"); |
duke@435 | 167 | } |
duke@435 | 168 | } |
duke@435 | 169 | static bool should_yield() { return _pending_yields > 0; } |
duke@435 | 170 | |
duke@435 | 171 | // CMS incremental mode. |
duke@435 | 172 | static void start_icms(); // notify thread to start a quantum of work |
duke@435 | 173 | static void stop_icms(); // request thread to stop working |
duke@435 | 174 | void icms_wait(); // if asked to stop, wait until notified to start |
duke@435 | 175 | |
duke@435 | 176 | // Incremental mode is enabled globally by the flag CMSIncrementalMode. It |
duke@435 | 177 | // must also be enabled/disabled dynamically to allow foreground collections. |
duke@435 | 178 | static inline void enable_icms() { _icms_enabled = true; } |
duke@435 | 179 | static inline void disable_icms() { _icms_enabled = false; } |
duke@435 | 180 | static inline void set_icms_enabled(bool val) { _icms_enabled = val; } |
duke@435 | 181 | static inline bool icms_enabled() { return _icms_enabled; } |
duke@435 | 182 | }; |
duke@435 | 183 | |
duke@435 | 184 | inline void ConcurrentMarkSweepThread::trace_state(const char* desc) { |
duke@435 | 185 | if (CMSTraceThreadState) { |
duke@435 | 186 | char buf[128]; |
duke@435 | 187 | TimeStamp& ts = gclog_or_tty->time_stamp(); |
duke@435 | 188 | if (!ts.is_updated()) { |
duke@435 | 189 | ts.update(); |
duke@435 | 190 | } |
duke@435 | 191 | jio_snprintf(buf, sizeof(buf), " [%.3f: CMSThread %s] ", |
duke@435 | 192 | ts.seconds(), desc); |
duke@435 | 193 | buf[sizeof(buf) - 1] = '\0'; |
duke@435 | 194 | gclog_or_tty->print(buf); |
duke@435 | 195 | } |
duke@435 | 196 | } |
duke@435 | 197 | |
duke@435 | 198 | // For scoped increment/decrement of yield requests |
duke@435 | 199 | class CMSSynchronousYieldRequest: public StackObj { |
duke@435 | 200 | public: |
duke@435 | 201 | CMSSynchronousYieldRequest() { |
duke@435 | 202 | ConcurrentMarkSweepThread::increment_pending_yields(); |
duke@435 | 203 | } |
duke@435 | 204 | ~CMSSynchronousYieldRequest() { |
duke@435 | 205 | ConcurrentMarkSweepThread::decrement_pending_yields(); |
duke@435 | 206 | } |
duke@435 | 207 | }; |
duke@435 | 208 | |
duke@435 | 209 | // Used to emit a warning in case of unexpectedly excessive |
duke@435 | 210 | // looping (in "apparently endless loops") in CMS code. |
duke@435 | 211 | class CMSLoopCountWarn: public StackObj { |
duke@435 | 212 | private: |
duke@435 | 213 | const char* _src; |
duke@435 | 214 | const char* _msg; |
duke@435 | 215 | const intx _threshold; |
duke@435 | 216 | intx _ticks; |
duke@435 | 217 | |
duke@435 | 218 | public: |
duke@435 | 219 | inline CMSLoopCountWarn(const char* src, const char* msg, |
duke@435 | 220 | const intx threshold) : |
duke@435 | 221 | _src(src), _msg(msg), _threshold(threshold), _ticks(0) { } |
duke@435 | 222 | |
duke@435 | 223 | inline void tick() { |
duke@435 | 224 | _ticks++; |
duke@435 | 225 | if (CMSLoopWarn && _ticks % _threshold == 0) { |
duke@435 | 226 | warning("%s has looped %d times %s", _src, _ticks, _msg); |
duke@435 | 227 | } |
duke@435 | 228 | } |
duke@435 | 229 | }; |