1 /*
   2  * Copyright (c) 2015, 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/g1CollectedHeap.inline.hpp"
  27 #include "gc/g1/g1CollectionSet.hpp"
  28 #include "gc/g1/g1ConcurrentMark.inline.hpp"
  29 #include "gc/g1/g1ConcurrentMarkThread.inline.hpp"
  30 #include "gc/g1/g1Policy.hpp"
  31 #include "gc/g1/g1YoungRemSetSamplingThread.hpp"
  32 #include "gc/g1/heapRegion.inline.hpp"
  33 #include "gc/g1/heapRegionRemSet.hpp"
  34 #include "gc/shared/suspendibleThreadSet.hpp"
  35 #include "runtime/mutexLocker.hpp"
  36 
  37 G1YoungRemSetSamplingThread::G1YoungRemSetSamplingThread() :
  38     ConcurrentGCThread(),
  39     _monitor(Mutex::nonleaf,
  40              "G1YoungRemSetSamplingThread monitor",
  41              true,
  42              Monitor::_safepoint_check_never),
  43     _last_periodic_gc_attempt_s(os::elapsedTime()),
  44     _vtime_accum(0) {
  45   set_name("G1 Young RemSet Sampling");
  46   create_and_start();
  47 }
  48 
  49 void G1YoungRemSetSamplingThread::sleep_before_next_cycle() {
  50   MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag);
  51   if (!should_terminate()) {
  52     uintx waitms = G1ConcRefinementServiceIntervalMillis;
  53     _monitor.wait(Mutex::_no_safepoint_check_flag, waitms);
  54   }
  55 }
  56 
  57 bool G1YoungRemSetSamplingThread::should_start_periodic_gc() {
  58   // If we are currently in a concurrent mark we are going to uncommit memory soon.
  59   if (G1CollectedHeap::heap()->concurrent_mark()->cm_thread()->during_cycle()) {
  60     log_debug(gc, periodic)("Concurrent cycle in progress. Skipping.");
  61     return false;
  62   }
  63 
  64   // Check if enough time has passed since the last GC.
  65   uintx time_since_last_gc = (uintx)Universe::heap()->millis_since_last_gc();
  66   if ((time_since_last_gc < G1PeriodicGCInterval)) {
  67     log_debug(gc, periodic)("Last GC occurred " UINTX_FORMAT "ms before which is below threshold " UINTX_FORMAT "ms. Skipping.",
  68                             time_since_last_gc, G1PeriodicGCInterval);
  69     return false;
  70   }
  71 
  72   // Check if load is lower than max.
  73   double recent_load;
  74   if ((G1PeriodicGCSystemLoadThreshold > 0.0f) &&
  75       (os::loadavg(&recent_load, 1) == -1 || recent_load > G1PeriodicGCSystemLoadThreshold)) {
  76     log_debug(gc, periodic)("Load %1.2f is higher than threshold %1.2f. Skipping.",
  77                             recent_load, G1PeriodicGCSystemLoadThreshold);
  78     return false;
  79   }
  80 
  81   return true;
  82 }
  83 
  84 void G1YoungRemSetSamplingThread::check_for_periodic_gc(){
  85   // If disabled, just return.
  86   if (G1PeriodicGCInterval == 0) {
  87     return;
  88   }
  89   if ((os::elapsedTime() - _last_periodic_gc_attempt_s) > (G1PeriodicGCInterval / 1000.0)) {
  90     log_debug(gc, periodic)("Checking for periodic GC.");
  91     if (should_start_periodic_gc()) {
  92       if (!G1CollectedHeap::heap()->attempt_collect(GCCause::_g1_periodic_collection, false)) {
  93         log_debug(gc, periodic)("GC request denied. Skipping.");
  94       }
  95     }
  96     _last_periodic_gc_attempt_s = os::elapsedTime();
  97   }
  98 }
  99 
 100 void G1YoungRemSetSamplingThread::run_service() {
 101   double vtime_start = os::elapsedVTime();
 102 
 103   // Print a message about periodic GC configuration.
 104   if (G1PeriodicGCInterval != 0) {
 105     log_info(gc)("Periodic GC enabled with interval " UINTX_FORMAT "ms", G1PeriodicGCInterval);
 106   } else {
 107     log_info(gc)("Periodic GC disabled");
 108   }
 109 
 110   while (!should_terminate()) {
 111     sample_young_list_rs_lengths();
 112 
 113     if (os::supports_vtime()) {
 114       _vtime_accum = (os::elapsedVTime() - vtime_start);
 115     } else {
 116       _vtime_accum = 0.0;
 117     }
 118 
 119     check_for_periodic_gc();
 120 
 121     sleep_before_next_cycle();
 122   }
 123 }
 124 
 125 void G1YoungRemSetSamplingThread::stop_service() {
 126   MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag);
 127   _monitor.notify();
 128 }
 129 
 130 class G1YoungRemSetSamplingClosure : public HeapRegionClosure {
 131   SuspendibleThreadSetJoiner* _sts;
 132   size_t _regions_visited;
 133   size_t _sampled_rs_lengths;
 134 public:
 135   G1YoungRemSetSamplingClosure(SuspendibleThreadSetJoiner* sts) :
 136     HeapRegionClosure(), _sts(sts), _regions_visited(0), _sampled_rs_lengths(0) { }
 137 
 138   virtual bool do_heap_region(HeapRegion* r) {
 139     size_t rs_length = r->rem_set()->occupied();
 140     _sampled_rs_lengths += rs_length;
 141 
 142     // Update the collection set policy information for this region
 143     G1CollectedHeap::heap()->collection_set()->update_young_region_prediction(r, rs_length);
 144 
 145     _regions_visited++;
 146 
 147     if (_regions_visited == 10) {
 148       if (_sts->should_yield()) {
 149         _sts->yield();
 150         // A gc may have occurred and our sampling data is stale and further
 151         // traversal of the collection set is unsafe
 152         return true;
 153       }
 154       _regions_visited = 0;
 155     }
 156     return false;
 157   }
 158 
 159   size_t sampled_rs_lengths() const { return _sampled_rs_lengths; }
 160 };
 161 
 162 void G1YoungRemSetSamplingThread::sample_young_list_rs_lengths() {
 163   SuspendibleThreadSetJoiner sts;
 164   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 165   G1Policy* g1p = g1h->g1_policy();
 166 
 167   if (g1p->adaptive_young_list_length()) {
 168     G1YoungRemSetSamplingClosure cl(&sts);
 169 
 170     G1CollectionSet* g1cs = g1h->collection_set();
 171     g1cs->iterate(&cl);
 172 
 173     if (cl.is_complete()) {
 174       g1p->revise_young_list_target_length_if_necessary(cl.sampled_rs_lengths());
 175     }
 176   }
 177 }