json-rpc: Add size and cumulative size to dev-memdump

Since we are walking the entire allocation tree anyway, and access the tal
metadata anyway, we can just as well also track the size of the memory
allocations to simplify debugging of memory use.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2019-08-02 17:17:06 +02:00
parent 0cd3823c98
commit 8ed77753ef

View file

@ -32,15 +32,17 @@ static void json_add_ptr(struct json_stream *response, const char *name,
json_add_string(response, name, ptrstr); json_add_string(response, name, ptrstr);
} }
static void add_memdump(struct json_stream *response, static size_t add_memdump(struct json_stream *response,
const char *name, const tal_t *root, const char *name, const tal_t *root,
struct command *cmd) struct command *cmd)
{ {
const tal_t *i; const tal_t *i;
size_t cumulative_size = 0;
json_array_start(response, name); json_array_start(response, name);
for (i = tal_first(root); i; i = tal_next(i)) { for (i = tal_first(root); i; i = tal_next(i)) {
const char *name = tal_name(i); const char *name = tal_name(i);
size_t size = tal_bytelen(i);
/* Don't try to dump this command! */ /* Don't try to dump this command! */
if (i == cmd || i == cmd->jcon) if (i == cmd || i == cmd->jcon)
@ -53,14 +55,18 @@ static void add_memdump(struct json_stream *response,
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_ptr(response, "parent", tal_parent(i)); json_add_ptr(response, "parent", tal_parent(i));
json_add_ptr(response, "value", i); json_add_ptr(response, "value", i);
json_add_u64(response, "size", size);
if (name) if (name)
json_add_string(response, "label", name); json_add_string(response, "label", name);
if (tal_first(i)) if (tal_first(i))
add_memdump(response, "children", i, cmd); size += add_memdump(response, "children", i, cmd);
json_add_u64(response, "cumulative_size", size);
json_object_end(response); json_object_end(response);
cumulative_size += size;
} }
json_array_end(response); json_array_end(response);
return cumulative_size;
} }
static struct command_result *json_memdump(struct command *cmd, static struct command_result *json_memdump(struct command *cmd,