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   bool evaluate_concurrently() const { return false; }
  71 
  72  protected:
  73   HandshakeThreadsOperation* const _op;
  74 
  75   VM_Handshake(HandshakeThreadsOperation* op) :
  76       _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)), _op(op) {}
  77 
  78   void set_handshake(JavaThread* target) {
  79     target->set_handshake_operation(_op);
  80   }
  81 
  82   // This method returns true for threads completed their operation
  83   // and true for threads canceled their operation.
  84   // A cancellation can happen if the thread is exiting.
  85   bool poll_for_completed_thread() { return _op->thread_has_completed(); }
  86 
  87   bool handshake_has_timed_out(jlong start_time);
  88   static void handle_timeout();
  89 };
  90 
  91 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
  92   // Check if handshake operation has timed out
  93   if (_handshake_timeout > 0) {
  94     return os::elapsed_counter() >= (start_time + _handshake_timeout);
  95   }
  96   return false;
  97 }
  98 
  99 void VM_Handshake::handle_timeout() {
 100   LogStreamHandle(Warning, handshake) log_stream;
 101   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
 102     if (thr->has_handshake()) {
 103       log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
 104       thr->print_thread_state_on(&log_stream);
 105     }
 106   }
 107   log_stream.flush();
 108   fatal("Handshake operation timed out");
 109 }
 110 
 111 static void log_handshake_info(jlong start_time_ns, const char* name, int targets, int vmt_executed, const char* extra = NULL) {
 112   if (start_time_ns != 0) {
 113     jlong completion_time = os::javaTimeNanos() - start_time_ns;
 114     log_info(handshake)("Handshake \"%s\", Targeted threads: %d, Executed by targeted threads: %d, Total completion time: " JLONG_FORMAT " ns%s%s",
 115                         name, targets,
 116                         targets - vmt_executed,
 117                         completion_time,
 118                         extra != NULL ? ", " : "",
 119                         extra != NULL ? extra : "");
 120   }
 121 }
 122 
 123 class VM_HandshakeOneThread: public VM_Handshake {
 124   JavaThread* _target;
 125  public:
 126   VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) :
 127     VM_Handshake(op), _target(target) {}
 128 
 129   void doit() {
 130     DEBUG_ONLY(_op->check_state();)
 131 
 132     jlong start_time_ns = 0;
 133     if (log_is_enabled(Info, handshake)) {
 134       start_time_ns = os::javaTimeNanos();
 135     }
 136 
 137     ThreadsListHandle tlh;
 138     if (tlh.includes(_target)) {
 139       set_handshake(_target);
 140     } else {
 141       log_handshake_info(start_time_ns, _op->name(), 0, 0, "(thread dead)");
 142       return;
 143     }
 144 
 145     log_trace(handshake)("JavaThread " INTPTR_FORMAT " signaled, begin attempt to process by VMThtread", p2i(_target));
 146     jlong timeout_start_time = os::elapsed_counter();
 147     bool by_vm_thread = false;
 148     do {
 149       if (handshake_has_timed_out(timeout_start_time)) {
 150         handle_timeout();
 151       }
 152 
 153       // We need to re-think this with SMR ThreadsList.
 154       // There is an assumption in the code that the Threads_lock should be
 155       // locked during certain phases.
 156       {
 157         MutexLocker ml(Threads_lock);
 158         by_vm_thread = _target->handshake_try_process_by_vmThread();
 159       }
 160     } while (!poll_for_completed_thread());
 161     DEBUG_ONLY(_op->check_state();)
 162     log_handshake_info(start_time_ns, _op->name(), 1, by_vm_thread ? 1 : 0);
 163   }
 164 
 165   VMOp_Type type() const { return VMOp_HandshakeOneThread; }
 166 
 167   bool executed() const { return _op->executed(); }
 168 };
 169 
 170 class VM_HandshakeAllThreads: public VM_Handshake {
 171  public:
 172   VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {}
 173 
 174   void doit() {
 175     DEBUG_ONLY(_op->check_state();)
 176 
 177     jlong start_time_ns = 0;
 178     if (log_is_enabled(Info, handshake)) {
 179       start_time_ns = os::javaTimeNanos();
 180     }
 181     int handshake_executed_by_vm_thread = 0;
 182 
 183     JavaThreadIteratorWithHandle jtiwh;
 184     int number_of_threads_issued = 0;
 185     for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 186       set_handshake(thr);
 187       number_of_threads_issued++;
 188     }
 189 
 190     if (number_of_threads_issued < 1) {
 191       log_handshake_info(start_time_ns, _op->name(), 0, 0);
 192       return;
 193     }
 194 
 195     log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread");
 196     const jlong start_time = os::elapsed_counter();
 197     int number_of_threads_completed = 0;
 198     do {
 199       // Check if handshake operation has timed out
 200       if (handshake_has_timed_out(start_time)) {
 201         handle_timeout();
 202       }
 203 
 204       // Have VM thread perform the handshake operation for blocked threads.
 205       // Observing a blocked state may of course be transient but the processing is guarded
 206       // by semaphores and we optimistically begin by working on the blocked threads
 207       {
 208           // We need to re-think this with SMR ThreadsList.
 209           // There is an assumption in the code that the Threads_lock should
 210           // be locked during certain phases.
 211           jtiwh.rewind();
 212           MutexLocker ml(Threads_lock);
 213           for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 214             // A new thread on the ThreadsList will not have an operation,
 215             // hence it is skipped in handshake_process_by_vmthread.
 216             if (thr->handshake_try_process_by_vmThread()) {
 217               handshake_executed_by_vm_thread++;
 218             }
 219           }
 220       }
 221 
 222       while (poll_for_completed_thread()) {
 223         // Includes canceled operations by exiting threads.
 224         number_of_threads_completed++;
 225       }
 226 
 227     } while (number_of_threads_issued > number_of_threads_completed);
 228     assert(number_of_threads_issued == number_of_threads_completed, "Must be the same");
 229     DEBUG_ONLY(_op->check_state();)
 230 
 231     log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, handshake_executed_by_vm_thread);
 232   }
 233 
 234   VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
 235 };
 236 
 237 class VM_HandshakeFallbackOperation : public VM_Operation {
 238   HandshakeClosure* _handshake_cl;
 239   Thread* _target_thread;
 240   bool _all_threads;
 241   bool _executed;
 242 public:
 243   VM_HandshakeFallbackOperation(HandshakeClosure* cl) :
 244       _handshake_cl(cl), _target_thread(NULL), _all_threads(true), _executed(false) {}
 245   VM_HandshakeFallbackOperation(HandshakeClosure* cl, Thread* target) :
 246       _handshake_cl(cl), _target_thread(target), _all_threads(false), _executed(false) {}
 247 
 248   void doit() {
 249     log_trace(handshake)("VMThread executing VM_HandshakeFallbackOperation, operation: %s", name());
 250     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 251       if (_all_threads || t == _target_thread) {
 252         if (t == _target_thread) {
 253           _executed = true;
 254         }
 255         _handshake_cl->do_thread(t);
 256       }
 257     }
 258   }
 259 
 260   VMOp_Type type() const { return VMOp_HandshakeFallback; }
 261   bool executed() const { return _executed; }
 262 };
 263 
 264 void HandshakeThreadsOperation::do_handshake(JavaThread* thread) {
 265   jlong start_time_ns = 0;
 266   if (log_is_enabled(Debug, handshake, task)) {
 267     start_time_ns = os::javaTimeNanos();
 268   }
 269 
 270   // Only actually execute the operation for non terminated threads.
 271   if (!thread->is_terminated()) {
 272     _handshake_cl->do_thread(thread);
 273     _executed = true;
 274   }
 275 
 276   // Use the semaphore to inform the VM thread that we have completed the operation
 277   _done.signal();
 278 
 279   if (start_time_ns != 0) {
 280     jlong completion_time = os::javaTimeNanos() - start_time_ns;
 281     log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns",
 282                                name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time);
 283   }
 284 }
 285 
 286 void Handshake::execute(HandshakeClosure* thread_cl) {
 287   if (ThreadLocalHandshakes) {
 288     HandshakeThreadsOperation cto(thread_cl);
 289     VM_HandshakeAllThreads handshake(&cto);
 290     VMThread::execute(&handshake);
 291   } else {
 292     VM_HandshakeFallbackOperation op(thread_cl);
 293     VMThread::execute(&op);
 294   }
 295 }
 296 
 297 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) {
 298   if (ThreadLocalHandshakes) {
 299     HandshakeThreadsOperation cto(thread_cl);
 300     VM_HandshakeOneThread handshake(&cto, target);
 301     VMThread::execute(&handshake);
 302     return handshake.executed();
 303   } else {
 304     VM_HandshakeFallbackOperation op(thread_cl, target);
 305     VMThread::execute(&op);
 306     return op.executed();
 307   }
 308 }
 309 
 310 HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _thread_in_process_handshake(false) {}
 311 
 312 void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) {
 313   _operation = op;
 314   SafepointMechanism::arm_local_poll_release(target);
 315 }
 316 
 317 void HandshakeState::clear_handshake(JavaThread* target) {
 318   _operation = NULL;
 319   SafepointMechanism::disarm_if_needed(target, true /* release */);
 320 }
 321 
 322 void HandshakeState::process_self_inner(JavaThread* thread) {
 323   assert(Thread::current() == thread, "should call from thread");
 324   assert(!thread->is_terminated(), "should not be a terminated thread");
 325   assert(thread->thread_state() != _thread_blocked, "should not be in a blocked state");
 326   assert(thread->thread_state() != _thread_in_native, "should not be in native");
 327 
 328   do {
 329     ThreadInVMForHandshake tivm(thread);
 330     if (!_semaphore.trywait()) {
 331       _semaphore.wait_with_safepoint_check(thread);
 332     }
 333     HandshakeOperation* op = Atomic::load_acquire(&_operation);
 334     if (op != NULL) {
 335       HandleMark hm(thread);
 336       CautiouslyPreserveExceptionMark pem(thread);
 337       // Disarm before execute the operation
 338       clear_handshake(thread);
 339       op->do_handshake(thread);
 340     }
 341     _semaphore.signal();
 342   } while (has_operation());
 343 }
 344 
 345 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) {
 346   // handshake_safe may only be called with polls armed.
 347   // VM thread controls this by first claiming the handshake via claim_handshake_for_vmthread.
 348   return SafepointSynchronize::handshake_safe(target);
 349 }
 350 
 351 static bool possibly_vmthread_can_process_handshake(JavaThread* target) {
 352   // An externally suspended thread cannot be resumed while the
 353   // Threads_lock is held so it is safe.
 354   // Note that this method is allowed to produce false positives.
 355   assert(Threads_lock->owned_by_self(), "Not holding Threads_lock.");
 356   if (target->is_ext_suspended()) {
 357     return true;
 358   }
 359   if (target->is_terminated()) {
 360     return true;
 361   }
 362   switch (target->thread_state()) {
 363   case _thread_in_native:
 364     // native threads are safe if they have no java stack or have walkable stack
 365     return !target->has_last_Java_frame() || target->frame_anchor()->walkable();
 366 
 367   case _thread_blocked:
 368     return true;
 369 
 370   default:
 371     return false;
 372   }
 373 }
 374 
 375 bool HandshakeState::claim_handshake_for_vmthread() {
 376   if (!_semaphore.trywait()) {
 377     return false;
 378   }
 379   if (has_operation()) {
 380     return true;
 381   }
 382   _semaphore.signal();
 383   return false;
 384 }
 385 
 386 bool HandshakeState::try_process_by_vmThread(JavaThread* target) {
 387   assert(Thread::current()->is_VM_thread(), "should call from vm thread");
 388   // Threads_lock must be held here, but that is assert()ed in
 389   // possibly_vmthread_can_process_handshake().
 390 
 391   if (!has_operation()) {
 392     // JT has already cleared its handshake
 393     return false;
 394   }
 395 
 396   if (!possibly_vmthread_can_process_handshake(target)) {
 397     // JT is observed in an unsafe state, it must notice the handshake itself
 398     return false;
 399   }
 400 
 401   // Claim the semaphore if there still an operation to be executed.
 402   if (!claim_handshake_for_vmthread()) {
 403     return false;
 404   }
 405 
 406   // If we own the semaphore at this point and while owning the semaphore
 407   // can observe a safe state the thread cannot possibly continue without
 408   // getting caught by the semaphore.
 409   bool executed = false;
 410   if (vmthread_can_process_handshake(target)) {
 411     guarantee(!_semaphore.trywait(), "we should already own the semaphore");
 412     log_trace(handshake)("Processing handshake by VMThtread");
 413     _operation->do_handshake(target);
 414     // Disarm after VM thread have executed the operation.
 415     clear_handshake(target);
 416     executed = true;
 417   }
 418 
 419   // Release the thread
 420   _semaphore.signal();
 421 
 422   return executed;
 423 }