diff --git a/internal/linux/mem/memUsage.go b/internal/linux/mem/memUsage.go index b7f64db68..de755192c 100644 --- a/internal/linux/mem/memUsage.go +++ b/internal/linux/mem/memUsage.go @@ -11,6 +11,7 @@ import ( "github.com/rs/zerolog/log" "github.com/shirou/gopsutil/v3/mem" + "github.com/sourcegraph/conc/pool" "github.com/joshuar/go-hass-agent/internal/device/helpers" "github.com/joshuar/go-hass-agent/internal/hass/sensor" @@ -18,16 +19,6 @@ import ( "github.com/joshuar/go-hass-agent/internal/linux" ) -var stats = []linux.SensorTypeValue{ - linux.SensorMemTotal, - linux.SensorMemAvail, - linux.SensorMemUsed, - linux.SensorMemPc, - linux.SensorSwapTotal, - linux.SensorSwapFree, - linux.SensorSwapPc, -} - type memorySensor struct { linux.Sensor } @@ -43,7 +34,7 @@ func (s *memorySensor) Attributes() any { } func Updater(ctx context.Context) chan sensor.Details { - sensorCh := make(chan sensor.Details, 5) + sensorCh := make(chan sensor.Details) sendMemStats := func(_ time.Duration) { var memDetails *mem.VirtualMemoryStat var err error @@ -52,21 +43,43 @@ func Updater(ctx context.Context) chan sensor.Details { Msg("Problem fetching memory stats.") return } + + // Memory stats to track as sensors. + stats := []linux.SensorTypeValue{ + linux.SensorMemTotal, + linux.SensorMemAvail, + linux.SensorMemUsed, + linux.SensorMemPc, + } + // If this system has swap enabled, track swap stats as sensors as well. + if memDetails.SwapTotal > 0 { + stats = append(stats, + linux.SensorSwapTotal, + linux.SensorSwapFree, + linux.SensorSwapPc, + ) + } + + p := pool.New() + for _, stat := range stats { - value, unit, deviceClass, stateClass := parseSensorType(stat, memDetails) - state := &memorySensor{ - linux.Sensor{ - Value: value, - SensorTypeValue: stat, - IconString: "mdi:memory", - UnitsString: unit, - SensorSrc: linux.DataSrcProcfs, - DeviceClassValue: deviceClass, - StateClassValue: stateClass, - }, - } - sensorCh <- state + p.Go(func() { + value, unit, deviceClass, stateClass := parseSensorType(stat, memDetails) + state := &memorySensor{ + linux.Sensor{ + Value: value, + SensorTypeValue: stat, + IconString: "mdi:memory", + UnitsString: unit, + SensorSrc: linux.DataSrcProcfs, + DeviceClassValue: deviceClass, + StateClassValue: stateClass, + }, + } + sensorCh <- state + }) } + p.Wait() } go helpers.PollSensors(ctx, sendMemStats, time.Minute, time.Second*5)