feat(memory): 重设计内存卡片并统一二进制容量单位

This commit is contained in:
2026-11-03 16:51:14 +08:00
parent cc38066de2
commit 90ed18e6ac
11 changed files with 186 additions and 17 deletions
+6
View File
@@ -11,6 +11,12 @@ nonisolated enum MetricFormat {
return String(format: "%.1f GB", Double(value) / 1_000_000_000)
}
static func memory(_ value: UInt64?) -> String {
guard let value else { return "--" }
if value == 0 { return "0 B" }
return String(format: "%.1f GiB", Double(value) / 1_073_741_824)
}
static func rate(_ value: Double?) -> String {
guard let value, value.isFinite, value >= 0 else { return "--" }
let units = ["B/s", "KB/s", "MB/s", "GB/s", "TB/s"]
+3
View File
@@ -14,6 +14,9 @@ nonisolated struct MemoryMetrics: Sendable {
let compressed: UInt64
let swapUsed: UInt64?
let pressure: Int?
var cached: UInt64? = nil
var available: UInt64 { total - min(total, used) }
var app: UInt64 { used - min(used, wired + compressed) }
var usage: Double { total == 0 ? 0 : Double(used) / Double(total) }
}
+2 -1
View File
@@ -27,6 +27,7 @@ nonisolated enum MemoryMonitor {
return MemoryMetrics(total: total, used: min(total, app + wired + compressed),
wired: wired, compressed: compressed,
swapUsed: swapOK ? swap.xsu_used : nil,
pressure: pressureOK ? Int(pressure) : nil)
pressure: pressureOK ? Int(pressure) : nil,
cached: UInt64(info.external_page_count) * page + purgeable)
}
}
@@ -0,0 +1,69 @@
import SwiftUI
struct MemoryDetailsView: View {
let memory: MemoryMetrics
@Environment(\.accessibilityReduceMotion) private var reduceMotion
private var colors: [Color] { [Color("MetricMemory"), Color("SeriesUpload"), Color("MetricDisk"), .secondary.opacity(0.25)] }
private var values: [UInt64] { [memory.app, memory.wired, memory.compressed, memory.available] }
private let labels: [LocalizedStringKey] = ["memory.app", "memory.wired", "memory.compressed", "memory.available"]
var body: some View {
VStack(alignment: .leading, spacing: 9) {
Divider()
HStack {
Text("memory.distribution").fontWeight(.semibold)
Spacer()
Text("panel.total").foregroundStyle(.secondary)
AnimatedMetricText(value: MetricFormat.percent(memory.usage)).foregroundStyle(Color("MetricMemory"))
}
GeometryReader { proxy in
HStack(spacing: 0) {
ForEach(0..<4) { index in
Rectangle().fill(colors[index])
.frame(width: CGFloat(values[index]) / CGFloat(max(1, memory.total)) * proxy.size.width)
}
}
// Round the combined shape; tiny segments never receive a minimum width.
.clipShape(Capsule())
.animation(reduceMotion ? nil : .easeInOut(duration: 0.35), value: values)
}.frame(height: 8).accessibilityHidden(true)
HStack(spacing: 4) {
ForEach(0..<4) { index in
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 3) {
Circle().fill(colors[index]).frame(width: 4, height: 4)
Text(labels[index]).foregroundStyle(.secondary)
}
AnimatedMetricText(value: MetricFormat.memory(values[index]))
.fontWeight(.medium).monospacedDigit()
}.frame(maxWidth: .infinity, alignment: .leading)
}
}.font(.system(size: 9)).lineLimit(1).minimumScaleFactor(0.7)
Divider()
HStack(alignment: .top, spacing: 8) {
VStack(alignment: .leading, spacing: 4) {
Text("memory.pressure").foregroundStyle(.secondary)
pressure
}.frame(maxWidth: .infinity, alignment: .leading)
VStack(alignment: .leading, spacing: 4) {
Text("memory.cached").foregroundStyle(.secondary)
AnimatedMetricText(value: MetricFormat.memory(memory.cached))
}.frame(maxWidth: .infinity, alignment: .leading)
.help(Text("memory.cachedHelp"))
VStack(alignment: .leading, spacing: 4) {
Text("memory.swap").foregroundStyle(.secondary)
AnimatedMetricText(value: MetricFormat.memory(memory.swapUsed))
}.frame(maxWidth: .infinity, alignment: .leading)
}.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.7)
}.padding(.top, 4)
}
@ViewBuilder private var pressure: some View {
switch memory.pressure {
case 1: Label("pressure.normal", systemImage: "circle.fill").foregroundStyle(Color("MetricCPU"))
case 2: Label("pressure.warning", systemImage: "circle.fill").foregroundStyle(.orange)
case 4: Label("pressure.critical", systemImage: "circle.fill").foregroundStyle(.red)
default: Text(verbatim: "--").foregroundStyle(.secondary)
}
}
}
+6 -16
View File
@@ -231,25 +231,15 @@ struct MenuPanelView: View {
}.font(.system(size: 10)).help(Text("gpu.pipelineHelp"))
}
private var memoryCard: some View {
ResourceCard(title: "metric.memory", symbol: "memorychip", color: Color("MetricMemory"), value: snapshot.memory.map { String(format: "%.1f / %.1f GB", Double($0.used) / 1_000_000_000, Double($0.total) / 1_000_000_000) } ?? "--") {
ResourceCard(title: "metric.memory", symbol: "memorychip", color: Color("MetricMemory"),
value: snapshot.memory.map { String(format: "%.1f / %.0f GiB", Double($0.used) / 1_073_741_824, Double($0.total) / 1_073_741_824) } ?? "--") {
PanelTrendChart(dates: samples.map(\.date),
first: samples.map { $0.memory.map { $0 * Double(snapshot.memory?.total ?? 1) } },
color: Color("MetricMemory"), fixedScale: Double(snapshot.memory?.total ?? 1),
scaleLabel: MetricFormat.bytes(snapshot.memory?.total), expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals)
if let m = snapshot.memory {
GeometryReader { geometry in
HStack(spacing: 0) {
ForEach(Array([m.used - min(m.used, m.wired + m.compressed), m.wired, m.compressed, m.total - min(m.total, m.used)].enumerated()), id: \.offset) { i, bytes in
Rectangle().fill(Color("MetricMemory").opacity([1, 0.75, 0.5, 0.15][i]))
.frame(width: Double(bytes) / Double(max(1, m.total)) * geometry.size.width)
}
}
}.frame(height: 6).clipped().accessibilityHidden(true)
line("memory.app", MetricFormat.bytes(m.used - min(m.used, m.wired + m.compressed)))
HStack { line("memory.wired", MetricFormat.bytes(m.wired)); line("memory.compressed", MetricFormat.bytes(m.compressed)) }
HStack { line("memory.available", MetricFormat.bytes(m.total - min(m.total, m.used))); line("memory.swap", MetricFormat.bytes(m.swapUsed)) }
HStack { Text("memory.pressure").foregroundStyle(.secondary); Spacer(); pressure(m.pressure); AnimatedMetricText(value: MetricFormat.percent(m.usage)).foregroundStyle(Color("MetricMemory")) }
scaleLabel: MetricFormat.memory(snapshot.memory?.total), firstValueFormat: .memory,
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals)
if let memory = snapshot.memory {
MemoryDetailsView(memory: memory)
} else { Text("metric.waiting") }
}
}
+69
View File
@@ -2077,6 +2077,75 @@
}
}
},
"memory.distribution" : {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Memory distribution"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "内存分布"
}
},
"zh-Hant": {
"stringUnit": {
"state": "translated",
"value": "記憶體分布"
}
}
}
},
"memory.cached" : {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Cached files"
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "文件缓存"
}
},
"zh-Hant": {
"stringUnit": {
"state": "translated",
"value": "檔案快取"
}
}
}
},
"memory.cachedHelp" : {
"extractionState": "manual",
"localizations": {
"en": {
"stringUnit": {
"state": "translated",
"value": "Reclaimable file-backed and purgeable memory; not an additional capacity segment."
}
},
"zh-Hans": {
"stringUnit": {
"state": "translated",
"value": "文件支持页与可清除内存,可回收,不作为额外容量叠加。"
}
},
"zh-Hant": {
"stringUnit": {
"state": "translated",
"value": "檔案支持頁與可清除記憶體,可回收,不作為額外容量疊加。"
}
}
}
},
"memory.app" : {
"extractionState" : "manual",
"localizations" : {
+4
View File
@@ -4,6 +4,7 @@ enum TrendValueFormat {
case percent
case rate
case bytes
case memory
case watts
}
@@ -19,6 +20,7 @@ struct PanelTrendChart: View {
var fixedScale: Double? = 1
var scaleLabel: String?
var secondSeriesUsesAutonomousScale = false
var firstValueFormat: TrendValueFormat?
var secondValueFormat: TrendValueFormat?
var firstLabel: LocalizedStringKey = "panel.total"
var secondLabel: LocalizedStringKey = "network.upload"
@@ -51,6 +53,7 @@ struct PanelTrendChart: View {
private var secondaryScale: Double { max(0.0001, retainedSecondScale) }
private var hasSecondaryScale: Bool { secondSeriesUsesAutonomousScale && !second.isEmpty }
private var automaticFormat: TrendValueFormat {
if let firstValueFormat { return firstValueFormat }
if fixedScale == 1 { return .percent }
if fixedScale == nil { return .rate }
return .bytes
@@ -66,6 +69,7 @@ struct PanelTrendChart: View {
case .percent: return MetricFormat.percent(value)
case .rate: return MetricFormat.rate(value)
case .bytes: return MetricFormat.bytes(UInt64(max(0, value)))
case .memory: return MetricFormat.memory(UInt64(max(0, value)))
case .watts: return MetricFormat.watts(value)
}
}