aboutsummaryrefslogtreecommitdiff
path: root/src/ui/profiler.h
blob: db16f8ec59791ff0ca4db9629db59caeab52389f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once

#include <array>
#include <cstdint>

namespace Hsdbg
{
    // a small fixed-size ring buffer of floats, sized for a live scrolling
    // graph; new samples overwrite the oldest once it fills up
    class TimeSeries
    {
    public:
        // roughly four seconds of history at 60 fps
        static constexpr int CAPACITY = 240;

        auto push(float value) -> void;
        auto clear() -> void;

        auto values() const -> const float* { return m_values.data(); }
        auto count() const -> int { return m_count; }

        // where the oldest sample sits, so ImGui::PlotLines can unwrap the ring
        auto offset() const -> int { return m_count == CAPACITY ? m_write : 0; }

        auto latest() const -> float;
        auto average() const -> float;
        auto maximum() const -> float;

    private:
        std::array<float, CAPACITY> m_values{};
        int m_count = 0;
        int m_write = 0;
    };

    // collects live performance samples for the profiler panel. this first pass
    // only tracks the debugger's own frame timing; sampling the debugged target
    // comes in a later step
    class Profiler
    {
    public:
        auto sample_frame(float delta_seconds) -> void;

        // resident set size of the target in bytes; alive says whether a process
        // is actually running so the graph can hold flat while nothing is loaded
        auto sample_target(uint64_t resident_bytes, bool alive) -> void;
        auto reset() -> void;

        auto frame_times() const -> const TimeSeries& { return m_frame_ms; }
        auto frame_rates() const -> const TimeSeries& { return m_fps; }
        auto target_memory_mb() const -> const TimeSeries& { return m_target_memory_mb; }

        auto paused() const -> bool { return m_paused; }
        auto set_paused(bool value) -> void { m_paused = value; }

    private:
        TimeSeries m_frame_ms;
        TimeSeries m_fps;
        TimeSeries m_target_memory_mb;
        bool m_paused = false;
    };
}