Show HN: I wrote a tool in Rust for tracking all allocations in a Linux process
11–20 of 31 posts
Re: Show HN: I wrote a tool in Rust for tracking all allocations in a Linux process
#12So it's like strace looking for brk()?
Sorry if this is a dumb question, but can't strace trace brk() calls? And as kind of a follow up what is the easiest way to trace all allocations (brk() and mmap) but nothing else?
Absolutely.
> what is the easiest way to trace all allocations (brk() and mmap) but nothing else?
strace -e mmap "$command"
I don't think anything modern still uses the program break but one should know brk and sbrk exist. To see deallocations, add munmap to the filter. Note that these represent operating system allocations: programs usually request huge chunks and then manage that memory in user space in order to avoid system call overhead. In many systems, this memory won't actually count as used unless the process actually touches it and causes page fault.Re: Show HN: I wrote a tool in Rust for tracking all allocations in a Linux process
#13Interesting approach. How is performance compared to something like https://github.com/koute/bytehound
Bytehound author here. Just from a cursory look at the README: > allocscope-trace attaches to another process as a debugger. By using breakpoints on memory allocation functions such as malloc it tracks allocations made by that process. Looks like it's using breakpoints so I'd expect it to be orders of magnitude slower. And looking at the source code it's also using `libunwind`, so even if it wasn't using breakpoints…
libbytehound.so (with extra debug assertions, because I'm too lazy to recompile in release mode): 4s
allocscope: did not finish after 4 minutes (I got bored waiting and CTRL+C'd it)
Re: Show HN: I wrote a tool in Rust for tracking all allocations in a Linux process
#14So it's like strace looking for brk()?
Re: Show HN: I wrote a tool in Rust for tracking all allocations in a Linux process
#15It made its own socket and thread to listen on it. It would just dump a snapshot of tree to anything that connected. I also had some tooling that would let you diff two snapshots, since it was helpful to see if particular stimuli cause persistent extra allocations. While finding the largest outstanding delta between allocated and free bytes was great for finding leaks, sorting by lifetime count of blocks allocated was also fun. I remember some little puzzle game I enjoyed playing at the time would allocate and free tens of thousands of blocks as you dragged a line around for a second.
There was a tricky chicken and egg problems with LD_PRELOAD wrapping one of the allocation functions, because it was used internally by dlsym, which I was using to retrieve pointers to the proper function implementations. (calloc if I recall correctly.) I hacked around it by making my library allocate bytes out of a static char array for the calloc call that would happen while dlsym-ing for calloc. Debugging this was a nightmare, since it would break so early in the process's lifetime that GDB breakpoints weren't functioning. Tracking in a second process seems like a way simpler idea, and probably doesn't have too much of an impact on performance.
Re: Show HN: I wrote a tool in Rust for tracking all allocations in a Linux process
#16Re: Show HN: I wrote a tool in Rust for tracking all allocations in a Linux process
#17Re: Show HN: I wrote a tool in Rust for tracking all allocations in a Linux process
#18However, as a practical matter those solutions might omit mmap which some applications might use for anonymous allocations.
Re: Show HN: I wrote a tool in Rust for tracking all allocations in a Linux process
#19I'd be curious to see how this ptrace tool performs compared with one that relies on ELF symbol interposition (a la LD_PRELOAD). Other heap profilers (heaptrack, libtcmalloc, etc) use this method. Presumably the loader resolves the symbols once at load time and there's little cost overhead to switch to the profiler code. However, as a practical matter those solutions might omit mmap which some applications might use…
I've posted some very quick numbers in my comment here comparing it to Bytehound: https://news.ycombinator.com/item?id=34806401
> However, as a practical matter those solutions might omit mmap which some applications might use for anonymous allocations.
Bytehound also gathers mmaps. (: