instanceRefKlass.cpp 19.9 KB
Newer Older
D
duke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

# include "incls/_precompiled.incl"
# include "incls/_instanceRefKlass.cpp.incl"

28 29 30 31
template <class T>
static void specialized_oop_follow_contents(instanceRefKlass* ref, oop obj) {
  T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
  oop referent = oopDesc::load_decode_heap_oop(referent_addr);
D
duke 已提交
32 33
  debug_only(
    if(TraceReferenceGC && PrintGCDetails) {
34
      gclog_or_tty->print_cr("instanceRefKlass::oop_follow_contents " INTPTR_FORMAT, obj);
D
duke 已提交
35 36 37 38 39
    }
  )
  if (referent != NULL) {
    if (!referent->is_gc_marked() &&
        MarkSweep::ref_processor()->
40
          discover_reference(obj, ref->reference_type())) {
D
duke 已提交
41
      // reference already enqueued, referent will be traversed later
42
      ref->instanceKlass::oop_follow_contents(obj);
D
duke 已提交
43 44
      debug_only(
        if(TraceReferenceGC && PrintGCDetails) {
45
          gclog_or_tty->print_cr("       Non NULL enqueued " INTPTR_FORMAT, obj);
D
duke 已提交
46 47 48 49 50 51 52
        }
      )
      return;
    } else {
      // treat referent as normal oop
      debug_only(
        if(TraceReferenceGC && PrintGCDetails) {
53
          gclog_or_tty->print_cr("       Non NULL normal " INTPTR_FORMAT, obj);
D
duke 已提交
54 55 56 57 58 59
        }
      )
      MarkSweep::mark_and_push(referent_addr);
    }
  }
  // treat next as normal oop.  next is a link in the pending list.
60
  T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
D
duke 已提交
61 62 63 64 65 66
  debug_only(
    if(TraceReferenceGC && PrintGCDetails) {
      gclog_or_tty->print_cr("   Process next as normal " INTPTR_FORMAT, next_addr);
    }
  )
  MarkSweep::mark_and_push(next_addr);
67 68 69 70 71 72 73 74 75
  ref->instanceKlass::oop_follow_contents(obj);
}

void instanceRefKlass::oop_follow_contents(oop obj) {
  if (UseCompressedOops) {
    specialized_oop_follow_contents<narrowOop>(this, obj);
  } else {
    specialized_oop_follow_contents<oop>(this, obj);
  }
D
duke 已提交
76 77 78
}

#ifndef SERIALGC
79 80 81 82 83 84
template <class T>
static void specialized_oop_follow_contents(instanceRefKlass* ref,
                                            ParCompactionManager* cm,
                                            oop obj) {
  T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
  oop referent = oopDesc::load_decode_heap_oop(referent_addr);
D
duke 已提交
85 86
  debug_only(
    if(TraceReferenceGC && PrintGCDetails) {
87
      gclog_or_tty->print_cr("instanceRefKlass::oop_follow_contents " INTPTR_FORMAT, obj);
D
duke 已提交
88 89 90 91 92
    }
  )
  if (referent != NULL) {
    if (PSParallelCompact::mark_bitmap()->is_unmarked(referent) &&
        PSParallelCompact::ref_processor()->
93
          discover_reference(obj, ref->reference_type())) {
D
duke 已提交
94
      // reference already enqueued, referent will be traversed later
95
      ref->instanceKlass::oop_follow_contents(cm, obj);
D
duke 已提交
96 97
      debug_only(
        if(TraceReferenceGC && PrintGCDetails) {
98
          gclog_or_tty->print_cr("       Non NULL enqueued " INTPTR_FORMAT, obj);
D
duke 已提交
99 100 101 102 103 104 105
        }
      )
      return;
    } else {
      // treat referent as normal oop
      debug_only(
        if(TraceReferenceGC && PrintGCDetails) {
106
          gclog_or_tty->print_cr("       Non NULL normal " INTPTR_FORMAT, obj);
D
duke 已提交
107 108 109 110 111 112
        }
      )
      PSParallelCompact::mark_and_push(cm, referent_addr);
    }
  }
  // treat next as normal oop.  next is a link in the pending list.
113
  T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
D
duke 已提交
114 115 116 117 118 119
  debug_only(
    if(TraceReferenceGC && PrintGCDetails) {
      gclog_or_tty->print_cr("   Process next as normal " INTPTR_FORMAT, next_addr);
    }
  )
  PSParallelCompact::mark_and_push(cm, next_addr);
120
  ref->instanceKlass::oop_follow_contents(cm, obj);
D
duke 已提交
121 122
}

