Repository navigation
Warm up the parser before measuring AST allocations in the parser memory profiler - #115739
Conversation
…ory profiler parser_memory_profiler parses exactly one query per process with no warm-up, so every lazy one-time initialization reachable from parseQuery is charged to that query's AST allocation. On Query 64 of test_queries.txt that is 2,400 bytes over five allocations. The largest is a single one-time 1,280 byte calloc made during the first call into the expression parser (DB::ParserExpressionImpl::tryParseOperand); its two leaf frames are in a shared object and absent from the executable's symbol table, so the profiler reports them unresolved. The other four are the function-local static sets in isIntegerTypeName (src/Parsers/ParserDataType.cpp, 608 and 192 bytes) and isArrayQuantifierPredicate (src/Parsers/ExpressionListParsers.cpp, 224 and 96 bytes), which allocate through operator new and unordered_set::insert. Whether that initialization lands inside the measured window is a property of the binary rather than of the SQL, and Parser memory check compares two different binaries (a downloaded master build against the PR build), so the two arms can differ by that whole amount while the parser source is identical. parser_memory_check.py then scores the difference as a per-query regression: 1,280 > 100 bytes and 34.7% > 1%, which always fails. Since 2026-08-19 this has failed 5 PRs across 117 test rows, every one of them reporting exactly +1,280 bytes and none of them touching the parser, while 8 other PRs in the same window were credited with the mirror-image improvement. Parsing the query once before the window opens restores the property the check rests on, namely that the window contains only allocations of the parse itself. Measured with the job's own analyze_heap_profiles on Query 64: 4,968 to 2,568 bytes, the 1,280 byte stack present to absent, and reverting restores it exactly. Over the full 100 query corpus the stack appears on exactly queries 64 and 99 before the change and on none after, with no errors on either arm. An injected per-parse 4,096 byte allocation is still reported as +4,112 bytes, so the check still catches a real regression. The warm-up deliberately does not catch exceptions: it parses the same text as the measured parse, so an invalid query throws here first and the existing handler still renders it as an error result. Reported values legitimately drop where one-time initialization leaves the window; both arms are measured the same way, so comparisons are unaffected. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review (2 rounds, 0 open blockers)Reviewed cold against the resulting code by an independent model, then adjudicated. Round 1 bounced ❌ Blocker (round 1, fixed): the named carrier was the wrong allocation. The first draft said
💡 Nit (round 2, folded in): the two unresolved frames are identifiable. They resolve against Two nits were disagreed with, on evidence. The sample stdout numbers in the profiler's header doc I re-derived the numbers rather than accepting them: re-ran the job's own Gate: 2 rounds, findings 1 then 0, $28.10 total. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20260820-192200 |
|
Workflow [PR], commit [6b3f31b] Summary: ✅
AI ReviewSummaryThis PR adds a warm-up parse ahead of the measured window in Final Verdict✅ No findings. |
|
cc @qoega @leshikus, could you review this? |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units8 translation units recompiled, 40 s compile time in total, 8 of them have a recent master baseline. |
b4fb542
…ck scenario The job ran the 9-scenario sequence once per binary and diffed each checkpoint-to-checkpoint window between the master and PR clickhouse-examples builds. With no warmup, a one-time process initialization first touched inside window k is charged entirely to window k, and two different binaries reach those first touches after a different cumulative number of events, so the same one-time cost lands in different windows on the two sides. One window then reports a fixed positive offset and an adjacent one the same negative offset, which is silently accepted as an improvement. In the last 36 hours this failed 7 of 113 PR heads across five mutually unrelated PRs, none of which touches ci/. On four of them the reported regression was the identical constant +1,280 bytes on four different scenarios. Identified at the frame level: the allocation is a calloc whose two unresolved frames sit at libc addresses, outside the binary's own text, below the first touch of a function-local static. glibc keeps __cxa_atexit registrations in 32-entry blocks, so the 33rd, 65th, ... registration callocs a fresh struct exit_function_list of next(8) + idx(8) + 32 * sizeof(struct exit_function)(32) = 1,040 bytes, which jemalloc rounds up to its 1,280-byte size class and never frees. Against the 8 KB baseline of 08_parts_analysis that offset is 15.8%, so it clears both the absolute (1,024 B) and the percentage (1.0%) threshold and fails the job. Run the sequence once unmeasured before the measured pass, by passing the scenario list twice to the existing --file option and measuring only the second pass, using the previous pass's final checkpoint as the baseline. By then every path the scenarios exercise has initialized its statics and grown its pools, so no first touch is left inside a measured window. This is the mechanism of PR ClickHouse#115739, which fixed the same class in Parser memory check with a warmup parse, adapted to a stateful profiler. The change is in the job script rather than in the profiler because the master arm binary is downloaded from S3 built from master: a new flag would make the old master binary exit with Unknown option, and an unconditional C++ warmup would give the PR arm a warm baseline while master stayed cold. A job-side change applies to both arms in the same run from the same script, so it is symmetric from the first execution and needs no transition window. Deliberately not done: STORAGE_CHANGE_THRESHOLD_BYTES is untouched, no stack is filtered, and the symbolized file count per arm stays at 10 so the dominant cost of the job does not grow. Stack exclusion was rejected because the frames are unsymbolized, leaving only "leading frames are ??" (which would blind the check to every allocation inside any non-ClickHouse library) or "the delta is exactly 1,280" as keys. The latter is unsound beyond being a magic number: the same window contains three further +1,280 rows that are ordinary operator new[] from ThreadPoolImpl::scheduleImpl, so such a filter would hide real thread-pool regressions. Validated against three local clickhouse-examples builds. The class is present without the fix (2 rows), absent with it (0), and present again on a fresh unfixed run. It does not blind the check: with the fix in place, an injected real 64 KiB-per-table allocation still fails at +65,080 bytes on 01_create_table, +132,016 on 05_multiple_tables and shows exactly +65,536 on 07_many_parts. Cross-arm, an antisymmetric +12,192 / -12,192 pair that was the entire reported change of two adjacent windows collapses to +40 / -40, and five windows go to exactly zero. The sequence is re-runnable, which the second pass depends on: 09_cleanup.sql drops all five objects the other eight scenarios create, and all six two-pass runs across the three binaries exited 0. If a future scenario leaves an object behind, 01_create_table.sql's plain CREATE TABLE makes the profiler exit non-zero and the job report ERROR rather than mis-measure. The README records that contract for the next scenario author. Cost: the master and PR scenario runs are doubled while the download, the single-scenario pr-async-no-cache run and all symbolization are unchanged. Sized from CI data, where the download plus all three profiler runs occupy 10m27s of a 1,807 s job, the estimate is roughly +8 minutes. A regression that manifests only on the first touch of a code path is now charged to the unmeasured pass and no longer reported. This is the same tradeoff PR ClickHouse#115739 accepted and the correct one for a check whose subject is per-scenario storage memory. Related: ClickHouse#119480 Related: ClickHouse#115739 Co-Authored-By: Claude Opus 5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Since 2026-08-20
Parser memory checkhas failed 5 PRs across 154 test rows, every row reportingexactly
+1,280 bytes: #115301 (37 queries), #114577 (37), #110180, #115630 and #100371 (2 each).None touches the parser. The same window credits 8 further PRs with the mirror-image
-1,280 Bimprovement, so the check can both fail innocent PRs and pass one adding a genuine 1,280 byte
regression.
The cause is in the instrument.
parser_memory_profilerparses one query per process with nowarm-up, so lazy one-time initialization reachable from
parseQueryis charged to that query:2,400 bytes over five allocations on Query 64. The largest is one 1,280 byte
callocduringthe first call into the expression parser (
ParserExpressionImpl::tryParseOperand), whose two leafframes sit in a shared object absent from the executable's symbol table, so the profiler leaves them
unresolved. Whether these land inside the window is a property of the binary, not the SQL, so the two
arms, being different builds, can differ by the whole amount with identical parser source, which
clears both thresholds (1,280 B, 34.7%).
Parsing once before the window opens leaves only this parse's allocations. With the job's own
analyzer on Query 64: 4,968 -> 2,568 bytes, the 1,280 byte stack present to absent, reverting
restores it. Over all 100 queries it appears on exactly 64 and 99 before, none after; an injected
per-parse 4,096 byte allocation still reports +4,112 B, so the check is not blind.
@ qoega, two consequences to rule on. Reported values drop for 37 of 100 queries, and for about one
master build a warmed PR arm is scored against an unwarmed baseline, inflating apparent
improvements; that self-heals and I added no compensating logic. New expensive lazy initialization
is also no longer visible here, which fits a per-query AST check.
Related: #113491
Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115630&sha=042aecf702d2050e274706aa20011eabedb78542&name_0=PR&name_1=Parser%20memory%20check
Query 64: the 2,400 bytes of one-time initialization, by allocating frame
Multiset of per-stack byte diffs removed by the warm-up, attributed to the allocator frame that
actually appears in the symbolized heap (not to an inlined neighbour):
je_calloc/__libc_calloc, two unresolved shared-object framesParserExpressionImpl::tryParseOperandoperator new/unordered_set::insertisIntegerTypeNamestatic set (ParserDataType.cpp)operator new/unordered_set::insertisArrayQuantifierPredicatestatic set (ExpressionListParsers.cpp)operator new/unordered_set::insertisIntegerTypeName, second allocationoperator new/unordered_set::insertisArrayQuantifierPredicate, second allocationThe 7 element
isArrayQuantifierPredicateset and the 19 elementisIntegerTypeNameset matchsizeof(__hash_node<string_view, void*>) = 32bytes per element (224 and 608), which is why the1,280 byte
callocis not attributed to either of them.The two unresolved frames do resolve against the profile's own
MAPPED_LIBRARIES, and on this hostthey are glibc's atexit registration:
libc.so.6is mapped at0x74e6aae00000, and the offsets0x470bd/0x47231land in__new_exitfnand__cxa_atexit+65. The callee at0x470b8iscalloc(1, 0x410), i.e. one 1,040 bytestruct exit_function_list, which jemalloc rounds up to its1,280 byte size class. That is allocated once, when the first function-local static with a
non-trivial destructor inside the parser registers it. The same two frames below
mainin that stackresolve to
__libc_start_main, which is how the offsets were checked. The exact figure is a propertyof this glibc, so the profiler is right to leave it unnamed; the point that matters here is only that
it is one-time.
Workflow [PR]
Sync PR [sync-upstream/pr/115739]
Version info
26.8.1.1850(included in26.8and later)