-
Notifications
You must be signed in to change notification settings - Fork 12
fix(codecache): make memoryUsage() accurate and live #677
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
Merged
+218
−8
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ea28f71
fix(codecache): make memoryUsage() accurate and live
rkennke e9ad692
docs(codecache): describe current memoryUsage(), drop old-formula ref…
rkennke d68cae2
fix(codecache): don't read build-id in memoryUsage() (data race); rev…
rkennke 7d44949
harden(codecache): enforce/scope the published-immutability invariant
rkennke cc9e523
test(codecache): cover array aggregation, DWARF term, exact blob-arra…
rkennke 90523b9
docs(codecache): reference JitCodeCache::_runtime_stubs, not Libraries::
rkennke 906d445
harden(codecache): make _published atomic (release/acquire)
rkennke 9800ad3
fix(codecache): deep-copy blob name strings in copyFrom()
rkennke 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * Copyright 2026, Datadog, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include "codeCache.h" | ||
|
|
||
|
Copilot marked this conversation as resolved.
|
||
| // memoryUsage() must reflect the actual per-symbol name length, not a fixed | ||
| // per-symbol constant: adding one symbol grows the reported usage by exactly | ||
| // the allocation size of its name string. | ||
| TEST(CodeCacheTest, MemoryUsageCountsSymbolNameLength) { | ||
| CodeCache cc("testlib"); | ||
| long long base = cc.memoryUsage(); | ||
|
|
||
| const char *name = | ||
| "a_long_symbol_name_well_beyond_sizeof_NativeFunc_padding_xxxxxxxxxxxx"; | ||
| cc.add(reinterpret_cast<const void *>(0x1000), 16, name); | ||
|
|
||
| EXPECT_EQ(NativeFunc::allocSize(name), (size_t)(cc.memoryUsage() - base)); | ||
| } | ||
|
|
||
| // A longer name costs more than a shorter one — the reported size scales with | ||
| // the actual symbol-name length. | ||
| TEST(CodeCacheTest, MemoryUsageGrowsWithNameLength) { | ||
| CodeCache shortc("lib"); | ||
| CodeCache longc("lib"); | ||
| long long short_base = shortc.memoryUsage(); | ||
| long long long_base = longc.memoryUsage(); | ||
|
|
||
| shortc.add(reinterpret_cast<const void *>(0x1000), 8, "f"); | ||
| longc.add(reinterpret_cast<const void *>(0x1000), 8, | ||
| "an_extremely_long_symbol_name_padded_out_further_and_further_1234"); | ||
|
|
||
| EXPECT_GT(longc.memoryUsage() - long_base, shortc.memoryUsage() - short_base); | ||
| } | ||
|
|
||
| // A freshly-constructed, empty cache reports exactly the blob array (counted at | ||
| // the full CodeBlob size, not a pointer's worth) plus its own name — nothing | ||
| // more. An exact check so over-counting or an inflated constant would fail. | ||
| TEST(CodeCacheTest, MemoryUsageCountsFullBlobArray) { | ||
| CodeCache cc("lib"); | ||
| EXPECT_EQ((long long)INITIAL_CODE_CACHE_CAPACITY * sizeof(CodeBlob) | ||
| + (long long)NativeFunc::allocSize("lib"), | ||
| cc.memoryUsage()); | ||
| } | ||
|
|
||
| // The DWARF unwind table contributes length * sizeof(FrameDesc). Populate it so | ||
| // a regression (e.g. * flipped to /) in that term is caught; the table is | ||
| // zero-length in the other tests. | ||
| TEST(CodeCacheTest, MemoryUsageCountsDwarfTable) { | ||
| CodeCache cc("lib"); | ||
| long long base = cc.memoryUsage(); | ||
|
|
||
| const int entries = 7; | ||
| // setDwarfTable() takes ownership; ~CodeCache frees it. Contents are | ||
| // irrelevant here — memoryUsage() reads only the length. | ||
| FrameDesc *table = (FrameDesc *)malloc(entries * sizeof(FrameDesc)); | ||
| cc.setDwarfTable(table, entries); | ||
|
|
||
| EXPECT_EQ((long long)entries * sizeof(FrameDesc), cc.memoryUsage() - base); | ||
| } | ||
|
|
||
| // Copying a CodeCache must deep-copy each blob's name string. With the previous | ||
| // shallow memcpy the copy shared the originals' _name pointers, so destroying | ||
| // both double-freed them. Here both copies are populated, sized equally, and | ||
| // destroyed — a double-free would abort (or trip ASan). | ||
| TEST(CodeCacheTest, CopyIsDeepAndDoesNotDoubleFree) { | ||
| CodeCache src("lib"); | ||
| src.add(reinterpret_cast<const void *>(0x1000), 16, "sym_one"); | ||
| src.add(reinterpret_cast<const void *>(0x2000), 16, "sym_two_longer_name"); | ||
| long long expected = src.memoryUsage(); | ||
|
|
||
| { | ||
| CodeCache copy(src); | ||
| EXPECT_EQ(expected, copy.memoryUsage()); | ||
| } // copy destroyed — must not free src's name strings | ||
|
|
||
| EXPECT_EQ(expected, src.memoryUsage()); // src's names still intact | ||
| } // src destroyed — no double free | ||
|
|
||
| // CodeCacheArray::memoryUsage() is the aggregation production actually consumes | ||
| // (Profiler::dump()). Verify it sums the per-library values — guards the loop | ||
| // bound and the accumulation against regressions the per-instance tests miss. | ||
| TEST(CodeCacheTest, ArrayMemoryUsageSumsLibraries) { | ||
| CodeCacheArray arr; | ||
| CodeCache *a = new CodeCache("liba"); | ||
| CodeCache *b = new CodeCache("libb"); | ||
| // Populate before registering: add() must run pre-publication. | ||
| a->add(reinterpret_cast<const void *>(0x1000), 16, "alpha_symbol"); | ||
| b->add(reinterpret_cast<const void *>(0x2000), 16, "beta_symbol_longer_name"); | ||
|
|
||
| long long expected = (long long)a->memoryUsage() + (long long)b->memoryUsage(); | ||
| ASSERT_TRUE(arr.add(a)); | ||
| ASSERT_TRUE(arr.add(b)); | ||
| EXPECT_EQ(expected, (long long)arr.memoryUsage()); | ||
|
|
||
| // CodeCacheArray does not own the entries. | ||
| delete a; | ||
| delete b; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.