annotate src/share/vm/prims/jvmtiTagMap.cpp @ 0:a61af66fc99e

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