18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.inline.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "gc/shared/collectedHeap.hpp"
29 #include "gc/shared/collectedHeap.inline.hpp"
30 #include "gc/shared/gcTimer.hpp"
31 #include "gc/shared/gcTraceTime.inline.hpp"
32 #include "gc/shared/referencePolicy.hpp"
33 #include "gc/shared/referenceProcessor.inline.hpp"
34 #include "logging/log.hpp"
35 #include "memory/allocation.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "oops/oop.inline.hpp"
38 #include "runtime/java.hpp"
39 #include "runtime/jniHandles.hpp"
40
41 ReferencePolicy* ReferenceProcessor::_always_clear_soft_ref_policy = NULL;
42 ReferencePolicy* ReferenceProcessor::_default_soft_ref_policy = NULL;
43 jlong ReferenceProcessor::_soft_ref_timestamp_clock = 0;
44
45 void referenceProcessor_init() {
46 ReferenceProcessor::init_statics();
47 }
48
49 void ReferenceProcessor::init_statics() {
50 // We need a monotonically non-decreasing time in ms but
51 // os::javaTimeMillis() does not guarantee monotonicity.
52 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
53
54 // Initialize the soft ref timestamp clock.
55 _soft_ref_timestamp_clock = now;
56 // Also update the soft ref clock in j.l.r.SoftReference
57 java_lang_ref_SoftReference::set_clock(_soft_ref_timestamp_clock);
241 // Phantom references
242 {
243 RefProcPhaseTimesTracker tt(REF_PHANTOM, phase_times, this);
244 process_discovered_reflist(_discoveredPhantomRefs, NULL, true,
245 is_alive, keep_alive, complete_gc, task_executor, phase_times);
246 }
247
248 // Weak global JNI references. It would make more sense (semantically) to
249 // traverse these simultaneously with the regular weak references above, but
250 // that is not how the JDK1.2 specification is. See #4126360. Native code can
251 // thus use JNI weak references to circumvent the phantom references and
252 // resurrect a "post-mortem" object.
253 {
254 GCTraceTime(Debug, gc, ref) tt("JNI Weak Reference", phase_times->gc_timer());
255 if (task_executor != NULL) {
256 task_executor->set_single_threaded_mode();
257 }
258 process_phaseJNI(is_alive, keep_alive, complete_gc);
259 }
260
261 phase_times->set_total_time_ms((os::elapsedTime() - start_time) * 1000);
262
263 log_develop_trace(gc, ref)("JNI Weak Reference count: " SIZE_FORMAT, count_jni_refs());
264
265 return stats;
266 }
267
268 #ifndef PRODUCT
269 // Calculate the number of jni handles.
270 size_t ReferenceProcessor::count_jni_refs() {
271 class CountHandleClosure: public OopClosure {
272 private:
273 size_t _count;
274 public:
275 CountHandleClosure(): _count(0) {}
276 void do_oop(oop* unused) { _count++; }
277 void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
278 size_t count() { return _count; }
279 };
280 CountHandleClosure global_handle_count;
281 JNIHandles::weak_oops_do(&global_handle_count);
282 return global_handle_count.count();
283 }
284 #endif
285
286 void ReferenceProcessor::process_phaseJNI(BoolObjectClosure* is_alive,
287 OopClosure* keep_alive,
288 VoidClosure* complete_gc) {
289 JNIHandles::weak_oops_do(is_alive, keep_alive);
290 complete_gc->do_void();
291 }
292
293 void ReferenceProcessor::enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor,
294 ReferenceProcessorPhaseTimes* phase_times) {
295 // Enqueue references that are not made active again, and
296 // clear the decks for the next collection (cycle).
297 enqueue_discovered_reflists(task_executor, phase_times);
298
299 // Stop treating discovered references specially.
300 disable_discovery();
301 }
302
303 void ReferenceProcessor::enqueue_discovered_reflist(DiscoveredList& refs_list) {
304 // Given a list of refs linked through the "discovered" field
305 // (java.lang.ref.Reference.discovered), self-loop their "next" field
306 // thus distinguishing them from active References, then
307 // prepend them to the pending list.
308 //
309 // The Java threads will see the Reference objects linked together through
310 // the discovered field. Instead of trying to do the write barrier updates
|
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/javaClasses.inline.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "gc/shared/collectedHeap.hpp"
29 #include "gc/shared/collectedHeap.inline.hpp"
30 #include "gc/shared/gcTimer.hpp"
31 #include "gc/shared/gcTraceTime.inline.hpp"
32 #include "gc/shared/referencePolicy.hpp"
33 #include "gc/shared/referenceProcessor.inline.hpp"
34 #include "logging/log.hpp"
35 #include "memory/allocation.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "oops/oop.inline.hpp"
38 #include "runtime/heapMonitoring.hpp"
39 #include "runtime/java.hpp"
40 #include "runtime/jniHandles.hpp"
41
42 ReferencePolicy* ReferenceProcessor::_always_clear_soft_ref_policy = NULL;
43 ReferencePolicy* ReferenceProcessor::_default_soft_ref_policy = NULL;
44 jlong ReferenceProcessor::_soft_ref_timestamp_clock = 0;
45
46 void referenceProcessor_init() {
47 ReferenceProcessor::init_statics();
48 }
49
50 void ReferenceProcessor::init_statics() {
51 // We need a monotonically non-decreasing time in ms but
52 // os::javaTimeMillis() does not guarantee monotonicity.
53 jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
54
55 // Initialize the soft ref timestamp clock.
56 _soft_ref_timestamp_clock = now;
57 // Also update the soft ref clock in j.l.r.SoftReference
58 java_lang_ref_SoftReference::set_clock(_soft_ref_timestamp_clock);
242 // Phantom references
243 {
244 RefProcPhaseTimesTracker tt(REF_PHANTOM, phase_times, this);
245 process_discovered_reflist(_discoveredPhantomRefs, NULL, true,
246 is_alive, keep_alive, complete_gc, task_executor, phase_times);
247 }
248
249 // Weak global JNI references. It would make more sense (semantically) to
250 // traverse these simultaneously with the regular weak references above, but
251 // that is not how the JDK1.2 specification is. See #4126360. Native code can
252 // thus use JNI weak references to circumvent the phantom references and
253 // resurrect a "post-mortem" object.
254 {
255 GCTraceTime(Debug, gc, ref) tt("JNI Weak Reference", phase_times->gc_timer());
256 if (task_executor != NULL) {
257 task_executor->set_single_threaded_mode();
258 }
259 process_phaseJNI(is_alive, keep_alive, complete_gc);
260 }
261
262 // Heap Monitoring references
263 size_t handled;
264 {
265 GCTraceTime(Debug, gc, ref) tt("Heap Sampler Weak Reference", phase_times->gc_timer());
266 handled = process_phaseHeapSampling(is_alive, keep_alive, complete_gc);
267 }
268
269 phase_times->set_total_time_ms((os::elapsedTime() - start_time) * 1000);
270
271 log_develop_trace(gc, ref)("JNI Weak Reference count: " SIZE_FORMAT, count_jni_refs());
272 log_develop_trace(gc, ref)("Heap Sampler Weak Reference handled: " SIZE_FORMAT, handled);
273
274 return stats;
275 }
276
277 #ifndef PRODUCT
278 // Calculate the number of jni handles.
279 size_t ReferenceProcessor::count_jni_refs() {
280 class CountHandleClosure: public OopClosure {
281 private:
282 size_t _count;
283 public:
284 CountHandleClosure(): _count(0) {}
285 void do_oop(oop* unused) { _count++; }
286 void do_oop(narrowOop* unused) { ShouldNotReachHere(); }
287 size_t count() { return _count; }
288 };
289 CountHandleClosure global_handle_count;
290 JNIHandles::weak_oops_do(&global_handle_count);
291 return global_handle_count.count();
292 }
293 #endif
294
295 void ReferenceProcessor::process_phaseJNI(BoolObjectClosure* is_alive,
296 OopClosure* keep_alive,
297 VoidClosure* complete_gc) {
298 JNIHandles::weak_oops_do(is_alive, keep_alive);
299 complete_gc->do_void();
300 }
301
302 size_t ReferenceProcessor::process_phaseHeapSampling(
303 BoolObjectClosure* is_alive,
304 OopClosure* keep_alive,
305 VoidClosure* complete_gc) {
306 size_t count = 0;
307 if (HeapMonitoring::enabled()) {
308 count = HeapMonitoring::weak_oops_do(is_alive, keep_alive);
309 complete_gc->do_void();
310 }
311 return count;
312 }
313
314 void ReferenceProcessor::enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor,
315 ReferenceProcessorPhaseTimes* phase_times) {
316 // Enqueue references that are not made active again, and
317 // clear the decks for the next collection (cycle).
318 enqueue_discovered_reflists(task_executor, phase_times);
319
320 // Stop treating discovered references specially.
321 disable_discovery();
322 }
323
324 void ReferenceProcessor::enqueue_discovered_reflist(DiscoveredList& refs_list) {
325 // Given a list of refs linked through the "discovered" field
326 // (java.lang.ref.Reference.discovered), self-loop their "next" field
327 // thus distinguishing them from active References, then
328 // prepend them to the pending list.
329 //
330 // The Java threads will see the Reference objects linked together through
331 // the discovered field. Instead of trying to do the write barrier updates
|