< prev index next >

src/share/vm/runtime/vmThread.cpp

Print this page




   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 {


 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 


 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     }




   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 "logging/log.hpp"
  29 #include "logging/logConfiguration.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/method.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "oops/verifyOopClosure.hpp"
  34 #include "runtime/interfaceSupport.hpp"
  35 #include "runtime/mutexLocker.hpp"
  36 #include "runtime/os.hpp"
  37 #include "runtime/safepoint.hpp"
  38 #include "runtime/thread.inline.hpp"
  39 #include "runtime/vmThread.hpp"
  40 #include "runtime/vm_operations.hpp"
  41 #include "services/runtimeService.hpp"
  42 #include "trace/tracing.hpp"
  43 #include "utilities/dtrace.hpp"
  44 #include "utilities/events.hpp"
  45 #include "utilities/vmError.hpp"
  46 #include "utilities/xmlstream.hpp"
  47 
  48 // Dummy VM operation to act as first element in our circular double-linked list
  49 class VM_Dummy: public VM_Operation {


 469     } // Release mu_queue_lock
 470 
 471     //
 472     // Execute VM operation
 473     //
 474     { HandleMark hm(VMThread::vm_thread());
 475 
 476       EventMark em("Executing VM operation: %s", vm_operation()->name());
 477       assert(_cur_vm_operation != NULL, "we should have found an operation to execute");
 478 
 479       // Give the VM thread an extra quantum.  Jobs tend to be bursty and this
 480       // helps the VM thread to finish up the job.
 481       // FIXME: When this is enabled and there are many threads, this can degrade
 482       // performance significantly.
 483       if( VMThreadHintNoPreempt )
 484         os::hint_no_preempt();
 485 
 486       // If we are at a safepoint we will evaluate all the operations that
 487       // follow that also require a safepoint
 488       if (_cur_vm_operation->evaluate_at_safepoint()) {
 489         log_debug(vmthread)("Evaluating safepoint VM operation: %s", _cur_vm_operation->name());
 490 
 491         _vm_queue->set_drain_list(safepoint_ops); // ensure ops can be scanned
 492 
 493         SafepointSynchronize::begin();
 494         evaluate_operation(_cur_vm_operation);
 495         // now process all queued safepoint ops, iteratively draining
 496         // the queue until there are none left
 497         do {
 498           _cur_vm_operation = safepoint_ops;
 499           if (_cur_vm_operation != NULL) {
 500             do {
 501               log_debug(vmthread)("Evaluating coalesced safepoint VM operation: %s", _cur_vm_operation->name());
 502               // evaluate_operation deletes the op object so we have
 503               // to grab the next op now
 504               VM_Operation* next = _cur_vm_operation->next();
 505               _vm_queue->set_drain_list(next);
 506               evaluate_operation(_cur_vm_operation);
 507               _cur_vm_operation = next;
 508               if (PrintSafepointStatistics) {
 509                 SafepointSynchronize::inc_vmop_coalesced_count();
 510               }
 511             } while (_cur_vm_operation != NULL);
 512           }
 513           // There is a chance that a thread enqueued a safepoint op
 514           // since we released the op-queue lock and initiated the safepoint.
 515           // So we drain the queue again if there is anything there, as an
 516           // optimization to try and reduce the number of safepoints.
 517           // As the safepoint synchronizes us with JavaThreads we will see
 518           // any enqueue made by a JavaThread, but the peek will not
 519           // necessarily detect a concurrent enqueue by a GC thread, but
 520           // that simply means the op will wait for the next major cycle of the
 521           // VMThread - just as it would if the GC thread lost the race for
 522           // the lock.
 523           if (_vm_queue->peek_at_safepoint_priority()) {
 524             // must hold lock while draining queue
 525             MutexLockerEx mu_queue(VMOperationQueue_lock,
 526                                      Mutex::_no_safepoint_check_flag);
 527             safepoint_ops = _vm_queue->drain_at_safepoint_priority();
 528           } else {
 529             safepoint_ops = NULL;
 530           }
 531         } while(safepoint_ops != NULL);
 532 
 533         _vm_queue->set_drain_list(NULL);
 534 
 535         // Complete safepoint synchronization
 536         SafepointSynchronize::end();
 537 
 538       } else {  // not a safepoint operation
 539         log_debug(vmthread)("Evaluating non-safepoint VM operation: %s", _cur_vm_operation->name());
 540         if (TraceLongCompiles) {
 541           elapsedTimer t;
 542           t.start();
 543           evaluate_operation(_cur_vm_operation);
 544           t.stop();
 545           double secs = t.seconds();
 546           if (secs * 1e3 > LongCompileThreshold) {
 547             // XXX - _cur_vm_operation should not be accessed after
 548             // the completed count has been incremented; the waiting
 549             // thread may have already freed this memory.
 550             tty->print_cr("vm %s: %3.7f secs]", _cur_vm_operation->name(), secs);
 551           }
 552         } else {
 553           evaluate_operation(_cur_vm_operation);
 554         }
 555 
 556         _cur_vm_operation = NULL;
 557       }
 558     }
 559 


 595 
 596     // Setup VM_operations for execution
 597     op->set_calling_thread(t, Thread::get_priority(t));
 598 
 599     // It does not make sense to execute the epilogue, if the VM operation object is getting
 600     // deallocated by the VM thread.
 601     bool execute_epilog = !op->is_cheap_allocated();
 602     assert(!concurrent || op->is_cheap_allocated(), "concurrent => cheap_allocated");
 603 
 604     // Get ticket number for non-concurrent VM operations
 605     int ticket = 0;
 606     if (!concurrent) {
 607       ticket = t->vm_operation_ticket();
 608     }
 609 
 610     // Add VM operation to list of waiting threads. We are guaranteed not to block while holding the
 611     // VMOperationQueue_lock, so we can block without a safepoint check. This allows vm operation requests
 612     // to be queued up during a safepoint synchronization.
 613     {
 614       VMOperationQueue_lock->lock_without_safepoint_check();
 615       log_debug(vmthread)("Adding VM operation: %s", op->name());
 616       bool ok = _vm_queue->add(op);
 617       op->set_timestamp(os::javaTimeMillis());
 618       VMOperationQueue_lock->notify();
 619       VMOperationQueue_lock->unlock();
 620       // VM_Operation got skipped
 621       if (!ok) {
 622         assert(concurrent, "can only skip concurrent tasks");
 623         if (op->is_cheap_allocated()) delete op;
 624         return;
 625       }
 626     }
 627 
 628     if (!concurrent) {
 629       // Wait for completion of request (non-concurrent)
 630       // Note: only a JavaThread triggers the safepoint check when locking
 631       MutexLocker mu(VMOperationRequest_lock);
 632       while(t->vm_operation_completed_count() < ticket) {
 633         VMOperationRequest_lock->wait(!t->is_Java_thread());
 634       }
 635     }


< prev index next >