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 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "logging/log.hpp"
27 #include "logging/logStream.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "runtime/handshake.hpp"
30 #include "runtime/interfaceSupport.inline.hpp"
31 #include "runtime/orderAccess.hpp"
32 #include "runtime/osThread.hpp"
33 #include "runtime/semaphore.inline.hpp"
34 #include "runtime/task.hpp"
35 #include "runtime/timerTrace.hpp"
36 #include "runtime/thread.hpp"
37 #include "runtime/vmThread.hpp"
38 #include "utilities/formatBuffer.hpp"
39 #include "utilities/preserveException.hpp"
40
41 class HandshakeOperation: public StackObj {
42 public:
43 virtual void do_handshake(JavaThread* thread) = 0;
44 };
45
46 class HandshakeThreadsOperation: public HandshakeOperation {
47 static Semaphore _done;
48 HandshakeClosure* _handshake_cl;
49
50 public:
51 HandshakeThreadsOperation(HandshakeClosure* cl) : _handshake_cl(cl) {}
52 void do_handshake(JavaThread* thread);
53 bool thread_has_completed() { return _done.trywait(); }
54
55 #ifdef ASSERT
56 void check_state() {
57 assert(!_done.trywait(), "Must be zero");
58 }
59 #endif
60 };
61
62 Semaphore HandshakeThreadsOperation::_done(0);
63
64 // Performing handshakes requires a custom yielding strategy because without it
65 // there is a clear performance regression vs plain spinning. We keep track of
66 // when we last saw progress by looking at why each targeted thread has not yet
67 // completed its handshake. After spinning for a while with no progress we will
68 // yield, but as long as there is progress, we keep spinning. Thus we avoid
69 // yielding when there is potential work to be done or the handshake is close
70 // to being finished.
71 class HandshakeSpinYield : public StackObj {
72 private:
73 jlong _start_time_ns;
179 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
180 // Check if handshake operation has timed out
181 if (_handshake_timeout > 0) {
182 return os::javaTimeNanos() >= (start_time + _handshake_timeout);
183 }
184 return false;
185 }
186
187 void VM_Handshake::handle_timeout() {
188 LogStreamHandle(Warning, handshake) log_stream;
189 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
190 if (thr->has_handshake()) {
191 log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
192 thr->print_thread_state_on(&log_stream);
193 }
194 }
195 log_stream.flush();
196 fatal("Handshake operation timed out");
197 }
198
199 class VM_HandshakeOneThread: public VM_Handshake {
200 JavaThread* _target;
201 bool _thread_alive;
202 public:
203 VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) :
204 VM_Handshake(op), _target(target), _thread_alive(false) {}
205
206 void doit() {
207 jlong start_time_ns = os::javaTimeNanos();
208 DEBUG_ONLY(_op->check_state();)
209 TraceTime timer("Performing single-target operation (vmoperation doit)", TRACETIME_LOG(Info, handshake));
210
211 ThreadsListHandle tlh;
212 if (tlh.includes(_target)) {
213 set_handshake(_target);
214 _thread_alive = true;
215 } else {
216 return;
217 }
218
219 if (!UseMembar) {
220 os::serialize_thread_states();
221 }
222
223 log_trace(handshake)("Thread signaled, begin processing by VMThtread");
224 HandshakeState::ProcessResult pr = HandshakeState::_no_operation;
225 HandshakeSpinYield hsy(start_time_ns);
226 do {
227 if (handshake_has_timed_out(start_time_ns)) {
228 handle_timeout();
229 }
230
231 // We need to re-think this with SMR ThreadsList.
232 // There is an assumption in the code that the Threads_lock should be
233 // locked during certain phases.
234 {
235 MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
236 pr = _target->handshake_process_by_vmthread();
237 }
238 hsy.add_result(pr);
239 hsy.process();
240 } while (!poll_for_completed_thread());
241 DEBUG_ONLY(_op->check_state();)
242 }
243
244 VMOp_Type type() const { return VMOp_HandshakeOneThread; }
245
246 bool thread_alive() const { return _thread_alive; }
247 };
248
249 class VM_HandshakeAllThreads: public VM_Handshake {
250 public:
251 VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {}
252
253 void doit() {
254 jlong start_time_ns = os::javaTimeNanos();
255 DEBUG_ONLY(_op->check_state();)
256 TraceTime timer("Performing operation (vmoperation doit)", TRACETIME_LOG(Info, handshake));
257
258 JavaThreadIteratorWithHandle jtiwh;
259 int number_of_threads_issued = 0;
260 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
261 set_handshake(thr);
262 number_of_threads_issued++;
263 }
264
265 if (number_of_threads_issued < 1) {
266 log_debug(handshake)("No threads to handshake.");
267 return;
268 }
269
270 if (!UseMembar) {
271 os::serialize_thread_states();
272 }
273
274 log_debug(handshake)("Threads signaled, begin processing blocked threads by VMThtread");
275 HandshakeSpinYield hsy(start_time_ns);
276 int number_of_threads_completed = 0;
277 do {
278 // Check if handshake operation has timed out
279 if (handshake_has_timed_out(start_time_ns)) {
280 handle_timeout();
281 }
282
283 // Have VM thread perform the handshake operation for blocked threads.
284 // Observing a blocked state may of course be transient but the processing is guarded
285 // by semaphores and we optimistically begin by working on the blocked threads
286 {
287 // We need to re-think this with SMR ThreadsList.
288 // There is an assumption in the code that the Threads_lock should
289 // be locked during certain phases.
290 jtiwh.rewind();
291 MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
292 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
293 // A new thread on the ThreadsList will not have an operation,
294 // hence it is skipped in handshake_process_by_vmthread.
295 HandshakeState::ProcessResult pr = thr->handshake_process_by_vmthread();
296 hsy.add_result(pr);
297 }
298 hsy.process();
299 }
300
301 while (poll_for_completed_thread()) {
302 // Includes canceled operations by exiting threads.
303 number_of_threads_completed++;
304 }
305
306 } while (number_of_threads_issued > number_of_threads_completed);
307 assert(number_of_threads_issued == number_of_threads_completed, "Must be the same");
308 DEBUG_ONLY(_op->check_state();)
309 }
310
311 VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
312 };
313
314 class VM_HandshakeFallbackOperation : public VM_Operation {
315 HandshakeClosure* _handshake_cl;
316 Thread* _target_thread;
317 bool _all_threads;
318 bool _thread_alive;
319 public:
320 VM_HandshakeFallbackOperation(HandshakeClosure* cl) :
321 _handshake_cl(cl), _target_thread(NULL), _all_threads(true) {}
322 VM_HandshakeFallbackOperation(HandshakeClosure* cl, Thread* target) :
323 _handshake_cl(cl), _target_thread(target), _all_threads(false) {}
324
325 void doit() {
326 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
327 if (_all_threads || t == _target_thread) {
328 if (t == _target_thread) {
329 _thread_alive = true;
330 }
331 _handshake_cl->do_thread(t);
332 }
333 }
334 }
335
336 VMOp_Type type() const { return VMOp_HandshakeFallback; }
337 bool thread_alive() const { return _thread_alive; }
338 };
339
340 void HandshakeThreadsOperation::do_handshake(JavaThread* thread) {
341 ResourceMark rm;
342 FormatBufferResource message("Operation for thread " PTR_FORMAT ", is_vm_thread: %s",
343 p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()));
344 TraceTime timer(message, TRACETIME_LOG(Debug, handshake, task));
345
346 // Only actually execute the operation for non terminated threads.
347 if (!thread->is_terminated()) {
348 _handshake_cl->do_thread(thread);
349 }
350
351 // Use the semaphore to inform the VM thread that we have completed the operation
352 _done.signal();
353 }
354
355 void Handshake::execute(HandshakeClosure* thread_cl) {
356 if (ThreadLocalHandshakes) {
357 HandshakeThreadsOperation cto(thread_cl);
358 VM_HandshakeAllThreads handshake(&cto);
359 VMThread::execute(&handshake);
360 } else {
361 VM_HandshakeFallbackOperation op(thread_cl);
362 VMThread::execute(&op);
363 }
364 }
365
366 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) {
367 if (ThreadLocalHandshakes) {
368 HandshakeThreadsOperation cto(thread_cl);
369 VM_HandshakeOneThread handshake(&cto, target);
370 VMThread::execute(&handshake);
371 return handshake.thread_alive();
372 } else {
435
436 case _thread_blocked:
437 return true;
438
439 default:
440 return false;
441 }
442 }
443
444 bool HandshakeState::claim_handshake_for_vmthread() {
445 if (!_semaphore.trywait()) {
446 return false;
447 }
448 if (has_operation()) {
449 return true;
450 }
451 _semaphore.signal();
452 return false;
453 }
454
455 HandshakeState::ProcessResult HandshakeState::process_by_vmthread(JavaThread* target) {
456 assert(Thread::current()->is_VM_thread(), "should call from vm thread");
457 // Threads_lock must be held here, but that is assert()ed in
458 // possibly_vmthread_can_process_handshake().
459
460 if (!has_operation()) {
461 // JT has already cleared its handshake
462 return _no_operation;
463 }
464
465 if (!possibly_vmthread_can_process_handshake(target)) {
466 // JT is observed in an unsafe state, it must notice the handshake itself
467 return _not_safe;
468 }
469
470 // Claim the semaphore if there still an operation to be executed.
471 if (!claim_handshake_for_vmthread()) {
472 return _state_busy;
473 }
474
475 // If we own the semaphore at this point and while owning the semaphore
|
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 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "logging/log.hpp"
27 #include "logging/logStream.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "runtime/handshake.hpp"
30 #include "runtime/interfaceSupport.inline.hpp"
31 #include "runtime/orderAccess.hpp"
32 #include "runtime/osThread.hpp"
33 #include "runtime/semaphore.inline.hpp"
34 #include "runtime/task.hpp"
35 #include "runtime/thread.hpp"
36 #include "runtime/vmThread.hpp"
37 #include "utilities/formatBuffer.hpp"
38 #include "utilities/preserveException.hpp"
39
40 class HandshakeOperation: public StackObj {
41 public:
42 virtual void do_handshake(JavaThread* thread) = 0;
43 };
44
45 class HandshakeThreadsOperation: public HandshakeOperation {
46 static Semaphore _done;
47 HandshakeClosure* _handshake_cl;
48
49 public:
50 HandshakeThreadsOperation(HandshakeClosure* cl) : _handshake_cl(cl) {}
51 void do_handshake(JavaThread* thread);
52 bool thread_has_completed() { return _done.trywait(); }
53 const char* name() { return _handshake_cl->name(); }
54
55 #ifdef ASSERT
56 void check_state() {
57 assert(!_done.trywait(), "Must be zero");
58 }
59 #endif
60 };
61
62 Semaphore HandshakeThreadsOperation::_done(0);
63
64 // Performing handshakes requires a custom yielding strategy because without it
65 // there is a clear performance regression vs plain spinning. We keep track of
66 // when we last saw progress by looking at why each targeted thread has not yet
67 // completed its handshake. After spinning for a while with no progress we will
68 // yield, but as long as there is progress, we keep spinning. Thus we avoid
69 // yielding when there is potential work to be done or the handshake is close
70 // to being finished.
71 class HandshakeSpinYield : public StackObj {
72 private:
73 jlong _start_time_ns;
179 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
180 // Check if handshake operation has timed out
181 if (_handshake_timeout > 0) {
182 return os::javaTimeNanos() >= (start_time + _handshake_timeout);
183 }
184 return false;
185 }
186
187 void VM_Handshake::handle_timeout() {
188 LogStreamHandle(Warning, handshake) log_stream;
189 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
190 if (thr->has_handshake()) {
191 log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
192 thr->print_thread_state_on(&log_stream);
193 }
194 }
195 log_stream.flush();
196 fatal("Handshake operation timed out");
197 }
198
199 static void log_handshake_info(jlong start_time_ns, const char* name, int targets, int vmt_executed, const char* extra = NULL) {
200 if (start_time_ns != 0) {
201 jlong completion_time = os::javaTimeNanos() - start_time_ns;
202 log_info(handshake)("Handshake \"%s\", Targeted threads: %d, Executed by targeted threads: %d, Total completion time: " JLONG_FORMAT " ns%s%s",
203 name, targets,
204 targets - vmt_executed,
205 completion_time,
206 extra != NULL ? ", " : "",
207 extra != NULL ? extra : "");
208 }
209 }
210
211 class VM_HandshakeOneThread: public VM_Handshake {
212 JavaThread* _target;
213 bool _thread_alive;
214 public:
215 VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) :
216 VM_Handshake(op), _target(target), _thread_alive(false) {}
217
218 void doit() {
219 DEBUG_ONLY(_op->check_state();)
220 jlong start_time_ns = os::javaTimeNanos();
221
222 ThreadsListHandle tlh;
223 if (tlh.includes(_target)) {
224 set_handshake(_target);
225 _thread_alive = true;
226 } else {
227 log_handshake_info(start_time_ns, _op->name(), 0, 0, "(thread dead)");
228 return;
229 }
230
231 if (!UseMembar) {
232 os::serialize_thread_states();
233 }
234
235 log_trace(handshake)("Thread signaled, begin processing by VMThtread");
236 HandshakeState::ProcessResult pr = HandshakeState::_no_operation;
237 HandshakeSpinYield hsy(start_time_ns);
238 do {
239 if (handshake_has_timed_out(start_time_ns)) {
240 handle_timeout();
241 }
242
243 // We need to re-think this with SMR ThreadsList.
244 // There is an assumption in the code that the Threads_lock should be
245 // locked during certain phases.
246 {
247 MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
248 pr = _target->handshake_try_process_by_vmThread(_op);
249 }
250 hsy.add_result(pr);
251 hsy.process();
252 } while (!poll_for_completed_thread());
253 DEBUG_ONLY(_op->check_state();)
254 log_handshake_info(start_time_ns, _op->name(), 1, (pr == HandshakeState::_success) ? 1 : 0);
255 }
256
257 VMOp_Type type() const { return VMOp_HandshakeOneThread; }
258
259 bool thread_alive() const { return _thread_alive; }
260 };
261
262 class VM_HandshakeAllThreads: public VM_Handshake {
263 public:
264 VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {}
265
266 void doit() {
267 DEBUG_ONLY(_op->check_state();)
268
269 jlong start_time_ns = os::javaTimeNanos();
270 int handshake_executed_by_vm_thread = 0;
271
272 JavaThreadIteratorWithHandle jtiwh;
273 int number_of_threads_issued = 0;
274 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
275 set_handshake(thr);
276 number_of_threads_issued++;
277 }
278
279 if (number_of_threads_issued < 1) {
280 log_handshake_info(start_time_ns, _op->name(), 0, 0);
281 return;
282 }
283
284 if (!UseMembar) {
285 os::serialize_thread_states();
286 }
287
288 log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread");
289 HandshakeSpinYield hsy(start_time_ns);
290 int number_of_threads_completed = 0;
291 do {
292 // Check if handshake operation has timed out
293 if (handshake_has_timed_out(start_time_ns)) {
294 handle_timeout();
295 }
296
297 // Have VM thread perform the handshake operation for blocked threads.
298 // Observing a blocked state may of course be transient but the processing is guarded
299 // by semaphores and we optimistically begin by working on the blocked threads
300 {
301 // We need to re-think this with SMR ThreadsList.
302 // There is an assumption in the code that the Threads_lock should
303 // be locked during certain phases.
304 jtiwh.rewind();
305 MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
306 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
307 // A new thread on the ThreadsList will not have an operation,
308 // hence it is skipped in handshake_process_by_vmthread.
309 HandshakeState::ProcessResult pr = thr->handshake_try_process_by_vmThread(_op);
310 if (pr == HandshakeState::_success) {
311 handshake_executed_by_vm_thread++;
312 }
313 hsy.add_result(pr);
314 }
315 hsy.process();
316 }
317
318 while (poll_for_completed_thread()) {
319 // Includes canceled operations by exiting threads.
320 number_of_threads_completed++;
321 }
322
323 } while (number_of_threads_issued > number_of_threads_completed);
324 assert(number_of_threads_issued == number_of_threads_completed, "Must be the same");
325 DEBUG_ONLY(_op->check_state();)
326
327 log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, handshake_executed_by_vm_thread);
328 }
329
330 VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
331 };
332
333 class VM_HandshakeFallbackOperation : public VM_Operation {
334 HandshakeClosure* _handshake_cl;
335 Thread* _target_thread;
336 bool _all_threads;
337 bool _thread_alive;
338 public:
339 VM_HandshakeFallbackOperation(HandshakeClosure* cl) :
340 _handshake_cl(cl), _target_thread(NULL), _all_threads(true) {}
341 VM_HandshakeFallbackOperation(HandshakeClosure* cl, Thread* target) :
342 _handshake_cl(cl), _target_thread(target), _all_threads(false) {}
343
344 void doit() {
345 log_trace(handshake)("VMThread executing VM_HandshakeFallbackOperation, operation: %s", name());
346 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
347 if (_all_threads || t == _target_thread) {
348 if (t == _target_thread) {
349 _thread_alive = true;
350 }
351 _handshake_cl->do_thread(t);
352 }
353 }
354 }
355
356 VMOp_Type type() const { return VMOp_HandshakeFallback; }
357 bool thread_alive() const { return _thread_alive; }
358 };
359
360 void HandshakeThreadsOperation::do_handshake(JavaThread* thread) {
361 jlong start_time_ns = 0;
362 if (log_is_enabled(Debug, handshake, task)) {
363 start_time_ns = os::javaTimeNanos();
364 }
365
366 // Only actually execute the operation for non terminated threads.
367 if (!thread->is_terminated()) {
368 _handshake_cl->do_thread(thread);
369 }
370
371 // Use the semaphore to inform the VM thread that we have completed the operation
372 _done.signal();
373
374 if (start_time_ns != 0) {
375 jlong completion_time = os::javaTimeNanos() - start_time_ns;
376 log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns",
377 name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time);
378 }
379 }
380
381 void Handshake::execute(HandshakeClosure* thread_cl) {
382 if (ThreadLocalHandshakes) {
383 HandshakeThreadsOperation cto(thread_cl);
384 VM_HandshakeAllThreads handshake(&cto);
385 VMThread::execute(&handshake);
386 } else {
387 VM_HandshakeFallbackOperation op(thread_cl);
388 VMThread::execute(&op);
389 }
390 }
391
392 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) {
393 if (ThreadLocalHandshakes) {
394 HandshakeThreadsOperation cto(thread_cl);
395 VM_HandshakeOneThread handshake(&cto, target);
396 VMThread::execute(&handshake);
397 return handshake.thread_alive();
398 } else {
461
462 case _thread_blocked:
463 return true;
464
465 default:
466 return false;
467 }
468 }
469
470 bool HandshakeState::claim_handshake_for_vmthread() {
471 if (!_semaphore.trywait()) {
472 return false;
473 }
474 if (has_operation()) {
475 return true;
476 }
477 _semaphore.signal();
478 return false;
479 }
480
481 HandshakeState::ProcessResult HandshakeState::try_process_by_vmThread(JavaThread* target) {
482 assert(Thread::current()->is_VM_thread(), "should call from vm thread");
483 // Threads_lock must be held here, but that is assert()ed in
484 // possibly_vmthread_can_process_handshake().
485
486 if (!has_operation()) {
487 // JT has already cleared its handshake
488 return _no_operation;
489 }
490
491 if (!possibly_vmthread_can_process_handshake(target)) {
492 // JT is observed in an unsafe state, it must notice the handshake itself
493 return _not_safe;
494 }
495
496 // Claim the semaphore if there still an operation to be executed.
497 if (!claim_handshake_for_vmthread()) {
498 return _state_busy;
499 }
500
501 // If we own the semaphore at this point and while owning the semaphore
|