comparison src/share/vm/memory/gcLocker.cpp @ 4867:1a2723f7ad8e

7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale Reviewed-by: kvn, iveresov, dholmes
author never
date Sun, 29 Jan 2012 16:46:04 -0800
parents f95d63e2154a
children 0382d2b469b2
comparison
equal deleted inserted replaced
4826:072384a61312 4867:1a2723f7ad8e
1 /* 1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
29 29
30 volatile jint GC_locker::_jni_lock_count = 0; 30 volatile jint GC_locker::_jni_lock_count = 0;
31 volatile jint GC_locker::_lock_count = 0; 31 volatile jint GC_locker::_lock_count = 0;
32 volatile bool GC_locker::_needs_gc = false; 32 volatile bool GC_locker::_needs_gc = false;
33 volatile bool GC_locker::_doing_gc = false; 33 volatile bool GC_locker::_doing_gc = false;
34 jlong GC_locker::_wait_begin = 0;
35
36 #ifdef ASSERT
37 volatile jint GC_locker::_debug_jni_lock_count = 0;
38 #endif
39
40
41 #ifdef ASSERT
42 void GC_locker::verify_critical_count() {
43 if (SafepointSynchronize::is_at_safepoint()) {
44 assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree");
45 int count = 0;
46 // Count the number of threads with critical operations in progress
47 for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) {
48 if (thr->in_critical()) {
49 count++;
50 }
51 }
52 if (_jni_lock_count != count) {
53 tty->print_cr("critical counts don't match: %d != %d", _jni_lock_count, count);
54 for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) {
55 if (thr->in_critical()) {
56 tty->print_cr(INTPTR_FORMAT " in_critical %d", thr, thr->in_critical());
57 }
58 }
59 }
60 assert(_jni_lock_count == count, "must be equal");
61 }
62 }
63 #endif
64
65 bool GC_locker::check_active_before_gc() {
66 assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint");
67 if (is_active() && !_needs_gc) {
68 verify_critical_count();
69 _needs_gc = true;
70 if (PrintJNIGCStalls && PrintGCDetails) {
71 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
72 _wait_begin = tty->time_stamp().milliseconds();
73 gclog_or_tty->print_cr(INT64_FORMAT ": Setting _needs_gc. Thread \"%s\" %d locked.",
74 _wait_begin, Thread::current()->name(), _jni_lock_count);
75 }
76
77 }
78 return is_active();
79 }
34 80
35 void GC_locker::stall_until_clear() { 81 void GC_locker::stall_until_clear() {
36 assert(!JavaThread::current()->in_critical(), "Would deadlock"); 82 assert(!JavaThread::current()->in_critical(), "Would deadlock");
37 if (PrintJNIGCStalls && PrintGCDetails) {
38 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
39 gclog_or_tty->print_cr(
40 "Allocation failed. Thread \"%s\" is stalled by JNI critical section.",
41 JavaThread::current()->name());
42 }
43 MutexLocker ml(JNICritical_lock); 83 MutexLocker ml(JNICritical_lock);
84
85 if (needs_gc()) {
86 if (PrintJNIGCStalls && PrintGCDetails) {
87 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
88 gclog_or_tty->print_cr(INT64_FORMAT ": Allocation failed. Thread \"%s\" is stalled by JNI critical section, %d locked.",
89 tty->time_stamp().milliseconds() - _wait_begin, Thread::current()->name(), _jni_lock_count);
90 }
91 }
92
44 // Wait for _needs_gc to be cleared 93 // Wait for _needs_gc to be cleared
45 while (GC_locker::needs_gc()) { 94 while (needs_gc()) {
46 JNICritical_lock->wait(); 95 JNICritical_lock->wait();
47 } 96 }
48 } 97 }
49 98
50 void GC_locker::jni_lock_slow() { 99 void GC_locker::jni_lock(JavaThread* thread) {
100 assert(!thread->in_critical(), "shouldn't currently be in a critical region");
51 MutexLocker mu(JNICritical_lock); 101 MutexLocker mu(JNICritical_lock);
52 // Block entering threads if we know at least one thread is in a 102 // Block entering threads if we know at least one thread is in a
53 // JNI critical region and we need a GC. 103 // JNI critical region and we need a GC.
54 // We check that at least one thread is in a critical region before 104 // We check that at least one thread is in a critical region before
55 // blocking because blocked threads are woken up by a thread exiting 105 // blocking because blocked threads are woken up by a thread exiting
56 // a JNI critical region. 106 // a JNI critical region.
57 while ((is_jni_active() && needs_gc()) || _doing_gc) { 107 while ((needs_gc() && is_jni_active()) || _doing_gc) {
58 JNICritical_lock->wait(); 108 JNICritical_lock->wait();
59 } 109 }
60 jni_lock(); 110 thread->enter_critical();
61 } 111 _jni_lock_count++;
62 112 increment_debug_jni_lock_count();
63 void GC_locker::jni_unlock_slow() { 113 }
114
115 void GC_locker::jni_unlock(JavaThread* thread) {
116 assert(thread->in_last_critical(), "should be exiting critical region");
64 MutexLocker mu(JNICritical_lock); 117 MutexLocker mu(JNICritical_lock);
65 jni_unlock(); 118 _jni_lock_count--;
119 decrement_debug_jni_lock_count();
120 thread->exit_critical();
66 if (needs_gc() && !is_jni_active()) { 121 if (needs_gc() && !is_jni_active()) {
67 // We're the last thread out. Cause a GC to occur. 122 // We're the last thread out. Cause a GC to occur.
68 // GC will also check is_active, so this check is not 123 // GC will also check is_active, so this check is not
69 // strictly needed. It's added here to make it clear that 124 // strictly needed. It's added here to make it clear that
70 // the GC will NOT be performed if any other caller 125 // the GC will NOT be performed if any other caller
72 if (!is_active()) { 127 if (!is_active()) {
73 _doing_gc = true; 128 _doing_gc = true;
74 { 129 {
75 // Must give up the lock while at a safepoint 130 // Must give up the lock while at a safepoint
76 MutexUnlocker munlock(JNICritical_lock); 131 MutexUnlocker munlock(JNICritical_lock);
132 if (PrintJNIGCStalls && PrintGCDetails) {
133 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
134 gclog_or_tty->print_cr(INT64_FORMAT ": Thread \"%s\" is performing GC after exiting critical section, %d locked",
135 tty->time_stamp().milliseconds() - _wait_begin, Thread::current()->name(), _jni_lock_count);
136 }
77 Universe::heap()->collect(GCCause::_gc_locker); 137 Universe::heap()->collect(GCCause::_gc_locker);
78 } 138 }
79 _doing_gc = false; 139 _doing_gc = false;
80 } 140 }
81 clear_needs_gc(); 141
142 _needs_gc = false;
82 JNICritical_lock->notify_all(); 143 JNICritical_lock->notify_all();
83 } 144 }
84 } 145 }
85 146
86 // Implementation of No_GC_Verifier 147 // Implementation of No_GC_Verifier