forked from intel/pti-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ze_kernel_collector.h
2113 lines (1811 loc) · 70.2 KB
/
ze_kernel_collector.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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#ifndef PTI_TOOLS_ZE_TRACER_ZE_KERNEL_COLLECTOR_H_
#define PTI_TOOLS_ZE_TRACER_ZE_KERNEL_COLLECTOR_H_
#include <atomic>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <mutex>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <level_zero/layers/zel_tracing_api.h>
#include "correlator.h"
#include "utils.h"
#include "ze_event_cache.h"
#include "ze_utils.h"
struct ZeSyncPoint {
uint64_t host_sync;
uint64_t device_sync;
};
struct ZeKernelGroupSize {
uint32_t x;
uint32_t y;
uint32_t z;
};
struct ZeKernelProps {
std::string name;
size_t simd_width;
size_t bytes_transferred;
uint32_t group_count[3];
uint32_t group_size[3];
};
struct ZeKernelCommand {
ZeKernelProps props;
ze_event_handle_t event = nullptr;
ze_device_handle_t device = nullptr;
uint64_t kernel_id = 0;
uint64_t append_time = 0;
uint64_t call_count = 0;
uint64_t timer_frequency = 0;
uint64_t timer_mask = 0;
};
struct ZeKernelCall {
ZeKernelCommand* command = nullptr;
ze_command_queue_handle_t queue = nullptr;
ze_fence_handle_t fence;
uint64_t submit_time = 0;
uint64_t device_submit_time = 0;
uint64_t call_id = 0;
bool need_to_process = true;
};
struct ZeKernelInfo {
uint64_t append_time;
uint64_t submit_time;
uint64_t execute_time;
uint64_t min_time;
uint64_t max_time;
uint64_t call_count;
bool operator>(const ZeKernelInfo& r) const {
if (execute_time != r.execute_time) {
return execute_time > r.execute_time;
}
return call_count > r.call_count;
}
bool operator!=(const ZeKernelInfo& r) const {
if (execute_time == r.execute_time) {
return call_count != r.call_count;
}
return true;
}
};
struct ZeCommandListInfo {
std::vector<ZeKernelCommand*> kernel_command_list;
ze_context_handle_t context;
ze_device_handle_t device;
bool immediate;
};
#ifdef PTI_KERNEL_INTERVALS
struct ZeDeviceInterval {
uint64_t start;
uint64_t end;
uint32_t sub_device_id;
};
struct ZeKernelInterval {
std::string kernel_name;
ze_device_handle_t device;
std::vector<ZeDeviceInterval> device_interval_list;
};
using ZeKernelIntervalList = std::vector<ZeKernelInterval>;
#endif // PTI_KERNEL_INTERVALS
using ZeKernelGroupSizeMap = std::map<ze_kernel_handle_t, ZeKernelGroupSize>;
using ZeKernelInfoMap = std::map<std::string, ZeKernelInfo>;
using ZeCommandListMap = std::map<ze_command_list_handle_t, ZeCommandListInfo>;
using ZeImageSizeMap = std::map<ze_image_handle_t, size_t>;
using ZeDeviceMap = std::map<
ze_device_handle_t, std::vector<ze_device_handle_t> >;
typedef void (*OnZeKernelFinishCallback)(
void* data,
const std::string& queue,
const std::string& id,
const std::string& name,
uint64_t appended,
uint64_t submitted,
uint64_t started,
uint64_t ended);
class ZeKernelCollector {
public: // Interface
static ZeKernelCollector* Create(
Correlator* correlator,
KernelCollectorOptions options,
OnZeKernelFinishCallback callback = nullptr,
void* callback_data = nullptr) {
ze_api_version_t version = utils::ze::GetVersion();
PTI_ASSERT(
ZE_MAJOR_VERSION(version) >= 1 &&
ZE_MINOR_VERSION(version) >= 2);
PTI_ASSERT(correlator != nullptr);
ZeKernelCollector* collector = new ZeKernelCollector(
correlator, options, callback, callback_data);
PTI_ASSERT(collector != nullptr);
ze_result_t status = ZE_RESULT_SUCCESS;
zel_tracer_desc_t tracer_desc = {
ZEL_STRUCTURE_TYPE_TRACER_EXP_DESC, nullptr, collector};
zel_tracer_handle_t tracer = nullptr;
status = zelTracerCreate(&tracer_desc, &tracer);
if (status != ZE_RESULT_SUCCESS) {
std::cerr << "[WARNING] Unable to create Level Zero tracer" << std::endl;
delete collector;
return nullptr;
}
collector->EnableTracing(tracer);
return collector;
}
~ZeKernelCollector() {
if (tracer_ != nullptr) {
#if !defined(_WIN32)
ze_result_t status = zelTracerDestroy(tracer_);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
#endif
}
}
void PrintKernelsTable() const {
std::set< std::pair<std::string, ZeKernelInfo>,
utils::Comparator > sorted_list(
kernel_info_map_.begin(), kernel_info_map_.end());
uint64_t total_duration = 0;
size_t max_name_length = kKernelLength;
for (auto& value : sorted_list) {
total_duration += value.second.execute_time;
if (value.first.size() > max_name_length) {
max_name_length = value.first.size();
}
}
if (total_duration == 0) {
return;
}
std::stringstream stream;
stream << std::setw(max_name_length) << "Kernel" << "," <<
std::setw(kCallsLength) << "Calls" << "," <<
std::setw(kTimeLength) << "Time (ns)" << "," <<
std::setw(kPercentLength) << "Time (%)" << "," <<
std::setw(kTimeLength) << "Average (ns)" << "," <<
std::setw(kTimeLength) << "Min (ns)" << "," <<
std::setw(kTimeLength) << "Max (ns)" << std::endl;
for (auto& value : sorted_list) {
const std::string& function = value.first;
uint64_t call_count = value.second.call_count;
uint64_t duration = value.second.execute_time;
uint64_t avg_duration = duration / call_count;
uint64_t min_duration = value.second.min_time;
uint64_t max_duration = value.second.max_time;
float percent_duration = 100.0f * duration / total_duration;
stream << std::setw(max_name_length) << function << "," <<
std::setw(kCallsLength) << call_count << "," <<
std::setw(kTimeLength) << duration << "," <<
std::setw(kPercentLength) << std::setprecision(2) <<
std::fixed << percent_duration << "," <<
std::setw(kTimeLength) << avg_duration << "," <<
std::setw(kTimeLength) << min_duration << "," <<
std::setw(kTimeLength) << max_duration << std::endl;
}
PTI_ASSERT(correlator_ != nullptr);
correlator_->Log(stream.str());
}
void PrintSubmissionTable() const {
std::set< std::pair<std::string, ZeKernelInfo>,
utils::Comparator > sorted_list(
kernel_info_map_.begin(), kernel_info_map_.end());
uint64_t total_append_duration = 0;
uint64_t total_submit_duration = 0;
uint64_t total_execute_duration = 0;
size_t max_name_length = kKernelLength;
for (auto& value : sorted_list) {
total_append_duration += value.second.append_time;
total_submit_duration += value.second.submit_time;
total_execute_duration += value.second.execute_time;
if (value.first.size() > max_name_length) {
max_name_length = value.first.size();
}
}
if (total_execute_duration == 0) {
return;
}
std::stringstream stream;
stream << std::setw(max_name_length) << "Kernel" << "," <<
std::setw(kCallsLength) << "Calls" << "," <<
std::setw(kTimeLength) << "Append (ns)" << "," <<
std::setw(kPercentLength) << "Append (%)" << "," <<
std::setw(kTimeLength) << "Submit (ns)" << "," <<
std::setw(kPercentLength) << "Submit (%)" << "," <<
std::setw(kTimeLength) << "Execute (ns)" << "," <<
std::setw(kPercentLength) << "Execute (%)" << "," << std::endl;
for (auto& value : sorted_list) {
const std::string& function = value.first;
uint64_t call_count = value.second.call_count;
uint64_t append_duration = value.second.append_time;
float append_percent =
100.0f * append_duration / total_append_duration;
uint64_t submit_duration = value.second.submit_time;
float submit_percent =
100.0f * submit_duration / total_submit_duration;
uint64_t execute_duration = value.second.execute_time;
float execute_percent =
100.0f * execute_duration / total_execute_duration;
stream << std::setw(max_name_length) << function << "," <<
std::setw(kCallsLength) << call_count << "," <<
std::setw(kTimeLength) << append_duration << "," <<
std::setw(kPercentLength) << std::setprecision(2) <<
std::fixed << append_percent << "," <<
std::setw(kTimeLength) << submit_duration << "," <<
std::setw(kPercentLength) << std::setprecision(2) <<
std::fixed << submit_percent << "," <<
std::setw(kTimeLength) << execute_duration << "," <<
std::setw(kPercentLength) << std::setprecision(2) <<
std::fixed << execute_percent << "," << std::endl;
}
PTI_ASSERT(correlator_ != nullptr);
correlator_->Log(stream.str());
}
void DisableTracing() {
PTI_ASSERT(tracer_ != nullptr);
#if !defined(_WIN32)
ze_result_t status = zelTracerSetEnabled(tracer_, false);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
#endif
}
const ZeKernelInfoMap& GetKernelInfoMap() const {
return kernel_info_map_;
}
#ifdef PTI_KERNEL_INTERVALS
const ZeKernelIntervalList& GetKernelIntervalList() const {
return kernel_interval_list_;
}
#endif // PTI_KERNEL_INTERVALS
private: // Implementation
ZeKernelCollector(
Correlator* correlator,
KernelCollectorOptions options,
OnZeKernelFinishCallback callback,
void* callback_data)
: correlator_(correlator),
options_(options),
callback_(callback),
callback_data_(callback_data),
kernel_id_(1),
event_cache_(ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP) {
PTI_ASSERT(correlator_ != nullptr);
CreateDeviceMap();
#ifdef PTI_KERNEL_INTERVALS
SetSyncPoints();
#endif
}
#ifdef PTI_KERNEL_INTERVALS
void SetSyncPoints() {
std::vector<ze_device_handle_t> device_list =
utils::ze::GetDeviceList();
for (auto device : device_list) {
std::vector<ze_device_handle_t> sub_device_list =
utils::ze::GetSubDeviceList(device);
if (sub_device_list.empty()) {
ZeSyncPoint sync_point{0, 0};
GetSyncTimestamps(
device, sync_point.host_sync, sync_point.device_sync);
PTI_ASSERT(sync_point_map_.count(device) == 0);
sync_point_map_[device] = sync_point;
} else {
for (auto sub_device : sub_device_list) {
ZeSyncPoint sync_point{0, 0};
GetSyncTimestamps(
sub_device, sync_point.host_sync, sync_point.device_sync);
PTI_ASSERT(sync_point_map_.count(sub_device) == 0);
sync_point_map_[sub_device] = sync_point;
}
}
}
}
#endif
void CreateDeviceMap() {
std::vector<ze_device_handle_t> device_list =
utils::ze::GetDeviceList();
for (auto device : device_list) {
std::vector<ze_device_handle_t> sub_device_list =
utils::ze::GetSubDeviceList(device);
PTI_ASSERT(device_map_.count(device) == 0);
device_map_[device] = sub_device_list;
}
}
int GetSubDeviceId(ze_device_handle_t sub_device) const {
for (auto it : device_map_) {
std::vector<ze_device_handle_t> sub_device_list = it.second;
for (size_t i = 0; i < sub_device_list.size(); ++i) {
if (sub_device_list[i] == sub_device) {
return static_cast<int>(i);
}
}
}
return -1;
}
ze_device_handle_t GetDeviceForSubDevice(
ze_device_handle_t sub_device) const {
for (auto it : device_map_) {
std::vector<ze_device_handle_t> sub_device_list = it.second;
for (size_t i = 0; i < sub_device_list.size(); ++i) {
if (sub_device_list[i] == sub_device) {
return it.first;
}
}
}
return nullptr;
}
uint64_t GetHostTimestamp() const {
PTI_ASSERT(correlator_ != nullptr);
return correlator_->GetTimestamp();
}
void GetSyncTimestamps(
ze_device_handle_t device,
uint64_t& host_timestamp,
uint64_t& device_timestamp) const {
PTI_ASSERT(device != nullptr);
PTI_ASSERT(correlator_ != nullptr);
#ifdef PTI_KERNEL_INTERVALS
utils::ze::GetMetricTimestamps(device, &host_timestamp, &device_timestamp);
host_timestamp = correlator_->GetTimestamp(host_timestamp);
device_timestamp &= utils::ze::GetMetricTimestampMask(device);
#else
utils::ze::GetDeviceTimestamps(device, &host_timestamp, &device_timestamp);
host_timestamp = correlator_->GetTimestamp(host_timestamp);
device_timestamp &= utils::ze::GetDeviceTimestampMask(device);
#endif
}
void EnableTracing(zel_tracer_handle_t tracer) {
PTI_ASSERT(tracer != nullptr);
tracer_ = tracer;
zet_core_callbacks_t prologue_callbacks{};
zet_core_callbacks_t epilogue_callbacks{};
prologue_callbacks.Event.pfnDestroyCb = OnEnterEventDestroy;
prologue_callbacks.Event.pfnHostResetCb = OnEnterEventHostReset;
prologue_callbacks.EventPool.pfnCreateCb = OnEnterEventPoolCreate;
epilogue_callbacks.EventPool.pfnCreateCb = OnExitEventPoolCreate;
prologue_callbacks.CommandList.pfnAppendLaunchKernelCb =
OnEnterCommandListAppendLaunchKernel;
epilogue_callbacks.CommandList.pfnAppendLaunchKernelCb =
OnExitCommandListAppendLaunchKernel;
prologue_callbacks.CommandList.pfnAppendLaunchCooperativeKernelCb =
OnEnterCommandListAppendLaunchCooperativeKernel;
epilogue_callbacks.CommandList.pfnAppendLaunchCooperativeKernelCb =
OnExitCommandListAppendLaunchCooperativeKernel;
prologue_callbacks.CommandList.pfnAppendLaunchKernelIndirectCb =
OnEnterCommandListAppendLaunchKernelIndirect;
epilogue_callbacks.CommandList.pfnAppendLaunchKernelIndirectCb =
OnExitCommandListAppendLaunchKernelIndirect;
prologue_callbacks.CommandList.pfnAppendMemoryCopyCb =
OnEnterCommandListAppendMemoryCopy;
epilogue_callbacks.CommandList.pfnAppendMemoryCopyCb =
OnExitCommandListAppendMemoryCopy;
prologue_callbacks.CommandList.pfnAppendMemoryFillCb =
OnEnterCommandListAppendMemoryFill;
epilogue_callbacks.CommandList.pfnAppendMemoryFillCb =
OnExitCommandListAppendMemoryFill;
prologue_callbacks.CommandList.pfnAppendBarrierCb =
OnEnterCommandListAppendBarrier;
epilogue_callbacks.CommandList.pfnAppendBarrierCb =
OnExitCommandListAppendBarrier;
prologue_callbacks.CommandList.pfnAppendMemoryRangesBarrierCb =
OnEnterCommandListAppendMemoryRangesBarrier;
epilogue_callbacks.CommandList.pfnAppendMemoryRangesBarrierCb =
OnExitCommandListAppendMemoryRangesBarrier;
prologue_callbacks.CommandList.pfnAppendMemoryCopyRegionCb =
OnEnterCommandListAppendMemoryCopyRegion;
epilogue_callbacks.CommandList.pfnAppendMemoryCopyRegionCb =
OnExitCommandListAppendMemoryCopyRegion;
prologue_callbacks.CommandList.pfnAppendMemoryCopyFromContextCb =
OnEnterCommandListAppendMemoryCopyFromContext;
epilogue_callbacks.CommandList.pfnAppendMemoryCopyFromContextCb =
OnExitCommandListAppendMemoryCopyFromContext;
prologue_callbacks.CommandList.pfnAppendImageCopyCb =
OnEnterCommandListAppendImageCopy;
epilogue_callbacks.CommandList.pfnAppendImageCopyCb =
OnExitCommandListAppendImageCopy;
prologue_callbacks.CommandList.pfnAppendImageCopyRegionCb =
OnEnterCommandListAppendImageCopyRegion;
epilogue_callbacks.CommandList.pfnAppendImageCopyRegionCb =
OnExitCommandListAppendImageCopyRegion;
prologue_callbacks.CommandList.pfnAppendImageCopyToMemoryCb =
OnEnterCommandListAppendImageCopyToMemory;
epilogue_callbacks.CommandList.pfnAppendImageCopyToMemoryCb =
OnExitCommandListAppendImageCopyToMemory;
prologue_callbacks.CommandList.pfnAppendImageCopyFromMemoryCb =
OnEnterCommandListAppendImageCopyFromMemory;
epilogue_callbacks.CommandList.pfnAppendImageCopyFromMemoryCb =
OnExitCommandListAppendImageCopyFromMemory;
prologue_callbacks.CommandQueue.pfnExecuteCommandListsCb =
OnEnterCommandQueueExecuteCommandLists;
epilogue_callbacks.CommandQueue.pfnExecuteCommandListsCb =
OnExitCommandQueueExecuteCommandLists;
epilogue_callbacks.CommandList.pfnCreateCb =
OnExitCommandListCreate;
epilogue_callbacks.CommandList.pfnCreateImmediateCb =
OnExitCommandListCreateImmediate;
epilogue_callbacks.CommandList.pfnDestroyCb =
OnExitCommandListDestroy;
epilogue_callbacks.CommandList.pfnResetCb =
OnExitCommandListReset;
epilogue_callbacks.CommandQueue.pfnSynchronizeCb =
OnExitCommandQueueSynchronize;
epilogue_callbacks.CommandQueue.pfnDestroyCb =
OnExitCommandQueueDestroy;
epilogue_callbacks.Image.pfnCreateCb =
OnExitImageCreate;
epilogue_callbacks.Image.pfnDestroyCb =
OnExitImageDestroy;
epilogue_callbacks.Kernel.pfnSetGroupSizeCb =
OnExitKernelSetGroupSize;
epilogue_callbacks.Kernel.pfnDestroyCb =
OnExitKernelDestroy;
epilogue_callbacks.Event.pfnHostSynchronizeCb =
OnExitEventHostSynchronize;
epilogue_callbacks.Fence.pfnHostSynchronizeCb =
OnExitFenceHostSynchronize;
epilogue_callbacks.Context.pfnDestroyCb =
OnExitContextDestroy;
ze_result_t status = ZE_RESULT_SUCCESS;
status = zelTracerSetPrologues(tracer_, &prologue_callbacks);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
status = zelTracerSetEpilogues(tracer_, &epilogue_callbacks);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
status = zelTracerSetEnabled(tracer_, true);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
}
void AddKernelCommand(
ze_command_list_handle_t command_list, ZeKernelCommand* command) {
PTI_ASSERT(command_list != nullptr);
PTI_ASSERT(command != nullptr);
const std::lock_guard<std::mutex> lock(lock_);
command->kernel_id =
kernel_id_.fetch_add(1, std::memory_order::memory_order_relaxed);
PTI_ASSERT(correlator_ != nullptr);
correlator_->SetKernelId(command->kernel_id);
correlator_->AddKernelId(command_list, command->kernel_id);
PTI_ASSERT(command_list_map_.count(command_list) == 1);
ZeCommandListInfo& command_list_info = command_list_map_[command_list];
command_list_info.kernel_command_list.push_back(command);
}
void AddKernelCall(
ze_command_list_handle_t command_list, ZeKernelCall* call) {
PTI_ASSERT(command_list != nullptr);
PTI_ASSERT(call != nullptr);
const std::lock_guard<std::mutex> lock(lock_);
ZeKernelCommand* command = call->command;
PTI_ASSERT(command != nullptr);
++(command->call_count);
call->call_id = command->call_count;
kernel_call_list_.push_back(call);
PTI_ASSERT(correlator_ != nullptr);
correlator_->AddCallId(command_list, call->call_id);
}
void ProcessCall(std::string callname, ze_event_handle_t event) {
PTI_ASSERT(event != nullptr);
const std::lock_guard<std::mutex> lock(lock_);
ze_result_t status = ZE_RESULT_SUCCESS;
status = zeEventQueryStatus(event);
if (status != ZE_RESULT_SUCCESS) {
return;
}
for (auto it = kernel_call_list_.begin();
it != kernel_call_list_.end(); ++it) {
ZeKernelCall* call = *it;
PTI_ASSERT(call != nullptr);
ZeKernelCommand* command = call->command;
PTI_ASSERT(command != nullptr);
if (command->event == event) {
ProcessCall(callname, call);
kernel_call_list_.erase(it);
break;
}
}
}
void ProcessCall(std::string callname, ze_fence_handle_t fence) {
PTI_ASSERT(fence != nullptr);
const std::lock_guard<std::mutex> lock(lock_);
ze_result_t status = ZE_RESULT_SUCCESS;
status = zeFenceQueryStatus(fence);
if (status != ZE_RESULT_SUCCESS) {
return;
}
for (auto it = kernel_call_list_.begin(); it != kernel_call_list_.end();) {
ZeKernelCall* call = *it;
PTI_ASSERT(call != nullptr);
if ((call->fence != nullptr) && (call->fence == fence)) {
ZeKernelCommand* command = call->command;
PTI_ASSERT(command != nullptr);
if (event_cache_.QueryEvent(command->event)) {
PTI_ASSERT(zeEventQueryStatus(command->event) == ZE_RESULT_SUCCESS);
#if 0
if (zeEventQueryStatus(command->event) != ZE_RESULT_SUCCESS) {
// this should not happen
zeEventHostSignal(command->event);
}
#endif /* 0 */
}
ProcessCall(callname, call);
it = kernel_call_list_.erase(it);
}
else {
it++;
}
}
}
uint64_t ComputeDuration(
uint64_t start, uint64_t end, uint64_t freq, uint64_t mask) {
uint64_t duration = 0;
if (start < end) {
duration = (end - start) *
static_cast<uint64_t>(NSEC_IN_SEC) / freq;
} else { // Timer Overflow
duration = ((mask + 1ull) + end - start) *
static_cast<uint64_t>(NSEC_IN_SEC) / freq;
}
return duration;
}
#ifdef PTI_KERNEL_INTERVALS
uint64_t ConvertToMetricTimestamp(
uint64_t kernel_timestamp,
uint64_t mask) {
return (kernel_timestamp & mask);
}
uint64_t ProcessTimerOverflow(
const ZeSyncPoint& base_sync,
const ZeSyncPoint& target_sync,
uint64_t mask,
uint64_t freq) {
PTI_ASSERT(base_sync.host_sync < target_sync.host_sync);
uint64_t duration = target_sync.host_sync - base_sync.host_sync;
uint64_t base_time = base_sync.device_sync *
static_cast<uint64_t>(NSEC_IN_SEC) / freq;
uint64_t target_time = base_time + duration;
uint64_t max_time = (mask + 1ull) *
static_cast<uint64_t>(NSEC_IN_SEC) / freq;
uint64_t shift = 0;
uint64_t metric_time = target_sync.device_sync *
static_cast<uint64_t>(NSEC_IN_SEC) / freq;
while (metric_time + shift + max_time < target_time) {
shift += max_time;
}
return shift;
}
void GetMetricTime(
const ZeKernelCall* call,
ze_device_handle_t device,
const ze_kernel_timestamp_result_t& timestamp,
uint64_t& metric_start, uint64_t& metric_end) {
PTI_ASSERT(call != nullptr);
ZeKernelCommand* command = call->command;
PTI_ASSERT(command != nullptr);
uint64_t freq = command->timer_frequency;
uint64_t mask = command->timer_mask;
PTI_ASSERT(freq > 0);
PTI_ASSERT(mask > 0);
uint64_t start = ConvertToMetricTimestamp(
timestamp.global.kernelStart, mask);
uint64_t end = ConvertToMetricTimestamp(
timestamp.global.kernelEnd, mask);
PTI_ASSERT(!sync_point_map_.empty());
PTI_ASSERT(sync_point_map_.count(device) == 1);
const ZeSyncPoint& base_sync = sync_point_map_[device];
PTI_ASSERT(call->submit_time > 0);
ZeSyncPoint submit_sync{call->submit_time, call->device_submit_time};
uint64_t time_shift = ComputeDuration(
submit_sync.device_sync, start, freq, mask);
uint64_t duration = ComputeDuration(start, end, freq, mask);
uint64_t metric_sync = submit_sync.device_sync *
static_cast<uint64_t>(NSEC_IN_SEC) / freq;
metric_sync += ProcessTimerOverflow(base_sync, submit_sync, mask, freq);
metric_start = metric_sync + time_shift;
metric_end = metric_start + duration;
}
#else // PTI_KERNEL_INTERVALS
void GetHostTime(
const ZeKernelCall* call,
const ze_kernel_timestamp_result_t& timestamp,
uint64_t& host_start, uint64_t& host_end) {
PTI_ASSERT(call != nullptr);
ZeKernelCommand* command = call->command;
PTI_ASSERT(command != nullptr);
uint64_t start = timestamp.global.kernelStart;
uint64_t end = timestamp.global.kernelEnd;
uint64_t freq = command->timer_frequency;
uint64_t mask = command->timer_mask;
PTI_ASSERT(freq > 0);
PTI_ASSERT(mask > 0);
PTI_ASSERT(call->submit_time > 0);
uint64_t time_shift =
ComputeDuration(call->device_submit_time, start, freq, mask);
uint64_t duration = ComputeDuration(start, end, freq, mask);
host_start = call->submit_time + time_shift;
host_end = host_start + duration;
}
void ProcessCall(
const ZeKernelCall* call,
const ze_kernel_timestamp_result_t& timestamp,
int tile, bool in_summary) {
PTI_ASSERT(call != nullptr);
ZeKernelCommand* command = call->command;
PTI_ASSERT(command != nullptr);
uint64_t host_start = 0, host_end = 0;
GetHostTime(call, timestamp, host_start, host_end);
PTI_ASSERT(host_start <= host_end);
std::string name = command->props.name;
PTI_ASSERT(!name.empty());
if (options_.verbose) {
name = GetVerboseName(&command->props);
}
if (tile >= 0) {
name += "(" + std::to_string(tile) + "T)";
}
if (in_summary) {
PTI_ASSERT(command->append_time > 0);
PTI_ASSERT(command->append_time <= call->submit_time);
uint64_t append_time = call->submit_time - command->append_time;
PTI_ASSERT(call->submit_time <= host_start);
uint64_t submit_time = host_start - call->submit_time;
PTI_ASSERT(host_start <= host_end);
uint64_t execute_time = host_end - host_start;
AddKernelInfo(append_time, submit_time, execute_time, name);
}
if (callback_ != nullptr) {
PTI_ASSERT(command->append_time > 0);
PTI_ASSERT(command->append_time <= call->submit_time);
PTI_ASSERT(call->queue != nullptr);
PTI_ASSERT(!command->props.name.empty());
std::string id = std::to_string(command->kernel_id) + "." +
std::to_string(call->call_id);
std::stringstream stream;
stream << std::hex << call->queue;
if (tile >= 0) {
stream << "." << std::dec << tile;
}
callback_(
callback_data_, stream.str(), id, name,
command->append_time, call->submit_time,
host_start, host_end);
}
}
#endif // PTI_KERNEL_INTERVALS
void ProcessCall(std::string callname, const ZeKernelCall* call) {
PTI_ASSERT(call != nullptr);
ZeKernelCommand* command = call->command;
PTI_ASSERT(command != nullptr);
if (call->need_to_process) {
#ifdef PTI_KERNEL_INTERVALS
AddKernelInterval(call);
#else // PTI_KERNEL_INTERVALS
ze_result_t status = ZE_RESULT_SUCCESS;
status = zeEventQueryStatus(command->event);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
ze_kernel_timestamp_result_t timestamp{};
status = zeEventQueryKernelTimestamp(command->event, ×tamp);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
if (options_.kernels_per_tile && command->props.simd_width > 0) {
if (device_map_.count(command->device) == 1 &&
!device_map_[command->device].empty()) { // Implicit Scaling
uint32_t count = 0;
status = zeEventQueryTimestampsExp(
command->event, command->device, &count, nullptr);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
PTI_ASSERT(count > 0);
std::vector<ze_kernel_timestamp_result_t> timestamps(count);
status = zeEventQueryTimestampsExp(
command->event, command->device, &count, timestamps.data());
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
if (count == 1) { // First tile is used only
ProcessCall(call, timestamp, 0, true);
} else {
ProcessCall(call, timestamp, -1, false);
for (uint32_t i = 0; i < count; ++i) {
ProcessCall(call, timestamps[i], static_cast<int>(i), true);
}
}
} else { // Explicit Scaling
if (device_map_.count(command->device) == 0) { // Subdevice
int sub_device_id = GetSubDeviceId(command->device);
PTI_ASSERT(sub_device_id >= 0);
ProcessCall(call, timestamp, sub_device_id, true);
} else { // Device with no subdevices
ProcessCall(call, timestamp, 0, true);
}
}
} else {
ProcessCall(call, timestamp, -1, true);
}
#endif // PTI_KERNEL_INTERVALS
}
//DO NOT RESET EVENT
//event_cache_.ResetEvent(command->event);
delete call;
}
void ProcessCalls(std::string callname) {
ze_result_t status = ZE_RESULT_SUCCESS;
const std::lock_guard<std::mutex> lock(lock_);
auto it = kernel_call_list_.begin();
while (it != kernel_call_list_.end()) {
ZeKernelCall* call = *it;
PTI_ASSERT(call != nullptr);
ZeKernelCommand* command = call->command;
PTI_ASSERT(command != nullptr);
PTI_ASSERT(command->event != nullptr);
status = zeEventQueryStatus(command->event);
if (status == ZE_RESULT_NOT_READY) {
++it;
} else if (status == ZE_RESULT_SUCCESS) {
ProcessCall(callname, call);
it = kernel_call_list_.erase(it);
} else {
PTI_ASSERT(0);
}
}
}
static std::string GetVerboseName(const ZeKernelProps* props) {
PTI_ASSERT(props != nullptr);
PTI_ASSERT(!props->name.empty());
std::stringstream sstream;
sstream << props->name;
if (props->simd_width > 0) {
sstream << "[SIMD";
if (props->simd_width == 1) {
sstream << "_ANY";
} else {
sstream << props->simd_width;
}
sstream << " {" <<
props->group_count[0] << "; " <<
props->group_count[1] << "; " <<
props->group_count[2] << "} {" <<
props->group_size[0] << "; " <<
props->group_size[1] << "; " <<
props->group_size[2] << "}]";
} else if (props->bytes_transferred > 0) {
sstream << "[" << props->bytes_transferred << " bytes]";
}
return sstream.str();
}
void AddKernelInfo(
uint64_t append_time, uint64_t submit_time,
uint64_t execute_time, const std::string& name) {
PTI_ASSERT(!name.empty());
if (kernel_info_map_.count(name) == 0) {
ZeKernelInfo info;
info.append_time = append_time;
info.submit_time = submit_time;
info.execute_time = execute_time;
info.min_time = execute_time;
info.max_time = execute_time;
info.call_count = 1;
kernel_info_map_[name] = info;
} else {
ZeKernelInfo& kernel = kernel_info_map_[name];
kernel.append_time += append_time;
kernel.submit_time += submit_time;
kernel.execute_time += execute_time;
if (execute_time > kernel.max_time) {
kernel.max_time = execute_time;
}
if (execute_time < kernel.min_time) {
kernel.min_time = execute_time;
}
kernel.call_count += 1;
}
}
#ifdef PTI_KERNEL_INTERVALS
void AddKernelInterval(const ZeKernelCall* call) {
PTI_ASSERT(call != nullptr);
const ZeKernelCommand* command = call->command;
PTI_ASSERT(command != nullptr);
if (command->props.simd_width == 0) {
return; // Process user kernels only
}
std::string name = command->props.name;
PTI_ASSERT(!name.empty());
if (options_.verbose) {
name = GetVerboseName(&command->props);
}
ze_result_t status = zeEventQueryStatus(command->event);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
if (device_map_.count(command->device) == 1 &&
!device_map_[command->device].empty()) { // Implicit Scaling
uint32_t count = 0;
status = zeEventQueryTimestampsExp(
command->event, command->device, &count, nullptr);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
PTI_ASSERT(count > 0);
std::vector<ze_kernel_timestamp_result_t> timestamps(count);
status = zeEventQueryTimestampsExp(
command->event, command->device, &count, timestamps.data());
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
PTI_ASSERT(count <= device_map_[command->device].size());
ZeKernelInterval kernel_interval{
name, command->device, std::vector<ZeDeviceInterval>()};
for (uint32_t i = 0; i < count; ++i) {
ze_device_handle_t sub_device = device_map_[command->device][i];
uint64_t host_start = 0, host_end = 0;
GetMetricTime(call, sub_device, timestamps[i], host_start, host_end);
PTI_ASSERT(host_start <= host_end);
kernel_interval.device_interval_list.push_back(
{host_start, host_end, i});
}
kernel_interval_list_.push_back(kernel_interval);
} else { // Explicit scaling
ze_kernel_timestamp_result_t timestamp{};
status = zeEventQueryKernelTimestamp(command->event, ×tamp);
PTI_ASSERT(status == ZE_RESULT_SUCCESS);
uint64_t host_start = 0, host_end = 0;
GetMetricTime(call, command->device, timestamp, host_start, host_end);
PTI_ASSERT(host_start <= host_end);
if (device_map_.count(command->device) == 0) { // Subdevice
ze_device_handle_t device = GetDeviceForSubDevice(command->device);
PTI_ASSERT(device != nullptr);
int sub_device_id = GetSubDeviceId(command->device);
PTI_ASSERT(sub_device_id >= 0);
ZeKernelInterval kernel_interval{