15#include <vsg/io/stream.h>
16#include <vsg/ui/FrameStamp.h>
17#include <vsg/utils/Instrumentation.h>
18#include <vsg/vk/Device.h>
40 vsg::time_point cpuTime = {};
43 const Object*
object =
nullptr;
44 uint64_t reference = 0;
45 std::thread::id thread_id = {};
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;
54 Entry& enter(uint64_t& reference, Type type)
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();
67 Entry& leave(uint64_t& reference, Type type)
69 Entry& enter_entry = entry(reference);
71 uint64_t new_reference = index.fetch_add(1);
72 Entry& leave_entry = entry(new_reference);
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;
85 Entry& entry(uint64_t reference)
87 return entries[reference % entries.size()];
90 void report(std::ostream& out);
91 uint64_t report(std::ostream& out, uint64_t reference);
94 void read(Input& input)
override;
95 void write(Output& output)
const override;
97 VSG_type_name(ProfileLog)
99 class VSG_DECLSPEC Profiler :
public Inherit<Instrumentation, Profiler>
102 struct Settings :
public Inherit<Object, Settings>
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;
110 Profiler(ref_ptr<Settings> in_settings = {});
112 ref_ptr<Settings> settings;
113 mutable ref_ptr<ProfileLog> log;
116 struct GPUStatsCollection :
public Inherit<Object, GPUStatsCollection>
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;
126 struct FrameStatsCollection
128 FrameStatsCollection() :
129 perDeviceGpuStats(VSG_MAX_DEVICES) {}
131 std::vector<ref_ptr<GPUStatsCollection>> perDeviceGpuStats;
134 mutable size_t frameIndex = 0;
135 mutable std::vector<FrameStatsCollection> perFrameGPUStats;
137 void writeGpuTimestamp(CommandBuffer& commandBuffer, uint64_t reference, VkPipelineStageFlagBits pipelineStage)
const;
138 VkResult getGpuResults(FrameStatsCollection& frameStats)
const;
141 void setThreadName(
const std::string& )
const override;
143 void enterFrame(
const SourceLocation* , uint64_t& , FrameStamp& )
const override;
144 void leaveFrame(
const SourceLocation* , uint64_t& , FrameStamp& )
const override;
146 void enter(
const SourceLocation* , uint64_t& ,
const Object* =
nullptr)
const override;
147 void leave(
const SourceLocation* , uint64_t& ,
const Object* =
nullptr)
const override;
149 void enterCommandBuffer(
const SourceLocation* , uint64_t& , CommandBuffer& )
const override;
150 void leaveCommandBuffer(
const SourceLocation* , uint64_t& , CommandBuffer& )
const override;
152 void enter(
const SourceLocation* , uint64_t& , CommandBuffer& ,
const Object* =
nullptr)
const override;
153 void leave(
const SourceLocation* , uint64_t& , CommandBuffer& ,
const Object* =
nullptr)
const override;
155 void finish()
const override;
157 VSG_type_name(Profiler)
Definition Instrumentation.h:40