Skip to content
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

[Xamarin.Android.Build.Tasks] parse JDK release file directly #8663

Merged
merged 1 commit into from
Jan 23, 2024

Conversation

jonathanpeppers
Copy link
Member

Fixes: #8369

During Android project builds, the <ValidateJavaVersion/> MSBuild task currently shells out to java -version and javac -version to determine if we have a valid & usable JDK.

Unfortunately, some customer logs have reported build times that are completely bananas:

Task ValidateJavaVersion 23.469s
...
Top 10 most expensive tasks
...
ValidateJavaVersion = 3:46.740, 35 calls

java -version and/or javac -version must be quite slow! Reading the source for OpenJDK, it appears that java -version actually starts a JVM to call a single method:

That could explain the slowness?

To improve this, let's just parse the file:

  • C:\Program Files\Microsoft\jdk-11.0.19.7-hotspot\release

With contents:

IMPLEMENTOR="Microsoft"
IMPLEMENTOR_VERSION="Microsoft-7621296"
JAVA_VERSION="11.0.19"
JAVA_VERSION_DATE="2023-04-18"
LIBC="default"
MODULES="java.base java.compiler java.datatransfer java.xml java.prefs java.desktop java.instrument java.logging java.management java.security.sasl java.naming java.rmi java.management.rmi java.net.http java.scripting java.security.jgss java.transaction.xa java.sql java.sql.rowset java.xml.crypto java.se java.smartcardio jdk.accessibility jdk.internal.vm.ci jdk.management jdk.unsupported jdk.internal.vm.compiler jdk.aot jdk.internal.jvmstat jdk.attach jdk.charsets jdk.compiler jdk.crypto.ec jdk.crypto.cryptoki jdk.crypto.mscapi jdk.dynalink jdk.internal.ed jdk.editpad jdk.hotspot.agent jdk.httpserver jdk.internal.le jdk.internal.opt jdk.internal.vm.compiler.management jdk.jartool jdk.javadoc jdk.jcmd jdk.management.agent jdk.jconsole jdk.jdeps jdk.jdwp.agent jdk.jdi jdk.jfr jdk.jlink jdk.jshell jdk.jsobject jdk.jstatd jdk.localedata jdk.management.jfr jdk.naming.dns jdk.naming.ldap jdk.naming.rmi jdk.net jdk.pack jdk.rmic jdk.scripting.nashorn jdk.scripting.nashorn.shell jdk.sctp jdk.security.auth jdk.security.jgss jdk.unsupported.desktop jdk.xml.dom jdk.zipfs"
OS_ARCH="x86_64"
OS_NAME="Windows"
SOURCE=".:git:6171c19d2c18"

We can just call StreamReader.ReadLine() until JAVA_VERSION=" is found.

I kept the existing logic in place, with an escape hatch if it were ever needed. After a few .NET 9 previews, we could consider removing old logic entirely?

This logic is considerably faster, because we no longer launch a JVM!

  • Before: Task ValidateJavaVersion 377ms
  • After: Task ValidateJavaVersion 2ms

Where you can test the "Before" logic by passing -p:_AndroidUseJavaExeVersion=true.

I assume the customer's log from before (35 calls) would be around 70ms give or take instead of 3 hours and 46 minutes? Even if this estimation was 10x worse, it would still be a huge improvement! 👀

Fixes: dotnet#8369

During Android project builds, the `<ValidateJavaVersion/>` MSBuild task
currently shells out to `java -version` and `javac -version` to
determine if we have a *valid* & usable JDK.

Unfortunately, some customer logs have reported build times that are
completely bananas:

    Task ValidateJavaVersion 23.469s
    ...
    Top 10 most expensive tasks
    ...
    ValidateJavaVersion = 3:46.740, 35 calls

`java -version` and/or `javac -version` must be quite slow! Reading the
source for OpenJDK, it appears that `java -version` actually starts a
JVM to call a single method:

* https://github.com/openjdk/jdk/blob/df370d725e5ae55a05479e8375bf665233ac3e44/src/java.base/share/native/libjli/java.c#L1895-L1913
* https://github.com/openjdk/jdk/blob/c9cacfb25d1f15c879c961d2965a63c9fe4d9fa7/src/java.base/share/classes/java/lang/VersionProps.java.template#L191-L233

That could explain the slowness?

To improve this, let's just parse the file:

