vsg: include/vsg/utils/Profiler.h Source File

vsg 1.1.9 VulkanSceneGraph library |

Loading...
Searching...
No Matches
Profiler.h
1/* <editor-fold desc="MIT License">
2
3Copyright(c) 2024 Robert Osfield
4
5Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
7The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
9THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
11</editor-fold> */
12
13#pragma once
14
15#include <vsg/io/stream.h>
16#include <vsg/ui/FrameStamp.h>
17#include <vsg/utils/Instrumentation.h>
18#include <vsg/vk/Device.h>
19
20namespace vsg
21{
22 class VSG_DECLSPEC ProfileLog : public Inherit<Object, ProfileLog>
23 {
24 public:
25 explicit ProfileLog(size_t size = 16384);
26
27 enum Type : uint8_t
28 {
29 NO_TYPE,
30 FRAME,
31 CPU,
32 COMMAND_BUFFER,
33 GPU
34 };
35
36 struct Entry
37 {
38 Type type = {};
39 bool enter = true;
40 vsg::time_point cpuTime = {};
41 uint64_t gpuTime = 0;
42 const SourceLocation* sourceLocation = nullptr;
43 const Object* object = nullptr;
44 uint64_t reference = 0;
45 std::thread::id thread_id = {};
46 };
47
48 std::map<std::thread::id, std::string> threadNames;
49 std::vector<Entry> entries;
50 std::atomic_uint64_t index = 0;
51 std::vector<uint64_t> frameIndices;
52 double timestampScaleToMilliseconds = 1e-6;
53
54 Entry& enter(uint64_t& reference, Type type)
55 {
56 reference = index.fetch_add(1);
57 Entry& enter_entry = entry(reference);
58 enter_entry.enter = true;
59 enter_entry.type = type;
60 enter_entry.reference = 0;
61 enter_entry.cpuTime = clock::now();
62 enter_entry.gpuTime = 0;
63 enter_entry.thread_id = std::this_thread::get_id();
64 return enter_entry;
65 }
66
67 Entry& leave(uint64_t& reference, Type type)
68 {
69 Entry& enter_entry = entry(reference);
70
71 uint64_t new_reference = index.fetch_add(1);
72 Entry& leave_entry = entry(new_reference);
73
74 enter_entry.reference = new_reference;
75 leave_entry.cpuTime = clock::now();
76 leave_entry.gpuTime = 0;
77 leave_entry.enter = false;
78 leave_entry.type = type;
79 leave_entry.reference = reference;
80 leave_entry.thread_id = std::this_thread::get_id();
81 reference = new_reference;
82 return leave_entry;
83 }
84
85 Entry& entry(uint64_t reference)
86 {
87 return entries[reference % entries.size()];
88 }
89
90 void report(std::ostream& out);
91 uint64_t report(std::ostream& out, uint64_t reference);
92
93 public:
94 void read(Input& input) override;
95 void write(Output& output) const override;
96 };
97 VSG_type_name(ProfileLog)
98
99 class VSG_DECLSPEC Profiler : public Inherit<Instrumentation, Profiler>
100 {
101 public:
102 struct Settings : public Inherit<Object, Settings>
103 {
104 unsigned int cpu_instrumentation_level = 1;
105 unsigned int gpu_instrumentation_level = 1;
106 uint32_t log_size = 16384;
107 uint32_t gpu_timestamp_size = 1024;
108 };
109
110 explicit Profiler(ref_ptr<Settings> in_settings = {});
111
112 ref_ptr<Settings> settings;
113 mutable ref_ptr<ProfileLog> log;
114
116 struct GPUStatsCollection : public Inherit<Object, GPUStatsCollection>
117 {
118 ref_ptr<Device> device;
119 mutable ref_ptr<QueryPool> queryPool;
120 mutable std::atomic<uint32_t> queryIndex = 0;
121 std::vector<uint64_t> references;
122 std::vector<uint64_t> timestamps;
123 };
124
126 struct FrameStatsCollection
127 {
128 FrameStatsCollection() :
129 perDeviceGpuStats(VSG_MAX_DEVICES) {}
130
131 std::vector<ref_ptr<GPUStatsCollection>> perDeviceGpuStats;
132 };
133
134 mutable size_t frameIndex = 0;
135 mutable std::vector<FrameStatsCollection> perFrameGPUStats;
136
137 void writeGpuTimestamp(CommandBuffer& commandBuffer, uint64_t reference, VkPipelineStageFlagBits pipelineStage) const;
138 VkResult getGpuResults(FrameStatsCollection& frameStats) const;
139
140 public:
141 void setThreadName(const std::string& /*name*/) const override;
142
143 void enterFrame(const SourceLocation* /*sl*/, uint64_t& /*reference*/, FrameStamp& /*frameStamp*/) const override;
144 void leaveFrame(const SourceLocation* /*sl*/, uint64_t& /*reference*/, FrameStamp& /*frameStamp*/) const override;
145
146 void enter(const SourceLocation* /*sl*/, uint64_t& /*reference*/, const Object* /*object*/ = nullptr) const override;
147 void leave(const SourceLocation* /*sl*/, uint64_t& /*reference*/, const Object* /*object*/ = nullptr) const override;
148
149 void enterCommandBuffer(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/) const override;
150 void leaveCommandBuffer(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/) const override;
151
152 void enter(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/, const Object* /*object*/ = nullptr) const override;
153 void leave(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/, const Object* /*object*/ = nullptr) const override;
154
155 void finish() const override;
156 };
157 VSG_type_name(Profiler)
158} // namespace vsg
vsg::Inherit
Definition Inherit.h:28
vsg::Object
Definition Object.h:60
vsg::ProfileLog
Definition Profiler.h:23
vsg::ProfileLog::Entry
Definition Profiler.h:37
vsg::SourceLocation
Definition Instrumentation.h:40