Skip to content

Commit

Permalink
use gdb for a more accurate rss value
Browse files Browse the repository at this point in the history
  • Loading branch information
acheronfail committed Jan 12, 2024
1 parent fae17de commit fae1aec
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ CMD
VERSION
STATS
SIZE
rss.txt
3 changes: 1 addition & 2 deletions count.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions rss.gdb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 4 additions & 5 deletions scripts/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ await writeFile(
<tr>
<th>Execution time</th>
<th>Binary size<sup>1</sup></th>
<th>Max Memory Usage<sup>2</sup></th>
<th>Max Memory Usage</th>
</tr>
<tr>
<td>
Expand Down Expand Up @@ -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'],
Expand All @@ -106,7 +106,6 @@ ${markdownTable(
</table>
> - <sup>1</sup>: only includes compiled files (i.e., does not include runtimes or libraries required for execution)
> - <sup>2</sup>: 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(
[
Expand Down

0 comments on commit fae1aec

Please sign in to comment.