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

vsg 1.1.13 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/state/QueryPool.h>
17#include <vsg/ui/FrameStamp.h>
18#include <vsg/utils/Instrumentation.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
100 class VSG_DECLSPEC GPUStatsCollection : public Inherit<Object, GPUStatsCollection>
101 {
102 public:
103 ref_ptr<Device> device;
104 mutable ref_ptr<QueryPool> queryPool;
105 mutable std::atomic<uint32_t> queryIndex = 0;
106 std::vector<uint64_t> references;
107 std::vector<uint64_t> timestamps;
108
109 void writeGpuTimestamp(CommandBuffer& commandBuffer, uint64_t reference, VkPipelineStageFlagBits pipelineStage);
110 };
111 VSG_type_name(GPUStatsCollection);
112
113 class VSG_DECLSPEC Profiler : public Inherit<Instrumentation, Profiler>
114 {
115 public:
116 struct Settings : public Inherit<Object, Settings>
117 {
118 unsigned int cpu_instrumentation_level = 1;
119 unsigned int gpu_instrumentation_level = 1;
120 uint32_t log_size = 16384;
121 uint32_t gpu_timestamp_size = 1024;
122 };
123
124 explicit Profiler(ref_ptr<Settings> in_settings = {});
125
126 ref_ptr<Settings> settings;
127 mutable ref_ptr<ProfileLog> log;
128
130 struct FrameStatsCollection
131 {
132 FrameStatsCollection() {}
133 std::vector<ref_ptr<GPUStatsCollection>> gpuStats;
134 };
135
136 mutable size_t frameIndex = 0;
137 mutable std::vector<FrameStatsCollection> perFrameGPUStats;
138
139 VkResult getGpuResults(FrameStatsCollection& frameStats) const;
140
141 public:
142 void setThreadName(const std::string& /*name*/) const override;
143
144 void enterFrame(const SourceLocation* /*sl*/, uint64_t& /*reference*/, FrameStamp& /*frameStamp*/) const override;
145 void leaveFrame(const SourceLocation* /*sl*/, uint64_t& /*reference*/, FrameStamp& /*frameStamp*/) const override;
146
147 void enter(const SourceLocation* /*sl*/, uint64_t& /*reference*/, const Object* /*object*/ = nullptr) const override;
148 void leave(const SourceLocation* /*sl*/, uint64_t& /*reference*/, const Object* /*object*/ = nullptr) const override;
149
150 void enterCommandBuffer(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/) const override;
151 void leaveCommandBuffer(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/) const override;
152
153 void enter(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/, const Object* /*object*/ = nullptr) const override;
154 void leave(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/, const Object* /*object*/ = nullptr) const override;
155
156 void finish() const override;
157 };
158 VSG_type_name(Profiler);
159
160} // namespace vsg
vsg::CommandBuffer
CommandBuffer encapsulates VkCommandBuffer.
Definition CommandBuffer.h:30
vsg::FrameStamp
FrameStamp represents the time and frame count of a specific frame.
Definition FrameStamp.h:22
vsg::GPUStatsCollection
resources for collecting GPU stats for a single device on a single frame
Definition Profiler.h:101
vsg::Object
Definition Object.h:60
vsg::ProfileLog
Definition Profiler.h:23
vsg::Profiler
Definition Profiler.h:114
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::ProfileLog::Entry
Definition Profiler.h:37
vsg::Profiler::FrameStatsCollection
resources for collecting GPU stats for all devices for a single frame
Definition Profiler.h:131
vsg::Profiler::Settings
Definition Profiler.h:117
vsg::SourceLocation
Definition Instrumentation.h:40