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 bool evaluate_at_safepoint() const { return false; }
69
70 bool evaluate_concurrently() const { return false; }
71
72 protected:
73 HandshakeThreadsOperation* const _op;
74
75 VM_Handshake(HandshakeThreadsOperation* op) :
76 _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)), _op(op) {}
77
78 void set_handshake(JavaThread* target) {
79 target->set_handshake_operation(_op);
80 }
81
82 // This method returns true for threads completed their operation
83 // and true for threads canceled their operation.
84 // A cancellation can happen if the thread is exiting.
85 bool poll_for_completed_thread() { return _op->thread_has_completed(); }
86
87 bool handshake_has_timed_out(jlong start_time);
88 static void handle_timeout();
89 };
90
91 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
92 // Check if handshake operation has timed out
93 if (_handshake_timeout > 0) {
94 return os::elapsed_counter() >= (start_time + _handshake_timeout);
95 }
96 return false;
97 }
98
99 void VM_Handshake::handle_timeout() {
100 LogStreamHandle(Warning, handshake) log_stream;
101 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
102 if (thr->has_handshake()) {
103 log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
104 thr->print_thread_state_on(&log_stream);
105 }
106 }
107 log_stream.flush();
108 fatal("Handshake operation timed out");
109 }
110
111 class VM_HandshakeOneThread: public VM_Handshake {
112 JavaThread* _target;
113 public:
114 VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) :
115 VM_Handshake(op), _target(target) {}
116
117 void doit() {
118 DEBUG_ONLY(_op->check_state();)
119 TraceTime timer("Finished executing single-target operation (VM_HandshakeOneThread::doit)", TRACETIME_LOG(Info, handshake));
120
121 ThreadsListHandle tlh;
122 if (tlh.includes(_target)) {
123 set_handshake(_target);
124 } else {
125 log_trace(handshake)("JavaThread " INTPTR_FORMAT " is not alive", p2i(_target));
126 return;
127 }
128
129 log_trace(handshake)("JavaThread " INTPTR_FORMAT " signaled, begin attempt to process by VMThtread", p2i(_target));
130 jlong start_time = os::elapsed_counter();
131 do {
132 if (handshake_has_timed_out(start_time)) {
133 handle_timeout();
134 }
135
136 // We need to re-think this with SMR ThreadsList.
137 // There is an assumption in the code that the Threads_lock should be
138 // locked during certain phases.
139 {
140 MutexLocker ml(Threads_lock);
141 _target->handshake_process_by_vmthread();
142 }
143 } while (!poll_for_completed_thread());
144 DEBUG_ONLY(_op->check_state();)
145 }
146
147 VMOp_Type type() const { return VMOp_HandshakeOneThread; }
148
149 bool executed() const { return _op->executed(); }
150 };
151
152 class VM_HandshakeAllThreads: public VM_Handshake {
153 public:
154 VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {}
155
156 void doit() {
157 DEBUG_ONLY(_op->check_state();)
158 TraceTime timer("Finished executing multi-target operation (VM_HandshakeAllThreads::doit)", TRACETIME_LOG(Info, handshake));
159
160 JavaThreadIteratorWithHandle jtiwh;
161 int number_of_threads_issued = 0;
162 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
163 set_handshake(thr);
164 number_of_threads_issued++;
165 }
166
167 if (number_of_threads_issued < 1) {
168 log_debug(handshake)("No threads to handshake.");
169 return;
170 }
171
172 log_debug(handshake)("Threads signaled, begin processing blocked threads by VMThtread");
173 const jlong start_time = os::elapsed_counter();
174 int number_of_threads_completed = 0;
175 do {
176 // Check if handshake operation has timed out
177 if (handshake_has_timed_out(start_time)) {
178 handle_timeout();
179 }
180
181 // Have VM thread perform the handshake operation for blocked threads.
182 // Observing a blocked state may of course be transient but the processing is guarded
183 // by semaphores and we optimistically begin by working on the blocked threads
184 {
185 // We need to re-think this with SMR ThreadsList.
186 // There is an assumption in the code that the Threads_lock should
187 // be locked during certain phases.
188 jtiwh.rewind();
189 MutexLocker ml(Threads_lock);
190 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
191 // A new thread on the ThreadsList will not have an operation,
192 // hence it is skipped in handshake_process_by_vmthread.
193 thr->handshake_process_by_vmthread();
194 }
195 }
196
197 while (poll_for_completed_thread()) {
198 // Includes canceled operations by exiting threads.
199 number_of_threads_completed++;
200 }
201
202 } while (number_of_threads_issued > number_of_threads_completed);
203 assert(number_of_threads_issued == number_of_threads_completed, "Must be the same");
204 DEBUG_ONLY(_op->check_state();)
205 }
206
207 VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
208 };
209
210 class VM_HandshakeFallbackOperation : public VM_Operation {
211 ThreadClosure* _thread_cl;
212 Thread* _target_thread;
213 bool _all_threads;
214 bool _executed;
215 public:
216 VM_HandshakeFallbackOperation(ThreadClosure* cl) :
217 _thread_cl(cl), _target_thread(NULL), _all_threads(true), _executed(false) {}
218 VM_HandshakeFallbackOperation(ThreadClosure* cl, Thread* target) :
219 _thread_cl(cl), _target_thread(target), _all_threads(false), _executed(false) {}
220
221 void doit() {
222 log_trace(handshake)("VMThread executing VM_HandshakeFallbackOperation");
223 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
224 if (_all_threads || t == _target_thread) {
225 if (t == _target_thread) {
226 _executed = true;
227 }
228 _thread_cl->do_thread(t);
229 }
230 }
231 }
232
233 VMOp_Type type() const { return VMOp_HandshakeFallback; }
234 bool executed() const { return _executed; }
235 };
236
237 void HandshakeThreadsOperation::do_handshake(JavaThread* thread) {
238 ResourceMark rm;
239 FormatBufferResource message("Operation for thread " PTR_FORMAT ", is_vm_thread: %s",
240 p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()));
241 TraceTime timer(message, TRACETIME_LOG(Debug, handshake, task));
242
243 // Only actually execute the operation for non terminated threads.
244 if (!thread->is_terminated()) {
245 _thread_cl->do_thread(thread);
246 _executed = true;
247 }
248
249 // Use the semaphore to inform the VM thread that we have completed the operation
250 _done.signal();
251 }
252
253 void Handshake::execute(ThreadClosure* thread_cl) {
254 if (ThreadLocalHandshakes) {
255 HandshakeThreadsOperation cto(thread_cl);
256 VM_HandshakeAllThreads handshake(&cto);
257 VMThread::execute(&handshake);
258 } else {
259 VM_HandshakeFallbackOperation op(thread_cl);
260 VMThread::execute(&op);
261 }
262 }
263
264 bool Handshake::execute(ThreadClosure* thread_cl, JavaThread* target) {
265 if (ThreadLocalHandshakes) {
266 HandshakeThreadsOperation cto(thread_cl);
267 VM_HandshakeOneThread handshake(&cto, target);
268 VMThread::execute(&handshake);
269 return handshake.executed();
270 } else {
271 VM_HandshakeFallbackOperation op(thread_cl, target);
272 VMThread::execute(&op);
273 return op.executed();
274 }
275 }
276
277 HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _thread_in_process_handshake(false) {}
278
279 void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) {
280 _operation = op;
281 SafepointMechanism::arm_local_poll_release(target);
282 }
283
284 void HandshakeState::clear_handshake(JavaThread* target) {
285 _operation = NULL;
286 SafepointMechanism::disarm_if_needed(target, true /* release */);
287 }
288
289 void HandshakeState::process_self_inner(JavaThread* thread) {
290 assert(Thread::current() == thread, "should call from thread");
291 assert(!thread->is_terminated(), "should not be a terminated thread");
329
330 case _thread_blocked:
331 return true;
332
333 default:
334 return false;
335 }
336 }
337
338 bool HandshakeState::claim_handshake_for_vmthread() {
339 if (!_semaphore.trywait()) {
340 return false;
341 }
342 if (has_operation()) {
343 return true;
344 }
345 _semaphore.signal();
346 return false;
347 }
348
349 void HandshakeState::process_by_vmthread(JavaThread* target) {
350 assert(Thread::current()->is_VM_thread(), "should call from vm thread");
351 // Threads_lock must be held here, but that is assert()ed in
352 // possibly_vmthread_can_process_handshake().
353
354 if (!has_operation()) {
355 // JT has already cleared its handshake
356 return;
357 }
358
359 if (!possibly_vmthread_can_process_handshake(target)) {
360 // JT is observed in an unsafe state, it must notice the handshake itself
361 return;
362 }
363
364 // Claim the semaphore if there still an operation to be executed.
365 if (!claim_handshake_for_vmthread()) {
366 return;
367 }
368
369 // If we own the semaphore at this point and while owning the semaphore
370 // can observe a safe state the thread cannot possibly continue without
371 // getting caught by the semaphore.
372 if (vmthread_can_process_handshake(target)) {
373 guarantee(!_semaphore.trywait(), "we should already own the semaphore");
374 log_trace(handshake)("Processing handshake by VMThtread");
375 _operation->do_handshake(target);
376 // Disarm after VM thread have executed the operation.
377 clear_handshake(target);
378 // Release the thread
379 }
380
381 _semaphore.signal();
382 }
|
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/thread.hpp"
36 #include "runtime/vmThread.hpp"
37 #include "utilities/formatBuffer.hpp"
38 #include "utilities/preserveException.hpp"
39
40 Semaphore HandshakeOperation::_done(0);
41
42 class VM_Handshake: public VM_Operation {
43 const jlong _handshake_timeout;
44 public:
45 bool evaluate_at_safepoint() const { return false; }
46
47 bool evaluate_concurrently() const { return false; }
48
49 protected:
50 HandshakeOperation* const _op;
51
52 VM_Handshake(HandshakeOperation* op) :
53 _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)), _op(op) {}
54
55 void set_handshake(JavaThread* target) {
56 target->set_handshake_operation(_op);
57 }
58
59 // This method returns true for threads completed their operation
60 // and true for threads canceled their operation.
61 // A cancellation can happen if the thread is exiting.
62 bool poll_for_completed_thread() { return _op->thread_has_completed(); }
63
64 bool handshake_has_timed_out(jlong start_time);
65 static void handle_timeout();
66 };
67
68 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
69 // Check if handshake operation has timed out
70 if (_handshake_timeout > 0) {
71 return os::elapsed_counter() >= (start_time + _handshake_timeout);
72 }
73 return false;
74 }
75
76 void VM_Handshake::handle_timeout() {
77 LogStreamHandle(Warning, handshake) log_stream;
78 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
79 if (thr->has_handshake()) {
80 log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
81 thr->print_thread_state_on(&log_stream);
82 }
83 }
84 log_stream.flush();
85 fatal("Handshake operation timed out");
86 }
87
88 static void log_handshake_info(jlong start_time_ns, const char* name, int targets, int vmt_executed, const char* extra) {
89 if (start_time_ns != 0) {
90 jlong completion_time = os::javaTimeNanos() - start_time_ns;
91 log_info(handshake)("Handshake \"%s\", Targeted threads: %d, Executed by targeted threads: %d, Total completion time: " JLONG_FORMAT " ns%s",
92 name, targets, targets - vmt_executed, completion_time, extra);
93 }
94 }
95
96 class VM_HandshakeOneThread: public VM_Handshake {
97 JavaThread* _target;
98 public:
99 VM_HandshakeOneThread(HandshakeOperation* op, JavaThread* target) :
100 VM_Handshake(op), _target(target) {}
101
102 void doit() {
103 DEBUG_ONLY(_op->check_state();)
104
105 jlong start_time_ns = 0;
106 if (log_is_enabled(Info, handshake)) {
107 start_time_ns = os::javaTimeNanos();
108 }
109
110 ThreadsListHandle tlh;
111 if (tlh.includes(_target)) {
112 set_handshake(_target);
113 } else {
114 log_handshake_info(start_time_ns, _op->name(), 0, 0, " (thread dead)");
115 return;
116 }
117
118 log_trace(handshake)("JavaThread " INTPTR_FORMAT " signaled, begin attempt to process by VMThtread", p2i(_target));
119 jlong timeout_start_time = os::elapsed_counter();
120 bool by_vm_thread = false;
121 do {
122 if (handshake_has_timed_out(timeout_start_time)) {
123 handle_timeout();
124 }
125
126 // We need to re-think this with SMR ThreadsList.
127 // There is an assumption in the code that the Threads_lock should be
128 // locked during certain phases.
129 {
130 MutexLocker ml(Threads_lock);
131 by_vm_thread = _target->handshake_process_by_vmthread();
132 }
133 } while (!poll_for_completed_thread());
134 DEBUG_ONLY(_op->check_state();)
135 log_handshake_info(start_time_ns, _op->name(), 1, by_vm_thread ? 1 : 0, "");
136 }
137
138 VMOp_Type type() const { return VMOp_HandshakeOneThread; }
139
140 bool executed() const { return _op->executed(); }
141 };
142
143 class VM_HandshakeAllThreads: public VM_Handshake {
144 public:
145 VM_HandshakeAllThreads(HandshakeOperation* op) : VM_Handshake(op) {}
146
147 void doit() {
148 DEBUG_ONLY(_op->check_state();)
149
150 jlong start_time_ns = 0;
151 if (log_is_enabled(Info, handshake)) {
152 start_time_ns = os::javaTimeNanos();
153 }
154 int handshake_executed_by_vm_thread = 0;
155
156 JavaThreadIteratorWithHandle jtiwh;
157 int number_of_threads_issued = 0;
158 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
159 set_handshake(thr);
160 number_of_threads_issued++;
161 }
162
163 if (number_of_threads_issued < 1) {
164 log_trace(handshake)("No threads to handshake.");
165 return;
166 }
167
168 log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThtread");
169 const jlong start_time = os::elapsed_counter();
170 int number_of_threads_completed = 0;
171 do {
172 // Check if handshake operation has timed out
173 if (handshake_has_timed_out(start_time)) {
174 handle_timeout();
175 }
176
177 // Have VM thread perform the handshake operation for blocked threads.
178 // Observing a blocked state may of course be transient but the processing is guarded
179 // by semaphores and we optimistically begin by working on the blocked threads
180 {
181 // We need to re-think this with SMR ThreadsList.
182 // There is an assumption in the code that the Threads_lock should
183 // be locked during certain phases.
184 jtiwh.rewind();
185 MutexLocker ml(Threads_lock);
186 for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
187 // A new thread on the ThreadsList will not have an operation,
188 // hence it is skipped in handshake_process_by_vmthread.
189 if (thr->handshake_process_by_vmthread()) {
190 handshake_executed_by_vm_thread++;
191 }
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 log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, handshake_executed_by_vm_thread, "");
205 }
206
207 VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
208 };
209
210 class VM_HandshakeFallbackOperation : public VM_Operation {
211 HandshakeOperation* _hs_op;
212 Thread* _target_thread;
213 bool _all_threads;
214 bool _executed;
215 public:
216 VM_HandshakeFallbackOperation(HandshakeOperation* hs_op) :
217 _hs_op(hs_op), _target_thread(NULL), _all_threads(true), _executed(false) {}
218 VM_HandshakeFallbackOperation(HandshakeOperation* hs_op, Thread* target) :
219 _hs_op(hs_op), _target_thread(target), _all_threads(false), _executed(false) {}
220
221 void doit() {
222 log_trace(handshake)("VMThread executing VM_HandshakeFallbackOperation, operation: %s", name());
223 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
224 if (_all_threads || t == _target_thread) {
225 if (t == _target_thread) {
226 _executed = true;
227 }
228 _hs_op->do_thread(t);
229 }
230 }
231 }
232
233 VMOp_Type type() const { return VMOp_HandshakeFallback; }
234 bool executed() const { return _executed; }
235 };
236
237 void HandshakeOperation::do_handshake(JavaThread* thread) {
238 jlong start_time_ns = 0;
239 if (log_is_enabled(Debug, handshake, task)) {
240 start_time_ns = os::javaTimeNanos();
241 }
242
243 // Only actually execute the operation for non terminated threads.
244 if (!thread->is_terminated()) {
245 do_thread(thread);
246 _executed = true;
247 }
248
249 // Use the semaphore to inform the VM thread that we have completed the operation
250 _done.signal();
251
252 if (start_time_ns != 0) {
253 jlong completion_time = os::javaTimeNanos() - start_time_ns;
254 log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns",
255 name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time);
256 }
257 }
258
259 void Handshake::execute(HandshakeOperation* hs_op) {
260 if (ThreadLocalHandshakes) {
261 VM_HandshakeAllThreads handshake(hs_op);
262 VMThread::execute(&handshake);
263 } else {
264 VM_HandshakeFallbackOperation op(hs_op);
265 VMThread::execute(&op);
266 }
267 }
268
269 bool Handshake::execute(HandshakeOperation* hs_op, JavaThread* target) {
270 if (ThreadLocalHandshakes) {
271 VM_HandshakeOneThread handshake(hs_op, target);
272 VMThread::execute(&handshake);
273 return handshake.executed();
274 } else {
275 VM_HandshakeFallbackOperation op(hs_op, target);
276 VMThread::execute(&op);
277 return op.executed();
278 }
279 }
280
281 HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _thread_in_process_handshake(false) {}
282
283 void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) {
284 _operation = op;
285 SafepointMechanism::arm_local_poll_release(target);
286 }
287
288 void HandshakeState::clear_handshake(JavaThread* target) {
289 _operation = NULL;
290 SafepointMechanism::disarm_if_needed(target, true /* release */);
291 }
292
293 void HandshakeState::process_self_inner(JavaThread* thread) {
294 assert(Thread::current() == thread, "should call from thread");
295 assert(!thread->is_terminated(), "should not be a terminated thread");
333
334 case _thread_blocked:
335 return true;
336
337 default:
338 return false;
339 }
340 }
341
342 bool HandshakeState::claim_handshake_for_vmthread() {
343 if (!_semaphore.trywait()) {
344 return false;
345 }
346 if (has_operation()) {
347 return true;
348 }
349 _semaphore.signal();
350 return false;
351 }
352
353 bool HandshakeState::process_by_vmthread(JavaThread* target) {
354 assert(Thread::current()->is_VM_thread(), "should call from vm thread");
355 // Threads_lock must be held here, but that is assert()ed in
356 // possibly_vmthread_can_process_handshake().
357
358 if (!has_operation()) {
359 // JT has already cleared its handshake
360 return false;
361 }
362
363 if (!possibly_vmthread_can_process_handshake(target)) {
364 // JT is observed in an unsafe state, it must notice the handshake itself
365 return false;
366 }
367
368 // Claim the semaphore if there still an operation to be executed.
369 if (!claim_handshake_for_vmthread()) {
370 return false;
371 }
372
373 // If we own the semaphore at this point and while owning the semaphore
374 // can observe a safe state the thread cannot possibly continue without
375 // getting caught by the semaphore.
376 bool executed = false;
377 if (vmthread_can_process_handshake(target)) {
378 guarantee(!_semaphore.trywait(), "we should already own the semaphore");
379 log_trace(handshake)("Processing handshake by VMThtread");
380 _operation->do_handshake(target);
381 // Disarm after VM thread have executed the operation.
382 clear_handshake(target);
383 executed = true;
384 }
385
386 // Release the thread
387 _semaphore.signal();
388
389 return executed;
390 }
|