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