forked from desktop-app/cmake_helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_allocation_trace_reader.cpp
215 lines (203 loc) · 5.97 KB
/
linux_allocation_trace_reader.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
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include <iostream>
#include <iomanip>
#include <locale>
#include <unordered_map>
#include <cstring>
#include <ctime>
#include <vector>
constexpr auto kBufferSize = 1024 * 1024;
char Buffer[kBufferSize];
struct Fields {
std::uint32_t time = 0;
std::size_t mallocs = 0;
std::size_t reallocs = 0;
std::size_t frees = 0;
std::size_t unknownReallocs = 0;
std::size_t unknownFrees = 0;
};
Fields State;
std::unordered_map<std::uint64_t, std::size_t> Map;
std::vector<Fields> Snapshots;
void WriteTime(std::uint32_t time) {
std::time_t t = std::time_t(time);
const auto parsed = std::localtime(&t);
std::cout
<< std::setw(2) << std::setfill('0') << parsed->tm_hour
<< ":"
<< std::setw(2) << std::setfill('0') << parsed->tm_min
<< ":"
<< std::setw(2) << std::setfill('0') << parsed->tm_sec;
}
void PrintState() {
if (!State.time) {
return;
}
WriteTime(State.time);
auto full = std::uint64_t(0);
for (const auto &[address, amount] : Map) {
full += amount;
}
class NumPunct final : public std::numpunct<char> {
protected:
char do_thousands_sep() const override { return '\''; }
std::string do_grouping() const override { return "\03"; }
};
const auto &locale = std::cout.getloc();
std::cout.imbue(std::locale(std::locale::classic(), new NumPunct()));
std::cout
<< ": "
<< std::setw(13) << std::setfill(' ') << full
<< " (unknown: "
<< State.unknownFrees
<< ")"
<< std::endl;
std::cout.imbue(locale);
}
void Add(std::uint64_t address, std::uint64_t size) {
const auto i = Map.find(address);
if (i != end(Map)) {
std::cout
<< "WARNING: Repeated entry for "
<< address
<< " (size: "
<< size
<< ")."
<< std::endl;
return;
}
Map.emplace(address, size);
}
void ParseMalloc(const char *buffer) {
const auto size = *reinterpret_cast<const std::uint64_t*>(buffer);
const auto address = *reinterpret_cast<const std::uint64_t*>(buffer + 8);
Add(address, size);
++State.mallocs;
}
void ParseRealloc(const char *buffer) {
const auto old = *reinterpret_cast<const std::uint64_t*>(buffer);
const auto size = *reinterpret_cast<const std::uint64_t*>(buffer + 8);
const auto address = *reinterpret_cast<const std::uint64_t*>(buffer + 16);
const auto i = Map.find(old);
if (i == end(Map)) {
++State.unknownReallocs;
Add(address, size);
} else if (old != address) {
Map.erase(i);
Add(address, size);
++State.reallocs;
} else {
i->second = size;
++State.reallocs;
}
}
void ParseFree(const char *buffer) {
const auto address = *reinterpret_cast<const std::uint64_t*>(buffer);
const auto i = Map.find(address);
if (i == end(Map)) {
++State.unknownFrees;
} else {
Map.erase(i);
++State.frees;
}
}
long Parse(const char *buffer, const char *end) {
auto result = 0;
while (end > buffer) {
const auto command = buffer[0];
auto entry = 0;
switch (command) {
case 1: entry = 5 + 2 * sizeof(std::uint64_t); break;
case 2: entry = 5 + 3 * sizeof(std::uint64_t); break;
case 3: entry = 5 + sizeof(std::uint64_t); break;
default:
std::cout
<< "WARNING: Garbage in trace file, command: "
<< int(command)
<< "."
<< std::endl;
return -1;
}
if (end - buffer < entry) {
break;
}
const auto time = *reinterpret_cast<const std::uint32_t*>(++buffer);
buffer += 4;
if (time > State.time) {
PrintState();
State.time = time;
} else if (time < State.time) {
std::cout
<< "WARNING: Time "
<< time
<< " after "
<< State.time
<< "."
<< std::endl;
}
switch (command) {
case 1: ParseMalloc(buffer); break;
case 2: ParseRealloc(buffer); break;
case 3: ParseFree(buffer); break;
}
buffer += entry - 5;
result += entry;
}
return result;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cout
<< "Usage 'allocation_trace_reader [trace_file_path]'."
<< std::endl;
return -1;
}
const auto file = fopen(argv[1], "rb");
if (!file) {
std::cout
<< "ERROR: Could not open '"
<< argv[1]
<< "'."
<< std::endl;
return -1;
}
char *data = Buffer;
while (true) {
const auto read = fread(data, 1, Buffer + kBufferSize - data, file);
if (read == 0) {
if (data != Buffer) {
std::cout
<< "WARNING: Trace file end is corrupt, could not parse: "
<< std::size_t(data - Buffer)
<< std::endl;
}
break;
}
data += read;
const auto parsed = Parse(Buffer, data);
if (parsed < 0) {
break;
}
data -= parsed;
if (data - Buffer > 0) {
std::memmove(Buffer, Buffer + parsed, data - Buffer);
}
}
PrintState();
std::cout << "Mallocs: " << State.mallocs << "." << std::endl;
std::cout << "Reallocs: " << State.reallocs << "." << std::endl;
std::cout << "Frees: " << State.frees << "." << std::endl;
if (State.unknownReallocs) {
std::cout
<< "Unknown realloc() calls: "
<< State.unknownReallocs
<< "."
<< std::endl;
}
return 0;
}