Added FreeBSD Nvidia GPU support using nvidia-smi

- Also refactored the temp_freebsd code to be modular.
This commit is contained in:
Kraust 2019-07-14 10:23:42 -04:00
parent f58d1e256a
commit 7b2f16a48c

View File

@ -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: