module Mall

Mall is a single module with several singleton methods, most of which are glibc-specific. All constants may be used as the first argument to Mall.opt.

Constants

ARENA_MAX

maximum number of arenas to allocate (glibc 2.10+)

ARENA_TEST

initial number of arenas to allocate (glibc 2.10+)

CHECK_ACTION

bitmask value used for debug message output (glibc)

GRAIN

unused in glibc

KEEP

unused in glibc

MMAP_MAX

the maximum number of active mmap() requests in use at once

MMAP_THRESHOLD

the request size threshold for using mmap() (instead of sbrk())

MXFAST

max request size for “fastbins”

NLBLKS

unused in glibc

PERTURB

perturb memory allocations with a given byte (for debugging) (glibc)

TOP_PAD

amount of extra space to allocate when allocating from the heap

TRIM_THRESHOLD

maximum amount of unused memory at the top of the heapto keep before releasing it back to the kernel

Public Class Methods

dump_stats click to toggle source

Dump malloc stats to STDERR

This calls malloc_stats() internally, a function that is glibc-specific

static VALUE dump_stats(VALUE klass)
{
        fflush(stderr);
        malloc_stats();
        fflush(stderr);
        return Qnil;
}
info → hash click to toggle source

Returns a hash with the following keys:

:arena      - bytes allocated via sbrk() (and not mmap())
:ordblks    - number of free (unused) chunks
:smblks     - number of fastbin blocks[1]
:hblks      - number of allocated mmap()-ed regions
:hblkhd     - bytes allocated in mmap()-ed regions
:usmblks    - maximum total allocated space[1]
:fsmblks    - space available in freed fastbin blocks[1]
:uordblks   - total allocated space in use
:fordblks   - total free space
:keepcost   - top-most, releasable (via malloc_trim) space

All values are limited to 32-bit integers. This uses the limited mallinfo(3) function, consider using Mall.xml and parsing its output if you are using glibc (2.10+) with malloc_info(3)

See also: http:// gnu.org/software/libc/manual/html_node/Statistics-of-Malloc.html

1
  • this key is unused by glibc (ptmalloc2)

static VALUE info(VALUE klass)
{
        VALUE rv = rb_hash_new();
        struct mallinfo stats = mallinfo(); /* whee aggregate returns :( */

#define MALLINFO_SET(KEY) \
        rb_hash_aset(rv, sym_##KEY, INT2FIX(stats.KEY))

        MALLINFO_SET(arena);
        MALLINFO_SET(ordblks);
        MALLINFO_SET(smblks);
        MALLINFO_SET(hblks);
        MALLINFO_SET(hblkhd);
        MALLINFO_SET(usmblks);
        MALLINFO_SET(fsmblks);
        MALLINFO_SET(uordblks);
        MALLINFO_SET(fordblks);
        MALLINFO_SET(keepcost);
#undef MALLINFO_SET
        return rv;
}
opt(Mall::MMAP_THRESHOLD, 128 * 1024) click to toggle source

some malloc implementations may not like mallopt() being called after malloc has been initialized (first call to malloc()). This is not the case with glibc malloc.

See also: gnu.org/software/libc/manual/html_node/Malloc-Tunable-Parameters.html

static VALUE opt(VALUE klass, VALUE param, VALUE value)
{
        int rv = mallopt(FIX2INT(param), FIX2INT(value));

        return rv == 0 ? Qfalse : Qtrue;
}
trim(pad) → true or false click to toggle source

Attempt to trim off the top of the heap and release it back to the OS. pad represents the amount of free space (in bytes) to leave unreleased for future allocations.

Returns true if memory was released and false if not.

This method is glibc-specific.

static VALUE trim(VALUE klass, VALUE pad)
{
        unsigned long tmp = NUM2ULONG(pad);
        int rv = malloc_trim((size_t)tmp);

        return rv == 1 ? Qtrue : Qfalse;
        return Qfalse;
}
xml → XML string click to toggle source
xml(options = 0, io = $stderr) → io

Called with no arguments, this returns an XML string suitable for parsing with your favorite XML parser.

If specified, options must currently be 0, but is reserved for future expansion.

The second optional argument may be any object that responds to “<<” so it may be an IO, Array, StringIO, or String object among other things.

This relies on malloc_info(3) which is only in glibc 2.10+

static VALUE xml(int argc, VALUE *argv, VALUE self)
{
        int err;
        long len;
        VALUE options, out, buf;
        int xoptions;
        FILE *fp;

        rb_scan_args(argc, argv, "02", &options, &out);
        xoptions = NIL_P(options) ? 0 : NUM2INT(options);

        fp = tmpfile();
        if (fp == NULL)
                rb_sys_fail("tmpfile");

        err = malloc_info(xoptions, fp);
        if (err != 0)
                xmlerr(fp, err, "malloc_info");

        len = ftell(fp);
        if (len < 0)
                xmlerr(fp, errno, "ftell");

        rewind(fp);
        buf = rb_str_new(0, len);
        if (fread(RSTRING_PTR(buf), 1, len, fp) != (size_t)len)
                xmlerr(fp, ferror(fp), "fread");
        fclose(fp);

        if (NIL_P(out))
                return buf;
        return rb_funcall(out, id_ltlt, 1, buf);
}