1 /*
2  * Copyright (c) 2017, 2020, 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 
41 class HandshakeOperation: public StackObj {
42   HandshakeClosure* _handshake_cl;
43   int32_t _pending_threads;
44   bool _executed;
45   bool _is_direct;
46 public:
HandshakeOperation(HandshakeClosure * cl,bool is_direct=false)47   HandshakeOperation(HandshakeClosure* cl, bool is_direct = false) :
48     _handshake_cl(cl),
49     _pending_threads(1),
50     _executed(false),
51     _is_direct(is_direct) {}
52 
53   void do_handshake(JavaThread* thread);
is_completed()54   bool is_completed() {
55     int32_t val = Atomic::load(&_pending_threads);
56     assert(val >= 0, "_pending_threads=%d cannot be negative", val);
57     return val == 0;
58   }
add_target_count(int count)59   void add_target_count(int count) { Atomic::add(&_pending_threads, count, memory_order_relaxed); }
executed() const60   bool executed() const { return _executed; }
name()61   const char* name() { return _handshake_cl->name(); }
62 
is_direct()63   bool is_direct() { return _is_direct; }
64 };
65 
66 // Performing handshakes requires a custom yielding strategy because without it
67 // there is a clear performance regression vs plain spinning. We keep track of
68 // when we last saw progress by looking at why each targeted thread has not yet
69 // completed its handshake. After spinning for a while with no progress we will
70 // yield, but as long as there is progress, we keep spinning. Thus we avoid
71 // yielding when there is potential work to be done or the handshake is close
72 // to being finished.
73 class HandshakeSpinYield : public StackObj {
74  private:
75   jlong _start_time_ns;
76   jlong _last_spin_start_ns;
77   jlong _spin_time_ns;
78 
79   int _result_count[2][HandshakeState::_number_states];
80   int _prev_result_pos;
81 
prev_result_pos()82   int prev_result_pos() { return _prev_result_pos & 0x1; }
current_result_pos()83   int current_result_pos() { return (_prev_result_pos + 1) & 0x1; }
84 
wait_raw(jlong now)85   void wait_raw(jlong now) {
86     // We start with fine-grained nanosleeping until a millisecond has
87     // passed, at which point we resort to plain naked_short_sleep.
88     if (now - _start_time_ns < NANOSECS_PER_MILLISEC) {
89       os::naked_short_nanosleep(10 * (NANOUNITS / MICROUNITS));
90     } else {
91       os::naked_short_sleep(1);
92     }
93   }
94 
wait_blocked(JavaThread * self,jlong now)95   void wait_blocked(JavaThread* self, jlong now) {
96     ThreadBlockInVM tbivm(self);
97     wait_raw(now);
98   }
99 
state_changed()100   bool state_changed() {
101     for (int i = 0; i < HandshakeState::_number_states; i++) {
102       if (_result_count[0][i] != _result_count[1][i]) {
103         return true;
104       }
105     }
106     return false;
107   }
108 
reset_state()109   void reset_state() {
110     _prev_result_pos++;
111     for (int i = 0; i < HandshakeState::_number_states; i++) {
112       _result_count[current_result_pos()][i] = 0;
113     }
114   }
115 
116  public:
HandshakeSpinYield(jlong start_time)117   HandshakeSpinYield(jlong start_time) :
118     _start_time_ns(start_time), _last_spin_start_ns(start_time),
119     _spin_time_ns(0), _result_count(), _prev_result_pos(0) {
120 
121     const jlong max_spin_time_ns = 100 /* us */ * (NANOUNITS / MICROUNITS);
122     int free_cpus = os::active_processor_count() - 1;
123     _spin_time_ns = (5 /* us */ * (NANOUNITS / MICROUNITS)) * free_cpus; // zero on UP
124     _spin_time_ns = _spin_time_ns > max_spin_time_ns ? max_spin_time_ns : _spin_time_ns;
125   }
126 
add_result(HandshakeState::ProcessResult pr)127   void add_result(HandshakeState::ProcessResult pr) {
128     _result_count[current_result_pos()][pr]++;
129   }
130 
process()131   void process() {
132     jlong now = os::javaTimeNanos();
133     if (state_changed()) {
134       reset_state();
135       // We spin for x amount of time since last state change.
136       _last_spin_start_ns = now;
137       return;
138     }
139     jlong wait_target = _last_spin_start_ns + _spin_time_ns;
140     if (wait_target < now) {
141       // On UP this is always true.
142       Thread* self = Thread::current();
143       if (self->is_Java_thread()) {
144         wait_blocked((JavaThread*)self, now);
145       } else {
146         wait_raw(now);
147       }
148       _last_spin_start_ns = os::javaTimeNanos();
149     }
150     reset_state();
151   }
152 };
153 
154 class VM_Handshake: public VM_Operation {
155   const jlong _handshake_timeout;
156  public:
evaluate_at_safepoint() const157   bool evaluate_at_safepoint() const { return false; }
158 
159  protected:
160   HandshakeOperation* const _op;
161 
VM_Handshake(HandshakeOperation * op)162   VM_Handshake(HandshakeOperation* op) :
163       _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)), _op(op) {}
164 
165   bool handshake_has_timed_out(jlong start_time);
166   static void handle_timeout();
167 };
168 
handshake_has_timed_out(jlong start_time)169 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
170   // Check if handshake operation has timed out
171   if (_handshake_timeout > 0) {
172     return os::javaTimeNanos() >= (start_time + _handshake_timeout);
173   }
174   return false;
175 }
176 
handle_timeout()177 void VM_Handshake::handle_timeout() {
178   LogStreamHandle(Warning, handshake) log_stream;
179   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
180     if (thr->has_handshake()) {
181       log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
182       thr->print_thread_state_on(&log_stream);
183     }
184   }
185   log_stream.flush();
186   fatal("Handshake operation timed out");
187 }
188 
log_handshake_info(jlong start_time_ns,const char * name,int targets,int vmt_executed,const char * extra=NULL)189 static void log_handshake_info(jlong start_time_ns, const char* name, int targets, int vmt_executed, const char* extra = NULL) {
190   if (start_time_ns != 0) {
191     jlong completion_time = os::javaTimeNanos() - start_time_ns;
192     log_info(handshake)("Handshake \"%s\", Targeted threads: %d, Executed by targeted threads: %d, Total completion time: " JLONG_FORMAT " ns%s%s",
193                         name, targets,
194                         targets - vmt_executed,
195                         completion_time,
196                         extra != NULL ? ", " : "",
197                         extra != NULL ? extra : "");
198   }
199 }
200 
201 class VM_HandshakeOneThread: public VM_Handshake {
202   JavaThread* _target;
203  public:
VM_HandshakeOneThread(HandshakeOperation * op,JavaThread * target)204   VM_HandshakeOneThread(HandshakeOperation* op, JavaThread* target) :
205     VM_Handshake(op), _target(target) {}
206 
doit()207   void doit() {
208     jlong start_time_ns = os::javaTimeNanos();
209 
210     ThreadsListHandle tlh;
211     if (tlh.includes(_target)) {
212       _target->set_handshake_operation(_op);
213     } else {
214       log_handshake_info(start_time_ns, _op->name(), 0, 0, "(thread dead)");
215       return;
216     }
217 
218     log_trace(handshake)("JavaThread " INTPTR_FORMAT " signaled, begin attempt to process by VMThtread", p2i(_target));
219     HandshakeState::ProcessResult pr = HandshakeState::_no_operation;
220     HandshakeSpinYield hsy(start_time_ns);
221     do {
222       if (handshake_has_timed_out(start_time_ns)) {
223         handle_timeout();
224       }
225       pr = _target->handshake_try_process(_op);
226       hsy.add_result(pr);
227       hsy.process();
228     } while (!_op->is_completed());
229 
230     // This pairs up with the release store in do_handshake(). It prevents future
231     // loads from floating above the load of _pending_threads in is_completed()
232     // and thus prevents reading stale data modified in the handshake closure
233     // by the Handshakee.
234     OrderAccess::acquire();
235 
236     log_handshake_info(start_time_ns, _op->name(), 1, (pr == HandshakeState::_success) ? 1 : 0);
237   }
238 
type() const239   VMOp_Type type() const { return VMOp_HandshakeOneThread; }
240 
executed() const241   bool executed() const { return _op->executed(); }
242 };
243 
244 class VM_HandshakeAllThreads: public VM_Handshake {
245  public:
VM_HandshakeAllThreads(HandshakeOperation * op)246   VM_HandshakeAllThreads(HandshakeOperation* op) : VM_Handshake(op) {}
247 
doit()248   void doit() {
249     jlong start_time_ns = os::javaTimeNanos();
250     int handshake_executed_by_vm_thread = 0;
251 
252     JavaThreadIteratorWithHandle jtiwh;
253     int number_of_threads_issued = 0;
254     for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
255       thr->set_handshake_operation(_op);
256       number_of_threads_issued++;
257     }
258 
259     if (number_of_threads_issued < 1) {
260       log_handshake_info(start_time_ns, _op->name(), 0, 0);
261       return;
262     }
263     // _op was created with a count == 1 so don't double count.
264     _op->add_target_count(number_of_threads_issued - 1);
265 
266     log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread");
267     HandshakeSpinYield hsy(start_time_ns);
268     do {
269       // Check if handshake operation has timed out
270       if (handshake_has_timed_out(start_time_ns)) {
271         handle_timeout();
272       }
273 
274       // Have VM thread perform the handshake operation for blocked threads.
275       // Observing a blocked state may of course be transient but the processing is guarded
276       // by semaphores and we optimistically begin by working on the blocked threads
277       jtiwh.rewind();
278       for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
279         // A new thread on the ThreadsList will not have an operation,
280         // hence it is skipped in handshake_try_process.
281         HandshakeState::ProcessResult pr = thr->handshake_try_process(_op);
282         if (pr == HandshakeState::_success) {
283           handshake_executed_by_vm_thread++;
284         }
285         hsy.add_result(pr);
286       }
287       hsy.process();
288     } while (!_op->is_completed());
289 
290     // This pairs up with the release store in do_handshake(). It prevents future
291     // loads from floating above the load of _pending_threads in is_completed()
292     // and thus prevents reading stale data modified in the handshake closure
293     // by the Handshakee.
294     OrderAccess::acquire();
295 
296     log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, handshake_executed_by_vm_thread);
297   }
298 
type() const299   VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
300 };
301 
do_handshake(JavaThread * thread)302 void HandshakeOperation::do_handshake(JavaThread* thread) {
303   jlong start_time_ns = 0;
304   if (log_is_enabled(Debug, handshake, task)) {
305     start_time_ns = os::javaTimeNanos();
306   }
307 
308   // Only actually execute the operation for non terminated threads.
309   if (!thread->is_terminated()) {
310     _handshake_cl->do_thread(thread);
311     _executed = true;
312   }
313 
314   if (start_time_ns != 0) {
315     jlong completion_time = os::javaTimeNanos() - start_time_ns;
316     log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns",
317                                name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time);
318   }
319 
320   // Inform VMThread/Handshaker that we have completed the operation.
321   // When this is executed by the Handshakee we need a release store
322   // here to make sure memory operations executed in the handshake
323   // closure are visible to the VMThread/Handshaker after it reads
324   // that the operation has completed.
325   Atomic::dec(&_pending_threads, memory_order_release);
326 
327   // It is no longer safe to refer to 'this' as the VMThread/Handshaker may have destroyed this operation
328 }
329 
execute(HandshakeClosure * thread_cl)330 void Handshake::execute(HandshakeClosure* thread_cl) {
331   HandshakeOperation cto(thread_cl);
332   VM_HandshakeAllThreads handshake(&cto);
333   VMThread::execute(&handshake);
334 }
335 
execute(HandshakeClosure * thread_cl,JavaThread * target)336 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) {
337   HandshakeOperation cto(thread_cl);
338   VM_HandshakeOneThread handshake(&cto, target);
339   VMThread::execute(&handshake);
340   return handshake.executed();
341 }
342 
execute_direct(HandshakeClosure * thread_cl,JavaThread * target)343 bool Handshake::execute_direct(HandshakeClosure* thread_cl, JavaThread* target) {
344   JavaThread* self = JavaThread::current();
345   HandshakeOperation op(thread_cl, /*is_direct*/ true);
346 
347   jlong start_time_ns = os::javaTimeNanos();
348 
349   ThreadsListHandle tlh;
350   if (tlh.includes(target)) {
351     target->set_handshake_operation(&op);
352   } else {
353     log_handshake_info(start_time_ns, op.name(), 0, 0, "(thread dead)");
354     return false;
355   }
356 
357   HandshakeState::ProcessResult pr =  HandshakeState::_no_operation;
358   HandshakeSpinYield hsy(start_time_ns);
359   while (!op.is_completed()) {
360     HandshakeState::ProcessResult pr = target->handshake_try_process(&op);
361     hsy.add_result(pr);
362     // Check for pending handshakes to avoid possible deadlocks where our
363     // target is trying to handshake us.
364     if (SafepointMechanism::should_block(self)) {
365       ThreadBlockInVM tbivm(self);
366     }
367     hsy.process();
368   }
369 
370   // This pairs up with the release store in do_handshake(). It prevents future
371   // loads from floating above the load of _pending_threads in is_completed()
372   // and thus prevents reading stale data modified in the handshake closure
373   // by the Handshakee.
374   OrderAccess::acquire();
375 
376   log_handshake_info(start_time_ns, op.name(), 1,  (pr == HandshakeState::_success) ? 1 : 0);
377 
378   return op.executed();
379 }
380 
HandshakeState()381 HandshakeState::HandshakeState() :
382   _operation(NULL),
383   _operation_direct(NULL),
384   _handshake_turn_sem(1),
385   _processing_sem(1),
386   _thread_in_process_handshake(false)
387 {
388   DEBUG_ONLY(_active_handshaker = NULL;)
389 }
390 
set_operation(HandshakeOperation * op)391 void HandshakeState::set_operation(HandshakeOperation* op) {
392   if (!op->is_direct()) {
393     assert(Thread::current()->is_VM_thread(), "should be the VMThread");
394     _operation = op;
395   } else {
396     assert(Thread::current()->is_Java_thread(), "should be a JavaThread");
397     // Serialize direct handshakes so that only one proceeds at a time for a given target
398     _handshake_turn_sem.wait_with_safepoint_check(JavaThread::current());
399     _operation_direct = op;
400   }
401   SafepointMechanism::arm_local_poll_release(_handshakee);
402 }
403 
clear_handshake(bool is_direct)404 void HandshakeState::clear_handshake(bool is_direct) {
405   if (!is_direct) {
406     _operation = NULL;
407   } else {
408     _operation_direct = NULL;
409     _handshake_turn_sem.signal();
410   }
411 }
412 
process_self_inner()413 void HandshakeState::process_self_inner() {
414   assert(Thread::current() == _handshakee, "should call from _handshakee");
415   assert(!_handshakee->is_terminated(), "should not be a terminated thread");
416   assert(_handshakee->thread_state() != _thread_blocked, "should not be in a blocked state");
417   assert(_handshakee->thread_state() != _thread_in_native, "should not be in native");
418   JavaThread* self = _handshakee;
419 
420   do {
421     ThreadInVMForHandshake tivm(self);
422     if (!_processing_sem.trywait()) {
423       _processing_sem.wait_with_safepoint_check(self);
424     }
425     if (has_operation()) {
426       HandleMark hm(self);
427       CautiouslyPreserveExceptionMark pem(self);
428       HandshakeOperation * op = _operation;
429       if (op != NULL) {
430         // Disarm before executing the operation
431         clear_handshake(/*is_direct*/ false);
432         op->do_handshake(self);
433       }
434       op = _operation_direct;
435       if (op != NULL) {
436         // Disarm before executing the operation
437         clear_handshake(/*is_direct*/ true);
438         op->do_handshake(self);
439       }
440     }
441     _processing_sem.signal();
442   } while (has_operation());
443 }
444 
can_process_handshake()445 bool HandshakeState::can_process_handshake() {
446   // handshake_safe may only be called with polls armed.
447   // Handshaker controls this by first claiming the handshake via claim_handshake().
448   return SafepointSynchronize::handshake_safe(_handshakee);
449 }
450 
possibly_can_process_handshake()451 bool HandshakeState::possibly_can_process_handshake() {
452   // Note that this method is allowed to produce false positives.
453   if (_handshakee->is_ext_suspended()) {
454     return true;
455   }
456   if (_handshakee->is_terminated()) {
457     return true;
458   }
459   switch (_handshakee->thread_state()) {
460   case _thread_in_native:
461     // native threads are safe if they have no java stack or have walkable stack
462     return !_handshakee->has_last_Java_frame() || _handshakee->frame_anchor()->walkable();
463 
464   case _thread_blocked:
465     return true;
466 
467   default:
468     return false;
469   }
470 }
471 
claim_handshake(bool is_direct)472 bool HandshakeState::claim_handshake(bool is_direct) {
473   if (!_processing_sem.trywait()) {
474     return false;
475   }
476   if (has_specific_operation(is_direct)){
477     return true;
478   }
479   _processing_sem.signal();
480   return false;
481 }
482 
try_process(HandshakeOperation * op)483 HandshakeState::ProcessResult HandshakeState::try_process(HandshakeOperation* op) {
484   bool is_direct = op->is_direct();
485 
486   if (!has_specific_operation(is_direct)){
487     // JT has already cleared its handshake
488     return _no_operation;
489   }
490 
491   if (!possibly_can_process_handshake()) {
492     // JT is observed in an unsafe state, it must notice the handshake itself
493     return _not_safe;
494   }
495 
496   // Claim the semaphore if there still an operation to be executed.
497   if (!claim_handshake(is_direct)) {
498     return _state_busy;
499   }
500 
501   // Check if the handshake operation is the same as the one we meant to execute. The
502   // handshake could have been already processed by the handshakee and a new handshake
503   // by another JavaThread might be in progress.
504   if (is_direct && op != _operation_direct) {
505     _processing_sem.signal();
506     return _no_operation;
507   }
508 
509   // If we own the semaphore at this point and while owning the semaphore
510   // can observe a safe state the thread cannot possibly continue without
511   // getting caught by the semaphore.
512   ProcessResult pr = _not_safe;
513   if (can_process_handshake()) {
514     guarantee(!_processing_sem.trywait(), "we should already own the semaphore");
515     log_trace(handshake)("Processing handshake by %s", Thread::current()->is_VM_thread() ? "VMThread" : "Handshaker");
516     DEBUG_ONLY(_active_handshaker = Thread::current();)
517     op->do_handshake(_handshakee);
518     DEBUG_ONLY(_active_handshaker = NULL;)
519     // Disarm after we have executed the operation.
520     clear_handshake(is_direct);
521     pr = _success;
522   }
523 
524   // Release the thread
525   _processing_sem.signal();
526 
527   return pr;
528 }
529