diff --git a/packages/serve-sim/src/__tests__/grid-memory-endpoint.test.ts b/packages/serve-sim/src/__tests__/grid-memory-endpoint.test.ts new file mode 100644 index 000000000..add753f13 --- /dev/null +++ b/packages/serve-sim/src/__tests__/grid-memory-endpoint.test.ts @@ -0,0 +1,102 @@ +import { afterEach, beforeEach, expect, test } from "bun:test"; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs"; +import { tmpdir } from "os"; +import { delimiter, join } from "path"; +import { simMiddleware } from "../middleware"; +import { servePreview, type PreviewServer } from "../runtime"; +import { freePort, waitFor } from "./server-test-support"; + +// The devices sidebar (open by default on wide windows) polls +// /grid/api/memory every 5 s. The handler shells out to `ps -axo rss=,args=`, +// which takes seconds on a loaded host. The preview server's event loop also +// delivers the simulator's MJPEG/AVCC frames, so a synchronous `ps` froze every +// stream for that long and the viewer's 2 s staleness watchdog flipped to +// "Connecting…" (#103). These fakes make `ps` block until the test releases it. + +const UDID = "00000000-0000-4000-8000-000000000103"; +let root = ""; +let preview: PreviewServer | null = null; +const originalPath = process.env.PATH; + +beforeEach(() => { + root = mkdtempSync(join(tmpdir(), "serve-sim-grid-memory-")); + const bin = join(root, "bin"); + mkdirSync(bin); + writeFileSync(join(bin, "ps"), `#!/bin/sh +echo run >> "${root}/ps-runs" +printf started > "${root}/ps-started" +i=0 +while [ ! -e "${root}/ps-release" ] && [ $i -lt 100 ]; do sleep 0.02; i=$((i+1)); done +printf finished > "${root}/ps-finished" +echo " 524288 /Users/me/Library/Developer/CoreSimulator/Devices/${UDID}/data/launchd_sim" +`, { mode: 0o700 }); + writeFileSync(join(bin, "sysctl"), `#!/bin/sh +case "$*" in + "-n hw.memsize") echo 17179869184 ;; + "-n hw.pagesize") echo 16384 ;; + *) exit 88 ;; +esac +`, { mode: 0o700 }); + writeFileSync(join(bin, "vm_stat"), `#!/bin/sh +echo "Pages free: 1000." +echo "Pages inactive: 2000." +echo "Pages speculative: 500." +`, { mode: 0o700 }); + process.env.PATH = `${bin}${delimiter}${originalPath ?? ""}`; +}); + +afterEach(() => { + try { writeFileSync(join(root, "ps-release"), "release"); } catch {} + preview?.stop(true); + preview = null; + process.env.PATH = originalPath; + rmSync(root, { recursive: true, force: true }); +}); + +async function startPreview(): Promise { + const port = await freePort(); + preview = await servePreview({ + port, + middleware: simMiddleware({ basePath: "/", proxyHelpers: false }), + }); + return `http://127.0.0.1:${port}`; +} + +test.skipIf(process.platform === "win32")("serves other requests while the memory probe's ps is pending", async () => { + const origin = await startPreview(); + const startedAt = performance.now(); + let memoryDone = false; + const memory = fetch(`${origin}/grid/api/memory`).finally(() => { memoryDone = true; }); + await waitFor(() => existsSync(join(root, "ps-started")), 3_000, 10); + + const probe = await fetch(`${origin}/__grid_memory_probe__`, { signal: AbortSignal.timeout(5_000) }); + await probe.arrayBuffer(); + const elapsed = performance.now() - startedAt; + + // A synchronous `ps` blocks the loop until it exits or its timeout kills it + // (seconds); an async one leaves the server free to answer right away. + expect(elapsed).toBeLessThan(1_000); + expect(memoryDone).toBe(false); + + writeFileSync(join(root, "ps-release"), "release"); + const res = await memory; + expect(res.status).toBe(200); + const report = await res.json() as Record; + expect(report.totalBytes).toBe(17179869184); + expect(report.availableBytes).toBe(3500 * 16384); + expect(report.runningSimulators).toBe(1); + expect(report.perSimAvgBytes).toBe(524288 * 1024); + expect(report.perSimSource).toBe("measured"); +}, 10_000); + +test.skipIf(process.platform === "win32")("concurrent memory polls share one ps run", async () => { + const origin = await startPreview(); + const first = fetch(`${origin}/grid/api/memory`); + await waitFor(() => existsSync(join(root, "ps-started")), 3_000, 10); + const second = fetch(`${origin}/grid/api/memory`); + await new Promise((resolve) => setTimeout(resolve, 100)); + writeFileSync(join(root, "ps-release"), "release"); + const [a, b] = await Promise.all([first, second]); + expect(await a.json()).toEqual(await b.json()); + expect(readFileSync(join(root, "ps-runs"), "utf8").trim().split("\n")).toHaveLength(1); +}, 10_000); diff --git a/packages/serve-sim/src/middleware.ts b/packages/serve-sim/src/middleware.ts index de5a9af78..b0023b2e6 100644 --- a/packages/serve-sim/src/middleware.ts +++ b/packages/serve-sim/src/middleware.ts @@ -3,6 +3,7 @@ import { execSync, spawn, exec, execFile, type ChildProcess, type ExecException import { tmpdir } from "os"; import { dirname, join } from "path"; import { fileURLToPath } from "url"; +import { promisify } from "util"; import { createServer as createNetServer } from "net"; import { createHash, randomBytes, timingSafeEqual } from "crypto"; import type { IncomingMessage, ServerResponse } from "http"; @@ -1288,27 +1289,29 @@ interface MemoryReport { estimatedAdditional: number; } -function readSystemMemory(): { totalBytes: number; availableBytes: number } { +// Async on purpose: this route is polled every 5 s by the devices sidebar, and +// the preview's event loop also delivers the simulator's MJPEG/AVCC frames. +// `ps` over every process can take seconds on a busy host; run synchronously it +// froze every stream for that long and viewers fell back to "Connecting…" (#103). +const execFileAsync = promisify(execFile); +async function runText(file: string, args: string[], timeout: number): Promise { + const { stdout } = await execFileAsync(file, args, { + encoding: "utf-8", + timeout, + maxBuffer: 8 * 1024 * 1024, + }); + return stdout; +} + +async function readSystemMemory(): Promise<{ totalBytes: number; availableBytes: number }> { try { - const totalBytes = Number( - execSync("sysctl -n hw.memsize", { - encoding: "utf-8", - stdio: ["ignore", "pipe", "ignore"], - timeout: 1500, - }).trim(), - ); - const pageSize = Number( - execSync("sysctl -n hw.pagesize", { - encoding: "utf-8", - stdio: ["ignore", "pipe", "ignore"], - timeout: 1500, - }).trim(), - ); - const vmStat = execSync("vm_stat", { - encoding: "utf-8", - stdio: ["ignore", "pipe", "ignore"], - timeout: 1500, - }); + const [memsize, pagesize, vmStat] = await Promise.all([ + runText("sysctl", ["-n", "hw.memsize"], 1500), + runText("sysctl", ["-n", "hw.pagesize"], 1500), + runText("vm_stat", [], 1500), + ]); + const totalBytes = Number(memsize.trim()); + const pageSize = Number(pagesize.trim()); const pages = (re: RegExp) => { const m = vmStat.match(re); return m ? Number(m[1]) : 0; @@ -1331,14 +1334,9 @@ function readSystemMemory(): { totalBytes: number; availableBytes: number } { // Sum RSS across every process whose argv path includes a CoreSimulator // device directory. Groups by UDID so we get a real per-sim footprint that // covers launchd_sim plus all child processes the runtime spawns. -function readSimulatorMemoryUsage(): { perUdid: Record; totalBytes: number } { +async function readSimulatorMemoryUsage(): Promise<{ perUdid: Record; totalBytes: number }> { try { - const output = execSync("ps -axo rss=,args=", { - encoding: "utf-8", - stdio: ["ignore", "pipe", "ignore"], - timeout: 3000, - maxBuffer: 8 * 1024 * 1024, - }); + const output = await runText("ps", ["-axo", "rss=,args="], 3000); const perUdid: Record = {}; let totalBytes = 0; const re = /\/Devices\/([0-9A-F-]{36})\//i; @@ -1360,9 +1358,11 @@ function readSimulatorMemoryUsage(): { perUdid: Record; totalByt } } -function buildMemoryReport(): MemoryReport { - const { totalBytes, availableBytes } = readSystemMemory(); - const usage = readSimulatorMemoryUsage(); +async function buildMemoryReport(): Promise { + const [{ totalBytes, availableBytes }, usage] = await Promise.all([ + readSystemMemory(), + readSimulatorMemoryUsage(), + ]); const runningSimulators = Object.keys(usage.perUdid).length; const measuredAvg = runningSimulators > 0 ? usage.totalBytes / runningSimulators @@ -1387,6 +1387,15 @@ function buildMemoryReport(): MemoryReport { }; } +// Several open tabs (or a slow `ps`) must not stack concurrent `ps` runs. +let memoryReportInFlight: Promise | null = null; +function sharedMemoryReport(): Promise { + memoryReportInFlight ??= buildMemoryReport().finally(() => { + memoryReportInFlight = null; + }); + return memoryReportInFlight; +} + export interface SimMiddlewareOptions { /** Base path to serve the preview at. Default: "/.sim" */ basePath?: string; @@ -1787,11 +1796,14 @@ export function simMiddleware(options?: SimMiddlewareOptions): SimMiddleware { // Memory capacity estimate: how much room is left to boot more sims. if (url === base + "/grid/api/memory") { - res.writeHead(200, { - "Content-Type": "application/json", - "Cache-Control": "no-store", + void sharedMemoryReport().then((report) => { + if (res.writableEnded || res.destroyed) return; + res.writeHead(200, { + "Content-Type": "application/json", + "Cache-Control": "no-store", + }); + res.end(JSON.stringify(report)); }); - res.end(JSON.stringify(buildMemoryReport())); return; }