* [PATCH] mwrap 2.0.0 mwrap - LD_PRELOAD malloc wrapper for Ruby
@ 2018-07-20 9:25 5% Eric Wong
0 siblings, 0 replies; 3+ 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 5%]
* [PATCH 07/19] memalign: check alignment on all public functions
2018-07-16 21:19 5% [PATCH 0/19] the heavy version of mwrap Eric Wong
@ 2018-07-16 21:19 7% ` Eric Wong
0 siblings, 0 replies; 3+ results
From: Eric Wong @ 2018-07-16 21:19 UTC (permalink / raw)
To: mwrap-public
And rework our internal_memalign interface to mimic the
posix_memalign API; since that's what Ruby favors and
avoids touching errno on errors.
---
ext/mwrap/mwrap.c | 74 ++++++++++++++++++++++++++++-------------------
1 file changed, 44 insertions(+), 30 deletions(-)
diff --git a/ext/mwrap/mwrap.c b/ext/mwrap/mwrap.c
index 7ae892c..6109070 100644
--- a/ext/mwrap/mwrap.c
+++ b/ext/mwrap/mwrap.c
@@ -42,11 +42,12 @@ void *__malloc(size_t);
void __free(void *);
static void *(*real_malloc)(size_t) = __malloc;
static void (*real_free)(void *) = __free;
-# define RETURN_IF_NOT_READY() do {} while (0) /* nothing */
+static const int ready = 1;
#else
static int ready;
static void *(*real_malloc)(size_t);
static void (*real_free)(void *);
+#endif /* !FreeBSD */
/*
* we need to fake an OOM condition while dlsym is running,
@@ -60,8 +61,6 @@ static void (*real_free)(void *);
} \
} while (0)
-#endif /* !FreeBSD */
-
static size_t generation;
static size_t page_size;
static struct cds_lfht *totals;
@@ -388,57 +387,72 @@ static void *ptr_align(void *ptr, size_t alignment)
return (void *)(((uintptr_t)ptr + (alignment - 1)) & ~(alignment - 1));
}
-static void *internal_memalign(size_t alignment, size_t size, uintptr_t caller)
+static bool is_power_of_two(size_t n) { return (n & (n - 1)) == 0; }
+
+static int
+internal_memalign(void **pp, size_t alignment, size_t size, uintptr_t caller)
{
struct src_loc *l;
struct alloc_hdr *h;
- void *p, *real;
+ void *real;
size_t asize;
+ size_t d = alignment / sizeof(void*);
+ size_t r = alignment % sizeof(void*);
- RETURN_IF_NOT_READY();
- if (alignment <= ASSUMED_MALLOC_ALIGNMENT)
- return malloc(size);
+ if (!ready) return ENOMEM;
+
+ if (r != 0 || d == 0 || !is_power_of_two(d))
+ return EINVAL;
+
+ if (alignment <= ASSUMED_MALLOC_ALIGNMENT) {
+ void *p = malloc(size);
+ if (!p) return ENOMEM;
+ *pp = p;
+ return 0;
+ }
for (; alignment < sizeof(struct alloc_hdr); alignment *= 2)
; /* double alignment until >= sizeof(struct alloc_hdr) */
if (__builtin_add_overflow(size, alignment, &asize) ||
__builtin_add_overflow(asize, sizeof(struct alloc_hdr), &asize))
- return 0;
+ return ENOMEM;
/* assert(asize == (alignment + size + sizeof(struct alloc_hdr))); */
rcu_read_lock();
l = update_stats_rcu(size, caller);
- p = real = real_malloc(asize);
+ real = real_malloc(asize);
if (real) {
- p = hdr2ptr(real);
+ void *p = hdr2ptr(real);
if (!ptr_is_aligned(p, alignment))
p = ptr_align(p, alignment);
h = ptr2hdr(p);
alloc_insert_rcu(l, h, size, real);
+ *pp = p;
}
rcu_read_unlock();
- return p;
+ return real ? 0 : ENOMEM;
}
-void *memalign(size_t alignment, size_t size)
+static void *
+memalign_result(int err, void *p)
{
- void *p = internal_memalign(alignment, size, RETURN_ADDRESS(0));
- if (caa_unlikely(!p)) errno = ENOMEM;
+ if (caa_unlikely(err)) {
+ errno = err;
+ return 0;
+ }
return p;
}
-static bool is_power_of_two(size_t n) { return (n & (n - 1)) == 0; }
+void *memalign(size_t alignment, size_t size)
+{
+ void *p;
+ int err = internal_memalign(&p, alignment, size, RETURN_ADDRESS(0));
+ return memalign_result(err, p);
+}
int posix_memalign(void **p, size_t alignment, size_t size)
{
- size_t d = alignment / sizeof(void*);
- size_t r = alignment % sizeof(void*);
-
- if (r != 0 || d == 0 || !is_power_of_two(d))
- return EINVAL;
-
- *p = internal_memalign(alignment, size, RETURN_ADDRESS(0));
- return *p ? 0 : ENOMEM;
+ return internal_memalign(p, alignment, size, RETURN_ADDRESS(0));
}
void *aligned_alloc(size_t, size_t) __attribute__((alias("memalign")));
@@ -446,9 +460,9 @@ void cfree(void *) __attribute__((alias("free")));
void *valloc(size_t size)
{
- void *p = internal_memalign(page_size, size, RETURN_ADDRESS(0));
- if (caa_unlikely(!p)) errno = ENOMEM;
- return p;
+ void *p;
+ int err = internal_memalign(&p, page_size, size, RETURN_ADDRESS(0));
+ return memalign_result(err, p);
}
#if __GNUC__ < 7
@@ -465,15 +479,15 @@ void *pvalloc(size_t size)
{
size_t alignment = page_size;
void *p;
+ int err;
if (add_overflow_p(size, alignment)) {
errno = ENOMEM;
return 0;
}
size = size_align(size, alignment);
- p = internal_memalign(alignment, size, RETURN_ADDRESS(0));
- if (caa_unlikely(!p)) errno = ENOMEM;
- return p;
+ err = internal_memalign(&p, alignment, size, RETURN_ADDRESS(0));
+ return memalign_result(err, p);
}
void *malloc(size_t size)
--
EW
^ permalink raw reply related [relevance 7%]
* [PATCH 0/19] the heavy version of mwrap
@ 2018-07-16 21:19 5% Eric Wong
2018-07-16 21:19 7% ` [PATCH 07/19] memalign: check alignment on all public functions Eric Wong
0 siblings, 1 reply; 3+ results
From: Eric Wong @ 2018-07-16 21:19 UTC (permalink / raw)
To: mwrap-public
TL;DR: live demo of the new features running inside a Rack app:
https://80x24.org/MWRAP/each/2000
The following changes since commit 834de3bc0da4af53535d5c9d4975e546df9fb186:
bin/mwrap: support LISTEN_FDS env from systemd (2018-07-16 19:33:12 +0000)
are available in the Git repository at:
https://80x24.org/mwrap.git heavy
for you to fetch changes up to c432e3ad30aa247dbac8575af87b0c594365d3fd:
mwrap_rack: Rack app to track live allocations (2018-07-16 21:14:13 +0000)
----------------------------------------------------------------
Eric Wong (19):
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
ext/mwrap/extconf.rb | 15 +
ext/mwrap/mwrap.c | 792 +++++++++++++++++++++++++++++++++++++++++++--------
lib/mwrap_rack.rb | 105 +++++++
test/test_mwrap.rb | 113 ++++++++
4 files changed, 901 insertions(+), 124 deletions(-)
create mode 100644 lib/mwrap_rack.rb
^ permalink raw reply [relevance 5%]
Results 1-3 of 3 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2018-07-16 21:19 5% [PATCH 0/19] the heavy version of mwrap Eric Wong
2018-07-16 21:19 7% ` [PATCH 07/19] memalign: check alignment on all public functions Eric Wong
2018-07-20 9:25 5% [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).