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