Sun, 25 Sep 2011 16:03:29 -0700
7089790: integrate bsd-port changes
Reviewed-by: kvn, twisti, jrose
Contributed-by: Kurt Miller <kurt@intricatesoftware.com>, Greg Lewis <glewis@eyesbeyond.com>, Jung-uk Kim <jkim@freebsd.org>, Christos Zoulas <christos@zoulas.com>, Landon Fuller <landonf@plausible.coop>, The FreeBSD Foundation <board@freebsdfoundation.org>, Michael Franz <mvfranz@gmail.com>, Roger Hoover <rhoover@apple.com>, Alexander Strange <astrange@apple.com>
duke@435 | 1 | /* |
ysr@2647 | 2 | * Copyright (c) 2001, 2011, Oracle and/or its affiliates. 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 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP |
stefank@2314 | 26 | #define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP |
stefank@2314 | 27 | |
stefank@2314 | 28 | #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.hpp" |
stefank@2314 | 29 | #include "gc_implementation/shared/concurrentGCThread.hpp" |
stefank@2314 | 30 | #ifdef TARGET_OS_FAMILY_linux |
stefank@2314 | 31 | # include "thread_linux.inline.hpp" |
stefank@2314 | 32 | #endif |
stefank@2314 | 33 | #ifdef TARGET_OS_FAMILY_solaris |
stefank@2314 | 34 | # include "thread_solaris.inline.hpp" |
stefank@2314 | 35 | #endif |
stefank@2314 | 36 | #ifdef TARGET_OS_FAMILY_windows |
stefank@2314 | 37 | # include "thread_windows.inline.hpp" |
stefank@2314 | 38 | #endif |
never@3156 | 39 | #ifdef TARGET_OS_FAMILY_bsd |
never@3156 | 40 | # include "thread_bsd.inline.hpp" |
never@3156 | 41 | #endif |
stefank@2314 | 42 | |
duke@435 | 43 | class ConcurrentMarkSweepGeneration; |
duke@435 | 44 | class CMSCollector; |
duke@435 | 45 | |
ysr@2647 | 46 | // The Concurrent Mark Sweep GC Thread |
duke@435 | 47 | class ConcurrentMarkSweepThread: public ConcurrentGCThread { |
duke@435 | 48 | friend class VMStructs; |
duke@435 | 49 | friend class ConcurrentMarkSweepGeneration; // XXX should remove friendship |
duke@435 | 50 | friend class CMSCollector; |
duke@435 | 51 | public: |
duke@435 | 52 | virtual void run(); |
duke@435 | 53 | |
duke@435 | 54 | private: |
duke@435 | 55 | static ConcurrentMarkSweepThread* _cmst; |
duke@435 | 56 | static CMSCollector* _collector; |
duke@435 | 57 | static SurrogateLockerThread* _slt; |
duke@435 | 58 | static SurrogateLockerThread::SLT_msg_type _sltBuffer; |
duke@435 | 59 | static Monitor* _sltMonitor; |
duke@435 | 60 | |
duke@435 | 61 | static bool _should_terminate; |
duke@435 | 62 | |
duke@435 | 63 | enum CMS_flag_type { |
duke@435 | 64 | CMS_nil = NoBits, |
duke@435 | 65 | CMS_cms_wants_token = nth_bit(0), |
duke@435 | 66 | CMS_cms_has_token = nth_bit(1), |
duke@435 | 67 | CMS_vm_wants_token = nth_bit(2), |
duke@435 | 68 | CMS_vm_has_token = nth_bit(3) |
duke@435 | 69 | }; |
duke@435 | 70 | |
duke@435 | 71 | static int _CMS_flag; |
duke@435 | 72 | |
duke@435 | 73 | static bool CMS_flag_is_set(int b) { return (_CMS_flag & b) != 0; } |
duke@435 | 74 | static bool set_CMS_flag(int b) { return (_CMS_flag |= b) != 0; } |
duke@435 | 75 | static bool clear_CMS_flag(int b) { return (_CMS_flag &= ~b) != 0; } |
duke@435 | 76 | void sleepBeforeNextCycle(); |
duke@435 | 77 | |
duke@435 | 78 | // CMS thread should yield for a young gen collection, direct allocation, |
duke@435 | 79 | // and iCMS activity. |
duke@435 | 80 | static char _pad_1[64 - sizeof(jint)]; // prevent cache-line sharing |
duke@435 | 81 | static volatile jint _pending_yields; |
duke@435 | 82 | static volatile jint _pending_decrements; // decrements to _pending_yields |
duke@435 | 83 | static char _pad_2[64 - sizeof(jint)]; // prevent cache-line sharing |
duke@435 | 84 | |
duke@435 | 85 | // Tracing messages, enabled by CMSTraceThreadState. |
duke@435 | 86 | static inline void trace_state(const char* desc); |
duke@435 | 87 | |
ysr@2647 | 88 | static volatile int _icms_disabled; // a counter to track #iCMS disable & enable |
duke@435 | 89 | static volatile bool _should_run; // iCMS may run |
duke@435 | 90 | static volatile bool _should_stop; // iCMS should stop |
duke@435 | 91 | |
duke@435 | 92 | // debugging |
duke@435 | 93 | void verify_ok_to_terminate() const PRODUCT_RETURN; |
duke@435 | 94 | |
duke@435 | 95 | public: |
duke@435 | 96 | // Constructor |
duke@435 | 97 | ConcurrentMarkSweepThread(CMSCollector* collector); |
duke@435 | 98 | |
duke@435 | 99 | static void makeSurrogateLockerThread(TRAPS); |
duke@435 | 100 | static SurrogateLockerThread* slt() { return _slt; } |
duke@435 | 101 | |
duke@435 | 102 | // Tester |
duke@435 | 103 | bool is_ConcurrentGC_thread() const { return true; } |
duke@435 | 104 | |
duke@435 | 105 | static void threads_do(ThreadClosure* tc); |
duke@435 | 106 | |
duke@435 | 107 | // Printing |
duke@435 | 108 | void print_on(outputStream* st) const; |
duke@435 | 109 | void print() const { print_on(tty); } |
duke@435 | 110 | static void print_all_on(outputStream* st); |
duke@435 | 111 | static void print_all() { print_all_on(tty); } |
duke@435 | 112 | |
duke@435 | 113 | // Returns the CMS Thread |
duke@435 | 114 | static ConcurrentMarkSweepThread* cmst() { return _cmst; } |
duke@435 | 115 | static CMSCollector* collector() { return _collector; } |
duke@435 | 116 | |
duke@435 | 117 | // Create and start the CMS Thread, or stop it on shutdown |
duke@435 | 118 | static ConcurrentMarkSweepThread* start(CMSCollector* collector); |
duke@435 | 119 | static void stop(); |
duke@435 | 120 | static bool should_terminate() { return _should_terminate; } |
duke@435 | 121 | |
duke@435 | 122 | // Synchronization using CMS token |
duke@435 | 123 | static void synchronize(bool is_cms_thread); |
duke@435 | 124 | static void desynchronize(bool is_cms_thread); |
duke@435 | 125 | static bool vm_thread_has_cms_token() { |
duke@435 | 126 | return CMS_flag_is_set(CMS_vm_has_token); |
duke@435 | 127 | } |
duke@435 | 128 | static bool cms_thread_has_cms_token() { |
duke@435 | 129 | return CMS_flag_is_set(CMS_cms_has_token); |
duke@435 | 130 | } |
duke@435 | 131 | static bool vm_thread_wants_cms_token() { |
duke@435 | 132 | return CMS_flag_is_set(CMS_vm_wants_token); |
duke@435 | 133 | } |
duke@435 | 134 | static bool cms_thread_wants_cms_token() { |
duke@435 | 135 | return CMS_flag_is_set(CMS_cms_wants_token); |
duke@435 | 136 | } |
duke@435 | 137 | |
duke@435 | 138 | // Wait on CMS lock until the next synchronous GC |
ysr@2242 | 139 | // or given timeout, whichever is earlier. A timeout value |
ysr@2242 | 140 | // of 0 indicates that there is no upper bound on the wait time. |
ysr@2242 | 141 | // A concurrent full gc request terminates the wait. |
ysr@2242 | 142 | void wait_on_cms_lock(long t_millis); |
duke@435 | 143 | |
ysr@2192 | 144 | // The CMS thread will yield during the work portion of its cycle |
duke@435 | 145 | // only when requested to. Both synchronous and asychronous requests |
ysr@2192 | 146 | // are provided: |
ysr@2192 | 147 | // (1) A synchronous request is used for young gen collections and |
ysr@2192 | 148 | // for direct allocations. The requesting thread increments |
ysr@2192 | 149 | // _pending_yields at the beginning of an operation, and decrements |
ysr@2192 | 150 | // _pending_yields when that operation is completed. |
ysr@2192 | 151 | // In turn, the CMS thread yields when _pending_yields is positive, |
ysr@2192 | 152 | // and continues to yield until the value reverts to 0. |
ysr@2192 | 153 | // (2) An asynchronous request, on the other hand, is used by iCMS |
ysr@2192 | 154 | // for the stop_icms() operation. A single yield satisfies all of |
ysr@2192 | 155 | // the outstanding asynch yield requests, of which there may |
ysr@2192 | 156 | // occasionally be several in close succession. To accomplish |
ysr@2192 | 157 | // this, an asynch-requesting thread atomically increments both |
ysr@2192 | 158 | // _pending_yields and _pending_decrements. An asynchr requesting |
ysr@2192 | 159 | // thread does not wait and "acknowledge" completion of an operation |
ysr@2192 | 160 | // and deregister the request, like the synchronous version described |
ysr@2192 | 161 | // above does. In turn, after yielding, the CMS thread decrements both |
ysr@2192 | 162 | // _pending_yields and _pending_decrements by the value seen in |
ysr@2192 | 163 | // _pending_decrements before the decrement. |
ysr@2192 | 164 | // NOTE: The above scheme is isomorphic to having two request counters, |
ysr@2192 | 165 | // one for async requests and one for sync requests, and for the CMS thread |
ysr@2192 | 166 | // to check the sum of the two counters to decide whether it should yield |
ysr@2192 | 167 | // and to clear only the async counter when it yields. However, it turns out |
ysr@2192 | 168 | // to be more efficient for CMS code to just check a single counter |
ysr@2192 | 169 | // _pending_yields that holds the sum (of both sync and async requests), and |
ysr@2192 | 170 | // a second counter _pending_decrements that only holds the async requests, |
ysr@2192 | 171 | // for greater efficiency, since in a typical CMS run, there are many more |
ysr@2192 | 172 | // pontential (i.e. static) yield points than there are actual |
ysr@2192 | 173 | // (i.e. dynamic) yields because of requests, which are few and far between. |
ysr@2192 | 174 | // |
duke@435 | 175 | // Note that, while "_pending_yields >= _pending_decrements" is an invariant, |
duke@435 | 176 | // we cannot easily test that invariant, since the counters are manipulated via |
duke@435 | 177 | // atomic instructions without explicit locking and we cannot read |
duke@435 | 178 | // the two counters atomically together: one suggestion is to |
duke@435 | 179 | // use (for example) 16-bit counters so as to be able to read the |
duke@435 | 180 | // two counters atomically even on 32-bit platforms. Notice that |
ysr@2192 | 181 | // the second assert in acknowledge_yield_request() below does indeed |
duke@435 | 182 | // check a form of the above invariant, albeit indirectly. |
duke@435 | 183 | |
duke@435 | 184 | static void increment_pending_yields() { |
duke@435 | 185 | Atomic::inc(&_pending_yields); |
duke@435 | 186 | assert(_pending_yields >= 0, "can't be negative"); |
duke@435 | 187 | } |
duke@435 | 188 | static void decrement_pending_yields() { |
duke@435 | 189 | Atomic::dec(&_pending_yields); |
duke@435 | 190 | assert(_pending_yields >= 0, "can't be negative"); |
duke@435 | 191 | } |
duke@435 | 192 | static void asynchronous_yield_request() { |
ysr@2192 | 193 | assert(CMSIncrementalMode, "Currently only used w/iCMS"); |
duke@435 | 194 | increment_pending_yields(); |
duke@435 | 195 | Atomic::inc(&_pending_decrements); |
duke@435 | 196 | assert(_pending_decrements >= 0, "can't be negative"); |
duke@435 | 197 | } |
duke@435 | 198 | static void acknowledge_yield_request() { |
duke@435 | 199 | jint decrement = _pending_decrements; |
duke@435 | 200 | if (decrement > 0) { |
ysr@2192 | 201 | assert(CMSIncrementalMode, "Currently only used w/iCMS"); |
duke@435 | 202 | // Order important to preserve: _pending_yields >= _pending_decrements |
duke@435 | 203 | Atomic::add(-decrement, &_pending_decrements); |
duke@435 | 204 | Atomic::add(-decrement, &_pending_yields); |
duke@435 | 205 | assert(_pending_decrements >= 0, "can't be negative"); |
duke@435 | 206 | assert(_pending_yields >= 0, "can't be negative"); |
duke@435 | 207 | } |
duke@435 | 208 | } |
duke@435 | 209 | static bool should_yield() { return _pending_yields > 0; } |
duke@435 | 210 | |
duke@435 | 211 | // CMS incremental mode. |
duke@435 | 212 | static void start_icms(); // notify thread to start a quantum of work |
duke@435 | 213 | static void stop_icms(); // request thread to stop working |
duke@435 | 214 | void icms_wait(); // if asked to stop, wait until notified to start |
duke@435 | 215 | |
duke@435 | 216 | // Incremental mode is enabled globally by the flag CMSIncrementalMode. It |
duke@435 | 217 | // must also be enabled/disabled dynamically to allow foreground collections. |
ysr@2647 | 218 | #define ICMS_ENABLING_ASSERT \ |
ysr@2647 | 219 | assert((CMSIncrementalMode && _icms_disabled >= 0) || \ |
ysr@2647 | 220 | (!CMSIncrementalMode && _icms_disabled <= 0), "Error") |
ysr@2647 | 221 | |
ysr@2647 | 222 | static inline void enable_icms() { |
ysr@2647 | 223 | ICMS_ENABLING_ASSERT; |
ysr@2647 | 224 | Atomic::dec(&_icms_disabled); |
ysr@2647 | 225 | } |
ysr@2647 | 226 | static inline void disable_icms() { |
ysr@2647 | 227 | ICMS_ENABLING_ASSERT; |
ysr@2647 | 228 | Atomic::inc(&_icms_disabled); |
ysr@2647 | 229 | } |
ysr@2647 | 230 | static inline bool icms_is_disabled() { |
ysr@2647 | 231 | ICMS_ENABLING_ASSERT; |
ysr@2647 | 232 | return _icms_disabled > 0; |
ysr@2647 | 233 | } |
ysr@2647 | 234 | static inline bool icms_is_enabled() { |
ysr@2647 | 235 | return !icms_is_disabled(); |
ysr@2647 | 236 | } |
duke@435 | 237 | }; |
duke@435 | 238 | |
duke@435 | 239 | inline void ConcurrentMarkSweepThread::trace_state(const char* desc) { |
duke@435 | 240 | if (CMSTraceThreadState) { |
duke@435 | 241 | char buf[128]; |
duke@435 | 242 | TimeStamp& ts = gclog_or_tty->time_stamp(); |
duke@435 | 243 | if (!ts.is_updated()) { |
duke@435 | 244 | ts.update(); |
duke@435 | 245 | } |
duke@435 | 246 | jio_snprintf(buf, sizeof(buf), " [%.3f: CMSThread %s] ", |
duke@435 | 247 | ts.seconds(), desc); |
duke@435 | 248 | buf[sizeof(buf) - 1] = '\0'; |
duke@435 | 249 | gclog_or_tty->print(buf); |
duke@435 | 250 | } |
duke@435 | 251 | } |
duke@435 | 252 | |
ysr@2192 | 253 | // For scoped increment/decrement of (synchronous) yield requests |
duke@435 | 254 | class CMSSynchronousYieldRequest: public StackObj { |
duke@435 | 255 | public: |
duke@435 | 256 | CMSSynchronousYieldRequest() { |
duke@435 | 257 | ConcurrentMarkSweepThread::increment_pending_yields(); |
duke@435 | 258 | } |
duke@435 | 259 | ~CMSSynchronousYieldRequest() { |
duke@435 | 260 | ConcurrentMarkSweepThread::decrement_pending_yields(); |
duke@435 | 261 | } |
duke@435 | 262 | }; |
duke@435 | 263 | |
duke@435 | 264 | // Used to emit a warning in case of unexpectedly excessive |
duke@435 | 265 | // looping (in "apparently endless loops") in CMS code. |
duke@435 | 266 | class CMSLoopCountWarn: public StackObj { |
duke@435 | 267 | private: |
duke@435 | 268 | const char* _src; |
duke@435 | 269 | const char* _msg; |
duke@435 | 270 | const intx _threshold; |
duke@435 | 271 | intx _ticks; |
duke@435 | 272 | |
duke@435 | 273 | public: |
duke@435 | 274 | inline CMSLoopCountWarn(const char* src, const char* msg, |
duke@435 | 275 | const intx threshold) : |
duke@435 | 276 | _src(src), _msg(msg), _threshold(threshold), _ticks(0) { } |
duke@435 | 277 | |
duke@435 | 278 | inline void tick() { |
duke@435 | 279 | _ticks++; |
duke@435 | 280 | if (CMSLoopWarn && _ticks % _threshold == 0) { |
duke@435 | 281 | warning("%s has looped %d times %s", _src, _ticks, _msg); |
duke@435 | 282 | } |
duke@435 | 283 | } |
duke@435 | 284 | }; |
stefank@2314 | 285 | |
stefank@2314 | 286 | #endif // SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP |