From 4bbf8c12bba953f5fe29965d49095378616e0e6c Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Wed, 11 Dec 2024 16:20:54 +0100 Subject: [PATCH] Fix excessive CPU and I/O load from terminfo polling thread --- .../mill/runner/client/MillProcessLauncher.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/runner/client/src/mill/runner/client/MillProcessLauncher.java b/runner/client/src/mill/runner/client/MillProcessLauncher.java index 95ccfa02e77..3aed5922159 100644 --- a/runner/client/src/mill/runner/client/MillProcessLauncher.java +++ b/runner/client/src/mill/runner/client/MillProcessLauncher.java @@ -9,6 +9,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import java.util.concurrent.atomic.AtomicReference; import mill.main.client.EnvVars; import mill.main.client.ServerFiles; import mill.main.client.Util; @@ -211,6 +212,8 @@ static int getTerminalDim(String s, boolean inheritError) throws Exception { return Integer.parseInt(new String(proc.getInputStream().readAllBytes()).trim()); } + private static AtomicReference memoizedTerminalDims = new AtomicReference(); + static void writeTerminalDims(boolean tputExists, Path serverDir) throws Exception { String str; @@ -227,7 +230,19 @@ static void writeTerminalDims(boolean tputExists, Path serverDir) throws Excepti } catch (Exception e) { str = "0 0"; } - Files.write(serverDir.resolve(ServerFiles.terminfo), str.getBytes()); + + // We memoize previously seen values to avoid causing lots + // of upstream work if the value hasn't actually changed. + // The upstream work could cause significant load, see + // + // https://github.com/com-lihaoyi/mill/discussions/4092 + // + // The cause is currently unknown, but this fixes the symptoms at least. + // + String oldValue = memoizedTerminalDims.getAndSet(str); + if ((oldValue == null) || !oldValue.equals(str)) { + Files.write(serverDir.resolve(ServerFiles.terminfo), str.getBytes()); + } } public static boolean checkTputExists() {