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   HandshakeClosure* _handshake_cl;
  49 
  50 public:
  51   HandshakeThreadsOperation(HandshakeClosure* cl) : _handshake_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 // Performing handshakes requires a custom yielding strategy because without it
  65 // there is a clear performance regression vs plain spinning. We keep track of
  66 // when we last saw progress by looking at why each targeted thread has not yet
  67 // completed its handshake. After spinning for a while with no progress we will
  68 // yield, but as long as there is progress, we keep spinning. Thus we avoid
  69 // yielding when there is potential work to be done or the handshake is close
  70 // to being finished.
  71 class HandshakeSpinYield : public StackObj {
  72  private:
  73   jlong _start_time_ns;
  74   jlong _last_spin_start_ns;
  75   jlong _spin_time_ns;
  76 
  77   int _result_count[2][HandshakeState::_number_states];
  78   int _prev_result_pos;
  79 
  80   int prev_result_pos() { return _prev_result_pos & 0x1; }
  81   int current_result_pos() { return (_prev_result_pos + 1) & 0x1; }
  82 
  83   void wait_raw(jlong now) {
  84     // We start with fine-grained nanosleeping until a millisecond has
  85     // passed, at which point we resort to plain naked_short_sleep.
  86     if (now - _start_time_ns < NANOSECS_PER_MILLISEC) {
  87       os::naked_short_nanosleep(10 * (NANOUNITS / MICROUNITS));
  88     } else {
  89       os::naked_short_sleep(1);
  90     }
  91   }
  92 
  93   void wait_blocked(JavaThread* self, jlong now) {
  94     ThreadBlockInVM tbivm(self);
  95     wait_raw(now);
  96   }
  97 
  98   bool state_changed() {
  99     for (int i = 0; i < HandshakeState::_number_states; i++) {
 100       if (_result_count[0][i] != _result_count[1][i]) {
 101         return true;
 102       }
 103     }
 104     return false;
 105   }
 106 
 107   void reset_state() {
 108     _prev_result_pos++;
 109     for (int i = 0; i < HandshakeState::_number_states; i++) {
 110       _result_count[current_result_pos()][i] = 0;
 111     }
 112   }
 113 
 114  public:
 115   HandshakeSpinYield(jlong start_time) :
 116     _start_time_ns(start_time), _last_spin_start_ns(start_time),
 117     _spin_time_ns(0), _result_count(), _prev_result_pos(0) {
 118 
 119     const jlong max_spin_time_ns = 100 /* us */ * (NANOUNITS / MICROUNITS);
 120     int free_cpus = os::active_processor_count() - 1;
 121     _spin_time_ns = (5 /* us */ * (NANOUNITS / MICROUNITS)) * free_cpus; // zero on UP
 122     _spin_time_ns = _spin_time_ns > max_spin_time_ns ? max_spin_time_ns : _spin_time_ns;
 123   }
 124 
 125   void add_result(HandshakeState::ProcessResult pr) {
 126     _result_count[current_result_pos()][pr]++;
 127   }
 128 
 129   void process() {
 130     jlong now = os::javaTimeNanos();
 131     if (state_changed()) {
 132       reset_state();
 133       // We spin for x amount of time since last state change.
 134       _last_spin_start_ns = now;
 135       return;
 136     }
 137     jlong wait_target = _last_spin_start_ns + _spin_time_ns;
 138     if (wait_target < now) {
 139       // On UP this is always true.
 140       Thread* self = Thread::current();
 141       if (self->is_Java_thread()) {
 142         wait_blocked((JavaThread*)self, now);
 143       } else {
 144         wait_raw(now);
 145       }
 146       _last_spin_start_ns = os::javaTimeNanos();
 147     }
 148     reset_state();
 149   }
 150 };
 151 
 152 class VM_Handshake: public VM_Operation {
 153   const jlong _handshake_timeout;
 154  public:
 155   bool evaluate_at_safepoint() const { return false; }
 156 
 157   bool evaluate_concurrently() const { return false; }
 158 
 159  protected:
 160   HandshakeThreadsOperation* const _op;
 161 
 162   VM_Handshake(HandshakeThreadsOperation* op) :
 163       _op(op),
 164       _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)) {}
 165 
 166   void set_handshake(JavaThread* target) {
 167     target->set_handshake_operation(_op);
 168   }
 169 
 170   // This method returns true for threads completed their operation
 171   // and true for threads canceled their operation.
 172   // A cancellation can happen if the thread is exiting.
 173   bool poll_for_completed_thread() { return _op->thread_has_completed(); }
 174 
 175   bool handshake_has_timed_out(jlong start_time);
 176   static void handle_timeout();
 177 };
 178 
 179 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
 180   // Check if handshake operation has timed out
 181   if (_handshake_timeout > 0) {
 182     return os::javaTimeNanos() >= (start_time + _handshake_timeout);
 183   }
 184   return false;
 185 }
 186 
 187 void VM_Handshake::handle_timeout() {
 188   LogStreamHandle(Warning, handshake) log_stream;
 189   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
 190     if (thr->has_handshake()) {
 191       log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
 192       thr->print_thread_state_on(&log_stream);
 193     }
 194   }
 195   log_stream.flush();
 196   fatal("Handshake operation timed out");
 197 }
 198 
 199 class VM_HandshakeOneThread: public VM_Handshake {
 200   JavaThread* _target;
 201   bool _thread_alive;
 202  public:
 203   VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) :
 204     VM_Handshake(op), _target(target), _thread_alive(false) {}
 205 
 206   void doit() {
 207     jlong start_time_ns = os::javaTimeNanos();
 208     DEBUG_ONLY(_op->check_state();)
 209     TraceTime timer("Performing single-target operation (vmoperation doit)", TRACETIME_LOG(Info, handshake));
 210 
 211     ThreadsListHandle tlh;
 212     if (tlh.includes(_target)) {
 213       set_handshake(_target);
 214       _thread_alive = true;
 215     } else {
 216       return;
 217     }
 218 
 219     if (!UseMembar) {
 220       os::serialize_thread_states();
 221     }
 222 
 223     log_trace(handshake)("Thread signaled, begin processing by VMThtread");
 224     HandshakeState::ProcessResult pr = HandshakeState::_no_operation;
 225     HandshakeSpinYield hsy(start_time_ns);
 226     do {
 227       if (handshake_has_timed_out(start_time_ns)) {
 228         handle_timeout();
 229       }
 230 
 231       // We need to re-think this with SMR ThreadsList.
 232       // There is an assumption in the code that the Threads_lock should be
 233       // locked during certain phases.
 234       {
 235         MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 236         pr = _target->handshake_process_by_vmthread();
 237       }
 238       hsy.add_result(pr);
 239       hsy.process();
 240     } while (!poll_for_completed_thread());
 241     DEBUG_ONLY(_op->check_state();)
 242   }
 243 
 244   VMOp_Type type() const { return VMOp_HandshakeOneThread; }
 245 
 246   bool thread_alive() const { return _thread_alive; }
 247 };
 248 
 249 class VM_HandshakeAllThreads: public VM_Handshake {
 250  public:
 251   VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {}
 252 
 253   void doit() {
 254     jlong start_time_ns = os::javaTimeNanos();
 255     DEBUG_ONLY(_op->check_state();)
 256     TraceTime timer("Performing operation (vmoperation doit)", TRACETIME_LOG(Info, handshake));
 257 
 258     JavaThreadIteratorWithHandle jtiwh;
 259     int number_of_threads_issued = 0;
 260     for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 261       set_handshake(thr);
 262       number_of_threads_issued++;
 263     }
 264 
 265     if (number_of_threads_issued < 1) {
 266       log_debug(handshake)("No threads to handshake.");
 267       return;
 268     }
 269 
 270     if (!UseMembar) {
 271       os::serialize_thread_states();
 272     }
 273 
 274     log_debug(handshake)("Threads signaled, begin processing blocked threads by VMThtread");
 275     HandshakeSpinYield hsy(start_time_ns);
 276     int number_of_threads_completed = 0;
 277     do {
 278       // Check if handshake operation has timed out
 279       if (handshake_has_timed_out(start_time_ns)) {
 280         handle_timeout();
 281       }
 282 
 283       // Have VM thread perform the handshake operation for blocked threads.
 284       // Observing a blocked state may of course be transient but the processing is guarded
 285       // by semaphores and we optimistically begin by working on the blocked threads
 286       {
 287           // We need to re-think this with SMR ThreadsList.
 288           // There is an assumption in the code that the Threads_lock should
 289           // be locked during certain phases.
 290           jtiwh.rewind();
 291           MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 292           for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 293             // A new thread on the ThreadsList will not have an operation,
 294             // hence it is skipped in handshake_process_by_vmthread.
 295             HandshakeState::ProcessResult pr = thr->handshake_process_by_vmthread();
 296             hsy.add_result(pr);
 297           }
 298           hsy.process();
 299       }
 300 
 301       while (poll_for_completed_thread()) {
 302         // Includes canceled operations by exiting threads.
 303         number_of_threads_completed++;
 304       }
 305 
 306     } while (number_of_threads_issued > number_of_threads_completed);
 307     assert(number_of_threads_issued == number_of_threads_completed, "Must be the same");
 308     DEBUG_ONLY(_op->check_state();)
 309   }
 310 
 311   VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
 312 };
 313 
 314 class VM_HandshakeFallbackOperation : public VM_Operation {
 315   HandshakeClosure* _handshake_cl;
 316   Thread* _target_thread;
 317   bool _all_threads;
 318   bool _thread_alive;
 319 public:
 320   VM_HandshakeFallbackOperation(HandshakeClosure* cl) :
 321       _handshake_cl(cl), _target_thread(NULL), _all_threads(true) {}
 322   VM_HandshakeFallbackOperation(HandshakeClosure* cl, Thread* target) :
 323       _handshake_cl(cl), _target_thread(target), _all_threads(false) {}
 324 
 325   void doit() {
 326     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
 327       if (_all_threads || t == _target_thread) {
 328         if (t == _target_thread) {
 329           _thread_alive = true;
 330         }
 331         _handshake_cl->do_thread(t);
 332       }
 333     }
 334   }
 335 
 336   VMOp_Type type() const { return VMOp_HandshakeFallback; }
 337   bool thread_alive() const { return _thread_alive; }
 338 };
 339 
 340 void HandshakeThreadsOperation::do_handshake(JavaThread* thread) {
 341   ResourceMark rm;
 342   FormatBufferResource message("Operation for thread " PTR_FORMAT ", is_vm_thread: %s",
 343                                p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()));
 344   TraceTime timer(message, TRACETIME_LOG(Debug, handshake, task));
 345 
 346   // Only actually execute the operation for non terminated threads.
 347   if (!thread->is_terminated()) {
 348     _handshake_cl->do_thread(thread);
 349   }
 350 
 351   // Use the semaphore to inform the VM thread that we have completed the operation
 352   _done.signal();
 353 }
 354 
 355 void Handshake::execute(HandshakeClosure* thread_cl) {
 356   if (ThreadLocalHandshakes) {
 357     HandshakeThreadsOperation cto(thread_cl);
 358     VM_HandshakeAllThreads handshake(&cto);
 359     VMThread::execute(&handshake);
 360   } else {
 361     VM_HandshakeFallbackOperation op(thread_cl);
 362     VMThread::execute(&op);
 363   }
 364 }
 365 
 366 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) {
 367   if (ThreadLocalHandshakes) {
 368     HandshakeThreadsOperation cto(thread_cl);
 369     VM_HandshakeOneThread handshake(&cto, target);
 370     VMThread::execute(&handshake);
 371     return handshake.thread_alive();
 372   } else {
 373     VM_HandshakeFallbackOperation op(thread_cl, target);
 374     VMThread::execute(&op);
 375     return op.thread_alive();
 376   }
 377 }
 378 
 379 HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _thread_in_process_handshake(false) {}
 380 
 381 void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) {
 382   _operation = op;
 383   SafepointMechanism::arm_local_poll_release(target);
 384 }
 385 
 386 void HandshakeState::clear_handshake(JavaThread* target) {
 387   _operation = NULL;
 388   SafepointMechanism::disarm_local_poll_release(target);
 389 }
 390 
 391 void HandshakeState::process_self_inner(JavaThread* thread) {
 392   assert(Thread::current() == thread, "should call from thread");
 393   assert(!thread->is_terminated(), "should not be a terminated thread");
 394 
 395   ThreadInVMForHandshake tivm(thread);
 396   if (!_semaphore.trywait()) {
 397     _semaphore.wait_with_safepoint_check(thread);
 398   }
 399   HandshakeOperation* op = OrderAccess::load_acquire(&_operation);
 400   if (op != NULL) {
 401     HandleMark hm(thread);
 402     CautiouslyPreserveExceptionMark pem(thread);
 403     // Disarm before execute the operation
 404     clear_handshake(thread);
 405     op->do_handshake(thread);
 406   }
 407   _semaphore.signal();
 408 }
 409 
 410 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) {
 411   // SafepointSynchronize::safepoint_safe() does not consider an externally
 412   // suspended thread to be safe. However, this function must be called with
 413   // the Threads_lock held so an externally suspended thread cannot be
 414   // resumed thus it is safe.
 415   assert(Threads_lock->owned_by_self(), "Not holding Threads_lock.");
 416   return SafepointSynchronize::safepoint_safe(target, target->thread_state()) ||
 417          target->is_ext_suspended() || target->is_terminated();
 418 }
 419 
 420 static bool possibly_vmthread_can_process_handshake(JavaThread* target) {
 421   // An externally suspended thread cannot be resumed while the
 422   // Threads_lock is held so it is safe.
 423   // Note that this method is allowed to produce false positives.
 424   assert(Threads_lock->owned_by_self(), "Not holding Threads_lock.");
 425   if (target->is_ext_suspended()) {
 426     return true;
 427   }
 428   if (target->is_terminated()) {
 429     return true;
 430   }
 431   switch (target->thread_state()) {
 432   case _thread_in_native:
 433     // native threads are safe if they have no java stack or have walkable stack
 434     return !target->has_last_Java_frame() || target->frame_anchor()->walkable();
 435 
 436   case _thread_blocked:
 437     return true;
 438 
 439   default:
 440     return false;
 441   }
 442 }
 443 
 444 bool HandshakeState::claim_handshake_for_vmthread() {
 445   if (!_semaphore.trywait()) {
 446     return false;
 447   }
 448   if (has_operation()) {
 449     return true;
 450   }
 451   _semaphore.signal();
 452   return false;
 453 }
 454 
 455 HandshakeState::ProcessResult HandshakeState::process_by_vmthread(JavaThread* target) {
 456   assert(Thread::current()->is_VM_thread(), "should call from vm thread");
 457   // Threads_lock must be held here, but that is assert()ed in
 458   // possibly_vmthread_can_process_handshake().
 459 
 460   if (!has_operation()) {
 461     // JT has already cleared its handshake
 462     return _no_operation;
 463   }
 464 
 465   if (!possibly_vmthread_can_process_handshake(target)) {
 466     // JT is observed in an unsafe state, it must notice the handshake itself
 467     return _not_safe;
 468   }
 469 
 470   // Claim the semaphore if there still an operation to be executed.
 471   if (!claim_handshake_for_vmthread()) {
 472     return _state_busy;
 473   }
 474 
 475   // If we own the semaphore at this point and while owning the semaphore
 476   // can observe a safe state the thread cannot possibly continue without
 477   // getting caught by the semaphore.
 478   ProcessResult pr = _not_safe;
 479   if (vmthread_can_process_handshake(target)) {
 480     guarantee(!_semaphore.trywait(), "we should already own the semaphore");
 481     _operation->do_handshake(target);
 482     // Disarm after VM thread have executed the operation.
 483     clear_handshake(target);
 484     // Release the thread
 485     pr = _success;
 486   }
 487 
 488   _semaphore.signal();
 489   return pr;
 490 }