Repository navigation
Fix Task Manager CPU samples #3588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
500fb5e
Prove task manager CPU samples need live deltas
austinywang 57d698c
Compute task manager CPU from task deltas
austinywang 4af3b93
Keep CPU sampler fix current with main CI updates
austinywang 06ba409
Stabilize concurrent CPU sample history
austinywang 4ce6267
Merge remote-tracking branch 'origin/main' into issue-3583-task-manag…
austinywang f3d3b8c
Preserve synchronous top sampling without racing CPU history
austinywang 22cbf3a
Make CPU sample pruning monotonic across captures
austinywang 5ca62d7
Preserve CPU tracker ordering under review feedback
austinywang 3e3a86d
Merge remote-tracking branch 'origin/main' into issue-3583-task-manag…
austinywang c53992e
Clarify CPU tracker state isolation
austinywang fc7db89
Make top snapshot isolation explicit
austinywang d3fb088
Remove fragile CPU snapshot joins
austinywang a2603c2
Keep process-detail helpers private
austinywang fbe5eeb
Merge remote-tracking branch 'origin/main' into issue-3583-task-manag…
austinywang 7c71267
Prevent stale PID scope cache entries
austinywang af82466
Merge remote-tracking branch 'origin/main' into issue-3583-task-manag…
austinywang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import Darwin | ||
| import Foundation | ||
| import os | ||
|
|
||
| nonisolated struct CmuxTopProcessCPUSample: Sendable { | ||
| let totalTimeTicks: UInt64 | ||
| let sampledAtNanoseconds: UInt64 | ||
| } | ||
|
|
||
| private nonisolated struct CmuxTopProcessCPUTrackerState: Sendable { | ||
| var samples: [CmuxTopProcessScopeCacheKey: CmuxTopProcessCPUSample] = [:] | ||
| var latestPrunedAtNanoseconds: UInt64 = 0 | ||
| } | ||
|
|
||
| private nonisolated final class CmuxTopProcessCPUTracker: @unchecked Sendable { | ||
| private let state = OSAllocatedUnfairLock(initialState: CmuxTopProcessCPUTrackerState()) | ||
|
|
||
| // Snapshot capture is synchronous for the v2 socket path, so an actor would | ||
| // force that caller to block on async state. Keep OS sampling outside this | ||
| // owner and serialize only the CPU history read/compute/write transaction. | ||
| func cpuPercentages( | ||
| for currentSamples: [CmuxTopProcessScopeCacheKey: CmuxTopProcessCPUSample], | ||
| activeKeys: Set<CmuxTopProcessScopeCacheKey>, | ||
| sampledAtNanoseconds: UInt64 | ||
| ) -> [CmuxTopProcessScopeCacheKey: Double] { | ||
| state.withLock { state in | ||
| var percentages: [CmuxTopProcessScopeCacheKey: Double] = [:] | ||
| percentages.reserveCapacity(currentSamples.count) | ||
|
|
||
| for (key, sample) in currentSamples { | ||
| let existing = state.samples[key] | ||
| if let existing, | ||
| existing.sampledAtNanoseconds > sample.sampledAtNanoseconds { | ||
| continue | ||
| } | ||
|
|
||
| percentages[key] = CmuxTopProcessSnapshot.cpuPercent( | ||
| current: sample, | ||
| previous: existing | ||
| ) | ||
| state.samples[key] = sample | ||
| } | ||
|
|
||
| // Overlapping captures can finish out of sample-time order; only | ||
| // the newest completed capture is allowed to evict inactive keys. | ||
| if sampledAtNanoseconds >= state.latestPrunedAtNanoseconds { | ||
| state.latestPrunedAtNanoseconds = sampledAtNanoseconds | ||
| state.samples = state.samples.filter { entry in | ||
| activeKeys.contains(entry.key) | ||
| } | ||
| } | ||
|
|
||
| return percentages | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private nonisolated let cmuxTopProcessCPUTracker = CmuxTopProcessCPUTracker() | ||
| private nonisolated let cmuxTopAbsoluteTimeNanosecondsRatio: Double? = { | ||
| var info = mach_timebase_info_data_t() | ||
| guard mach_timebase_info(&info) == KERN_SUCCESS, info.denom > 0 else { | ||
| return nil | ||
| } | ||
| return Double(info.numer) / Double(info.denom) | ||
| }() | ||
|
|
||
| nonisolated extension CmuxTopProcessSnapshot { | ||
| static func cpuSampleClockNanoseconds() -> UInt64 { | ||
| clock_gettime_nsec_np(CLOCK_UPTIME_RAW) | ||
| } | ||
|
|
||
| static func cpuPercentages( | ||
| for samples: [CmuxTopProcessScopeCacheKey: CmuxTopProcessCPUSample], | ||
| activeKeys: Set<CmuxTopProcessScopeCacheKey>, | ||
| sampledAtNanoseconds: UInt64 | ||
| ) -> [CmuxTopProcessScopeCacheKey: Double] { | ||
| cmuxTopProcessCPUTracker.cpuPercentages( | ||
| for: samples, | ||
| activeKeys: activeKeys, | ||
| sampledAtNanoseconds: sampledAtNanoseconds | ||
| ) | ||
| } | ||
|
|
||
| static func cpuSample( | ||
| from taskInfo: proc_taskinfo, | ||
| sampledAtNanoseconds: UInt64 | ||
| ) -> CmuxTopProcessCPUSample { | ||
| CmuxTopProcessCPUSample( | ||
| totalTimeTicks: clampedCPUTimeTicks(taskInfo.pti_total_user, taskInfo.pti_total_system), | ||
| sampledAtNanoseconds: sampledAtNanoseconds | ||
| ) | ||
| } | ||
|
|
||
| static func cpuPercent( | ||
| current: CmuxTopProcessCPUSample, | ||
| previous: CmuxTopProcessCPUSample? | ||
| ) -> Double { | ||
| guard let previous, | ||
| current.sampledAtNanoseconds > previous.sampledAtNanoseconds, | ||
| current.totalTimeTicks >= previous.totalTimeTicks, | ||
| current.totalTimeTicks != UInt64.max, | ||
| previous.totalTimeTicks != UInt64.max else { | ||
| return 0 | ||
| } | ||
|
|
||
| let cpuDelta = current.totalTimeTicks - previous.totalTimeTicks | ||
| let wallDeltaNanoseconds = current.sampledAtNanoseconds - previous.sampledAtNanoseconds | ||
| guard wallDeltaNanoseconds > 0 else { return 0 } | ||
|
|
||
| guard let cpuNanoseconds = absoluteTimeNanoseconds(cpuDelta) else { return 0 } | ||
| let wallNanoseconds = Double(wallDeltaNanoseconds) | ||
|
|
||
| return max(0, cpuNanoseconds / wallNanoseconds * 100.0) | ||
| } | ||
|
|
||
| private static func clampedCPUTimeTicks(_ user: UInt64, _ system: UInt64) -> UInt64 { | ||
| let (sum, overflow) = user.addingReportingOverflow(system) | ||
| return overflow ? UInt64.max : sum | ||
| } | ||
|
|
||
| private static func absoluteTimeNanoseconds(_ ticks: UInt64) -> Double? { | ||
| guard let ratio = cmuxTopAbsoluteTimeNanosecondsRatio else { return nil } | ||
| return Double(ticks) * ratio | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import ObjectiveC.runtime | ||
| import WebKit | ||
|
|
||
| enum CmuxWebContentProcessIdentifier { | ||
| @MainActor | ||
| static func pid(for webView: WKWebView) -> Int? { | ||
| let selector = NSSelectorFromString("_webProcessIdentifier") | ||
| guard let method = class_getInstanceMethod(WKWebView.self, selector) else { | ||
| return nil | ||
| } | ||
|
|
||
| typealias WebProcessIdentifierFn = @convention(c) (AnyObject, Selector) -> Int32 | ||
| let implementation = method_getImplementation(method) | ||
| let pid = unsafeBitCast(implementation, to: WebProcessIdentifierFn.self)(webView, selector) | ||
| return pid > 0 ? Int(pid) : nil | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.