Skip to content

Commit

Permalink
perf(linux/hwmon): ⚡ rework hwmon sensors
Browse files Browse the repository at this point in the history
- remove conc.Pool usage in favour of channels and sync.Waitgroups
- split large functions
- handle errors more gracefully
  • Loading branch information
joshuar committed Sep 8, 2024
1 parent 8d208b4 commit 0164429
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 271 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/pelletier/go-toml/v2 v2.2.3
github.com/robfig/cron/v3 v3.0.1
github.com/shirou/gopsutil/v3 v3.24.5
github.com/sourcegraph/conc v0.3.0
github.com/stretchr/testify v1.9.0
github.com/yassinebenaid/godump v0.10.0
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
Expand Down Expand Up @@ -93,7 +92,6 @@ require (
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
gitlab.com/digitalxero/go-conventional-commit v1.0.7 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/sync v0.8.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,6 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
Expand Down Expand Up @@ -562,8 +560,6 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
12 changes: 5 additions & 7 deletions internal/linux/system/hwmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ type hwSensor struct {
linux.Sensor
}

//nolint:errcheck
func (s *hwSensor) asBool(details *hwmon.Sensor) {
s.Value, _ = details.Value()
s.Value = details.Value()
if v, ok := s.Value.(bool); ok && v {
s.IconString = "mdi:alarm-light"
} else {
Expand All @@ -45,11 +44,10 @@ func (s *hwSensor) asBool(details *hwmon.Sensor) {
s.IsBinary = true
}

//nolint:errcheck
func (s *hwSensor) asFloat(details *hwmon.Sensor) {
s.Value, _ = details.Value()
s.Value = details.Value()
s.UnitsString = details.Units()
i, d := parseSensorType(details.SensorType.String())
i, d := parseSensorType(details.MonitorType.String())
s.IconString = i
s.DeviceClassValue = d
s.StateClassValue = types.StateClassMeasurement
Expand Down Expand Up @@ -77,8 +75,8 @@ func (s *hwSensor) Attributes() map[string]any {

func newHWSensor(details *hwmon.Sensor) *hwSensor {
newSensor := &hwSensor{
hwType: details.SensorType.String(),
path: details.SysFSPath,
hwType: details.MonitorType.String(),
path: details.HWMonPath,
ExtraAttrs: make(map[string]float64),
Sensor: linux.Sensor{
DisplayName: details.Name(),
Expand Down
2 changes: 2 additions & 0 deletions pkg/linux/hwmon/examples/getAllSensors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

//nolint:cyclop,govet,wsl
func main() {
slog.SetLogLoggerLevel(slog.LevelDebug)

cpu, err := os.Create("cpu.prof")
if err != nil {
slog.Warn("Cannot create CPU profile.", "error", err.Error())
Expand Down
Loading

0 comments on commit 0164429

Please sign in to comment.