この章で説明する関数を使用すると、クエリのプロファイリングのために ELF と DWARF を調査できます。
イントロスペクション関数を正しく動作させるには:
-
clickhouse-common-static-dbgパッケージをインストールします。 -
allow_introspection_functions 設定を 1 に設定します。
セキュリティ上の理由から、イントロスペクション関数はデフォルトで無効になっています。
ClickHouse はプロファイラレポートを trace_log システムテーブルに保存します。テーブルとプロファイラが正しく設定されていることを確認してください。
addressToLine
導入バージョン: v20.1.0
ClickHouseサーバープロセス内の仮想メモリアドレスを、ClickHouse のソースコード内のファイル名と行番号に変換します。
このイントロスペクション関数を有効にするには:
clickhouse-common-static-dbgパッケージをインストールします。- 設定
allow_introspection_functionsを1に設定します。
構文
addressToLine(address_of_binary_instruction)引数
address_of_binary_instruction— 実行中のプロセス内にある命令のアドレス。UInt64
戻り値
コロン区切りのソースコードのファイル名と行番号を返します。たとえば、/build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:199 のようになります。デバッグ情報が見つからない場合はバイナリ名を返し、アドレスが無効な場合は空文字列を返します。String
例
trace_log システムテーブルから最初の文字列を取得する
SET allow_introspection_functions=1;
SELECT * FROM system.trace_log LIMIT 1 FORMAT Vertical;-- The `trace` field contains the stack trace at the moment of sampling.
Row 1:
──────
event_date: 2019-11-19
event_time: 2019-11-19 18:57:23
revision: 54429
timer_type: Real
thread_number: 48
query_id: 421b6855-1858-45a5-8f37-f383409d6d72
trace: [140658411141617,94784174532828,94784076370703,94784076372094,94784076361020,94784175007680,140658411116251,140658403895439]単一アドレスのソースコードのファイル名と行番号を取得する
SET allow_introspection_functions=1;
SELECT addressToLine(94784076370703) FORMAT Vertical;Row 1:
──────
addressToLine(94784076370703): /build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:199スタックトレース全体に関数を適用する
-- The arrayMap function in this example processing each individual element of the trace array by the addressToLine function.
-- The result of this processing is seen in the trace_source_code_lines column of output.
SELECT
arrayStringConcat(arrayMap(x -> addressToLine(x), trace), '\n') AS trace_source_code_lines
FROM system.trace_log
LIMIT 1
FORMAT Vertical;Row 1:
──────
trace_source_code_lines: /lib/x86_64-linux-gnu/libpthread-2.27.so
/usr/lib/debug/usr/bin/clickhouse
/build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:199
/build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.h:155
/usr/include/c++/9/bits/atomic_base.h:551
/usr/lib/debug/usr/bin/clickhouse
/lib/x86_64-linux-gnu/libpthread-2.27.so
/build/glibc-OTsEL5/glibc-2.27/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:97addressToLineWithInlines
導入バージョン: v22.2.0
addressToLine に似ていますが、すべてのインライン関数を含む Array を返します。
このため、addressToLine より低速です。
このイントロスペクション関数を有効にするには:
clickhouse-common-static-dbgパッケージをインストールします。- 設定
allow_introspection_functionsの値を1に設定します。
構文
addressToLineWithInlines(address_of_binary_instruction)引数
address_of_binary_instruction— 実行中のプロセス内の命令のアドレス。UInt64
戻り値
最初の要素が、コロンで区切られたソースコードのファイル名と行番号である配列を返します。2 番目、3 番目以降の要素には、インライン関数のソースコードのファイル名、行番号、および関数名が含まれます。デバッグ情報が見つからなかった場合は、バイナリ名のみを要素に持つ配列が返されます。アドレスが無効な場合は、空の配列が返されます。Array(String)
例
アドレスに関数を適用する
SET allow_introspection_functions=1;
SELECT addressToLineWithInlines(531055181::UInt64);┌─addressToLineWithInlines(CAST('531055181', 'UInt64'))────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ['./src/Functions/addressToLineWithInlines.cpp:98','./build_normal_debug/./src/Functions/addressToLineWithInlines.cpp:176:DB::(anonymous namespace)::FunctionAddressToLineWithInlines::implCached(unsigned long) const'] │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘スタックトレース全体に関数を適用する
SET allow_introspection_functions=1;
-- The arrayJoin function will split array to rows
SELECT
ta, addressToLineWithInlines(arrayJoin(trace) AS ta)
FROM system.trace_log
WHERE
query_id = '5e173544-2020-45de-b645-5deebe2aae54';┌────────ta─┬─addressToLineWithInlines(arrayJoin(trace))───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ 365497529 │ ['./build_normal_debug/./contrib/libcxx/include/string_view:252'] │
│ 365593602 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:191'] │
│ 365593866 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:0'] │
│ 365592528 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:0'] │
│ 365591003 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:477'] │
│ 365590479 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:442'] │
│ 365590600 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:457'] │
│ 365598941 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:0'] │
│ 365607098 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:0'] │
│ 365590571 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:451'] │
│ 365598941 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:0'] │
│ 365607098 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:0'] │
│ 365590571 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:451'] │
│ 365598941 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:0'] │
│ 365607098 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:0'] │
│ 365590571 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:451'] │
│ 365598941 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:0'] │
│ 365597289 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:807'] │
│ 365599840 │ ['./build_normal_debug/./src/Common/Dwarf.cpp:1118'] │
│ 531058145 │ ['./build_normal_debug/./src/Functions/addressToLineWithInlines.cpp:152'] │
│ 531055181 │ ['./src/Functions/addressToLineWithInlines.cpp:98','./build_normal_debug/./src/Functions/addressToLineWithInlines.cpp:176:DB::(anonymous namespace)::FunctionAddressToLineWithInlines::implCached(unsigned long) const'] │
│ 422333613 │ ['./build_normal_debug/./src/Functions/IFunctionAdaptors.h:21'] │
│ 586866022 │ ['./build_normal_debug/./src/Functions/IFunction.cpp:216'] │
│ 586869053 │ ['./build_normal_debug/./src/Functions/IFunction.cpp:264'] │
│ 586873237 │ ['./build_normal_debug/./src/Functions/IFunction.cpp:334'] │
│ 597901620 │ ['./build_normal_debug/./src/Interpreters/ExpressionActions.cpp:601'] │
│ 597898534 │ ['./build_normal_debug/./src/Interpreters/ExpressionActions.cpp:718'] │
│ 630442912 │ ['./build_normal_debug/./src/Processors/Transforms/ExpressionTransform.cpp:23'] │
│ 546354050 │ ['./build_normal_debug/./src/Processors/ISimpleTransform.h:38'] │
│ 626026993 │ ['./build_normal_debug/./src/Processors/ISimpleTransform.cpp:89'] │
│ 626294022 │ ['./build_normal_debug/./src/Processors/Executors/ExecutionThreadContext.cpp:45'] │
│ 626293730 │ ['./build_normal_debug/./src/Processors/Executors/ExecutionThreadContext.cpp:63'] │
│ 626169525 │ ['./build_normal_debug/./src/Processors/Executors/PipelineExecutor.cpp:213'] │
│ 626170308 │ ['./build_normal_debug/./src/Processors/Executors/PipelineExecutor.cpp:178'] │
│ 626166348 │ ['./build_normal_debug/./src/Processors/Executors/PipelineExecutor.cpp:329'] │
│ 626163461 │ ['./build_normal_debug/./src/Processors/Executors/PipelineExecutor.cpp:84'] │
│ 626323536 │ ['./build_normal_debug/./src/Processors/Executors/PullingAsyncPipelineExecutor.cpp:85'] │
│ 626323277 │ ['./build_normal_debug/./src/Processors/Executors/PullingAsyncPipelineExecutor.cpp:112'] │
│ 626323133 │ ['./build_normal_debug/./contrib/libcxx/include/type_traits:3682'] │
│ 626323041 │ ['./build_normal_debug/./contrib/libcxx/include/tuple:1415'] │
└───────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘addressToSymbol
導入バージョン: v20.1.0
ClickHouseサーバープロセス内の仮想メモリアドレスを、ClickHouseのオブジェクトファイル内のシンボルに変換します。
構文
addressToSymbol(address_of_binary_instruction)引数
address_of_binary_instruction— 実行中のプロセス内の命令のアドレス。UInt64
戻り値
ClickHouse のオブジェクトファイル内のシンボルを返します。アドレスが無効な場合は空文字列を返します。String
例
trace_log システムテーブルから最初の文字列を取得する
SET allow_introspection_functions=1;
SELECT * FROM system.trace_log LIMIT 1 FORMAT Vertical;-- The `trace` field contains the stack trace at the moment of sampling.
Row 1:
──────
event_date: 2019-11-20
event_time: 2019-11-20 16:57:59
revision: 54429
timer_type: Real
thread_number: 48
query_id: 724028bf-f550-45aa-910d-2af6212b94ac
trace: [94138803686098,94138815010911,94138815096522,94138815101224,94138815102091,94138814222988,94138806823642,94138814457211,94138806823642,94138814457211,94138806823642,94138806795179,94138806796144,94138753770094,94138753771646,94138753760572,94138852407232,140399185266395,140399178045583]単一アドレスに対応するシンボルを取得する
SET allow_introspection_functions=1;
SELECT addressToSymbol(94138803686098) FORMAT Vertical;Row 1:
──────
addressToSymbol(94138803686098): _ZNK2DB24IAggregateFunctionHelperINS_20AggregateFunctionSumImmNS_24AggregateFunctionSumDataImEEEEE19addBatchSinglePlaceEmPcPPKNS_7IColumnEPNS_5ArenaEスタックトレース全体に関数を適用する
SET allow_introspection_functions=1;
-- The arrayMap function allows to process each individual element of the trace array by the addressToSymbols function.
-- The result of this processing is shown in the trace_symbols column of output.
SELECT
arrayStringConcat(arrayMap(x -> addressToSymbol(x), trace), '\n') AS trace_symbols
FROM system.trace_log
LIMIT 1
FORMAT Vertical;Row 1:
──────
trace_symbols: _ZNK2DB24IAggregateFunctionHelperINS_20AggregateFunctionSumImmNS_24AggregateFunctionSumDataImEEEEE19addBatchSinglePlaceEmPcPPKNS_7IColumnEPNS_5ArenaE
_ZNK2DB10Aggregator21executeWithoutKeyImplERPcmPNS0_28AggregateFunctionInstructionEPNS_5ArenaE
_ZN2DB10Aggregator14executeOnBlockESt6vectorIN3COWINS_7IColumnEE13immutable_ptrIS3_EESaIS6_EEmRNS_22AggregatedDataVariantsERS1_IPKS3_SaISC_EERS1_ISE_SaISE_EERb
_ZN2DB10Aggregator14executeOnBlockERKNS_5BlockERNS_22AggregatedDataVariantsERSt6vectorIPKNS_7IColumnESaIS9_EERS6_ISB_SaISB_EERb
_ZN2DB10Aggregator7executeERKSt10shared_ptrINS_17IBlockInputStreamEERNS_22AggregatedDataVariantsE
_ZN2DB27AggregatingBlockInputStream8readImplEv
_ZN2DB17IBlockInputStream4readEv
_ZN2DB26ExpressionBlockInputStream8readImplEv
_ZN2DB17IBlockInputStream4readEv
_ZN2DB26ExpressionBlockInputStream8readImplEv
_ZN2DB17IBlockInputStream4readEv
_ZN2DB28AsynchronousBlockInputStream9calculateEv
_ZNSt17_Function_handlerIFvvEZN2DB28AsynchronousBlockInputStream4nextEvEUlvE_E9_M_invokeERKSt9_Any_data
_ZN14ThreadPoolImplI20ThreadFromGlobalPoolE6workerESt14_List_iteratorIS0_E
_ZZN20ThreadFromGlobalPoolC4IZN14ThreadPoolImplIS_E12scheduleImplIvEET_St8functionIFvvEEiSt8optionalImEEUlvE1_JEEEOS4_DpOT0_ENKUlvE_clEv
_ZN14ThreadPoolImplISt6threadE6workerESt14_List_iteratorIS0_E
execute_native_thread_routine
start_thread
clonedemangle
導入バージョン: v20.1.0
シンボルを C++ の関数名に変換します。
このシンボルは通常、関数 addressToSymbol が返します。
構文
demangle(symbol)引数
symbol— オブジェクトファイルのシンボル。String
戻り値
C++ 関数名を返します。シンボルが無効な場合は空文字列を返します。String
例
trace_log システムテーブルから最初の文字列を取得する
SELECT * FROM system.trace_log LIMIT 1 FORMAT Vertical;-- The `trace` field contains the stack trace at the moment of sampling.
Row 1:
──────
event_date: 2019-11-20
event_time: 2019-11-20 16:57:59
revision: 54429
timer_type: Real
thread_number: 48
query_id: 724028bf-f550-45aa-910d-2af6212b94ac
trace: [94138803686098,94138815010911,94138815096522,94138815101224,94138815102091,94138814222988,94138806823642,94138814457211,94138806823642,94138814457211,94138806823642,94138806795179,94138806796144,94138753770094,94138753771646,94138753760572,94138852407232,140399185266395,140399178045583]単一アドレスの関数名を取得する
SET allow_introspection_functions=1;
SELECT demangle(addressToSymbol(94138803686098)) FORMAT Vertical;Row 1:
──────
demangle(addressToSymbol(94138803686098)): DB::IAggregateFunctionHelper<DB::AggregateFunctionSum<unsigned long, unsigned long, DB::AggregateFunctionSumData<unsigned long> > >::addBatchSinglePlace(unsigned long, char*, DB::IColumn const**, DB::Arena*) constスタックトレース全体に関数を適用する
SET allow_introspection_functions=1;
-- The arrayMap function allows to process each individual element of the trace array by the demangle function.
-- The result of this processing is shown in the trace_functions column of output.
SELECT
arrayStringConcat(arrayMap(x -> demangle(addressToSymbol(x)), trace), '\n') AS trace_functions
FROM system.trace_log
LIMIT 1
FORMAT Vertical;Row 1:
──────
trace_functions: DB::IAggregateFunctionHelper<DB::AggregateFunctionSum<unsigned long, unsigned long, DB::AggregateFunctionSumData<unsigned long> > >::addBatchSinglePlace(unsigned long, char*, DB::IColumn const**, DB::Arena*) const
DB::Aggregator::executeWithoutKeyImpl(char*&, unsigned long, DB::Aggregator::AggregateFunctionInstruction*, DB::Arena*) const
DB::Aggregator::executeOnBlock(...)
DB::Aggregator::executeOnBlock(DB::Block const&, ...)
DB::Aggregator::execute(std::shared_ptr<DB::IBlockInputStream> const&, DB::AggregatedDataVariants&)
DB::AggregatingBlockInputStream::readImpl()
DB::IBlockInputStream::read()
DB::ExpressionBlockInputStream::readImpl()
DB::IBlockInputStream::read()
DB::ExpressionBlockInputStream::readImpl()
DB::IBlockInputStream::read()
DB::AsynchronousBlockInputStream::calculate()
std::_Function_handler<void (), DB::AsynchronousBlockInputStream::next()::{lambda()#1}>::_M_invoke(std::_Any_data const&)
ThreadPoolImpl<ThreadFromGlobalPool>::worker(std::_List_iterator<ThreadFromGlobalPool>)
ThreadFromGlobalPool::ThreadFromGlobalPool<ThreadPoolImpl<ThreadFromGlobalPool>::scheduleImpl<void>(std::function<void ()>, int, std::optional<unsigned long>)::{lambda()#3}>(ThreadPoolImpl<ThreadFromGlobalPool>::scheduleImpl<void>(std::function<void ()>, int, std::optional<unsigned long>)::{lambda()#3}&&)::{lambda()#1}::operator()() const
ThreadPoolImpl<std::thread>::worker(std::_List_iterator<std::thread>)
execute_native_thread_routine
start_thread
cloneisMergeTreePartCoveredBy
導入バージョン: v25.6.0
第1引数のパーツが第2引数のパーツに包含されているかどうかを確認する関数です。
構文
isMergeTreePartCoveredBy(nested_part, covering_part)引数
戻り値
カバーしている場合は 1、そうでない場合は 0 を返します。UInt8
例
基本的な例
WITH 'all_12_25_7_4' AS lhs, 'all_7_100_10_20' AS rhs
SELECT isMergeTreePartCoveredBy(rhs, lhs), isMergeTreePartCoveredBy(lhs, rhs);┌─isMergeTreePartCoveredBy(rhs, lhs)─┬─isMergeTreePartCoveredBy(lhs, rhs)─┐
│ 0 │ 1 │
└────────────────────────────────────┴────────────────────────────────────┘logTrace
導入バージョン: v20.12.0
サーバーログにトレースログメッセージを出力します。
この関数は定数引数のみを受け付けるため、呼び出しはクエリ分析中に評価され、その結果に置き換えられます。したがって、メッセージはクエリの分析中に一度だけ出力され、処理される
Blockごとに出力されるわけではありません。行数と設定 max_block_size は、ログに出力されるメッセージ数に影響しません。
構文
logTrace(message)引数
message— サーバーログに出力されるメッセージ。const String
戻り値
常に 0 を返します。UInt8
例
基本例
SELECT logTrace('logTrace message');┌─logTrace('logTrace message')─┐
│ 0 │
└──────────────────────────────┘mergeTreePartInfo
導入バージョン: v25.6.0
MergeTree のパーツ名から有用な値を取り出すのに役立つ関数です。
構文
mergeTreePartInfo(part_name)引数
part_name— 展開するパーツの名前。String
戻り値
partition_id、min_block、max_block、level、mutation の各サブカラムを持つ Tuple を返します。Tuple
例
基本例
WITH mergeTreePartInfo('all_12_25_7_4') AS info
SELECT info.partition_id, info.min_block, info.max_block, info.level, info.mutation;┌─info.partition_id─┬─info.min_block─┬─info.max_block─┬─info.level─┬─info.mutation─┐
│ all │ 12 │ 25 │ 7 │ 4 │
└───────────────────┴────────────────┴────────────────┴────────────┴───────────────┘tid
導入バージョン: v20.12.0
現在のBlockを処理しているスレッドのIDを返します。
構文
tid()引数
- なし。
戻り値
現在のスレッド ID を返します。UInt64
例
使用例
SELECT tid();┌─tid()─┐
│ 3878 │
└───────┘