mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
- move CPU frequency and temperature to the dedicated reader (less CPU consumption)
- add model identifier to the model name tooltip
This commit is contained in:
@@ -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<SMCService>?
|
||||
private let store: UnsafePointer<Store>
|
||||
|
||||
@@ -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: "%")
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -214,3 +214,29 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
self.callback(processes)
|
||||
}
|
||||
}
|
||||
|
||||
public class AdditionalReader: Reader<CPU_additional> {
|
||||
private let smc: UnsafePointer<SMCService>?
|
||||
private var data: CPU_additional = CPU_additional()
|
||||
|
||||
init(_ smc: UnsafePointer<SMCService>) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1200,7 +1200,7 @@
|
||||
New,
|
||||
);
|
||||
LastSwiftUpdateCheck = 1150;
|
||||
LastUpgradeCheck = 1200;
|
||||
LastUpgradeCheck = 1210;
|
||||
ORGANIZATIONNAME = "Serhiy Mytrovtsiy";
|
||||
TargetAttributes = {
|
||||
9A0C82D924460F7200FAE3D4 = {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "1200"
|
||||
LastUpgradeVersion = "1210"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
|
||||
@@ -238,6 +238,7 @@ class ApplicationSettings: NSView {
|
||||
deviceNameField.font = NSFont.systemFont(ofSize: 14, weight: .regular)
|
||||
deviceNameField.stringValue = systemKit.device.model.name
|
||||
deviceNameField.isSelectable = true
|
||||
deviceNameField.toolTip = systemKit.device.modelIdentifier
|
||||
|
||||
let osField: NSTextField = TextView(frame: NSRect(x: 0, y: 52, width: leftPanel.frame.width, height: 18))
|
||||
osField.alignment = .center
|
||||
|
||||
@@ -70,6 +70,7 @@ public struct info_s {
|
||||
|
||||
public struct device_s {
|
||||
public var model: model_s = model_s(name: LocalizedString("Unknown"), year: 2020, type: .unknown)
|
||||
public var modelIdentifier: String? = nil
|
||||
public var os: os_s? = nil
|
||||
public var info: info_s? = info_s()
|
||||
}
|
||||
@@ -87,6 +88,9 @@ public class SystemKit {
|
||||
os_log(.error, log: self.log, "unknown device %s", modelName)
|
||||
}
|
||||
}
|
||||
if let id = self.modelID() {
|
||||
self.device.modelIdentifier = id
|
||||
}
|
||||
|
||||
let procInfo = ProcessInfo()
|
||||
let systemVersion = procInfo.operatingSystemVersion
|
||||
@@ -123,6 +127,17 @@ public class SystemKit {
|
||||
return nil
|
||||
}
|
||||
|
||||
func modelID() -> 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)
|
||||
|
||||
Reference in New Issue
Block a user