Skip to content

Commit 56f7566

Browse files
committed
implement memory stats to JSON
1 parent 4ea7db2 commit 56f7566

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/MicroOcpp/Core/Memory.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void mo_mem_print_stats() {
207207

208208
for (const auto& heapEntry : memBlocks) {
209209
size += heapEntry.second.size;
210-
#if MO_DBG_LEVEL >= MO_DL_DEBUG
210+
#if MO_DBG_LEVEL >= MO_DL_VERBOSE
211211
{
212212
MO_CONSOLE_PRINTF("@%p - %zu B (%s)\n", heapEntry.first, heapEntry.second.size, heapEntry.second.tag.c_str());
213213
}
@@ -254,6 +254,41 @@ void mo_mem_print_stats() {
254254
#endif
255255
}
256256

257+
int mo_mem_write_stats_json(char *buf, size_t size) {
258+
DynamicJsonDocument doc {size * 2};
259+
260+
doc["total_current"] = memTotal;
261+
doc["total_max"] = memTotalMax;
262+
doc["total_blocks"] = memBlocks.size();
263+
264+
JsonArray by_tag = doc.createNestedArray("by_tag");
265+
for (const auto& tag : memTags) {
266+
JsonObject entry = by_tag.createNestedObject();
267+
entry["tag"] = tag.first.c_str();
268+
entry["current"] = tag.second.current_size;
269+
entry["max"] = tag.second.max_size;
270+
}
271+
272+
size_t untagged = 0, untagged_size = 0;
273+
274+
for (const auto& heapEntry : memBlocks) {
275+
if (heapEntry.second.tag.empty()) {
276+
untagged ++;
277+
untagged_size += heapEntry.second.size;
278+
}
279+
}
280+
281+
doc["untagged_blocks"] = untagged;
282+
doc["untagged_size"] = untagged_size;
283+
284+
if (doc.overflowed()) {
285+
MO_DBG_ERR("exceeded JSON capacity");
286+
return -1;
287+
}
288+
289+
return (int)serializeJson(doc, buf, size);
290+
}
291+
257292
#endif //MO_OVERRIDE_ALLOCATION && MO_ENABLE_HEAP_PROFILER
258293

259294
namespace MicroOcpp {

0 commit comments

Comments
 (0)