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