aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "gc_implementation/shared/markSweep.hpp" aoqi@0: #include "gc_interface/collectedHeap.hpp" aoqi@0: #include "gc_interface/collectedHeap.inline.hpp" aoqi@0: #include "memory/genCollectedHeap.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "runtime/init.hpp" aoqi@0: #include "runtime/interfaceSupport.hpp" goetz@6911: #include "runtime/orderAccess.inline.hpp" aoqi@0: #include "runtime/threadLocalStorage.hpp" aoqi@0: #include "runtime/vframe.hpp" aoqi@0: #include "utilities/preserveException.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: // Implementation of InterfaceSupport aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: aoqi@0: long InterfaceSupport::_number_of_calls = 0; aoqi@0: long InterfaceSupport::_scavenge_alot_counter = 1; aoqi@0: long InterfaceSupport::_fullgc_alot_counter = 1; aoqi@0: long InterfaceSupport::_fullgc_alot_invocation = 0; aoqi@0: aoqi@0: Histogram* RuntimeHistogram; aoqi@0: aoqi@0: RuntimeHistogramElement::RuntimeHistogramElement(const char* elementName) { aoqi@0: static volatile jint RuntimeHistogram_lock = 0; aoqi@0: _name = elementName; aoqi@0: uintx count = 0; aoqi@0: aoqi@0: while (Atomic::cmpxchg(1, &RuntimeHistogram_lock, 0) != 0) { aoqi@0: while (OrderAccess::load_acquire(&RuntimeHistogram_lock) != 0) { aoqi@0: count +=1; aoqi@0: if ( (WarnOnStalledSpinLock > 0) aoqi@0: && (count % WarnOnStalledSpinLock == 0)) { aoqi@0: warning("RuntimeHistogram_lock seems to be stalled"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (RuntimeHistogram == NULL) { aoqi@0: RuntimeHistogram = new Histogram("VM Runtime Call Counts",200); aoqi@0: } aoqi@0: aoqi@0: RuntimeHistogram->add_element(this); aoqi@0: Atomic::dec(&RuntimeHistogram_lock); aoqi@0: } aoqi@0: aoqi@0: void InterfaceSupport::trace(const char* result_type, const char* header) { aoqi@0: tty->print_cr("%6d %s", _number_of_calls, header); aoqi@0: } aoqi@0: aoqi@0: void InterfaceSupport::gc_alot() { aoqi@0: Thread *thread = Thread::current(); aoqi@0: if (!thread->is_Java_thread()) return; // Avoid concurrent calls aoqi@0: // Check for new, not quite initialized thread. A thread in new mode cannot initiate a GC. aoqi@0: JavaThread *current_thread = (JavaThread *)thread; aoqi@0: if (current_thread->active_handles() == NULL) return; aoqi@0: aoqi@0: // Short-circuit any possible re-entrant gc-a-lot attempt aoqi@0: if (thread->skip_gcalot()) return; aoqi@0: kbarrett@7360: if (Threads::is_vm_complete()) { aoqi@0: aoqi@0: if (++_fullgc_alot_invocation < FullGCALotStart) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Use this line if you want to block at a specific point, aoqi@0: // e.g. one number_of_calls/scavenge/gc before you got into problems aoqi@0: if (FullGCALot) _fullgc_alot_counter--; aoqi@0: aoqi@0: // Check if we should force a full gc aoqi@0: if (_fullgc_alot_counter == 0) { aoqi@0: // Release dummy so objects are forced to move aoqi@0: if (!Universe::release_fullgc_alot_dummy()) { aoqi@0: warning("FullGCALot: Unable to release more dummies at bottom of heap"); aoqi@0: } aoqi@0: HandleMark hm(thread); aoqi@0: Universe::heap()->collect(GCCause::_full_gc_alot); aoqi@0: unsigned int invocations = Universe::heap()->total_full_collections(); aoqi@0: // Compute new interval aoqi@0: if (FullGCALotInterval > 1) { aoqi@0: _fullgc_alot_counter = 1+(long)((double)FullGCALotInterval*os::random()/(max_jint+1.0)); aoqi@0: if (PrintGCDetails && Verbose) { aoqi@0: tty->print_cr("Full gc no: %u\tInterval: %d", invocations, aoqi@0: _fullgc_alot_counter); aoqi@0: } aoqi@0: } else { aoqi@0: _fullgc_alot_counter = 1; aoqi@0: } aoqi@0: // Print progress message aoqi@0: if (invocations % 100 == 0) { aoqi@0: if (PrintGCDetails && Verbose) tty->print_cr("Full gc no: %u", invocations); aoqi@0: } aoqi@0: } else { aoqi@0: if (ScavengeALot) _scavenge_alot_counter--; aoqi@0: // Check if we should force a scavenge aoqi@0: if (_scavenge_alot_counter == 0) { aoqi@0: HandleMark hm(thread); aoqi@0: Universe::heap()->collect(GCCause::_scavenge_alot); aoqi@0: unsigned int invocations = Universe::heap()->total_collections() - Universe::heap()->total_full_collections(); aoqi@0: // Compute new interval aoqi@0: if (ScavengeALotInterval > 1) { aoqi@0: _scavenge_alot_counter = 1+(long)((double)ScavengeALotInterval*os::random()/(max_jint+1.0)); aoqi@0: if (PrintGCDetails && Verbose) { aoqi@0: tty->print_cr("Scavenge no: %u\tInterval: %d", invocations, aoqi@0: _scavenge_alot_counter); aoqi@0: } aoqi@0: } else { aoqi@0: _scavenge_alot_counter = 1; aoqi@0: } aoqi@0: // Print progress message aoqi@0: if (invocations % 1000 == 0) { aoqi@0: if (PrintGCDetails && Verbose) tty->print_cr("Scavenge no: %u", invocations); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: vframe* vframe_array[50]; aoqi@0: int walk_stack_counter = 0; aoqi@0: aoqi@0: void InterfaceSupport::walk_stack_from(vframe* start_vf) { aoqi@0: // walk aoqi@0: int i = 0; aoqi@0: for (vframe* f = start_vf; f; f = f->sender() ) { aoqi@0: if (i < 50) vframe_array[i++] = f; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void InterfaceSupport::walk_stack() { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: walk_stack_counter++; aoqi@0: if (!thread->has_last_Java_frame()) return; aoqi@0: ResourceMark rm(thread); aoqi@0: RegisterMap reg_map(thread); aoqi@0: walk_stack_from(thread->last_java_vframe(®_map)); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: # ifdef ENABLE_ZAP_DEAD_LOCALS aoqi@0: aoqi@0: static int zap_traversals = 0; aoqi@0: aoqi@0: void InterfaceSupport::zap_dead_locals_old() { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: if (zap_traversals == -1) // edit constant for debugging aoqi@0: warning("I am here"); aoqi@0: int zap_frame_count = 0; // count frames to help debugging aoqi@0: for (StackFrameStream sfs(thread); !sfs.is_done(); sfs.next()) { aoqi@0: sfs.current()->zap_dead_locals(thread, sfs.register_map()); aoqi@0: ++zap_frame_count; aoqi@0: } aoqi@0: ++zap_traversals; aoqi@0: } aoqi@0: aoqi@0: # endif aoqi@0: iignatyev@7875: // invocation counter for InterfaceSupport::deoptimizeAll/zombieAll functions aoqi@0: int deoptimizeAllCounter = 0; aoqi@0: int zombieAllCounter = 0; aoqi@0: aoqi@0: void InterfaceSupport::zombieAll() { iignatyev@7875: // This method is called by all threads when a thread make iignatyev@7875: // transition to VM state (for example, runtime calls). iignatyev@7875: // Divide number of calls by number of threads to avoid iignatyev@7875: // dependence of ZombieAll events frequency on number of threads. iignatyev@7875: int value = zombieAllCounter / Threads::number_of_threads(); iignatyev@7875: if (is_init_completed() && value > ZombieALotInterval) { aoqi@0: zombieAllCounter = 0; aoqi@0: VM_ZombieAll op; aoqi@0: VMThread::execute(&op); aoqi@0: } iignatyev@7875: zombieAllCounter++; aoqi@0: } aoqi@0: aoqi@0: void InterfaceSupport::unlinkSymbols() { aoqi@0: VM_UnlinkSymbols op; aoqi@0: VMThread::execute(&op); aoqi@0: } aoqi@0: aoqi@0: void InterfaceSupport::deoptimizeAll() { iignatyev@7875: // This method is called by all threads when a thread make iignatyev@7875: // transition to VM state (for example, runtime calls). iignatyev@7875: // Divide number of calls by number of threads to avoid iignatyev@7875: // dependence of DeoptimizeAll events frequency on number of threads. iignatyev@7875: int value = deoptimizeAllCounter / Threads::number_of_threads(); iignatyev@7875: if (is_init_completed()) { iignatyev@7875: if (DeoptimizeALot && value > DeoptimizeALotInterval) { aoqi@0: deoptimizeAllCounter = 0; aoqi@0: VM_DeoptimizeAll op; aoqi@0: VMThread::execute(&op); iignatyev@7875: } else if (DeoptimizeRandom && (value & 0x1F) == (os::random() & 0x1F)) { aoqi@0: VM_DeoptimizeAll op; aoqi@0: VMThread::execute(&op); aoqi@0: } aoqi@0: } aoqi@0: deoptimizeAllCounter++; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void InterfaceSupport::stress_derived_pointers() { aoqi@0: #ifdef COMPILER2 aoqi@0: JavaThread *thread = JavaThread::current(); aoqi@0: if (!is_init_completed()) return; aoqi@0: ResourceMark rm(thread); aoqi@0: bool found = false; aoqi@0: for (StackFrameStream sfs(thread); !sfs.is_done() && !found; sfs.next()) { aoqi@0: CodeBlob* cb = sfs.current()->cb(); aoqi@0: if (cb != NULL && cb->oop_maps() ) { aoqi@0: // Find oopmap for current method aoqi@0: OopMap* map = cb->oop_map_for_return_address(sfs.current()->pc()); aoqi@0: assert(map != NULL, "no oopmap found for pc"); aoqi@0: found = map->has_derived_pointer(); aoqi@0: } aoqi@0: } aoqi@0: if (found) { aoqi@0: // $$$ Not sure what to do here. aoqi@0: /* aoqi@0: Scavenge::invoke(0); aoqi@0: */ aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void InterfaceSupport::verify_stack() { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: ResourceMark rm(thread); aoqi@0: // disabled because it throws warnings that oop maps should only be accessed aoqi@0: // in VM thread or during debugging aoqi@0: aoqi@0: if (!thread->has_pending_exception()) { aoqi@0: // verification does not work if there are pending exceptions aoqi@0: StackFrameStream sfs(thread); aoqi@0: CodeBlob* cb = sfs.current()->cb(); aoqi@0: // In case of exceptions we might not have a runtime_stub on aoqi@0: // top of stack, hence, all callee-saved registers are not going aoqi@0: // to be setup correctly, hence, we cannot do stack verify aoqi@0: if (cb != NULL && !(cb->is_runtime_stub() || cb->is_uncommon_trap_stub())) return; aoqi@0: aoqi@0: for (; !sfs.is_done(); sfs.next()) { aoqi@0: sfs.current()->verify(sfs.register_map()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void InterfaceSupport::verify_last_frame() { aoqi@0: JavaThread* thread = JavaThread::current(); aoqi@0: ResourceMark rm(thread); aoqi@0: RegisterMap reg_map(thread); aoqi@0: frame fr = thread->last_frame(); aoqi@0: fr.verify(®_map); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #endif // ASSERT aoqi@0: aoqi@0: aoqi@0: void InterfaceSupport_init() { aoqi@0: #ifdef ASSERT aoqi@0: if (ScavengeALot || FullGCALot) { aoqi@0: srand(ScavengeALotInterval * FullGCALotInterval); aoqi@0: } aoqi@0: #endif aoqi@0: }