> ## Documentation Index
> Fetch the complete documentation index at: https://codspeed.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Performance Profiling and Flame Graphs

> Debug performance issues with detailed flame graphs and execution profiles generated by CodSpeed's instrumentation

<img src="https://mintcdn.com/codspeed/CInbng288QuXBkrC/features/assets/cover.png?fit=max&auto=format&n=CInbng288QuXBkrC&q=85&s=302d47fea90881b1af8ab6c21148c245" alt="Cover Image" width="1171" height="685" data-path="features/assets/cover.png" />

CodSpeed's profiling capabilities provide deep insights into your application's
performance through detailed flame graphs and execution traces.

Flame graphs are available for benchmarks using the
[CPU Simulation instrument](/instruments/cpu) and the
[Walltime instrument](/instruments/walltime).

## Reading Flame Graphs

Flame graphs are a visualization tool for profiling software. They provide a
graphical representation of your program's execution, making it easier to
understand the runtime complexities involved.

Let's start with an example:

<img src="https://mintcdn.com/codspeed/ruzgZs0sFfLvVt32/features/assets/flamegraph.dark.png?fit=max&auto=format&n=ruzgZs0sFfLvVt32&q=85&s=1c04e8945f57dbbb53b52aa5ffff9dc7" alt="Flame graph Example" className="w-full max-w-lg mx-auto" width="1566" height="509" data-path="features/assets/flamegraph.dark.png" />

Here, each rectangle represents a function. The width of the rectangle is
proportional to the amount of time spent in that function. The wider the
rectangle, the more time was spent in that function. The vertical axis
represents the call depth (a.k.a. stack depth), which represents the call
hierarchy.

For this example, here is the call hierarchy:

1. The root caller is the `app` function.
2. `app` calls the `init`, `handleRequest` and `terminate` functions.
3. `handleRequest` calls both `authenticateUser` and `processData`.
4. `processData` calls `foo` which in turn calls `bar`.

### Aggregated function calls

Functions calls are aggregated, so if a function is called multiple times, the
time spent in all calls is aggregated into a single block.

Thus, the following code:

```python theme={null}
def bar():
    pass

def foo():
    bar()
    bar()

def main():
    foo()
    foo()
```

Will generate the following flame graph:

<img src="https://mintcdn.com/codspeed/ruzgZs0sFfLvVt32/features/assets/aggregated-functions.dark.png?fit=max&auto=format&n=ruzgZs0sFfLvVt32&q=85&s=2e92e2983ff7ba79acc02dce9d6ac9d1" className="rounded-xl w-full max-w-sm mx-auto" alt="Aggregated functions in a flame graph" width="804" height="298" data-path="features/assets/aggregated-functions.dark.png" />

Where the `foo` function is called twice and the `bar` function is called 4
times, but the time spent is aggregated into a single block for each function.

### Self-costs

In the previous example, we could see the global cost of each function call
quite clearly. However, it can be tricky to find out how much time was spent
within the function itself.

<img src="https://mintcdn.com/codspeed/ruzgZs0sFfLvVt32/features/assets/flamegraph-self-cost.dark.png?fit=max&auto=format&n=ruzgZs0sFfLvVt32&q=85&s=0c91a311609c7012a119ea1ae46c439a" className="rounded-xl w-full max-w-sm mx-auto" alt="Self costs in flame graphs" width="808" height="475" data-path="features/assets/flamegraph-self-cost.dark.png" />

In the above illustration, we can see two types of self-costs:

* **<span style={{color: "#1864ab"}}>(implicit) self-costs</span>**: the time
  spent in the function itself is the whole width of the rectangle since it
  doesn't call any other functions.
* **<span style={{color: "#ce3f3f"}}>self-costs</span>**: the self-cost here is
  visible as the space not occupied by the children of the block.

<Note>
  **Self-costs in interpreted languages**

  In Python or Node.js, the self-cost is the time spent in the function itself,
  but also the time spent by the interpreter. This means that a function will
  always have a self-cost, even if the function does nothing.
</Note>

If we change a bit the example, adding a lot of computation directly in the main
function:

```python {2-3} theme={null}
def main():
    for i in range(100):
        # do some computation
    foo()
    foo()
```

Then the flame graph would look like this, with the self-cost of `main` being
much bigger than before:

<img src="https://mintcdn.com/codspeed/ruzgZs0sFfLvVt32/features/assets/flamegraph-bigger-self-cost.dark.png?fit=max&auto=format&n=ruzgZs0sFfLvVt32&q=85&s=fe38f35767ee5fdeccf5c8035ed74899" className="rounded-xl w-full max-w-sm mx-auto" alt="Bigger self costs in flame graphs" width="809" height="387" data-path="features/assets/flamegraph-bigger-self-cost.dark.png" />

## Viewing Flame Graphs

On the pull request page, you can access the flame graphs of a benchmark by
expanding it.

<Frame caption="Example of flame graphs on a pull request page">
  <img src="https://mintcdn.com/codspeed/ruzgZs0sFfLvVt32/features/assets/benchmark-flame-graphs.png?fit=max&auto=format&n=ruzgZs0sFfLvVt32&q=85&s=8d5e4f362b24a69b91756d0eb8916ae7" className="rounded-xl w-full max-w-lg mx-auto" alt="Example of flame graphs on a pull request page" width="1245" height="1139" data-path="features/assets/benchmark-flame-graphs.png" />
</Frame>

Three types of flame graphs are available:

