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