1 /*
   2  * Copyright 1997-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 # include "incls/_precompiled.incl"
  26 # include "incls/_sweeper.cpp.incl"
  27 
  28 long      NMethodSweeper::_traversals = 0;   // No. of stack traversals performed
  29 CodeBlob* NMethodSweeper::_current = NULL;   // Current nmethod
  30 int       NMethodSweeper::_seen = 0 ;        // No. of blobs we have currently processed in current pass of CodeCache
  31 int       NMethodSweeper::_invocations = 0;  // No. of invocations left until we are completed with this pass
  32 
  33 jint      NMethodSweeper::_locked_seen = 0;
  34 jint      NMethodSweeper::_not_entrant_seen_on_stack = 0;
  35 bool      NMethodSweeper::_rescan = false;
  36 
  37 class MarkActivationClosure: public CodeBlobClosure {
  38 public:
  39   virtual void do_code_blob(CodeBlob* cb) {
  40     // If we see an activation belonging to a non_entrant nmethod, we mark it.
  41     if (cb->is_nmethod() && ((nmethod*)cb)->is_not_entrant()) {
  42       ((nmethod*)cb)->mark_as_seen_on_stack();
  43     }
  44   }
  45 };
  46 static MarkActivationClosure mark_activation_closure;
  47 
  48 void NMethodSweeper::sweep() {
  49   assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
  50   if (!MethodFlushing) return;
  51 
  52   // No need to synchronize access, since this is always executed at a
  53   // safepoint.  If we aren't in the middle of scan and a rescan
  54   // hasn't been requested then just return.
  55   if (_current == NULL && !_rescan) return;
  56 
  57   // Make sure CompiledIC_lock in unlocked, since we might update some
  58   // inline caches. If it is, we just bail-out and try later.
  59   if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return;
  60 
  61   // Check for restart
  62   assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
  63   if (_current == NULL) {
  64     _seen        = 0;
  65     _invocations = NmethodSweepFraction;
  66     _current     = CodeCache::first();
  67     _traversals  += 1;
  68     if (PrintMethodFlushing) {
  69       tty->print_cr("### Sweep: stack traversal %d", _traversals);
  70     }
  71     Threads::nmethods_do(&mark_activation_closure);
  72 
  73     // reset the flags since we started a scan from the beginning.
  74     _rescan = false;
  75     _locked_seen = 0;
  76     _not_entrant_seen_on_stack = 0;
  77   }
  78 
  79   if (PrintMethodFlushing && Verbose) {
  80     tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations);
  81   }
  82 
  83   // We want to visit all nmethods after NmethodSweepFraction invocations.
  84   // If invocation is 1 we do the rest
  85   int todo = CodeCache::nof_blobs();
  86   if (_invocations != 1) {
  87     todo = (CodeCache::nof_blobs() - _seen) / _invocations;
  88     _invocations--;
  89   }
  90 
  91   for(int i = 0; i < todo && _current != NULL; i++) {
  92     CodeBlob* next = CodeCache::next(_current); // Read next before we potentially delete current
  93     if (_current->is_nmethod()) {
  94       process_nmethod((nmethod *)_current);
  95     }
  96     _seen++;
  97     _current = next;
  98   }
  99   // Because we could stop on a codeBlob other than an nmethod we skip forward
 100   // to the next nmethod (if any). codeBlobs other than nmethods can be freed
 101   // async to us and make _current invalid while we sleep.
 102   while (_current != NULL && !_current->is_nmethod()) {
 103     _current = CodeCache::next(_current);
 104   }
 105 
 106   if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) {
 107     // we've completed a scan without making progress but there were
 108     // nmethods we were unable to process either because they were
 109     // locked or were still on stack.  We don't have to aggresively
 110     // clean them up so just stop scanning.  We could scan once more
 111     // but that complicates the control logic and it's unlikely to
 112     // matter much.
 113     if (PrintMethodFlushing) {
 114       tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
 115     }
 116   }
 117 }
 118 
 119 
 120 void NMethodSweeper::process_nmethod(nmethod *nm) {
 121   // Skip methods that are currently referenced by the VM
 122   if (nm->is_locked_by_vm()) {
 123     // But still remember to clean-up inline caches for alive nmethods
 124     if (nm->is_alive()) {
 125       // Clean-up all inline caches that points to zombie/non-reentrant methods
 126       nm->cleanup_inline_caches();
 127     } else {
 128       _locked_seen++;
 129     }
 130     return;
 131   }
 132 
 133   if (nm->is_zombie()) {
 134     // If it is first time, we see nmethod then we mark it. Otherwise,
 135     // we reclame it. When we have seen a zombie method twice, we know that
 136     // there are no inline caches that referes to it.
 137     if (nm->is_marked_for_reclamation()) {
 138       assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
 139       if (PrintMethodFlushing && Verbose) {
 140         tty->print_cr("### Nmethod 0x%x (marked for reclamation) being flushed", nm);
 141       }
 142       nm->flush();
 143     } else {
 144       if (PrintMethodFlushing && Verbose) {
 145         tty->print_cr("### Nmethod 0x%x (zombie) being marked for reclamation", nm);
 146       }
 147       nm->mark_for_reclamation();
 148       _rescan = true;
 149     }
 150   } else if (nm->is_not_entrant()) {
 151     // If there is no current activations of this method on the
 152     // stack we can safely convert it to a zombie method
 153     if (nm->can_not_entrant_be_converted()) {
 154       if (PrintMethodFlushing && Verbose) {
 155         tty->print_cr("### Nmethod 0x%x (not entrant) being made zombie", nm);
 156       }
 157       nm->make_zombie();
 158       _rescan = true;
 159     } else {
 160       // Still alive, clean up its inline caches
 161       nm->cleanup_inline_caches();
 162       // we coudn't transition this nmethod so don't immediately
 163       // request a rescan.  If this method stays on the stack for a
 164       // long time we don't want to keep rescanning at every safepoint.
 165       _not_entrant_seen_on_stack++;
 166     }
 167   } else if (nm->is_unloaded()) {
 168     // Unloaded code, just make it a zombie
 169     if (PrintMethodFlushing && Verbose)
 170       tty->print_cr("### Nmethod 0x%x (unloaded) being made zombie", nm);
 171     if (nm->is_osr_method()) {
 172       // No inline caches will ever point to osr methods, so we can just remove it
 173       nm->flush();
 174     } else {
 175       nm->make_zombie();
 176       _rescan = true;
 177     }
 178   } else {
 179     assert(nm->is_alive(), "should be alive");
 180     // Clean-up all inline caches that points to zombie/non-reentrant methods
 181     nm->cleanup_inline_caches();
 182   }
 183 }