2020-08-17 17:30:09 +02:00
|
|
|
//
|
|
|
|
|
// main.swift
|
|
|
|
|
// GPU
|
|
|
|
|
//
|
|
|
|
|
// Created by Serhiy Mytrovtsiy on 17/08/2020.
|
|
|
|
|
// Using Swift 5.0.
|
|
|
|
|
// Running on macOS 10.15.
|
|
|
|
|
//
|
|
|
|
|
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Cocoa
|
2021-06-04 19:37:29 +02:00
|
|
|
import Kit
|
2020-08-17 17:30:09 +02:00
|
|
|
|
2020-12-19 19:38:39 +01:00
|
|
|
public typealias GPU_type = String
|
|
|
|
|
public enum GPU_types: GPU_type {
|
|
|
|
|
case unknown = ""
|
|
|
|
|
|
|
|
|
|
case integrated = "i"
|
|
|
|
|
case external = "e"
|
|
|
|
|
case discrete = "d"
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 17:09:38 +02:00
|
|
|
public struct GPU_Info: Codable {
|
2021-01-09 12:37:15 +01:00
|
|
|
public let id: String
|
2020-12-19 19:38:39 +01:00
|
|
|
public let type: GPU_type
|
2021-01-09 12:37:15 +01:00
|
|
|
|
|
|
|
|
public let IOClass: String
|
|
|
|
|
public var vendor: String? = nil
|
|
|
|
|
public let model: String
|
2022-01-14 17:26:06 +01:00
|
|
|
public var cores: Int? = nil
|
2021-01-09 12:37:15 +01:00
|
|
|
|
2020-12-31 11:57:25 +01:00
|
|
|
public var state: Bool = true
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2021-01-09 12:37:15 +01:00
|
|
|
public var fanSpeed: Int? = nil
|
|
|
|
|
public var coreClock: Int? = nil
|
|
|
|
|
public var memoryClock: Int? = nil
|
|
|
|
|
public var temperature: Double? = nil
|
|
|
|
|
public var utilization: Double? = nil
|
2022-01-14 17:26:06 +01:00
|
|
|
public var renderUtilization: Double? = nil
|
|
|
|
|
public var tilerUtilization: Double? = nil
|
2021-01-09 12:37:15 +01:00
|
|
|
|
2022-01-14 17:26:06 +01:00
|
|
|
init(id: String, type: GPU_type, IOClass: String, vendor: String? = nil, model: String, cores: Int?) {
|
2021-02-20 18:54:02 +01:00
|
|
|
self.id = id
|
2021-01-09 12:37:15 +01:00
|
|
|
self.type = type
|
|
|
|
|
self.IOClass = IOClass
|
|
|
|
|
self.vendor = vendor
|
|
|
|
|
self.model = model
|
2022-01-14 17:26:06 +01:00
|
|
|
self.cores = cores
|
2021-01-09 12:37:15 +01:00
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 17:09:38 +02:00
|
|
|
public struct GPUs: value_t, Codable {
|
2020-08-17 20:37:20 +02:00
|
|
|
public var list: [GPU_Info] = []
|
|
|
|
|
|
|
|
|
|
internal func active() -> [GPU_Info] {
|
2021-01-09 12:37:15 +01:00
|
|
|
return self.list.filter{ $0.state && $0.utilization != nil }.sorted{ $0.utilization ?? 0 > $1.utilization ?? 0 }
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-22 14:58:20 +02:00
|
|
|
public var widgetValue: Double {
|
2020-08-17 20:37:20 +02:00
|
|
|
get {
|
2021-01-09 12:37:15 +01:00
|
|
|
return list.isEmpty ? 0 : (list[0].utilization ?? 0)
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-17 17:30:09 +02:00
|
|
|
|
|
|
|
|
public class GPU: Module {
|
2023-02-25 11:46:59 +01:00
|
|
|
private let popupView: Popup
|
|
|
|
|
private let settingsView: Settings
|
|
|
|
|
private let portalView: Portal
|
|
|
|
|
|
2020-08-17 20:37:20 +02:00
|
|
|
private var infoReader: InfoReader? = nil
|
|
|
|
|
|
|
|
|
|
private var selectedGPU: String = ""
|
2022-07-05 20:22:50 +02:00
|
|
|
private var notificationLevelState: Bool = false
|
|
|
|
|
private var notificationID: String? = nil
|
2020-08-17 17:30:09 +02:00
|
|
|
|
2020-12-19 19:38:39 +01:00
|
|
|
private var showType: Bool {
|
|
|
|
|
get {
|
2021-03-20 16:33:14 +01:00
|
|
|
return Store.shared.bool(key: "\(self.config.name)_showType", defaultValue: false)
|
2020-12-19 19:38:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-05 20:22:50 +02:00
|
|
|
private var notificationLevel: String {
|
|
|
|
|
get {
|
|
|
|
|
return Store.shared.string(key: "\(self.config.name)_notificationLevel", defaultValue: "Disabled")
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-19 19:38:39 +01:00
|
|
|
|
2021-03-25 22:14:20 +01:00
|
|
|
public init() {
|
2023-02-25 11:46:59 +01:00
|
|
|
self.popupView = Popup()
|
2021-03-20 16:33:14 +01:00
|
|
|
self.settingsView = Settings("GPU")
|
2023-02-25 11:46:59 +01:00
|
|
|
self.portalView = Portal("GPU")
|
2020-08-17 17:30:09 +02:00
|
|
|
|
|
|
|
|
super.init(
|
2020-08-17 20:37:20 +02:00
|
|
|
popup: self.popupView,
|
2023-02-25 11:46:59 +01:00
|
|
|
settings: self.settingsView,
|
|
|
|
|
portal: self.portalView
|
2020-08-17 17:30:09 +02:00
|
|
|
)
|
|
|
|
|
guard self.available else { return }
|
|
|
|
|
|
2023-06-29 17:46:40 +02:00
|
|
|
self.infoReader = InfoReader(.GPU)
|
2021-03-20 16:33:14 +01:00
|
|
|
self.selectedGPU = Store.shared.string(key: "\(self.config.name)_gpu", defaultValue: self.selectedGPU)
|
2020-08-17 17:30:09 +02:00
|
|
|
|
2023-08-13 13:15:27 +02:00
|
|
|
self.infoReader?.callbackHandler = { [weak self] value in
|
|
|
|
|
self?.infoCallback(value)
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2023-08-13 13:15:27 +02:00
|
|
|
self.infoReader?.readyCallback = { [weak self] in
|
|
|
|
|
self?.readyHandler()
|
2021-02-16 20:01:46 +01:00
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2023-08-13 13:15:27 +02:00
|
|
|
self.settingsView.selectedGPUHandler = { [weak self] value in
|
|
|
|
|
self?.selectedGPU = value
|
|
|
|
|
self?.infoReader?.read()
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2023-08-13 13:15:27 +02:00
|
|
|
self.settingsView.setInterval = { [weak self] value in
|
|
|
|
|
self?.infoReader?.setInterval(value)
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
2023-08-13 13:15:27 +02:00
|
|
|
self.settingsView.callback = { [weak self] in
|
|
|
|
|
self?.infoReader?.read()
|
2020-12-19 19:38:39 +01:00
|
|
|
}
|
2020-08-17 17:30:09 +02:00
|
|
|
|
2020-08-17 20:37:20 +02:00
|
|
|
if let reader = self.infoReader {
|
2020-08-17 17:30:09 +02:00
|
|
|
self.addReader(reader)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2020-12-19 17:25:11 +01:00
|
|
|
private func infoCallback(_ raw: GPUs?) {
|
2023-08-13 13:15:27 +02:00
|
|
|
guard raw != nil && !raw!.list.isEmpty, let value = raw, self.enabled else { return }
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2020-11-26 15:35:30 +01:00
|
|
|
DispatchQueue.main.async(execute: {
|
2020-12-19 17:25:11 +01:00
|
|
|
self.popupView.infoCallback(value)
|
2020-11-26 15:35:30 +01:00
|
|
|
})
|
2020-12-19 17:25:11 +01:00
|
|
|
self.settingsView.setList(value)
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2020-12-19 17:25:11 +01:00
|
|
|
let activeGPUs = value.active()
|
2020-12-19 19:38:39 +01:00
|
|
|
guard let activeGPU = activeGPUs.first(where: { $0.state }) ?? activeGPUs.first else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let selectedGPU: GPU_Info = activeGPUs.first{ $0.model == self.selectedGPU } ?? activeGPU
|
2021-01-09 12:37:15 +01:00
|
|
|
guard let utilization = selectedGPU.utilization else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
|
2023-02-25 11:46:59 +01:00
|
|
|
self.portalView.loadCallback(selectedGPU)
|
2022-07-05 20:22:50 +02:00
|
|
|
self.checkNotificationLevel(utilization)
|
|
|
|
|
|
2022-07-09 11:37:00 +02:00
|
|
|
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
|
2021-02-13 16:39:11 +01:00
|
|
|
switch w.item {
|
|
|
|
|
case let widget as Mini:
|
|
|
|
|
widget.setValue(utilization)
|
|
|
|
|
widget.setTitle(self.showType ? "\(selectedGPU.type)GPU" : nil)
|
|
|
|
|
case let widget as LineChart: widget.setValue(utilization)
|
2021-08-02 20:11:48 +02:00
|
|
|
case let widget as BarChart: widget.setValue([[ColorValue(utilization)]])
|
2021-10-13 18:27:31 +02:00
|
|
|
case let widget as Tachometer:
|
|
|
|
|
widget.setValue([
|
|
|
|
|
circle_segment(value: utilization, color: NSColor.systemBlue)
|
|
|
|
|
])
|
2021-02-13 16:39:11 +01:00
|
|
|
default: break
|
|
|
|
|
}
|
2020-08-17 20:37:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-05 20:22:50 +02:00
|
|
|
|
|
|
|
|
private func checkNotificationLevel(_ value: Double) {
|
|
|
|
|
guard self.notificationLevel != "Disabled", let level = Double(self.notificationLevel) else { return }
|
|
|
|
|
|
|
|
|
|
if let id = self.notificationID, value < level && self.notificationLevelState {
|
2023-01-31 18:27:43 +01:00
|
|
|
removeNotification(id)
|
2022-07-05 20:22:50 +02:00
|
|
|
self.notificationID = nil
|
|
|
|
|
self.notificationLevelState = false
|
|
|
|
|
} else if value >= level && !self.notificationLevelState {
|
2023-01-31 18:27:43 +01:00
|
|
|
self.notificationID = showNotification(
|
|
|
|
|
title: localizedString("GPU usage threshold"),
|
|
|
|
|
subtitle: localizedString("GPU usage is", "\(Int((value)*100))%")
|
|
|
|
|
)
|
2022-07-05 20:22:50 +02:00
|
|
|
self.notificationLevelState = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-17 17:30:09 +02:00
|
|
|
}
|