From 7b2f16a48c25f6ba0a9c8402500e87761659c655 Mon Sep 17 00:00:00 2001 From: Kraust Date: Sun, 14 Jul 2019 10:23:42 -0400 Subject: [PATCH] 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 22dc4ca..01f89dd 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 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, err := collectSensors() - if err != nil { - log.Printf("error recieved from gopsutil: %v", err) - } + sensors := collectSensors() + for _, sensor := range sensors { switch self.TempScale { case Fahrenheit: