< prev index next >

src/hotspot/share/runtime/vmThread.cpp

Print this page




 403     long interval = SafepointSynchronize::last_non_safepoint_interval();
 404     bool max_time_exceeded = GuaranteedSafepointInterval != 0 &&
 405                              (interval > GuaranteedSafepointInterval);
 406     if (!max_time_exceeded) {
 407       return false;
 408     }
 409   }
 410   _no_op_reason = "Cleanup";
 411   return true;
 412 }
 413 
 414 void VMThread::loop() {
 415   assert(_cur_vm_operation == NULL, "no current one should be executing");
 416 
 417   while(true) {
 418     VM_Operation* safepoint_ops = NULL;
 419     //
 420     // Wait for VM operation
 421     //
 422     // use no_safepoint_check to get lock without attempting to "sneak"
 423     { MutexLockerEx mu_queue(VMOperationQueue_lock,
 424                              Mutex::_no_safepoint_check_flag);
 425 
 426       // Look for new operation
 427       assert(_cur_vm_operation == NULL, "no current one should be executing");
 428       _cur_vm_operation = _vm_queue->remove_next();
 429 
 430       // Stall time tracking code
 431       if (PrintVMQWaitTime && _cur_vm_operation != NULL &&
 432           !_cur_vm_operation->evaluate_concurrently()) {
 433         long stall = os::javaTimeMillis() - _cur_vm_operation->timestamp();
 434         if (stall > 0)
 435           tty->print_cr("%s stall: %ld",  _cur_vm_operation->name(), stall);
 436       }
 437 
 438       while (!should_terminate() && _cur_vm_operation == NULL) {
 439         // wait with a timeout to guarantee safepoints at regular intervals
 440         bool timedout =
 441           VMOperationQueue_lock->wait(Mutex::_no_safepoint_check_flag,
 442                                       GuaranteedSafepointInterval);
 443 
 444         // Support for self destruction
 445         if ((SelfDestructTimer != 0) && !VMError::is_error_reported() &&
 446             (os::elapsedTime() > (double)SelfDestructTimer * 60.0)) {
 447           tty->print_cr("VM self-destructed");
 448           exit(-1);
 449         }
 450 
 451         if (timedout && VMThread::no_op_safepoint_needed(false)) {
 452           MutexUnlockerEx mul(VMOperationQueue_lock,
 453                               Mutex::_no_safepoint_check_flag);
 454           // Force a safepoint since we have not had one for at least
 455           // 'GuaranteedSafepointInterval' milliseconds.  This will run all
 456           // the clean-up processing that needs to be done regularly at a
 457           // safepoint
 458           SafepointSynchronize::begin();
 459           #ifdef ASSERT
 460             if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot();
 461           #endif
 462           SafepointSynchronize::end();
 463         }
 464         _cur_vm_operation = _vm_queue->remove_next();
 465 
 466         // If we are at a safepoint we will evaluate all the operations that
 467         // follow that also require a safepoint
 468         if (_cur_vm_operation != NULL &&
 469             _cur_vm_operation->evaluate_at_safepoint()) {
 470           safepoint_ops = _vm_queue->drain_at_safepoint_priority();
 471         }
 472       }
 473 


 504               _vm_queue->set_drain_list(next);
 505               evaluate_operation(_cur_vm_operation);
 506               _cur_vm_operation = next;
 507               if (log_is_enabled(Debug, safepoint, stats)) {
 508                 SafepointSynchronize::inc_vmop_coalesced_count();
 509               }
 510             } while (_cur_vm_operation != NULL);
 511           }
 512           // There is a chance that a thread enqueued a safepoint op
 513           // since we released the op-queue lock and initiated the safepoint.
 514           // So we drain the queue again if there is anything there, as an
 515           // optimization to try and reduce the number of safepoints.
 516           // As the safepoint synchronizes us with JavaThreads we will see
 517           // any enqueue made by a JavaThread, but the peek will not
 518           // necessarily detect a concurrent enqueue by a GC thread, but
 519           // that simply means the op will wait for the next major cycle of the
 520           // VMThread - just as it would if the GC thread lost the race for
 521           // the lock.
 522           if (_vm_queue->peek_at_safepoint_priority()) {
 523             // must hold lock while draining queue
 524             MutexLockerEx mu_queue(VMOperationQueue_lock,
 525                                      Mutex::_no_safepoint_check_flag);
 526             safepoint_ops = _vm_queue->drain_at_safepoint_priority();
 527           } else {
 528             safepoint_ops = NULL;
 529           }
 530         } while(safepoint_ops != NULL);
 531 
 532         _vm_queue->set_drain_list(NULL);
 533 
 534         // Complete safepoint synchronization
 535         SafepointSynchronize::end();
 536 
 537       } else {  // not a safepoint operation
 538         log_debug(vmthread)("Evaluating non-safepoint VM operation: %s", _cur_vm_operation->name());
 539         if (TraceLongCompiles) {
 540           elapsedTimer t;
 541           t.start();
 542           evaluate_operation(_cur_vm_operation);
 543           t.stop();
 544           double secs = t.seconds();
 545           if (secs * 1e3 > LongCompileThreshold) {
 546             // XXX - _cur_vm_operation should not be accessed after
 547             // the completed count has been incremented; the waiting
 548             // thread may have already freed this memory.
 549             tty->print_cr("vm %s: %3.7f secs]", _cur_vm_operation->name(), secs);
 550           }
 551         } else {
 552           evaluate_operation(_cur_vm_operation);
 553         }
 554 
 555         _cur_vm_operation = NULL;
 556       }
 557     }
 558 
 559     //
 560     //  Notify (potential) waiting Java thread(s) - lock without safepoint
 561     //  check so that sneaking is not possible
 562     { MutexLockerEx mu(VMOperationRequest_lock,
 563                        Mutex::_no_safepoint_check_flag);
 564       VMOperationRequest_lock->notify_all();
 565     }
 566 
 567     //
 568     // We want to make sure that we get to a safepoint regularly.
 569     //
 570     if (VMThread::no_op_safepoint_needed(true)) {
 571       HandleMark hm(VMThread::vm_thread());
 572       SafepointSynchronize::begin();
 573       SafepointSynchronize::end();
 574     }
 575   }
 576 }
 577 
 578 // A SkipGCALot object is used to elide the usual effect of gc-a-lot
 579 // over a section of execution by a thread. Currently, it's used only to
 580 // prevent re-entrant calls to GC.
 581 class SkipGCALot : public StackObj {
 582   private:
 583    bool _saved;


 618     }
 619 
 620     // Setup VM_operations for execution
 621     op->set_calling_thread(t, Thread::get_priority(t));
 622 
 623     // It does not make sense to execute the epilogue, if the VM operation object is getting
 624     // deallocated by the VM thread.
 625     bool execute_epilog = !op->is_cheap_allocated();
 626     assert(!concurrent || op->is_cheap_allocated(), "concurrent => cheap_allocated");
 627 
 628     // Get ticket number for non-concurrent VM operations
 629     int ticket = 0;
 630     if (!concurrent) {
 631       ticket = t->vm_operation_ticket();
 632     }
 633 
 634     // Add VM operation to list of waiting threads. We are guaranteed not to block while holding the
 635     // VMOperationQueue_lock, so we can block without a safepoint check. This allows vm operation requests
 636     // to be queued up during a safepoint synchronization.
 637     {
 638       VMOperationQueue_lock->lock_without_safepoint_check();
 639       log_debug(vmthread)("Adding VM operation: %s", op->name());
 640       bool ok = _vm_queue->add(op);
 641       op->set_timestamp(os::javaTimeMillis());
 642       VMOperationQueue_lock->notify();
 643       VMOperationQueue_lock->unlock();
 644       // VM_Operation got skipped
 645       if (!ok) {
 646         assert(concurrent, "can only skip concurrent tasks");
 647         if (op->is_cheap_allocated()) delete op;
 648         return;
 649       }
 650     }
 651 
 652     if (!concurrent) {
 653       // Wait for completion of request (non-concurrent)
 654       // Note: only a JavaThread triggers the safepoint check when locking
 655       MutexLocker mu(VMOperationRequest_lock);
 656       while(t->vm_operation_completed_count() < ticket) {
 657         VMOperationRequest_lock->wait(!t->is_Java_thread());
 658       }




 403     long interval = SafepointSynchronize::last_non_safepoint_interval();
 404     bool max_time_exceeded = GuaranteedSafepointInterval != 0 &&
 405                              (interval > GuaranteedSafepointInterval);
 406     if (!max_time_exceeded) {
 407       return false;
 408     }
 409   }
 410   _no_op_reason = "Cleanup";
 411   return true;
 412 }
 413 
 414 void VMThread::loop() {
 415   assert(_cur_vm_operation == NULL, "no current one should be executing");
 416 
 417   while(true) {
 418     VM_Operation* safepoint_ops = NULL;
 419     //
 420     // Wait for VM operation
 421     //
 422     // use no_safepoint_check to get lock without attempting to "sneak"
 423     { MutexLocker mu_queue(VMOperationQueue_lock);

 424 
 425       // Look for new operation
 426       assert(_cur_vm_operation == NULL, "no current one should be executing");
 427       _cur_vm_operation = _vm_queue->remove_next();
 428 
 429       // Stall time tracking code
 430       if (PrintVMQWaitTime && _cur_vm_operation != NULL &&
 431           !_cur_vm_operation->evaluate_concurrently()) {
 432         long stall = os::javaTimeMillis() - _cur_vm_operation->timestamp();
 433         if (stall > 0)
 434           tty->print_cr("%s stall: %ld",  _cur_vm_operation->name(), stall);
 435       }
 436 
 437       while (!should_terminate() && _cur_vm_operation == NULL) {
 438         // wait with a timeout to guarantee safepoints at regular intervals
 439         bool timedout =
 440           VMOperationQueue_lock->wait(Mutex::_no_safepoint_check_flag,
 441                                       GuaranteedSafepointInterval);
 442 
 443         // Support for self destruction
 444         if ((SelfDestructTimer != 0) && !VMError::is_error_reported() &&
 445             (os::elapsedTime() > (double)SelfDestructTimer * 60.0)) {
 446           tty->print_cr("VM self-destructed");
 447           exit(-1);
 448         }
 449 
 450         if (timedout && VMThread::no_op_safepoint_needed(false)) {
 451           MutexUnlocker mul(VMOperationQueue_lock);

 452           // Force a safepoint since we have not had one for at least
 453           // 'GuaranteedSafepointInterval' milliseconds.  This will run all
 454           // the clean-up processing that needs to be done regularly at a
 455           // safepoint
 456           SafepointSynchronize::begin();
 457           #ifdef ASSERT
 458             if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot();
 459           #endif
 460           SafepointSynchronize::end();
 461         }
 462         _cur_vm_operation = _vm_queue->remove_next();
 463 
 464         // If we are at a safepoint we will evaluate all the operations that
 465         // follow that also require a safepoint
 466         if (_cur_vm_operation != NULL &&
 467             _cur_vm_operation->evaluate_at_safepoint()) {
 468           safepoint_ops = _vm_queue->drain_at_safepoint_priority();
 469         }
 470       }
 471 


 502               _vm_queue->set_drain_list(next);
 503               evaluate_operation(_cur_vm_operation);
 504               _cur_vm_operation = next;
 505               if (log_is_enabled(Debug, safepoint, stats)) {
 506                 SafepointSynchronize::inc_vmop_coalesced_count();
 507               }
 508             } while (_cur_vm_operation != NULL);
 509           }
 510           // There is a chance that a thread enqueued a safepoint op
 511           // since we released the op-queue lock and initiated the safepoint.
 512           // So we drain the queue again if there is anything there, as an
 513           // optimization to try and reduce the number of safepoints.
 514           // As the safepoint synchronizes us with JavaThreads we will see
 515           // any enqueue made by a JavaThread, but the peek will not
 516           // necessarily detect a concurrent enqueue by a GC thread, but
 517           // that simply means the op will wait for the next major cycle of the
 518           // VMThread - just as it would if the GC thread lost the race for
 519           // the lock.
 520           if (_vm_queue->peek_at_safepoint_priority()) {
 521             // must hold lock while draining queue
 522             MutexLocker mu_queue(VMOperationQueue_lock);

 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         log_debug(vmthread)("Evaluating non-safepoint VM operation: %s", _cur_vm_operation->name());
 536         if (TraceLongCompiles) {
 537           elapsedTimer t;
 538           t.start();
 539           evaluate_operation(_cur_vm_operation);
 540           t.stop();
 541           double secs = t.seconds();
 542           if (secs * 1e3 > LongCompileThreshold) {
 543             // XXX - _cur_vm_operation should not be accessed after
 544             // the completed count has been incremented; the waiting
 545             // thread may have already freed this memory.
 546             tty->print_cr("vm %s: %3.7f secs]", _cur_vm_operation->name(), secs);
 547           }
 548         } else {
 549           evaluate_operation(_cur_vm_operation);
 550         }
 551 
 552         _cur_vm_operation = NULL;
 553       }
 554     }
 555 
 556     //
 557     //  Notify (potential) waiting Java thread(s) - lock without safepoint
 558     //  check so that sneaking is not possible
 559     { MutexLockerEx mu(VMOperationRequest_lock);

 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 // A SkipGCALot object is used to elide the usual effect of gc-a-lot
 575 // over a section of execution by a thread. Currently, it's used only to
 576 // prevent re-entrant calls to GC.
 577 class SkipGCALot : public StackObj {
 578   private:
 579    bool _saved;


 614     }
 615 
 616     // Setup VM_operations for execution
 617     op->set_calling_thread(t, Thread::get_priority(t));
 618 
 619     // It does not make sense to execute the epilogue, if the VM operation object is getting
 620     // deallocated by the VM thread.
 621     bool execute_epilog = !op->is_cheap_allocated();
 622     assert(!concurrent || op->is_cheap_allocated(), "concurrent => cheap_allocated");
 623 
 624     // Get ticket number for non-concurrent VM operations
 625     int ticket = 0;
 626     if (!concurrent) {
 627       ticket = t->vm_operation_ticket();
 628     }
 629 
 630     // Add VM operation to list of waiting threads. We are guaranteed not to block while holding the
 631     // VMOperationQueue_lock, so we can block without a safepoint check. This allows vm operation requests
 632     // to be queued up during a safepoint synchronization.
 633     {
 634       VMOperationQueue_lock->lock();
 635       log_debug(vmthread)("Adding VM operation: %s", op->name());
 636       bool ok = _vm_queue->add(op);
 637       op->set_timestamp(os::javaTimeMillis());
 638       VMOperationQueue_lock->notify();
 639       VMOperationQueue_lock->unlock();
 640       // VM_Operation got skipped
 641       if (!ok) {
 642         assert(concurrent, "can only skip concurrent tasks");
 643         if (op->is_cheap_allocated()) delete op;
 644         return;
 645       }
 646     }
 647 
 648     if (!concurrent) {
 649       // Wait for completion of request (non-concurrent)
 650       // Note: only a JavaThread triggers the safepoint check when locking
 651       MutexLocker mu(VMOperationRequest_lock);
 652       while(t->vm_operation_completed_count() < ticket) {
 653         VMOperationRequest_lock->wait(!t->is_Java_thread());
 654       }


< prev index next >