diff --git a/buildSrc/src/main/kotlin/buildlogic/TestHeapPolicy.kt b/buildSrc/src/main/kotlin/buildlogic/TestHeapPolicy.kt index a6d0bf75..57784739 100644 --- a/buildSrc/src/main/kotlin/buildlogic/TestHeapPolicy.kt +++ b/buildSrc/src/main/kotlin/buildlogic/TestHeapPolicy.kt @@ -1,9 +1,12 @@ package buildlogic import com.sun.management.OperatingSystemMXBean +import org.gradle.api.logging.Logging import java.io.IOException import java.lang.management.ManagementFactory import java.nio.charset.StandardCharsets +import java.nio.file.Files +import java.nio.file.Path import java.util.concurrent.TimeUnit import java.util.regex.Pattern @@ -14,6 +17,7 @@ object TestHeapPolicy { "Mach Virtual Memory Statistics:\\s*\\(page size of ([0-9]+) bytes\\)" ) private val PAGE_COUNTER = Pattern.compile("Pages (free|inactive)\\s*:\\s*([0-9]+)\\.?") + private val LINUX_MEMORY_COUNTER = Pattern.compile("(MemTotal|MemAvailable):\\s+([0-9]+)\\s+kB") @JvmStatic fun calculateHeapMiB(availableBytes: Long, maxParallelForks: Int, maxWorkers: Int): Long { @@ -34,7 +38,8 @@ object TestHeapPolicy { @JvmStatic fun readAvailableBytes(): Long { - val availableBytes = if (System.getProperty("os.name").startsWith("Mac")) { + val osName = System.getProperty("os.name") + val availableBytes = if (osName.startsWith("Mac")) { val builder = ProcessBuilder("/usr/bin/vm_stat").redirectErrorStream(true) builder.environment()["LC_ALL"] = "C" try { @@ -66,7 +71,25 @@ object TestHeapPolicy { val operatingSystem = checkNotNull(ManagementFactory.getPlatformMXBean(OperatingSystemMXBean::class.java)) { "OperatingSystemMXBean free-memory observation is unavailable" } - operatingSystem.freeMemorySize + if (osName == "Linux") { + val memInfo = try { + Files.readString(Path.of("/proc/meminfo"), StandardCharsets.US_ASCII) + } catch (failure: IOException) { + throw IllegalStateException("Unable to read Linux available memory from /proc/meminfo", failure) + } + val jvmTotalBytes = operatingSystem.totalMemorySize + val jvmFreeBytes = operatingSystem.freeMemorySize + val available = resolveLinuxAvailableBytes(memInfo, jvmTotalBytes, jvmFreeBytes) + Logging.getLogger(TestHeapPolicy::class.java).lifecycle( + "Test memory (Linux): JVM total={} MiB, JVM free={} MiB, effective available={} MiB", + jvmTotalBytes / MIB, + jvmFreeBytes / MIB, + available / MIB + ) + available + } else { + operatingSystem.freeMemorySize + } } check(availableBytes > 0) { "Available-memory observation must be positive: availableBytes=$availableBytes" @@ -74,6 +97,41 @@ object TestHeapPolicy { return availableBytes } + internal fun resolveLinuxAvailableBytes(memInfo: String, jvmTotalBytes: Long, jvmFreeBytes: Long): Long { + var hostTotalBytes = -1L + var hostAvailableBytes = -1L + try { + for (rawLine in memInfo.lineSequence()) { + val line = rawLine.trim() + if (line.startsWith("MemTotal:") || line.startsWith("MemAvailable:")) { + val counter = LINUX_MEMORY_COUNTER.matcher(line) + require(counter.matches()) { "Malformed Linux memory counter: $line" } + val bytes = Math.multiplyExact(counter.group(2).toLong(), 1024L) + if (counter.group(1) == "MemTotal") { + require(hostTotalBytes < 0) { "Duplicate Linux MemTotal counter" } + hostTotalBytes = bytes + } else { + require(hostAvailableBytes < 0) { "Duplicate Linux MemAvailable counter" } + hostAvailableBytes = bytes + } + } + } + } catch (failure: NumberFormatException) { + throw IllegalArgumentException("Linux memory counters exceed the supported long range", failure) + } catch (failure: ArithmeticException) { + throw IllegalArgumentException("Linux memory counters exceed the supported long range", failure) + } + require(hostTotalBytes > 0 && hostAvailableBytes >= 0 && hostAvailableBytes <= hostTotalBytes) { + "Linux /proc/meminfo requires positive MemTotal and MemAvailable within that total" + } + check(jvmTotalBytes > 0) { "JVM total-memory observation must be positive" } + if (jvmTotalBytes < hostTotalBytes) { + check(jvmFreeBytes >= 0) { "Container free-memory observation must not be negative" } + return minOf(hostAvailableBytes, jvmFreeBytes, jvmTotalBytes) + } + return hostAvailableBytes + } + internal fun parseMacVmStat(output: String): Long { var pageSize = -1L var freePages = -1L diff --git a/buildSrc/src/test/kotlin/buildlogic/LinuxAvailableMemoryTest.kt b/buildSrc/src/test/kotlin/buildlogic/LinuxAvailableMemoryTest.kt new file mode 100644 index 00000000..706c195a --- /dev/null +++ b/buildSrc/src/test/kotlin/buildlogic/LinuxAvailableMemoryTest.kt @@ -0,0 +1,109 @@ +package buildlogic + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.DynamicTest +import org.junit.jupiter.api.DynamicTest.dynamicTest +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestFactory + +class LinuxAvailableMemoryTest { + @Test + fun shouldUseReclaimableHostMemoryInsteadOfTheReportedJenkinsFreeMemory() { + val available = TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(), 16 * GIB, 1315 * MIB) + + assertEquals(10 * GIB, available) + assertEquals(3840L, TestHeapPolicy.calculateHeapMiB(available, 1, 1)) + } + + @Test + fun shouldAllowHostMemoryWhenJvmTotalIsLargerThanProcTotal() { + assertEquals(10 * GIB, TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(), 16 * GIB + 4096, 1315 * MIB)) + } + + @Test + fun shouldNotTreatZeroHostFreePagesAsZeroAvailableMemory() { + assertEquals(10 * GIB, TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(), 16 * GIB, 0)) + } + + @Test + fun shouldCapAvailableMemoryByRecognizedContainerHeadroom() { + assertEquals(3 * GIB, TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(), 4 * GIB, 3 * GIB)) + } + + @Test + fun shouldRespectHostPressureEvenWhenTheContainerHasMoreHeadroom() { + assertEquals(GIB, TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(availableKiB = GIB / 1024), 4 * GIB, 3 * GIB)) + } + + @Test + fun shouldNeverExceedTheRecognizedContainerTotal() { + assertEquals(4 * GIB, TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(), 4 * GIB, 8 * GIB)) + } + + @Test + fun shouldPreserveZeroAvailableMemoryWithoutFallingBackToHostFree() { + assertEquals(0L, TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(availableKiB = 0), 16 * GIB, 1315 * MIB)) + assertEquals(0L, TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(), 4 * GIB, 0)) + } + + @Test + fun shouldRejectUnknownRemainingMemoryForARecognizedContainerLimit() { + assertThrows(IllegalStateException::class.java) { + TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(), 4 * GIB, -1) + } + } + + @TestFactory + fun shouldRejectUnknownJvmTotalMemory(): List = listOf(0L, -1L).map { total -> + dynamicTest("JVM total $total") { + assertThrows(IllegalStateException::class.java) { + TestHeapPolicy.resolveLinuxAvailableBytes(memInfo(), total, GIB) + } + } + } + + @Test + fun shouldParseReorderedKiBCountersWithoutAddingCachedOrSwapPages() { + val input = "MemAvailable:\t3072 kB\nCached: 999999 kB\nSwapFree: 999999 kB\n MemTotal: 4096 kB\n" + assertEquals(3 * MIB, TestHeapPolicy.resolveLinuxAvailableBytes(input, 4 * MIB, MIB)) + } + + @TestFactory + fun shouldRejectMissingDuplicateMalformedOrOverflowingProcCounters(): List { + val valid = memInfo() + return listOf( + "", + "MemTotal: 16777216 kB\n", + "MemAvailable: 10485760 kB\n", + valid + "MemTotal: 16777216 kB\n", + valid + "MemAvailable: 10485760 kB\n", + valid.replace("16777216 kB", "0 kB"), + valid.replace("10485760 kB", "16777217 kB"), + valid.replace("10485760 kB", "-1 kB"), + valid.replace("10485760 kB", "1.5 kB"), + valid.replace("10485760 kB", "invalid kB"), + valid.replace("10485760 kB", "10485760 MB"), + valid.replace("10485760 kB", "10485760"), + valid.replace("10485760 kB", "10485760 kB trailing"), + valid.replace("16777216 kB", "9223372036854775808 kB"), + valid.replace("10485760 kB", "9223372036854775808 kB"), + valid.replace("16777216 kB", "9007199254740992 kB"), + valid.replace("10485760 kB", "9007199254740992 kB") + ).map { input -> + dynamicTest("rejects ${input.replace('\n', ' ')}") { + assertThrows(IllegalArgumentException::class.java) { + TestHeapPolicy.resolveLinuxAvailableBytes(input, 16 * GIB, GIB) + } + } + } + } + + private fun memInfo(availableKiB: Long = 10 * GIB / 1024): String = + "MemTotal: 16777216 kB\nMemFree: 1346560 kB\nMemAvailable: $availableKiB kB\n" + + companion object { + private const val MIB = 1_048_576L + private const val GIB = 1_073_741_824L + } +}