-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
421 lines (350 loc) · 13.3 KB
/
main.cpp
File metadata and controls
421 lines (350 loc) · 13.3 KB
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
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <thread>
#include <cerrno>
#include <memory>
#include <vector>
#include <unordered_map>
#include <windows.h>
#include <fmt/fmt.hpp>
#define CHECK_FREAD(dst, file)\
do {\
int result = fread(dst, 1, sizeof(*dst), file);\
if (result != sizeof(*dst)) {\
fprintf(stderr, "Error reading field: " #dst\
": (Expected %d, Actual: %d) %s\n", sizeof(*dst), result, \
strerror(errno));\
return false;\
}\
} while (false)
#define CHECK_FREAD_CUSTOM(dst, file, size)\
do {\
if (fread(dst, 1, size, file) != size) return false;\
} while (false)
using namespace std;
struct FileVersion
{
uint16_t patch = 0;
uint8_t minor = 0;
uint8_t major = 0;
};
// Note: you can't memcpy this from binary b/c of padding,
// so the fields are out of order.
struct FileHeader
{
uint32_t signature = 0;
FileVersion version;
uint64_t profiledProcessId = 0;
int64_t cpuFrequencyRatio = 0;
uint64_t beginTime = 0;
uint64_t endTime = 0;
uint32_t numBlocks = 0;
uint32_t numDescriptors = 0; // In the binary format, this...
uint64_t blocksMemoryUsage = 0; // goes after this.
uint64_t descriptorsMemoryUsage = 0;
uint32_t descriptorSectionSize() const
{
return numDescriptors * 2 + descriptorsMemoryUsage;
}
};
// ARGB (Little Endian Order)
union Color
{
struct
{
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a;
};
uint32_t packed = 0;
};
struct Descriptor
{
uint32_t id = 0;
uint32_t line = 0;
Color color;
uint8_t type = 0;
uint8_t status = 0;
uint16_t nameLength = 0;
std::unique_ptr<char[]> name;
std::unique_ptr<char[]> fileName;
};
struct SizedDescriptor
{
uint16_t size = 0;
Descriptor descriptor;
};
struct ThreadInfo
{
uint64_t id;
std::unique_ptr<char[]> name;
uint32_t numContextSwitches;
uint32_t numBlocks;
uint16_t nameLength;
};
struct ContextSwitch
{
uint64_t beginTime = 0;
uint64_t endTime = 0;
uint64_t targetThreadId = 0;
std::unique_ptr<char[]> processInfo;
};
struct Block
{
uint64_t beginTime = 0;
uint64_t endTime = 0;
uint32_t id = 0;
std::unique_ptr<char[]> runtimeName;
};
struct SizedContextSwitch
{
uint16_t size = 0;
ContextSwitch contextSwitch;
};
struct SizedBlock
{
uint16_t size = 0;
Block block;
};
using SizedContextSwitchList = std::vector<SizedContextSwitch>;
using SizedBlockList = std::vector<SizedBlock>;
using SizedDescriptorList = std::vector<SizedDescriptor>;
struct ThreadData
{
ThreadInfo info;
SizedContextSwitchList switches;
SizedBlockList blocks;
};
using ThreadDataList = std::vector<ThreadData>;
struct ProfilerData
{
FileHeader header;
SizedDescriptorList sizedDescriptorList;
ThreadDataList threadDataList;
};
bool parse_args(int argc, char** argv, std::string& inFile1, std::string& inFile2)
{
(void)argc;
inFile1 = argv[1];
inFile2 = argv[2];
return true;
}
bool parse_header(FILE* file, ProfilerData& data)
{
FileHeader& header = data.header;
auto contiguousOffset = offsetof(FileHeader, numBlocks);
CHECK_FREAD_CUSTOM(&header, file, contiguousOffset);
CHECK_FREAD(&header.numBlocks, file);
CHECK_FREAD(&header.blocksMemoryUsage, file);
CHECK_FREAD(&header.numDescriptors, file);
CHECK_FREAD(&header.descriptorsMemoryUsage, file);
return true;
}
bool parse_descriptors(FILE* file, ProfilerData& data)
{
SizedDescriptorList& list = data.sizedDescriptorList;
list.resize(data.header.numDescriptors);
int numDescriptors = (int)data.header.numDescriptors;
for (int i = 0; i < numDescriptors; i++) {
constexpr size_t dynamicPartOffset = offsetof(Descriptor, name);
SizedDescriptor& cur = list[i];
Descriptor& desc = cur.descriptor;
// Read the contiguous parts, up until the name and file name fields.
CHECK_FREAD(&cur.size, file);
CHECK_FREAD_CUSTOM(&desc, file, dynamicPartOffset);
uint16_t nameLength = desc.nameLength;
uint16_t fileNameLength = cur.size - dynamicPartOffset - nameLength;
desc.name = std::unique_ptr<char[]>(new char[nameLength]);
desc.fileName = std::unique_ptr<char[]>(new char[fileNameLength]);
CHECK_FREAD_CUSTOM(desc.name.get(), file, desc.nameLength);
CHECK_FREAD_CUSTOM(desc.fileName.get(), file, fileNameLength);
}
return true;
}
bool parse_thread_data(FILE* file, ProfilerData& data)
{
// "THREAD EVENTS AND BLOCKS" (See doc.) sections continue until eof.
int cur = ftell(file);
if (fseek(file, 0, SEEK_END) != 0) {
fprintf(stderr, "Failed to seek: %s\n", strerror(errno));
return false;
}
int fileSize = ftell(file);
if (fseek(file, cur, SEEK_SET) != 0) {
fprintf(stderr, "Failed to seek: %s\n", strerror(errno));
return false;
}
int offset = cur;
for (int i = 0; offset < fileSize; i++, offset = ftell(file)) {
data.threadDataList.emplace_back();
ThreadData& threadData = data.threadDataList[i];
ThreadInfo& info = threadData.info;
SizedContextSwitchList& cSwitches = threadData.switches;
SizedBlockList& blocks = threadData.blocks;
CHECK_FREAD(&info.id, file);
CHECK_FREAD(&info.nameLength, file);
uint32_t nameLength = info.nameLength;
info.name = std::unique_ptr<char[]>(new char[nameLength]);
CHECK_FREAD_CUSTOM(info.name.get(), file, nameLength);
CHECK_FREAD(&info.numContextSwitches, file);
uint32_t numContextSwitches = info.numContextSwitches;
cSwitches.resize(numContextSwitches);
for (int i = 0; i < (int)numContextSwitches; i++) {
constexpr uint32_t contiguousOffset = offsetof(ContextSwitch, processInfo);
SizedContextSwitch& ss = cSwitches[i];
ContextSwitch& cur = ss.contextSwitch;
CHECK_FREAD(&ss.size, file);
CHECK_FREAD_CUSTOM(&cur, file, contiguousOffset);
uint32_t processInfoSize = ss.size - contiguousOffset;
cur.processInfo = std::unique_ptr<char[]>(new char[processInfoSize]);
}
CHECK_FREAD(&info.numBlocks, file);
uint32_t numBlocks = info.numBlocks;
blocks.resize(numBlocks);
for (int i = 0; i < (int)numBlocks; i++) {
constexpr uint32_t contiguousOffset = sizeof(uint64_t)*2 + sizeof(uint32_t);
SizedBlock& sb = blocks[i];
Block& cur = sb.block;
CHECK_FREAD(&sb.size, file);
CHECK_FREAD(&cur.beginTime, file);
CHECK_FREAD(&cur.endTime, file);
CHECK_FREAD(&cur.id, file);
if (sb.size == 0) sb.size = 21;
uint32_t runtimeNameSize = sb.size - contiguousOffset;
cur.runtimeName = std::unique_ptr<char[]>(new char[runtimeNameSize]);
CHECK_FREAD_CUSTOM(cur.runtimeName.get(), file, runtimeNameSize);
}
}
return true;
}
bool parse_prof_file(FILE* file, ProfilerData& data)
{
if (!parse_header(file, data)) return false;
if (!parse_descriptors(file, data)) return false;
if (!parse_thread_data(file, data)) return false;
return true;
}
void write_header(const FileHeader& header, FILE* file)
{
fprintf(file, "Header:\n");
const char* signature = (const char*)&header.signature;
fmt::ifprintf(file, 4, "Signature: %d(%c%c%c%c)\n", header.signature, signature[0], signature[1],
signature[2], signature[3]);
const FileVersion& version = header.version;
fmt::ifprintf(file, 4, "Version: %d.%d.%d\n", version.major, version.minor, version.patch);
fmt::ifprintf(file, 4, "Profiled Process ID: %llu\n", header.profiledProcessId);
fmt::ifprintf(file, 4, "CPU Frequency Ratio: %lld\n", header.cpuFrequencyRatio);
fmt::ifprintf(file, 4, "Begin Time: %llu\n", header.beginTime);
double duration = 100*((header.endTime - header.beginTime)/(double)header.cpuFrequencyRatio);
fmt::ifprintf(file, 4, "End Time: %llu (Total time: %f ms)\n", header.endTime, duration);
fmt::ifprintf(file, 4, "Num Blocks: %u\n", header.numBlocks);
fmt::ifprintf(file, 4, "Blocks Memory Usage: %llu\n", header.blocksMemoryUsage);
fmt::ifprintf(file, 4, "Num Descriptors: %u\n", header.numDescriptors);
fmt::ifprintf(file, 4, "Descriptor Memory Usage: %llu\n", header.descriptorsMemoryUsage);
}
void write_descriptors(const SizedDescriptorList& list, FILE* file)
{
fprintf(file, "Descriptors:\n");
for (const SizedDescriptor& sd : list) {
const Descriptor& d = sd.descriptor;
fmt::ifprintf(file, 4, "Descriptor for block: %s\n", d.name.get());
fmt::ifprintf(file, 4, "Size: %u\n", sd.size);
fmt::ifprintf(file, 4, "ID: %u\n", d.id);
fmt::ifprintf(file, 4, "Line: %u\n", d.line);
const Color& color = d.color;
fmt::ifprintf(file, 4, "Color(ARGB): Packed: %u, Unpacked: (%u, %u, %u, %u)\n",
color.packed, color.a, color.r, color.g, color.b);
fmt::ifprintf(file, 4, "Type: %u\n", d.type);
fmt::ifprintf(file, 4, "Status: %u\n", d.status);
fmt::ifprintf(file, 4, "Name Length: %u\n", d.nameLength);
fmt::ifprintf(file, 4, "Name: %s\n", d.name.get());
uint32_t fileNameLength = sd.size - offsetof(Descriptor, name) - d.nameLength;
fmt::ifprintf(file, 4, "File Name: %s (%u bytes)\n", d.fileName.get(), fileNameLength);
}
}
void write_thread_data(const ThreadDataList& list, FILE* file)
{
fprintf(file, "Per Thread Data:\n");
for (int i = 0; i < (int)list.size(); i++) {
const ThreadData& cur = list[i];
const ThreadInfo& info = cur.info;
fmt::ifprintf(file, 0, "Thread Info:\n");
fmt::ifprintf(file, 4, "ID: %llu\n", info.id);
fmt::ifprintf(file, 4, "Name: %s\n", info.name.get());
fmt::ifprintf(file, 4, "Name Length: %u\n", info.nameLength);
fmt::ifprintf(file, 4, "Num Context Switches: %u\n", info.numContextSwitches);
fmt::ifprintf(file, 4, "Num Blocks: %u\n", info.numBlocks);
fprintf(file, "\n");
fmt::Tree tree;
tree.fprintf_root(file, "Context Switches:\n");
const SizedContextSwitchList& switchList = cur.switches;
int64_t numSwitches = (int64_t)switchList.size();
for (int64_t i = 0; i < numSwitches; i++) {
const SizedContextSwitch& sw = switchList[i];
const ContextSwitch& cs = sw.contextSwitch;
tree.fprintf_last_if(i == numSwitches-1, file, 1, "CS %d:\n", i);
tree.fprintf_level(file, 2, "Process Info:\n", cs.processInfo.get());
tree.fprintf_level(file, 2, "Target Thread ID:\n", cs.targetThreadId);
tree.fprintf_level(file, 2, "Begin Time: %llu\n", cs.beginTime);
tree.fprintf_level(file, 2, "End Time: %llu\n", cs.endTime);
tree.fprintf_last(file, 2, "Size:\n", sw.size);
}
tree.fprintf_root(file, "Blocks:\n");
const SizedBlockList& blocks = cur.blocks;
int64_t numBlocks = (int64_t)blocks.size();
for (int64_t i = 0; i < (int64_t)numBlocks; i++) {
const SizedBlock& sb = blocks[i];
const Block& cur = sb.block;
tree.fprintf_last_if(i == numBlocks-1, file, 1, "Block %d:\n", i);
tree.fprintf_level(file, 2, "Size: %u\n", sb.size);
tree.fprintf_level(file, 2, "Name: %s\n", cur.runtimeName.get());
tree.fprintf_level(file, 2, "ID: %u\n", cur.id);
tree.fprintf_level(file, 2, "Begin Time: %llu\n", cur.beginTime);
tree.fprintf_last(file, 2, "End Time: %llu\n", cur.endTime);
}
fprintf(file, "\n");
}
}
void write_profiler_data(const ProfilerData& data, FILE* file)
{
write_header(data.header, file);
fprintf(file, "\n");
write_descriptors(data.sizedDescriptorList, file);
fprintf(file, "\n");
write_thread_data(data.threadDataList, file);
}
bool print_file(const std::string& fileName)
{
FILE* file = fopen(fileName.c_str(), "rb");
if (!file) {
printf("Failed to open \"%s\" for reading: %s\n", fileName.c_str(), strerror(errno));
return false;
}
ProfilerData data;
if (!parse_prof_file(file, data)) return false;
write_profiler_data(data, stdout);
fclose(file);
return true;
}
int main(int argc, char** argv)
{
std::string inFile1;
std::string inFile2;
if (!parse_args(argc, argv, inFile1, inFile2)) return EXIT_FAILURE;
fprintf(stdout, "File: %s\n", inFile1.c_str());
if (!print_file(inFile1)) {
fprintf(stderr, "Error Parsing \"%s\"\n", inFile1.c_str());
return EXIT_FAILURE;
}
printf("\n");
fprintf(stdout, "File: %s\n", inFile2.c_str());
if (!print_file(inFile2)) {
fprintf(stderr, "Error Parsing \"%s\"\n", inFile2.c_str());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}