feat(cpu): 区分核心类型并标注运行时间

This commit is contained in:
2026-11-03 13:25:52 +08:00
parent d81825ba8c
commit 96490c4983
4 changed files with 159 additions and 4 deletions
+26
View File
@@ -0,0 +1,26 @@
import Foundation
import IOKit
/// The registry identifies logical CPUs individually; absence or unknown types stay unclassified.
nonisolated enum CPUCoreTopology {
enum Kind: String, Sendable { case efficiency = "E", middle = "M", performance = "P" }
static let cores: [Int: Kind] = {
let root = IORegistryEntryFromPath(kIOMainPortDefault, "IODeviceTree:/cpus")
guard root != 0 else { return [:] }
defer { IOObjectRelease(root) }
var iterator: io_iterator_t = 0
guard IORegistryEntryGetChildIterator(root, kIODeviceTreePlane, &iterator) == KERN_SUCCESS else { return [:] }
defer { IOObjectRelease(iterator) }
var result: [Int: Kind] = [:]
while case let entry = IOIteratorNext(iterator), entry != 0 {
defer { IOObjectRelease(entry) }
guard let id = IORegistryEntryCreateCFProperty(entry, "logical-cpu-id" as CFString, kCFAllocatorDefault, 0)?.takeRetainedValue() as? NSNumber,
let data = IORegistryEntryCreateCFProperty(entry, "cluster-type" as CFString, kCFAllocatorDefault, 0)?.takeRetainedValue() as? Data,
let raw = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .controlCharacters),
let kind = Kind(rawValue: raw), id.intValue >= 0 else { continue }
result[id.intValue] = kind
}
return result
}()
}
+34 -4
View File
@@ -102,6 +102,26 @@ struct MenuPanelView: View {
HStack(alignment: .firstTextBaseline) { Text(key).foregroundStyle(.secondary); Spacer(minLength: 6); AnimatedMetricText(value: value) }
}
private func coreColor(_ kind: CPUCoreTopology.Kind?) -> Color {
switch kind {
case .efficiency: Color("MetricCPU")
case .middle: Color("MetricMemory")
case .performance: Color("MetricGPU")
case nil: .secondary
}
}
private func coreLabel(_ kind: CPUCoreTopology.Kind) -> Text {
switch kind {
case .efficiency: Text("cpu.kind.efficiency", tableName: "CPUDetails")
case .middle: Text("cpu.kind.performance", tableName: "CPUDetails")
case .performance:
CPUCoreTopology.cores.values.contains(.middle)
? Text("cpu.kind.super", tableName: "CPUDetails")
: Text("cpu.kind.performance", tableName: "CPUDetails")
}
}
private var cpuCard: some View {
ResourceCard(title: "metric.cpu", symbol: "cpu", color: Color("MetricCPU"), value: MetricFormat.percent(snapshot.cpu?.usage)) {
PanelTrendChart(dates: samples.map(\.date), first: samples.map(\.cpu),
@@ -110,19 +130,29 @@ struct MenuPanelView: View {
firstLabel: "panel.total", secondLabel: "cpu.user", thirdLabel: "cpu.system", expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals)
if let cpu = snapshot.cpu {
HStack { line("cpu.user", MetricFormat.percent(cpu.user)); line("cpu.system", MetricFormat.percent(cpu.system)) }
let kinds = [CPUCoreTopology.Kind.efficiency, .middle, .performance].filter { CPUCoreTopology.cores.values.contains($0) }
if !kinds.isEmpty {
HStack(spacing: 10) {
ForEach(kinds, id: \.rawValue) { kind in
HStack(spacing: 3) { Circle().fill(coreColor(kind)).frame(width: 6, height: 6); coreLabel(kind) }
}
}.font(.system(size: 9)).foregroundStyle(.secondary).frame(maxWidth: .infinity, alignment: .leading)
}
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 6), count: 7), spacing: 5) {
ForEach(Array(cpu.cores.enumerated()), id: \.offset) { index, value in
let kind = CPUCoreTopology.cores[index]
HStack(spacing: 3) {
Text(verbatim: "\(index + 1)").font(.system(size: 9)).frame(width: 12)
MetricProgressBar(value: value, color: Color("MetricCPU")).frame(height: 7)
}.help("\(index + 1): \(MetricFormat.percent(value))")
.accessibilityLabel(Text("cpu.core")).accessibilityValue("\(index + 1): \(MetricFormat.percent(value))")
MetricProgressBar(value: value, color: coreColor(kind)).frame(height: 7)
}.help((kind.map { coreLabel($0) + Text(verbatim: " · ") } ?? Text("cpu.core")) + Text(verbatim: "\(index + 1): \(MetricFormat.percent(value))"))
.accessibilityLabel(kind.map { coreLabel($0) } ?? Text("cpu.core")).accessibilityValue("\(index + 1): \(MetricFormat.percent(value))")
}
}.padding(.vertical, 3)
} else { Text("metric.waiting").foregroundStyle(.secondary) }
Spacer(minLength: 0)
HStack { line("system.load", snapshot.load.map(MetricFormat.decimal).joined(separator: " / ")); Spacer(minLength: 8); AnimatedMetricText(value: String(format: "%.1f h", snapshot.uptime / 3600)) }
HStack { line("system.load", snapshot.load.map(MetricFormat.decimal).joined(separator: " / ")).help(Text("cpu.load.help", tableName: "CPUDetails")); Spacer(minLength: 8); Text("panel.uptime").foregroundStyle(.secondary); AnimatedMetricText(value: String(format: "%.1f h", snapshot.uptime / 3600)) }
.lineLimit(1).minimumScaleFactor(0.7)
}
}
+98
View File
@@ -0,0 +1,98 @@
{
"sourceLanguage": "en",
"strings": {
"cpu.kind.efficiency": {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Efficiency"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "能效核"
}
},
"zh-Hant": {
"stringUnit": {
"state": "translated",
"value": "能效核心"
}
}
}
},
"cpu.kind.performance": {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Performance"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "性能核"
}
},
"zh-Hant": {
"stringUnit": {
"state": "translated",
"value": "效能核心"
}
}
}
},
"cpu.kind.super": {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Super"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "超性能核"
}
},
"zh-Hant": {
"stringUnit": {
"state": "translated",
"value": "超效能核心"
}
}
}
},
"cpu.load.help": {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Average system load over the last 1, 5 and 15 minutes."
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "最近 1、5、15 分钟的系统平均负载。"
}
},
"zh-Hant": {
"stringUnit": {
"state": "translated",
"value": "最近 1、5、15 分鐘的系統平均負載。"
}
}
}
}
},
"version": "1.0"
}