hotspot/src/share/vm/runtime/sweeper.cpp
author jrose
Wed, 13 Jan 2010 23:05:52 -0800
changeset 4644 2b1a1bc98168
parent 3919 b15d85d98b61
child 4750 71fd601907dc
permissions -rw-r--r--
6912065: final fields in objects need to support inlining optimizations for JSR 292 Reviewed-by: twisti, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
3795
6227ff014cfe 6884624: Update copyright year
xdono
parents: 3696
diff changeset
     2
 * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_sweeper.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
long      NMethodSweeper::_traversals = 0;   // No. of stack traversals performed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
CodeBlob* NMethodSweeper::_current = NULL;   // Current nmethod
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
int       NMethodSweeper::_seen = 0 ;        // No. of blobs we have currently processed in current pass of CodeCache
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
int       NMethodSweeper::_invocations = 0;  // No. of invocations left until we are completed with this pass
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
jint      NMethodSweeper::_locked_seen = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
jint      NMethodSweeper::_not_entrant_seen_on_stack = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
bool      NMethodSweeper::_rescan = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
3908
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    37
class MarkActivationClosure: public CodeBlobClosure {
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    38
public:
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    39
  virtual void do_code_blob(CodeBlob* cb) {
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    40
    // If we see an activation belonging to a non_entrant nmethod, we mark it.
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    41
    if (cb->is_nmethod() && ((nmethod*)cb)->is_not_entrant()) {
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    42
      ((nmethod*)cb)->mark_as_seen_on_stack();
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    43
    }
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    44
  }
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    45
};
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    46
static MarkActivationClosure mark_activation_closure;
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    47
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
void NMethodSweeper::sweep() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  if (!MethodFlushing) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  // No need to synchronize access, since this is always executed at a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  // safepoint.  If we aren't in the middle of scan and a rescan
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  // hasn't been requested then just return.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  if (_current == NULL && !_rescan) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  // Make sure CompiledIC_lock in unlocked, since we might update some
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  // inline caches. If it is, we just bail-out and try later.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  // Check for restart
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  if (_current == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    _seen        = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    _invocations = NmethodSweepFraction;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    _current     = CodeCache::first();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    _traversals  += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    if (PrintMethodFlushing) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
      tty->print_cr("### Sweep: stack traversal %d", _traversals);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    }
3908
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
    71
    Threads::nmethods_do(&mark_activation_closure);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    // reset the flags since we started a scan from the beginning.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    _rescan = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    _locked_seen = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    _not_entrant_seen_on_stack = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  if (PrintMethodFlushing && Verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  // We want to visit all nmethods after NmethodSweepFraction invocations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  // If invocation is 1 we do the rest
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  int todo = CodeCache::nof_blobs();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  if (_invocations != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    todo = (CodeCache::nof_blobs() - _seen) / _invocations;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    _invocations--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  for(int i = 0; i < todo && _current != NULL; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    CodeBlob* next = CodeCache::next(_current); // Read next before we potentially delete current
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    if (_current->is_nmethod()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      process_nmethod((nmethod *)_current);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    _seen++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    _current = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // Because we could stop on a codeBlob other than an nmethod we skip forward
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  // to the next nmethod (if any). codeBlobs other than nmethods can be freed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // async to us and make _current invalid while we sleep.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  while (_current != NULL && !_current->is_nmethod()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    _current = CodeCache::next(_current);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    // we've completed a scan without making progress but there were
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    // nmethods we were unable to process either because they were
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    // locked or were still on stack.  We don't have to aggresively
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    // clean them up so just stop scanning.  We could scan once more
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    // but that complicates the control logic and it's unlikely to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    // matter much.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    if (PrintMethodFlushing) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
void NMethodSweeper::process_nmethod(nmethod *nm) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  // Skip methods that are currently referenced by the VM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  if (nm->is_locked_by_vm()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    // But still remember to clean-up inline caches for alive nmethods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    if (nm->is_alive()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      // Clean-up all inline caches that points to zombie/non-reentrant methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      nm->cleanup_inline_caches();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
      _locked_seen++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  if (nm->is_zombie()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    // If it is first time, we see nmethod then we mark it. Otherwise,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    // we reclame it. When we have seen a zombie method twice, we know that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    // there are no inline caches that referes to it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    if (nm->is_marked_for_reclamation()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   139
      if (PrintMethodFlushing && Verbose) {
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   140
        tty->print_cr("### Nmethod 0x%x (marked for reclamation) being flushed", nm);
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   141
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      nm->flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    } else {
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   144
      if (PrintMethodFlushing && Verbose) {
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   145
        tty->print_cr("### Nmethod 0x%x (zombie) being marked for reclamation", nm);
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   146
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
      nm->mark_for_reclamation();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
      _rescan = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  } else if (nm->is_not_entrant()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    // If there is no current activations of this method on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    // stack we can safely convert it to a zombie method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    if (nm->can_not_entrant_be_converted()) {
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   154
      if (PrintMethodFlushing && Verbose) {
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   155
        tty->print_cr("### Nmethod 0x%x (not entrant) being made zombie", nm);
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   156
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
      nm->make_zombie();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
      _rescan = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
      // Still alive, clean up its inline caches
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      nm->cleanup_inline_caches();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      // we coudn't transition this nmethod so don't immediately
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      // request a rescan.  If this method stays on the stack for a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      // long time we don't want to keep rescanning at every safepoint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      _not_entrant_seen_on_stack++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  } else if (nm->is_unloaded()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    // Unloaded code, just make it a zombie
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   169
    if (PrintMethodFlushing && Verbose)
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   170
      tty->print_cr("### Nmethod 0x%x (unloaded) being made zombie", nm);
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   171
    if (nm->is_osr_method()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      // No inline caches will ever point to osr methods, so we can just remove it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      nm->flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
      nm->make_zombie();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
      _rescan = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    assert(nm->is_alive(), "should be alive");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    // Clean-up all inline caches that points to zombie/non-reentrant methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    nm->cleanup_inline_caches();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
}