From 142540bc3ce8fdadcbf1d443e0731ea78ced5f57 Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:23:50 +0100 Subject: [PATCH] Add a Copy Report button to the popover --- Sources/BoostKit/MemoryReport.swift | 85 +++++++++++++++++++++ Sources/BoostKit/MenuBarPopover.swift | 19 +++++ Tests/BoostKitTests/MemoryReportTests.swift | 79 +++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 Sources/BoostKit/MemoryReport.swift create mode 100644 Tests/BoostKitTests/MemoryReportTests.swift diff --git a/Sources/BoostKit/MemoryReport.swift b/Sources/BoostKit/MemoryReport.swift new file mode 100644 index 0000000..6bc0cdd --- /dev/null +++ b/Sources/BoostKit/MemoryReport.swift @@ -0,0 +1,85 @@ +import Foundation + +/// Plain text a person can paste into a bug report, a forum thread or a message +/// to someone helping with a slow Mac. It holds the figures that answer the +/// first questions anyone asks: how much memory, how much swap, which apps. +/// +/// It lists app names and sizes and nothing else: no file paths, no user name, +/// no process arguments. Nothing leaves the Mac unless the person pastes it. +enum MemoryReport { + + struct App: Equatable { + let name: String + let bytes: UInt64 + } + + struct Machine: Equatable { + var os: String + var model: String + var chip: String + } + + static func text(version: String, machine: Machine, mem: MemStats, apps: [App], + pausedCount: Int, history: LongHistory?, now: Date = Date()) -> String { + var lines: [String] = [] + lines.append("Boost \(version) memory report") + lines.append([machine.os, machine.model, machine.chip, fmtBytes(mem.total)] + .filter { !$0.isEmpty }.joined(separator: ", ")) + lines.append("") + lines.append("Pressure \(Int(mem.pressure * 100))% (\(label(for: mem.level)))") + lines.append("In use \(fmtBytes(mem.used)) of \(fmtBytes(mem.total))") + lines.append("Cached \(fmtBytes(mem.cached)), compressed \(fmtBytes(mem.compressed)), " + + "swap \(mem.swapUsed == 0 ? "none" : fmtBytes(mem.swapUsed))") + if pausedCount > 0 { lines.append("Paused apps: \(pausedCount)") } + + if !apps.isEmpty { + lines.append("") + lines.append("Biggest apps") + for app in apps { lines.append(" \(app.name) \(fmtBytes(app.bytes))") } + } + + if let history, let summary = historySummary(history, now: now) { + lines.append("") + lines.append(summary) + } + return lines.joined(separator: "\n") + } + + static func label(for level: MemStats.Level) -> String { + switch level { + case .easy: return "plenty of headroom" + case .moderate: return "getting busy" + case .tight: return "under pressure" + } + } + + /// Nil until there is enough to say something true about the last day. + static func historySummary(_ history: LongHistory, now: Date) -> String? { + guard history.points(for: .day, now: now).count >= 3, + let peak = history.peak(in: .day, now: now), + let average = history.average(in: .day, now: now) else { return nil } + var parts = ["Last 24 hours: peak \(Int(peak.pressure * 100))% at " + + peak.at.formatted(date: .omitted, time: .shortened), + "average \(Int(average * 100))%"] + let swaps = history.swapEpisodes(in: .day, now: now) + parts.append(swaps == 0 ? "no swap" : "swap appeared \(swaps) \(swaps == 1 ? "time" : "times")") + return parts.joined(separator: ", ") + } + + /// The machine as the report names it. Read from sysctl and ProcessInfo, so + /// it needs no permission. + static func currentMachine() -> Machine { + Machine(os: "macOS " + ProcessInfo.processInfo.operatingSystemVersionString + .replacingOccurrences(of: "Version ", with: ""), + model: sysctlString("hw.model"), + chip: sysctlString("machdep.cpu.brand_string")) + } + + private static func sysctlString(_ name: String) -> String { + var size = 0 + guard sysctlbyname(name, nil, &size, nil, 0) == 0, size > 0 else { return "" } + var buffer = [CChar](repeating: 0, count: size) + guard sysctlbyname(name, &buffer, &size, nil, 0) == 0 else { return "" } + return String(cString: buffer) + } +} diff --git a/Sources/BoostKit/MenuBarPopover.swift b/Sources/BoostKit/MenuBarPopover.swift index 5a2b218..c380cf6 100644 --- a/Sources/BoostKit/MenuBarPopover.swift +++ b/Sources/BoostKit/MenuBarPopover.swift @@ -11,6 +11,7 @@ import AppKit public struct MenuBarContent: View { @ObservedObject var engine: Engine @ObservedObject var disk: DiskEngine + @StateObject private var copied = Flag() public init(engine: Engine = .shared, disk: DiskEngine = .shared) { self.engine = engine @@ -169,6 +170,8 @@ public struct MenuBarContent: View { openMainWindow() } Spacer() + Button(copied.on ? "Copied" : "Copy Report") { copyReport() } + .help("Copies memory figures and app names as plain text, for a bug report. Nothing is sent anywhere.") Button("Settings") { NSApp.activate(ignoringOtherApps: true) NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil) @@ -198,6 +201,22 @@ public struct MenuBarContent: View { } } + private func copyReport() { + let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "dev" + let report = MemoryReport.text( + version: version, machine: MemoryReport.currentMachine(), mem: engine.mem, + apps: topItems.map { MemoryReport.App(name: $0.name, bytes: $0.rssBytes) }, + pausedCount: engine.pausedItems.count, history: engine.longHistory) + NSPasteboard.general.clearContents() + NSPasteboard.general.setString(report, forType: .string) + copied.on = true + let flag = copied + Task { @MainActor in + try? await Task.sleep(for: .seconds(1.5)) + flag.on = false + } + } + /// The window is closed, not destroyed, when you click its red button, so /// bringing it back means finding it rather than making another. private func openMainWindow() { diff --git a/Tests/BoostKitTests/MemoryReportTests.swift b/Tests/BoostKitTests/MemoryReportTests.swift new file mode 100644 index 0000000..f2f39c7 --- /dev/null +++ b/Tests/BoostKitTests/MemoryReportTests.swift @@ -0,0 +1,79 @@ +import Testing +import Foundation +@testable import BoostKit + +private let gb: UInt64 = 1_073_741_824 + +@Suite("Memory report") +struct MemoryReportTests { + private let machine = MemoryReport.Machine(os: "macOS 15.1", model: "Mac15,3", chip: "Apple M3") + private let now = Date(timeIntervalSince1970: 1_800_000_000) + + private func mem(used: UInt64 = 11 * gb, swap: UInt64 = 0) -> MemStats { + MemStats(total: 16 * gb, used: used, cached: 3 * gb, free: gb, compressed: 4 * gb, swapUsed: swap) + } + + @Test("States the figures a helper asks for first") + func headlineFigures() { + let text = MemoryReport.text(version: "1.3.0", machine: machine, mem: mem(), apps: [], + pausedCount: 0, history: nil, now: now) + #expect(text.contains("Boost 1.3.0 memory report")) + #expect(text.contains("macOS 15.1, Mac15,3, Apple M3, 16.00 GB")) + #expect(text.contains("Pressure 68% (plenty of headroom)")) + #expect(text.contains("In use 11.00 GB of 16.00 GB")) + #expect(text.contains("Cached 3.00 GB, compressed 4.00 GB, swap none")) + } + + @Test("Names swap and the level when memory is tight") + func swapAndLevel() { + let text = MemoryReport.text(version: "1.3.0", machine: machine, + mem: mem(used: 15 * gb, swap: 2 * gb), apps: [], + pausedCount: 0, history: nil, now: now) + #expect(text.contains("(under pressure)")) + #expect(text.contains("swap 2.00 GB")) + } + + @Test("Lists apps by name and size, and nothing that identifies the person") + func appsOnly() { + let apps = [MemoryReport.App(name: "Safari", bytes: 700 * 1_048_576), + MemoryReport.App(name: "Claude", bytes: 4 * gb)] + let text = MemoryReport.text(version: "1.3.0", machine: machine, mem: mem(), apps: apps, + pausedCount: 2, history: nil, now: now) + #expect(text.contains("Biggest apps\n Safari 700 MB\n Claude 4.00 GB")) + #expect(text.contains("Paused apps: 2")) + #expect(!text.contains("/Users")) + #expect(!text.contains(NSUserName())) + } + + @Test("Leaves the paused line out when nothing is paused") + func noPausedLine() { + let text = MemoryReport.text(version: "1.3.0", machine: machine, mem: mem(), apps: [], + pausedCount: 0, history: nil, now: now) + #expect(!text.contains("Paused apps")) + } + + @Test("Says nothing about history until there is enough of it") + func shortHistory() { + var history = LongHistory() + history.record(at: now.addingTimeInterval(-60), pressure: 0.5, swapBytes: 0) + #expect(MemoryReport.historySummary(history, now: now) == nil) + } + + @Test("Summarises the last day once it has readings") + func daySummary() throws { + var history = LongHistory() + for m in 0..<30 { + history.record(at: now.addingTimeInterval(Double(m - 30) * 60), + pressure: m == 10 ? 0.9 : 0.6, swapBytes: m == 10 ? 1_500_000_000 : 0) + } + let summary = try #require(MemoryReport.historySummary(history, now: now)) + #expect(summary.hasPrefix("Last 24 hours: peak 90% at ")) + #expect(summary.contains("swap appeared 1 time")) + } + + @Test("Reads this machine without trouble") + func currentMachineIsFilled() { + let m = MemoryReport.currentMachine() + #expect(m.os.hasPrefix("macOS ")) + } +}