forked from KhronosGroup/VulkanSC-pcutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcinfo.cpp
425 lines (379 loc) · 15.3 KB
/
pcinfo.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
/* Copyright (c) 2021, NVIDIA CORPORATION
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Daniel Koch <[email protected]>
*/
#include <cstddef>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <limits>
#include <cctype>
#include <string>
#include "pcreader.hpp"
#include <vulkan/vulkan_sc_core.hpp>
using namespace std;
typedef enum DetailMode
{
DETAIL_MODE_NONE = 0,
DETAIL_MODE_BASIC,
DETAIL_MODE_FIRST_HEADER,
DETAIL_MODE_ALL_HEADERS,
DETAIL_MODE_ALL,
} DetailMode;
std::ostream & operator<< (std::ostream &out, uint8_t i)
{
out << static_cast<unsigned int>(i);
return out;
}
std::ostream & operator<< (std::ostream &out, uint8_t const uuid[VK_UUID_SIZE])
{
out << "0x" << std::uppercase << std::hex;
for (uint32_t i=0; i<VK_UUID_SIZE; ++i)
{
out << std::setfill('0') << std::setw(2) << uuid[i];
}
out << std::dec;
return out;
}
std::ostream & operator<< (std::ostream &out, VkPipelineCacheHeaderVersionOne const &s)
{
out << "{" << std::endl;
out << s.headerSize << std::endl;
out << s.headerVersion << std::endl;
out << std::hex;
out << "0x" << s.vendorID << std::endl;
out << "0x" << s.deviceID << std::endl;
out << std::dec;
out << s.pipelineCacheUUID << std::endl;
out << "}";
return out;
}
std::ostream & operator<< (std::ostream &out, VkPipelineCacheHeaderVersionSafetyCriticalOne const &s)
{
out << "{" << std::endl;
out << s.headerVersionOne << std::endl;
out << s.validationVersion << std::endl;
out << s.implementationData << std::endl;
out << s.pipelineIndexCount << std::endl;
out << s.pipelineIndexStride << std::endl;
out << s.pipelineIndexOffset << std::endl;
out << "}";
return out;
}
std::ostream & operator<< (std::ostream &out, VkPipelineCacheSafetyCriticalIndexEntry const &s)
{
out << "{" << std::endl;
out << "identifier " << s.pipelineIdentifier << std::endl;
out << "pipelineMemorySize " << s.pipelineMemorySize << std::endl;
out << "jsonSize " << s.jsonSize << std::endl;
out << "jsonOffset " << s.jsonOffset << std::endl;
out << "stageIndexCount " << s.stageIndexCount << std::endl;
out << "stageIndexStride " << s.stageIndexStride << std::endl;
out << "stageIndexOffset " << s.stageIndexOffset << std::endl;
out << "}";
return out;
}
std::ostream & operator<< (std::ostream &out, VkPipelineCacheStageValidationIndexEntry const &s)
{
out << "{" << std::endl;
out << s.codeSize << std::endl;
out << s.codeOffset << std::endl;
out << "}";
return out;
}
struct PipelinePool {
PipelinePool(uint64_t size) : poolSize(size) {};
uint64_t poolSize;
uint64_t poolMin{ std::numeric_limits<uint64_t>::max() };
uint64_t poolMax{};
uint64_t poolTotal{};
std::vector<uint32_t> poolEntries{};
};
std::ostream & operator<< (std::ostream &out, PipelinePool const &pool)
{
out << "pool size: " << pool.poolSize << ", pool entries: " << pool.poolEntries.size() << std::endl;
return out;
}
// <pools> must be sorted in increasing order by poolSize
bool bucketPipelines(VKSCPipelineCacheHeaderReader &pcr, std::vector<PipelinePool> &pools)
{
bool allAdded = true;
// iterate through the pipelines and add to the smallest pool that they fit
for (uint32_t i=0; i < pcr.getPipelineIndexCount(); i++)
{
VkPipelineCacheSafetyCriticalIndexEntry const *pie = pcr.getPipelineIndexEntry(i);
assert(pie != nullptr);
bool addedToPool = false;
for( PipelinePool &pool : pools )
{
if (pie->pipelineMemorySize <= pool.poolSize)
{
//cout << "adding index " << i << " to pool " << pool.poolSize << endl;
pool.poolEntries.push_back(i);
pool.poolMin = std::min(pool.poolMin, pie->pipelineMemorySize);
pool.poolMax = std::max(pool.poolMax, pie->pipelineMemorySize);
pool.poolTotal += pie->pipelineMemorySize;
addedToPool = true;
break;
}
}
if (!addedToPool)
{
// pipeline didn't fit in any pool
cerr << "WARNING: index " << i
<< " id: " << pie->pipelineIdentifier
<< " pipelineMemorySize: " << pie->pipelineMemorySize
<< " did not fit in any pool!" << std::endl;
allAdded = false;
}
}
return allAdded;
}
bool printCacheInfo(VKSCPipelineCacheHeaderReader &pcr, DetailMode details)
{
VkPipelineCacheHeaderVersionOne const * hv1 = pcr.getHeaderVersionOne();
uint64_t minPoolSize = std::numeric_limits<uint64_t>::max();
uint64_t maxPoolSize = 0;
if (details >= DETAIL_MODE_FIRST_HEADER)
{
std::cout << std::endl
<< "headerSize: " << hv1->headerSize << std::endl
<< "headerVersion: " << hv1->headerVersion << std::endl
<< std::setbase(16)
<< "vendorID: 0x" << hv1->vendorID << std::endl
<< "deviceID: 0x" << hv1->deviceID << std::endl
<< "pipelineCacheUUID: " << hv1->pipelineCacheUUID << std::endl
<< std::setbase(10)
<< "validationVersion: " << pcr.getValidationVersion() << std::endl
<< "implementationData: " << pcr.getImplementationData() << std::endl
<< "pipelineIndexCount: " << pcr.getPipelineIndexCount() << std::endl
<< "pipelineIndexStride: " << pcr.getPipelineIndexStride() << std::endl
<< "pipelineIndexOffset: " << pcr.getPipelineIndexOffset() << std::endl
<< std::endl;
}
// iterate over each pipeline and print the UUID
for (uint32_t i=0; i < pcr.getPipelineIndexCount(); i++)
{
VkPipelineCacheSafetyCriticalIndexEntry const *pie = pcr.getPipelineIndexEntry(i);
if (nullptr != pie)
{
if (details >= DETAIL_MODE_ALL_HEADERS)
{
std::cout << "pipeline " << i << ":" << std::endl
<< " pipelineIdentifier: " << pie->pipelineIdentifier << std::endl
<< " pipelineMemorySize: " << pie->pipelineMemorySize << std::endl
<< " jsonSize: " << pie->jsonSize << std::endl
<< " jsonOffset: " << pie->jsonOffset << std::endl
<< " stageIndexCount: " << pie->stageIndexCount << std::endl
<< " stageIndexStride: " << pie->stageIndexStride << std::endl
<< " stageIndexOffset: " << pie->stageIndexOffset << std::endl;
for (uint32_t j=0; j < pie->stageIndexCount; j++)
{
VkPipelineCacheStageValidationIndexEntry const *vie = pcr.getStageIndexEntry(*pie, j);
if (nullptr != vie)
{
std::cout << " stage " << j << ":" << std::endl
<< " codeSize: " << vie->codeSize << std::endl
<< " codeOffset: " << vie->codeOffset << std::endl;
if (details >= DETAIL_MODE_ALL)
{
uint32_t const *spirv = reinterpret_cast<uint32_t const *>(pcr.getSPIRV(*vie));
std::cout << " spirv: ";
for (uint32_t k=0; k < vie->codeSize / 4; k++)
{
std::cout << "0x" << std::setw(8) << std::setbase(16) << spirv[k] << ",";
if ((k % 8) == 7)
{
std::cout << std::endl << " ";
}
}
std::cout << std::setbase(10) << std::endl;
}
}
}
if (details >= DETAIL_MODE_ALL)
{
std::string const json = std::string(reinterpret_cast<char const *>(pcr.getJson(*pie)),
static_cast<size_t>(pie->jsonSize));
std::cout << " json:" << std::endl << json << std::endl;
}
std::cout << std::endl;
}
else if (details == DETAIL_MODE_BASIC)
{
std::cout << "index: " << std::setw(3) << i
<< " id: " << pie->pipelineIdentifier
<< " pipelineMemorySize: " << pie->pipelineMemorySize << std::endl;
}
minPoolSize = std::min(minPoolSize, pie->pipelineMemorySize);
maxPoolSize = std::max(maxPoolSize, pie->pipelineMemorySize);
//std::cout << "pie " << i << ": " << *pie << std::endl;
} else {
std::cout << "pie " << i << ": not found" << std::endl;
std::cout << "malformed pipeline cache!" << std::endl;
exit(EXIT_FAILURE);
}
}
cout << "found " << pcr.getPipelineIndexCount() << " pipelines, sizes ["
<< minPoolSize << ", " << maxPoolSize << "]" << endl;
return true;
}
void printUsageAndExit(char *executable, int exitCode)
{
std::cerr << "usage: " << executable << " [-h|--help] [-l|-f|-d|-a] [-pool <poolsize>] <pipeline_cache_file>"
<< std::endl << std::endl;
std::cerr << " -h | --help: print this usage message" << std::endl << std::endl;
std::cerr << " -l: list basic pipeline info (index, identifier, poolSize)" << std::endl << std::endl;
std::cerr << " -f: list first header (device info, pipeline index)" << std::endl << std::endl;
std::cerr << " -d: list detailed pipeline info (-l, plus all header fields)" << std::endl << std::endl;
std::cerr << " -a: list all pipeline info (-d, plus JSON and SPIR-V)" << std::endl << std::endl;
std::cerr << " -pool <poolsize>: add a bucket of size <poolsize>" << endl << endl;
std::cerr << " <pipeline_cache_file>: the pipeline cache file to parse (generated by PCC tool)" << std::endl << std::endl;
exit(exitCode);
}
int main(int argc, char **argv)
{
ifstream file_cache;
char *cache_filename{nullptr};
int const lastArg{argc};
DetailMode listMode{DETAIL_MODE_NONE};
std::vector<PipelinePool> pools{};
if (argc < 2)
{
printUsageAndExit(argv[0], EXIT_FAILURE);
}
// handle any optional arguments
for (int i = 1; i < lastArg; i++)
{
if (strcmp(argv[i], "-l") == 0)
{
listMode = DETAIL_MODE_BASIC;
}
else if (strcmp(argv[i], "-f") == 0)
{
listMode = DETAIL_MODE_FIRST_HEADER;
}
else if (strcmp(argv[i], "-d") == 0)
{
listMode = DETAIL_MODE_ALL_HEADERS;
}
else if (strcmp(argv[i], "-a") == 0)
{
listMode = DETAIL_MODE_ALL;
}
else if (strcmp(argv[i], "-pool") == 0)
{
if (++i >= lastArg)
{
cout << "missing parameter <poolsize>" << endl << endl;
printUsageAndExit(argv[0], EXIT_FAILURE);
}
std::string sizeString(argv[i]);
bool isNumber = true;
for (auto c : sizeString)
{
if (!std::isdigit(c)) isNumber = false;
}
if (!isNumber)
{
cout << "parameter <poolsize> must be numeric, found " << sizeString << endl << endl;
printUsageAndExit(argv[0], EXIT_FAILURE);
}
uint64_t size = stoul(sizeString);
cout << "adding pool of size " << size << endl;
PipelinePool poolToAdd(size);
pools.push_back(poolToAdd);
}
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
{
printUsageAndExit(argv[0], EXIT_SUCCESS);
}
else if (argv[i][0] != '-')
{
cache_filename = argv[i];
}
else
{
cerr << "unrecognized argument: " << argv[i] << endl << endl;
printUsageAndExit(argv[0], EXIT_FAILURE);
}
}
if (cache_filename == nullptr)
{
cerr << "missing parameter <pipeline_cache_file>" << endl << endl;
printUsageAndExit(argv[0], EXIT_FAILURE);
}
std::cout << "parsing file: " << cache_filename << std::endl;
file_cache.open(cache_filename, ios::in|ios::binary|ios::ate);
if (!file_cache.is_open())
{
std::cerr << "error: failed to open " << cache_filename << std::endl;
return EXIT_FAILURE;
}
streampos size = file_cache.tellg();
std::vector<uint8_t> mem_cache(static_cast<size_t>(size));
file_cache.seekg(0, ios::beg);
file_cache.read(reinterpret_cast<char *>(mem_cache.data()), static_cast<streamsize>(mem_cache.size()));
file_cache.close();
std::cout << "read " << size << " bytes from file" << std::endl;
VKSCPipelineCacheHeaderReader pcr(mem_cache.size(), mem_cache.data());
if (!pcr.isValid())
{
std::cerr << "Requested file is not a valid VK_PIPELINE_CACHE_HEADER_VERSION_SAFETY_CRITICAL_ONE "
"cache. Aborting!" << std::endl;
return EXIT_FAILURE;
}
if (listMode >= DETAIL_MODE_BASIC)
{
printCacheInfo(pcr, listMode);
}
if (pools.size() > 0)
{
// sort the pools
std::sort(pools.begin(), pools.end(),
[](PipelinePool &a, PipelinePool &b)
{
return (a.poolSize < b.poolSize);
}
);
cout << "requested pools" << endl;
for (auto pool : pools)
{
cout << " pool size: " << pool.poolSize << endl;
}
bucketPipelines(pcr, pools);
// print the pools, basic stats, and pipelines in them
for (auto pool : pools)
{
cout << "======================================" << endl;
cout << pool;
cout << "pool min size: " << pool.poolMin << endl;
cout << "pool max size: " << pool.poolMax << endl;
cout << "pool average size: " << (pool.poolTotal / pool.poolEntries.size()) << endl;
cout << "pool allocated space: " << (pool.poolSize * pool.poolEntries.size()) << endl;
cout << "pool wasted space: " << (pool.poolSize * pool.poolEntries.size() - pool.poolTotal) << endl;
cout << "pool entry identifiers (hex):" << endl;
for (auto id : pool.poolEntries)
{
VkPipelineCacheSafetyCriticalIndexEntry const *pie = pcr.getPipelineIndexEntry(id);
cout << " " << pie->pipelineIdentifier << endl;
}
}
}
return EXIT_SUCCESS;
}