From 6cee395f0636e17174881fa88e4a34f1f1e6a3dc Mon Sep 17 00:00:00 2001 From: "Sean E. Russell" Date: Thu, 23 Apr 2020 13:01:13 -0500 Subject: Add sensible defaults for temps. Improve config writing. --- cmd/gotop/main.go | 2 +- config.go | 34 +++++++++++++++++++++++++++++----- devices/devices.go | 37 ++++++++++++++++++++++++++----------- devices/temp_darwin.go | 16 +++++++++++++--- devices/temp_freebsd.go | 2 +- devices/temp_linux.go | 42 +++++++++++++++++++++++++++++------------- devices/temp_windows.go | 2 +- widgets/temp.go | 2 +- 8 files changed, 101 insertions(+), 36 deletions(-) diff --git a/cmd/gotop/main.go b/cmd/gotop/main.go index 18525c6..4f611a1 100644 --- a/cmd/gotop/main.go +++ b/cmd/gotop/main.go @@ -578,7 +578,7 @@ func listDevices() { sort.Strings(ms) for _, m := range ms { fmt.Printf("%s:\n", m) - for _, d := range devices.Devices(m) { + for _, d := range devices.Devices(m, true) { fmt.Printf("\t%s\n", d) } } diff --git a/config.go b/config.go index 7809918..0979806 100644 --- a/config.go +++ b/config.go @@ -107,11 +107,15 @@ func (conf *Config) Load() error { } conf.PercpuLoad = bv case tempscale: - iv, err := strconv.Atoi(kv[1]) - if err != nil { - return err + switch kv[1] { + case "C": + conf.TempScale = 'C' + case "F": + conf.TempScale = 'F' + default: + conf.TempScale = 'C' + return fmt.Errorf("invalid TempScale value %s", kv[1]) } - conf.TempScale = widgets.TempScale(iv) case statusbar: bv, err := strconv.ParseBool(kv[1]) if err != nil { @@ -159,19 +163,39 @@ func (c *Config) Write() (string, error) { func marshal(c *Config) []byte { buff := bytes.NewBuffer(nil) + fmt.Fprintln(buff, "# Scale graphs to this level; 7 is the default, 2 is zoomed out.") fmt.Fprintf(buff, "%s=%d\n", graphhorizontalscale, c.GraphHorizontalScale) + fmt.Fprintln(buff, "# If true, start the UI with the help visible") fmt.Fprintf(buff, "%s=%t\n", helpvisible, c.HelpVisible) + fmt.Fprintln(buff, "# The color scheme to use. See `--list colorschemes`") fmt.Fprintf(buff, "%s=%s\n", colorscheme, c.Colorscheme.Name) + fmt.Fprintln(buff, "# How frequently to update the UI, in nanoseconds") fmt.Fprintf(buff, "%s=%d\n", updateinterval, c.UpdateInterval) + fmt.Fprintln(buff, "# If true, show the average CPU load") fmt.Fprintf(buff, "%s=%t\n", averagecpu, c.AverageLoad) + fmt.Fprintln(buff, "# If true, show load per CPU") fmt.Fprintf(buff, "%s=%t\n", percpuload, c.PercpuLoad) - fmt.Fprintf(buff, "%s=%d\n", tempscale, c.TempScale) + fmt.Fprintln(buff, "# Temperature units. C for Celcius, F for Fahrenheit") + fmt.Fprintf(buff, "%s=%c\n", tempscale, c.TempScale) + fmt.Fprintln(buff, "# If true, display a status bar") fmt.Fprintf(buff, "%s=%t\n", statusbar, c.Statusbar) + fmt.Fprintln(buff, "# The network interface to monitor") fmt.Fprintf(buff, "%s=%s\n", netinterface, c.NetInterface) + fmt.Fprintln(buff, "# A layout name. See `--list layouts`") fmt.Fprintf(buff, "%s=%s\n", layout, c.Layout) + fmt.Fprintln(buff, "# The maximum log file size, in bytes") fmt.Fprintf(buff, "%s=%d\n", maxlogsize, c.MaxLogSize) + fmt.Fprintln(buff, "# If set, export data as Promethius metrics on the interface:port.\n# E.g., `:8080` (colon is required, interface is not)") + if c.ExportPort == "" { + fmt.Fprint(buff, "#") + } fmt.Fprintf(buff, "%s=%s\n", export, c.ExportPort) + fmt.Fprintln(buff, "# Display network IO in mpbs if true") fmt.Fprintf(buff, "%s=%t\n", mbps, c.Mbps) + fmt.Fprintln(buff, "# A list of enabled temp sensors. See `--list devices`") + if len(c.Temps) == 0 { + fmt.Fprint(buff, "#") + } fmt.Fprintf(buff, "%s=%s\n", temperatures, strings.Join(c.Temps, ",")) return buff.Bytes() } diff --git a/devices/devices.go b/devices/devices.go index efdc404..4a029cd 100644 --- a/devices/devices.go +++ b/devices/devices.go @@ -3,12 +3,13 @@ package devices import "log" const ( - Temperatures = "Temperatures" + Temperatures = "Temperatures" // Device domain for temperature sensors ) var Domains []string = []string{Temperatures} -var shutdownFuncs []func() error +var _shutdownFuncs []func() error var _devs map[string][]string +var _defaults map[string][]string // RegisterShutdown stores a function to be called by gotop on exit, allowing // extensions to properly release resources. Extensions should register a @@ -16,14 +17,14 @@ var _devs map[string][]string // released. The returned error will be logged, but no other action will be // taken. func RegisterShutdown(f func() error) { - shutdownFuncs = append(shutdownFuncs, f) + _shutdownFuncs = append(_shutdownFuncs, f) } // Shutdown will be called by the `main()` function if gotop is exited // cleanly. It will call all of the registered shutdown functions of devices, // logging all errors but otherwise not responding to them. func Shutdown() { - for _, f := range shutdownFuncs { + for _, f := range _shutdownFuncs { err := f() if err != nil { log.Print(err) @@ -31,17 +32,31 @@ func Shutdown() { } } -func RegisterDeviceList(typ string, f func() []string) { +func RegisterDeviceList(typ string, all func() []string, def func() []string) { if _devs == nil { _devs = make(map[string][]string) } - if ls, ok := _devs[typ]; ok { - _devs[typ] = append(ls, f()...) - return + if _defaults == nil { + _defaults = make(map[string][]string) } - _devs[typ] = f() + if _, ok := _devs[typ]; !ok { + _devs[typ] = []string{} + } + _devs[typ] = append(_devs[typ], all()...) + if _, ok := _defaults[typ]; !ok { + _defaults[typ] = []string{} + } + _defaults[typ] = append(_defaults[typ], def()...) } -func Devices(domain string) []string { - return _devs[domain] +// Return a list of devices registered under domain, where `domain` is one of the +// defined constants in `devices`, e.g., devices.Temperatures. The +// `enabledOnly` flag determines whether all devices are returned (false), or +// only the ones that have been enabled for the domain. +func Devices(domain string, all bool) []string { + if all { + return _devs[domain] + } else { + return _defaults[domain] + } } diff --git a/devices/temp_darwin.go b/devices/temp_darwin.go index fe98fc2..37559d1 100644 --- a/devices/temp_darwin.go +++ b/devices/temp_darwin.go @@ -6,7 +6,7 @@ import smc "github.com/xxxserxxx/iSMC" func init() { RegisterTemp(update) - RegisterDeviceList(Temperatures, devs) + RegisterDeviceList(Temperatures, devs, defs) ts = make(map[string]float32) } @@ -25,8 +25,6 @@ func update(temps map[string]int) map[string]error { return nil } -// TODO: Set reasonable default devices -// CPU (TC[01]P), GPU (TG0P), Memory (Ts0S) and Disk (TH0P) func devs() []string { rv := make([]string, len(smc.AppleTemp)) for i, v := range smc.AppleTemp { @@ -34,3 +32,15 @@ func devs() []string { } return rv } + +func defs() []string { + // CPU 0 CPU 1 GPU Memory Disk + ids := map[string]bool{"TC0P": true, "TC1P": true, "TG0P": true, "Ts0S": true, "TH0P": true} + rv := make([]string, 0, len(ids)) + for _, v := range smc.AppleTemp { + if ids[v.Key] { + rv = append(rv, v.Desc) + } + } + return rv +} diff --git a/devices/temp_freebsd.go b/devices/temp_freebsd.go index a0277a6..1a2104b 100644 --- a/devices/temp_freebsd.go +++ b/devices/temp_freebsd.go @@ -12,7 +12,7 @@ import ( func init() { RegisterTemp(update) - RegisterDeviceList(Temperatures, devs) + RegisterDeviceList(Temperatures, devs, devs) } var sensorOIDS = map[string]string{ diff --git a/devices/temp_linux.go b/devices/temp_linux.go index 2ca01f5..09e7d3c 100644 --- a/devices/temp_linux.go +++ b/devices/temp_linux.go @@ -9,8 +9,9 @@ import ( ) func init() { + devs() // Populate the sensorMap RegisterTemp(getTemps) - RegisterDeviceList(Temperatures, devs) + RegisterDeviceList(Temperatures, devs, defs) } func getTemps(temps map[string]int) map[string]error { @@ -19,30 +20,45 @@ func getTemps(temps map[string]int) map[string]error { return map[string]error{"psHost": err} } for _, sensor := range sensors { - // removes '_input' from the end of the sensor name - idx := strings.Index(sensor.SensorKey, "_input") - if idx >= 0 { - label := sensor.SensorKey[:idx] - if _, ok := temps[label]; ok { - temps[label] = int(sensor.Temperature) - } + label := sensorMap[sensor.SensorKey] + if _, ok := temps[label]; ok { + temps[label] = int(sensor.Temperature) } } return nil } +// Optimization to avoid string manipulation every update +var sensorMap map[string]string + func devs() []string { + if sensorMap == nil { + sensorMap = make(map[string]string) + } sensors, err := psHost.SensorsTemperatures() if err != nil { return []string{} } rv := make([]string, 0, len(sensors)) for _, sensor := range sensors { - // only sensors with input in their name are giving us live temp info - if strings.Contains(sensor.SensorKey, "input") && sensor.Temperature != 0 { - // removes '_input' from the end of the sensor name - label := sensor.SensorKey[:strings.Index(sensor.SensorKey, "_input")] - rv = append(rv, label) + label := sensor.SensorKey + if strings.Contains(sensor.SensorKey, "input") { + label = sensor.SensorKey[:strings.Index(sensor.SensorKey, "_input")] + } + rv = append(rv, label) + sensorMap[sensor.SensorKey] = label + } + return rv +} + +// Only include sensors with input in their name; these are the only sensors +// returning live data +func defs() []string { + // MUST be called AFTER init() + rv := make([]string, 0) + for k, v := range sensorMap { + if k != v { // then it's an _input sensor + rv = append(rv, v) } } return rv diff --git a/devices/temp_windows.go b/devices/temp_windows.go index 54fe0eb..108b923 100644 --- a/devices/temp_windows.go +++ b/devices/temp_windows.go @@ -8,7 +8,7 @@ import ( func init() { RegisterTemp(update) - RegisterDeviceList(Temperatures, devs) + RegisterDeviceList(Temperatures, devs, devs) } func update(temps map[string]int) map[string]error { diff --git a/widgets/temp.go b/widgets/temp.go index 3e1b067..2db648f 100644 --- a/widgets/temp.go +++ b/widgets/temp.go @@ -46,7 +46,7 @@ func NewTempWidget(tempScale TempScale, filter []string) *TempWidget { self.Data[t] = 0 } } else { - for _, t := range devices.Devices(devices.Temperatures) { + for _, t := range devices.Devices(devices.Temperatures, false) { self.Data[t] = 0 } } -- cgit v1.2.3