Skip to content

Commit 012c2d6

Browse files
committed
readme update
1 parent f70aa8f commit 012c2d6

5 files changed

Lines changed: 33 additions & 32 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Minimal monitor with threshold alerting:
2727
#include <ESPMemoryMonitor.h>
2828
#include <esp_log.h>
2929

30-
static ESPMemoryMonitor monitor;
30+
ESPMemoryMonitor monitor;
3131
static MemoryTag httpTag;
3232

3333
void setup() {

examples/panic_hook/panic_hook.ino

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@
1010
#endif
1111
#include <ESPMemoryMonitor.h>
1212
#include <esp_log.h>
13+
1314
#include <vector>
1415

15-
static ESPMemoryMonitor monitor;
16+
ESPMemoryMonitor monitor;
1617
static uint32_t lastSampleMs = 0;
1718

18-
static void emitSnapshot(const MemorySnapshot &snap) {
19+
static void emitSnapshot(const MemorySnapshot& snap) {
1920
#if ESPMM_HAS_ARDUINOJSON && ESPMM_EXAMPLE_HAS_JSON
2021
JsonDocument doc;
2122
toJson(snap, doc);
2223
serializeJson(doc, Serial);
2324
Serial.println();
2425
#else
25-
for (const auto &region : snap.regions) {
26-
const char *name = region.region == MemoryRegion::Psram ? "PSRAM" : "DRAM";
26+
for (const auto& region : snap.regions) {
27+
const char* name = region.region == MemoryRegion::Psram ? "PSRAM" : "DRAM";
2728
ESP_LOGI("MEM", "%s free=%uB min=%uB", name,
2829
static_cast<unsigned>(region.freeBytes),
2930
static_cast<unsigned>(region.minimumFreeBytes));
@@ -52,12 +53,12 @@ void setup() {
5253

5354
monitor.onSample(emitSnapshot);
5455

55-
monitor.onFailedAlloc([](const FailedAllocEvent &evt) {
56+
monitor.onFailedAlloc([](const FailedAllocEvent& evt) {
5657
ESP_LOGE("ALLOC", "failed alloc size=%u caps=0x%08x from %s",
5758
static_cast<unsigned>(evt.requestedBytes), evt.caps, evt.functionName);
5859
});
5960

60-
monitor.onTaskStackThreshold([](const TaskStackEvent &evt) {
61+
monitor.onTaskStackThreshold([](const TaskStackEvent& evt) {
6162
if (evt.appeared) {
6263
ESP_LOGI("TASK", "task appeared: %s", evt.usage.name.c_str());
6364
return;
@@ -68,12 +69,12 @@ void setup() {
6869
}
6970
ESP_LOGW("TASK", "%s stack %s (%uB headroom)",
7071
evt.usage.name.c_str(),
71-
evt.state == StackState::Critical ? "CRITICAL" :
72-
evt.state == StackState::Warn ? "WARN" : "SAFE",
72+
evt.state == StackState::Critical ? "CRITICAL" : evt.state == StackState::Warn ? "WARN"
73+
: "SAFE",
7374
static_cast<unsigned>(evt.usage.freeHighWaterBytes));
7475
});
7576

76-
if (!monitor.installPanicHook([](const MemorySnapshot &snap) {
77+
if (!monitor.installPanicHook([](const MemorySnapshot& snap) {
7778
ESP_LOGE("PANIC", "captured panic snapshot");
7879
emitSnapshot(snap);
7980
})) {

examples/scopes_and_leakcheck/scopes_and_leakcheck.ino

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <ESPMemoryMonitor.h>
1212
#include <esp_log.h>
1313

14-
static ESPMemoryMonitor monitor;
14+
ESPMemoryMonitor monitor;
1515
static MemoryTag httpTag;
1616
static MemoryTag otaTag;
1717

@@ -35,33 +35,33 @@ void setup() {
3535
monitor.setTagBudget(httpTag, {60 * 1024, 80 * 1024});
3636
monitor.setTagBudget(otaTag, {40 * 1024, 64 * 1024});
3737

38-
monitor.onScope([](const ScopeStats &s) {
38+
monitor.onScope([](const ScopeStats& s) {
3939
ESP_LOGI("SCOPE", "%s used %+d DRAM %+d PSRAM in %llu us",
4040
s.name.c_str(),
4141
static_cast<int>(s.deltaInternalBytes),
4242
static_cast<int>(s.deltaPsramBytes),
4343
static_cast<unsigned long long>(s.durationUs));
4444
});
4545

46-
monitor.onTagThreshold([](const TagThresholdEvent &evt) {
46+
monitor.onTagThreshold([](const TagThresholdEvent& evt) {
4747
ESP_LOGW("TAG", "%s now %s at %u bytes",
4848
evt.usage.name.c_str(),
49-
evt.usage.state == ThresholdState::Critical ? "CRITICAL" :
50-
evt.usage.state == ThresholdState::Warn ? "WARN" : "OK",
49+
evt.usage.state == ThresholdState::Critical ? "CRITICAL" : evt.usage.state == ThresholdState::Warn ? "WARN"
50+
: "OK",
5151
static_cast<unsigned>(evt.usage.totalInternalBytes + evt.usage.totalPsramBytes));
5252
});
5353

54-
monitor.onLeakCheck([](const LeakCheckResult &res) {
55-
for (const auto &d : res.deltas) {
56-
const char *regionName = d.region == MemoryRegion::Psram ? "PSRAM" : "DRAM";
54+
monitor.onLeakCheck([](const LeakCheckResult& res) {
55+
for (const auto& d : res.deltas) {
56+
const char* regionName = d.region == MemoryRegion::Psram ? "PSRAM" : "DRAM";
5757
ESP_LOGI("LEAK", "%s drift %+0.1fB frag %+0.2f", regionName, d.deltaFreeBytes, d.deltaFragmentation);
5858
}
5959
if (res.leakSuspected) {
6060
ESP_LOGW("LEAK", "possible leak between %s -> %s", res.fromLabel.c_str(), res.toLabel.c_str());
6161
}
6262
});
6363

64-
monitor.onTaskStackThreshold([](const TaskStackEvent &evt) {
64+
monitor.onTaskStackThreshold([](const TaskStackEvent& evt) {
6565
if (evt.appeared) {
6666
ESP_LOGI("TASK", "task created: %s", evt.usage.name.c_str());
6767
return;
@@ -72,8 +72,8 @@ void setup() {
7272
}
7373
ESP_LOGW("TASK", "%s stack %s (%uB headroom)",
7474
evt.usage.name.c_str(),
75-
evt.state == StackState::Critical ? "CRITICAL" :
76-
evt.state == StackState::Warn ? "WARN" : "SAFE",
75+
evt.state == StackState::Critical ? "CRITICAL" : evt.state == StackState::Warn ? "WARN"
76+
: "SAFE",
7777
static_cast<unsigned>(evt.usage.freeHighWaterBytes));
7878
});
7979
}

src/esp_memory_monitor/memory_monitor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#include "esp_memory_monitor/memory_monitor.h"
22

3+
#include <esp_log.h>
4+
#include <esp_system.h>
5+
36
#include <algorithm>
47
#include <cmath>
58
#include <numeric>
69
#include <utility>
710

8-
#include <esp_log.h>
9-
#include <esp_system.h>
10-
1111
namespace {
1212
constexpr const char* kSamplerTaskName = "ESPMemoryMon";
1313
constexpr const char* kLogTag = "ESPMemoryMon";
@@ -27,7 +27,7 @@ inline size_t saturatingSubtract(size_t value, size_t delta) {
2727
}
2828
} // namespace
2929

30-
static ESPMemoryMonitor* gPanicInstance = nullptr;
30+
ESPMemoryMonitor* gPanicInstance = nullptr;
3131
ESPMemoryMonitor* ESPMemoryMonitor::_failedAllocInstance = nullptr;
3232

3333
MemoryScope::MemoryScope(ESPMemoryMonitor* monitor, std::string name, MemoryTag tag, size_t startInternal, size_t startPsram, uint64_t startUs)

src/esp_memory_monitor/memory_monitor.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class MemoryScope;
203203
class ESPMemoryMonitor;
204204

205205
class MemoryScope {
206-
public:
206+
public:
207207
MemoryScope() = default;
208208
~MemoryScope();
209209

@@ -215,7 +215,7 @@ class MemoryScope {
215215
ScopeStats end();
216216
bool active() const { return _monitor != nullptr && !_ended; }
217217

218-
private:
218+
private:
219219
friend class ESPMemoryMonitor;
220220
MemoryScope(ESPMemoryMonitor* monitor, std::string name, MemoryTag tag, size_t startInternal, size_t startPsram, uint64_t startUs);
221221

@@ -229,7 +229,7 @@ class MemoryScope {
229229
};
230230

231231
class ESPMemoryMonitor {
232-
public:
232+
public:
233233
friend class MemoryScope;
234234
ESPMemoryMonitor() = default;
235235
~ESPMemoryMonitor();
@@ -263,7 +263,7 @@ class ESPMemoryMonitor {
263263
bool installPanicHook(PanicCallback callback = nullptr);
264264
void uninstallPanicHook();
265265

266-
private:
266+
private:
267267
enum class AllocHookType {
268268
None = 0,
269269
WithArg,
@@ -286,16 +286,16 @@ class ESPMemoryMonitor {
286286
LockGuard(const LockGuard&) = delete;
287287
LockGuard& operator=(const LockGuard&) = delete;
288288

289-
private:
289+
private:
290290
SemaphoreHandle_t _handle;
291291
};
292292

293293
static void samplerTaskThunk(void* arg);
294294
void samplerTaskLoop();
295295

296296
static void allocFailedHook(size_t requestedBytes, uint32_t caps, const char* functionName);
297-
static ESPMemoryMonitor* _failedAllocInstance;
298-
297+
ESPMemoryMonitor* _failedAllocInstance;
298+
299299
MemorySnapshot captureSnapshot() const;
300300
RegionStats captureRegion(uint32_t caps, MemoryRegion region) const;
301301
std::vector<TaskStackUsage> captureStacks() const;

0 commit comments

Comments
 (0)