Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

Performance recorder: a stall report's cost is counted once, by CPU, not multiplied out

Victor7951db49bf80
diff --git a/Sources/Cra/Perf/MainLoopObserver.swift b/Sources/Cra/Perf/MainLoopObserver.swift
index 7194248..14df694 100644
--- a/Sources/Cra/Perf/MainLoopObserver.swift
+++ b/Sources/Cra/Perf/MainLoopObserver.swift
@@ -27,6 +27,7 @@ final class MainLoopObserver: @unchecked Sendable {
var callbacks = 0
var sampledNs: UInt64 = 0
var sampled = 0
+ var stallReportNs: UInt64 = 0
var reportedPass: UInt64 = 0
var reportedSlow: UInt64 = 0
/// Until the run loop first sleeps: AppKit finishes launching (Cra's bootstrap) inside
@@ -38,6 +39,11 @@ final class MainLoopObserver: @unchecked Sendable {
private let shared = OSAllocatedUnfairLock(initialState: Shared())
private var observers: [CFRunLoopObserver] = []
+ /// Main thread only: callbacks until the next timed one.
+ private var untilTimed = MainLoopObserver.timedEvery
+ /// One callback in this many is timed, for the self-cost estimate. Odd: the loop alternates
+ /// wake-ups and commits, and one in 256 only ever timed the same kind.
+ static let timedEvery = 255
private let stallMs: Double
private let slowMs: Double
private let hangMs: Double
@@ -96,19 +102,23 @@ final class MainLoopObserver: @unchecked Sendable {
func drain() -> Drained {
shared.withLock { s in
let estimate = s.sampled > 0 ? s.sampledNs / UInt64(s.sampled) * UInt64(s.callbacks) : 0
- let d = Drained(passes: s.passes, callbacks: s.callbacks, observerNs: estimate, watchdogNs: s.watchdogNs)
+ let d = Drained(passes: s.passes, callbacks: s.callbacks, observerNs: estimate &+ s.stallReportNs, watchdogNs: s.watchdogNs)
s.passes = LogHistogram()
s.callbacks = 0
s.sampledNs = 0
s.sampled = 0
+ s.stallReportNs = 0
s.watchdogNs = 0
return d
}
}
private func callback(_ activity: CFRunLoopActivity) {
+ untilTimed -= 1
+ let timed = untilTimed == 0
+ if timed { untilTimed = Self.timedEvery }
let now = Perf.now()
- let (ended, launch, timed): (Double?, Bool, Bool) = shared.withLock { s in
+ let (ended, launch): (Double?, Bool) = shared.withLock { s in
s.callbacks += 1
var ms: Double?
let launch = s.launching
@@ -126,14 +136,21 @@ final class MainLoopObserver: @unchecked Sendable {
s.passStart = activity == .beforeWaiting ? 0 : now
if activity == .beforeWaiting { s.launching = false }
}
- // One callback in 256 is timed, for the self-cost estimate.
- return (ms, launch, s.callbacks & 255 == 0)
+ return (ms, launch)
}
- if let ended { onStall(ended, now, launch ? "launch" : Self.currentMode()) }
+ // Timed and multiplied out: the bookkeeping every callback does, a clock read and a lock.
if timed {
let cost = Perf.now() &- now
shared.withLock { $0.sampledNs += cost; $0.sampled += 1 }
}
+ // A stall's report is rare and costs more, so it is counted in full, never multiplied
+ // out, and by the thread's CPU clock: time spent waiting or preempted in it is no cost.
+ if let ended {
+ let cpu = Self.threadCPU()
+ onStall(ended, now, launch ? "launch" : Self.currentMode())
+ let cost = Self.threadCPU() &- cpu
+ shared.withLock { $0.stallReportNs += cost }
+ }
}
private func watch() {
diff --git a/Tests/PerfTests.swift b/Tests/PerfTests.swift
index cb49871..7c02c3d 100644
--- a/Tests/PerfTests.swift
+++ b/Tests/PerfTests.swift
@@ -20,6 +20,7 @@ enum PerfTests {
aSlowLaunchIsAStallButNotAFreeze()
workAcrossATimerBoundaryIsOnePass()
theFirstSlowPassGetsAStack()
+ theObserversCostIsCPUNotWaiting()
print("Perf recorder: \(checks) checks passed")
}
@@ -318,6 +319,28 @@ enum PerfTests {
return out
}
+ /// The observer's self-cost counts the CPU its stall reports use, not time the main thread
+ /// spent waiting in one, and never multiplies a report out as if every callback made one.
+ /// Before, one callback in 256 was timed by the wall clock and multiplied by 256, a stall
+ /// report included. Here each stall report waits 2 ms off the CPU.
+ @MainActor static func theObserversCostIsCPUNotWaiting() {
+ let observer = MainLoopObserver(stallMs: 0, slowMs: 60_000, hangMs: 60_000,
+ onStall: { _, _, _ in usleep(2_000) }, onSlow: { _, _, _ in }, onHang: { _, _, _ in }, onWatch: {})
+ observer.start()
+ // One extra turn, so callbacks that end a pass (and report the stall) fall on the old
+ // one-in-256 timing too.
+ RunLoop.main.run(mode: .default, before: Date())
+ let timer = Timer(timeInterval: 0.001, repeats: true) { _ in }
+ RunLoop.main.add(timer, forMode: .common)
+ spin(1.0)
+ timer.invalidate()
+ observer.stop()
+ let d = observer.drain()
+ let perCallback = Double(d.observerNs) / Double(max(d.callbacks, 1))
+ expect(d.callbacks >= 512, "the loop turned often enough to time some callbacks (\(d.callbacks))")
+ expect(perCallback < 100_000, "a callback costs its CPU, not the 2 ms it waited (\(Int(perCallback)) ns each)")
+ }
+
private static func expect(_ condition: @autoclosure () -> Bool, _ message: String) {
guard condition() else {
FileHandle.standardError.write(Data("FAIL: \(message)\n".utf8))