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