mwrap user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH] mwrap 2.0.0 mwrap - LD_PRELOAD malloc wrapper for Ruby
@ 2018-07-20  9:25  7% Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2018-07-20  9:25 UTC (permalink / raw)
  To: ruby-talk, mwrap-public

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%
increase memory and runtime.

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 trunk (2.6.0dev+) on a few platforms:

* GNU/Linux
* FreeBSD (tested 11.1)

It may work on NetBSD, OpenBSD and DragonFly BSD.


Changes in 2.0.0:

This release includes significant changes to track live
allocations and frees.  It can find memory leaks from malloc
with less overhead than valgrind's leakchecker and there is a
new Rack endpoint (MwrapRack) which can display live allocation
stats.

API additions:

* Mwrap#[] - https://80x24.org/mwrap/Mwrap.html#method-c-5B-5D
* Mwrap::SourceLocation - https://80x24.org/mwrap/Mwrap/SourceLocation.html
* MwrapRack - https://80x24.org/mwrap/MwrapRack.html

Incompatible changes:

* Mwrap.clear now an alias to Mwrap.reset; as it's unsafe
  to implement the new Mwrap#[] API otherwise:
  https://80x24.org/mwrap-public/20180716211933.5835-12-e@80x24.org/

26 changes since v1.0.0:

      README: improve usage example
      MANIFEST: add .document
      add benchmark
      use __attribute__((weak)) instead of dlsym
      Mwrap.dump: do not segfault on invalid IO arg
      bin/mwrap: support LISTEN_FDS env from systemd
      support per-allocation headers for per-alloc tracking
      mwrap: use malloc to do our own memalign
      hold RCU read lock to insert each allocation
      realloc: do not copy if allocation failed
      internal_memalign: do not assume real_malloc succeeds
      ensure ENOMEM is preserved in errno when appropriate
      memalign: check alignment on all public functions
      reduce stack usage from file names
      resolve real_malloc earlier for C++ programs
      allow analyzing live allocations via Mwrap[location]
      alias Mwrap.clear to Mwrap.reset
      implement accessors for SourceLocation
      mwrap_aref: quiet -Wshorten-64-to-32 warning
      fixes for FreeBSD 11.1...
      use memrchr to extract address under glibc
      do not track allocations for constructor and Init_
      disable memalign tracking by default
      support Mwrap.quiet to temporarily disable allocation tracking
      mwrap_rack: Rack app to track live allocations
      documentation updates for 2.0.0 release

^ permalink raw reply	[relevance 7%]

* [PATCH] documentation updates for 2.0.0 release
@ 2018-07-20  9:11  5% Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2018-07-20  9:11 UTC (permalink / raw)
  To: mwrap-public

---
 .document         |  1 +
 README            | 34 ++++++++++++++++++++++------------
 ext/mwrap/mwrap.c | 40 +++++++++++++++++++++++++++++++---------
 lib/mwrap_rack.rb | 26 +++++++++++++++++++++-----
 4 files changed, 75 insertions(+), 26 deletions(-)

diff --git a/.document b/.document
index c35f075..4ca33e3 100644
--- a/.document
+++ b/.document
@@ -1 +1,2 @@
 ext/mwrap/mwrap.c
+lib/mwrap_rack.rb
diff --git a/README b/README
index e3bcceb..3a20258 100644
--- a/README
+++ b/README
@@ -4,20 +4,23 @@ mwrap is designed to answer the question:
 
    Which lines of Ruby are hitting malloc the most?
 
-mwrap wraps all malloc, calloc, and realloc calls to trace the Ruby
-source location of such calls and bytes allocated at each callsite.
-This functionality may be expanded in the future.
-
-It does not track allocation lifetimes, or frees, however.  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.
+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%
+increase memory and runtime.
+
+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 trunk (2.6.0dev+) on a few platforms:
+It does not require recompiling or rebuilding Ruby, but only
+supports Ruby trunk (2.6.0dev+) on a few platforms:
 
 * GNU/Linux
 * FreeBSD (tested 11.1)
