feat(memory): 增加内存分类历史趋势曲线

This commit is contained in:
2026-11-03 16:59:25 +08:00
parent b3f5b07e81
commit 74f6bdd7f7
6 changed files with 40 additions and 10 deletions
+2
View File
@@ -208,3 +208,5 @@ CPU、GPU、电池在三列及窄屏网格中共享显式行高:基础 260pt
### 内存卡片单位与分布
内存容量、曲线轴及悬浮值统一按二进制 GiB 展示,其他存储字节格式不变。MemoryMonitor 从 VM external_page_count 与 purgeable_count 提供可回收文件缓存估算;缓存不是额外物理容量,不叠加到分布条。MemoryDetailsView 展示应用、联动、压缩、可用分布,统一 Capsule 裁切避免小数值溢出;系统压力等级与用量百分比分开,不生成压力百分比或曲线。
内存分类历史由 MetricHistory 随每次采样保存应用、联动、压缩的字节值,缺失保持 nil,不使用当前分类回填过去。PanelTrendChart 支持可选四序列;内存采用总量与三类分量同一容量轴,配色与分类条一致,总量单独使用青色。
+4
View File
@@ -223,3 +223,7 @@ GPU 专项第二轮:已完成 LLDB 失败分支定位、探针调用原样转
### 内存卡片配色与底栏细化
- 使用独立内存色板:应用亮蓝、联动淡紫、压缩琥珀、可用灰,分布条和图例共用顺序,避免复用其他指标颜色导致漂移。
- 底栏三项均为左对齐双行,等宽列之间加入两条竖线;用量标题改为总利用率。
### 内存分类历史曲线
- 新增应用、联动、压缩历史曲线,保留总量,所有值采用 GiB 轴及悬浮格式;总量青色、应用蓝色、联动紫色、压缩琥珀色。
- MemoryChecks 覆盖分类历史存储与采样失败后保持缺失值;完整面板的四图例密度与交互效果尚待重新运行验收。
+7 -1
View File
@@ -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) }
+4 -1
View File
@@ -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)
+13 -8
View File
@@ -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<Date> = []
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))))
}
}
+10
View File
@@ -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))")
}
}