1 /*
   2  * Copyright (c) 2001, 2016, 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/g1/concurrentG1Refine.hpp"
  27 #include "gc/g1/concurrentG1RefineThread.hpp"
  28 #include "gc/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc/g1/g1CollectorPolicy.hpp"
  30 #include "gc/g1/suspendibleThreadSet.hpp"
  31 #include "logging/log.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "runtime/handles.inline.hpp"
  34 #include "runtime/mutexLocker.hpp"
  35 
  36 ConcurrentG1RefineThread::
  37 ConcurrentG1RefineThread(ConcurrentG1Refine* cg1r, ConcurrentG1RefineThread *next,
  38                          CardTableEntryClosure* refine_closure,
  39                          uint worker_id_offset, uint worker_id) :
  40   ConcurrentGCThread(),
  41   _refine_closure(refine_closure),
  42   _worker_id_offset(worker_id_offset),
  43   _worker_id(worker_id),
  44   _active(false),
  45   _next(next),
  46   _monitor(NULL),
  47   _cg1r(cg1r),
  48   _vtime_accum(0.0)
  49 {
  50 
  51   // Each thread has its own monitor. The i-th thread is responsible for signaling
  52   // to thread i+1 if the number of buffers in the queue exceeds a threshold for this
  53   // thread. Monitors are also used to wake up the threads during termination.
  54   // The 0th (primary) worker is notified by mutator threads and has a special monitor.
  55   if (!is_primary()) {
  56     _monitor = new Monitor(Mutex::nonleaf, "Refinement monitor", true,
  57                            Monitor::_safepoint_check_never);
  58   } else {
  59     _monitor = DirtyCardQ_CBL_mon;
  60   }
  61   initialize();
  62 
  63   // set name
  64   set_name("G1 Refine#%d", worker_id);
  65   create_and_start();
  66 }
  67 
  68 void ConcurrentG1RefineThread::initialize() {
  69   // Current thread activation threshold
  70   _threshold = MIN2<int>(cg1r()->thread_threshold_step() * (_worker_id + 1) + cg1r()->green_zone(),
  71                          cg1r()->yellow_zone());
  72   // A thread deactivates once the number of buffer reached a deactivation threshold
  73   _deactivation_threshold = MAX2<int>(_threshold - cg1r()->thread_threshold_step(), cg1r()->green_zone());
  74 }
  75 
  76 void ConcurrentG1RefineThread::wait_for_completed_buffers() {
  77   DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
  78   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  79   while (!_should_terminate && !is_active()) {
  80     _monitor->wait(Mutex::_no_safepoint_check_flag);
  81   }
  82 }
  83 
  84 bool ConcurrentG1RefineThread::is_active() {
  85   DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
  86   return is_primary() ? dcqs.process_completed_buffers() : _active;
  87 }
  88 
  89 void ConcurrentG1RefineThread::activate() {
  90   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  91   if (!is_primary()) {
  92     set_active(true);
  93   } else {
  94     DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
  95     dcqs.set_process_completed(true);
  96   }
  97   _monitor->notify();
  98 }
  99 
 100 void ConcurrentG1RefineThread::deactivate() {
 101   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
 102   if (!is_primary()) {
 103     set_active(false);
 104   } else {
 105     DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
 106     dcqs.set_process_completed(false);
 107   }
 108 }
 109 
 110 void ConcurrentG1RefineThread::run() {
 111   initialize_in_thread();
 112   wait_for_universe_init();
 113 
 114   run_service();
 115 
 116   terminate();
 117 }
 118 
 119 void ConcurrentG1RefineThread::run_service() {
 120   _vtime_start = os::elapsedVTime();
 121 
 122   while (!_should_terminate) {
 123     // Wait for work
 124     wait_for_completed_buffers();
 125     if (_should_terminate) {
 126       break;
 127     }
 128 
 129     DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
 130     log_debug(gc, refine)("Activated %d, on threshold: %d, current: %d",
 131                           _worker_id, _threshold, dcqs.completed_buffers_num());
 132 
 133     {
 134       SuspendibleThreadSetJoiner sts_join;
 135 
 136       do {
 137         int curr_buffer_num = (int)dcqs.completed_buffers_num();
 138         // If the number of the buffers falls down into the yellow zone,
 139         // that means that the transition period after the evacuation pause has ended.
 140         if (dcqs.completed_queue_padding() > 0 && curr_buffer_num <= cg1r()->yellow_zone()) {
 141           dcqs.set_completed_queue_padding(0);
 142         }
 143 
 144         // Check if we need to activate the next thread.
 145         if (_next != NULL && !_next->is_active() && curr_buffer_num > _next->_threshold) {
 146           _next->activate();
 147         }
 148       } while (dcqs.apply_closure_to_completed_buffer(_refine_closure,
 149                                                       _worker_id + _worker_id_offset,
 150                                                       _deactivation_threshold,
 151                                                       false /* during_pause */));
 152 
 153       deactivate();
 154       log_debug(gc, refine)("Deactivated %d, off threshold: %d, current: %d",
 155                             _worker_id, _deactivation_threshold,
 156                             dcqs.completed_buffers_num());
 157     }
 158 
 159     if (os::supports_vtime()) {
 160       _vtime_accum = (os::elapsedVTime() - _vtime_start);
 161     } else {
 162       _vtime_accum = 0.0;
 163     }
 164   }
 165 
 166   log_debug(gc, refine)("Stopping %d", _worker_id);
 167 }
 168 
 169 void ConcurrentG1RefineThread::stop() {
 170   // it is ok to take late safepoints here, if needed
 171   {
 172     MutexLockerEx mu(Terminator_lock);
 173     _should_terminate = true;
 174   }
 175 
 176   stop_service();
 177 
 178   {
 179     MutexLockerEx mu(Terminator_lock);
 180     while (!_has_terminated) {
 181       Terminator_lock->wait();
 182     }
 183   }
 184 }
 185 
 186 void ConcurrentG1RefineThread::stop_service() {
 187   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
 188   _monitor->notify();
 189 }