Skip to content

Commit 9d099dd

Browse files
committed
Add memory info to OOM crash logs
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent ac46edd commit 9d099dd

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Added the ability to run beams from the CLI for Generic Unix platform (it was already possible
1212
with nodejs and emscripten)
1313
- Added preliminary support for ESP32P4 (no networking support yet).
14+
- Added memory info in `out_of_memory` crash logs to help developers fix memory issues.
1415

1516
### Fixed
1617

src/libAtomVM/opcodesswitch.h

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,8 @@ COLD_FUNC static void cp_to_mod_lbl_off(term cp, Context *ctx, Module **cp_mod,
13521352

13531353
COLD_FUNC static void dump(Context *ctx)
13541354
{
1355+
GlobalContext *glb = ctx->global;
1356+
13551357
fprintf(stderr, "CRASH \n======\n");
13561358

13571359
fprintf(stderr, "pid: ");
@@ -1377,7 +1379,7 @@ COLD_FUNC static void dump(Context *ctx)
13771379
term_display(stderr, ctx->x[1], ctx);
13781380
fprintf(stderr, "\nx[2]: ");
13791381
term_display(stderr, ctx->x[2], ctx);
1380-
fprintf(stderr, "\n\nStack \n------\n\n");
1382+
fprintf(stderr, "\n\nStack \n-----\n\n");
13811383

13821384
term *ct = ctx->e;
13831385

@@ -1402,12 +1404,12 @@ COLD_FUNC static void dump(Context *ctx)
14021404
ct++;
14031405
}
14041406

1405-
fprintf(stderr, "\n\nMailbox\n--------\n");
1407+
fprintf(stderr, "\n\nMailbox\n-------\n");
14061408
mailbox_crashdump(ctx);
14071409

14081410
fprintf(stderr, "\n\nMonitors\n--------\n");
14091411
// Lock processes table to make sure any dying process will not modify monitors
1410-
struct ListHead *processes_table = synclist_rdlock(&ctx->global->processes_table);
1412+
struct ListHead *processes_table = synclist_rdlock(&glb->processes_table);
14111413
UNUSED(processes_table);
14121414
struct ListHead *item;
14131415
LIST_FOR_EACH (item, &ctx->monitors_head) {
@@ -1425,7 +1427,36 @@ COLD_FUNC static void dump(Context *ctx)
14251427
term_display(stderr, term_from_local_process_id(ctx->process_id), ctx);
14261428
fprintf(stderr, "\n");
14271429
}
1428-
synclist_unlock(&ctx->global->processes_table);
1430+
synclist_unlock(&glb->processes_table);
1431+
1432+
// If crash is caused by out_of_memory, print more data about memory usage
1433+
if (ctx->x[0] == ERROR_ATOM && ctx->x[1] == OUT_OF_MEMORY_ATOM) {
1434+
fprintf(stderr, "\n\nContext memory info\n-------------------\n");
1435+
fprintf(stderr, "context_size = %zu\n", context_size(ctx));
1436+
fprintf(stderr, "context_avail_free_memory = %zu\n", context_avail_free_memory(ctx));
1437+
fprintf(stderr, "heap_size = %zu\n", memory_heap_youngest_size(&ctx->heap));
1438+
fprintf(stderr, "total_heap_size = %zu\n", memory_heap_memory_size(&ctx->heap));
1439+
fprintf(stderr, "stack_size = %zu\n", context_stack_size(ctx));
1440+
fprintf(stderr, "message_queue_len = %zu\n", context_message_queue_len(ctx));
1441+
fprintf(stderr, "\n\nGlobal memory info\n------------------\n");
1442+
1443+
processes_table = synclist_rdlock(&glb->processes_table);
1444+
size_t process_count = 0;
1445+
size_t ports_count = 0;
1446+
LIST_FOR_EACH (item, processes_table) {
1447+
Context *p = GET_LIST_ENTRY(item, Context, processes_table_head);
1448+
process_count++;
1449+
if (p->native_handler) {
1450+
ports_count++;
1451+
}
1452+
}
1453+
synclist_unlock(&glb->processes_table);
1454+
1455+
fprintf(stderr, "process_count = %zu\n", process_count);
1456+
fprintf(stderr, "ports_count = %zu\n", ports_count);
1457+
fprintf(stderr, "atoms_count = %d\n", atom_table_count(glb->atom_table));
1458+
fprintf(stderr, "refc_binary_total_size = %zu\n", refc_binary_total_size(ctx));
1459+
}
14291460
fprintf(stderr, "\n\n**End Of Crash Report**\n");
14301461
}
14311462

0 commit comments

Comments
 (0)