Skip to content

Commit f25d350

Browse files
committed
no-throw/thread-safe baseline
1 parent 5a1b38d commit f25d350

5 files changed

Lines changed: 41 additions & 17 deletions

File tree

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,22 @@ on:
88
workflow_dispatch:
99

1010
jobs:
11+
source-audit:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Audit production sources
18+
run: |
19+
if rg -n '\bthrow\b|std::abort\(' src; then
20+
echo "Embedded safety audit failed"
21+
exit 1
22+
fi
23+
1124
build-examples:
1225
runs-on: ubuntu-latest
26+
needs: source-audit
1327
strategy:
1428
fail-fast: false
1529
matrix:
@@ -56,6 +70,7 @@ jobs:
5670
5771
arduino-cli:
5872
runs-on: ubuntu-latest
73+
needs: source-audit
5974
env:
6075
ESP32_CORE_VERSION: 3.3.3
6176
ARDUINO_BOARDS: |

library.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"headers": ["ESPCpuMonitor.h"],
3737
"build": {
3838
"flags": [
39-
"-std=gnu++17"
39+
"-std=gnu++17",
40+
"-fno-exceptions"
4041
]
4142
}
4243
}

src/esp_cpu_monitor/cpu_monitor.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ static float clampUsagePercent(float usage) {
5050
return usage;
5151
}
5252

53+
template <typename Callback, typename... Args>
54+
static void invokeCpuMonitorCallback(const Callback &callback, Args... args) noexcept {
55+
if (!callback) {
56+
return;
57+
}
58+
59+
#if defined(__cpp_exceptions)
60+
try {
61+
callback(args...);
62+
} catch (...) {
63+
}
64+
#else
65+
callback(args...);
66+
#endif
67+
}
68+
5369
void ESPCpuMonitor::resetState(const CpuMonitorConfig &cfg) {
5470
config_ = cfg;
5571
if (!isValidSmoothingMode(config_.smoothingMode)) {
@@ -388,7 +404,7 @@ bool ESPCpuMonitor::sampleNow(CpuUsageSample &out) {
388404
bool ready = captureSample(out, callbacks);
389405
for (const auto &cb : callbacks) {
390406
if (cb) {
391-
cb(out);
407+
invokeCpuMonitorCallback(cb, out);
392408
}
393409
}
394410
return ready;
@@ -427,7 +443,7 @@ void ESPCpuMonitor::timerCallback(void *arg) {
427443
if (self->captureSample(sample, callbacks)) {
428444
for (const auto &cb : callbacks) {
429445
if (cb) {
430-
cb(sample);
446+
invokeCpuMonitorCallback(cb, sample);
431447
}
432448
}
433449
}
@@ -482,7 +498,8 @@ bool ESPCpuMonitor::computeSampleLocked(CpuUsageSample &out) {
482498
baseline = expectedIdle;
483499
}
484500
}
485-
const float idleRatio = baseline > 0.0f ? static_cast<float>(deltaIdle) / baseline : 0.0f;
501+
const float idleRatio =
502+
baseline > 0.0f ? static_cast<float>(deltaIdle) / baseline : 0.0f;
486503
const float usage = clampUsagePercent(100.0f * (1.0f - idleRatio));
487504
perCoreUsage[i] = usage;
488505
avg += usage;
@@ -496,8 +513,7 @@ bool ESPCpuMonitor::computeSampleLocked(CpuUsageSample &out) {
496513
calibrationSamplesDone_++;
497514
if (calibrationSamplesDone_ >= calibrationSamplesNeeded_) {
498515
const float calibrationWindowUs = static_cast<float>(calibrationWindowUs_);
499-
const float fallbackWindowUs =
500-
static_cast<float>(config_.sampleIntervalMs) * 1000.0f;
516+
const float fallbackWindowUs = static_cast<float>(config_.sampleIntervalMs) * 1000.0f;
501517
for (int i = 0; i < portNUM_PROCESSORS; ++i) {
502518
idleBaseline_[i] /= static_cast<float>(calibrationSamplesNeeded_);
503519
if (idleBaseline_[i] < 1.0f) {

src/esp_cpu_monitor/cpu_monitor_allocator.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,12 @@ template <typename T> class CpuMonitorAllocator {
5858
return nullptr;
5959
}
6060
if (n > (std::numeric_limits<std::size_t>::max() / sizeof(T))) {
61-
#if defined(__cpp_exceptions)
62-
throw std::bad_alloc();
63-
#else
64-
std::abort();
65-
#endif
61+
return nullptr;
6662
}
6763

6864
void *memory = cpu_monitor_allocator_detail::allocate(n * sizeof(T), usePSRAMBuffers_);
6965
if (memory == nullptr) {
70-
#if defined(__cpp_exceptions)
71-
throw std::bad_alloc();
72-
#else
73-
std::abort();
74-
#endif
66+
return nullptr;
7567
}
7668
return static_cast<T *>(memory);
7769
}

test/test_cpu_monitor/test_cpu_monitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <Arduino.h>
22
#include <ESPCpuMonitor.h>
3-
#include <unity.h>
43
#include <cmath>
4+
#include <unity.h>
55

66
namespace {
77

0 commit comments

Comments
 (0)