summaryrefslogtreecommitdiffstats
path: root/source/core/slang-performance-profiler.h
blob: 40ace4e6dbddeafba043b6adf951c57b4f367b19 (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
#ifndef SLANG_CORE_PERFORMANCE_PROFILER_H
#define SLANG_CORE_PERFORMANCE_PROFILER_H

#include "../core/slang-list.h"
#include "slang-com-helper.h"
#include "slang-string.h"

#include <chrono>
#include <vector>

namespace Slang
{

struct FuncProfileInfo
{
    int invocationCount = 0;
    std::chrono::nanoseconds duration = std::chrono::nanoseconds::zero();
};

struct FuncProfileContext
{
    const char* funcName = nullptr;
    std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
};

class PerformanceProfiler
{
public:
    virtual FuncProfileContext enterFunction(const char* funcName) = 0;
    virtual void exitFunction(FuncProfileContext context) = 0;
    virtual void getResult(StringBuilder& out) = 0;
    virtual void clear() = 0;
    virtual void dispose() = 0;

public:
    static PerformanceProfiler* getProfiler();
};

struct PerformanceProfilerFuncRAIIContext
{
    FuncProfileContext context;
    PerformanceProfilerFuncRAIIContext(const char* funcName)
    {
        context = PerformanceProfiler::getProfiler()->enterFunction(funcName);
    }
    ~PerformanceProfilerFuncRAIIContext()
    {
        PerformanceProfiler::getProfiler()->exitFunction(context);
    }
};

struct SlangProfiler : public ISlangProfiler, public RefObject
{
public:
    SLANG_REF_OBJECT_IUNKNOWN_ALL
    struct ProfileInfo
    {
        char funcName[256] = {0};
        int invocationCount = 0;
        std::chrono::nanoseconds duration = std::chrono::nanoseconds::zero();
    };
    SlangProfiler(PerformanceProfiler* profiler);
    ISlangUnknown* getInterface(const Guid& guid);

    virtual SLANG_NO_THROW size_t SLANG_MCALL getEntryCount() override;
    virtual SLANG_NO_THROW const char* SLANG_MCALL getEntryName(uint32_t index) override;
    virtual SLANG_NO_THROW long SLANG_MCALL getEntryTimeMS(uint32_t index) override;
    virtual SLANG_NO_THROW uint32_t SLANG_MCALL getEntryInvocationTimes(uint32_t index) override;

private:
    List<ProfileInfo> m_profilEntries;
};

#define SLANG_PROFILE PerformanceProfilerFuncRAIIContext _profileContext(__func__)
#define SLANG_PROFILE_SECTION(s) PerformanceProfilerFuncRAIIContext _profileContext##s(#s)

} // namespace Slang

#endif