forked from GPUOpen-Tools/gpu_performance_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpa_sample.cc
More file actions
291 lines (243 loc) · 6.75 KB
/
Copy pathgpa_sample.cc
File metadata and controls
291 lines (243 loc) · 6.75 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//==============================================================================
// Copyright (c) 2017-2020 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief GPA Sample Implementation
//==============================================================================
#include "gpa_sample.h"
#include "gpa_pass.h"
#include "gpa_command_list_interface.h"
GPASample::GPASample(GPAPass* pPass, IGPACommandList* pGpaCmdList, GpaSampleType sampleType, ClientSampleId clientSampleId)
: m_pPass(pPass)
, m_pGpaCmdList(pGpaCmdList)
, m_gpaSampleType(sampleType)
, m_clientSampleId(clientSampleId)
, m_driverSampleId(0)
, m_gpaSampleState(GPASampleState::INITIALIZED)
, m_pSampleResult(nullptr)
, m_pContinuingSample(nullptr)
, m_isOpened(false)
, m_isClosedByClient(false)
, m_isContinuedByClient(false)
, m_isCopiedSample(false)
{
m_isSecondary = (GPA_COMMAND_LIST_SECONDARY == pGpaCmdList->GetCmdType());
// First, only allocate result space for primary samples.
// If a secondary sample gets copied, then it will also get space allocated for the results.
if (!m_isSecondary)
{
AllocateSampleResultSpace();
}
}
GPA_THREAD_SAFE_FUNCTION bool GPASample::Begin()
{
bool result = false;
std::lock_guard<std::mutex> lockSample(m_sampleMutex);
result = BeginRequest();
if (result)
{
m_gpaSampleState = GPASampleState::STARTED;
m_isOpened = true;
}
return result;
}
/// Ends a counter sample.
/// \return True on success; false on error.
bool GPASample::End()
{
// make sure this request is valid before trying to end it.
if (m_gpaSampleState != GPASampleState::STARTED)
{
return false;
}
bool result = EndRequest();
if (result)
{
m_gpaSampleState = GPASampleState::PENDING_RESULTS;
}
return result;
}
bool GPASample::GetResult(CounterIndex counterIndexInSample, gpa_uint64* pResult) const
{
bool hasResult = false;
if (!IsSecondary() || IsCopied())
{
if (nullptr != pResult && IsResultCollected())
{
if (nullptr != m_pSampleResult && counterIndexInSample < m_pSampleResult->GetAsCounterSampleResult()->GetNumCounters() &&
nullptr != m_pSampleResult->GetAsCounterSampleResult()->GetResultBuffer())
{
hasResult = true;
*pResult = m_pSampleResult->GetAsCounterSampleResult()->GetResultBuffer()[counterIndexInSample];
}
else
{
GPA_LogError("Counter Index out of range.");
}
}
else
{
GPA_LogError("Either the sample is not completed or the result buffer is invalid.");
}
}
return hasResult;
}
void GPASample::SetDriverSampleId(const DriverSampleId& driverSampleId)
{
m_driverSampleId = driverSampleId;
}
DriverSampleId GPASample::GetDriverSampleId() const
{
return m_driverSampleId;
}
ClientSampleId GPASample::GetClientSampleId() const
{
return m_clientSampleId;
}
void GPASample::AllocateSampleResultSpace()
{
if (nullptr == m_pSampleResult)
{
m_pSampleResult = new (std::nothrow) GPACounterSampleResult(m_pPass->GetEnabledCounterCount());
}
}
bool GPASample::IsSecondary() const
{
return m_isSecondary;
}
GpaSampleType GPASample::GetGpaSampleType() const
{
return m_gpaSampleType;
}
GPASampleState GPASample::GetGpaSampleState() const
{
return m_gpaSampleState;
}
GPASampleResult* GPASample::GetSampleResultLocation() const
{
return m_pSampleResult;
}
GPASample* GPASample::GetContinuingSample() const
{
return m_pContinuingSample;
}
GPA_THREAD_SAFE_FUNCTION bool GPASample::SetAsClosedByClient()
{
bool success = false;
std::lock_guard<std::mutex> lockSample(m_sampleMutex);
if (!m_isContinuedByClient)
{
m_isClosedByClient = true;
success = true;
}
else
{
GPA_LogError("Sample has already been continued by client.");
}
return success;
}
bool GPASample::SetAsCopied()
{
bool success = false;
std::lock_guard<std::mutex> lockSample(m_sampleMutex);
m_gpaSampleState = GPASampleState::PENDING_RESULTS;
if (!m_isCopiedSample)
{
m_isCopiedSample = true;
AllocateSampleResultSpace();
success = true;
}
else
{
GPA_LogError("Sample has already been copied by client.");
}
return success;
}
bool GPASample::IsCopied() const
{
return m_isCopiedSample;
}
bool GPASample::IsClosedByClient() const
{
return m_isClosedByClient;
}
bool GPASample::IsContinuedByClient() const
{
return m_isContinuedByClient;
}
GPA_THREAD_SAFE_FUNCTION bool GPASample::SetAsContinuedByClient()
{
std::lock_guard<std::mutex> lockSample(m_sampleMutex);
bool success = false;
if (!m_isClosedByClient)
{
m_isContinuedByClient = true;
success = true;
}
else
{
GPA_LogError("Sample has already been closed by client.");
}
return success;
}
bool GPASample::IsSampleValid() const
{
// A sample is only valid if it is opened and either it is continued/copied on another command list or closed on the same command list on which it was created
bool valid = (m_isOpened && ((m_isClosedByClient && !m_isContinuedByClient) || (!m_isClosedByClient && m_isContinuedByClient))) || m_isCopiedSample;
return valid;
}
bool GPASample::IsClosed() const
{
return m_isClosedByClient || m_isContinuedByClient;
}
void GPASample::MarkAsCompleted()
{
m_gpaSampleState = GPASampleState::RESULTS_COLLECTED;
}
bool GPASample::IsSampleContinuing() const
{
return nullptr != m_pContinuingSample;
}
IGPACommandList* GPASample::GetCmdList() const
{
return m_pGpaCmdList;
}
bool GPASample::LinkContinuingSample(GPASample* pContinuingSample)
{
std::lock_guard<std::recursive_mutex> lock(m_continueSampleMutex);
if (nullptr == pContinuingSample)
{
return false;
}
bool success = true;
if (nullptr != m_pContinuingSample)
{
success &= m_pContinuingSample->LinkContinuingSample(pContinuingSample);
}
else
{
// Check whether the continuing sample is on different command list or not
if (pContinuingSample->m_pGpaCmdList == m_pGpaCmdList)
{
success = false;
}
else
{
m_pContinuingSample = pContinuingSample;
success = true;
}
}
return success;
}
GPAPass* GPASample::GetPass() const
{
return m_pPass;
}
GPASample::~GPASample()
{
if (nullptr != m_pContinuingSample)
{
delete m_pContinuingSample;
}
delete m_pSampleResult;
}