diff --git a/Kit/helpers.swift b/Kit/helpers.swift index 516f9417..93371a43 100644 --- a/Kit/helpers.swift +++ b/Kit/helpers.swift @@ -1076,7 +1076,7 @@ public class SMCHelper { guard let helper = self.helper(nil) else { return } helper.setFanSpeed(id: id, value: speed) { result in if let result, !result.isEmpty { - print(result) + NSLog("set fan speed: \(result)") } } } @@ -1085,11 +1085,18 @@ public class SMCHelper { guard let helper = self.helper(nil) else { return } helper.setFanMode(id: id, mode: mode) { result in if let result, !result.isEmpty { - print(result) + NSLog("set fan mode: \(result)") } } } + public func powermetrics(_ samplers: [String], completion: @escaping (String?) -> Void) { + guard let helper = self.helper(nil) else { return } + helper.powermetrics(samplers) { result in + completion(result) + } + } + public func isActive() -> Bool { return self.connection != nil } diff --git a/Modules/CPU/main.swift b/Modules/CPU/main.swift index b9a78604..1d6a0fdc 100644 --- a/Modules/CPU/main.swift +++ b/Modules/CPU/main.swift @@ -32,13 +32,6 @@ public struct CPU_Limit: Codable { var speed: Int = 0 } -public struct CPU_Frequency: Codable { - var ECores: Int? = nil - var PCores: Int? = nil - var power: Int? = nil - var cores: [Int] = [] -} - public class CPU: Module { private let popupView: Popup private let settingsView: Settings diff --git a/SMC/Helper/Info.plist b/SMC/Helper/Info.plist index 07ad550c..e606227f 100644 --- a/SMC/Helper/Info.plist +++ b/SMC/Helper/Info.plist @@ -7,7 +7,7 @@ CFBundleName eu.exelban.Stats.SMC.Helper CFBundleShortVersionString - 1.0.0 + 1.0.1 CFBundleVersion 1 SMAuthorizedClients diff --git a/SMC/Helper/main.swift b/SMC/Helper/main.swift index 03dd1550..ea7db856 100644 --- a/SMC/Helper/main.swift +++ b/SMC/Helper/main.swift @@ -69,7 +69,7 @@ class Helper: NSObject, NSXPCListenerDelegate, HelperProtocol { private func uninstallHelper() { let process = Process() process.launchPath = "/bin/launchctl" - process.qualityOfService = QualityOfService.utility + process.qualityOfService = QualityOfService.userInitiated process.arguments = ["unload", "/Library/LaunchDaemons/eu.exelban.Stats.SMC.Helper.plist"] process.launch() process.waitUntilExit() @@ -109,7 +109,14 @@ extension Helper { return } let result = syncShell("\(smc) fan \(id) -m \(mode)") - completion(result) + + if let error = result.error, !error.isEmpty { + NSLog("error set fan mode: \(error)") + completion(nil) + return + } + + completion(result.output) } func setFanSpeed(id: Int, value: Int, completion: (String?) -> Void) { @@ -117,28 +124,62 @@ extension Helper { completion("missing smc tool") return } + let result = syncShell("\(smc) fan \(id) -v \(value)") - completion(result) + + if let error = result.error, !error.isEmpty { + NSLog("error set fan speed: \(error)") + completion(nil) + return + } + + completion(result.output) } - public func syncShell(_ args: String) -> String { + func powermetrics(_ samplers: [String], completion: @escaping (String?) -> Void) { + let result = syncShell("powermetrics -n 1 -s \(samplers.joined(separator: ",")) --sample-rate 1000") + if let error = result.error, !error.isEmpty { + NSLog("error call powermetrics: \(error)") + completion(nil) + return + } + completion(result.output) + } + + public func syncShell(_ args: String) -> (output: String?, error: String?) { let task = Process() task.launchPath = "/bin/sh" task.arguments = ["-c", args] - let pipe = Pipe() - task.standardOutput = pipe - task.launch() - task.waitUntilExit() + let outputPipe = Pipe() + let errorPipe = Pipe() - let data = pipe.fileHandleForReading.readDataToEndOfFile() - return String(data: data, encoding: .utf8)! + defer { + outputPipe.fileHandleForReading.closeFile() + errorPipe.fileHandleForReading.closeFile() + } + + task.standardOutput = outputPipe + task.standardError = errorPipe + + do { + try task.run() + } catch let err { + return (nil, "syncShell: \(err.localizedDescription)") + } + + let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile() + let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile() + let output = String(decoding: outputData, as: UTF8.self) + let error = String(decoding: errorData, as: UTF8.self) + + return (output, error) } func uninstall() { let process = Process() process.launchPath = "/Library/PrivilegedHelperTools/eu.exelban.Stats.SMC.Helper" - process.qualityOfService = QualityOfService.utility + process.qualityOfService = QualityOfService.userInitiated process.arguments = ["uninstall", String(getpid())] process.launch() exit(0) diff --git a/SMC/Helper/protocol.swift b/SMC/Helper/protocol.swift index 1f70a348..e22e52d2 100644 --- a/SMC/Helper/protocol.swift +++ b/SMC/Helper/protocol.swift @@ -17,6 +17,7 @@ import Foundation func setFanMode(id: Int, mode: Int, completion: @escaping (String?) -> Void) func setFanSpeed(id: Int, value: Int, completion: @escaping (String?) -> Void) + func powermetrics(_ samplers: [String], completion: @escaping (String?) -> Void) func uninstall() }