123 124 125 126 127 128 129 130 131
void instanceRefKlass::oop_follow_contents(ParCompactionManager* cm,
                                           oop obj) {
  if (UseCompressedOops) {
    specialized_oop_follow_contents<narrowOop>(this, cm, obj);
  } else {
    specialized_oop_follow_contents<oop>(this, cm, obj);
  }
}
#endif // SERIALGC
D
duke 已提交
132 133

#ifdef ASSERT
134 135 136 137
template <class T> void trace_reference_gc(const char *s, oop obj,
                                           T* referent_addr,
                                           T* next_addr,
                                           T* discovered_addr) {
D
duke 已提交
138
  if(TraceReferenceGC && PrintGCDetails) {
139
    gclog_or_tty->print_cr("%s obj " INTPTR_FORMAT, s, (address)obj);
D
duke 已提交
140
    gclog_or_tty->print_cr("     referent_addr/* " INTPTR_FORMAT " / "
141 142 143
         INTPTR_FORMAT, referent_addr,
         referent_addr ?
           (address)oopDesc::load_decode_heap_oop(referent_addr) : NULL);
D
duke 已提交
144
    gclog_or_tty->print_cr("     next_addr/* " INTPTR_FORMAT " / "
145 146
         INTPTR_FORMAT, next_addr,
         next_addr ? (address)oopDesc::load_decode_heap_oop(next_addr) : NULL);
D
duke 已提交
147
    gclog_or_tty->print_cr("     discovered_addr/* " INTPTR_FORMAT " / "
148 149 150
         INTPTR_FORMAT, discovered_addr,
         discovered_addr ?
           (address)oopDesc::load_decode_heap_oop(discovered_addr) : NULL);
D
duke 已提交
151
  }
152
}
D
duke 已提交
153 154
#endif

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
template <class T> void specialized_oop_adjust_pointers(instanceRefKlass *ref, oop obj) {
  T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
  MarkSweep::adjust_pointer(referent_addr);
  T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
  MarkSweep::adjust_pointer(next_addr);
  T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);
  MarkSweep::adjust_pointer(discovered_addr);
  debug_only(trace_reference_gc("instanceRefKlass::oop_adjust_pointers", obj,
                                referent_addr, next_addr, discovered_addr);)
}

int instanceRefKlass::oop_adjust_pointers(oop obj) {
  int size = size_helper();
  instanceKlass::oop_adjust_pointers(obj);

  if (UseCompressedOops) {
    specialized_oop_adjust_pointers<narrowOop>(this, obj);
  } else {
    specialized_oop_adjust_pointers<oop>(this, obj);
  }
D
duke 已提交
175 176 177
  return size;
}

178 179 180 181
#define InstanceRefKlass_SPECIALIZED_OOP_ITERATE(T, nv_suffix, contains)        \
  T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);           \
  oop referent = oopDesc::load_decode_heap_oop(referent_addr);                  \
  if (referent != NULL && contains(referent_addr)) {                            \
D
duke 已提交
182 183
    ReferenceProcessor* rp = closure->_ref_processor;                           \
    if (!referent->is_gc_marked() && (rp != NULL) &&                            \
184
        rp->discover_reference(obj, reference_type())) {                        \
D
duke 已提交
185 186 187 188 189 190 191 192
      return size;                                                              \
    } else {                                                                    \
      /* treat referent as normal oop */                                        \
      SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk);\
      closure->do_oop##nv_suffix(referent_addr);                                \
    }                                                                           \
  }                                                                             \
  /* treat next as normal oop */                                                \
