hotspot/src/share/vm/gc_implementation/g1/concurrentZFThread.cpp
changeset 7923 fc200fcd4e05
parent 7922 e97540c35e38
child 7924 6cbca8cebf93
equal deleted inserted replaced
7922:e97540c35e38 7923:fc200fcd4e05
     1 /*
       
     2  * Copyright (c) 2001, 2010, 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  */
       
    24 
       
    25 #include "precompiled.hpp"
       
    26 #include "gc_implementation/g1/concurrentZFThread.hpp"
       
    27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
       
    28 #include "gc_implementation/g1/heapRegion.hpp"
       
    29 #include "memory/space.inline.hpp"
       
    30 #include "runtime/mutexLocker.hpp"
       
    31 #include "utilities/copy.hpp"
       
    32 
       
    33 // ======= Concurrent Zero-Fill Thread ========
       
    34 
       
    35 // The CM thread is created when the G1 garbage collector is used
       
    36 
       
    37 int ConcurrentZFThread::_region_allocs = 0;
       
    38 int ConcurrentZFThread::_sync_zfs = 0;
       
    39 int ConcurrentZFThread::_zf_waits = 0;
       
    40 int ConcurrentZFThread::_regions_filled = 0;
       
    41 
       
    42 ConcurrentZFThread::ConcurrentZFThread() :
       
    43   ConcurrentGCThread()
       
    44 {
       
    45   create_and_start();
       
    46 }
       
    47 
       
    48 void ConcurrentZFThread::wait_for_ZF_completed(HeapRegion* hr) {
       
    49   assert(ZF_mon->owned_by_self(), "Precondition.");
       
    50   note_zf_wait();
       
    51   while (hr->zero_fill_state() == HeapRegion::ZeroFilling) {
       
    52     ZF_mon->wait(Mutex::_no_safepoint_check_flag);
       
    53   }
       
    54 }
       
    55 
       
    56 void ConcurrentZFThread::processHeapRegion(HeapRegion* hr) {
       
    57   assert(!Universe::heap()->is_gc_active(),
       
    58          "This should not happen during GC.");
       
    59   assert(hr != NULL, "Precondition");
       
    60   // These are unlocked reads, but if this test is successful, then no
       
    61   // other thread will attempt this zero filling.  Only a GC thread can
       
    62   // modify the ZF state of a region whose state is zero-filling, and this
       
    63   // should only happen while the ZF thread is locking out GC.
       
    64   if (hr->zero_fill_state() == HeapRegion::ZeroFilling
       
    65       && hr->zero_filler() == Thread::current()) {
       
    66     assert(hr->top() == hr->bottom(), "better be empty!");
       
    67     assert(!hr->isHumongous(), "Only free regions on unclean list.");
       
    68     Copy::fill_to_words(hr->bottom(), hr->capacity()/HeapWordSize);
       
    69     note_region_filled();
       
    70   }
       
    71 }
       
    72 
       
    73 void ConcurrentZFThread::run() {
       
    74   initialize_in_thread();
       
    75   Thread* thr_self = Thread::current();
       
    76   _vtime_start = os::elapsedVTime();
       
    77   wait_for_universe_init();
       
    78 
       
    79   G1CollectedHeap* g1 = G1CollectedHeap::heap();
       
    80   _sts.join();
       
    81   while (!_should_terminate) {
       
    82     _sts.leave();
       
    83 
       
    84     {
       
    85       MutexLockerEx x(ZF_mon, Mutex::_no_safepoint_check_flag);
       
    86 
       
    87       // This local variable will hold a region being zero-filled.  This
       
    88       // region will neither be on the unclean or zero-filled lists, and
       
    89       // will not be available for allocation; thus, we might have an
       
    90       // allocation fail, causing a full GC, because of this, but this is a
       
    91       // price we will pay.  (In future, we might want to make the fact
       
    92       // that there's a region being zero-filled apparent to the G1 heap,
       
    93       // which could then wait for it in this extreme case...)
       
    94       HeapRegion* to_fill;
       
    95 
       
    96       while (!g1->should_zf()
       
    97              || (to_fill = g1->pop_unclean_region_list_locked()) == NULL)
       
    98         ZF_mon->wait(Mutex::_no_safepoint_check_flag);
       
    99       while (to_fill->zero_fill_state() == HeapRegion::ZeroFilling)
       
   100         ZF_mon->wait(Mutex::_no_safepoint_check_flag);
       
   101 
       
   102       // So now to_fill is non-NULL and is not ZeroFilling.  It might be
       
   103       // Allocated or ZeroFilled.  (The latter could happen if this thread
       
   104       // starts the zero-filling of a region, but a GC intervenes and
       
   105       // pushes new regions needing on the front of the filling on the
       
   106       // front of the list.)
       
   107 
       
   108       switch (to_fill->zero_fill_state()) {
       
   109       case HeapRegion::Allocated:
       
   110         to_fill = NULL;
       
   111         break;
       
   112 
       
   113       case HeapRegion::NotZeroFilled:
       
   114         to_fill->set_zero_fill_in_progress(thr_self);
       
   115 
       
   116         ZF_mon->unlock();
       
   117         _sts.join();
       
   118         processHeapRegion(to_fill);
       
   119         _sts.leave();
       
   120         ZF_mon->lock_without_safepoint_check();
       
   121 
       
   122         if (to_fill->zero_fill_state() == HeapRegion::ZeroFilling
       
   123             && to_fill->zero_filler() == thr_self) {
       
   124           to_fill->set_zero_fill_complete();
       
   125           (void)g1->put_free_region_on_list_locked(to_fill);
       
   126         }
       
   127         break;
       
   128 
       
   129       case HeapRegion::ZeroFilled:
       
   130         (void)g1->put_free_region_on_list_locked(to_fill);
       
   131         break;
       
   132 
       
   133       case HeapRegion::ZeroFilling:
       
   134         ShouldNotReachHere();
       
   135         break;
       
   136       }
       
   137     }
       
   138     _vtime_accum = (os::elapsedVTime() - _vtime_start);
       
   139     _sts.join();
       
   140   }
       
   141   _sts.leave();
       
   142 
       
   143   assert(_should_terminate, "just checking");
       
   144   terminate();
       
   145 }
       
   146 
       
   147 bool ConcurrentZFThread::offer_yield() {
       
   148   if (_sts.should_yield()) {
       
   149     _sts.yield("Concurrent ZF");
       
   150     return true;
       
   151   } else {
       
   152     return false;
       
   153   }
       
   154 }
       
   155 
       
   156 void ConcurrentZFThread::stop() {
       
   157   // it is ok to take late safepoints here, if needed
       
   158   MutexLockerEx mu(Terminator_lock);
       
   159   _should_terminate = true;
       
   160   while (!_has_terminated) {
       
   161     Terminator_lock->wait();
       
   162   }
       
   163 }
       
   164 
       
   165 void ConcurrentZFThread::print() const {
       
   166   print_on(tty);
       
   167 }
       
   168 
       
   169 void ConcurrentZFThread::print_on(outputStream* st) const {
       
   170   st->print("\"G1 Concurrent Zero-Fill Thread\" ");
       
   171   Thread::print_on(st);
       
   172   st->cr();
       
   173 }
       
   174 
       
   175 
       
   176 double ConcurrentZFThread::_vtime_accum;
       
   177 
       
   178 void ConcurrentZFThread::print_summary_info() {
       
   179   gclog_or_tty->print("\nConcurrent Zero-Filling:\n");
       
   180   gclog_or_tty->print("  Filled %d regions, used %5.2fs.\n",
       
   181                       _regions_filled,
       
   182                       vtime_accum());
       
   183   gclog_or_tty->print("  Of %d region allocs, %d (%5.2f%%) required sync ZF,\n",
       
   184                       _region_allocs, _sync_zfs,
       
   185                       (_region_allocs > 0 ?
       
   186                        (float)_sync_zfs/(float)_region_allocs*100.0 :
       
   187                        0.0));
       
   188   gclog_or_tty->print("     and %d (%5.2f%%) required a ZF wait.\n",
       
   189                       _zf_waits,
       
   190                       (_region_allocs > 0 ?
       
   191                        (float)_zf_waits/(float)_region_allocs*100.0 :
       
   192                        0.0));
       
   193 
       
   194 }