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_thread_local_storage();
 244   this->initialize_named_thread();
 245   this->record_stack_base_and_size();
 246   // Notify_lock wait checks on active_handles() to rewait in
 247   // case of spurious wakeup, it should wait on the last
 248   // value set prior to the notify
 249   this->set_active_handles(JNIHandleBlock::allocate_block());
 250 
 251   {
 252     MutexLocker ml(Notify_lock);
 253     Notify_lock->notify();
 254   }
 255   // Notify_lock is destroyed by Threads::create_vm()
 256 
 257   int prio = (VMThreadPriority == -1)
 258     ? os::java_to_os_priority[NearMaxPriority]
 259     : VMThreadPriority;
 260   // Note that I cannot call os::set_priority because it expects Java
 261   // priorities and I am *explicitly* using OS priorities so that it's
 262   // possible to set the VM thread priority higher than any Java thread.
 263   os::set_native_priority( this, prio );
 264 
 265   // Wait for VM_Operations until termination
 266   this->loop();
 267 
 268   // Note the intention to exit before safepointing.
 269   // 6295565  This has the effect of waiting for any large tty
 270   // outputs to finish.
 271   if (xtty != NULL) {
 272     ttyLocker ttyl;
 273     xtty->begin_elem("destroy_vm");
 274     xtty->stamp();
 275     xtty->end_elem();
 276     assert(should_terminate(), "termination flag must be set");
 277   }
 278 
 279   // 4526887 let VM thread exit at Safepoint
 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     os::check_heap();
 287     // Silent verification so as not to pollute normal output,
 288     // unless we really asked for it.
 289     Universe::verify(!(PrintGCDetails || Verbose) || VerifySilently);
 290   }
 291 
 292   CompileBroker::set_should_block();
 293 
 294   // wait for threads (compiler threads or daemon threads) in the
 295   // _thread_in_native state to block.
 296   VM_Exit::wait_for_threads_in_native_to_block();
 297 
 298   // signal other threads that VM process is gone
 299   {
 300     // Note: we must have the _no_safepoint_check_flag. Mutex::lock() allows
 301     // VM thread to enter any lock at Safepoint as long as its _owner is NULL.
 302     // If that happens after _terminate_lock->wait() has unset _owner
 303     // but before it actually drops the lock and waits, the notification below
 304     // may get lost and we will have a hang. To avoid this, we need to use
 305     // Mutex::lock_without_safepoint_check().
 306     MutexLockerEx ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
 307     _terminated = true;
 308     _terminate_lock->notify();
 309   }
 310 
 311   // Thread destructor usually does this.
 312   ThreadLocalStorage::set_thread(NULL);
 313 
 314   // Deletion must be done synchronously by the JNI DestroyJavaVM thread
 315   // so that the VMThread deletion completes before the main thread frees
 316   // up the CodeHeap.
 317 
 318 }
 319 
 320 
 321 // Notify the VMThread that the last non-daemon JavaThread has terminated,
 322 // and wait until operation is performed.
 323 void VMThread::wait_for_vm_thread_exit() {
 324   { MutexLocker mu(VMOperationQueue_lock);
 325     _should_terminate = true;
 326     VMOperationQueue_lock->notify();
 327   }
 328 
 329   // Note: VM thread leaves at Safepoint. We are not stopped by Safepoint
 330   // because this thread has been removed from the threads list. But anything
 331   // that could get blocked by Safepoint should not be used after this point,
 332   // otherwise we will hang, since there is no one can end the safepoint.
 333 
 334   // Wait until VM thread is terminated
 335   // Note: it should be OK to use Terminator_lock here. But this is called
 336   // at a very delicate time (VM shutdown) and we are operating in non- VM
 337   // thread at Safepoint. It's safer to not share lock with other threads.
 338   { MutexLockerEx ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
 339     while(!VMThread::is_terminated()) {
 340         _terminate_lock->wait(Mutex::_no_safepoint_check_flag);
 341     }
 342   }
 343 }
 344 
 345 void VMThread::evaluate_operation(VM_Operation* op) {
 346   ResourceMark rm;
 347 
 348   {
 349     PerfTraceTime vm_op_timer(perf_accumulated_vm_operation_time());
 350     HOTSPOT_VMOPS_BEGIN(
 351                      (char *) op->name(), strlen(op->name()),
 352                      op->evaluation_mode());
 353 
 354     EventExecuteVMOperation event;
 355 
 356     op->evaluate();
 357 
 358     if (event.should_commit()) {
 359       bool is_concurrent = op->evaluate_concurrently();
 360       event.set_operation(op->type());
 361       event.set_safepoint(op->evaluate_at_safepoint());
 362       event.set_blocking(!is_concurrent);
 363       // Only write caller thread information for non-concurrent vm operations.
 364       // For concurrent vm operations, the thread id is set to 0 indicating thread is unknown.
 365       // This is because the caller thread could have exited already.
 366       event.set_caller(is_concurrent ? 0 : op->calling_thread()->osthread()->thread_id());
 367       event.commit();
 368     }
 369 
 370     HOTSPOT_VMOPS_END(
 371                      (char *) op->name(), strlen(op->name()),
 372                      op->evaluation_mode());
 373   }
 374 
 375   // Last access of info in _cur_vm_operation!
 376   bool c_heap_allocated = op->is_cheap_allocated();
 377 
 378   // Mark as completed
 379   if (!op->evaluate_concurrently()) {
 380     op->calling_thread()->increment_vm_operation_completed_count();
 381   }
 382   // It is unsafe to access the _cur_vm_operation after the 'increment_vm_operation_completed_count' call,
 383   // since if it is stack allocated the calling thread might have deallocated
 384   if (c_heap_allocated) {
 385     delete _cur_vm_operation;
 386   }
 387 }
 388 
 389 
 390 void VMThread::loop() {
 391   assert(_cur_vm_operation == NULL, "no current one should be executing");
 392 
 393   while(true) {
 394     VM_Operation* safepoint_ops = NULL;
 395     //
 396     // Wait for VM operation
 397     //
 398     // use no_safepoint_check to get lock without attempting to "sneak"
 399     { MutexLockerEx mu_queue(VMOperationQueue_lock,
 400                              Mutex::_no_safepoint_check_flag);
 401 
 402       // Look for new operation
 403       assert(_cur_vm_operation == NULL, "no current one should be executing");
 404       _cur_vm_operation = _vm_queue->remove_next();
 405 
 406       // Stall time tracking code
 407       if (PrintVMQWaitTime && _cur_vm_operation != NULL &&
 408           !_cur_vm_operation->evaluate_concurrently()) {
 409         long stall = os::javaTimeMillis() - _cur_vm_operation->timestamp();
 410         if (stall > 0)
 411           tty->print_cr("%s stall: %ld",  _cur_vm_operation->name(), stall);
 412       }
 413 
 414       while (!should_terminate() && _cur_vm_operation == NULL) {
 415         // wait with a timeout to guarantee safepoints at regular intervals
 416         bool timedout =
 417           VMOperationQueue_lock->wait(Mutex::_no_safepoint_check_flag,
 418                                       GuaranteedSafepointInterval);
 419 
 420         // Support for self destruction
 421         if ((SelfDestructTimer != 0) && !is_error_reported() &&
 422             (os::elapsedTime() > (double)SelfDestructTimer * 60.0)) {
 423           tty->print_cr("VM self-destructed");
 424           exit(-1);
 425         }
 426 
 427         if (timedout && (SafepointALot ||
 428                          SafepointSynchronize::is_cleanup_needed())) {
 429           MutexUnlockerEx mul(VMOperationQueue_lock,
 430                               Mutex::_no_safepoint_check_flag);
 431           // Force a safepoint since we have not had one for at least
 432           // 'GuaranteedSafepointInterval' milliseconds.  This will run all
 433           // the clean-up processing that needs to be done regularly at a
 434           // safepoint
 435           SafepointSynchronize::begin();
 436           #ifdef ASSERT
 437             if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot();
 438           #endif
 439           SafepointSynchronize::end();
 440         }
 441         _cur_vm_operation = _vm_queue->remove_next();
 442 
 443         // If we are at a safepoint we will evaluate all the operations that
 444         // follow that also require a safepoint
 445         if (_cur_vm_operation != NULL &&
 446             _cur_vm_operation->evaluate_at_safepoint()) {
 447           safepoint_ops = _vm_queue->drain_at_safepoint_priority();
 448         }
 449       }
 450 
 451       if (should_terminate()) break;
 452     } // Release mu_queue_lock
 453 
 454     //
 455     // Execute VM operation
 456     //
 457     { HandleMark hm(VMThread::vm_thread());
 458 
 459       EventMark em("Executing VM operation: %s", vm_operation()->name());
 460       assert(_cur_vm_operation != NULL, "we should have found an operation to execute");
 461 
 462       // Give the VM thread an extra quantum.  Jobs tend to be bursty and this
 463       // helps the VM thread to finish up the job.
 464       // FIXME: When this is enabled and there are many threads, this can degrade
 465       // performance significantly.
 466       if( VMThreadHintNoPreempt )
 467         os::hint_no_preempt();
 468 
 469       // If we are at a safepoint we will evaluate all the operations that
 470       // follow that also require a safepoint
 471       if (_cur_vm_operation->evaluate_at_safepoint()) {
 472 
 473         _vm_queue->set_drain_list(safepoint_ops); // ensure ops can be scanned
 474 
 475         SafepointSynchronize::begin();
 476         evaluate_operation(_cur_vm_operation);
 477         // now process all queued safepoint ops, iteratively draining
 478         // the queue until there are none left
 479         do {
 480           _cur_vm_operation = safepoint_ops;
 481           if (_cur_vm_operation != NULL) {
 482             do {
 483               // evaluate_operation deletes the op object so we have
 484               // to grab the next op now
 485               VM_Operation* next = _cur_vm_operation->next();
 486               _vm_queue->set_drain_list(next);
 487               evaluate_operation(_cur_vm_operation);
 488               _cur_vm_operation = next;
 489               if (PrintSafepointStatistics) {
 490                 SafepointSynchronize::inc_vmop_coalesced_count();
 491               }
 492             } while (_cur_vm_operation != NULL);
 493           }
 494           // There is a chance that a thread enqueued a safepoint op
 495           // since we released the op-queue lock and initiated the safepoint.
 496           // So we drain the queue again if there is anything there, as an
 497           // optimization to try and reduce the number of safepoints.
 498           // As the safepoint synchronizes us with JavaThreads we will see
 499           // any enqueue made by a JavaThread, but the peek will not
 500           // necessarily detect a concurrent enqueue by a GC thread, but
 501           // that simply means the op will wait for the next major cycle of the
 502           // VMThread - just as it would if the GC thread lost the race for
 503           // the lock.
 504           if (_vm_queue->peek_at_safepoint_priority()) {
 505             // must hold lock while draining queue
 506             MutexLockerEx mu_queue(VMOperationQueue_lock,
 507                                      Mutex::_no_safepoint_check_flag);
 508             safepoint_ops = _vm_queue->drain_at_safepoint_priority();
 509           } else {
 510             safepoint_ops = NULL;
 511           }
 512         } while(safepoint_ops != NULL);
 513 
 514         _vm_queue->set_drain_list(NULL);
 515 
 516         // Complete safepoint synchronization
 517         SafepointSynchronize::end();
 518 
 519       } else {  // not a safepoint operation
 520         if (TraceLongCompiles) {
 521           elapsedTimer t;
 522           t.start();
 523           evaluate_operation(_cur_vm_operation);
 524           t.stop();
 525           double secs = t.seconds();
 526           if (secs * 1e3 > LongCompileThreshold) {
 527             // XXX - _cur_vm_operation should not be accessed after
 528             // the completed count has been incremented; the waiting
 529             // thread may have already freed this memory.
 530             tty->print_cr("vm %s: %3.7f secs]", _cur_vm_operation->name(), secs);
 531           }
 532         } else {
 533           evaluate_operation(_cur_vm_operation);
 534         }
 535 
 536         _cur_vm_operation = NULL;
 537       }
 538     }
 539 
 540     //
 541     //  Notify (potential) waiting Java thread(s) - lock without safepoint
 542     //  check so that sneaking is not possible
 543     { MutexLockerEx mu(VMOperationRequest_lock,
 544                        Mutex::_no_safepoint_check_flag);
 545       VMOperationRequest_lock->notify_all();
 546     }
 547 
 548     //
 549     // We want to make sure that we get to a safepoint regularly.
 550     //
 551     if (SafepointALot || SafepointSynchronize::is_cleanup_needed()) {
 552       long interval          = SafepointSynchronize::last_non_safepoint_interval();
 553       bool max_time_exceeded = GuaranteedSafepointInterval != 0 && (interval > GuaranteedSafepointInterval);
 554       if (SafepointALot || max_time_exceeded) {
 555         HandleMark hm(VMThread::vm_thread());
 556         SafepointSynchronize::begin();
 557         SafepointSynchronize::end();
 558       }
 559     }
 560   }
 561 }
 562 
 563 void VMThread::execute(VM_Operation* op) {
 564   Thread* t = Thread::current();
 565 
 566   if (!t->is_VM_thread()) {
 567     SkipGCALot sgcalot(t);    // avoid re-entrant attempts to gc-a-lot
 568     // JavaThread or WatcherThread
 569     bool concurrent = op->evaluate_concurrently();
 570     // only blocking VM operations need to verify the caller's safepoint state:
 571     if (!concurrent) {
 572       t->check_for_valid_safepoint_state(true);
 573     }
 574 
 575     // New request from Java thread, evaluate prologue
 576     if (!op->doit_prologue()) {
 577       return;   // op was cancelled
 578     }
 579 
 580     // Setup VM_operations for execution
 581     op->set_calling_thread(t, Thread::get_priority(t));
 582 
 583     // It does not make sense to execute the epilogue, if the VM operation object is getting
 584     // deallocated by the VM thread.
 585     bool execute_epilog = !op->is_cheap_allocated();
 586     assert(!concurrent || op->is_cheap_allocated(), "concurrent => cheap_allocated");
 587 
 588     // Get ticket number for non-concurrent VM operations
 589     int ticket = 0;
 590     if (!concurrent) {
 591       ticket = t->vm_operation_ticket();
 592     }
 593 
 594     // Add VM operation to list of waiting threads. We are guaranteed not to block while holding the
 595     // VMOperationQueue_lock, so we can block without a safepoint check. This allows vm operation requests
 596     // to be queued up during a safepoint synchronization.
 597     {
 598       VMOperationQueue_lock->lock_without_safepoint_check();
 599       bool ok = _vm_queue->add(op);
 600     op->set_timestamp(os::javaTimeMillis());
 601       VMOperationQueue_lock->notify();
 602       VMOperationQueue_lock->unlock();
 603       // VM_Operation got skipped
 604       if (!ok) {
 605         assert(concurrent, "can only skip concurrent tasks");
 606         if (op->is_cheap_allocated()) delete op;
 607         return;
 608       }
 609     }
 610 
 611     if (!concurrent) {
 612       // Wait for completion of request (non-concurrent)
 613       // Note: only a JavaThread triggers the safepoint check when locking
 614       MutexLocker mu(VMOperationRequest_lock);
 615       while(t->vm_operation_completed_count() < ticket) {
 616         VMOperationRequest_lock->wait(!t->is_Java_thread());
 617       }
 618     }
 619 
 620     if (execute_epilog) {
 621       op->doit_epilogue();
 622     }
 623   } else {
 624     // invoked by VM thread; usually nested VM operation
 625     assert(t->is_VM_thread(), "must be a VM thread");
 626     VM_Operation* prev_vm_operation = vm_operation();
 627     if (prev_vm_operation != NULL) {
 628       // Check the VM operation allows nested VM operation. This normally not the case, e.g., the compiler
 629       // does not allow nested scavenges or compiles.
 630       if (!prev_vm_operation->allow_nested_vm_operations()) {
 631         fatal("Nested VM operation %s requested by operation %s",
 632               op->name(), vm_operation()->name());
 633       }
 634       op->set_calling_thread(prev_vm_operation->calling_thread(), prev_vm_operation->priority());
 635     }
 636 
 637     EventMark em("Executing %s VM operation: %s", prev_vm_operation ? "nested" : "", op->name());
 638 
 639     // Release all internal handles after operation is evaluated
 640     HandleMark hm(t);
 641     _cur_vm_operation = op;
 642 
 643     if (op->evaluate_at_safepoint() && !SafepointSynchronize::is_at_safepoint()) {
 644       SafepointSynchronize::begin();
 645       op->evaluate();
 646       SafepointSynchronize::end();
 647     } else {
 648       op->evaluate();
 649     }
 650 
 651     // Free memory if needed
 652     if (op->is_cheap_allocated()) delete op;
 653 
 654     _cur_vm_operation = prev_vm_operation;
 655   }
 656 }
 657 
 658 
 659 void VMThread::oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) {
 660   Thread::oops_do(f, cld_f, cf);
 661   _vm_queue->oops_do(f);
 662 }
 663 
 664 //------------------------------------------------------------------------------------------------------------------
 665 #ifndef PRODUCT
 666 
 667 void VMOperationQueue::verify_queue(int prio) {
 668   // Check that list is correctly linked
 669   int length = _queue_length[prio];
 670   VM_Operation *cur = _queue[prio];
 671   int i;
 672 
 673   // Check forward links
 674   for(i = 0; i < length; i++) {
 675     cur = cur->next();
 676     assert(cur != _queue[prio], "list to short (forward)");
 677   }
 678   assert(cur->next() == _queue[prio], "list to long (forward)");
 679 
 680   // Check backwards links
 681   cur = _queue[prio];
 682   for(i = 0; i < length; i++) {
 683     cur = cur->prev();
 684     assert(cur != _queue[prio], "list to short (backwards)");
 685   }
 686   assert(cur->prev() == _queue[prio], "list to long (backwards)");
 687 }
 688 
 689 #endif
 690 
 691 void VMThread::verify() {
 692   oops_do(&VerifyOopClosure::verify_oop, NULL, NULL);
 693 }