-
Notifications
You must be signed in to change notification settings - Fork 14
feat(nativemem): measure allocator overhead instead of assuming a blanket factor #759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rkennke
wants to merge
3
commits into
main
Choose a base branch
from
pr-allocator-overhead-counters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Copyright 2026, Datadog, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| #ifndef _MALLOCFOOTPRINT_H | ||
| #define _MALLOCFOOTPRINT_H | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| #if defined(__linux__) | ||
| #include <malloc.h> | ||
| #define DD_HAVE_MALLOC_USABLE_SIZE 1 | ||
| #elif defined(__APPLE__) | ||
| #include <malloc/malloc.h> | ||
| #define DD_HAVE_MALLOC_SIZE 1 | ||
| #endif | ||
|
|
||
| // Real resident cost of a heap allocation, as opposed to the size that was | ||
| // requested. | ||
| // | ||
| // The profiler's NM_* gauges record requested (logical) bytes, which is what | ||
| // makes them comparable against sizeof() arithmetic. RSS, however, is paid in | ||
| // allocator chunks: the request is rounded up to an alignment quantum and | ||
| // carries a per-chunk header. Reconciliation previously multiplied logical bytes | ||
| // by a single blanket factor measured on one workload, which is wrong whenever | ||
| // the allocation-size mix differs -- overhead is a function of *per-allocation* | ||
| // size, not of total bytes. A 512 KB chunk pays ~0.003 %; a 96-byte tree node | ||
| // pays 16.7 %. | ||
| // | ||
| // This measures it instead: usable size comes from the allocator, and the | ||
| // per-chunk header is probed once at first use rather than assumed. | ||
| class MallocFootprint { | ||
| private: | ||
| // Determined empirically: allocate several same-size blocks and take the | ||
| // smallest positive address stride between any two. That stride is | ||
| // usable + header, so header = stride - usable. | ||
| // | ||
| // Probed rather than hardcoded because the allocator in force at runtime is | ||
| // not knowable at compile time -- an LD_PRELOAD'd tcmalloc or jemalloc leaves | ||
| // __GLIBC__ defined while adding no per-object header at all (their metadata | ||
| // is out of band). Assuming glibc's 8 bytes would then invent overhead that | ||
| // does not exist, at one allocation's worth per allocation. | ||
| static size_t probeHeaderBytes() { | ||
| #ifdef DD_HAVE_MALLOC_USABLE_SIZE | ||
| const int N = 16; | ||
| const size_t SZ = 48; | ||
| void *p[N]; | ||
| for (int i = 0; i < N; i++) { | ||
| p[i] = malloc(SZ); | ||
| if (p[i] == NULL) { // give up cleanly rather than guess | ||
| for (int j = 0; j < i; j++) free(p[j]); | ||
| return 0; | ||
| } | ||
| } | ||
| size_t usable = malloc_usable_size(p[0]); | ||
| long best = 0; | ||
| for (int i = 0; i < N; i++) { | ||
| for (int j = 0; j < N; j++) { | ||
| long d = (char *)p[j] - (char *)p[i]; | ||
| if (d > 0 && (best == 0 || d < best)) { | ||
| best = d; | ||
| } | ||
| } | ||
| } | ||
| for (int i = 0; i < N; i++) free(p[i]); | ||
| long header = best - (long)usable; | ||
| // Sanity-bound the result. A negative or implausibly large value means the | ||
| // blocks were not laid out contiguously (a size-class allocator, or an | ||
| // arena boundary landed mid-probe), in which case 0 is the honest answer: | ||
| // report only the rounding we can see, and understate rather than invent. | ||
| if (header < 0 || header > 64) { | ||
| return 0; | ||
| } | ||
| return (size_t)header; | ||
| #else | ||
| return 0; | ||
| #endif | ||
| } | ||
|
|
||
| public: | ||
| // Per-chunk header size for the allocator actually in force. Probed once; | ||
| // the C++11 function-local static makes initialisation thread-safe. NOT | ||
| // async-signal-safe (it allocates), so first use must not be from a signal | ||
| // handler -- every current call site is on a normal thread. | ||
| static size_t headerBytes() { | ||
| static const size_t header = probeHeaderBytes(); | ||
| return header; | ||
| } | ||
|
|
||
| // Bytes this allocation actually costs: allocator-reported usable size plus | ||
| // the per-chunk header. Page rounding for large mmap'd chunks is already | ||
| // inside the usable size, so it must not be added again. | ||
| static size_t of(void *p, size_t requested) { | ||
| if (p == NULL) { | ||
| return 0; | ||
| } | ||
| #ifdef DD_HAVE_MALLOC_USABLE_SIZE | ||
| return malloc_usable_size(p) + headerBytes(); | ||
| #elif defined(DD_HAVE_MALLOC_SIZE) | ||
| return malloc_size(p) + headerBytes(); | ||
| #else | ||
| return requested; // no introspection available: report no overhead | ||
| #endif | ||
| } | ||
|
|
||
| // Overhead alone -- the part RSS pays for that the logical counters miss. | ||
| static size_t overheadOf(void *p, size_t requested) { | ||
| size_t total = of(p, requested); | ||
| return total > requested ? total - requested : 0; | ||
| } | ||
| }; | ||
|
|
||
| #endif // _MALLOCFOOTPRINT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the process uses
LD_PRELOADwith tcmalloc or jemalloc—as the repository's reliability jobs do—__GLIBC__remains defined, so this branch still calls glibc'smallinfo2(). That function reports glibc's internal arenas rather than allocations redirected to the replacement allocator, causing the five new process-wide counters to contain zero or unrelated glibc state precisely during allocator-comparison experiments. Detect the active allocator and use its statistics API, or mark these counters unavailable outside glibc malloc.Useful? React with 👍 / 👎.