25
#include "precompiled.hpp"
26
#include "compiler/compileBroker.hpp"
27
#include "gc/shared/collectedHeap.hpp"
28
#include "jfr/jfrEvents.hpp"
29
#include "jfr/support/jfrThreadId.hpp"
30
#include "logging/log.hpp"
31
#include "logging/logStream.hpp"
32
#include "logging/logConfiguration.hpp"
33
#include "memory/resourceArea.hpp"
34
#include "memory/universe.hpp"
35
#include "oops/oop.inline.hpp"
36
#include "oops/verifyOopClosure.hpp"
37
#include "runtime/atomic.hpp"
38
#include "runtime/cpuTimeCounters.hpp"
39
#include "runtime/handles.inline.hpp"
40
#include "runtime/interfaceSupport.inline.hpp"
41
#include "runtime/javaThread.inline.hpp"
42
#include "runtime/jniHandles.hpp"
43
#include "runtime/mutexLocker.hpp"
44
#include "runtime/os.hpp"
45
#include "runtime/perfData.hpp"
46
#include "runtime/safepoint.hpp"
47
#include "runtime/synchronizer.hpp"
48
#include "runtime/timerTrace.hpp"
49
#include "runtime/vmThread.hpp"
50
#include "runtime/vmOperations.hpp"
51
#include "utilities/dtrace.hpp"
52
#include "utilities/events.hpp"
53
#include "utilities/vmError.hpp"
59
void VMOperationTimeoutTask::task() {
60
assert(AbortVMOnVMOperationTimeout, "only if enabled");
62
jlong delay = nanos_to_millis(os::javaTimeNanos() - _arm_time);
63
if (delay > AbortVMOnVMOperationTimeoutDelay) {
64
fatal("%s VM operation took too long: " JLONG_FORMAT " ms elapsed since VM-op start (timeout: " INTX_FORMAT " ms)",
65
_vm_op_name, delay, AbortVMOnVMOperationTimeoutDelay);
70
bool VMOperationTimeoutTask::is_armed() {
71
return Atomic::load_acquire(&_armed) != 0;
74
void VMOperationTimeoutTask::arm(const char* vm_op_name) {
75
_vm_op_name = vm_op_name;
76
_arm_time = os::javaTimeNanos();
77
Atomic::release_store_fence(&_armed, 1);
80
void VMOperationTimeoutTask::disarm() {
81
Atomic::release_store_fence(&_armed, 0);
85
jlong vm_op_duration = nanos_to_millis(os::javaTimeNanos() - _arm_time);
90
if (vm_op_duration > AbortVMOnVMOperationTimeoutDelay) {
91
fatal("%s VM operation took too long: completed in " JLONG_FORMAT " ms (timeout: " INTX_FORMAT " ms)",
92
_vm_op_name, vm_op_duration, AbortVMOnVMOperationTimeoutDelay);
94
_vm_op_name = nullptr;
100
static VM_SafepointALot safepointALot_op;
101
static VM_ForceSafepoint no_op;
103
bool VMThread::_should_terminate = false;
104
bool VMThread::_terminated = false;
105
Monitor* VMThread::_terminate_lock = nullptr;
106
VMThread* VMThread::_vm_thread = nullptr;
107
VM_Operation* VMThread::_cur_vm_operation = nullptr;
108
VM_Operation* VMThread::_next_vm_operation = &no_op;
109
PerfCounter* VMThread::_perf_accumulated_vm_operation_time = nullptr;
110
VMOperationTimeoutTask* VMThread::_timeout_task = nullptr;
113
void VMThread::create() {
114
assert(vm_thread() == nullptr, "we can only allocate one VMThread");
115
_vm_thread = new VMThread();
117
if (AbortVMOnVMOperationTimeout) {
121
size_t interval = (size_t)AbortVMOnVMOperationTimeoutDelay / 10;
122
interval = interval / PeriodicTask::interval_gran * PeriodicTask::interval_gran;
123
interval = MAX2<size_t>(interval, PeriodicTask::min_interval);
124
interval = MIN2<size_t>(interval, PeriodicTask::max_interval);
126
_timeout_task = new VMOperationTimeoutTask(interval);
127
_timeout_task->enroll();
129
assert(_timeout_task == nullptr, "sanity");
132
_terminate_lock = new Monitor(Mutex::nosafepoint, "VMThreadTerminate_lock");
136
JavaThread* THREAD = JavaThread::current();
137
_perf_accumulated_vm_operation_time =
138
PerfDataManager::create_counter(SUN_THREADS, "vmOperationTime",
139
PerfData::U_Ticks, CHECK);
140
CPUTimeCounters::create_counter(CPUTimeGroups::CPUTimeType::vm);
144
VMThread::VMThread() : NamedThread(), _is_running(false) {
145
set_name("VM Thread");
148
void VMThread::destroy() {
149
_vm_thread = nullptr;
152
static VM_Halt halt_op;
154
void VMThread::run() {
155
assert(this == vm_thread(), "check");
160
Atomic::store(&_is_running, true);
163
MutexLocker ml(Notify_lock);
164
Notify_lock->notify();
168
int prio = (VMThreadPriority == -1)
169
? os::java_to_os_priority[NearMaxPriority]
174
os::set_native_priority( this, prio );
182
if (xtty != nullptr) {
184
xtty->begin_elem("destroy_vm");
187
assert(should_terminate(), "termination flag must be set");
191
_cur_vm_operation = &halt_op;
192
SafepointSynchronize::begin();
194
if (VerifyBeforeExit) {
195
HandleMark hm(VMThread::vm_thread());
197
Universe::heap()->prepare_for_verify();
203
CompileBroker::set_should_block();
207
VM_Exit::wait_for_threads_in_native_to_block();
212
ObjectSynchronizer::do_final_audit_and_print_stats();
222
MonitorLocker ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
236
void VMThread::wait_for_vm_thread_exit() {
237
assert(JavaThread::current()->is_terminated(), "Should be terminated");
239
MonitorLocker mu(VMOperation_lock);
240
_should_terminate = true;
254
MonitorLocker ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
255
while (!VMThread::is_terminated()) {
261
static void post_vm_operation_event(EventExecuteVMOperation* event, VM_Operation* op) {
262
assert(event != nullptr, "invariant");
263
assert(op != nullptr, "invariant");
264
const bool evaluate_at_safepoint = op->evaluate_at_safepoint();
265
event->set_operation(op->type());
266
event->set_safepoint(evaluate_at_safepoint);
267
event->set_blocking(true);
268
event->set_caller(JFR_THREAD_ID(op->calling_thread()));
269
event->set_safepointId(evaluate_at_safepoint ? SafepointSynchronize::safepoint_id() : 0);
273
void VMThread::evaluate_operation(VM_Operation* op) {
277
PerfTraceTime vm_op_timer(perf_accumulated_vm_operation_time());
279
(char *) op->name(), strlen(op->name()),
280
op->evaluate_at_safepoint() ? 0 : 1);
282
EventExecuteVMOperation event;
284
if (event.should_commit()) {
285
post_vm_operation_event(&event, op);
289
(char *) op->name(), strlen(op->name()),
290
op->evaluate_at_safepoint() ? 0 : 1);
293
if (UsePerfData && os::is_thread_cpu_time_supported()) {
294
assert(Thread::current() == this, "Must be called from VM thread");
296
ThreadTotalCPUTimeClosure tttc(CPUTimeGroups::CPUTimeType::vm);
297
tttc.do_thread(this);
301
class HandshakeALotClosure : public HandshakeClosure {
303
HandshakeALotClosure() : HandshakeClosure("HandshakeALot") {}
304
void do_thread(Thread* thread) {
306
JavaThread::cast(thread)->verify_states_for_handshake();
311
bool VMThread::handshake_or_safepoint_alot() {
312
assert(_cur_vm_operation == nullptr, "should not have an op yet");
313
assert(_next_vm_operation == nullptr, "should not have an op yet");
314
if (!HandshakeALot && !SafepointALot) {
317
static jlong last_alot_ms = 0;
318
jlong now_ms = nanos_to_millis(os::javaTimeNanos());
322
jlong interval = GuaranteedSafepointInterval != 0 ? GuaranteedSafepointInterval : 1000;
323
jlong deadline_ms = interval + last_alot_ms;
324
if (now_ms > deadline_ms) {
325
last_alot_ms = now_ms;
331
bool VMThread::set_next_operation(VM_Operation *op) {
332
if (_next_vm_operation != nullptr) {
335
log_debug(vmthread)("Adding VM operation: %s", op->name());
337
_next_vm_operation = op;
339
HOTSPOT_VMOPS_REQUEST(
340
(char *) op->name(), strlen(op->name()),
341
op->evaluate_at_safepoint() ? 0 : 1);
345
void VMThread::wait_until_executed(VM_Operation* op) {
346
MonitorLocker ml(VMOperation_lock,
347
Thread::current()->is_Java_thread() ?
348
Mutex::_safepoint_check_flag :
349
Mutex::_no_safepoint_check_flag);
351
TraceTime timer("Installing VM operation", TRACETIME_LOG(Trace, vmthread));
353
if (VMThread::vm_thread()->set_next_operation(op)) {
358
log_trace(vmthread)("A VM operation already set, waiting");
364
TraceTime timer("Waiting for VM operation to be completed", TRACETIME_LOG(Trace, vmthread));
367
while (_next_vm_operation == op) {
374
static void self_destruct_if_needed() {
376
if ((SelfDestructTimer != 0.0) && !VMError::is_error_reported() &&
377
(os::elapsedTime() > SelfDestructTimer * 60.0)) {
378
tty->print_cr("VM self-destructed");
383
void VMThread::inner_execute(VM_Operation* op) {
384
assert(Thread::current()->is_VM_thread(), "Must be the VM thread");
386
VM_Operation* prev_vm_operation = nullptr;
387
if (_cur_vm_operation != nullptr) {
391
if (!_cur_vm_operation->allow_nested_vm_operations()) {
392
fatal("Unexpected nested VM operation %s requested by operation %s",
393
op->name(), _cur_vm_operation->name());
395
op->set_calling_thread(_cur_vm_operation->calling_thread());
396
prev_vm_operation = _cur_vm_operation;
399
_cur_vm_operation = op;
401
HandleMark hm(VMThread::vm_thread());
403
const char* const cause = op->cause();
404
EventMarkVMOperation em("Executing %sVM operation: %s%s%s%s",
405
prev_vm_operation != nullptr ? "nested " : "",
407
cause != nullptr ? " (" : "",
408
cause != nullptr ? cause : "",
409
cause != nullptr ? ")" : "");
411
log_debug(vmthread)("Evaluating %s %s VM operation: %s",
412
prev_vm_operation != nullptr ? "nested" : "",
413
_cur_vm_operation->evaluate_at_safepoint() ? "safepoint" : "non-safepoint",
414
_cur_vm_operation->name());
416
bool end_safepoint = false;
417
bool has_timeout_task = (_timeout_task != nullptr);
418
if (_cur_vm_operation->evaluate_at_safepoint() &&
419
!SafepointSynchronize::is_at_safepoint()) {
420
SafepointSynchronize::begin();
421
if (has_timeout_task) {
422
_timeout_task->arm(_cur_vm_operation->name());
424
end_safepoint = true;
427
evaluate_operation(_cur_vm_operation);
430
if (has_timeout_task) {
431
_timeout_task->disarm();
433
SafepointSynchronize::end();
436
_cur_vm_operation = prev_vm_operation;
439
void VMThread::wait_for_operation() {
440
assert(Thread::current()->is_VM_thread(), "Must be the VM thread");
441
MonitorLocker ml_op_lock(VMOperation_lock, Mutex::_no_safepoint_check_flag);
445
_next_vm_operation = nullptr;
447
ml_op_lock.notify_all();
449
while (!should_terminate()) {
450
self_destruct_if_needed();
451
if (_next_vm_operation != nullptr) {
454
if (handshake_or_safepoint_alot()) {
456
MutexUnlocker mul(VMOperation_lock);
457
HandshakeALotClosure hal_cl;
458
Handshake::execute(&hal_cl);
461
if (_next_vm_operation != nullptr) {
465
_next_vm_operation = &safepointALot_op;
469
assert(_next_vm_operation == nullptr, "Must be");
470
assert(_cur_vm_operation == nullptr, "Must be");
473
ml_op_lock.notify_all();
474
ml_op_lock.wait(GuaranteedSafepointInterval);
478
void VMThread::loop() {
479
assert(_cur_vm_operation == nullptr, "no current one should be executing");
481
SafepointSynchronize::init(_vm_thread);
485
no_op.set_calling_thread(_vm_thread);
486
safepointALot_op.set_calling_thread(_vm_thread);
489
if (should_terminate()) break;
490
wait_for_operation();
491
if (should_terminate()) break;
492
assert(_next_vm_operation != nullptr, "Must have one");
493
inner_execute(_next_vm_operation);
500
class SkipGCALot : public StackObj {
507
SkipGCALot(Thread* t) : _t(t) {
508
_saved = _t->skip_gcalot();
509
_t->set_skip_gcalot(true);
513
assert(_t->skip_gcalot(), "Save-restore protocol invariant");
514
_t->set_skip_gcalot(_saved);
517
SkipGCALot(Thread* t) { }
522
void VMThread::execute(VM_Operation* op) {
523
Thread* t = Thread::current();
525
if (t->is_VM_thread()) {
526
op->set_calling_thread(t);
527
((VMThread*)t)->inner_execute(op);
532
SkipGCALot sgcalot(t);
535
if (t->is_Java_thread()) {
536
JavaThread::cast(t)->check_for_valid_safepoint_state();
540
if (!op->doit_prologue()) {
544
op->set_calling_thread(t);
546
wait_until_executed(op);
551
void VMThread::verify() {
552
oops_do(&VerifyOopClosure::verify_oop, nullptr);