1 /*
   2  * Copyright (c) 2017, 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.hpp"
  31 #include "runtime/osThread.hpp"
  32 #include "runtime/semaphore.hpp"
  33 #include "runtime/task.hpp"
  34 #include "runtime/timerTrace.hpp"
  35 #include "runtime/thread.hpp"
  36 #include "runtime/vmThread.hpp"
  37 #include "utilities/formatBuffer.hpp"
  38 #include "utilities/preserveException.hpp"
  39 
  40 #define ALL_JAVA_THREADS(X) for (JavaThread* X = Threads::first(); X; X = X->next())
  41 
  42 class HandshakeOperation: public StackObj {
  43 public:
  44   virtual void do_handshake(JavaThread* thread) = 0;
  45   virtual void cancel_handshake(JavaThread* thread) = 0;
  46 };
  47 
  48 class HandshakeThreadsOperation: public HandshakeOperation {
  49   Semaphore _done;
  50   ThreadClosure* _thread_cl;
  51 
  52 public:
  53   HandshakeThreadsOperation(ThreadClosure* cl) : _done(0), _thread_cl(cl) {}
  54   void do_handshake(JavaThread* thread);
  55   void cancel_handshake(JavaThread* thread) { _done.signal(); };
  56 
  57   bool thread_has_completed() { return _done.trywait(); }
  58 };
  59 
  60 class VM_Handshake: public VM_Operation {
  61   HandshakeThreadsOperation* const _op;
  62   const jlong _handshake_timeout;
  63  public:
  64   bool evaluate_at_safepoint() const { return false; }
  65 
  66   bool evaluate_concurrently() const { return false; }
  67 
  68  protected:
  69 
  70   VM_Handshake(HandshakeThreadsOperation* op) :
  71       _op(op),
  72       _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)) {}
  73 
  74   void set_handshake(JavaThread* target) {
  75     target->set_handshake_operation(_op);
  76   }
  77 
  78   bool poll_for_completed_thread() { return _op->thread_has_completed(); }
  79 
  80   bool handshake_has_timed_out(jlong start_time);
  81   static void handle_timeout();
  82 };
  83 
  84 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
  85   // Check if handshake operation has timed out
  86   if (_handshake_timeout > 0) {
  87     return os::elapsed_counter() >= (start_time + _handshake_timeout);
  88   }
  89   return false;
  90 }
  91 
  92 void VM_Handshake::handle_timeout() {
  93   LogStreamHandle(Warning, handshake) log_stream;
  94   MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
  95   ALL_JAVA_THREADS(thr) {
  96     if (thr->has_handshake()) {
  97       log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
  98       thr->print_thread_state_on(&log_stream);
  99     }
 100   }
 101   log_stream.flush();
 102   fatal("Handshake operation timed out");
 103 }
 104 
 105 
 106 class VM_HandshakeOneThread: public VM_Handshake {
 107   JavaThread* _target;
 108   bool _thread_alive;
 109  public:
 110   VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) :
 111     VM_Handshake(op), _target(target), _thread_alive(false) {}
 112 
 113   void doit() {
 114     TraceTime timer("Performing single-target operation (vmoperation doit)", TRACETIME_LOG(Info, handshake));
 115 
 116     {
 117       MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 118       if (Threads::includes(_target)) {
 119         set_handshake(_target);
 120         _thread_alive = true;
 121       }
 122     }
 123 
 124     if (!_thread_alive) {
 125       return;
 126     }
 127 
 128     if (!UseMembar) {
 129       os::serialize_thread_states();
 130     }
 131 
 132     log_trace(handshake)("Thread signaled, begin processing by VMThtread");
 133     jlong start_time = os::elapsed_counter();
 134     do {
 135       if (handshake_has_timed_out(start_time)) {
 136         handle_timeout();
 137       }
 138 
 139       MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 140       _target->handshake_process_by_vmthread();
 141 
 142     } while (!poll_for_completed_thread());
 143   }
 144 
 145   VMOp_Type type() const { return VMOp_HandshakeOneThread; }
 146 
 147   bool thread_alive() const { return _thread_alive; }
 148 };
 149 
 150 class VM_HandshakeAllThreads: public VM_Handshake {
 151  public:
 152   VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {}
 153 
 154   void doit() {
 155     TraceTime timer("Performing operation (vmoperation doit)", TRACETIME_LOG(Info, handshake));
 156 
 157     int number_of_threads_issued = -1;
 158     int number_of_threads_completed = 0;
 159     {
 160       MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 161       number_of_threads_issued = Threads::number_of_threads();
 162 
 163       ALL_JAVA_THREADS(thr) {
 164         set_handshake(thr);
 165       }
 166     }
 167 
 168     if (!UseMembar) {
 169       os::serialize_thread_states();
 170     }
 171 
 172     log_debug(handshake)("Threads signaled, begin processing blocked threads by VMThtread");
 173     const jlong start_time = os::elapsed_counter();
 174     do {
 175       // Check if handshake operation has timed out
 176       if (handshake_has_timed_out(start_time)) {
 177         handle_timeout();
 178       }
 179 
 180       // Have VM thread perform the handshake operation for blocked threads.
 181       // Observing a blocked state may of course be transient but the processing is guarded
 182       // by semaphores and we optimistically begin by working on the blocked threads
 183       {
 184           MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 185           ALL_JAVA_THREADS(thr) {
 186             thr->handshake_process_by_vmthread();
 187           }
 188       }
 189 
 190       while (poll_for_completed_thread()) {
 191         number_of_threads_completed++;
 192       }
 193 
 194     } while (number_of_threads_issued != number_of_threads_completed);
 195   }
 196 
 197   VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
 198 };
 199 
 200 class VM_HandshakeFallbackOperation : public VM_Operation {
 201   ThreadClosure* _thread_cl;
 202   Thread* _target_thread;
 203   bool _all_threads;
 204   bool _thread_alive;
 205 public:
 206   VM_HandshakeFallbackOperation(ThreadClosure* cl) :
 207       _thread_cl(cl), _target_thread(NULL), _all_threads(true), _thread_alive(true) {}
 208   VM_HandshakeFallbackOperation(ThreadClosure* cl, Thread* target) :
 209       _thread_cl(cl), _target_thread(target), _all_threads(false), _thread_alive(false) {}
 210 
 211   void doit() {
 212     ALL_JAVA_THREADS(t) {
 213       if (_all_threads || t == _target_thread) {
 214         if (t == _target_thread) {
 215           _thread_alive = true;
 216         }
 217         _thread_cl->do_thread(t);
 218       }
 219     }
 220   }
 221 
 222   VMOp_Type type() const { return VMOp_HandshakeFallback; }
 223   bool thread_alive() const { return _thread_alive; }
 224 };
 225 
 226 #undef ALL_JAVA_THREADS
 227 
 228 void HandshakeThreadsOperation::do_handshake(JavaThread* thread) {
 229   ResourceMark rm;
 230   FormatBufferResource message("Operation for thread " PTR_FORMAT ", is_vm_thread: %s",
 231                                p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()));
 232   TraceTime timer(message, TRACETIME_LOG(Debug, handshake, task));
 233   _thread_cl->do_thread(thread);
 234 
 235   // Use the semaphore to inform the VM thread that we have completed the operation
 236   _done.signal();
 237 }
 238 
 239 void Handshake::execute(ThreadClosure* thread_cl) {
 240   if (ThreadLocalHandshakes) {
 241     HandshakeThreadsOperation cto(thread_cl);
 242     VM_HandshakeAllThreads handshake(&cto);
 243     VMThread::execute(&handshake);
 244   } else {
 245     VM_HandshakeFallbackOperation op(thread_cl);
 246     VMThread::execute(&op);
 247   }
 248 }
 249 
 250 bool Handshake::execute(ThreadClosure* thread_cl, JavaThread* target) {
 251   if (ThreadLocalHandshakes) {
 252     HandshakeThreadsOperation cto(thread_cl);
 253     VM_HandshakeOneThread handshake(&cto, target);
 254     VMThread::execute(&handshake);
 255     return handshake.thread_alive();
 256   } else {
 257     VM_HandshakeFallbackOperation op(thread_cl, target);
 258     VMThread::execute(&op);
 259     return op.thread_alive();
 260   }
 261 }
 262 
 263 HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _vmthread_holds_semaphore(false), _thread_in_process_handshake(false) {}
 264 
 265 void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) {
 266   _operation = op;
 267   SafepointMechanism::arm_local_poll(target);
 268 }
 269 
 270 void HandshakeState::clear_handshake(JavaThread* target) {
 271   _operation = NULL;
 272   SafepointMechanism::disarm_local_poll(target);
 273 }
 274 
 275 void HandshakeState::process_self_inner(JavaThread* thread) {
 276   assert(Thread::current() == thread, "should call from thread");
 277   CautiouslyPreserveExceptionMark pem(thread);
 278   ThreadInVMForHandshake tivm(thread);
 279   if (!_semaphore.trywait()) {
 280     ThreadBlockInVM tbivm(thread);
 281     _semaphore.wait();
 282   }
 283   if (has_operation()) {
 284     HandshakeOperation* op = _operation;
 285     clear_handshake(thread);
 286     if (op != NULL) {
 287       op->do_handshake(thread);
 288     }
 289   }
 290   _semaphore.signal();
 291 }
 292 
 293 void HandshakeState::cancel_inner(JavaThread* thread) {
 294   assert(Thread::current() == thread, "should call from thread");
 295   assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
 296 #ifdef DEBUG
 297   {
 298     MutexLockerEx ml(Threads_lock,  Mutex::_no_safepoint_check_flag);
 299     assert(!Threads::includes(thread), "java thread must not be on threads list");
 300   }
 301 #endif
 302   HandshakeOperation* op = _operation;
 303   clear_handshake(thread);
 304   if (op != NULL) {
 305     op->cancel_handshake(thread);
 306   }
 307 }
 308 
 309 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) {
 310   return SafepointSynchronize::safepoint_safe(target, target->thread_state());
 311 }
 312 
 313 bool HandshakeState::claim_handshake_for_vmthread() {
 314   if (_semaphore.trywait()) {
 315     if (has_operation()) {
 316       _vmthread_holds_semaphore = true;
 317     } else {
 318       _semaphore.signal();
 319     }
 320   }
 321   return _vmthread_holds_semaphore;
 322 }
 323 
 324 void HandshakeState::process_by_vmthread(JavaThread* target) {
 325   assert(Thread::current()->is_VM_thread(), "should call from vm thread");
 326 
 327   if (!has_operation()) {
 328     // JT has already cleared its handshake
 329     return;
 330   }
 331 
 332   if (!vmthread_can_process_handshake(target)) {
 333     // JT is observed in an unsafe state, it must notice the handshake itself
 334     return;
 335   }
 336 
 337   // If we own the semaphore at this point and while owning the semaphore
 338   // can observe a safe state the thread cannot possibly continue without
 339   // getting caught by the semaphore.
 340   if (claim_handshake_for_vmthread() && vmthread_can_process_handshake(target)) {
 341     guarantee(!_semaphore.trywait(), "we should already own the semaphore");
 342 
 343     _operation->do_handshake(target);
 344     clear_handshake(target);
 345     _vmthread_holds_semaphore = false;
 346     // Release the thread
 347     _semaphore.signal();
 348   }
 349 }