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