Help wanted!

The following content of this documentation page has been machine-translated. But unlike other websites, it is not done on the fly. This translated text lives on GitHub repository alongside main ClickHouse codebase and waits for fellow native speakers to make it more human-readable. You can also use the original English version as a reference.

Help ClickHouse documentation by editing this page

内省功能

您可以使用本章中描述的函数来反省 ELFDWARF 用于查询分析。

警告

这些功能很慢,可能会强加安全考虑。

对于内省功能的正确操作:

  • 安装 clickhouse-common-static-dbg 包。

  • 设置 allow_introspection_functions 设置为1。

    1. 出于安全考虑,内省函数默认是关闭的。

ClickHouse将探查器报告保存到 trace_log 系统表. 确保正确配置了表和探查器。

addressToLine

将ClickHouse服务器进程内的虚拟内存地址转换为ClickHouse源代码中的文件名和行号。

如果您使用官方的ClickHouse软件包,您需要安装 clickhouse-common-static-dbg 包。

语法

  1. addressToLine(address_of_binary_instruction)

参数

  • address_of_binary_instruction (UInt64) — 正在运行进程的指令地址。

返回值

  • 源代码文件名和行号(用冒号分隔的行号)

    1. 示例, `/build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:199`, where `199` is a line number.
  • 如果函数找不到调试信息,返回二进制文件的名称。

  • 如果地址无效,返回空字符串。

类型: 字符串.

示例

启用内省功能:

  1. SET allow_introspection_functions=1

从中选择第一个字符串 trace_log 系统表:

  1. SELECT * FROM system.trace_log LIMIT 1 \G
  1. Row 1:
  2. ──────
  3. event_date: 2019-11-19
  4. event_time: 2019-11-19 18:57:23
  5. revision: 54429
  6. timer_type: Real
  7. thread_number: 48
  8. query_id: 421b6855-1858-45a5-8f37-f383409d6d72
  9. trace: [140658411141617,94784174532828,94784076370703,94784076372094,94784076361020,94784175007680,140658411116251,140658403895439]

trace 字段包含采样时的堆栈跟踪。

获取单个地址的源代码文件名和行号:

  1. SELECT addressToLine(94784076370703) \G
  1. Row 1:
  2. ──────
  3. addressToLine(94784076370703): /build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:199

将函数应用于整个堆栈跟踪:

  1. SELECT
  2. arrayStringConcat(arrayMap(x -> addressToLine(x), trace), '\n') AS trace_source_code_lines
  3. FROM system.trace_log
  4. LIMIT 1
  5. \G

arrayMap 功能允许处理的每个单独的元素 trace 阵列由 addressToLine 功能。 这种处理的结果,你在看 trace_source_code_lines 列的输出。

  1. Row 1:
  2. ──────
  3. trace_source_code_lines: /lib/x86_64-linux-gnu/libpthread-2.27.so
  4. /usr/lib/debug/usr/bin/clickhouse
  5. /build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:199
  6. /build/obj-x86_64-linux-gnu/../src/Common/ThreadPool.h:155
  7. /usr/include/c++/9/bits/atomic_base.h:551
  8. /usr/lib/debug/usr/bin/clickhouse
  9. /lib/x86_64-linux-gnu/libpthread-2.27.so
  10. /build/glibc-OTsEL5/glibc-2.27/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:97

addressToSymbol

将ClickHouse服务器进程内的虚拟内存地址转换为ClickHouse对象文件中的符号。

语法

  1. addressToSymbol(address_of_binary_instruction)

参数

  • address_of_binary_instruction (UInt64) — Address of instruction in a running process.

返回值

  • 来自ClickHouse对象文件的符号。
  • 如果地址无效,返回空字符串。

类型: 字符串.

示例

启用内省功能:

  1. SET allow_introspection_functions=1

从中选择第一个字符串 trace_log 系统表:

  1. SELECT * FROM system.trace_log LIMIT 1 \G
  1. Row 1:
  2. ──────
  3. event_date: 2019-11-20
  4. event_time: 2019-11-20 16:57:59
  5. revision: 54429
  6. timer_type: Real
  7. thread_number: 48
  8. query_id: 724028bf-f550-45aa-910d-2af6212b94ac
  9. trace: [94138803686098,94138815010911,94138815096522,94138815101224,94138815102091,94138814222988,94138806823642,94138814457211,94138806823642,94138814457211,94138806823642,94138806795179,94138806796144,94138753770094,94138753771646,94138753760572,94138852407232,140399185266395,140399178045583]

trace 字段包含采样时的堆栈跟踪。

获取单个地址的符号:

  1. SELECT addressToSymbol(94138803686098) \G
  1. Row 1:
  2. ──────
  3. addressToSymbol(94138803686098): _ZNK2DB24IAggregateFunctionHelperINS_20AggregateFunctionSumImmNS_24AggregateFunctionSumDataImEEEEE19addBatchSinglePlaceEmPcPPKNS_7IColumnEPNS_5ArenaE

将函数应用于整个堆栈跟踪:

  1. SELECT
  2. arrayStringConcat(arrayMap(x -> addressToSymbol(x), trace), '\n') AS trace_symbols
  3. FROM system.trace_log
  4. LIMIT 1
  5. \G

