From 74f6bdd7f7f7c432dbffbb8de28a7d4fc5883bf5 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:59:25 +0800 Subject: [PATCH] =?UTF-8?q?feat(memory):=20=E5=A2=9E=E5=8A=A0=E5=86=85?= =?UTF-8?q?=E5=AD=98=E5=88=86=E7=B1=BB=E5=8E=86=E5=8F=B2=E8=B6=8B=E5=8A=BF?= =?UTF-8?q?=E6=9B=B2=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/architecture.md | 2 ++ docs/unified-panel-validation.md | 4 ++++ kymo/Core/Models/MetricHistory.swift | 8 +++++++- kymo/Features/MenuBar/MenuPanelView.swift | 5 ++++- kymo/UI/Charts/PanelTrendChart.swift | 21 +++++++++++++-------- kymoTests/MemoryChecks.swift | 10 ++++++++++ 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index a936865..c1a5a10 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -208,3 +208,5 @@ CPU、GPU、电池在三列及窄屏网格中共享显式行高:基础 260pt ### 内存卡片单位与分布 内存容量、曲线轴及悬浮值统一按二进制 GiB 展示,其他存储字节格式不变。MemoryMonitor 从 VM external_page_count 与 purgeable_count 提供可回收文件缓存估算;缓存不是额外物理容量,不叠加到分布条。MemoryDetailsView 展示应用、联动、压缩、可用分布,统一 Capsule 裁切避免小数值溢出;系统压力等级与用量百分比分开,不生成压力百分比或曲线。 + +内存分类历史由 MetricHistory 随每次采样保存应用、联动、压缩的字节值,缺失保持 nil,不使用当前分类回填过去。PanelTrendChart 支持可选四序列;内存采用总量与三类分量同一容量轴,配色与分类条一致,总量单独使用青色。 diff --git a/docs/unified-panel-validation.md b/docs/unified-panel-validation.md index 3d62a05..f5f8e03 100644 --- a/docs/unified-panel-validation.md +++ b/docs/unified-panel-validation.md @@ -223,3 +223,7 @@ GPU 专项第二轮:已完成 LLDB 失败分支定位、探针调用原样转 ### 内存卡片配色与底栏细化 - 使用独立内存色板:应用亮蓝、联动淡紫、压缩琥珀、可用灰,分布条和图例共用顺序,避免复用其他指标颜色导致漂移。 - 底栏三项均为左对齐双行,等宽列之间加入两条竖线;用量标题改为总利用率。 + +### 内存分类历史曲线 +- 新增应用、联动、压缩历史曲线,保留总量,所有值采用 GiB 轴及悬浮格式;总量青色、应用蓝色、联动紫色、压缩琥珀色。 +- MemoryChecks 覆盖分类历史存储与采样失败后保持缺失值;完整面板的四图例密度与交互效果尚待重新运行验收。 diff --git a/kymo/Core/Models/MetricHistory.swift b/kymo/Core/Models/MetricHistory.swift index fa560c8..68d0a43 100644 --- a/kymo/Core/Models/MetricHistory.swift +++ b/kymo/Core/Models/MetricHistory.swift @@ -16,6 +16,9 @@ nonisolated struct TrendSample: Sendable { var cpuSystem: Double? var gpuRenderer: Double? var gpuTiler: Double? + var memoryApp: Double? + var memoryWired: Double? + var memoryCompressed: Double? var breakBefore = false var expectedInterval: TimeInterval = 2 } @@ -33,7 +36,10 @@ nonisolated struct MetricHistory: Sendable { battery: snapshot.battery?.charge, batteryPower: snapshot.battery?.power, upload: snapshot.uploadRate, read: snapshot.diskIO?.read, write: snapshot.diskIO?.written, - cpuUser: snapshot.cpu?.user, cpuSystem: snapshot.cpu?.system, gpuRenderer: snapshot.gpu?.renderer, gpuTiler: snapshot.gpu?.tiler, breakBefore: breakBefore, expectedInterval: expectedInterval)) + cpuUser: snapshot.cpu?.user, cpuSystem: snapshot.cpu?.system, gpuRenderer: snapshot.gpu?.renderer, gpuTiler: snapshot.gpu?.tiler, + memoryApp: snapshot.memory.map { Double($0.app) }, + memoryWired: snapshot.memory.map { Double($0.wired) }, + memoryCompressed: snapshot.memory.map { Double($0.compressed) }, breakBefore: breakBefore, expectedInterval: expectedInterval)) let current = snapshot.network ?? [] let keys = Set(current.map(\.identity)) interfaces = interfaces.filter { keys.contains($0.key) } diff --git a/kymo/Features/MenuBar/MenuPanelView.swift b/kymo/Features/MenuBar/MenuPanelView.swift index 219147d..787dc66 100644 --- a/kymo/Features/MenuBar/MenuPanelView.swift +++ b/kymo/Features/MenuBar/MenuPanelView.swift @@ -235,8 +235,11 @@ struct MenuPanelView: View { 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("MemoryApplication"), fixedScale: Double(snapshot.memory?.total ?? 1), + second: samples.map(\.memoryApp), third: samples.map(\.memoryWired), fourth: samples.map(\.memoryCompressed), + color: Color("MetricCPU"), secondColor: Color("MemoryApplication"), + thirdColor: Color("MemoryWired"), fourthColor: Color("MemoryCompressed"), fixedScale: Double(snapshot.memory?.total ?? 1), scaleLabel: MetricFormat.memory(snapshot.memory?.total), firstValueFormat: .memory, firstLabel: "gpu.totalUtilization", + secondLabel: "memory.app", thirdLabel: "memory.wired", fourthLabel: "memory.compressed", expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals) if let memory = snapshot.memory { MemoryDetailsView(memory: memory) diff --git a/kymo/UI/Charts/PanelTrendChart.swift b/kymo/UI/Charts/PanelTrendChart.swift index 6d634a1..43d9dd6 100644 --- a/kymo/UI/Charts/PanelTrendChart.swift +++ b/kymo/UI/Charts/PanelTrendChart.swift @@ -14,9 +14,11 @@ struct PanelTrendChart: View { let first: [Double?] var second: [Double?] = [] var third: [Double?] = [] + var fourth: [Double?] = [] let color: Color var secondColor: Color = .orange var thirdColor: Color = .purple + var fourthColor: Color = .yellow var fixedScale: Double? = 1 var scaleLabel: String? var secondSeriesUsesAutonomousScale = false @@ -25,6 +27,7 @@ struct PanelTrendChart: View { var firstLabel: LocalizedStringKey = "panel.total" var secondLabel: LocalizedStringKey = "network.upload" var thirdLabel: LocalizedStringKey = "cpu.system" + var fourthLabel: LocalizedStringKey = "memory.compressed" var expectedInterval: TimeInterval = 2 var boundaryDates: Set = [] var intervals: [TimeInterval] = [] @@ -43,11 +46,11 @@ struct PanelTrendChart: View { @State private var displayedFrame: PanelCurveFrame? private var inputFrame: PanelCurveFrame { - PanelCurveFrame(dates: dates, series: [first, second, third], + PanelCurveFrame(dates: dates, series: [first, second, third, fourth], intervals: intervals.count == dates.count ? intervals : Array(repeating: expectedInterval, count: dates.count), boundaryDates: boundaryDates) } - private var maximum: Double { max(1, (first + second + third).compactMap { $0 }.filter(\.isFinite).max() ?? 1) } + private var maximum: Double { max(1, (first + second + third + fourth).compactMap { $0 }.filter(\.isFinite).max() ?? 1) } private var secondMaximum: Double { max(1, second.compactMap { $0 }.filter(\.isFinite).max() ?? 1) } private var scale: Double { max(0.0001, fixedScale ?? retainedScale) } private var secondaryScale: Double { max(0.0001, retainedSecondScale) } @@ -95,6 +98,7 @@ struct PanelTrendChart: View { tooltipRow(firstLabel, values: first, tint: color, index: index) if !second.isEmpty { tooltipRow(secondLabel, values: second, tint: secondColor, format: secondValueFormat, index: index) } if !third.isEmpty { tooltipRow(thirdLabel, values: third, tint: thirdColor, index: index) } + if !fourth.isEmpty { tooltipRow(fourthLabel, values: fourth, tint: fourthColor, index: index) } } .font(.system(size: 10)).padding(8) .background { TooltipMaterialBackground() } @@ -108,6 +112,7 @@ struct PanelTrendChart: View { legend(firstLabel, values: first, tint: color) if !second.isEmpty { legend(secondLabel, values: second, tint: secondColor, format: secondValueFormat) } if !third.isEmpty { legend(thirdLabel, values: third, tint: thirdColor) } + if !fourth.isEmpty { legend(fourthLabel, values: fourth, tint: fourthColor) } Spacer(minLength: 0) }.frame(height: 14) HStack(spacing: 5) { @@ -121,8 +126,8 @@ struct PanelTrendChart: View { .lineLimit(1).minimumScaleFactor(0.6).frame(width: fixedScale == 1 ? 28 : 47, height: plotHeight) GeometryReader { proxy in ZStack(alignment: .topLeading) { - PanelCurveCanvas(frame: displayedFrame ?? inputFrame, colors: [color, secondColor, thirdColor], - scales: [scale, hasSecondaryScale ? secondaryScale : scale, hasSecondaryScale ? secondaryScale : scale], + PanelCurveCanvas(frame: displayedFrame ?? inputFrame, colors: [color, secondColor, thirdColor, fourthColor], + scales: [scale, hasSecondaryScale ? secondaryScale : scale, hasSecondaryScale ? secondaryScale : scale, scale], maximumGap: max(5, expectedInterval * 2), targetBegin: (lastFrame ?? inputFrame).dates.first, targetEnd: (lastFrame ?? inputFrame).dates.last, previousBegin: previousBegin, previousEnd: previousEnd, progress: progress) @@ -131,11 +136,11 @@ struct PanelTrendChart: View { Path { path in path.move(to: CGPoint(x: x, y: 0)); path.addLine(to: CGPoint(x: x, y: proxy.size.height)) }.stroke(.primary.opacity(0.45), style: StrokeStyle(lineWidth: 1, dash: [3, 2])) - ForEach(0..<3, id: \.self) { series in - let values = [first, second, third][series] + ForEach(0..<4, id: \.self) { series in + let values = [first, second, third, fourth][series] if index < values.count, let value = values[index], value.isFinite { - let seriesScale = [scale, hasSecondaryScale ? secondaryScale : scale, hasSecondaryScale ? secondaryScale : scale][series] - Circle().fill([color, secondColor, thirdColor][series]).frame(width: 5, height: 5) + let seriesScale = [scale, hasSecondaryScale ? secondaryScale : scale, hasSecondaryScale ? secondaryScale : scale, scale][series] + Circle().fill([color, secondColor, thirdColor, fourthColor][series]).frame(width: 5, height: 5) .position(x: x, y: proxy.size.height * (1 - min(1, max(0, value / seriesScale)))) } } diff --git a/kymoTests/MemoryChecks.swift b/kymoTests/MemoryChecks.swift index 700b95f..00de641 100644 --- a/kymoTests/MemoryChecks.swift +++ b/kymoTests/MemoryChecks.swift @@ -6,6 +6,16 @@ import Foundation precondition(MetricFormat.memory(nil) == "--") guard let m = MemoryMonitor.sample() else { fatalError("No sample") } precondition(m.app + m.wired + m.compressed + m.available == m.total) + var snapshot = MetricsSnapshot() + snapshot.memory = m + var history = MetricHistory() + history.append(snapshot) + precondition(history.samples.last?.memoryApp == Double(m.app)) + precondition(history.samples.last?.memoryWired == Double(m.wired)) + precondition(history.samples.last?.memoryCompressed == Double(m.compressed)) + snapshot.memory = nil + history.append(snapshot) + precondition(history.samples.last?.memoryApp == nil) print("Memory checks passed: total=\(MetricFormat.memory(m.total)), used=\(MetricFormat.memory(m.used)), cached=\(MetricFormat.memory(m.cached)), pressure=\(String(describing: m.pressure))") } }