贡献 / #1925
#1925 Add optional GPU electricity cost display to Power card
open · @hash13 · 0 评论 · 去 GitHub 看
说明
Minimal version without electricity pricing: https://github.com/Niko1221/Strata/pull/1876
# Add opt-in GPU energy and electricity cost monitoring to the Power card
## Summary
This PR adds opt-in GPU energy monitoring and optional electricity cost calculations to the existing Power card, based on Strata v0.1.42.
The implementation reuses Strata's existing GPU telemetry, multi-GPU support, and Monitor refresh infrastructure. No additional services, dependencies, or persistent storage are required.
The changes are intentionally limited to cumulative GPU energy consumption and total electricity costs.
There is no request-level energy attribution, token cost calculation, or additional details dialog.
## Screenshot
<img width="353" height="212" alt="grafik" src="https://github.com/user-attachments/assets/efc6d451-e2b2-47f9-ad10-faaec18ab68f" />
## Configuration
GPU energy monitoring is enabled by adding an `electricity` block at the top level of the existing Strata JSON configuration.
**Energy monitoring only:**
```json
"electricity": {}
```
An empty object is sufficient to enable cumulative energy monitoring.
**Energy monitoring with electricity costs:**
```json
"electricity": {
"price_per_kwh": 0.32,
"currency": "EUR"
}
```
- `price_per_kwh`: Electricity price per kWh in the configured currency (e.g. `0.32` = €0.32/kWh).
- `currency`: Currency code used for displaying electricity costs (defaults to `EUR`).
- Both settings are optional. Without a valid price, only energy consumption is displayed.
Without an `electricity` block, GPU energy monitoring is disabled and the existing Power card remains unchanged.
## Power Card
The existing Power card continues to display:
- Current GPU power consumption (W)
- NVIDIA power limit (W)
- Power history sparkline
When electricity pricing is configured, the card uses a compact two-column layout:
| Left | Right |
|---|---|
| Current GPU power (W) | Accumulated electricity cost |
| NVIDIA power limit (W) | Accumulated GPU energy (Wh/kWh) |
**Example:**
| Power | Electricity cost |
|---|---|
| **278 W** | **0.155 €** |
| of 520 W limit | 483.0 Wh |
The layout reuses the existing Speed card styling, without introducing additional CSS or increasing the card height.
When only energy monitoring is enabled, cumulative energy consumption is displayed alongside the existing power limit.
All values update automatically through the existing Monitor refresh cycle.
### Value Formatting
**Energy consumption:**
| Consumption | Display |
|---|---|
| Below 1 kWh | Wh, one decimal place |
| From 1 kWh | kWh, three decimal places |
**Electricity costs:**
| Cost | Display |
|---|---|
| Positive values below 0.001 | < 0.001 € |
| Below 10 | Three decimal places |
| From 10 | Two decimal places |
The currency symbol is displayed in a smaller font, matching the existing unit styling.
Number formatting follows the browser's locale.
## Energy Measurement
The implementation prefers NVIDIA's cumulative NVML hardware energy counter (`nvmlDeviceGetTotalEnergyConsumption`).
This provides accumulated energy directly in millijoules and avoids estimating consumption solely from periodically sampled power values.
If the hardware counter is unavailable, energy is estimated by integrating the existing GPU power samples over time.
For multi-GPU configurations, energy consumption is aggregated across the monitored GPUs.
Energy accumulation starts with the telemetry sampler and resets when Strata restarts.
Electricity costs are calculated from accumulated energy:
**Cost = GPU energy (kWh) × configured price per kWh**
No additional NVML reads are required for the cost calculation.
Measurements cover GPU energy only. CPU, RAM, motherboard, storage, fans, and PSU losses are not included.
### Operating Modes
| Configuration | Power Card Display |
|---|---|
| No `electricity` block | Current GPU power (W) and power limit only |
| `"electricity": {}` | GPU power, power limit, and cumulative energy consumption (Wh/kWh) |
| `electricity` with `price_per_kwh` | Two-column layout: GPU power and power limit on the left; total electricity cost and cumulative energy consumption on the right |
### Configuration Examples
**Energy monitoring only:**
```json
"electricity": {}
```
**Energy monitoring with electricity costs:**
```json
"electricity": {
"price_per_kwh": 0.32,
"currency": "EUR"
}
```
- `price_per_kwh`: Required to calculate electricity costs.
- `currency`: Optional, defaults to `EUR`. Explicit configuration is recommended.
- Without a valid electricity price, only cumulative GPU energy consumption is displayed.
- Without an `electricity` block, energy monitoring remains disabled.
Electricity costs are calculated as:
**Cost = accumulated GPU energy (kWh) × price_per_kwh**
## Scope
Cumulative energy consumption is exposed through the existing `/metrics` hardware data. The optional electricity price and currency are provided through the existing static telemetry data.
Request-level energy attribution is deferred to a later PR, initially limited to serial requests to avoid double counting energy during overlapping requests.
## Changed Files
The complete implementation, including the optional electricity cost extension, affects three files:
| File | Changes |
|---|---|
| `serve/telemetry.py` | Cumulative GPU energy measurement, NVML fallback, and optional electricity price configuration |
| `serve/server.py` | Pass the optional `electricity` configuration to telemetry |
| `serve/web/app.js` | Display cumulative GPU energy and optional electricity costs in the Power card |
The electricity cost extension itself only modifies `serve/telemetry.py` and `serve/web/app.js`.
本站相关内容
相关页面的快捷入口。