7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "memory/allocation.hpp"
26 #include "memory/universe.hpp"
27 #include "oops/oop.inline.hpp"
28 #include "oops/weakHandle.inline.hpp"
29 #include "prims/jvmtiExport.hpp"
30 #include "prims/jvmtiTagMapTable.hpp"
31
32
33 JvmtiTagMapKey::JvmtiTagMapKey(oop obj) : _obj(obj) {}
34
35 JvmtiTagMapKey::JvmtiTagMapKey(const JvmtiTagMapKey& src) {
36 // move object into WeakHandle when copying into the table
37 if (src._obj != nullptr) {
38
39 // obj was read with AS_NO_KEEPALIVE, or equivalent, like during
40 // a heap walk. The object needs to be kept alive when it is published.
41 Universe::heap()->keep_alive(src._obj);
42
43 _wh = WeakHandle(JvmtiExport::weak_tag_storage(), src._obj);
44 } else {
45 // resizing needs to create a copy.
46 _wh = src._wh;
47 }
48 // obj is always null after a copy.
49 _obj = nullptr;
50 }
51
52 void JvmtiTagMapKey::release_weak_handle() {
53 _wh.release(JvmtiExport::weak_tag_storage());
54 }
55
56 oop JvmtiTagMapKey::object() const {
57 assert(_obj == nullptr, "Must have a handle and not object");
58 return _wh.resolve();
59 }
60
61 oop JvmtiTagMapKey::object_no_keepalive() const {
62 assert(_obj == nullptr, "Must have a handle and not object");
63 return _wh.peek();
64 }
65
66 static const int INITIAL_TABLE_SIZE = 1007;
67 static const int MAX_TABLE_SIZE = 0x3fffffff;
68
69 JvmtiTagMapTable::JvmtiTagMapTable() : _table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE) {}
70
71 void JvmtiTagMapTable::clear() {
72 struct RemoveAll {
73 bool do_entry(JvmtiTagMapKey& entry, const jlong& tag) {
74 entry.release_weak_handle();
75 return true;
76 }
77 } remove_all;
78 // The unlink method of ResourceHashTable gets a pointer to a type whose 'do_entry(K,V)' method is callled
79 // while iterating over all the elements of the table. If the do_entry() method returns true the element
80 // will be removed.
81 // In this case, we always return true from do_entry to clear all the elements.
82 _table.unlink(&remove_all);
83
84 assert(_table.number_of_entries() == 0, "should have removed all entries");
85 }
86
87 JvmtiTagMapTable::~JvmtiTagMapTable() {
88 clear();
89 }
90
91 jlong JvmtiTagMapTable::find(oop obj) {
92 if (is_empty()) {
93 return 0;
94 }
95
96 if (obj->fast_no_hash_check()) {
97 // Objects in the table all have a hashcode.
98 return 0;
99 }
100
101 JvmtiTagMapKey jtme(obj);
102 jlong* found = _table.get(jtme);
103 return found == nullptr ? 0 : *found;
104 }
105
106 void JvmtiTagMapTable::add(oop obj, jlong tag) {
107 JvmtiTagMapKey new_entry(obj);
108 bool is_added;
109 if (obj->fast_no_hash_check()) {
110 // Can't be in the table so add it fast.
111 is_added = _table.put_when_absent(new_entry, tag);
112 } else {
113 jlong* value = _table.put_if_absent(new_entry, tag, &is_added);
114 *value = tag; // assign the new tag
115 }
116 if (is_added) {
117 if (_table.maybe_grow(5, true /* use_large_table_sizes */)) {
118 int max_bucket_size = DEBUG_ONLY(_table.verify()) NOT_DEBUG(0);
119 log_info(jvmti, table) ("JvmtiTagMap table resized to %d for %d entries max bucket %d",
120 _table.table_size(), _table.number_of_entries(), max_bucket_size);
121 }
122 }
123 }
124
125 void JvmtiTagMapTable::remove(oop obj) {
126 JvmtiTagMapKey jtme(obj);
127 auto clean = [] (JvmtiTagMapKey& entry, jlong tag) {
128 entry.release_weak_handle();
129 };
130 _table.remove(jtme, clean);
131 }
132
133 void JvmtiTagMapTable::entry_iterate(JvmtiTagMapKeyClosure* closure) {
134 _table.iterate(closure);
135 }
136
137 void JvmtiTagMapTable::remove_dead_entries(GrowableArray<jlong>* objects) {
138 struct IsDead {
139 GrowableArray<jlong>* _objects;
140 IsDead(GrowableArray<jlong>* objects) : _objects(objects) {}
141 bool do_entry(JvmtiTagMapKey& entry, jlong tag) {
142 if (entry.object_no_keepalive() == nullptr) {
143 if (_objects != nullptr) {
144 _objects->append(tag);
145 }
146 entry.release_weak_handle();
147 return true;
148 }
149 return false;;
150 }
151 } is_dead(objects);
152 _table.unlink(&is_dead);
153 }
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "memory/allocation.hpp"
26 #include "memory/universe.hpp"
27 #include "oops/fieldStreams.inline.hpp"
28 #include "oops/inlineKlass.hpp"
29 #include "oops/oop.inline.hpp"
30 #include "oops/weakHandle.inline.hpp"
31 #include "prims/jvmtiExport.hpp"
32 #include "prims/jvmtiTagMapTable.hpp"
33
34
35 static unsigned get_value_object_hash(oop holder, int offset, Klass* klass) {
36 assert(klass->is_inline_klass(), "Must be InlineKlass");
37 // For inline types, use the klass as a hash code and let the equals match the obj.
38 // It might have a long bucket but TBD to improve this if a customer situation arises.
39 return (unsigned)((int64_t)klass >> 3);
40 }
41
42 static unsigned get_value_object_hash(const JvmtiHeapwalkObject & obj) {
43 assert(obj.is_value(), "Must be value class");
44 return get_value_object_hash(obj.obj(), obj.offset(), obj.inline_klass());
45 }
46
47 static bool equal_oops(oop obj1, oop obj2); // forward declaration
48
49 static bool equal_fields(char type, oop obj1, int offset1, oop obj2, int offset2) {
50 switch (type) {
51 case JVM_SIGNATURE_BOOLEAN:
52 return obj1->bool_field(offset1) == obj2->bool_field(offset2);
53 case JVM_SIGNATURE_CHAR:
54 return obj1->char_field(offset1) == obj2->char_field(offset2);
55 case JVM_SIGNATURE_FLOAT:
56 return obj1->float_field(offset1) == obj2->float_field(offset2);
57 case JVM_SIGNATURE_DOUBLE:
58 return obj1->double_field(offset1) == obj2->double_field(offset2);
59 case JVM_SIGNATURE_BYTE:
60 return obj1->byte_field(offset1) == obj2->byte_field(offset2);
61 case JVM_SIGNATURE_SHORT:
62 return obj1->short_field(offset1) == obj2->short_field(offset2);
63 case JVM_SIGNATURE_INT:
64 return obj1->int_field(offset1) == obj2->int_field(offset2);
65 case JVM_SIGNATURE_LONG:
66 return obj1->long_field(offset1) == obj2->long_field(offset2);
67 case JVM_SIGNATURE_CLASS:
68 case JVM_SIGNATURE_ARRAY:
69 return equal_oops(obj1->obj_field(offset1), obj2->obj_field(offset2));
70 }
71 ShouldNotReachHere();
72 }
73
74 static bool is_null_flat_field(oop obj, int offset, InlineKlass* klass) {
75 return klass->is_payload_marked_as_null(cast_from_oop<address>(obj) + offset);
76 }
77
78 // For heap-allocated objects offset is 0 and 'klass' is obj1->klass() (== obj2->klass()).
79 // For flattened objects offset is the offset in the holder object, 'klass' is inlined object class.
80 // The object must be prechecked for non-null values.
81 static bool equal_value_objects(oop obj1, int offset1, oop obj2, int offset2, InlineKlass* klass) {
82 for (JavaFieldStream fld(klass); !fld.done(); fld.next()) {
83 // ignore static fields
84 if (fld.access_flags().is_static()) {
85 continue;
86 }
87 int field_offset1 = offset1 + fld.offset() - (offset1 > 0 ? klass->payload_offset() : 0);
88 int field_offset2 = offset2 + fld.offset() - (offset2 > 0 ? klass->payload_offset() : 0);
89 if (fld.is_flat()) { // flat value field
90 InstanceKlass* holder_klass = fld.field_holder();
91 InlineKlass* field_klass = holder_klass->get_inline_type_field_klass(fld.index());
92 if (!fld.is_null_free_inline_type()) {
93 bool field1_is_null = is_null_flat_field(obj1, field_offset1, field_klass);
94 bool field2_is_null = is_null_flat_field(obj2, field_offset2, field_klass);
95 if (field1_is_null != field2_is_null) {
96 return false;
97 }
98 if (field1_is_null) { // if both fields are null, go to next field
99 continue;
100 }
101 }
102
103 if (!equal_value_objects(obj1, field_offset1, obj2, field_offset2, field_klass)) {
104 return false;
105 }
106 } else {
107 if (!equal_fields(fld.signature()->char_at(0), obj1, field_offset1, obj2, field_offset2)) {
108 return false;
109 }
110 }
111 }
112 return true;
113 }
114
115 // handles null oops
116 static bool equal_oops(oop obj1, oop obj2) {
117 if (obj1 == obj2) {
118 return true;
119 }
120
121 if (obj1 != nullptr && obj2 != nullptr && obj1->klass() == obj2->klass() && obj1->is_inline_type()) {
122 InlineKlass* vk = InlineKlass::cast(obj1->klass());
123 return equal_value_objects(obj1, 0, obj2, 0, vk);
124 }
125 return false;
126 }
127
128 bool JvmtiHeapwalkObject::equals(const JvmtiHeapwalkObject& obj1, const JvmtiHeapwalkObject& obj2) {
129 if (obj1 == obj2) { // the same oop/offset/inline_klass
130 return true;
131 }
132
133 if (obj1.is_value() && obj1.inline_klass() == obj2.inline_klass()) {
134 // instances of the same value class
135 return equal_value_objects(obj1.obj(), obj1.offset(), obj2.obj(), obj2.offset(), obj1.inline_klass());
136 }
137 return false;
138 }
139
140 JvmtiTagMapKey::JvmtiTagMapKey(const JvmtiHeapwalkObject* obj) : _obj(obj) {
141 }
142
143 JvmtiTagMapKey::JvmtiTagMapKey(const JvmtiTagMapKey& src): _h() {
144 if (src._obj != nullptr) {
145 // move object into Handle when copying into the table
146 assert(!src._obj->is_flat(), "cannot put flat object to JvmtiTagMapKey");
147 _is_weak = !src._obj->is_value();
148
149 // obj was read with AS_NO_KEEPALIVE, or equivalent, like during
150 // a heap walk. The object needs to be kept alive when it is published.
151 Universe::heap()->keep_alive(src._obj->obj());
152
153 if (_is_weak) {
154 _wh = WeakHandle(JvmtiExport::weak_tag_storage(), src._obj->obj());
155 } else {
156 _h = OopHandle(JvmtiExport::jvmti_oop_storage(), src._obj->obj());
157 }
158 } else {
159 // resizing needs to create a copy.
160 _is_weak = src._is_weak;
161 if (_is_weak) {
162 _wh = src._wh;
163 } else {
164 _h = src._h;
165 }
166 }
167 // obj is always null after a copy.
168 _obj = nullptr;
169 }
170
171 void JvmtiTagMapKey::release_handle() {
172 if (_is_weak) {
173 _wh.release(JvmtiExport::weak_tag_storage());
174 } else {
175 _h.release(JvmtiExport::jvmti_oop_storage());
176 }
177 }
178
179 JvmtiHeapwalkObject JvmtiTagMapKey::heapwalk_object() const {
180 return _obj != nullptr ? JvmtiHeapwalkObject(_obj->obj(), _obj->offset(), _obj->inline_klass(), _obj->layout_kind())
181 : JvmtiHeapwalkObject(object_no_keepalive());
182 }
183
184 oop JvmtiTagMapKey::object() const {
185 assert(_obj == nullptr, "Must have a handle and not object");
186 return _is_weak ? _wh.resolve() : _h.resolve();
187 }
188
189 oop JvmtiTagMapKey::object_no_keepalive() const {
190 assert(_obj == nullptr, "Must have a handle and not object");
191 return _is_weak ? _wh.peek() : _h.peek();
192 }
193
194 unsigned JvmtiTagMapKey::get_hash(const JvmtiTagMapKey& entry) {
195 const JvmtiHeapwalkObject* obj = entry._obj;
196 assert(obj != nullptr, "must lookup obj to hash");
197 if (obj->is_value()) {
198 return get_value_object_hash(*obj);
199 } else {
200 return (unsigned)obj->obj()->identity_hash();
201 }
202 }
203
204 bool JvmtiTagMapKey::equals(const JvmtiTagMapKey& lhs, const JvmtiTagMapKey& rhs) {
205 JvmtiHeapwalkObject lhs_obj = lhs.heapwalk_object();
206 JvmtiHeapwalkObject rhs_obj = rhs.heapwalk_object();
207 return JvmtiHeapwalkObject::equals(lhs_obj, rhs_obj);
208 }
209
210 static const int INITIAL_TABLE_SIZE = 1007;
211 static const int MAX_TABLE_SIZE = 0x3fffffff;
212
213 JvmtiTagMapTable::JvmtiTagMapTable() : _table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE) {}
214
215 void JvmtiTagMapTable::clear() {
216 struct RemoveAll {
217 bool do_entry(JvmtiTagMapKey& entry, const jlong& tag) {
218 entry.release_handle();
219 return true;
220 }
221 } remove_all;
222 // The unlink method of ResourceHashTable gets a pointer to a type whose 'do_entry(K,V)' method is callled
223 // while iterating over all the elements of the table. If the do_entry() method returns true the element
224 // will be removed.
225 // In this case, we always return true from do_entry to clear all the elements.
226 _table.unlink(&remove_all);
227
228 assert(_table.number_of_entries() == 0, "should have removed all entries");
229 }
230
231 JvmtiTagMapTable::~JvmtiTagMapTable() {
232 clear();
233 }
234
235 jlong* JvmtiTagMapTable::lookup(const JvmtiHeapwalkObject& obj) const {
236 if (is_empty()) {
237 return nullptr;
238 }
239
240 if (!obj.is_value()) {
241 if (obj.obj()->fast_no_hash_check()) {
242 // Objects in the table all have a hashcode, unless inlined types.
243 return nullptr;
244 }
245 }
246 JvmtiTagMapKey entry(&obj);
247 jlong* found = _table.get(entry);
248 return found;
249 }
250
251
252 jlong JvmtiTagMapTable::find(const JvmtiHeapwalkObject& obj) const {
253 jlong* found = lookup(obj);
254 return found == nullptr ? 0 : *found;
255 }
256
257 void JvmtiTagMapTable::add(const JvmtiHeapwalkObject& obj, jlong tag) {
258 assert(!obj.is_flat(), "Cannot add flat object to JvmtiTagMapTable");
259 JvmtiTagMapKey new_entry(&obj);
260 bool is_added;
261 if (!obj.is_value() && obj.obj()->fast_no_hash_check()) {
262 // Can't be in the table so add it fast.
263 is_added = _table.put_when_absent(new_entry, tag);
264 } else {
265 jlong* value = _table.put_if_absent(new_entry, tag, &is_added);
266 *value = tag; // assign the new tag
267 }
268 if (is_added) {
269 if (_table.maybe_grow(5, true /* use_large_table_sizes */)) {
270 int max_bucket_size = DEBUG_ONLY(_table.verify()) NOT_DEBUG(0);
271 log_info(jvmti, table) ("JvmtiTagMap table resized to %d for %d entries max bucket %d",
272 _table.table_size(), _table.number_of_entries(), max_bucket_size);
273 }
274 }
275 }
276
277 bool JvmtiTagMapTable::update(const JvmtiHeapwalkObject& obj, jlong tag) {
278 jlong* found = lookup(obj);
279 if (found == nullptr) {
280 return false;
281 }
282 *found = tag;
283 return true;
284 }
285
286 bool JvmtiTagMapTable::remove(const JvmtiHeapwalkObject& obj) {
287 JvmtiTagMapKey entry(&obj);
288 auto clean = [](JvmtiTagMapKey & entry, jlong tag) {
289 entry.release_handle();
290 };
291 return _table.remove(entry, clean);
292 }
293
294 void JvmtiTagMapTable::entry_iterate(JvmtiTagMapKeyClosure* closure) {
295 _table.iterate(closure);
296 }
297
298 void JvmtiTagMapTable::remove_dead_entries(GrowableArray<jlong>* objects) {
299 struct IsDead {
300 GrowableArray<jlong>* _objects;
301 IsDead(GrowableArray<jlong>* objects) : _objects(objects) {}
302 bool do_entry(JvmtiTagMapKey& entry, jlong tag) {
303 if (entry.object_no_keepalive() == nullptr) {
304 if (_objects != nullptr) {
305 _objects->append(tag);
306 }
307 entry.release_handle();
308 return true;
309 }
310 return false;;
311 }
312 } is_dead(objects);
313 _table.unlink(&is_dead);
314 }
315
316 JvmtiFlatTagMapKey::JvmtiFlatTagMapKey(const JvmtiHeapwalkObject& obj)
317 : _holder(obj.obj()), _offset(obj.offset()), _inline_klass(obj.inline_klass()), _layout_kind(obj.layout_kind()) {
318 }
319
320 JvmtiFlatTagMapKey::JvmtiFlatTagMapKey(const JvmtiFlatTagMapKey& src) : _h() {
321 // move object into Handle when copying into the table
322 if (src._holder != nullptr) {
323 // Holder object was read with AS_NO_KEEPALIVE. Needs to be kept alive when it is published.
324 Universe::heap()->keep_alive(src._holder);
325 _h = OopHandle(JvmtiExport::jvmti_oop_storage(), src._holder);
326 } else {
327 // resizing needs to create a copy.
328 _h = src._h;
329 }
330 // holder object is always null after a copy.
331 _holder = nullptr;
332 _offset = src._offset;
333 _inline_klass = src._inline_klass;
334 _layout_kind = src._layout_kind;
335 }
336
337 JvmtiHeapwalkObject JvmtiFlatTagMapKey::heapwalk_object() const {
338 return JvmtiHeapwalkObject(_holder != nullptr ? _holder : holder_no_keepalive(), _offset, _inline_klass, _layout_kind);
339 }
340
341 oop JvmtiFlatTagMapKey::holder() const {
342 assert(_holder == nullptr, "Must have a handle and not object");
343 return _h.resolve();
344 }
345
346 oop JvmtiFlatTagMapKey::holder_no_keepalive() const {
347 assert(_holder == nullptr, "Must have a handle and not object");
348 return _h.peek();
349
350 }
351
352 void JvmtiFlatTagMapKey::release_handle() {
353 _h.release(JvmtiExport::jvmti_oop_storage());
354 }
355
356 unsigned JvmtiFlatTagMapKey::get_hash(const JvmtiFlatTagMapKey& entry) {
357 return get_value_object_hash(entry._holder, entry._offset, entry._inline_klass);
358 }
359
360 bool JvmtiFlatTagMapKey::equals(const JvmtiFlatTagMapKey& lhs, const JvmtiFlatTagMapKey& rhs) {
361 if (lhs._inline_klass == rhs._inline_klass) {
362 oop lhs_obj = lhs._holder != nullptr ? lhs._holder : lhs._h.peek();
363 oop rhs_obj = rhs._holder != nullptr ? rhs._holder : rhs._h.peek();
364 return equal_value_objects(lhs_obj, lhs._offset, rhs_obj, rhs._offset, lhs._inline_klass);
365 }
366 return false;
367 }
368
369 JvmtiFlatTagMapTable::JvmtiFlatTagMapTable(): _table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE) {}
370
371 JvmtiFlatTagMapTable::~JvmtiFlatTagMapTable() {
372 clear();
373 }
374
375 jlong JvmtiFlatTagMapTable::find(const JvmtiHeapwalkObject& obj) const {
376 if (is_empty()) {
377 return 0;
378 }
379
380 JvmtiFlatTagMapKey entry(obj);
381 jlong* found = _table.get(entry);
382 return found == nullptr ? 0 : *found;
383 }
384
385 void JvmtiFlatTagMapTable::add(const JvmtiHeapwalkObject& obj, jlong tag) {
386 assert(obj.is_value() && obj.is_flat(), "Must be flattened value object");
387 JvmtiFlatTagMapKey entry(obj);
388 bool is_added;
389 jlong* value = _table.put_if_absent(entry, tag, &is_added);
390 *value = tag; // assign the new tag
391 if (is_added) {
392 if (_table.maybe_grow(5, true /* use_large_table_sizes */)) {
393 int max_bucket_size = DEBUG_ONLY(_table.verify()) NOT_DEBUG(0);
394 log_info(jvmti, table) ("JvmtiFlatTagMapTable table resized to %d for %d entries max bucket %d",
395 _table.table_size(), _table.number_of_entries(), max_bucket_size);
396 }
397 }
398 }
399
400 jlong JvmtiFlatTagMapTable::remove(const JvmtiHeapwalkObject& obj) {
401 JvmtiFlatTagMapKey entry(obj);
402 jlong ret = 0;
403 auto clean = [&](JvmtiFlatTagMapKey& entry, jlong tag) {
404 ret = tag;
405 entry.release_handle();
406 };
407 _table.remove(entry, clean);
408 return ret;
409 }
410
411 void JvmtiFlatTagMapTable::entry_iterate(JvmtiFlatTagMapKeyClosure* closure) {
412 _table.iterate(closure);
413 }
414
415 void JvmtiFlatTagMapTable::clear() {
416 struct RemoveAll {
417 bool do_entry(JvmtiFlatTagMapKey& entry, const jlong& tag) {
418 entry.release_handle();
419 return true;
420 }
421 } remove_all;
422 // The unlink method of ResourceHashTable gets a pointer to a type whose 'do_entry(K,V)' method is callled
423 // while iterating over all the elements of the table. If the do_entry() method returns true the element
424 // will be removed.
425 // In this case, we always return true from do_entry to clear all the elements.
426 _table.unlink(&remove_all);
427
428 assert(_table.number_of_entries() == 0, "should have removed all entries");
429 }
|