193 194 195 196 197
  T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);                   \
  if (contains(next_addr)) {                                                    \
    SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk); \
    closure->do_oop##nv_suffix(next_addr);                                      \
  }                                                                             \
D
duke 已提交
198
  return size;                                                                  \
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219


template <class T> bool contains(T *t) { return true; }

// Macro to define instanceRefKlass::oop_oop_iterate for virtual/nonvirtual for
// all closures.  Macros calling macros above for each oop size.

#define InstanceRefKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix)        \
                                                                                \
int instanceRefKlass::                                                          \
oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) {                  \
  /* Get size before changing pointers */                                       \
  SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\
                                                                                \
  int size = instanceKlass::oop_oop_iterate##nv_suffix(obj, closure);           \
                                                                                \
  if (UseCompressedOops) {                                                      \
    InstanceRefKlass_SPECIALIZED_OOP_ITERATE(narrowOop, nv_suffix, contains);   \
  } else {                                                                      \
    InstanceRefKlass_SPECIALIZED_OOP_ITERATE(oop, nv_suffix, contains);         \
  }                                                                             \
D
duke 已提交
220 221 222 223 224 225 226 227 228 229 230
}

#define InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix)      \
                                                                                \
int instanceRefKlass::                                                          \
oop_oop_iterate##nv_suffix##_m(oop obj,                                         \
                               OopClosureType* closure,                         \
                               MemRegion mr) {                                  \
  SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\
                                                                                \
  int size = instanceKlass::oop_oop_iterate##nv_suffix##_m(obj, closure, mr);   \
231 232 233 234
  if (UseCompressedOops) {                                                      \
    InstanceRefKlass_SPECIALIZED_OOP_ITERATE(narrowOop, nv_suffix, mr.contains); \
  } else {                                                                      \
    InstanceRefKlass_SPECIALIZED_OOP_ITERATE(oop, nv_suffix, mr.contains);      \
D
duke 已提交
235 236 237 238 239 240 241 242 243
  }                                                                             \
}

ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_DEFN)
ALL_OOP_OOP_ITERATE_CLOSURES_3(InstanceRefKlass_OOP_OOP_ITERATE_DEFN)
ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m)
ALL_OOP_OOP_ITERATE_CLOSURES_3(InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m)

#ifndef SERIALGC
244 245 246
template <class T>
void specialized_oop_copy_contents(instanceRefKlass *ref,
                                   PSPromotionManager* pm, oop obj) {
D
duke 已提交
247
  assert(!pm->depth_first(), "invariant");
248 249
  T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
  if (PSScavenge::should_scavenge(referent_addr)) {
D
duke 已提交
250
    ReferenceProcessor* rp = PSScavenge::reference_processor();
251
    if (rp->discover_reference(obj, ref->reference_type())) {
D
duke 已提交
252
      // reference already enqueued, referent and next will be traversed later
253
      ref->instanceKlass::oop_copy_contents(pm, obj);
D
duke 已提交
254 255 256 257 258 259 260
      return;
    } else {
      // treat referent as normal oop
      pm->claim_or_forward_breadth(referent_addr);
    }
  }
  // treat next as normal oop
261 262
  T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
  if (PSScavenge::should_scavenge(next_addr)) {
D
duke 已提交
263 264
    pm->claim_or_forward_breadth(next_addr);
  }
265
  ref->instanceKlass::oop_copy_contents(pm, obj);
D
duke 已提交
266 267
}

268 269 270 271 272 273 274 275 276 277 278
void instanceRefKlass::oop_copy_contents(PSPromotionManager* pm, oop obj) {
  if (UseCompressedOops) {
    specialized_oop_copy_contents<narrowOop>(this, pm, obj);
  } else {
    specialized_oop_copy_contents<oop>(this, pm, obj);
  }
}

