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