Skip to content

Add total_memory_profiler_sample_probability setting alias and enable the memory sample profiler on CI - #88405

Draft
azat wants to merge 27 commits into
ClickHouse:masterfrom
azat:ci-memory-samples
Draft

azat wants to merge 27 commits into
ClickHouse:masterfrom
azat:ci-memory-samples

Conversation

@azat

@azat azat commented Oct 12, 2025 •

Copy link
Copy Markdown
Member

Two changes related to the memory sample profiler:

  1. Adds a total_memory_profiler_sample_probability server setting as an alias of the existing total_memory_tracker_sample_probability, for naming consistency with the per-query memory_profiler_sample_probability setting and the other total_memory_profiler_* server settings. Server settings can now be loaded from the config via either an alias or the canonical name.
  2. Enables the memory sample profiler on CI (a low global total_memory_tracker_sample_probability plus a per-query memory_profiler_sample_probability in the test profile) so that MemorySample events are collected across the whole test suite, not just a couple of tests.

Note on the merge with master: the original change to MemoryTracker::allocImpl/free (so that the global total_memory_profiler_* settings no longer override per-query memory_profiler_sample_* settings) has been dropped, because it was superseded by #101267, which reworked sampling into getResolvedSampleConfig together with the .changed gating in configureMemoryTrackerFromSettings and landed on master. After merging master, src/Common/MemoryTracker.cpp is identical to master, so this PR no longer changes the memory profiler logic.

Related: #101267

Changelog category (leave one):

  • Improvement

Changelog entry:

Added a total_memory_profiler_sample_probability server setting as an alias of total_memory_tracker_sample_probability.

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ef440ae]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_debug, parallel) FAIL
00980_merge_alter_settings FAIL cidb
Stress test (amd_tsan) FAIL
Logical error: '(isConst() || isSparse() || isReplicated() || rhs.isConst() || rhs.isSparse() || rhs.isReplicated()) ? getDataType() == rhs.getDataType() : typeid(*this) == typeid(rhs)' (STID: 2508-30f6) FAIL cidb
Stress test (arm_msan) FAIL
Logical error: Bad cast from type A to B (STID: 3520-4525) FAIL cidb

AI Review

Summary

This PR adds the total_memory_profiler_sample_probability server-setting alias, wires server-setting aliases into config loading and system.documentation, and enables suite-wide memory-sample collection in CI. The code and test updates look functionally sound, but the official server-settings reference still does not surface the new alias.

Findings

⚠️ Majors

  • [src/Core/ServerSettings.cpp:1078] The runtime help surfaces now know about total_memory_profiler_sample_probability, but the generated English server-settings reference still documents only total_memory_tracker_sample_probability in docs/reference/settings/server-settings/settings/total-memory.mdx. Because this PR also switches the shipped config examples to the alias, users following the website docs can no longer search the reference for the name they see in config. Update the server-settings docs generation or regenerated English page so it emits an alias note for this setting, like session-setting aliases already do.
Final Verdict

Functionally this looks correct, but I would document the new alias on the official server-settings reference before merge.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.80% +0.00%

