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
+3
View File
@@ -205,3 +205,6 @@ ResourceCard 新增可选标题详情标签,GPU 核心数移到标题行,其
CPU、GPU、电池在三列及窄屏网格中共享显式行高:基础 260pt,CPU 需要第三行时增加 26pt,多风扇行按每行 19pt 扩展,取两者较大值。核心排布使用当前列数对应的实际卡片宽度计算。三张趋势图统一为 58pt 绘图区;标题、图例、图表下沿和首条分隔线保持一致。CPU 核心区与 GPU 统计区由上下弹性间距平衡,电池列表靠底;风扇区域不再提供悬浮帮助。采样与数据语义不变。 CPU、GPU、电池在三列及窄屏网格中共享显式行高:基础 260pt,CPU 需要第三行时增加 26pt,多风扇行按每行 19pt 扩展,取两者较大值。核心排布使用当前列数对应的实际卡片宽度计算。三张趋势图统一为 58pt 绘图区;标题、图例、图表下沿和首条分隔线保持一致。CPU 核心区与 GPU 统计区由上下弹性间距平衡,电池列表靠底;风扇区域不再提供悬浮帮助。采样与数据语义不变。
电池耗电进程区固定预留 72pt(标题及三行),等待采样、少量进程与完整 Top 3 使用相同尺寸;电池摘要固定 32pt,避免异步数据填入后其固有高度突破首排行高。 电池耗电进程区固定预留 72pt(标题及三行),等待采样、少量进程与完整 Top 3 使用相同尺寸;电池摘要固定 32pt,避免异步数据填入后其固有高度突破首排行高。
### 内存卡片单位与分布
内存容量、曲线轴及悬浮值统一按二进制 GiB 展示,其他存储字节格式不变。MemoryMonitor 从 VM external_page_count 与 purgeable_count 提供可回收文件缓存估算;缓存不是额外物理容量,不叠加到分布条。MemoryDetailsView 展示应用、联动、压缩、可用分布,统一 Capsule 裁切避免小数值溢出;系统压力等级与用量百分比分开,不生成压力百分比或曲线。
+7
View File
@@ -212,3 +212,10 @@ GPU 专项第二轮:已完成 LLDB 失败分支定位、探针调用原样转
- 电池两行摘要固定 32pt,取消额外上下内边距,避免完整内容超过 260pt 卡片预算。 - 电池两行摘要固定 32pt,取消额外上下内边距,避免完整内容超过 260pt 卡片预算。
- 实际运行中加载前后的整卡对齐尚未验收;不重启用户现有实例。 - 实际运行中加载前后的整卡对齐尚未验收;不重启用户现有实例。
- 签名 Debug 构建及 `git diff --check` 通过。 - 签名 Debug 构建及 `git diff --check` 通过。
### 内存卡片重设计
- 48 GiB 对应 51,539,607,552 字节,内存卡片不再以十进制显示 51.5 GB。
- 用量趋势及悬浮值统一 GiB;分类条共同裁切,零值不设最小宽度。
- 压力只显示系统等级;缓存单独显示,不叠加到容量分类。
- 实际面板视觉验收待重新运行;构建与采样检查见本轮交付。
- 本轮签名 Debug 构建通过;MemoryChecks 通过 48 GiB 换算、零值/缺失值和分类总和验证。本机采样总量 48.0 GiB,已用约 38.1 GiB、缓存约 7.8 GiB、压力等级 1(正常)。保留用户原有进程,未重新启动面板,完整 UI 展示尚未实测。
+6
View File
@@ -11,6 +11,12 @@ nonisolated enum MetricFormat {
return String(format: "%.1f GB", Double(value) / 1_000_000_000) 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 { static func rate(_ value: Double?) -> String {
guard let value, value.isFinite, value >= 0 else { return "--" } guard let value, value.isFinite, value >= 0 else { return "--" }
let units = ["B/s", "KB/s", "MB/s", "GB/s", "TB/s"] 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 compressed: UInt64
let swapUsed: UInt64? let swapUsed: UInt64?
let pressure: Int? 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) } 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), return MemoryMetrics(total: total, used: min(total, app + wired + compressed),
wired: wired, compressed: compressed, wired: wired, compressed: compressed,
swapUsed: swapOK ? swap.xsu_used : nil, 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")) }.font(.system(size: 10)).help(Text("gpu.pipelineHelp"))
} }
private var memoryCard: some View { 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), PanelTrendChart(dates: samples.map(\.date),
first: samples.map { $0.memory.map { $0 * Double(snapshot.memory?.total ?? 1) } }, first: samples.map { $0.memory.map { $0 * Double(snapshot.memory?.total ?? 1) } },
color: Color("MetricMemory"), fixedScale: 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) scaleLabel: MetricFormat.memory(snapshot.memory?.total), firstValueFormat: .memory,
if let m = snapshot.memory { expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals)
if let memory = snapshot.memory {
GeometryReader { geometry in MemoryDetailsView(memory: memory)
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")) }
} else { Text("metric.waiting") } } 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" : { "memory.app" : {
"extractionState" : "manual", "extractionState" : "manual",
"localizations" : { "localizations" : {
+4
View File
@@ -4,6 +4,7 @@ enum TrendValueFormat {
case percent case percent
case rate case rate
case bytes case bytes
case memory
case watts case watts
} }
@@ -19,6 +20,7 @@ struct PanelTrendChart: View {
var fixedScale: Double? = 1 var fixedScale: Double? = 1
var scaleLabel: String? var scaleLabel: String?
var secondSeriesUsesAutonomousScale = false var secondSeriesUsesAutonomousScale = false
var firstValueFormat: TrendValueFormat?
var secondValueFormat: TrendValueFormat? var secondValueFormat: TrendValueFormat?
var firstLabel: LocalizedStringKey = "panel.total" var firstLabel: LocalizedStringKey = "panel.total"
var secondLabel: LocalizedStringKey = "network.upload" var secondLabel: LocalizedStringKey = "network.upload"
@@ -51,6 +53,7 @@ struct PanelTrendChart: View {
private var secondaryScale: Double { max(0.0001, retainedSecondScale) } private var secondaryScale: Double { max(0.0001, retainedSecondScale) }
private var hasSecondaryScale: Bool { secondSeriesUsesAutonomousScale && !second.isEmpty } private var hasSecondaryScale: Bool { secondSeriesUsesAutonomousScale && !second.isEmpty }
private var automaticFormat: TrendValueFormat { private var automaticFormat: TrendValueFormat {
if let firstValueFormat { return firstValueFormat }
if fixedScale == 1 { return .percent } if fixedScale == 1 { return .percent }
if fixedScale == nil { return .rate } if fixedScale == nil { return .rate }
return .bytes return .bytes
@@ -66,6 +69,7 @@ struct PanelTrendChart: View {
case .percent: return MetricFormat.percent(value) case .percent: return MetricFormat.percent(value)
case .rate: return MetricFormat.rate(value) case .rate: return MetricFormat.rate(value)
case .bytes: return MetricFormat.bytes(UInt64(max(0, 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) case .watts: return MetricFormat.watts(value)
} }
} }
+11
View File
@@ -0,0 +1,11 @@
import Foundation
@main struct Check {
static func main() {
precondition(MetricFormat.memory(51_539_607_552) == "48.0 GiB")
precondition(MetricFormat.memory(0) == "0 B")
precondition(MetricFormat.memory(nil) == "--")
guard let m = MemoryMonitor.sample() else { fatalError("No sample") }
precondition(m.app + m.wired + m.compressed + m.available == m.total)
print("Memory checks passed: total=\(MetricFormat.memory(m.total)), used=\(MetricFormat.memory(m.used)), cached=\(MetricFormat.memory(m.cached)), pressure=\(String(describing: m.pressure))")
}
}
+6
View File
@@ -202,3 +202,9 @@ swiftc kymo/Core/Models/FanMetrics.swift kymo/Core/Monitors/CPUTemperatureMonito
``` ```
检查解码/非法/零RPM、速度档位、连续相位及停转。动态手感仍需实际面板验收,不将状态检查等同于帧呈现验证。 检查解码/非法/零RPM、速度档位、连续相位及停转。动态手感仍需实际面板验收,不将状态检查等同于帧呈现验证。
内存容量单位、分类守恒与本机采样:
```bash
swiftc -parse-as-library kymo/Core/Models/*.swift kymo/Core/Monitors/*.swift kymo/Core/Services/*.swift kymo/Core/Extensions/*.swift kymoTests/MemoryChecks.swift -o /private/tmp/kymo-memory-check
/private/tmp/kymo-memory-check
```