From 2100c0b134c6b4a889cbaf7c63af042005237afd Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Thu, 22 Oct 2020 20:50:34 +0200 Subject: [PATCH] - move CPU frequency and temperature to the dedicated reader (less CPU consumption) - add model identifier to the model name tooltip --- Modules/CPU/main.swift | 24 ++++++++++----- Modules/CPU/popup.swift | 30 ++++++++++++------- Modules/CPU/readers.swift | 26 ++++++++++++++++ Stats.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/xcschemes/Stats.xcscheme | 2 +- Stats/Views/AppSettings.swift | 1 + StatsKit/SystemKit.swift | 15 ++++++++++ 7 files changed, 81 insertions(+), 19 deletions(-) diff --git a/Modules/CPU/main.swift b/Modules/CPU/main.swift index 112844d2..b4a964d7 100644 --- a/Modules/CPU/main.swift +++ b/Modules/CPU/main.swift @@ -25,12 +25,18 @@ public struct CPU_Load: value_t { } } +public struct CPU_additional { + var temperature: Double? + var frequency: Double? +} + public class CPU: Module { private var popupView: Popup private var settingsView: Settings private var loadReader: LoadReader? = nil private var processReader: ProcessReader? = nil + private var additionalReader: AdditionalReader? = nil private let smc: UnsafePointer? private let store: UnsafePointer @@ -58,6 +64,7 @@ public class CPU: Module { self.loadReader?.store = store self.processReader = ProcessReader() + self.additionalReader = AdditionalReader(smc) self.settingsView.callback = { [unowned self] in self.loadReader?.read() @@ -79,12 +86,21 @@ public class CPU: Module { } } + self.additionalReader?.callbackHandler = { [unowned self] value in + if value != nil { + self.popupView.additionalCallback(value!) + } + } + if let reader = self.loadReader { self.addReader(reader) } if let reader = self.processReader { self.addReader(reader) } + if let reader = self.additionalReader { + self.addReader(reader) + } } public override func willTerminate() { @@ -96,13 +112,7 @@ public class CPU: Module { return } - var frequency: Double? = nil - if let readFrequency = PG_getCPUFrequency() { - frequency = readFrequency.pointee - } - - let temperature = self.smc?.pointee.getValue("TC0C") ?? self.smc?.pointee.getValue("TC0D") ?? self.smc?.pointee.getValue("TC0P") ?? self.smc?.pointee.getValue("TC0E") - self.popupView.loadCallback(value!, tempValue: temperature, frequency: frequency) + self.popupView.loadCallback(value!) if let widget = self.widget as? Mini { widget.setValue(value!.totalUsage, sufix: "%") diff --git a/Modules/CPU/popup.swift b/Modules/CPU/popup.swift index 9a16b5d7..19e55d92 100644 --- a/Modules/CPU/popup.swift +++ b/Modules/CPU/popup.swift @@ -33,6 +33,7 @@ internal class Popup: NSView { private var temperatureCircle: HalfCircleGraphView? = nil private var frequencyCircle: HalfCircleGraphView? = nil private var initialized: Bool = false + private var initializedAdditional: Bool = false private var processesInitialized: Bool = false private var processes: [ProcessView] = [] @@ -157,12 +158,8 @@ internal class Popup: NSView { return valueView } - public func loadCallback(_ value: CPU_Load, tempValue: Double?, frequency: Double?) { + public func loadCallback(_ value: CPU_Load) { DispatchQueue.main.async(execute: { - if frequency != nil && (self.frequencyCircle! as NSView).isHidden { - (self.frequencyCircle! as NSView).isHidden = false - } - if (self.window?.isVisible ?? false) || !self.initialized { self.systemField?.stringValue = "\(Int(value.systemLoad.rounded(toPlaces: 2) * 100)) %" self.userField?.stringValue = "\(Int(value.userLoad.rounded(toPlaces: 2) * 100)) %" @@ -177,11 +174,23 @@ internal class Popup: NSView { circle_segment(value: value.systemLoad, color: NSColor.systemRed), circle_segment(value: value.userLoad, color: NSColor.systemBlue), ]) - if tempValue != nil { - self.temperatureCircle?.setValue(tempValue!) - self.temperatureCircle?.setText(Temperature(tempValue!)) + } + self.chart?.addValue(value.totalUsage) + }) + } + + public func additionalCallback(_ value: CPU_additional) { + DispatchQueue.main.async(execute: { + if value.frequency != nil && (self.frequencyCircle! as NSView).isHidden { + (self.frequencyCircle! as NSView).isHidden = false + } + + if (self.window?.isVisible ?? false) || !self.initializedAdditional { + if let temperature = value.temperature { + self.temperatureCircle?.setValue(temperature) + self.temperatureCircle?.setText(Temperature(temperature)) } - if let freq = frequency { + if let freq = value.frequency { if freq > self.maxFreq { self.maxFreq = freq } @@ -191,8 +200,9 @@ internal class Popup: NSView { freqCircle.setText("\((freq/1000).rounded(toPlaces: 2))\nGHz") } } + + self.initializedAdditional = true } - self.chart?.addValue(value.totalUsage) }) } diff --git a/Modules/CPU/readers.swift b/Modules/CPU/readers.swift index 450926fc..5d5909eb 100644 --- a/Modules/CPU/readers.swift +++ b/Modules/CPU/readers.swift @@ -214,3 +214,29 @@ public class ProcessReader: Reader<[TopProcess]> { self.callback(processes) } } + +public class AdditionalReader: Reader { + private let smc: UnsafePointer? + private var data: CPU_additional = CPU_additional() + + init(_ smc: UnsafePointer) { + self.smc = smc + super.init() + self.popup = true + } + + public override func setup() { + PG_getCPUFrequency() + PG_getCPUFrequency() + } + + public override func read() { + if let readFrequency = PG_getCPUFrequency() { + self.data.frequency = readFrequency.pointee + } + + self.data.temperature = self.smc?.pointee.getValue("TC0C") ?? self.smc?.pointee.getValue("TC0D") ?? self.smc?.pointee.getValue("TC0P") ?? self.smc?.pointee.getValue("TC0E") + + self.callback(self.data) + } +} diff --git a/Stats.xcodeproj/project.pbxproj b/Stats.xcodeproj/project.pbxproj index 31473447..ef15f004 100644 --- a/Stats.xcodeproj/project.pbxproj +++ b/Stats.xcodeproj/project.pbxproj @@ -1200,7 +1200,7 @@ New, ); LastSwiftUpdateCheck = 1150; - LastUpgradeCheck = 1200; + LastUpgradeCheck = 1210; ORGANIZATIONNAME = "Serhiy Mytrovtsiy"; TargetAttributes = { 9A0C82D924460F7200FAE3D4 = { diff --git a/Stats.xcodeproj/xcshareddata/xcschemes/Stats.xcscheme b/Stats.xcodeproj/xcshareddata/xcschemes/Stats.xcscheme index f91638a2..562677d8 100644 --- a/Stats.xcodeproj/xcshareddata/xcschemes/Stats.xcscheme +++ b/Stats.xcodeproj/xcshareddata/xcschemes/Stats.xcscheme @@ -1,6 +1,6 @@ String? { + let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")) + var modelIdentifier: String? + if let modelData = IORegistryEntryCreateCFProperty(service, "model" as CFString, kCFAllocatorDefault, 0).takeRetainedValue() as? Data { + modelIdentifier = String(data: modelData, encoding: .utf8)?.trimmingCharacters(in: .controlCharacters) + } + + IOObjectRelease(service) + return modelIdentifier + } + private func getCPUInfo() -> cpu_s? { var sizeOfName = 0 sysctlbyname("machdep.cpu.brand_string", nil, &sizeOfName, nil, 0)