@@ -45,10 +48,12 @@ variable to append the results to a log file:
 	sort -k1,1rn </path/to/log | $PAGER
 
 You may also `require "mwrap"' in your Ruby code and use
-Mwrap.dump, Mwrap.clear, Mwrap.reset, Mwrap.each, etc.
+Mwrap.dump, Mwrap.reset, Mwrap.each, etc.
 
 However, mwrap MUST be loaded via LD_PRELOAD to have any
-effect in tracking malloc use.
+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:
 
@@ -59,6 +64,11 @@ 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 2.0.0+ also supports a Rack application endpoint,
+it is documented at:
+
+	https://80x24.org/mwrap/MwrapRack.html
+
 == Known problems
 
 * 32-bit machines are prone to overflow (WONTFIX)
diff --git a/ext/mwrap/mwrap.c b/ext/mwrap/mwrap.c
index c1a59e7..acc8960 100644
--- a/ext/mwrap/mwrap.c
+++ b/ext/mwrap/mwrap.c
@@ -942,6 +942,8 @@ static struct src_loc *src_loc_get(VALUE self)
  * yielding the +size+ (in bytes) and +generation+ of each allocation.
  * The +generation+ is the value of the GC.count method at the time
  * the allocation was made.
+ *
+ * This functionality is only available in mwrap 2.0.0+
  */
 static VALUE src_loc_each(VALUE self)
 {
@@ -954,6 +956,10 @@ static VALUE src_loc_each(VALUE self)
 	return self;
 }
 
+/*
+ * The the mean lifespan (in GC generations) of allocations made from this
+ * location.  This does not account for live allocations.
+ */
 static VALUE src_loc_mean_lifespan(VALUE self)
 {
 	struct src_loc *l = src_loc_get(self);
@@ -964,21 +970,28 @@ static VALUE src_loc_mean_lifespan(VALUE self)
 	return DBL2NUM(frees ? ((double)tot/(double)frees) : HUGE_VAL);
 }
 
+/* The number of frees made from this location */
 static VALUE src_loc_frees(VALUE self)
 {
 	return SIZET2NUM(uatomic_read(&src_loc_get(self)->frees));
 }
 
+/* The number of allocations made from this location */
 static VALUE src_loc_allocations(VALUE self)
 {
 	return SIZET2NUM(uatomic_read(&src_loc_get(self)->allocations));
 }
 
+/* The total number of bytes allocated from this location */
 static VALUE src_loc_total(VALUE self)
 {
 	return SIZET2NUM(uatomic_read(&src_loc_get(self)->total));
 }
 
+/*
+ * The maximum age (in GC generations) of an allocation before it was freed.
+ * This does not account for live allocations.
+ */
 static VALUE src_loc_max_lifespan(VALUE self)
 {
 	return SIZET2NUM(uatomic_read(&src_loc_get(self)->max_lifespan));
@@ -1011,6 +1024,8 @@ static VALUE reset_locating(VALUE ign) { --locating; return Qfalse; }
  * Stops allocation tracking inside the block.  This is useful for
  * monitoring code which calls other Mwrap (or ObjectSpace/GC)
  * functions which unavoidably allocate memory.
+ *
+ * This feature was added in mwrap 2.0.0+
  */
 static VALUE mwrap_quiet(VALUE mod)
 {
@@ -1052,9 +1067,16 @@ void Init_mwrap(void)
 	VALUE mod;
 
 	++locating;
-        mod = rb_define_module("Mwrap");
+	mod = rb_define_module("Mwrap");
 	id_uminus = rb_intern("-@");
 
+	/*
+	 * Represents a location in source code or library
+	 * address which calls a memory allocation.  It is
+	 * updated automatically as allocations are made, so
+	 * there is no need to reload or reread it from Mwrap#[].
+	 * This class is only available since mwrap 2.0.0+.
+	 */
 	cSrcLoc = rb_define_class_under(mod, "SourceLocation", rb_cObject);
 	rb_define_singleton_method(mod, "dump", mwrap_dump, -1);
 	rb_define_singleton_method(mod, "reset", mwrap_reset, 0);
@@ -1080,18 +1102,18 @@ void Init_mwrap(void)
 __attribute__ ((destructor))
 static void mwrap_dump_destructor(void)
 {
-        const char *opt = getenv("MWRAP");
-        const char *modes[] = { "a", "a+", "w", "w+", "r+" };
-        struct dump_arg a;
-        size_t i;
-        int dump_fd;
+	const char *opt = getenv("MWRAP");
+	const char *modes[] = { "a", "a+", "w", "w+", "r+" };
+	struct dump_arg a;
+	size_t i;
+	int dump_fd;
 	char *dump_path;
 
 	if (!opt)
 		return;
 
-        ++locating;
-        if ((dump_path = strstr(opt, "dump_path:")) &&
+	++locating;
+	if ((dump_path = strstr(opt, "dump_path:")) &&
 			(dump_path += sizeof("dump_path")) &&
 			*dump_path) {
 		char *end = strchr(dump_path, ',');
@@ -1136,5 +1158,5 @@ static void mwrap_dump_destructor(void)
 	}
 	dump_to_file(&a);
 out:
-    --locating;
+	--locating;
 }
diff --git a/lib/mwrap_rack.rb b/lib/mwrap_rack.rb
index ef3872b..a750f32 100644
--- a/lib/mwrap_rack.rb
+++ b/lib/mwrap_rack.rb
@@ -5,9 +5,24 @@ require 'mwrap'
 require 'rack'
 require 'cgi'
 
-# Usage: mwrap rackup ...
+# MwrapRack is a standalone Rack application which can be
+# mounted to run within your application process.
+#
+# Using the Rack::Builder API in config.ru, you can map it to
+# the "/MWRAP/" endpoint.  As with the rest of the Mwrap API,
+# your Rack server needs to be spawned with the mwrap(1)
+# wrapper to enable the LD_PRELOAD.
+#
+#     require 'mwrap_rack'
+#     map('/MWRAP') { run(MwrapRack.new) }
+#     map('/') { run(your_normal_app) }
+#
+# A live demo is available at https://80x24.org/MWRAP/
+# (warning the demo machine is 32-bit, so counters will overflow)
+#
+# This module is only available in mwrap 2.0.0+
 class MwrapRack
-  module HtmlResponse
+  module HtmlResponse # :nodoc:
     def response
       [ 200, {
           'Expires' => 'Fri, 01 Jan 1980 00:00:00 GMT',
@@ -18,7 +33,7 @@ class MwrapRack
     end
   end
 
-  class Each < Struct.new(:script_name, :min, :sort)
+  class Each < Struct.new(:script_name, :min, :sort) # :nodoc:
     include HtmlResponse
     HEADER = '<tr><th>' + %w(total allocations frees mean_life max_life
                 location).join('</th><th>') + '</th></tr>'
@@ -61,7 +76,7 @@ class MwrapRack
     end
   end
 
-  class EachAt < Struct.new(:loc)
+  class EachAt < Struct.new(:loc) # :nodoc:
     include HtmlResponse
     HEADER = '<tr><th>size</th><th>generation</th></tr>'
 
@@ -77,10 +92,11 @@ class MwrapRack
     end
   end
 
-  def r404
+  def r404 # :nodoc:
     [404,{'Content-Type'=>'text/plain'},["Not found\n"]]
   end
 
+  # The standard Rack application endpoint for MwrapRack
   def call(env)
     case env['PATH_INFO']
     when %r{\A/each/(\d+)\z}
-- 
EW


^ permalink raw reply related	[relevance 5%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2018-07-20  9:11  5% [PATCH] documentation updates for 2.0.0 release Eric Wong
2018-07-20  9:25  7% [PATCH] mwrap 2.0.0 mwrap - LD_PRELOAD malloc wrapper for Ruby Eric Wong

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).