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: |
* [ANN] mwrap 2.1.0 mwrap - LD_PRELOAD malloc wrapper for Ruby
@ 2018-08-11  4:31  5% Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2018-08-11  4:31 UTC (permalink / raw)
  To: ruby-talk, mwrap-public

Changes:

    mwrap 2.1.0 - heap_page_body struct tracking

    This release enables tracking of memalign allocations for
    "struct heap_page_body" in the Ruby GC.  This can be useful
    for tracking deathspans (time between free and re-allocation)
    of heap page bodies which can cause fragmentation in some
    malloc implementations, including glibc.

    The documentation for it is available at:

      https://80x24.org/mwrap/Mwrap/HeapPageBody.html

    And a live demo runs at:

      https://80x24.org/MWRAP/heap_pages

    This release also includes global counters for
     Mwrap.total_bytes_allocated and Mwrap.total_bytes_freed

    10 changes since v2.0.0 (2018-07-20):

          add olddoc.yml to generate links in page footers
          add .olddoc.yml to MANIFEST
          gemspec: use "git describe" output for prereleases
          add global counters for total bytes allocated/freed
          keep stats for memalign-ed heap_page_body in Ruby
          remove "memalign:" MWRAP option
          allow dump_heap: mask via MWRAP env
          tweak hpb stats destructor output
          struct acc: use 64-bit counters
          doc: 2.1 pre-release updates

About:

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.

Mailing list and archives:

	https://80x24.org/mwrap-public/
	nntp://80x24.org/inbox.comp.lang.ruby.mwrap
	mailto:mwrap-public@80x24.org (no HTML mail, please)

Note: I might not be able answer questions about this for a few days.

git clone https://80x24.org/mwrap.git
homepage + rdoc: https://80x24.org/mwrap/

^ permalink raw reply	[relevance 5%]

* [PATCH] allow dump_heap: mask via MWRAP env
@ 2018-08-10 22:24  7% Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2018-08-10 22:24 UTC (permalink / raw)
  To: mwrap-public

This is useful for people who do not want to modify existing
Ruby programs.
---
 ext/mwrap/mwrap.c  | 73 +++++++++++++++++++++++++++++++++++++++++-----
 test/test_mwrap.rb |  7 +++++
 2 files changed, 72 insertions(+), 8 deletions(-)

diff --git a/ext/mwrap/mwrap.c b/ext/mwrap/mwrap.c
index 986ca70..138384d 100644
--- a/ext/mwrap/mwrap.c
+++ b/ext/mwrap/mwrap.c
@@ -345,14 +345,20 @@ acc_mean(const struct acc *acc)
 	return DBL2NUM(acc->nr ? acc->mean : HUGE_VAL);
 }
 
-static VALUE
-acc_stddev(const struct acc *acc)
+static double
+acc_stddev_dbl(const struct acc *acc)
 {
 	if (acc->nr > 1) {
 		double variance = acc->m2 / (acc->nr - 1);
-		DBL2NUM(sqrt(variance));
+		return sqrt(variance);
 	}
-	return INT2NUM(0);
+	return 0.0;
+}
+
+static VALUE
+acc_stddev(const struct acc *acc)
+{
+	return DBL2NUM(acc_stddev_dbl(acc));
 }
 
 static struct src_loc *totals_add_rcu(struct src_loc *k)
@@ -1276,9 +1282,15 @@ static VALUE hpb_stat(int argc, VALUE *argv, VALUE hpb)
  * * dump_fd: a writable FD to dump to
  * * dump_path: a path to dump to, the file is opened in O_APPEND mode
  * * dump_min: the minimum allocation size (total) to dump
+ * * dump_heap: mask of heap_page_body statistics to dump
  *
  * If both `dump_fd' and `dump_path' are specified, dump_path takes
  * precedence.
+ *
+ * dump_heap bitmask
+ * * 0x01 - summary stats (same info as HeapPageBody.stat)
+ * * 0x02 - all live heaps (similar to HeapPageBody.each)
+ * * 0x04 - skip non-heap_page_body-related output
  */
 void Init_mwrap(void)
 {
@@ -1328,6 +1340,44 @@ void Init_mwrap(void)
 	--locating;
 }
 
