47 static SurrogateLockerThread::SLT_msg_type _sltBuffer;
48 static Monitor* _sltMonitor;
49
50 static bool _should_terminate;
51
52 enum CMS_flag_type {
53 CMS_nil = NoBits,
54 CMS_cms_wants_token = nth_bit(0),
55 CMS_cms_has_token = nth_bit(1),
56 CMS_vm_wants_token = nth_bit(2),
57 CMS_vm_has_token = nth_bit(3)
58 };
59
60 static int _CMS_flag;
61
62 static bool CMS_flag_is_set(int b) { return (_CMS_flag & b) != 0; }
63 static bool set_CMS_flag(int b) { return (_CMS_flag |= b) != 0; }
64 static bool clear_CMS_flag(int b) { return (_CMS_flag &= ~b) != 0; }
65 void sleepBeforeNextCycle();
66
67 // CMS thread should yield for a young gen collection, direct allocation,
68 // and iCMS activity.
69 static char _pad_1[64 - sizeof(jint)]; // prevent cache-line sharing
70 static volatile jint _pending_yields;
71 static volatile jint _pending_decrements; // decrements to _pending_yields
72 static char _pad_2[64 - sizeof(jint)]; // prevent cache-line sharing
73
74 // Tracing messages, enabled by CMSTraceThreadState.
75 static inline void trace_state(const char* desc);
76
77 static volatile int _icms_disabled; // a counter to track #iCMS disable & enable
78 static volatile bool _should_run; // iCMS may run
79 static volatile bool _should_stop; // iCMS should stop
80
81 // debugging
82 void verify_ok_to_terminate() const PRODUCT_RETURN;
83
84 public:
85 // Constructor
86 ConcurrentMarkSweepThread(CMSCollector* collector);
87
88 static void makeSurrogateLockerThread(TRAPS);
89 static SurrogateLockerThread* slt() { return _slt; }
90
91 // Tester
92 bool is_ConcurrentGC_thread() const { return true; }
93
94 static void threads_do(ThreadClosure* tc);
95
96 // Printing
97 static void print_all_on(outputStream* st);
98 static void print_all() { print_all_on(tty); }
99
100 // Returns the CMS Thread
118 static bool vm_thread_wants_cms_token() {
119 return CMS_flag_is_set(CMS_vm_wants_token);
120 }
121 static bool cms_thread_wants_cms_token() {
122 return CMS_flag_is_set(CMS_cms_wants_token);
123 }
124
125 // Wait on CMS lock until the next synchronous GC
126 // or given timeout, whichever is earlier. A timeout value
127 // of 0 indicates that there is no upper bound on the wait time.
128 // A concurrent full gc request terminates the wait.
129 void wait_on_cms_lock(long t_millis);
130
131 // Wait on CMS lock until the next synchronous GC
132 // or given timeout, whichever is earlier. A timeout value
133 // of 0 indicates that there is no upper bound on the wait time.
134 // A concurrent full gc request terminates the wait.
135 void wait_on_cms_lock_for_scavenge(long t_millis);
136
137 // The CMS thread will yield during the work portion of its cycle
138 // only when requested to. Both synchronous and asychronous requests
139 // are provided:
140 // (1) A synchronous request is used for young gen collections and
141 // for direct allocations. The requesting thread increments
142 // _pending_yields at the beginning of an operation, and decrements
143 // _pending_yields when that operation is completed.
144 // In turn, the CMS thread yields when _pending_yields is positive,
145 // and continues to yield until the value reverts to 0.
146 // (2) An asynchronous request, on the other hand, is used by iCMS
147 // for the stop_icms() operation. A single yield satisfies all of
148 // the outstanding asynch yield requests, of which there may
149 // occasionally be several in close succession. To accomplish
150 // this, an asynch-requesting thread atomically increments both
151 // _pending_yields and _pending_decrements. An asynchr requesting
152 // thread does not wait and "acknowledge" completion of an operation
153 // and deregister the request, like the synchronous version described
154 // above does. In turn, after yielding, the CMS thread decrements both
155 // _pending_yields and _pending_decrements by the value seen in
156 // _pending_decrements before the decrement.
157 // NOTE: The above scheme is isomorphic to having two request counters,
158 // one for async requests and one for sync requests, and for the CMS thread
159 // to check the sum of the two counters to decide whether it should yield
160 // and to clear only the async counter when it yields. However, it turns out
161 // to be more efficient for CMS code to just check a single counter
162 // _pending_yields that holds the sum (of both sync and async requests), and
163 // a second counter _pending_decrements that only holds the async requests,
164 // for greater efficiency, since in a typical CMS run, there are many more
165 // potential (i.e. static) yield points than there are actual
166 // (i.e. dynamic) yields because of requests, which are few and far between.
167 //
168 // Note that, while "_pending_yields >= _pending_decrements" is an invariant,
169 // we cannot easily test that invariant, since the counters are manipulated via
170 // atomic instructions without explicit locking and we cannot read
171 // the two counters atomically together: one suggestion is to
172 // use (for example) 16-bit counters so as to be able to read the
173 // two counters atomically even on 32-bit platforms. Notice that
174 // the second assert in acknowledge_yield_request() below does indeed
175 // check a form of the above invariant, albeit indirectly.
176
177 static void increment_pending_yields() {
178 Atomic::inc(&_pending_yields);
179 assert(_pending_yields >= 0, "can't be negative");
180 }
181 static void decrement_pending_yields() {
182 Atomic::dec(&_pending_yields);
183 assert(_pending_yields >= 0, "can't be negative");
184 }
185 static void asynchronous_yield_request() {
186 assert(CMSIncrementalMode, "Currently only used w/iCMS");
187 increment_pending_yields();
188 Atomic::inc(&_pending_decrements);
189 assert(_pending_decrements >= 0, "can't be negative");
190 }
191 static void acknowledge_yield_request() {
192 jint decrement = _pending_decrements;
193 if (decrement > 0) {
194 assert(CMSIncrementalMode, "Currently only used w/iCMS");
195 // Order important to preserve: _pending_yields >= _pending_decrements
196 Atomic::add(-decrement, &_pending_decrements);
197 Atomic::add(-decrement, &_pending_yields);
198 assert(_pending_decrements >= 0, "can't be negative");
199 assert(_pending_yields >= 0, "can't be negative");
200 }
201 }
202 static bool should_yield() { return _pending_yields > 0; }
203
204 // CMS incremental mode.
205 static void start_icms(); // notify thread to start a quantum of work
206 static void stop_icms(); // request thread to stop working
207 void icms_wait(); // if asked to stop, wait until notified to start
208
209 // Incremental mode is enabled globally by the flag CMSIncrementalMode. It
210 // must also be enabled/disabled dynamically to allow foreground collections.
211 #define ICMS_ENABLING_ASSERT \
212 assert((CMSIncrementalMode && _icms_disabled >= 0) || \
213 (!CMSIncrementalMode && _icms_disabled <= 0), "Error")
214
215 static inline void enable_icms() {
216 ICMS_ENABLING_ASSERT;
217 Atomic::dec(&_icms_disabled);
218 }
219 static inline void disable_icms() {
220 ICMS_ENABLING_ASSERT;
221 Atomic::inc(&_icms_disabled);
222 }
223 static inline bool icms_is_disabled() {
224 ICMS_ENABLING_ASSERT;
225 return _icms_disabled > 0;
226 }
227 static inline bool icms_is_enabled() {
228 return !icms_is_disabled();
229 }
230 };
231
232 inline void ConcurrentMarkSweepThread::trace_state(const char* desc) {
233 if (CMSTraceThreadState) {
234 char buf[128];
235 TimeStamp& ts = gclog_or_tty->time_stamp();
236 if (!ts.is_updated()) {
237 ts.update();
238 }
239 jio_snprintf(buf, sizeof(buf), " [%.3f: CMSThread %s] ",
240 ts.seconds(), desc);
241 buf[sizeof(buf) - 1] = '\0';
242 gclog_or_tty->print("%s", buf);
243 }
244 }
245
246 // For scoped increment/decrement of (synchronous) yield requests
247 class CMSSynchronousYieldRequest: public StackObj {
248 public:
249 CMSSynchronousYieldRequest() {
|
47 static SurrogateLockerThread::SLT_msg_type _sltBuffer;
48 static Monitor* _sltMonitor;
49
50 static bool _should_terminate;
51
52 enum CMS_flag_type {
53 CMS_nil = NoBits,
54 CMS_cms_wants_token = nth_bit(0),
55 CMS_cms_has_token = nth_bit(1),
56 CMS_vm_wants_token = nth_bit(2),
57 CMS_vm_has_token = nth_bit(3)
58 };
59
60 static int _CMS_flag;
61
62 static bool CMS_flag_is_set(int b) { return (_CMS_flag & b) != 0; }
63 static bool set_CMS_flag(int b) { return (_CMS_flag |= b) != 0; }
64 static bool clear_CMS_flag(int b) { return (_CMS_flag &= ~b) != 0; }
65 void sleepBeforeNextCycle();
66
67 // CMS thread should yield for a young gen collection and direct allocations
68 static char _pad_1[64 - sizeof(jint)]; // prevent cache-line sharing
69 static volatile jint _pending_yields;
70 static char _pad_2[64 - sizeof(jint)]; // prevent cache-line sharing
71
72 // Tracing messages, enabled by CMSTraceThreadState.
73 static inline void trace_state(const char* desc);
74
75 // debugging
76 void verify_ok_to_terminate() const PRODUCT_RETURN;
77
78 public:
79 // Constructor
80 ConcurrentMarkSweepThread(CMSCollector* collector);
81
82 static void makeSurrogateLockerThread(TRAPS);
83 static SurrogateLockerThread* slt() { return _slt; }
84
85 // Tester
86 bool is_ConcurrentGC_thread() const { return true; }
87
88 static void threads_do(ThreadClosure* tc);
89
90 // Printing
91 static void print_all_on(outputStream* st);
92 static void print_all() { print_all_on(tty); }
93
94 // Returns the CMS Thread
112 static bool vm_thread_wants_cms_token() {
113 return CMS_flag_is_set(CMS_vm_wants_token);
114 }
115 static bool cms_thread_wants_cms_token() {
116 return CMS_flag_is_set(CMS_cms_wants_token);
117 }
118
119 // Wait on CMS lock until the next synchronous GC
120 // or given timeout, whichever is earlier. A timeout value
121 // of 0 indicates that there is no upper bound on the wait time.
122 // A concurrent full gc request terminates the wait.
123 void wait_on_cms_lock(long t_millis);
124
125 // Wait on CMS lock until the next synchronous GC
126 // or given timeout, whichever is earlier. A timeout value
127 // of 0 indicates that there is no upper bound on the wait time.
128 // A concurrent full gc request terminates the wait.
129 void wait_on_cms_lock_for_scavenge(long t_millis);
130
131 // The CMS thread will yield during the work portion of its cycle
132 // only when requested to.
133 // A synchronous request is used for young gen collections and
134 // for direct allocations. The requesting thread increments
135 // _pending_yields at the beginning of an operation, and decrements
136 // _pending_yields when that operation is completed.
137 // In turn, the CMS thread yields when _pending_yields is positive,
138 // and continues to yield until the value reverts to 0.
139
140 static void increment_pending_yields() {
141 Atomic::inc(&_pending_yields);
142 assert(_pending_yields >= 0, "can't be negative");
143 }
144 static void decrement_pending_yields() {
145 Atomic::dec(&_pending_yields);
146 assert(_pending_yields >= 0, "can't be negative");
147 }
148 static bool should_yield() { return _pending_yields > 0; }
149 };
150
151 inline void ConcurrentMarkSweepThread::trace_state(const char* desc) {
152 if (CMSTraceThreadState) {
153 char buf[128];
154 TimeStamp& ts = gclog_or_tty->time_stamp();
155 if (!ts.is_updated()) {
156 ts.update();
157 }
158 jio_snprintf(buf, sizeof(buf), " [%.3f: CMSThread %s] ",
159 ts.seconds(), desc);
160 buf[sizeof(buf) - 1] = '\0';
161 gclog_or_tty->print("%s", buf);
162 }
163 }
164
165 // For scoped increment/decrement of (synchronous) yield requests
166 class CMSSynchronousYieldRequest: public StackObj {
167 public:
168 CMSSynchronousYieldRequest() {
|