-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Call System.gc between work requests #558
Merged
restingbull
merged 3 commits into
bazelbuild:master
from
Bencodes:call-system-gc-between-work-requests
Jul 21, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/io/bazel/worker/CpuTimeBasedGcScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.bazel.worker | ||
|
||
import com.sun.management.OperatingSystemMXBean | ||
import java.lang.management.ManagementFactory | ||
import java.time.Duration | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
// This class is intended to mirror https://github.com/Bencodes/bazel/blob/3835d9b21ad524d06873dfbf465ffd2dfb635ba8/src/main/java/com/google/devtools/build/lib/worker/WorkRequestHandler.java#L431-L474 | ||
class CpuTimeBasedGcScheduler( | ||
/** | ||
* After this much CPU time has elapsed, we may force a GC run. Set to [Duration.ZERO] to | ||
* disable. | ||
*/ | ||
private val cpuUsageBeforeGc: Duration | ||
) { | ||
|
||
/** The total process CPU time at the last GC run (or from the start of the worker). */ | ||
private val cpuTime: Duration | ||
get() = if (cpuUsageBeforeGc.isZero) Duration.ZERO else Duration.ofNanos(bean.processCpuTime) | ||
private val cpuTimeAtLastGc: AtomicReference<Duration> = AtomicReference(cpuTime) | ||
|
||
/** Call occasionally to perform a GC if enough CPU time has been used. */ | ||
fun maybePerformGc() { | ||
if (!cpuUsageBeforeGc.isZero) { | ||
val currentCpuTime = cpuTime | ||
val lastCpuTime = cpuTimeAtLastGc.get() | ||
// Do GC when enough CPU time has been used, but only if nobody else beat us to it. | ||
if (currentCpuTime.minus(lastCpuTime).compareTo(cpuUsageBeforeGc) > 0 | ||
&& cpuTimeAtLastGc.compareAndSet(lastCpuTime, currentCpuTime) | ||
) { | ||
System.gc() | ||
// Avoid counting GC CPU time against CPU time before next GC. | ||
cpuTimeAtLastGc.compareAndSet(currentCpuTime, cpuTime) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
/** Used to get the CPU time used by this process. */ | ||
private val bean = ManagementFactory.getOperatingSystemMXBean() as OperatingSystemMXBean | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mirroring what's being done inside the generic Bazel worker
https://github.com/Bencodes/bazel/blob/3835d9b21ad524d06873dfbf465ffd2dfb635ba8/src/main/java/com/google/devtools/build/lib/worker/WorkRequestHandler.java#L431-L474
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It maybe worthwhile for future readers to inline the link to bazel source and part of the PR description in a class comment :)