ysr@777: /* stefank@2314: * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/g1/g1MMUTracker.hpp" stefank@2314: #include "runtime/mutexLocker.hpp" stefank@2314: #include "utilities/ostream.hpp" ysr@777: ysr@777: #define _DISABLE_MMU 0 ysr@777: ysr@777: // can't rely on comparing doubles with tolerating a small margin for error ysr@777: #define SMALL_MARGIN 0.0000001 ysr@777: #define is_double_leq_0(_value) ( (_value) < SMALL_MARGIN ) ysr@777: #define is_double_leq(_val1, _val2) is_double_leq_0((_val1) - (_val2)) ysr@777: #define is_double_geq(_val1, _val2) is_double_leq_0((_val2) - (_val1)) ysr@777: ysr@777: /***** ALL TIMES ARE IN SECS!!!!!!! *****/ ysr@777: ysr@777: G1MMUTracker::G1MMUTracker(double time_slice, double max_gc_time) : ysr@777: _time_slice(time_slice), tonyp@1371: _max_gc_time(max_gc_time) { } ysr@777: ysr@777: G1MMUTrackerQueue::G1MMUTrackerQueue(double time_slice, double max_gc_time) : ysr@777: G1MMUTracker(time_slice, max_gc_time), ysr@777: _head_index(0), ysr@777: _tail_index(trim_index(_head_index+1)), ysr@777: _no_entries(0) { } ysr@777: ysr@777: void G1MMUTrackerQueue::remove_expired_entries(double current_time) { ysr@777: double limit = current_time - _time_slice; ysr@777: while (_no_entries > 0) { ysr@777: if (is_double_geq(limit, _array[_tail_index].end_time())) { ysr@777: _tail_index = trim_index(_tail_index + 1); ysr@777: --_no_entries; ysr@777: } else ysr@777: return; ysr@777: } ysr@777: guarantee(_no_entries == 0, "should have no entries in the array"); ysr@777: } ysr@777: ysr@777: double G1MMUTrackerQueue::calculate_gc_time(double current_time) { ysr@777: double gc_time = 0.0; ysr@777: double limit = current_time - _time_slice; ysr@777: for (int i = 0; i < _no_entries; ++i) { ysr@777: int index = trim_index(_tail_index + i); ysr@777: G1MMUTrackerQueueElem *elem = &_array[index]; ysr@777: if (elem->end_time() > limit) { ysr@777: if (elem->start_time() > limit) ysr@777: gc_time += elem->duration(); ysr@777: else ysr@777: gc_time += elem->end_time() - limit; ysr@777: } ysr@777: } ysr@777: return gc_time; ysr@777: } ysr@777: ysr@777: void G1MMUTrackerQueue::add_pause(double start, double end, bool gc_thread) { ysr@777: double longest_allowed = longest_pause_internal(start); ysr@777: if (longest_allowed < 0.0) ysr@777: longest_allowed = 0.0; ysr@777: double duration = end - start; ysr@777: ysr@777: remove_expired_entries(end); ysr@777: if (_no_entries == QueueLength) { johnc@1946: // OK, we've filled up the queue. There are a few ways johnc@1946: // of dealing with this "gracefully" ysr@777: // increase the array size (:-) ysr@777: // remove the oldest entry (this might allow more GC time for johnc@1946: // the time slice than what's allowed) - this is what we johnc@1946: // currently do ysr@1523: // consolidate the two entries with the minimum gap between them ysr@1523: // (this might allow less GC time than what's allowed) johnc@1946: ysr@1523: // In the case where ScavengeALot is true, such overflow is not ysr@1523: // uncommon; in such cases, we can, without much loss of precision ysr@1523: // or performance (we are GC'ing most of the time anyway!), johnc@1946: // simply overwrite the oldest entry in the tracker. johnc@1946: johnc@1946: if (G1PolicyVerbose > 1) { johnc@1946: warning("MMU Tracker Queue overflow. Replacing earliest entry."); johnc@1946: } johnc@1946: ysr@1523: _head_index = trim_index(_head_index + 1); ysr@1523: assert(_head_index == _tail_index, "Because we have a full circular buffer"); ysr@1523: _tail_index = trim_index(_tail_index + 1); ysr@1523: } else { ysr@1523: _head_index = trim_index(_head_index + 1); ysr@1523: ++_no_entries; ysr@777: } ysr@777: _array[_head_index] = G1MMUTrackerQueueElem(start, end); ysr@777: } ysr@777: ysr@777: // basically the _internal call does not remove expired entries ysr@777: // this is for trying things out in the future and a couple ysr@777: // of other places (debugging) ysr@777: ysr@777: double G1MMUTrackerQueue::longest_pause(double current_time) { ysr@777: if (_DISABLE_MMU) ysr@777: return _max_gc_time; ysr@777: ysr@777: MutexLockerEx x(MMUTracker_lock, Mutex::_no_safepoint_check_flag); ysr@777: remove_expired_entries(current_time); ysr@777: ysr@777: return longest_pause_internal(current_time); ysr@777: } ysr@777: ysr@777: double G1MMUTrackerQueue::longest_pause_internal(double current_time) { ysr@777: double target_time = _max_gc_time; ysr@777: ysr@777: while( 1 ) { ysr@777: double gc_time = tonyp@1371: calculate_gc_time(current_time + target_time); ysr@777: double diff = target_time + gc_time - _max_gc_time; ysr@777: if (!is_double_leq_0(diff)) { ysr@777: target_time -= diff; ysr@777: if (is_double_leq_0(target_time)) { ysr@777: target_time = -1.0; ysr@777: break; ysr@777: } ysr@777: } else { ysr@777: break; ysr@777: } ysr@777: } ysr@777: ysr@777: return target_time; ysr@777: } ysr@777: ysr@777: // basically the _internal call does not remove expired entries ysr@777: // this is for trying things out in the future and a couple ysr@777: // of other places (debugging) ysr@777: ysr@777: double G1MMUTrackerQueue::when_sec(double current_time, double pause_time) { ysr@777: if (_DISABLE_MMU) ysr@777: return 0.0; ysr@777: ysr@777: MutexLockerEx x(MMUTracker_lock, Mutex::_no_safepoint_check_flag); ysr@777: remove_expired_entries(current_time); ysr@777: ysr@777: return when_internal(current_time, pause_time); ysr@777: } ysr@777: ysr@777: double G1MMUTrackerQueue::when_internal(double current_time, ysr@777: double pause_time) { ysr@777: // if the pause is over the maximum, just assume that it's the maximum ysr@777: double adjusted_pause_time = ysr@777: (pause_time > max_gc_time()) ? max_gc_time() : pause_time; ysr@777: double earliest_end = current_time + adjusted_pause_time; ysr@777: double limit = earliest_end - _time_slice; ysr@777: double gc_time = calculate_gc_time(earliest_end); ysr@777: double diff = gc_time + adjusted_pause_time - max_gc_time(); ysr@777: if (is_double_leq_0(diff)) ysr@777: return 0.0; ysr@777: ysr@777: int index = _tail_index; ysr@777: while ( 1 ) { ysr@777: G1MMUTrackerQueueElem *elem = &_array[index]; ysr@777: if (elem->end_time() > limit) { ysr@777: if (elem->start_time() > limit) ysr@777: diff -= elem->duration(); ysr@777: else ysr@777: diff -= elem->end_time() - limit; ysr@777: if (is_double_leq_0(diff)) ysr@777: return elem->end_time() + diff + _time_slice - adjusted_pause_time - current_time; ysr@777: } ysr@777: index = trim_index(index+1); ysr@777: guarantee(index != trim_index(_head_index + 1), "should not go past head"); ysr@777: } ysr@777: }