> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.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는 크기와 ptr가 동일한 메모리 할당(size > 0)과 메모리 해제(size \< 0)를 서로 매핑합니다.
  해제되지 않은 메모리 할당만 표시됩니다.
  매핑되지 않은 메모리 해제는 무시됩니다.
</Note>

**구문**

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

**인수**

* `traces` — 원시 주소 또는 이미 심볼화된 문자열(예: `arrayMap(addressToSymbol, trace)`)로 표현된 스택트레이스입니다. [`Array(UInt64)`](/ko/reference/data-types/array) 또는 [`Array(String)`](/ko/reference/data-types/array)
* `size` — 선택 사항입니다. 메모리 프로파일링을 위한 할당 크기입니다(기본값 1). [`UInt64`](/ko/reference/data-types/int-uint)
* `ptr` — 선택 사항입니다. 할당 주소입니다(기본값 0). [`UInt64`](/ko/reference/data-types/int-uint)

**반환 값**

`flamegraph.pl` 유틸리티에서 사용할 문자열 배열을 반환합니다. [`Array(String)`](/ko/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
```
