1 /* 2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. 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 * 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/atomic.hpp" 30 #include "runtime/handshake.hpp" 31 #include "runtime/interfaceSupport.inline.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 bool _executed; 49 public: 50 HandshakeThreadsOperation(HandshakeClosure* cl) : _handshake_cl(cl), _executed(false) {} 51 void do_handshake(JavaThread* thread); 52 bool thread_has_completed() { return _done.trywait(); } 53 bool executed() const { return _executed; } 54 const char* name() { return _handshake_cl->name(); } 55 56 #ifdef ASSERT 57 void check_state() { 58 assert(!_done.trywait(), "Must be zero"); 59 } 60 #endif 61 }; 62 63 Semaphore HandshakeThreadsOperation::_done(0); 64 65 class VM_Handshake: public VM_Operation { 66 const jlong _handshake_timeout; 67 public: 68 bool evaluate_at_safepoint() const { return false; } 69 70 protected: 71 HandshakeThreadsOperation* const _op; 72 73 VM_Handshake(HandshakeThreadsOperation* op) : 74 _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)), _op(op) {} 75 76 void set_handshake(JavaThread* target) { 77 target->set_handshake_operation(_op); 78 } 79 80 // This method returns true for threads completed their operation 81 // and true for threads canceled their operation. 82 // A cancellation can happen if the thread is exiting. 83 bool poll_for_completed_thread() { return _op->thread_has_completed(); } 84 85 bool handshake_has_timed_out(jlong start_time); 86 static void handle_timeout(); 87 }; 88 89 bool VM_Handshake::handshake_has_timed_out(jlong start_time) { 90 // Check if handshake operation has timed out 91 if (_handshake_timeout > 0) { 92 return os::elapsed_counter() >= (start_time + _handshake_timeout); 93 } 94 return false; 95 } 96 97 void VM_Handshake::handle_timeout() { 98 LogStreamHandle(Warning, handshake) log_stream; 99 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) { 100 if (thr->has_handshake()) { 101 log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr)); 102 thr->print_thread_state_on(&log_stream); 103 } 104 } 105 log_stream.flush(); 106 fatal("Handshake operation timed out"); 107 } 108 109 static void log_handshake_info(jlong start_time_ns, const char* name, int targets, int vmt_executed, const char* extra = NULL) { 110 if (start_time_ns != 0) { 111 jlong completion_time = os::javaTimeNanos() - start_time_ns; 112 log_info(handshake)("Handshake \"%s\", Targeted threads: %d, Executed by targeted threads: %d, Total completion time: " JLONG_FORMAT " ns%s%s", 113 name, targets, 114 targets - vmt_executed, 115 completion_time, 116 extra != NULL ? ", " : "", 117 extra != NULL ? extra : ""); 118 } 119 } 120 121 class VM_HandshakeOneThread: public VM_Handshake { 122 JavaThread* _target; 123 public: 124 VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) : 125 VM_Handshake(op), _target(target) {} 126 127 void doit() { 128 DEBUG_ONLY(_op->check_state();) 129 130 jlong start_time_ns = 0; 131 if (log_is_enabled(Info, handshake)) { 132 start_time_ns = os::javaTimeNanos(); 133 } 134 135 ThreadsListHandle tlh; 136 if (tlh.includes(_target)) { 137 set_handshake(_target); 138 } else { 139 log_handshake_info(start_time_ns, _op->name(), 0, 0, "(thread dead)"); 140 return; 141 } 142 143 log_trace(handshake)("JavaThread " INTPTR_FORMAT " signaled, begin attempt to process by VMThtread", p2i(_target)); 144 jlong timeout_start_time = os::elapsed_counter(); 145 bool by_vm_thread = false; 146 do { 147 if (handshake_has_timed_out(timeout_start_time)) { 148 handle_timeout(); 149 } 150 by_vm_thread = _target->handshake_try_process_by_vmThread(); 151 } while (!poll_for_completed_thread()); 152 DEBUG_ONLY(_op->check_state();) 153 log_handshake_info(start_time_ns, _op->name(), 1, by_vm_thread ? 1 : 0); 154 } 155 156 VMOp_Type type() const { return VMOp_HandshakeOneThread; } 157 158 bool executed() const { return _op->executed(); } 159 }; 160 161 class VM_HandshakeAllThreads: public VM_Handshake { 162 public: 163 VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {} 164 165 void doit() { 166 DEBUG_ONLY(_op->check_state();) 167 168 jlong start_time_ns = 0; 169 if (log_is_enabled(Info, handshake)) { 170 start_time_ns = os::javaTimeNanos(); 171 } 172 int handshake_executed_by_vm_thread = 0; 173 174 JavaThreadIteratorWithHandle jtiwh; 175 int number_of_threads_issued = 0; 176 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) { 177 set_handshake(thr); 178 number_of_threads_issued++; 179 } 180 181 if (number_of_threads_issued < 1) { 182 log_handshake_info(start_time_ns, _op->name(), 0, 0); 183 return; 184 } 185 186 log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread"); 187 const jlong start_time = os::elapsed_counter(); 188 int number_of_threads_completed = 0; 189 do { 190 // Check if handshake operation has timed out 191 if (handshake_has_timed_out(start_time)) { 192 handle_timeout(); 193 } 194 195 // Have VM thread perform the handshake operation for blocked threads. 196 // Observing a blocked state may of course be transient but the processing is guarded 197 // by semaphores and we optimistically begin by working on the blocked threads 198 jtiwh.rewind(); 199 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) { 200 // A new thread on the ThreadsList will not have an operation, 201 // hence it is skipped in handshake_process_by_vmthread. 202 if (thr->handshake_try_process_by_vmThread()) { 203 handshake_executed_by_vm_thread++; 204 } 205 } 206 while (poll_for_completed_thread()) { 207 // Includes canceled operations by exiting threads. 208 number_of_threads_completed++; 209 } 210 211 } while (number_of_threads_issued > number_of_threads_completed); 212 assert(number_of_threads_issued == number_of_threads_completed, "Must be the same"); 213 DEBUG_ONLY(_op->check_state();) 214 215 log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, handshake_executed_by_vm_thread); 216 } 217 218 VMOp_Type type() const { return VMOp_HandshakeAllThreads; } 219 }; 220 221 class VM_HandshakeFallbackOperation : public VM_Operation { 222 HandshakeClosure* _handshake_cl; 223 Thread* _target_thread; 224 bool _all_threads; 225 bool _executed; 226 public: 227 VM_HandshakeFallbackOperation(HandshakeClosure* cl) : 228 _handshake_cl(cl), _target_thread(NULL), _all_threads(true), _executed(false) {} 229 VM_HandshakeFallbackOperation(HandshakeClosure* cl, Thread* target) : 230 _handshake_cl(cl), _target_thread(target), _all_threads(false), _executed(false) {} 231 232 void doit() { 233 log_trace(handshake)("VMThread executing VM_HandshakeFallbackOperation, operation: %s", name()); 234 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 235 if (_all_threads || t == _target_thread) { 236 if (t == _target_thread) { 237 _executed = true; 238 } 239 _handshake_cl->do_thread(t); 240 } 241 } 242 } 243 244 VMOp_Type type() const { return VMOp_HandshakeFallback; } 245 bool executed() const { return _executed; } 246 }; 247 248 void HandshakeThreadsOperation::do_handshake(JavaThread* thread) { 249 jlong start_time_ns = 0; 250 if (log_is_enabled(Debug, handshake, task)) { 251 start_time_ns = os::javaTimeNanos(); 252 } 253 254 // Only actually execute the operation for non terminated threads. 255 if (!thread->is_terminated()) { 256 _handshake_cl->do_thread(thread); 257 _executed = true; 258 } 259 260 if (start_time_ns != 0) { 261 jlong completion_time = os::javaTimeNanos() - start_time_ns; 262 log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns", 263 name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time); 264 } 265 266 // Use the semaphore to inform the VM thread that we have completed the operation 267 _done.signal(); 268 269 // It is no longer safe to refer to 'this' as the VMThread may have destroyed this operation 270 } 271 272 void Handshake::execute(HandshakeClosure* thread_cl) { 273 if (SafepointMechanism::uses_thread_local_poll()) { 274 HandshakeThreadsOperation cto(thread_cl); 275 VM_HandshakeAllThreads handshake(&cto); 276 VMThread::execute(&handshake); 277 } else { 278 VM_HandshakeFallbackOperation op(thread_cl); 279 VMThread::execute(&op); 280 } 281 } 282 283 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) { 284 if (SafepointMechanism::uses_thread_local_poll()) { 285 HandshakeThreadsOperation cto(thread_cl); 286 VM_HandshakeOneThread handshake(&cto, target); 287 VMThread::execute(&handshake); 288 return handshake.executed(); 289 } else { 290 VM_HandshakeFallbackOperation op(thread_cl, target); 291 VMThread::execute(&op); 292 return op.executed(); 293 } 294 } 295 296 HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _thread_in_process_handshake(false) { 297 DEBUG_ONLY(_vmthread_processing_handshake = false;) 298 } 299 300 void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) { 301 _operation = op; 302 SafepointMechanism::arm_local_poll_release(target); 303 } 304 305 void HandshakeState::clear_handshake(JavaThread* target) { 306 _operation = NULL; 307 SafepointMechanism::disarm_if_needed(target, true /* release */); 308 } 309 310 void HandshakeState::process_self_inner(JavaThread* thread) { 311 assert(Thread::current() == thread, "should call from thread"); 312 assert(!thread->is_terminated(), "should not be a terminated thread"); 313 assert(thread->thread_state() != _thread_blocked, "should not be in a blocked state"); 314 assert(thread->thread_state() != _thread_in_native, "should not be in native"); 315 316 do { 317 ThreadInVMForHandshake tivm(thread); 318 if (!_semaphore.trywait()) { 319 _semaphore.wait_with_safepoint_check(thread); 320 } 321 HandshakeOperation* op = Atomic::load_acquire(&_operation); 322 if (op != NULL) { 323 HandleMark hm(thread); 324 CautiouslyPreserveExceptionMark pem(thread); 325 // Disarm before execute the operation 326 clear_handshake(thread); 327 op->do_handshake(thread); 328 } 329 _semaphore.signal(); 330 } while (has_operation()); 331 } 332 333 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) { 334 // handshake_safe may only be called with polls armed. 335 // VM thread controls this by first claiming the handshake via claim_handshake_for_vmthread. 336 return SafepointSynchronize::handshake_safe(target); 337 } 338 339 static bool possibly_vmthread_can_process_handshake(JavaThread* target) { 340 // Note that this method is allowed to produce false positives. 341 if (target->is_ext_suspended()) { 342 return true; 343 } 344 if (target->is_terminated()) { 345 return true; 346 } 347 switch (target->thread_state()) { 348 case _thread_in_native: 349 // native threads are safe if they have no java stack or have walkable stack 350 return !target->has_last_Java_frame() || target->frame_anchor()->walkable(); 351 352 case _thread_blocked: 353 return true; 354 355 default: 356 return false; 357 } 358 } 359 360 bool HandshakeState::claim_handshake_for_vmthread() { 361 if (!_semaphore.trywait()) { 362 return false; 363 } 364 if (has_operation()) { 365 return true; 366 } 367 _semaphore.signal(); 368 return false; 369 } 370 371 bool HandshakeState::try_process_by_vmThread(JavaThread* target) { 372 assert(Thread::current()->is_VM_thread(), "should call from vm thread"); 373 374 if (!has_operation()) { 375 // JT has already cleared its handshake 376 return false; 377 } 378 379 if (!possibly_vmthread_can_process_handshake(target)) { 380 // JT is observed in an unsafe state, it must notice the handshake itself 381 return false; 382 } 383 384 // Claim the semaphore if there still an operation to be executed. 385 if (!claim_handshake_for_vmthread()) { 386 return false; 387 } 388 389 // If we own the semaphore at this point and while owning the semaphore 390 // can observe a safe state the thread cannot possibly continue without 391 // getting caught by the semaphore. 392 bool executed = false; 393 if (vmthread_can_process_handshake(target)) { 394 guarantee(!_semaphore.trywait(), "we should already own the semaphore"); 395 log_trace(handshake)("Processing handshake by VMThtread"); 396 DEBUG_ONLY(_vmthread_processing_handshake = true;) 397 _operation->do_handshake(target); 398 DEBUG_ONLY(_vmthread_processing_handshake = false;) 399 // Disarm after VM thread have executed the operation. 400 clear_handshake(target); 401 executed = true; 402 } 403 404 // Release the thread 405 _semaphore.signal(); 406 407 return executed; 408 } | 1 /* 2 * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. 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 * 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/atomic.hpp" 30 #include "runtime/handshake.hpp" 31 #include "runtime/interfaceSupport.inline.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 41 class HandshakeOperation: public StackObj { 42 HandshakeClosure* _handshake_cl; 43 int64_t _pending_threads; 44 bool _executed; 45 bool _is_direct; 46 public: 47 HandshakeOperation(HandshakeClosure* cl, bool is_direct = false) : _handshake_cl(cl), _pending_threads(1), _executed(false), _is_direct(is_direct) {} 48 void do_handshake(JavaThread* thread); 49 bool is_completed() { 50 int64_t val = Atomic::load(&_pending_threads); 51 assert(val >= 0, "_pending_threads cannot be negative"); 52 return val == 0; 53 } 54 void add_target_count(int count) { Atomic::add(&_pending_threads, count); } 55 bool executed() const { return _executed; } 56 const char* name() { return _handshake_cl->name(); } 57 58 bool is_direct() { return _is_direct; } 59 60 #ifdef ASSERT 61 void check_state() { 62 assert(_pending_threads == 0, "Must be zero"); 63 } 64 #endif 65 }; 66 67 class VM_Handshake: public VM_Operation { 68 const jlong _handshake_timeout; 69 public: 70 bool evaluate_at_safepoint() const { return false; } 71 72 protected: 73 HandshakeOperation* const _op; 74 75 VM_Handshake(HandshakeOperation* op) : 76 _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)), _op(op) {} 77 78 bool handshake_has_timed_out(jlong start_time); 79 static void handle_timeout(); 80 }; 81 82 bool VM_Handshake::handshake_has_timed_out(jlong start_time) { 83 // Check if handshake operation has timed out 84 if (_handshake_timeout > 0) { 85 return os::elapsed_counter() >= (start_time + _handshake_timeout); 86 } 87 return false; 88 } 89 90 void VM_Handshake::handle_timeout() { 91 LogStreamHandle(Warning, handshake) log_stream; 92 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) { 93 if (thr->has_handshake()) { 94 log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr)); 95 thr->print_thread_state_on(&log_stream); 96 } 97 } 98 log_stream.flush(); 99 fatal("Handshake operation timed out"); 100 } 101 102 static void log_handshake_info(jlong start_time_ns, const char* name, int targets, int vmt_executed, const char* extra = NULL) { 103 if (start_time_ns != 0) { 104 jlong completion_time = os::javaTimeNanos() - start_time_ns; 105 log_info(handshake)("Handshake \"%s\", Targeted threads: %d, Executed by targeted threads: %d, Total completion time: " JLONG_FORMAT " ns%s%s", 106 name, targets, 107 targets - vmt_executed, 108 completion_time, 109 extra != NULL ? ", " : "", 110 extra != NULL ? extra : ""); 111 } 112 } 113 114 class VM_HandshakeOneThread: public VM_Handshake { 115 JavaThread* _target; 116 public: 117 VM_HandshakeOneThread(HandshakeOperation* op, JavaThread* target) : 118 VM_Handshake(op), _target(target) {} 119 120 void doit() { 121 jlong start_time_ns = 0; 122 if (log_is_enabled(Info, handshake)) { 123 start_time_ns = os::javaTimeNanos(); 124 } 125 126 ThreadsListHandle tlh; 127 if (tlh.includes(_target)) { 128 _target->set_handshake_operation(_op); 129 } else { 130 log_handshake_info(start_time_ns, _op->name(), 0, 0, "(thread dead)"); 131 return; 132 } 133 134 log_trace(handshake)("JavaThread " INTPTR_FORMAT " signaled, begin attempt to process by VMThtread", p2i(_target)); 135 jlong timeout_start_time = os::elapsed_counter(); 136 bool by_vm_thread = false; 137 do { 138 if (handshake_has_timed_out(timeout_start_time)) { 139 handle_timeout(); 140 } 141 by_vm_thread = _target->handshake_try_process(_op); 142 } while (!_op->is_completed()); 143 DEBUG_ONLY(_op->check_state();) 144 log_handshake_info(start_time_ns, _op->name(), 1, by_vm_thread ? 1 : 0); 145 } 146 147 VMOp_Type type() const { return VMOp_HandshakeOneThread; } 148 149 bool executed() const { return _op->executed(); } 150 }; 151 152 class VM_HandshakeAllThreads: public VM_Handshake { 153 public: 154 VM_HandshakeAllThreads(HandshakeOperation* op) : VM_Handshake(op) {} 155 156 void doit() { 157 jlong start_time_ns = 0; 158 if (log_is_enabled(Info, handshake)) { 159 start_time_ns = os::javaTimeNanos(); 160 } 161 int handshake_executed_by_vm_thread = 0; 162 163 JavaThreadIteratorWithHandle jtiwh; 164 int number_of_threads_issued = 0; 165 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) { 166 thr->set_handshake_operation(_op); 167 number_of_threads_issued++; 168 } 169 170 if (number_of_threads_issued < 1) { 171 log_handshake_info(start_time_ns, _op->name(), 0, 0); 172 return; 173 } 174 // _op was created with a count == 1 so don't double count. 175 _op->add_target_count(number_of_threads_issued - 1); 176 177 log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread"); 178 const jlong start_time = os::elapsed_counter(); 179 do { 180 // Check if handshake operation has timed out 181 if (handshake_has_timed_out(start_time)) { 182 handle_timeout(); 183 } 184 185 // Have VM thread perform the handshake operation for blocked threads. 186 // Observing a blocked state may of course be transient but the processing is guarded 187 // by semaphores and we optimistically begin by working on the blocked threads 188 jtiwh.rewind(); 189 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) { 190 // A new thread on the ThreadsList will not have an operation, 191 // hence it is skipped in handshake_try_process. 192 if (thr->handshake_try_process(_op)) { 193 handshake_executed_by_vm_thread++; 194 } 195 } 196 } while (!_op->is_completed()); 197 DEBUG_ONLY(_op->check_state();) 198 199 log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, handshake_executed_by_vm_thread); 200 } 201 202 VMOp_Type type() const { return VMOp_HandshakeAllThreads; } 203 }; 204 205 class VM_HandshakeFallbackOperation : public VM_Operation { 206 HandshakeClosure* _handshake_cl; 207 Thread* _target_thread; 208 bool _all_threads; 209 bool _executed; 210 public: 211 VM_HandshakeFallbackOperation(HandshakeClosure* cl) : 212 _handshake_cl(cl), _target_thread(NULL), _all_threads(true), _executed(false) {} 213 VM_HandshakeFallbackOperation(HandshakeClosure* cl, Thread* target) : 214 _handshake_cl(cl), _target_thread(target), _all_threads(false), _executed(false) {} 215 216 void doit() { 217 log_trace(handshake)("VMThread executing VM_HandshakeFallbackOperation, operation: %s", name()); 218 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { 219 if (_all_threads || t == _target_thread) { 220 if (t == _target_thread) { 221 _executed = true; 222 } 223 _handshake_cl->do_thread(t); 224 } 225 } 226 } 227 228 VMOp_Type type() const { return VMOp_HandshakeFallback; } 229 bool executed() const { return _executed; } 230 }; 231 232 void HandshakeOperation::do_handshake(JavaThread* thread) { 233 jlong start_time_ns = 0; 234 if (log_is_enabled(Debug, handshake, task)) { 235 start_time_ns = os::javaTimeNanos(); 236 } 237 238 // Only actually execute the operation for non terminated threads. 239 if (!thread->is_terminated()) { 240 _handshake_cl->do_thread(thread); 241 _executed = true; 242 } 243 244 if (start_time_ns != 0) { 245 jlong completion_time = os::javaTimeNanos() - start_time_ns; 246 log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns", 247 name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time); 248 } 249 250 // Inform VMThread/Handshaker that we have completed the operation 251 Atomic::dec(&_pending_threads); 252 253 // It is no longer safe to refer to 'this' as the VMThread/Handshaker may have destroyed this operation 254 } 255 256 void Handshake::execute(HandshakeClosure* thread_cl) { 257 if (SafepointMechanism::uses_thread_local_poll()) { 258 HandshakeOperation ho(thread_cl); 259 VM_HandshakeAllThreads handshake(&ho); 260 VMThread::execute(&handshake); 261 } else { 262 VM_HandshakeFallbackOperation op(thread_cl); 263 VMThread::execute(&op); 264 } 265 } 266 267 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) { 268 if (SafepointMechanism::uses_thread_local_poll()) { 269 HandshakeOperation ho(thread_cl); 270 VM_HandshakeOneThread handshake(&ho, target); 271 VMThread::execute(&handshake); 272 return handshake.executed(); 273 } else { 274 VM_HandshakeFallbackOperation op(thread_cl, target); 275 VMThread::execute(&op); 276 return op.executed(); 277 } 278 } 279 280 bool Handshake::execute_direct(HandshakeClosure* thread_cl, JavaThread* target) { 281 if (!SafepointMechanism::uses_thread_local_poll()) { 282 VM_HandshakeFallbackOperation op(thread_cl, target); 283 VMThread::execute(&op); 284 return op.executed(); 285 } 286 JavaThread *self = JavaThread::current(); 287 HandshakeOperation op(thread_cl, /*is_direct*/ true); 288 289 jlong start_time_ns = 0; 290 if (log_is_enabled(Info, handshake)) { 291 start_time_ns = os::javaTimeNanos(); 292 } 293 294 ThreadsListHandle tlh; 295 if (tlh.includes(target)) { 296 target->set_handshake_operation(&op); 297 } else { 298 log_handshake_info(start_time_ns, op.name(), 0, 0, "(thread dead)"); 299 return false; 300 } 301 302 bool by_handshaker = false; 303 while (!op.is_completed()) { 304 by_handshaker = target->handshake_try_process(&op); 305 // Check for pending handshakes to avoid possible deadlocks where our 306 // target is trying to handshake us. 307 if (SafepointMechanism::should_block(self)) { 308 ThreadBlockInVM tbivm(self); 309 } 310 } 311 DEBUG_ONLY(op.check_state();) 312 log_handshake_info(start_time_ns, op.name(), 1, by_handshaker ? 1 : 0); 313 314 return op.executed(); 315 } 316 317 HandshakeState::HandshakeState() : 318 _operation(NULL), 319 _operation_direct(NULL), 320 _handshake_turn_sem(1), 321 _processing_sem(1), 322 _thread_in_process_handshake(false) 323 { 324 DEBUG_ONLY(_active_handshaker = NULL;) 325 } 326 327 void HandshakeState::set_operation(HandshakeOperation* op) { 328 if (!op->is_direct()) { 329 assert(Thread::current()->is_VM_thread(), "should be the VMThread"); 330 _operation = op; 331 } else { 332 assert(Thread::current()->is_Java_thread(), "should be a JavaThread"); 333 // Serialize direct handshakes so that only one proceeds at a time for a given target 334 _handshake_turn_sem.wait_with_safepoint_check(JavaThread::current()); 335 _operation_direct = op; 336 } 337 SafepointMechanism::arm_local_poll_release(_handshakee); 338 } 339 340 void HandshakeState::clear_handshake(bool is_direct) { 341 if (!is_direct) { 342 _operation = NULL; 343 } else { 344 _operation_direct = NULL; 345 _handshake_turn_sem.signal(); 346 } 347 } 348 349 void HandshakeState::process_self_inner() { 350 assert(Thread::current() == _handshakee, "should call from _handshakee"); 351 assert(!_handshakee->is_terminated(), "should not be a terminated thread"); 352 assert(_handshakee->thread_state() != _thread_blocked, "should not be in a blocked state"); 353 assert(_handshakee->thread_state() != _thread_in_native, "should not be in native"); 354 JavaThread* self = _handshakee; 355 356 do { 357 ThreadInVMForHandshake tivm(self); 358 if (!_processing_sem.trywait()) { 359 _processing_sem.wait_with_safepoint_check(self); 360 } 361 if (has_operation()) { 362 HandleMark hm(self); 363 CautiouslyPreserveExceptionMark pem(self); 364 HandshakeOperation * op = _operation; 365 if (op != NULL) { 366 // Disarm before executing the operation 367 clear_handshake( /*is_direct*/ false); 368 op->do_handshake(self); 369 } 370 op = _operation_direct; 371 if (op != NULL) { 372 // Disarm before executing the operation 373 clear_handshake( /*is_direct*/ true); 374 op->do_handshake(self); 375 } 376 } 377 _processing_sem.signal(); 378 } while (has_operation()); 379 } 380 381 bool HandshakeState::can_process_handshake() { 382 // handshake_safe may only be called with polls armed. 383 // Handshaker controls this by first claiming the handshake via claim_handshake(). 384 return SafepointSynchronize::handshake_safe(_handshakee); 385 } 386 387 bool HandshakeState::possibly_can_process_handshake() { 388 // Note that this method is allowed to produce false positives. 389 if (_handshakee->is_ext_suspended()) { 390 return true; 391 } 392 if (_handshakee->is_terminated()) { 393 return true; 394 } 395 switch (_handshakee->thread_state()) { 396 case _thread_in_native: 397 // native threads are safe if they have no java stack or have walkable stack 398 return !_handshakee->has_last_Java_frame() || _handshakee->frame_anchor()->walkable(); 399 400 case _thread_blocked: 401 return true; 402 403 default: 404 return false; 405 } 406 } 407 408 bool HandshakeState::claim_handshake(bool is_direct) { 409 if (!_processing_sem.trywait()) { 410 return false; 411 } 412 if (has_specific_operation(is_direct)){ 413 return true; 414 } 415 _processing_sem.signal(); 416 return false; 417 } 418 419 bool HandshakeState::try_process(HandshakeOperation* op) { 420 bool is_direct = op->is_direct(); 421 422 if (!has_specific_operation(is_direct)){ 423 // JT has already cleared its handshake 424 return false; 425 } 426 427 if (!possibly_can_process_handshake()) { 428 // JT is observed in an unsafe state, it must notice the handshake itself 429 return false; 430 } 431 432 // Claim the semaphore if there still an operation to be executed. 433 if (!claim_handshake(is_direct)) { 434 return false; 435 } 436 437 // Check if the handshake operation is the same as the one we meant to execute. The 438 // handshake could have been already processed by the handshakee and a new handshake 439 // by another JavaThread might be in progress. 440 if ( (is_direct && op != _operation_direct)) { 441 _processing_sem.signal(); 442 return false; 443 } 444 445 // If we own the semaphore at this point and while owning the semaphore 446 // can observe a safe state the thread cannot possibly continue without 447 // getting caught by the semaphore. 448 bool executed = false; 449 if (can_process_handshake()) { 450 guarantee(!_processing_sem.trywait(), "we should already own the semaphore"); 451 log_trace(handshake)("Processing handshake by %s", Thread::current()->is_VM_thread() ? "VMThread" : "Handshaker"); 452 DEBUG_ONLY(_active_handshaker = Thread::current();) 453 op->do_handshake(_handshakee); 454 DEBUG_ONLY(_active_handshaker = NULL;) 455 // Disarm after we have executed the operation. 456 clear_handshake(is_direct); 457 executed = true; 458 } 459 460 // Release the thread 461 _processing_sem.signal(); 462 463 return executed; 464 } |