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 G1YoungRemSetSamplingThread::G1YoungRemSetSamplingThread() : ConcurrentGCThread() {
  33   _monitor = new Monitor(Mutex::nonleaf,
  34                          "G1YoungRemSetSamplingThread monitor",
  35                          true,
  36                          Monitor::_safepoint_check_never);
  37 
  38   set_name("G1 Young RemSet Sampling");
  39   create_and_start();
  40 }
  41 
  42 void G1YoungRemSetSamplingThread::sleep_before_next_cycle() {
  43   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  44   if (!_should_terminate) {
  45     intx waitms = G1ConcRefinementServiceIntervalMillis; // 300, really should be?
  46     _monitor->wait(Mutex::_no_safepoint_check_flag, waitms);
  47   }
  48 }
  49 
  50 void G1YoungRemSetSamplingThread::run_service() {
  51   double vtime_start = os::elapsedVTime();
  52 
  53   while (!_should_terminate) {
  54     sample_young_list_rs_lengths();
  55 
  56     if (os::supports_vtime()) {
  57       _vtime_accum = (os::elapsedVTime() - vtime_start);
  58     } else {
  59       _vtime_accum = 0.0;
  60     }
  61 
  62     sleep_before_next_cycle();
  63   }
  64 }
  65 
  66 void G1YoungRemSetSamplingThread::stop_service() {
  67   MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
  68   _monitor->notify();
  69 }
  70 
  71 void G1YoungRemSetSamplingThread::sample_young_list_rs_lengths() {
  72   SuspendibleThreadSetJoiner sts;
  73   G1CollectedHeap* g1h = G1CollectedHeap::heap();
  74   G1CollectorPolicy* g1p = g1h->g1_policy();
  75   if (g1p->adaptive_young_list_length()) {
  76     int regions_visited = 0;
  77     g1h->young_list()->rs_length_sampling_init();
  78     while (g1h->young_list()->rs_length_sampling_more()) {
  79       g1h->young_list()->rs_length_sampling_next();
  80       ++regions_visited;
  81 
  82       // we try to yield every time we visit 10 regions
  83       if (regions_visited == 10) {
  84         if (sts.should_yield()) {
  85           sts.yield();
  86           // we just abandon the iteration
  87           break;
  88         }
  89         regions_visited = 0;
  90       }
  91     }
  92 
  93     g1p->revise_young_list_target_length_if_necessary();
  94   }
  95 }