Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148import CryptoKitimport Foundation
// Measurement reports deliberately distinguish missing readings from measured zeroes.enum MeasurementTools { static func run(_ command: String, _ args: [String]) throws -> Int32 { switch command { case "bench-ab-summary": return try MeasurementAB.run(args) case "bench-identity": guard args.isEmpty else { throw DevError("bench-identity takes no arguments") } print(try identity()) case "idle-summary": return try MeasurementIdle.run(args) case "quiet-readings": return try MeasurementQuiet.run(args) case "trace-summary": return try MeasurementTrace.run(args) case "trace-template-field": guard args.count == 3, ["custom", "installed"].contains(args[2]) else { throw DevError("trace-template-field requires manifest, mode and custom|installed") } let manifest = try DevJSON.read(args[0]) guard let value = manifest[args[1]]?[args[2]]?.string, !value.isEmpty else { throw DevError("Missing template field: \(args[1]).\(args[2])") } print(value) case "trace-verdict": guard args.count == 1 else { throw DevError("trace-verdict requires an xunit report") } return try MeasurementTrace.verdict(Data(contentsOf: URL(fileURLWithPath: args[0]))) ? 0 : 1 case "inspectable-entitlements": guard args.count == 1 else { throw DevError("inspectable-entitlements requires a plist") } let entitlements = try PropertyListDecoder().decode( Entitlements.self, from: Data(contentsOf: URL(fileURLWithPath: args[0]))) return entitlements.inspectable == true ? 0 : 2 default: throw DevError("Unknown measurement command: \(command)") } return 0 }
private struct Entitlements: Decodable { let inspectable: Bool? enum CodingKeys: String, CodingKey { case inspectable = "com.apple.security.get-task-allow" } }
private struct PlatformRecord: Decodable { let IOPlatformUUID: String }
static func identity() throws -> String { func command(_ executable: String, _ args: [String]) throws -> String { String(decoding: try DevProcess.checked(executable, args), as: UTF8.self) .trimmingCharacters(in: .whitespacesAndNewlines) } let host = try command("scutil", ["--get", "LocalHostName"]) let model = try command("sysctl", ["-n", "hw.model"]) let cores = try command("sysctl", ["-n", "hw.ncpu"]) let records = try PropertyListDecoder().decode( [PlatformRecord].self, from: DevProcess.checked("ioreg", ["-a", "-r", "-d", "1", "-c", "IOPlatformExpertDevice"])) guard let first = records.first, let uuid = UUID(uuidString: first.IOPlatformUUID) else { throw DevError("Invalid platform UUID") } guard try matches("^[A-Za-z0-9,._-]+$", host), try matches("^[A-Za-z0-9,._-]+$", model) else { throw DevError("Unsafe host or hardware model") } guard try matches("^[1-9][0-9]*$", cores) else { throw DevError("Invalid CPU count") } let digest = SHA256.hash(data: Data(uuid.uuidString.uppercased().utf8)) .map { String(format: "%02x", $0) }.joined().prefix(8) return "\(host)-\(digest)-\(model)-\(cores).json" }
static func options(_ args: [String], allowed: Set<String>) throws -> [String: [String]] { var result: [String: [String]] = [:] var index = 0 while index < args.count { let key = args[index] guard allowed.contains(key), index + 1 < args.count else { throw DevError("Unknown or incomplete measurement option: \(key)") } result[key, default: []].append(args[index + 1]) index += 2 } return result }
static func required(_ key: String, _ options: [String: [String]]) throws -> String { guard let values = options[key], values.count == 1, let value = values.first, !value.isEmpty else { throw DevError("Exactly one \(key) is required") } return value }
static func optional(_ key: String, _ options: [String: [String]]) throws -> String? { guard options[key] != nil else { return nil } return try required(key, options) }
static func read(_ path: String) throws -> String { try String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8) }
static func write<T: Encodable>(_ value: T, output: String?) throws -> Data { let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] var data = try encoder.encode(value) data.append(10) if let output { let url = URL(fileURLWithPath: output) try FileManager.default.createDirectory( at: url.deletingLastPathComponent(), withIntermediateDirectories: true) try data.write(to: url) } return data }
static func captures(_ pattern: String, _ text: String, options: NSRegularExpression.Options = []) throws -> [[String]] { let expression = try NSRegularExpression(pattern: pattern, options: options) return expression.matches(in: text, range: NSRange(text.startIndex..., in: text)).map { match in (0..<match.numberOfRanges).map { index in guard let range = Range(match.range(at: index), in: text) else { return "" } return String(text[range]) } } }
static func matches(_ pattern: String, _ text: String) throws -> Bool { !(try captures(pattern, text)).isEmpty }
static func split(_ pattern: String, _ text: String) throws -> [String] { let expression = try NSRegularExpression(pattern: pattern) let matches = expression.matches(in: text, range: NSRange(text.startIndex..., in: text)) var start = text.startIndex var result: [String] = [] for match in matches { guard let range = Range(match.range, in: text) else { throw DevError("Invalid text range") } result.append(String(text[start..<range.lowerBound])) start = range.upperBound } result.append(String(text[start...])) return result }
static func median(_ values: [Double]) throws -> Double { guard !values.isEmpty else { throw DevError("No readings to summarize") } let sorted = values.sorted() let middle = sorted.count / 2 return sorted.count.isMultiple(of: 2) ? sorted[middle - 1] / 2 + sorted[middle] / 2 : sorted[middle] }}