Skip to content

Commit

Permalink
Support fail threshold for high GC usage (runningcode#185)
Browse files Browse the repository at this point in the history
* Support fail threshold for high GC usage

* Update kotlin instructions as well.

* Fix Kotlin Formatting

Co-authored-by: Nelson Osacky <[email protected]>
Co-authored-by: Nelson Osacky <[email protected]>
  • Loading branch information
3 people authored Feb 14, 2022
1 parent 87f378c commit bd9a013
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## 0.8.0
* [Skip multiple daemons check on non-Unix machines, not supported yet](https://github.com/runningcode/gradle-doctor/issues/84)
* [Detect Kotlin Compiler Daemon failing to connect.](https://github.com/runningcode/gradle-doctor/issues/194)
* [Support fail threshold for high GC usage](https://github.com/runningcode/gradle-doctor/issues/183)

## 0.7.3
* Fix [compatiblity with Java 8.](https://github.com/runningcode/gradle-doctor/issues/171)
Expand Down
8 changes: 8 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
* The level at which to warn when a build spends more than this percent garbage collecting.
*/
GCWarningThreshold = 0.10f
/**
* The level at which to fail when a build spends more than this percent garbage collecting.
*/
GCFailThreshold = 0.9f
/**
* Print a warning to the console if we spend more than this amount of time with Dagger annotation processors.
*/
Expand Down Expand Up @@ -104,6 +108,10 @@
* The level at which to warn when a build spends more than this percent garbage collecting.
*/
GCWarningThreshold.set(0.10f)
/**
* The level at which to fail when a build spends more than this percent garbage collecting.
*/
GCFailThreshold = 0.9f
/**
* Print a warning to the console if we spend more than this amount of time with Dagger annotation processors.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ open class DoctorExtension(objects: ObjectFactory) {
* The level at which to warn when a build spends more than this percent garbage collecting.
*/
val GCWarningThreshold = objects.property<Float>().convention(0.10f)
/**
* The level at which to fail when a build spends more than this percent garbage collecting.
*/
val GCFailThreshold = objects.property<Float>().convention(0.9f)
/**
* Print a warning to the console if we spend more than this amount of time with Dagger annotation processors.
*/
Expand Down
11 changes: 9 additions & 2 deletions doctor-plugin/src/main/java/com/osacky/doctor/GarbagePrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.osacky.doctor
import com.osacky.doctor.internal.Clock
import com.osacky.doctor.internal.DirtyBeanCollector
import com.osacky.tagger.ScanApi
import org.gradle.api.GradleException
import java.text.NumberFormat

class GarbagePrinter(
Expand All @@ -27,15 +28,21 @@ class GarbagePrinter(
val garbageDuration = endGarbageTime - startGarbageTime

val percentGarbageCollecting = (garbageDuration * 1f / buildDuration)
if (buildDuration > warningThreshold && percentGarbageCollecting > extension.GCWarningThreshold.get()) {
val isThresholdExceeded = percentGarbageCollecting > extension.GCWarningThreshold.get() ||
percentGarbageCollecting > extension.GCFailThreshold.get()
if (buildDuration > warningThreshold && isThresholdExceeded) {
val message =
"""
This build spent ${formatter.format(percentGarbageCollecting)} garbage collecting.
If this is the first build with this Daemon, it likely means that this build needs more heap space.
Otherwise, if this is happening after several builds it could indicate a memory leak.
For a quick fix, restart this Gradle daemon. ./gradlew --stop
""".trimIndent()
return listOf(message)
return if (percentGarbageCollecting > extension.GCFailThreshold.get()) {
throw GradleException(message)
} else {
listOf(message)
}
}
return emptyList()
}
Expand Down

0 comments on commit bd9a013

Please sign in to comment.