* [ANN] mwrap 2.1.0 mwrap - LD_PRELOAD malloc wrapper for Ruby
@ 2018-08-11 4:31 7% 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 7%]
* [PATCH] add global counters for total bytes allocated/freed
@ 2018-07-26 6:07 6% Eric Wong
0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2018-07-26 6:07 UTC (permalink / raw)
To: mwrap-public
They can be accessed via new methods:
Mwrap.total_bytes_allocated
Mwrap.total_bytes_freed
And reset (non-atomically) via Mwrap.reset
Requested-by: Sam Saffron
https://80x24.org/mwrap-public/CAAtdryPDZjXo0QWPznKgRnd7+jQPMBFBOQdfM31KWGUVgGPWeg@mail.gmail.com/
---
ext/mwrap/mwrap.c | 21 +++++++++++++++++++++
test/test_mwrap.rb | 11 +++++++++++
2 files changed, 32 insertions(+)
diff --git a/ext/mwrap/mwrap.c b/ext/mwrap/mwrap.c
index acc8960..9bb44d0 100644
--- a/ext/mwrap/mwrap.c
+++ b/ext/mwrap/mwrap.c
@@ -32,6 +32,8 @@ extern size_t __attribute__((weak)) rb_gc_count(void);
extern VALUE __attribute__((weak)) rb_cObject;
extern VALUE __attribute__((weak)) rb_yield(VALUE);
+static size_t total_bytes_inc, total_bytes_dec;
+
/* true for glibc/dlmalloc/ptmalloc, not sure about jemalloc */
#define ASSUMED_MALLOC_ALIGNMENT (sizeof(void *) * 2)
@@ -327,6 +329,8 @@ static struct src_loc *update_stats_rcu_lock(size_t size, uintptr_t caller)
if (caa_unlikely(!totals)) return 0;
if (locating++) goto out; /* do not recurse into another *alloc */
+ uatomic_add(&total_bytes_inc, size);
+
rcu_read_lock();
if (has_ec_p()) {
int line;
@@ -390,6 +394,7 @@ void free(void *p)
if (l) {
size_t age = generation - h->as.live.gen;
+ uatomic_add(&total_bytes_dec, h->size);
uatomic_set(&h->size, 0);
uatomic_add(&l->frees, 1);
uatomic_add(&l->age_total, age);
@@ -710,12 +715,16 @@ static VALUE mwrap_dump(int argc, VALUE * argv, VALUE mod)
return Qnil;
}
+/* The whole operation is not remotely atomic... */
static void *totals_reset(void *ign)
{
struct cds_lfht *t;
struct cds_lfht_iter iter;
struct src_loc *l;
+ uatomic_set(&total_bytes_inc, 0);
+ uatomic_set(&total_bytes_dec, 0);
+
rcu_read_lock();
t = rcu_dereference(totals);
cds_lfht_for_each_entry(t, &iter, l, hnode) {
@@ -1033,6 +1042,16 @@ static VALUE mwrap_quiet(VALUE mod)
return rb_ensure(rb_yield, SIZET2NUM(cur), reset_locating, 0);
}
+static VALUE total_inc(VALUE mod)
+{
+ return SIZET2NUM(total_bytes_inc);
+}
+
+static VALUE total_dec(VALUE mod)
+{
+ return SIZET2NUM(total_bytes_dec);
+}
+
/*
* Document-module: Mwrap
*
@@ -1084,6 +1103,8 @@ void Init_mwrap(void)
rb_define_singleton_method(mod, "each", mwrap_each, -1);
rb_define_singleton_method(mod, "[]", mwrap_aref, 1);
rb_define_singleton_method(mod, "quiet", mwrap_quiet, 0);
+ rb_define_singleton_method(mod, "total_bytes_allocated", total_inc, 0);
+ rb_define_singleton_method(mod, "total_bytes_freed", total_dec, 0);
rb_define_method(cSrcLoc, "each", src_loc_each, 0);
rb_define_method(cSrcLoc, "frees", src_loc_frees, 0);
rb_define_method(cSrcLoc, "allocations", src_loc_allocations, 0);
diff --git a/test/test_mwrap.rb b/test/test_mwrap.rb
index 8425c35..d112b4e 100644
--- a/test/test_mwrap.rb
+++ b/test/test_mwrap.rb
@@ -272,4 +272,15 @@ class TestMwrap < Test::Unit::TestCase
res == :foo or abort 'Mwrap.quiet did not return block result'
end;
end
+
+ def test_total_bytes
+ assert_separately(+"#{<<~"begin;"}\n#{<<~'end;'}")
+ begin;
+ require 'mwrap'
+ Mwrap.total_bytes_allocated > 0 or abort 'nothing allocated'
+ Mwrap.total_bytes_freed > 0 or abort 'nothing freed'
+ Mwrap.total_bytes_allocated > Mwrap.total_bytes_freed or
+ abort 'freed more than allocated'
+ end;
+ end
end
--
EW
^ permalink raw reply related [relevance 6%]
Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2018-07-26 6:07 6% [PATCH] add global counters for total bytes allocated/freed Eric Wong
2018-08-11 4:31 7% [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).