forked from GPUOpen-Tools/gpu_performance_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpa_pass.cc
605 lines (494 loc) · 18 KB
/
gpa_pass.cc
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//==============================================================================
// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved.
/// @author AMD Developer Tools Team
/// @file
/// @brief GPA Pass Object Implementation.
//==============================================================================
#include "gpu_perf_api_common/gpa_pass.h"
#include "gpu_perf_api_counter_generator/gpa_hardware_counters.h"
#include "gpu_perf_api_common/gpa_command_list.h"
#include "gpu_perf_api_common/gpa_context_counter_mediator.h"
GpaPass::GpaPass(IGpaSession* gpa_session, PassIndex pass_index, GpaCounterSource counter_source, CounterList* pass_counters)
: gpa_session_(gpa_session)
, pass_index_(pass_index)
, counter_source_(counter_source)
, is_result_collected_(false)
, is_result_ready_(false)
, is_timing_pass_(false)
, gpa_internal_sample_counter_(0U)
, command_list_counter_(0U)
, is_all_sample_valid_in_pass_(false)
, is_pass_complete_(false)
{
counter_list_ = pass_counters;
if (nullptr != counter_list_ && !counter_list_->empty())
{
const GpaHardwareCounters* hardware_counters =
GpaContextCounterMediator::Instance()->GetCounterAccessor(GetGpaSession()->GetParentContext())->GetHardwareCounters();
if (hardware_counters->IsTimeCounterIndex(counter_list_->at(0)))
{
is_timing_pass_ = true;
}
}
}
GpaPass::~GpaPass()
{
gpa_cmd_list_mutex_.lock();
for (auto it = gpa_cmd_lists_.begin(); it != gpa_cmd_lists_.end(); ++it)
{
delete (*it);
}
gpa_cmd_lists_.clear();
gpa_cmd_list_mutex_.unlock();
std::lock_guard<std::mutex> lock(samples_unordered_map_mutex_);
for (auto sample_pair : samples_unordered_map_)
{
GpaSample* sample = sample_pair.second;
delete sample;
}
samples_unordered_map_.clear();
}
GpaCounterSource GpaPass::GetCounterSource() const
{
return counter_source_;
}
GpaSample* GpaPass::GetSampleById(ClientSampleId sample_id) const
{
std::lock_guard<std::mutex> lock(samples_unordered_map_mutex_);
return GetSampleByIdNotThreadSafe(sample_id);
}
GpaSample* GpaPass::GetSampleByIdNotThreadSafe(ClientSampleId client_sample_id) const
{
GpaSample* ret_val = nullptr;
if (samples_unordered_map_.find(client_sample_id) != samples_unordered_map_.end())
{
ret_val = samples_unordered_map_.at(client_sample_id);
}
return ret_val;
}
GpaSample* GpaPass::CreateAndBeginSample(ClientSampleId client_sample_id, IGpaCommandList* gpa_cmd_list)
{
std::lock_guard<std::mutex> lock(samples_unordered_map_mutex_);
GpaSample* sample = nullptr;
if (!DoesSampleExistNotThreadSafe(client_sample_id))
{
if (GpaCounterSource::kHardware == counter_source_)
{
sample = CreateApiSpecificSample(gpa_cmd_list, GpaSampleType::kHardware, client_sample_id);
}
else if (GpaCounterSource::kSoftware == counter_source_)
{
sample = CreateApiSpecificSample(gpa_cmd_list, GpaSampleType::kSoftware, client_sample_id);
}
if (nullptr != sample)
{
if (!gpa_cmd_list->BeginSample(client_sample_id, sample))
{
GPA_LOG_ERROR("Unable to begin sample in pass.");
delete sample;
sample = nullptr;
}
else
{
samples_unordered_map_.insert(std::pair<ClientSampleId, GpaSample*>(client_sample_id, sample));
}
}
else
{
GPA_LOG_ERROR("Unable to create sample.");
}
}
else
{
GPA_LOG_ERROR("Sample Id already exists.");
}
return sample;
}
bool GpaPass::ContinueSample(ClientSampleId src_sample_id, IGpaCommandList* primary_gpa_cmd_list)
{
std::lock_guard<std::mutex> lock(samples_unordered_map_mutex_);
// 1. Validate that sample already exists in the pass.
// 2. Create a new sample on the cmd.
// 3. Link the new GPA Sample to the old GPA Sample.
// 4. Update the sample map.
// In continuing a sample, we need to check only that the srcSampleId exists (it may not be closed at this point)
// and primary command list is not same as that of that of the src_sample_id.
// Rather than closing the sample over here, we will let the sample close upon closing the command list.
// In this way, we do not need to synchronize the samples on different command list (in multi-thread application they might be on different thread).
// We will mark the parent sample as to be continued by the client.
bool success = false;
GpaSample* parent_sample = GetSampleByIdNotThreadSafe(src_sample_id);
if (nullptr != parent_sample)
{
IGpaCommandList* parent_sample_cmd_list = parent_sample->GetCmdList();
// Validate that both command list are different and passed command list is not secondary.
if (nullptr != parent_sample_cmd_list && nullptr != primary_gpa_cmd_list && kGpaCommandListSecondary != primary_gpa_cmd_list->GetCmdType() &&
primary_gpa_cmd_list != parent_sample_cmd_list)
{
if (nullptr != parent_sample && primary_gpa_cmd_list->IsCommandListRunning() && primary_gpa_cmd_list->IsLastSampleClosed())
{
GpaSampleType sample_type = (GpaCounterSource::kHardware == GetCounterSource()) ? GpaSampleType::kHardware : GpaSampleType::kSoftware;
// We don't need to add this sample to the sample map as it will be linked to the parent sample.
GpaSample* new_sample = CreateApiSpecificSample(primary_gpa_cmd_list, sample_type, src_sample_id);
if (nullptr != new_sample)
{
if (!primary_gpa_cmd_list->BeginSample(src_sample_id, new_sample))
{
GPA_LOG_ERROR("Unable to begin continued sample in pass.");
delete new_sample;
new_sample = nullptr;
}
else
{
parent_sample->SetAsContinuedByClient();
// Link the sample to the parent sample.
parent_sample->LinkContinuingSample(new_sample);
success = true;
}
}
}
else
{
GPA_LOG_ERROR(
"Unable to continue sample: Either the specified command list has already been closed or the previous sample has not been closed.");
}
}
else
{
GPA_LOG_ERROR(
"Unable to continue sample: The specified command list must be a secondary command list and it must be different than the parent sample's "
"command list.");
}
}
else
{
GPA_LOG_ERROR("Unable to continue sample: The specified sample id was not found in this pass.");
}
return success;
}
IGpaCommandList* GpaPass::CreateCommandList(void* cmd_list, GpaCommandListType cmd_type)
{
IGpaCommandList* ret_cmd_list = CreateApiSpecificCommandList(cmd_list, command_list_counter_, cmd_type);
command_list_counter_++;
if (nullptr != ret_cmd_list)
{
AddCommandList(ret_cmd_list);
}
return ret_cmd_list;
}
SampleCount GpaPass::GetSampleCount() const
{
std::lock_guard<std::mutex> lock(samples_unordered_map_mutex_);
return static_cast<SampleCount>(samples_unordered_map_.size());
}
bool GpaPass::GetSampleIdByIndex(SampleIndex sample_index, ClientSampleId& client_sample_id) const
{
std::lock_guard<std::mutex> lock(samples_unordered_map_mutex_);
bool found = client_gpa_samples_map_.find(sample_index) != client_gpa_samples_map_.end();
if (found)
{
client_sample_id = client_gpa_samples_map_.at(sample_index);
}
return found;
}
bool GpaPass::IsAllSampleValidInPass() const
{
if (!is_all_sample_valid_in_pass_)
{
bool success = true;
std::lock_guard<std::mutex> lock_samples(samples_unordered_map_mutex_);
for (auto sample_iter = samples_unordered_map_.cbegin(); sample_iter != samples_unordered_map_.cend(); ++sample_iter)
{
success &= sample_iter->second->IsSampleValid();
}
if (success)
{
is_all_sample_valid_in_pass_ = true;
}
}
return is_all_sample_valid_in_pass_;
}
CounterCount GpaPass::GetEnabledCounterCount() const
{
return static_cast<unsigned int>(used_counter_list_for_pass_.size());
}
CounterCount GpaPass::GetSkippedCounterCount() const
{
return static_cast<unsigned int>(skipped_counter_list_.size());
}
PassIndex GpaPass::GetIndex() const
{
return pass_index_;
}
bool GpaPass::IsTimingPass() const
{
return is_timing_pass_;
}
bool GpaPass::UpdateResults()
{
std::lock_guard<std::mutex> lock(samples_unordered_map_mutex_);
if (!is_result_collected_)
{
bool tmp_all_results_collected = true;
for (auto sampleIter = samples_unordered_map_.begin(); sampleIter != samples_unordered_map_.end(); ++sampleIter)
{
tmp_all_results_collected &= sampleIter->second->UpdateResults();
}
is_result_collected_ = tmp_all_results_collected;
}
return is_result_collected_;
}
GpaStatus GpaPass::IsComplete() const
{
GpaStatus ret_status = kGpaStatusOk;
std::lock_guard<std::mutex> lock_cmd_list(gpa_cmd_list_mutex_);
if (!is_pass_complete_)
{
bool completed = true;
for (auto cmd_iter = gpa_cmd_lists_.cbegin(); cmd_iter != gpa_cmd_lists_.cend() && completed; ++cmd_iter)
{
completed = !(*cmd_iter)->IsCommandListRunning();
}
if (!completed)
{
ret_status = kGpaStatusErrorCommandListNotEnded;
}
else
{
completed = IsAllSampleValidInPass();
if (!completed)
{
ret_status = kGpaStatusErrorSampleNotEnded;
}
else
{
is_pass_complete_ = true;
}
}
}
return ret_status;
}
bool GpaPass::IsResultReady() const
{
std::lock_guard<std::mutex> lock_cmd_list(gpa_cmd_list_mutex_);
bool is_ready = is_result_ready_;
if (!is_ready)
{
is_ready = true;
for (auto cmdIter = gpa_cmd_lists_.cbegin(); cmdIter != gpa_cmd_lists_.cend() && is_ready; ++cmdIter)
{
is_ready &= (*cmdIter)->IsResultReady();
}
if (is_ready)
{
is_result_ready_ = true;
}
}
return is_ready;
}
bool GpaPass::IsResultCollected() const
{
return is_result_collected_;
}
GpaStatus GpaPass::GetResult(ClientSampleId client_sample_id, CounterIndex internal_counter_index, GpaUInt64* result_buffer) const
{
*result_buffer = 0;
std::lock_guard<std::mutex> lock(samples_unordered_map_mutex_);
GpaStatus status = kGpaStatusOk;
SamplesMap::const_iterator sample_iter = samples_unordered_map_.find(client_sample_id);
if (sample_iter == samples_unordered_map_.cend())
{
GPA_LOG_ERROR("Invalid SampleId supplied while getting pass results.");
status = kGpaStatusErrorInvalidParameter;
}
else
{
CounterIndex counter_index_within_sample;
if (GetCounterIndexInPass(internal_counter_index, &counter_index_within_sample))
{
if (!sample_iter->second->GetResult(counter_index_within_sample, result_buffer))
{
GPA_LOG_ERROR("Failed to get counter result within pass.");
status = kGpaStatusErrorFailed;
}
}
else if (skipped_counter_list_.find(internal_counter_index) == skipped_counter_list_.end())
{
// We didn't skip the counter, so we wrongly think it was in this pass.
GPA_LOG_ERROR("Failed to find internal counter index within pass counters.");
status = kGpaStatusErrorInvalidParameter;
}
}
return status;
}
bool GpaPass::DoesSampleExist(ClientSampleId client_sample_id) const
{
std::lock_guard<std::mutex> lock(samples_unordered_map_mutex_);
return DoesSampleExistNotThreadSafe(client_sample_id);
}
bool GpaPass::DoesSampleExistNotThreadSafe(ClientSampleId client_sample_id) const
{
bool exists = false;
if (samples_unordered_map_.find(client_sample_id) != samples_unordered_map_.end())
{
exists = true;
}
return exists;
}
bool GpaPass::DoesCommandListExist(IGpaCommandList* gpa_cmd_list) const
{
bool exists = false;
std::lock_guard<std::mutex> lock(gpa_cmd_list_mutex_);
for (auto const_iter = gpa_cmd_lists_.cbegin(); !exists && const_iter != gpa_cmd_lists_.cend(); ++const_iter)
{
if (*const_iter == gpa_cmd_list)
{
exists = true;
}
}
return exists;
}
const GpaCommandLists& GpaPass::GetCmdList() const
{
return gpa_cmd_lists_;
}
void GpaPass::EnableCounterForPass(const CounterIndex& counter_index)
{
std::lock_guard<std::mutex> lock_pass(counter_list_mutex_);
used_counter_list_for_pass_.push_back(counter_index);
}
void GpaPass::DisableCounterForPass(const CounterIndex& counter_index)
{
std::lock_guard<std::mutex> lock_pass(counter_list_mutex_);
skipped_counter_list_.insert(counter_index);
}
void GpaPass::EnableAllCountersForPass()
{
std::lock_guard<std::mutex> lock_pass(counter_list_mutex_);
used_counter_list_for_pass_ = *counter_list_;
skipped_counter_list_.clear();
}
void GpaPass::DisableAllCountersForPass()
{
std::lock_guard<std::mutex> lock_pass(counter_list_mutex_);
skipped_counter_list_.clear();
skipped_counter_list_.insert(counter_list_->begin(), counter_list_->end());
used_counter_list_for_pass_.clear();
}
CounterCount GpaPass::GetNumEnabledCountersForPass() const
{
std::lock_guard<std::mutex> lock_pass(counter_list_mutex_);
return static_cast<CounterCount>(counter_list_->size() - skipped_counter_list_.size());
}
bool GpaPass::GetCounterIndexInPass(CounterIndex internal_counter_index, CounterIndex* counter_indices_in_pass) const
{
bool found = false;
if (!used_counter_list_for_pass_.empty())
{
CounterList::const_iterator iter = std::find(used_counter_list_for_pass_.begin(), used_counter_list_for_pass_.end(), internal_counter_index);
if (used_counter_list_for_pass_.end() != iter)
{
*counter_indices_in_pass = static_cast<CounterIndex>(iter - used_counter_list_for_pass_.begin());
found = true;
}
}
return found;
}
bool GpaPass::GetCounterByIndexInPass(CounterIndex counter_index_in_pass, CounterIndex* internal_counter_index) const
{
if (!internal_counter_index)
{
assert(0);
return false;
}
bool found = counter_index_in_pass < used_counter_list_for_pass_.size();
*internal_counter_index = static_cast<CounterIndex>(-1);
if (found)
{
*internal_counter_index = used_counter_list_for_pass_[counter_index_in_pass];
}
return found;
}
bool GpaPass::IsResultsCollectedFromDriver() const
{
return is_result_collected_;
}
const IGpaCounterAccessor* GpaPass::GetSessionContextCounterAccessor() const
{
return GpaContextCounterMediator::Instance()->GetCounterAccessor(GetGpaSession()->GetParentContext());
}
void GpaPass::AddCommandList(IGpaCommandList* gpa_command_list)
{
std::lock_guard<std::mutex> lock_cmd_list(gpa_cmd_list_mutex_);
gpa_cmd_lists_.push_back(gpa_command_list);
}
void GpaPass::LockCommandListMutex() const
{
gpa_cmd_list_mutex_.lock();
}
void GpaPass::UnlockCommandListMutex() const
{
gpa_cmd_list_mutex_.unlock();
}
void GpaPass::AddClientSample(ClientSampleId sample_id, GpaSample* gpa_sample)
{
samples_unordered_map_mutex_.lock();
samples_unordered_map_.insert(std::pair<ClientSampleId, GpaSample*>(sample_id, gpa_sample));
unsigned int internal_sample_id = gpa_internal_sample_counter_.fetch_add(1);
client_gpa_samples_map_.insert(std::pair<unsigned int, unsigned int>(internal_sample_id, sample_id));
samples_unordered_map_mutex_.unlock();
}
void GpaPass::IteratePassCounterList(std::function<bool(const CounterIndex& counter_index)> function) const
{
bool next = true;
for (auto it = (*counter_list_).cbegin(); it != (*counter_list_).cend() && next; ++it)
{
next = function(*it);
}
}
void GpaPass::IterateEnabledCounterList(std::function<bool(const CounterIndex& counter_index)> function) const
{
bool next = true;
for (auto it = used_counter_list_for_pass_.cbegin(); it != used_counter_list_for_pass_.cend() && next; ++it)
{
next = function(*it);
}
}
void GpaPass::IterateSkippedCounterList(std::function<bool(const CounterIndex& counter_index)> function) const
{
bool next = true;
for (auto it = skipped_counter_list_.cbegin(); it != skipped_counter_list_.cend() && next; ++it)
{
next = function(*it);
}
}
GpaUInt32 GpaPass::GetBottomToBottomTimingDurationCounterIndex() const
{
const GpaHardwareCounters* hardware_counters =
GpaContextCounterMediator::Instance()->GetCounterAccessor(GetGpaSession()->GetParentContext())->GetHardwareCounters();
for (GpaUInt32 i = 0; i < static_cast<GpaUInt32>(counter_list_->size()); i++)
{
if ((*counter_list_)[i] == hardware_counters->gpu_time_bottom_to_bottom_duration_counter_index_)
{
return i;
}
}
return static_cast<GpaUInt32>(-1);
}
GpaUInt32 GpaPass::GetTopToBottomTimingDurationCounterIndex() const
{
const GpaHardwareCounters* hardware_counters =
GpaContextCounterMediator::Instance()->GetCounterAccessor(GetGpaSession()->GetParentContext())->GetHardwareCounters();
for (GpaUInt32 i = 0; i < static_cast<GpaUInt32>(counter_list_->size()); i++)
{
if ((*counter_list_)[i] == hardware_counters->gpu_time_top_to_bottom_duration_counter_index_)
{
return i;
}
}
return static_cast<GpaUInt32>(-1);
}
IGpaSession* GpaPass::GetGpaSession() const
{
return gpa_session_;
}