* **Base**: flame graph of the
  [benchmark base run](/features/understanding-the-metrics/#baseline-report-selection)
* **Head**: flame graph of the benchmark run from the latest commit of the pull
  request
* **Diff**: difference between the head and the base flame graphs

<Tip>
  **FFI Support**

  If you're using
  [Foreign Function Interface](https://en.wikipedia.org/wiki/Foreign_function_interface),
  typically calling C/C++/Rust code from Python or Node.js, make sure to generate
  debug symbols for the foreign functions so that you can see them in the flame
  graph.
</Tip>

## Inspector

Hover any bar to open the span details. This panel shows you what the function
is, where it comes from, and how its time is spent.

<img src="https://mintcdn.com/codspeed/J_9QffCKgr3Hbs6D/instruments/cpu/assets/tooltip-explanation.excalidraw.png?fit=max&auto=format&n=J_9QffCKgr3Hbs6D&q=85&s=207eebeeb34900a7d27eed74c14c9b20" alt="Flamegraph inspector" width="2383" height="1357" data-path="instruments/cpu/assets/tooltip-explanation.excalidraw.png" />

* **Metadata**: Function name, source file, code origin
* **Self time**: Time spent in the function body only, excluding child calls.
* **Total time**: Time spent in the function including all its children.

The details of metrics displayed depend on the instrument used to collect the
profiling data, head over to the respective instrument documentation for more
information:

* [CPU Simulation metrics](/instruments/cpu#inspector-metrics)
* [Walltime metrics](/instruments/walltime#inspector-metrics)

## Color modes

### By Origin

Colors by code origin: User, Library, System, or Unknown. Useful to separate
your code from dependencies and the kernel.

<img src="https://mintcdn.com/codspeed/CInbng288QuXBkrC/features/assets/flamegraph-color-by-origin.png?fit=max&auto=format&n=CInbng288QuXBkrC&q=85&s=63af2626b8a6fa0538046069c74662c1" alt="Flamegraph color mode example: By Origin" width="1301" height="586" data-path="features/assets/flamegraph-color-by-origin.png" />

### Differential

Compares Base vs Head and colors spans by change: slower, faster, added, or
removed. Ideal for scanning regressions and wins after a commit.

<img src="https://mintcdn.com/codspeed/CInbng288QuXBkrC/features/assets/flamegraph-color-diff.png?fit=max&auto=format&n=CInbng288QuXBkrC&q=85&s=08e31aa883ea06e405c4a2c23f30cb71" alt="Flamegraph color mode example: Diff" width="1301" height="586" data-path="features/assets/flamegraph-color-diff.png" />

Colors reflect the status of the span compared to the base run:

* **<span className="text-red-400">Slower</span>**: The span is slower than in
  the base run.
* **<span className="text-green-400">Faster</span>**: The span is faster than in
  the base run.
* **<span className="text-blue-400">Added</span>**: The span has been added in
  the head run.
* **<span className="text-orange-400">Removed</span>**: The span is removed in
  the head run.

### By Bottleneck

Colors each span by the dominant bound on its self time: instruction-bound,
cache-bound, memory-bound, or system-bound. Fast way to see what is blocking
work.

<img src="https://mintcdn.com/codspeed/CInbng288QuXBkrC/features/assets/flamegraph-color-by-bottleneck.png?fit=max&auto=format&n=CInbng288QuXBkrC&q=85&s=43fc435d4969e050969796880d1df3ab" alt="Flamegraph color mode example: By Bottleneck" width="1301" height="586" data-path="features/assets/flamegraph-color-by-bottleneck.png" />

### By Function

Colors spans by function symbol so identical functions share a color, no matter
where they are called. Helps spot hot functions across call sites.

<img src="https://mintcdn.com/codspeed/CInbng288QuXBkrC/features/assets/flamegraph-color-by-function.png?fit=max&auto=format&n=CInbng288QuXBkrC&q=85&s=5a823a77285393269fcd4fbb449634a1" alt="Flamegraph color mode example: By Function" width="1301" height="586" data-path="features/assets/flamegraph-color-by-function.png" />

<Tip>
  Function colors will be the same across benchmarks, projects, and runs. So you
  can easily recognize the same function across different runs.
</Tip>

### System Calls toggle

Include kernel and low-level runtime contributions. Off keeps focus on
application and library code.

## Function list

Upon expanding a flamegraph, you can access the function list. And dive in the
details of each span.

<img src="https://mintcdn.com/codspeed/CInbng288QuXBkrC/features/assets/function-list.png?fit=max&auto=format&n=CInbng288QuXBkrC&q=85&s=4e535467c1e8a6410668d5eb6bf902a0" alt="Function list" width="2729" height="1584" data-path="features/assets/function-list.png" />

## Next Steps

<Columns cols={2}>
  <Card title="Setup Benchmarks with CPU Simulation" href="/instruments/cpu/#usage-with-github-actions" icon="https://mintcdn.com/codspeed/DYQLS2JiztYwuRW0/assets/icons/data-primary.svg?fit=max&auto=format&n=DYQLS2JiztYwuRW0&q=85&s=d73410b814160a15cf20f797c167af08" width="24" height="24" data-path="assets/icons/data-primary.svg">
    Learn how to enable CPU Simulation to generate flame graphs
  </Card>

  <Card title="Setup Benchmarks with the Walltime instrument" href="/instruments/walltime/#usage-with-github-actions" icon="stopwatch">
    Learn how to enable Walltime instrumentation to generate flame graphs
  </Card>

  <Card title="Performance Regression Detection" href="/features/performance-checks" icon="shield-check">
    Set up automated checks to catch performance issues early
  </Card>

  <Card title="Benchmark Creation Guides" href="/benchmarks/overview" icon="code">
    Create comprehensive benchmarks for your codebase
  </Card>
</Columns>
