< prev index next >

src/hotspot/share/gc/g1/g1ConcurrentRefineThread.cpp

Print this page


   1 /*
   2  * Copyright (c) 2001, 2017, 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/g1ConcurrentRefine.hpp"
  27 #include "gc/g1/g1ConcurrentRefineThread.hpp"
  28 #include "gc/shared/suspendibleThreadSet.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "runtime/handles.inline.hpp"
  32 #include "runtime/mutexLocker.hpp"
  33 
  34 G1ConcurrentRefineThread::G1ConcurrentRefineThread(G1ConcurrentRefine* cr, uint worker_id) :
  35   ConcurrentGCThread(),
  36   _worker_id(worker_id),
  37   _active(false),
  38   _monitor(NULL),
  39   _cr(cr),
  40   _vtime_accum(0.0)
  41 {
  42   // Each thread has its own monitor. The i-th thread is responsible for signaling
  43   // to thread i+1 if the number of buffers in the queue exceeds a threshold for this
  44   // thread. Monitors are also used to wake up the threads during termination.
  45   // The 0th (primary) worker is notified by mutator threads and has a special monitor.
  46   if (!is_primary()) {
  47     _monitor = new Monitor(Mutex::nonleaf, "Refinement monitor", true,
  48                            Monitor::_safepoint_check_never);
  49   } else {
  50     _monitor = DirtyCardQ_CBL_mon;
  51   }
  52 
  53   // set name
  54   set_name("G1 Refine#%d", worker_id);
  55   create_and_start();
  56 }
  57 
  58 void G1ConcurrentRefineThread::wait_for_completed_buffers() {
  59   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  60   while (!should_terminate() && !is_active()) {
  61     _monitor->wait(Mutex::_no_safepoint_check_flag);
  62   }
  63 }
  64 
  65 bool G1ConcurrentRefineThread::is_active() {
  66   DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
  67   return is_primary() ? dcqs.process_completed_buffers() : _active;
  68 }
  69 
  70 void G1ConcurrentRefineThread::activate() {
  71   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  72   if (!is_primary()) {
  73     set_active(true);
  74   } else {
  75     DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
  76     dcqs.set_process_completed(true);
  77   }
  78   _monitor->notify();
  79 }
  80 
  81 void G1ConcurrentRefineThread::deactivate() {
  82   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  83   if (!is_primary()) {
  84     set_active(false);
  85   } else {
  86     DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
  87     dcqs.set_process_completed(false);
  88   }
  89 }
  90 
  91 void G1ConcurrentRefineThread::run_service() {
  92   _vtime_start = os::elapsedVTime();
  93 
  94   while (!should_terminate()) {
  95     // Wait for work
  96     wait_for_completed_buffers();
  97     if (should_terminate()) {
  98       break;
  99     }
 100 
 101     size_t buffers_processed = 0;
 102     log_debug(gc, refine)("Activated worker %d, on threshold: " SIZE_FORMAT ", current: " SIZE_FORMAT,
 103                           _worker_id, _cr->activation_threshold(_worker_id),
 104                            JavaThread::dirty_card_queue_set().completed_buffers_num());
 105 
 106     {
 107       SuspendibleThreadSetJoiner sts_join;
 108 
 109       while (!should_terminate()) {
 110         if (sts_join.should_yield()) {
 111           sts_join.yield();
 112           continue;             // Re-check for termination after yield delay.
 113         }
 114 
 115         if (!_cr->do_refinement_step(_worker_id)) {
 116           break;
 117         }
 118         ++buffers_processed;
 119       }
 120     }
 121 
 122     deactivate();
 123     log_debug(gc, refine)("Deactivated worker %d, off threshold: " SIZE_FORMAT
 124                           ", current: " SIZE_FORMAT ", processed: " SIZE_FORMAT,
 125                           _worker_id, _cr->deactivation_threshold(_worker_id),
 126                           JavaThread::dirty_card_queue_set().completed_buffers_num(),
 127                           buffers_processed);
 128 
 129     if (os::supports_vtime()) {
 130       _vtime_accum = (os::elapsedVTime() - _vtime_start);
 131     } else {
 132       _vtime_accum = 0.0;
 133     }
 134   }
 135 
 136   log_debug(gc, refine)("Stopping %d", _worker_id);
 137 }
 138 
 139 void G1ConcurrentRefineThread::stop_service() {
 140   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
 141   _monitor->notify();
 142 }
   1 /*
   2  * Copyright (c) 2001, 2018, 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/g1BarrierSet.hpp"
  27 #include "gc/g1/g1ConcurrentRefine.hpp"
  28 #include "gc/g1/g1ConcurrentRefineThread.hpp"
  29 #include "gc/shared/suspendibleThreadSet.hpp"
  30 #include "logging/log.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/mutexLocker.hpp"
  34 
  35 G1ConcurrentRefineThread::G1ConcurrentRefineThread(G1ConcurrentRefine* cr, uint worker_id) :
  36   ConcurrentGCThread(),
  37   _worker_id(worker_id),
  38   _active(false),
  39   _monitor(NULL),
  40   _cr(cr),
  41   _vtime_accum(0.0)
  42 {
  43   // Each thread has its own monitor. The i-th thread is responsible for signaling
  44   // to thread i+1 if the number of buffers in the queue exceeds a threshold for this
  45   // thread. Monitors are also used to wake up the threads during termination.
  46   // The 0th (primary) worker is notified by mutator threads and has a special monitor.
  47   if (!is_primary()) {
  48     _monitor = new Monitor(Mutex::nonleaf, "Refinement monitor", true,
  49                            Monitor::_safepoint_check_never);
  50   } else {
  51     _monitor = DirtyCardQ_CBL_mon;
  52   }
  53 
  54   // set name
  55   set_name("G1 Refine#%d", worker_id);
  56   create_and_start();
  57 }
  58 
  59 void G1ConcurrentRefineThread::wait_for_completed_buffers() {
  60   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  61   while (!should_terminate() && !is_active()) {
  62     _monitor->wait(Mutex::_no_safepoint_check_flag);
  63   }
  64 }
  65 
  66 bool G1ConcurrentRefineThread::is_active() {
  67   DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
  68   return is_primary() ? dcqs.process_completed_buffers() : _active;
  69 }
  70 
  71 void G1ConcurrentRefineThread::activate() {
  72   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  73   if (!is_primary()) {
  74     set_active(true);
  75   } else {
  76     DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
  77     dcqs.set_process_completed(true);
  78   }
  79   _monitor->notify();
  80 }
  81 
  82 void G1ConcurrentRefineThread::deactivate() {
  83   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  84   if (!is_primary()) {
  85     set_active(false);
  86   } else {
  87     DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
  88     dcqs.set_process_completed(false);
  89   }
  90 }
  91 
  92 void G1ConcurrentRefineThread::run_service() {
  93   _vtime_start = os::elapsedVTime();
  94 
  95   while (!should_terminate()) {
  96     // Wait for work
  97     wait_for_completed_buffers();
  98     if (should_terminate()) {
  99       break;
 100     }
 101 
 102     size_t buffers_processed = 0;
 103     log_debug(gc, refine)("Activated worker %d, on threshold: " SIZE_FORMAT ", current: " SIZE_FORMAT,
 104                           _worker_id, _cr->activation_threshold(_worker_id),
 105                            G1BarrierSet::dirty_card_queue_set().completed_buffers_num());
 106 
 107     {
 108       SuspendibleThreadSetJoiner sts_join;
 109 
 110       while (!should_terminate()) {
 111         if (sts_join.should_yield()) {
 112           sts_join.yield();
 113           continue;             // Re-check for termination after yield delay.
 114         }
 115 
 116         if (!_cr->do_refinement_step(_worker_id)) {
 117           break;
 118         }
 119         ++buffers_processed;
 120       }
 121     }
 122 
 123     deactivate();
 124     log_debug(gc, refine)("Deactivated worker %d, off threshold: " SIZE_FORMAT
 125                           ", current: " SIZE_FORMAT ", processed: " SIZE_FORMAT,
 126                           _worker_id, _cr->deactivation_threshold(_worker_id),
 127                           G1BarrierSet::dirty_card_queue_set().completed_buffers_num(),
 128                           buffers_processed);
 129 
 130     if (os::supports_vtime()) {
 131       _vtime_accum = (os::elapsedVTime() - _vtime_start);
 132     } else {
 133       _vtime_accum = 0.0;
 134     }
 135   }
 136 
 137   log_debug(gc, refine)("Stopping %d", _worker_id);
 138 }
 139 
 140 void G1ConcurrentRefineThread::stop_service() {
 141   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
 142   _monitor->notify();
 143 }
< prev index next >