Skip to content

Commit af88a11

Browse files
Backport #87614 to 25.9: Small fixes for jemalloc profiler
1 parent c435106 commit af88a11

File tree

10 files changed

+37
-39
lines changed

10 files changed

+37
-39
lines changed

docs/en/operations/allocation-profiling.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,6 @@ SYSTEM JEMALLOC FLUSH PROFILE
189189

190190
It will return the location of the flushed profile.
191191

192-
You can also specify a different prefix for the file in the query:
193-
```sql
194-
SYSTEM JEMALLOC FLUSH PROFILE TO '/tmp/my_own_prefix'
195-
```
196-
197192
</TabItem>
198193
<TabItem value="keeper" label="Keeper">
199194

programs/local/LocalServer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <Common/ThreadPool.h>
3636
#include <Common/CurrentMetrics.h>
3737
#include <Common/NamedCollections/NamedCollectionsFactory.h>
38+
#include <Common/Jemalloc.h>
3839
#include <Interpreters/Cache/FileCacheFactory.h>
3940
#include <Loggers/OwnFormattingChannel.h>
4041
#include <Loggers/OwnPatternFormatter.h>
@@ -134,6 +135,10 @@ namespace ServerSetting
134135
extern const ServerSettingsUInt64 max_format_parsing_thread_pool_free_size;
135136
extern const ServerSettingsUInt64 format_parsing_thread_pool_queue_size;
136137
extern const ServerSettingsString allowed_disks_for_table_engines;
138+
extern const ServerSettingsBool jemalloc_enable_global_profiler;
139+
extern const ServerSettingsBool jemalloc_collect_global_profile_samples_in_trace_log;
140+
extern const ServerSettingsBool jemalloc_enable_background_threads;
141+
extern const ServerSettingsUInt64 jemalloc_max_background_threads_num;
137142
}
138143

139144
namespace ErrorCodes
@@ -216,6 +221,14 @@ void LocalServer::initialize(Poco::Util::Application & self)
216221

217222
server_settings.loadSettingsFromConfig(config());
218223

224+
#if USE_JEMALLOC
225+
Jemalloc::setup(
226+
server_settings[ServerSetting::jemalloc_enable_global_profiler],
227+
server_settings[ServerSetting::jemalloc_enable_background_threads],
228+
server_settings[ServerSetting::jemalloc_max_background_threads_num],
229+
server_settings[ServerSetting::jemalloc_collect_global_profile_samples_in_trace_log]);
230+
#endif
231+
219232
GlobalThreadPool::initialize(
220233
server_settings[ServerSetting::max_thread_pool_size],
221234
server_settings[ServerSetting::max_thread_pool_free_size],

src/Core/ServerSettings.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,9 +1122,12 @@ The policy on how to perform a scheduling of CPU slots specified by `concurrent_
11221122
DECLARE(Bool, abort_on_logical_error, false, R"(Crash the server on LOGICAL_ERROR exceptions. Only for experts.)", 0) \
11231123
DECLARE(UInt64, jemalloc_flush_profile_interval_bytes, 0, R"(Flushing jemalloc profile will be done after global peak memory usage increased by jemalloc_flush_profile_interval_bytes)", 0) \
11241124
DECLARE(Bool, jemalloc_flush_profile_on_memory_exceeded, 0, R"(Flushing jemalloc profile will be done on total memory exceeded errors)", 0) \
1125-
DECLARE(Bool, jemalloc_enable_global_profiler, 0, R"(Enable jemalloc profiler)", 0) \
1126-
DECLARE(Bool, jemalloc_collect_global_profile_samples_in_trace_log, 0, R"(Store jemalloc sampled allocations in system.trace_log)", 0) \
1127-
DECLARE(Bool, jemalloc_enable_background_threads, 1, R"(Enable jemalloc background threads)", 0) \
1125+
DECLARE(Bool, jemalloc_enable_global_profiler, 0, R"(Enable jemalloc's allocation profiler for all threads. Jemalloc will sample allocations and all deallocations for sampled allocations.
1126+
Profiles can be flushed using SYSTEM JEMALLOC FLUSH PROFILE which can be used for allocation analysis.
1127+
Samples can also be stored in system.trace_log using config jemalloc_collect_global_profile_samples_in_trace_log or with query setting jemalloc_collect_profile_samples_in_trace_log.
1128+
See [Allocation Profiling](/operations/allocation-profiling))", 0) \
1129+
DECLARE(Bool, jemalloc_collect_global_profile_samples_in_trace_log, 0, R"(Store jemalloc's sampled allocations in system.trace_log)", 0) \
1130+
DECLARE(Bool, jemalloc_enable_background_threads, 1, R"(Enable jemalloc background threads. Jemalloc uses background threads to cleanup unused memory pages. Disabling it could lead to performance degradation.)", 0) \
11281131
DECLARE(UInt64, jemalloc_max_background_threads_num, 0, R"(Maximum amount of jemalloc background threads to create, set to 0 to use jemalloc's default value)", 0) \
11291132
DECLARE(NonZeroUInt64, threadpool_local_fs_reader_pool_size, 100, R"(The number of threads in the thread pool for reading from local filesystem when `local_filesystem_read_method = 'pread_threadpool'`.)", 0) \
11301133
DECLARE(UInt64, threadpool_local_fs_reader_queue_size, 1000000, R"(The maximum number of jobs that can be scheduled on the thread pool for reading from local filesystem.)", 0) \
@@ -1153,7 +1156,7 @@ The policy on how to perform a scheduling of CPU slots specified by `concurrent_
11531156
Requires CAP_SYS_NICE capability, otherwise no-op.
11541157
11551158
Possible values: -20 to 19.
1156-
)", 0) \
1159+
)", 0)
11571160