arrayMap 功能允许处理的每个单独的元素 trace 阵列由 addressToSymbols 功能。 这种处理的结果,你在看 trace_symbols 列的输出。

  1. Row 1:
  2. ──────
  3. trace_symbols: _ZNK2DB24IAggregateFunctionHelperINS_20AggregateFunctionSumImmNS_24AggregateFunctionSumDataImEEEEE19addBatchSinglePlaceEmPcPPKNS_7IColumnEPNS_5ArenaE
  4. _ZNK2DB10Aggregator21executeWithoutKeyImplERPcmPNS0_28AggregateFunctionInstructionEPNS_5ArenaE
  5. _ZN2DB10Aggregator14executeOnBlockESt6vectorIN3COWINS_7IColumnEE13immutable_ptrIS3_EESaIS6_EEmRNS_22AggregatedDataVariantsERS1_IPKS3_SaISC_EERS1_ISE_SaISE_EERb
  6. _ZN2DB10Aggregator14executeOnBlockERKNS_5BlockERNS_22AggregatedDataVariantsERSt6vectorIPKNS_7IColumnESaIS9_EERS6_ISB_SaISB_EERb
  7. _ZN2DB10Aggregator7executeERKSt10shared_ptrINS_17IBlockInputStreamEERNS_22AggregatedDataVariantsE
  8. _ZN2DB27AggregatingBlockInputStream8readImplEv
  9. _ZN2DB17IBlockInputStream4readEv
  10. _ZN2DB26ExpressionBlockInputStream8readImplEv
  11. _ZN2DB17IBlockInputStream4readEv
  12. _ZN2DB26ExpressionBlockInputStream8readImplEv
  13. _ZN2DB17IBlockInputStream4readEv
  14. _ZN2DB28AsynchronousBlockInputStream9calculateEv
  15. _ZNSt17_Function_handlerIFvvEZN2DB28AsynchronousBlockInputStream4nextEvEUlvE_E9_M_invokeERKSt9_Any_data
  16. _ZN14ThreadPoolImplI20ThreadFromGlobalPoolE6workerESt14_List_iteratorIS0_E
  17. _ZZN20ThreadFromGlobalPoolC4IZN14ThreadPoolImplIS_E12scheduleImplIvEET_St8functionIFvvEEiSt8optionalImEEUlvE1_JEEEOS4_DpOT0_ENKUlvE_clEv
  18. _ZN14ThreadPoolImplISt6threadE6workerESt14_List_iteratorIS0_E
  19. execute_native_thread_routine
  20. start_thread
  21. clone

demangle

转换一个符号,您可以使用 addressToSymbol 函数到C++函数名。

语法

  1. demangle(symbol)

参数

  • symbol (字符串) — Symbol from an object file.

返回值

  • C++函数的名称。
  • 如果符号无效,则为空字符串。

类型: 字符串.

示例

启用内省功能:

  1. SET allow_introspection_functions=1

从中选择第一个字符串 trace_log 系统表:

  1. SELECT * FROM system.trace_log LIMIT 1 \G
  1. Row 1:
  2. ──────
  3. event_date: 2019-11-20
  4. event_time: 2019-11-20 16:57:59
  5. revision: 54429
  6. timer_type: Real
  7. thread_number: 48
  8. query_id: 724028bf-f550-45aa-910d-2af6212b94ac
  9. trace: [94138803686098,94138815010911,94138815096522,94138815101224,94138815102091,94138814222988,94138806823642,94138814457211,94138806823642,94138814457211,94138806823642,94138806795179,94138806796144,94138753770094,94138753771646,94138753760572,94138852407232,140399185266395,140399178045583]

trace 字段包含采样时的堆栈跟踪。

获取单个地址的函数名称:

  1. SELECT demangle(addressToSymbol(94138803686098)) \G
  1. Row 1:
  2. ──────
  3. 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

将函数应用于整个堆栈跟踪:

  1. SELECT
  2. arrayStringConcat(arrayMap(x -> demangle(addressToSymbol(x)), trace), '\n') AS trace_functions
  3. FROM system.trace_log
  4. LIMIT 1
  5. \G

arrayMap 功能允许处理的每个单独的元素 trace 阵列由 demangle 功能。 这种处理的结果,你在看 trace_functions 列的输出。

  1. Row 1:
  2. ──────
  3. trace_functions: DB::IAggregateFunctionHelper<DB::AggregateFunctionSum<unsigned long, unsigned long, DB::AggregateFunctionSumData<unsigned long> > >::addBatchSinglePlace(unsigned long, char*, DB::IColumn const**, DB::Arena*) const
  4. DB::Aggregator::executeWithoutKeyImpl(char*&, unsigned long, DB::Aggregator::AggregateFunctionInstruction*, DB::Arena*) const
  5. 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&)
  6. 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&)
  7. DB::Aggregator::execute(std::shared_ptr<DB::IBlockInputStream> const&, DB::AggregatedDataVariants&)
  8. DB::AggregatingBlockInputStream::readImpl()
  9. DB::IBlockInputStream::read()
  10. DB::ExpressionBlockInputStream::readImpl()
  11. DB::IBlockInputStream::read()
  12. DB::ExpressionBlockInputStream::readImpl()
  13. DB::IBlockInputStream::read()
  14. DB::AsynchronousBlockInputStream::calculate()
  15. std::_Function_handler<void (), DB::AsynchronousBlockInputStream::next()::{lambda()#1}>::_M_invoke(std::_Any_data const&)
  16. ThreadPoolImpl<ThreadFromGlobalPool>::worker(std::_List_iterator<ThreadFromGlobalPool>)
  17. 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
  18. ThreadPoolImpl<std::thread>::worker(std::_List_iterator<std::thread>)
  19. execute_native_thread_routine
  20. start_thread
  21. clone