1 /* 2 * Copyright (c) 1998, 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 "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/method.hpp" 36 #include "oops/oop.inline.hpp" 37 #include "oops/verifyOopClosure.hpp" 38 #include "runtime/atomic.hpp" 39 #include "runtime/handles.inline.hpp" 40 #include "runtime/interfaceSupport.inline.hpp" 41 #include "runtime/mutexLocker.hpp" 42 #include "runtime/os.hpp" 43 #include "runtime/safepoint.hpp" 44 #include "runtime/synchronizer.hpp" 45 #include "runtime/thread.inline.hpp" 46 #include "runtime/vmThread.hpp" 47 #include "runtime/vmOperations.hpp" 48 #include "services/runtimeService.hpp" 49 #include "utilities/dtrace.hpp" 50 #include "utilities/events.hpp" 51 #include "utilities/vmError.hpp" 52 #include "utilities/xmlstream.hpp" 53 54 VM_QueueHead VMOperationQueue::_queue_head[VMOperationQueue::nof_priorities]; 55 56 VMOperationQueue::VMOperationQueue() { 57 // The queue is a circular doubled-linked list, which always contains 58 // one element (i.e., one element means empty). 59 for(int i = 0; i < nof_priorities; i++) { 60 _queue_length[i] = 0; 61 _queue_counter = 0; 62 _queue[i] = &_queue_head[i]; 63 _queue[i]->set_next(_queue[i]); 64 _queue[i]->set_prev(_queue[i]); 65 } 66 } 67 68 69 bool VMOperationQueue::queue_empty(int prio) { 70 // It is empty if there is exactly one element 71 bool empty = (_queue[prio] == _queue[prio]->next()); 72 assert( (_queue_length[prio] == 0 && empty) || 73 (_queue_length[prio] > 0 && !empty), "sanity check"); 74 return _queue_length[prio] == 0; 75 } 76 77 // Inserts an element to the right of the q element 78 void VMOperationQueue::insert(VM_Operation* q, VM_Operation* n) { 79 assert(q->next()->prev() == q && q->prev()->next() == q, "sanity check"); 80 n->set_prev(q); 81 n->set_next(q->next()); 82 q->next()->set_prev(n); 83 q->set_next(n); 84 } 85 86 void VMOperationQueue::queue_add(int prio, VM_Operation *op) { 87 _queue_length[prio]++; 88 insert(_queue[prio]->prev(), op); 89 } 90 91 92 void VMOperationQueue::unlink(VM_Operation* q) { 93 assert(q->next()->prev() == q && q->prev()->next() == q, "sanity check"); 94 q->prev()->set_next(q->next()); 95 q->next()->set_prev(q->prev()); 96 } 97 98 VM_Operation* VMOperationQueue::queue_remove_front(int prio) { 99 if (queue_empty(prio)) return NULL; 100 assert(_queue_length[prio] >= 0, "sanity check"); 101 _queue_length[prio]--; 102 VM_Operation* r = _queue[prio]->next(); 103 assert(r != _queue[prio], "cannot remove base element"); 104 unlink(r); 105 return r; 106 } 107 108 VM_Operation* VMOperationQueue::queue_drain(int prio) { 109 if (queue_empty(prio)) return NULL; 110 DEBUG_ONLY(int length = _queue_length[prio];); 111 assert(length >= 0, "sanity check"); 112 _queue_length[prio] = 0; 113 VM_Operation* r = _queue[prio]->next(); 114 assert(r != _queue[prio], "cannot remove base element"); 115 // remove links to base element from head and tail 116 r->set_prev(NULL); 117 _queue[prio]->prev()->set_next(NULL); 118 // restore queue to empty state 119 _queue[prio]->set_next(_queue[prio]); 120 _queue[prio]->set_prev(_queue[prio]); 121 assert(queue_empty(prio), "drain corrupted queue"); 122 #ifdef ASSERT 123 int len = 0; 124 VM_Operation* cur; 125 for(cur = r; cur != NULL; cur=cur->next()) len++; 126 assert(len == length, "drain lost some ops"); 127 #endif 128 return r; 129 } 130 131 //----------------------------------------------------------------- 132 // High-level interface 133 void VMOperationQueue::add(VM_Operation *op) { 134 135 HOTSPOT_VMOPS_REQUEST( 136 (char *) op->name(), strlen(op->name()), 137 op->evaluate_at_safepoint() ? 0 : 1); 138 139 // Encapsulates VM queue policy. Currently, that 140 // only involves putting them on the right list 141 queue_add(op->evaluate_at_safepoint() ? SafepointPriority : MediumPriority, op); 142 } 143 144 VM_Operation* VMOperationQueue::remove_next() { 145 // Assuming VMOperation queue is two-level priority queue. If there are 146 // more than two priorities, we need a different scheduling algorithm. 147 assert(SafepointPriority == 0 && MediumPriority == 1 && nof_priorities == 2, 148 "current algorithm does not work"); 149 150 // simple counter based scheduling to prevent starvation of lower priority 151 // queue. -- see 4390175 152 int high_prio, low_prio; 153 if (_queue_counter++ < 10) { 154 high_prio = SafepointPriority; 155 low_prio = MediumPriority; 156 } else { 157 _queue_counter = 0; 158 high_prio = MediumPriority; 159 low_prio = SafepointPriority; 160 } 161 162 return queue_remove_front(queue_empty(high_prio) ? low_prio : high_prio); 163 } 164 165 //------------------------------------------------------------------------------------------------------------------ 166 // Timeout machinery 167 168 void VMOperationTimeoutTask::task() { 169 assert(AbortVMOnVMOperationTimeout, "only if enabled"); 170 if (is_armed()) { 171 jlong delay = nanos_to_millis(os::javaTimeNanos() - _arm_time); 172 if (delay > AbortVMOnVMOperationTimeoutDelay) { 173 fatal("VM operation took too long: " JLONG_FORMAT " ms (timeout: " INTX_FORMAT " ms)", 174 delay, AbortVMOnVMOperationTimeoutDelay); 175 } 176 } 177 } 178 179 bool VMOperationTimeoutTask::is_armed() { 180 return Atomic::load_acquire(&_armed) != 0; 181 } 182 183 void VMOperationTimeoutTask::arm() { 184 _arm_time = os::javaTimeNanos(); 185 Atomic::release_store_fence(&_armed, 1); 186 } 187 188 void VMOperationTimeoutTask::disarm() { 189 Atomic::release_store_fence(&_armed, 0); 190 } 191 192 //------------------------------------------------------------------------------------------------------------------ 193 // Implementation of VMThread stuff 194 195 bool VMThread::_should_terminate = false; 196 bool VMThread::_terminated = false; 197 Monitor* VMThread::_terminate_lock = NULL; 198 VMThread* VMThread::_vm_thread = NULL; 199 VM_Operation* VMThread::_cur_vm_operation = NULL; 200 VMOperationQueue* VMThread::_vm_queue = NULL; 201 PerfCounter* VMThread::_perf_accumulated_vm_operation_time = NULL; 202 uint64_t VMThread::_coalesced_count = 0; 203 VMOperationTimeoutTask* VMThread::_timeout_task = NULL; 204 205 206 void VMThread::create() { 207 assert(vm_thread() == NULL, "we can only allocate one VMThread"); 208 _vm_thread = new VMThread(); 209 210 if (AbortVMOnVMOperationTimeout) { 211 // Make sure we call the timeout task frequently enough, but not too frequent. 212 // Try to make the interval 10% of the timeout delay, so that we miss the timeout 213 // by those 10% at max. Periodic task also expects it to fit min/max intervals. 214 size_t interval = (size_t)AbortVMOnVMOperationTimeoutDelay / 10; 215 interval = interval / PeriodicTask::interval_gran * PeriodicTask::interval_gran; 216 interval = MAX2<size_t>(interval, PeriodicTask::min_interval); 217 interval = MIN2<size_t>(interval, PeriodicTask::max_interval); 218 219 _timeout_task = new VMOperationTimeoutTask(interval); 220 _timeout_task->enroll(); 221 } else { 222 assert(_timeout_task == NULL, "sanity"); 223 } 224 225 // Create VM operation queue 226 _vm_queue = new VMOperationQueue(); 227 guarantee(_vm_queue != NULL, "just checking"); 228 229 _terminate_lock = new Monitor(Mutex::safepoint, "VMThread::_terminate_lock", true, 230 Monitor::_safepoint_check_never); 231 232 if (UsePerfData) { 233 // jvmstat performance counters 234 Thread* THREAD = Thread::current(); 235 _perf_accumulated_vm_operation_time = 236 PerfDataManager::create_counter(SUN_THREADS, "vmOperationTime", 237 PerfData::U_Ticks, CHECK); 238 } 239 } 240 241 VMThread::VMThread() : NamedThread() { 242 set_name("VM Thread"); 243 } 244 245 void VMThread::destroy() { 246 _vm_thread = NULL; // VM thread is gone 247 } 248 249 static VM_None halt_op("Halt"); 250 251 void VMThread::run() { 252 assert(this == vm_thread(), "check"); 253 254 // Notify_lock wait checks on active_handles() to rewait in 255 // case of spurious wakeup, it should wait on the last 256 // value set prior to the notify 257 this->set_active_handles(JNIHandleBlock::allocate_block()); 258 259 { 260 MutexLocker ml(Notify_lock); 261 Notify_lock->notify(); 262 } 263 // Notify_lock is destroyed by Threads::create_vm() 264 265 int prio = (VMThreadPriority == -1) 266 ? os::java_to_os_priority[NearMaxPriority] 267 : VMThreadPriority; 268 // Note that I cannot call os::set_priority because it expects Java 269 // priorities and I am *explicitly* using OS priorities so that it's 270 // possible to set the VM thread priority higher than any Java thread. 271 os::set_native_priority( this, prio ); 272 273 // Wait for VM_Operations until termination 274 this->loop(); 275 276 // Note the intention to exit before safepointing. 277 // 6295565 This has the effect of waiting for any large tty 278 // outputs to finish. 279 if (xtty != NULL) { 280 ttyLocker ttyl; 281 xtty->begin_elem("destroy_vm"); 282 xtty->stamp(); 283 xtty->end_elem(); 284 assert(should_terminate(), "termination flag must be set"); 285 } 286 287 if (AsyncDeflateIdleMonitors && log_is_enabled(Info, monitorinflation)) { 288 // AsyncDeflateIdleMonitors does a special deflation in order 289 // to reduce the in-use monitor population that is reported by 290 // ObjectSynchronizer::log_in_use_monitor_details() at VM exit. 291 ObjectSynchronizer::request_deflate_idle_monitors(); 292 } 293 294 // 4526887 let VM thread exit at Safepoint 295 _cur_vm_operation = &halt_op; 296 SafepointSynchronize::begin(); 297 298 if (VerifyBeforeExit) { 299 HandleMark hm(VMThread::vm_thread()); 300 // Among other things, this ensures that Eden top is correct. 301 Universe::heap()->prepare_for_verify(); 302 // Silent verification so as not to pollute normal output, 303 // unless we really asked for it. 304 Universe::verify(); 305 } 306 307 CompileBroker::set_should_block(); 308 309 // wait for threads (compiler threads or daemon threads) in the 310 // _thread_in_native state to block. 311 VM_Exit::wait_for_threads_in_native_to_block(); 312 313 // signal other threads that VM process is gone 314 { 315 // Note: we must have the _no_safepoint_check_flag. Mutex::lock() allows 316 // VM thread to enter any lock at Safepoint as long as its _owner is NULL. 317 // If that happens after _terminate_lock->wait() has unset _owner 318 // but before it actually drops the lock and waits, the notification below 319 // may get lost and we will have a hang. To avoid this, we need to use 320 // Mutex::lock_without_safepoint_check(). 321 MonitorLocker ml(_terminate_lock, Mutex::_no_safepoint_check_flag); 322 _terminated = true; 323 ml.notify(); 324 } 325 326 // We are now racing with the VM termination being carried out in 327 // another thread, so we don't "delete this". Numerous threads don't 328 // get deleted when the VM terminates 329 330 } 331 332 333 // Notify the VMThread that the last non-daemon JavaThread has terminated, 334 // and wait until operation is performed. 335 void VMThread::wait_for_vm_thread_exit() { 336 assert(Thread::current()->is_Java_thread(), "Should be a JavaThread"); 337 assert(((JavaThread*)Thread::current())->is_terminated(), "Should be terminated"); 338 { MonitorLocker mu(VMOperationQueue_lock, Mutex::_no_safepoint_check_flag); 339 _should_terminate = true; 340 mu.notify(); 341 } 342 343 // Note: VM thread leaves at Safepoint. We are not stopped by Safepoint 344 // because this thread has been removed from the threads list. But anything 345 // that could get blocked by Safepoint should not be used after this point, 346 // otherwise we will hang, since there is no one can end the safepoint. 347 348 // Wait until VM thread is terminated 349 // Note: it should be OK to use Terminator_lock here. But this is called 350 // at a very delicate time (VM shutdown) and we are operating in non- VM 351 // thread at Safepoint. It's safer to not share lock with other threads. 352 { MonitorLocker ml(_terminate_lock, Mutex::_no_safepoint_check_flag); 353 while(!VMThread::is_terminated()) { 354 ml.wait(); 355 } 356 } 357 } 358 359 static void post_vm_operation_event(EventExecuteVMOperation* event, VM_Operation* op) { 360 assert(event != NULL, "invariant"); 361 assert(event->should_commit(), "invariant"); 362 assert(op != NULL, "invariant"); 363 const bool evaluate_at_safepoint = op->evaluate_at_safepoint(); 364 event->set_operation(op->type()); 365 event->set_safepoint(evaluate_at_safepoint); 366 event->set_blocking(true); 367 event->set_caller(JFR_THREAD_ID(op->calling_thread())); 368 event->set_safepointId(evaluate_at_safepoint ? SafepointSynchronize::safepoint_id() : 0); 369 event->commit(); 370 } 371 372 void VMThread::evaluate_operation(VM_Operation* op) { 373 ResourceMark rm; 374 375 { 376 PerfTraceTime vm_op_timer(perf_accumulated_vm_operation_time()); 377 HOTSPOT_VMOPS_BEGIN( 378 (char *) op->name(), strlen(op->name()), 379 op->evaluate_at_safepoint() ? 0 : 1); 380 381 EventExecuteVMOperation event; 382 op->evaluate(); 383 if (event.should_commit()) { 384 post_vm_operation_event(&event, op); 385 } 386 387 HOTSPOT_VMOPS_END( 388 (char *) op->name(), strlen(op->name()), 389 op->evaluate_at_safepoint() ? 0 : 1); 390 } 391 392 // Mark as completed 393 op->calling_thread()->increment_vm_operation_completed_count(); 394 } 395 396 static VM_None safepointALot_op("SafepointALot"); 397 static VM_Cleanup cleanup_op; 398 399 class HandshakeALotClosure : public HandshakeClosure { 400 public: 401 HandshakeALotClosure() : HandshakeClosure("HandshakeALot") {} 402 void do_thread(Thread* thread) { 403 #ifdef ASSERT 404 assert(thread->is_Java_thread(), "must be"); 405 JavaThread* jt = (JavaThread*)thread; 406 jt->verify_states_for_handshake(); 407 #endif 408 } 409 }; 410 411 VM_Operation* VMThread::no_op_safepoint() { 412 // Check for handshakes first since we may need to return a VMop. 413 if (HandshakeALot) { 414 HandshakeALotClosure hal_cl; 415 Handshake::execute(&hal_cl); 416 } 417 // Check for a cleanup before SafepointALot to keep stats correct. 418 long interval_ms = SafepointTracing::time_since_last_safepoint_ms(); 419 bool max_time_exceeded = GuaranteedSafepointInterval != 0 && 420 (interval_ms >= GuaranteedSafepointInterval); 421 if (max_time_exceeded && SafepointSynchronize::is_cleanup_needed()) { 422 return &cleanup_op; 423 } 424 if (SafepointALot) { 425 return &safepointALot_op; 426 } 427 // Nothing to be done. 428 return NULL; 429 } 430 431 void VMThread::loop() { 432 assert(_cur_vm_operation == NULL, "no current one should be executing"); 433 434 SafepointSynchronize::init(_vm_thread); 435 436 while(true) { 437 VM_Operation* safepoint_ops = NULL; 438 // 439 // Wait for VM operation 440 // 441 // use no_safepoint_check to get lock without attempting to "sneak" 442 { MonitorLocker mu_queue(VMOperationQueue_lock, 443 Mutex::_no_safepoint_check_flag); 444 445 // Look for new operation 446 assert(_cur_vm_operation == NULL, "no current one should be executing"); 447 _cur_vm_operation = _vm_queue->remove_next(); 448 449 // Stall time tracking code 450 if (PrintVMQWaitTime && _cur_vm_operation != NULL) { 451 jlong stall = nanos_to_millis(os::javaTimeNanos() - _cur_vm_operation->timestamp()); 452 if (stall > 0) 453 tty->print_cr("%s stall: " JLONG_FORMAT, _cur_vm_operation->name(), stall); 454 } 455 456 while (!should_terminate() && _cur_vm_operation == NULL) { 457 // wait with a timeout to guarantee safepoints at regular intervals 458 // (if there is cleanup work to do) 459 (void)mu_queue.wait(GuaranteedSafepointInterval); 460 461 // Support for self destruction 462 if ((SelfDestructTimer != 0) && !VMError::is_error_reported() && 463 (os::elapsedTime() > (double)SelfDestructTimer * 60.0)) { 464 tty->print_cr("VM self-destructed"); 465 exit(-1); 466 } 467 468 // If the queue contains a safepoint VM op, 469 // clean up will be done so we can skip this part. 470 if (!_vm_queue->peek_at_safepoint_priority()) { 471 472 // Have to unlock VMOperationQueue_lock just in case no_op_safepoint() 473 // has to do a handshake when HandshakeALot is enabled. 474 MutexUnlocker mul(VMOperationQueue_lock, Mutex::_no_safepoint_check_flag); 475 if ((_cur_vm_operation = VMThread::no_op_safepoint()) != NULL) { 476 // Force a safepoint since we have not had one for at least 477 // 'GuaranteedSafepointInterval' milliseconds and we need to clean 478 // something. This will run all the clean-up processing that needs 479 // to be done at a safepoint. 480 SafepointSynchronize::begin(); 481 #ifdef ASSERT 482 if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot(); 483 #endif 484 SafepointSynchronize::end(); 485 _cur_vm_operation = NULL; 486 } 487 } 488 _cur_vm_operation = _vm_queue->remove_next(); 489 490 // If we are at a safepoint we will evaluate all the operations that 491 // follow that also require a safepoint 492 if (_cur_vm_operation != NULL && 493 _cur_vm_operation->evaluate_at_safepoint()) { 494 safepoint_ops = _vm_queue->drain_at_safepoint_priority(); 495 } 496 } 497 498 if (should_terminate()) break; 499 } // Release mu_queue_lock 500 501 // 502 // Execute VM operation 503 // 504 { HandleMark hm(VMThread::vm_thread()); 505 506 EventMark em("Executing VM operation: %s", vm_operation()->name()); 507 assert(_cur_vm_operation != NULL, "we should have found an operation to execute"); 508 509 // If we are at a safepoint we will evaluate all the operations that 510 // follow that also require a safepoint 511 if (_cur_vm_operation->evaluate_at_safepoint()) { 512 log_debug(vmthread)("Evaluating safepoint VM operation: %s", _cur_vm_operation->name()); 513 514 SafepointSynchronize::begin(); 515 516 if (_timeout_task != NULL) { 517 _timeout_task->arm(); 518 } 519 520 evaluate_operation(_cur_vm_operation); 521 // now process all queued safepoint ops, iteratively draining 522 // the queue until there are none left 523 do { 524 _cur_vm_operation = safepoint_ops; 525 if (_cur_vm_operation != NULL) { 526 do { 527 EventMark em("Executing coalesced safepoint VM operation: %s", _cur_vm_operation->name()); 528 log_debug(vmthread)("Evaluating coalesced safepoint VM operation: %s", _cur_vm_operation->name()); 529 // evaluate_operation deletes the op object so we have 530 // to grab the next op now 531 VM_Operation* next = _cur_vm_operation->next(); 532 evaluate_operation(_cur_vm_operation); 533 _cur_vm_operation = next; 534 _coalesced_count++; 535 } while (_cur_vm_operation != NULL); 536 } 537 // There is a chance that a thread enqueued a safepoint op 538 // since we released the op-queue lock and initiated the safepoint. 539 // So we drain the queue again if there is anything there, as an 540 // optimization to try and reduce the number of safepoints. 541 // As the safepoint synchronizes us with JavaThreads we will see 542 // any enqueue made by a JavaThread, but the peek will not 543 // necessarily detect a concurrent enqueue by a GC thread, but 544 // that simply means the op will wait for the next major cycle of the 545 // VMThread - just as it would if the GC thread lost the race for 546 // the lock. 547 if (_vm_queue->peek_at_safepoint_priority()) { 548 // must hold lock while draining queue 549 MutexLocker mu_queue(VMOperationQueue_lock, 550 Mutex::_no_safepoint_check_flag); 551 safepoint_ops = _vm_queue->drain_at_safepoint_priority(); 552 } else { 553 safepoint_ops = NULL; 554 } 555 } while(safepoint_ops != NULL); 556 557 if (_timeout_task != NULL) { 558 _timeout_task->disarm(); 559 } 560 561 // Complete safepoint synchronization 562 SafepointSynchronize::end(); 563 564 } else { // not a safepoint operation 565 log_debug(vmthread)("Evaluating non-safepoint VM operation: %s", _cur_vm_operation->name()); 566 if (TraceLongCompiles) { 567 elapsedTimer t; 568 t.start(); 569 evaluate_operation(_cur_vm_operation); 570 t.stop(); 571 double secs = t.seconds(); 572 if (secs * 1e3 > LongCompileThreshold) { 573 // XXX - _cur_vm_operation should not be accessed after 574 // the completed count has been incremented; the waiting 575 // thread may have already freed this memory. 576 tty->print_cr("vm %s: %3.7f secs]", _cur_vm_operation->name(), secs); 577 } 578 } else { 579 evaluate_operation(_cur_vm_operation); 580 } 581 582 _cur_vm_operation = NULL; 583 } 584 } 585 586 // 587 // Notify (potential) waiting Java thread(s) 588 { MonitorLocker mu(VMOperationRequest_lock, Mutex::_no_safepoint_check_flag); 589 mu.notify_all(); 590 } 591 } 592 } 593 594 // A SkipGCALot object is used to elide the usual effect of gc-a-lot 595 // over a section of execution by a thread. Currently, it's used only to 596 // prevent re-entrant calls to GC. 597 class SkipGCALot : public StackObj { 598 private: 599 bool _saved; 600 Thread* _t; 601 602 public: 603 #ifdef ASSERT 604 SkipGCALot(Thread* t) : _t(t) { 605 _saved = _t->skip_gcalot(); 606 _t->set_skip_gcalot(true); 607 } 608 609 ~SkipGCALot() { 610 assert(_t->skip_gcalot(), "Save-restore protocol invariant"); 611 _t->set_skip_gcalot(_saved); 612 } 613 #else 614 SkipGCALot(Thread* t) { } 615 ~SkipGCALot() { } 616 #endif 617 }; 618 619 void VMThread::execute(VM_Operation* op) { 620 Thread* t = Thread::current(); 621 622 if (!t->is_VM_thread()) { 623 SkipGCALot sgcalot(t); // avoid re-entrant attempts to gc-a-lot 624 // JavaThread or WatcherThread 625 t->check_for_valid_safepoint_state(); 626 627 // New request from Java thread, evaluate prologue 628 if (!op->doit_prologue()) { 629 return; // op was cancelled 630 } 631 632 // Setup VM_operations for execution 633 op->set_calling_thread(t); 634 635 // Get ticket number for the VM operation 636 int ticket = t->vm_operation_ticket(); 637 638 // Add VM operation to list of waiting threads. We are guaranteed not to block while holding the 639 // VMOperationQueue_lock, so we can block without a safepoint check. This allows vm operation requests 640 // to be queued up during a safepoint synchronization. 641 { 642 MonitorLocker ml(VMOperationQueue_lock, Mutex::_no_safepoint_check_flag); 643 log_debug(vmthread)("Adding VM operation: %s", op->name()); 644 _vm_queue->add(op); 645 op->set_timestamp(os::javaTimeNanos()); 646 ml.notify(); 647 } 648 { 649 // Wait for completion of request 650 // Note: only a JavaThread triggers the safepoint check when locking 651 MonitorLocker ml(VMOperationRequest_lock, 652 t->is_Java_thread() ? Mutex::_safepoint_check_flag : Mutex::_no_safepoint_check_flag); 653 while(t->vm_operation_completed_count() < ticket) { 654 ml.wait(); 655 } 656 } 657 op->doit_epilogue(); 658 } else { 659 // invoked by VM thread; usually nested VM operation 660 assert(t->is_VM_thread(), "must be a VM thread"); 661 VM_Operation* prev_vm_operation = vm_operation(); 662 if (prev_vm_operation != NULL) { 663 // Check the VM operation allows nested VM operation. This normally not the case, e.g., the compiler 664 // does not allow nested scavenges or compiles. 665 if (!prev_vm_operation->allow_nested_vm_operations()) { 666 fatal("Nested VM operation %s requested by operation %s", 667 op->name(), vm_operation()->name()); 668 } 669 op->set_calling_thread(prev_vm_operation->calling_thread()); 670 } 671 672 EventMark em("Executing %s VM operation: %s", prev_vm_operation ? "nested" : "", op->name()); 673 674 // Release all internal handles after operation is evaluated 675 HandleMark hm(t); 676 _cur_vm_operation = op; 677 678 if (op->evaluate_at_safepoint() && !SafepointSynchronize::is_at_safepoint()) { 679 SafepointSynchronize::begin(); 680 op->evaluate(); 681 SafepointSynchronize::end(); 682 } else { 683 op->evaluate(); 684 } 685 686 _cur_vm_operation = prev_vm_operation; 687 } 688 } 689 690 void VMThread::verify() { 691 oops_do(&VerifyOopClosure::verify_oop, NULL); 692 }