instanceRefKlass.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25# include "incls/_precompiled.incl"
26# include "incls/_instanceRefKlass.cpp.incl"
27
28void instanceRefKlass::oop_follow_contents(oop obj) {
29  oop* referent_addr = java_lang_ref_Reference::referent_addr(obj);
30  oop referent = *referent_addr;
31  debug_only(
32    if(TraceReferenceGC && PrintGCDetails) {
33      gclog_or_tty->print_cr("instanceRefKlass::oop_follow_contents " INTPTR_FORMAT, (address)obj);
34    }
35  )
36  if (referent != NULL) {
37    if (!referent->is_gc_marked() &&
38        MarkSweep::ref_processor()->
39          discover_reference(obj, reference_type())) {
40      // reference already enqueued, referent will be traversed later
41      instanceKlass::oop_follow_contents(obj);
42      debug_only(
43        if(TraceReferenceGC && PrintGCDetails) {
44          gclog_or_tty->print_cr("       Non NULL enqueued " INTPTR_FORMAT, (address)obj);
45        }
46      )
47      return;
48    } else {
49      // treat referent as normal oop
50      debug_only(
51        if(TraceReferenceGC && PrintGCDetails) {
52          gclog_or_tty->print_cr("       Non NULL normal " INTPTR_FORMAT, (address)obj);
53        }
54      )
55      MarkSweep::mark_and_push(referent_addr);
56    }
57  }
58  // treat next as normal oop.  next is a link in the pending list.
59  oop* next_addr = java_lang_ref_Reference::next_addr(obj);
60  debug_only(
61    if(TraceReferenceGC && PrintGCDetails) {
62      gclog_or_tty->print_cr("   Process next as normal " INTPTR_FORMAT, next_addr);
63    }
64  )
65  MarkSweep::mark_and_push(next_addr);
66  instanceKlass::oop_follow_contents(obj);
67}
68
69#ifndef SERIALGC
70void instanceRefKlass::oop_follow_contents(ParCompactionManager* cm,
71                                           oop obj) {
72  oop* referent_addr = java_lang_ref_Reference::referent_addr(obj);
73  oop referent = *referent_addr;
74  debug_only(
75    if(TraceReferenceGC && PrintGCDetails) {
76      gclog_or_tty->print_cr("instanceRefKlass::oop_follow_contents " INTPTR_FORMAT, (address)obj);
77    }
78  )
79  if (referent != NULL) {
80    if (PSParallelCompact::mark_bitmap()->is_unmarked(referent) &&
81        PSParallelCompact::ref_processor()->
82          discover_reference(obj, reference_type())) {
83      // reference already enqueued, referent will be traversed later
84      instanceKlass::oop_follow_contents(cm, obj);
85      debug_only(
86        if(TraceReferenceGC && PrintGCDetails) {
87          gclog_or_tty->print_cr("       Non NULL enqueued " INTPTR_FORMAT, (address)obj);
88        }
89      )
90      return;
91    } else {
92      // treat referent as normal oop
93      debug_only(
94        if(TraceReferenceGC && PrintGCDetails) {
95          gclog_or_tty->print_cr("       Non NULL normal " INTPTR_FORMAT, (address)obj);
96        }
97      )
98      PSParallelCompact::mark_and_push(cm, referent_addr);
99    }
100  }
101  // treat next as normal oop.  next is a link in the pending list.
102  oop* next_addr = java_lang_ref_Reference::next_addr(obj);
103  debug_only(
104    if(TraceReferenceGC && PrintGCDetails) {
105      gclog_or_tty->print_cr("   Process next as normal " INTPTR_FORMAT, next_addr);
106    }
107  )
108  PSParallelCompact::mark_and_push(cm, next_addr);
109  instanceKlass::oop_follow_contents(cm, obj);
110}
111#endif // SERIALGC
112
113
114int instanceRefKlass::oop_adjust_pointers(oop obj) {
115  int size = size_helper();
116  instanceKlass::oop_adjust_pointers(obj);
117
118  oop* referent_addr = java_lang_ref_Reference::referent_addr(obj);
119  MarkSweep::adjust_pointer(referent_addr);
120  oop* next_addr = java_lang_ref_Reference::next_addr(obj);
121  MarkSweep::adjust_pointer(next_addr);
122  oop* discovered_addr = java_lang_ref_Reference::discovered_addr(obj);
123  MarkSweep::adjust_pointer(discovered_addr);
124
125#ifdef ASSERT
126  if(TraceReferenceGC && PrintGCDetails) {
127    gclog_or_tty->print_cr("instanceRefKlass::oop_adjust_pointers obj "
128                           INTPTR_FORMAT, (address)obj);
129    gclog_or_tty->print_cr("     referent_addr/* " INTPTR_FORMAT " / "
130                           INTPTR_FORMAT, referent_addr,
131                           referent_addr ? (address)*referent_addr : NULL);
132    gclog_or_tty->print_cr("     next_addr/* " INTPTR_FORMAT " / "
133                           INTPTR_FORMAT, next_addr,
134                           next_addr ? (address)*next_addr : NULL);
135    gclog_or_tty->print_cr("     discovered_addr/* " INTPTR_FORMAT " / "
136                           INTPTR_FORMAT, discovered_addr,
137                           discovered_addr ? (address)*discovered_addr : NULL);
138  }
139#endif
140
141  return size;
142}
143
144#define InstanceRefKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix)        \
145                                                                                \
146int instanceRefKlass::                                                          \
147oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) {                  \
148  /* Get size before changing pointers */                                       \
149  SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\
150                                                                                \
151  int size = instanceKlass::oop_oop_iterate##nv_suffix(obj, closure);           \
152                                                                                \
153  oop* referent_addr = java_lang_ref_Reference::referent_addr(obj);             \
154  oop referent = *referent_addr;                                                \
155  if (referent != NULL) {                                                       \
156    ReferenceProcessor* rp = closure->_ref_processor;                           \
157    if (!referent->is_gc_marked() && (rp != NULL) &&                            \
158        rp->discover_reference(obj, reference_type())) {              \
159      return size;                                                              \
160    } else {                                                                    \
161      /* treat referent as normal oop */                                        \
162      SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk);\
163      closure->do_oop##nv_suffix(referent_addr);                                \
164    }                                                                           \
165  }                                                                             \
166                                                                                \
167  /* treat next as normal oop */                                                \
168  oop* next_addr = java_lang_ref_Reference::next_addr(obj);                     \
169  SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk); \
170  closure->do_oop##nv_suffix(next_addr);                                        \
171  return size;                                                                  \
172}
173
174#define InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix)      \
175                                                                                \
176int instanceRefKlass::                                                          \
177oop_oop_iterate##nv_suffix##_m(oop obj,                                         \
178                               OopClosureType* closure,                         \
179                               MemRegion mr) {                                  \
180  SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\
181                                                                                \
182  int size = instanceKlass::oop_oop_iterate##nv_suffix##_m(obj, closure, mr);   \
183                                                                                \
184  oop* referent_addr = java_lang_ref_Reference::referent_addr(obj);             \
185  oop referent = *referent_addr;                                                \
186  if (referent != NULL && mr.contains(referent_addr)) {                         \
187    ReferenceProcessor* rp = closure->_ref_processor;                           \
188    if (!referent->is_gc_marked() && (rp != NULL) &&                            \
189        rp->discover_reference(obj, reference_type())) {              \
190      return size;                                                              \
191    } else {                                                                    \
192      /* treat referent as normal oop */                                        \
193      SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk);\
194      closure->do_oop##nv_suffix(referent_addr);                                \
195    }                                                                           \
196  }                                                                             \
197                                                                                \
198  /* treat next as normal oop */                                                \
199  oop* next_addr = java_lang_ref_Reference::next_addr(obj);                     \
200  if (mr.contains(next_addr)) {                                                 \
201    SpecializationStats::record_do_oop_call##nv_suffix(SpecializationStats::irk);\
202    closure->do_oop##nv_suffix(next_addr);                                      \
203  }                                                                             \
204  return size;                                                                  \
205}
206
207ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_DEFN)
208ALL_OOP_OOP_ITERATE_CLOSURES_3(InstanceRefKlass_OOP_OOP_ITERATE_DEFN)
209ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m)
210ALL_OOP_OOP_ITERATE_CLOSURES_3(InstanceRefKlass_OOP_OOP_ITERATE_DEFN_m)
211
212
213#ifndef SERIALGC
214void instanceRefKlass::oop_copy_contents(PSPromotionManager* pm, oop obj) {
215  assert(!pm->depth_first(), "invariant");
216  oop* referent_addr = java_lang_ref_Reference::referent_addr(obj);
217  if (PSScavenge::should_scavenge(*referent_addr)) {
218    ReferenceProcessor* rp = PSScavenge::reference_processor();
219    if (rp->discover_reference(obj, reference_type())) {
220      // reference already enqueued, referent and next will be traversed later
221      instanceKlass::oop_copy_contents(pm, obj);
222      return;
223    } else {
224      // treat referent as normal oop
225      pm->claim_or_forward_breadth(referent_addr);
226    }
227  }
228  // treat next as normal oop
229  oop* next_addr = java_lang_ref_Reference::next_addr(obj);
230  if (PSScavenge::should_scavenge(*next_addr)) {
231    pm->claim_or_forward_breadth(next_addr);
232  }
233  instanceKlass::oop_copy_contents(pm, obj);
234}
235
236void instanceRefKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
237  assert(pm->depth_first(), "invariant");
238  oop* referent_addr = java_lang_ref_Reference::referent_addr(obj);
239  if (PSScavenge::should_scavenge(*referent_addr)) {
240    ReferenceProcessor* rp = PSScavenge::reference_processor();
241    if (rp->discover_reference(obj, reference_type())) {
242      // reference already enqueued, referent and next will be traversed later
243      instanceKlass::oop_push_contents(pm, obj);
244      return;
245    } else {
246      // treat referent as normal oop
247      pm->claim_or_forward_depth(referent_addr);
248    }
249  }
250  // treat next as normal oop
251  oop* next_addr = java_lang_ref_Reference::next_addr(obj);
252  if (PSScavenge::should_scavenge(*next_addr)) {
253    pm->claim_or_forward_depth(next_addr);
254  }
255  instanceKlass::oop_push_contents(pm, obj);
256}
257
258int instanceRefKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
259  instanceKlass::oop_update_pointers(cm, obj);
260
261  oop* referent_addr = java_lang_ref_Reference::referent_addr(obj);
262  PSParallelCompact::adjust_pointer(referent_addr);
263  oop* next_addr = java_lang_ref_Reference::next_addr(obj);
264  PSParallelCompact::adjust_pointer(next_addr);
265  oop* discovered_addr = java_lang_ref_Reference::discovered_addr(obj);
266  PSParallelCompact::adjust_pointer(discovered_addr);
267
268#ifdef ASSERT
269  if(TraceReferenceGC && PrintGCDetails) {
270    gclog_or_tty->print_cr("instanceRefKlass::oop_update_pointers obj "
271                           INTPTR_FORMAT, (oopDesc*) obj);
272    gclog_or_tty->print_cr("     referent_addr/* " INTPTR_FORMAT " / "
273                           INTPTR_FORMAT, referent_addr,
274                           referent_addr ? (oopDesc*) *referent_addr : NULL);
275    gclog_or_tty->print_cr("     next_addr/* " INTPTR_FORMAT " / "
276                           INTPTR_FORMAT, next_addr,
277                           next_addr ? (oopDesc*) *next_addr : NULL);
278    gclog_or_tty->print_cr("     discovered_addr/* " INTPTR_FORMAT " / "
279                   INTPTR_FORMAT, discovered_addr,
280                   discovered_addr ? (oopDesc*) *discovered_addr : NULL);
281  }
282#endif
283
284  return size_helper();
285}
286
287int
288instanceRefKlass::oop_update_pointers(ParCompactionManager* cm, oop obj,
289                                      HeapWord* beg_addr, HeapWord* end_addr) {
290  instanceKlass::oop_update_pointers(cm, obj, beg_addr, end_addr);
291
292  oop* p;
293  oop* referent_addr = p = java_lang_ref_Reference::referent_addr(obj);
294  PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
295  oop* next_addr = p = java_lang_ref_Reference::next_addr(obj);
296  PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
297  oop* discovered_addr = p = java_lang_ref_Reference::discovered_addr(obj);
298  PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
299
300#ifdef ASSERT
301  if(TraceReferenceGC && PrintGCDetails) {
302    gclog_or_tty->print_cr("instanceRefKlass::oop_update_pointers obj "
303                           INTPTR_FORMAT, (oopDesc*) obj);
304    gclog_or_tty->print_cr("     referent_addr/* " INTPTR_FORMAT " / "
305                           INTPTR_FORMAT, referent_addr,
306                           referent_addr ? (oopDesc*) *referent_addr : NULL);
307    gclog_or_tty->print_cr("     next_addr/* " INTPTR_FORMAT " / "
308                           INTPTR_FORMAT, next_addr,
309                           next_addr ? (oopDesc*) *next_addr : NULL);
310    gclog_or_tty->print_cr("     discovered_addr/* " INTPTR_FORMAT " / "
311                   INTPTR_FORMAT, discovered_addr,
312                   discovered_addr ? (oopDesc*) *discovered_addr : NULL);
313  }
314#endif
315
316  return size_helper();
317}
318#endif // SERIALGC
319
320void instanceRefKlass::update_nonstatic_oop_maps(klassOop k) {
321  // Clear the nonstatic oop-map entries corresponding to referent
322  // and nextPending field.  They are treated specially by the
323  // garbage collector.
324  // The discovered field is used only by the garbage collector
325  // and is also treated specially.
326  instanceKlass* ik = instanceKlass::cast(k);
327
328  // Check that we have the right class
329  debug_only(static bool first_time = true);
330  assert(k == SystemDictionary::reference_klass() && first_time,
331         "Invalid update of maps");
332  debug_only(first_time = false);
333  assert(ik->nonstatic_oop_map_size() == 1, "just checking");
334
335  OopMapBlock* map = ik->start_of_nonstatic_oop_maps();
336
337  // Check that the current map is (2,4) - currently points at field with
338  // offset 2 (words) and has 4 map entries.
339  debug_only(int offset = java_lang_ref_Reference::referent_offset);
340  debug_only(int length = ((java_lang_ref_Reference::discovered_offset -
341    java_lang_ref_Reference::referent_offset)/wordSize) + 1);
342
343  if (UseSharedSpaces) {
344    assert(map->offset() == java_lang_ref_Reference::queue_offset &&
345           map->length() == 1, "just checking");
346  } else {
347    assert(map->offset() == offset && map->length() == length,
348           "just checking");
349
350    // Update map to (3,1) - point to offset of 3 (words) with 1 map entry.
351    map->set_offset(java_lang_ref_Reference::queue_offset);
352    map->set_length(1);
353  }
354}
355
356
357// Verification
358
359void instanceRefKlass::oop_verify_on(oop obj, outputStream* st) {
360  instanceKlass::oop_verify_on(obj, st);
361  // Verify referent field
362  oop referent = java_lang_ref_Reference::referent(obj);
363
364  // We should make this general to all heaps
365  GenCollectedHeap* gch = NULL;
366  if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap)
367    gch = GenCollectedHeap::heap();
368
369  if (referent != NULL) {
370    guarantee(referent->is_oop(), "referent field heap failed");
371    if (gch != NULL && !gch->is_in_youngest(obj))
372      // We do a specific remembered set check here since the referent
373      // field is not part of the oop mask and therefore skipped by the
374      // regular verify code.
375      obj->verify_old_oop(java_lang_ref_Reference::referent_addr(obj), true);
376  }
377  // Verify next field
378  oop next = java_lang_ref_Reference::next(obj);
379  if (next != NULL) {
380    guarantee(next->is_oop(), "next field verify failed");
381    guarantee(next->is_instanceRef(), "next field verify failed");
382    if (gch != NULL && !gch->is_in_youngest(obj)) {
383      // We do a specific remembered set check here since the next field is
384      // not part of the oop mask and therefore skipped by the regular
385      // verify code.
386      obj->verify_old_oop(java_lang_ref_Reference::next_addr(obj), true);
387    }
388  }
389}
390
391void instanceRefKlass::acquire_pending_list_lock(BasicLock *pending_list_basic_lock) {
392  // we may enter this with pending exception set
393  PRESERVE_EXCEPTION_MARK;  // exceptions are never thrown, needed for TRAPS argument
394  Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());
395  ObjectSynchronizer::fast_enter(h_lock, pending_list_basic_lock, false, THREAD);
396  assert(ObjectSynchronizer::current_thread_holds_lock(
397           JavaThread::current(), h_lock),
398         "Locking should have succeeded");
399  if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;
400}
401
402void instanceRefKlass::release_and_notify_pending_list_lock(
403  BasicLock *pending_list_basic_lock) {
404  // we may enter this with pending exception set
405  PRESERVE_EXCEPTION_MARK;  // exceptions are never thrown, needed for TRAPS argument
406  //
407  Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());
408  assert(ObjectSynchronizer::current_thread_holds_lock(
409           JavaThread::current(), h_lock),
410         "Lock should be held");
411  // Notify waiters on pending lists lock if there is any reference.
412  if (java_lang_ref_Reference::pending_list() != NULL) {
413    ObjectSynchronizer::notifyall(h_lock, THREAD);
414  }
415  ObjectSynchronizer::fast_exit(h_lock(), pending_list_basic_lock, THREAD);
416  if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;
417}
418