30
30
using namespace std ;
31
31
using namespace tbox ;
32
32
33
- // ! 统计
33
+ // ! 统计数据
34
34
struct Stat {
35
35
uint64_t name_index = 0 ;
36
36
uint64_t module_index = 0 ;
@@ -56,11 +56,14 @@ using RecordHandleFunc = std::function<void(uint64_t, uint64_t, uint64_t, uint64
56
56
void PrintUsage (const char *proc_name)
57
57
{
58
58
std::cout
59
- << " Usage: " << proc_name << " <dir_path> [output_filename]" << std::endl
60
- << " Exp : " << proc_name << " /some/where/my_proc.20240531_032237.114" << std::endl
61
- << " " << proc_name << " /some/where/my_proc.20240531_032237.114 output.json" << std::endl;
59
+ << " This is cpp-tbox trace analyze tool." << std::endl
60
+ << " It reads record files from the specified directory, and generates view.json and stat.txt in this directory." << std::endl
61
+ << std::endl
62
+ << " Usage: " << proc_name << " <dir_path>" << std::endl
63
+ << " Exp : " << proc_name << " /some/where/my_proc.20240531_032237.114" << std::endl;
62
64
}
63
65
66
+ // ! 从Buffer中提取5个uint64_t的数值
64
67
bool PickRecord (util::Buffer &buffer, uint64_t &end_diff_us, uint64_t &duration_us,
65
68
uint64_t &thread_index, uint64_t &name_index, uint64_t &module_index)
66
69
{
@@ -99,11 +102,12 @@ bool PickRecord(util::Buffer &buffer, uint64_t &end_diff_us, uint64_t &duration_
99
102
return true ;
100
103
}
101
104
105
+ // ! 读取指定的记录文件
102
106
void ReadRecordFile (const std::string &filename, const RecordHandleFunc &func)
103
107
{
104
108
std::ifstream ifs (filename, std::ifstream::binary);
105
109
if (!ifs) {
106
- std::cerr << " read '" << filename << " ' fail!" << std::endl;
110
+ std::cerr << " Err: Read '" << filename << " ' fail!" << std::endl;
107
111
return ;
108
112
}
109
113
@@ -132,17 +136,25 @@ void ReadRecordFile(const std::string &filename, const RecordHandleFunc &func)
132
136
}
133
137
}
134
138
135
- void ReadAllRecordFiles (const std::string &records_dir, const StringVec &record_file_vec, const RecordHandleFunc &func)
139
+ // ! 读取目录下所有的记录文件
140
+ void ReadAllRecordFiles (const std::string &records_dir, const RecordHandleFunc &func)
136
141
{
142
+ StringVec record_file_vec;
143
+ if (!util::fs::ListDirectory (records_dir, record_file_vec)) {
144
+ std::cerr << " Err: List '" << records_dir << " ' fail!" << std::endl;
145
+ return ;
146
+ }
147
+
137
148
for (auto record_file : record_file_vec)
138
149
ReadRecordFile (records_dir + ' /' + record_file, func);
139
150
}
140
151
152
+ // ! 导出统计数据到文件
141
153
void DumpStatToFile (const StringVec &name_vec, const StatVec &stat_vec, const std::string &stat_filename)
142
154
{
143
155
std::ofstream ofs (stat_filename);
144
156
if (!ofs) {
145
- std::cout << " Error: open stat file '" << stat_filename << " ' fail!" << std::endl;
157
+ std::cerr << " Err: Create stat file '" << stat_filename << " ' fail!" << std::endl;
146
158
return ;
147
159
}
148
160
@@ -167,24 +179,23 @@ void DumpStatToFile(const StringVec &name_vec, const StatVec &stat_vec, const st
167
179
168
180
int main (int argc, char **argv)
169
181
{
170
- if (argc < 2 ) {
182
+ if (argc <= 1 ) {
171
183
PrintUsage (argv[0 ]);
172
184
return 0 ;
173
185
}
174
186
175
187
std::string dir_path = argv[1 ];
176
188
if (!util::fs::IsDirectoryExist (dir_path)) {
177
- std::cout << " Error : dir_path '" << dir_path << " ' not exist!" << std::endl;
189
+ std::cerr << " Err : dir_path '" << dir_path << " ' not exist!" << std::endl;
178
190
return 0 ;
179
191
}
180
192
181
- std::string output_filename = dir_path + " /output.json" ;
182
- if (argc > 2 )
183
- output_filename = argv[2 ];
193
+ std::string view_filename = dir_path + " /view.json" ;
194
+ std::string stat_filename = dir_path + " /stat.txt" ;
184
195
185
196
trace::Writer writer;
186
- if (!writer.open (output_filename )) {
187
- std::cout << " Error: output_filename '" << output_filename << " ' can't be create !" << std::endl;
197
+ if (!writer.open (view_filename )) {
198
+ std::cerr << " Err: Create '" << view_filename << " ' fail !" << std::endl;
188
199
return 0 ;
189
200
}
190
201
@@ -195,25 +206,19 @@ int main(int argc, char **argv)
195
206
196
207
StringVec name_vec, module_vec, thread_vec;
197
208
if (!util::fs::ReadAllLinesFromTextFile (names_filename, name_vec))
198
- std::cerr << " Warn: load names.txt fail!" << std::endl;
209
+ std::cerr << " Warn: Load names.txt fail!" << std::endl;
199
210
if (!util::fs::ReadAllLinesFromTextFile (modules_filename, module_vec))
200
- std::cerr << " Warn: load modules.txt fail!" << std::endl;
211
+ std::cerr << " Warn: Load modules.txt fail!" << std::endl;
201
212
if (!util::fs::ReadAllLinesFromTextFile (threads_filename, thread_vec))
202
- std::cerr << " Warn: load threads.txt fail!" << std::endl;
203
-
204
- std::string records_dir = dir_path + " /records" ;
205
- StringVec record_file_name_vec;
206
- if (!util::fs::ListDirectory (records_dir, record_file_name_vec)) {
207
- std::cerr << " Err: list '" << records_dir << " ' fail!" << std::endl;
208
- return 0 ;
209
- }
213
+ std::cerr << " Warn: Load threads.txt fail!" << std::endl;
210
214
211
215
std::vector<Stat> stat_vec (name_vec.size ());
216
+ std::string records_dir = dir_path + " /records" ;
212
217
213
218
writer.writeHeader ();
214
219
215
220
// ! 第一次遍历记录文件
216
- ReadAllRecordFiles (records_dir, record_file_name_vec,
221
+ ReadAllRecordFiles (records_dir,
217
222
[&] (uint64_t start_ts_us, uint64_t duration_us, uint64_t name_index, uint64_t module_index, uint64_t thread_index) {
218
223
auto &name = name_vec.at (name_index);
219
224
auto &module = module_vec.at (module_index);
@@ -244,7 +249,7 @@ int main(int argc, char **argv)
244
249
}
245
250
246
251
// ! 第二次遍历记录文件,标出超出警告线的
247
- ReadAllRecordFiles (records_dir, record_file_name_vec,
252
+ ReadAllRecordFiles (records_dir,
248
253
[&] (uint64_t start_ts_us, uint64_t duration_us, uint64_t name_index, uint64_t module_index, uint64_t ) {
249
254
auto &stat = stat_vec.at (name_index);
250
255
if (duration_us < stat.dur_warn_line_us )
@@ -268,8 +273,9 @@ int main(int argc, char **argv)
268
273
writer.writeFooter ();
269
274
270
275
// ! 输出统计到 stat.txt
271
- std::string stat_filename = dir_path + " /stat.txt" ;
272
276
DumpStatToFile (name_vec, stat_vec, stat_filename);
277
+
278
+ std::cout << " Info: Success." << std::endl;
273
279
return 0 ;
274
280
}
275
281
0 commit comments