mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-28 15:44:32 +00:00
CPU Usage Percentages Fix
This fixes the CPU Usage Percentages. They are now Core-accurate, meaning they won't go over 100% (real utilization, normalized across all CPU Cores).
This commit is contained in:
parent
86f57c88b7
commit
caa7049f1e
1 changed files with 35 additions and 20 deletions
|
|
@ -54,6 +54,9 @@ class StatsManager: ObservableObject {
|
||||||
// Disk monitoring state
|
// Disk monitoring state
|
||||||
private var previousDiskStats: (bytesRead: UInt64, bytesWritten: UInt64) = (0, 0)
|
private var previousDiskStats: (bytesRead: UInt64, bytesWritten: UInt64) = (0, 0)
|
||||||
|
|
||||||
|
// CPU Usage History
|
||||||
|
private var previousProcessCPU: [pid_t: (userTime: UInt64, systemTime: UInt64, lastSample: Date)] = [:]
|
||||||
|
|
||||||
// MARK: - Initialization
|
// MARK: - Initialization
|
||||||
private init() {
|
private init() {
|
||||||
// Initialize with empty history
|
// Initialize with empty history
|
||||||
|
|
@ -474,44 +477,56 @@ class StatsManager: ObservableObject {
|
||||||
|
|
||||||
private func getRunningProcesses() -> [ProcessStats] {
|
private func getRunningProcesses() -> [ProcessStats] {
|
||||||
var processes: [ProcessStats] = []
|
var processes: [ProcessStats] = []
|
||||||
|
|
||||||
// Get all running applications from NSWorkspace
|
|
||||||
let runningApps = NSWorkspace.shared.runningApplications
|
let runningApps = NSWorkspace.shared.runningApplications
|
||||||
|
let cpuCount = ProcessInfo.processInfo.processorCount
|
||||||
|
let now = Date()
|
||||||
|
|
||||||
for app in runningApps {
|
for app in runningApps {
|
||||||
// Skip system processes and processes without bundle identifier
|
|
||||||
guard let bundleId = app.bundleIdentifier,
|
guard let bundleId = app.bundleIdentifier,
|
||||||
!bundleId.hasPrefix("com.apple.") || bundleId == "com.apple.finder" else {
|
!bundleId.hasPrefix("com.apple.") || bundleId == "com.apple.finder"
|
||||||
continue
|
else { continue }
|
||||||
}
|
|
||||||
|
|
||||||
let pid = app.processIdentifier
|
let pid = app.processIdentifier
|
||||||
var cpuUsage: Double = 0.0
|
var cpuUsage: Double = 0.0
|
||||||
var memoryUsage: UInt64 = 0
|
var memoryUsage: UInt64 = 0
|
||||||
|
|
||||||
// Get CPU and memory usage using proc_pidinfo
|
|
||||||
var taskInfo = proc_taskinfo()
|
var taskInfo = proc_taskinfo()
|
||||||
let result = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskInfo, Int32(MemoryLayout<proc_taskinfo>.size))
|
let result = proc_pidinfo(pid, PROC_PIDTASKINFO, 0,
|
||||||
|
&taskInfo, Int32(MemoryLayout<proc_taskinfo>.size))
|
||||||
|
|
||||||
if result == MemoryLayout<proc_taskinfo>.size {
|
if result == MemoryLayout<proc_taskinfo>.size {
|
||||||
// Calculate CPU usage (basic approximation)
|
let userTime = taskInfo.pti_total_user
|
||||||
cpuUsage = Double(taskInfo.pti_total_user + taskInfo.pti_total_system) / 1000000.0
|
let systemTime = taskInfo.pti_total_system
|
||||||
|
|
||||||
// Get memory usage
|
|
||||||
memoryUsage = taskInfo.pti_resident_size
|
memoryUsage = taskInfo.pti_resident_size
|
||||||
|
|
||||||
|
// Retrieve previous sample for this PID
|
||||||
|
if let prev = previousProcessCPU[pid] {
|
||||||
|
let deltaUser = Double(userTime - prev.userTime)
|
||||||
|
let deltaSystem = Double(systemTime - prev.systemTime)
|
||||||
|
let deltaTime = now.timeIntervalSince(prev.lastSample)
|
||||||
|
|
||||||
|
if deltaTime > 0 {
|
||||||
|
// Convert nanoseconds to seconds and normalize
|
||||||
|
let totalDelta = (deltaUser + deltaSystem) / 1e9
|
||||||
|
cpuUsage = (totalDelta / deltaTime) * 100.0 / Double(cpuCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update stored snapshot
|
||||||
|
previousProcessCPU[pid] = (userTime, systemTime, now)
|
||||||
}
|
}
|
||||||
|
|
||||||
let processStats = ProcessStats(
|
let processStats = ProcessStats(
|
||||||
pid: pid,
|
pid: pid,
|
||||||
name: app.localizedName ?? bundleId,
|
name: app.localizedName ?? bundleId,
|
||||||
cpuUsage: cpuUsage,
|
cpuUsage: max(0.0, min(cpuUsage, 100.0)), // Clamp for sanity
|
||||||
memoryUsage: memoryUsage,
|
memoryUsage: memoryUsage,
|
||||||
icon: app.icon
|
icon: app.icon
|
||||||
)
|
)
|
||||||
|
|
||||||
processes.append(processStats)
|
processes.append(processStats)
|
||||||
}
|
}
|
||||||
|
|
||||||
return processes
|
return processes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue