Hyperthreading mode in Bar Chart

This commit is contained in:
Serhiy Mytrovtsiy
2019-07-12 22:19:44 +02:00
parent 6fc7b97a53
commit fae60f74b7
7 changed files with 69 additions and 37 deletions

View File

@@ -18,6 +18,7 @@ class CPU: Module {
var available: Observable<Bool>
var color: Observable<Bool>
var label: Observable<Bool>
var hyperthreading: Observable<Bool>
var reader: Reader = CPUReader()
let defaults = UserDefaults.standard
@@ -26,6 +27,7 @@ class CPU: Module {
init() {
self.available = Observable(true)
self.active = Observable(defaults.object(forKey: name) != nil ? defaults.bool(forKey: name) : true)
self.hyperthreading = Observable(defaults.object(forKey: "\(name)_hyperthreading") != nil ? defaults.bool(forKey: "\(name)_hyperthreading") : true)
self.widgetType = defaults.object(forKey: "\(name)_widget") != nil ? defaults.float(forKey: "\(name)_widget") : Widgets.Mini
self.color = Observable(defaults.object(forKey: "\(name)_color") != nil ? defaults.bool(forKey: "\(name)_color") : false)
self.label = Observable(defaults.object(forKey: "\(name)_label") != nil ? defaults.bool(forKey: "\(name)_label") : true)
@@ -34,6 +36,7 @@ class CPU: Module {
if self.widgetType == Widgets.BarChart {
(self.reader as! CPUReader).perCoreMode = true
(self.reader as! CPUReader).hyperthreading = self.hyperthreading.value
}
}
@@ -64,6 +67,10 @@ class CPU: Module {
barChart.state = self.widgetType == Widgets.BarChart ? NSControl.StateValue.on : NSControl.StateValue.off
barChart.target = self
let hyperthreading = NSMenuItem(title: "Hyperthreading", action: #selector(toggleHyperthreading), keyEquivalent: "")
hyperthreading.state = self.hyperthreading.value ? NSControl.StateValue.on : NSControl.StateValue.off
hyperthreading.target = self
let color = NSMenuItem(title: "Color", action: #selector(toggleColor), keyEquivalent: "")
color.state = self.color.value ? NSControl.StateValue.on : NSControl.StateValue.off
color.target = self
@@ -79,6 +86,9 @@ class CPU: Module {
submenu.addItem(NSMenuItem.separator())
if self.widgetType == Widgets.BarChart {
submenu.addItem(hyperthreading)
}
if self.widgetType == Widgets.BarChart || self.widgetType == Widgets.ChartWithValue || self.widgetType == Widgets.Chart {
submenu.addItem(label)
}
@@ -145,6 +155,13 @@ class CPU: Module {
self.active << true
}
@objc func toggleHyperthreading(_ sender: NSMenuItem) {
sender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.on
self.defaults.set(sender.state == NSControl.StateValue.on, forKey: "\(name)_hyperthreading")
self.hyperthreading << (sender.state == NSControl.StateValue.on)
(self.reader as! CPUReader).hyperthreading = sender.state == NSControl.StateValue.on
}
@objc func toggleColor(_ sender: NSMenuItem) {
sender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.on
self.defaults.set(sender.state == NSControl.StateValue.on, forKey: "\(name)_color")

View File

@@ -20,6 +20,7 @@ class CPUReader: Reader {
let CPUUsageLock: NSLock = NSLock()
var perCoreMode: Bool = false
var hyperthreading: Bool = true
init() {
let mibKeys: [Int32] = [ CTL_HW, HW_NCPU ]
@@ -59,7 +60,12 @@ class CPUReader: Reader {
var totalOnAllCores: Int32 = 0
var usagePerCore: [Double] = []
for i in 0 ..< Int32(numCPUs) {
var incrementNumber = 1
if !self.hyperthreading && self.perCoreMode {
incrementNumber = 2
}
for i in stride(from: 0, to: Int32(numCPUs), by: incrementNumber){
var inUse: Int32
var total: Int32
if let prevCpuInfo = prevCpuInfo {
@@ -80,7 +86,7 @@ class CPUReader: Reader {
inUseOnAllCores = inUseOnAllCores + inUse
totalOnAllCores = totalOnAllCores + total
usagePerCore.insert((Double(inUse) / Double(total)), at: Int(i))
usagePerCore.append(Double(inUse) / Double(total))
}
if self.perCoreMode {

View File

@@ -74,6 +74,10 @@ class Network: Module {
arrowsWithText.state = self.widgetType == Widgets.NetworkArrowsWithText ? NSControl.StateValue.on : NSControl.StateValue.off
arrowsWithText.target = self
let chart = NSMenuItem(title: "Chart", action: #selector(toggleWidget), keyEquivalent: "")
chart.state = self.widgetType == Widgets.NetworkChart ? NSControl.StateValue.on : NSControl.StateValue.off
chart.target = self
submenu.addItem(dots)
submenu.addItem(arrows)
submenu.addItem(text)
@@ -113,6 +117,8 @@ class Network: Module {
widgetCode = Widgets.NetworkDotsWithText
case "Arrows with text":
widgetCode = Widgets.NetworkArrowsWithText
case "Chart":
widgetCode = Widgets.NetworkChart
default:
break
}
@@ -122,7 +128,7 @@ class Network: Module {
}
for item in self.submenu.items {
if item.title == "Dots" || item.title == "Arrows" || item.title == "Text" || item.title == "Dots with text" || item.title == "Arrows with text" {
if item.title == "Dots" || item.title == "Arrows" || item.title == "Text" || item.title == "Dots with text" || item.title == "Arrows with text" || item.title == "Chart" {
item.state = NSControl.StateValue.off
}
}

View File

@@ -46,12 +46,7 @@ class NetworkReader: Reader {
guard let download = Int64(arr[2]), let upload = Int64(arr[5]) else {
return
}
guard let value: Double = Double("\(download).\(upload)") else {
return
}
self.value << [value]
self.value << [Double(download), Double(upload)]
}
}

View File

@@ -122,6 +122,9 @@ class BarChart: NSView, Widget {
if self.partitions.count == 1 {
width = 18
}
if self.partitions.count == 2 {
width = 28
}
if self.labelEnabled {
width += labelPadding
}

View File

@@ -68,14 +68,14 @@ class NetworkDotsView: NSView, Widget {
}
func setValue(data: [Double]) {
let value: Double = data.first!
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])
let values = value.splitAtDecimal()
if self.download != values[0] {
self.download = values[0]
if self.download != download {
self.download = download
}
if self.upload != values[1] {
self.upload = values[1]
if self.upload != upload {
self.upload = upload
}
}
@@ -118,10 +118,11 @@ class NetworkTextView: NSView, Widget {
}
func setValue(data: [Double]) {
let value: Double = data.first!
let values = value.splitAtDecimal()
downloadValue.stringValue = Units(bytes: values[0]).getReadableUnit()
uploadValue.stringValue = Units(bytes: values[1]).getReadableUnit()
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])
downloadValue.stringValue = Units(bytes: download).getReadableUnit()
uploadValue.stringValue = Units(bytes: upload).getReadableUnit()
}
func toggleColor(state: Bool) {
@@ -237,13 +238,14 @@ class NetworkArrowsView: NSView, Widget {
}
func setValue(data: [Double]) {
let value: Double = data.first!
let values = value.splitAtDecimal()
if self.download != values[0] {
self.download = values[0]
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])
if self.download != download {
self.download = download
}
if self.upload != values[1] {
self.upload = values[1]
if self.upload != upload {
self.upload = upload
}
}
@@ -320,14 +322,15 @@ class NetworkDotsTextView: NSView, Widget {
}
func setValue(data: [Double]) {
let value: Double = data.first!
let values = value.splitAtDecimal()
if self.download != values[0] {
self.download = values[0]
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])
if self.download != download {
self.download = download
downloadValue.stringValue = Units(bytes: self.download).getReadableUnit()
}
if self.upload != values[1] {
self.upload = values[1]
if self.upload != upload {
self.upload = upload
uploadValue.stringValue = Units(bytes: self.upload).getReadableUnit()
}
}
@@ -448,14 +451,15 @@ class NetworkArrowsTextView: NSView, Widget {
}
func setValue(data: [Double]) {
let value: Double = data.first!
let values = value.splitAtDecimal()
if self.download != values[0] {
self.download = values[0]
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])
if self.download != download {
self.download = download
downloadValue.stringValue = Units(bytes: self.download).getReadableUnit()
}
if self.upload != values[1] {
self.upload = values[1]
if self.upload != upload {
self.upload = upload
uploadValue.stringValue = Units(bytes: self.upload).getReadableUnit()
}
}

View File

@@ -36,6 +36,7 @@ struct Widgets {
static let NetworkText: WidgetType = 2.2
static let NetworkDotsWithText: WidgetType = 2.3
static let NetworkArrowsWithText: WidgetType = 2.4
static let NetworkChart: WidgetType = 2.5
static let BarChart: WidgetType = 3.0
}