Skip to content

Check NI size after it is built #12996

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

Merged
merged 6 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ lazy val Benchmark = config("bench") extend sbt.Test
lazy val rebuildNativeImage = taskKey[Unit]("Force to rebuild native image")
lazy val buildNativeImage =
taskKey[Unit]("Ensure that the Native Image is built.")
lazy val checkNativeImageSize =
taskKey[Unit]("Ensures the generated Native Image has reasonable size")

// ============================================================================
// === Global Project =========================================================
Expand Down Expand Up @@ -4064,7 +4066,16 @@ lazy val `engine-runner` = project
"enso",
targetDir = engineDistributionRoot.value / "bin"
)
}.value
}.value,
checkNativeImageSize := Def
.taskDyn {
NativeImage.checkNativeImageSize(
name = "enso",
targetDir = engineDistributionRoot.value / "bin"
)
}
.dependsOn(buildNativeImage)
.value
)
.dependsOn(`version-output`)
.dependsOn(pkg)
Expand Down Expand Up @@ -5671,6 +5682,7 @@ buildEngineDistributionNoIndex := Def.taskIf {
createEnginePackageNoIndex.value
if (shouldBuildNativeImage.value) {
(`engine-runner` / buildNativeImage).value
(`engine-runner` / checkNativeImageSize).value
}
}.value

Expand Down
37 changes: 37 additions & 0 deletions project/NativeImage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,43 @@ object NativeImage {
}
}

def checkNativeImageSize(
name: String,
targetDir: File
): Def.Initialize[Task[Unit]] = Def.task {
val generatedBin = artifactFile(targetDir, name)
val logger = streams.value.log
if (!generatedBin.exists) {
logger.error(s"Generated binary $generatedBin does not exist.")
logger.error(
"Ensure that the dependency on `buildNativeImage` is properly set."
)
}
val bytes = generatedBin.attributes.size()
Copy link
Member

Choose a reason for hiding this comment

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

  • the constant should be more prominent
  • def GraalVM.EnsoLauncher.maxImageSizeInMb = 450 maybe?
  • it shouldn't be hidden here

val productionNIBounds = (150, 450)
Copy link
Member Author

Choose a reason for hiding this comment

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

Based on latest nightly build:

  • Linux production NI has 433 MB
  • Windows production NI has 390 MB
  • MacOS aarch64 production NI has 393 MB
  • MacOS x64 production NI has 378 MB

Copy link
Member

Choose a reason for hiding this comment

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

Maybe the maxImageSizeInMb function should return different value for different OS to allow incremental individual improvements on each platform

val testNIBounds = (110, 550)
val mb = bytes / (1024 * 1024)
val bounds = if (GraalVM.EnsoLauncher.release) {
productionNIBounds
} else {
testNIBounds
}
val isInBounds =
bounds._1 <= mb && mb <= bounds._2
if (!isInBounds) {
logger.error(
s"Generated binary $generatedBin has unexpected size: $mb MB. " +
s"Expected size is between ${bounds._1} and ${bounds._2} MB."
)
throw new RuntimeException(s"Generated binary $generatedBin is too large")
} else {
logger.info(
s"Generated binary $generatedBin size ($mb MB) " +
s"is within the expected size: [${bounds._1}, ${bounds._2}] MB."
)
}
}

/** [[File]] representing the artifact called `name` built with the Native
* Image.
*/
Expand Down
Loading