1 /*
   2  * Copyright (c) 1998, 2015, 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/thread.inline.hpp"
  36 #include "runtime/vmThread.hpp"
  37 #include "runtime/vm_operations.hpp"
  38 #include "services/runtimeService.hpp"
  39 #include "trace/tracing.hpp"
  40 #include "utilities/dtrace.hpp"
  41 #include "utilities/events.hpp"
  42 #include "utilities/xmlstream.hpp"
  43 
  44 // Dummy VM operation to act as first element in our circular double-linked list
  45 class VM_Dummy: public VM_Operation {
  46   VMOp_Type type() const { return VMOp_Dummy; }
  47   void  doit() {};
  48 };
  49 
  50 VMOperationQueue::VMOperationQueue() {
  51   // The queue is a circular doubled-linked list, which always contains
  52   // one element (i.e., one element means empty).
  53   for(int i = 0; i < nof_priorities; i++) {
  54     _queue_length[i] = 0;
  55     _queue_counter = 0;
  56     _queue[i] = new VM_Dummy();
  57     _queue[i]->set_next(_queue[i]);
  58     _queue[i]->set_prev(_queue[i]);
  59   }
  60   _drain_list = NULL;
  61 }
  62 
  63 
  64 bool VMOperationQueue::queue_empty(int prio) {
  65   // It is empty if there is exactly one element
  66   bool empty = (_queue[prio] == _queue[prio]->next());
  67   assert( (_queue_length[prio] == 0 && empty) ||
  68           (_queue_length[prio] > 0  && !empty), "sanity check");
  69   return _queue_length[prio] == 0;
  70 }
  71 
  72 // Inserts an element to the right of the q element
  73 void VMOperationQueue::insert(VM_Operation* q, VM_Operation* n) {
  74   assert(q->next()->prev() == q && q->prev()->next() == q, "sanity check");
  75   n->set_prev(q);
  76   n->set_next(q->next());
  77   q->next()->set_prev(n);
  78   q->set_next(n);
  79 }
  80 
  81 void VMOperationQueue::queue_add_front(int prio, VM_Operation *op) {
  82   _queue_length[prio]++;
  83   insert(_queue[prio]->next(), op);
  84 }
  85 
  86 void VMOperationQueue::queue_add_back(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 void VMOperationQueue::queue_oops_do(int queue, OopClosure* f) {
 132   VM_Operation* cur = _queue[queue];
 133   cur = cur->next();
 134   while (cur != _queue[queue]) {
 135     cur->oops_do(f);
 136     cur = cur->next();
 137   }
 138 }
 139 
 140 void VMOperationQueue::drain_list_oops_do(OopClosure* f) {
 141   VM_Operation* cur = _drain_list;
 142   while (cur != NULL) {
 143     cur->oops_do(f);
 144     cur = cur->next();
 145   }
 146 }
 147 
 148 //-----------------------------------------------------------------
 149 // High-level interface
 150 bool VMOperationQueue::add(VM_Operation *op) {
 151 
 152   HOTSPOT_VMOPS_REQUEST(
 153                    (char *) op->name(), strlen(op->name()),
 154                    op->evaluation_mode());
 155 
 156   // Encapsulates VM queue policy. Currently, that
 157   // only involves putting them on the right list
 158   if (op->evaluate_at_safepoint()) {
 159     queue_add_back(SafepointPriority, op);
 160     return true;
 161   }
 162 
 163   queue_add_back(MediumPriority, op);
 164   return true;
 165 }
 166 
 167 VM_Operation* VMOperationQueue::remove_next() {
 168   // Assuming VMOperation queue is two-level priority queue. If there are
 169   // more than two priorities, we need a different scheduling algorithm.
 170   assert(SafepointPriority == 0 && MediumPriority == 1 && nof_priorities == 2,
 171          "current algorithm does not work");
 172 
 173   // simple counter based scheduling to prevent starvation of lower priority
 174   // queue. -- see 4390175
 175   int high_prio, low_prio;
 176   if (_queue_counter++ < 10) {
 177       high_prio = SafepointPriority;
 178       low_prio  = MediumPriority;
 179   } else {
 180       _queue_counter = 0;
 181       high_prio = MediumPriority;
 182       low_prio  = SafepointPriority;
 183   }
 184 
 185   return queue_remove_front(queue_empty(high_prio) ? low_prio : high_prio);
 186 }
 187 
 188 void VMOperationQueue::oops_do(OopClosure* f) {
 189   for(int i = 0; i < nof_priorities; i++) {
 190     queue_oops_do(i, f);
 191   }
 192   drain_list_oops_do(f);
 193 }
 194 
 195 
 196 //------------------------------------------------------------------------------------------------------------------
 197 // Implementation of VMThread stuff
 198 
 199 bool                VMThread::_should_terminate   = false;
 200 bool              VMThread::_terminated         = false;
 201 Monitor*          VMThread::_terminate_lock     = NULL;
 202 VMThread*         VMThread::_vm_thread          = NULL;
 203 VM_Operation*     VMThread::_cur_vm_operation   = NULL;
 204 VMOperationQueue* VMThread::_vm_queue           = NULL;
 205 PerfCounter*      VMThread::_perf_accumulated_vm_operation_time = NULL;
 206 
 207 
 208 void VMThread::create() {
 209   assert(vm_thread() == NULL, "we can only allocate one VMThread");
 210   _vm_thread = new VMThread();
 211 
 212   // Create VM operation queue
 213   _vm_queue = new VMOperationQueue();
 214   guarantee(_vm_queue != NULL, "just checking");
 215 
 216   _terminate_lock = new Monitor(Mutex::safepoint, "VMThread::_terminate_lock", true,
 217                                 Monitor::_safepoint_check_never);
 218 
 219   if (UsePerfData) {
 220     // jvmstat performance counters
 221     Thread* THREAD = Thread::current();
 222     _perf_accumulated_vm_operation_time =
 223                  PerfDataManager::create_counter(SUN_THREADS, "vmOperationTime",
 224                                                  PerfData::U_Ticks, CHECK);
 225   }
 226 }
 227 
 228 
 229 VMThread::VMThread() : NamedThread() {
 230   set_name("VM Thread");
 231 }
 232 
 233 void VMThread::destroy() {
 234   if (_vm_thread != NULL) {
 235     delete _vm_thread;
 236     _vm_thread = NULL;      // VM thread is gone
 237   }
 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   SafepointSynchronize::begin();
 280 
 281   if (VerifyBeforeExit) {
 282     HandleMark hm(VMThread::vm_thread());
 283     // Among other things, this ensures that Eden top is correct.
 284     Universe::heap()->prepare_for_verify();
 285     os::check_heap();
 286     // Silent verification so as not to pollute normal output,
 287     // unless we really asked for it.
 288     Universe::verify(!(PrintGCDetails || Verbose) || VerifySilently);
 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   // Deletion must be done synchronously by the JNI DestroyJavaVM thread
 311   // so that the VMThread deletion completes before the main thread frees
 312   // up the CodeHeap.
 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 void VMThread::evaluate_operation(VM_Operation* op) {
 342   ResourceMark rm;
 343 
 344   {
 345     PerfTraceTime vm_op_timer(perf_accumulated_vm_operation_time());
 346     HOTSPOT_VMOPS_BEGIN(
 347                      (char *) op->name(), strlen(op->name()),
 348                      op->evaluation_mode());
 349 
 350     EventExecuteVMOperation event;
 351 
 352     op->evaluate();
 353 
 354     if (event.should_commit()) {
 355       bool is_concurrent = op->evaluate_concurrently();
 356       event.set_operation(op->type());
 357       event.set_safepoint(op->evaluate_at_safepoint());
 358       event.set_blocking(!is_concurrent);
 359       // Only write caller thread information for non-concurrent vm operations.
 360       // For concurrent vm operations, the thread id is set to 0 indicating thread is unknown.
 361       // This is because the caller thread could have exited already.
 362       event.set_caller(is_concurrent ? 0 : op->calling_thread()->osthread()->thread_id());
 363       event.commit();
 364     }
 365 
 366     HOTSPOT_VMOPS_END(
 367                      (char *) op->name(), strlen(op->name()),
 368                      op->evaluation_mode());
 369   }
 370 
 371   // Last access of info in _cur_vm_operation!
 372   bool c_heap_allocated = op->is_cheap_allocated();
 373 
 374   // Mark as completed
 375   if (!op->evaluate_concurrently()) {
 376     op->calling_thread()->increment_vm_operation_completed_count();
 377   }
 378   // It is unsafe to access the _cur_vm_operation after the 'increment_vm_operation_completed_count' call,
 379   // since if it is stack allocated the calling thread might have deallocated
 380   if (c_heap_allocated) {
 381     delete _cur_vm_operation;
 382   }
 383 }
 384 
 385 
 386 void VMThread::loop() {
 387   assert(_cur_vm_operation == NULL, "no current one should be executing");
 388 
 389   while(true) {
 390     VM_Operation* safepoint_ops = NULL;
 391     //
 392     // Wait for VM operation
 393     //
 394     // use no_safepoint_check to get lock without attempting to "sneak"
 395     { MutexLockerEx mu_queue(VMOperationQueue_lock,
 396                              Mutex::_no_safepoint_check_flag);
 397 
 398       // Look for new operation
 399       assert(_cur_vm_operation == NULL, "no current one should be executing");
 400       _cur_vm_operation = _vm_queue->remove_next();
 401 
 402       // Stall time tracking code
 403       if (PrintVMQWaitTime && _cur_vm_operation != NULL &&
 404           !_cur_vm_operation->evaluate_concurrently()) {
 405         long stall = os::javaTimeMillis() - _cur_vm_operation->timestamp();
 406         if (stall > 0)
 407           tty->print_cr("%s stall: %ld",  _cur_vm_operation->name(), stall);
 408       }
 409 
 410       while (!should_terminate() && _cur_vm_operation == NULL) {
 411         // wait with a timeout to guarantee safepoints at regular intervals
 412         bool timedout =
 413           VMOperationQueue_lock->wait(Mutex::_no_safepoint_check_flag,
 414                                       GuaranteedSafepointInterval);
 415 
 416         // Support for self destruction
 417         if ((SelfDestructTimer != 0) && !is_error_reported() &&
 418             (os::elapsedTime() > (double)SelfDestructTimer * 60.0)) {
 419           tty->print_cr("VM self-destructed");
 420           exit(-1);
 421         }
 422 
 423         if (timedout && (SafepointALot ||
 424                          SafepointSynchronize::is_cleanup_needed())) {
 425           MutexUnlockerEx mul(VMOperationQueue_lock,
 426                               Mutex::_no_safepoint_check_flag);
 427           // Force a safepoint since we have not had one for at least
 428           // 'GuaranteedSafepointInterval' milliseconds.  This will run all
 429           // the clean-up processing that needs to be done regularly at a
 430           // safepoint
 431           SafepointSynchronize::begin();
 432           #ifdef ASSERT
 433             if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot();
 434           #endif
 435           SafepointSynchronize::end();
 436         }
 437         _cur_vm_operation = _vm_queue->remove_next();
 438 
 439         // If we are at a safepoint we will evaluate all the operations that
 440         // follow that also require a safepoint
 441         if (_cur_vm_operation != NULL &&
 442             _cur_vm_operation->evaluate_at_safepoint()) {
 443           safepoint_ops = _vm_queue->drain_at_safepoint_priority();
 444         }
 445       }
 446 
 447       if (should_terminate()) break;
 448     } // Release mu_queue_lock
 449 
 450     //
 451     // Execute VM operation
 452     //
 453     { HandleMark hm(VMThread::vm_thread());
 454 
 455       EventMark em("Executing VM operation: %s", vm_operation()->name());
 456       assert(_cur_vm_operation != NULL, "we should have found an operation to execute");
 457 
 458       // Give the VM thread an extra quantum.  Jobs tend to be bursty and this
 459       // helps the VM thread to finish up the job.
 460       // FIXME: When this is enabled and there are many threads, this can degrade
 461       // performance significantly.
 462       if( VMThreadHintNoPreempt )
 463         os::hint_no_preempt();
 464 
 465       // If we are at a safepoint we will evaluate all the operations that
 466       // follow that also require a safepoint
 467       if (_cur_vm_operation->evaluate_at_safepoint()) {
 468 
 469         _vm_queue->set_drain_list(safepoint_ops); // ensure ops can be scanned
 470 
 471         SafepointSynchronize::begin();
 472         evaluate_operation(_cur_vm_operation);
 473         // now process all queued safepoint ops, iteratively draining
 474         // the queue until there are none left
 475         do {
 476           _cur_vm_operation = safepoint_ops;
 477           if (_cur_vm_operation != NULL) {
 478             do {
 479               // evaluate_operation deletes the op object so we have
 480               // to grab the next op now
 481               VM_Operation* next = _cur_vm_operation->next();
 482               _vm_queue->set_drain_list(next);
 483               evaluate_operation(_cur_vm_operation);
 484               _cur_vm_operation = next;
 485               if (PrintSafepointStatistics) {
 486                 SafepointSynchronize::inc_vmop_coalesced_count();
 487               }
 488             } while (_cur_vm_operation != NULL);
 489           }
 490           // There is a chance that a thread enqueued a safepoint op
 491           // since we released the op-queue lock and initiated the safepoint.
 492           // So we drain the queue again if there is anything there, as an
 493           // optimization to try and reduce the number of safepoints.
 494           // As the safepoint synchronizes us with JavaThreads we will see
 495           // any enqueue made by a JavaThread, but the peek will not
 496           // necessarily detect a concurrent enqueue by a GC thread, but
 497           // that simply means the op will wait for the next major cycle of the
 498           // VMThread - just as it would if the GC thread lost the race for
 499           // the lock.
 500           if (_vm_queue->peek_at_safepoint_priority()) {
 501             // must hold lock while draining queue
 502             MutexLockerEx mu_queue(VMOperationQueue_lock,
 503                                      Mutex::_no_safepoint_check_flag);
 504             safepoint_ops = _vm_queue->drain_at_safepoint_priority();
 505           } else {
 506             safepoint_ops = NULL;
 507           }
 508         } while(safepoint_ops != NULL);
 509 
 510         _vm_queue->set_drain_list(NULL);
 511 
 512         // Complete safepoint synchronization
 513         SafepointSynchronize::end();
 514 
 515       } else {  // not a safepoint operation
 516         if (TraceLongCompiles) {
 517           elapsedTimer t;
 518           t.start();
 519           evaluate_operation(_cur_vm_operation);
 520           t.stop();
 521           double secs = t.seconds();
 522           if (secs * 1e3 > LongCompileThreshold) {
 523             // XXX - _cur_vm_operation should not be accessed after
 524             // the completed count has been incremented; the waiting
 525             // thread may have already freed this memory.
 526             tty->print_cr("vm %s: %3.7f secs]", _cur_vm_operation->name(), secs);
 527           }
 528         } else {
 529           evaluate_operation(_cur_vm_operation);
 530         }
 531 
 532         _cur_vm_operation = NULL;
 533       }
 534     }
 535 
 536     //
 537     //  Notify (potential) waiting Java thread(s) - lock without safepoint
 538     //  check so that sneaking is not possible
 539     { MutexLockerEx mu(VMOperationRequest_lock,
 540                        Mutex::_no_safepoint_check_flag);
 541       VMOperationRequest_lock->notify_all();
 542     }
 543 
 544     //
 545     // We want to make sure that we get to a safepoint regularly.
 546     //
 547     if (SafepointALot || SafepointSynchronize::is_cleanup_needed()) {
 548       long interval          = SafepointSynchronize::last_non_safepoint_interval();
 549       bool max_time_exceeded = GuaranteedSafepointInterval != 0 && (interval > GuaranteedSafepointInterval);
 550       if (SafepointALot || max_time_exceeded) {
 551         HandleMark hm(VMThread::vm_thread());
 552         SafepointSynchronize::begin();
 553         SafepointSynchronize::end();
 554       }
 555     }
 556   }
 557 }
 558 
 559 void VMThread::execute(VM_Operation* op) {
 560   Thread* t = Thread::current();
 561 
 562   if (!t->is_VM_thread()) {
 563     SkipGCALot sgcalot(t);    // avoid re-entrant attempts to gc-a-lot
 564     // JavaThread or WatcherThread
 565     bool concurrent = op->evaluate_concurrently();
 566     // only blocking VM operations need to verify the caller's safepoint state:
 567     if (!concurrent) {
 568       t->check_for_valid_safepoint_state(true);
 569     }
 570 
 571     // New request from Java thread, evaluate prologue
 572     if (!op->doit_prologue()) {
 573       return;   // op was cancelled
 574     }
 575 
 576     // Setup VM_operations for execution
 577     op->set_calling_thread(t, Thread::get_priority(t));
 578 
 579     // It does not make sense to execute the epilogue, if the VM operation object is getting
 580     // deallocated by the VM thread.
 581     bool execute_epilog = !op->is_cheap_allocated();
 582     assert(!concurrent || op->is_cheap_allocated(), "concurrent => cheap_allocated");
 583 
 584     // Get ticket number for non-concurrent VM operations
 585     int ticket = 0;
 586     if (!concurrent) {
 587       ticket = t->vm_operation_ticket();
 588     }
 589 
 590     // Add VM operation to list of waiting threads. We are guaranteed not to block while holding the
 591     // VMOperationQueue_lock, so we can block without a safepoint check. This allows vm operation requests
 592     // to be queued up during a safepoint synchronization.
 593     {
 594       VMOperationQueue_lock->lock_without_safepoint_check();
 595       bool ok = _vm_queue->add(op);
 596     op->set_timestamp(os::javaTimeMillis());
 597       VMOperationQueue_lock->notify();
 598       VMOperationQueue_lock->unlock();
 599       // VM_Operation got skipped
 600       if (!ok) {
 601         assert(concurrent, "can only skip concurrent tasks");
 602         if (op->is_cheap_allocated()) delete op;
 603         return;
 604       }
 605     }
 606 
 607     if (!concurrent) {
 608       // Wait for completion of request (non-concurrent)
 609       // Note: only a JavaThread triggers the safepoint check when locking
 610       MutexLocker mu(VMOperationRequest_lock);
 611       while(t->vm_operation_completed_count() < ticket) {
 612         VMOperationRequest_lock->wait(!t->is_Java_thread());
 613       }
 614     }
 615 
 616     if (execute_epilog) {
 617       op->doit_epilogue();
 618     }
 619   } else {
 620     // invoked by VM thread; usually nested VM operation
 621     assert(t->is_VM_thread(), "must be a VM thread");
 622     VM_Operation* prev_vm_operation = vm_operation();
 623     if (prev_vm_operation != NULL) {
 624       // Check the VM operation allows nested VM operation. This normally not the case, e.g., the compiler
 625       // does not allow nested scavenges or compiles.
 626       if (!prev_vm_operation->allow_nested_vm_operations()) {
 627         fatal("Nested VM operation %s requested by operation %s",
 628               op->name(), vm_operation()->name());
 629       }
 630       op->set_calling_thread(prev_vm_operation->calling_thread(), prev_vm_operation->priority());
 631     }
 632 
 633     EventMark em("Executing %s VM operation: %s", prev_vm_operation ? "nested" : "", op->name());
 634 
 635     // Release all internal handles after operation is evaluated
 636     HandleMark hm(t);
 637     _cur_vm_operation = op;
 638 
 639     if (op->evaluate_at_safepoint() && !SafepointSynchronize::is_at_safepoint()) {
 640       SafepointSynchronize::begin();
 641       op->evaluate();
 642       SafepointSynchronize::end();
 643     } else {
 644       op->evaluate();
 645     }
 646 
 647     // Free memory if needed
 648     if (op->is_cheap_allocated()) delete op;
 649 
 650     _cur_vm_operation = prev_vm_operation;
 651   }
 652 }
 653 
 654 
 655 void VMThread::oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) {
 656   Thread::oops_do(f, cld_f, cf);
 657   _vm_queue->oops_do(f);
 658 }
 659 
 660 //------------------------------------------------------------------------------------------------------------------
 661 #ifndef PRODUCT
 662 
 663 void VMOperationQueue::verify_queue(int prio) {
 664   // Check that list is correctly linked
 665   int length = _queue_length[prio];
 666   VM_Operation *cur = _queue[prio];
 667   int i;
 668 
 669   // Check forward links
 670   for(i = 0; i < length; i++) {
 671     cur = cur->next();
 672     assert(cur != _queue[prio], "list to short (forward)");
 673   }
 674   assert(cur->next() == _queue[prio], "list to long (forward)");
 675 
 676   // Check backwards links
 677   cur = _queue[prio];
 678   for(i = 0; i < length; i++) {
 679     cur = cur->prev();
 680     assert(cur != _queue[prio], "list to short (backwards)");
 681   }
 682   assert(cur->prev() == _queue[prio], "list to long (backwards)");
 683 }
 684 
 685 #endif
 686 
 687 void VMThread::verify() {
 688   oops_do(&VerifyOopClosure::verify_oop, NULL, NULL);
 689 }