SpiecsEngine
 
Loading...
Searching...
No Matches
NsightPerfGPUProfilerOneshotCollection.cpp
Go to the documentation of this file.
1/**
2* @file NsightPerfGPUProfilerOneshotCollection.cpp
3* @brief The NsightPerfGPUProfilerOneshotCollection Class Implementation.
4* @author Spices
5*/
6
7#include "Pchheader.h"
10#include "Render/FrameInfo.h"
11#include "Core/Library/ProcessLibrary.h"
12
13#include <NvPerfCounterConfiguration.h>
14#include <NvPerfCpuMarkerTrace.h>
15#include <NvPerfVulkan.h>
16#include <NvPerfReportGeneratorVulkan.h>
17
18namespace Spices {
19
21
22 constexpr size_t numRangesPerFrame = 50; // @brief Sampling max level depth.
24
26 : m_VulkanState(state)
27 , m_IsInSession(false)
29 {
31
32 /**
33 * @brief Create this in construct.
34 */
35 Create(state);
36
37 /**
38 * @brief Init Query Pool here.
39 */
40 InitApiTracer(state);
41
42 /**
43 * @brief End Session after initalized,
44 * for Session can not be owned by multiple instance.
45 */
46 Reset();
47 }
48
50 {
52
53 if (!m_NsightPerfGPUProfilerOneshotCollection)
54 {
55 m_NsightPerfGPUProfilerOneshotCollection = std::make_shared<NsightPerfGPUProfilerOneshotCollection>(state);
56 }
57 }
58
60 {
62
63 /**
64 * @brief Sampling Frequence.
65 */
66 constexpr size_t samplingIntervalInNanoSeconds = 1024 * 16;
67 constexpr size_t maxIntervalPerFrameInNanoSeconds = 100 * 1000 * 1000; // 100ms
68
69 /**
70 * @brief Get traces.yaml path on sampling finished.
71 */
72 auto onStopSampling = [this](const char* outputDirectory) {
73 m_OutputDirectory = outputDirectory;
74 };
75
76 /**
77 * @brief Initialize.
78 */
79 NSPERF_CHECK(m_PeriodicSamplerOneShot.Initialize(
80 state.m_Instance ,
81 state.m_PhysicalDevice ,
82 state.m_Device ,
83 samplingIntervalInNanoSeconds ,
84 maxIntervalPerFrameInNanoSeconds ,
85 numRangesPerFrame ,
86 onStopSampling
87 ))
88
89 /**
90 * @brief Set Output folder.
91 */
92 m_PeriodicSamplerOneShot.m_outputOption.directoryName = SPICES_GPUPROFILEONESHOT_PATH;
93 m_PeriodicSamplerOneShot.m_outputOption.appendDateTime = nv::perf::sampler::PeriodicSamplerOneShotVulkan::AppendDateTime::yes;
94
95 /**
96 * @brief Set InSession true.
97 */
98 m_IsInSession = true;
99 }
100
101 void NsightPerfGPUProfilerOneshotCollection::BeginFrame(VulkanState& state, VkCommandBuffer commandBuffer)
102 {
104
105 /**
106 * @brief Begin Session if want capture this frame.
107 */
109 {
110 Create(state);
111 m_PeriodicSamplerOneShot.StartCollectionOnFrameEnd();
112 }
113
114 /**
115 * @brief Clear apiTracer.
116 */
117 auto& apiTracer = m_ApiTracers[FrameInfo::Get().m_FrameIndex];
118 apiTracer.ClearData();
119 apiTracer.ResetQueries(commandBuffer);
120 }
121
123 VkCommandBuffer cmd ,
124 const std::string& name ,
125 size_t nestingLevel ,
126 uint32_t index
127 )
128 {
130
131 auto& apiTracer = m_ApiTracers[FrameInfo::Get().m_FrameIndex];
132 NSPERF_CHECK(apiTracer.BeginRange(cmd, name.c_str(), nestingLevel, m_FrameLevelTraceIndice[index]))
133 }
134
135 void NsightPerfGPUProfilerOneshotCollection::EndRange(VkCommandBuffer cmd, size_t index)
136 {
138
139 auto& apiTracer = m_ApiTracers[FrameInfo::Get().m_FrameIndex];
140 NSPERF_CHECK(apiTracer.EndRange(cmd, index))
141 }
142
144 {
146
147 /**
148 * @brief End this frame.
149 */
150 NSPERF_CHECK(m_PeriodicSamplerOneShot.OnFrameEnd())
151
152 if (!m_OutputDirectory.empty())
153 {
154 /**
155 * @brief ResolveQueries.
156 */
157 std::vector<nv::perf::mini_trace::APITraceData> apiTraceData;
158 for (auto& apiTracer : m_ApiTracers)
159 {
160 NSPERF_CHECK(apiTracer.ResolveQueries(apiTraceData))
161 }
162
163 /**
164 * @brief Write traces to files.
165 */
166 const std::string yamlText = SerializeAPITraceDataToYaml(apiTraceData);
167 const std::string filename = std::string(m_OutputDirectory) + NV_PERF_PATH_SEPARATOR + "traces.yaml";
168 std::ofstream file(filename);
169 if (file.is_open())
170 {
171 file << yamlText;
172 }
173 else
174 {
175 std::stringstream ss;
176 ss << "Failed to open file: " << filename;
177 SPICES_CORE_WARN(ss.str());
178 }
179
180 /**
181 * @brief Open timelineview
182 */
183 std::stringstream ss;
184 ss << SPICES_EXTENT_PROCESS_PATH << "TimelineViewer/TimelineViewer.exe";
185 ProcessLibrary::OpenProcess(ss.str().c_str());
186
187 /**
188 * @brief Clear and Reset.
189 */
191 Reset();
192 SPICES_CORE_INFO("Nsight Perf: One-Shot sampling Finished.");
193 }
194
195 /**
196 * @brief Do not want to capture next frame.
197 */
199 {
201 }
202
203 /**
204 * @brief Debug Status.
205 */
206 SamplerStatus samplerStatus = m_PeriodicSamplerOneShot.GetSamplerStatus();
207 if (samplerStatus == SamplerStatus::Failed)
208 {
209 SPICES_CORE_ERROR("Nsight Perf: Failed to initialize");
210 }
211 else if (samplerStatus == SamplerStatus::Sampling)
212 {
213 SPICES_CORE_INFO("Nsight Perf: One-Shot is sampling.");
214 }
215 }
216
218 {
220
221 /**
222 * @brief Initialize apiTracer.
223 */
224 m_FrameLevelTraceIndice.resize(numFramesToSample, 0);
225 m_ApiTracers.resize(numFramesToSample);
226 for (auto& apiTracer : m_ApiTracers)
227 {
228 NSPERF_CHECK(apiTracer.Initialize(state.m_Instance, state.m_PhysicalDevice, state.m_Device, numRangesPerFrame))
229 }
230 }
231
233 {
235
236 NSPERF_CHECK(m_PeriodicSamplerOneShot.Reset())
237
238 m_IsInSession = false;
239 }
240
242 {
244
245 Reset();
247 }
248
250 {
252
253 /**
254 * @brief Destroy Query Pool.
255 */
256 for (auto& apiTracer : m_ApiTracers)
257 {
258 apiTracer.Destroy();
259 }
260 }
261
263 {
265
267 }
268
269}
#define NSPERF_CHECK(val)
#define SPICES_PROFILE_ZONE
static std::shared_ptr< NsightPerfGPUProfilerOneshotCollection > m_NsightPerfGPUProfilerOneshotCollection
This Single Instance.
void BeginRange(VkCommandBuffer cmd, const std::string &name, size_t nestingLevel, uint32_t index)
Begin recording a Oneshot Collection Range.
NsightPerfGPUProfilerOneshotCollection(VulkanState &state)
Constructor Function.
void EndRange(VkCommandBuffer cmd, size_t index)
End recording a Oneshot Collection Range.
void BeginFrame(VulkanState &state, VkCommandBuffer commandBuffer)
Begin a Frame.
static void CreateInstance(VulkanState &state)
Create this Single Instance.
Wrapper of Nvidia Nsight Performance OneshotCollection.
static bool OpenProcess(const char *processPath, const char *commandLine="")
Open a Process with command.
Process Static Function Library.
This struct contains all Vulkan object in used global.
Definition VulkanUtils.h:74