-
Notifications
You must be signed in to change notification settings - Fork 9
/
GpuMemDumpPerfetto.py
executable file
·480 lines (411 loc) · 18 KB
/
GpuMemDumpPerfetto.py
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
#!/usr/bin/env python3
# kram - Copyright 2020-2023 by Alec Miller. - MIT License
# The license and copyright notice shall be included
# in all copies or substantial portions of the Software.
#
# This is derived from GpuMemDumpVis.py, and like it, doesn't handle aliasing.
# Only reads size and not offset of allocations. But Perfetto can't handle overlapping rects.
# Unlike the png, Pefetto can zoom in and display the allocation names and sizes.
#
# Copyright (c) 2018-2023 Advanced Micro Devices, Inc. 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.
#
import argparse
import json
# this is all for doing queries, not writing out traces which only has C++ calls
# did a pip3 install perfetto, but no docs on it https://pypi.org/project/perfetto/
# import perfetto
# https://android.googlesource.com/platform/external/perfetto/+/refs/heads/master/python/example.py
# from perfetto.trace_processor import TraceProcessor, TraceProcessorConfig
# https://perfetto.dev/docs/analysis/batch-trace-processor
#
# https://perfetto.dev/docs/reference/synthetic-track-event
# No phtyon writer, and so everything has to be protobuf based
# https://perfetto.dev/docs/design-docs/protozero
PROGRAM_VERSION = 'Vulkan/D3D12 Memory Allocator Dump Perfetto 3.0.3'
# DONE: Perfetto can't handle empty string for name on the allocs
# so had to set them to 'M'. This is getting fixed.
# https://r.android.com/2817378
# DONE: cname doesn't seem to import and has limited colors
# cname is ignored by parser, there is a layer and bg color, but need to set color for a block
# find out how Peretto consistently colors layers by name
# https://github.com/google/perfetto/blob/master/src/trace_processor/importers/json/json_trace_parser.cc
# https://github.com/google/perfetto/issues/620
# DONE: call 'set timestamp format' to seconds from UI (cmd+shift+P),
# but this doesn't affect duration display which is still formatted. Second lines reflect MB.
# TODO: Pefetto doesn't want to extend/support Cataylst json format, but doesn't have a json of its own
# https://github.com/google/perfetto/issues/622
# https://github.com/google/perfetto/issues/623
# TODO: add totals, already know none are empty. Add these to a summary track.
# can have count/mem of each time and potentially across all types
# dx12 or vulkan
currentApi = ""
# input data dictionary
data = {}
# remap names to index for obfuscation, can then share mem maps
nameIndexer = { "": 0 }
nameIndexerCounter = 0
# now convert the dictionaries to new dictionaries, and then out to json
# TODO: ms = *1e-3 not quite kb, ns = *1E-9
# when using ms, then values can hit minute and hour time conversions which /60 instead of /100,
# but ns is also goofy due to 1e9 being gb.
perfettoDict = {
'displayTimeUnit': 'ms',
'systemTraceEvents': 'SystemTraceData',
'traceEvents': [],
}
def ParseArgs():
argParser = argparse.ArgumentParser(description='Visualization of Vulkan/D3D12 Memory Allocator JSON dump in Perfetto.')
argParser.add_argument('DumpFile', help='Path to source JSON file with memory dump created by Vulkan/D3D12 Memory Allocator library')
argParser.add_argument('-v', '--version', action='version', version=PROGRAM_VERSION)
# TODO: derive output from input name if not present
argParser.add_argument('-o', '--output', required=True, help='Path to destination trace file')
return argParser.parse_args()
def GetDataForMemoryPool(poolTypeName):
global data
if poolTypeName in data:
return data[poolTypeName]
else:
newPoolData = {'DedicatedAllocations':[],
'Blocks':[],
'CustomPools':{}}
data[poolTypeName] = newPoolData
return newPoolData
def ProcessBlock(poolData, block):
blockInfo = {'ID': block[0],
'Size': int(block[1]['TotalBytes']),
'Suballocations':[]}
for alloc in block[1]['Suballocations']:
allocData = {'Type': alloc['Type'],
'Size': int(alloc['Size']),
'Usage': int(alloc['Usage']) if 'Usage' in alloc else 0,
'Name': alloc['Name'] if 'Name' in alloc else 'M' }
blockInfo['Suballocations'].append(allocData)
poolData['Blocks'].append(blockInfo)
def IsDataEmpty():
global data
for poolData in data.values():
if len(poolData['DedicatedAllocations']) > 0:
return False
if len(poolData['Blocks']) > 0:
return False
for customPool in poolData['CustomPools'].values():
if len(customPool['Blocks']) > 0:
return False
if len(customPool['DedicatedAllocations']) > 0:
return False
return True
def RemoveEmptyType():
global data
for poolType in list(data.keys()):
pool = data[poolType]
if len(pool['DedicatedAllocations']) > 0:
continue
if len(pool['Blocks']) > 0:
continue
empty = True
for customPool in pool['CustomPools'].values():
if len(customPool['Blocks']) > 0:
empty = False
break
if len(customPool['DedicatedAllocations']) > 0:
empty = False
break
if empty:
del data[poolType]
def AllocTypeToCategory(type, usage):
global currentApi
if type == 'FREE':
return " "
elif type == 'UNKNOWN':
return "??"
if currentApi == 'Vulkan':
if type == 'BUFFER':
# https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkBufferUsageFlagBits.html
if (usage & 0x0080) != 0: # VK_USAGE_VERTEX_BUFFER_BIT
return "VB"
elif (usage & 0x040) != 0: # INDEX_BUFFER
return "IB"
elif (usage & 0x0014) != 0: # UNIFORM_BUFFER | UNIFORM_TEXEL_BUFFER
return "UB"
elif (usage & 0x0100) != 0: # INDIRECT_BUFFER
return "DB"
elif (usage & 0x0028) != 0: # STORAGE_BUFFER | STORAGE_TEXEL_BUFFER
return "SB"
elif (usage & 0x0003) != 0: # Staging buffer only sets 1 or 2 bit, calling this MB for memory
return "MB"
else:
return "?B" # TODO: getting this on some buffers, so identify more
elif type == 'IMAGE_OPTIMAL':
# TODO: need tex type (2d, 3d, ...)
# https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkImageUsageFlagBits.html
if (usage & 0x20) != 0: # DEPTH_STENCIL_ATTACHMENT
return "DT"
elif (usage & 0xD0) != 0: # INPUT_ATTACHMENT | TRANSIENT_ATTACHMENT | COLOR_ATTACHMENT
return "RT"
elif (usage & 0x4) != 0: # SAMPLED
return "TT"
else:
return "?T"
elif type == 'IMAGE_LINEAR' :
return "LT"
elif type == 'IMAGE_UNKNOWN':
return "?T"
elif currentApi == 'Direct3D 12':
if type == 'BUFFER':
return "?B"
elif type == 'TEXTURE1D' or type == 'TEXTURE2D' or type == 'TEXTURE3D':
if (usage & 0x2) != 0: # D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL
return "DT"
elif (usage & 0x5) != 0: # D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS
return "RT"
elif (usage & 0x8) == 0: # Not having D3D12_RESOURCE_FLAG_DENY_SHARED_RESOURCE
return "TT"
else:
return "?T"
else:
print("Unknown graphics API!")
exit(1)
assert False
return "??"
# not many cname colors to choose from
# https://github.com/catapult-project/catapult/blob/master/tracing/tracing/base/color_scheme.html
def AllocCategoryToColor(category):
color = "grey"
if category[1] == 'B':
if category == 'VB':
color = "olive"
elif category == 'IB':
color = "white"
elif category == 'DB':
color = "white"
else:
color = "white"
elif category[1] == 'T':
color = "yellow"
return color
# a way to obscure names, so can share maps publicly
def RemapName(name):
# TODO: perfetto doesn't uniquely color the names if using numbers
# or even N + num when they differ. Find out what color criteria needs.
useNameIndexer = False
if useNameIndexer:
global nameIndexer
global nameIndexerCounter
if name in nameIndexer:
name = str(nameIndexer[name])
else:
nameIndexer[name] = nameIndexerCounter
name = str(nameIndexerCounter)
nameIndexerCounter += 1
return name
def AddTraceEventsAlloc(alloc, addr, blockCounter):
global perfettoDict
# settings
# this makes it harder to look for strings, but Perfetto can't control color
# so this prepends the type/category
prependCategory = True
# this has a downside that empty blocks and tail end of block isn't clear
# but it does cut down on data
skipFreeAlloc = True
isFreeAlloc = alloc['Type'] == 'FREE'
if (skipFreeAlloc and isFreeAlloc):
return
size = alloc['Size']
category = AllocTypeToCategory(alloc['Type'], alloc['Usage'])
# this is optinonal, Pefetto will psuedocolor different names
# but this is one option for consistent coloring
# perfetto doesn't seem to honor set cname
# https://github.com/catapult-project/catapult/blob/master/tracing/tracing/base/color_scheme.html
#color = AllocCategoryToColor(category)
name = RemapName(alloc['Name'])
# prepend category
if (prependCategory and not isFreeAlloc):
name = category + "-" + name
traceEvent = {
'name': name,
'ph': 'X',
'ts': int(addr),
'dur': int(size),
'tid': int(blockCounter),
#'pid': 0,
#'cname': color,
'cat': category
}
# complete event X is much less data than B/E
# these cannot be nested or overlap, so no aliasing
perfettoDict['traceEvents'].append(traceEvent)
def AddTraceEventsBlock(block, blockCounter):
global perfettoDict
# TODO: could collect together contig empty blocks. Lots of 'M' values otherwise.
# this would require passing down size
addr = int(0)
for alloc in block['Suballocations']:
AddTraceEventsAlloc(alloc, addr, blockCounter)
addr = addr + int(alloc['Size'])
def AddBlockName(blockName, blockCounter):
global perfettoDict
perfettoDict['traceEvents'].append({
'name': 'thread_name',
'ph': 'M',
'tid': int(blockCounter),
#'pid': 0,
'args': {
'name': blockName
}
})
def AddProcessName(processName):
global perfettoDict
perfettoDict['traceEvents'].append({
'name': 'process_name',
'ph': 'M',
'pid': 0,
'args': {
'name': processName
}
})
def AddTraceEvents(addBlockNames):
global perfettoDict
blockCounter = 0
# DONE: add all block names first across all pools, then add the allocations
# TODO: do dedicated allocations need sorted?
# TODO: could specify pid for memType, but think has to be stored per alloc
# for poolData in data.values():
for memType in sorted(data.keys()):
poolData = data[memType]
# strip 'Type ' off string
poolIndex = memType[5:]
# report for default pool
# block allocs
blockIndex = 0
for block in poolData['Blocks']:
if addBlockNames:
blockName = "T{} b{} {}".format(poolIndex, blockIndex, block['ID'])
AddBlockName(blockName, blockCounter)
else:
AddTraceEventsBlock(block, blockCounter)
blockCounter += 1
blockIndex += 1
# dedicated allocs
allocationIndex = 0
for dedicatedAlloc in poolData['DedicatedAllocations']:
if addBlockNames:
blockName = 'T{} a{} {}'.format(poolIndex, allocationIndex, dedicatedAlloc['Name'])
AddBlockName(blockName, blockCounter)
else:
AddTraceEventsAlloc(dedicatedAlloc, 0, blockCounter)
blockCounter += 1
allocationIndex += 1
# repeat for custom pools
for customPoolName, customPoolData in poolData['CustomPools'].items():
# pool block allocs
blockIndex = 0
for block in customPoolData['Blocks']:
if addBlockNames:
blockName = 'T{} {} b{} {}'.format(poolIndex, customPoolName, blockIndex, block['ID'])
AddBlockName(blockName, blockCounter)
else:
AddTraceEventsBlock(block, blockCounter)
blockCounter += 1
blockIndex += 1
# pool dedicated allocs
allocationIndex = 0
for dedicatedAlloc in customPoolData['DedicatedAllocations']:
if addBlockNames:
blockName = 'T{} {} a{} {}'.format(poolIndex, customPoolName, allocationIndex, dedicatedAlloc['Name'])
AddBlockName(blockName, blockCounter)
else:
AddTraceEventsAlloc(dedicatedAlloc, 0, blockCounter)
blockCounter += 1
allocationIndex += 1
if __name__ == '__main__':
args = ParseArgs()
jsonSrc = json.load(open(args.DumpFile, 'rb'))
if 'General' in jsonSrc:
currentApi = jsonSrc['General']['API']
else:
print("Wrong JSON format, cannot determine graphics API!")
exit(1)
# Process default pools
if 'DefaultPools' in jsonSrc:
for memoryPool in jsonSrc['DefaultPools'].items():
poolData = GetDataForMemoryPool(memoryPool[0])
# Get dedicated allocations
for dedicatedAlloc in memoryPool[1]['DedicatedAllocations']:
allocData = {'Type': dedicatedAlloc['Type'],
'Size': int(dedicatedAlloc['Size']),
'Usage': int(dedicatedAlloc['Usage']),
'Name': dedicatedAlloc['Name'] if 'Name' in dedicatedAlloc else 'M'}
poolData['DedicatedAllocations'].append(allocData)
# Get allocations in block vectors
for block in memoryPool[1]['Blocks'].items():
ProcessBlock(poolData, block)
# Process custom pools
if 'CustomPools' in jsonSrc:
for memoryPool in jsonSrc['CustomPools'].items():
poolData = GetDataForMemoryPool(memoryPool[0])
for pool in memoryPool[1]:
poolName = pool['Name']
poolData['CustomPools'][poolName] = {'DedicatedAllocations':[], 'Blocks':[]}
# Get dedicated allocations
for dedicatedAlloc in pool['DedicatedAllocations']:
allocData = {'Type': dedicatedAlloc['Type'],
'Size': int(dedicatedAlloc['Size']),
'Usage': int(dedicatedAlloc['Usage']),
'Name': dedicatedAlloc['Name'] if 'Name' in dedicatedAlloc else 'M'}
poolData['CustomPools'][poolName]['DedicatedAllocations'].append(allocData)
# Get allocations in block vectors
for block in pool['Blocks'].items():
ProcessBlock(poolData['CustomPools'][poolName], block)
if IsDataEmpty():
print("There is nothing to write. Please make sure you generated the stats string with detailed map enabled.")
exit(1)
RemoveEmptyType()
# add process name to indicate source file
AddProcessName(args.DumpFile)
# add thread names to indicate block names
AddTraceEvents(True)
# add the actual memory block size/offset/name
AddTraceEvents(False)
# setting
compactJson = False
if compactJson:
perfettoJson = json.dumps(perfettoDict)
else:
perfettoJson = json.dumps(perfettoDict, indent=0)
with open(args.output, "w") as outfile:
outfile.write(perfettoJson)
"""
Main data structure - variable `data` - is a dictionary. Key is string - memory type name. Value is dictionary of:
- Fixed key 'DedicatedAllocations'. Value is list of objects, each containing dictionary with:
- Fixed key 'Type'. Value is string.
- Fixed key 'Size'. Value is int.
- Fixed key 'Usage'. Value is int.
- Key 'Name' optional, Value is string
- Key'CustomData' optional
- Fixed key 'Blocks'. Value is list of objects, each containing dictionary with:
- Fixed key 'ID'. Value is int.
- Fixed key 'Size'. Value is int.
- Fixed key 'Suballocations'. Value is list of objects as above.
- Fixed key 'CustomPools'. Value is dictionary.
- Key is string with pool ID/name. Value is a dictionary with:
- Fixed key 'DedicatedAllocations'. Value is list of objects as above.
- Fixed key 'Blocks'. Value is a list of objects representing memory blocks as above.
"""