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