-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linux/cpu): ✨ add preferences to optionally disable all cpu (and…
… specifically, cpu frequency) sensors
- Loading branch information
Showing
3 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | ||
|
@@ -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 ( | ||
|
@@ -39,6 +39,7 @@ type usageWorker struct { | |
linux.PollingSensorWorker | ||
clktck int64 | ||
delta time.Duration | ||
prefs WorkerPrefs | ||
} | ||
|
||
func (w *usageWorker) UpdateDelta(delta time.Duration) { | ||
|
@@ -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) | ||
|
@@ -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, | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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]) | ||
|