duke@435: /* drchase@6680: * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "compiler/compileBroker.hpp" stefank@2314: #include "gc_interface/collectedHeap.hpp" stefank@2314: #include "memory/resourceArea.hpp" coleenp@4037: #include "oops/method.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/interfaceSupport.hpp" stefank@2314: #include "runtime/mutexLocker.hpp" stefank@2314: #include "runtime/os.hpp" stefank@4299: #include "runtime/thread.inline.hpp" stefank@2314: #include "runtime/vmThread.hpp" stefank@2314: #include "runtime/vm_operations.hpp" stefank@2314: #include "services/runtimeService.hpp" sla@5237: #include "trace/tracing.hpp" stefank@2314: #include "utilities/dtrace.hpp" stefank@2314: #include "utilities/events.hpp" stefank@2314: #include "utilities/xmlstream.hpp" duke@435: dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE_DECL3(hotspot, vmops__request, char *, uintptr_t, int); fparain@1759: HS_DTRACE_PROBE_DECL3(hotspot, vmops__begin, char *, uintptr_t, int); fparain@1759: HS_DTRACE_PROBE_DECL3(hotspot, vmops__end, char *, uintptr_t, int); dcubed@3202: #endif /* !USDT2 */ fparain@1759: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: duke@435: // Dummy VM operation to act as first element in our circular double-linked list duke@435: class VM_Dummy: public VM_Operation { duke@435: VMOp_Type type() const { return VMOp_Dummy; } duke@435: void doit() {}; duke@435: }; duke@435: duke@435: VMOperationQueue::VMOperationQueue() { duke@435: // The queue is a circular doubled-linked list, which always contains duke@435: // one element (i.e., one element means empty). duke@435: for(int i = 0; i < nof_priorities; i++) { duke@435: _queue_length[i] = 0; duke@435: _queue_counter = 0; duke@435: _queue[i] = new VM_Dummy(); duke@435: _queue[i]->set_next(_queue[i]); duke@435: _queue[i]->set_prev(_queue[i]); duke@435: } duke@435: _drain_list = NULL; duke@435: } duke@435: duke@435: duke@435: bool VMOperationQueue::queue_empty(int prio) { duke@435: // It is empty if there is exactly one element duke@435: bool empty = (_queue[prio] == _queue[prio]->next()); duke@435: assert( (_queue_length[prio] == 0 && empty) || duke@435: (_queue_length[prio] > 0 && !empty), "sanity check"); duke@435: return _queue_length[prio] == 0; duke@435: } duke@435: duke@435: // Inserts an element to the right of the q element duke@435: void VMOperationQueue::insert(VM_Operation* q, VM_Operation* n) { duke@435: assert(q->next()->prev() == q && q->prev()->next() == q, "sanity check"); duke@435: n->set_prev(q); duke@435: n->set_next(q->next()); duke@435: q->next()->set_prev(n); duke@435: q->set_next(n); duke@435: } duke@435: duke@435: void VMOperationQueue::queue_add_front(int prio, VM_Operation *op) { duke@435: _queue_length[prio]++; duke@435: insert(_queue[prio]->next(), op); duke@435: } duke@435: duke@435: void VMOperationQueue::queue_add_back(int prio, VM_Operation *op) { duke@435: _queue_length[prio]++; duke@435: insert(_queue[prio]->prev(), op); duke@435: } duke@435: duke@435: duke@435: void VMOperationQueue::unlink(VM_Operation* q) { duke@435: assert(q->next()->prev() == q && q->prev()->next() == q, "sanity check"); duke@435: q->prev()->set_next(q->next()); duke@435: q->next()->set_prev(q->prev()); duke@435: } duke@435: duke@435: VM_Operation* VMOperationQueue::queue_remove_front(int prio) { duke@435: if (queue_empty(prio)) return NULL; duke@435: assert(_queue_length[prio] >= 0, "sanity check"); duke@435: _queue_length[prio]--; duke@435: VM_Operation* r = _queue[prio]->next(); duke@435: assert(r != _queue[prio], "cannot remove base element"); duke@435: unlink(r); duke@435: return r; duke@435: } duke@435: duke@435: VM_Operation* VMOperationQueue::queue_drain(int prio) { duke@435: if (queue_empty(prio)) return NULL; duke@435: DEBUG_ONLY(int length = _queue_length[prio];); duke@435: assert(length >= 0, "sanity check"); duke@435: _queue_length[prio] = 0; duke@435: VM_Operation* r = _queue[prio]->next(); duke@435: assert(r != _queue[prio], "cannot remove base element"); duke@435: // remove links to base element from head and tail duke@435: r->set_prev(NULL); duke@435: _queue[prio]->prev()->set_next(NULL); duke@435: // restore queue to empty state duke@435: _queue[prio]->set_next(_queue[prio]); duke@435: _queue[prio]->set_prev(_queue[prio]); jcoomes@1844: assert(queue_empty(prio), "drain corrupted queue"); drchase@4942: #ifdef ASSERT duke@435: int len = 0; duke@435: VM_Operation* cur; duke@435: for(cur = r; cur != NULL; cur=cur->next()) len++; duke@435: assert(len == length, "drain lost some ops"); duke@435: #endif duke@435: return r; duke@435: } duke@435: duke@435: void VMOperationQueue::queue_oops_do(int queue, OopClosure* f) { duke@435: VM_Operation* cur = _queue[queue]; duke@435: cur = cur->next(); duke@435: while (cur != _queue[queue]) { duke@435: cur->oops_do(f); duke@435: cur = cur->next(); duke@435: } duke@435: } duke@435: duke@435: void VMOperationQueue::drain_list_oops_do(OopClosure* f) { duke@435: VM_Operation* cur = _drain_list; duke@435: while (cur != NULL) { duke@435: cur->oops_do(f); duke@435: cur = cur->next(); duke@435: } duke@435: } duke@435: duke@435: //----------------------------------------------------------------- duke@435: // High-level interface duke@435: bool VMOperationQueue::add(VM_Operation *op) { fparain@1759: dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE3(hotspot, vmops__request, op->name(), strlen(op->name()), fparain@1759: op->evaluation_mode()); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_VMOPS_REQUEST( dcubed@3202: (char *) op->name(), strlen(op->name()), dcubed@3202: op->evaluation_mode()); dcubed@3202: #endif /* USDT2 */ fparain@1759: duke@435: // Encapsulates VM queue policy. Currently, that duke@435: // only involves putting them on the right list duke@435: if (op->evaluate_at_safepoint()) { duke@435: queue_add_back(SafepointPriority, op); duke@435: return true; duke@435: } duke@435: duke@435: queue_add_back(MediumPriority, op); duke@435: return true; duke@435: } duke@435: duke@435: VM_Operation* VMOperationQueue::remove_next() { duke@435: // Assuming VMOperation queue is two-level priority queue. If there are duke@435: // more than two priorities, we need a different scheduling algorithm. duke@435: assert(SafepointPriority == 0 && MediumPriority == 1 && nof_priorities == 2, duke@435: "current algorithm does not work"); duke@435: duke@435: // simple counter based scheduling to prevent starvation of lower priority duke@435: // queue. -- see 4390175 duke@435: int high_prio, low_prio; duke@435: if (_queue_counter++ < 10) { duke@435: high_prio = SafepointPriority; duke@435: low_prio = MediumPriority; duke@435: } else { duke@435: _queue_counter = 0; duke@435: high_prio = MediumPriority; duke@435: low_prio = SafepointPriority; duke@435: } duke@435: duke@435: return queue_remove_front(queue_empty(high_prio) ? low_prio : high_prio); duke@435: } duke@435: duke@435: void VMOperationQueue::oops_do(OopClosure* f) { duke@435: for(int i = 0; i < nof_priorities; i++) { duke@435: queue_oops_do(i, f); duke@435: } duke@435: drain_list_oops_do(f); duke@435: } duke@435: duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------ duke@435: // Implementation of VMThread stuff duke@435: duke@435: bool VMThread::_should_terminate = false; duke@435: bool VMThread::_terminated = false; duke@435: Monitor* VMThread::_terminate_lock = NULL; duke@435: VMThread* VMThread::_vm_thread = NULL; duke@435: VM_Operation* VMThread::_cur_vm_operation = NULL; duke@435: VMOperationQueue* VMThread::_vm_queue = NULL; duke@435: PerfCounter* VMThread::_perf_accumulated_vm_operation_time = NULL; duke@435: duke@435: duke@435: void VMThread::create() { duke@435: assert(vm_thread() == NULL, "we can only allocate one VMThread"); duke@435: _vm_thread = new VMThread(); duke@435: duke@435: // Create VM operation queue duke@435: _vm_queue = new VMOperationQueue(); duke@435: guarantee(_vm_queue != NULL, "just checking"); duke@435: duke@435: _terminate_lock = new Monitor(Mutex::safepoint, "VMThread::_terminate_lock", true); duke@435: duke@435: if (UsePerfData) { duke@435: // jvmstat performance counters duke@435: Thread* THREAD = Thread::current(); duke@435: _perf_accumulated_vm_operation_time = duke@435: PerfDataManager::create_counter(SUN_THREADS, "vmOperationTime", duke@435: PerfData::U_Ticks, CHECK); duke@435: } duke@435: } duke@435: duke@435: minqi@1554: VMThread::VMThread() : NamedThread() { minqi@1554: set_name("VM Thread"); duke@435: } duke@435: duke@435: void VMThread::destroy() { duke@435: if (_vm_thread != NULL) { duke@435: delete _vm_thread; duke@435: _vm_thread = NULL; // VM thread is gone duke@435: } duke@435: } duke@435: duke@435: void VMThread::run() { duke@435: assert(this == vm_thread(), "check"); duke@435: duke@435: this->initialize_thread_local_storage(); duke@435: this->record_stack_base_and_size(); duke@435: // Notify_lock wait checks on active_handles() to rewait in duke@435: // case of spurious wakeup, it should wait on the last duke@435: // value set prior to the notify duke@435: this->set_active_handles(JNIHandleBlock::allocate_block()); duke@435: duke@435: { duke@435: MutexLocker ml(Notify_lock); duke@435: Notify_lock->notify(); duke@435: } duke@435: // Notify_lock is destroyed by Threads::create_vm() duke@435: duke@435: int prio = (VMThreadPriority == -1) duke@435: ? os::java_to_os_priority[NearMaxPriority] duke@435: : VMThreadPriority; duke@435: // Note that I cannot call os::set_priority because it expects Java duke@435: // priorities and I am *explicitly* using OS priorities so that it's duke@435: // possible to set the VM thread priority higher than any Java thread. duke@435: os::set_native_priority( this, prio ); duke@435: duke@435: // Wait for VM_Operations until termination duke@435: this->loop(); duke@435: duke@435: // Note the intention to exit before safepointing. duke@435: // 6295565 This has the effect of waiting for any large tty duke@435: // outputs to finish. duke@435: if (xtty != NULL) { duke@435: ttyLocker ttyl; duke@435: xtty->begin_elem("destroy_vm"); duke@435: xtty->stamp(); duke@435: xtty->end_elem(); duke@435: assert(should_terminate(), "termination flag must be set"); duke@435: } duke@435: duke@435: // 4526887 let VM thread exit at Safepoint duke@435: SafepointSynchronize::begin(); duke@435: duke@435: if (VerifyBeforeExit) { duke@435: HandleMark hm(VMThread::vm_thread()); duke@435: // Among other things, this ensures that Eden top is correct. duke@435: Universe::heap()->prepare_for_verify(); duke@435: os::check_heap(); ysr@2825: // Silent verification so as not to pollute normal output, ysr@2825: // unless we really asked for it. stefank@5018: Universe::verify(!(PrintGCDetails || Verbose) || VerifySilently); duke@435: } duke@435: duke@435: CompileBroker::set_should_block(); duke@435: duke@435: // wait for threads (compiler threads or daemon threads) in the duke@435: // _thread_in_native state to block. duke@435: VM_Exit::wait_for_threads_in_native_to_block(); duke@435: duke@435: // signal other threads that VM process is gone duke@435: { duke@435: // Note: we must have the _no_safepoint_check_flag. Mutex::lock() allows duke@435: // VM thread to enter any lock at Safepoint as long as its _owner is NULL. duke@435: // If that happens after _terminate_lock->wait() has unset _owner duke@435: // but before it actually drops the lock and waits, the notification below duke@435: // may get lost and we will have a hang. To avoid this, we need to use duke@435: // Mutex::lock_without_safepoint_check(). duke@435: MutexLockerEx ml(_terminate_lock, Mutex::_no_safepoint_check_flag); duke@435: _terminated = true; duke@435: _terminate_lock->notify(); duke@435: } duke@435: kevinw@6553: // Thread destructor usually does this. kevinw@6553: ThreadLocalStorage::set_thread(NULL); kevinw@6553: duke@435: // Deletion must be done synchronously by the JNI DestroyJavaVM thread duke@435: // so that the VMThread deletion completes before the main thread frees duke@435: // up the CodeHeap. duke@435: duke@435: } duke@435: duke@435: duke@435: // Notify the VMThread that the last non-daemon JavaThread has terminated, duke@435: // and wait until operation is performed. duke@435: void VMThread::wait_for_vm_thread_exit() { duke@435: { MutexLocker mu(VMOperationQueue_lock); duke@435: _should_terminate = true; duke@435: VMOperationQueue_lock->notify(); duke@435: } duke@435: duke@435: // Note: VM thread leaves at Safepoint. We are not stopped by Safepoint duke@435: // because this thread has been removed from the threads list. But anything duke@435: // that could get blocked by Safepoint should not be used after this point, duke@435: // otherwise we will hang, since there is no one can end the safepoint. duke@435: duke@435: // Wait until VM thread is terminated duke@435: // Note: it should be OK to use Terminator_lock here. But this is called duke@435: // at a very delicate time (VM shutdown) and we are operating in non- VM duke@435: // thread at Safepoint. It's safer to not share lock with other threads. duke@435: { MutexLockerEx ml(_terminate_lock, Mutex::_no_safepoint_check_flag); duke@435: while(!VMThread::is_terminated()) { duke@435: _terminate_lock->wait(Mutex::_no_safepoint_check_flag); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void VMThread::print_on(outputStream* st) const { duke@435: st->print("\"%s\" ", name()); duke@435: Thread::print_on(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: void VMThread::evaluate_operation(VM_Operation* op) { duke@435: ResourceMark rm; duke@435: duke@435: { duke@435: PerfTraceTime vm_op_timer(perf_accumulated_vm_operation_time()); dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE3(hotspot, vmops__begin, op->name(), strlen(op->name()), fparain@1759: op->evaluation_mode()); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_VMOPS_BEGIN( dcubed@3202: (char *) op->name(), strlen(op->name()), dcubed@3202: op->evaluation_mode()); dcubed@3202: #endif /* USDT2 */ sla@5237: sla@5237: EventExecuteVMOperation event; sla@5237: duke@435: op->evaluate(); sla@5237: sla@5237: if (event.should_commit()) { sla@5237: bool is_concurrent = op->evaluate_concurrently(); sla@5237: event.set_operation(op->type()); sla@5237: event.set_safepoint(op->evaluate_at_safepoint()); sla@5237: event.set_blocking(!is_concurrent); sla@5237: // Only write caller thread information for non-concurrent vm operations. sla@5237: // For concurrent vm operations, the thread id is set to 0 indicating thread is unknown. sla@5237: // This is because the caller thread could have exited already. sla@5237: event.set_caller(is_concurrent ? 0 : op->calling_thread()->osthread()->thread_id()); sla@5237: event.commit(); sla@5237: } sla@5237: dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE3(hotspot, vmops__end, op->name(), strlen(op->name()), fparain@1759: op->evaluation_mode()); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_VMOPS_END( dcubed@3202: (char *) op->name(), strlen(op->name()), dcubed@3202: op->evaluation_mode()); dcubed@3202: #endif /* USDT2 */ duke@435: } duke@435: duke@435: // Last access of info in _cur_vm_operation! duke@435: bool c_heap_allocated = op->is_cheap_allocated(); duke@435: duke@435: // Mark as completed duke@435: if (!op->evaluate_concurrently()) { duke@435: op->calling_thread()->increment_vm_operation_completed_count(); duke@435: } duke@435: // It is unsafe to access the _cur_vm_operation after the 'increment_vm_operation_completed_count' call, duke@435: // since if it is stack allocated the calling thread might have deallocated duke@435: if (c_heap_allocated) { duke@435: delete _cur_vm_operation; duke@435: } duke@435: } duke@435: duke@435: duke@435: void VMThread::loop() { duke@435: assert(_cur_vm_operation == NULL, "no current one should be executing"); duke@435: duke@435: while(true) { duke@435: VM_Operation* safepoint_ops = NULL; duke@435: // duke@435: // Wait for VM operation duke@435: // duke@435: // use no_safepoint_check to get lock without attempting to "sneak" duke@435: { MutexLockerEx mu_queue(VMOperationQueue_lock, duke@435: Mutex::_no_safepoint_check_flag); duke@435: duke@435: // Look for new operation duke@435: assert(_cur_vm_operation == NULL, "no current one should be executing"); duke@435: _cur_vm_operation = _vm_queue->remove_next(); duke@435: duke@435: // Stall time tracking code duke@435: if (PrintVMQWaitTime && _cur_vm_operation != NULL && duke@435: !_cur_vm_operation->evaluate_concurrently()) { duke@435: long stall = os::javaTimeMillis() - _cur_vm_operation->timestamp(); duke@435: if (stall > 0) duke@435: tty->print_cr("%s stall: %Ld", _cur_vm_operation->name(), stall); duke@435: } duke@435: duke@435: while (!should_terminate() && _cur_vm_operation == NULL) { duke@435: // wait with a timeout to guarantee safepoints at regular intervals duke@435: bool timedout = duke@435: VMOperationQueue_lock->wait(Mutex::_no_safepoint_check_flag, duke@435: GuaranteedSafepointInterval); duke@435: duke@435: // Support for self destruction duke@435: if ((SelfDestructTimer != 0) && !is_error_reported() && duke@435: (os::elapsedTime() > SelfDestructTimer * 60)) { duke@435: tty->print_cr("VM self-destructed"); duke@435: exit(-1); duke@435: } duke@435: duke@435: if (timedout && (SafepointALot || duke@435: SafepointSynchronize::is_cleanup_needed())) { duke@435: MutexUnlockerEx mul(VMOperationQueue_lock, duke@435: Mutex::_no_safepoint_check_flag); duke@435: // Force a safepoint since we have not had one for at least duke@435: // 'GuaranteedSafepointInterval' milliseconds. This will run all duke@435: // the clean-up processing that needs to be done regularly at a duke@435: // safepoint duke@435: SafepointSynchronize::begin(); duke@435: #ifdef ASSERT duke@435: if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot(); duke@435: #endif duke@435: SafepointSynchronize::end(); duke@435: } duke@435: _cur_vm_operation = _vm_queue->remove_next(); duke@435: duke@435: // If we are at a safepoint we will evaluate all the operations that duke@435: // follow that also require a safepoint duke@435: if (_cur_vm_operation != NULL && duke@435: _cur_vm_operation->evaluate_at_safepoint()) { duke@435: safepoint_ops = _vm_queue->drain_at_safepoint_priority(); duke@435: } duke@435: } duke@435: duke@435: if (should_terminate()) break; duke@435: } // Release mu_queue_lock duke@435: duke@435: // duke@435: // Execute VM operation duke@435: // duke@435: { HandleMark hm(VMThread::vm_thread()); duke@435: duke@435: EventMark em("Executing VM operation: %s", vm_operation()->name()); duke@435: assert(_cur_vm_operation != NULL, "we should have found an operation to execute"); duke@435: duke@435: // Give the VM thread an extra quantum. Jobs tend to be bursty and this duke@435: // helps the VM thread to finish up the job. duke@435: // FIXME: When this is enabled and there are many threads, this can degrade duke@435: // performance significantly. duke@435: if( VMThreadHintNoPreempt ) duke@435: os::hint_no_preempt(); duke@435: duke@435: // If we are at a safepoint we will evaluate all the operations that duke@435: // follow that also require a safepoint duke@435: if (_cur_vm_operation->evaluate_at_safepoint()) { duke@435: duke@435: _vm_queue->set_drain_list(safepoint_ops); // ensure ops can be scanned duke@435: duke@435: SafepointSynchronize::begin(); duke@435: evaluate_operation(_cur_vm_operation); duke@435: // now process all queued safepoint ops, iteratively draining duke@435: // the queue until there are none left duke@435: do { duke@435: _cur_vm_operation = safepoint_ops; duke@435: if (_cur_vm_operation != NULL) { duke@435: do { duke@435: // evaluate_operation deletes the op object so we have duke@435: // to grab the next op now duke@435: VM_Operation* next = _cur_vm_operation->next(); duke@435: _vm_queue->set_drain_list(next); duke@435: evaluate_operation(_cur_vm_operation); duke@435: _cur_vm_operation = next; duke@435: if (PrintSafepointStatistics) { duke@435: SafepointSynchronize::inc_vmop_coalesced_count(); duke@435: } duke@435: } while (_cur_vm_operation != NULL); duke@435: } duke@435: // There is a chance that a thread enqueued a safepoint op duke@435: // since we released the op-queue lock and initiated the safepoint. duke@435: // So we drain the queue again if there is anything there, as an duke@435: // optimization to try and reduce the number of safepoints. duke@435: // As the safepoint synchronizes us with JavaThreads we will see duke@435: // any enqueue made by a JavaThread, but the peek will not duke@435: // necessarily detect a concurrent enqueue by a GC thread, but duke@435: // that simply means the op will wait for the next major cycle of the duke@435: // VMThread - just as it would if the GC thread lost the race for duke@435: // the lock. duke@435: if (_vm_queue->peek_at_safepoint_priority()) { duke@435: // must hold lock while draining queue duke@435: MutexLockerEx mu_queue(VMOperationQueue_lock, duke@435: Mutex::_no_safepoint_check_flag); duke@435: safepoint_ops = _vm_queue->drain_at_safepoint_priority(); duke@435: } else { duke@435: safepoint_ops = NULL; duke@435: } duke@435: } while(safepoint_ops != NULL); duke@435: duke@435: _vm_queue->set_drain_list(NULL); duke@435: duke@435: // Complete safepoint synchronization duke@435: SafepointSynchronize::end(); duke@435: duke@435: } else { // not a safepoint operation duke@435: if (TraceLongCompiles) { duke@435: elapsedTimer t; duke@435: t.start(); duke@435: evaluate_operation(_cur_vm_operation); duke@435: t.stop(); duke@435: double secs = t.seconds(); duke@435: if (secs * 1e3 > LongCompileThreshold) { duke@435: // XXX - _cur_vm_operation should not be accessed after duke@435: // the completed count has been incremented; the waiting duke@435: // thread may have already freed this memory. duke@435: tty->print_cr("vm %s: %3.7f secs]", _cur_vm_operation->name(), secs); duke@435: } duke@435: } else { duke@435: evaluate_operation(_cur_vm_operation); duke@435: } duke@435: duke@435: _cur_vm_operation = NULL; duke@435: } duke@435: } duke@435: duke@435: // duke@435: // Notify (potential) waiting Java thread(s) - lock without safepoint duke@435: // check so that sneaking is not possible duke@435: { MutexLockerEx mu(VMOperationRequest_lock, duke@435: Mutex::_no_safepoint_check_flag); duke@435: VMOperationRequest_lock->notify_all(); duke@435: } duke@435: duke@435: // duke@435: // We want to make sure that we get to a safepoint regularly. duke@435: // duke@435: if (SafepointALot || SafepointSynchronize::is_cleanup_needed()) { duke@435: long interval = SafepointSynchronize::last_non_safepoint_interval(); duke@435: bool max_time_exceeded = GuaranteedSafepointInterval != 0 && (interval > GuaranteedSafepointInterval); duke@435: if (SafepointALot || max_time_exceeded) { duke@435: HandleMark hm(VMThread::vm_thread()); duke@435: SafepointSynchronize::begin(); duke@435: SafepointSynchronize::end(); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: void VMThread::execute(VM_Operation* op) { duke@435: Thread* t = Thread::current(); duke@435: duke@435: if (!t->is_VM_thread()) { ysr@1241: SkipGCALot sgcalot(t); // avoid re-entrant attempts to gc-a-lot duke@435: // JavaThread or WatcherThread dcubed@4472: bool concurrent = op->evaluate_concurrently(); dcubed@4472: // only blocking VM operations need to verify the caller's safepoint state: dcubed@4472: if (!concurrent) { dcubed@4472: t->check_for_valid_safepoint_state(true); dcubed@4472: } duke@435: duke@435: // New request from Java thread, evaluate prologue duke@435: if (!op->doit_prologue()) { duke@435: return; // op was cancelled duke@435: } duke@435: duke@435: // Setup VM_operations for execution duke@435: op->set_calling_thread(t, Thread::get_priority(t)); duke@435: duke@435: // It does not make sense to execute the epilogue, if the VM operation object is getting duke@435: // deallocated by the VM thread. duke@435: bool execute_epilog = !op->is_cheap_allocated(); duke@435: assert(!concurrent || op->is_cheap_allocated(), "concurrent => cheap_allocated"); duke@435: duke@435: // Get ticket number for non-concurrent VM operations duke@435: int ticket = 0; duke@435: if (!concurrent) { duke@435: ticket = t->vm_operation_ticket(); duke@435: } duke@435: duke@435: // Add VM operation to list of waiting threads. We are guaranteed not to block while holding the duke@435: // VMOperationQueue_lock, so we can block without a safepoint check. This allows vm operation requests duke@435: // to be queued up during a safepoint synchronization. duke@435: { duke@435: VMOperationQueue_lock->lock_without_safepoint_check(); duke@435: bool ok = _vm_queue->add(op); sla@5237: op->set_timestamp(os::javaTimeMillis()); duke@435: VMOperationQueue_lock->notify(); duke@435: VMOperationQueue_lock->unlock(); duke@435: // VM_Operation got skipped duke@435: if (!ok) { duke@435: assert(concurrent, "can only skip concurrent tasks"); duke@435: if (op->is_cheap_allocated()) delete op; duke@435: return; duke@435: } duke@435: } duke@435: duke@435: if (!concurrent) { duke@435: // Wait for completion of request (non-concurrent) duke@435: // Note: only a JavaThread triggers the safepoint check when locking duke@435: MutexLocker mu(VMOperationRequest_lock); duke@435: while(t->vm_operation_completed_count() < ticket) { duke@435: VMOperationRequest_lock->wait(!t->is_Java_thread()); duke@435: } duke@435: } duke@435: duke@435: if (execute_epilog) { duke@435: op->doit_epilogue(); duke@435: } duke@435: } else { duke@435: // invoked by VM thread; usually nested VM operation duke@435: assert(t->is_VM_thread(), "must be a VM thread"); duke@435: VM_Operation* prev_vm_operation = vm_operation(); duke@435: if (prev_vm_operation != NULL) { duke@435: // Check the VM operation allows nested VM operation. This normally not the case, e.g., the compiler duke@435: // does not allow nested scavenges or compiles. duke@435: if (!prev_vm_operation->allow_nested_vm_operations()) { jcoomes@1845: fatal(err_msg("Nested VM operation %s requested by operation %s", jcoomes@1845: op->name(), vm_operation()->name())); duke@435: } duke@435: op->set_calling_thread(prev_vm_operation->calling_thread(), prev_vm_operation->priority()); duke@435: } duke@435: duke@435: EventMark em("Executing %s VM operation: %s", prev_vm_operation ? "nested" : "", op->name()); duke@435: duke@435: // Release all internal handles after operation is evaluated duke@435: HandleMark hm(t); duke@435: _cur_vm_operation = op; duke@435: duke@435: if (op->evaluate_at_safepoint() && !SafepointSynchronize::is_at_safepoint()) { duke@435: SafepointSynchronize::begin(); duke@435: op->evaluate(); duke@435: SafepointSynchronize::end(); duke@435: } else { duke@435: op->evaluate(); duke@435: } duke@435: duke@435: // Free memory if needed duke@435: if (op->is_cheap_allocated()) delete op; duke@435: duke@435: _cur_vm_operation = prev_vm_operation; duke@435: } duke@435: } duke@435: duke@435: stefank@6973: void VMThread::oops_do(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf) { stefank@4298: Thread::oops_do(f, cld_f, cf); duke@435: _vm_queue->oops_do(f); duke@435: } duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------ duke@435: #ifndef PRODUCT duke@435: duke@435: void VMOperationQueue::verify_queue(int prio) { duke@435: // Check that list is correctly linked duke@435: int length = _queue_length[prio]; duke@435: VM_Operation *cur = _queue[prio]; duke@435: int i; duke@435: duke@435: // Check forward links duke@435: for(i = 0; i < length; i++) { duke@435: cur = cur->next(); duke@435: assert(cur != _queue[prio], "list to short (forward)"); duke@435: } duke@435: assert(cur->next() == _queue[prio], "list to long (forward)"); duke@435: duke@435: // Check backwards links duke@435: cur = _queue[prio]; duke@435: for(i = 0; i < length; i++) { duke@435: cur = cur->prev(); duke@435: assert(cur != _queue[prio], "list to short (backwards)"); duke@435: } duke@435: assert(cur->prev() == _queue[prio], "list to long (backwards)"); duke@435: } duke@435: duke@435: #endif duke@435: duke@435: void VMThread::verify() { stefank@4298: oops_do(&VerifyOopClosure::verify_oop, NULL, NULL); duke@435: }