1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 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 "gc/shared/collectedHeap.hpp" 27 #include "gc/shared/collectedHeap.inline.hpp" 28 #include "gc/shared/genCollectedHeap.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "runtime/atomic.inline.hpp" 31 #include "runtime/init.hpp" 32 #include "runtime/interfaceSupport.hpp" 33 #include "runtime/orderAccess.inline.hpp" 34 #include "runtime/os.inline.hpp" 35 #include "runtime/threadLocalStorage.hpp" 36 #include "runtime/vframe.hpp" 37 #include "utilities/preserveException.hpp" 38 39 // Implementation of InterfaceSupport 40 41 #ifdef ASSERT 42 43 long InterfaceSupport::_number_of_calls = 0; 44 long InterfaceSupport::_scavenge_alot_counter = 1; 45 long InterfaceSupport::_fullgc_alot_counter = 1; 46 long InterfaceSupport::_fullgc_alot_invocation = 0; 47 48 Histogram* RuntimeHistogram; 49 50 RuntimeHistogramElement::RuntimeHistogramElement(const char* elementName) { 51 static volatile jint RuntimeHistogram_lock = 0; 52 _name = elementName; 53 uintx count = 0; 54 55 while (Atomic::cmpxchg(1, &RuntimeHistogram_lock, 0) != 0) { 56 while (OrderAccess::load_acquire(&RuntimeHistogram_lock) != 0) { 57 count +=1; 58 if ( (WarnOnStalledSpinLock > 0) 59 && (count % WarnOnStalledSpinLock == 0)) { 60 warning("RuntimeHistogram_lock seems to be stalled"); 61 } 62 } 63 } 64 65 if (RuntimeHistogram == NULL) { 66 RuntimeHistogram = new Histogram("VM Runtime Call Counts",200); 67 } 68 69 RuntimeHistogram->add_element(this); 70 Atomic::dec(&RuntimeHistogram_lock); 71 } 72 73 void InterfaceSupport::trace(const char* result_type, const char* header) { 74 tty->print_cr("%6ld %s", _number_of_calls, header); 75 } 76 77 void InterfaceSupport::gc_alot() { 78 Thread *thread = Thread::current(); 79 if (!thread->is_Java_thread()) return; // Avoid concurrent calls 80 // Check for new, not quite initialized thread. A thread in new mode cannot initiate a GC. 81 JavaThread *current_thread = (JavaThread *)thread; 82 if (current_thread->active_handles() == NULL) return; 83 84 // Short-circuit any possible re-entrant gc-a-lot attempt 85 if (thread->skip_gcalot()) return; 86 87 if (Threads::is_vm_complete()) { 88 89 if (++_fullgc_alot_invocation < FullGCALotStart) { 90 return; 91 } 92 93 // Use this line if you want to block at a specific point, 94 // e.g. one number_of_calls/scavenge/gc before you got into problems 95 if (FullGCALot) _fullgc_alot_counter--; 96 97 // Check if we should force a full gc 98 if (_fullgc_alot_counter == 0) { 99 // Release dummy so objects are forced to move 100 if (!Universe::release_fullgc_alot_dummy()) { 101 warning("FullGCALot: Unable to release more dummies at bottom of heap"); 102 } 103 HandleMark hm(thread); 104 Universe::heap()->collect(GCCause::_full_gc_alot); 105 unsigned int invocations = Universe::heap()->total_full_collections(); 106 // Compute new interval 107 if (FullGCALotInterval > 1) { 108 _fullgc_alot_counter = 1+(long)((double)FullGCALotInterval*os::random()/(max_jint+1.0)); 109 log_trace(gc)("Full gc no: %u\tInterval: %ld", invocations, _fullgc_alot_counter); 110 } else { 111 _fullgc_alot_counter = 1; 112 } 113 // Print progress message 114 if (invocations % 100 == 0) { 115 log_trace(gc)("Full gc no: %u", invocations); 116 } 117 } else { 118 if (ScavengeALot) _scavenge_alot_counter--; 119 // Check if we should force a scavenge 120 if (_scavenge_alot_counter == 0) { 121 HandleMark hm(thread); 122 Universe::heap()->collect(GCCause::_scavenge_alot); 123 unsigned int invocations = Universe::heap()->total_collections() - Universe::heap()->total_full_collections(); 124 // Compute new interval 125 if (ScavengeALotInterval > 1) { 126 _scavenge_alot_counter = 1+(long)((double)ScavengeALotInterval*os::random()/(max_jint+1.0)); 127 log_trace(gc)("Scavenge no: %u\tInterval: %ld", invocations, _scavenge_alot_counter); 128 } else { 129 _scavenge_alot_counter = 1; 130 } 131 // Print progress message 132 if (invocations % 1000 == 0) { 133 log_trace(gc)("Scavenge no: %u", invocations); 134 } 135 } 136 } 137 } 138 } 139 140 141 vframe* vframe_array[50]; 142 int walk_stack_counter = 0; 143 144 void InterfaceSupport::walk_stack_from(vframe* start_vf) { 145 // walk 146 int i = 0; 147 for (vframe* f = start_vf; f; f = f->sender() ) { 148 if (i < 50) vframe_array[i++] = f; 149 } 150 } 151 152 153 void InterfaceSupport::walk_stack() { 154 JavaThread* thread = JavaThread::current(); 155 walk_stack_counter++; 156 if (!thread->has_last_Java_frame()) return; 157 ResourceMark rm(thread); 158 RegisterMap reg_map(thread); 159 walk_stack_from(thread->last_java_vframe(®_map)); 160 } 161 162 // invocation counter for InterfaceSupport::deoptimizeAll/zombieAll functions 163 int deoptimizeAllCounter = 0; 164 int zombieAllCounter = 0; 165 166 void InterfaceSupport::zombieAll() { 167 // This method is called by all threads when a thread make 168 // transition to VM state (for example, runtime calls). 169 // Divide number of calls by number of threads to avoid 170 // dependence of ZombieAll events frequency on number of threads. 171 int value = zombieAllCounter / Threads::number_of_threads(); 172 if (is_init_completed() && value > ZombieALotInterval) { 173 zombieAllCounter = 0; 174 VM_ZombieAll op; 175 VMThread::execute(&op); 176 } 177 zombieAllCounter++; 178 } 179 180 void InterfaceSupport::unlinkSymbols() { 181 VM_UnlinkSymbols op; 182 VMThread::execute(&op); 183 } 184 185 void InterfaceSupport::deoptimizeAll() { 186 // This method is called by all threads when a thread make 187 // transition to VM state (for example, runtime calls). 188 // Divide number of calls by number of threads to avoid 189 // dependence of DeoptimizeAll events frequency on number of threads. 190 int value = deoptimizeAllCounter / Threads::number_of_threads(); 191 if (is_init_completed()) { 192 if (DeoptimizeALot && value > DeoptimizeALotInterval) { 193 deoptimizeAllCounter = 0; 194 VM_DeoptimizeAll op; 195 VMThread::execute(&op); 196 } else if (DeoptimizeRandom && (value & 0x1F) == (os::random() & 0x1F)) { 197 VM_DeoptimizeAll op; 198 VMThread::execute(&op); 199 } 200 } 201 deoptimizeAllCounter++; 202 } 203 204 205 void InterfaceSupport::stress_derived_pointers() { 206 #ifdef COMPILER2 207 JavaThread *thread = JavaThread::current(); 208 if (!is_init_completed()) return; 209 ResourceMark rm(thread); 210 bool found = false; 211 for (StackFrameStream sfs(thread); !sfs.is_done() && !found; sfs.next()) { 212 CodeBlob* cb = sfs.current()->cb(); 213 if (cb != NULL && cb->oop_maps() ) { 214 // Find oopmap for current method 215 const ImmutableOopMap* map = cb->oop_map_for_return_address(sfs.current()->pc()); 216 assert(map != NULL, "no oopmap found for pc"); 217 found = map->has_derived_pointer(); 218 } 219 } 220 if (found) { 221 // $$$ Not sure what to do here. 222 /* 223 Scavenge::invoke(0); 224 */ 225 } 226 #endif 227 } 228 229 230 void InterfaceSupport::verify_stack() { 231 JavaThread* thread = JavaThread::current(); 232 ResourceMark rm(thread); 233 // disabled because it throws warnings that oop maps should only be accessed 234 // in VM thread or during debugging 235 236 if (!thread->has_pending_exception()) { 237 // verification does not work if there are pending exceptions 238 StackFrameStream sfs(thread); 239 CodeBlob* cb = sfs.current()->cb(); 240 // In case of exceptions we might not have a runtime_stub on 241 // top of stack, hence, all callee-saved registers are not going 242 // to be setup correctly, hence, we cannot do stack verify 243 if (cb != NULL && !(cb->is_runtime_stub() || cb->is_uncommon_trap_stub())) return; 244 245 for (; !sfs.is_done(); sfs.next()) { 246 sfs.current()->verify(sfs.register_map()); 247 } 248 } 249 } 250 251 252 void InterfaceSupport::verify_last_frame() { 253 JavaThread* thread = JavaThread::current(); 254 ResourceMark rm(thread); 255 RegisterMap reg_map(thread); 256 frame fr = thread->last_frame(); 257 fr.verify(®_map); 258 } 259 260 261 #endif // ASSERT 262 263 264 void InterfaceSupport_init() { 265 #ifdef ASSERT 266 if (ScavengeALot || FullGCALot) { 267 srand(ScavengeALotInterval * FullGCALotInterval); 268 } 269 #endif 270 }