From 3877b95e4e3952635dce45a666a84b7891a83550 Mon Sep 17 00:00:00 2001 From: Austin Wang Date: Wed, 30 Sep 2026 11:22:44 -0700 Subject: [PATCH] test: bound the footprint check by reads on both sides of the capture testSummaryPayloadIncludesPhysicalFootprintMemoryBytes compared the snapshot's memory_bytes with a footprint read taken before the capture. Since #13014 the capture is an async request to the app's shared snapshot service, which may wait behind the host's own census users while the host keeps allocating and freeing. The capture's own footprint read therefore lands some time after that reference. In #15488's validation run the host footprint moved 112 MiB (7 x 16 MiB) in that gap, past the 20% tolerance. The test now reads the footprint again after the capture and requires memory_bytes to be within the same tolerance of the range the two reads span. With no drift the accepted range is unchanged. It also requires memory_source_fallback_pids to be empty, which shows the value came from ri_phys_footprint rather than the resident-size fallback. Refs #15488 Co-Authored-By: Claude Opus 5.5 --- cmuxTests/CmuxTopSnapshotScopeTests.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmuxTests/CmuxTopSnapshotScopeTests.swift b/cmuxTests/CmuxTopSnapshotScopeTests.swift index c5d332d00bf3..f9acd754b340 100644 --- a/cmuxTests/CmuxTopSnapshotScopeTests.swift +++ b/cmuxTests/CmuxTopSnapshotScopeTests.swift @@ -271,14 +271,18 @@ struct CmuxTopSnapshotScopeTests { @Test func testSummaryPayloadIncludesPhysicalFootprintMemoryBytes() async throws { let pid = Int(Darwin.getpid()) - let expectedFootprintBytes = try #require(physicalFootprintBytes(for: pid), "proc_pid_rusage did not return physical footprint for current process") - + // The capture samples this process between these two reads, while the app host keeps allocating. + let footprintBeforeCapture = try #require(physicalFootprintBytes(for: pid), "proc_pid_rusage did not return physical footprint for current process") let snapshot = await CmuxTopProcessSnapshot.capture(includeProcessDetails: false) + let footprintAfterCapture = try #require(physicalFootprintBytes(for: pid), "proc_pid_rusage did not return physical footprint for current process") let payload = snapshot.summaryPayload(for: [pid]) let memoryBytes = int64(payload["memory_bytes"]) + let (lowest, highest) = (min(footprintBeforeCapture, footprintAfterCapture), max(footprintBeforeCapture, footprintAfterCapture)) #expect((memoryBytes) > (0)) - #expect((abs(memoryBytes - expectedFootprintBytes)) <= (max(16 * 1024 * 1024, expectedFootprintBytes / 5))) + #expect(intArray(payload["memory_source_fallback_pids"]).isEmpty) + #expect((memoryBytes) >= (lowest - max(16 * 1024 * 1024, lowest / 5))) + #expect((memoryBytes) <= (highest + max(16 * 1024 * 1024, highest / 5))) } @Test func testSamplePayloadDescribesPhysicalFootprintFallbackSource() async { @@ -887,11 +891,7 @@ while allocations: // proc_pid_rusage imports as rusage_info_t *; callers pass the concrete // rusage struct address cast to that opaque buffer type. let buffer = baseAddress.assumingMemoryBound(to: rusage_info_t?.self) - return proc_pid_rusage( - pid_t(pid), - RUSAGE_INFO_V2, - buffer - ) + return proc_pid_rusage(pid_t(pid), RUSAGE_INFO_V2, buffer) } guard result == 0 else { return nil } return int64Clamped(info.ri_phys_footprint)