-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathMemAudit.cpp
More file actions
116 lines (97 loc) · 3.28 KB
/
Copy pathMemAudit.cpp
File metadata and controls
116 lines (97 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "MemAudit.h"
#if MESHTASTIC_MEM_AUDIT
#include "DebugConfiguration.h"
#include <atomic>
#include <stdio.h>
#include <string.h>
namespace memaudit
{
namespace
{
struct Entry {
std::atomic<const char *> tag; // registered literal; nullptr = free slot
std::atomic<int32_t> bytes;
};
// Static storage only - the accounting registry must never itself allocate.
// Zero-initialized (BSS), so it is usable from constructors of static objects.
Entry table[kMaxTags];
// Find the slot for a tag, registering it on first use.
// Returns nullptr for a null tag or when the table is full (update dropped).
Entry *findOrRegister(const char *tag)
{
if (!tag)
return nullptr;
// Fast path: same literal, pointer compare only. This is all the hot
// per-packet add() ever executes once the tag is registered.
size_t used = 0;
for (; used < kMaxTags; used++) {
const char *cur = table[used].tag.load(std::memory_order_acquire);
if (!cur)
break; // slots fill in order - first empty slot ends the table
if (cur == tag)
return &table[used];
}
// Slow path: same text from a different literal (duplicated across
// translation units, so not pointer-identical).
for (size_t i = 0; i < used; i++) {
if (strcmp(table[i].tag.load(std::memory_order_relaxed), tag) == 0)
return &table[i];
}
// First use: claim a free slot. compare_exchange keeps a registration race
// from double-claiming; the loser re-checks what the winner wrote.
for (size_t i = used; i < kMaxTags; i++) {
const char *expected = nullptr;
if (table[i].tag.compare_exchange_strong(expected, tag, std::memory_order_acq_rel))
return &table[i];
if (expected == tag || strcmp(expected, tag) == 0)
return &table[i];
}
return nullptr; // table full - bump kMaxTags if this ever happens
}
} // namespace
void add(const char *tag, int32_t delta)
{
Entry *e = findOrRegister(tag);
if (e)
e->bytes.fetch_add(delta, std::memory_order_relaxed);
}
void set(const char *tag, uint32_t bytes)
{
Entry *e = findOrRegister(tag);
if (e)
e->bytes.store((int32_t)bytes, std::memory_order_relaxed);
}
size_t snapshot(Tag *out, size_t max)
{
size_t n = 0;
for (size_t i = 0; i < kMaxTags && n < max; i++) {
const char *tag = table[i].tag.load(std::memory_order_acquire);
if (!tag)
break;
out[n].tag = tag;
out[n].bytes = table[i].bytes.load(std::memory_order_relaxed);
n++;
}
return n;
}
void logBreakdown(const char *when)
{
Tag rows[kMaxTags];
size_t n = snapshot(rows, kMaxTags);
if (n == 0)
return;
// Worst case per row: 16-char tag + '=' + "-2147483648" + ' ' = 29 bytes.
char line[kMaxTags * 30 + 1];
size_t pos = 0;
int32_t total = 0;
for (size_t i = 0; i < n; i++) {
int written = snprintf(line + pos, sizeof(line) - pos, "%s%s=%ld", pos ? " " : "", rows[i].tag, (long)rows[i].bytes);
if (written < 0 || pos + written >= sizeof(line))
break;
pos += written;
total += rows[i].bytes;
}
LOG_INFO("MemAudit[%s]: %s total=%ld", when ? when : "?", line, (long)total);
}
} // namespace memaudit
#endif // MESHTASTIC_MEM_AUDIT