Tue, 15 Sep 2009 21:53:47 -0700
6863023: need non-perm oops in code cache for JSR 292
Summary: Make a special root-list for those few nmethods which might contain non-perm oops.
Reviewed-by: twisti, kvn, never, jmasa, ysr
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 */
25 # include "incls/_precompiled.incl"
26 # include "incls/_sweeper.cpp.incl"
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
33 jint NMethodSweeper::_locked_seen = 0;
34 jint NMethodSweeper::_not_entrant_seen_on_stack = 0;
35 bool NMethodSweeper::_rescan = false;
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;
48 void NMethodSweeper::sweep() {
49 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
50 if (!MethodFlushing) return;
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;
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;
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);
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 }
79 if (PrintMethodFlushing && Verbose) {
80 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_blobs(), _invocations);
81 }
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 }
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 }
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 }
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 }
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 nm->flush();
140 } else {
141 nm->mark_for_reclamation();
142 _rescan = true;
143 }
144 } else if (nm->is_not_entrant()) {
145 // If there is no current activations of this method on the
146 // stack we can safely convert it to a zombie method
147 if (nm->can_not_entrant_be_converted()) {
148 nm->make_zombie();
149 _rescan = true;
150 } else {
151 // Still alive, clean up its inline caches
152 nm->cleanup_inline_caches();
153 // we coudn't transition this nmethod so don't immediately
154 // request a rescan. If this method stays on the stack for a
155 // long time we don't want to keep rescanning at every safepoint.
156 _not_entrant_seen_on_stack++;
157 }
158 } else if (nm->is_unloaded()) {
159 // Unloaded code, just make it a zombie
160 if (nm->is_osr_only_method()) {
161 // No inline caches will ever point to osr methods, so we can just remove it
162 nm->flush();
163 } else {
164 nm->make_zombie();
165 _rescan = true;
166 }
167 } else {
168 assert(nm->is_alive(), "should be alive");
169 // Clean-up all inline caches that points to zombie/non-reentrant methods
170 nm->cleanup_inline_caches();
171 }
172 }