+enum {
+	DUMP_HPB_STATS = 0x1,
+	DUMP_HPB_EACH = 0x2,
+	DUMP_HPB_EXCL = 0x4,
+};
+
+static void dump_hpb(FILE *fp, unsigned flags)
+{
+	if (flags & DUMP_HPB_STATS) {
+		fprintf(fp,
+			"lifespan_max: %zu\n"
+			"lifespan_min: %zu\n"
+			"lifespan_mean: %0.3f\n"
+			"lifespan_stddev: %0.3f\n"
+			"deathspan_max: %zu\n"
+			"deathspan_min: %zu\n"
+			"deathspan_mean: %0.3f\n"
+			"deathspan_stddev: %0.3f\n",
+			hpb_stats.alive.max,
+			hpb_stats.alive.min,
+			hpb_stats.alive.mean,
+			acc_stddev_dbl(&hpb_stats.alive),
+			hpb_stats.reborn.max,
+			hpb_stats.reborn.min,
+			hpb_stats.reborn.mean,
+			acc_stddev_dbl(&hpb_stats.reborn));
+	}
+	if (flags & DUMP_HPB_EACH) {
+		struct alloc_hdr *h;
+
+		cds_list_for_each_entry(h, &hpb_stats.bodies, anode) {
+			void *addr = hdr2ptr(h);
+
+			fprintf(fp, "%p\t%zu\n", addr, h->as.live.gen);
+		}
+	}
+}
+
 /* rb_cloexec_open isn't usable by non-Ruby processes */
 #ifndef O_CLOEXEC
 #  define O_CLOEXEC 0
@@ -1338,10 +1388,12 @@ static void mwrap_dump_destructor(void)
 {
 	const char *opt = getenv("MWRAP");
 	const char *modes[] = { "a", "a+", "w", "w+", "r+" };
-	struct dump_arg a;
+	struct dump_arg a = { .min = 0 };
 	size_t i;
 	int dump_fd;
+	unsigned dump_heap = 0;
 	char *dump_path;
+	char *s;
 
 	if (!opt)
 		return;
@@ -1368,8 +1420,11 @@ static void mwrap_dump_destructor(void)
 	else if (!sscanf(opt, "dump_fd:%d", &dump_fd))
 		goto out;
 
-	if (!sscanf(opt, "dump_min:%zu", &a.min))
-		a.min = 0;
+	if ((s = strstr(opt, "dump_min:")))
+		sscanf(s, "dump_min:%zu", &a.min);
+
+	if ((s = strstr(opt, "dump_heap:")))
+		sscanf(s, "dump_heap:%u", &dump_heap);
 
 	switch (dump_fd) {
 	case 0: goto out;
@@ -1390,7 +1445,9 @@ static void mwrap_dump_destructor(void)
 		}
 		/* we'll leak some memory here, but this is a destructor */
 	}
-	dump_to_file(&a);
+	if ((dump_heap & DUMP_HPB_EXCL) == 0)
+		dump_to_file(&a);
+	dump_hpb(a.fp, dump_heap);
 out:
 	--locating;
 }
diff --git a/test/test_mwrap.rb b/test/test_mwrap.rb
index 9c6141d..48fba23 100644
--- a/test/test_mwrap.rb
+++ b/test/test_mwrap.rb
@@ -58,6 +58,13 @@ class TestMwrap < Test::Unit::TestCase
       res = system(env, *cmd)
       assert res, $?.inspect
       assert_match(/\b10001\s+1\s+-e:1$/, tmp.read)
+
+      tmp.rewind
+      tmp.truncate(0)
+      env['MWRAP'] = "dump_path:#{tmp.path},dump_heap:5"
+      res = system(env, *cmd)
+      assert res, $?.inspect
+      assert_match %r{lifespan_stddev}, tmp.read
     end
   end
 
-- 
EW


^ permalink raw reply related	[relevance 7%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2018-08-10 22:24  7% [PATCH] allow dump_heap: mask via MWRAP env Eric Wong
2018-08-11  4:31  5% [ANN] mwrap 2.1.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).