Changed lines: Changed C/C++ lines covered: 92/95 (96.84%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 12, 2025
@azat
azat force-pushed the ci-memory-samples branch from b837e7a to aab22c1 Compare October 12, 2025 17:08
@serxa serxa self-assigned this Oct 13, 2025

@serxa serxa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but failed tests are related

@azat
azat marked this pull request as draft October 13, 2025 21:42
@clickhouse-gh

clickhouse-gh Bot commented Dec 16, 2025

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@azat
azat force-pushed the ci-memory-samples branch from aab22c1 to bed3b35 Compare January 2, 2026 18:39
@azat
azat marked this pull request as ready for review January 2, 2026 18:39
@azat
azat force-pushed the ci-memory-samples branch from 217bbab to 4427419 Compare January 2, 2026 20:16
@azat
azat marked this pull request as draft January 3, 2026 07:09
@azat

azat commented Jan 3, 2026

Copy link
Copy Markdown
Member Author

I will tune the limits to increase number of MemorySample events, since previously we have more events just for a few tests than we have now for all tests

azat added 4 commits January 5, 2026 10:10
Get sample probability from the first active memory tracker, otherwise
settings for total memory tracker may override the settings for the
nested tracker.

I.e. consider total_memory_profiler_sample_min_allocation_size=4MiB and the following query:

  select 1 settings max_untracked_memory=0, memory_profiler_sample_probability=1

It should record some MemorySample, but, because of
max_untracked_memory=0 (which uses sample probability calculation inside
MemoryTracker::allocImpl()/freeImpl(), w/o it it uses correct
getSampleProbability()) w/o this patch it will fallback to
total_memory_profiler_sample_min_allocation_size, and will not record
anything.
@azat
azat force-pushed the ci-memory-samples branch from 4300de2 to e8d4167 Compare January 5, 2026 09:27
@azat azat changed the title ci: gather memory samples Avoid total_memory_profiler_* overrides per-query memory_profiler_sample_* settings and enable memory sample profiler on CI Jan 5, 2026
@azat
azat force-pushed the ci-memory-samples branch from e8d4167 to ace00c6 Compare January 5, 2026 10:04
@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Jan 5, 2026
@azat
azat force-pushed the ci-memory-samples branch from ace00c6 to 93d37cb Compare January 5, 2026 10:07
MemorySample are more useful then Memory in trace_log
@azat
azat force-pushed the ci-memory-samples branch from 93d37cb to a188f50 Compare January 5, 2026 10:25
@azat
azat marked this pull request as ready for review January 5, 2026 11:38
Comment thread src/Core/ServerSettings.cpp Resolved
alexey-milovidov and others added 3 commits July 6, 2026 08:58
The `check-settings-style` script parsed only the `DECLARE` and
`DECLARE_WITH_ALIAS` declarators, but `LIST_OF_SERVER_SETTINGS_*`,
`FORMAT_FACTORY_SETTINGS` and `LIST_OF_COORDINATION_SETTINGS` name the
with-alias declarator `ALIAS` instead. The first actual `ALIAS(...)`
usage - the `total_memory_profiler_sample_probability` alias of
`total_memory_tracker_sample_probability` - made the `cpp` style check
fail with `Could not find setting (maybe obsolete?)
total_memory_tracker_sample_probability`, because the canonical setting
name was never registered as a declaration.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Prove the `total_memory_profiler_sample_probability` /
`total_memory_tracker_sample_probability` server setting alias contract
directly at the `ServerSettings::loadSettingsFromConfig` level:

- loading via the alias populates the canonical setting;
- loading via the canonical name still works (regression guard for the
  new alias-loading loop);
- a config that specifies both the canonical name and its alias is
  rejected.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Core/ServerSettings.cpp Resolved
Addresses the AI review Major on
ClickHouse#88405: the new
`total_memory_profiler_sample_probability` alias was accepted by config
parsing but was not rendered on the public help surface that documents
setting aliases. `ServerSettings` was the only settings collection that
did not register its aliases in `system.documentation`, because it lacked
the `getAllAliasNames` / `resolveName` API that `Settings` and
`MergeTreeSettings` expose.

Add `getAllAliasNames` and `resolveName` to `ServerSettings` (mirroring
the other two collections) and call `addSettingAliases` for server
settings in `StorageSystemDocumentation`, so the alias now appears as
`Alias of \`total_memory_tracker_sample_probability\`.`. Extend the
focused gtest to assert the alias is enumerable and resolvable.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

Pushed f5c7285 to address the AI review Major about the alias not being discoverable.

The alias total_memory_profiler_sample_probability is now rendered on the public help surface that documents setting aliases: ServerSettings was the only settings collection that did not register its aliases in system.documentation. I added getAllAliasNames / resolveName to ServerSettings (mirroring Settings and MergeTreeSettings) and enabled addSettingAliases for server settings, so system.documentation now lists it as Alias of \total_memory_tracker_sample_probability`.`. The focused gtest was extended to assert the alias is enumerable and resolvable.

I intentionally did not touch system.server_settings: unlike system.settings (which has an alias_for column), system.server_settings has no such column, so surfacing the alias there would mean adding a new user-facing column to that system table. That is a deliberate schema decision, so I left it out of scope here. @azat @alexey-milovidov — happy to add the alias_for column + alias rows to system.server_settings too if you'd like it in this PR, otherwise the documentation surface already covers discoverability.

Enabling `memory_profiler_sample_probability` in the default test profile
(this PR's `tests/config/users.d/memory_profiler.xml`) makes ordinary queries
emit `MemorySample` rows under their own `query_id`. The assertions in
`00974_query_profiler.sql` read `system.trace_log` filtering only by `query_id`
and `symbol`, so a `MemorySample` stack that happens to contain `%Source%`
(likely during a `numbers_mt` scan) could satisfy the check even if the
real/CPU query profiler stopped emitting samples, silently weakening the
regression coverage.

Constrain each assertion to the `trace_type` it is meant to exercise
(`Real` for the real-time profiler checks, `CPU` for the CPU profiler check),
so only samples from the intended profiler can satisfy it. The expected
output is unchanged.

Addresses the unresolved review comment on
tests/config/users.d/memory_profiler.xml.
alexey-milovidov and others added 4 commits July 21, 2026 15:47
…e='Real'

The new default `memory_profiler_sample_probability=0.001` causes ordinary
queries to emit `MemorySample` rows under their own query_id. The test forces
the real-time profiler but only asserted `count(*) > 1` on `system.trace_log`
filtered by query_id, so the assertion could pass on sampled allocations alone
even if the real-time profiler stopped emitting traces. Constrain the check to
`trace_type = 'Real'`, matching the earlier fix to 00974_query_profiler.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…race_log

Two fast test runs on 2e80898 timed out (60s per-test limit) in
01473_event_time_microseconds and 03096_order_by_system_tables, both of
which start with `SYSTEM FLUSH LOGS` and wait for the shared
`system.trace_log` backlog. The suite-wide
`memory_profiler_sample_probability = 0.001` samples every memory
tracker event, and with `max_untracked_memory = 1Mi` small allocations
reach the tracker as ~1MiB batches, so the whole test suite floods
`trace_log` with `MemorySample` rows and the flush exceeds the fast
test timeout. CIDB shows these tests time out only ~monthly on random
PRs, but three times across two runs on this PR - so it is related.

Add `memory_profiler_sample_min_allocation_size = 4Mi` to the test
profile, matching `total_memory_profiler_sample_min_allocation_size`
in `config.d/memory_profiler.yaml`, so only genuinely large
allocations are sampled. All tests that rely on sampling small
allocations (01526, 02497, 02818, 04411) set the threshold explicitly
and are unaffected; 01092 allocates far above 4MiB with probability 1.

Update `01293_show_settings.reference` for the new changed setting.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=88405&sha=2e80898b66ca81e200389e23a0f059eda107f608&name_0=PR&name_1=Fast%20test
PR: ClickHouse#88405

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

The last two Fast test runs on 2e80898b66c timed out (60 s per-test limit) in 01473_event_time_microseconds and 03096_order_by_system_tables — both start with SYSTEM FLUSH LOGS and wait for the shared system.trace_log backlog. CIDB shows these tests time out only ~monthly on random PRs, but three times across two runs here, so this is caused by the PR: with max_untracked_memory = 1Mi, small allocations reach the memory tracker as ~1 MiB batches, and the suite-wide memory_profiler_sample_probability = 0.001 samples them all, flooding trace_log with MemorySample rows (the same volume problem behind the January TOO_MANY_ROWS failures).

Fixed in 652f894 by adding memory_profiler_sample_min_allocation_size = 4Mi to the test profile, matching total_memory_profiler_sample_min_allocation_size in config.d/memory_profiler.yaml, so only genuinely large allocations are sampled. Tests that rely on sampling small allocations (01526, 02497, 02818, 04411) set the threshold explicitly and are unaffected; 01092_memory_profiler allocates far above 4 MiB with probability 1. Also updated 01293_show_settings.reference for the new changed setting, and merged master (clean, 25 merges behind).

Comment thread programs/server/config.xml Resolved
…ple`

`ServerSettings::loadSettingsFromConfig` rejects a config that contains both
`total_memory_tracker_sample_probability` and its new alias
`total_memory_profiler_sample_probability`. `config.xml` no longer ships the
canonical key, but `config.yaml.example` still did, so a YAML-based deployment
that keeps the shipped main config and adds the alias in an override file would
fail to start. Comment the key out there as well, using the alias name, exactly
as in `config.xml`.

Also cover the merged main-config + override case in
`gtest_server_settings_alias`: the alias coming from an override layer is
loaded, and the canonical name in the main config plus the alias in an override
is still rejected.
…timing-sensitive

The flaky check on this PR reported `00974_query_profiler` failing on the first
`system.trace_log` assertion (`sleep(0.5)` with a `100ms` real-time profiler
period): no `Real` sample was recorded for the query. The test has been flaky in
this way on unrelated pull requests and on master as well.

Lower `query_profiler_real_time_period_ns` to `10ms` for that query, so ~50
ticks are expected during the sleep instead of ~5, and a few missed ticks on a
loaded sanitizer runner no longer make the assertion fail.
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.40% 86.40% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.50% 78.50% +0.00%

Changed lines: Changed C/C++ lines covered: 92/95 (96.84%) · Uncovered code

Full report · Diff report

alexey-milovidov and others added 3 commits August 6, 2026 14:40
The flaky check (amd_asan_ubsan) failed 3 runs of `00974_query_profiler` even
after shortening the profiler period to 10 ms: the `sleep(0.5)` sub-test got
0 `FunctionSleep` samples with `trace_type = 'Real'`. Profiler samples are
sent to the trace pipe with `WriteBufferFromFileDescriptorDiscardOnFailure`
over a non-blocking pipe: when a concurrent sample-heavy query (e.g. another
instance of this test in the flaky check, or the suite-wide memory sample
profiler this PR enables) keeps the pipe full, samples are silently dropped,
and the single sleeping thread produces so few of them that all can be lost
at once, while the sample-heavy `numbers_mt` sub-tests passed in the same
failing runs.

Sleep in 16 threads (one single-row block per thread via `numbers_mt(16)`
with `max_block_size = 1`) instead of one: an order of magnitude more
samples, and the sub-test also survives a single thread failing to create
its profiler timer. The reference output is unchanged. Same fix as in
ClickHouse#113107 (verified there locally
10/10 and 8/8 under a synthetic trace-pipe flood).

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=88405&sha=f5c9438a6df42a63494bba778b56fbe1a3191f10&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29
PR: ClickHouse#88405

Co-Authored-By: Claude Fable 5 <[email protected]>
DECLARE(Seconds, replicated_fetches_http_receive_timeout, 0, R"(HTTP receive timeout for fetch part requests. Inherited from default profile `http_receive_timeout` if not set explicitly.)", 0) \
DECLARE(UInt64, total_memory_profiler_step, 0, R"(Whenever server memory usage becomes larger than every next step in number of bytes the memory profiler will collect the allocating stack trace. Zero means disabled memory profiler. Values lower than a few megabytes will slow down server.)", 0) \
DECLARE(Double, total_memory_tracker_sample_probability, 0, R"(
ALIAS(Double, total_memory_tracker_sample_probability, 0, R"(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ The runtime help surfaces are covered now, but the official server-settings reference is still out of sync with this alias. docs/reference/settings/server-settings/settings/total-memory.mdx still renders only ## total_memory_tracker_sample_probability, while session-setting aliases get a generated **Aliases**: line. After this change and the updated shipped config examples, a user who searches the docs for total_memory_profiler_sample_probability still will not find the documented setting page. Please update/regenerate the English server-settings docs so the alias is visible there too.

@clickhouse-gh

clickhouse-gh Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ef440ae6d with master e5c489c41 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 989d4755e; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 690.17 MiB 687.14 MiB -3.02 MiB (-0.44%)

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.

Object file sizes

4 object files changed (+48.35 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Core/ServerSettings.cpp.o 853.82 KiB 900.04 KiB +46.21 KiB (+5.41%)

737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

95 translation units recompiled, 745 s compile time in total, 94 of them have a recent master baseline.

Job report

@abashkeev abashkeev added the comp-settings Settings & configuration infrastructure (server/user/query settings, profiles). label Sep 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

2 similar comments
@clickhouse-gh

clickhouse-gh Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Dear @azat, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-settings Settings & configuration infrastructure (server/user/query settings, profiles). pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants