Repository navigation
Expand file tree
/
Copy pathtest_monitor.swift
More file actions
59 lines (49 loc) · 1.92 KB
/
Copy pathtest_monitor.swift
File metadata and controls
59 lines (49 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import Foundation
// Simulate ConfigurationManager with fixed values
class TestConfig {
var cpuThreshold: Double = 50.0 // Lower threshold for testing
var memoryThreshold: Double = 85.0
var samplingInterval: TimeInterval = 2.0
var logRateLimitInterval: TimeInterval = 5.0
}
let config = TestConfig()
print("Test Configuration:")
print(" CPU Threshold: \(config.cpuThreshold)%")
print(" Memory Threshold: \(config.memoryThreshold)%")
print("")
// Get some process info
var pids = [pid_t](repeating: 0, count: 4096)
let size = proc_listallpids(&pids, Int32(pids.count * MemoryLayout<pid_t>.stride))
let count = Int(size) / MemoryLayout<pid_t>.stride
print("Found \(count) processes")
print("")
// Check top 5 processes
var cpuUsages: [(String, Double)] = []
for pid in pids.prefix(min(count, 100)) {
guard pid > 0 else { continue }
var taskInfo = proc_taskallinfo()
let infoSize = MemoryLayout<proc_taskallinfo>.stride
let result = proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &taskInfo, Int32(infoSize))
guard result == Int32(infoSize) else { continue }
// Get process name
var pathBuffer = [CChar](repeating: 0, count: 4096)
let pathResult = proc_pidpath(pid, &pathBuffer, UInt32(4096))
let name: String
if pathResult > 0 {
let path = String(cString: pathBuffer)
name = (path as NSString).lastPathComponent
} else {
name = "Unknown"
}
// Simple CPU estimate (not accurate for first sample)
let totalTime = taskInfo.ptinfo.pti_total_user + taskInfo.ptinfo.pti_total_system
let cpuPercent = Double(totalTime) / 1_000_000_000.0 // Convert nanoseconds
if cpuPercent > 0.1 {
cpuUsages.append((name, cpuPercent))
}
}
cpuUsages.sort { $0.1 > $1.1 }
print("Top processes by accumulated CPU time:")
for (i, item) in cpuUsages.prefix(10).enumerated() {
print(" \(i+1). \(item.0): \(String(format: "%.2f", item.1)) CPU units")
}