-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathgpuvis_etl.cpp
1003 lines (832 loc) · 28.1 KB
/
gpuvis_etl.cpp
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 2019 Valve Software
*
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// Parsing ETL files is only supported on windows
// We heavily rely on the TDH windows library for the heavy lifting
#ifdef _WIN32
#define INITGUID
#include <windows.h>
#include <stdio.h>
#include <wbemidl.h>
#include <wmistr.h>
#include <evntrace.h>
#include <tdh.h>
#include <string>
#include <array>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
#include <SDL.h>
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h" // BeginColumns(), EndColumns(), PushColumnClipRect()
#include "imgui/imgui_impl_sdl_gl3.h"
#define GPUVIS_TRACE_IMPLEMENTATION
#include "gpuvis_macros.h"
#include "tdopexpr.h"
#include "trace-cmd/trace-read.h"
#include "stlini.h"
#include "gpuvis_utils.h"
#include "etl_utils.h"
#include "gpuvis_etl.h"
#include "gpuvis.h"
/**
* Extract a data member from an ETL trace using TDH
*
* This method will extract the property at index 'prop' from pEvent
*
* If this property is an array, 'idx' will specify which array element to index
*/
bool tdh_extract_property( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, int idx, int intype, void* &out )
{
DWORD status = ERROR_SUCCESS;
USHORT ArraySize = 0;
PEVENT_MAP_INFO pMapInfo = nullptr;
PROPERTY_DATA_DESCRIPTOR DataDescriptors[2];
ULONG DescriptorsCount = 0;
DWORD PropertySize = 0;
PBYTE pData = nullptr;
status = GetArraySize( pEvent, pInfo, prop, &ArraySize );
if ( status != ERROR_SUCCESS )
{
logf( "Failed to extract property: error calculating array size\n" );
goto error;
}
//wprintf( L"%s", (LPWSTR)( (PBYTE)(pInfo)+pInfo->EventPropertyInfoArray[prop].NameOffset ) );
// We only support simple properties at the moment, no structs
if ( ( pInfo->EventPropertyInfoArray[prop].Flags & PropertyStruct ) == PropertyStruct ||
ArraySize != 1)
{
logf( "Failed to extract property: complex types unsupported\n" );
goto error;
}
ZeroMemory( &DataDescriptors, sizeof( DataDescriptors ) );
DataDescriptors[0].PropertyName = (ULONGLONG)( (PBYTE)(pInfo)+pInfo->EventPropertyInfoArray[prop].NameOffset );
DataDescriptors[0].ArrayIndex = idx;
DescriptorsCount = 1;
status = TdhGetPropertySize( pEvent, 0, nullptr, DescriptorsCount, &DataDescriptors[0], &PropertySize );
if ( status != ERROR_SUCCESS )
{
logf( "Failed to extract property: error calculating property size\n" );
goto error;
}
pData = (PBYTE)malloc( PropertySize );
if ( nullptr == pData )
{
logf( "Failed to extract property: error calculating property size\n" );
goto error;
}
status = TdhGetProperty( pEvent, 0, nullptr, DescriptorsCount, &DataDescriptors[0], PropertySize, pData );
if ( status != ERROR_SUCCESS )
{
logf( "Failed to extract property: error retriving property\n" );
goto error;
}
status = GetMapInfo( pEvent,
(PWCHAR)( (PBYTE)(pInfo)+pInfo->EventPropertyInfoArray[prop].nonStructType.MapNameOffset ),
pInfo->DecodingSource,
pMapInfo );
if ( status != ERROR_SUCCESS )
{
logf( "Failed to extract property: retriving map info\n" );
goto error;
}
// verify that the call matches our expected type
_TDH_IN_TYPE eInfoType = (_TDH_IN_TYPE)pInfo->EventPropertyInfoArray[prop].nonStructType.InType;
_TDH_IN_TYPE eRequestedType = ( _TDH_IN_TYPE)intype;
if ( eInfoType != eRequestedType )
{
logf( "Failed to extract property: type mismatch\n" );
goto error;
}
out = pData;
if ( pMapInfo )
free( pMapInfo );
return true;
error:
if ( pData )
free( pData );
if ( pMapInfo )
free( pMapInfo );
return false;
}
template< typename T, int intype >
bool tdh_extract_property_typed( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, int idx, T &out )
{
void *pData = nullptr;
if ( !tdh_extract_property( pEvent, pInfo, prop, idx, intype, pData ) || !pData )
{
goto error;
}
out = *( (T*)pData );
free( pData );
return true;
error:
if ( pData )
free( pData );
return false;
}
template< int intype >
bool tdh_extract_property_typed( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, int idx, std::string &out )
{
void *pData = nullptr;
if ( !tdh_extract_property( pEvent, pInfo, prop, idx, intype, pData ) || !pData )
{
goto error;
}
out = (const char *)pData;
free( pData );
return true;
error:
if ( pData )
free( pData );
return false;
}
template< int intype >
bool tdh_extract_property_typed( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, int idx, std::wstring &out )
{
void *pData = nullptr;
if ( !tdh_extract_property( pEvent, pInfo, prop, idx, intype, pData ) || !pData )
{
goto error;
}
out = (const wchar_t *)pData;
free( pData );
return true;
error:
if ( pData )
free( pData );
return false;
}
/**
* Helper for simple extraction from type information
*/
bool tdh_extract_a( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, int idx, std::string &out )
{
return tdh_extract_property_typed<TDH_INTYPE_ANSISTRING>( pEvent, pInfo, prop, idx, out );
}
bool tdh_extract( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, std::string &out )
{
return tdh_extract_a( pEvent, pInfo, prop, 0, out );
}
bool tdh_extract_a( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, int idx, std::wstring &out )
{
return tdh_extract_property_typed<TDH_INTYPE_UNICODESTRING>( pEvent, pInfo, prop, idx, out );
}
bool tdh_extract( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, std::wstring &out )
{
return tdh_extract_a( pEvent, pInfo, prop, 0, out );
}
bool tdh_extract_a( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, int idx, uint32_t &out )
{
return tdh_extract_property_typed<uint32_t, TDH_INTYPE_UINT32>( pEvent, pInfo, prop, idx, out );
}
bool tdh_extract( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, uint32_t &out )
{
return tdh_extract_a( pEvent, pInfo, prop, 0, out );
}
bool tdh_extract_a( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, int idx, uint64_t &out )
{
return tdh_extract_property_typed<uint64_t, TDH_INTYPE_UINT64>( pEvent, pInfo, prop, idx, out );
}
bool tdh_extract( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, uint64_t &out )
{
return tdh_extract_a( pEvent, pInfo, prop, 0, out );
}
bool tdh_extract_a( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, int idx, void* &out )
{
return tdh_extract_property_typed<void*, TDH_INTYPE_POINTER>( pEvent, pInfo, prop, idx, out );
}
bool tdh_extract( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO pInfo, int prop, void* &out )
{
return tdh_extract_a( pEvent, pInfo, prop, 0, out );
}
/**
* etl_reader_t reads a etl file and provides each event as a set of key/value pairs
*
* Refer to:
* https://docs.microsoft.com/en-us/windows/desktop/etw/event-trace-logfile
* https://docs.microsoft.com/en-us/windows/desktop/etw/using-tdhformatproperty-to-consume-event-data
*/
class etl_reader_t
{
public:
struct etl_reader_cb_data_t
{
void* ctx;
PEVENT_RECORD event;
PTRACE_EVENT_INFO info;
};
typedef void( *EventCallback )( etl_reader_cb_data_t *cbdata );
etl_reader_t( const char *file, EventCallback cb, void *ctx )
: mFileName( file )
, mTraceHandle( 0 )
, mParserCallback( cb )
, mParserCtx( ctx )
{
}
DWORD get_event_info( PEVENT_RECORD pEvent, PTRACE_EVENT_INFO & pInfo )
{
DWORD status = ERROR_SUCCESS;
DWORD BufferSize = 0;
// Retrieve the required buffer size for the event metadata.
status = TdhGetEventInformation( pEvent, 0, nullptr, pInfo, &BufferSize );
if ( ERROR_INSUFFICIENT_BUFFER == status )
{
pInfo = (TRACE_EVENT_INFO*)malloc( BufferSize );
if ( pInfo == nullptr )
{
logf( "Failed to allocate memory for event info (size=%lu).\n", BufferSize );
return ERROR_OUTOFMEMORY;
}
// Retrieve the event metadata.
status = TdhGetEventInformation( pEvent, 0, nullptr, pInfo, &BufferSize );
}
if ( ERROR_SUCCESS != status )
{
logf( "TdhGetEventInformation failed with 0x%x.\n", status );
}
return status;
}
bool is_parseable_event( PTRACE_EVENT_INFO info )
{
switch ( info->DecodingSource )
{
case DecodingSourceWbem:
case DecodingSourceXMLFile:
return true;
default:
return false;
}
}
static void WINAPI process_event_cb( PEVENT_RECORD event )
{
etl_reader_t *ctx = (etl_reader_t *)event->UserContext;
ctx->process_event( event );
}
void process_event( PEVENT_RECORD event )
{
DWORD status = ERROR_SUCCESS;
PTRACE_EVENT_INFO info = nullptr;
status = get_event_info( event, info );
if ( ERROR_SUCCESS != status )
{
logf( "Failed to get event information failed with %lu\n", status );
return;
}
if ( !is_parseable_event( info ) )
{
return;
}
etl_reader_cb_data_t cbdata = { 0 };
cbdata.ctx = mParserCtx;
cbdata.event = event;
cbdata.info = info;
mParserCallback( &cbdata );
free( info );
}
int process()
{
TDHSTATUS status = ERROR_SUCCESS;
EVENT_TRACE_LOGFILE trace;
TRACE_LOGFILE_HEADER* pHeader = &trace.LogfileHeader;
ZeroMemory( &trace, sizeof( EVENT_TRACE_LOGFILE ) );
trace.LogFileName = (char *)mFileName;
trace.EventRecordCallback = (PEVENT_RECORD_CALLBACK)( process_event_cb );
trace.ProcessTraceMode = PROCESS_TRACE_MODE_EVENT_RECORD;
trace.Context = this;
mTraceHandle = OpenTrace( &trace );
if ( INVALID_PROCESSTRACE_HANDLE == mTraceHandle )
{
logf( "Failed to open etl trace %s: %lu\n", mFileName, GetLastError() );
return -1;
}
mIsUserTrace = pHeader->LogFileMode & EVENT_TRACE_PRIVATE_LOGGER_MODE;
logf( "Number of events lost: %lu\n", pHeader->EventsLost );
logf( "Number of buffers lost: %lu\n", pHeader->BuffersLost );
status = ProcessTrace( &mTraceHandle, 1, 0, 0 );
if ( status != ERROR_SUCCESS && status != ERROR_CANCELLED )
{
logf( "Failed to process trace: %lu\n", status );
return -1;
}
logf( "Loading OK\n" );
return 0;
}
// return false at end of stream
bool parse_entry( std::istringstream &stream, std::string &key, std::string &val )
{
std::string garbage;
std::getline( stream, key, '=' );
if ( stream.fail() )
return false;
std::getline( stream, garbage, '`' );
if ( stream.fail() )
return false;
std::getline( stream, val, '`' );
if ( stream.fail() )
return false;
// Eat the last space
std::getline( stream, garbage, ' ' );
return true;
}
std::unordered_map<std::string, std::string> get_event()
{
std::unordered_map<std::string, std::string> map = {};
//if ( !mFileStream )
return map;
std::string entry;
//if ( !std::getline( mFileStream, entry ) )
return map;
std::istringstream event( entry );
std::string key, val;
while ( parse_entry( event, key, val ) )
{
map[key] = val;
}
// Save the original text for error handling
map["etl_line"] = entry;
return map;
}
private:
const char *mFileName;
TRACEHANDLE mTraceHandle;
bool mIsUserTrace;
EventCallback mParserCallback;
void * mParserCtx;
};
/**
* Extract the i'th property into a variable by reference
*/
#define ETL_EXTRACT( i, ref ) tdh_extract( cbdata->event, cbdata->info, i, ref )
/**
* Dump all properties to figure out what is needed
*/
#define ETL_DUMP() DumpEventMetadata( cbdata->info ); DumpProperties( cbdata->event, cbdata->info )
/**
* The x_entry_t classes are helpers to interpret the etl data as c++ types
*/
class context_entry_t
{
public:
std::wstring file;
std::string os_version;
uint32_t num_cpu;
uint64_t start_time;
uint64_t end_time;
context_entry_t( etl_reader_t::etl_reader_cb_data_t *cbdata )
{
os_version = "windows";
ETL_EXTRACT( 22, file );
ETL_EXTRACT( 3, num_cpu );
ETL_EXTRACT( 18, start_time );
ETL_EXTRACT( 4, end_time );
}
};
class event_entry_t
{
public:
uint64_t ts;
int cpu;
int pid;
int tid;
std::string pname;
event_entry_t( etl_reader_t::etl_reader_cb_data_t *cbdata )
{
PTRACE_EVENT_INFO pinfo = cbdata->info;
EVENT_HEADER *header = &cbdata->event->EventHeader;
ts = header->TimeStamp.QuadPart;
cpu = cbdata->event->BufferContext.ProcessorNumber;
pid = header->ProcessId;
tid = header->ThreadId;
pname = "process"; //TODO
}
};
class steamvr_entry_t : public event_entry_t
{
public:
std::string vrevent;
steamvr_entry_t( etl_reader_t::etl_reader_cb_data_t *cbdata ) :
event_entry_t( cbdata )
{
ETL_EXTRACT( 0, vrevent );
}
};
class vsync_entry_t : public event_entry_t
{
public:
void *adapter;
uint32_t display;
uint64_t address;
vsync_entry_t( etl_reader_t::etl_reader_cb_data_t *cbdata ) :
event_entry_t( cbdata )
{
ETL_EXTRACT( 0, adapter );
ETL_EXTRACT( 1, display );
ETL_EXTRACT( 2, address );
}
};
class queue_packet_header_entry_t : public event_entry_t
{
public:
void *ctx;
uint32_t ptype;
uint32_t seq;
queue_packet_header_entry_t( etl_reader_t::etl_reader_cb_data_t *cbdata ) :
event_entry_t( cbdata )
{
ETL_EXTRACT( 0, ctx );
ETL_EXTRACT( 1, ptype );
ETL_EXTRACT( 2, seq );
}
};
class dma_packet_header_entry_t : public event_entry_t
{
public:
void *ctx;
void *qctx;
uint32_t ptype;
uint32_t submit_seq;
uint32_t seq;
dma_packet_header_entry_t( etl_reader_t::etl_reader_cb_data_t *cbdata ) :
event_entry_t( cbdata )
{
int i = 0;
UCHAR opcode = cbdata->event->EventHeader.EventDescriptor.Opcode;
ETL_EXTRACT( i++, ctx );
// Field only present in the start packet
if ( opcode == EVENT_TRACE_TYPE_START )
ETL_EXTRACT( i++, qctx );
else
qctx = nullptr;
ETL_EXTRACT( i++, ptype );
ETL_EXTRACT( i++, submit_seq );
ETL_EXTRACT( i++, seq );
}
};
/**
* Parses the ETL information stream
*
* The ETL input stream is converted into a trace_info_t + a sequence of trace_event_t
*/
class etl_parser_t
{
private:
class __declspec( uuid( "{8F8F13B1-60EB-4B6A-A433-DE86104115AC}" ) ) kSteamVrProvider;
class __declspec( uuid( "{802ec45a-1e99-4b83-9920-87c98277ba9d}" ) ) kDxcProvider;
// Get these from Microsoft-Windows-DxgKrnl.manifest.xml
static const int kDxcVsyncTaskId = 10;
static const int kDxcQueuePacketTaskId = 9;
static const int kDxcDmaPacketTaskId = 8;
public:
// Forward the callback to the right object
static void process_event_cb_proxy( etl_reader_t::etl_reader_cb_data_t *cbdata )
{
etl_parser_t *pthis = (etl_parser_t *)cbdata->ctx;
pthis->process_event_cb( cbdata );
}
etl_parser_t( const char *file, StrPool &strpool, trace_info_t &trace_info, EventCallback &cb )
: mFileName( file )
, mStrPool( strpool )
, mTraceInfo( trace_info )
, mCallback( cb )
, mReader( file, process_event_cb_proxy, this )
, mCurrentEventId( 0 )
, mStartTicks( 0 )
, mAdapterCount( 0 )
, mCrtcCount( 0 )
{
memset( mCrtcCurrentSeq, 0, sizeof( mCrtcCurrentSeq ) );
}
int process()
{
int err;
err = mReader.process();
if ( err )
{
return err;
}
return 0;
}
int process_event_cb( etl_reader_t::etl_reader_cb_data_t *cbdata )
{
PEVENT_RECORD event = cbdata->event;
PTRACE_EVENT_INFO info = cbdata->info;
GUID *providerGuid = &event->EventHeader.ProviderId;
UCHAR opcode = event->EventHeader.EventDescriptor.Opcode;
USHORT task = event->EventHeader.EventDescriptor.Task;
int ret = -1;
// Trace events provide context information
if ( IsEqualGUID( *providerGuid, EventTraceGuid ) )
{
switch ( opcode )
{
case EVENT_TRACE_TYPE_INFO:
ret = process_context_entry( context_entry_t( cbdata ) );
break;
}
}
// SteamVR is a known user provider that generates events
else if ( IsEqualGUID( *providerGuid, __uuidof( kSteamVrProvider ) ) )
{
switch ( opcode )
{
case EVENT_TRACE_TYPE_INFO:
ret = process_steamvr_entry( steamvr_entry_t( cbdata ) );
break;
}
}
// The DX driver has a lot of interesting information
else if ( IsEqualGUID( *providerGuid, __uuidof( kDxcProvider ) ) )
{
switch ( task )
{
case kDxcVsyncTaskId:
switch ( opcode )
{
case EVENT_TRACE_TYPE_INFO:
ret = process_vsync_entry( vsync_entry_t( cbdata ) );
break;
}
break;
case kDxcQueuePacketTaskId:
ret = process_queue_packet_entry( cbdata );
break;
case kDxcDmaPacketTaskId:
ret = process_dma_packet_entry( cbdata );
break;
}
}
else
{
//DumpEventMetadata( info );
//DumpProperties( event, info );
}
return ret;
}
private:
static const int kMaxCrtc = 32;
const char *mFileName;
StrPool &mStrPool;
trace_info_t &mTraceInfo;
EventCallback &mCallback;
etl_reader_t mReader;
uint32_t mCurrentEventId;
uint64_t mStartTicks;
std::unordered_map<uint64_t, int> mAdapterMap;
int mAdapterCount;
std::unordered_map<uint64_t, int> mCrtcMap;
int mCrtcCount;
uint64_t mCrtcCurrentSeq[ kMaxCrtc ];
int GetAdapterIdx( uint64_t key )
{
if ( mAdapterMap.find( key ) == mAdapterMap.end() )
{
mAdapterMap[key] = mAdapterCount++;
}
return mAdapterMap[key];
}
int GetCrtcIdx( uint64_t key )
{
if ( mCrtcMap.find( key ) == mCrtcMap.end() )
{
mCrtcMap[key] = mCrtcCount++;
assert( mCrtcCount < kMaxCrtc );
}
return mCrtcMap[key];
}
int64_t ticks_to_relative_us( uint64_t ticks )
{
return ( ticks - mStartTicks ) * 100;
}
std::string sfromws( std::wstring ws)
{
return std::string( ws.begin(), ws.end() );
}
int process_context_entry( context_entry_t entry )
{
mStartTicks = entry.start_time;
mTraceInfo.cpus = entry.num_cpu;
mTraceInfo.file = sfromws(entry.file);
mTraceInfo.uname = entry.os_version;
mTraceInfo.timestamp_in_us = true; // nanoseconds?
mTraceInfo.min_file_ts = ticks_to_relative_us( entry.start_time );
mTraceInfo.cpu_info.resize( entry.num_cpu );
for ( size_t cpu = 0; cpu < entry.num_cpu; cpu++ )
{
cpu_info_t &cpu_info = mTraceInfo.cpu_info[cpu];
cpu_info.file_offset = 0;
cpu_info.file_size = 0;
cpu_info.entries = 0;
cpu_info.overrun = 0;
cpu_info.commit_overrun = 0;
cpu_info.bytes = 0;
cpu_info.oldest_event_ts = ticks_to_relative_us( entry.start_time );;
cpu_info.now_ts = ticks_to_relative_us( entry.end_time );
cpu_info.dropped_events = 0;
cpu_info.read_events = 0;
}
return 0;
}
// In linux tgid is the process id
bool is_process_known( int pid )
{
return mTraceInfo.tgid_pids.m_map.find( pid ) != mTraceInfo.tgid_pids.m_map.end();
}
// In linux pid is the thread id
bool is_thread_known( int tid )
{
return mTraceInfo.pid_comm_map.m_map.find( tid ) != mTraceInfo.pid_comm_map.m_map.end();
}
// Process the common information for all events
int process_event_entry( event_entry_t entry, trace_event_t &event )
{
const char *comm = mStrPool.getstrf( "%s-%u", entry.pname.c_str(), entry.tid );
if ( !is_thread_known( entry.tid ) )
{
mTraceInfo.pid_comm_map.get_val( entry.tid, mStrPool.getstr( comm ) );
}
if ( !is_process_known( entry.pid ) )
{
tgid_info_t *tgid_info = mTraceInfo.tgid_pids.get_val_create( entry.pid );
if ( !tgid_info->tgid )
{
tgid_info->tgid = entry.pid;
tgid_info->hashval += hashstr32( comm );
}
tgid_info->add_pid( entry.tid );
// Pid --> tgid
mTraceInfo.pid_tgid_map.get_val( entry.tid, entry.pid );
}
event.pid = entry.tid;
event.id = mCurrentEventId++;
event.cpu = entry.cpu;
event.ts = ticks_to_relative_us( entry.ts );
event.comm = comm;
event.user_comm = comm;
event.seqno = 0;
return 0;
}
// Process steamvr event specific information
int process_steamvr_entry( steamvr_entry_t entry )
{
int err;
trace_event_t event;
err = process_event_entry( entry, event );
if ( err )
return err;
event.system = mStrPool.getstr( "ftrace-print" ); // For dat compatibility
event.name = mStrPool.getstr( "steamvr" );
event.numfields = 1;
event.fields = new event_field_t[event.numfields];
event.fields[0].key = mStrPool.getstr( "buf" );
event.fields[0].value = mStrPool.getstr( entry.vrevent.c_str() );
event.flags = TRACE_FLAG_FTRACE_PRINT;
return mCallback( event );
}
// Process vsync event specific information
int process_vsync_entry( vsync_entry_t entry )
{
int err;
trace_event_t event;
err = process_event_entry( entry, event );
if ( err )
return err;
int crtc = GetCrtcIdx( entry.display );
int adapter = GetAdapterIdx( (uint64_t)entry.adapter );
uint64_t seq = mCrtcCurrentSeq[crtc]++;
event.system = mStrPool.getstr( "drm" ); // For dat compatibility
event.name = mStrPool.getstr( "drm_vblank_event" ); // For dat compatibility
event.crtc = crtc;
event.numfields = 2;
event.fields = new event_field_t[event.numfields];
event.fields[0].key = mStrPool.getstr( "crtc" );
event.fields[0].value = mStrPool.getstrf( "%d", crtc );
event.fields[1].key = mStrPool.getstr( "seq" );
event.fields[1].value = mStrPool.getstrf( "%ull", seq );
event.flags = TRACE_FLAG_VBLANK;
return mCallback( event );
}
int process_queue_packet_entry( etl_reader_t::etl_reader_cb_data_t *cbdata )
{
int err = -1;
trace_event_t event;
std::string timeline = "";
queue_packet_header_entry_t header( cbdata );
UCHAR opcode = cbdata->event->EventHeader.EventDescriptor.Opcode;
switch ( header.ptype )
{
case DXGKETW_RENDER_COMMAND_BUFFER:
case DXGKETW_DEFERRED_COMMAND_BUFFER:
case DXGKETW_SYSTEM_COMMAND_BUFFER:
timeline = "gfx";
break;
default:
//ETL_DUMP();
return -1;
}
switch ( opcode )
{
case EVENT_TRACE_TYPE_START:
// Packet was received by the scheduler
event.name = mStrPool.getstr( "amdgpu_cs_ioctl" ); // For dat compatibility
event.flags = TRACE_FLAG_SW_QUEUE;
break;
case EVENT_TRACE_TYPE_INFO:
// Begin move to HW queue? Use DmaPacket/Start instead
return 0;
case EVENT_TRACE_TYPE_STOP:
// Packet is no longer in use by the driver, don't care
return 0;
default:
return 0;
}
err = process_event_entry( header, event );
if ( err )
return err;
event.system = mStrPool.getstr( "QueuePacket" );
event.numfields = 3;
event.fields = new event_field_t[event.numfields];
event.fields[0].key = mStrPool.getstr( "timeline" );
event.fields[0].value = mStrPool.getstr( timeline.c_str() );
event.fields[1].key = mStrPool.getstr( "context" );
event.fields[1].value = mStrPool.getstrf( "0x%xll", header.ctx );
event.fields[ 2 ].key = mStrPool.getstr( "seq" );
event.fields[ 2 ].value = mStrPool.getstrf( "%u", header.seq );
event.seqno = header.seq;
return mCallback( event );
}
int process_dma_packet_entry( etl_reader_t::etl_reader_cb_data_t *cbdata )
{
int err = -1;
trace_event_t event;
dma_packet_header_entry_t header( cbdata );
std::string timeline = "gfx";
UCHAR opcode = cbdata->event->EventHeader.EventDescriptor.Opcode;
switch ( opcode )
{
case EVENT_TRACE_TYPE_START:
// Submit to the HW engine
event.name = mStrPool.getstr( "amdgpu_sched_run_job" ); // For dat compatibility
event.flags = TRACE_FLAG_HW_QUEUE;
break;
case EVENT_TRACE_TYPE_INFO:
// Finished processing by the GPU ISR
event.name = mStrPool.getstr( "fence_signaled" ); // For dat compatibility
event.flags = TRACE_FLAG_FENCE_SIGNALED;
break;
default:
return 0;
}
err = process_event_entry( header, event );
if ( err )
return err;
event.system = mStrPool.getstr( "QueuePacket" );
event.numfields = 3;
event.fields = new event_field_t[ event.numfields ];
event.fields[ 0 ].key = mStrPool.getstr( "timeline" );
event.fields[ 0 ].value = mStrPool.getstr( timeline.c_str() );
event.fields[ 1 ].key = mStrPool.getstr( "context" );
event.fields[ 1 ].value = mStrPool.getstrf( "0x%xll", header.ctx );
event.fields[ 2 ].key = mStrPool.getstr( "seq" );
event.fields[ 2 ].value = mStrPool.getstrf( "%u", header.seq );
event.seqno = header.seq;
return mCallback( event );
}
};
int read_etl_file( const char *file, StrPool &strpool, trace_info_t &trace_info, EventCallback &cb )
{
etl_parser_t parser( file, strpool, trace_info, cb );
return parser.process();
}
#else
// Stub implementation for non-windows OSs
int read_etl_file( const char *file, StrPool &strpool, trace_info_t &trace_info, EventCallback &cb )
{
return -1;