Mercurial > hg > graal-jvmci-8
annotate src/share/vm/prims/jvmtiTagMap.cpp @ 2316:d411927672ed
7012072: CompileTheWorld causes incorrect class initialization
Reviewed-by: kvn, twisti
author | never |
---|---|
date | Wed, 23 Feb 2011 19:09:16 -0800 |
parents | 3582bf76420e |
children | c7f3d0b4570f |
rev | line source |
---|---|
0 | 1 /* |
1972 | 2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
0 | 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 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1142
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1142
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1142
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #include "precompiled.hpp" |
26 #include "classfile/symbolTable.hpp" | |
27 #include "classfile/systemDictionary.hpp" | |
28 #include "classfile/vmSymbols.hpp" | |
29 #include "jvmtifiles/jvmtiEnv.hpp" | |
30 #include "oops/objArrayKlass.hpp" | |
31 #include "oops/oop.inline2.hpp" | |
32 #include "prims/jvmtiEventController.hpp" | |
33 #include "prims/jvmtiEventController.inline.hpp" | |
34 #include "prims/jvmtiExport.hpp" | |
35 #include "prims/jvmtiImpl.hpp" | |
36 #include "prims/jvmtiTagMap.hpp" | |
37 #include "runtime/biasedLocking.hpp" | |
38 #include "runtime/javaCalls.hpp" | |
39 #include "runtime/jniHandles.hpp" | |
40 #include "runtime/mutex.hpp" | |
41 #include "runtime/mutexLocker.hpp" | |
42 #include "runtime/reflectionUtils.hpp" | |
43 #include "runtime/vframe.hpp" | |
44 #include "runtime/vmThread.hpp" | |
45 #include "runtime/vm_operations.hpp" | |
46 #include "services/serviceUtil.hpp" | |
47 #ifndef SERIALGC | |
48 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp" | |
49 #endif | |
0 | 50 |
51 // JvmtiTagHashmapEntry | |
52 // | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
53 // Each entry encapsulates a reference to the tagged object |
0 | 54 // and the tag value. In addition an entry includes a next pointer which |
55 // is used to chain entries together. | |
56 | |
57 class JvmtiTagHashmapEntry : public CHeapObj { | |
58 private: | |
59 friend class JvmtiTagMap; | |
60 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
61 oop _object; // tagged object |
0 | 62 jlong _tag; // the tag |
63 JvmtiTagHashmapEntry* _next; // next on the list | |
64 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
65 inline void init(oop object, jlong tag) { |
0 | 66 _object = object; |
67 _tag = tag; | |
68 _next = NULL; | |
69 } | |
70 | |
71 // constructor | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
72 JvmtiTagHashmapEntry(oop object, jlong tag) { init(object, tag); } |
0 | 73 |
74 public: | |
75 | |
76 // accessor methods | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
77 inline oop object() const { return _object; } |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
78 inline oop* object_addr() { return &_object; } |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
79 inline jlong tag() const { return _tag; } |
0 | 80 |
81 inline void set_tag(jlong tag) { | |
82 assert(tag != 0, "can't be zero"); | |
83 _tag = tag; | |
84 } | |
85 | |
86 inline JvmtiTagHashmapEntry* next() const { return _next; } | |
87 inline void set_next(JvmtiTagHashmapEntry* next) { _next = next; } | |
88 }; | |
89 | |
90 | |
91 // JvmtiTagHashmap | |
92 // | |
93 // A hashmap is essentially a table of pointers to entries. Entries | |
94 // are hashed to a location, or position in the table, and then | |
95 // chained from that location. The "key" for hashing is address of | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
96 // the object, or oop. The "value" is the tag value. |
0 | 97 // |
98 // A hashmap maintains a count of the number entries in the hashmap | |
99 // and resizes if the number of entries exceeds a given threshold. | |
100 // The threshold is specified as a percentage of the size - for | |
101 // example a threshold of 0.75 will trigger the hashmap to resize | |
102 // if the number of entries is >75% of table size. | |
103 // | |
104 // A hashmap provides functions for adding, removing, and finding | |
105 // entries. It also provides a function to iterate over all entries | |
106 // in the hashmap. | |
107 | |
108 class JvmtiTagHashmap : public CHeapObj { | |
109 private: | |
110 friend class JvmtiTagMap; | |
111 | |
112 enum { | |
113 small_trace_threshold = 10000, // threshold for tracing | |
114 medium_trace_threshold = 100000, | |
115 large_trace_threshold = 1000000, | |
116 initial_trace_threshold = small_trace_threshold | |
117 }; | |
118 | |
119 static int _sizes[]; // array of possible hashmap sizes | |
120 int _size; // actual size of the table | |
121 int _size_index; // index into size table | |
122 | |
123 int _entry_count; // number of entries in the hashmap | |
124 | |
125 float _load_factor; // load factor as a % of the size | |
126 int _resize_threshold; // computed threshold to trigger resizing. | |
127 bool _resizing_enabled; // indicates if hashmap can resize | |
128 | |
129 int _trace_threshold; // threshold for trace messages | |
130 | |
131 JvmtiTagHashmapEntry** _table; // the table of entries. | |
132 | |
133 // private accessors | |
134 int resize_threshold() const { return _resize_threshold; } | |
135 int trace_threshold() const { return _trace_threshold; } | |
136 | |
137 // initialize the hashmap | |
138 void init(int size_index=0, float load_factor=4.0f) { | |
139 int initial_size = _sizes[size_index]; | |
140 _size_index = size_index; | |
141 _size = initial_size; | |
142 _entry_count = 0; | |
143 if (TraceJVMTIObjectTagging) { | |
144 _trace_threshold = initial_trace_threshold; | |
145 } else { | |
146 _trace_threshold = -1; | |
147 } | |
148 _load_factor = load_factor; | |
149 _resize_threshold = (int)(_load_factor * _size); | |
150 _resizing_enabled = true; | |
151 size_t s = initial_size * sizeof(JvmtiTagHashmapEntry*); | |
152 _table = (JvmtiTagHashmapEntry**)os::malloc(s); | |
153 if (_table == NULL) { | |
154 vm_exit_out_of_memory(s, "unable to allocate initial hashtable for jvmti object tags"); | |
155 } | |
156 for (int i=0; i<initial_size; i++) { | |
157 _table[i] = NULL; | |
158 } | |
159 } | |
160 | |
161 // hash a given key (oop) with the specified size | |
162 static unsigned int hash(oop key, int size) { | |
163 // shift right to get better distribution (as these bits will be zero | |
164 // with aligned addresses) | |
165 unsigned int addr = (unsigned int)((intptr_t)key); | |
166 #ifdef _LP64 | |
167 return (addr >> 3) % size; | |
168 #else | |
169 return (addr >> 2) % size; | |
170 #endif | |
171 } | |
172 | |
173 // hash a given key (oop) | |
174 unsigned int hash(oop key) { | |
175 return hash(key, _size); | |
176 } | |
177 | |
178 // resize the hashmap - allocates a large table and re-hashes | |
179 // all entries into the new table. | |
180 void resize() { | |
181 int new_size_index = _size_index+1; | |
182 int new_size = _sizes[new_size_index]; | |
183 if (new_size < 0) { | |
184 // hashmap already at maximum capacity | |
185 return; | |
186 } | |
187 | |
188 // allocate new table | |
189 size_t s = new_size * sizeof(JvmtiTagHashmapEntry*); | |
190 JvmtiTagHashmapEntry** new_table = (JvmtiTagHashmapEntry**)os::malloc(s); | |
191 if (new_table == NULL) { | |
192 warning("unable to allocate larger hashtable for jvmti object tags"); | |
193 set_resizing_enabled(false); | |
194 return; | |
195 } | |
196 | |
197 // initialize new table | |
198 int i; | |
199 for (i=0; i<new_size; i++) { | |
200 new_table[i] = NULL; | |
201 } | |
202 | |
203 // rehash all entries into the new table | |
204 for (i=0; i<_size; i++) { | |
205 JvmtiTagHashmapEntry* entry = _table[i]; | |
206 while (entry != NULL) { | |
207 JvmtiTagHashmapEntry* next = entry->next(); | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
208 oop key = entry->object(); |
0 | 209 assert(key != NULL, "jni weak reference cleared!!"); |
210 unsigned int h = hash(key, new_size); | |
211 JvmtiTagHashmapEntry* anchor = new_table[h]; | |
212 if (anchor == NULL) { | |
213 new_table[h] = entry; | |
214 entry->set_next(NULL); | |
215 } else { | |
216 entry->set_next(anchor); | |
217 new_table[h] = entry; | |
218 } | |
219 entry = next; | |
220 } | |
221 } | |
222 | |
223 // free old table and update settings. | |
224 os::free((void*)_table); | |
225 _table = new_table; | |
226 _size_index = new_size_index; | |
227 _size = new_size; | |
228 | |
229 // compute new resize threshold | |
230 _resize_threshold = (int)(_load_factor * _size); | |
231 } | |
232 | |
233 | |
234 // internal remove function - remove an entry at a given position in the | |
235 // table. | |
236 inline void remove(JvmtiTagHashmapEntry* prev, int pos, JvmtiTagHashmapEntry* entry) { | |
237 assert(pos >= 0 && pos < _size, "out of range"); | |
238 if (prev == NULL) { | |
239 _table[pos] = entry->next(); | |
240 } else { | |
241 prev->set_next(entry->next()); | |
242 } | |
243 assert(_entry_count > 0, "checking"); | |
244 _entry_count--; | |
245 } | |
246 | |
247 // resizing switch | |
248 bool is_resizing_enabled() const { return _resizing_enabled; } | |
249 void set_resizing_enabled(bool enable) { _resizing_enabled = enable; } | |
250 | |
251 // debugging | |
252 void print_memory_usage(); | |
253 void compute_next_trace_threshold(); | |
254 | |
255 public: | |
256 | |
257 // create a JvmtiTagHashmap of a preferred size and optionally a load factor. | |
258 // The preferred size is rounded down to an actual size. | |
259 JvmtiTagHashmap(int size, float load_factor=0.0f) { | |
260 int i=0; | |
261 while (_sizes[i] < size) { | |
262 if (_sizes[i] < 0) { | |
263 assert(i > 0, "sanity check"); | |
264 i--; | |
265 break; | |
266 } | |
267 i++; | |
268 } | |
269 | |
270 // if a load factor is specified then use it, otherwise use default | |
271 if (load_factor > 0.01f) { | |
272 init(i, load_factor); | |
273 } else { | |
274 init(i); | |
275 } | |
276 } | |
277 | |
278 // create a JvmtiTagHashmap with default settings | |
279 JvmtiTagHashmap() { | |
280 init(); | |
281 } | |
282 | |
283 // release table when JvmtiTagHashmap destroyed | |
284 ~JvmtiTagHashmap() { | |
285 if (_table != NULL) { | |
286 os::free((void*)_table); | |
287 _table = NULL; | |
288 } | |
289 } | |
290 | |
291 // accessors | |
292 int size() const { return _size; } | |
293 JvmtiTagHashmapEntry** table() const { return _table; } | |
294 int entry_count() const { return _entry_count; } | |
295 | |
296 // find an entry in the hashmap, returns NULL if not found. | |
297 inline JvmtiTagHashmapEntry* find(oop key) { | |
298 unsigned int h = hash(key); | |
299 JvmtiTagHashmapEntry* entry = _table[h]; | |
300 while (entry != NULL) { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
301 if (entry->object() == key) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
302 return entry; |
0 | 303 } |
304 entry = entry->next(); | |
305 } | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
306 return NULL; |
0 | 307 } |
308 | |
309 | |
310 // add a new entry to hashmap | |
311 inline void add(oop key, JvmtiTagHashmapEntry* entry) { | |
312 assert(key != NULL, "checking"); | |
313 assert(find(key) == NULL, "duplicate detected"); | |
314 unsigned int h = hash(key); | |
315 JvmtiTagHashmapEntry* anchor = _table[h]; | |
316 if (anchor == NULL) { | |
317 _table[h] = entry; | |
318 entry->set_next(NULL); | |
319 } else { | |
320 entry->set_next(anchor); | |
321 _table[h] = entry; | |
322 } | |
323 | |
324 _entry_count++; | |
325 if (trace_threshold() > 0 && entry_count() >= trace_threshold()) { | |
326 assert(TraceJVMTIObjectTagging, "should only get here when tracing"); | |
327 print_memory_usage(); | |
328 compute_next_trace_threshold(); | |
329 } | |
330 | |
331 // if the number of entries exceed the threshold then resize | |
332 if (entry_count() > resize_threshold() && is_resizing_enabled()) { | |
333 resize(); | |
334 } | |
335 } | |
336 | |
337 // remove an entry with the given key. | |
338 inline JvmtiTagHashmapEntry* remove(oop key) { | |
339 unsigned int h = hash(key); | |
340 JvmtiTagHashmapEntry* entry = _table[h]; | |
341 JvmtiTagHashmapEntry* prev = NULL; | |
342 while (entry != NULL) { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
343 if (key == entry->object()) { |
0 | 344 break; |
345 } | |
346 prev = entry; | |
347 entry = entry->next(); | |
348 } | |
349 if (entry != NULL) { | |
350 remove(prev, h, entry); | |
351 } | |
352 return entry; | |
353 } | |
354 | |
355 // iterate over all entries in the hashmap | |
356 void entry_iterate(JvmtiTagHashmapEntryClosure* closure); | |
357 }; | |
358 | |
359 // possible hashmap sizes - odd primes that roughly double in size. | |
360 // To avoid excessive resizing the odd primes from 4801-76831 and | |
361 // 76831-307261 have been removed. The list must be terminated by -1. | |
362 int JvmtiTagHashmap::_sizes[] = { 4801, 76831, 307261, 614563, 1228891, | |
363 2457733, 4915219, 9830479, 19660831, 39321619, 78643219, -1 }; | |
364 | |
365 | |
366 // A supporting class for iterating over all entries in Hashmap | |
367 class JvmtiTagHashmapEntryClosure { | |
368 public: | |
369 virtual void do_entry(JvmtiTagHashmapEntry* entry) = 0; | |
370 }; | |
371 | |
372 | |
373 // iterate over all entries in the hashmap | |
374 void JvmtiTagHashmap::entry_iterate(JvmtiTagHashmapEntryClosure* closure) { | |
375 for (int i=0; i<_size; i++) { | |
376 JvmtiTagHashmapEntry* entry = _table[i]; | |
377 JvmtiTagHashmapEntry* prev = NULL; | |
378 while (entry != NULL) { | |
379 // obtain the next entry before invoking do_entry - this is | |
380 // necessary because do_entry may remove the entry from the | |
381 // hashmap. | |
382 JvmtiTagHashmapEntry* next = entry->next(); | |
383 closure->do_entry(entry); | |
384 entry = next; | |
385 } | |
386 } | |
387 } | |
388 | |
389 // debugging | |
390 void JvmtiTagHashmap::print_memory_usage() { | |
391 intptr_t p = (intptr_t)this; | |
392 tty->print("[JvmtiTagHashmap @ " INTPTR_FORMAT, p); | |
393 | |
394 // table + entries in KB | |
395 int hashmap_usage = (size()*sizeof(JvmtiTagHashmapEntry*) + | |
396 entry_count()*sizeof(JvmtiTagHashmapEntry))/K; | |
397 | |
398 int weak_globals_usage = (int)(JNIHandles::weak_global_handle_memory_usage()/K); | |
399 tty->print_cr(", %d entries (%d KB) <JNI weak globals: %d KB>]", | |
400 entry_count(), hashmap_usage, weak_globals_usage); | |
401 } | |
402 | |
403 // compute threshold for the next trace message | |
404 void JvmtiTagHashmap::compute_next_trace_threshold() { | |
405 if (trace_threshold() < medium_trace_threshold) { | |
406 _trace_threshold += small_trace_threshold; | |
407 } else { | |
408 if (trace_threshold() < large_trace_threshold) { | |
409 _trace_threshold += medium_trace_threshold; | |
410 } else { | |
411 _trace_threshold += large_trace_threshold; | |
412 } | |
413 } | |
414 } | |
415 | |
416 // create a JvmtiTagMap | |
417 JvmtiTagMap::JvmtiTagMap(JvmtiEnv* env) : | |
418 _env(env), | |
419 _lock(Mutex::nonleaf+2, "JvmtiTagMap._lock", false), | |
420 _free_entries(NULL), | |
421 _free_entries_count(0) | |
422 { | |
423 assert(JvmtiThreadState_lock->is_locked(), "sanity check"); | |
424 assert(((JvmtiEnvBase *)env)->tag_map() == NULL, "tag map already exists for environment"); | |
425 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
426 _hashmap = new JvmtiTagHashmap(); |
0 | 427 |
428 // finally add us to the environment | |
429 ((JvmtiEnvBase *)env)->set_tag_map(this); | |
430 } | |
431 | |
432 | |
433 // destroy a JvmtiTagMap | |
434 JvmtiTagMap::~JvmtiTagMap() { | |
435 | |
436 // no lock acquired as we assume the enclosing environment is | |
437 // also being destroryed. | |
438 ((JvmtiEnvBase *)_env)->set_tag_map(NULL); | |
439 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
440 JvmtiTagHashmapEntry** table = _hashmap->table(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
441 for (int j = 0; j < _hashmap->size(); j++) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
442 JvmtiTagHashmapEntry* entry = table[j]; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
443 while (entry != NULL) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
444 JvmtiTagHashmapEntry* next = entry->next(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
445 delete entry; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
446 entry = next; |
0 | 447 } |
448 } | |
449 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
450 // finally destroy the hashmap |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
451 delete _hashmap; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
452 _hashmap = NULL; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
453 |
0 | 454 // remove any entries on the free list |
455 JvmtiTagHashmapEntry* entry = _free_entries; | |
456 while (entry != NULL) { | |
457 JvmtiTagHashmapEntry* next = entry->next(); | |
458 delete entry; | |
459 entry = next; | |
460 } | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
461 _free_entries = NULL; |
0 | 462 } |
463 | |
464 // create a hashmap entry | |
465 // - if there's an entry on the (per-environment) free list then this | |
466 // is returned. Otherwise an new entry is allocated. | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
467 JvmtiTagHashmapEntry* JvmtiTagMap::create_entry(oop ref, jlong tag) { |
0 | 468 assert(Thread::current()->is_VM_thread() || is_locked(), "checking"); |
469 JvmtiTagHashmapEntry* entry; | |
470 if (_free_entries == NULL) { | |
471 entry = new JvmtiTagHashmapEntry(ref, tag); | |
472 } else { | |
473 assert(_free_entries_count > 0, "mismatched _free_entries_count"); | |
474 _free_entries_count--; | |
475 entry = _free_entries; | |
476 _free_entries = entry->next(); | |
477 entry->init(ref, tag); | |
478 } | |
479 return entry; | |
480 } | |
481 | |
482 // destroy an entry by returning it to the free list | |
483 void JvmtiTagMap::destroy_entry(JvmtiTagHashmapEntry* entry) { | |
484 assert(SafepointSynchronize::is_at_safepoint() || is_locked(), "checking"); | |
485 // limit the size of the free list | |
486 if (_free_entries_count >= max_free_entries) { | |
487 delete entry; | |
488 } else { | |
489 entry->set_next(_free_entries); | |
490 _free_entries = entry; | |
491 _free_entries_count++; | |
492 } | |
493 } | |
494 | |
495 // returns the tag map for the given environments. If the tag map | |
496 // doesn't exist then it is created. | |
497 JvmtiTagMap* JvmtiTagMap::tag_map_for(JvmtiEnv* env) { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
498 JvmtiTagMap* tag_map = ((JvmtiEnvBase*)env)->tag_map(); |
0 | 499 if (tag_map == NULL) { |
500 MutexLocker mu(JvmtiThreadState_lock); | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
501 tag_map = ((JvmtiEnvBase*)env)->tag_map(); |
0 | 502 if (tag_map == NULL) { |
503 tag_map = new JvmtiTagMap(env); | |
504 } | |
505 } else { | |
506 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); | |
507 } | |
508 return tag_map; | |
509 } | |
510 | |
511 // iterate over all entries in the tag map. | |
512 void JvmtiTagMap::entry_iterate(JvmtiTagHashmapEntryClosure* closure) { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
513 hashmap()->entry_iterate(closure); |
0 | 514 } |
515 | |
516 // returns true if the hashmaps are empty | |
517 bool JvmtiTagMap::is_empty() { | |
518 assert(SafepointSynchronize::is_at_safepoint() || is_locked(), "checking"); | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
519 return hashmap()->entry_count() == 0; |
0 | 520 } |
521 | |
522 | |
523 // Return the tag value for an object, or 0 if the object is | |
524 // not tagged | |
525 // | |
526 static inline jlong tag_for(JvmtiTagMap* tag_map, oop o) { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
527 JvmtiTagHashmapEntry* entry = tag_map->hashmap()->find(o); |
0 | 528 if (entry == NULL) { |
529 return 0; | |
530 } else { | |
531 return entry->tag(); | |
532 } | |
533 } | |
534 | |
535 // If the object is a java.lang.Class then return the klassOop, | |
536 // otherwise return the original object | |
537 static inline oop klassOop_if_java_lang_Class(oop o) { | |
1142 | 538 if (o->klass() == SystemDictionary::Class_klass()) { |
0 | 539 if (!java_lang_Class::is_primitive(o)) { |
540 o = (oop)java_lang_Class::as_klassOop(o); | |
541 assert(o != NULL, "class for non-primitive mirror must exist"); | |
542 } | |
543 } | |
544 return o; | |
545 } | |
546 | |
547 // A CallbackWrapper is a support class for querying and tagging an object | |
548 // around a callback to a profiler. The constructor does pre-callback | |
549 // work to get the tag value, klass tag value, ... and the destructor | |
550 // does the post-callback work of tagging or untagging the object. | |
551 // | |
552 // { | |
553 // CallbackWrapper wrapper(tag_map, o); | |
554 // | |
555 // (*callback)(wrapper.klass_tag(), wrapper.obj_size(), wrapper.obj_tag_p(), ...) | |
556 // | |
557 // } // wrapper goes out of scope here which results in the destructor | |
558 // checking to see if the object has been tagged, untagged, or the | |
559 // tag value has changed. | |
560 // | |
561 class CallbackWrapper : public StackObj { | |
562 private: | |
563 JvmtiTagMap* _tag_map; | |
564 JvmtiTagHashmap* _hashmap; | |
565 JvmtiTagHashmapEntry* _entry; | |
566 oop _o; | |
567 jlong _obj_size; | |
568 jlong _obj_tag; | |
569 klassOop _klass; // the object's class | |
570 jlong _klass_tag; | |
571 | |
572 protected: | |
573 JvmtiTagMap* tag_map() const { return _tag_map; } | |
574 | |
575 // invoked post-callback to tag, untag, or update the tag of an object | |
576 void inline post_callback_tag_update(oop o, JvmtiTagHashmap* hashmap, | |
577 JvmtiTagHashmapEntry* entry, jlong obj_tag); | |
578 public: | |
579 CallbackWrapper(JvmtiTagMap* tag_map, oop o) { | |
580 assert(Thread::current()->is_VM_thread() || tag_map->is_locked(), | |
581 "MT unsafe or must be VM thread"); | |
582 | |
583 // for Classes the klassOop is tagged | |
584 _o = klassOop_if_java_lang_Class(o); | |
585 | |
586 // object size | |
587 _obj_size = _o->size() * wordSize; | |
588 | |
589 // record the context | |
590 _tag_map = tag_map; | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
591 _hashmap = tag_map->hashmap(); |
0 | 592 _entry = _hashmap->find(_o); |
593 | |
594 // get object tag | |
595 _obj_tag = (_entry == NULL) ? 0 : _entry->tag(); | |
596 | |
597 // get the class and the class's tag value | |
598 if (_o == o) { | |
599 _klass = _o->klass(); | |
600 } else { | |
601 // if the object represents a runtime class then use the | |
602 // tag for java.lang.Class | |
1142 | 603 _klass = SystemDictionary::Class_klass(); |
0 | 604 } |
605 _klass_tag = tag_for(tag_map, _klass); | |
606 } | |
607 | |
608 ~CallbackWrapper() { | |
609 post_callback_tag_update(_o, _hashmap, _entry, _obj_tag); | |
610 } | |
611 | |
612 inline jlong* obj_tag_p() { return &_obj_tag; } | |
613 inline jlong obj_size() const { return _obj_size; } | |
614 inline jlong obj_tag() const { return _obj_tag; } | |
615 inline klassOop klass() const { return _klass; } | |
616 inline jlong klass_tag() const { return _klass_tag; } | |
617 }; | |
618 | |
619 | |
620 | |
621 // callback post-callback to tag, untag, or update the tag of an object | |
622 void inline CallbackWrapper::post_callback_tag_update(oop o, | |
623 JvmtiTagHashmap* hashmap, | |
624 JvmtiTagHashmapEntry* entry, | |
625 jlong obj_tag) { | |
626 if (entry == NULL) { | |
627 if (obj_tag != 0) { | |
628 // callback has tagged the object | |
629 assert(Thread::current()->is_VM_thread(), "must be VMThread"); | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
630 entry = tag_map()->create_entry(o, obj_tag); |
0 | 631 hashmap->add(o, entry); |
632 } | |
633 } else { | |
634 // object was previously tagged - the callback may have untagged | |
635 // the object or changed the tag value | |
636 if (obj_tag == 0) { | |
637 | |
638 JvmtiTagHashmapEntry* entry_removed = hashmap->remove(o); | |
639 assert(entry_removed == entry, "checking"); | |
640 tag_map()->destroy_entry(entry); | |
641 | |
642 } else { | |
643 if (obj_tag != entry->tag()) { | |
644 entry->set_tag(obj_tag); | |
645 } | |
646 } | |
647 } | |
648 } | |
649 | |
650 // An extended CallbackWrapper used when reporting an object reference | |
651 // to the agent. | |
652 // | |
653 // { | |
654 // TwoOopCallbackWrapper wrapper(tag_map, referrer, o); | |
655 // | |
656 // (*callback)(wrapper.klass_tag(), | |
657 // wrapper.obj_size(), | |
658 // wrapper.obj_tag_p() | |
659 // wrapper.referrer_tag_p(), ...) | |
660 // | |
661 // } // wrapper goes out of scope here which results in the destructor | |
662 // checking to see if the referrer object has been tagged, untagged, | |
663 // or the tag value has changed. | |
664 // | |
665 class TwoOopCallbackWrapper : public CallbackWrapper { | |
666 private: | |
667 bool _is_reference_to_self; | |
668 JvmtiTagHashmap* _referrer_hashmap; | |
669 JvmtiTagHashmapEntry* _referrer_entry; | |
670 oop _referrer; | |
671 jlong _referrer_obj_tag; | |
672 jlong _referrer_klass_tag; | |
673 jlong* _referrer_tag_p; | |
674 | |
675 bool is_reference_to_self() const { return _is_reference_to_self; } | |
676 | |
677 public: | |
678 TwoOopCallbackWrapper(JvmtiTagMap* tag_map, oop referrer, oop o) : | |
679 CallbackWrapper(tag_map, o) | |
680 { | |
681 // self reference needs to be handled in a special way | |
682 _is_reference_to_self = (referrer == o); | |
683 | |
684 if (_is_reference_to_self) { | |
685 _referrer_klass_tag = klass_tag(); | |
686 _referrer_tag_p = obj_tag_p(); | |
687 } else { | |
688 // for Classes the klassOop is tagged | |
689 _referrer = klassOop_if_java_lang_Class(referrer); | |
690 // record the context | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
691 _referrer_hashmap = tag_map->hashmap(); |
0 | 692 _referrer_entry = _referrer_hashmap->find(_referrer); |
693 | |
694 // get object tag | |
695 _referrer_obj_tag = (_referrer_entry == NULL) ? 0 : _referrer_entry->tag(); | |
696 _referrer_tag_p = &_referrer_obj_tag; | |
697 | |
698 // get referrer class tag. | |
699 klassOop k = (_referrer == referrer) ? // Check if referrer is a class... | |
700 _referrer->klass() // No, just get its class | |
1142 | 701 : SystemDictionary::Class_klass(); // Yes, its class is Class |
0 | 702 _referrer_klass_tag = tag_for(tag_map, k); |
703 } | |
704 } | |
705 | |
706 ~TwoOopCallbackWrapper() { | |
707 if (!is_reference_to_self()){ | |
708 post_callback_tag_update(_referrer, | |
709 _referrer_hashmap, | |
710 _referrer_entry, | |
711 _referrer_obj_tag); | |
712 } | |
713 } | |
714 | |
715 // address of referrer tag | |
716 // (for a self reference this will return the same thing as obj_tag_p()) | |
717 inline jlong* referrer_tag_p() { return _referrer_tag_p; } | |
718 | |
719 // referrer's class tag | |
720 inline jlong referrer_klass_tag() { return _referrer_klass_tag; } | |
721 }; | |
722 | |
723 // tag an object | |
724 // | |
725 // This function is performance critical. If many threads attempt to tag objects | |
726 // around the same time then it's possible that the Mutex associated with the | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
727 // tag map will be a hot lock. |
0 | 728 void JvmtiTagMap::set_tag(jobject object, jlong tag) { |
729 MutexLocker ml(lock()); | |
730 | |
731 // resolve the object | |
732 oop o = JNIHandles::resolve_non_null(object); | |
733 | |
734 // for Classes we tag the klassOop | |
735 o = klassOop_if_java_lang_Class(o); | |
736 | |
737 // see if the object is already tagged | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
738 JvmtiTagHashmap* hashmap = _hashmap; |
0 | 739 JvmtiTagHashmapEntry* entry = hashmap->find(o); |
740 | |
741 // if the object is not already tagged then we tag it | |
742 if (entry == NULL) { | |
743 if (tag != 0) { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
744 entry = create_entry(o, tag); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
745 hashmap->add(o, entry); |
0 | 746 } else { |
747 // no-op | |
748 } | |
749 } else { | |
750 // if the object is already tagged then we either update | |
751 // the tag (if a new tag value has been provided) | |
752 // or remove the object if the new tag value is 0. | |
753 if (tag == 0) { | |
754 hashmap->remove(o); | |
755 destroy_entry(entry); | |
756 } else { | |
757 entry->set_tag(tag); | |
758 } | |
759 } | |
760 } | |
761 | |
762 // get the tag for an object | |
763 jlong JvmtiTagMap::get_tag(jobject object) { | |
764 MutexLocker ml(lock()); | |
765 | |
766 // resolve the object | |
767 oop o = JNIHandles::resolve_non_null(object); | |
768 | |
769 // for Classes get the tag from the klassOop | |
770 return tag_for(this, klassOop_if_java_lang_Class(o)); | |
771 } | |
772 | |
773 | |
774 // Helper class used to describe the static or instance fields of a class. | |
775 // For each field it holds the field index (as defined by the JVMTI specification), | |
776 // the field type, and the offset. | |
777 | |
778 class ClassFieldDescriptor: public CHeapObj { | |
779 private: | |
780 int _field_index; | |
781 int _field_offset; | |
782 char _field_type; | |
783 public: | |
784 ClassFieldDescriptor(int index, char type, int offset) : | |
785 _field_index(index), _field_type(type), _field_offset(offset) { | |
786 } | |
787 int field_index() const { return _field_index; } | |
788 char field_type() const { return _field_type; } | |
789 int field_offset() const { return _field_offset; } | |
790 }; | |
791 | |
792 class ClassFieldMap: public CHeapObj { | |
793 private: | |
794 enum { | |
795 initial_field_count = 5 | |
796 }; | |
797 | |
798 // list of field descriptors | |
799 GrowableArray<ClassFieldDescriptor*>* _fields; | |
800 | |
801 // constructor | |
802 ClassFieldMap(); | |
803 | |
804 // add a field | |
805 void add(int index, char type, int offset); | |
806 | |
807 // returns the field count for the given class | |
808 static int compute_field_count(instanceKlassHandle ikh); | |
809 | |
810 public: | |
811 ~ClassFieldMap(); | |
812 | |
813 // access | |
814 int field_count() { return _fields->length(); } | |
815 ClassFieldDescriptor* field_at(int i) { return _fields->at(i); } | |
816 | |
817 // functions to create maps of static or instance fields | |
818 static ClassFieldMap* create_map_of_static_fields(klassOop k); | |
819 static ClassFieldMap* create_map_of_instance_fields(oop obj); | |
820 }; | |
821 | |
822 ClassFieldMap::ClassFieldMap() { | |
823 _fields = new (ResourceObj::C_HEAP) GrowableArray<ClassFieldDescriptor*>(initial_field_count, true); | |
824 } | |
825 | |
826 ClassFieldMap::~ClassFieldMap() { | |
827 for (int i=0; i<_fields->length(); i++) { | |
828 delete _fields->at(i); | |
829 } | |
830 delete _fields; | |
831 } | |
832 | |
833 void ClassFieldMap::add(int index, char type, int offset) { | |
834 ClassFieldDescriptor* field = new ClassFieldDescriptor(index, type, offset); | |
835 _fields->append(field); | |
836 } | |
837 | |
838 // Returns a heap allocated ClassFieldMap to describe the static fields | |
839 // of the given class. | |
840 // | |
841 ClassFieldMap* ClassFieldMap::create_map_of_static_fields(klassOop k) { | |
842 HandleMark hm; | |
843 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k); | |
844 | |
845 // create the field map | |
846 ClassFieldMap* field_map = new ClassFieldMap(); | |
847 | |
848 FilteredFieldStream f(ikh, false, false); | |
849 int max_field_index = f.field_count()-1; | |
850 | |
851 int index = 0; | |
852 for (FilteredFieldStream fld(ikh, true, true); !fld.eos(); fld.next(), index++) { | |
853 // ignore instance fields | |
854 if (!fld.access_flags().is_static()) { | |
855 continue; | |
856 } | |
857 field_map->add(max_field_index - index, fld.signature()->byte_at(0), fld.offset()); | |
858 } | |
859 return field_map; | |
860 } | |
861 | |
862 // Returns a heap allocated ClassFieldMap to describe the instance fields | |
863 // of the given class. All instance fields are included (this means public | |
864 // and private fields declared in superclasses and superinterfaces too). | |
865 // | |
866 ClassFieldMap* ClassFieldMap::create_map_of_instance_fields(oop obj) { | |
867 HandleMark hm; | |
868 instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), obj->klass()); | |
869 | |
870 // create the field map | |
871 ClassFieldMap* field_map = new ClassFieldMap(); | |
872 | |
873 FilteredFieldStream f(ikh, false, false); | |
874 | |
875 int max_field_index = f.field_count()-1; | |
876 | |
877 int index = 0; | |
878 for (FilteredFieldStream fld(ikh, false, false); !fld.eos(); fld.next(), index++) { | |
879 // ignore static fields | |
880 if (fld.access_flags().is_static()) { | |
881 continue; | |
882 } | |
883 field_map->add(max_field_index - index, fld.signature()->byte_at(0), fld.offset()); | |
884 } | |
885 | |
886 return field_map; | |
887 } | |
888 | |
889 // Helper class used to cache a ClassFileMap for the instance fields of | |
890 // a cache. A JvmtiCachedClassFieldMap can be cached by an instanceKlass during | |
891 // heap iteration and avoid creating a field map for each object in the heap | |
892 // (only need to create the map when the first instance of a class is encountered). | |
893 // | |
894 class JvmtiCachedClassFieldMap : public CHeapObj { | |
895 private: | |
896 enum { | |
897 initial_class_count = 200 | |
898 }; | |
899 ClassFieldMap* _field_map; | |
900 | |
901 ClassFieldMap* field_map() const { return _field_map; } | |
902 | |
903 JvmtiCachedClassFieldMap(ClassFieldMap* field_map); | |
904 ~JvmtiCachedClassFieldMap(); | |
905 | |
906 static GrowableArray<instanceKlass*>* _class_list; | |
907 static void add_to_class_list(instanceKlass* ik); | |
908 | |
909 public: | |
910 // returns the field map for a given object (returning map cached | |
911 // by instanceKlass if possible | |
912 static ClassFieldMap* get_map_of_instance_fields(oop obj); | |
913 | |
914 // removes the field map from all instanceKlasses - should be | |
915 // called before VM operation completes | |
916 static void clear_cache(); | |
917 | |
918 // returns the number of ClassFieldMap cached by instanceKlasses | |
919 static int cached_field_map_count(); | |
920 }; | |
921 | |
922 GrowableArray<instanceKlass*>* JvmtiCachedClassFieldMap::_class_list; | |
923 | |
924 JvmtiCachedClassFieldMap::JvmtiCachedClassFieldMap(ClassFieldMap* field_map) { | |
925 _field_map = field_map; | |
926 } | |
927 | |
928 JvmtiCachedClassFieldMap::~JvmtiCachedClassFieldMap() { | |
929 if (_field_map != NULL) { | |
930 delete _field_map; | |
931 } | |
932 } | |
933 | |
934 // Marker class to ensure that the class file map cache is only used in a defined | |
935 // scope. | |
936 class ClassFieldMapCacheMark : public StackObj { | |
937 private: | |
938 static bool _is_active; | |
939 public: | |
940 ClassFieldMapCacheMark() { | |
941 assert(Thread::current()->is_VM_thread(), "must be VMThread"); | |
942 assert(JvmtiCachedClassFieldMap::cached_field_map_count() == 0, "cache not empty"); | |
943 assert(!_is_active, "ClassFieldMapCacheMark cannot be nested"); | |
944 _is_active = true; | |
945 } | |
946 ~ClassFieldMapCacheMark() { | |
947 JvmtiCachedClassFieldMap::clear_cache(); | |
948 _is_active = false; | |
949 } | |
950 static bool is_active() { return _is_active; } | |
951 }; | |
952 | |
953 bool ClassFieldMapCacheMark::_is_active; | |
954 | |
955 | |
956 // record that the given instanceKlass is caching a field map | |
957 void JvmtiCachedClassFieldMap::add_to_class_list(instanceKlass* ik) { | |
958 if (_class_list == NULL) { | |
959 _class_list = new (ResourceObj::C_HEAP) GrowableArray<instanceKlass*>(initial_class_count, true); | |
960 } | |
961 _class_list->push(ik); | |
962 } | |
963 | |
964 // returns the instance field map for the given object | |
965 // (returns field map cached by the instanceKlass if possible) | |
966 ClassFieldMap* JvmtiCachedClassFieldMap::get_map_of_instance_fields(oop obj) { | |
967 assert(Thread::current()->is_VM_thread(), "must be VMThread"); | |
968 assert(ClassFieldMapCacheMark::is_active(), "ClassFieldMapCacheMark not active"); | |
969 | |
970 klassOop k = obj->klass(); | |
971 instanceKlass* ik = instanceKlass::cast(k); | |
972 | |
973 // return cached map if possible | |
974 JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map(); | |
975 if (cached_map != NULL) { | |
976 assert(cached_map->field_map() != NULL, "missing field list"); | |
977 return cached_map->field_map(); | |
978 } else { | |
979 ClassFieldMap* field_map = ClassFieldMap::create_map_of_instance_fields(obj); | |
980 cached_map = new JvmtiCachedClassFieldMap(field_map); | |
981 ik->set_jvmti_cached_class_field_map(cached_map); | |
982 add_to_class_list(ik); | |
983 return field_map; | |
984 } | |
985 } | |
986 | |
987 // remove the fields maps cached from all instanceKlasses | |
988 void JvmtiCachedClassFieldMap::clear_cache() { | |
989 assert(Thread::current()->is_VM_thread(), "must be VMThread"); | |
990 if (_class_list != NULL) { | |
991 for (int i = 0; i < _class_list->length(); i++) { | |
992 instanceKlass* ik = _class_list->at(i); | |
993 JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map(); | |
994 assert(cached_map != NULL, "should not be NULL"); | |
995 ik->set_jvmti_cached_class_field_map(NULL); | |
996 delete cached_map; // deletes the encapsulated field map | |
997 } | |
998 delete _class_list; | |
999 _class_list = NULL; | |
1000 } | |
1001 } | |
1002 | |
1003 // returns the number of ClassFieldMap cached by instanceKlasses | |
1004 int JvmtiCachedClassFieldMap::cached_field_map_count() { | |
1005 return (_class_list == NULL) ? 0 : _class_list->length(); | |
1006 } | |
1007 | |
1008 // helper function to indicate if an object is filtered by its tag or class tag | |
1009 static inline bool is_filtered_by_heap_filter(jlong obj_tag, | |
1010 jlong klass_tag, | |
1011 int heap_filter) { | |
1012 // apply the heap filter | |
1013 if (obj_tag != 0) { | |
1014 // filter out tagged objects | |
1015 if (heap_filter & JVMTI_HEAP_FILTER_TAGGED) return true; | |
1016 } else { | |
1017 // filter out untagged objects | |
1018 if (heap_filter & JVMTI_HEAP_FILTER_UNTAGGED) return true; | |
1019 } | |
1020 if (klass_tag != 0) { | |
1021 // filter out objects with tagged classes | |
1022 if (heap_filter & JVMTI_HEAP_FILTER_CLASS_TAGGED) return true; | |
1023 } else { | |
1024 // filter out objects with untagged classes. | |
1025 if (heap_filter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) return true; | |
1026 } | |
1027 return false; | |
1028 } | |
1029 | |
1030 // helper function to indicate if an object is filtered by a klass filter | |
1031 static inline bool is_filtered_by_klass_filter(oop obj, KlassHandle klass_filter) { | |
1032 if (!klass_filter.is_null()) { | |
1033 if (obj->klass() != klass_filter()) { | |
1034 return true; | |
1035 } | |
1036 } | |
1037 return false; | |
1038 } | |
1039 | |
1040 // helper function to tell if a field is a primitive field or not | |
1041 static inline bool is_primitive_field_type(char type) { | |
1042 return (type != 'L' && type != '['); | |
1043 } | |
1044 | |
1045 // helper function to copy the value from location addr to jvalue. | |
1046 static inline void copy_to_jvalue(jvalue *v, address addr, jvmtiPrimitiveType value_type) { | |
1047 switch (value_type) { | |
1048 case JVMTI_PRIMITIVE_TYPE_BOOLEAN : { v->z = *(jboolean*)addr; break; } | |
1049 case JVMTI_PRIMITIVE_TYPE_BYTE : { v->b = *(jbyte*)addr; break; } | |
1050 case JVMTI_PRIMITIVE_TYPE_CHAR : { v->c = *(jchar*)addr; break; } | |
1051 case JVMTI_PRIMITIVE_TYPE_SHORT : { v->s = *(jshort*)addr; break; } | |
1052 case JVMTI_PRIMITIVE_TYPE_INT : { v->i = *(jint*)addr; break; } | |
1053 case JVMTI_PRIMITIVE_TYPE_LONG : { v->j = *(jlong*)addr; break; } | |
1054 case JVMTI_PRIMITIVE_TYPE_FLOAT : { v->f = *(jfloat*)addr; break; } | |
1055 case JVMTI_PRIMITIVE_TYPE_DOUBLE : { v->d = *(jdouble*)addr; break; } | |
1056 default: ShouldNotReachHere(); | |
1057 } | |
1058 } | |
1059 | |
1060 // helper function to invoke string primitive value callback | |
1061 // returns visit control flags | |
1062 static jint invoke_string_value_callback(jvmtiStringPrimitiveValueCallback cb, | |
1063 CallbackWrapper* wrapper, | |
1064 oop str, | |
1065 void* user_data) | |
1066 { | |
1142 | 1067 assert(str->klass() == SystemDictionary::String_klass(), "not a string"); |
0 | 1068 |
1069 // get the string value and length | |
1070 // (string value may be offset from the base) | |
1071 int s_len = java_lang_String::length(str); | |
1072 typeArrayOop s_value = java_lang_String::value(str); | |
1073 int s_offset = java_lang_String::offset(str); | |
1074 jchar* value; | |
1075 if (s_len > 0) { | |
1076 value = s_value->char_at_addr(s_offset); | |
1077 } else { | |
1078 value = (jchar*) s_value->base(T_CHAR); | |
1079 } | |
1080 | |
1081 // invoke the callback | |
1082 return (*cb)(wrapper->klass_tag(), | |
1083 wrapper->obj_size(), | |
1084 wrapper->obj_tag_p(), | |
1085 value, | |
1086 (jint)s_len, | |
1087 user_data); | |
1088 } | |
1089 | |
1090 // helper function to invoke string primitive value callback | |
1091 // returns visit control flags | |
1092 static jint invoke_array_primitive_value_callback(jvmtiArrayPrimitiveValueCallback cb, | |
1093 CallbackWrapper* wrapper, | |
1094 oop obj, | |
1095 void* user_data) | |
1096 { | |
1097 assert(obj->is_typeArray(), "not a primitive array"); | |
1098 | |
1099 // get base address of first element | |
1100 typeArrayOop array = typeArrayOop(obj); | |
1101 BasicType type = typeArrayKlass::cast(array->klass())->element_type(); | |
1102 void* elements = array->base(type); | |
1103 | |
1104 // jvmtiPrimitiveType is defined so this mapping is always correct | |
1105 jvmtiPrimitiveType elem_type = (jvmtiPrimitiveType)type2char(type); | |
1106 | |
1107 return (*cb)(wrapper->klass_tag(), | |
1108 wrapper->obj_size(), | |
1109 wrapper->obj_tag_p(), | |
1110 (jint)array->length(), | |
1111 elem_type, | |
1112 elements, | |
1113 user_data); | |
1114 } | |
1115 | |
1116 // helper function to invoke the primitive field callback for all static fields | |
1117 // of a given class | |
1118 static jint invoke_primitive_field_callback_for_static_fields | |
1119 (CallbackWrapper* wrapper, | |
1120 oop obj, | |
1121 jvmtiPrimitiveFieldCallback cb, | |
1122 void* user_data) | |
1123 { | |
1124 // for static fields only the index will be set | |
1125 static jvmtiHeapReferenceInfo reference_info = { 0 }; | |
1126 | |
1142 | 1127 assert(obj->klass() == SystemDictionary::Class_klass(), "not a class"); |
0 | 1128 if (java_lang_Class::is_primitive(obj)) { |
1129 return 0; | |
1130 } | |
1131 klassOop k = java_lang_Class::as_klassOop(obj); | |
1132 Klass* klass = k->klass_part(); | |
1133 | |
1134 // ignore classes for object and type arrays | |
1135 if (!klass->oop_is_instance()) { | |
1136 return 0; | |
1137 } | |
1138 | |
1139 // ignore classes which aren't linked yet | |
1140 instanceKlass* ik = instanceKlass::cast(k); | |
1141 if (!ik->is_linked()) { | |
1142 return 0; | |
1143 } | |
1144 | |
1145 // get the field map | |
1146 ClassFieldMap* field_map = ClassFieldMap::create_map_of_static_fields(k); | |
1147 | |
1148 // invoke the callback for each static primitive field | |
1149 for (int i=0; i<field_map->field_count(); i++) { | |
1150 ClassFieldDescriptor* field = field_map->field_at(i); | |
1151 | |
1152 // ignore non-primitive fields | |
1153 char type = field->field_type(); | |
1154 if (!is_primitive_field_type(type)) { | |
1155 continue; | |
1156 } | |
1157 // one-to-one mapping | |
1158 jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type; | |
1159 | |
1160 // get offset and field value | |
1161 int offset = field->field_offset(); | |
1162 address addr = (address)k + offset; | |
1163 jvalue value; | |
1164 copy_to_jvalue(&value, addr, value_type); | |
1165 | |
1166 // field index | |
1167 reference_info.field.index = field->field_index(); | |
1168 | |
1169 // invoke the callback | |
1170 jint res = (*cb)(JVMTI_HEAP_REFERENCE_STATIC_FIELD, | |
1171 &reference_info, | |
1172 wrapper->klass_tag(), | |
1173 wrapper->obj_tag_p(), | |
1174 value, | |
1175 value_type, | |
1176 user_data); | |
1177 if (res & JVMTI_VISIT_ABORT) { | |
1178 delete field_map; | |
1179 return res; | |
1180 } | |
1181 } | |
1182 | |
1183 delete field_map; | |
1184 return 0; | |
1185 } | |
1186 | |
1187 // helper function to invoke the primitive field callback for all instance fields | |
1188 // of a given object | |
1189 static jint invoke_primitive_field_callback_for_instance_fields( | |
1190 CallbackWrapper* wrapper, | |
1191 oop obj, | |
1192 jvmtiPrimitiveFieldCallback cb, | |
1193 void* user_data) | |
1194 { | |
1195 // for instance fields only the index will be set | |
1196 static jvmtiHeapReferenceInfo reference_info = { 0 }; | |
1197 | |
1198 // get the map of the instance fields | |
1199 ClassFieldMap* fields = JvmtiCachedClassFieldMap::get_map_of_instance_fields(obj); | |
1200 | |
1201 // invoke the callback for each instance primitive field | |
1202 for (int i=0; i<fields->field_count(); i++) { | |
1203 ClassFieldDescriptor* field = fields->field_at(i); | |
1204 | |
1205 // ignore non-primitive fields | |
1206 char type = field->field_type(); | |
1207 if (!is_primitive_field_type(type)) { | |
1208 continue; | |
1209 } | |
1210 // one-to-one mapping | |
1211 jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type; | |
1212 | |
1213 // get offset and field value | |
1214 int offset = field->field_offset(); | |
1215 address addr = (address)obj + offset; | |
1216 jvalue value; | |
1217 copy_to_jvalue(&value, addr, value_type); | |
1218 | |
1219 // field index | |
1220 reference_info.field.index = field->field_index(); | |
1221 | |
1222 // invoke the callback | |
1223 jint res = (*cb)(JVMTI_HEAP_REFERENCE_FIELD, | |
1224 &reference_info, | |
1225 wrapper->klass_tag(), | |
1226 wrapper->obj_tag_p(), | |
1227 value, | |
1228 value_type, | |
1229 user_data); | |
1230 if (res & JVMTI_VISIT_ABORT) { | |
1231 return res; | |
1232 } | |
1233 } | |
1234 return 0; | |
1235 } | |
1236 | |
1237 | |
1238 // VM operation to iterate over all objects in the heap (both reachable | |
1239 // and unreachable) | |
1240 class VM_HeapIterateOperation: public VM_Operation { | |
1241 private: | |
1242 ObjectClosure* _blk; | |
1243 public: | |
1244 VM_HeapIterateOperation(ObjectClosure* blk) { _blk = blk; } | |
1245 | |
1246 VMOp_Type type() const { return VMOp_HeapIterateOperation; } | |
1247 void doit() { | |
1248 // allows class files maps to be cached during iteration | |
1249 ClassFieldMapCacheMark cm; | |
1250 | |
1251 // make sure that heap is parsable (fills TLABs with filler objects) | |
1252 Universe::heap()->ensure_parsability(false); // no need to retire TLABs | |
1253 | |
1254 // Verify heap before iteration - if the heap gets corrupted then | |
1255 // JVMTI's IterateOverHeap will crash. | |
1256 if (VerifyBeforeIteration) { | |
1257 Universe::verify(); | |
1258 } | |
1259 | |
1260 // do the iteration | |
517
e9be0e04635a
6689653: JMapPerm fails with UseConcMarkSweepIncGC and compressed oops off
jmasa
parents:
356
diff
changeset
|
1261 // If this operation encounters a bad object when using CMS, |
e9be0e04635a
6689653: JMapPerm fails with UseConcMarkSweepIncGC and compressed oops off
jmasa
parents:
356
diff
changeset
|
1262 // consider using safe_object_iterate() which avoids perm gen |
e9be0e04635a
6689653: JMapPerm fails with UseConcMarkSweepIncGC and compressed oops off
jmasa
parents:
356
diff
changeset
|
1263 // objects that may contain bad references. |
0 | 1264 Universe::heap()->object_iterate(_blk); |
1265 | |
1266 // when sharing is enabled we must iterate over the shared spaces | |
1267 if (UseSharedSpaces) { | |
1268 GenCollectedHeap* gch = GenCollectedHeap::heap(); | |
1269 CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen(); | |
1270 gen->ro_space()->object_iterate(_blk); | |
1271 gen->rw_space()->object_iterate(_blk); | |
1272 } | |
1273 } | |
1274 | |
1275 }; | |
1276 | |
1277 | |
1278 // An ObjectClosure used to support the deprecated IterateOverHeap and | |
1279 // IterateOverInstancesOfClass functions | |
1280 class IterateOverHeapObjectClosure: public ObjectClosure { | |
1281 private: | |
1282 JvmtiTagMap* _tag_map; | |
1283 KlassHandle _klass; | |
1284 jvmtiHeapObjectFilter _object_filter; | |
1285 jvmtiHeapObjectCallback _heap_object_callback; | |
1286 const void* _user_data; | |
1287 | |
1288 // accessors | |
1289 JvmtiTagMap* tag_map() const { return _tag_map; } | |
1290 jvmtiHeapObjectFilter object_filter() const { return _object_filter; } | |
1291 jvmtiHeapObjectCallback object_callback() const { return _heap_object_callback; } | |
1292 KlassHandle klass() const { return _klass; } | |
1293 const void* user_data() const { return _user_data; } | |
1294 | |
1295 // indicates if iteration has been aborted | |
1296 bool _iteration_aborted; | |
1297 bool is_iteration_aborted() const { return _iteration_aborted; } | |
1298 void set_iteration_aborted(bool aborted) { _iteration_aborted = aborted; } | |
1299 | |
1300 public: | |
1301 IterateOverHeapObjectClosure(JvmtiTagMap* tag_map, | |
1302 KlassHandle klass, | |
1303 jvmtiHeapObjectFilter object_filter, | |
1304 jvmtiHeapObjectCallback heap_object_callback, | |
1305 const void* user_data) : | |
1306 _tag_map(tag_map), | |
1307 _klass(klass), | |
1308 _object_filter(object_filter), | |
1309 _heap_object_callback(heap_object_callback), | |
1310 _user_data(user_data), | |
1311 _iteration_aborted(false) | |
1312 { | |
1313 } | |
1314 | |
1315 void do_object(oop o); | |
1316 }; | |
1317 | |
1318 // invoked for each object in the heap | |
1319 void IterateOverHeapObjectClosure::do_object(oop o) { | |
1320 // check if iteration has been halted | |
1321 if (is_iteration_aborted()) return; | |
1322 | |
1323 // ignore any objects that aren't visible to profiler | |
1324 if (!ServiceUtil::visible_oop(o)) return; | |
1325 | |
1326 // instanceof check when filtering by klass | |
1327 if (!klass().is_null() && !o->is_a(klass()())) { | |
1328 return; | |
1329 } | |
1330 // prepare for the calllback | |
1331 CallbackWrapper wrapper(tag_map(), o); | |
1332 | |
1333 // if the object is tagged and we're only interested in untagged objects | |
1334 // then don't invoke the callback. Similiarly, if the object is untagged | |
1335 // and we're only interested in tagged objects we skip the callback. | |
1336 if (wrapper.obj_tag() != 0) { | |
1337 if (object_filter() == JVMTI_HEAP_OBJECT_UNTAGGED) return; | |
1338 } else { | |
1339 if (object_filter() == JVMTI_HEAP_OBJECT_TAGGED) return; | |
1340 } | |
1341 | |
1342 // invoke the agent's callback | |
1343 jvmtiIterationControl control = (*object_callback())(wrapper.klass_tag(), | |
1344 wrapper.obj_size(), | |
1345 wrapper.obj_tag_p(), | |
1346 (void*)user_data()); | |
1347 if (control == JVMTI_ITERATION_ABORT) { | |
1348 set_iteration_aborted(true); | |
1349 } | |
1350 } | |
1351 | |
1352 // An ObjectClosure used to support the IterateThroughHeap function | |
1353 class IterateThroughHeapObjectClosure: public ObjectClosure { | |
1354 private: | |
1355 JvmtiTagMap* _tag_map; | |
1356 KlassHandle _klass; | |
1357 int _heap_filter; | |
1358 const jvmtiHeapCallbacks* _callbacks; | |
1359 const void* _user_data; | |
1360 | |
1361 // accessor functions | |
1362 JvmtiTagMap* tag_map() const { return _tag_map; } | |
1363 int heap_filter() const { return _heap_filter; } | |
1364 const jvmtiHeapCallbacks* callbacks() const { return _callbacks; } | |
1365 KlassHandle klass() const { return _klass; } | |
1366 const void* user_data() const { return _user_data; } | |
1367 | |
1368 // indicates if the iteration has been aborted | |
1369 bool _iteration_aborted; | |
1370 bool is_iteration_aborted() const { return _iteration_aborted; } | |
1371 | |
1372 // used to check the visit control flags. If the abort flag is set | |
1373 // then we set the iteration aborted flag so that the iteration completes | |
1374 // without processing any further objects | |
1375 bool check_flags_for_abort(jint flags) { | |
1376 bool is_abort = (flags & JVMTI_VISIT_ABORT) != 0; | |
1377 if (is_abort) { | |
1378 _iteration_aborted = true; | |
1379 } | |
1380 return is_abort; | |
1381 } | |
1382 | |
1383 public: | |
1384 IterateThroughHeapObjectClosure(JvmtiTagMap* tag_map, | |
1385 KlassHandle klass, | |
1386 int heap_filter, | |
1387 const jvmtiHeapCallbacks* heap_callbacks, | |
1388 const void* user_data) : | |
1389 _tag_map(tag_map), | |
1390 _klass(klass), | |
1391 _heap_filter(heap_filter), | |
1392 _callbacks(heap_callbacks), | |
1393 _user_data(user_data), | |
1394 _iteration_aborted(false) | |
1395 { | |
1396 } | |
1397 | |
1398 void do_object(oop o); | |
1399 }; | |
1400 | |
1401 // invoked for each object in the heap | |
1402 void IterateThroughHeapObjectClosure::do_object(oop obj) { | |
1403 // check if iteration has been halted | |
1404 if (is_iteration_aborted()) return; | |
1405 | |
1406 // ignore any objects that aren't visible to profiler | |
1407 if (!ServiceUtil::visible_oop(obj)) return; | |
1408 | |
1409 // apply class filter | |
1410 if (is_filtered_by_klass_filter(obj, klass())) return; | |
1411 | |
1412 // prepare for callback | |
1413 CallbackWrapper wrapper(tag_map(), obj); | |
1414 | |
1415 // check if filtered by the heap filter | |
1416 if (is_filtered_by_heap_filter(wrapper.obj_tag(), wrapper.klass_tag(), heap_filter())) { | |
1417 return; | |
1418 } | |
1419 | |
1420 // for arrays we need the length, otherwise -1 | |
1421 bool is_array = obj->is_array(); | |
1422 int len = is_array ? arrayOop(obj)->length() : -1; | |
1423 | |
1424 // invoke the object callback (if callback is provided) | |
1425 if (callbacks()->heap_iteration_callback != NULL) { | |
1426 jvmtiHeapIterationCallback cb = callbacks()->heap_iteration_callback; | |
1427 jint res = (*cb)(wrapper.klass_tag(), | |
1428 wrapper.obj_size(), | |
1429 wrapper.obj_tag_p(), | |
1430 (jint)len, | |
1431 (void*)user_data()); | |
1432 if (check_flags_for_abort(res)) return; | |
1433 } | |
1434 | |
1435 // for objects and classes we report primitive fields if callback provided | |
1436 if (callbacks()->primitive_field_callback != NULL && obj->is_instance()) { | |
1437 jint res; | |
1438 jvmtiPrimitiveFieldCallback cb = callbacks()->primitive_field_callback; | |
1142 | 1439 if (obj->klass() == SystemDictionary::Class_klass()) { |
0 | 1440 res = invoke_primitive_field_callback_for_static_fields(&wrapper, |
1441 obj, | |
1442 cb, | |
1443 (void*)user_data()); | |
1444 } else { | |
1445 res = invoke_primitive_field_callback_for_instance_fields(&wrapper, | |
1446 obj, | |
1447 cb, | |
1448 (void*)user_data()); | |
1449 } | |
1450 if (check_flags_for_abort(res)) return; | |
1451 } | |
1452 | |
1453 // string callback | |
1454 if (!is_array && | |
1455 callbacks()->string_primitive_value_callback != NULL && | |
1142 | 1456 obj->klass() == SystemDictionary::String_klass()) { |
0 | 1457 jint res = invoke_string_value_callback( |
1458 callbacks()->string_primitive_value_callback, | |
1459 &wrapper, | |
1460 obj, | |
1461 (void*)user_data() ); | |
1462 if (check_flags_for_abort(res)) return; | |
1463 } | |
1464 | |
1465 // array callback | |
1466 if (is_array && | |
1467 callbacks()->array_primitive_value_callback != NULL && | |
1468 obj->is_typeArray()) { | |
1469 jint res = invoke_array_primitive_value_callback( | |
1470 callbacks()->array_primitive_value_callback, | |
1471 &wrapper, | |
1472 obj, | |
1473 (void*)user_data() ); | |
1474 if (check_flags_for_abort(res)) return; | |
1475 } | |
1476 }; | |
1477 | |
1478 | |
1479 // Deprecated function to iterate over all objects in the heap | |
1480 void JvmtiTagMap::iterate_over_heap(jvmtiHeapObjectFilter object_filter, | |
1481 KlassHandle klass, | |
1482 jvmtiHeapObjectCallback heap_object_callback, | |
1483 const void* user_data) | |
1484 { | |
1485 MutexLocker ml(Heap_lock); | |
1486 IterateOverHeapObjectClosure blk(this, | |
1487 klass, | |
1488 object_filter, | |
1489 heap_object_callback, | |
1490 user_data); | |
1491 VM_HeapIterateOperation op(&blk); | |
1492 VMThread::execute(&op); | |
1493 } | |
1494 | |
1495 | |
1496 // Iterates over all objects in the heap | |
1497 void JvmtiTagMap::iterate_through_heap(jint heap_filter, | |
1498 KlassHandle klass, | |
1499 const jvmtiHeapCallbacks* callbacks, | |
1500 const void* user_data) | |
1501 { | |
1502 MutexLocker ml(Heap_lock); | |
1503 IterateThroughHeapObjectClosure blk(this, | |
1504 klass, | |
1505 heap_filter, | |
1506 callbacks, | |
1507 user_data); | |
1508 VM_HeapIterateOperation op(&blk); | |
1509 VMThread::execute(&op); | |
1510 } | |
1511 | |
1512 // support class for get_objects_with_tags | |
1513 | |
1514 class TagObjectCollector : public JvmtiTagHashmapEntryClosure { | |
1515 private: | |
1516 JvmtiEnv* _env; | |
1517 jlong* _tags; | |
1518 jint _tag_count; | |
1519 | |
1520 GrowableArray<jobject>* _object_results; // collected objects (JNI weak refs) | |
1521 GrowableArray<uint64_t>* _tag_results; // collected tags | |
1522 | |
1523 public: | |
1524 TagObjectCollector(JvmtiEnv* env, const jlong* tags, jint tag_count) { | |
1525 _env = env; | |
1526 _tags = (jlong*)tags; | |
1527 _tag_count = tag_count; | |
1528 _object_results = new (ResourceObj::C_HEAP) GrowableArray<jobject>(1,true); | |
1529 _tag_results = new (ResourceObj::C_HEAP) GrowableArray<uint64_t>(1,true); | |
1530 } | |
1531 | |
1532 ~TagObjectCollector() { | |
1533 delete _object_results; | |
1534 delete _tag_results; | |
1535 } | |
1536 | |
1537 // for each tagged object check if the tag value matches | |
1538 // - if it matches then we create a JNI local reference to the object | |
1539 // and record the reference and tag value. | |
1540 // | |
1541 void do_entry(JvmtiTagHashmapEntry* entry) { | |
1542 for (int i=0; i<_tag_count; i++) { | |
1543 if (_tags[i] == entry->tag()) { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
1544 oop o = entry->object(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
1545 assert(o != NULL, "sanity check"); |
0 | 1546 |
1547 // the mirror is tagged | |
1548 if (o->is_klass()) { | |
1549 klassOop k = (klassOop)o; | |
1550 o = Klass::cast(k)->java_mirror(); | |
1551 } | |
1552 | |
1553 jobject ref = JNIHandles::make_local(JavaThread::current(), o); | |
1554 _object_results->append(ref); | |
1555 _tag_results->append((uint64_t)entry->tag()); | |
1556 } | |
1557 } | |
1558 } | |
1559 | |
1560 // return the results from the collection | |
1561 // | |
1562 jvmtiError result(jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) { | |
1563 jvmtiError error; | |
1564 int count = _object_results->length(); | |
1565 assert(count >= 0, "sanity check"); | |
1566 | |
1567 // if object_result_ptr is not NULL then allocate the result and copy | |
1568 // in the object references. | |
1569 if (object_result_ptr != NULL) { | |
1570 error = _env->Allocate(count * sizeof(jobject), (unsigned char**)object_result_ptr); | |
1571 if (error != JVMTI_ERROR_NONE) { | |
1572 return error; | |
1573 } | |
1574 for (int i=0; i<count; i++) { | |
1575 (*object_result_ptr)[i] = _object_results->at(i); | |
1576 } | |
1577 } | |
1578 | |
1579 // if tag_result_ptr is not NULL then allocate the result and copy | |
1580 // in the tag values. | |
1581 if (tag_result_ptr != NULL) { | |
1582 error = _env->Allocate(count * sizeof(jlong), (unsigned char**)tag_result_ptr); | |
1583 if (error != JVMTI_ERROR_NONE) { | |
1584 if (object_result_ptr != NULL) { | |
1585 _env->Deallocate((unsigned char*)object_result_ptr); | |
1586 } | |
1587 return error; | |
1588 } | |
1589 for (int i=0; i<count; i++) { | |
1590 (*tag_result_ptr)[i] = (jlong)_tag_results->at(i); | |
1591 } | |
1592 } | |
1593 | |
1594 *count_ptr = count; | |
1595 return JVMTI_ERROR_NONE; | |
1596 } | |
1597 }; | |
1598 | |
1599 // return the list of objects with the specified tags | |
1600 jvmtiError JvmtiTagMap::get_objects_with_tags(const jlong* tags, | |
1601 jint count, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) { | |
1602 | |
1603 TagObjectCollector collector(env(), tags, count); | |
1604 { | |
1605 // iterate over all tagged objects | |
1606 MutexLocker ml(lock()); | |
1607 entry_iterate(&collector); | |
1608 } | |
1609 return collector.result(count_ptr, object_result_ptr, tag_result_ptr); | |
1610 } | |
1611 | |
1612 | |
1613 // ObjectMarker is used to support the marking objects when walking the | |
1614 // heap. | |
1615 // | |
1616 // This implementation uses the existing mark bits in an object for | |
1617 // marking. Objects that are marked must later have their headers restored. | |
1618 // As most objects are unlocked and don't have their identity hash computed | |
1619 // we don't have to save their headers. Instead we save the headers that | |
1620 // are "interesting". Later when the headers are restored this implementation | |
1621 // restores all headers to their initial value and then restores the few | |
1622 // objects that had interesting headers. | |
1623 // | |
1624 // Future work: This implementation currently uses growable arrays to save | |
1625 // the oop and header of interesting objects. As an optimization we could | |
1626 // use the same technique as the GC and make use of the unused area | |
1627 // between top() and end(). | |
1628 // | |
1629 | |
1630 // An ObjectClosure used to restore the mark bits of an object | |
1631 class RestoreMarksClosure : public ObjectClosure { | |
1632 public: | |
1633 void do_object(oop o) { | |
1634 if (o != NULL) { | |
1635 markOop mark = o->mark(); | |
1636 if (mark->is_marked()) { | |
1637 o->init_mark(); | |
1638 } | |
1639 } | |
1640 } | |
1641 }; | |
1642 | |
1643 // ObjectMarker provides the mark and visited functions | |
1644 class ObjectMarker : AllStatic { | |
1645 private: | |
1646 // saved headers | |
1647 static GrowableArray<oop>* _saved_oop_stack; | |
1648 static GrowableArray<markOop>* _saved_mark_stack; | |
1649 | |
1650 public: | |
1651 static void init(); // initialize | |
1652 static void done(); // clean-up | |
1653 | |
1654 static inline void mark(oop o); // mark an object | |
1655 static inline bool visited(oop o); // check if object has been visited | |
1656 }; | |
1657 | |
1658 GrowableArray<oop>* ObjectMarker::_saved_oop_stack = NULL; | |
1659 GrowableArray<markOop>* ObjectMarker::_saved_mark_stack = NULL; | |
1660 | |
1661 // initialize ObjectMarker - prepares for object marking | |
1662 void ObjectMarker::init() { | |
1663 assert(Thread::current()->is_VM_thread(), "must be VMThread"); | |
1664 | |
1665 // prepare heap for iteration | |
1666 Universe::heap()->ensure_parsability(false); // no need to retire TLABs | |
1667 | |
1668 // create stacks for interesting headers | |
1669 _saved_mark_stack = new (ResourceObj::C_HEAP) GrowableArray<markOop>(4000, true); | |
1670 _saved_oop_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(4000, true); | |
1671 | |
1672 if (UseBiasedLocking) { | |
1673 BiasedLocking::preserve_marks(); | |
1674 } | |
1675 } | |
1676 | |
1677 // Object marking is done so restore object headers | |
1678 void ObjectMarker::done() { | |
1679 // iterate over all objects and restore the mark bits to | |
1680 // their initial value | |
1681 RestoreMarksClosure blk; | |
1682 Universe::heap()->object_iterate(&blk); | |
1683 | |
1684 // When sharing is enabled we need to restore the headers of the objects | |
1685 // in the readwrite space too. | |
1686 if (UseSharedSpaces) { | |
1687 GenCollectedHeap* gch = GenCollectedHeap::heap(); | |
1688 CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen(); | |
1689 gen->rw_space()->object_iterate(&blk); | |
1690 } | |
1691 | |
1692 // now restore the interesting headers | |
1693 for (int i = 0; i < _saved_oop_stack->length(); i++) { | |
1694 oop o = _saved_oop_stack->at(i); | |
1695 markOop mark = _saved_mark_stack->at(i); | |
1696 o->set_mark(mark); | |
1697 } | |
1698 | |
1699 if (UseBiasedLocking) { | |
1700 BiasedLocking::restore_marks(); | |
1701 } | |
1702 | |
1703 // free the stacks | |
1704 delete _saved_oop_stack; | |
1705 delete _saved_mark_stack; | |
1706 } | |
1707 | |
1708 // mark an object | |
1709 inline void ObjectMarker::mark(oop o) { | |
1710 assert(Universe::heap()->is_in(o), "sanity check"); | |
1711 assert(!o->mark()->is_marked(), "should only mark an object once"); | |
1712 | |
1713 // object's mark word | |
1714 markOop mark = o->mark(); | |
1715 | |
1716 if (mark->must_be_preserved(o)) { | |
1717 _saved_mark_stack->push(mark); | |
1718 _saved_oop_stack->push(o); | |
1719 } | |
1720 | |
1721 // mark the object | |
1722 o->set_mark(markOopDesc::prototype()->set_marked()); | |
1723 } | |
1724 | |
1725 // return true if object is marked | |
1726 inline bool ObjectMarker::visited(oop o) { | |
1727 return o->mark()->is_marked(); | |
1728 } | |
1729 | |
1730 // Stack allocated class to help ensure that ObjectMarker is used | |
1731 // correctly. Constructor initializes ObjectMarker, destructor calls | |
1732 // ObjectMarker's done() function to restore object headers. | |
1733 class ObjectMarkerController : public StackObj { | |
1734 public: | |
1735 ObjectMarkerController() { | |
1736 ObjectMarker::init(); | |
1737 } | |
1738 ~ObjectMarkerController() { | |
1739 ObjectMarker::done(); | |
1740 } | |
1741 }; | |
1742 | |
1743 | |
1744 // helper to map a jvmtiHeapReferenceKind to an old style jvmtiHeapRootKind | |
1745 // (not performance critical as only used for roots) | |
1746 static jvmtiHeapRootKind toJvmtiHeapRootKind(jvmtiHeapReferenceKind kind) { | |
1747 switch (kind) { | |
1748 case JVMTI_HEAP_REFERENCE_JNI_GLOBAL: return JVMTI_HEAP_ROOT_JNI_GLOBAL; | |
1749 case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS: return JVMTI_HEAP_ROOT_SYSTEM_CLASS; | |
1750 case JVMTI_HEAP_REFERENCE_MONITOR: return JVMTI_HEAP_ROOT_MONITOR; | |
1751 case JVMTI_HEAP_REFERENCE_STACK_LOCAL: return JVMTI_HEAP_ROOT_STACK_LOCAL; | |
1752 case JVMTI_HEAP_REFERENCE_JNI_LOCAL: return JVMTI_HEAP_ROOT_JNI_LOCAL; | |
1753 case JVMTI_HEAP_REFERENCE_THREAD: return JVMTI_HEAP_ROOT_THREAD; | |
1754 case JVMTI_HEAP_REFERENCE_OTHER: return JVMTI_HEAP_ROOT_OTHER; | |
1755 default: ShouldNotReachHere(); return JVMTI_HEAP_ROOT_OTHER; | |
1756 } | |
1757 } | |
1758 | |
1759 // Base class for all heap walk contexts. The base class maintains a flag | |
1760 // to indicate if the context is valid or not. | |
1761 class HeapWalkContext VALUE_OBJ_CLASS_SPEC { | |
1762 private: | |
1763 bool _valid; | |
1764 public: | |
1765 HeapWalkContext(bool valid) { _valid = valid; } | |
1766 void invalidate() { _valid = false; } | |
1767 bool is_valid() const { return _valid; } | |
1768 }; | |
1769 | |
1770 // A basic heap walk context for the deprecated heap walking functions. | |
1771 // The context for a basic heap walk are the callbacks and fields used by | |
1772 // the referrer caching scheme. | |
1773 class BasicHeapWalkContext: public HeapWalkContext { | |
1774 private: | |
1775 jvmtiHeapRootCallback _heap_root_callback; | |
1776 jvmtiStackReferenceCallback _stack_ref_callback; | |
1777 jvmtiObjectReferenceCallback _object_ref_callback; | |
1778 | |
1779 // used for caching | |
1780 oop _last_referrer; | |
1781 jlong _last_referrer_tag; | |
1782 | |
1783 public: | |
1784 BasicHeapWalkContext() : HeapWalkContext(false) { } | |
1785 | |
1786 BasicHeapWalkContext(jvmtiHeapRootCallback heap_root_callback, | |
1787 jvmtiStackReferenceCallback stack_ref_callback, | |
1788 jvmtiObjectReferenceCallback object_ref_callback) : | |
1789 HeapWalkContext(true), | |
1790 _heap_root_callback(heap_root_callback), | |
1791 _stack_ref_callback(stack_ref_callback), | |
1792 _object_ref_callback(object_ref_callback), | |
1793 _last_referrer(NULL), | |
1794 _last_referrer_tag(0) { | |
1795 } | |
1796 | |
1797 // accessors | |
1798 jvmtiHeapRootCallback heap_root_callback() const { return _heap_root_callback; } | |
1799 jvmtiStackReferenceCallback stack_ref_callback() const { return _stack_ref_callback; } | |
1800 jvmtiObjectReferenceCallback object_ref_callback() const { return _object_ref_callback; } | |
1801 | |
1802 oop last_referrer() const { return _last_referrer; } | |
1803 void set_last_referrer(oop referrer) { _last_referrer = referrer; } | |
1804 jlong last_referrer_tag() const { return _last_referrer_tag; } | |
1805 void set_last_referrer_tag(jlong value) { _last_referrer_tag = value; } | |
1806 }; | |
1807 | |
1808 // The advanced heap walk context for the FollowReferences functions. | |
1809 // The context is the callbacks, and the fields used for filtering. | |
1810 class AdvancedHeapWalkContext: public HeapWalkContext { | |
1811 private: | |
1812 jint _heap_filter; | |
1813 KlassHandle _klass_filter; | |
1814 const jvmtiHeapCallbacks* _heap_callbacks; | |
1815 | |
1816 public: | |
1817 AdvancedHeapWalkContext() : HeapWalkContext(false) { } | |
1818 | |
1819 AdvancedHeapWalkContext(jint heap_filter, | |
1820 KlassHandle klass_filter, | |
1821 const jvmtiHeapCallbacks* heap_callbacks) : | |
1822 HeapWalkContext(true), | |
1823 _heap_filter(heap_filter), | |
1824 _klass_filter(klass_filter), | |
1825 _heap_callbacks(heap_callbacks) { | |
1826 } | |
1827 | |
1828 // accessors | |
1829 jint heap_filter() const { return _heap_filter; } | |
1830 KlassHandle klass_filter() const { return _klass_filter; } | |
1831 | |
1832 const jvmtiHeapReferenceCallback heap_reference_callback() const { | |
1833 return _heap_callbacks->heap_reference_callback; | |
1834 }; | |
1835 const jvmtiPrimitiveFieldCallback primitive_field_callback() const { | |
1836 return _heap_callbacks->primitive_field_callback; | |
1837 } | |
1838 const jvmtiArrayPrimitiveValueCallback array_primitive_value_callback() const { | |
1839 return _heap_callbacks->array_primitive_value_callback; | |
1840 } | |
1841 const jvmtiStringPrimitiveValueCallback string_primitive_value_callback() const { | |
1842 return _heap_callbacks->string_primitive_value_callback; | |
1843 } | |
1844 }; | |
1845 | |
1846 // The CallbackInvoker is a class with static functions that the heap walk can call | |
1847 // into to invoke callbacks. It works in one of two modes. The "basic" mode is | |
1848 // used for the deprecated IterateOverReachableObjects functions. The "advanced" | |
1849 // mode is for the newer FollowReferences function which supports a lot of | |
1850 // additional callbacks. | |
1851 class CallbackInvoker : AllStatic { | |
1852 private: | |
1853 // heap walk styles | |
1854 enum { basic, advanced }; | |
1855 static int _heap_walk_type; | |
1856 static bool is_basic_heap_walk() { return _heap_walk_type == basic; } | |
1857 static bool is_advanced_heap_walk() { return _heap_walk_type == advanced; } | |
1858 | |
1859 // context for basic style heap walk | |
1860 static BasicHeapWalkContext _basic_context; | |
1861 static BasicHeapWalkContext* basic_context() { | |
1862 assert(_basic_context.is_valid(), "invalid"); | |
1863 return &_basic_context; | |
1864 } | |
1865 | |
1866 // context for advanced style heap walk | |
1867 static AdvancedHeapWalkContext _advanced_context; | |
1868 static AdvancedHeapWalkContext* advanced_context() { | |
1869 assert(_advanced_context.is_valid(), "invalid"); | |
1870 return &_advanced_context; | |
1871 } | |
1872 | |
1873 // context needed for all heap walks | |
1874 static JvmtiTagMap* _tag_map; | |
1875 static const void* _user_data; | |
1876 static GrowableArray<oop>* _visit_stack; | |
1877 | |
1878 // accessors | |
1879 static JvmtiTagMap* tag_map() { return _tag_map; } | |
1880 static const void* user_data() { return _user_data; } | |
1881 static GrowableArray<oop>* visit_stack() { return _visit_stack; } | |
1882 | |
1883 // if the object hasn't been visited then push it onto the visit stack | |
1884 // so that it will be visited later | |
1885 static inline bool check_for_visit(oop obj) { | |
1886 if (!ObjectMarker::visited(obj)) visit_stack()->push(obj); | |
1887 return true; | |
1888 } | |
1889 | |
1890 // invoke basic style callbacks | |
1891 static inline bool invoke_basic_heap_root_callback | |
1892 (jvmtiHeapRootKind root_kind, oop obj); | |
1893 static inline bool invoke_basic_stack_ref_callback | |
1894 (jvmtiHeapRootKind root_kind, jlong thread_tag, jint depth, jmethodID method, | |
1895 int slot, oop obj); | |
1896 static inline bool invoke_basic_object_reference_callback | |
1897 (jvmtiObjectReferenceKind ref_kind, oop referrer, oop referree, jint index); | |
1898 | |
1899 // invoke advanced style callbacks | |
1900 static inline bool invoke_advanced_heap_root_callback | |
1901 (jvmtiHeapReferenceKind ref_kind, oop obj); | |
1902 static inline bool invoke_advanced_stack_ref_callback | |
1903 (jvmtiHeapReferenceKind ref_kind, jlong thread_tag, jlong tid, int depth, | |
1904 jmethodID method, jlocation bci, jint slot, oop obj); | |
1905 static inline bool invoke_advanced_object_reference_callback | |
1906 (jvmtiHeapReferenceKind ref_kind, oop referrer, oop referree, jint index); | |
1907 | |
1908 // used to report the value of primitive fields | |
1909 static inline bool report_primitive_field | |
1910 (jvmtiHeapReferenceKind ref_kind, oop obj, jint index, address addr, char type); | |
1911 | |
1912 public: | |
1913 // initialize for basic mode | |
1914 static void initialize_for_basic_heap_walk(JvmtiTagMap* tag_map, | |
1915 GrowableArray<oop>* visit_stack, | |
1916 const void* user_data, | |
1917 BasicHeapWalkContext context); | |
1918 | |
1919 // initialize for advanced mode | |
1920 static void initialize_for_advanced_heap_walk(JvmtiTagMap* tag_map, | |
1921 GrowableArray<oop>* visit_stack, | |
1922 const void* user_data, | |
1923 AdvancedHeapWalkContext context); | |
1924 | |
1925 // functions to report roots | |
1926 static inline bool report_simple_root(jvmtiHeapReferenceKind kind, oop o); | |
1927 static inline bool report_jni_local_root(jlong thread_tag, jlong tid, jint depth, | |
1928 jmethodID m, oop o); | |
1929 static inline bool report_stack_ref_root(jlong thread_tag, jlong tid, jint depth, | |
1930 jmethodID method, jlocation bci, jint slot, oop o); | |
1931 | |
1932 // functions to report references | |
1933 static inline bool report_array_element_reference(oop referrer, oop referree, jint index); | |
1934 static inline bool report_class_reference(oop referrer, oop referree); | |
1935 static inline bool report_class_loader_reference(oop referrer, oop referree); | |
1936 static inline bool report_signers_reference(oop referrer, oop referree); | |
1937 static inline bool report_protection_domain_reference(oop referrer, oop referree); | |
1938 static inline bool report_superclass_reference(oop referrer, oop referree); | |
1939 static inline bool report_interface_reference(oop referrer, oop referree); | |
1940 static inline bool report_static_field_reference(oop referrer, oop referree, jint slot); | |
1941 static inline bool report_field_reference(oop referrer, oop referree, jint slot); | |
1942 static inline bool report_constant_pool_reference(oop referrer, oop referree, jint index); | |
1943 static inline bool report_primitive_array_values(oop array); | |
1944 static inline bool report_string_value(oop str); | |
1945 static inline bool report_primitive_instance_field(oop o, jint index, address value, char type); | |
1946 static inline bool report_primitive_static_field(oop o, jint index, address value, char type); | |
1947 }; | |
1948 | |
1949 // statics | |
1950 int CallbackInvoker::_heap_walk_type; | |
1951 BasicHeapWalkContext CallbackInvoker::_basic_context; | |
1952 AdvancedHeapWalkContext CallbackInvoker::_advanced_context; | |
1953 JvmtiTagMap* CallbackInvoker::_tag_map; | |
1954 const void* CallbackInvoker::_user_data; | |
1955 GrowableArray<oop>* CallbackInvoker::_visit_stack; | |
1956 | |
1957 // initialize for basic heap walk (IterateOverReachableObjects et al) | |
1958 void CallbackInvoker::initialize_for_basic_heap_walk(JvmtiTagMap* tag_map, | |
1959 GrowableArray<oop>* visit_stack, | |
1960 const void* user_data, | |
1961 BasicHeapWalkContext context) { | |
1962 _tag_map = tag_map; | |
1963 _visit_stack = visit_stack; | |
1964 _user_data = user_data; | |
1965 _basic_context = context; | |
1966 _advanced_context.invalidate(); // will trigger assertion if used | |
1967 _heap_walk_type = basic; | |
1968 } | |
1969 | |
1970 // initialize for advanced heap walk (FollowReferences) | |
1971 void CallbackInvoker::initialize_for_advanced_heap_walk(JvmtiTagMap* tag_map, | |
1972 GrowableArray<oop>* visit_stack, | |
1973 const void* user_data, | |
1974 AdvancedHeapWalkContext context) { | |
1975 _tag_map = tag_map; | |
1976 _visit_stack = visit_stack; | |
1977 _user_data = user_data; | |
1978 _advanced_context = context; | |
1979 _basic_context.invalidate(); // will trigger assertion if used | |
1980 _heap_walk_type = advanced; | |
1981 } | |
1982 | |
1983 | |
1984 // invoke basic style heap root callback | |
1985 inline bool CallbackInvoker::invoke_basic_heap_root_callback(jvmtiHeapRootKind root_kind, oop obj) { | |
1986 assert(ServiceUtil::visible_oop(obj), "checking"); | |
1987 | |
1988 // if we heap roots should be reported | |
1989 jvmtiHeapRootCallback cb = basic_context()->heap_root_callback(); | |
1990 if (cb == NULL) { | |
1991 return check_for_visit(obj); | |
1992 } | |
1993 | |
1994 CallbackWrapper wrapper(tag_map(), obj); | |
1995 jvmtiIterationControl control = (*cb)(root_kind, | |
1996 wrapper.klass_tag(), | |
1997 wrapper.obj_size(), | |
1998 wrapper.obj_tag_p(), | |
1999 (void*)user_data()); | |
2000 // push root to visit stack when following references | |
2001 if (control == JVMTI_ITERATION_CONTINUE && | |
2002 basic_context()->object_ref_callback() != NULL) { | |
2003 visit_stack()->push(obj); | |
2004 } | |
2005 return control != JVMTI_ITERATION_ABORT; | |
2006 } | |
2007 | |
2008 // invoke basic style stack ref callback | |
2009 inline bool CallbackInvoker::invoke_basic_stack_ref_callback(jvmtiHeapRootKind root_kind, | |
2010 jlong thread_tag, | |
2011 jint depth, | |
2012 jmethodID method, | |
2013 jint slot, | |
2014 oop obj) { | |
2015 assert(ServiceUtil::visible_oop(obj), "checking"); | |
2016 | |
2017 // if we stack refs should be reported | |
2018 jvmtiStackReferenceCallback cb = basic_context()->stack_ref_callback(); | |
2019 if (cb == NULL) { | |
2020 return check_for_visit(obj); | |
2021 } | |
2022 | |
2023 CallbackWrapper wrapper(tag_map(), obj); | |
2024 jvmtiIterationControl control = (*cb)(root_kind, | |
2025 wrapper.klass_tag(), | |
2026 wrapper.obj_size(), | |
2027 wrapper.obj_tag_p(), | |
2028 thread_tag, | |
2029 depth, | |
2030 method, | |
2031 slot, | |
2032 (void*)user_data()); | |
2033 // push root to visit stack when following references | |
2034 if (control == JVMTI_ITERATION_CONTINUE && | |
2035 basic_context()->object_ref_callback() != NULL) { | |
2036 visit_stack()->push(obj); | |
2037 } | |
2038 return control != JVMTI_ITERATION_ABORT; | |
2039 } | |
2040 | |
2041 // invoke basic style object reference callback | |
2042 inline bool CallbackInvoker::invoke_basic_object_reference_callback(jvmtiObjectReferenceKind ref_kind, | |
2043 oop referrer, | |
2044 oop referree, | |
2045 jint index) { | |
2046 | |
2047 assert(ServiceUtil::visible_oop(referrer), "checking"); | |
2048 assert(ServiceUtil::visible_oop(referree), "checking"); | |
2049 | |
2050 BasicHeapWalkContext* context = basic_context(); | |
2051 | |
2052 // callback requires the referrer's tag. If it's the same referrer | |
2053 // as the last call then we use the cached value. | |
2054 jlong referrer_tag; | |
2055 if (referrer == context->last_referrer()) { | |
2056 referrer_tag = context->last_referrer_tag(); | |
2057 } else { | |
2058 referrer_tag = tag_for(tag_map(), klassOop_if_java_lang_Class(referrer)); | |
2059 } | |
2060 | |
2061 // do the callback | |
2062 CallbackWrapper wrapper(tag_map(), referree); | |
2063 jvmtiObjectReferenceCallback cb = context->object_ref_callback(); | |
2064 jvmtiIterationControl control = (*cb)(ref_kind, | |
2065 wrapper.klass_tag(), | |
2066 wrapper.obj_size(), | |
2067 wrapper.obj_tag_p(), | |
2068 referrer_tag, | |
2069 index, | |
2070 (void*)user_data()); | |
2071 | |
2072 // record referrer and referrer tag. For self-references record the | |
2073 // tag value from the callback as this might differ from referrer_tag. | |
2074 context->set_last_referrer(referrer); | |
2075 if (referrer == referree) { | |
2076 context->set_last_referrer_tag(*wrapper.obj_tag_p()); | |
2077 } else { | |
2078 context->set_last_referrer_tag(referrer_tag); | |
2079 } | |
2080 | |
2081 if (control == JVMTI_ITERATION_CONTINUE) { | |
2082 return check_for_visit(referree); | |
2083 } else { | |
2084 return control != JVMTI_ITERATION_ABORT; | |
2085 } | |
2086 } | |
2087 | |
2088 // invoke advanced style heap root callback | |
2089 inline bool CallbackInvoker::invoke_advanced_heap_root_callback(jvmtiHeapReferenceKind ref_kind, | |
2090 oop obj) { | |
2091 assert(ServiceUtil::visible_oop(obj), "checking"); | |
2092 | |
2093 AdvancedHeapWalkContext* context = advanced_context(); | |
2094 | |
2095 // check that callback is provided | |
2096 jvmtiHeapReferenceCallback cb = context->heap_reference_callback(); | |
2097 if (cb == NULL) { | |
2098 return check_for_visit(obj); | |
2099 } | |
2100 | |
2101 // apply class filter | |
2102 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { | |
2103 return check_for_visit(obj); | |
2104 } | |
2105 | |
2106 // setup the callback wrapper | |
2107 CallbackWrapper wrapper(tag_map(), obj); | |
2108 | |
2109 // apply tag filter | |
2110 if (is_filtered_by_heap_filter(wrapper.obj_tag(), | |
2111 wrapper.klass_tag(), | |
2112 context->heap_filter())) { | |
2113 return check_for_visit(obj); | |
2114 } | |
2115 | |
2116 // for arrays we need the length, otherwise -1 | |
2117 jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1); | |
2118 | |
2119 // invoke the callback | |
2120 jint res = (*cb)(ref_kind, | |
2121 NULL, // referrer info | |
2122 wrapper.klass_tag(), | |
2123 0, // referrer_class_tag is 0 for heap root | |
2124 wrapper.obj_size(), | |
2125 wrapper.obj_tag_p(), | |
2126 NULL, // referrer_tag_p | |
2127 len, | |
2128 (void*)user_data()); | |
2129 if (res & JVMTI_VISIT_ABORT) { | |
2130 return false;// referrer class tag | |
2131 } | |
2132 if (res & JVMTI_VISIT_OBJECTS) { | |
2133 check_for_visit(obj); | |
2134 } | |
2135 return true; | |
2136 } | |
2137 | |
2138 // report a reference from a thread stack to an object | |
2139 inline bool CallbackInvoker::invoke_advanced_stack_ref_callback(jvmtiHeapReferenceKind ref_kind, | |
2140 jlong thread_tag, | |
2141 jlong tid, | |
2142 int depth, | |
2143 jmethodID method, | |
2144 jlocation bci, | |
2145 jint slot, | |
2146 oop obj) { | |
2147 assert(ServiceUtil::visible_oop(obj), "checking"); | |
2148 | |
2149 AdvancedHeapWalkContext* context = advanced_context(); | |
2150 | |
2151 // check that callback is provider | |
2152 jvmtiHeapReferenceCallback cb = context->heap_reference_callback(); | |
2153 if (cb == NULL) { | |
2154 return check_for_visit(obj); | |
2155 } | |
2156 | |
2157 // apply class filter | |
2158 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { | |
2159 return check_for_visit(obj); | |
2160 } | |
2161 | |
2162 // setup the callback wrapper | |
2163 CallbackWrapper wrapper(tag_map(), obj); | |
2164 | |
2165 // apply tag filter | |
2166 if (is_filtered_by_heap_filter(wrapper.obj_tag(), | |
2167 wrapper.klass_tag(), | |
2168 context->heap_filter())) { | |
2169 return check_for_visit(obj); | |
2170 } | |
2171 | |
2172 // setup the referrer info | |
2173 jvmtiHeapReferenceInfo reference_info; | |
2174 reference_info.stack_local.thread_tag = thread_tag; | |
2175 reference_info.stack_local.thread_id = tid; | |
2176 reference_info.stack_local.depth = depth; | |
2177 reference_info.stack_local.method = method; | |
2178 reference_info.stack_local.location = bci; | |
2179 reference_info.stack_local.slot = slot; | |
2180 | |
2181 // for arrays we need the length, otherwise -1 | |
2182 jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1); | |
2183 | |
2184 // call into the agent | |
2185 int res = (*cb)(ref_kind, | |
2186 &reference_info, | |
2187 wrapper.klass_tag(), | |
2188 0, // referrer_class_tag is 0 for heap root (stack) | |
2189 wrapper.obj_size(), | |
2190 wrapper.obj_tag_p(), | |
2191 NULL, // referrer_tag is 0 for root | |
2192 len, | |
2193 (void*)user_data()); | |
2194 | |
2195 if (res & JVMTI_VISIT_ABORT) { | |
2196 return false; | |
2197 } | |
2198 if (res & JVMTI_VISIT_OBJECTS) { | |
2199 check_for_visit(obj); | |
2200 } | |
2201 return true; | |
2202 } | |
2203 | |
2204 // This mask is used to pass reference_info to a jvmtiHeapReferenceCallback | |
2205 // only for ref_kinds defined by the JVM TI spec. Otherwise, NULL is passed. | |
2206 #define REF_INFO_MASK ((1 << JVMTI_HEAP_REFERENCE_FIELD) \ | |
2207 | (1 << JVMTI_HEAP_REFERENCE_STATIC_FIELD) \ | |
2208 | (1 << JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT) \ | |
2209 | (1 << JVMTI_HEAP_REFERENCE_CONSTANT_POOL) \ | |
2210 | (1 << JVMTI_HEAP_REFERENCE_STACK_LOCAL) \ | |
2211 | (1 << JVMTI_HEAP_REFERENCE_JNI_LOCAL)) | |
2212 | |
2213 // invoke the object reference callback to report a reference | |
2214 inline bool CallbackInvoker::invoke_advanced_object_reference_callback(jvmtiHeapReferenceKind ref_kind, | |
2215 oop referrer, | |
2216 oop obj, | |
2217 jint index) | |
2218 { | |
2219 // field index is only valid field in reference_info | |
2220 static jvmtiHeapReferenceInfo reference_info = { 0 }; | |
2221 | |
2222 assert(ServiceUtil::visible_oop(referrer), "checking"); | |
2223 assert(ServiceUtil::visible_oop(obj), "checking"); | |
2224 | |
2225 AdvancedHeapWalkContext* context = advanced_context(); | |
2226 | |
2227 // check that callback is provider | |
2228 jvmtiHeapReferenceCallback cb = context->heap_reference_callback(); | |
2229 if (cb == NULL) { | |
2230 return check_for_visit(obj); | |
2231 } | |
2232 | |
2233 // apply class filter | |
2234 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { | |
2235 return check_for_visit(obj); | |
2236 } | |
2237 | |
2238 // setup the callback wrapper | |
2239 TwoOopCallbackWrapper wrapper(tag_map(), referrer, obj); | |
2240 | |
2241 // apply tag filter | |
2242 if (is_filtered_by_heap_filter(wrapper.obj_tag(), | |
2243 wrapper.klass_tag(), | |
2244 context->heap_filter())) { | |
2245 return check_for_visit(obj); | |
2246 } | |
2247 | |
2248 // field index is only valid field in reference_info | |
2249 reference_info.field.index = index; | |
2250 | |
2251 // for arrays we need the length, otherwise -1 | |
2252 jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1); | |
2253 | |
2254 // invoke the callback | |
2255 int res = (*cb)(ref_kind, | |
2256 (REF_INFO_MASK & (1 << ref_kind)) ? &reference_info : NULL, | |
2257 wrapper.klass_tag(), | |
2258 wrapper.referrer_klass_tag(), | |
2259 wrapper.obj_size(), | |
2260 wrapper.obj_tag_p(), | |
2261 wrapper.referrer_tag_p(), | |
2262 len, | |
2263 (void*)user_data()); | |
2264 | |
2265 if (res & JVMTI_VISIT_ABORT) { | |
2266 return false; | |
2267 } | |
2268 if (res & JVMTI_VISIT_OBJECTS) { | |
2269 check_for_visit(obj); | |
2270 } | |
2271 return true; | |
2272 } | |
2273 | |
2274 // report a "simple root" | |
2275 inline bool CallbackInvoker::report_simple_root(jvmtiHeapReferenceKind kind, oop obj) { | |
2276 assert(kind != JVMTI_HEAP_REFERENCE_STACK_LOCAL && | |
2277 kind != JVMTI_HEAP_REFERENCE_JNI_LOCAL, "not a simple root"); | |
2278 assert(ServiceUtil::visible_oop(obj), "checking"); | |
2279 | |
2280 if (is_basic_heap_walk()) { | |
2281 // map to old style root kind | |
2282 jvmtiHeapRootKind root_kind = toJvmtiHeapRootKind(kind); | |
2283 return invoke_basic_heap_root_callback(root_kind, obj); | |
2284 } else { | |
2285 assert(is_advanced_heap_walk(), "wrong heap walk type"); | |
2286 return invoke_advanced_heap_root_callback(kind, obj); | |
2287 } | |
2288 } | |
2289 | |
2290 | |
2291 // invoke the primitive array values | |
2292 inline bool CallbackInvoker::report_primitive_array_values(oop obj) { | |
2293 assert(obj->is_typeArray(), "not a primitive array"); | |
2294 | |
2295 AdvancedHeapWalkContext* context = advanced_context(); | |
2296 assert(context->array_primitive_value_callback() != NULL, "no callback"); | |
2297 | |
2298 // apply class filter | |
2299 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { | |
2300 return true; | |
2301 } | |
2302 | |
2303 CallbackWrapper wrapper(tag_map(), obj); | |
2304 | |
2305 // apply tag filter | |
2306 if (is_filtered_by_heap_filter(wrapper.obj_tag(), | |
2307 wrapper.klass_tag(), | |
2308 context->heap_filter())) { | |
2309 return true; | |
2310 } | |
2311 | |
2312 // invoke the callback | |
2313 int res = invoke_array_primitive_value_callback(context->array_primitive_value_callback(), | |
2314 &wrapper, | |
2315 obj, | |
2316 (void*)user_data()); | |
2317 return (!(res & JVMTI_VISIT_ABORT)); | |
2318 } | |
2319 | |
2320 // invoke the string value callback | |
2321 inline bool CallbackInvoker::report_string_value(oop str) { | |
1142 | 2322 assert(str->klass() == SystemDictionary::String_klass(), "not a string"); |
0 | 2323 |
2324 AdvancedHeapWalkContext* context = advanced_context(); | |
2325 assert(context->string_primitive_value_callback() != NULL, "no callback"); | |
2326 | |
2327 // apply class filter | |
2328 if (is_filtered_by_klass_filter(str, context->klass_filter())) { | |
2329 return true; | |
2330 } | |
2331 | |
2332 CallbackWrapper wrapper(tag_map(), str); | |
2333 | |
2334 // apply tag filter | |
2335 if (is_filtered_by_heap_filter(wrapper.obj_tag(), | |
2336 wrapper.klass_tag(), | |
2337 context->heap_filter())) { | |
2338 return true; | |
2339 } | |
2340 | |
2341 // invoke the callback | |
2342 int res = invoke_string_value_callback(context->string_primitive_value_callback(), | |
2343 &wrapper, | |
2344 str, | |
2345 (void*)user_data()); | |
2346 return (!(res & JVMTI_VISIT_ABORT)); | |
2347 } | |
2348 | |
2349 // invoke the primitive field callback | |
2350 inline bool CallbackInvoker::report_primitive_field(jvmtiHeapReferenceKind ref_kind, | |
2351 oop obj, | |
2352 jint index, | |
2353 address addr, | |
2354 char type) | |
2355 { | |
2356 // for primitive fields only the index will be set | |
2357 static jvmtiHeapReferenceInfo reference_info = { 0 }; | |
2358 | |
2359 AdvancedHeapWalkContext* context = advanced_context(); | |
2360 assert(context->primitive_field_callback() != NULL, "no callback"); | |
2361 | |
2362 // apply class filter | |
2363 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { | |
2364 return true; | |
2365 } | |
2366 | |
2367 CallbackWrapper wrapper(tag_map(), obj); | |
2368 | |
2369 // apply tag filter | |
2370 if (is_filtered_by_heap_filter(wrapper.obj_tag(), | |
2371 wrapper.klass_tag(), | |
2372 context->heap_filter())) { | |
2373 return true; | |
2374 } | |
2375 | |
2376 // the field index in the referrer | |
2377 reference_info.field.index = index; | |
2378 | |
2379 // map the type | |
2380 jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type; | |
2381 | |
2382 // setup the jvalue | |
2383 jvalue value; | |
2384 copy_to_jvalue(&value, addr, value_type); | |
2385 | |
2386 jvmtiPrimitiveFieldCallback cb = context->primitive_field_callback(); | |
2387 int res = (*cb)(ref_kind, | |
2388 &reference_info, | |
2389 wrapper.klass_tag(), | |
2390 wrapper.obj_tag_p(), | |
2391 value, | |
2392 value_type, | |
2393 (void*)user_data()); | |
2394 return (!(res & JVMTI_VISIT_ABORT)); | |
2395 } | |
2396 | |
2397 | |
2398 // instance field | |
2399 inline bool CallbackInvoker::report_primitive_instance_field(oop obj, | |
2400 jint index, | |
2401 address value, | |
2402 char type) { | |
2403 return report_primitive_field(JVMTI_HEAP_REFERENCE_FIELD, | |
2404 obj, | |
2405 index, | |
2406 value, | |
2407 type); | |
2408 } | |
2409 | |
2410 // static field | |
2411 inline bool CallbackInvoker::report_primitive_static_field(oop obj, | |
2412 jint index, | |
2413 address value, | |
2414 char type) { | |
2415 return report_primitive_field(JVMTI_HEAP_REFERENCE_STATIC_FIELD, | |
2416 obj, | |
2417 index, | |
2418 value, | |
2419 type); | |
2420 } | |
2421 | |
2422 // report a JNI local (root object) to the profiler | |
2423 inline bool CallbackInvoker::report_jni_local_root(jlong thread_tag, jlong tid, jint depth, jmethodID m, oop obj) { | |
2424 if (is_basic_heap_walk()) { | |
2425 return invoke_basic_stack_ref_callback(JVMTI_HEAP_ROOT_JNI_LOCAL, | |
2426 thread_tag, | |
2427 depth, | |
2428 m, | |
2429 -1, | |
2430 obj); | |
2431 } else { | |
2432 return invoke_advanced_stack_ref_callback(JVMTI_HEAP_REFERENCE_JNI_LOCAL, | |
2433 thread_tag, tid, | |
2434 depth, | |
2435 m, | |
2436 (jlocation)-1, | |
2437 -1, | |
2438 obj); | |
2439 } | |
2440 } | |
2441 | |
2442 | |
2443 // report a local (stack reference, root object) | |
2444 inline bool CallbackInvoker::report_stack_ref_root(jlong thread_tag, | |
2445 jlong tid, | |
2446 jint depth, | |
2447 jmethodID method, | |
2448 jlocation bci, | |
2449 jint slot, | |
2450 oop obj) { | |
2451 if (is_basic_heap_walk()) { | |
2452 return invoke_basic_stack_ref_callback(JVMTI_HEAP_ROOT_STACK_LOCAL, | |
2453 thread_tag, | |
2454 depth, | |
2455 method, | |
2456 slot, | |
2457 obj); | |
2458 } else { | |
2459 return invoke_advanced_stack_ref_callback(JVMTI_HEAP_REFERENCE_STACK_LOCAL, | |
2460 thread_tag, | |
2461 tid, | |
2462 depth, | |
2463 method, | |
2464 bci, | |
2465 slot, | |
2466 obj); | |
2467 } | |
2468 } | |
2469 | |
2470 // report an object referencing a class. | |
2471 inline bool CallbackInvoker::report_class_reference(oop referrer, oop referree) { | |
2472 if (is_basic_heap_walk()) { | |
2473 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CLASS, referrer, referree, -1); | |
2474 } else { | |
2475 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_CLASS, referrer, referree, -1); | |
2476 } | |
2477 } | |
2478 | |
2479 // report a class referencing its class loader. | |
2480 inline bool CallbackInvoker::report_class_loader_reference(oop referrer, oop referree) { | |
2481 if (is_basic_heap_walk()) { | |
2482 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CLASS_LOADER, referrer, referree, -1); | |
2483 } else { | |
2484 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_CLASS_LOADER, referrer, referree, -1); | |
2485 } | |
2486 } | |
2487 | |
2488 // report a class referencing its signers. | |
2489 inline bool CallbackInvoker::report_signers_reference(oop referrer, oop referree) { | |
2490 if (is_basic_heap_walk()) { | |
2491 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_SIGNERS, referrer, referree, -1); | |
2492 } else { | |
2493 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_SIGNERS, referrer, referree, -1); | |
2494 } | |
2495 } | |
2496 | |
2497 // report a class referencing its protection domain.. | |
2498 inline bool CallbackInvoker::report_protection_domain_reference(oop referrer, oop referree) { | |
2499 if (is_basic_heap_walk()) { | |
2500 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_PROTECTION_DOMAIN, referrer, referree, -1); | |
2501 } else { | |
2502 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN, referrer, referree, -1); | |
2503 } | |
2504 } | |
2505 | |
2506 // report a class referencing its superclass. | |
2507 inline bool CallbackInvoker::report_superclass_reference(oop referrer, oop referree) { | |
2508 if (is_basic_heap_walk()) { | |
2509 // Send this to be consistent with past implementation | |
2510 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CLASS, referrer, referree, -1); | |
2511 } else { | |
2512 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_SUPERCLASS, referrer, referree, -1); | |
2513 } | |
2514 } | |
2515 | |
2516 // report a class referencing one of its interfaces. | |
2517 inline bool CallbackInvoker::report_interface_reference(oop referrer, oop referree) { | |
2518 if (is_basic_heap_walk()) { | |
2519 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_INTERFACE, referrer, referree, -1); | |
2520 } else { | |
2521 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_INTERFACE, referrer, referree, -1); | |
2522 } | |
2523 } | |
2524 | |
2525 // report a class referencing one of its static fields. | |
2526 inline bool CallbackInvoker::report_static_field_reference(oop referrer, oop referree, jint slot) { | |
2527 if (is_basic_heap_walk()) { | |
2528 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_STATIC_FIELD, referrer, referree, slot); | |
2529 } else { | |
2530 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_STATIC_FIELD, referrer, referree, slot); | |
2531 } | |
2532 } | |
2533 | |
2534 // report an array referencing an element object | |
2535 inline bool CallbackInvoker::report_array_element_reference(oop referrer, oop referree, jint index) { | |
2536 if (is_basic_heap_walk()) { | |
2537 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_ARRAY_ELEMENT, referrer, referree, index); | |
2538 } else { | |
2539 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT, referrer, referree, index); | |
2540 } | |
2541 } | |
2542 | |
2543 // report an object referencing an instance field object | |
2544 inline bool CallbackInvoker::report_field_reference(oop referrer, oop referree, jint slot) { | |
2545 if (is_basic_heap_walk()) { | |
2546 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_FIELD, referrer, referree, slot); | |
2547 } else { | |
2548 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_FIELD, referrer, referree, slot); | |
2549 } | |
2550 } | |
2551 | |
2552 // report an array referencing an element object | |
2553 inline bool CallbackInvoker::report_constant_pool_reference(oop referrer, oop referree, jint index) { | |
2554 if (is_basic_heap_walk()) { | |
2555 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CONSTANT_POOL, referrer, referree, index); | |
2556 } else { | |
2557 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_CONSTANT_POOL, referrer, referree, index); | |
2558 } | |
2559 } | |
2560 | |
2561 // A supporting closure used to process simple roots | |
2562 class SimpleRootsClosure : public OopClosure { | |
2563 private: | |
2564 jvmtiHeapReferenceKind _kind; | |
2565 bool _continue; | |
2566 | |
2567 jvmtiHeapReferenceKind root_kind() { return _kind; } | |
2568 | |
2569 public: | |
2570 void set_kind(jvmtiHeapReferenceKind kind) { | |
2571 _kind = kind; | |
2572 _continue = true; | |
2573 } | |
2574 | |
2575 inline bool stopped() { | |
2576 return !_continue; | |
2577 } | |
2578 | |
2579 void do_oop(oop* obj_p) { | |
2580 // iteration has terminated | |
2581 if (stopped()) { | |
2582 return; | |
2583 } | |
2584 | |
2585 // ignore null or deleted handles | |
2586 oop o = *obj_p; | |
2587 if (o == NULL || o == JNIHandles::deleted_handle()) { | |
2588 return; | |
2589 } | |
2590 | |
2591 jvmtiHeapReferenceKind kind = root_kind(); | |
2592 | |
2593 // many roots are Klasses so we use the java mirror | |
2594 if (o->is_klass()) { | |
2595 klassOop k = (klassOop)o; | |
2596 o = Klass::cast(k)->java_mirror(); | |
2597 } else { | |
2598 | |
2599 // SystemDictionary::always_strong_oops_do reports the application | |
2600 // class loader as a root. We want this root to be reported as | |
2601 // a root kind of "OTHER" rather than "SYSTEM_CLASS". | |
2602 if (o->is_instance() && root_kind() == JVMTI_HEAP_REFERENCE_SYSTEM_CLASS) { | |
2603 kind = JVMTI_HEAP_REFERENCE_OTHER; | |
2604 } | |
2605 } | |
2606 | |
2607 // some objects are ignored - in the case of simple | |
2177
3582bf76420e
6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents:
2145
diff
changeset
|
2608 // roots it's mostly Symbol*s that we are skipping |
0 | 2609 // here. |
2610 if (!ServiceUtil::visible_oop(o)) { | |
2611 return; | |
2612 } | |
2613 | |
2614 // invoke the callback | |
2615 _continue = CallbackInvoker::report_simple_root(kind, o); | |
2616 | |
2617 } | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2618 virtual void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); } |
0 | 2619 }; |
2620 | |
2621 // A supporting closure used to process JNI locals | |
2622 class JNILocalRootsClosure : public OopClosure { | |
2623 private: | |
2624 jlong _thread_tag; | |
2625 jlong _tid; | |
2626 jint _depth; | |
2627 jmethodID _method; | |
2628 bool _continue; | |
2629 public: | |
2630 void set_context(jlong thread_tag, jlong tid, jint depth, jmethodID method) { | |
2631 _thread_tag = thread_tag; | |
2632 _tid = tid; | |
2633 _depth = depth; | |
2634 _method = method; | |
2635 _continue = true; | |
2636 } | |
2637 | |
2638 inline bool stopped() { | |
2639 return !_continue; | |
2640 } | |
2641 | |
2642 void do_oop(oop* obj_p) { | |
2643 // iteration has terminated | |
2644 if (stopped()) { | |
2645 return; | |
2646 } | |
2647 | |
2648 // ignore null or deleted handles | |
2649 oop o = *obj_p; | |
2650 if (o == NULL || o == JNIHandles::deleted_handle()) { | |
2651 return; | |
2652 } | |
2653 | |
2654 if (!ServiceUtil::visible_oop(o)) { | |
2655 return; | |
2656 } | |
2657 | |
2658 // invoke the callback | |
2659 _continue = CallbackInvoker::report_jni_local_root(_thread_tag, _tid, _depth, _method, o); | |
2660 } | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2661 virtual void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); } |
0 | 2662 }; |
2663 | |
2664 | |
2665 // A VM operation to iterate over objects that are reachable from | |
2666 // a set of roots or an initial object. | |
2667 // | |
2668 // For VM_HeapWalkOperation the set of roots used is :- | |
2669 // | |
2670 // - All JNI global references | |
2671 // - All inflated monitors | |
2672 // - All classes loaded by the boot class loader (or all classes | |
2673 // in the event that class unloading is disabled) | |
2674 // - All java threads | |
2675 // - For each java thread then all locals and JNI local references | |
2676 // on the thread's execution stack | |
2677 // - All visible/explainable objects from Universes::oops_do | |
2678 // | |
2679 class VM_HeapWalkOperation: public VM_Operation { | |
2680 private: | |
2681 enum { | |
2682 initial_visit_stack_size = 4000 | |
2683 }; | |
2684 | |
2685 bool _is_advanced_heap_walk; // indicates FollowReferences | |
2686 JvmtiTagMap* _tag_map; | |
2687 Handle _initial_object; | |
2688 GrowableArray<oop>* _visit_stack; // the visit stack | |
2689 | |
2690 bool _collecting_heap_roots; // are we collecting roots | |
2691 bool _following_object_refs; // are we following object references | |
2692 | |
2693 bool _reporting_primitive_fields; // optional reporting | |
2694 bool _reporting_primitive_array_values; | |
2695 bool _reporting_string_values; | |
2696 | |
2697 GrowableArray<oop>* create_visit_stack() { | |
2698 return new (ResourceObj::C_HEAP) GrowableArray<oop>(initial_visit_stack_size, true); | |
2699 } | |
2700 | |
2701 // accessors | |
2702 bool is_advanced_heap_walk() const { return _is_advanced_heap_walk; } | |
2703 JvmtiTagMap* tag_map() const { return _tag_map; } | |
2704 Handle initial_object() const { return _initial_object; } | |
2705 | |
2706 bool is_following_references() const { return _following_object_refs; } | |
2707 | |
2708 bool is_reporting_primitive_fields() const { return _reporting_primitive_fields; } | |
2709 bool is_reporting_primitive_array_values() const { return _reporting_primitive_array_values; } | |
2710 bool is_reporting_string_values() const { return _reporting_string_values; } | |
2711 | |
2712 GrowableArray<oop>* visit_stack() const { return _visit_stack; } | |
2713 | |
2714 // iterate over the various object types | |
2715 inline bool iterate_over_array(oop o); | |
2716 inline bool iterate_over_type_array(oop o); | |
2717 inline bool iterate_over_class(klassOop o); | |
2718 inline bool iterate_over_object(oop o); | |
2719 | |
2720 // root collection | |
2721 inline bool collect_simple_roots(); | |
2722 inline bool collect_stack_roots(); | |
2723 inline bool collect_stack_roots(JavaThread* java_thread, JNILocalRootsClosure* blk); | |
2724 | |
2725 // visit an object | |
2726 inline bool visit(oop o); | |
2727 | |
2728 public: | |
2729 VM_HeapWalkOperation(JvmtiTagMap* tag_map, | |
2730 Handle initial_object, | |
2731 BasicHeapWalkContext callbacks, | |
2732 const void* user_data); | |
2733 | |
2734 VM_HeapWalkOperation(JvmtiTagMap* tag_map, | |
2735 Handle initial_object, | |
2736 AdvancedHeapWalkContext callbacks, | |
2737 const void* user_data); | |
2738 | |
2739 ~VM_HeapWalkOperation(); | |
2740 | |
2741 VMOp_Type type() const { return VMOp_HeapWalkOperation; } | |
2742 void doit(); | |
2743 }; | |
2744 | |
2745 | |
2746 VM_HeapWalkOperation::VM_HeapWalkOperation(JvmtiTagMap* tag_map, | |
2747 Handle initial_object, | |
2748 BasicHeapWalkContext callbacks, | |
2749 const void* user_data) { | |
2750 _is_advanced_heap_walk = false; | |
2751 _tag_map = tag_map; | |
2752 _initial_object = initial_object; | |
2753 _following_object_refs = (callbacks.object_ref_callback() != NULL); | |
2754 _reporting_primitive_fields = false; | |
2755 _reporting_primitive_array_values = false; | |
2756 _reporting_string_values = false; | |
2757 _visit_stack = create_visit_stack(); | |
2758 | |
2759 | |
2760 CallbackInvoker::initialize_for_basic_heap_walk(tag_map, _visit_stack, user_data, callbacks); | |
2761 } | |
2762 | |
2763 VM_HeapWalkOperation::VM_HeapWalkOperation(JvmtiTagMap* tag_map, | |
2764 Handle initial_object, | |
2765 AdvancedHeapWalkContext callbacks, | |
2766 const void* user_data) { | |
2767 _is_advanced_heap_walk = true; | |
2768 _tag_map = tag_map; | |
2769 _initial_object = initial_object; | |
2770 _following_object_refs = true; | |
2771 _reporting_primitive_fields = (callbacks.primitive_field_callback() != NULL);; | |
2772 _reporting_primitive_array_values = (callbacks.array_primitive_value_callback() != NULL);; | |
2773 _reporting_string_values = (callbacks.string_primitive_value_callback() != NULL);; | |
2774 _visit_stack = create_visit_stack(); | |
2775 | |
2776 CallbackInvoker::initialize_for_advanced_heap_walk(tag_map, _visit_stack, user_data, callbacks); | |
2777 } | |
2778 | |
2779 VM_HeapWalkOperation::~VM_HeapWalkOperation() { | |
2780 if (_following_object_refs) { | |
2781 assert(_visit_stack != NULL, "checking"); | |
2782 delete _visit_stack; | |
2783 _visit_stack = NULL; | |
2784 } | |
2785 } | |
2786 | |
2787 // an array references its class and has a reference to | |
2788 // each element in the array | |
2789 inline bool VM_HeapWalkOperation::iterate_over_array(oop o) { | |
2790 objArrayOop array = objArrayOop(o); | |
2791 if (array->klass() == Universe::systemObjArrayKlassObj()) { | |
2792 // filtered out | |
2793 return true; | |
2794 } | |
2795 | |
2796 // array reference to its class | |
2797 oop mirror = objArrayKlass::cast(array->klass())->java_mirror(); | |
2798 if (!CallbackInvoker::report_class_reference(o, mirror)) { | |
2799 return false; | |
2800 } | |
2801 | |
2802 // iterate over the array and report each reference to a | |
2803 // non-null element | |
2804 for (int index=0; index<array->length(); index++) { | |
2805 oop elem = array->obj_at(index); | |
2806 if (elem == NULL) { | |
2807 continue; | |
2808 } | |
2809 | |
2810 // report the array reference o[index] = elem | |
2811 if (!CallbackInvoker::report_array_element_reference(o, elem, index)) { | |
2812 return false; | |
2813 } | |
2814 } | |
2815 return true; | |
2816 } | |
2817 | |
2818 // a type array references its class | |
2819 inline bool VM_HeapWalkOperation::iterate_over_type_array(oop o) { | |
2820 klassOop k = o->klass(); | |
2821 oop mirror = Klass::cast(k)->java_mirror(); | |
2822 if (!CallbackInvoker::report_class_reference(o, mirror)) { | |
2823 return false; | |
2824 } | |
2825 | |
2826 // report the array contents if required | |
2827 if (is_reporting_primitive_array_values()) { | |
2828 if (!CallbackInvoker::report_primitive_array_values(o)) { | |
2829 return false; | |
2830 } | |
2831 } | |
2832 return true; | |
2833 } | |
2834 | |
2835 // verify that a static oop field is in range | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2836 static inline bool verify_static_oop(instanceKlass* ik, |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2837 klassOop k, int offset) { |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2838 address obj_p = (address)k + offset; |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2839 address start = (address)ik->start_of_static_fields(); |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2840 address end = start + (ik->static_oop_field_size() * heapOopSize); |
0 | 2841 assert(end >= start, "sanity check"); |
2842 | |
2843 if (obj_p >= start && obj_p < end) { | |
2844 return true; | |
2845 } else { | |
2846 return false; | |
2847 } | |
2848 } | |
2849 | |
2850 // a class references its super class, interfaces, class loader, ... | |
2851 // and finally its static fields | |
2852 inline bool VM_HeapWalkOperation::iterate_over_class(klassOop k) { | |
2853 int i; | |
2854 Klass* klass = klassOop(k)->klass_part(); | |
2855 | |
2856 if (klass->oop_is_instance()) { | |
2857 instanceKlass* ik = instanceKlass::cast(k); | |
2858 | |
2859 // ignore the class if it's has been initialized yet | |
2860 if (!ik->is_linked()) { | |
2861 return true; | |
2862 } | |
2863 | |
2864 // get the java mirror | |
2865 oop mirror = klass->java_mirror(); | |
2866 | |
2867 // super (only if something more interesting than java.lang.Object) | |
2868 klassOop java_super = ik->java_super(); | |
1142 | 2869 if (java_super != NULL && java_super != SystemDictionary::Object_klass()) { |
0 | 2870 oop super = Klass::cast(java_super)->java_mirror(); |
2871 if (!CallbackInvoker::report_superclass_reference(mirror, super)) { | |
2872 return false; | |
2873 } | |
2874 } | |
2875 | |
2876 // class loader | |
2877 oop cl = ik->class_loader(); | |
2878 if (cl != NULL) { | |
2879 if (!CallbackInvoker::report_class_loader_reference(mirror, cl)) { | |
2880 return false; | |
2881 } | |
2882 } | |
2883 | |
2884 // protection domain | |
2885 oop pd = ik->protection_domain(); | |
2886 if (pd != NULL) { | |
2887 if (!CallbackInvoker::report_protection_domain_reference(mirror, pd)) { | |
2888 return false; | |
2889 } | |
2890 } | |
2891 | |
2892 // signers | |
2893 oop signers = ik->signers(); | |
2894 if (signers != NULL) { | |
2895 if (!CallbackInvoker::report_signers_reference(mirror, signers)) { | |
2896 return false; | |
2897 } | |
2898 } | |
2899 | |
2900 // references from the constant pool | |
2901 { | |
2902 const constantPoolOop pool = ik->constants(); | |
2903 for (int i = 1; i < pool->length(); i++) { | |
2904 constantTag tag = pool->tag_at(i).value(); | |
2905 if (tag.is_string() || tag.is_klass()) { | |
2906 oop entry; | |
2907 if (tag.is_string()) { | |
2908 entry = pool->resolved_string_at(i); | |
2909 assert(java_lang_String::is_instance(entry), "must be string"); | |
2910 } else { | |
2911 entry = Klass::cast(pool->resolved_klass_at(i))->java_mirror(); | |
2912 } | |
2913 if (!CallbackInvoker::report_constant_pool_reference(mirror, entry, (jint)i)) { | |
2914 return false; | |
2915 } | |
2916 } | |
2917 } | |
2918 } | |
2919 | |
2920 // interfaces | |
2921 // (These will already have been reported as references from the constant pool | |
2922 // but are specified by IterateOverReachableObjects and must be reported). | |
2923 objArrayOop interfaces = ik->local_interfaces(); | |
2924 for (i = 0; i < interfaces->length(); i++) { | |
2925 oop interf = Klass::cast((klassOop)interfaces->obj_at(i))->java_mirror(); | |
2926 if (interf == NULL) { | |
2927 continue; | |
2928 } | |
2929 if (!CallbackInvoker::report_interface_reference(mirror, interf)) { | |
2930 return false; | |
2931 } | |
2932 } | |
2933 | |
2934 // iterate over the static fields | |
2935 | |
2936 ClassFieldMap* field_map = ClassFieldMap::create_map_of_static_fields(k); | |
2937 for (i=0; i<field_map->field_count(); i++) { | |
2938 ClassFieldDescriptor* field = field_map->field_at(i); | |
2939 char type = field->field_type(); | |
2940 if (!is_primitive_field_type(type)) { | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2941 oop fld_o = k->obj_field(field->field_offset()); |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2942 assert(verify_static_oop(ik, k, field->field_offset()), "sanity check"); |
0 | 2943 if (fld_o != NULL) { |
2944 int slot = field->field_index(); | |
2945 if (!CallbackInvoker::report_static_field_reference(mirror, fld_o, slot)) { | |
2946 delete field_map; | |
2947 return false; | |
2948 } | |
2949 } | |
2950 } else { | |
2951 if (is_reporting_primitive_fields()) { | |
2952 address addr = (address)k + field->field_offset(); | |
2953 int slot = field->field_index(); | |
2954 if (!CallbackInvoker::report_primitive_static_field(mirror, slot, addr, type)) { | |
2955 delete field_map; | |
2956 return false; | |
2957 } | |
2958 } | |
2959 } | |
2960 } | |
2961 delete field_map; | |
2962 | |
2963 return true; | |
2964 } | |
2965 | |
2966 return true; | |
2967 } | |
2968 | |
2969 // an object references a class and its instance fields | |
2970 // (static fields are ignored here as we report these as | |
2971 // references from the class). | |
2972 inline bool VM_HeapWalkOperation::iterate_over_object(oop o) { | |
2973 // reference to the class | |
2974 if (!CallbackInvoker::report_class_reference(o, Klass::cast(o->klass())->java_mirror())) { | |
2975 return false; | |
2976 } | |
2977 | |
2978 // iterate over instance fields | |
2979 ClassFieldMap* field_map = JvmtiCachedClassFieldMap::get_map_of_instance_fields(o); | |
2980 for (int i=0; i<field_map->field_count(); i++) { | |
2981 ClassFieldDescriptor* field = field_map->field_at(i); | |
2982 char type = field->field_type(); | |
2983 if (!is_primitive_field_type(type)) { | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
2984 oop fld_o = o->obj_field(field->field_offset()); |
0 | 2985 if (fld_o != NULL) { |
2986 // reflection code may have a reference to a klassOop. | |
2987 // - see sun.reflect.UnsafeStaticFieldAccessorImpl and sun.misc.Unsafe | |
2988 if (fld_o->is_klass()) { | |
2989 klassOop k = (klassOop)fld_o; | |
2990 fld_o = Klass::cast(k)->java_mirror(); | |
2991 } | |
2992 int slot = field->field_index(); | |
2993 if (!CallbackInvoker::report_field_reference(o, fld_o, slot)) { | |
2994 return false; | |
2995 } | |
2996 } | |
2997 } else { | |
2998 if (is_reporting_primitive_fields()) { | |
2999 // primitive instance field | |
3000 address addr = (address)o + field->field_offset(); | |
3001 int slot = field->field_index(); | |
3002 if (!CallbackInvoker::report_primitive_instance_field(o, slot, addr, type)) { | |
3003 return false; | |
3004 } | |
3005 } | |
3006 } | |
3007 } | |
3008 | |
3009 // if the object is a java.lang.String | |
3010 if (is_reporting_string_values() && | |
1142 | 3011 o->klass() == SystemDictionary::String_klass()) { |
0 | 3012 if (!CallbackInvoker::report_string_value(o)) { |
3013 return false; | |
3014 } | |
3015 } | |
3016 return true; | |
3017 } | |
3018 | |
3019 | |
3020 // collects all simple (non-stack) roots. | |
3021 // if there's a heap root callback provided then the callback is | |
3022 // invoked for each simple root. | |
3023 // if an object reference callback is provided then all simple | |
3024 // roots are pushed onto the marking stack so that they can be | |
3025 // processed later | |
3026 // | |
3027 inline bool VM_HeapWalkOperation::collect_simple_roots() { | |
3028 SimpleRootsClosure blk; | |
3029 | |
3030 // JNI globals | |
3031 blk.set_kind(JVMTI_HEAP_REFERENCE_JNI_GLOBAL); | |
3032 JNIHandles::oops_do(&blk); | |
3033 if (blk.stopped()) { | |
3034 return false; | |
3035 } | |
3036 | |
3037 // Preloaded classes and loader from the system dictionary | |
3038 blk.set_kind(JVMTI_HEAP_REFERENCE_SYSTEM_CLASS); | |
3039 SystemDictionary::always_strong_oops_do(&blk); | |
3040 if (blk.stopped()) { | |
3041 return false; | |
3042 } | |
3043 | |
3044 // Inflated monitors | |
3045 blk.set_kind(JVMTI_HEAP_REFERENCE_MONITOR); | |
3046 ObjectSynchronizer::oops_do(&blk); | |
3047 if (blk.stopped()) { | |
3048 return false; | |
3049 } | |
3050 | |
3051 // Threads | |
3052 for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) { | |
3053 oop threadObj = thread->threadObj(); | |
3054 if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) { | |
3055 bool cont = CallbackInvoker::report_simple_root(JVMTI_HEAP_REFERENCE_THREAD, threadObj); | |
3056 if (!cont) { | |
3057 return false; | |
3058 } | |
3059 } | |
3060 } | |
3061 | |
3062 // Other kinds of roots maintained by HotSpot | |
3063 // Many of these won't be visible but others (such as instances of important | |
3064 // exceptions) will be visible. | |
3065 blk.set_kind(JVMTI_HEAP_REFERENCE_OTHER); | |
3066 Universe::oops_do(&blk); | |
989
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
579
diff
changeset
|
3067 |
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
579
diff
changeset
|
3068 // If there are any non-perm roots in the code cache, visit them. |
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
579
diff
changeset
|
3069 blk.set_kind(JVMTI_HEAP_REFERENCE_OTHER); |
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
579
diff
changeset
|
3070 CodeBlobToOopClosure look_in_blobs(&blk, false); |
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
579
diff
changeset
|
3071 CodeCache::scavenge_root_nmethods_do(&look_in_blobs); |
148e5441d916
6863023: need non-perm oops in code cache for JSR 292
jrose
parents:
579
diff
changeset
|
3072 |
0 | 3073 return true; |
3074 } | |
3075 | |
3076 // Walk the stack of a given thread and find all references (locals | |
3077 // and JNI calls) and report these as stack references | |
3078 inline bool VM_HeapWalkOperation::collect_stack_roots(JavaThread* java_thread, | |
3079 JNILocalRootsClosure* blk) | |
3080 { | |
3081 oop threadObj = java_thread->threadObj(); | |
3082 assert(threadObj != NULL, "sanity check"); | |
3083 | |
3084 // only need to get the thread's tag once per thread | |
3085 jlong thread_tag = tag_for(_tag_map, threadObj); | |
3086 | |
3087 // also need the thread id | |
3088 jlong tid = java_lang_Thread::thread_id(threadObj); | |
3089 | |
3090 | |
3091 if (java_thread->has_last_Java_frame()) { | |
3092 | |
3093 // vframes are resource allocated | |
3094 Thread* current_thread = Thread::current(); | |
3095 ResourceMark rm(current_thread); | |
3096 HandleMark hm(current_thread); | |
3097 | |
3098 RegisterMap reg_map(java_thread); | |
3099 frame f = java_thread->last_frame(); | |
3100 vframe* vf = vframe::new_vframe(&f, ®_map, java_thread); | |
3101 | |
3102 bool is_top_frame = true; | |
3103 int depth = 0; | |
3104 frame* last_entry_frame = NULL; | |
3105 | |
3106 while (vf != NULL) { | |
3107 if (vf->is_java_frame()) { | |
3108 | |
3109 // java frame (interpreted, compiled, ...) | |
3110 javaVFrame *jvf = javaVFrame::cast(vf); | |
3111 | |
3112 // the jmethodID | |
3113 jmethodID method = jvf->method()->jmethod_id(); | |
3114 | |
3115 if (!(jvf->method()->is_native())) { | |
3116 jlocation bci = (jlocation)jvf->bci(); | |
3117 StackValueCollection* locals = jvf->locals(); | |
3118 for (int slot=0; slot<locals->size(); slot++) { | |
3119 if (locals->at(slot)->type() == T_OBJECT) { | |
3120 oop o = locals->obj_at(slot)(); | |
3121 if (o == NULL) { | |
3122 continue; | |
3123 } | |
3124 | |
3125 // stack reference | |
3126 if (!CallbackInvoker::report_stack_ref_root(thread_tag, tid, depth, method, | |
3127 bci, slot, o)) { | |
3128 return false; | |
3129 } | |
3130 } | |
3131 } | |
3132 } else { | |
3133 blk->set_context(thread_tag, tid, depth, method); | |
3134 if (is_top_frame) { | |
3135 // JNI locals for the top frame. | |
3136 java_thread->active_handles()->oops_do(blk); | |
3137 } else { | |
3138 if (last_entry_frame != NULL) { | |
3139 // JNI locals for the entry frame | |
3140 assert(last_entry_frame->is_entry_frame(), "checking"); | |
3141 last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(blk); | |
3142 } | |
3143 } | |
3144 } | |
3145 last_entry_frame = NULL; | |
3146 depth++; | |
3147 } else { | |
3148 // externalVFrame - for an entry frame then we report the JNI locals | |
3149 // when we find the corresponding javaVFrame | |
3150 frame* fr = vf->frame_pointer(); | |
3151 assert(fr != NULL, "sanity check"); | |
3152 if (fr->is_entry_frame()) { | |
3153 last_entry_frame = fr; | |
3154 } | |
3155 } | |
3156 | |
3157 vf = vf->sender(); | |
3158 is_top_frame = false; | |
3159 } | |
3160 } else { | |
3161 // no last java frame but there may be JNI locals | |
3162 blk->set_context(thread_tag, tid, 0, (jmethodID)NULL); | |
3163 java_thread->active_handles()->oops_do(blk); | |
3164 } | |
3165 return true; | |
3166 } | |
3167 | |
3168 | |
3169 // collects all stack roots - for each thread it walks the execution | |
3170 // stack to find all references and local JNI refs. | |
3171 inline bool VM_HeapWalkOperation::collect_stack_roots() { | |
3172 JNILocalRootsClosure blk; | |
3173 for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) { | |
3174 oop threadObj = thread->threadObj(); | |
3175 if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) { | |
3176 if (!collect_stack_roots(thread, &blk)) { | |
3177 return false; | |
3178 } | |
3179 } | |
3180 } | |
3181 return true; | |
3182 } | |
3183 | |
3184 // visit an object | |
3185 // first mark the object as visited | |
3186 // second get all the outbound references from this object (in other words, all | |
3187 // the objects referenced by this object). | |
3188 // | |
3189 bool VM_HeapWalkOperation::visit(oop o) { | |
3190 // mark object as visited | |
3191 assert(!ObjectMarker::visited(o), "can't visit same object more than once"); | |
3192 ObjectMarker::mark(o); | |
3193 | |
3194 // instance | |
3195 if (o->is_instance()) { | |
1142 | 3196 if (o->klass() == SystemDictionary::Class_klass()) { |
0 | 3197 o = klassOop_if_java_lang_Class(o); |
3198 if (o->is_klass()) { | |
3199 // a java.lang.Class | |
3200 return iterate_over_class(klassOop(o)); | |
3201 } | |
3202 } else { | |
3203 return iterate_over_object(o); | |
3204 } | |
3205 } | |
3206 | |
3207 // object array | |
3208 if (o->is_objArray()) { | |
3209 return iterate_over_array(o); | |
3210 } | |
3211 | |
3212 // type array | |
3213 if (o->is_typeArray()) { | |
3214 return iterate_over_type_array(o); | |
3215 } | |
3216 | |
3217 return true; | |
3218 } | |
3219 | |
3220 void VM_HeapWalkOperation::doit() { | |
3221 ResourceMark rm; | |
3222 ObjectMarkerController marker; | |
3223 ClassFieldMapCacheMark cm; | |
3224 | |
3225 assert(visit_stack()->is_empty(), "visit stack must be empty"); | |
3226 | |
3227 // the heap walk starts with an initial object or the heap roots | |
3228 if (initial_object().is_null()) { | |
3229 if (!collect_simple_roots()) return; | |
3230 if (!collect_stack_roots()) return; | |
3231 } else { | |
3232 visit_stack()->push(initial_object()()); | |
3233 } | |
3234 | |
3235 // object references required | |
3236 if (is_following_references()) { | |
3237 | |
3238 // visit each object until all reachable objects have been | |
3239 // visited or the callback asked to terminate the iteration. | |
3240 while (!visit_stack()->is_empty()) { | |
3241 oop o = visit_stack()->pop(); | |
3242 if (!ObjectMarker::visited(o)) { | |
3243 if (!visit(o)) { | |
3244 break; | |
3245 } | |
3246 } | |
3247 } | |
3248 } | |
3249 } | |
3250 | |
3251 // iterate over all objects that are reachable from a set of roots | |
3252 void JvmtiTagMap::iterate_over_reachable_objects(jvmtiHeapRootCallback heap_root_callback, | |
3253 jvmtiStackReferenceCallback stack_ref_callback, | |
3254 jvmtiObjectReferenceCallback object_ref_callback, | |
3255 const void* user_data) { | |
3256 MutexLocker ml(Heap_lock); | |
3257 BasicHeapWalkContext context(heap_root_callback, stack_ref_callback, object_ref_callback); | |
3258 VM_HeapWalkOperation op(this, Handle(), context, user_data); | |
3259 VMThread::execute(&op); | |
3260 } | |
3261 | |
3262 // iterate over all objects that are reachable from a given object | |
3263 void JvmtiTagMap::iterate_over_objects_reachable_from_object(jobject object, | |
3264 jvmtiObjectReferenceCallback object_ref_callback, | |
3265 const void* user_data) { | |
3266 oop obj = JNIHandles::resolve(object); | |
3267 Handle initial_object(Thread::current(), obj); | |
3268 | |
3269 MutexLocker ml(Heap_lock); | |
3270 BasicHeapWalkContext context(NULL, NULL, object_ref_callback); | |
3271 VM_HeapWalkOperation op(this, initial_object, context, user_data); | |
3272 VMThread::execute(&op); | |
3273 } | |
3274 | |
3275 // follow references from an initial object or the GC roots | |
3276 void JvmtiTagMap::follow_references(jint heap_filter, | |
3277 KlassHandle klass, | |
3278 jobject object, | |
3279 const jvmtiHeapCallbacks* callbacks, | |
3280 const void* user_data) | |
3281 { | |
3282 oop obj = JNIHandles::resolve(object); | |
3283 Handle initial_object(Thread::current(), obj); | |
3284 | |
3285 MutexLocker ml(Heap_lock); | |
3286 AdvancedHeapWalkContext context(heap_filter, klass, callbacks); | |
3287 VM_HeapWalkOperation op(this, initial_object, context, user_data); | |
3288 VMThread::execute(&op); | |
3289 } | |
3290 | |
3291 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3292 void JvmtiTagMap::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { |
2145
c1a0ede55d6f
7012493: 2/2 6849574/Test.java fails with Internal Error (src/share/vm/prims/jvmtiTagMap.cpp:3294)
dcubed
parents:
2125
diff
changeset
|
3293 // No locks during VM bring-up (0 threads) and no safepoints after main |
c1a0ede55d6f
7012493: 2/2 6849574/Test.java fails with Internal Error (src/share/vm/prims/jvmtiTagMap.cpp:3294)
dcubed
parents:
2125
diff
changeset
|
3294 // thread creation and before VMThread creation (1 thread); initial GC |
c1a0ede55d6f
7012493: 2/2 6849574/Test.java fails with Internal Error (src/share/vm/prims/jvmtiTagMap.cpp:3294)
dcubed
parents:
2125
diff
changeset
|
3295 // verification can happen in that window which gets to here. |
c1a0ede55d6f
7012493: 2/2 6849574/Test.java fails with Internal Error (src/share/vm/prims/jvmtiTagMap.cpp:3294)
dcubed
parents:
2125
diff
changeset
|
3296 assert(Threads::number_of_threads() <= 1 || |
c1a0ede55d6f
7012493: 2/2 6849574/Test.java fails with Internal Error (src/share/vm/prims/jvmtiTagMap.cpp:3294)
dcubed
parents:
2125
diff
changeset
|
3297 SafepointSynchronize::is_at_safepoint(), |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3298 "must be executed at a safepoint"); |
0 | 3299 if (JvmtiEnv::environments_might_exist()) { |
3300 JvmtiEnvIterator it; | |
3301 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) { | |
3302 JvmtiTagMap* tag_map = env->tag_map(); | |
3303 if (tag_map != NULL && !tag_map->is_empty()) { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3304 tag_map->do_weak_oops(is_alive, f); |
0 | 3305 } |
3306 } | |
3307 } | |
3308 } | |
3309 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3310 void JvmtiTagMap::do_weak_oops(BoolObjectClosure* is_alive, OopClosure* f) { |
0 | 3311 |
3312 // does this environment have the OBJECT_FREE event enabled | |
3313 bool post_object_free = env()->is_enabled(JVMTI_EVENT_OBJECT_FREE); | |
3314 | |
3315 // counters used for trace message | |
3316 int freed = 0; | |
3317 int moved = 0; | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3318 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3319 JvmtiTagHashmap* hashmap = this->hashmap(); |
0 | 3320 |
3321 // reenable sizing (if disabled) | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3322 hashmap->set_resizing_enabled(true); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3323 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3324 // if the hashmap is empty then we can skip it |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3325 if (hashmap->_entry_count == 0) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3326 return; |
0 | 3327 } |
3328 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3329 // now iterate through each entry in the table |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3330 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3331 JvmtiTagHashmapEntry** table = hashmap->table(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3332 int size = hashmap->size(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3333 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3334 JvmtiTagHashmapEntry* delayed_add = NULL; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3335 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3336 for (int pos = 0; pos < size; ++pos) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3337 JvmtiTagHashmapEntry* entry = table[pos]; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3338 JvmtiTagHashmapEntry* prev = NULL; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3339 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3340 while (entry != NULL) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3341 JvmtiTagHashmapEntry* next = entry->next(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3342 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3343 oop* obj = entry->object_addr(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3344 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3345 // has object been GC'ed |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3346 if (!is_alive->do_object_b(entry->object())) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3347 // grab the tag |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3348 jlong tag = entry->tag(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3349 guarantee(tag != 0, "checking"); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3350 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3351 // remove GC'ed entry from hashmap and return the |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3352 // entry to the free list |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3353 hashmap->remove(prev, pos, entry); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3354 destroy_entry(entry); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3355 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3356 // post the event to the profiler |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3357 if (post_object_free) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3358 JvmtiExport::post_object_free(env(), tag); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3359 } |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3360 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3361 ++freed; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3362 } else { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3363 f->do_oop(entry->object_addr()); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3364 oop new_oop = entry->object(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3365 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3366 // if the object has moved then re-hash it and move its |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3367 // entry to its new location. |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3368 unsigned int new_pos = JvmtiTagHashmap::hash(new_oop, size); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3369 if (new_pos != (unsigned int)pos) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3370 if (prev == NULL) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3371 table[pos] = next; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3372 } else { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3373 prev->set_next(next); |
0 | 3374 } |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3375 if (new_pos < (unsigned int)pos) { |
0 | 3376 entry->set_next(table[new_pos]); |
3377 table[new_pos] = entry; | |
3378 } else { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3379 // Delay adding this entry to it's new position as we'd end up |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3380 // hitting it again during this iteration. |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3381 entry->set_next(delayed_add); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3382 delayed_add = entry; |
0 | 3383 } |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3384 moved++; |
0 | 3385 } else { |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3386 // object didn't move |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3387 prev = entry; |
0 | 3388 } |
3389 } | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3390 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3391 entry = next; |
0 | 3392 } |
3393 } | |
3394 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3395 // Re-add all the entries which were kept aside |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3396 while (delayed_add != NULL) { |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3397 JvmtiTagHashmapEntry* next = delayed_add->next(); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3398 unsigned int pos = JvmtiTagHashmap::hash(delayed_add->object(), size); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3399 delayed_add->set_next(table[pos]); |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3400 table[pos] = delayed_add; |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3401 delayed_add = next; |
0 | 3402 } |
3403 | |
3404 // stats | |
3405 if (TraceJVMTIObjectTagging) { | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3406 int post_total = hashmap->_entry_count; |
0 | 3407 int pre_total = post_total + freed; |
3408 | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3409 tty->print_cr("(%d->%d, %d freed, %d total moves)", |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
3410 pre_total, post_total, freed, moved); |
0 | 3411 } |
3412 } |