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
+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))))
}
}