Skip to content

Commit d68271f

Browse files
committed
tidy(trace): 将to_json更名为analyzer,将进行了代码优化
1 parent 9294a11 commit d68271f

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

modules/trace/tools/to_json/Makefile renamed to modules/trace/tools/analyzer/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# of the source tree.
1919
#
2020

21-
PROJECT := trace/tools/to_json
22-
EXE_NAME := tools/trace/to_json
21+
PROJECT := trace/tools/analyzer
22+
EXE_NAME := tools/trace/analyzer
2323

2424
CPP_SRC_FILES := \
2525
main.cpp \

modules/trace/tools/to_json/main.cpp renamed to modules/trace/tools/analyzer/main.cpp

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
using namespace std;
3131
using namespace tbox;
3232

33-
//! 统计
33+
//! 统计数据
3434
struct 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
5656
void 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的数值
6467
bool 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+
//! 读取指定的记录文件
102106
void 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+
//! 导出统计数据到文件
141153
void 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

168180
int 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

Comments
 (0)