mwrap.git  about / heads / tags
LD_PRELOAD malloc wrapper + line stats for Ruby
$ git log --pretty=format:'%h %s (%cs)%d' v3.0.0-pre1 --
458c801 3.0.0-pre1 (2023-01-09)
	(tag: v3.0.0-pre1)
0de44f7 fix test_sigusr1_works (2023-01-09)
e8eb118 httpd: add CSV download link to /each/$MIN HTML (2023-01-09)
316732f httpd: add notes about arenas and object heaps (2023-01-09)
f617762 httpd: do not sort CSV output by default (2023-01-09)
4106dfd various doc updates, add mwrap(1) manpage (2023-01-09)
ee995d4 httpd: s/X-Mwrap-BT-Depth/X-Mwrap-BT/ (2023-01-09)
043831f httpd: different URLs for Ruby vs Perl versions (2023-01-09)
9039f35 httpd: show GC count (2023-01-09)
b344bcc test spawning non-Ruby process with socket_dir (2023-01-09)
...

$ git cat-file blob v3.0.0-pre1:README
= mwrap - LD_PRELOAD malloc wrapper + line stats for Ruby

mwrap is designed to answer the question:

   Which lines of Ruby are hitting malloc the most?

mwrap wraps all malloc-family calls to trace the Ruby source
location of such calls and bytes allocated at each callsite.
As of mwrap 2.0.0, it can also function as a leak detector
and show live allocations at every call site.  Depending on
your application and workload, the overhead is roughly a 50-100%
increase memory and runtime with default settings.

It works best for allocations under GVL, but tries to track
numeric caller addresses for allocations made without GVL so you
can get an idea of how much memory usage certain extensions and
native libraries use.

It requires the concurrent lock-free hash table from the
Userspace RCU project: https://liburcu.org/

It does not require recompiling or rebuilding Ruby, but only
supports Ruby 2.7.0 or later on a few platforms:

* GNU/Linux (only tested --without-jemalloc, mwrap 3.x provides its own)

It may work on FreeBSD, NetBSD, OpenBSD and DragonFly BSD if given
appropriate build options.

== Install

	# Debian-based systems: apt-get liburcu-dev

	# Install mwrap via RubyGems.org
	gem install mwrap

== Usage

mwrap works as an LD_PRELOAD and supplies a mwrap RubyGem executable to
improve ease-of-use.  You can set dump_path: in the MWRAP environment
variable to append the results to a log file:

	MWRAP=dump_path:/path/to/log mwrap RUBY_COMMAND

	# And to display the locations with the most allocations:
	sort -k1,1rn </path/to/log | $PAGER

You may also `require "mwrap"' in your Ruby code and use
Mwrap.dump, Mwrap.reset, Mwrap.each, etc.

However, mwrap MUST be loaded via LD_PRELOAD to have any
effect in tracking malloc use.  However, it is safe to keep
"require 'mwrap'" in performance-critical deployments,
as overhead is only incurred when used as an LD_PRELOAD.

The output of the mwrap dump is a text file with 3 columns:

	total_bytes	call_count	location

Where location is a Ruby source location (if made under GVL)
or an address retrieved by backtrace_symbols(3).  It is
recommended to use the sort(1) command on either of the
first two columns to find the hottest malloc locations.

mwrap 3.0.0+ also supports an embedded HTTP server
it is documented at:

https://80x24.org/mwrap.git/tree/Documentation/mwrap.pod

== Known problems

* 32-bit machines are prone to overflow (WONTFIX)

* signalfd(2)-reliant code will need latest URCU with commit
  ea3a28a3f71dd02f (Disable signals in URCU background threads, 2022-09-23)
  This doesn't affect C Ruby itself, and signalfd(2) use is rare
  3rd-party processes.

* Ruby source files over 16.7 million lines long are not supported :P

== Public mail archives (HTTP, Atom feeds, IMAP mailbox, NNTP group, POP3):

* https://80x24.org/mwrap-public/
* nntps://80x24.org/inbox.comp.lang.ruby.mwrap
* imaps://;AUTH=ANONYMOUS@80x24.org/inbox.comp.lang.ruby.mwrap.0
* https://80x24.org/mwrap-public/_/text/help/#pop3

No subscription nor real identities will ever be required to obtain support,
but HTML mail is rejected.  Memory usage reductions start with you;
only send plain-text mail to us and do not top-post.  HTML mail and
top-posting costs everybody memory and bandwidth.

		mwrap-public@80x24.org

== Hacking

	git clone https://80x24.org/mwrap.git

Send all patches ("git format-patch" + "git send-email") and
pull requests (use "git request-pull" to format) via email
to mwrap-perl@80x24.org.  We do not and will not use
proprietary messaging systems.

== License

GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>

# heads (aka `branches'):
$ git for-each-ref --sort=-creatordate refs/heads \
	--format='%(HEAD) %(refname:short) %(subject) (%(creatordate:short))'
* master       README: fixup references to the Perl version (2024-01-08)
  heavy        mwrap_rack: Rack app to track live allocations (2018-07-16)

# tags:
$ git for-each-ref --sort=-creatordate refs/tags \
	--format='%(refname:short) %(subject) (%(creatordate:short))'
v3.0.0-pre1  mwrap 3.0.0-pre1 (2023-01-09) tar.gz
v2.3.0       mwrap 2.3.0 (2022-09-03) tar.gz
v2.2.0       mwrap 2.2.0 (2022-08-22) tar.gz
v2.1.0       mwrap 2.1.0 - heap_page_body struct tracking (2018-08-11) tar.gz
v2.0.0       mwrap 2.0.0 (2018-07-20) tar.gz
v1.0.0       mwrap 1.0.0 - initial release (2018-07-02) tar.gz

# associated public inboxes:
# (number on the left is used for dev purposes)
            https://80x24.org/mwrap-public

git clone https://80x24.org/mwrap.git