From 90ed18e6ac416c6a3848de636ec13c405a1116e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=81=E5=86=9C=E9=98=BF=E6=A5=A0?= Date: Tue, 3 Nov 2026 16:51:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(memory):=20=E9=87=8D=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E5=86=85=E5=AD=98=E5=8D=A1=E7=89=87=E5=B9=B6=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B6=E5=AE=B9=E9=87=8F=E5=8D=95=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/architecture.md | 3 + docs/unified-panel-validation.md | 7 ++ kymo/Core/Extensions/MetricFormat.swift | 6 ++ kymo/Core/Models/MetricsSnapshot.swift | 3 + kymo/Core/Monitors/MemoryMonitor.swift | 3 +- kymo/Features/MenuBar/MemoryDetailsView.swift | 69 +++++++++++++++++++ kymo/Features/MenuBar/MenuPanelView.swift | 22 ++---- kymo/Resources/Localizable.xcstrings | 69 +++++++++++++++++++ kymo/UI/Charts/PanelTrendChart.swift | 4 ++ kymoTests/MemoryChecks.swift | 11 +++ kymoTests/README.md | 6 ++ 11 files changed, 186 insertions(+), 17 deletions(-) create mode 100644 kymo/Features/MenuBar/MemoryDetailsView.swift create mode 100644 kymoTests/MemoryChecks.swift diff --git a/docs/architecture.md b/docs/architecture.md index b1cb4cb..a936865 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -205,3 +205,6 @@ ResourceCard 新增可选标题详情标签,GPU 核心数移到标题行,其 CPU、GPU、电池在三列及窄屏网格中共享显式行高:基础 260pt,CPU 需要第三行时增加 26pt,多风扇行按每行 19pt 扩展,取两者较大值。核心排布使用当前列数对应的实际卡片宽度计算。三张趋势图统一为 58pt 绘图区;标题、图例、图表下沿和首条分隔线保持一致。CPU 核心区与 GPU 统计区由上下弹性间距平衡,电池列表靠底;风扇区域不再提供悬浮帮助。采样与数据语义不变。 电池耗电进程区固定预留 72pt(标题及三行),等待采样、少量进程与完整 Top 3 使用相同尺寸;电池摘要固定 32pt,避免异步数据填入后其固有高度突破首排行高。 + +### 内存卡片单位与分布 +内存容量、曲线轴及悬浮值统一按二进制 GiB 展示,其他存储字节格式不变。MemoryMonitor 从 VM external_page_count 与 purgeable_count 提供可回收文件缓存估算;缓存不是额外物理容量,不叠加到分布条。MemoryDetailsView 展示应用、联动、压缩、可用分布,统一 Capsule 裁切避免小数值溢出;系统压力等级与用量百分比分开,不生成压力百分比或曲线。 diff --git a/docs/unified-panel-validation.md b/docs/unified-panel-validation.md index 77b918e..af441a2 100644 --- a/docs/unified-panel-validation.md +++ b/docs/unified-panel-validation.md @@ -212,3 +212,10 @@ GPU 专项第二轮:已完成 LLDB 失败分支定位、探针调用原样转 - 电池两行摘要固定 32pt,取消额外上下内边距,避免完整内容超过 260pt 卡片预算。 - 实际运行中加载前后的整卡对齐尚未验收;不重启用户现有实例。 - 签名 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 展示尚未实测。 diff --git a/kymo/Core/Extensions/MetricFormat.swift b/kymo/Core/Extensions/MetricFormat.swift index 42919b5..d6adbca 100644 --- a/kymo/Core/Extensions/MetricFormat.swift +++ b/kymo/Core/Extensions/MetricFormat.swift @@ -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"] diff --git a/kymo/Core/Models/MetricsSnapshot.swift b/kymo/Core/Models/MetricsSnapshot.swift index 6a550b5..f599b6e 100644 --- a/kymo/Core/Models/MetricsSnapshot.swift +++ b/kymo/Core/Models/MetricsSnapshot.swift @@ -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) } } diff --git a/kymo/Core/Monitors/MemoryMonitor.swift b/kymo/Core/Monitors/MemoryMonitor.swift index 80b391e..3eb886c 100644 --- a/kymo/Core/Monitors/MemoryMonitor.swift +++ b/kymo/Core/Monitors/MemoryMonitor.swift @@ -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) } } diff --git a/kymo/Features/MenuBar/MemoryDetailsView.swift b/kymo/Features/MenuBar/MemoryDetailsView.swift new file mode 100644 index 0000000..dbd72fb --- /dev/null +++ b/kymo/Features/MenuBar/MemoryDetailsView.swift @@ -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) + } + } +} diff --git a/kymo/Features/MenuBar/MenuPanelView.swift b/kymo/Features/MenuBar/MenuPanelView.swift index 6b6f2fe..6db455c 100644 --- a/kymo/Features/MenuBar/MenuPanelView.swift +++ b/kymo/Features/MenuBar/MenuPanelView.swift @@ -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") } } } diff --git a/kymo/Resources/Localizable.xcstrings b/kymo/Resources/Localizable.xcstrings index e7faf3c..9408e7c 100644 --- a/kymo/Resources/Localizable.xcstrings +++ b/kymo/Resources/Localizable.xcstrings @@ -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" : { diff --git a/kymo/UI/Charts/PanelTrendChart.swift b/kymo/UI/Charts/PanelTrendChart.swift index f06961f..6d634a1 100644 --- a/kymo/UI/Charts/PanelTrendChart.swift +++ b/kymo/UI/Charts/PanelTrendChart.swift @@ -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) } } diff --git a/kymoTests/MemoryChecks.swift b/kymoTests/MemoryChecks.swift new file mode 100644 index 0000000..700b95f --- /dev/null +++ b/kymoTests/MemoryChecks.swift @@ -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))") + } +} diff --git a/kymoTests/README.md b/kymoTests/README.md index e1c20a3..1357533 100644 --- a/kymoTests/README.md +++ b/kymoTests/README.md @@ -202,3 +202,9 @@ swiftc kymo/Core/Models/FanMetrics.swift kymo/Core/Monitors/CPUTemperatureMonito ``` 检查解码/非法/零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 +```