Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/ProcessBarMonitor/MonitorViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ final class MonitorViewModel: ObservableObject {

var menuBarTitle: String {
let cpu = String(format: "%.0f%%", summary.cpuPercent)
let memoryUsed = shortMemoryString(bytes: summary.memoryUsedBytes)
let memoryUsed = shortMemoryString(bytes: summary.systemMemoryUsedBytes)
let temp = summary.cpuTemperatureC.map { String(format: "%.0f°", $0) } ?? "--°"

switch menuBarDisplayMode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"summary.cpu" = "CPU";
"summary.ram" = "RAM";
"summary.ram" = "RAM (System-wide)";
"summary.temp" = "Temp";
"summary.thermal" = "Thermal";
"metric.used_percent" = "%.0f%% used";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"summary.cpu" = "CPU";
"summary.ram" = "内存";
"summary.ram" = "内存 (系统级)";
"summary.temp" = "温度";
"summary.thermal" = "热状态";
"metric.used_percent" = "已用 %.0f%%";
Expand Down
11 changes: 10 additions & 1 deletion Sources/ProcessBarMonitor/SystemMetricsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ actor SystemMetricsProvider {

return SystemSummary(
cpuPercent: cpuUsagePercent(),
memoryUsedBytes: currentUsedMemory(),
systemMemoryUsedBytes: currentUsedMemory(),
appMemoryUsedBytes: currentAppMemoryBytes(),
memoryTotalBytes: ProcessInfo.processInfo.physicalMemory,
thermalState: ProcessInfo.processInfo.thermalState,
cpuTemperatureC: temperature,
Expand All @@ -40,6 +41,14 @@ actor SystemMetricsProvider {
)
}

/// Computes the current app's total RSS across all threads via proc_pidinfo.
private func currentAppMemoryBytes() -> UInt64 {
var info = proc_taskinfo()
let size = proc_pidinfo(getpid(), PROC_PIDTASKINFO, 0, &info, Int32(MemoryLayout<proc_taskinfo>.size))
guard size == Int32(MemoryLayout<proc_taskinfo>.size) else { return 0 }
return UInt64(info.pti_resident_size)
}

private func currentUsedMemory() -> UInt64 {
var vmStats = vm_statistics64()
var count = mach_msg_type_number_t(MemoryLayout<vm_statistics64_data_t>.size / MemoryLayout<integer_t>.size)
Expand Down
11 changes: 8 additions & 3 deletions Sources/ProcessBarMonitor/SystemModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ struct MetricPoint: Identifiable, Hashable {

struct SystemSummary {
let cpuPercent: Double
let memoryUsedBytes: UInt64
/// System-wide memory pages: active + inactive + wired + compressor.
/// This is the macOS "memory pressure" figure — not per-process RSS.
let systemMemoryUsedBytes: UInt64
/// Actual RSS of this app's processes, summed across all children.
let appMemoryUsedBytes: UInt64
let memoryTotalBytes: UInt64
let thermalState: ProcessInfo.ThermalState
let cpuTemperatureC: Double?
Expand All @@ -134,12 +138,13 @@ struct SystemSummary {

var memoryPressurePercent: Double {
guard memoryTotalBytes > 0 else { return 0 }
return (Double(memoryUsedBytes) / Double(memoryTotalBytes)) * 100
return (Double(systemMemoryUsedBytes) / Double(memoryTotalBytes)) * 100
}

static let empty = SystemSummary(
cpuPercent: 0,
memoryUsedBytes: 0,
systemMemoryUsedBytes: 0,
appMemoryUsedBytes: 0,
memoryTotalBytes: 0,
thermalState: .nominal,
cpuTemperatureC: nil,
Expand Down
2 changes: 1 addition & 1 deletion Sources/ProcessBarMonitor/Views.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct MenuBarContentView: View {
}

private var memorySummary: String {
let used = ByteCountFormatter.string(fromByteCount: Int64(viewModel.summary.memoryUsedBytes), countStyle: .memory)
let used = ByteCountFormatter.string(fromByteCount: Int64(viewModel.summary.systemMemoryUsedBytes), countStyle: .memory)
let total = ByteCountFormatter.string(fromByteCount: Int64(viewModel.summary.memoryTotalBytes), countStyle: .memory)
return "\(used) / \(total)"
}
Expand Down