microsoft / microsoft/mimalloc

Get fresh global statistics

Open
#265 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
13.4k
Forks
1.2k
Avg merge
4d 45m
Merged PRs (30d)
13

Description

When you access the statistics, it seems that you have two options: either print the "global" statistics with mi_stats_print_out or alternatively, each thread can print its own statistics.
Neither option is ideal for us, because we have a large pool of worker threads that never return (global statistics seem to be updated only when a thread exits) and each of them may allocate/free memory anytime; you can send a query to the process, but it will be served by a random thread in a different pool, so basically if you ask the stats, you get back a list of very small numbers, while the process itself is using gigabytes.

One option could be to enumerate all the stats objects of all heaps/threads and merge them on request into a temporary (without resetting them), but it's a bit unclear how to achieve this.

For the moment, we added a very crude grand total with a very simple patch: just to give you an idea, in alloc.c:

volatile _Atomic(uintptr_t) grandtotal = 0;

extern inline mi_decl_restrict void* mi_malloc(size_t size) mi_attr_noexcept {
mi_atomic_add(&grandtotal, (uintptr_t)size);
return mi_heap_malloc(mi_get_default_heap(), size);
}

extern inline mi_decl_restrict size_t mi_total_malloc_size() mi_attr_noexcept {
// not ideal, but it should work everywhere
return (size_t)mi_atomic_add(&grandtotal, 0);
}

and later in mi_free we subtract mi_page_usable_block_size(page)

however, I'd like to get an opinion on this, and I'm not totally sure of which entry points I should touch to update the grand total (e.g. mi_heap_malloc_small...).

(p.s.: there's a typo in the documentation:
[https://microsoft.github.io/mimalloc/group__extended.html#ga256cc6f13a142deabbadd954a217e228], where mi_stats_print_out is incorrectly called mi_stats_print)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in alloc.c with the existing grandtotal experiment, then trace mi_malloc, mi_free, and the mentioned mi_heap_malloc_small entry points to understand where statistics change. Review how mi_stats_print_out currently obtains global statistics and determine how a fresh aggregate could be produced without resetting counters. Check the extended documentation link for the mi_stats_print typo as a separate completion item.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.