/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "compiler/compileBroker.hpp"
#include "gc_interface/collectedHeap.hpp"
#include "memory/resourceArea.hpp"
#include "oops/methodOop.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/interfaceSupport.hpp"
#include "runtime/mutexLocker.hpp"
#include "runtime/vmThread.hpp"
#include "runtime/vm_operations.hpp"
#include "services/runtimeService.hpp"
#include "trace/tracing.hpp"
#include "utilities/dtrace.hpp"
#include "utilities/events.hpp"
#include "utilities/xmlstream.hpp"
#ifdef TARGET_OS_FAMILY_linux
# include "thread_linux.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_solaris
# include "thread_solaris.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_windows
# include "thread_windows.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_bsd
# include "thread_bsd.inline.hpp"
#endif
#ifndef USDT2
#endif /* !USDT2 */
// Dummy VM operation to act as first element in our circular double-linked list
void doit() {};
};
// The queue is a circular doubled-linked list, which always contains
// one element (i.e., one element means empty).
for(int i = 0; i < nof_priorities; i++) {
_queue_length[i] = 0;
_queue_counter = 0;
}
_drain_list = NULL;
}
// It is empty if there is exactly one element
return _queue_length[prio] == 0;
}
// Inserts an element to the right of the q element
n->set_prev(q);
q->set_next(n);
}
_queue_length[prio]++;
}
_queue_length[prio]++;
}
}
_queue_length[prio]--;
unlink(r);
return r;
}
_queue_length[prio] = 0;
// remove links to base element from head and tail
// restore queue to empty state
#ifdef DEBUG
int len = 0;
#endif
return r;
}
}
}
}
}
//-----------------------------------------------------------------
// High-level interface
#ifndef USDT2
op->evaluation_mode());
#else /* USDT2 */
op->evaluation_mode());
#endif /* USDT2 */
// Encapsulates VM queue policy. Currently, that
// only involves putting them on the right list
if (op->evaluate_at_safepoint()) {
return true;
}
return true;
}
// Assuming VMOperation queue is two-level priority queue. If there are
// more than two priorities, we need a different scheduling algorithm.
"current algorithm does not work");
// simple counter based scheduling to prevent starvation of lower priority
// queue. -- see 4390175
if (_queue_counter++ < 10) {
} else {
_queue_counter = 0;
}
}
for(int i = 0; i < nof_priorities; i++) {
queue_oops_do(i, f);
}
}
//------------------------------------------------------------------------------------------------------------------
// Implementation of VMThread stuff
_vm_thread = new VMThread();
// Create VM operation queue
_vm_queue = new VMOperationQueue();
if (UsePerfData) {
// jvmstat performance counters
}
}
set_name("VM Thread");
}
if (_vm_thread != NULL) {
delete _vm_thread;
}
}
this->initialize_thread_local_storage();
this->record_stack_base_and_size();
// Notify_lock wait checks on active_handles() to rewait in
// case of spurious wakeup, it should wait on the last
// value set prior to the notify
{
Notify_lock->notify();
}
// Notify_lock is destroyed by Threads::create_vm()
// Note that I cannot call os::set_priority because it expects Java
// priorities and I am *explicitly* using OS priorities so that it's
// possible to set the VM thread priority higher than any Java thread.
// Wait for VM_Operations until termination
this->loop();
// Note the intention to exit before safepointing.
// 6295565 This has the effect of waiting for any large tty
// outputs to finish.
}
// 4526887 let VM thread exit at Safepoint
if (VerifyBeforeExit) {
// Among other things, this ensures that Eden top is correct.
os::check_heap();
// Silent verification so as not to pollute normal output,
// unless we really asked for it.
}
// wait for threads (compiler threads or daemon threads) in the
// _thread_in_native state to block.
// signal other threads that VM process is gone
{
// Note: we must have the _no_safepoint_check_flag. Mutex::lock() allows
// VM thread to enter any lock at Safepoint as long as its _owner is NULL.
// If that happens after _terminate_lock->wait() has unset _owner
// but before it actually drops the lock and waits, the notification below
// may get lost and we will have a hang. To avoid this, we need to use
// Mutex::lock_without_safepoint_check().
_terminated = true;
}
// Deletion must be done synchronously by the JNI DestroyJavaVM thread
// so that the VMThread deletion completes before the main thread frees
// up the CodeHeap.
}
// Notify the VMThread that the last non-daemon JavaThread has terminated,
// and wait until operation is performed.
_should_terminate = true;
}
// Note: VM thread leaves at Safepoint. We are not stopped by Safepoint
// because this thread has been removed from the threads list. But anything
// that could get blocked by Safepoint should not be used after this point,
// otherwise we will hang, since there is no one can end the safepoint.
// Wait until VM thread is terminated
// Note: it should be OK to use Terminator_lock here. But this is called
// at a very delicate time (VM shutdown) and we are operating in non- VM
// thread at Safepoint. It's safer to not share lock with other threads.
while(!VMThread::is_terminated()) {
}
}
}
}
{
#ifndef USDT2
op->evaluation_mode());
#else /* USDT2 */
op->evaluation_mode());
#endif /* USDT2 */
if (event.should_commit()) {
// Only write caller thread information for non-concurrent vm operations.
// For concurrent vm operations, the thread id is set to 0 indicating thread is unknown.
// This is because the caller thread could have exited already.
}
#ifndef USDT2
op->evaluation_mode());
#else /* USDT2 */
op->evaluation_mode());
#endif /* USDT2 */
}
// Last access of info in _cur_vm_operation!
// Mark as completed
if (!op->evaluate_concurrently()) {
}
// It is unsafe to access the _cur_vm_operation after the 'increment_vm_operation_completed_count' call,
// since if it is stack allocated the calling thread might have deallocated
if (c_heap_allocated) {
delete _cur_vm_operation;
}
}
while(true) {
//
// Wait for VM operation
//
// use no_safepoint_check to get lock without attempting to "sneak"
// Look for new operation
// Stall time tracking code
if (stall > 0)
}
// wait with a timeout to guarantee safepoints at regular intervals
bool timedout =
// Support for self destruction
if ((SelfDestructTimer != 0) && !is_error_reported() &&
exit(-1);
}
if (timedout && (SafepointALot ||
// Force a safepoint since we have not had one for at least
// 'GuaranteedSafepointInterval' milliseconds. This will run all
// the clean-up processing that needs to be done regularly at a
// safepoint
#ifdef ASSERT
#endif
}
// If we are at a safepoint we will evaluate all the operations that
// follow that also require a safepoint
if (_cur_vm_operation != NULL &&
}
}
if (should_terminate()) break;
} // Release mu_queue_lock
//
// Execute VM operation
//
// Give the VM thread an extra quantum. Jobs tend to be bursty and this
// helps the VM thread to finish up the job.
// FIXME: When this is enabled and there are many threads, this can degrade
// performance significantly.
if( VMThreadHintNoPreempt )
os::hint_no_preempt();
// If we are at a safepoint we will evaluate all the operations that
// follow that also require a safepoint
if (_cur_vm_operation->evaluate_at_safepoint()) {
// now process all queued safepoint ops, iteratively draining
// the queue until there are none left
do {
if (_cur_vm_operation != NULL) {
do {
// evaluate_operation deletes the op object so we have
// to grab the next op now
if (PrintSafepointStatistics) {
}
} while (_cur_vm_operation != NULL);
}
// There is a chance that a thread enqueued a safepoint op
// since we released the op-queue lock and initiated the safepoint.
// So we drain the queue again if there is anything there, as an
// optimization to try and reduce the number of safepoints.
// As the safepoint synchronizes us with JavaThreads we will see
// any enqueue made by a JavaThread, but the peek will not
// necessarily detect a concurrent enqueue by a GC thread, but
// that simply means the op will wait for the next major cycle of the
// VMThread - just as it would if the GC thread lost the race for
// the lock.
if (_vm_queue->peek_at_safepoint_priority()) {
// must hold lock while draining queue
} else {
}
} while(safepoint_ops != NULL);
// Complete safepoint synchronization
} else { // not a safepoint operation
if (TraceLongCompiles) {
elapsedTimer t;
t.start();
t.stop();
// XXX - _cur_vm_operation should not be accessed after
// the completed count has been incremented; the waiting
// thread may have already freed this memory.
}
} else {
}
}
}
//
// Notify (potential) waiting Java thread(s) - lock without safepoint
// check so that sneaking is not possible
}
//
// We want to make sure that we get to a safepoint regularly.
//
bool max_time_exceeded = GuaranteedSafepointInterval != 0 && (interval > GuaranteedSafepointInterval);
if (SafepointALot || max_time_exceeded) {
}
}
}
}
if (!t->is_VM_thread()) {
// JavaThread or WatcherThread
// only blocking VM operations need to verify the caller's safepoint state:
if (!concurrent) {
t->check_for_valid_safepoint_state(true);
}
// New request from Java thread, evaluate prologue
if (!op->doit_prologue()) {
return; // op was cancelled
}
// Setup VM_operations for execution
// It does not make sense to execute the epilogue, if the VM operation object is getting
// deallocated by the VM thread.
// Get ticket number for non-concurrent VM operations
int ticket = 0;
if (!concurrent) {
ticket = t->vm_operation_ticket();
}
// Add VM operation to list of waiting threads. We are guaranteed not to block while holding the
// VMOperationQueue_lock, so we can block without a safepoint check. This allows vm operation requests
// to be queued up during a safepoint synchronization.
{
// VM_Operation got skipped
if (!ok) {
return;
}
}
if (!concurrent) {
// Wait for completion of request (non-concurrent)
// Note: only a JavaThread triggers the safepoint check when locking
while(t->vm_operation_completed_count() < ticket) {
}
}
if (execute_epilog) {
op->doit_epilogue();
}
} else {
// invoked by VM thread; usually nested VM operation
if (prev_vm_operation != NULL) {
// Check the VM operation allows nested VM operation. This normally not the case, e.g., the compiler
// does not allow nested scavenges or compiles.
if (!prev_vm_operation->allow_nested_vm_operations()) {
}
}
// Release all internal handles after operation is evaluated
HandleMark hm(t);
} else {
}
// Free memory if needed
}
}
}
//------------------------------------------------------------------------------------------------------------------
#ifndef PRODUCT
// Check that list is correctly linked
int i;
// Check forward links
for(i = 0; i < length; i++) {
}
// Check backwards links
for(i = 0; i < length; i++) {
}
}
#endif
}