code cleanup
This commit is contained in:
@@ -22,6 +22,7 @@ type PowerDriver interface {
|
|||||||
|
|
||||||
type ClockSensor struct {
|
type ClockSensor struct {
|
||||||
path string
|
path string
|
||||||
|
number int
|
||||||
Name string
|
Name string
|
||||||
FrequencyGHz float64
|
FrequencyGHz float64
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-37
@@ -2,7 +2,6 @@ package drivers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -10,6 +9,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const milliJoulePerJoule = float64(1000000)
|
||||||
|
|
||||||
type EnergyState struct {
|
type EnergyState struct {
|
||||||
lastEnergy uint64
|
lastEnergy uint64
|
||||||
lastTime time.Time
|
lastTime time.Time
|
||||||
@@ -72,19 +73,9 @@ func getRaplLabel(inputPath string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *RaplDriver) calculateEnergyDelta(inputs []string, out *[]PowerSensor) {
|
func (r *RaplDriver) calculateEnergyDelta(inputs []string, out *[]PowerSensor) {
|
||||||
now := time.Now()
|
for _, path := range inputs {
|
||||||
|
now := time.Now()
|
||||||
if r.energyState == nil {
|
valBytes, err := os.ReadFile(path)
|
||||||
r.energyState = make(map[string]EnergyState)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, rawPath := range inputs {
|
|
||||||
key, err := filepath.EvalSymlinks(rawPath)
|
|
||||||
if err != nil {
|
|
||||||
key = filepath.Clean(rawPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
valBytes, err := os.ReadFile(key)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -94,49 +85,38 @@ func (r *RaplDriver) calculateEnergyDelta(inputs []string, out *[]PowerSensor) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
prevState, exists := r.energyState[key]
|
sensorLabel := getRaplLabel(path)
|
||||||
|
|
||||||
|
prevState, exists := r.energyState[path]
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
r.energyState[key] = EnergyState{
|
|
||||||
|
r.energyState[path] = EnergyState{
|
||||||
lastEnergy: microJoules,
|
lastEnergy: microJoules,
|
||||||
lastTime: now,
|
lastTime: now,
|
||||||
}
|
}
|
||||||
*out = append(*out, PowerSensor{
|
*out = append(*out, PowerSensor{
|
||||||
Name: getRaplLabel(key),
|
Name: sensorLabel,
|
||||||
Watts: 0.0,
|
Watts: 0.0,
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
timeDiff := now.Sub(prevState.lastTime).Seconds()
|
timeDiff := now.Sub(prevState.lastTime).Seconds()
|
||||||
if timeDiff < 0.05 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
maxRange := uint64(math.MaxUint64)
|
// technically the energy counter could overflow, at ~18 TerraJoules
|
||||||
rangeBytes, err := os.ReadFile(filepath.Join(filepath.Dir(key), "max_energy_range_uj"))
|
// the value will be off for that one measurement, then become correct again
|
||||||
if err == nil {
|
energyDiff := float64(microJoules - prevState.lastEnergy)
|
||||||
if parsedMax, err := strconv.ParseUint(strings.TrimSpace(string(rangeBytes)), 10, 64); err == nil && parsedMax > 0 {
|
|
||||||
maxRange = parsedMax
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var energyDiff uint64
|
watts := (energyDiff / milliJoulePerJoule) / timeDiff
|
||||||
if microJoules >= prevState.lastEnergy {
|
|
||||||
energyDiff = microJoules - prevState.lastEnergy
|
|
||||||
} else {
|
|
||||||
energyDiff = (maxRange - prevState.lastEnergy) + microJoules
|
|
||||||
}
|
|
||||||
|
|
||||||
watts := (float64(energyDiff) / 1_000_000.0) / timeDiff
|
r.energyState[path] = EnergyState{
|
||||||
|
|
||||||
r.energyState[key] = EnergyState{
|
|
||||||
lastEnergy: microJoules,
|
lastEnergy: microJoules,
|
||||||
lastTime: now,
|
lastTime: now,
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = append(*out, PowerSensor{
|
*out = append(*out, PowerSensor{
|
||||||
Name: getRaplLabel(key),
|
Name: sensorLabel,
|
||||||
Watts: watts,
|
Watts: watts,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ func (r *SysDriver) ReadClockSensors() ([]ClockSensor, error) {
|
|||||||
|
|
||||||
clockSensors = append(clockSensors, ClockSensor{
|
clockSensors = append(clockSensors, ClockSensor{
|
||||||
path: path,
|
path: path,
|
||||||
|
number: coreID,
|
||||||
Name: fmt.Sprintf("core %d", coreID),
|
Name: fmt.Sprintf("core %d", coreID),
|
||||||
FrequencyGHz: khz / 1000000.0, // Convert KHz to GHz
|
FrequencyGHz: khz / 1000000.0, // Convert KHz to GHz
|
||||||
})
|
})
|
||||||
@@ -60,7 +61,7 @@ func (r *SysDriver) ReadClockSensors() ([]ClockSensor, error) {
|
|||||||
|
|
||||||
// Guarantee numerical order by core ID
|
// Guarantee numerical order by core ID
|
||||||
sort.Slice(clockSensors, func(i, j int) bool {
|
sort.Slice(clockSensors, func(i, j int) bool {
|
||||||
return clockSensors[i].Name < clockSensors[j].Name
|
return clockSensors[i].number < clockSensors[j].number
|
||||||
})
|
})
|
||||||
|
|
||||||
return clockSensors, nil
|
return clockSensors, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user