forked from larryhou/MemoryProfiler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
390 lines (370 loc) · 16.3 KB
/
Copy pathmain.cpp
File metadata and controls
390 lines (370 loc) · 16.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
//
// main.cpp
// UnityProfiler
//
// Created by larryhou on 2019/5/5.
// Copyright © 2019 larryhou. All rights reserved.
//
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include "utils.h"
#include "Crawler/record.h"
using std::cout;
using std::endl;
std::ifstream *stream;
void processRecord(const char *filepath)
{
RecordCrawler crawler;
crawler.load(filepath);
auto filename = basename(filepath);
char cmdpath[256];
mkdir("__commands", 0777);
memset(cmdpath, 0, sizeof(cmdpath));
sprintf(cmdpath, "__commands/%s.plog", filename);
delete [] filename;
std::ofstream plog;
plog.open(cmdpath, std::ofstream::app);
std::istream *stream = &std::cin;
while (true)
{
auto replaying = typeid(*stream) != typeid(std::cin);
auto recordable = true;
std::cout << "\e[93m/> ";
std::string input;
while (!getline(*stream, input))
{
if (replaying)
{
((ifstream *)stream)->close();
stream = &std::cin;
replaying = false;
}
stream->clear();
replaying ? usleep(500000) : usleep(100000);
}
if (replaying)
{
auto iter = input.begin();
while (iter != input.end())
{
cout << *iter++ << std::flush;
usleep(50000);
}
cout << endl;
}
cout << "\e[0m" << "\e[36m";
const char *command = input.c_str();
if (strbeg(command, "frame"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() == 1)
{
crawler.inspectFrame(0);
}
else
{
int32_t depth = 0;
if (options.size() >= 3)
{
depth = atoi(options[2]);
}
crawler.inspectFrame(atoi(options[1]), depth);
}
});
}
else if (strbeg(command, "next"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() == 1)
{
crawler.next();
}
else
{
crawler.next(atoi(options[1]));
}
});
}
else if (strbeg(command, "prev"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() == 1)
{
crawler.prev();
}
else
{
crawler.prev(atoi(options[1]));
}
});
}
else if (strbeg(command, "info"))
{
crawler.summarize(false);
}
else if (strbeg(command, "alloc"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() == 1)
{
crawler.findFramesWithAlloc();
}
else
{
if (options.size() >= 2)
{
auto count = -1;
auto offset = atoi(options[1]);
if (options.size() >= 3)
{
count = atoi(options[2]);
}
crawler.findFramesWithAlloc(offset, count);
}
}
});
}
else if (strbeg(command, "func"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() == 1)
{
crawler.statByFunction();
}
else
{
crawler.statByFunction(atoi(options[1]));
}
});
}
else if (strbeg(command, "find"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() > 1)
{
crawler.findFramesWithFunction(atoi(options[1]));
}
});
}
else if (strbeg(command, "list"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() == 1)
{
crawler.list();
}
else
{
int32_t count = 0;
int32_t offset = atoi(options[1]);
int32_t sorting = 0;
if (options.size() >= 3)
{
count = atoi(options[2]);
if (options.size() >= 4)
{
auto sign = options[3];
if (strcmp(sign, "+"))
{
sorting = 1;
}
else if( strcmp(sign, "-"))
{
sorting = -1;
}
}
}
crawler.list(offset, count, sorting);
}
});
}
else if (strbeg(command, "fps"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() == 1)
{
crawler.summarize(true);
}
else
{
float fps = atof(options[1]);
if (options.size() >= 3)
{
auto sign = options[2];
if (strbeg(sign, "="))
{
crawler.findFramesWithFPS(fps, [](auto a, auto b) { return a == b; });
}
else if (strbeg(sign, "<"))
{
crawler.findFramesWithFPS(fps, [](auto a, auto b) { return a < b; });
}
else
{
crawler.findFramesWithFPS(fps, [](auto a, auto b) { return a > b; });
}
}
else
{
crawler.findFramesWithFPS(fps, [](auto a, auto b) { return a < b; });
}
}
});
}
else if (strbeg(command, "replay"))
{
recordable = false;
std::vector<string> commands;
readCommandOptions(command, [&](std::vector<const char *> &options)
{
auto ifs = new ifstream;
if (options.size() == 1)
{
ifs->open(cmdpath);
}
else
{
ifs->open(options[1]);
}
stream = ifs;
});
}
else if (strbeg(command, "meta"))
{
crawler.dumpMetadatas();
}
else if (strbeg(command, "stat"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() >= 2)
{
int32_t property = 0;
int32_t area = atoi(options[1]);
if (options.size() >= 3)
{
property = atoi(options[2]);
}
crawler.statValues((ProfilerArea)area, property);
}
});
}
else if (strbeg(command, "seek"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() >= 4)
{
int32_t property = 0;
int32_t area = atoi(options[1]);
property = atoi(options[2]);
float threshold = atof(options[3]);
if (options.size() >= 5)
{
auto sign = options[4];
if (strbeg(sign, ">"))
{
crawler.findFramesMatchValue((ProfilerArea)area, property, threshold, [](float a, float b) {return a > b;});
}
else if (strbeg(sign, "<"))
{
crawler.findFramesMatchValue((ProfilerArea)area, property, threshold, [](float a, float b) {return a < b;});
}
else
{
crawler.findFramesMatchValue((ProfilerArea)area, property, threshold, [](float a, float b) {return a == b;});
}
}
else
{
crawler.findFramesMatchValue((ProfilerArea)area, property, threshold, [](float a, float b) {return a > b;});
}
}
else
{
printf("seek [PROFILER_AREA] [PROPERTY] [VALUE]\n");
}
});
}
else if (strbeg(command, "lock"))
{
readCommandOptions(command, [&](std::vector<const char *> &options)
{
if (options.size() == 1)
{
crawler.lock();
}
else
{
int32_t frameCount = -1;
int32_t frameIndex = atoi(options[1]);
if (options.size() >= 3)
{
frameCount = atoi(options[2]);
}
crawler.lock(frameIndex, frameCount);
}
});
}
else if (strbeg(command, "quit"))
{
recordable = false;
cout << "\e[0m";
exit(0);
}
else if (strbeg(command, "help"))
{
recordable = false;
const int __indent = 5;
help("alloc", "[FRAME_OFFSET] [FRAME_COUNT]", "搜索申请动态内存的帧", __indent);
help("frame","[FRAME_INDEX]", "查看帧时间消耗详情", __indent);
help("func", NULL, "按照方法名统计时间消耗", __indent);
help("find", "[FUNCTION_NAME_REF]*", "按照方法名索引查找调用帧", __indent);
help("list", "[FRAME_OFFSET] [±FRAME_COUNT] [+|-]", "列举帧简报 支持排序(+按fps升序 -按fps降序)输出 默认不排序", __indent);
help("next", "[STEP]", "查看后STEP[=1]帧时间消耗详情", __indent);
help("prev", "[STEP]", "查看前STEP[=1]帧时间消耗详情", __indent);
help("meta", NULL, "查看性能指标参数", __indent);
help("lock", "[FRAME_INDEX] [FRAME_COUNT]", "锁定帧范围", __indent);
help("stat", "[PROFILER_AREA] [PROPERTY]", "统计性能指标", __indent);
help("seek", "[PROFILER_AREA] [PROPERTY] [VALUE] [>|=|<]", "搜索性能指标满足条件(>大于VALUE[默认] =等于VALUE <小于VALUE)的帧", __indent);
help("info", NULL, "性能摘要", __indent);
help("fps", "[FPS] [>|=|<]", "搜索满足条件(>大于FPS =等于FPS <小于FPS[默认])的帧", __indent);
help("help", NULL, "帮助", __indent);
help("quit", NULL, "退出", __indent);
cout << std::flush;
}
else
{
recordable = false;
printf("not supported command [%s]\n", command);
}
if (!replaying && recordable)
{
plog.clear();
plog << input.c_str() << endl;
}
}
}
int main(int argc, const char * argv[])
{
cout << "argc=" << argc << endl;
for (auto i = 0; i < argc; i++)
{
cout << "argv[" << i << "]=" << argv[i] << endl;
}
if (argc > 1)
{
processRecord(argv[1]);
}
return 0;
}