aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP aoqi@0: #define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP aoqi@0: aoqi@0: #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.hpp" aoqi@0: #include "gc_implementation/shared/concurrentGCThread.hpp" aoqi@0: #include "runtime/thread.inline.hpp" aoqi@0: aoqi@0: class ConcurrentMarkSweepGeneration; aoqi@0: class CMSCollector; aoqi@0: aoqi@0: // The Concurrent Mark Sweep GC Thread aoqi@0: class ConcurrentMarkSweepThread: public ConcurrentGCThread { aoqi@0: friend class VMStructs; aoqi@0: friend class ConcurrentMarkSweepGeneration; // XXX should remove friendship aoqi@0: friend class CMSCollector; aoqi@0: public: aoqi@0: virtual void run(); aoqi@0: aoqi@0: private: aoqi@0: static ConcurrentMarkSweepThread* _cmst; aoqi@0: static CMSCollector* _collector; aoqi@0: static SurrogateLockerThread* _slt; aoqi@0: static SurrogateLockerThread::SLT_msg_type _sltBuffer; aoqi@0: static Monitor* _sltMonitor; aoqi@0: aoqi@0: static bool _should_terminate; aoqi@0: aoqi@0: enum CMS_flag_type { aoqi@0: CMS_nil = NoBits, aoqi@0: CMS_cms_wants_token = nth_bit(0), aoqi@0: CMS_cms_has_token = nth_bit(1), aoqi@0: CMS_vm_wants_token = nth_bit(2), aoqi@0: CMS_vm_has_token = nth_bit(3) aoqi@0: }; aoqi@0: aoqi@0: static int _CMS_flag; aoqi@0: aoqi@0: static bool CMS_flag_is_set(int b) { return (_CMS_flag & b) != 0; } aoqi@0: static bool set_CMS_flag(int b) { return (_CMS_flag |= b) != 0; } aoqi@0: static bool clear_CMS_flag(int b) { return (_CMS_flag &= ~b) != 0; } aoqi@0: void sleepBeforeNextCycle(); aoqi@0: aoqi@0: // CMS thread should yield for a young gen collection, direct allocation, aoqi@0: // and iCMS activity. aoqi@0: static char _pad_1[64 - sizeof(jint)]; // prevent cache-line sharing aoqi@0: static volatile jint _pending_yields; aoqi@0: static volatile jint _pending_decrements; // decrements to _pending_yields aoqi@0: static char _pad_2[64 - sizeof(jint)]; // prevent cache-line sharing aoqi@0: aoqi@0: // Tracing messages, enabled by CMSTraceThreadState. aoqi@0: static inline void trace_state(const char* desc); aoqi@0: aoqi@0: static volatile int _icms_disabled; // a counter to track #iCMS disable & enable aoqi@0: static volatile bool _should_run; // iCMS may run aoqi@0: static volatile bool _should_stop; // iCMS should stop aoqi@0: aoqi@0: // debugging aoqi@0: void verify_ok_to_terminate() const PRODUCT_RETURN; aoqi@0: aoqi@0: public: aoqi@0: // Constructor aoqi@0: ConcurrentMarkSweepThread(CMSCollector* collector); aoqi@0: aoqi@0: static void makeSurrogateLockerThread(TRAPS); aoqi@0: static SurrogateLockerThread* slt() { return _slt; } aoqi@0: aoqi@0: // Tester aoqi@0: bool is_ConcurrentGC_thread() const { return true; } aoqi@0: aoqi@0: static void threads_do(ThreadClosure* tc); aoqi@0: aoqi@0: // Printing aoqi@0: void print_on(outputStream* st) const; aoqi@0: void print() const { print_on(tty); } aoqi@0: static void print_all_on(outputStream* st); aoqi@0: static void print_all() { print_all_on(tty); } aoqi@0: aoqi@0: // Returns the CMS Thread aoqi@0: static ConcurrentMarkSweepThread* cmst() { return _cmst; } aoqi@0: static CMSCollector* collector() { return _collector; } aoqi@0: aoqi@0: // Create and start the CMS Thread, or stop it on shutdown aoqi@0: static ConcurrentMarkSweepThread* start(CMSCollector* collector); aoqi@0: static void stop(); aoqi@0: static bool should_terminate() { return _should_terminate; } aoqi@0: aoqi@0: // Synchronization using CMS token aoqi@0: static void synchronize(bool is_cms_thread); aoqi@0: static void desynchronize(bool is_cms_thread); aoqi@0: static bool vm_thread_has_cms_token() { aoqi@0: return CMS_flag_is_set(CMS_vm_has_token); aoqi@0: } aoqi@0: static bool cms_thread_has_cms_token() { aoqi@0: return CMS_flag_is_set(CMS_cms_has_token); aoqi@0: } aoqi@0: static bool vm_thread_wants_cms_token() { aoqi@0: return CMS_flag_is_set(CMS_vm_wants_token); aoqi@0: } aoqi@0: static bool cms_thread_wants_cms_token() { aoqi@0: return CMS_flag_is_set(CMS_cms_wants_token); aoqi@0: } aoqi@0: aoqi@0: // Wait on CMS lock until the next synchronous GC aoqi@0: // or given timeout, whichever is earlier. A timeout value aoqi@0: // of 0 indicates that there is no upper bound on the wait time. aoqi@0: // A concurrent full gc request terminates the wait. aoqi@0: void wait_on_cms_lock(long t_millis); aoqi@0: aoqi@0: // Wait on CMS lock until the next synchronous GC aoqi@0: // or given timeout, whichever is earlier. A timeout value aoqi@0: // of 0 indicates that there is no upper bound on the wait time. aoqi@0: // A concurrent full gc request terminates the wait. aoqi@0: void wait_on_cms_lock_for_scavenge(long t_millis); aoqi@0: aoqi@0: // The CMS thread will yield during the work portion of its cycle aoqi@0: // only when requested to. Both synchronous and asychronous requests aoqi@0: // are provided: aoqi@0: // (1) A synchronous request is used for young gen collections and aoqi@0: // for direct allocations. The requesting thread increments aoqi@0: // _pending_yields at the beginning of an operation, and decrements aoqi@0: // _pending_yields when that operation is completed. aoqi@0: // In turn, the CMS thread yields when _pending_yields is positive, aoqi@0: // and continues to yield until the value reverts to 0. aoqi@0: // (2) An asynchronous request, on the other hand, is used by iCMS aoqi@0: // for the stop_icms() operation. A single yield satisfies all of aoqi@0: // the outstanding asynch yield requests, of which there may aoqi@0: // occasionally be several in close succession. To accomplish aoqi@0: // this, an asynch-requesting thread atomically increments both aoqi@0: // _pending_yields and _pending_decrements. An asynchr requesting aoqi@0: // thread does not wait and "acknowledge" completion of an operation aoqi@0: // and deregister the request, like the synchronous version described aoqi@0: // above does. In turn, after yielding, the CMS thread decrements both aoqi@0: // _pending_yields and _pending_decrements by the value seen in aoqi@0: // _pending_decrements before the decrement. aoqi@0: // NOTE: The above scheme is isomorphic to having two request counters, aoqi@0: // one for async requests and one for sync requests, and for the CMS thread aoqi@0: // to check the sum of the two counters to decide whether it should yield aoqi@0: // and to clear only the async counter when it yields. However, it turns out aoqi@0: // to be more efficient for CMS code to just check a single counter aoqi@0: // _pending_yields that holds the sum (of both sync and async requests), and aoqi@0: // a second counter _pending_decrements that only holds the async requests, aoqi@0: // for greater efficiency, since in a typical CMS run, there are many more aoqi@0: // pontential (i.e. static) yield points than there are actual aoqi@0: // (i.e. dynamic) yields because of requests, which are few and far between. aoqi@0: // aoqi@0: // Note that, while "_pending_yields >= _pending_decrements" is an invariant, aoqi@0: // we cannot easily test that invariant, since the counters are manipulated via aoqi@0: // atomic instructions without explicit locking and we cannot read aoqi@0: // the two counters atomically together: one suggestion is to aoqi@0: // use (for example) 16-bit counters so as to be able to read the aoqi@0: // two counters atomically even on 32-bit platforms. Notice that aoqi@0: // the second assert in acknowledge_yield_request() below does indeed aoqi@0: // check a form of the above invariant, albeit indirectly. aoqi@0: aoqi@0: static void increment_pending_yields() { aoqi@0: Atomic::inc(&_pending_yields); aoqi@0: assert(_pending_yields >= 0, "can't be negative"); aoqi@0: } aoqi@0: static void decrement_pending_yields() { aoqi@0: Atomic::dec(&_pending_yields); aoqi@0: assert(_pending_yields >= 0, "can't be negative"); aoqi@0: } aoqi@0: static void asynchronous_yield_request() { aoqi@0: assert(CMSIncrementalMode, "Currently only used w/iCMS"); aoqi@0: increment_pending_yields(); aoqi@0: Atomic::inc(&_pending_decrements); aoqi@0: assert(_pending_decrements >= 0, "can't be negative"); aoqi@0: } aoqi@0: static void acknowledge_yield_request() { aoqi@0: jint decrement = _pending_decrements; aoqi@0: if (decrement > 0) { aoqi@0: assert(CMSIncrementalMode, "Currently only used w/iCMS"); aoqi@0: // Order important to preserve: _pending_yields >= _pending_decrements aoqi@0: Atomic::add(-decrement, &_pending_decrements); aoqi@0: Atomic::add(-decrement, &_pending_yields); aoqi@0: assert(_pending_decrements >= 0, "can't be negative"); aoqi@0: assert(_pending_yields >= 0, "can't be negative"); aoqi@0: } aoqi@0: } aoqi@0: static bool should_yield() { return _pending_yields > 0; } aoqi@0: aoqi@0: // CMS incremental mode. aoqi@0: static void start_icms(); // notify thread to start a quantum of work aoqi@0: static void stop_icms(); // request thread to stop working aoqi@0: void icms_wait(); // if asked to stop, wait until notified to start aoqi@0: aoqi@0: // Incremental mode is enabled globally by the flag CMSIncrementalMode. It aoqi@0: // must also be enabled/disabled dynamically to allow foreground collections. aoqi@0: #define ICMS_ENABLING_ASSERT \ aoqi@0: assert((CMSIncrementalMode && _icms_disabled >= 0) || \ aoqi@0: (!CMSIncrementalMode && _icms_disabled <= 0), "Error") aoqi@0: aoqi@0: static inline void enable_icms() { aoqi@0: ICMS_ENABLING_ASSERT; aoqi@0: Atomic::dec(&_icms_disabled); aoqi@0: } aoqi@0: static inline void disable_icms() { aoqi@0: ICMS_ENABLING_ASSERT; aoqi@0: Atomic::inc(&_icms_disabled); aoqi@0: } aoqi@0: static inline bool icms_is_disabled() { aoqi@0: ICMS_ENABLING_ASSERT; aoqi@0: return _icms_disabled > 0; aoqi@0: } aoqi@0: static inline bool icms_is_enabled() { aoqi@0: return !icms_is_disabled(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: inline void ConcurrentMarkSweepThread::trace_state(const char* desc) { aoqi@0: if (CMSTraceThreadState) { aoqi@0: char buf[128]; aoqi@0: TimeStamp& ts = gclog_or_tty->time_stamp(); aoqi@0: if (!ts.is_updated()) { aoqi@0: ts.update(); aoqi@0: } aoqi@0: jio_snprintf(buf, sizeof(buf), " [%.3f: CMSThread %s] ", aoqi@0: ts.seconds(), desc); aoqi@0: buf[sizeof(buf) - 1] = '\0'; aoqi@0: gclog_or_tty->print("%s", buf); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // For scoped increment/decrement of (synchronous) yield requests aoqi@0: class CMSSynchronousYieldRequest: public StackObj { aoqi@0: public: aoqi@0: CMSSynchronousYieldRequest() { aoqi@0: ConcurrentMarkSweepThread::increment_pending_yields(); aoqi@0: } aoqi@0: ~CMSSynchronousYieldRequest() { aoqi@0: ConcurrentMarkSweepThread::decrement_pending_yields(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // Used to emit a warning in case of unexpectedly excessive aoqi@0: // looping (in "apparently endless loops") in CMS code. aoqi@0: class CMSLoopCountWarn: public StackObj { aoqi@0: private: aoqi@0: const char* _src; aoqi@0: const char* _msg; aoqi@0: const intx _threshold; aoqi@0: intx _ticks; aoqi@0: aoqi@0: public: aoqi@0: inline CMSLoopCountWarn(const char* src, const char* msg, aoqi@0: const intx threshold) : aoqi@0: _src(src), _msg(msg), _threshold(threshold), _ticks(0) { } aoqi@0: aoqi@0: inline void tick() { aoqi@0: _ticks++; aoqi@0: if (CMSLoopWarn && _ticks % _threshold == 0) { aoqi@0: warning("%s has looped " INTX_FORMAT " times %s", _src, _ticks, _msg); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP