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) : _handshake_cl(cl), _pending_threads(1), _executed(false), _is_direct(false) {}
  48   void do_handshake(JavaThread* thread);
  49   bool is_completed() {
  50     assert(_pending_threads >= 0, "_pending_threads cannot be negative");
  51     return _pending_threads == 0;
  52   }
  53   void add_target_count(int count) { Atomic::add(&_pending_threads, count); }
  54   bool executed() const { return _executed; }
  55   const char* name() { return _handshake_cl->name(); }
  56 
  57   void set_isdirect() { _is_direct = true; }
  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->add_target_count(number_of_threads_issued - 1);
 175 
 176     log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread");
 177     const jlong start_time = os::elapsed_counter();
 178     do {
 179       // Check if handshake operation has timed out
 180       if (handshake_has_timed_out(start_time)) {
 181         handle_timeout();
 182       }
 183 
 184       // Have VM thread perform the handshake operation for blocked threads.
 185       // Observing a blocked state may of course be transient but the processing is guarded
 186       // by semaphores and we optimistically begin by working on the blocked threads
 187       jtiwh.rewind();
 188       for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 189         // A new thread on the ThreadsList will not have an operation,
 190         // hence it is skipped in handshake_try_process.
 191         if (thr->handshake_try_process(_op)) {
 192           handshake_executed_by_vm_thread++;
 193         }
 194       }
 195     } while (!_op->is_completed());
 196     DEBUG_ONLY(_op->check_state();)
 197 
 198     log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, handshake_executed_by_vm_thread);
 199   }
 200 
 201   VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
 202 };
 203 
 204 class VM_HandshakeFallbackOperation : public VM_Operation {
 205   HandshakeClosure* _handshake_cl;
 206   Thread* _target_thread;
 207   bool _all_threads;
 208   bool _executed;
 209 public:
 210   VM_HandshakeFallbackOperation(HandshakeClosure* cl) :
 211       _handshake_cl(cl), _target_thread(NULL), _all_threads(true), _executed(false) {}
 212   VM_HandshakeFallbackOperation(HandshakeClosure* cl, Thread* target) :
 213       _handshake_cl(cl), _target_thread(target), _all_threads(false), _executed(false) {}
 214 
 215   void doit() {
 216     log_trace(handshake)("VMThread executing VM_HandshakeFallbackOperation, operation: %s", name());
 217     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 218       if (_all_threads || t == _target_thread) {
 219         if (t == _target_thread) {
 220           _executed = true;
 221         }
 222         _handshake_cl->do_thread(t);
 223       }
 224     }
 225   }
 226 
 227   VMOp_Type type() const { return VMOp_HandshakeFallback; }
 228   bool executed() const { return _executed; }
 229 };
 230 
 231 void HandshakeOperation::do_handshake(JavaThread* thread) {
 232   jlong start_time_ns = 0;
 233   if (log_is_enabled(Debug, handshake, task)) {
 234     start_time_ns = os::javaTimeNanos();
 235   }
 236 
 237   // Only actually execute the operation for non terminated threads.
 238   if (!thread->is_terminated()) {
 239     _handshake_cl->do_thread(thread);
 240     _executed = true;
 241   }
 242 
 243   if (start_time_ns != 0) {
 244     jlong completion_time = os::javaTimeNanos() - start_time_ns;
 245     log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns",
 246                                name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time);
 247   }
 248 
 249   // Inform VMThread/Handshaker that we have completed the operation
 250   Atomic::dec(&_pending_threads);
 251 
 252   // It is no longer safe to refer to 'this' as the VMThread/Handshaker may have destroyed this operation
 253 }
 254 
 255 void Handshake::execute(HandshakeClosure* thread_cl) {
 256   if (SafepointMechanism::uses_thread_local_poll()) {
 257     HandshakeOperation cto(thread_cl);
 258     VM_HandshakeAllThreads handshake(&cto);
 259     VMThread::execute(&handshake);
 260   } else {
 261     VM_HandshakeFallbackOperation op(thread_cl);
 262     VMThread::execute(&op);
 263   }
 264 }
 265 
 266 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target, bool is_direct_handshake) {
 267   if (SafepointMechanism::uses_thread_local_poll()) {
 268     HandshakeOperation ho(thread_cl);
 269     if (is_direct_handshake) {
 270       direct_handshake(target, &ho);
 271     } else {
 272       VM_HandshakeOneThread op(&ho, target);
 273       VMThread::execute(&op);
 274     }
 275     return ho.executed();
 276   } else {
 277     VM_HandshakeFallbackOperation op(thread_cl, target);
 278     VMThread::execute(&op);
 279     return op.executed();
 280   }
 281 }
 282 
 283 void Handshake::direct_handshake(JavaThread* target, HandshakeOperation *op) {
 284   JavaThread *self = (JavaThread*)Thread::current();
 285   op->set_isdirect();
 286 
 287   jlong start_time_ns = 0;
 288   if (log_is_enabled(Info, handshake)) {
 289     start_time_ns = os::javaTimeNanos();
 290   }
 291 
 292   ThreadsListHandle tlh;
 293   if (tlh.includes(target)) {
 294     target->set_handshake_operation(op);
 295   } else {
 296     log_handshake_info(start_time_ns, op->name(), 0, 0, "(thread dead)");
 297     return;
 298   }
 299 
 300   bool by_handshaker = false;
 301   while (!op->is_completed()) {
 302     by_handshaker = target->handshake_try_process(op);
 303     // Check for pending handshakes to avoid possible deadlocks where our
 304     // target is trying to handshake us.
 305     if (SafepointMechanism::should_block(self)) {
 306       ThreadBlockInVM tbivm(self);
 307     }
 308   }
 309   DEBUG_ONLY(op->check_state();)
 310   log_handshake_info(start_time_ns, op->name(), 1, by_handshaker ? 1 : 0);
 311 }
 312 
 313 HandshakeState::HandshakeState() : _operation(NULL), _operation_direct(NULL), _handshake_turn_sem(1), _processing_sem(1), _thread_in_process_handshake(false) {
 314   DEBUG_ONLY(_active_handshaker = NULL;)
 315 }
 316 
 317 void HandshakeState::set_operation(HandshakeOperation* op) {
 318   if (!op->is_direct()) {
 319     _operation = op;
 320   } else {
 321     // Serialize direct handshakes so that only one proceeds at a time for a given target
 322     _handshake_turn_sem.wait_with_safepoint_check((JavaThread*)Thread::current());
 323     _operation_direct = op;
 324   }
 325   SafepointMechanism::arm_local_poll_release(_thread);
 326 }
 327 
 328 void HandshakeState::clear_handshake(bool is_direct) {
 329   if (!is_direct) {
 330     _operation = NULL;
 331   } else {
 332     _operation_direct = NULL;
 333     _handshake_turn_sem.signal();
 334   }
 335 }
 336 
 337 void HandshakeState::process_self_inner() {
 338   assert(Thread::current() == _thread, "should call from thread");
 339   assert(!_thread->is_terminated(), "should not be a terminated thread");
 340   assert(_thread->thread_state() != _thread_blocked, "should not be in a blocked state");
 341   assert(_thread->thread_state() != _thread_in_native, "should not be in native");
 342 
 343   do {
 344     ThreadInVMForHandshake tivm(_thread);
 345     if (!_processing_sem.trywait()) {
 346       _processing_sem.wait_with_safepoint_check(_thread);
 347     }
 348     bool is_direct_handshake = false;
 349     HandshakeOperation* op = Atomic::load_acquire(&_operation);
 350     if (op == NULL) {
 351       op = Atomic::load_acquire(&_operation_direct);
 352       is_direct_handshake = true;
 353     }
 354     if (op != NULL) {
 355       HandleMark hm(_thread);
 356       CautiouslyPreserveExceptionMark pem(_thread);
 357       // Disarm before execute the operation
 358       clear_handshake(is_direct_handshake);
 359       op->do_handshake(_thread);
 360     }
 361     _processing_sem.signal();
 362   } while (has_operation());
 363 }
 364 
 365 bool HandshakeState::can_process_handshake() {
 366   // handshake_safe may only be called with polls armed.
 367   // Handshaker controls this by first claiming the handshake via claim_handshake().
 368   return SafepointSynchronize::handshake_safe(_thread);
 369 }
 370 
 371 bool HandshakeState::possibly_can_process_handshake() {
 372   // Note that this method is allowed to produce false positives.
 373   if (_thread->is_ext_suspended()) {
 374     return true;
 375   }
 376   if (_thread->is_terminated()) {
 377     return true;
 378   }
 379   switch (_thread->thread_state()) {
 380   case _thread_in_native:
 381     // native threads are safe if they have no java stack or have walkable stack
 382     return !_thread->has_last_Java_frame() || _thread->frame_anchor()->walkable();
 383 
 384   case _thread_blocked:
 385     return true;
 386 
 387   default:
 388     return false;
 389   }
 390 }
 391 
 392 bool HandshakeState::claim_handshake(bool is_direct) {
 393   if (!_processing_sem.trywait()) {
 394     return false;
 395   }
 396   if ((!is_direct && _operation != NULL) || (is_direct && _operation_direct != NULL)){
 397     return true;
 398   }
 399   _processing_sem.signal();
 400   return false;
 401 }
 402 
 403 bool HandshakeState::try_process(HandshakeOperation* op) {
 404   bool is_direct = op->is_direct();
 405 
 406   if ((!is_direct && _operation == NULL) || (is_direct && _operation_direct == NULL)){
 407     // JT has already cleared its handshake
 408     return false;
 409   }
 410 
 411   if (!possibly_can_process_handshake()) {
 412     // JT is observed in an unsafe state, it must notice the handshake itself
 413     return false;
 414   }
 415 
 416   // Claim the semaphore if there still an operation to be executed.
 417   if (!claim_handshake(is_direct)) {
 418     return false;
 419   }
 420 
 421   // Check if the handshake operation is the same as the one we meant to execute. The
 422   // handshake could have been already processed by the handshakee and a new handshake
 423   // by another JavaThread might be in progress.
 424   if ( (is_direct && op != _operation_direct)) {
 425     _processing_sem.signal();
 426     return false;
 427   }
 428 
 429   // If we own the semaphore at this point and while owning the semaphore
 430   // can observe a safe state the thread cannot possibly continue without
 431   // getting caught by the semaphore.
 432   bool executed = false;
 433   if (can_process_handshake()) {
 434     guarantee(!_processing_sem.trywait(), "we should already own the semaphore");
 435     log_trace(handshake)("Processing handshake by %s", Thread::current()->is_VM_thread() ? "VMThread" : "Handshaker");
 436     DEBUG_ONLY(_active_handshaker = Thread::current();)
 437     op->do_handshake(_thread);
 438     DEBUG_ONLY(_active_handshaker = NULL;)
 439     // Disarm after we have executed the operation.
 440     clear_handshake(is_direct);
 441     executed = true;
 442   }
 443 
 444   // Release the thread
 445   _processing_sem.signal();
 446 
 447   return executed;
 448 }