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/heapRegion.inline.hpp" 30 #include "gc/g1/heapRegionRemSet.hpp" 31 #include "gc/g1/suspendibleThreadSet.hpp" 32 #include "runtime/mutexLocker.hpp" 33 34 void G1YoungRemSetSamplingThread::run() { 35 initialize_in_thread(); 36 wait_for_universe_init(); 37 38 run_service(); 39 40 terminate(); 41 } 42 43 void G1YoungRemSetSamplingThread::stop() { 44 // it is ok to take late safepoints here, if needed 45 { 46 MutexLockerEx mu(Terminator_lock); 47 _should_terminate = true; 48 } 49 50 stop_service(); 51 52 { 53 MutexLockerEx mu(Terminator_lock); 54 while (!_has_terminated) { 55 Terminator_lock->wait(); 56 } 57 } 58 } 59 60 G1YoungRemSetSamplingThread::G1YoungRemSetSamplingThread() : ConcurrentGCThread() { 61 _monitor = new Monitor(Mutex::nonleaf, 62 "G1YoungRemSetSamplingThread monitor", 63 true, 64 Monitor::_safepoint_check_never); 65 66 set_name("G1 Young RemSet Sampling"); 67 create_and_start(); 68 } 69 70 void G1YoungRemSetSamplingThread::sleep_before_next_cycle() { 71 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag); 72 if (!_should_terminate) { 73 intx waitms = G1ConcRefinementServiceIntervalMillis; // 300, really should be? 74 _monitor->wait(Mutex::_no_safepoint_check_flag, waitms); 75 } 76 } 77 78 void G1YoungRemSetSamplingThread::run_service() { 79 double vtime_start = os::elapsedVTime(); 80 81 while (!_should_terminate) { 82 sample_young_list_rs_lengths(); 83 84 if (os::supports_vtime()) { 85 _vtime_accum = (os::elapsedVTime() - vtime_start); 86 } else { 87 _vtime_accum = 0.0; 88 } 89 90 sleep_before_next_cycle(); 91 } 92 } 93 94 void G1YoungRemSetSamplingThread::stop_service() { 95 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag); 96 _monitor->notify(); 97 } 98 99 void G1YoungRemSetSamplingThread::sample_young_list_rs_lengths() { 100 SuspendibleThreadSetJoiner sts; 101 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 102 G1CollectorPolicy* g1p = g1h->g1_policy(); 103 if (g1p->adaptive_young_list_length()) { 104 int regions_visited = 0; 105 HeapRegion* hr = g1h->young_list()->first_region(); 106 size_t sampled_rs_lengths = 0; 107 108 while (hr != NULL) { 109 size_t rs_length = hr->rem_set()->occupied(); 110 sampled_rs_lengths += rs_length; 111 112 // The current region may not yet have been added to the 113 // incremental collection set (it gets added when it is 114 // retired as the current allocation region). 115 if (hr->in_collection_set()) { 116 // Update the collection set policy information for this region 117 g1p->update_incremental_cset_info(hr, rs_length); 118 } 119 120 ++regions_visited; 121 122 // we try to yield every time we visit 10 regions 123 if (regions_visited == 10) { 124 if (sts.should_yield()) { 125 sts.yield(); 126 // A gc may have occurred and our sampling data is stale and further 127 // traversal of the young list is unsafe 128 return; 129 } 130 regions_visited = 0; 131 } 132 hr = hr->get_next_young_region(); 133 } 134 g1p->revise_young_list_target_length_if_necessary(sampled_rs_lengths); 135 } 136 }