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

> スタックトレースのリストを用いてフレームグラフを構築する集約関数。

# flameGraph

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

導入バージョン: v23.8.0

スタックトレースのリストを使って[フレームグラフ](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)`](/ja/reference/data-types/array) または [`Array(String)`](/ja/reference/data-types/array)
* `size` — オプション。メモリ profiling 用の割り当てサイズ (デフォルトは 1) 。[`UInt64`](/ja/reference/data-types/int-uint)
* `ptr` — オプション。割り当てアドレス (デフォルトは 0) 。[`UInt64`](/ja/reference/data-types/int-uint)

**戻り値**

`flamegraph.pl` ユーティリティで使用する String の配列を返します。[`Array(String)`](/ja/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. 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
```
