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() : 61 ConcurrentGCThread(), 62 _monitor(Mutex::nonleaf, 63 "G1YoungRemSetSamplingThread monitor", 64 true, 65 Monitor::_safepoint_check_never) { 66 set_name("G1 Young RemSet Sampling"); 67 create_and_start(); 68 } 69 70 G1YoungRemSetSamplingThread::~G1YoungRemSetSamplingThread() { 71 assert(!_monitor.is_locked(), "don't delete locked monitor"); 72 } 73 74 void G1YoungRemSetSamplingThread::sleep_before_next_cycle() { 75 MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag); 76 if (!_should_terminate) { 77 intx waitms = G1ConcRefinementServiceIntervalMillis; // 300, really should be? 78 _monitor.wait(Mutex::_no_safepoint_check_flag, waitms); 79 } 80 } 81 82 void G1YoungRemSetSamplingThread::run_service() { 83 double vtime_start = os::elapsedVTime(); 84 85 while (!_should_terminate) { 86 sample_young_list_rs_lengths(); 87 88 if (os::supports_vtime()) { 89 _vtime_accum = (os::elapsedVTime() - vtime_start); 90 } else { 91 _vtime_accum = 0.0; 92 } 93 94 sleep_before_next_cycle(); 95 } 96 } 97 98 void G1YoungRemSetSamplingThread::stop_service() { 99 MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag); 100 _monitor.notify(); 101 } 102 103 void G1YoungRemSetSamplingThread::sample_young_list_rs_lengths() { 104 SuspendibleThreadSetJoiner sts; 105 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 106 G1CollectorPolicy* g1p = g1h->g1_policy(); 107 if (g1p->adaptive_young_list_length()) { 108 int regions_visited = 0; 109 HeapRegion* hr = g1h->young_list()->first_region(); 110 size_t sampled_rs_lengths = 0; 111 112 while (hr != NULL) { 113 size_t rs_length = hr->rem_set()->occupied(); 114 sampled_rs_lengths += rs_length; 115 116 // The current region may not yet have been added to the 117 // incremental collection set (it gets added when it is 118 // retired as the current allocation region). 119 if (hr->in_collection_set()) { 120 // Update the collection set policy information for this region 121 g1p->update_incremental_cset_info(hr, rs_length); 122 } 123 124 ++regions_visited; 125 126 // we try to yield every time we visit 10 regions 127 if (regions_visited == 10) { 128 if (sts.should_yield()) { 129 sts.yield(); 130 // A gc may have occurred and our sampling data is stale and further 131 // traversal of the young list is unsafe 132 return; 133 } 134 regions_visited = 0; 135 } 136 hr = hr->get_next_young_region(); 137 } 138 g1p->revise_young_list_target_length_if_necessary(sampled_rs_lengths); 139 } 140 }