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