1 /* 2 * Copyright (c) 1997, 2018, 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 "memory/resourceArea.hpp" 29 #include "runtime/atomic.hpp" 30 #include "runtime/frame.inline.hpp" 31 #include "runtime/handles.inline.hpp" 32 #include "runtime/init.hpp" 33 #include "runtime/interfaceSupport.inline.hpp" 34 #include "runtime/orderAccess.inline.hpp" 35 #include "runtime/os.inline.hpp" 36 #include "runtime/thread.inline.hpp" 37 #include "runtime/safepointVerifiers.hpp" 38 #include "runtime/vframe.hpp" 39 #include "runtime/vmThread.hpp" 40 #include "utilities/preserveException.hpp" 41 42 // Implementation of InterfaceSupport 43 44 #ifdef ASSERT 45 VMEntryWrapper::VMEntryWrapper() { 46 if (VerifyLastFrame) { 47 InterfaceSupport::verify_last_frame(); 48 } 49 } 50 51 VMEntryWrapper::~VMEntryWrapper() { 52 InterfaceSupport::check_gc_alot(); 53 if (WalkStackALot) { 54 InterfaceSupport::walk_stack(); 55 } 56 #ifdef COMPILER2 57 // This option is not used by Compiler 1 58 if (StressDerivedPointers) { 59 InterfaceSupport::stress_derived_pointers(); 60 } 61 #endif 62 if (DeoptimizeALot || DeoptimizeRandom) { 63 InterfaceSupport::deoptimizeAll(); 64 } 65 if (ZombieALot) { 66 InterfaceSupport::zombieAll(); 67 } 68 if (UnlinkSymbolsALot) { 69 InterfaceSupport::unlinkSymbols(); 70 } 71 // do verification AFTER potential deoptimization 72 if (VerifyStack) { 73 InterfaceSupport::verify_stack(); 74 } 75 } 76 77 long InterfaceSupport::_number_of_calls = 0; 78 long InterfaceSupport::_scavenge_alot_counter = 1; 79 long InterfaceSupport::_fullgc_alot_counter = 1; 80 long InterfaceSupport::_fullgc_alot_invocation = 0; 81 82 Histogram* RuntimeHistogram; 83 84 RuntimeHistogramElement::RuntimeHistogramElement(const char* elementName) { 85 static volatile int RuntimeHistogram_lock = 0; 86 _name = elementName; 87 uintx count = 0; 88 89 while (Atomic::cmpxchg(1, &RuntimeHistogram_lock, 0) != 0) { 90 while (OrderAccess::load_acquire(&RuntimeHistogram_lock) != 0) { 91 count +=1; 92 if ( (WarnOnStalledSpinLock > 0) 93 && (count % WarnOnStalledSpinLock == 0)) { 94 warning("RuntimeHistogram_lock seems to be stalled"); 95 } 96 } 97 } 98 99 if (RuntimeHistogram == NULL) { 100 RuntimeHistogram = new Histogram("VM Runtime Call Counts",200); 101 } 102 103 RuntimeHistogram->add_element(this); 104 Atomic::dec(&RuntimeHistogram_lock); 105 } 106 107 void InterfaceSupport::gc_alot() { 108 Thread *thread = Thread::current(); 109 if (!thread->is_Java_thread()) return; // Avoid concurrent calls 110 // Check for new, not quite initialized thread. A thread in new mode cannot initiate a GC. 111 JavaThread *current_thread = (JavaThread *)thread; 112 if (current_thread->active_handles() == NULL) return; 113 114 // Short-circuit any possible re-entrant gc-a-lot attempt 115 if (thread->skip_gcalot()) return; 116 117 if (Threads::is_vm_complete()) { 118 119 if (++_fullgc_alot_invocation < FullGCALotStart) { 120 return; 121 } 122 123 // Use this line if you want to block at a specific point, 124 // e.g. one number_of_calls/scavenge/gc before you got into problems 125 if (FullGCALot) _fullgc_alot_counter--; 126 127 // Check if we should force a full gc 128 if (_fullgc_alot_counter == 0) { 129 // Release dummy so objects are forced to move 130 if (!Universe::release_fullgc_alot_dummy()) { 131 warning("FullGCALot: Unable to release more dummies at bottom of heap"); 132 } 133 HandleMark hm(thread); 134 Universe::heap()->collect(GCCause::_full_gc_alot); 135 unsigned int invocations = Universe::heap()->total_full_collections(); 136 // Compute new interval 137 if (FullGCALotInterval > 1) { 138 _fullgc_alot_counter = 1+(long)((double)FullGCALotInterval*os::random()/(max_jint+1.0)); 139 log_trace(gc)("Full gc no: %u\tInterval: %ld", invocations, _fullgc_alot_counter); 140 } else { 141 _fullgc_alot_counter = 1; 142 } 143 // Print progress message 144 if (invocations % 100 == 0) { 145 log_trace(gc)("Full gc no: %u", invocations); 146 } 147 } else { 148 if (ScavengeALot) _scavenge_alot_counter--; 149 // Check if we should force a scavenge 150 if (_scavenge_alot_counter == 0) { 151 HandleMark hm(thread); 152 Universe::heap()->collect(GCCause::_scavenge_alot); 153 unsigned int invocations = Universe::heap()->total_collections() - Universe::heap()->total_full_collections(); 154 // Compute new interval 155 if (ScavengeALotInterval > 1) { 156 _scavenge_alot_counter = 1+(long)((double)ScavengeALotInterval*os::random()/(max_jint+1.0)); 157 log_trace(gc)("Scavenge no: %u\tInterval: %ld", invocations, _scavenge_alot_counter); 158 } else { 159 _scavenge_alot_counter = 1; 160 } 161 // Print progress message 162 if (invocations % 1000 == 0) { 163 log_trace(gc)("Scavenge no: %u", invocations); 164 } 165 } 166 } 167 } 168 } 169 170 171 vframe* vframe_array[50]; 172 int walk_stack_counter = 0; 173 174 void InterfaceSupport::walk_stack_from(vframe* start_vf) { 175 // walk 176 int i = 0; 177 for (vframe* f = start_vf; f; f = f->sender() ) { 178 if (i < 50) vframe_array[i++] = f; 179 } 180 } 181 182 183 void InterfaceSupport::walk_stack() { 184 JavaThread* thread = JavaThread::current(); 185 walk_stack_counter++; 186 if (!thread->has_last_Java_frame()) return; 187 ResourceMark rm(thread); 188 RegisterMap reg_map(thread); 189 walk_stack_from(thread->last_java_vframe(®_map)); 190 } 191 192 // invocation counter for InterfaceSupport::deoptimizeAll/zombieAll functions 193 int deoptimizeAllCounter = 0; 194 int zombieAllCounter = 0; 195 196 void InterfaceSupport::zombieAll() { 197 // This method is called by all threads when a thread make 198 // transition to VM state (for example, runtime calls). 199 // Divide number of calls by number of threads to avoid 200 // dependence of ZombieAll events frequency on number of threads. 201 int value = zombieAllCounter / Threads::number_of_threads(); 202 if (is_init_completed() && value > ZombieALotInterval) { 203 zombieAllCounter = 0; 204 VM_ZombieAll op; 205 VMThread::execute(&op); 206 } 207 zombieAllCounter++; 208 } 209 210 void InterfaceSupport::unlinkSymbols() { 211 VM_UnlinkSymbols op; 212 VMThread::execute(&op); 213 } 214 215 void InterfaceSupport::deoptimizeAll() { 216 // This method is called by all threads when a thread make 217 // transition to VM state (for example, runtime calls). 218 // Divide number of calls by number of threads to avoid 219 // dependence of DeoptimizeAll events frequency on number of threads. 220 int value = deoptimizeAllCounter / Threads::number_of_threads(); 221 if (is_init_completed()) { 222 if (DeoptimizeALot && value > DeoptimizeALotInterval) { 223 deoptimizeAllCounter = 0; 224 VM_DeoptimizeAll op; 225 VMThread::execute(&op); 226 } else if (DeoptimizeRandom && (value & 0x1F) == (os::random() & 0x1F)) { 227 VM_DeoptimizeAll op; 228 VMThread::execute(&op); 229 } 230 } 231 deoptimizeAllCounter++; 232 } 233 234 235 void InterfaceSupport::stress_derived_pointers() { 236 #ifdef COMPILER2 237 JavaThread *thread = JavaThread::current(); 238 if (!is_init_completed()) return; 239 ResourceMark rm(thread); 240 bool found = false; 241 for (StackFrameStream sfs(thread); !sfs.is_done() && !found; sfs.next()) { 242 CodeBlob* cb = sfs.current()->cb(); 243 if (cb != NULL && cb->oop_maps() ) { 244 // Find oopmap for current method 245 const ImmutableOopMap* map = cb->oop_map_for_return_address(sfs.current()->pc()); 246 assert(map != NULL, "no oopmap found for pc"); 247 found = map->has_derived_pointer(); 248 } 249 } 250 if (found) { 251 // $$$ Not sure what to do here. 252 /* 253 Scavenge::invoke(0); 254 */ 255 } 256 #endif 257 } 258 259 260 void InterfaceSupport::verify_stack() { 261 JavaThread* thread = JavaThread::current(); 262 ResourceMark rm(thread); 263 // disabled because it throws warnings that oop maps should only be accessed 264 // in VM thread or during debugging 265 266 if (!thread->has_pending_exception()) { 267 // verification does not work if there are pending exceptions 268 StackFrameStream sfs(thread); 269 CodeBlob* cb = sfs.current()->cb(); 270 // In case of exceptions we might not have a runtime_stub on 271 // top of stack, hence, all callee-saved registers are not going 272 // to be setup correctly, hence, we cannot do stack verify 273 if (cb != NULL && !(cb->is_runtime_stub() || cb->is_uncommon_trap_stub())) return; 274 275 for (; !sfs.is_done(); sfs.next()) { 276 sfs.current()->verify(sfs.register_map()); 277 } 278 } 279 } 280 281 282 void InterfaceSupport::verify_last_frame() { 283 JavaThread* thread = JavaThread::current(); 284 ResourceMark rm(thread); 285 RegisterMap reg_map(thread); 286 frame fr = thread->last_frame(); 287 fr.verify(®_map); 288 } 289 290 291 #endif // ASSERT 292 293 294 void InterfaceSupport_init() { 295 #ifdef ASSERT 296 if (ScavengeALot || FullGCALot) { 297 srand(ScavengeALotInterval * FullGCALotInterval); 298 } 299 #endif 300 } 301 302 #ifdef ASSERT 303 // JRT_LEAF rules: 304 // A JRT_LEAF method may not interfere with safepointing by 305 // 1) acquiring or blocking on a Mutex or JavaLock - checked 306 // 2) allocating heap memory - checked 307 // 3) executing a VM operation - checked 308 // 4) executing a system call (including malloc) that could block or grab a lock 309 // 5) invoking GC 310 // 6) reaching a safepoint 311 // 7) running too long 312 // Nor may any method it calls. 313 JRTLeafVerifier::JRTLeafVerifier() 314 : NoSafepointVerifier(true, JRTLeafVerifier::should_verify_GC()) 315 { 316 } 317 318 JRTLeafVerifier::~JRTLeafVerifier() 319 { 320 } 321 322 bool JRTLeafVerifier::should_verify_GC() { 323 switch (JavaThread::current()->thread_state()) { 324 case _thread_in_Java: 325 // is in a leaf routine, there must be no safepoint. 326 return true; 327 case _thread_in_native: 328 // A native thread is not subject to safepoints. 329 // Even while it is in a leaf routine, GC is ok 330 return false; 331 default: 332 // Leaf routines cannot be called from other contexts. 333 ShouldNotReachHere(); 334 return false; 335 } 336 } 337 #endif // ASSERT