From fae1aecbe9995a13de45729a4b9dc3ba7febcff5 Mon Sep 17 00:00:00 2001 From: acheronfail Date: Fri, 12 Jan 2024 11:29:31 +1030 Subject: [PATCH] use gdb for a more accurate rss value --- .gitignore | 1 + count.asm | 3 +-- justfile | 5 +++-- rss.gdb | 19 +++++++++++++++++++ scripts/summary.js | 9 ++++----- 5 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 rss.gdb diff --git a/.gitignore b/.gitignore index 37b425c..f82113b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ CMD VERSION STATS SIZE +rss.txt \ No newline at end of file diff --git a/count.asm b/count.asm index 31ca650..c8c41ae 100644 --- a/count.asm +++ b/count.asm @@ -47,8 +47,7 @@ program_header: ; Elf64_Phdr output db 10 dup(0), 10 ; max 32bit number is 10 digits (decimal) long, followed by newline _start: ; argc is at `rsp`, argv is at `rsp + 8` - mov rdi, [rsp + 8] ; put argv into `rdi` - add rdi, 8 ; skip the first argument (program name) + mov rdi, [rsp + 16] ; put argv[1] into `rdi` (+ 8 to skip argv[0]) xor rcx, rcx ; zero xor rax, rax ; zero diff --git a/justfile b/justfile index 7a844d5..47f6878 100644 --- a/justfile +++ b/justfile @@ -90,8 +90,9 @@ measure what: if [[ -f SIZE ]]; then jq '. += {"size":"'"$(cat SIZE)"'"}' "$out" | sponge "$out" fi - timers $(cat CMD) >/dev/null 2> STATS - jq '. += {"max_rss":'$(rg -oP '(?:max_rss:\s*)(\d+)' -r '$1' ./STATS)'}' "$out" | sponge "$out" + + gdb -return-child-result --command rss.gdb --args $(cat CMD) >/dev/null + jq '. += {"rss":'$(( $(rg -oPr '$1' 'Rss:\s*(\d+)' ./rss.txt) * 1024 ))'}' "$out" | sponge "$out" measure-all: #!/usr/bin/env bash diff --git a/rss.gdb b/rss.gdb new file mode 100644 index 0000000..766c3e7 --- /dev/null +++ b/rss.gdb @@ -0,0 +1,19 @@ +# run this with: + +# catch program exit +catch syscall exit +run + +# capture program pid +python +gdb.execute("set $pid = " + str(gdb.selected_inferior().pid)) +end + +# while it's still running, extract rss from /proc +eval "shell cat /proc/%d/smaps_rollup > rss.txt", $pid + +# allow program to exit, and exit gdb +# when gdb is run with the `-return-child-result` flag it will cause gdb to exit +# with the same code the child did +continue +exit diff --git a/scripts/summary.js b/scripts/summary.js index 3602291..8c92710 100644 --- a/scripts/summary.js +++ b/scripts/summary.js @@ -39,7 +39,7 @@ await writeFile( Execution time Binary size1 - Max Memory Usage2 + Max Memory Usage @@ -90,11 +90,11 @@ ${[...sizeTypes.values()] ${markdownTable( [ - ['#', 'name', 'max_rss'], + ['#', 'name', 'rss'], ...results .slice() - .sort((a, b) => a.max_rss - b.max_rss) - .map(({ name, max_rss }, i) => [i + 1, wrap(name), formatSize(max_rss, { minimumFractionDigits: 7 })]), + .sort((a, b) => a.rss - b.rss) + .map(({ name, rss }, i) => [i + 1, wrap(name), formatSize(rss, { minimumFractionDigits: 7 })]), ], { align: ['l', 'l', 'r'], @@ -106,7 +106,6 @@ ${markdownTable( > - 1: only includes compiled files (i.e., does not include runtimes or libraries required for execution) -> - 2: Getting the \`max_rss\` isn't 100% reliable for very small binary sizes. This appears to be [a limitation of the linux kernel](https://github.com/acheronfail/timeRS/blob/master/LIMITATIONS.md). ${markdownTable( [