Skip to content

Commit fdc6228

Browse files
AkirathanFrizi
authored andcommitted
Check NI size after it is built (#12996)
* Add Enso test for NI size * better test clue * Revert "better test clue" This reverts commit cb94968. * Revert "Add Enso test for NI size" This reverts commit 48bf3cf. * NI size is checked in sbt during buildEngineDistribution * NativeImageSize constants are more visible, and platform specific
1 parent 9836060 commit fdc6228

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

build.sbt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ lazy val Benchmark = config("bench") extend sbt.Test
308308
lazy val rebuildNativeImage = taskKey[Unit]("Force to rebuild native image")
309309
lazy val buildNativeImage =
310310
taskKey[Unit]("Ensure that the Native Image is built.")
311+
lazy val checkNativeImageSize =
312+
taskKey[Unit]("Ensures the generated Native Image has reasonable size")
311313

312314
// ============================================================================
313315
// === Global Project =========================================================
@@ -4056,7 +4058,16 @@ lazy val `engine-runner` = project
40564058
"enso",
40574059
targetDir = engineDistributionRoot.value / "bin"
40584060
)
4059-
}.value
4061+
}.value,
4062+
checkNativeImageSize := Def
4063+
.taskDyn {
4064+
NativeImage.checkNativeImageSize(
4065+
name = "enso",
4066+
targetDir = engineDistributionRoot.value / "bin"
4067+
)
4068+
}
4069+
.dependsOn(buildNativeImage)
4070+
.value
40604071
)
40614072
.dependsOn(`version-output`)
40624073
.dependsOn(pkg)
@@ -5657,6 +5668,7 @@ buildEngineDistributionNoIndex := Def.taskIf {
56575668
createEnginePackageNoIndex.value
56585669
if (shouldBuildNativeImage.value) {
56595670
(`engine-runner` / buildNativeImage).value
5671+
(`engine-runner` / checkNativeImageSize).value
56605672
}
56615673
}.value
56625674

project/GraalVM.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,40 @@ object GraalVM {
7272
def release = native && !test && !debug && !fast && !disableLanguageServer
7373
}
7474

75+
case class NativeImageSize(
76+
minMb: Int,
77+
maxMb: Int
78+
)
79+
80+
object NativeImageSize {
81+
def expectedSizeForCurrentPlatform(): NativeImageSize = {
82+
if (EnsoLauncher.release) {
83+
if (Platform.isWindows) {
84+
windowsX64Release
85+
} else if (Platform.isLinux) {
86+
linuxX64Release
87+
} else if (Platform.isMacOS && Platform.isAmd64) {
88+
macX64Release
89+
} else if (Platform.isMacOS && Platform.isArm64) {
90+
macARM64Release
91+
} else {
92+
throw new IllegalArgumentException("Unexpected platform")
93+
}
94+
} else {
95+
testNISize
96+
}
97+
}
98+
99+
// Expected production NI sizes deduced from sizes on latest
100+
// nightly builds: https://github.com/enso-org/enso/pull/12996#discussion_r2073742680
101+
// With maximal size relaxed by 30 MB.
102+
private val windowsX64Release = NativeImageSize(200, 420)
103+
private val linuxX64Release = NativeImageSize(200, 463)
104+
private val macX64Release = NativeImageSize(200, 408)
105+
private val macARM64Release = NativeImageSize(200, 423)
106+
private val testNISize = NativeImageSize(100, 550)
107+
}
108+
75109
/** Has the user requested to use Espresso for Java interop? */
76110
private def isEspressoMode(): Boolean =
77111
"espresso".equals(System.getenv("ENSO_JAVA"))

project/NativeImage.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,37 @@ object NativeImage {
340340
}
341341
}
342342

343+
def checkNativeImageSize(
344+
name: String,
345+
targetDir: File
346+
): Def.Initialize[Task[Unit]] = Def.task {
347+
val generatedBin = artifactFile(targetDir, name)
348+
val logger = streams.value.log
349+
if (!generatedBin.exists) {
350+
logger.error(s"Generated binary $generatedBin does not exist.")
351+
logger.error(
352+
"Ensure that the dependency on `buildNativeImage` is properly set."
353+
)
354+
}
355+
val bytes = generatedBin.attributes.size()
356+
val mb = bytes / (1024 * 1024)
357+
val expectedSize = GraalVM.NativeImageSize.expectedSizeForCurrentPlatform()
358+
val isInBounds =
359+
expectedSize.minMb <= mb && mb <= expectedSize.maxMb
360+
if (!isInBounds) {
361+
logger.error(
362+
s"Generated binary $generatedBin has unexpected size: $mb MB. " +
363+
s"Expected size is between ${expectedSize.minMb} and ${expectedSize.maxMb} MB."
364+
)
365+
throw new RuntimeException(s"Generated binary $generatedBin is too large")
366+
} else {
367+
logger.info(
368+
s"Generated binary $generatedBin size ($mb MB) " +
369+
s"is within the expected size: [${expectedSize.minMb}, ${expectedSize.maxMb}] MB."
370+
)
371+
}
372+
}
373+
343374
/** [[File]] representing the artifact called `name` built with the Native
344375
* Image.
345376
*/

0 commit comments

Comments
 (0)