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