template <class T>
void specialized_oop_push_contents(instanceRefKlass *ref,
                                   PSPromotionManager* pm, oop obj) {
D
duke 已提交
279
  assert(pm->depth_first(), "invariant");
280 281
  T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
  if (PSScavenge::should_scavenge(referent_addr)) {
D
duke 已提交
282
    ReferenceProcessor* rp = PSScavenge::reference_processor();
283
    if (rp->discover_reference(obj, ref->reference_type())) {
D
duke 已提交
284
      // reference already enqueued, referent and next will be traversed later
285
      ref->instanceKlass::oop_push_contents(pm, obj);
D
duke 已提交
286 287 288 289 290 291 292
      return;
    } else {
      // treat referent as normal oop
      pm->claim_or_forward_depth(referent_addr);
    }
  }
  // treat next as normal oop
293 294
  T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
  if (PSScavenge::should_scavenge(next_addr)) {
D
duke 已提交
295 296
    pm->claim_or_forward_depth(next_addr);
  }
297
  ref->instanceKlass::oop_push_contents(pm, obj);
D
duke 已提交
298 299
}

300 301 302 303 304 305 306
void instanceRefKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
  if (UseCompressedOops) {
    specialized_oop_push_contents<narrowOop>(this, pm, obj);
  } else {
    specialized_oop_push_contents<oop>(this, pm, obj);
  }
}
D
duke 已提交
307

308 309 310 311
template <class T>
void specialized_oop_update_pointers(instanceRefKlass *ref,
                                    ParCompactionManager* cm, oop obj) {
  T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
D
duke 已提交
312
  PSParallelCompact::adjust_pointer(referent_addr);
313
  T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
D
duke 已提交
314
  PSParallelCompact::adjust_pointer(next_addr);
315
  T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);
D
duke 已提交
316
  PSParallelCompact::adjust_pointer(discovered_addr);
317 318 319
  debug_only(trace_reference_gc("instanceRefKlass::oop_update_ptrs", obj,
                                referent_addr, next_addr, discovered_addr);)
}
D
duke 已提交
320

321 322 323 324 325 326
int instanceRefKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
  instanceKlass::oop_update_pointers(cm, obj);
  if (UseCompressedOops) {
    specialized_oop_update_pointers<narrowOop>(this, cm, obj);
  } else {
    specialized_oop_update_pointers<oop>(this, cm, obj);
D
duke 已提交
327 328 329 330 331
  }
  return size_helper();
}


332 333 334 335 336
template <class T> void
specialized_oop_update_pointers(ParCompactionManager* cm, oop obj,
                                HeapWord* beg_addr, HeapWord* end_addr) {
  T* p;
  T* referent_addr = p = (T*)java_lang_ref_Reference::referent_addr(obj);
D
duke 已提交
337
  PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
338
  T* next_addr = p = (T*)java_lang_ref_Reference::next_addr(obj);
D
duke 已提交
339
  PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
340
  T* discovered_addr = p = (T*)java_lang_ref_Reference::discovered_addr(obj);
D
duke 已提交
341
  PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
342 343 344
  debug_only(trace_reference_gc("instanceRefKlass::oop_update_ptrs", obj,
                                referent_addr, next_addr, discovered_addr);)
}
D
duke 已提交
345

346 347 348 349 350 351 352 353
int
instanceRefKlass::oop_update_pointers(ParCompactionManager* cm, oop obj,
                                      HeapWord* beg_addr, HeapWord* end_addr) {
  instanceKlass::oop_update_pointers(cm, obj, beg_addr, end_addr);
  if (UseCompressedOops) {
    specialized_oop_update_pointers<narrowOop>(cm, obj, beg_addr, end_addr);
  } else {
    specialized_oop_update_pointers<oop>(cm, obj, beg_addr, end_addr);
D
duke 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
  }
  return size_helper();
}
#endif // SERIALGC

