1 /*
   2  * Copyright (c) 2015, 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/g1CollectedHeap.inline.hpp"
  27 #include "gc/g1/g1CollectorPolicy.hpp"
  28 #include "gc/g1/g1YoungRemSetSamplingThread.hpp"
  29 #include "gc/g1/suspendibleThreadSet.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 
  32 void G1YoungRemSetSamplingThread::run() {
  33   initialize_in_thread();
  34   wait_for_universe_init();
  35 
  36   run_service();
  37 
  38   terminate();
  39 }
  40 
  41 void G1YoungRemSetSamplingThread::stop() {
  42   // it is ok to take late safepoints here, if needed
  43   {
  44     MutexLockerEx mu(Terminator_lock);
  45     _should_terminate = true;
  46   }
  47 
  48   stop_service();
  49 
  50   {
  51     MutexLockerEx mu(Terminator_lock);
  52     while (!_has_terminated) {
  53       Terminator_lock->wait();
  54     }
  55   }
  56 }
  57 
  58 G1YoungRemSetSamplingThread::G1YoungRemSetSamplingThread() : ConcurrentGCThread() {
  59   _monitor = new Monitor(Mutex::nonleaf,
  60                          "G1YoungRemSetSamplingThread monitor",
  61                          true,
  62                          Monitor::_safepoint_check_never);
  63 
  64   set_name("G1 Young RemSet Sampling");
  65   create_and_start();
  66 }
  67 
  68 void G1YoungRemSetSamplingThread::sleep_before_next_cycle() {
  69   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  70   if (!_should_terminate) {
  71     intx waitms = G1ConcRefinementServiceIntervalMillis; // 300, really should be?
  72     _monitor->wait(Mutex::_no_safepoint_check_flag, waitms);
  73   }
  74 }
  75 
  76 void G1YoungRemSetSamplingThread::run_service() {
  77   double vtime_start = os::elapsedVTime();
  78 
  79   while (!_should_terminate) {
  80     sample_young_list_rs_lengths();
  81 
  82     if (os::supports_vtime()) {
  83       _vtime_accum = (os::elapsedVTime() - vtime_start);
  84     } else {
  85       _vtime_accum = 0.0;
  86     }
  87 
  88     sleep_before_next_cycle();
  89   }
  90 }
  91 
  92 void G1YoungRemSetSamplingThread::stop_service() {
  93   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  94   _monitor->notify();
  95 }
  96 
  97 void G1YoungRemSetSamplingThread::sample_young_list_rs_lengths() {
  98   SuspendibleThreadSetJoiner sts;
  99   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 100   G1CollectorPolicy* g1p = g1h->g1_policy();
 101   if (g1p->adaptive_young_list_length()) {
 102     int regions_visited = 0;
 103     g1h->young_list()->rs_length_sampling_init();
 104     while (g1h->young_list()->rs_length_sampling_more()) {
 105       g1h->young_list()->rs_length_sampling_next();
 106       ++regions_visited;
 107 
 108       // we try to yield every time we visit 10 regions
 109       if (regions_visited == 10) {
 110         if (sts.should_yield()) {
 111           sts.yield();
 112           // we just abandon the iteration
 113           break;
 114         }
 115         regions_visited = 0;
 116       }
 117     }
 118 
 119     g1p->revise_young_list_target_length_if_necessary();
 120   }
 121 }