When everything else fails, you can always count with printf() debugging, in this case, printf() profiling.
- The client prints events to stdout.
- The script reads the events and convert them into a Trace Event (https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/edit?pli=1)
- The file that is created can be open in a Trace Event viewer such as https://ui.perfetto.dev/
// Setup
CatTrace.setPid(0, name = "Process Name")
import com.gplio.cattrace.trace
// Emit event with the start and end of a particular method
fun originalMethod(argument: String) = trace(name = "originalMethod", arguments = mapOf("argument" to argument)) {
// Original body
}
// Instant event
CatTrace.instant("Start", CatTrace.InstantType.Global)
// Complete event
CatTrace.complete("Event that started and ended", startTime, endTime)
// Event
val event = CatTrace.begin("Long Operation")
// Long operation
event.end()
// Counter - Track values across time.
CatTrace.counter("beans", mapOf("value" to i))
// Thread names are continuously collected.
// Call threadMetadata to send a metadata event with their names.
CatTrace.sendThreadMetadata()
Pipe the events into the script:
adb -s b8435fb0 logcat -v time | grep --line-buffered -Ei "CatTrace" | python3 cattrace.py
Open the .json file in https://ui.perfetto.dev/
Follow https://jitpack.io/#goncalopalaio/CatTrace/ and get the latest version.
// settings.gradle
pluginManagement {
repositories {
// (...)
maven { url 'https://jitpack.io' }
}
}
dependencyResolutionManagement {
repositories {
// (...)
maven { url 'https://jitpack.io' }
}
}
(...)
// build.gradle (:app)
implementation 'com.github.goncalopalaio:CatTrace:0.3'
- Transition between threads
- If different threads have the same id across multiple processes, only one of the threads display the name in Perfetto.