Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
PEEL_MEASURE names a report in Peel's logs folder, never a path
Țugui Dragoșa0aef6815872
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/MeasureFile.swift b/Packages/PeelCore/Sources/PeelCore/Support/MeasureFile.swiftnew file mode 100644index 00000000..05e89018--- /dev/null+++ b/Packages/PeelCore/Sources/PeelCore/Support/MeasureFile.swift@@ -0,0 +1,23 @@+public import Foundation++/// Where `PEEL_MEASURE` sends its reports: a file name in Peel's own logs folder, never a path. Whoever launches+/// Peel chooses the name, and Peel may hold rights its launcher lacks, such as Full Disk Access.+public struct MeasureFile: Sendable {+ public let url: URL++ /// Nil for anything but a plain file name: a path, or a hidden name.+ public init?(named name: String, home: URL = .homeDirectory) {+ guard !name.isEmpty, !name.contains("/"), !name.hasPrefix(".") else { return nil }+ url = home.appending(path: "Library/Logs/Peel", directoryHint: .isDirectory).appending(path: name)+ }++ /// Appends `lines`, making the folder and the file when needed, and never through a link put at that name.+ public func append(_ lines: [String]) {+ try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ let descriptor = open(url.path(percentEncoded: false), O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0o600)+ guard descriptor >= 0 else { return }+ defer { close(descriptor) }+ let data = Data(lines.map { $0 + "\n" }.joined().utf8)+ _ = data.withUnsafeBytes { write(descriptor, $0.baseAddress, $0.count) }+ }+}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/MeasureFileTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/MeasureFileTests.swiftnew file mode 100644index 00000000..e982b9b7--- /dev/null+++ b/Packages/PeelCore/Tests/PeelCoreTests/MeasureFileTests.swift@@ -0,0 +1,37 @@+import Foundation+@testable import PeelCore+import Testing++struct MeasureFileTests {+ @Test func takesAFileNameAndNeverAPath() throws {+ let home = URL(filePath: "/Users/x", directoryHint: .isDirectory)+ for refused in ["", "/tmp/report.txt", "Logs/report.txt", "../report.txt", ".report", "."] {+ #expect(MeasureFile(named: refused, home: home) == nil, "\(refused.debugDescription) was taken")+ }+ let file = try #require(MeasureFile(named: "report.txt", home: home))+ #expect(file.url.path(percentEncoded: false) == "/Users/x/Library/Logs/Peel/report.txt")+ }++ @Test func appendsEachReport() throws {+ let directory = try TemporaryDirectory()+ let file = try #require(MeasureFile(named: "report.txt", home: directory.url))++ file.append(["first frame: 10.0 ms after exec"])+ file.append(["run loop: 3 turns"])++ #expect(try String(contentsOf: file.url, encoding: .utf8) == "first frame: 10.0 ms after exec\nrun loop: 3 turns\n")+ }++ /// Peel may hold rights its launcher lacks, so a link put at the report's name is never written through.+ @Test func neverWritesThroughALink() throws {+ let directory = try TemporaryDirectory()+ let target = try directory.file("Elsewhere/data.db", contents: Data("kept".utf8))+ try directory.directory("Library/Logs/Peel")+ try FileManager.default.createSymbolicLink(at: directory.url.appending(path: "Library/Logs/Peel/report.txt"), withDestinationURL: target)+ let file = try #require(MeasureFile(named: "report.txt", home: directory.url))++ file.append(["run loop: 3 turns"])++ #expect(try String(contentsOf: target, encoding: .utf8) == "kept")+ }+}diff --git a/Peel/FrameWatch.swift b/Peel/FrameWatch.swiftindex 493ae214..8002c164 100644--- a/Peel/FrameWatch.swift+++ b/Peel/FrameWatch.swift@@ -4,15 +4,15 @@ import PeelCoreimport QuartzCoreimport SwiftUI-/// Counts hitches while `PEEL_MEASURE=<file>` is set: main thread turns longer than a frame, and missed frames.-/// Peel appends to that file how long after exec its first frame came, then a report (`Hitches`) on every SIGUSR1-/// and when it quits. Long turns also go to the log as Points of Interest events. It is in every build, not only-/// Debug, because Release timings are the ones that count. Its display link wakes Peel every frame, so leave it-/// off when measuring idle CPU.+/// Counts hitches while `PEEL_MEASURE=<name>` is set: main thread turns longer than a frame, and missed frames.+/// Peel appends to `~/Library/Logs/Peel/<name>` (`MeasureFile`) how long after exec its first frame came, then a+/// report (`Hitches`) on every SIGUSR1 and when it quits. Long turns also go to the log as Points of Interest events.+/// It is in every build, not only Debug, because Release timings are the ones that count. Its display link wakes+/// Peel every frame, so leave it off when measuring idle CPU.final class FrameWatch: NSObject {- static let shared = ProcessInfo.processInfo.environment["PEEL_MEASURE"].map { FrameWatch(file: URL(filePath: $0)) }+ static let shared = ProcessInfo.processInfo.environment["PEEL_MEASURE"].flatMap { MeasureFile(named: $0) }.map { FrameWatch(file: $0) }- private let file: URL+ private let file: MeasureFileprivate var hitches = Hitches()private var busySince: CFTimeInterval?private var link: CADisplayLink?@@ -21,7 +21,7 @@ final class FrameWatch: NSObject {private var sight: (any NSObjectProtocol)?private var hasDrawn = false- private init(file: URL) {+ private init(file: MeasureFile) {self.file = file}@@ -57,7 +57,7 @@ final class FrameWatch: NSObject {if !hasDrawn {hasDrawn = trueif let started = Self.processStart() {- write([String(format: "first frame: %.1f ms after exec", (Date().timeIntervalSince1970 - started) * 1000)])+ file.append([String(format: "first frame: %.1f ms after exec", (Date().timeIntervalSince1970 - started) * 1000)])}}hitches.frame(at: link.timestamp, due: link.targetTimestamp)@@ -103,20 +103,10 @@ final class FrameWatch: NSObject {}private func report() {- write(hitches.report)+ file.append(hitches.report)hitches = Hitches()}- private func write(_ lines: [String]) {- if !FileManager.default.fileExists(atPath: file.path) {- FileManager.default.createFile(atPath: file.path, contents: nil)- }- guard let handle = try? FileHandle(forWritingTo: file) else { return }- defer { try? handle.close() }- _ = try? handle.seekToEnd()- try? handle.write(contentsOf: Data(lines.map { $0 + "\n" }.joined().utf8))- }-/// Returns when the kernel started this process, in seconds since 1970. Nothing the process times itself can be/// earlier, so the first frame is timed from the real start.private static func processStart() -> Double? {