You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,11 +9,13 @@ All notable changes to this project will be documented in this file.
9
9
- Optional average smoothing in `CpuUsageSample` via `smoothedAverage`, with configurable `smoothingMode` (`None`, `RollingMean`, `Ewma`), rolling window size, and EWMA alpha.
10
10
-`getLastSmoothedAverage()` helper for quickly reading the latest trend/baseline value when smoothing is enabled.
11
11
-`CpuMonitorConfig::usePSRAMBuffers` toggle to prefer PSRAM-backed internal history and callback container/snapshot storage through `ESPBufferManager`, with automatic fallback to normal heap.
12
+
- Token-based measurement API via `startMeasure()` / `stopMeasure(token)` with precise timing fields (`durationUs`, `durationMs`, `durationSec`) and optional per-window CPU usage in `CpuMeasure`.
12
13
13
14
### Changed
14
15
- Removed the library-provided global `cpuMonitor`; create and manage your own `ESPCpuMonitor` instance (only one active monitor at a time) and updated examples/docs accordingly.
15
16
- Made `ESPCpuMonitor` non-copyable/movable to prevent accidental double-free of FreeRTOS handles.
16
17
-`toJson()` now exports `avgSmoothed` when smoothing is enabled and a smoothed sample is available.
18
+
- Baseline calibration now tracks idle rates per microsecond so code-path measurements can estimate CPU usage across arbitrary measurement durations.
Copy file name to clipboardExpand all lines: README.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ ESPCpuMonitor is a tiny C++17 helper that estimates per-core CPU usage on ESP32
14
14
- Optional smoothing helpers for average CPU usage (`RollingMean` or `EWMA`) so you can track baseline/trend load without forcing smoothing globally (disabled by default, fixed-size buffer when enabled).
15
15
- Thread-safe per-sample callbacks for logging, telemetry, or UI updates; swap `enablePerCore` off to collapse cores to an overall average.
16
16
- Manual `sampleNow()` path for users who already have their own schedulers (set `sampleIntervalMs` to `0`).
17
+
- Token-based manual measurement API (`startMeasure()` / `stopMeasure(token)`) with precise microsecond durations and optional per-window CPU usage.
17
18
- Optional CPU temperature readings (current + running average) using the ESP-IDF temperature sensor driver, gracefully disabled when unsupported.
18
19
- Optional ArduinoJson export helper to stream samples over HTTP/MQTT/WebSockets alongside the rest of ESPToolKit.
19
20
@@ -75,6 +76,25 @@ When you set `sampleIntervalMs` to `0`, call `sampleNow()` on your monitor insta
75
76
If temperature is enabled, `getLastTemperature(current, average)` returns the latest reading and running mean (returns `false` when unsupported or not ready).
76
77
When your feature shuts down (task exit, OTA handoff, mode switch), call `cpuMonitor.deinit()` to release idle hooks and timer resources.
CpuMeasure result = cpuMonitor.stopMeasure(token);
85
+
86
+
if (result.valid) {
87
+
// precise timings
88
+
double ms = result.durationMs;
89
+
double sec = result.durationSec;
90
+
91
+
// CPU usage for this window (available after calibration)
92
+
if (result.hasCpuData) {
93
+
float avg = result.averageUsage;
94
+
}
95
+
}
96
+
```
97
+
78
98
## Gotchas
79
99
- CPU usage numbers are floats in percent so you can see tiny changes; `1.0` means ~1% busy, not 100%. Feel free to round (`%.0f%%`) in your logs/UI if you prefer whole numbers.
80
100
- Allow the calibration window (`calibrationSamples`) to finish before trusting numbers; keep the device as idle as possible during that phase.
@@ -91,6 +111,7 @@ When your feature shuts down (task exit, OTA handoff, mode switch), call `cpuMon
91
111
-`bool getLastSample(CpuUsageSample &out) const` / `float getLastAverage() const` / `float getLastSmoothedAverage() const` – read the latest sample; average/smoothed values return `-1.0f` until ready (or when smoothing is disabled).
92
112
-`bool getLastTemperature(float ¤tC, float &averageC) const` – latest temperature and running average; returns `false` if disabled, unsupported, or not yet sampled.
93
113
-`std::vector<CpuUsageSample> history() const` – copy of the ring buffer (size capped by `historySize`, `0` disables storage).
114
+
-`CpuMeasureToken startMeasure() const` / `CpuMeasure stopMeasure(const CpuMeasureToken &token) const` – token-based code-path timing using `esp_timer_get_time()`. Requires `init()`. `stopMeasure()` returns `valid=false` for invalid/stale tokens. `hasCpuData` becomes true once baseline calibration is available.
94
115
-`bool sampleNow(CpuUsageSample &out)` – immediate sampling, useful when periodic timer is disabled.
95
116
-`void onSample(CpuSampleCallback cb)` – subscribe to every stored sample.
96
117
-`void toJson(const CpuUsageSample&, JsonDocument &doc)` – ArduinoJson export helper (compiled only when ArduinoJson is available).
@@ -107,6 +128,7 @@ When your feature shuts down (task exit, OTA handoff, mode switch), call `cpuMon
107
128
-`smoothingAlpha` (default `0.2`) – EWMA alpha (used when `smoothingMode = Ewma`, clamped to `(0.0, 1.0]`).
108
129
109
130
`CpuUsageSample` contains `timestampUs`, `perCore[portNUM_PROCESSORS]`, `average` (%), `smoothedAverage` (%/NaN when smoothing is disabled), `temperatureC`, and `temperatureAvgC` (NaN when unsupported).
0 commit comments