-
Notifications
You must be signed in to change notification settings - Fork 43
/
view_handler.h
772 lines (676 loc) · 27.5 KB
/
view_handler.h
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#ifndef SRC_API_VIEW_HANDLER_H_
#define SRC_API_VIEW_HANDLER_H_
#include <array>
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include <unordered_map>
#include "common.h"
#include "default_buffer_callbacks.h"
#include "pti_view.h"
#include "spdlog/spdlog.h"
#if defined(PTI_TRACE_SYCL)
#include "sycl_collector.h"
#endif
#include "overhead_kinds.h"
#include "unikernel.h"
#include "view_buffer.h"
#include "view_record_info.h"
#include "ze_collector.h"
using AskForBufferEvent = std::function<void(unsigned char**, size_t*)>;
using ReturnBufferEvent = std::function<void(unsigned char*, size_t, size_t)>;
using ViewInsert = std::function<void(void*, const ZeKernelCommandExecutionRecord&)>;
inline void MemCopyEvent(void* data, const ZeKernelCommandExecutionRecord& rec);
inline void MemFillEvent(void* data, const ZeKernelCommandExecutionRecord& rec);
inline void KernelEvent(void* data, const ZeKernelCommandExecutionRecord& rec);
inline void SyclRuntimeEvent(void* data, const ZeKernelCommandExecutionRecord& rec);
inline void OverheadCollectionEvent(void* data, const ZeKernelCommandExecutionRecord& rec);
inline void ZeChromeKernelStagesCallback(void* data,
std::vector<ZeKernelCommandExecutionRecord>& kcexecrec);
inline void SyclRuntimeViewCallback(void* data, ZeKernelCommandExecutionRecord& rec);
inline void OverheadCollectionCallback(void* data, ZeKernelCommandExecutionRecord& rec);
struct ViewData {
const char* fn_name = "";
ViewInsert callback;
};
inline const std::vector<ViewData>& GetViewNameAndCallback(pti_view_kind view) {
// clang-format off
static const std::map<pti_view_kind, std::vector<ViewData>> view_data_map =
{
{PTI_VIEW_DEVICE_GPU_KERNEL,
{
ViewData{"KernelEvent", KernelEvent}
}
},
{PTI_VIEW_SYCL_RUNTIME_CALLS,
{
ViewData{"SyclRuntimeEvent", SyclRuntimeEvent}
}
},
{PTI_VIEW_COLLECTION_OVERHEAD,
{
ViewData{"OverheadCollectionEvent", OverheadCollectionEvent}
}
},
{PTI_VIEW_DEVICE_GPU_MEM_COPY,
{
ViewData{"zeCommandListAppendMemoryCopy", MemCopyEvent},
}
},
{PTI_VIEW_DEVICE_GPU_MEM_FILL,
{
ViewData{"zeCommandListAppendMemoryFill", MemFillEvent}
}
},
};
// clang-format on
const auto result = view_data_map.find(view);
if (result == view_data_map.end()) {
throw std::out_of_range("No view record handling routine in table");
}
return result->second;
}
constexpr auto kDefaultBufferQueueDepth = 50UL;
static std::atomic<bool> external_collection_enabled = false;
struct PtiViewRecordHandler {
public:
using ViewBuffer = pti::view::utilities::ViewBuffer;
using ViewBufferQueue = pti::view::utilities::ViewBufferQueue;
using ViewBufferTable = pti::view::utilities::ViewBufferTable<std::thread::id>;
using ViewEventTable = pti::view::utilities::ThreadSafeHashTable<std::string, ViewInsert>;
using KernelNameStorageQueue =
pti::view::utilities::ViewRecordBufferQueue<std::unique_ptr<std::string>>;
PtiViewRecordHandler()
: get_new_buffer_(pti::view::defaults::DefaultBufferAllocation),
deliver_buffer_(pti::view::defaults::DefaultRecordParser),
buffer_consumer_([this]() { return BufferConsumer(); }) {
// Set queue depth based on number of hardware threads supported.
constexpr auto kBufQueueDepthMult = 2UL;
const auto threads_supported = std::thread::hardware_concurrency();
if (threads_supported) {
buffer_queue_.SetBufferDepth(kBufQueueDepthMult * threads_supported);
}
if (!collector_) {
CollectorOptions collector_options;
collector_options.kernel_tracing = true;
collector_ = ZeCollector::Create(collector_options, ZeChromeKernelStagesCallback, nullptr,
nullptr, nullptr);
overhead::SetOverheadCallback(OverheadCollectionCallback);
}
}
PtiViewRecordHandler(const PtiViewRecordHandler&) = delete;
PtiViewRecordHandler& operator=(const PtiViewRecordHandler&) = delete;
PtiViewRecordHandler(PtiViewRecordHandler&&) = delete;
PtiViewRecordHandler& operator=(PtiViewRecordHandler&&) = delete;
virtual ~PtiViewRecordHandler() {
overhead::overhead_collection_enabled = false;
DisableTracing();
collector_->DisableTracing();
delete collector_;
stop_consumer_thread_ = true;
buffer_queue_.ResetBufferDepth();
buffer_queue_.Push(ViewBuffer{}); // Stop consumer
if (buffer_consumer_.joinable()) {
buffer_consumer_.join();
}
}
void BufferConsumer() {
while (!stop_consumer_thread_) {
auto consume_buffer = buffer_queue_.Pop();
if (!consume_buffer.IsNull()) {
DeliverBuffer(std::move(consume_buffer));
}
}
}
inline pti_result FlushBuffers() {
view_buffers_.ForEach(
[this](const auto&, auto&& buffer) { buffer_queue_.Push(std::move(buffer)); });
buffer_queue_.WaitUntilEmptyOr(stop_consumer_thread_);
return PTI_SUCCESS;
}
template <typename T>
inline void InsertRecord(const T& view_record) {
static_assert(std::is_trivially_copyable<T>::value,
"One can only insert trivially copyable types into the "
"ViewBuffer (view records)");
auto& buffer = view_buffers_[std::this_thread::get_id()];
if (buffer.IsNull()) {
RequestNewBuffer(buffer);
}
buffer.Insert(view_record);
static_assert(SizeOfLargestViewRecord() != 0, "Largest record not avaiable on compile time");
if (buffer.FreeBytes() >= SizeOfLargestViewRecord()) {
// There's space to insert more records. No need for swap.
return;
}
buffer_queue_.Push(std::move(buffer));
}
inline pti_result RegisterBufferCallbacks(AskForBufferEvent&& get_new_buf,
ReturnBufferEvent&& return_new_buf) {
pti_result result = pti_result::PTI_ERROR_BAD_ARGUMENT;
auto get_new_buffer = std::move(get_new_buf);
auto deliver_buffer = std::move(return_new_buf);
if (!get_new_buffer || !deliver_buffer) {
// Keep using default callbacks
return result;
}
unsigned char* raw_buffer = nullptr;
std::size_t raw_buffer_size = 0;
get_new_buffer(&raw_buffer, &raw_buffer_size);
if (raw_buffer_size < SizeOfLargestViewRecord() || !raw_buffer) {
// Keep using default callbacks
result = pti_result::PTI_ERROR_BAD_ARGUMENT;
deliver_buffer(raw_buffer, raw_buffer_size, 0);
} else {
// User callback is fine, keep memory they gave us
result = pti_result::PTI_SUCCESS;
}
if (result == pti_result::PTI_SUCCESS) {
// Use user-defined callbacks
{
std::lock_guard<std::mutex> cb_lock(get_new_buffer_mtx_);
get_new_buffer_ = std::move(get_new_buffer);
}
{
std::lock_guard<std::mutex> cb_lock(deliver_buffer_mtx_);
deliver_buffer_ = std::move(deliver_buffer);
}
} else {
get_new_buffer_(&raw_buffer, &raw_buffer_size);
}
auto* buffer_to_replace = view_buffers_.TryFindElement(std::this_thread::get_id());
if (buffer_to_replace) {
DeliverBuffer(std::move(*buffer_to_replace));
}
view_buffers_[std::this_thread::get_id()].Refresh(raw_buffer, raw_buffer_size);
callbacks_set_ = true;
return result;
}
inline pti_result Enable(pti_view_kind type) {
if (!callbacks_set_) return pti_result::PTI_ERROR_NO_CALLBACKS_SET;
auto result = pti_result::PTI_SUCCESS;
bool collection_enabled = collection_enabled_;
//
// TBD --- implement and remove the checks for below pti_view_kinds
//
if ((type == pti_view_kind::PTI_VIEW_DEVICE_CPU_KERNEL) ||
(type == pti_view_kind::PTI_VIEW_LEVEL_ZERO_CALLS) ||
(type == pti_view_kind::PTI_VIEW_OPENCL_CALLS)) {
return pti_result::PTI_ERROR_NOT_IMPLEMENTED;
}
if (type == pti_view_kind::PTI_VIEW_COLLECTION_OVERHEAD) {
overhead::overhead_collection_enabled = true;
}
if (type == pti_view_kind::PTI_VIEW_EXTERNAL_CORRELATION) {
external_collection_enabled = true;
}
if (type == pti_view_kind::PTI_VIEW_SYCL_RUNTIME_CALLS) {
#if defined(PTI_TRACE_SYCL)
if (!view_event_map_.TryFindElement("SyclRuntimeEvent")) {
SyclCollector::Instance().SetCallback(SyclRuntimeViewCallback);
SyclCollector::Instance().EnableTracing();
collection_enabled = true;
}
#else
SPDLOG_DEBUG(
"Sycl tracing activated, but the library has not been compiled with "
"-DPTI_TRACE_SYCL");
return pti_result::PTI_ERROR_NOT_IMPLEMENTED;
#endif
}
if (collector_) {
collector_->EnableTracer();
collection_enabled = true;
}
collection_enabled_ = collection_enabled;
if (!collection_enabled_) {
return pti_result::PTI_ERROR_NOT_IMPLEMENTED;
}
try {
if (type != pti_view_kind::PTI_VIEW_EXTERNAL_CORRELATION) {
for (const auto& view_types : GetViewNameAndCallback(type)) {
view_event_map_[view_types.fn_name] = view_types.callback;
}
}
} catch (const std::out_of_range&) {
result = pti_result::PTI_ERROR_BAD_ARGUMENT;
}
return result;
}
inline pti_result Disable(pti_view_kind type) {
pti_result result = pti_result::PTI_SUCCESS;
if (type == pti_view_kind::PTI_VIEW_COLLECTION_OVERHEAD) {
overhead::overhead_collection_enabled = false;
}
if (type == pti_view_kind::PTI_VIEW_EXTERNAL_CORRELATION) {
external_collection_enabled = false;
}
if (type == pti_view_kind::PTI_VIEW_SYCL_RUNTIME_CALLS) {
#if defined(PTI_TRACE_SYCL)
SyclCollector::Instance().DisableTracing();
#endif
}
if (type == pti_view_kind::PTI_VIEW_INVALID) {
return pti_result::PTI_ERROR_BAD_ARGUMENT;
}
try {
for (const auto& view_types : GetViewNameAndCallback(type)) {
view_event_map_.Erase(view_types.fn_name);
}
} catch (const std::out_of_range&) {
result = pti_result::PTI_ERROR_BAD_ARGUMENT;
}
if (view_event_map_.Empty()) {
DisableTracing();
}
return result;
}
inline pti_result PushExternalKindId(pti_view_external_kind external_kind, uint64_t external_id) {
pti_result result = pti_result::PTI_SUCCESS;
pti_view_record_external_correlation ext_corr_rec = pti_view_record_external_correlation();
ext_corr_rec._external_id = external_id;
ext_corr_rec._external_kind = external_kind;
auto it = map_ext_corrid_vectors.find({external_kind});
if (it != map_ext_corrid_vectors.cend()) {
it->second.push(ext_corr_rec);
} else {
map_ext_corrid_vectors[{external_kind}].push(ext_corr_rec);
}
return result;
}
inline pti_result PopExternalKindId(pti_view_external_kind external_kind,
uint64_t* p_external_id) {
pti_result result = pti_result::PTI_SUCCESS;
auto it = map_ext_corrid_vectors.find({external_kind});
if (it != map_ext_corrid_vectors.cend()) {
pti_view_record_external_correlation ext_record = it->second.top();
if (p_external_id != nullptr) *p_external_id = ext_record._external_id;
it->second.pop();
if (!it->second.size()) {
map_ext_corrid_vectors.erase(it);
}
} else {
result = pti_result::PTI_ERROR_EXTERNAL_ID_QUEUE_EMPTY;
}
return result;
}
inline void operator()(const std::string& key, void* data,
const ZeKernelCommandExecutionRecord& rec) {
auto* view_event_callback = view_event_map_.TryFindElement(key);
if (view_event_callback) {
(*view_event_callback)(data, rec);
}
}
inline const char* InsertKernel(const std::string& name) {
auto kernel_name = std::make_unique<std::string>(name);
const auto* kernel_name_str = kernel_name->c_str();
kernel_name_storage_.Push(std::move(kernel_name));
return kernel_name_str;
}
private:
inline void RequestNewBuffer(pti::view::utilities::ViewBuffer& buffer) {
unsigned char* raw_buffer = nullptr;
std::size_t buffer_size = 0;
{
std::lock_guard<std::mutex> cb_lock(get_new_buffer_mtx_);
get_new_buffer_(&raw_buffer, &buffer_size);
}
buffer.Refresh(raw_buffer, buffer_size);
}
inline void DeliverBuffer(pti::view::utilities::ViewBuffer&& buffer) {
auto buffer_to_deliver = std::move(buffer);
{
std::lock_guard<std::mutex> cb_lock(deliver_buffer_mtx_);
if (buffer_to_deliver.GetBuffer()) {
deliver_buffer_(buffer_to_deliver.GetBuffer(), buffer_to_deliver.GetBufferSize(),
buffer_to_deliver.GetValidBytes());
}
}
}
inline void DisableTracing() {
#if defined(PTI_TRACE_SYCL)
SyclCollector::Instance().DisableTracing();
#endif
if (collector_) {
collector_->DisableTracer();
}
collection_enabled_ = false;
}
ZeCollector* collector_ = nullptr;
std::atomic<bool> flush_operation_ = false;
std::atomic<bool> stop_consumer_thread_ = false;
std::atomic<bool> collection_enabled_ = false;
std::atomic<bool> callbacks_set_ = false;
ViewBufferQueue buffer_queue_ = ViewBufferQueue{kDefaultBufferQueueDepth};
AskForBufferEvent get_new_buffer_;
ReturnBufferEvent deliver_buffer_;
mutable std::mutex get_new_buffer_mtx_;
mutable std::mutex deliver_buffer_mtx_;
ViewEventTable view_event_map_;
KernelNameStorageQueue kernel_name_storage_;
ViewBufferTable view_buffers_;
std::thread buffer_consumer_;
};
// Required to access buffer from ze_collector callbacks
inline static auto& Instance() {
static PtiViewRecordHandler data_container;
return data_container;
}
inline pti_result GetNextRecord(uint8_t* buffer, size_t valid_bytes,
pti_view_record_base** record) {
if (!record) {
return pti_result::PTI_ERROR_BAD_ARGUMENT;
}
pti::view::utilities::ViewBuffer view_buffer(buffer, valid_bytes, valid_bytes);
if (view_buffer.IsNull() || !view_buffer.GetValidBytes()) {
return pti_result::PTI_STATUS_END_OF_BUFFER;
}
auto* current_record = *record;
// User passed a nullptr for the record. Give them the first record.
if (!current_record) {
*record = view_buffer.Peek<pti_view_record_base>();
return pti_result::PTI_SUCCESS;
}
auto next_element_loc = GetViewSize(current_record->_view_kind);
// Found invalid record
if (next_element_loc == SIZE_MAX) {
return pti_result::PTI_ERROR_BAD_ARGUMENT;
}
auto* next_element_ptr = view_buffer.Peek(*record, next_element_loc);
if (!next_element_ptr) {
return pti_result::PTI_STATUS_END_OF_BUFFER;
}
*record = next_element_ptr;
return pti_result::PTI_SUCCESS;
}
//
// TODO -- can we save on the string compare cost by optimizing at the source in
// L0 collector?
//
inline void SetMemFillType(pti_view_record_memory_fill& mem_record,
const ZeKernelCommandExecutionRecord& rec) {
std::size_t found_pos = rec.name_.find_last_of("(");
std::string tmp_str = rec.name_.substr(found_pos);
if (tmp_str == "(M)") {
mem_record._mem_type = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_MEMORY;
return;
}
if (tmp_str == "(H)") {
mem_record._mem_type = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_HOST;
return;
}
if (tmp_str == "(D)") {
mem_record._mem_type = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_DEVICE;
return;
}
if (tmp_str == "(S)") {
mem_record._mem_type = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_SHARED;
return;
}
}
//
// TODO -- can we save on the string compare cost by optimizing at the source in
// L0 collector?
//
inline void SetMemCopyType(pti_view_record_memory_copy& mem_record,
const ZeKernelCommandExecutionRecord& rec) {
std::size_t found_pos = rec.name_.find_last_of("(");
std::string tmp_str = rec.name_.substr(found_pos, 4);
tmp_str.push_back(')');
if (tmp_str == "(M2M)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_M2M;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_MEMORY;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_MEMORY;
return;
}
if (tmp_str == "(M2H)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_M2H;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_MEMORY;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_HOST;
return;
}
if (tmp_str == "(M2D)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_M2D;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_MEMORY;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_DEVICE;
return;
}
if (tmp_str == "(M2S)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_M2S;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_MEMORY;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_SHARED;
return;
}
if (tmp_str == "(H2M)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_H2M;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_HOST;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_MEMORY;
return;
}
if (tmp_str == "(H2H)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_H2H;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_HOST;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_HOST;
return;
}
if (tmp_str == "(H2D)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_H2D;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_HOST;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_DEVICE;
return;
}
if (tmp_str == "(H2S)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_H2S;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_HOST;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_SHARED;
return;
}
if (tmp_str == "(D2M)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_D2M;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_DEVICE;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_MEMORY;
return;
}
if (tmp_str == "(D2H)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_D2H;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_DEVICE;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_HOST;
return;
}
if (tmp_str == "(D2D)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_D2D;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_DEVICE;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_DEVICE;
return;
}
if (tmp_str == "(D2S)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_D2S;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_DEVICE;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_SHARED;
return;
}
if (tmp_str == "(S2M)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_S2M;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_SHARED;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_MEMORY;
return;
}
if (tmp_str == "(S2H)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_S2H;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_SHARED;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_HOST;
return;
}
if (tmp_str == "(S2D)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_S2D;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_SHARED;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_DEVICE;
return;
}
if (tmp_str == "(S2S)") {
mem_record._memcpy_type = pti_view_memcpy_type::PTI_VIEW_MEMCPY_TYPE_S2S;
mem_record._mem_src = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_SHARED;
mem_record._mem_dst = pti_view_memory_type::PTI_VIEW_MEMORY_TYPE_SHARED;
return;
}
}
inline void GetDeviceId(char* buf, const ZeKernelCommandExecutionRecord& rec) {
// determined by pti_view_record_kernel _pci_address
constexpr auto kMaxDeviceIdLength = 16;
std::snprintf(buf, kMaxDeviceIdLength, "%x:%x:%x.%x", rec.pci_prop_.address.domain,
rec.pci_prop_.address.bus, rec.pci_prop_.address.device,
rec.pci_prop_.address.function);
}
inline void GenerateExternalCorrelationRecords(const ZeKernelCommandExecutionRecord& rec) {
for (auto it = map_ext_corrid_vectors.cbegin(); it != map_ext_corrid_vectors.cend(); it++) {
// for ( pti_view_record_external_correlation ext_record : it->second ) {
pti_view_record_external_correlation ext_record = it->second.top();
ext_record._correlation_id = rec.cid_;
ext_record._view_kind._view_kind = pti_view_kind::PTI_VIEW_EXTERNAL_CORRELATION;
Instance().InsertRecord(ext_record);
//}
}
}
inline uint64_t ApplyTimeShift(uint64_t timestamp, int64_t time_shift) {
uint64_t out_ts = 0;
if (time_shift < 0) {
PTI_ASSERT(timestamp >= static_cast<uint64_t>(-time_shift)); // underflow?
out_ts = timestamp - static_cast<uint64_t>(-time_shift);
} else {
PTI_ASSERT((UINT64_MAX - timestamp) >= static_cast<uint64_t>(time_shift)); // overflow?
out_ts = timestamp + static_cast<uint64_t>(time_shift);
}
return out_ts;
}
inline void MemCopyEvent(void* /*data*/, const ZeKernelCommandExecutionRecord& rec) {
pti_view_record_memory_copy record;
record._view_kind._view_kind = pti_view_kind::PTI_VIEW_DEVICE_GPU_MEM_COPY;
int64_t ts_shift = utils::ConvertionFactorMonotonicRawToReal();
record._append_timestamp = ApplyTimeShift(rec.append_time_, ts_shift);
record._start_timestamp = ApplyTimeShift(rec.start_time_, ts_shift);
record._end_timestamp = ApplyTimeShift(rec.end_time_, ts_shift);
record._submit_timestamp = ApplyTimeShift(rec.submit_time_, ts_shift);
record._queue_handle = rec.queue_;
record._device_handle = rec.device_;
record._context_handle = rec.context_;
record._bytes = rec.bytes_xfered_;
GetDeviceId(record._pci_address, rec);
SetMemCopyType(record, rec);
// We're storing it in a kernel map so this shouldn't go out of scope
record._name = Instance().InsertKernel(rec.name_);
record._thread_id = rec.tid_;
record._mem_op_id = rec.cid_;
record._correlation_id = rec.cid_;
Instance().InsertRecord(record);
}
inline void MemFillEvent(void* /*data*/, const ZeKernelCommandExecutionRecord& rec) {
pti_view_record_memory_fill record;
record._view_kind._view_kind = pti_view_kind::PTI_VIEW_DEVICE_GPU_MEM_FILL;
int64_t ts_shift = utils::ConvertionFactorMonotonicRawToReal();
record._append_timestamp = ApplyTimeShift(rec.append_time_, ts_shift);
record._start_timestamp = ApplyTimeShift(rec.start_time_, ts_shift);
record._end_timestamp = ApplyTimeShift(rec.end_time_, ts_shift);
record._submit_timestamp = ApplyTimeShift(rec.submit_time_, ts_shift);
record._queue_handle = rec.queue_;
record._device_handle = rec.device_;
record._context_handle = rec.context_;
record._bytes = rec.bytes_xfered_;
record._value_for_set = rec.value_set_;
GetDeviceId(record._pci_address, rec);
SetMemFillType(record, rec);
// We're storing it in a kernel map so this shouldn't go out of scope
record._name = Instance().InsertKernel(rec.name_);
record._thread_id = rec.tid_;
record._mem_op_id = rec.cid_;
record._correlation_id = rec.cid_;
Instance().InsertRecord(record);
}
inline void OverheadCollectionEvent(void* data, const ZeKernelCommandExecutionRecord& /*rec*/) {
int64_t ts_shift = utils::ConvertionFactorMonotonicRawToReal();
pti_view_record_overhead* ohRec = reinterpret_cast<pti_view_record_overhead*>(data);
ohRec->_overhead_start_timestamp_ns =
ApplyTimeShift(ohRec->_overhead_start_timestamp_ns, ts_shift);
ohRec->_overhead_end_timestamp_ns = ApplyTimeShift(ohRec->_overhead_end_timestamp_ns, ts_shift);
// ohRec->_overhead_api_name =
// Instance().InsertKernel(ohRec->_overhead_api_name);
Instance().InsertRecord(*ohRec);
}
inline void SyclRuntimeEvent(void* /*data*/, const ZeKernelCommandExecutionRecord& rec) {
pti_view_record_sycl_runtime record;
record._view_kind._view_kind = pti_view_kind::PTI_VIEW_SYCL_RUNTIME_CALLS;
int64_t ts_shift = utils::ConvertionFactorMonotonicRawToReal();
if (external_collection_enabled) {
GenerateExternalCorrelationRecords(rec);
}
record._start_timestamp = ApplyTimeShift(rec.start_time_, ts_shift);
record._end_timestamp = ApplyTimeShift(rec.end_time_, ts_shift);
record._thread_id = rec.tid_;
record._process_id = rec.pid_;
record._correlation_id = rec.cid_;
record._name = rec.sycl_func_name_;
Instance().InsertRecord(record);
}
inline void KernelEvent(void* /*data*/, const ZeKernelCommandExecutionRecord& rec) {
pti_view_record_kernel record;
record._view_kind._view_kind = pti_view_kind::PTI_VIEW_DEVICE_GPU_KERNEL;
int64_t ts_shift = utils::ConvertionFactorMonotonicRawToReal();
if (external_collection_enabled) {
GenerateExternalCorrelationRecords(rec);
}
record._append_timestamp = ApplyTimeShift(rec.append_time_, ts_shift);
record._start_timestamp = ApplyTimeShift(rec.start_time_, ts_shift);
record._end_timestamp = ApplyTimeShift(rec.end_time_, ts_shift);
record._submit_timestamp = ApplyTimeShift(rec.submit_time_, ts_shift);
record._queue_handle = rec.queue_;
record._device_handle = rec.device_;
record._context_handle = rec.context_;
GetDeviceId(record._pci_address, rec);
// We're storing it in a kernel map so this shouldn't go out of scope
record._name = Instance().InsertKernel(rec.name_);
record._thread_id = rec.tid_;
record._kernel_id = rec.kid_;
record._correlation_id = rec.cid_;
record._source_file_name = Instance().InsertKernel(rec.source_file_name_);
record._source_line_number =
rec.source_line_number_ != UINT32_MAX ? rec.source_line_number_ : 0ULL;
record._sycl_node_id = rec.sycl_node_id_;
record._sycl_invocation_id = rec.sycl_invocation_id_;
record._sycl_enqk_begin_timestamp = ApplyTimeShift(rec.sycl_enqk_begin_time_, ts_shift);
record._sycl_task_begin_timestamp = ApplyTimeShift(rec.sycl_task_begin_time_, ts_shift);
Instance().InsertRecord(record);
}
inline void SyclRuntimeViewCallback(void* data, ZeKernelCommandExecutionRecord& rec) {
Instance()("SyclRuntimeEvent", data, rec);
}
inline void OverheadCollectionCallback(void* data, ZeKernelCommandExecutionRecord& rec) {
Instance()("OverheadCollectionEvent", data, rec);
}
inline void ZeChromeKernelStagesCallback(void* data,
std::vector<ZeKernelCommandExecutionRecord>& kcexecrec) {
for (const auto& rec : kcexecrec) {
if (rec.name_.find("zeCommandListAppendMemoryCopy") != std::string::npos) {
Instance()("zeCommandListAppendMemoryCopy", data, rec);
} else if (rec.name_.find("zeCommandListAppendMemoryFill") != std::string::npos) {
Instance()("zeCommandListAppendMemoryFill", data, rec);
} else if (rec.name_.find("zeCommandListAppendBarrier") != std::string::npos) {
// no-op for now
} else {
Instance()("KernelEvent", data, rec);
}
}
}
#endif // SRC_API_VIEW_HANDLER_H_