• Stars
    star
    132
  • Rank 274,205 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ptmalloc2 heap dumper and visualizer

ptmalloc2 heap dumper and visualizer

This is a tool for dumping the ptmalloc2 heap into a file, and for visualizing that dump. I wrote this as part of my research into what causes memory bloating in Ruby.

Dumper

ptmallocdump.c is a library responsible for dumping the heap to a file.

Caveats

  • This dumper has only been tested on Ubuntu 18.04. The dumper relies on specific glibc/ptmalloc2 internals, so it will most likely break if you use it on any other OS/distro/glibc version.
  • This dumper is also not thread-safe. It does not acquire any ptmalloc2 mutexes. When using it, make sure that the process you're dumping is idling in all threads.
  • Although I haven't explicitly tested, this dumper is probably incompatible with swapping. It uses mincore() to check whether a page is in use or whether it has been madvise(MADV_DONTNEED)'ed, but I suspect that if a page has been swapped to disk then mincore() would return the same result. So disable swap space.

Compilation

Compile it as follows:

gcc -shared -g ptmallocdump.c -fPIC -o libptmallocdump.so -Wall -fno-strict-aliasing

Usage

This library contains two functions that you must call in order to dump the heap of the current process:

void dump_main_heap(const char *path, void *main_arena);
void dump_non_main_heaps(const char *path, void *main_arena);

If you want to call this library from Ruby then you can use FFI to load it:

require 'ffi'

module PtmallocDumper
  extend FFI::Library
  ffi_lib '/path-to/libptmallocdump.so'

  attach_function :dump_non_main_heaps, [:string, :size_t], :void
  attach_function :dump_main_heap, [:string, :size_t], :void
end

dump_main_heap dumps the ptmalloc2 main heap, while dump_non_main_heaps dumps all the other (i.e. non-main) heaps. For a full dump, you must call both functions.

path is the file to which to dump to. That file will be opened in append mode so it's fine if you pass the same filename to both functions.

main_arena is the address of the main_arena static global variable in glibc's malloc/arena.c. You can find out what that address is through the following method. It is required to have the glibc debugging symbols installed (libc6-dbg package).

  1. Obtain the relative address of the 'main_arena' variable within glibc:

    objdump -t /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.27.so | grep ' main_arena' | awk '{ print $1 }'
    
  2. Obtain the base address of the glibc library mapping in the process that you want to dump:

    grep '/libc-2.27.so$' /proc/<PID>/maps | grep ' r-xp ' | cut -d- -f 1
    
  3. Sum both addresses. That's the address to pass to the main_arena argument for those function calls.

Visualizer

Requirements

gem install oily_png --no-document

Usage

ruby ./visualize_heap.rb <DUMPFILE> <OUTPUT DIR>

More Repositories

1

default_value_for

Provides a way to specify default values for ActiveRecord models
Ruby
736
star
2

debian-packaging-for-the-modern-developer

Debian packaging tutorials for the modern developer
Shell
387
star
3

daemon_controller

A library for implementing daemon management capabilities.
Ruby
212
star
4

boyer-moore-horspool

Booyer-Moore-Horspool string search algorithm implementation in C++
HTML
105
star
5

multipart-parser

A C++ multipart MIME parser that isn't bloated with unnecessary stuff and doesn't depend on huge external libraries
C++
94
star
6

rubyenterpriseedition

Ruby Enterprise Edition based on MRI 1.8.6
Ruby
75
star
7

crash-watch

Monitor processes and display useful information when they crash
Ruby
71
star
8

mizuho

Documentation formatting tool. Converts Asciidoc input into nicely formatted HTML.
Python
71
star
9

encrypted_cookie_store

EncryptedCookieStore for Ruby on Rails 2.3
Ruby
48
star
10

matchhostfsowner

Solves the Docker host filesystem owner matching problem
Rust
45
star
11

rubyenterpriseedition187

Ruby Enterprise Edition based on MRI 1.8.7-p174
Ruby
42
star
12

rubyenterpriseedition187-330

Ruby Enterprise Edition based on Ruby 1.8.7-p330 and later
Ruby
25
star
13

paypal

Paypal IPN handling library
Ruby
22
star
14

rubyenterpriseedition187-248

Ruby Enterprise Edition based on MRI 1.8.7-p248
Ruby
22
star
15

better

A collection of better replacements for Ruby standard libaries
Ruby
17
star
16

gedit-class-browser-plugin

Gedit class browser plugin
Python
16
star
17

distributed-lock-google-cloud-storage-ruby

Ruby implementation of a distributed lock based on Google Cloud Storage
Ruby
16
star
18

streaming_find

Streams ActiveRecord query results instead of loading the entire result set into memory.
Ruby
14
star
19

work_batcher

Small library for batching work
Ruby
13
star
20

gedit-snapopen-plugin

Plugin for making it convenient to open related source files by typing in their name. Inspired by the similar TextMate feature.
Python
9
star
21

mikuru

Mikuru static image gallery generator
Ruby
8
star
22

yuumius_comments

Quickly and easily add a comments area to your website
Ruby
7
star
23

gedit-advanced-bookmarks-plugin

Advanced bookmarks plugin for Gedit
Python
6
star
24

p2

An implementation of the P^2 algorithm by Jain et al
C++
6
star
25

rubygems

RubyGems
Ruby
5
star
26

auto_redirection

Rails plugin for easily implementing correct, nested redirections
Ruby
5
star
27

clean-google-search-url

Get rid of Google search result link redirections
JavaScript
4
star
28

waph

Web Application Packaging Helper
Ruby
4
star
29

ruby-debug

enchanced ruby-debug
Emacs Lisp
4
star
30

platform_info

Various system autodetection code, extracted from Phusion Passenger
Ruby
3
star
31

sandbox

2
star
32

threadtest

A research prototype for investigating ways to implement the fastest possible multi-core-utilizing Phusion Passenger application pool
C++
2
star
33

authlogic_rails3_rspec2_test

Ruby
2
star
34

revelation

Revelation password manager
2
star
35

pulljoy

Make external pull requests CI-able again
Ruby
1
star
36

queryexplainer

Ruby
1
star
37

BulkRecorder

Bulk recording audio files on OS X
Objective-C
1
star
38

debendable

Debian package shared library dependencies inferer
Ruby
1
star