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

> Fonction d’agrégation qui construit un flamegraph à partir d’une liste de stacktraces.

# flameGraph

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

Introduit dans : v23.8.0

Construit un [flamegraph](https://www.brendangregg.com/flamegraphs.html) à partir d’une liste de stacktraces.
Produit un tableau de chaînes de caractères pouvant être utilisé par l’utilitaire [flamegraph.pl](https://github.com/brendangregg/FlameGraph) pour générer le SVG du flamegraph.

<Note>
  Lorsque `ptr != 0`, flameGraph associe les allocations (size > 0) et les désallocations (size \< 0) ayant la même taille et le même ptr.
  Seules les allocations qui n’ont pas été libérées sont affichées.
  Les désallocations non associées sont ignorées.
</Note>

**Syntaxe**

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

**Arguments**

* `traces` — Une stacktrace, soit sous forme d’adresses brutes, soit sous forme de chaînes déjà symbolisées (par exemple `arrayMap(addressToSymbol, trace)`). [`Array(UInt64)`](/fr/reference/data-types/array) ou [`Array(String)`](/fr/reference/data-types/array)
* `size` — Facultatif. Une taille d’allocation pour le profilage mémoire (1 par défaut). [`UInt64`](/fr/reference/data-types/int-uint)
* `ptr` — Facultatif. Une adresse d’allocation (0 par défaut). [`UInt64`](/fr/reference/data-types/int-uint)

**Valeur renvoyée**

Renvoie un tableau de chaînes à utiliser avec l’utilitaire flamegraph.pl. [`Array(String)`](/fr/reference/data-types/array)

**Exemples**

**Création d’un flamegraph à partir du query profiler 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
```

**Création d’un flamegraph à partir du profileur mémoire des requêtes, montrant toutes les allocations**

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

**Construction d’un flamegraph à partir d’un profileur mémoire des requêtes, montrant les allocations qui n’ont pas été libérées**

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

**Créer un flamegraph à partir du profileur mémoire des requêtes, montrant les allocations actives à un instant précis**

```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. Memory usage per second
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. Find a time point with maximal memory usage
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. Fix active allocations at fixed point of time
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. Find deallocations at fixed point of time
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
```
