Skip to content

Commit

Permalink
allow passing ignored refs to command line
Browse files Browse the repository at this point in the history
  • Loading branch information
pyricau committed Apr 22, 2024
1 parent 8368276 commit 33d142c
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions shark/shark-cli/src/main/java/shark/HeapGrowthCommand.kt
Expand Up @@ -4,6 +4,7 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.CliktError
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.split
import com.github.ajalt.clikt.parameters.options.validate
import com.github.ajalt.clikt.parameters.types.int
import jline.console.ConsoleReader
Expand All @@ -12,6 +13,7 @@ import kotlin.time.Duration.Companion.nanoseconds
import shark.DumpProcessCommand.Companion.dumpHeap
import shark.HprofHeapGraph.Companion.openHeapGraph
import shark.ReferencePattern.InstanceFieldPattern
import shark.ReferencePattern.StaticFieldPattern
import shark.SharkCliCommand.Companion.sharkCliParams
import shark.SharkCliCommand.HeapDumpSource.HprofDirectorySource
import shark.SharkCliCommand.HeapDumpSource.HprofFileSource
Expand All @@ -31,18 +33,44 @@ class HeapGrowthCommand : CliktCommand(
help = "The max number of heap dumps to perform live before returning the results."
).int().default(5).validate { if (it <= 1) fail("$it is not greater than 1") }

private val ignoredFields by option("--ignored-fields")
.split(",")

private val ignoredStaticFields by option("--ignored-static-fields")
.split(",")

override fun run() {
val params = context.sharkCliParams

val detector = RepeatedObjectGrowthDetectorAndroidFactory.create()
val ignoredInstanceFieldReferences = ignoredFields?.let { ignoredFields ->
ignoredFields.map { ignoredField ->
val className = ignoredField.substringBeforeLast(".")
val fieldName = ignoredField.substringAfterLast(".")
IgnoredReferenceMatcher(InstanceFieldPattern(className, fieldName))
}
} ?: emptyList()

val ignoredStaticFieldReferences = ignoredStaticFields?.let { ignoredStaticFields ->
ignoredStaticFields.map { ignoredStaticField ->
val className = ignoredStaticField.substringBeforeLast(".")
val fieldName = ignoredStaticField.substringAfterLast(".")
IgnoredReferenceMatcher(StaticFieldPattern(className, fieldName))
}
} ?: emptyList()

val referenceMatchers = AndroidHeapGrowthIgnoredReferences.defaults +
ignoredInstanceFieldReferences +
ignoredStaticFieldReferences

val detector = RepeatedObjectGrowthDetectorAndroidFactory.create(referenceMatchers)

data class Metrics(
val randomAccessByteReads: Long,
val randomAccessReadCount: Long,
val duration: Duration
)
val metrics = mutableListOf<Metrics>()

val metrics = mutableListOf<Metrics>()

val results = when (val source = params.source) {
is HprofFileSource -> throw CliktError(
Expand All @@ -69,7 +97,9 @@ class HeapGrowthCommand : CliktCommand(
val heapGraphs = hprofFiles.asSequence().map { file ->
val openTime = System.nanoTime().nanoseconds
previous?.let {
metrics += Metrics(it.randomAccessByteReads, it.randomAccessReadCount, openTime - previousStartTime)
metrics += Metrics(
it.randomAccessByteReads, it.randomAccessReadCount, openTime - previousStartTime
)
}
val sourceProvider = ConstantMemoryMetricsDualSourceProvider(FileSourceProvider(file))
previous = sourceProvider
Expand All @@ -79,12 +109,13 @@ class HeapGrowthCommand : CliktCommand(
detector.findRepeatedlyGrowingObjects(heapGraphs, scenarioLoopsPerDump).also {
previous?.let {
val finishTime = System.nanoTime().nanoseconds
metrics += Metrics(it.randomAccessByteReads, it.randomAccessReadCount, finishTime - previousStartTime)
metrics += Metrics(
it.randomAccessByteReads, it.randomAccessReadCount, finishTime - previousStartTime
)
}
}
}


is ProcessSource -> {
echo("Detecting heap growth live")

Expand Down

0 comments on commit 33d142c

Please sign in to comment.