author | dholmes |
Tue, 04 Jun 2019 21:25:40 -0400 | |
changeset 55213 | b78597cfcced |
parent 55153 | 47d928ba8733 |
child 57698 | 9dc92e89243a |
permissions | -rw-r--r-- |
1 | 1 |
/* |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
53075
diff
changeset
|
2 |
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
4571
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
4571
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
4571
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
48816
3495d6050efe
8194898: Move OopStorage inline definitions to an .inline.hpp
kbarrett
parents:
48808
diff
changeset
|
26 |
#include "gc/shared/oopStorage.inline.hpp" |
35061 | 27 |
#include "logging/log.hpp" |
37106 | 28 |
#include "memory/iterator.hpp" |
54786 | 29 |
#include "memory/universe.hpp" |
49656
be608cad0b2a
8195972: Refactor oops in JNI to use the Access API
kbarrett
parents:
49393
diff
changeset
|
30 |
#include "oops/access.inline.hpp" |
7397 | 31 |
#include "oops/oop.inline.hpp" |
49393 | 32 |
#include "runtime/handles.inline.hpp" |
49192
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
33 |
#include "runtime/jniHandles.inline.hpp" |
7397 | 34 |
#include "runtime/mutexLocker.hpp" |
14583
d70ee55535f4
8003935: Simplify the needed includes for using Thread::current()
stefank
parents:
13728
diff
changeset
|
35 |
#include "runtime/thread.inline.hpp" |
46625 | 36 |
#include "utilities/align.hpp" |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
37 |
#include "utilities/debug.hpp" |
1 | 38 |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
39 |
OopStorage* JNIHandles::_global_handles = NULL; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
40 |
OopStorage* JNIHandles::_weak_global_handles = NULL; |
1 | 41 |
|
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
42 |
OopStorage* JNIHandles::global_handles() { |
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
43 |
assert(_global_handles != NULL, "Uninitialized JNI global handles"); |
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
44 |
return _global_handles; |
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
45 |
} |
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
46 |
|
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
47 |
OopStorage* JNIHandles::weak_global_handles() { |
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
48 |
assert(_weak_global_handles != NULL, "Uninitialized JNI weak global handles"); |
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
49 |
return _weak_global_handles; |
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
50 |
} |
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
51 |
|
1 | 52 |
|
53 |
jobject JNIHandles::make_local(oop obj) { |
|
54 |
if (obj == NULL) { |
|
55 |
return NULL; // ignore null handles |
|
56 |
} else { |
|
57 |
Thread* thread = Thread::current(); |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
58 |
assert(oopDesc::is_oop(obj), "not an oop"); |
48465
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
59 |
assert(!current_thread_in_native(), "must not be in native"); |
1 | 60 |
return thread->active_handles()->allocate_handle(obj); |
61 |
} |
|
62 |
} |
|
63 |
||
64 |
||
65 |
// optimized versions |
|
66 |
||
67 |
jobject JNIHandles::make_local(Thread* thread, oop obj) { |
|
68 |
if (obj == NULL) { |
|
69 |
return NULL; // ignore null handles |
|
70 |
} else { |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
71 |
assert(oopDesc::is_oop(obj), "not an oop"); |
48465
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
72 |
assert(thread->is_Java_thread(), "not a Java thread"); |
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
73 |
assert(!current_thread_in_native(), "must not be in native"); |
1 | 74 |
return thread->active_handles()->allocate_handle(obj); |
75 |
} |
|
76 |
} |
|
77 |
||
78 |
||
79 |
jobject JNIHandles::make_local(JNIEnv* env, oop obj) { |
|
80 |
if (obj == NULL) { |
|
81 |
return NULL; // ignore null handles |
|
82 |
} else { |
|
83 |
JavaThread* thread = JavaThread::thread_from_jni_environment(env); |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
84 |
assert(oopDesc::is_oop(obj), "not an oop"); |
48465
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
85 |
assert(!current_thread_in_native(), "must not be in native"); |
1 | 86 |
return thread->active_handles()->allocate_handle(obj); |
87 |
} |
|
88 |
} |
|
89 |
||
90 |
||
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
91 |
static void report_handle_allocation_failure(AllocFailType alloc_failmode, |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
92 |
const char* handle_kind) { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
93 |
if (alloc_failmode == AllocFailStrategy::EXIT_OOM) { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
94 |
// Fake size value, since we don't know the min allocation size here. |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
95 |
vm_exit_out_of_memory(sizeof(oop), OOM_MALLOC_ERROR, |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
96 |
"Cannot create %s JNI handle", handle_kind); |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
97 |
} else { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
98 |
assert(alloc_failmode == AllocFailStrategy::RETURN_NULL, "invariant"); |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
99 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
100 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
101 |
|
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
102 |
jobject JNIHandles::make_global(Handle obj, AllocFailType alloc_failmode) { |
5896
c60924a89622
6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents:
5547
diff
changeset
|
103 |
assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); |
48465
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
104 |
assert(!current_thread_in_native(), "must not be in native"); |
1 | 105 |
jobject res = NULL; |
106 |
if (!obj.is_null()) { |
|
107 |
// ignore null handles |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
108 |
assert(oopDesc::is_oop(obj()), "not an oop"); |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
109 |
oop* ptr = global_handles()->allocate(); |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
110 |
// Return NULL on allocation failure. |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
111 |
if (ptr != NULL) { |
49656
be608cad0b2a
8195972: Refactor oops in JNI to use the Access API
kbarrett
parents:
49393
diff
changeset
|
112 |
assert(*ptr == NULL, "invariant"); |
50803
45c1fde86050
8205559: Remove IN_CONCURRENT_ROOT Access decorator
kbarrett
parents:
50728
diff
changeset
|
113 |
NativeAccess<>::oop_store(ptr, obj()); |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
114 |
res = reinterpret_cast<jobject>(ptr); |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
115 |
} else { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
116 |
report_handle_allocation_failure(alloc_failmode, "global"); |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
117 |
} |
1 | 118 |
} else { |
119 |
CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); |
|
120 |
} |
|
121 |
||
122 |
return res; |
|
123 |
} |
|
124 |
||
125 |
||
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
126 |
jobject JNIHandles::make_weak_global(Handle obj, AllocFailType alloc_failmode) { |
5896
c60924a89622
6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents:
5547
diff
changeset
|
127 |
assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); |
48465
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
128 |
assert(!current_thread_in_native(), "must not be in native"); |
1 | 129 |
jobject res = NULL; |
130 |
if (!obj.is_null()) { |
|
131 |
// ignore null handles |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
132 |
assert(oopDesc::is_oop(obj()), "not an oop"); |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
133 |
oop* ptr = weak_global_handles()->allocate(); |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
134 |
// Return NULL on allocation failure. |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
135 |
if (ptr != NULL) { |
49656
be608cad0b2a
8195972: Refactor oops in JNI to use the Access API
kbarrett
parents:
49393
diff
changeset
|
136 |
assert(*ptr == NULL, "invariant"); |
50599
ecc2af326b5f
8204939: Change Access nomenclature: root to native
kbarrett
parents:
50525
diff
changeset
|
137 |
NativeAccess<ON_PHANTOM_OOP_REF>::oop_store(ptr, obj()); |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
138 |
char* tptr = reinterpret_cast<char*>(ptr) + weak_tag_value; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
139 |
res = reinterpret_cast<jobject>(tptr); |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
140 |
} else { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
141 |
report_handle_allocation_failure(alloc_failmode, "weak global"); |
44406
a46a6c4d1dd9
8176100: [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles
mgerdin
parents:
44093
diff
changeset
|
142 |
} |
1 | 143 |
} else { |
144 |
CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); |
|
145 |
} |
|
146 |
return res; |
|
147 |
} |
|
148 |
||
49192
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
149 |
// Resolve some erroneous cases to NULL, rather than treating them as |
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
150 |
// possibly unchecked errors. In particular, deleted handles are |
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
151 |
// treated as NULL (though a deleted and later reallocated handle |
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
152 |
// isn't detected). |
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
153 |
oop JNIHandles::resolve_external_guard(jobject handle) { |
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
154 |
oop result = NULL; |
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
155 |
if (handle != NULL) { |
53075
747d29313e5a
8215492: Rename INTERNAL_EMPTY to something less "internal"
pliden
parents:
53060
diff
changeset
|
156 |
result = resolve_impl<DECORATORS_NONE, true /* external_guard */>(handle); |
49192
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
157 |
} |
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
158 |
return result; |
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
159 |
} |
6734eeef4283
8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents:
48816
diff
changeset
|
160 |
|
47794
e84aa2c71241
8188102: [JVMCI] Convert special JVMCI oops in nmethod to jweak values
dnsimon
parents:
47779
diff
changeset
|
161 |
bool JNIHandles::is_global_weak_cleared(jweak handle) { |
48808
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
162 |
assert(handle != NULL, "precondition"); |
47794
e84aa2c71241
8188102: [JVMCI] Convert special JVMCI oops in nmethod to jweak values
dnsimon
parents:
47779
diff
changeset
|
163 |
assert(is_jweak(handle), "not a weak handle"); |
49656
be608cad0b2a
8195972: Refactor oops in JNI to use the Access API
kbarrett
parents:
49393
diff
changeset
|
164 |
oop* oop_ptr = jweak_ptr(handle); |
50599
ecc2af326b5f
8204939: Change Access nomenclature: root to native
kbarrett
parents:
50525
diff
changeset
|
165 |
oop value = NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(oop_ptr); |
49656
be608cad0b2a
8195972: Refactor oops in JNI to use the Access API
kbarrett
parents:
49393
diff
changeset
|
166 |
return value == NULL; |
47794
e84aa2c71241
8188102: [JVMCI] Convert special JVMCI oops in nmethod to jweak values
dnsimon
parents:
47779
diff
changeset
|
167 |
} |
e84aa2c71241
8188102: [JVMCI] Convert special JVMCI oops in nmethod to jweak values
dnsimon
parents:
47779
diff
changeset
|
168 |
|
1 | 169 |
void JNIHandles::destroy_global(jobject handle) { |
170 |
if (handle != NULL) { |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
171 |
assert(!is_jweak(handle), "wrong method for detroying jweak"); |
49656
be608cad0b2a
8195972: Refactor oops in JNI to use the Access API
kbarrett
parents:
49393
diff
changeset
|
172 |
oop* oop_ptr = jobject_ptr(handle); |
50803
45c1fde86050
8205559: Remove IN_CONCURRENT_ROOT Access decorator
kbarrett
parents:
50728
diff
changeset
|
173 |
NativeAccess<>::oop_store(oop_ptr, (oop)NULL); |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
174 |
global_handles()->release(oop_ptr); |
1 | 175 |
} |
176 |
} |
|
177 |
||
178 |
||
179 |
void JNIHandles::destroy_weak_global(jobject handle) { |
|
180 |
if (handle != NULL) { |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
181 |
assert(is_jweak(handle), "JNI handle not jweak"); |
49656
be608cad0b2a
8195972: Refactor oops in JNI to use the Access API
kbarrett
parents:
49393
diff
changeset
|
182 |
oop* oop_ptr = jweak_ptr(handle); |
50599
ecc2af326b5f
8204939: Change Access nomenclature: root to native
kbarrett
parents:
50525
diff
changeset
|
183 |
NativeAccess<ON_PHANTOM_OOP_REF>::oop_store(oop_ptr, (oop)NULL); |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
184 |
weak_global_handles()->release(oop_ptr); |
1 | 185 |
} |
186 |
} |
|
187 |
||
188 |
||
189 |
void JNIHandles::oops_do(OopClosure* f) { |
|
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
190 |
global_handles()->oops_do(f); |
1 | 191 |
} |
192 |
||
193 |
||
32817
acc2744fd84b
8136627: Backout JDK-8133818 Additional number of processed references printed with -XX:+PrintReferenceGC after JDK-8047125
kbarrett
parents:
32607
diff
changeset
|
194 |
void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
195 |
weak_global_handles()->weak_oops_do(is_alive, f); |
1 | 196 |
} |
197 |
||
198 |
||
37106 | 199 |
void JNIHandles::weak_oops_do(OopClosure* f) { |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
200 |
weak_global_handles()->weak_oops_do(f); |
37106 | 201 |
} |
202 |
||
203 |
||
1 | 204 |
void JNIHandles::initialize() { |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
205 |
_global_handles = new OopStorage("JNI Global", |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
206 |
JNIGlobalAlloc_lock, |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
207 |
JNIGlobalActive_lock); |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
208 |
_weak_global_handles = new OopStorage("JNI Weak", |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
209 |
JNIWeakAlloc_lock, |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
210 |
JNIWeakActive_lock); |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
211 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
212 |
|
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
213 |
|
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
214 |
inline bool is_storage_handle(const OopStorage* storage, const oop* ptr) { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
215 |
return storage->allocation_status(ptr) == OopStorage::ALLOCATED_ENTRY; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
216 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
217 |
|
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
218 |
|
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
219 |
jobjectRefType JNIHandles::handle_type(Thread* thread, jobject handle) { |
48808
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
220 |
assert(handle != NULL, "precondition"); |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
221 |
jobjectRefType result = JNIInvalidRefType; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
222 |
if (is_jweak(handle)) { |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
223 |
if (is_storage_handle(weak_global_handles(), jweak_ptr(handle))) { |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
224 |
result = JNIWeakGlobalRefType; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
225 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
226 |
} else { |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
227 |
switch (global_handles()->allocation_status(jobject_ptr(handle))) { |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
228 |
case OopStorage::ALLOCATED_ENTRY: |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
229 |
result = JNIGlobalRefType; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
230 |
break; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
231 |
|
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
232 |
case OopStorage::UNALLOCATED_ENTRY: |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
233 |
break; // Invalid global handle |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
234 |
|
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
235 |
case OopStorage::INVALID_ENTRY: |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
236 |
// Not in global storage. Might be a local handle. |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
237 |
if (is_local_handle(thread, handle) || |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
238 |
(thread->is_Java_thread() && |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
239 |
is_frame_handle((JavaThread*)thread, handle))) { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
240 |
result = JNILocalRefType; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
241 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
242 |
break; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
243 |
|
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
244 |
default: |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
245 |
ShouldNotReachHere(); |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
246 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
247 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
248 |
return result; |
1 | 249 |
} |
250 |
||
251 |
||
252 |
bool JNIHandles::is_local_handle(Thread* thread, jobject handle) { |
|
48808
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
253 |
assert(handle != NULL, "precondition"); |
1 | 254 |
JNIHandleBlock* block = thread->active_handles(); |
255 |
||
256 |
// Look back past possible native calls to jni_PushLocalFrame. |
|
257 |
while (block != NULL) { |
|
258 |
if (block->chain_contains(handle)) { |
|
259 |
return true; |
|
260 |
} |
|
261 |
block = block->pop_frame_link(); |
|
262 |
} |
|
263 |
return false; |
|
264 |
} |
|
265 |
||
266 |
||
267 |
// Determine if the handle is somewhere in the current thread's stack. |
|
268 |
// We easily can't isolate any particular stack frame the handle might |
|
269 |
// come from, so we'll check the whole stack. |
|
270 |
||
48808
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
271 |
bool JNIHandles::is_frame_handle(JavaThread* thr, jobject handle) { |
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
272 |
assert(handle != NULL, "precondition"); |
1 | 273 |
// If there is no java frame, then this must be top level code, such |
274 |
// as the java command executable, in which case, this type of handle |
|
275 |
// is not permitted. |
|
276 |
return (thr->has_last_Java_frame() && |
|
48808
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
277 |
(void*)handle < (void*)thr->stack_base() && |
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
278 |
(void*)handle >= (void*)thr->last_Java_sp()); |
1 | 279 |
} |
280 |
||
281 |
||
282 |
bool JNIHandles::is_global_handle(jobject handle) { |
|
48808
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
283 |
assert(handle != NULL, "precondition"); |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
284 |
return !is_jweak(handle) && is_storage_handle(global_handles(), jobject_ptr(handle)); |
1 | 285 |
} |
286 |
||
287 |
||
288 |
bool JNIHandles::is_weak_global_handle(jobject handle) { |
|
48808
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
289 |
assert(handle != NULL, "precondition"); |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
290 |
return is_jweak(handle) && is_storage_handle(weak_global_handles(), jweak_ptr(handle)); |
1 | 291 |
} |
292 |
||
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
293 |
size_t JNIHandles::global_handle_memory_usage() { |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
294 |
return global_handles()->total_memory_usage(); |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
295 |
} |
1 | 296 |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
297 |
size_t JNIHandles::weak_global_handle_memory_usage() { |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
298 |
return weak_global_handles()->total_memory_usage(); |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
299 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
300 |
|
1 | 301 |
|
302 |
// We assume this is called at a safepoint: no lock is needed. |
|
303 |
void JNIHandles::print_on(outputStream* st) { |
|
304 |
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
|
305 |
||
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
306 |
st->print_cr("JNI global refs: " SIZE_FORMAT ", weak refs: " SIZE_FORMAT, |
49909
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
307 |
global_handles()->allocation_count(), |
f276b348ec14
8202230: Provide accessors for JNIHandles storage objects
kbarrett
parents:
49656
diff
changeset
|
308 |
weak_global_handles()->allocation_count()); |
1 | 309 |
st->cr(); |
310 |
st->flush(); |
|
311 |
} |
|
312 |
||
54807 | 313 |
void JNIHandles::print() { print_on(tty); } |
314 |
||
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
315 |
class VerifyJNIHandles: public OopClosure { |
1 | 316 |
public: |
360
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
317 |
virtual void do_oop(oop* root) { |
53057
c7ab0761ef88
8215395: Allow null oops in Dictionary and JNIHandle verification
pliden
parents:
52851
diff
changeset
|
318 |
guarantee(oopDesc::is_oop_or_null(RawAccess<>::oop_load(root)), "Invalid oop"); |
1 | 319 |
} |
360
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
320 |
virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); } |
1 | 321 |
}; |
322 |
||
323 |
void JNIHandles::verify() { |
|
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
324 |
VerifyJNIHandles verify_handle; |
1 | 325 |
|
326 |
oops_do(&verify_handle); |
|
52851
f0c62b8f73c0
8214784: Adjust Dictionary and JNIHandle verification
pliden
parents:
51268
diff
changeset
|
327 |
weak_oops_do(&verify_handle); |
1 | 328 |
} |
329 |
||
48465
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
330 |
// This method is implemented here to avoid circular includes between |
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
331 |
// jniHandles.hpp and thread.hpp. |
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
332 |
bool JNIHandles::current_thread_in_native() { |
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
333 |
Thread* thread = Thread::current(); |
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
334 |
return (thread->is_Java_thread() && |
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
335 |
JavaThread::current()->thread_state() == _thread_in_native); |
f82e79958beb
8167372: Add code to check for getting oops while thread is in native
hseigel
parents:
48147
diff
changeset
|
336 |
} |
1 | 337 |
|
338 |
||
339 |
void jni_handles_init() { |
|
340 |
JNIHandles::initialize(); |
|
341 |
} |
|
342 |
||
343 |
||
344 |
int JNIHandleBlock::_blocks_allocated = 0; |
|
345 |
JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL; |
|
346 |
#ifndef PRODUCT |
|
347 |
JNIHandleBlock* JNIHandleBlock::_block_list = NULL; |
|
348 |
#endif |
|
349 |
||
350 |
||
48147 | 351 |
#ifdef ASSERT |
1 | 352 |
void JNIHandleBlock::zap() { |
353 |
// Zap block values |
|
46703
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
354 |
_top = 0; |
1 | 355 |
for (int index = 0; index < block_size_in_oops; index++) { |
49656
be608cad0b2a
8195972: Refactor oops in JNI to use the Access API
kbarrett
parents:
49393
diff
changeset
|
356 |
// NOT using Access here; just bare clobbering to NULL, since the |
be608cad0b2a
8195972: Refactor oops in JNI to use the Access API
kbarrett
parents:
49393
diff
changeset
|
357 |
// block no longer contains valid oops. |
48147 | 358 |
_handles[index] = NULL; |
1 | 359 |
} |
360 |
} |
|
48147 | 361 |
#endif // ASSERT |
1 | 362 |
|
363 |
JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread) { |
|
364 |
assert(thread == NULL || thread == Thread::current(), "sanity check"); |
|
365 |
JNIHandleBlock* block; |
|
366 |
// Check the thread-local free list for a block so we don't |
|
367 |
// have to acquire a mutex. |
|
368 |
if (thread != NULL && thread->free_handle_block() != NULL) { |
|
369 |
block = thread->free_handle_block(); |
|
370 |
thread->set_free_handle_block(block->_next); |
|
371 |
} |
|
372 |
else { |
|
373 |
// locking with safepoint checking introduces a potential deadlock: |
|
374 |
// - we would hold JNIHandleBlockFreeList_lock and then Threads_lock |
|
375 |
// - another would hold Threads_lock (jni_AttachCurrentThread) and then |
|
376 |
// JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) |
|
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
53075
diff
changeset
|
377 |
MutexLocker ml(JNIHandleBlockFreeList_lock, |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
53075
diff
changeset
|
378 |
Mutex::_no_safepoint_check_flag); |
1 | 379 |
if (_block_free_list == NULL) { |
380 |
// Allocate new block |
|
381 |
block = new JNIHandleBlock(); |
|
382 |
_blocks_allocated++; |
|
48147 | 383 |
block->zap(); |
1 | 384 |
#ifndef PRODUCT |
385 |
// Link new block to list of all allocated blocks |
|
386 |
block->_block_list_link = _block_list; |
|
387 |
_block_list = block; |
|
388 |
#endif |
|
389 |
} else { |
|
390 |
// Get block from free list |
|
391 |
block = _block_free_list; |
|
392 |
_block_free_list = _block_free_list->_next; |
|
393 |
} |
|
394 |
} |
|
46703
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
395 |
block->_top = 0; |
1 | 396 |
block->_next = NULL; |
397 |
block->_pop_frame_link = NULL; |
|
25058
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
398 |
block->_planned_capacity = block_size_in_oops; |
55213
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
399 |
// _last, _free_list & _allocate_before_rebuild initialized in allocate_handle |
1 | 400 |
debug_only(block->_last = NULL); |
55213
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
401 |
debug_only(block->_free_list = NULL); |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
402 |
debug_only(block->_allocate_before_rebuild = -1); |
1 | 403 |
return block; |
404 |
} |
|
405 |
||
406 |
||
407 |
void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) { |
|
408 |
assert(thread == NULL || thread == Thread::current(), "sanity check"); |
|
409 |
JNIHandleBlock* pop_frame_link = block->pop_frame_link(); |
|
410 |
// Put returned block at the beginning of the thread-local free list. |
|
411 |
// Note that if thread == NULL, we use it as an implicit argument that |
|
412 |
// we _don't_ want the block to be kept on the free_handle_block. |
|
413 |
// See for instance JavaThread::exit(). |
|
414 |
if (thread != NULL ) { |
|
48147 | 415 |
block->zap(); |
1 | 416 |
JNIHandleBlock* freelist = thread->free_handle_block(); |
417 |
block->_pop_frame_link = NULL; |
|
418 |
thread->set_free_handle_block(block); |
|
419 |
||
420 |
// Add original freelist to end of chain |
|
421 |
if ( freelist != NULL ) { |
|
422 |
while ( block->_next != NULL ) block = block->_next; |
|
423 |
block->_next = freelist; |
|
424 |
} |
|
425 |
block = NULL; |
|
426 |
} |
|
427 |
if (block != NULL) { |
|
428 |
// Return blocks to free list |
|
429 |
// locking with safepoint checking introduces a potential deadlock: |
|
430 |
// - we would hold JNIHandleBlockFreeList_lock and then Threads_lock |
|
431 |
// - another would hold Threads_lock (jni_AttachCurrentThread) and then |
|
432 |
// JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) |
|
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
53075
diff
changeset
|
433 |
MutexLocker ml(JNIHandleBlockFreeList_lock, |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
53075
diff
changeset
|
434 |
Mutex::_no_safepoint_check_flag); |
1 | 435 |
while (block != NULL) { |
48147 | 436 |
block->zap(); |
1 | 437 |
JNIHandleBlock* next = block->_next; |
438 |
block->_next = _block_free_list; |
|
439 |
_block_free_list = block; |
|
440 |
block = next; |
|
441 |
} |
|
442 |
} |
|
443 |
if (pop_frame_link != NULL) { |
|
444 |
// As a sanity check we release blocks pointed to by the pop_frame_link. |
|
445 |
// This should never happen (only if PopLocalFrame is not called the |
|
446 |
// correct number of times). |
|
447 |
release_block(pop_frame_link, thread); |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
||
452 |
void JNIHandleBlock::oops_do(OopClosure* f) { |
|
453 |
JNIHandleBlock* current_chain = this; |
|
454 |
// Iterate over chain of blocks, followed by chains linked through the |
|
455 |
// pop frame links. |
|
456 |
while (current_chain != NULL) { |
|
457 |
for (JNIHandleBlock* current = current_chain; current != NULL; |
|
458 |
current = current->_next) { |
|
459 |
assert(current == current_chain || current->pop_frame_link() == NULL, |
|
460 |
"only blocks first in chain should have pop frame link set"); |
|
461 |
for (int index = 0; index < current->_top; index++) { |
|
462 |
oop* root = &(current->_handles)[index]; |
|
55213
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
463 |
oop value = *root; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
464 |
// traverse heap pointers only, not deleted handles or free list |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
465 |
// pointers |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
466 |
if (value != NULL && Universe::heap()->is_in_reserved(value)) { |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
467 |
f->do_oop(root); |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
468 |
} |
1 | 469 |
} |
470 |
// the next handle block is valid only if current block is full |
|
471 |
if (current->_top < block_size_in_oops) { |
|
472 |
break; |
|
473 |
} |
|
474 |
} |
|
475 |
current_chain = current_chain->pop_frame_link(); |
|
476 |
} |
|
477 |
} |
|
478 |
||
479 |
||
480 |
jobject JNIHandleBlock::allocate_handle(oop obj) { |
|
481 |
assert(Universe::heap()->is_in_reserved(obj), "sanity check"); |
|
482 |
if (_top == 0) { |
|
483 |
// This is the first allocation or the initial block got zapped when |
|
484 |
// entering a native function. If we have any following blocks they are |
|
485 |
// not valid anymore. |
|
486 |
for (JNIHandleBlock* current = _next; current != NULL; |
|
487 |
current = current->_next) { |
|
488 |
assert(current->_last == NULL, "only first block should have _last set"); |
|
55213
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
489 |
assert(current->_free_list == NULL, |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
490 |
"only first block should have _free_list set"); |
46703
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
491 |
if (current->_top == 0) { |
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
492 |
// All blocks after the first clear trailing block are already cleared. |
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
493 |
#ifdef ASSERT |
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
494 |
for (current = current->_next; current != NULL; current = current->_next) { |
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
495 |
assert(current->_top == 0, "trailing blocks must already be cleared"); |
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
496 |
} |
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
497 |
#endif |
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
498 |
break; |
f152b500376e
8175318: Performance issue regarding local JNI references
kbarrett
parents:
46625
diff
changeset
|
499 |
} |
1 | 500 |
current->_top = 0; |
48147 | 501 |
current->zap(); |
1 | 502 |
} |
503 |
// Clear initial block |
|
55213
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
504 |
_free_list = NULL; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
505 |
_allocate_before_rebuild = 0; |
1 | 506 |
_last = this; |
48147 | 507 |
zap(); |
1 | 508 |
} |
509 |
||
510 |
// Try last block |
|
511 |
if (_last->_top < block_size_in_oops) { |
|
512 |
oop* handle = &(_last->_handles)[_last->_top++]; |
|
50728 | 513 |
NativeAccess<IS_DEST_UNINITIALIZED>::oop_store(handle, obj); |
1 | 514 |
return (jobject) handle; |
515 |
} |
|
516 |
||
55213
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
517 |
// Try free list |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
518 |
if (_free_list != NULL) { |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
519 |
oop* handle = _free_list; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
520 |
_free_list = (oop*) *_free_list; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
521 |
NativeAccess<IS_DEST_UNINITIALIZED>::oop_store(handle, obj); |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
522 |
return (jobject) handle; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
523 |
} |
1 | 524 |
// Check if unused block follow last |
525 |
if (_last->_next != NULL) { |
|
526 |
// update last and retry |
|
527 |
_last = _last->_next; |
|
528 |
return allocate_handle(obj); |
|
529 |
} |
|
530 |
||
55213
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
531 |
// No space available, we have to rebuild free list or expand |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
532 |
if (_allocate_before_rebuild == 0) { |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
533 |
rebuild_free_list(); // updates _allocate_before_rebuild counter |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
534 |
} else { |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
535 |
// Append new block |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
536 |
Thread* thread = Thread::current(); |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
537 |
Handle obj_handle(thread, obj); |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
538 |
// This can block, so we need to preserve obj across call. |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
539 |
_last->_next = JNIHandleBlock::allocate_block(thread); |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
540 |
_last = _last->_next; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
541 |
_allocate_before_rebuild--; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
542 |
obj = obj_handle(); |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
543 |
} |
1 | 544 |
return allocate_handle(obj); // retry |
545 |
} |
|
546 |
||
55213
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
547 |
void JNIHandleBlock::rebuild_free_list() { |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
548 |
assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking"); |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
549 |
int free = 0; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
550 |
int blocks = 0; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
551 |
for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
552 |
for (int index = 0; index < current->_top; index++) { |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
553 |
oop* handle = &(current->_handles)[index]; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
554 |
if (*handle == NULL) { |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
555 |
// this handle was cleared out by a delete call, reuse it |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
556 |
*handle = (oop) _free_list; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
557 |
_free_list = handle; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
558 |
free++; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
559 |
} |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
560 |
} |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
561 |
// we should not rebuild free list if there are unused handles at the end |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
562 |
assert(current->_top == block_size_in_oops, "just checking"); |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
563 |
blocks++; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
564 |
} |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
565 |
// Heuristic: if more than half of the handles are free we rebuild next time |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
566 |
// as well, otherwise we append a corresponding number of new blocks before |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
567 |
// attempting a free list rebuild again. |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
568 |
int total = blocks * block_size_in_oops; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
569 |
int extra = total - 2*free; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
570 |
if (extra > 0) { |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
571 |
// Not as many free handles as we would like - compute number of new blocks to append |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
572 |
_allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops; |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
573 |
} |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
574 |
} |
b78597cfcced
8225217: Backout: JDK-8224814: Remove dead JNIHandleBlock freelist code
dholmes
parents:
55153
diff
changeset
|
575 |
|
1 | 576 |
|
577 |
bool JNIHandleBlock::contains(jobject handle) const { |
|
578 |
return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]); |
|
579 |
} |
|
580 |
||
581 |
||
582 |
bool JNIHandleBlock::chain_contains(jobject handle) const { |
|
583 |
for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) { |
|
584 |
if (current->contains(handle)) { |
|
585 |
return true; |
|
586 |
} |
|
587 |
} |
|
588 |
return false; |
|
589 |
} |
|
590 |
||
591 |
||
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
592 |
size_t JNIHandleBlock::length() const { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
593 |
size_t result = 1; |
1 | 594 |
for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) { |
595 |
result++; |
|
596 |
} |
|
597 |
return result; |
|
598 |
} |
|
599 |
||
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
600 |
class CountJNIHandleClosure: public OopClosure { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
601 |
private: |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
602 |
int _count; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
603 |
public: |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
604 |
CountJNIHandleClosure(): _count(0) {} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
605 |
virtual void do_oop(oop* ooph) { _count++; } |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
606 |
virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); } |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
607 |
int count() { return _count; } |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
608 |
}; |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
609 |
|
25058
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
610 |
const size_t JNIHandleBlock::get_number_of_live_handles() { |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
611 |
CountJNIHandleClosure counter; |
25058
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
612 |
oops_do(&counter); |
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
613 |
return counter.count(); |
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
614 |
} |
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
615 |
|
22551 | 616 |
// This method is not thread-safe, i.e., must be called while holding a lock on the |
1 | 617 |
// structure. |
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
618 |
size_t JNIHandleBlock::memory_usage() const { |
1 | 619 |
return length() * sizeof(JNIHandleBlock); |
620 |
} |
|
621 |
||
622 |
||
623 |
#ifndef PRODUCT |
|
624 |
||
48787
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
625 |
bool JNIHandles::is_local_handle(jobject handle) { |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
626 |
return JNIHandleBlock::any_contains(handle); |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
627 |
} |
7638bf98a312
8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
48465
diff
changeset
|
628 |
|
1 | 629 |
bool JNIHandleBlock::any_contains(jobject handle) { |
48808
2b0b7f222800
8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents:
48787
diff
changeset
|
630 |
assert(handle != NULL, "precondition"); |
1 | 631 |
for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) { |
632 |
if (current->contains(handle)) { |
|
633 |
return true; |
|
634 |
} |
|
635 |
} |
|
636 |
return false; |
|
637 |
} |
|
638 |
||
639 |
void JNIHandleBlock::print_statistics() { |
|
640 |
int used_blocks = 0; |
|
641 |
int free_blocks = 0; |
|
642 |
int used_handles = 0; |
|
643 |
int free_handles = 0; |
|
644 |
JNIHandleBlock* block = _block_list; |
|
645 |
while (block != NULL) { |
|
646 |
if (block->_top > 0) { |
|
647 |
used_blocks++; |
|
648 |
} else { |
|
649 |
free_blocks++; |
|
650 |
} |
|
651 |
used_handles += block->_top; |
|
652 |
free_handles += (block_size_in_oops - block->_top); |
|
653 |
block = block->_block_list_link; |
|
654 |
} |
|
655 |
tty->print_cr("JNIHandleBlocks statistics"); |
|
656 |
tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks); |
|
657 |
tty->print_cr("- blocks in use: %d", used_blocks); |
|
658 |
tty->print_cr("- blocks free: %d", free_blocks); |
|
659 |
tty->print_cr("- handles in use: %d", used_handles); |
|
660 |
tty->print_cr("- handles free: %d", free_handles); |
|
661 |
} |
|
662 |
||
663 |
#endif |