* C:\Program Files\Microsoft\jdk-11.0.19.7-hotspot\release

With contents:

    IMPLEMENTOR="Microsoft"
    IMPLEMENTOR_VERSION="Microsoft-7621296"
    JAVA_VERSION="11.0.19"
    JAVA_VERSION_DATE="2023-04-18"
    LIBC="default"
    MODULES="java.base java.compiler java.datatransfer java.xml java.prefs java.desktop java.instrument java.logging java.management java.security.sasl java.naming java.rmi java.management.rmi java.net.http java.scripting java.security.jgss java.transaction.xa java.sql java.sql.rowset java.xml.crypto java.se java.smartcardio jdk.accessibility jdk.internal.vm.ci jdk.management jdk.unsupported jdk.internal.vm.compiler jdk.aot jdk.internal.jvmstat jdk.attach jdk.charsets jdk.compiler jdk.crypto.ec jdk.crypto.cryptoki jdk.crypto.mscapi jdk.dynalink jdk.internal.ed jdk.editpad jdk.hotspot.agent jdk.httpserver jdk.internal.le jdk.internal.opt jdk.internal.vm.compiler.management jdk.jartool jdk.javadoc jdk.jcmd jdk.management.agent jdk.jconsole jdk.jdeps jdk.jdwp.agent jdk.jdi jdk.jfr jdk.jlink jdk.jshell jdk.jsobject jdk.jstatd jdk.localedata jdk.management.jfr jdk.naming.dns jdk.naming.ldap jdk.naming.rmi jdk.net jdk.pack jdk.rmic jdk.scripting.nashorn jdk.scripting.nashorn.shell jdk.sctp jdk.security.auth jdk.security.jgss jdk.unsupported.desktop jdk.xml.dom jdk.zipfs"
    OS_ARCH="x86_64"
    OS_NAME="Windows"
    SOURCE=".:git:6171c19d2c18"

We can just call `StreamReader.ReadLine()` until `JAVA_VERSION="` is
found.

I kept the existing logic in place, with an escape hatch if it were ever
needed. After a few .NET 9 previews, we could consider removing old
logic entirely?

This logic is considerably faster, because we no longer launch a JVM!

* Before: Task ValidateJavaVersion 377ms
* After: Task ValidateJavaVersion 2ms

Where you can test the "Before" logic by passing
`-p:_AndroidUseJavaExeVersion=true`.

I assume the customer's log from before (35 calls) would be around 70ms
give or take instead of 3 hours and 46 minutes? Even if this estimation
was 10x worse, it would still be a huge improvement! :eyes:
@jonathanpeppers
Copy link
Member Author

3 hours and 46 minutes

I might have misread and this is 3 minutes 46 seconds

@jonathanpeppers jonathanpeppers marked this pull request as ready for review January 22, 2024 23:13
@dellis1972 dellis1972 merged commit 766ac33 into dotnet:main Jan 23, 2024
47 checks passed
@jonathanpeppers jonathanpeppers deleted the ValidateJavaVersion branch January 23, 2024 14:22
grendello added a commit that referenced this pull request Jan 24, 2024
* main:
  LEGO: Merge pull request 8665
  [Xamarin.Android.Build.Tasks] parse JDK `release` file directly (#8663)
grendello added a commit that referenced this pull request Jan 25, 2024
* main:
  Localized file check-in by OneLocBuild Task (#8668)
  [Xamarin.Android.build.Tasks] `<CheckDuplicateJavaLibraries/>` ignores `repackaged.jar` (#8664)
  LEGO: Merge pull request 8665
  [Xamarin.Android.Build.Tasks] parse JDK `release` file directly (#8663)
  Bump to dotnet/installer@f91d4ca399 9.0.100-alpha.1.24070.3 (#8635)
  [.github] Re-enable locking issues after 30 days of inactivity (#8655)
  Localized file check-in by OneLocBuild Task (#8657)
  LEGO: Merge pull request 8656
  Localized file check-in by OneLocBuild Task (#8652)
  Bump to xamarin/xamarin-android-tools/main@b175674 (#8644)
  [Xamarin.Android.Build.Tasks] remove checks for `$(UsingAndroidNETSdk)` (#8647)
  [Xamarin.Android.Build.Tasks] XA1039 error for Android.Support (#8629)
@github-actions github-actions bot locked and limited conversation to collaborators Feb 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ValidateJavaVersion is expensive
2 participants