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