> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 用于根据 stacktraces 列表构建火焰图的聚合函数。

# flameGraph

<div id="flameGraph">
  ## flameGraph
</div>

引入版本：v23.8.0

根据 stacktraces 列表构建一个 [火焰图](https://www.brendangregg.com/flamegraphs.html)。
输出一个字符串数组，可供 [flamegraph.pl](https://github.com/brendangregg/FlameGraph) 工具用于渲染火焰图的 SVG。

<Note>
  在 `ptr != 0` 的情况下，flameGraph 会将 size 和 ptr 相同的内存分配 (size > 0) 与释放 (size \< 0) 进行映射。
  仅显示尚未释放的内存分配。
  未映射到的释放会被忽略。
</Note>

**语法**

```sql theme={null}
flameGraph(traces[, size[, ptr]])
```

**参数**

* `traces` — 一条调用栈，可以是原始地址，也可以是已解析符号的字符串 (例如 `arrayMap(addressToSymbol, trace)`) 。[`Array(UInt64)`](/zh/reference/data-types/array) 或 [`Array(String)`](/zh/reference/data-types/array)
* `size` — 可选。用于内存性能分析的分配大小 (默认值为 1) 。[`UInt64`](/zh/reference/data-types/int-uint)
* `ptr` — 可选。分配地址 (默认值为 0) 。[`UInt64`](/zh/reference/data-types/int-uint)

**返回值**

返回一个供 flamegraph.pl 工具使用的字符串数组。[`Array(String)`](/zh/reference/data-types/array)

**示例**

**基于 CPU 查询分析器构建火焰图**

```sql title=Query theme={null}
SET query_profiler_cpu_time_period_ns=10000000;
SELECT SearchPhrase, COUNT(DISTINCT UserID) AS u FROM hits WHERE SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY u DESC LIMIT 10;
```

```response title=Response theme={null}
clickhouse client --allow_introspection_functions=1 -q "select arrayJoin(flameGraph(arrayReverse(trace))) from system.trace_log where trace_type = 'CPU' and query_id = 'xxx'" | ~/dev/FlameGraph/flamegraph.pl  > flame_cpu.svg
```

**使用内存查询分析器构建显示所有内存分配的 火焰图**

```sql title=Query theme={null}
SET memory_profiler_sample_probability=1, max_untracked_memory=1;
SELECT SearchPhrase, COUNT(DISTINCT UserID) AS u FROM hits WHERE SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY u DESC LIMIT 10;
```

```response title=Response theme={null}
clickhouse client --allow_introspection_functions=1 -q "select arrayJoin(flameGraph(trace, size)) from system.trace_log where trace_type = 'MemorySample' and query_id = 'xxx'" | ~/dev/FlameGraph/flamegraph.pl --countname=bytes --color=mem > flame_mem.svg
```

**根据内存查询分析器构建的 火焰图，显示未释放的内存分配**

```sql title=Query theme={null}
SET memory_profiler_sample_probability=1, max_untracked_memory=1, use_uncompressed_cache=1, merge_tree_max_rows_to_use_cache=100000000000, merge_tree_max_bytes_to_use_cache=1000000000000;
SELECT SearchPhrase, COUNT(DISTINCT UserID) AS u FROM hits WHERE SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY u DESC LIMIT 10;
```

```response title=Response theme={null}
clickhouse client --allow_introspection_functions=1 -q "SELECT arrayJoin(flameGraph(trace, size, ptr)) FROM system.trace_log WHERE trace_type = 'MemorySample' AND query_id = 'xxx'" | ~/dev/FlameGraph/flamegraph.pl --countname=bytes --color=mem > flame_mem_untracked.svg
```

**基于内存查询分析器生成火焰图，显示某一固定时刻的活跃分配**

```sql title=Query theme={null}
SET memory_profiler_sample_probability=1, max_untracked_memory=1;
SELECT SearchPhrase, COUNT(DISTINCT UserID) AS u FROM hits WHERE SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY u DESC LIMIT 10;

-- 1. 每秒内存使用量
SELECT event_time, m, formatReadableSize(max(s) AS m) FROM (SELECT event_time, sum(size) OVER (ORDER BY event_time) AS s FROM system.trace_log WHERE query_id = 'xxx' AND trace_type = 'MemorySample') GROUP BY event_time ORDER BY event_time;

-- 2. 找到内存使用量最大的时间点
SELECT argMax(event_time, s), max(s) FROM (SELECT event_time, sum(size) OVER (ORDER BY event_time) AS s FROM system.trace_log WHERE query_id = 'xxx' AND trace_type = 'MemorySample');
```

```response title=Response theme={null}
-- 3. 修复特定时间点的活跃分配
clickhouse client --allow_introspection_functions=1 -q "SELECT arrayJoin(flameGraph(trace, size, ptr)) FROM (SELECT * FROM system.trace_log WHERE trace_type = 'MemorySample' AND query_id = 'xxx' AND event_time <= 'yyy' ORDER BY event_time\)\" | ~/dev/FlameGraph/flamegraph.pl --countname=bytes --color=mem > flame_mem_time_point_pos.svg

-- 4. 查找特定时间点的释放
clickhouse client --allow_introspection_functions=1 -q "SELECT arrayJoin(flameGraph(trace, -size, ptr)) FROM (SELECT * FROM system.trace_log WHERE trace_type = 'MemorySample' AND query_id = 'xxx' AND event_time > 'yyy' ORDER BY event_time desc\)\" | ~/dev/FlameGraph/flamegraph.pl --countname=bytes --color=mem > flame_mem_time_point_neg.svg
```
