annotate src/share/vm/runtime/jniHandles.cpp @ 12144:d8e99408faad

8009062: poor performance of JNI AttachCurrentThread after fix for 7017193 Summary: don't re-evaluate stack bounds for main thread before install guard page Reviewed-by: coleenp, dholmes, dlong
author dsamersoff
date Thu, 29 Aug 2013 21:48:23 +0400
parents 001ec9515f84
children de6a9e811145
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
6725
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 3960
diff changeset
2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1142
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1142
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1142
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1616
diff changeset
25 #include "precompiled.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1616
diff changeset
26 #include "classfile/systemDictionary.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1616
diff changeset
27 #include "oops/oop.inline.hpp"
2147
9afee0b9fc1d 7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents: 2125
diff changeset
28 #include "prims/jvmtiExport.hpp"
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1616
diff changeset
29 #include "runtime/jniHandles.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1616
diff changeset
30 #include "runtime/mutexLocker.hpp"
7180
f34d701e952e 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 6725
diff changeset
31 #include "runtime/thread.inline.hpp"
0
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 JNIHandleBlock* JNIHandles::_global_handles = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
35 JNIHandleBlock* JNIHandles::_weak_global_handles = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 oop JNIHandles::_deleted_handle = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 jobject JNIHandles::make_local(oop obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
40 if (obj == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
41 return NULL; // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
42 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
43 Thread* thread = Thread::current();
a61af66fc99e Initial load
duke
parents:
diff changeset
44 assert(Universe::heap()->is_in_reserved(obj), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
45 return thread->active_handles()->allocate_handle(obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
46 }
a61af66fc99e Initial load
duke
parents:
diff changeset
47 }
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // optimized versions
a61af66fc99e Initial load
duke
parents:
diff changeset
51
a61af66fc99e Initial load
duke
parents:
diff changeset
52 jobject JNIHandles::make_local(Thread* thread, oop obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
53 if (obj == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
54 return NULL; // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
55 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
56 assert(Universe::heap()->is_in_reserved(obj), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
57 return thread->active_handles()->allocate_handle(obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
58 }
a61af66fc99e Initial load
duke
parents:
diff changeset
59 }
a61af66fc99e Initial load
duke
parents:
diff changeset
60
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 jobject JNIHandles::make_local(JNIEnv* env, oop obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
63 if (obj == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
64 return NULL; // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
65 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 JavaThread* thread = JavaThread::thread_from_jni_environment(env);
a61af66fc99e Initial load
duke
parents:
diff changeset
67 assert(Universe::heap()->is_in_reserved(obj), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
68 return thread->active_handles()->allocate_handle(obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
69 }
a61af66fc99e Initial load
duke
parents:
diff changeset
70 }
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72
a61af66fc99e Initial load
duke
parents:
diff changeset
73 jobject JNIHandles::make_global(Handle obj) {
1616
38e8278318ca 6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents: 1552
diff changeset
74 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
75 jobject res = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 if (!obj.is_null()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
78 MutexLocker ml(JNIGlobalHandle_lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
79 assert(Universe::heap()->is_in_reserved(obj()), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
80 res = _global_handles->allocate_handle(obj());
a61af66fc99e Initial load
duke
parents:
diff changeset
81 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
a61af66fc99e Initial load
duke
parents:
diff changeset
83 }
a61af66fc99e Initial load
duke
parents:
diff changeset
84
a61af66fc99e Initial load
duke
parents:
diff changeset
85 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 jobject JNIHandles::make_weak_global(Handle obj) {
1616
38e8278318ca 6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents: 1552
diff changeset
90 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
91 jobject res = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
92 if (!obj.is_null()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
93 // ignore null handles
a61af66fc99e Initial load
duke
parents:
diff changeset
94 MutexLocker ml(JNIGlobalHandle_lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
95 assert(Universe::heap()->is_in_reserved(obj()), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
96 res = _weak_global_handles->allocate_handle(obj());
a61af66fc99e Initial load
duke
parents:
diff changeset
97 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 return res;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103
a61af66fc99e Initial load
duke
parents:
diff changeset
104 void JNIHandles::destroy_global(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 if (handle != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 assert(is_global_handle(handle), "Invalid delete of global JNI handle");
a61af66fc99e Initial load
duke
parents:
diff changeset
107 *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
a61af66fc99e Initial load
duke
parents:
diff changeset
108 }
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 void JNIHandles::destroy_weak_global(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
113 if (handle != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 assert(!CheckJNICalls || is_weak_global_handle(handle), "Invalid delete of weak global JNI handle");
a61af66fc99e Initial load
duke
parents:
diff changeset
115 *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
a61af66fc99e Initial load
duke
parents:
diff changeset
116 }
a61af66fc99e Initial load
duke
parents:
diff changeset
117 }
a61af66fc99e Initial load
duke
parents:
diff changeset
118
a61af66fc99e Initial load
duke
parents:
diff changeset
119
a61af66fc99e Initial load
duke
parents:
diff changeset
120 void JNIHandles::oops_do(OopClosure* f) {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 f->do_oop(&_deleted_handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
122 _global_handles->oops_do(f);
a61af66fc99e Initial load
duke
parents:
diff changeset
123 }
a61af66fc99e Initial load
duke
parents:
diff changeset
124
a61af66fc99e Initial load
duke
parents:
diff changeset
125
a61af66fc99e Initial load
duke
parents:
diff changeset
126 void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
a61af66fc99e Initial load
duke
parents:
diff changeset
127 _weak_global_handles->weak_oops_do(is_alive, f);
a61af66fc99e Initial load
duke
parents:
diff changeset
128 }
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130
a61af66fc99e Initial load
duke
parents:
diff changeset
131 void JNIHandles::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
132 _global_handles = JNIHandleBlock::allocate_block();
a61af66fc99e Initial load
duke
parents:
diff changeset
133 _weak_global_handles = JNIHandleBlock::allocate_block();
a61af66fc99e Initial load
duke
parents:
diff changeset
134 EXCEPTION_MARK;
a61af66fc99e Initial load
duke
parents:
diff changeset
135 // We will never reach the CATCH below since Exceptions::_throw will cause
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // the VM to exit if an exception is thrown during initialization
6725
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 3960
diff changeset
137 Klass* k = SystemDictionary::Object_klass();
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 3960
diff changeset
138 _deleted_handle = InstanceKlass::cast(k)->allocate_instance(CATCH);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141
a61af66fc99e Initial load
duke
parents:
diff changeset
142 bool JNIHandles::is_local_handle(Thread* thread, jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 JNIHandleBlock* block = thread->active_handles();
a61af66fc99e Initial load
duke
parents:
diff changeset
144
a61af66fc99e Initial load
duke
parents:
diff changeset
145 // Look back past possible native calls to jni_PushLocalFrame.
a61af66fc99e Initial load
duke
parents:
diff changeset
146 while (block != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 if (block->chain_contains(handle)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
148 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
149 }
a61af66fc99e Initial load
duke
parents:
diff changeset
150 block = block->pop_frame_link();
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
153 }
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155
a61af66fc99e Initial load
duke
parents:
diff changeset
156 // Determine if the handle is somewhere in the current thread's stack.
a61af66fc99e Initial load
duke
parents:
diff changeset
157 // We easily can't isolate any particular stack frame the handle might
a61af66fc99e Initial load
duke
parents:
diff changeset
158 // come from, so we'll check the whole stack.
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 bool JNIHandles::is_frame_handle(JavaThread* thr, jobject obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
161 // If there is no java frame, then this must be top level code, such
a61af66fc99e Initial load
duke
parents:
diff changeset
162 // as the java command executable, in which case, this type of handle
a61af66fc99e Initial load
duke
parents:
diff changeset
163 // is not permitted.
a61af66fc99e Initial load
duke
parents:
diff changeset
164 return (thr->has_last_Java_frame() &&
a61af66fc99e Initial load
duke
parents:
diff changeset
165 (void*)obj < (void*)thr->stack_base() &&
a61af66fc99e Initial load
duke
parents:
diff changeset
166 (void*)obj >= (void*)thr->last_Java_sp());
a61af66fc99e Initial load
duke
parents:
diff changeset
167 }
a61af66fc99e Initial load
duke
parents:
diff changeset
168
a61af66fc99e Initial load
duke
parents:
diff changeset
169
a61af66fc99e Initial load
duke
parents:
diff changeset
170 bool JNIHandles::is_global_handle(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
171 return _global_handles->chain_contains(handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
172 }
a61af66fc99e Initial load
duke
parents:
diff changeset
173
a61af66fc99e Initial load
duke
parents:
diff changeset
174
a61af66fc99e Initial load
duke
parents:
diff changeset
175 bool JNIHandles::is_weak_global_handle(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
176 return _weak_global_handles->chain_contains(handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179 long JNIHandles::global_handle_memory_usage() {
a61af66fc99e Initial load
duke
parents:
diff changeset
180 return _global_handles->memory_usage();
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182
a61af66fc99e Initial load
duke
parents:
diff changeset
183 long JNIHandles::weak_global_handle_memory_usage() {
a61af66fc99e Initial load
duke
parents:
diff changeset
184 return _weak_global_handles->memory_usage();
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }
a61af66fc99e Initial load
duke
parents:
diff changeset
186
a61af66fc99e Initial load
duke
parents:
diff changeset
187
a61af66fc99e Initial load
duke
parents:
diff changeset
188 class AlwaysAliveClosure: public BoolObjectClosure {
a61af66fc99e Initial load
duke
parents:
diff changeset
189 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
190 bool do_object_b(oop obj) { return true; }
a61af66fc99e Initial load
duke
parents:
diff changeset
191 };
a61af66fc99e Initial load
duke
parents:
diff changeset
192
a61af66fc99e Initial load
duke
parents:
diff changeset
193 class CountHandleClosure: public OopClosure {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
195 int _count;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
197 CountHandleClosure(): _count(0) {}
113
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
198 virtual void do_oop(oop* unused) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
199 _count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
200 }
113
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
201 virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
202 int count() { return _count; }
a61af66fc99e Initial load
duke
parents:
diff changeset
203 };
a61af66fc99e Initial load
duke
parents:
diff changeset
204
a61af66fc99e Initial load
duke
parents:
diff changeset
205 // We assume this is called at a safepoint: no lock is needed.
a61af66fc99e Initial load
duke
parents:
diff changeset
206 void JNIHandles::print_on(outputStream* st) {
a61af66fc99e Initial load
duke
parents:
diff changeset
207 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
a61af66fc99e Initial load
duke
parents:
diff changeset
208 assert(_global_handles != NULL && _weak_global_handles != NULL,
a61af66fc99e Initial load
duke
parents:
diff changeset
209 "JNIHandles not initialized");
a61af66fc99e Initial load
duke
parents:
diff changeset
210
a61af66fc99e Initial load
duke
parents:
diff changeset
211 CountHandleClosure global_handle_count;
a61af66fc99e Initial load
duke
parents:
diff changeset
212 AlwaysAliveClosure always_alive;
a61af66fc99e Initial load
duke
parents:
diff changeset
213 oops_do(&global_handle_count);
a61af66fc99e Initial load
duke
parents:
diff changeset
214 weak_oops_do(&always_alive, &global_handle_count);
a61af66fc99e Initial load
duke
parents:
diff changeset
215
a61af66fc99e Initial load
duke
parents:
diff changeset
216 st->print_cr("JNI global references: %d", global_handle_count.count());
a61af66fc99e Initial load
duke
parents:
diff changeset
217 st->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
218 st->flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
219 }
a61af66fc99e Initial load
duke
parents:
diff changeset
220
a61af66fc99e Initial load
duke
parents:
diff changeset
221 class VerifyHandleClosure: public OopClosure {
a61af66fc99e Initial load
duke
parents:
diff changeset
222 public:
113
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
223 virtual void do_oop(oop* root) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
224 (*root)->verify();
a61af66fc99e Initial load
duke
parents:
diff changeset
225 }
113
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
226 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
227 };
a61af66fc99e Initial load
duke
parents:
diff changeset
228
a61af66fc99e Initial load
duke
parents:
diff changeset
229 void JNIHandles::verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
230 VerifyHandleClosure verify_handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
231 AlwaysAliveClosure always_alive;
a61af66fc99e Initial load
duke
parents:
diff changeset
232
a61af66fc99e Initial load
duke
parents:
diff changeset
233 oops_do(&verify_handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
234 weak_oops_do(&always_alive, &verify_handle);
a61af66fc99e Initial load
duke
parents:
diff changeset
235 }
a61af66fc99e Initial load
duke
parents:
diff changeset
236
a61af66fc99e Initial load
duke
parents:
diff changeset
237
a61af66fc99e Initial load
duke
parents:
diff changeset
238
a61af66fc99e Initial load
duke
parents:
diff changeset
239 void jni_handles_init() {
a61af66fc99e Initial load
duke
parents:
diff changeset
240 JNIHandles::initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243
a61af66fc99e Initial load
duke
parents:
diff changeset
244 int JNIHandleBlock::_blocks_allocated = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
245 JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
246 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
247 JNIHandleBlock* JNIHandleBlock::_block_list = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
248 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
249
a61af66fc99e Initial load
duke
parents:
diff changeset
250
a61af66fc99e Initial load
duke
parents:
diff changeset
251 void JNIHandleBlock::zap() {
a61af66fc99e Initial load
duke
parents:
diff changeset
252 // Zap block values
a61af66fc99e Initial load
duke
parents:
diff changeset
253 _top = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
254 for (int index = 0; index < block_size_in_oops; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
255 _handles[index] = badJNIHandle;
a61af66fc99e Initial load
duke
parents:
diff changeset
256 }
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258
a61af66fc99e Initial load
duke
parents:
diff changeset
259 JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread) {
a61af66fc99e Initial load
duke
parents:
diff changeset
260 assert(thread == NULL || thread == Thread::current(), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
261 JNIHandleBlock* block;
a61af66fc99e Initial load
duke
parents:
diff changeset
262 // Check the thread-local free list for a block so we don't
a61af66fc99e Initial load
duke
parents:
diff changeset
263 // have to acquire a mutex.
a61af66fc99e Initial load
duke
parents:
diff changeset
264 if (thread != NULL && thread->free_handle_block() != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
265 block = thread->free_handle_block();
a61af66fc99e Initial load
duke
parents:
diff changeset
266 thread->set_free_handle_block(block->_next);
a61af66fc99e Initial load
duke
parents:
diff changeset
267 }
a61af66fc99e Initial load
duke
parents:
diff changeset
268 else {
a61af66fc99e Initial load
duke
parents:
diff changeset
269 // locking with safepoint checking introduces a potential deadlock:
a61af66fc99e Initial load
duke
parents:
diff changeset
270 // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
a61af66fc99e Initial load
duke
parents:
diff changeset
271 // - another would hold Threads_lock (jni_AttachCurrentThread) and then
a61af66fc99e Initial load
duke
parents:
diff changeset
272 // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
a61af66fc99e Initial load
duke
parents:
diff changeset
273 MutexLockerEx ml(JNIHandleBlockFreeList_lock,
a61af66fc99e Initial load
duke
parents:
diff changeset
274 Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
275 if (_block_free_list == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
276 // Allocate new block
a61af66fc99e Initial load
duke
parents:
diff changeset
277 block = new JNIHandleBlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
278 _blocks_allocated++;
a61af66fc99e Initial load
duke
parents:
diff changeset
279 if (TraceJNIHandleAllocation) {
a61af66fc99e Initial load
duke
parents:
diff changeset
280 tty->print_cr("JNIHandleBlock " INTPTR_FORMAT " allocated (%d total blocks)",
a61af66fc99e Initial load
duke
parents:
diff changeset
281 block, _blocks_allocated);
a61af66fc99e Initial load
duke
parents:
diff changeset
282 }
a61af66fc99e Initial load
duke
parents:
diff changeset
283 if (ZapJNIHandleArea) block->zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
284 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
285 // Link new block to list of all allocated blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
286 block->_block_list_link = _block_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
287 _block_list = block;
a61af66fc99e Initial load
duke
parents:
diff changeset
288 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
289 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
290 // Get block from free list
a61af66fc99e Initial load
duke
parents:
diff changeset
291 block = _block_free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
292 _block_free_list = _block_free_list->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
293 }
a61af66fc99e Initial load
duke
parents:
diff changeset
294 }
a61af66fc99e Initial load
duke
parents:
diff changeset
295 block->_top = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
296 block->_next = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
297 block->_pop_frame_link = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
298 // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle
a61af66fc99e Initial load
duke
parents:
diff changeset
299 debug_only(block->_last = NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
300 debug_only(block->_free_list = NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
301 debug_only(block->_allocate_before_rebuild = -1);
a61af66fc99e Initial load
duke
parents:
diff changeset
302 return block;
a61af66fc99e Initial load
duke
parents:
diff changeset
303 }
a61af66fc99e Initial load
duke
parents:
diff changeset
304
a61af66fc99e Initial load
duke
parents:
diff changeset
305
a61af66fc99e Initial load
duke
parents:
diff changeset
306 void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) {
a61af66fc99e Initial load
duke
parents:
diff changeset
307 assert(thread == NULL || thread == Thread::current(), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
308 JNIHandleBlock* pop_frame_link = block->pop_frame_link();
a61af66fc99e Initial load
duke
parents:
diff changeset
309 // Put returned block at the beginning of the thread-local free list.
a61af66fc99e Initial load
duke
parents:
diff changeset
310 // Note that if thread == NULL, we use it as an implicit argument that
a61af66fc99e Initial load
duke
parents:
diff changeset
311 // we _don't_ want the block to be kept on the free_handle_block.
a61af66fc99e Initial load
duke
parents:
diff changeset
312 // See for instance JavaThread::exit().
a61af66fc99e Initial load
duke
parents:
diff changeset
313 if (thread != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 if (ZapJNIHandleArea) block->zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
315 JNIHandleBlock* freelist = thread->free_handle_block();
a61af66fc99e Initial load
duke
parents:
diff changeset
316 block->_pop_frame_link = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
317 thread->set_free_handle_block(block);
a61af66fc99e Initial load
duke
parents:
diff changeset
318
a61af66fc99e Initial load
duke
parents:
diff changeset
319 // Add original freelist to end of chain
a61af66fc99e Initial load
duke
parents:
diff changeset
320 if ( freelist != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
321 while ( block->_next != NULL ) block = block->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
322 block->_next = freelist;
a61af66fc99e Initial load
duke
parents:
diff changeset
323 }
a61af66fc99e Initial load
duke
parents:
diff changeset
324 block = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
325 }
a61af66fc99e Initial load
duke
parents:
diff changeset
326 if (block != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
327 // Return blocks to free list
a61af66fc99e Initial load
duke
parents:
diff changeset
328 // locking with safepoint checking introduces a potential deadlock:
a61af66fc99e Initial load
duke
parents:
diff changeset
329 // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock
a61af66fc99e Initial load
duke
parents:
diff changeset
330 // - another would hold Threads_lock (jni_AttachCurrentThread) and then
a61af66fc99e Initial load
duke
parents:
diff changeset
331 // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block)
a61af66fc99e Initial load
duke
parents:
diff changeset
332 MutexLockerEx ml(JNIHandleBlockFreeList_lock,
a61af66fc99e Initial load
duke
parents:
diff changeset
333 Mutex::_no_safepoint_check_flag);
a61af66fc99e Initial load
duke
parents:
diff changeset
334 while (block != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
335 if (ZapJNIHandleArea) block->zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
336 JNIHandleBlock* next = block->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
337 block->_next = _block_free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
338 _block_free_list = block;
a61af66fc99e Initial load
duke
parents:
diff changeset
339 block = next;
a61af66fc99e Initial load
duke
parents:
diff changeset
340 }
a61af66fc99e Initial load
duke
parents:
diff changeset
341 }
a61af66fc99e Initial load
duke
parents:
diff changeset
342 if (pop_frame_link != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
343 // As a sanity check we release blocks pointed to by the pop_frame_link.
a61af66fc99e Initial load
duke
parents:
diff changeset
344 // This should never happen (only if PopLocalFrame is not called the
a61af66fc99e Initial load
duke
parents:
diff changeset
345 // correct number of times).
a61af66fc99e Initial load
duke
parents:
diff changeset
346 release_block(pop_frame_link, thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
347 }
a61af66fc99e Initial load
duke
parents:
diff changeset
348 }
a61af66fc99e Initial load
duke
parents:
diff changeset
349
a61af66fc99e Initial load
duke
parents:
diff changeset
350
a61af66fc99e Initial load
duke
parents:
diff changeset
351 void JNIHandleBlock::oops_do(OopClosure* f) {
a61af66fc99e Initial load
duke
parents:
diff changeset
352 JNIHandleBlock* current_chain = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
353 // Iterate over chain of blocks, followed by chains linked through the
a61af66fc99e Initial load
duke
parents:
diff changeset
354 // pop frame links.
a61af66fc99e Initial load
duke
parents:
diff changeset
355 while (current_chain != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
356 for (JNIHandleBlock* current = current_chain; current != NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
357 current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
358 assert(current == current_chain || current->pop_frame_link() == NULL,
a61af66fc99e Initial load
duke
parents:
diff changeset
359 "only blocks first in chain should have pop frame link set");
a61af66fc99e Initial load
duke
parents:
diff changeset
360 for (int index = 0; index < current->_top; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
361 oop* root = &(current->_handles)[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
362 oop value = *root;
a61af66fc99e Initial load
duke
parents:
diff changeset
363 // traverse heap pointers only, not deleted handles or free list
a61af66fc99e Initial load
duke
parents:
diff changeset
364 // pointers
a61af66fc99e Initial load
duke
parents:
diff changeset
365 if (value != NULL && Universe::heap()->is_in_reserved(value)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
366 f->do_oop(root);
a61af66fc99e Initial load
duke
parents:
diff changeset
367 }
a61af66fc99e Initial load
duke
parents:
diff changeset
368 }
a61af66fc99e Initial load
duke
parents:
diff changeset
369 // the next handle block is valid only if current block is full
a61af66fc99e Initial load
duke
parents:
diff changeset
370 if (current->_top < block_size_in_oops) {
a61af66fc99e Initial load
duke
parents:
diff changeset
371 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
372 }
a61af66fc99e Initial load
duke
parents:
diff changeset
373 }
a61af66fc99e Initial load
duke
parents:
diff changeset
374 current_chain = current_chain->pop_frame_link();
a61af66fc99e Initial load
duke
parents:
diff changeset
375 }
a61af66fc99e Initial load
duke
parents:
diff changeset
376 }
a61af66fc99e Initial load
duke
parents:
diff changeset
377
a61af66fc99e Initial load
duke
parents:
diff changeset
378
a61af66fc99e Initial load
duke
parents:
diff changeset
379 void JNIHandleBlock::weak_oops_do(BoolObjectClosure* is_alive,
a61af66fc99e Initial load
duke
parents:
diff changeset
380 OopClosure* f) {
a61af66fc99e Initial load
duke
parents:
diff changeset
381 for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
382 assert(current->pop_frame_link() == NULL,
a61af66fc99e Initial load
duke
parents:
diff changeset
383 "blocks holding weak global JNI handles should not have pop frame link set");
a61af66fc99e Initial load
duke
parents:
diff changeset
384 for (int index = 0; index < current->_top; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
385 oop* root = &(current->_handles)[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
386 oop value = *root;
a61af66fc99e Initial load
duke
parents:
diff changeset
387 // traverse heap pointers only, not deleted handles or free list pointers
a61af66fc99e Initial load
duke
parents:
diff changeset
388 if (value != NULL && Universe::heap()->is_in_reserved(value)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
389 if (is_alive->do_object_b(value)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
390 // The weakly referenced object is alive, update pointer
a61af66fc99e Initial load
duke
parents:
diff changeset
391 f->do_oop(root);
a61af66fc99e Initial load
duke
parents:
diff changeset
392 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
393 // The weakly referenced object is not alive, clear the reference by storing NULL
a61af66fc99e Initial load
duke
parents:
diff changeset
394 if (TraceReferenceGC) {
a61af66fc99e Initial load
duke
parents:
diff changeset
395 tty->print_cr("Clearing JNI weak reference (" INTPTR_FORMAT ")", root);
a61af66fc99e Initial load
duke
parents:
diff changeset
396 }
a61af66fc99e Initial load
duke
parents:
diff changeset
397 *root = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
398 }
a61af66fc99e Initial load
duke
parents:
diff changeset
399 }
a61af66fc99e Initial load
duke
parents:
diff changeset
400 }
a61af66fc99e Initial load
duke
parents:
diff changeset
401 // the next handle block is valid only if current block is full
a61af66fc99e Initial load
duke
parents:
diff changeset
402 if (current->_top < block_size_in_oops) {
a61af66fc99e Initial load
duke
parents:
diff changeset
403 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
404 }
a61af66fc99e Initial load
duke
parents:
diff changeset
405 }
2125
7246a374a9f2 6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents: 1972
diff changeset
406
7246a374a9f2 6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents: 1972
diff changeset
407 /*
2147
9afee0b9fc1d 7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents: 2125
diff changeset
408 * JVMTI data structures may also contain weak oops. The iteration of them
9afee0b9fc1d 7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents: 2125
diff changeset
409 * is placed here so that we don't need to add it to each of the collectors.
2125
7246a374a9f2 6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents: 1972
diff changeset
410 */
2147
9afee0b9fc1d 7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents: 2125
diff changeset
411 JvmtiExport::weak_oops_do(is_alive, f);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
412 }
a61af66fc99e Initial load
duke
parents:
diff changeset
413
a61af66fc99e Initial load
duke
parents:
diff changeset
414
a61af66fc99e Initial load
duke
parents:
diff changeset
415 jobject JNIHandleBlock::allocate_handle(oop obj) {
a61af66fc99e Initial load
duke
parents:
diff changeset
416 assert(Universe::heap()->is_in_reserved(obj), "sanity check");
a61af66fc99e Initial load
duke
parents:
diff changeset
417 if (_top == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
418 // This is the first allocation or the initial block got zapped when
a61af66fc99e Initial load
duke
parents:
diff changeset
419 // entering a native function. If we have any following blocks they are
a61af66fc99e Initial load
duke
parents:
diff changeset
420 // not valid anymore.
a61af66fc99e Initial load
duke
parents:
diff changeset
421 for (JNIHandleBlock* current = _next; current != NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
422 current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
423 assert(current->_last == NULL, "only first block should have _last set");
a61af66fc99e Initial load
duke
parents:
diff changeset
424 assert(current->_free_list == NULL,
a61af66fc99e Initial load
duke
parents:
diff changeset
425 "only first block should have _free_list set");
a61af66fc99e Initial load
duke
parents:
diff changeset
426 current->_top = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
427 if (ZapJNIHandleArea) current->zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
428 }
a61af66fc99e Initial load
duke
parents:
diff changeset
429 // Clear initial block
a61af66fc99e Initial load
duke
parents:
diff changeset
430 _free_list = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
431 _allocate_before_rebuild = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
432 _last = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
433 if (ZapJNIHandleArea) zap();
a61af66fc99e Initial load
duke
parents:
diff changeset
434 }
a61af66fc99e Initial load
duke
parents:
diff changeset
435
a61af66fc99e Initial load
duke
parents:
diff changeset
436 // Try last block
a61af66fc99e Initial load
duke
parents:
diff changeset
437 if (_last->_top < block_size_in_oops) {
a61af66fc99e Initial load
duke
parents:
diff changeset
438 oop* handle = &(_last->_handles)[_last->_top++];
a61af66fc99e Initial load
duke
parents:
diff changeset
439 *handle = obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
440 return (jobject) handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
441 }
a61af66fc99e Initial load
duke
parents:
diff changeset
442
a61af66fc99e Initial load
duke
parents:
diff changeset
443 // Try free list
a61af66fc99e Initial load
duke
parents:
diff changeset
444 if (_free_list != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
445 oop* handle = _free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
446 _free_list = (oop*) *_free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
447 *handle = obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
448 return (jobject) handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
449 }
a61af66fc99e Initial load
duke
parents:
diff changeset
450 // Check if unused block follow last
a61af66fc99e Initial load
duke
parents:
diff changeset
451 if (_last->_next != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
452 // update last and retry
a61af66fc99e Initial load
duke
parents:
diff changeset
453 _last = _last->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
454 return allocate_handle(obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
455 }
a61af66fc99e Initial load
duke
parents:
diff changeset
456
a61af66fc99e Initial load
duke
parents:
diff changeset
457 // No space available, we have to rebuild free list or expand
a61af66fc99e Initial load
duke
parents:
diff changeset
458 if (_allocate_before_rebuild == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
459 rebuild_free_list(); // updates _allocate_before_rebuild counter
a61af66fc99e Initial load
duke
parents:
diff changeset
460 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
461 // Append new block
a61af66fc99e Initial load
duke
parents:
diff changeset
462 Thread* thread = Thread::current();
a61af66fc99e Initial load
duke
parents:
diff changeset
463 Handle obj_handle(thread, obj);
a61af66fc99e Initial load
duke
parents:
diff changeset
464 // This can block, so we need to preserve obj accross call.
a61af66fc99e Initial load
duke
parents:
diff changeset
465 _last->_next = JNIHandleBlock::allocate_block(thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
466 _last = _last->_next;
a61af66fc99e Initial load
duke
parents:
diff changeset
467 _allocate_before_rebuild--;
a61af66fc99e Initial load
duke
parents:
diff changeset
468 obj = obj_handle();
a61af66fc99e Initial load
duke
parents:
diff changeset
469 }
a61af66fc99e Initial load
duke
parents:
diff changeset
470 return allocate_handle(obj); // retry
a61af66fc99e Initial load
duke
parents:
diff changeset
471 }
a61af66fc99e Initial load
duke
parents:
diff changeset
472
a61af66fc99e Initial load
duke
parents:
diff changeset
473
a61af66fc99e Initial load
duke
parents:
diff changeset
474 void JNIHandleBlock::rebuild_free_list() {
a61af66fc99e Initial load
duke
parents:
diff changeset
475 assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
476 int free = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
477 int blocks = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
478 for (JNIHandleBlock* current = this; current != NULL; current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
479 for (int index = 0; index < current->_top; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
480 oop* handle = &(current->_handles)[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
481 if (*handle == JNIHandles::deleted_handle()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
482 // this handle was cleared out by a delete call, reuse it
a61af66fc99e Initial load
duke
parents:
diff changeset
483 *handle = (oop) _free_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
484 _free_list = handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
485 free++;
a61af66fc99e Initial load
duke
parents:
diff changeset
486 }
a61af66fc99e Initial load
duke
parents:
diff changeset
487 }
a61af66fc99e Initial load
duke
parents:
diff changeset
488 // we should not rebuild free list if there are unused handles at the end
a61af66fc99e Initial load
duke
parents:
diff changeset
489 assert(current->_top == block_size_in_oops, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
490 blocks++;
a61af66fc99e Initial load
duke
parents:
diff changeset
491 }
a61af66fc99e Initial load
duke
parents:
diff changeset
492 // Heuristic: if more than half of the handles are free we rebuild next time
a61af66fc99e Initial load
duke
parents:
diff changeset
493 // as well, otherwise we append a corresponding number of new blocks before
a61af66fc99e Initial load
duke
parents:
diff changeset
494 // attempting a free list rebuild again.
a61af66fc99e Initial load
duke
parents:
diff changeset
495 int total = blocks * block_size_in_oops;
a61af66fc99e Initial load
duke
parents:
diff changeset
496 int extra = total - 2*free;
a61af66fc99e Initial load
duke
parents:
diff changeset
497 if (extra > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
498 // Not as many free handles as we would like - compute number of new blocks to append
a61af66fc99e Initial load
duke
parents:
diff changeset
499 _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops;
a61af66fc99e Initial load
duke
parents:
diff changeset
500 }
a61af66fc99e Initial load
duke
parents:
diff changeset
501 if (TraceJNIHandleAllocation) {
a61af66fc99e Initial load
duke
parents:
diff changeset
502 tty->print_cr("Rebuild free list JNIHandleBlock " INTPTR_FORMAT " blocks=%d used=%d free=%d add=%d",
a61af66fc99e Initial load
duke
parents:
diff changeset
503 this, blocks, total-free, free, _allocate_before_rebuild);
a61af66fc99e Initial load
duke
parents:
diff changeset
504 }
a61af66fc99e Initial load
duke
parents:
diff changeset
505 }
a61af66fc99e Initial load
duke
parents:
diff changeset
506
a61af66fc99e Initial load
duke
parents:
diff changeset
507
a61af66fc99e Initial load
duke
parents:
diff changeset
508 bool JNIHandleBlock::contains(jobject handle) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
509 return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]);
a61af66fc99e Initial load
duke
parents:
diff changeset
510 }
a61af66fc99e Initial load
duke
parents:
diff changeset
511
a61af66fc99e Initial load
duke
parents:
diff changeset
512
a61af66fc99e Initial load
duke
parents:
diff changeset
513 bool JNIHandleBlock::chain_contains(jobject handle) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
514 for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
515 if (current->contains(handle)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
516 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
517 }
a61af66fc99e Initial load
duke
parents:
diff changeset
518 }
a61af66fc99e Initial load
duke
parents:
diff changeset
519 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
520 }
a61af66fc99e Initial load
duke
parents:
diff changeset
521
a61af66fc99e Initial load
duke
parents:
diff changeset
522
a61af66fc99e Initial load
duke
parents:
diff changeset
523 int JNIHandleBlock::length() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
524 int result = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
525 for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) {
a61af66fc99e Initial load
duke
parents:
diff changeset
526 result++;
a61af66fc99e Initial load
duke
parents:
diff changeset
527 }
a61af66fc99e Initial load
duke
parents:
diff changeset
528 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
529 }
a61af66fc99e Initial load
duke
parents:
diff changeset
530
a61af66fc99e Initial load
duke
parents:
diff changeset
531 // This method is not thread-safe, i.e., must be called whule holding a lock on the
a61af66fc99e Initial load
duke
parents:
diff changeset
532 // structure.
a61af66fc99e Initial load
duke
parents:
diff changeset
533 long JNIHandleBlock::memory_usage() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
534 return length() * sizeof(JNIHandleBlock);
a61af66fc99e Initial load
duke
parents:
diff changeset
535 }
a61af66fc99e Initial load
duke
parents:
diff changeset
536
a61af66fc99e Initial load
duke
parents:
diff changeset
537
a61af66fc99e Initial load
duke
parents:
diff changeset
538 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
539
a61af66fc99e Initial load
duke
parents:
diff changeset
540 bool JNIHandleBlock::any_contains(jobject handle) {
a61af66fc99e Initial load
duke
parents:
diff changeset
541 for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) {
a61af66fc99e Initial load
duke
parents:
diff changeset
542 if (current->contains(handle)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
543 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
544 }
a61af66fc99e Initial load
duke
parents:
diff changeset
545 }
a61af66fc99e Initial load
duke
parents:
diff changeset
546 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
547 }
a61af66fc99e Initial load
duke
parents:
diff changeset
548
a61af66fc99e Initial load
duke
parents:
diff changeset
549 void JNIHandleBlock::print_statistics() {
a61af66fc99e Initial load
duke
parents:
diff changeset
550 int used_blocks = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
551 int free_blocks = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
552 int used_handles = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
553 int free_handles = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
554 JNIHandleBlock* block = _block_list;
a61af66fc99e Initial load
duke
parents:
diff changeset
555 while (block != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
556 if (block->_top > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
557 used_blocks++;
a61af66fc99e Initial load
duke
parents:
diff changeset
558 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
559 free_blocks++;
a61af66fc99e Initial load
duke
parents:
diff changeset
560 }
a61af66fc99e Initial load
duke
parents:
diff changeset
561 used_handles += block->_top;
a61af66fc99e Initial load
duke
parents:
diff changeset
562 free_handles += (block_size_in_oops - block->_top);
a61af66fc99e Initial load
duke
parents:
diff changeset
563 block = block->_block_list_link;
a61af66fc99e Initial load
duke
parents:
diff changeset
564 }
a61af66fc99e Initial load
duke
parents:
diff changeset
565 tty->print_cr("JNIHandleBlocks statistics");
a61af66fc99e Initial load
duke
parents:
diff changeset
566 tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks);
a61af66fc99e Initial load
duke
parents:
diff changeset
567 tty->print_cr("- blocks in use: %d", used_blocks);
a61af66fc99e Initial load
duke
parents:
diff changeset
568 tty->print_cr("- blocks free: %d", free_blocks);
a61af66fc99e Initial load
duke
parents:
diff changeset
569 tty->print_cr("- handles in use: %d", used_handles);
a61af66fc99e Initial load
duke
parents:
diff changeset
570 tty->print_cr("- handles free: %d", free_handles);
a61af66fc99e Initial load
duke
parents:
diff changeset
571 }
a61af66fc99e Initial load
duke
parents:
diff changeset
572
a61af66fc99e Initial load
duke
parents:
diff changeset
573 #endif