aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/common/profiler_reporting.h
blob: df98e05b78b0ac73620965986e631c5d70ae1cfd (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2015 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <cstddef>
#include <vector>

#include "common/profiler.h"
#include "common/synchronized_wrapper.h"

namespace Common {
namespace Profiling {

struct TimingCategoryInfo {
    static const unsigned int NO_PARENT = -1;

    TimingCategory* category;
    const char* name;
    unsigned int parent;
};

struct ProfilingFrameResult {
    /// Time since the last delivered frame
    Duration interframe_time;

    /// Time spent processing a frame, excluding VSync
    Duration frame_time;

    /// Total amount of time spent inside each category in this frame. Indexed by the category id
    std::vector<Duration> time_per_category;
};

class ProfilingManager final {
public:
    ProfilingManager();

    unsigned int RegisterTimingCategory(TimingCategory* category, const char* name);
    void SetTimingCategoryParent(unsigned int category, unsigned int parent);

    const std::vector<TimingCategoryInfo>& GetTimingCategoriesInfo() const {
        return timing_categories;
    }

    /// This should be called after swapping screen buffers.
    void BeginFrame();
    /// This should be called before swapping screen buffers.
    void FinishFrame();

    /// Get the timing results from the previous frame. This is updated when you call FinishFrame().
    const ProfilingFrameResult& GetPreviousFrameResults() const {
        return results;
    }

private:
    std::vector<TimingCategoryInfo> timing_categories;
    Clock::time_point last_frame_end;
    Clock::time_point this_frame_start;

    ProfilingFrameResult results;
};

struct AggregatedDuration {
    Duration avg, min, max;
};

struct AggregatedFrameResult {
    /// Time since the last delivered frame
    AggregatedDuration interframe_time;

    /// Time spent processing a frame, excluding VSync
    AggregatedDuration frame_time;

    float fps;

    /// Total amount of time spent inside each category in this frame. Indexed by the category id
    std::vector<AggregatedDuration> time_per_category;
};

class TimingResultsAggregator final {
public:
    TimingResultsAggregator(size_t window_size);

    void Clear();
    void SetNumberOfCategories(size_t n);

    void AddFrame(const ProfilingFrameResult& frame_result);

    AggregatedFrameResult GetAggregatedResults() const;

    size_t max_window_size;
    size_t window_size;
    size_t cursor;

    std::vector<Duration> interframe_times;
    std::vector<Duration> frame_times;
    std::vector<std::vector<Duration>> times_per_category;
};

ProfilingManager& GetProfilingManager();
SynchronizedRef<TimingResultsAggregator> GetTimingResultsAggregator();

} // namespace Profiling
} // namespace Common