11581161
// clang-format on
11591162

src/Core/Settings.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6886,10 +6886,12 @@ Possible values:
68866886
- 1 - For `Date32` the result is always `Date`. For `DateTime64` the result is `DateTime` for time units `second` and higher.
68876887
)", 0) \
68886888
DECLARE(Bool, jemalloc_enable_profiler, false, R"(
6889-
Enable jemalloc profiler.
6890-
)", 0) \
6889+
Enable jemalloc profiler for the query. Jemalloc will sample allocations and all deallocations for sampled allocations.
6890+
Profiles can be flushed using SYSTEM JEMALLOC FLUSH PROFILE which can be used for allocation analysis.
6891+
Samples can also be stored in system.trace_log using config jemalloc_collect_global_profile_samples_in_trace_log or with query setting jemalloc_collect_profile_samples_in_trace_log.
6892+
See [Allocation Profiling](/operations/allocation-profiling))", 0) \
68916893
DECLARE(Bool, jemalloc_collect_profile_samples_in_trace_log, false, R"(
6892-
Collect jemalloc profile samples in trace log.
6894+
Collect jemalloc allocation and deallocation samples in trace log.
68936895
)", 0) \
68946896
DECLARE_WITH_ALIAS(Int32, os_threads_nice_value_query, 0, R"(
68956897
Linux nice value for query processing threads. Lower values mean higher CPU priority.

src/Interpreters/InterpreterSystemQuery.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,7 @@ BlockIO InterpreterSystemQuery::execute()
915915
case Type::JEMALLOC_FLUSH_PROFILE:
916916
{
917917
getContext()->checkAccess(AccessType::SYSTEM_JEMALLOC);
918-
Jemalloc::flushProfile(query.jemalloc_profile_path.empty() ? "/tmp/jemalloc_clickhouse" : query.jemalloc_profile_path);
919-
auto filename = Jemalloc::getLastFlushProfileForThread();
918+
auto filename = Jemalloc::flushProfile("/tmp/jemalloc_clickhouse");
920919
auto col = ColumnString::create();
921920
col->insertData(filename.data(), filename.size());
922921
Columns columns;

src/Parsers/ASTSystemQuery.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -468,17 +468,6 @@ void ASTSystemQuery::formatImpl(WriteBuffer & ostr, const FormatSettings & setti
468468
}
469469
break;
470470
}
471-
case Type::JEMALLOC_FLUSH_PROFILE:
472-
{
473-
#if USE_JEMALLOC
474-
if (!jemalloc_profile_path.empty())
475-
{
476-
print_keyword(" TO ");
477-
ostr << quoteString(jemalloc_profile_path);
478-
}
479-
#endif
480-
break;
481-
}
482471
case Type::KILL:
483472
case Type::SHUTDOWN:
484473
case Type::DROP_DNS_CACHE:
@@ -498,6 +487,7 @@ void ASTSystemQuery::formatImpl(WriteBuffer & ostr, const FormatSettings & setti
498487
case Type::RESET_COVERAGE:
499488
case Type::RESTART_REPLICAS:
500489
case Type::JEMALLOC_PURGE:
490+
case Type::JEMALLOC_FLUSH_PROFILE:
501491
case Type::JEMALLOC_ENABLE_PROFILE:
502492
case Type::JEMALLOC_DISABLE_PROFILE:
503493
case Type::SYNC_TRANSACTION_LOG:

src/Parsers/ASTSystemQuery.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#pragma once
22

3-
#include "config.h"
4-
53
#include <Parsers/ASTQueryWithOnCluster.h>
64
#include <Parsers/IAST.h>
75
#include <Parsers/SyncReplicaMode.h>
@@ -177,10 +175,6 @@ class ASTSystemQuery : public IAST, public ASTQueryWithOnCluster
177175

178176
ServerType server_type;
179177

180-
#if USE_JEMALLOC
181-
String jemalloc_profile_path;
182-
#endif
183-
184178
/// For SYSTEM TEST VIEW <name> (SET FAKE TIME <time> | UNSET FAKE TIME).
185179
/// Unix time.
186180
std::optional<Int64> fake_time_for_view;

src/Parsers/ParserSystemQuery.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -753,14 +753,6 @@ bool ParserSystemQuery::parseImpl(IParser::Pos & pos, ASTPtr & node, Expected &
753753
if (!parseQueryWithOnCluster(res, pos, expected))
754754
return false;
755755
}
756-
757-
if (ParserKeyword{Keyword::TO}.ignore(pos, expected))
758-
{
759-
ASTPtr ast;
760-
if (ParserStringLiteral{}.parse(pos, ast, expected))
761-
res->jemalloc_profile_path = ast->as<ASTLiteral &>().value.safeGet<String>();
762-
}
763-
764756
break;
765757
}
766758
#endif

tests/queries/0_stateless/03631_local_jemalloc_flush_profile.reference

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
# Tags: no-tsan, no-asan, no-msan, no-ubsan, no-fasttest
3+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
# NOTE: jemalloc is disabled under sanitizers
5+
6+
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
7+
# shellcheck source=../shell_config.sh
8+
. "$CUR_DIR"/../shell_config.sh
9+
10+
$CLICKHOUSE_LOCAL --query "SYSTEM JEMALLOC FLUSH PROFILE" -- --enable_jemalloc_global_profiler | grep -q ".heap" || echo "Didn't return profile path"

0 commit comments

Comments
 (0)