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