src/share/vm/memory/gcLocker.cpp

Tue, 24 Jun 2014 16:20:15 +0200

author
stefank
date
Tue, 24 Jun 2014 16:20:15 +0200
changeset 6982
4c1b88a53c74
parent 6911
ce8f6bb717c9
child 7535
7ae4e26cb1e0
child 9787
9f28a4cac6d9
permissions
-rw-r--r--

8046670: Make CMS metadata aware closures applicable for other collectors
Reviewed-by: ehelin, mgerdin

     1 /*
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. 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 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  */
    25 #include "precompiled.hpp"
    26 #include "memory/gcLocker.inline.hpp"
    27 #include "memory/resourceArea.hpp"
    28 #include "memory/sharedHeap.hpp"
    29 #include "runtime/thread.inline.hpp"
    31 volatile jint GC_locker::_jni_lock_count = 0;
    32 volatile bool GC_locker::_needs_gc       = false;
    33 volatile bool GC_locker::_doing_gc       = false;
    35 #ifdef ASSERT
    36 volatile jint GC_locker::_debug_jni_lock_count = 0;
    37 #endif
    40 #ifdef ASSERT
    41 void GC_locker::verify_critical_count() {
    42   if (SafepointSynchronize::is_at_safepoint()) {
    43     assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree");
    44     int count = 0;
    45     // Count the number of threads with critical operations in progress
    46     for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) {
    47       if (thr->in_critical()) {
    48         count++;
    49       }
    50     }
    51     if (_jni_lock_count != count) {
    52       tty->print_cr("critical counts don't match: %d != %d", _jni_lock_count, count);
    53       for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) {
    54         if (thr->in_critical()) {
    55           tty->print_cr(INTPTR_FORMAT " in_critical %d", p2i(thr), thr->in_critical());
    56         }
    57       }
    58     }
    59     assert(_jni_lock_count == count, "must be equal");
    60   }
    61 }
    62 #endif
    64 bool GC_locker::check_active_before_gc() {
    65   assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint");
    66   if (is_active() && !_needs_gc) {
    67     verify_critical_count();
    68     _needs_gc = true;
    69     if (PrintJNIGCStalls && PrintGCDetails) {
    70       ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
    71       gclog_or_tty->print_cr("%.3f: Setting _needs_gc. Thread \"%s\" %d locked.",
    72                              gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count);
    73     }
    75   }
    76   return is_active();
    77 }
    79 void GC_locker::stall_until_clear() {
    80   assert(!JavaThread::current()->in_critical(), "Would deadlock");
    81   MutexLocker   ml(JNICritical_lock);
    83   if (needs_gc()) {
    84     if (PrintJNIGCStalls && PrintGCDetails) {
    85       ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
    86       gclog_or_tty->print_cr("%.3f: Allocation failed. Thread \"%s\" is stalled by JNI critical section, %d locked.",
    87                              gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count);
    88     }
    89   }
    91   // Wait for _needs_gc  to be cleared
    92   while (needs_gc()) {
    93     JNICritical_lock->wait();
    94   }
    95 }
    97 void GC_locker::jni_lock(JavaThread* thread) {
    98   assert(!thread->in_critical(), "shouldn't currently be in a critical region");
    99   MutexLocker mu(JNICritical_lock);
   100   // Block entering threads if we know at least one thread is in a
   101   // JNI critical region and we need a GC.
   102   // We check that at least one thread is in a critical region before
   103   // blocking because blocked threads are woken up by a thread exiting
   104   // a JNI critical region.
   105   while (is_active_and_needs_gc() || _doing_gc) {
   106     JNICritical_lock->wait();
   107   }
   108   thread->enter_critical();
   109   _jni_lock_count++;
   110   increment_debug_jni_lock_count();
   111 }
   113 void GC_locker::jni_unlock(JavaThread* thread) {
   114   assert(thread->in_last_critical(), "should be exiting critical region");
   115   MutexLocker mu(JNICritical_lock);
   116   _jni_lock_count--;
   117   decrement_debug_jni_lock_count();
   118   thread->exit_critical();
   119   if (needs_gc() && !is_active_internal()) {
   120     // We're the last thread out. Cause a GC to occur.
   121     _doing_gc = true;
   122     {
   123       // Must give up the lock while at a safepoint
   124       MutexUnlocker munlock(JNICritical_lock);
   125       if (PrintJNIGCStalls && PrintGCDetails) {
   126         ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
   127         gclog_or_tty->print_cr("%.3f: Thread \"%s\" is performing GC after exiting critical section, %d locked",
   128             gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count);
   129       }
   130       Universe::heap()->collect(GCCause::_gc_locker);
   131     }
   132     _doing_gc = false;
   133     _needs_gc = false;
   134     JNICritical_lock->notify_all();
   135   }
   136 }
   138 // Implementation of No_GC_Verifier
   140 #ifdef ASSERT
   142 No_GC_Verifier::No_GC_Verifier(bool verifygc) {
   143   _verifygc = verifygc;
   144   if (_verifygc) {
   145     CollectedHeap* h = Universe::heap();
   146     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
   147     _old_invocations = h->total_collections();
   148   }
   149 }
   152 No_GC_Verifier::~No_GC_Verifier() {
   153   if (_verifygc) {
   154     CollectedHeap* h = Universe::heap();
   155     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
   156     if (_old_invocations != h->total_collections()) {
   157       fatal("collection in a No_GC_Verifier secured function");
   158     }
   159   }
   160 }
   162 Pause_No_GC_Verifier::Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {
   163   _ngcv = ngcv;
   164   if (_ngcv->_verifygc) {
   165     // if we were verifying, then make sure that nothing is
   166     // wrong before we "pause" verification
   167     CollectedHeap* h = Universe::heap();
   168     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
   169     if (_ngcv->_old_invocations != h->total_collections()) {
   170       fatal("collection in a No_GC_Verifier secured function");
   171     }
   172   }
   173 }
   176 Pause_No_GC_Verifier::~Pause_No_GC_Verifier() {
   177   if (_ngcv->_verifygc) {
   178     // if we were verifying before, then reenable verification
   179     CollectedHeap* h = Universe::heap();
   180     assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
   181     _ngcv->_old_invocations = h->total_collections();
   182   }
   183 }
   186 // JRT_LEAF rules:
   187 // A JRT_LEAF method may not interfere with safepointing by
   188 //   1) acquiring or blocking on a Mutex or JavaLock - checked
   189 //   2) allocating heap memory - checked
   190 //   3) executing a VM operation - checked
   191 //   4) executing a system call (including malloc) that could block or grab a lock
   192 //   5) invoking GC
   193 //   6) reaching a safepoint
   194 //   7) running too long
   195 // Nor may any method it calls.
   196 JRT_Leaf_Verifier::JRT_Leaf_Verifier()
   197   : No_Safepoint_Verifier(true, JRT_Leaf_Verifier::should_verify_GC())
   198 {
   199 }
   201 JRT_Leaf_Verifier::~JRT_Leaf_Verifier()
   202 {
   203 }
   205 bool JRT_Leaf_Verifier::should_verify_GC() {
   206   switch (JavaThread::current()->thread_state()) {
   207   case _thread_in_Java:
   208     // is in a leaf routine, there must be no safepoint.
   209     return true;
   210   case _thread_in_native:
   211     // A native thread is not subject to safepoints.
   212     // Even while it is in a leaf routine, GC is ok
   213     return false;
   214   default:
   215     // Leaf routines cannot be called from other contexts.
   216     ShouldNotReachHere();
   217     return false;
   218   }
   219 }
   220 #endif

mercurial