package buildlogic import com.sun.management.OperatingSystemMXBean import java.io.IOException import java.lang.management.ManagementFactory import java.nio.charset.StandardCharsets import java.util.concurrent.TimeUnit import java.util.regex.Pattern object TestHeapPolicy { private const val MIB = 1L shl 20 private const val JVM_MINIMUM_HEAP_MIB = 2L private val PAGE_SIZE = Pattern.compile( "Mach Virtual Memory Statistics:\\s*\\(page size of ([0-9]+) bytes\\)" ) private val PAGE_COUNTER = Pattern.compile("Pages (free|inactive)\\s*:\\s*([0-9]+)\\.?") @JvmStatic fun calculateHeapMiB(availableBytes: Long, maxParallelForks: Int, maxWorkers: Int): Long { require(availableBytes > 0 && maxParallelForks > 0 && maxWorkers > 0) { "Available bytes, maxParallelForks and maxWorkers must be positive" } val effectiveWorkers = minOf(maxParallelForks, maxWorkers) val perWorkerBytes = availableBytes / 2 / effectiveWorkers val nativeReserveBytes = (perWorkerBytes + 3) / 4 val heapMiB = (perWorkerBytes - nativeReserveBytes) / MIB check(heapMiB >= JVM_MINIMUM_HEAP_MIB) { "Insufficient test memory: availableBytes=$availableBytes, effectiveWorkers=$effectiveWorkers" + ", heapMiB=$heapMiB. JVM requires at least 2 MiB. Close other applications or reduce workers." } return heapMiB } @JvmStatic fun readAvailableBytes(): Long { val availableBytes = if (System.getProperty("os.name").startsWith("Mac")) { val builder = ProcessBuilder("/usr/bin/vm_stat").redirectErrorStream(true) builder.environment()["LC_ALL"] = "C" try { val process = builder.start() try { process.inputStream.use { output -> process.errorStream.use { process.outputStream.use { check(process.waitFor(5, TimeUnit.SECONDS)) { "vm_stat did not finish within 5 seconds" } check(process.exitValue() == 0) { "vm_stat failed with exit code ${process.exitValue()}" } parseMacVmStat(String(output.readAllBytes(), StandardCharsets.US_ASCII)) } } } } finally { process.destroyForcibly() } } catch (failure: InterruptedException) { Thread.currentThread().interrupt() throw IllegalStateException("Interrupted while reading available memory with vm_stat", failure) } catch (failure: IOException) { throw IllegalStateException("Unable to read available memory with vm_stat", failure) } } else { val operatingSystem = checkNotNull(ManagementFactory.getPlatformMXBean(OperatingSystemMXBean::class.java)) { "OperatingSystemMXBean free-memory observation is unavailable" } operatingSystem.freeMemorySize } check(availableBytes > 0) { "Available-memory observation must be positive: availableBytes=$availableBytes" } return availableBytes } internal fun parseMacVmStat(output: String): Long { var pageSize = -1L var freePages = -1L var inactivePages = -1L try { for (rawLine in output.lineSequence()) { val line = rawLine.trim { Character.isWhitespace(it) } if (line.startsWith("Mach Virtual Memory Statistics")) { val header = PAGE_SIZE.matcher(line) require(pageSize < 0 && header.matches()) { "Duplicate or malformed vm_stat page-size header: $line" } pageSize = header.group(1).toLong() } else if (line.startsWith("Pages free") || line.startsWith("Pages inactive")) { val counter = PAGE_COUNTER.matcher(line) require(counter.matches()) { "Malformed vm_stat page counter: $line" } val pages = counter.group(2).toLong() if (counter.group(1) == "free") { require(freePages < 0) { "Duplicate vm_stat Pages free counter" } freePages = pages } else { require(inactivePages < 0) { "Duplicate vm_stat Pages inactive counter" } inactivePages = pages } } } require(pageSize > 0 && freePages >= 0 && inactivePages >= 0) { "vm_stat requires a positive page size and both free and inactive counters" } // Raw free already includes speculative pages; do not subtract or add them separately. return Math.multiplyExact(Math.addExact(freePages, inactivePages), pageSize) } catch (failure: NumberFormatException) { throw IllegalArgumentException("vm_stat page size or counters exceed the supported long range", failure) } catch (failure: ArithmeticException) { throw IllegalArgumentException("vm_stat page size or counters exceed the supported long range", failure) } } }