Wed, 27 Apr 2016 01:25:04 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP |
aoqi@0 | 26 | #define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.hpp" |
aoqi@0 | 29 | #include "gc_implementation/shared/concurrentGCThread.hpp" |
aoqi@0 | 30 | #include "runtime/thread.inline.hpp" |
aoqi@0 | 31 | |
aoqi@0 | 32 | class ConcurrentMarkSweepGeneration; |
aoqi@0 | 33 | class CMSCollector; |
aoqi@0 | 34 | |
aoqi@0 | 35 | // The Concurrent Mark Sweep GC Thread |
aoqi@0 | 36 | class ConcurrentMarkSweepThread: public ConcurrentGCThread { |
aoqi@0 | 37 | friend class VMStructs; |
aoqi@0 | 38 | friend class ConcurrentMarkSweepGeneration; // XXX should remove friendship |
aoqi@0 | 39 | friend class CMSCollector; |
aoqi@0 | 40 | public: |
aoqi@0 | 41 | virtual void run(); |
aoqi@0 | 42 | |
aoqi@0 | 43 | private: |
aoqi@0 | 44 | static ConcurrentMarkSweepThread* _cmst; |
aoqi@0 | 45 | static CMSCollector* _collector; |
aoqi@0 | 46 | static SurrogateLockerThread* _slt; |
aoqi@0 | 47 | static SurrogateLockerThread::SLT_msg_type _sltBuffer; |
aoqi@0 | 48 | static Monitor* _sltMonitor; |
aoqi@0 | 49 | |
aoqi@0 | 50 | static bool _should_terminate; |
aoqi@0 | 51 | |
aoqi@0 | 52 | enum CMS_flag_type { |
aoqi@0 | 53 | CMS_nil = NoBits, |
aoqi@0 | 54 | CMS_cms_wants_token = nth_bit(0), |
aoqi@0 | 55 | CMS_cms_has_token = nth_bit(1), |
aoqi@0 | 56 | CMS_vm_wants_token = nth_bit(2), |
aoqi@0 | 57 | CMS_vm_has_token = nth_bit(3) |
aoqi@0 | 58 | }; |
aoqi@0 | 59 | |
aoqi@0 | 60 | static int _CMS_flag; |
aoqi@0 | 61 | |
aoqi@0 | 62 | static bool CMS_flag_is_set(int b) { return (_CMS_flag & b) != 0; } |
aoqi@0 | 63 | static bool set_CMS_flag(int b) { return (_CMS_flag |= b) != 0; } |
aoqi@0 | 64 | static bool clear_CMS_flag(int b) { return (_CMS_flag &= ~b) != 0; } |
aoqi@0 | 65 | void sleepBeforeNextCycle(); |
aoqi@0 | 66 | |
aoqi@0 | 67 | // CMS thread should yield for a young gen collection, direct allocation, |
aoqi@0 | 68 | // and iCMS activity. |
aoqi@0 | 69 | static char _pad_1[64 - sizeof(jint)]; // prevent cache-line sharing |
aoqi@0 | 70 | static volatile jint _pending_yields; |
aoqi@0 | 71 | static volatile jint _pending_decrements; // decrements to _pending_yields |
aoqi@0 | 72 | static char _pad_2[64 - sizeof(jint)]; // prevent cache-line sharing |
aoqi@0 | 73 | |
aoqi@0 | 74 | // Tracing messages, enabled by CMSTraceThreadState. |
aoqi@0 | 75 | static inline void trace_state(const char* desc); |
aoqi@0 | 76 | |
aoqi@0 | 77 | static volatile int _icms_disabled; // a counter to track #iCMS disable & enable |
aoqi@0 | 78 | static volatile bool _should_run; // iCMS may run |
aoqi@0 | 79 | static volatile bool _should_stop; // iCMS should stop |
aoqi@0 | 80 | |
aoqi@0 | 81 | // debugging |
aoqi@0 | 82 | void verify_ok_to_terminate() const PRODUCT_RETURN; |
aoqi@0 | 83 | |
aoqi@0 | 84 | public: |
aoqi@0 | 85 | // Constructor |
aoqi@0 | 86 | ConcurrentMarkSweepThread(CMSCollector* collector); |
aoqi@0 | 87 | |
aoqi@0 | 88 | static void makeSurrogateLockerThread(TRAPS); |
aoqi@0 | 89 | static SurrogateLockerThread* slt() { return _slt; } |
aoqi@0 | 90 | |
aoqi@0 | 91 | // Tester |
aoqi@0 | 92 | bool is_ConcurrentGC_thread() const { return true; } |
aoqi@0 | 93 | |
aoqi@0 | 94 | static void threads_do(ThreadClosure* tc); |
aoqi@0 | 95 | |
aoqi@0 | 96 | // Printing |
aoqi@0 | 97 | void print_on(outputStream* st) const; |
aoqi@0 | 98 | void print() const { print_on(tty); } |
aoqi@0 | 99 | static void print_all_on(outputStream* st); |
aoqi@0 | 100 | static void print_all() { print_all_on(tty); } |
aoqi@0 | 101 | |
aoqi@0 | 102 | // Returns the CMS Thread |
aoqi@0 | 103 | static ConcurrentMarkSweepThread* cmst() { return _cmst; } |
aoqi@0 | 104 | static CMSCollector* collector() { return _collector; } |
aoqi@0 | 105 | |
aoqi@0 | 106 | // Create and start the CMS Thread, or stop it on shutdown |
aoqi@0 | 107 | static ConcurrentMarkSweepThread* start(CMSCollector* collector); |
aoqi@0 | 108 | static void stop(); |
aoqi@0 | 109 | static bool should_terminate() { return _should_terminate; } |
aoqi@0 | 110 | |
aoqi@0 | 111 | // Synchronization using CMS token |
aoqi@0 | 112 | static void synchronize(bool is_cms_thread); |
aoqi@0 | 113 | static void desynchronize(bool is_cms_thread); |
aoqi@0 | 114 | static bool vm_thread_has_cms_token() { |
aoqi@0 | 115 | return CMS_flag_is_set(CMS_vm_has_token); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | static bool cms_thread_has_cms_token() { |
aoqi@0 | 118 | return CMS_flag_is_set(CMS_cms_has_token); |
aoqi@0 | 119 | } |
aoqi@0 | 120 | static bool vm_thread_wants_cms_token() { |
aoqi@0 | 121 | return CMS_flag_is_set(CMS_vm_wants_token); |
aoqi@0 | 122 | } |
aoqi@0 | 123 | static bool cms_thread_wants_cms_token() { |
aoqi@0 | 124 | return CMS_flag_is_set(CMS_cms_wants_token); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | |
aoqi@0 | 127 | // Wait on CMS lock until the next synchronous GC |
aoqi@0 | 128 | // or given timeout, whichever is earlier. A timeout value |
aoqi@0 | 129 | // of 0 indicates that there is no upper bound on the wait time. |
aoqi@0 | 130 | // A concurrent full gc request terminates the wait. |
aoqi@0 | 131 | void wait_on_cms_lock(long t_millis); |
aoqi@0 | 132 | |
aoqi@0 | 133 | // Wait on CMS lock until the next synchronous GC |
aoqi@0 | 134 | // or given timeout, whichever is earlier. A timeout value |
aoqi@0 | 135 | // of 0 indicates that there is no upper bound on the wait time. |
aoqi@0 | 136 | // A concurrent full gc request terminates the wait. |
aoqi@0 | 137 | void wait_on_cms_lock_for_scavenge(long t_millis); |
aoqi@0 | 138 | |
aoqi@0 | 139 | // The CMS thread will yield during the work portion of its cycle |
aoqi@0 | 140 | // only when requested to. Both synchronous and asychronous requests |
aoqi@0 | 141 | // are provided: |
aoqi@0 | 142 | // (1) A synchronous request is used for young gen collections and |
aoqi@0 | 143 | // for direct allocations. The requesting thread increments |
aoqi@0 | 144 | // _pending_yields at the beginning of an operation, and decrements |
aoqi@0 | 145 | // _pending_yields when that operation is completed. |
aoqi@0 | 146 | // In turn, the CMS thread yields when _pending_yields is positive, |
aoqi@0 | 147 | // and continues to yield until the value reverts to 0. |
aoqi@0 | 148 | // (2) An asynchronous request, on the other hand, is used by iCMS |
aoqi@0 | 149 | // for the stop_icms() operation. A single yield satisfies all of |
aoqi@0 | 150 | // the outstanding asynch yield requests, of which there may |
aoqi@0 | 151 | // occasionally be several in close succession. To accomplish |
aoqi@0 | 152 | // this, an asynch-requesting thread atomically increments both |
aoqi@0 | 153 | // _pending_yields and _pending_decrements. An asynchr requesting |
aoqi@0 | 154 | // thread does not wait and "acknowledge" completion of an operation |
aoqi@0 | 155 | // and deregister the request, like the synchronous version described |
aoqi@0 | 156 | // above does. In turn, after yielding, the CMS thread decrements both |
aoqi@0 | 157 | // _pending_yields and _pending_decrements by the value seen in |
aoqi@0 | 158 | // _pending_decrements before the decrement. |
aoqi@0 | 159 | // NOTE: The above scheme is isomorphic to having two request counters, |
aoqi@0 | 160 | // one for async requests and one for sync requests, and for the CMS thread |
aoqi@0 | 161 | // to check the sum of the two counters to decide whether it should yield |
aoqi@0 | 162 | // and to clear only the async counter when it yields. However, it turns out |
aoqi@0 | 163 | // to be more efficient for CMS code to just check a single counter |
aoqi@0 | 164 | // _pending_yields that holds the sum (of both sync and async requests), and |
aoqi@0 | 165 | // a second counter _pending_decrements that only holds the async requests, |
aoqi@0 | 166 | // for greater efficiency, since in a typical CMS run, there are many more |
aoqi@0 | 167 | // pontential (i.e. static) yield points than there are actual |
aoqi@0 | 168 | // (i.e. dynamic) yields because of requests, which are few and far between. |
aoqi@0 | 169 | // |
aoqi@0 | 170 | // Note that, while "_pending_yields >= _pending_decrements" is an invariant, |
aoqi@0 | 171 | // we cannot easily test that invariant, since the counters are manipulated via |
aoqi@0 | 172 | // atomic instructions without explicit locking and we cannot read |
aoqi@0 | 173 | // the two counters atomically together: one suggestion is to |
aoqi@0 | 174 | // use (for example) 16-bit counters so as to be able to read the |
aoqi@0 | 175 | // two counters atomically even on 32-bit platforms. Notice that |
aoqi@0 | 176 | // the second assert in acknowledge_yield_request() below does indeed |
aoqi@0 | 177 | // check a form of the above invariant, albeit indirectly. |
aoqi@0 | 178 | |
aoqi@0 | 179 | static void increment_pending_yields() { |
aoqi@0 | 180 | Atomic::inc(&_pending_yields); |
aoqi@0 | 181 | assert(_pending_yields >= 0, "can't be negative"); |
aoqi@0 | 182 | } |
aoqi@0 | 183 | static void decrement_pending_yields() { |
aoqi@0 | 184 | Atomic::dec(&_pending_yields); |
aoqi@0 | 185 | assert(_pending_yields >= 0, "can't be negative"); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | static void asynchronous_yield_request() { |
aoqi@0 | 188 | assert(CMSIncrementalMode, "Currently only used w/iCMS"); |
aoqi@0 | 189 | increment_pending_yields(); |
aoqi@0 | 190 | Atomic::inc(&_pending_decrements); |
aoqi@0 | 191 | assert(_pending_decrements >= 0, "can't be negative"); |
aoqi@0 | 192 | } |
aoqi@0 | 193 | static void acknowledge_yield_request() { |
aoqi@0 | 194 | jint decrement = _pending_decrements; |
aoqi@0 | 195 | if (decrement > 0) { |
aoqi@0 | 196 | assert(CMSIncrementalMode, "Currently only used w/iCMS"); |
aoqi@0 | 197 | // Order important to preserve: _pending_yields >= _pending_decrements |
aoqi@0 | 198 | Atomic::add(-decrement, &_pending_decrements); |
aoqi@0 | 199 | Atomic::add(-decrement, &_pending_yields); |
aoqi@0 | 200 | assert(_pending_decrements >= 0, "can't be negative"); |
aoqi@0 | 201 | assert(_pending_yields >= 0, "can't be negative"); |
aoqi@0 | 202 | } |
aoqi@0 | 203 | } |
aoqi@0 | 204 | static bool should_yield() { return _pending_yields > 0; } |
aoqi@0 | 205 | |
aoqi@0 | 206 | // CMS incremental mode. |
aoqi@0 | 207 | static void start_icms(); // notify thread to start a quantum of work |
aoqi@0 | 208 | static void stop_icms(); // request thread to stop working |
aoqi@0 | 209 | void icms_wait(); // if asked to stop, wait until notified to start |
aoqi@0 | 210 | |
aoqi@0 | 211 | // Incremental mode is enabled globally by the flag CMSIncrementalMode. It |
aoqi@0 | 212 | // must also be enabled/disabled dynamically to allow foreground collections. |
aoqi@0 | 213 | #define ICMS_ENABLING_ASSERT \ |
aoqi@0 | 214 | assert((CMSIncrementalMode && _icms_disabled >= 0) || \ |
aoqi@0 | 215 | (!CMSIncrementalMode && _icms_disabled <= 0), "Error") |
aoqi@0 | 216 | |
aoqi@0 | 217 | static inline void enable_icms() { |
aoqi@0 | 218 | ICMS_ENABLING_ASSERT; |
aoqi@0 | 219 | Atomic::dec(&_icms_disabled); |
aoqi@0 | 220 | } |
aoqi@0 | 221 | static inline void disable_icms() { |
aoqi@0 | 222 | ICMS_ENABLING_ASSERT; |
aoqi@0 | 223 | Atomic::inc(&_icms_disabled); |
aoqi@0 | 224 | } |
aoqi@0 | 225 | static inline bool icms_is_disabled() { |
aoqi@0 | 226 | ICMS_ENABLING_ASSERT; |
aoqi@0 | 227 | return _icms_disabled > 0; |
aoqi@0 | 228 | } |
aoqi@0 | 229 | static inline bool icms_is_enabled() { |
aoqi@0 | 230 | return !icms_is_disabled(); |
aoqi@0 | 231 | } |
aoqi@0 | 232 | }; |
aoqi@0 | 233 | |
aoqi@0 | 234 | inline void ConcurrentMarkSweepThread::trace_state(const char* desc) { |
aoqi@0 | 235 | if (CMSTraceThreadState) { |
aoqi@0 | 236 | char buf[128]; |
aoqi@0 | 237 | TimeStamp& ts = gclog_or_tty->time_stamp(); |
aoqi@0 | 238 | if (!ts.is_updated()) { |
aoqi@0 | 239 | ts.update(); |
aoqi@0 | 240 | } |
aoqi@0 | 241 | jio_snprintf(buf, sizeof(buf), " [%.3f: CMSThread %s] ", |
aoqi@0 | 242 | ts.seconds(), desc); |
aoqi@0 | 243 | buf[sizeof(buf) - 1] = '\0'; |
aoqi@0 | 244 | gclog_or_tty->print("%s", buf); |
aoqi@0 | 245 | } |
aoqi@0 | 246 | } |
aoqi@0 | 247 | |
aoqi@0 | 248 | // For scoped increment/decrement of (synchronous) yield requests |
aoqi@0 | 249 | class CMSSynchronousYieldRequest: public StackObj { |
aoqi@0 | 250 | public: |
aoqi@0 | 251 | CMSSynchronousYieldRequest() { |
aoqi@0 | 252 | ConcurrentMarkSweepThread::increment_pending_yields(); |
aoqi@0 | 253 | } |
aoqi@0 | 254 | ~CMSSynchronousYieldRequest() { |
aoqi@0 | 255 | ConcurrentMarkSweepThread::decrement_pending_yields(); |
aoqi@0 | 256 | } |
aoqi@0 | 257 | }; |
aoqi@0 | 258 | |
aoqi@0 | 259 | // Used to emit a warning in case of unexpectedly excessive |
aoqi@0 | 260 | // looping (in "apparently endless loops") in CMS code. |
aoqi@0 | 261 | class CMSLoopCountWarn: public StackObj { |
aoqi@0 | 262 | private: |
aoqi@0 | 263 | const char* _src; |
aoqi@0 | 264 | const char* _msg; |
aoqi@0 | 265 | const intx _threshold; |
aoqi@0 | 266 | intx _ticks; |
aoqi@0 | 267 | |
aoqi@0 | 268 | public: |
aoqi@0 | 269 | inline CMSLoopCountWarn(const char* src, const char* msg, |
aoqi@0 | 270 | const intx threshold) : |
aoqi@0 | 271 | _src(src), _msg(msg), _threshold(threshold), _ticks(0) { } |
aoqi@0 | 272 | |
aoqi@0 | 273 | inline void tick() { |
aoqi@0 | 274 | _ticks++; |
aoqi@0 | 275 | if (CMSLoopWarn && _ticks % _threshold == 0) { |
aoqi@0 | 276 | warning("%s has looped " INTX_FORMAT " times %s", _src, _ticks, _msg); |
aoqi@0 | 277 | } |
aoqi@0 | 278 | } |
aoqi@0 | 279 | }; |
aoqi@0 | 280 | |
aoqi@0 | 281 | #endif // SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP |