> ## 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.

> Função de agregação que gera um flamegraph usando a lista de stacktraces.

# flameGraph

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

Introduzido em: v23.8.0

Cria um [flamegraph](https://www.brendangregg.com/flamegraphs.html) a partir da lista de stacktraces.
Produz um array de strings que pode ser usado pelo utilitário [flamegraph.pl](https://github.com/brendangregg/FlameGraph) para gerar um SVG do flamegraph.

<Note>
  No caso em que `ptr != 0`, um flameGraph fará o mapeamento de alocações (size > 0) e desalocações (size \< 0) com o mesmo size e ptr.
  Apenas as alocações que não foram liberadas são mostradas.
  Desalocações não mapeadas são ignoradas.
</Note>

**Sintaxe**

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

**Argumentos**

* `traces` — Um stacktrace, seja como endereços brutos ou como strings já simbolizadas (por exemplo, `arrayMap(addressToSymbol, trace)`). [`Array(UInt64)`](/pt-BR/reference/data-types/array) ou [`Array(String)`](/pt-BR/reference/data-types/array)
* `size` — Opcional. Um tamanho de alocação para perfilamento de memória (padrão: 1). [`UInt64`](/pt-BR/reference/data-types/int-uint)
* `ptr` — Opcional. Um endereço de alocação (padrão: 0). [`UInt64`](/pt-BR/reference/data-types/int-uint)

**Valor retornado**

Retorna um array de strings para uso com o utilitário flamegraph.pl. [`Array(String)`](/pt-BR/reference/data-types/array)

**Exemplos**

**Criando um flamegraph com base em um profiler de consultas de 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
```

**Criando um flamegraph com base em um profiler de consulta de memória, mostrando todas as alocações**

```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
```

**Criando um flamegraph com base em um profiler de consulta de memória, mostrando alocações que não foram liberadas**

```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
```

**Crie um flamegraph com base no profiler de memória da consulta, mostrando as alocações ativas em um dado instante**

```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. Uso de memória por segundo
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. Encontrar um ponto no tempo com uso máximo de memória
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. Corrigir alocações ativas em um ponto fixo no tempo
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. Encontrar desalocações em um ponto fixo no tempo
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
```
