Skip to content

Commit

Permalink
8290376: G1: Refactor G1MMUTracker::when_sec
Browse files Browse the repository at this point in the history
Reviewed-by: tschatzl, iwalulya
  • Loading branch information
albertnetymk committed Aug 25, 2022
1 parent 5a20bc4 commit dc7e256
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
83 changes: 49 additions & 34 deletions src/hotspot/share/gc/g1/g1MMUTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,46 +111,61 @@ void G1MMUTracker::add_pause(double start, double end) {
}
}

// current_timestamp
// GC events / pause_time
// / | \ \ | / /
// -------------[----]-[---]--[--]---[---]------|[--]-----> Time
// | | |
// | | |
// |<- limit | |
// | |<- balance_timestamp |
// | ^ |
// | |
// |<-------- _time_slice ------>|
//
// The MMU constraint requires that we can spend up to `max_gc_time()` on GC
// pauses inside a window of `_time_slice` length. Therefore, we have a GC
// budget of `max_gc_time() - pause_time`, which is to be accounted for by past
// GC events.
//
// Focusing on GC events that are inside [limit, current_timestamp], we iterate
// over them from the newest to the oldest (right-to-left in the diagram) and
// try to locate the timestamp annotated with ^, so that the accumulated GC
// time inside [balance_timestamp, current_timestamp] is equal to the budget.
// Next, return `balance_timestamp - limit`.
//
// When there are no enough GC events, i.e. we have a surplus buget, a new GC
// pause can start right away, so return 0.
double G1MMUTracker::when_sec(double current_timestamp, double pause_time) {
assert(pause_time > 0.0, "precondition");

// If the pause is over the maximum, just assume that it's the maximum.
double adjusted_pause_time =
(pause_time > max_gc_time()) ? max_gc_time() : pause_time;
pause_time = MIN2(pause_time, max_gc_time());

// Earliest end time of a hypothetical pause starting now, taking pause_time.
double earliest_end_time = current_timestamp + adjusted_pause_time;
double gc_time_in_recent_time_slice = calculate_gc_time(earliest_end_time) + adjusted_pause_time;
double gc_budget = max_gc_time() - pause_time;

// How much gc time is needed to pass within the MMU window to fit the given pause into the MMU.
double gc_time_to_pass = gc_time_in_recent_time_slice - max_gc_time();
double limit = current_timestamp + pause_time - _time_slice;
// Iterate from newest to oldest.
for (int i = 0; i < _no_entries; ++i) {
int index = trim_index(_head_index + i);
G1MMUTrackerElem *elem = &_array[index];
// Outside the window.
if (elem->end_time() <= limit) {
break;
}

// If that time to pass is zero or negative we could start the pause immediately.
if (is_double_leq_0(gc_time_to_pass)) {
return 0.0;
}
double duration = (elem->end_time() - MAX2(elem->start_time(), limit));
// This duration would exceed (strictly greater than) the budget.
if (duration > gc_budget) {
// This timestamp captures the instant the budget is balanced (or used up).
double balance_timestamp = elem->end_time() - gc_budget;
assert(balance_timestamp >= limit, "inv");
return balance_timestamp - limit;
}

// Trivially, if the pause is of maximum pause time, the required delay is what the MMU dictates by
// the time slice and maximum gc pause, counted from the end of the last pause.
if (adjusted_pause_time == max_gc_time()) {
G1MMUTrackerElem *elem = &_array[_head_index];
return (elem->end_time() + (_time_slice - max_gc_time())) - current_timestamp;
gc_budget -= duration;
}

// Now go through the recent pause time events,
double limit = earliest_end_time - _time_slice;
int index = _tail_index;
while ( 1 ) {
G1MMUTrackerElem *elem = &_array[index];
if (elem->end_time() > limit) {
if (elem->start_time() > limit) {
gc_time_to_pass -= elem->duration();
} else {
gc_time_to_pass -= elem->end_time() - limit;
}
if (is_double_leq_0(gc_time_to_pass)) {
return elem->end_time() + (_time_slice + gc_time_to_pass) - earliest_end_time;
}
}
index = trim_index(index+1);
guarantee(index != trim_index(_head_index + 1), "should not go past head");
}
// Not enough gc time spent inside the window, we have a budget surplus.
return 0;
}
1 change: 0 additions & 1 deletion src/hotspot/share/gc/g1/g1MMUTracker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class G1MMUTracker: public CHeapObj<mtGC> {
// Returns the amount of time spent in gc pauses in the time slice before the
// given timestamp.
double calculate_gc_time(double current_timestamp);

public:
G1MMUTracker(double time_slice, double max_gc_time);

Expand Down

1 comment on commit dc7e256

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.