인트로스펙션 함수
이 장에서 설명하는 함수들을 사용하여 쿼리 프로파일링을 위해 ELF 및 DWARF를 인트로스펙션할 수 있습니다.
이러한 함수는 실행 속도가 느리고 보안상 주의가 필요합니다.
인트로스펙션 함수가 올바르게 동작하려면 다음을 수행해야 합니다.
-
clickhouse-common-static-dbg패키지를 설치합니다. -
allow_introspection_functions 설정을 1로 지정합니다.
보안상의 이유로 인트로스펙션 함수는 기본적으로 비활성화되어 있습니다.
ClickHouse는 프로파일러 보고서를 trace_log 시스템 테이블에 저장합니다. 해당 테이블과 프로파일러가 올바르게 구성되어 있는지 확인하십시오.
addressToLine
도입된 버전: v20.1
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 \G;
-- 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) \G;
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
\G
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:97
addressToLineWithInlines
도입 버전: v22.2
addressToLine과 유사하지만, 모든 인라인 함수가 포함된 배열(Array)을 반환합니다.
이로 인해 addressToLine보다 느리게 동작합니다.
이 introspection 함수를 사용하려면:
clickhouse-common-static-dbg패키지를 설치하십시오.allow_introspection_functions설정을1로 지정하십시오.
구문
addressToLineWithInlines(address_of_binary_instruction)
인수(Arguments)
address_of_binary_instruction— 실행 중인 프로세스에서 명령어의 주소입니다.UInt64
반환 값(Returned value)
첫 번째 요소에는 소스 코드 파일 이름과 줄 번호가 콜론으로 구분된 문자열이 들어 있는 배열을 반환합니다. 두 번째, 세 번째 등의 요소에는 인라인 함수의 소스 코드 파일 이름, 줄 번호, 함수 이름이 나열됩니다. 디버그 정보를 찾을 수 없는 경우에는 이진 파일 이름 하나만을 요소로 갖는 배열을 반환하고, 주소가 유효하지 않으면 빈 배열을 반환합니다. Array(String)
예시(Examples)
주소에 함수를 적용하기(Applying the function to an address)
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
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 \G;
-- 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) \G;
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
\G
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
clone
demangle
도입 버전: v20.1
심볼을 C++ 함수 이름으로 변환합니다.
이 심볼은 일반적으로 addressToSymbol 함수가 반환합니다.
구문
demangle(symbol)
인수
symbol— 오브젝트 파일의 심볼입니다.String
반환값
C++ 함수 이름을 반환하며, 심볼이 유효하지 않은 경우 빈 문자열을 반환합니다. String
예시
trace_log 시스템 테이블에서 첫 번째 문자열 선택하기
SELECT * FROM system.trace_log LIMIT 1 \G;
-- 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)) \G;
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
전체 스택 트레이스에 FUNCTION을 적용하기
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
\G
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(std::vector<COW<DB::IColumn>::immutable_ptr<DB::IColumn>, std::allocator<COW<DB::IColumn>::immutable_ptr<DB::IColumn> > >, unsigned long, DB::AggregatedDataVariants&, std::vector<DB::IColumn const*, std::allocator<DB::IColumn const*> >&, std::vector<std::vector<DB::IColumn const*, std::allocator<DB::IColumn const*> >, std::allocator<std::vector<DB::IColumn const*, std::allocator<DB::IColumn const*> > > >&, bool&)
DB::Aggregator::executeOnBlock(DB::Block const&, DB::AggregatedDataVariants&, std::vector<DB::IColumn const*, std::allocator<DB::IColumn const*> >&, std::vector<std::vector<DB::IColumn const*, std::allocator<DB::IColumn const*> >, std::allocator<std::vector<DB::IColumn const*, std::allocator<DB::IColumn const*> > > >&, bool&)
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
clone
isMergeTreePartCoveredBy
도입 버전: v25.6
첫 번째 인수의 파트가 두 번째 인수의 파트에 포함되어 있는지 확인하는 함수입니다.
구문
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
각 Block에 대해 서버 로그에 trace 로그 메시지를 기록합니다.
구문
logTrace(message)
인수
message— 서버 로그로 출력되는 메시지입니다.const String
반환 값
항상 0을 반환합니다. UInt8
예제
기본 예제
SELECT logTrace('logTrace message');
┌─logTrace('logTrace message')─┐
│ 0 │
└──────────────────────────────┘
mergeTreePartInfo
도입된 버전: v25.6
MergeTree 파트 이름에서 필요한 값을 추출하는 FUNCTION입니다.
구문
mergeTreePartInfo(part_name)
인수
part_name— 언팩할 파트 이름입니다.String
반환 값
다음 서브컬럼을 포함하는 Tuple을 반환합니다: partition_id, min_block, max_block, level, mutation. 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
현재 Block이 처리 중인 스레드의 ID를 반환합니다.
구문
tid()
인수(Arguments)
- 없음.
반환값(Returned value)
현재 스레드 ID를 반환합니다. UInt64
예제(Examples)
사용 예(Usage example)
SELECT tid();
┌─tid()─┐
│ 3878 │
└───────┘