cosmopolitan/third_party/dlmalloc/dlmalloc_stats.c
Justine Tunney 226aaf3547 Improve memory safety
This commit makes numerous refinements to cosmopolitan memory handling.

The default stack size has been reduced from 2mb to 128kb. A new macro
is now provided so you can easily reconfigure the stack size to be any
value you want. Work around the breaking change by adding to your main:

    STATIC_STACK_SIZE(0x00200000);  // 2mb stack

If you're not sure how much stack you need, then you can use:

    STATIC_YOINK("stack_usage_logging");

After which you can `sort -nr o/$MODE/stack.log`. Based on the unit test
suite, nothing in the Cosmopolitan repository (except for Python) needs
a stack size greater than 30kb. There are also new macros for detecting
the size and address of the stack at runtime, e.g. GetStackAddr(). We
also now support sigaltstack() so if you want to see nice looking crash
reports whenever a stack overflow happens, you can put this in main():

    ShowCrashReports();

Under `make MODE=dbg` and `make MODE=asan` the unit testing framework
will now automatically print backtraces of memory allocations when
things like memory leaks happen. Bugs are now fixed in ASAN global
variable overrun detection. The memtrack and asan runtimes also handle
edge cases now. The new tools helped to identify a few memory leaks,
which are fixed by this change.

This change should fix an issue reported in #288 with ARG_MAX limits.
Fixing this doubled the performance of MKDEPS.COM and AR.COM yet again.
2021-10-13 17:27:13 -07:00

48 lines
1.7 KiB
C

#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "third_party/dlmalloc/dlmalloc.internal.h"
/**
* Prints on stderr the amount of space obtained from the system (both
* via sbrk and mmap), the maximum amount (which may be more than
* current if malloc_trim and/or munmap got called), and the current
* number of bytes allocated via malloc (or realloc, etc) but not yet
* freed. Note that this is the number of bytes allocated, not the
* number requested. It will be larger than the number requested because
* of alignment and bookkeeping overhead. Because it includes alignment
* wastage as being in use, this figure may be greater than zero even
* when no user-level chunks are allocated.
*
* The reported current and maximum system memory can be inaccurate if a
* program makes other calls to system memory allocation functions
* (normally sbrk) outside of malloc.
*
* malloc_stats prints only the most commonly interesting statistics.
* More information can be obtained by calling mallinfo.
*/
struct MallocStats dlmalloc_stats(mstate m) {
struct MallocChunk *q;
struct MallocStats res;
bzero(&res, sizeof(res));
ensure_initialization();
if (!PREACTION(m)) {
check_malloc_state(m);
if (is_initialized(m)) {
msegmentptr s = &m->seg;
res.maxfp = m->max_footprint;
res.fp = m->footprint;
res.used = res.fp - (m->topsize + TOP_FOOT_SIZE);
while (s != 0) {
q = align_as_chunk(s->base);
while (segment_holds(s, q) && q != m->top &&
q->head != FENCEPOST_HEAD) {
if (!is_inuse(q)) res.used -= chunksize(q);
q = next_chunk(q);
}
s = s->next;
}
}
POSTACTION(m); /* drop lock */
}
return res;
}