void instanceRefKlass::update_nonstatic_oop_maps(klassOop k) {
  // Clear the nonstatic oop-map entries corresponding to referent
  // and nextPending field.  They are treated specially by the
  // garbage collector.
  // The discovered field is used only by the garbage collector
  // and is also treated specially.
  instanceKlass* ik = instanceKlass::cast(k);

  // Check that we have the right class
  debug_only(static bool first_time = true);
  assert(k == SystemDictionary::reference_klass() && first_time,
         "Invalid update of maps");
  debug_only(first_time = false);
  assert(ik->nonstatic_oop_map_size() == 1, "just checking");

  OopMapBlock* map = ik->start_of_nonstatic_oop_maps();

  // Check that the current map is (2,4) - currently points at field with
  // offset 2 (words) and has 4 map entries.
  debug_only(int offset = java_lang_ref_Reference::referent_offset);
  debug_only(int length = ((java_lang_ref_Reference::discovered_offset -
380
    java_lang_ref_Reference::referent_offset)/heapOopSize) + 1);
D
duke 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

  if (UseSharedSpaces) {
    assert(map->offset() == java_lang_ref_Reference::queue_offset &&
           map->length() == 1, "just checking");
  } else {
    assert(map->offset() == offset && map->length() == length,
           "just checking");

    // Update map to (3,1) - point to offset of 3 (words) with 1 map entry.
    map->set_offset(java_lang_ref_Reference::queue_offset);
    map->set_length(1);
  }
}


// Verification

void instanceRefKlass::oop_verify_on(oop obj, outputStream* st) {
  instanceKlass::oop_verify_on(obj, st);
  // Verify referent field
  oop referent = java_lang_ref_Reference::referent(obj);

  // We should make this general to all heaps
  GenCollectedHeap* gch = NULL;
  if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap)
    gch = GenCollectedHeap::heap();

  if (referent != NULL) {
    guarantee(referent->is_oop(), "referent field heap failed");
410
    if (gch != NULL && !gch->is_in_youngest(obj)) {
D
duke 已提交
411 412 413
      // We do a specific remembered set check here since the referent
      // field is not part of the oop mask and therefore skipped by the
      // regular verify code.
414 415 416 417 418 419 420 421
      if (UseCompressedOops) {
        narrowOop* referent_addr = (narrowOop*)java_lang_ref_Reference::referent_addr(obj);
        obj->verify_old_oop(referent_addr, true);
      } else {
        oop* referent_addr = (oop*)java_lang_ref_Reference::referent_addr(obj);
        obj->verify_old_oop(referent_addr, true);
      }
    }
D
duke 已提交
422 423 424 425
  }
  // Verify next field
  oop next = java_lang_ref_Reference::next(obj);
  if (next != NULL) {
426
    guarantee(next->is_oop(), "next field verify fa iled");
D
duke 已提交
427 428 429 430 431
    guarantee(next->is_instanceRef(), "next field verify failed");
    if (gch != NULL && !gch->is_in_youngest(obj)) {
      // We do a specific remembered set check here since the next field is
      // not part of the oop mask and therefore skipped by the regular
      // verify code.
432 433 434 435 436 437 438
      if (UseCompressedOops) {
        narrowOop* next_addr = (narrowOop*)java_lang_ref_Reference::next_addr(obj);
        obj->verify_old_oop(next_addr, true);
      } else {
        oop* next_addr = (oop*)java_lang_ref_Reference::next_addr(obj);
        obj->verify_old_oop(next_addr, true);
      }
D
duke 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    }
  }
}

void instanceRefKlass::acquire_pending_list_lock(BasicLock *pending_list_basic_lock) {
  // we may enter this with pending exception set
  PRESERVE_EXCEPTION_MARK;  // exceptions are never thrown, needed for TRAPS argument
  Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());
  ObjectSynchronizer::fast_enter(h_lock, pending_list_basic_lock, false, THREAD);
  assert(ObjectSynchronizer::current_thread_holds_lock(
           JavaThread::current(), h_lock),
         "Locking should have succeeded");
  if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;
}

void instanceRefKlass::release_and_notify_pending_list_lock(
  BasicLock *pending_list_basic_lock) {
  // we may enter this with pending exception set
  PRESERVE_EXCEPTION_MARK;  // exceptions are never thrown, needed for TRAPS argument
  //
  Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());
  assert(ObjectSynchronizer::current_thread_holds_lock(
           JavaThread::current(), h_lock),
         "Lock should be held");
  // Notify waiters on pending lists lock if there is any reference.
  if (java_lang_ref_Reference::pending_list() != NULL) {
    ObjectSynchronizer::notifyall(h_lock, THREAD);
  }
  ObjectSynchronizer::fast_exit(h_lock(), pending_list_basic_lock, THREAD);
  if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;
}