Skip to content

[Kapt] Support dumping processors stats to a file #4280

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

Closed
wants to merge 3 commits into from

Conversation

Udinic
Copy link
Contributor

@Udinic Udinic commented Apr 5, 2021

There's already an option to print stats on the Annotation Processors execution, using the -Kapt-show-processor-timings plugin option. This PR adds a way to dump that report into a file, using a new plugin option -Kapt-dump-processor-timings (or org.jetbrains.kotlin.kapt3:dumpProcessorTimings).

Example for such report:

Kapt Annotation Processing performance report:
com.udinic.aptplayground.processor.TestingProcessor: total: 133 ms, init: 36 ms, 2 round(s): 97 ms, 0 ms
com.udinic.aptplayground.processor.AnotherProcessor: total: 100 ms, init: 6 ms, 1 round(s): 93 ms

This is similar to the kotlinc options -Xreport-perf and -Xdump-perf, which are also providing a performance report printed to the log or dumped into a file, respectively.

I tested my changes in command line, by generating a new Kapt jar with these changes and then using it in a sample project that has Annotation Processors. Example command:

kotlinc -cp $MY_CLASSPATH \
-Xplugin=kotlin-annotation-processing-SNAPSHOT.jar -P \
plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt,\
plugin:org.jetbrains.kotlin.kapt3:apclasspath=processor/build/libs/processor.jar,\
plugin:org.jetbrains.kotlin.kapt3:dumpProcessorTimings=cli-tests/ap-perf-report.file \
-Xplugin=$JAVA_HOME/lib/tools.jar \
-d cli-tests/out \
-no-jdk -no-reflect -no-stdlib -verbose \
sample/src/main/

This command will run kapt and dump the stats to the file "cli-tests/ap-perf-report.file"

private fun dumpProcessorTiming(wrappedProcessors: List, apReportFile: File?, logger: (String) -> Unit) {
logger("Dumping Kapt Annotation Processing performance report to ${apReportFile?.absolutePath}")

apReportFile?.writeBytes(buildString {
Copy link
Contributor

Choose a reason for hiding this comment

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

File.writeText maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.
Note that I did it this way because that's how -Xdump-perf works (see code)

@@ -142,6 +144,17 @@ private fun showProcessorTimings(wrappedProcessors: List, logg
}
}

private fun dumpProcessorTiming(wrappedProcessors: List, apReportFile: File?, logger: (String) -> Unit) {
Copy link
Contributor

Choose a reason for hiding this comment

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

apReportFile can be non-nullable here, because you already check it's nullability in ?.let about

@abelkov abelkov added the KAPT label Apr 6, 2021
@Udinic
Copy link
Contributor Author

Udinic commented Apr 14, 2021

Hey @zveznicht, any other feedback on these changes?

@zveznicht
Copy link
Contributor

There are some failing integration tests, I'll into them later this week or next week

@Udinic
Copy link
Contributor Author

Udinic commented Apr 15, 2021

OK thanks for the update @zveznicht. Please let me know if I can help speed up the process.

Meanwhile, I'd like to generate modified jars with these changes for us to use. When I use the gradle "jar" task, the jars I'm getting are twice in size when compared to the original jars. Are you using the same gradle task to generate the jars you're shipping, or do you have some other process?

For more context, I'm generating the following jars:
kotlin-annotation-processing.jar
kotlin-annotation-processing-cli.jar
kotlin-annotation-processing-runtime.jar

I created these jars for both kotlin 1.4.21 and 1.4.32 by checking out the version's tag (v1.4.21-2 and v1.4.32, respectively) and then running the "jar" gradle task from within each module. I did this from IntelliJ and command line, in both cases the jar I'm getting is much bigger than the one that you ship with the kotlin distribution. I was thinking maybe you have a different method to do this.

@zveznicht
Copy link
Contributor

zveznicht commented Apr 21, 2021

When I use the gradle "jar" task, the jars I'm getting are twice in size when compared to the original jars

I believe by default jars doesn't get compressed. You can try to build with -Pteamcity=true

@zveznicht
Copy link
Contributor

Changes were commited to master in b8002cb

@zveznicht zveznicht closed this Apr 28, 2021
facebook-github-bot pushed a commit to facebook/buck that referenced this pull request May 4, 2021
…sor-timings option

Summary:
These modified Kapt library jars add support for a new parameter -Kapt-dump-processor-timings (or -P plugin:org.jetbrains.kotlin.kapt3:dumpProcessorTimings) for dumping performance stats of Annotation Processors into a file. In the next diffs, we'll use the new option in Buck to get the Annotation Processing perf stats and report them to Scuba.

The modified jars were generated from our forked Kotlin repo, with the changes added in D28120848.
Note: This change was upstreamed to Kotlin's repository ([Pull Request](JetBrains/kotlin#4280)) and will be available as part of the public release of Kotlin 1.5.

----------------------------------------------------------------------------
For future reference - in case we would need to generate jars for new Kotlin versions (until Kotlin 1.5 comes out), here's how to generate new jars:

Make sure our Kotlin fork repository is upgraded to the new Kotlin version (e.g. 1.4.99)

As part of the upgrade, all existing patches should be re-applied (see [wiki](https://www.internalfb.com/intern/wiki/Kotlin/Kotlin_IDE_Plugin_Development/#source-code-upgrade))

Generate the jars
```cd 
./gradlew :kotlin-annotation-processing:jar :kotlin-annotation-processing-runtime:jar :kotlin-annotation-processing-cli:jar -Pteamcity=true
```

Get the generated jars
```cp plugins/kapt3/kapt3-cli/build/libs/kotlin-annotation-processing-cli*.jar ~/tmp/kotlin-annotation-processing-cli.jar
cp plugins/kapt3/kapt3-compiler/build/libs/kotlin-annotation-processing*.jar ~/tmp/kotlin-annotation-processing.jar
cp plugins/kapt3/kapt3-runtime/build/libs/kotlin-annotation-processing-runtime*.jar ~/tmp/kotlin-annotation-processing-runtime.jar
```
Copy jars to
```fbsource/third-parth/kotlin//lib/```
and
```xplat/build_infra/buck_client/test/com/facebook/buck/toolchains/kotlin/kotlinc/libexec/lib/kotlin-annotation-processing.jar```

Create diffs

Reviewed By: amnn

fbshipit-source-id: 1a3baea7ac0d5d2b5f0a845357257a3bbcc65602
facebook-github-bot pushed a commit to facebook/buck that referenced this pull request May 4, 2021
Summary:
Adds support for Buck to read Annotation Processing performance report files, generated by Kotlin's Kapt plugin. This new step would run after compiling a Kotlin rule, parse the generated report file and send the data to be stored elsewhere using Buck's event bus.

Note: requires a new Kapt option that was added in this [Pull Request](JetBrains/kotlin#4280).

Example for a stats report:
```Kapt Annotation Processing performance report:
com.udinic.MetagenProcessor: total: 1650 ms, init: 39 ms, 3 round(s): 1611 ms, 0 ms, 0 ms
com.udinic.ComponentsProcessor: total: 11 ms, init: 11 ms, 0 round(s):
com.udinic.InjectorProcessor: total: 390 ms, init: 17 ms, 3 round(s): 293 ms, 47 ms, 33 ms
```

Reviewed By: IanChilds

fbshipit-source-id: fd31009eb6823c53347d43da044b21f3781c1f55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants