hotspot/src/share/vm/runtime/sweeper.cpp
author never
Thu, 03 Apr 2008 10:20:44 -0700
changeset 350 d6d713dd712a
parent 1 489c9b5090e2
child 3696 9e5d9b5e1049
child 3908 24b55ad4c228
permissions -rw-r--r--
6619271: The -Xprintflags causes the VM to segv Summary: add null checks Reviewed-by: jrose, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
void NMethodSweeper::sweep() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  if (!MethodFlushing) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  // No need to synchronize access, since this is always executed at a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  // safepoint.  If we aren't in the middle of scan and a rescan
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  // hasn't been requested then just return.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  if (_current == NULL && !_rescan) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // Make sure CompiledIC_lock in unlocked, since we might update some
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  // inline caches. If it is, we just bail-out and try later.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  if (CompiledIC_lock->is_locked() || Patching_lock->is_locked()) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  // Check for restart
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  if (_current == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    _seen        = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    _invocations = NmethodSweepFraction;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    _current     = CodeCache::first();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    _traversals  += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    if (PrintMethodFlushing) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
      tty->print_cr("### Sweep: stack traversal %d", _traversals);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    Threads::nmethods_do();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    // reset the flags since we started a scan from the beginning.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    _rescan = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    _locked_seen = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    _not_entrant_seen_on_stack = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  if (PrintMethodFlushing && Verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  // We want to visit all nmethods after NmethodSweepFraction invocations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // If invocation is 1 we do the rest
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  int todo = CodeCache::nof_blobs();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  if (_invocations != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    todo = (CodeCache::nof_blobs() - _seen) / _invocations;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
    _invocations--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  for(int i = 0; i < todo && _current != NULL; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
    CodeBlob* next = CodeCache::next(_current); // Read next before we potentially delete current
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    if (_current->is_nmethod()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
      process_nmethod((nmethod *)_current);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    _seen++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    _current = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  // Because we could stop on a codeBlob other than an nmethod we skip forward
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  // to the next nmethod (if any). codeBlobs other than nmethods can be freed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  // async to us and make _current invalid while we sleep.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  while (_current != NULL && !_current->is_nmethod()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    _current = CodeCache::next(_current);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  if (_current == NULL && !_rescan && (_locked_seen || _not_entrant_seen_on_stack)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    // we've completed a scan without making progress but there were
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    // nmethods we were unable to process either because they were
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    // locked or were still on stack.  We don't have to aggresively
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    // clean them up so just stop scanning.  We could scan once more
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    // but that complicates the control logic and it's unlikely to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    // matter much.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    if (PrintMethodFlushing) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
      tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
void NMethodSweeper::process_nmethod(nmethod *nm) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  // Skip methods that are currently referenced by the VM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  if (nm->is_locked_by_vm()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    // But still remember to clean-up inline caches for alive nmethods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    if (nm->is_alive()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      // Clean-up all inline caches that points to zombie/non-reentrant methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
      nm->cleanup_inline_caches();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
      _locked_seen++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  if (nm->is_zombie()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    // If it is first time, we see nmethod then we mark it. Otherwise,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    // we reclame it. When we have seen a zombie method twice, we know that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    // there are no inline caches that referes to it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    if (nm->is_marked_for_reclamation()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
      assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
      nm->flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
      nm->mark_for_reclamation();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
      _rescan = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  } else if (nm->is_not_entrant()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    // If there is no current activations of this method on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    // stack we can safely convert it to a zombie method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    if (nm->can_not_entrant_be_converted()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
      nm->make_zombie();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      _rescan = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
      // Still alive, clean up its inline caches
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
      nm->cleanup_inline_caches();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      // we coudn't transition this nmethod so don't immediately
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      // request a rescan.  If this method stays on the stack for a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
      // long time we don't want to keep rescanning at every safepoint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      _not_entrant_seen_on_stack++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  } else if (nm->is_unloaded()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    // Unloaded code, just make it a zombie
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    if (nm->is_osr_only_method()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      // No inline caches will ever point to osr methods, so we can just remove it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
      nm->flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
      nm->make_zombie();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
      _rescan = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    assert(nm->is_alive(), "should be alive");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    // Clean-up all inline caches that points to zombie/non-reentrant methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    nm->cleanup_inline_caches();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
}