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