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