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/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   ThreadClosure* _thread_cl;
  49   bool _executed;
  50 public:
  51   HandshakeThreadsOperation(ThreadClosure* cl) : _thread_cl(cl), _executed(false) {}
  52   void do_handshake(JavaThread* thread);
  53   bool thread_has_completed() { return _done.trywait(); }
  54   bool executed() const { return _executed; }
  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   virtual Mode evaluation_mode() const { return _no_safepoint; }
  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 class VM_HandshakeOneThread: public VM_Handshake {
 110   JavaThread* _target;
 111  public:
 112   VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) :
 113     VM_Handshake(op), _target(target) {}
 114 
 115   void doit() {
 116     DEBUG_ONLY(_op->check_state();)
 117     TraceTime timer("Finished executing single-target operation (VM_HandshakeOneThread::doit)", TRACETIME_LOG(Info, handshake));
 118 
 119     ThreadsListHandle tlh;
 120     if (tlh.includes(_target)) {
 121       set_handshake(_target);
 122     } else {
 123       log_trace(handshake)("JavaThread " INTPTR_FORMAT " is not alive", p2i(_target));
 124       return;
 125     }
 126 
 127     log_trace(handshake)("JavaThread " INTPTR_FORMAT " signaled, begin attempt to process by VMThtread", p2i(_target));
 128     jlong start_time = os::elapsed_counter();
 129     do {
 130       if (handshake_has_timed_out(start_time)) {
 131         handle_timeout();
 132       }
 133 
 134       // We need to re-think this with SMR ThreadsList.
 135       // There is an assumption in the code that the Threads_lock should be
 136       // locked during certain phases.
 137       {
 138         MutexLocker ml(Threads_lock);
 139         _target->handshake_process_by_vmthread();
 140       }
 141     } while (!poll_for_completed_thread());
 142     DEBUG_ONLY(_op->check_state();)
 143   }
 144 
 145   VMOp_Type type() const { return VMOp_HandshakeOneThread; }
 146 
 147   bool executed() const { return _op->executed(); }
 148 };
 149 
 150 class VM_HandshakeAllThreads: public VM_Handshake {
 151  public:
 152   VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {}
 153 
 154   void doit() {
 155     DEBUG_ONLY(_op->check_state();)
 156     TraceTime timer("Finished executing multi-target operation (VM_HandshakeAllThreads::doit)", TRACETIME_LOG(Info, handshake));
 157 
 158     JavaThreadIteratorWithHandle jtiwh;
 159     int number_of_threads_issued = 0;
 160     for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 161       set_handshake(thr);
 162       number_of_threads_issued++;
 163     }
 164 
 165     if (number_of_threads_issued < 1) {
 166       log_debug(handshake)("No threads to handshake.");
 167       return;
 168     }
 169 
 170     log_debug(handshake)("Threads signaled, begin processing blocked threads by VMThtread");
 171     const jlong start_time = os::elapsed_counter();
 172     int number_of_threads_completed = 0;
 173     do {
 174       // Check if handshake operation has timed out
 175       if (handshake_has_timed_out(start_time)) {
 176         handle_timeout();
 177       }
 178 
 179       // Have VM thread perform the handshake operation for blocked threads.
 180       // Observing a blocked state may of course be transient but the processing is guarded
 181       // by semaphores and we optimistically begin by working on the blocked threads
 182       {
 183           // We need to re-think this with SMR ThreadsList.
 184           // There is an assumption in the code that the Threads_lock should
 185           // be locked during certain phases.
 186           jtiwh.rewind();
 187           MutexLocker ml(Threads_lock);
 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_process_by_vmthread.
 191             thr->handshake_process_by_vmthread();
 192           }
 193       }
 194 
 195       while (poll_for_completed_thread()) {
 196         // Includes canceled operations by exiting threads.
 197         number_of_threads_completed++;
 198       }
 199 
 200     } while (number_of_threads_issued > number_of_threads_completed);
 201     assert(number_of_threads_issued == number_of_threads_completed, "Must be the same");
 202     DEBUG_ONLY(_op->check_state();)
 203   }
 204 
 205   VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
 206 };
 207 
 208 class VM_HandshakeFallbackOperation : public VM_Operation {
 209   ThreadClosure* _thread_cl;
 210   Thread* _target_thread;
 211   bool _all_threads;
 212   bool _executed;
 213 public:
 214   VM_HandshakeFallbackOperation(ThreadClosure* cl) :
 215       _thread_cl(cl), _target_thread(NULL), _all_threads(true), _executed(false) {}
 216   VM_HandshakeFallbackOperation(ThreadClosure* cl, Thread* target) :
 217       _thread_cl(cl), _target_thread(target), _all_threads(false), _executed(false) {}
 218 
 219   void doit() {
 220     log_trace(handshake)("VMThread executing VM_HandshakeFallbackOperation");
 221     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 222       if (_all_threads || t == _target_thread) {
 223         if (t == _target_thread) {
 224           _executed = true;
 225         }
 226         _thread_cl->do_thread(t);
 227       }
 228     }
 229   }
 230 
 231   VMOp_Type type() const { return VMOp_HandshakeFallback; }
 232   bool executed() const { return _executed; }
 233 };
 234 
 235 void HandshakeThreadsOperation::do_handshake(JavaThread* thread) {
 236   ResourceMark rm;
 237   FormatBufferResource message("Operation for thread " PTR_FORMAT ", is_vm_thread: %s",
 238                                p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()));
 239   TraceTime timer(message, TRACETIME_LOG(Debug, handshake, task));
 240 
 241   // Only actually execute the operation for non terminated threads.
 242   if (!thread->is_terminated()) {
 243     _thread_cl->do_thread(thread);
 244     _executed = true;
 245   }
 246 
 247   // Use the semaphore to inform the VM thread that we have completed the operation
 248   _done.signal();
 249 }
 250 
 251 void Handshake::execute(ThreadClosure* thread_cl) {
 252   if (ThreadLocalHandshakes) {
 253     HandshakeThreadsOperation cto(thread_cl);
 254     VM_HandshakeAllThreads handshake(&cto);
 255     VMThread::execute(&handshake);
 256   } else {
 257     VM_HandshakeFallbackOperation op(thread_cl);
 258     VMThread::execute(&op);
 259   }
 260 }
 261 
 262 bool Handshake::execute(ThreadClosure* thread_cl, JavaThread* target) {
 263   if (ThreadLocalHandshakes) {
 264     HandshakeThreadsOperation cto(thread_cl);
 265     VM_HandshakeOneThread handshake(&cto, target);
 266     VMThread::execute(&handshake);
 267     return handshake.executed();
 268   } else {
 269     VM_HandshakeFallbackOperation op(thread_cl, target);
 270     VMThread::execute(&op);
 271     return op.executed();
 272   }
 273 }
 274 
 275 HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _thread_in_process_handshake(false) {}
 276 
 277 void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) {
 278   _operation = op;
 279   SafepointMechanism::arm_local_poll_release(target);
 280 }
 281 
 282 void HandshakeState::clear_handshake(JavaThread* target) {
 283   _operation = NULL;
 284   SafepointMechanism::disarm_if_needed(target, true /* release */);
 285 }
 286 
 287 void HandshakeState::process_self_inner(JavaThread* thread) {
 288   assert(Thread::current() == thread, "should call from thread");
 289   assert(!thread->is_terminated(), "should not be a terminated thread");
 290 
 291   ThreadInVMForHandshake tivm(thread);
 292   if (!_semaphore.trywait()) {
 293     _semaphore.wait_with_safepoint_check(thread);
 294   }
 295   HandshakeOperation* op = OrderAccess::load_acquire(&_operation);
 296   if (op != NULL) {
 297     HandleMark hm(thread);
 298     CautiouslyPreserveExceptionMark pem(thread);
 299     // Disarm before execute the operation
 300     clear_handshake(thread);
 301     op->do_handshake(thread);
 302   }
 303   _semaphore.signal();
 304 }
 305 
 306 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) {
 307   // handshake_safe may only be called with polls armed.
 308   // VM thread controls this by first claiming the handshake via claim_handshake_for_vmthread.
 309   return SafepointSynchronize::handshake_safe(target);
 310 }
 311 
 312 static bool possibly_vmthread_can_process_handshake(JavaThread* target) {
 313   // An externally suspended thread cannot be resumed while the
 314   // Threads_lock is held so it is safe.
 315   // Note that this method is allowed to produce false positives.
 316   assert(Threads_lock->owned_by_self(), "Not holding Threads_lock.");
 317   if (target->is_ext_suspended()) {
 318     return true;
 319   }
 320   if (target->is_terminated()) {
 321     return true;
 322   }
 323   switch (target->thread_state()) {
 324   case _thread_in_native:
 325     // native threads are safe if they have no java stack or have walkable stack
 326     return !target->has_last_Java_frame() || target->frame_anchor()->walkable();
 327 
 328   case _thread_blocked:
 329     return true;
 330 
 331   default:
 332     return false;
 333   }
 334 }
 335 
 336 bool HandshakeState::claim_handshake_for_vmthread() {
 337   if (!_semaphore.trywait()) {
 338     return false;
 339   }
 340   if (has_operation()) {
 341     return true;
 342   }
 343   _semaphore.signal();
 344   return false;
 345 }
 346 
 347 void HandshakeState::process_by_vmthread(JavaThread* target) {
 348   assert(Thread::current()->is_VM_thread(), "should call from vm thread");
 349   // Threads_lock must be held here, but that is assert()ed in
 350   // possibly_vmthread_can_process_handshake().
 351 
 352   if (!has_operation()) {
 353     // JT has already cleared its handshake
 354     return;
 355   }
 356 
 357   if (!possibly_vmthread_can_process_handshake(target)) {
 358     // JT is observed in an unsafe state, it must notice the handshake itself
 359     return;
 360   }
 361 
 362   // Claim the semaphore if there still an operation to be executed.
 363   if (!claim_handshake_for_vmthread()) {
 364     return;
 365   }
 366 
 367   // If we own the semaphore at this point and while owning the semaphore
 368   // can observe a safe state the thread cannot possibly continue without
 369   // getting caught by the semaphore.
 370   if (vmthread_can_process_handshake(target)) {
 371     guarantee(!_semaphore.trywait(), "we should already own the semaphore");
 372     log_trace(handshake)("Processing handshake by VMThtread");
 373     _operation->do_handshake(target);
 374     // Disarm after VM thread have executed the operation.
 375     clear_handshake(target);
 376     // Release the thread
 377   }
 378 
 379   _semaphore.signal();
 380 }