From aa496ba38d7c51e2e0063ef2bc72c8e32fe4a711 Mon Sep 17 00:00:00 2001 From: Kraust Date: Sun, 14 Jul 2019 09:46:07 -0400 Subject: [PATCH 1/4] FreeBSD Fixes - Added NVIDIA GPU Temperature Support - Fixed a bug where CPU temp would not be displayed if a syctl option was not found - Fixed a bug where Swap was not being displayed on mirroed ZFS partitions. --- src/widgets/mem_freebsd.go | 5 ++++- src/widgets/temp_freebsd.go | 44 ++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/widgets/mem_freebsd.go b/src/widgets/mem_freebsd.go index b3773c9e..f11c9ab1 100644 --- a/src/widgets/mem_freebsd.go +++ b/src/widgets/mem_freebsd.go @@ -42,7 +42,10 @@ func gatherSwapInfo() (MemoryInfo, error) { } } - ss := strings.Split(strings.TrimSuffix(string(output), "\n"), " ") + s := strings.TrimSuffix(string(output), "\n") + s = strings.ReplaceAll(s, "\n", " ") + ss := strings.Split(s, " ") + ss = ss[((len(ss)/3)-1)*3:] return convert(ss) } diff --git a/src/widgets/temp_freebsd.go b/src/widgets/temp_freebsd.go index 29901236..714e6749 100644 --- a/src/widgets/temp_freebsd.go +++ b/src/widgets/temp_freebsd.go @@ -35,22 +35,60 @@ func refineOutput(output []byte) (float64, error) { return value, nil } +func collectNvidiaGPUSensors() []sensorMeasurement { + var measurements []sensorMeasurement + + _, err := exec.Command("sysctl", "-n", "hw.nvidia.gpus.0.model").Output() + if err == nil { + output, err := exec.Command("nvidia-settings", "-q", "gpucoretemp", "-t").Output() + if err != nil { + log.Printf("Failed to get nvidia gpu temperature from nvidia-settings") + } else { + s := strings.TrimSuffix(string(output), "\n") + s = strings.ReplaceAll(s, "\n", " ") + ss := strings.Split(s, " ") + for i := 1; i < len(ss); i++ { + value, err := refineOutput([]byte(ss[i])) + if err != nil { + log.Printf("Failed to parse nvidia-settings output") + } + label := fmt.Sprintf("Nvidia GPU %d", i-1) + measurements = append(measurements, sensorMeasurement{label, value}) + } + } + } + + return measurements +} + +func collectGPUSensors() []sensorMeasurement { + var measurements []sensorMeasurement + + m := collectNvidiaGPUSensors() + measurements = append(measurements, m...) + + return measurements +} + func collectSensors() ([]sensorMeasurement, error) { var measurements []sensorMeasurement for k, v := range sensorOIDS { output, err := exec.Command("sysctl", "-n", k).Output() if err != nil { - return nil, fmt.Errorf("failed to execute 'sysctl' command: %v", err) + continue } value, err := refineOutput(output) if err != nil { - return nil, fmt.Errorf("failed to execute 'sysctl' command: %v", err) + continue } measurements = append(measurements, sensorMeasurement{v, value}) - } + + m := collectGPUSensors() + measurements = append(measurements, m...) + return measurements, nil } From f58d1e256a0781bcb1dc817b6650452873e06fbe Mon Sep 17 00:00:00 2001 From: Kraust Date: Sun, 14 Jul 2019 09:59:18 -0400 Subject: [PATCH 2/4] Removed nvidia temps as another PR handles this. --- src/widgets/temp_freebsd.go | 39 ------------------------------------- 1 file changed, 39 deletions(-) diff --git a/src/widgets/temp_freebsd.go b/src/widgets/temp_freebsd.go index 714e6749..22dc4ca0 100644 --- a/src/widgets/temp_freebsd.go +++ b/src/widgets/temp_freebsd.go @@ -1,7 +1,6 @@ package widgets import ( - "fmt" "log" "os/exec" "strconv" @@ -35,41 +34,6 @@ func refineOutput(output []byte) (float64, error) { return value, nil } -func collectNvidiaGPUSensors() []sensorMeasurement { - var measurements []sensorMeasurement - - _, err := exec.Command("sysctl", "-n", "hw.nvidia.gpus.0.model").Output() - if err == nil { - output, err := exec.Command("nvidia-settings", "-q", "gpucoretemp", "-t").Output() - if err != nil { - log.Printf("Failed to get nvidia gpu temperature from nvidia-settings") - } else { - s := strings.TrimSuffix(string(output), "\n") - s = strings.ReplaceAll(s, "\n", " ") - ss := strings.Split(s, " ") - for i := 1; i < len(ss); i++ { - value, err := refineOutput([]byte(ss[i])) - if err != nil { - log.Printf("Failed to parse nvidia-settings output") - } - label := fmt.Sprintf("Nvidia GPU %d", i-1) - measurements = append(measurements, sensorMeasurement{label, value}) - } - } - } - - return measurements -} - -func collectGPUSensors() []sensorMeasurement { - var measurements []sensorMeasurement - - m := collectNvidiaGPUSensors() - measurements = append(measurements, m...) - - return measurements -} - func collectSensors() ([]sensorMeasurement, error) { var measurements []sensorMeasurement for k, v := range sensorOIDS { @@ -86,9 +50,6 @@ func collectSensors() ([]sensorMeasurement, error) { measurements = append(measurements, sensorMeasurement{v, value}) } - m := collectGPUSensors() - measurements = append(measurements, m...) - return measurements, nil } From 7b2f16a48c25f6ba0a9c8402500e87761659c655 Mon Sep 17 00:00:00 2001 From: Kraust Date: Sun, 14 Jul 2019 10:23:42 -0400 Subject: [PATCH 3/4] Added FreeBSD Nvidia GPU support using nvidia-smi - Also refactored the temp_freebsd code to be modular. --- src/widgets/temp_freebsd.go | 66 +++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/src/widgets/temp_freebsd.go b/src/widgets/temp_freebsd.go index 22dc4ca0..01f89dd1 100644 --- a/src/widgets/temp_freebsd.go +++ b/src/widgets/temp_freebsd.go @@ -1,12 +1,12 @@ package widgets import ( - "log" "os/exec" "strconv" "strings" "github.com/cjbassi/gotop/src/utils" + "github.com/rai-project/nvidia-smi" ) var sensorOIDS = map[string]string{ @@ -34,7 +34,7 @@ func refineOutput(output []byte) (float64, error) { return value, nil } -func collectSensors() ([]sensorMeasurement, error) { +func collectSysctlSensors() []sensorMeasurement { var measurements []sensorMeasurement for k, v := range sensorOIDS { output, err := exec.Command("sysctl", "-n", k).Output() @@ -50,15 +50,67 @@ func collectSensors() ([]sensorMeasurement, error) { measurements = append(measurements, sensorMeasurement{v, value}) } - return measurements, nil + return measurements +} + +func collectNvidiaSensors() []sensorMeasurement { + var measurements []sensorMeasurement + info, _ := nvidiasmi.New() + if info.HasGPU() { + for i := range info.GPUS { + gpu := info.GPUS[i] + var s sensorMeasurement + s.name = strings.ReplaceAll(strings.ToLower(gpu.ProductName), " ", "_") + "_" + strconv.Itoa(i) + "_input" + s.temperature, _ = strconv.ParseFloat(strings.ReplaceAll(gpu.GpuTemp, " C", ""), 10) + measurements = append(measurements, s) + } + } + + return measurements } -func (self *TempWidget) update() { - sensors, err := collectSensors() - if err != nil { - log.Printf("error recieved from gopsutil: %v", err) +func collectAMDGPUSensors() []sensorMeasurement { + var measurments []sensorMeasurement + + return measurments +} + +func collectGPUSensors() []sensorMeasurement { + var measurements []sensorMeasurement + + measurements = append(measurements, collectSysctlSensors()...) + measurements = append(measurements, collectNvidiaSensors()...) + measurements = append(measurements, collectAMDGPUSensors()...) + + return measurements +} + +func collectSensors() []sensorMeasurement { + var measurements []sensorMeasurement + for k, v := range sensorOIDS { + output, err := exec.Command("sysctl", "-n", k).Output() + if err != nil { + continue + } + + value, err := refineOutput(output) + if err != nil { + continue + } + + measurements = append(measurements, sensorMeasurement{v, value}) } + + measurements = append(measurements, collectGPUSensors()...) + + return measurements + +} + +func (self *TempWidget) update() { + sensors := collectSensors() + for _, sensor := range sensors { switch self.TempScale { case Fahrenheit: From 42e65038b0cc36b81bb8c6308ff5e7c8014d5d85 Mon Sep 17 00:00:00 2001 From: Kraust Date: Sun, 14 Jul 2019 10:44:03 -0400 Subject: [PATCH 4/4] Changed GPU naming scheme --- src/widgets/temp_freebsd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/widgets/temp_freebsd.go b/src/widgets/temp_freebsd.go index 01f89dd1..3320873c 100644 --- a/src/widgets/temp_freebsd.go +++ b/src/widgets/temp_freebsd.go @@ -61,7 +61,7 @@ func collectNvidiaSensors() []sensorMeasurement { for i := range info.GPUS { gpu := info.GPUS[i] var s sensorMeasurement - s.name = strings.ReplaceAll(strings.ToLower(gpu.ProductName), " ", "_") + "_" + strconv.Itoa(i) + "_input" + s.name = gpu.ProductName + " " + strconv.Itoa(i) s.temperature, _ = strconv.ParseFloat(strings.ReplaceAll(gpu.GpuTemp, " C", ""), 10) measurements = append(measurements, s) }