summaryrefslogtreecommitdiffstats
path: root/source/core/slang-performance-profiler.cpp
blob: f08b4998d9e99938067cd6e94a57b9f1e4937098 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "slang-performance-profiler.h"
#include "slang-dictionary.h"

namespace Slang
{
    class PerformanceProfilerImpl : public PerformanceProfiler
    {
    public:
        OrderedDictionary<const char*, FuncProfileInfo> data;

        virtual FuncProfileContext enterFunction(const char* funcName) override
        {
            auto entry = data.tryGetValue(funcName);
            if (!entry)
            {
                data.add(funcName, FuncProfileInfo());
                entry = data.tryGetValue(funcName);
            }
            entry->invocationCount++;
            FuncProfileContext ctx;
            ctx.funcName = funcName;
            ctx.startTime = std::chrono::high_resolution_clock::now();
            return ctx;
        }
        virtual void exitFunction(FuncProfileContext ctx) override
        {
            auto endTime = std::chrono::high_resolution_clock::now();
            auto duration = endTime - ctx.startTime;
            auto entry = data.tryGetValue(ctx.funcName);
            entry->duration += duration;
        }
        virtual void getResult(StringBuilder& out) override
        {
            char buffer[512];
            for (const auto& func : data)
            {
                memset(buffer, 0, sizeof(buffer));
                snprintf(buffer, sizeof(buffer), "[*] %30s", func.key);
                out << buffer << " \t";
                auto milliseconds = std::chrono::duration_cast< std::chrono::milliseconds >(func.value.duration);
                out << func.value.invocationCount << " \t" << milliseconds.count() << "ms\n";
            }
        }
        virtual void clear() override
        {
            data.clear();
        }
        virtual void dispose() override
        {
            data = decltype(data)();
        }
    };

    PerformanceProfiler* Slang::PerformanceProfiler::getProfiler()
    {
        thread_local static PerformanceProfilerImpl profiler = PerformanceProfilerImpl();
        return &profiler;
    }

    SlangProfiler::SlangProfiler(PerformanceProfiler* profiler)
    {
        PerformanceProfilerImpl* profilerImpl = static_cast<PerformanceProfilerImpl*>(profiler);
        size_t entryCount = profilerImpl->data.getCount();

        m_profilEntries.reserve(entryCount);

        int index = 0;
        for (auto func : profilerImpl->data)
        {
            ProfileInfo profileEntry {};
            size_t strSize = std::min(sizeof(profileEntry.funcName) - 1, strlen(func.key));

            if (strSize > 0)
            {
                memcpy(profileEntry.funcName, func.key, strSize);
            }
            profileEntry.invocationCount = func.value.invocationCount;
            profileEntry.duration = func.value.duration;

            m_profilEntries.insert(index, profileEntry);
            index++;
        }
    }

    ISlangUnknown* SlangProfiler::getInterface(const Guid& guid)
    {
        if(guid == SlangProfiler::getTypeGuid())
            return static_cast<ISlangUnknown*>(this);
        else
            return nullptr;
    }

    size_t SlangProfiler::getEntryCount()
    {
        return m_profilEntries.getCount();
    }

    const char* SlangProfiler::getEntryName(uint32_t index)
    {
        if (index >= (uint32_t)m_profilEntries.getCount())
            return nullptr;

        return m_profilEntries[index].funcName;
    }

    long SlangProfiler::getEntryTimeMS(uint32_t index)
    {
        if (index >= (uint32_t)m_profilEntries.getCount())
            return 0;

        auto milliseconds = std::chrono::duration_cast< std::chrono::milliseconds >(m_profilEntries[index].duration);
        return (long)milliseconds.count();
    }

    uint32_t SlangProfiler::getEntryInvocationTimes(uint32_t index)
    {
        if (index >= (uint32_t)m_profilEntries.getCount())
            return 0;

        return m_profilEntries[index].invocationCount;
    }
}