3030using namespace std ;
3131using namespace tbox ;
3232
33- // ! 统计
33+ // ! 统计数据
3434struct Stat {
3535 uint64_t name_index = 0 ;
3636 uint64_t module_index = 0 ;
@@ -56,11 +56,14 @@ using RecordHandleFunc = std::function<void(uint64_t, uint64_t, uint64_t, uint64
5656void PrintUsage (const char *proc_name)
5757{
5858 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;
6264}
6365
66+ // ! 从Buffer中提取5个uint64_t的数值
6467bool PickRecord (util::Buffer &buffer, uint64_t &end_diff_us, uint64_t &duration_us,
6568 uint64_t &thread_index, uint64_t &name_index, uint64_t &module_index)
6669{
@@ -99,11 +102,12 @@ bool PickRecord(util::Buffer &buffer, uint64_t &end_diff_us, uint64_t &duration_
99102 return true ;
100103}
101104
105+ // ! 读取指定的记录文件
102106void ReadRecordFile (const std::string &filename, const RecordHandleFunc &func)
103107{
104108 std::ifstream ifs (filename, std::ifstream::binary);
105109 if (!ifs) {
106- std::cerr << " read '" << filename << " ' fail!" << std::endl;
110+ std::cerr << " Err: Read '" << filename << " ' fail!" << std::endl;
107111 return ;
108112 }
109113
@@ -132,17 +136,25 @@ void ReadRecordFile(const std::string &filename, const RecordHandleFunc &func)
132136 }
133137}
134138
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)
136141{
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+
137148 for (auto record_file : record_file_vec)
138149 ReadRecordFile (records_dir + ' /' + record_file, func);
139150}
140151
152+ // ! 导出统计数据到文件
141153void DumpStatToFile (const StringVec &name_vec, const StatVec &stat_vec, const std::string &stat_filename)
142154{
143155 std::ofstream ofs (stat_filename);
144156 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;
146158 return ;
147159 }
148160
@@ -167,24 +179,23 @@ void DumpStatToFile(const StringVec &name_vec, const StatVec &stat_vec, const st
167179
168180int main (int argc, char **argv)
169181{
170- if (argc < 2 ) {
182+ if (argc <= 1 ) {
171183 PrintUsage (argv[0 ]);
172184 return 0 ;
173185 }
174186
175187 std::string dir_path = argv[1 ];
176188 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;
178190 return 0 ;
179191 }
180192
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" ;
184195
185196 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;
188199 return 0 ;
189200 }
190201
@@ -195,25 +206,19 @@ int main(int argc, char **argv)
195206
196207 StringVec name_vec, module_vec, thread_vec;
197208 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;
199210 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;
201212 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;
210214
211215 std::vector<Stat> stat_vec (name_vec.size ());
216+ std::string records_dir = dir_path + " /records" ;
212217
213218 writer.writeHeader ();
214219
215220 // ! 第一次遍历记录文件
216- ReadAllRecordFiles (records_dir, record_file_name_vec,
221+ ReadAllRecordFiles (records_dir,
217222 [&] (uint64_t start_ts_us, uint64_t duration_us, uint64_t name_index, uint64_t module_index, uint64_t thread_index) {
218223 auto &name = name_vec.at (name_index);
219224 auto &module = module_vec.at (module_index);
@@ -244,7 +249,7 @@ int main(int argc, char **argv)
244249 }
245250
246251 // ! 第二次遍历记录文件,标出超出警告线的
247- ReadAllRecordFiles (records_dir, record_file_name_vec,
252+ ReadAllRecordFiles (records_dir,
248253 [&] (uint64_t start_ts_us, uint64_t duration_us, uint64_t name_index, uint64_t module_index, uint64_t ) {
249254 auto &stat = stat_vec.at (name_index);
250255 if (duration_us < stat.dur_warn_line_us )
@@ -268,8 +273,9 @@ int main(int argc, char **argv)
268273 writer.writeFooter ();
269274
270275 // ! 输出统计到 stat.txt
271- std::string stat_filename = dir_path + " /stat.txt" ;
272276 DumpStatToFile (name_vec, stat_vec, stat_filename);
277+
278+ std::cout << " Info: Success." << std::endl;
273279 return 0 ;
274280}
275281
0 commit comments