Skip to content

Commit

Permalink
feat(linux/cpu): ✨ add preferences to optionally disable all cpu (and…
Browse files Browse the repository at this point in the history
… specifically, cpu frequency) sensors
  • Loading branch information
joshuar committed Nov 4, 2024
1 parent b17deb2 commit ecc5cc6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"README",
"linux/net",
"linux/media",
"agent/sensor"
"agent/sensor",
"linux/cpu"
],
"go.testFlags": ["-v"],
"[markdown]": {
Expand Down
15 changes: 15 additions & 0 deletions internal/linux/cpu/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Joshua Rich <[email protected]>.
// SPDX-License-Identifier: MIT

package cpu

import "github.com/joshuar/go-hass-agent/internal/preferences"

const (
preferencesID = "cpu_sensors"
)

type WorkerPrefs struct {
DisableCPUFreq bool `toml:"disable_cpufreq" comment:"Set to true to disable CPU frequency sensors."`
preferences.CommonWorkerPrefs
}
37 changes: 31 additions & 6 deletions internal/linux/cpu/usageWorker.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// Copyright 2024 Joshua Rich <[email protected]>.
// SPDX-License-Identifier: MIT

//revive:disable:unused-receiver
package cpu

import (
Expand All @@ -19,6 +18,7 @@ import (
"github.com/joshuar/go-hass-agent/internal/hass/sensor"
"github.com/joshuar/go-hass-agent/internal/hass/sensor/types"
"github.com/joshuar/go-hass-agent/internal/linux"
"github.com/joshuar/go-hass-agent/internal/preferences"
)

const (
Expand All @@ -39,6 +39,7 @@ type usageWorker struct {
linux.PollingSensorWorker
clktck int64
delta time.Duration
prefs WorkerPrefs
}

func (w *usageWorker) UpdateDelta(delta time.Duration) {
Expand All @@ -49,7 +50,17 @@ func (w *usageWorker) Sensors(_ context.Context) ([]sensor.Entity, error) {
return w.getUsageStats()
}

func (w *usageWorker) PreferencesID() string {
return preferencesID
}

func (w *usageWorker) DefaultPreferences() WorkerPrefs {
return WorkerPrefs{}
}

func NewUsageWorker(ctx context.Context) (*linux.PollingSensorWorker, error) {
var err error

worker := linux.NewPollingSensorWorker(usageWorkerID, usageUpdateInterval, usageUpdateJitter)

clktck, found := linux.CtxGetClkTck(ctx)
Expand All @@ -62,7 +73,7 @@ func NewUsageWorker(ctx context.Context) (*linux.PollingSensorWorker, error) {
return worker, fmt.Errorf("%w: no boottime value", linux.ErrInvalidCtx)
}

worker.PollingSensorType = &usageWorker{
cpuUsageWorker := &usageWorker{
path: filepath.Join(linux.ProcFSRoot, "stat"),
boottime: boottime,
clktck: clktck,
Expand All @@ -72,6 +83,18 @@ func NewUsageWorker(ctx context.Context) (*linux.PollingSensorWorker, error) {
},
}

cpuUsageWorker.prefs, err = preferences.LoadWorkerPreferences(ctx, cpuUsageWorker)
if err != nil {
return worker, fmt.Errorf("could not load preferences: %w", err)
}

// If disabled, don't use the addressWorker.
if cpuUsageWorker.prefs.Disabled {
return worker, nil
}

worker.PollingSensorType = cpuUsageWorker

return worker, nil
}

Expand Down Expand Up @@ -105,7 +128,9 @@ func (w *usageWorker) getUsageStats() ([]sensor.Entity, error) {
sensors = append(sensors, newUsageSensor(w.clktck, cols, types.CategoryDefault))
case strings.Contains(cols[0], "cpu"):
sensors = append(sensors, newUsageSensor(w.clktck, cols, types.CategoryDiagnostic))
sensors = append(sensors, newCPUFreqSensor(cols[0]))
if !w.prefs.DisableCPUFreq {
sensors = append(sensors, newCPUFreqSensor(cols[0]))
}
case cols[0] == "ctxt":
if _, found := w.rateSensors["ctxt"]; found {
w.rateSensors["ctxt"].update(w.delta, cols[1])
Expand Down

0 comments on commit ecc5cc6

Please sign in to comment.