@@ -31,6 +31,9 @@ using namespace tbox;
31
31
32
32
using StringVec = std::vector<std::string>;
33
33
34
+ // ! start_time_us, duration_us, name_index, module_index, thread_index
35
+ using RecordHandleFunc = std::function<void (uint64_t , uint64_t , uint64_t , uint64_t , uint64_t )>;
36
+
34
37
void PrintUsage (const char *proc_name)
35
38
{
36
39
std::cout
@@ -77,7 +80,7 @@ bool PickRecord(util::Buffer &buffer, uint64_t &end_diff_us, uint64_t &duration_
77
80
return true ;
78
81
}
79
82
80
- void ParseRecordFile (const std::string &filename, const StringVec &names, const StringVec &modules, const StringVec &threads, trace::Writer &writer )
83
+ void ReadRecordFile (const std::string &filename, const RecordHandleFunc &func )
81
84
{
82
85
std::ifstream ifs (filename, std::ifstream::binary);
83
86
if (!ifs) {
@@ -89,38 +92,33 @@ void ParseRecordFile(const std::string &filename, const StringVec &names, const
89
92
util::Buffer buffer;
90
93
91
94
while (true ) {
92
- char tmp[1024 ];
93
- auto rsize = ifs.readsome (tmp, sizeof (tmp));
94
- if (rsize == 0 )
95
- break ;
96
-
97
- buffer.append (tmp, rsize);
98
-
99
- while (buffer.readableSize () >= 4 ) {
100
- uint64_t end_diff_us, duration_us, thread_index, name_index, module_index;
101
- if (!PickRecord (buffer, end_diff_us, duration_us, thread_index, name_index, module_index))
102
- break ;
103
-
104
- uint64_t end_ts_us = last_end_ts_us + end_diff_us;
105
- uint64_t start_ts_us = end_ts_us - duration_us;
106
- last_end_ts_us = end_ts_us;
95
+ char tmp[1024 ];
96
+ auto rsize = ifs.readsome (tmp, sizeof (tmp));
97
+ if (rsize == 0 )
98
+ break ;
107
99
108
- std::string name = " unknown-name " , thread = " unknown-thread " , module = " unknown-module " ;
100
+ buffer. append (tmp, rsize) ;
109
101
110
- if (name_index < names.size ())
111
- name = names[name_index];
102
+ while (buffer.readableSize () >= 4 ) {
103
+ uint64_t end_diff_us, duration_us, thread_index, name_index, module_index;
104
+ if (!PickRecord (buffer, end_diff_us, duration_us, thread_index, name_index, module_index))
105
+ break ;
112
106
113
- if (thread_index < threads.size ())
114
- thread = threads[thread_index];
107
+ uint64_t end_ts_us = last_end_ts_us + end_diff_us;
108
+ uint64_t start_ts_us = end_ts_us - duration_us;
109
+ last_end_ts_us = end_ts_us;
115
110
116
- if (module_index < modules.size ())
117
- module = modules[module_index];
118
-
119
- writer.writeRecorder (name, module , thread, start_ts_us, duration_us);
120
- }
111
+ func (start_ts_us, duration_us, name_index, module_index, thread_index);
112
+ }
121
113
}
122
114
}
123
115
116
+ void ReadAllRecordFiles (const std::string &records_dir, const StringVec &record_file_vec, const RecordHandleFunc &func)
117
+ {
118
+ for (auto record_file : record_file_vec)
119
+ ReadRecordFile (records_dir + ' /' + record_file, func);
120
+ }
121
+
124
122
int main (int argc, char **argv)
125
123
{
126
124
if (argc < 2 ) {
@@ -165,13 +163,22 @@ int main(int argc, char **argv)
165
163
166
164
writer.writeHeader ();
167
165
168
- for (auto record_file_name : record_file_name_vec) {
169
- ParseRecordFile (
170
- records_dir + ' /' + record_file_name,
171
- name_vec, module_vec, thread_vec,
172
- writer
173
- );
174
- }
166
+ ReadAllRecordFiles (records_dir, record_file_name_vec,
167
+ [&] (uint64_t start_ts_us, uint64_t duration_us, uint64_t name_index, uint64_t module_index, uint64_t thread_index) {
168
+ std::string name = " unknown-name" , thread = " unknown-thread" , module = " unknown-module" ;
169
+
170
+ if (name_index < name_vec.size ())
171
+ name = name_vec[name_index];
172
+
173
+ if (thread_index < thread_vec.size ())
174
+ thread = thread_vec[thread_index];
175
+
176
+ if (module_index < module_vec.size ())
177
+ module = module_vec[module_index];
178
+
179
+ writer.writeRecorder (name, module , thread, start_ts_us, duration_us);
180
+ }
181
+ );
175
182
176
183
writer.writeFooter ();
177
184
return 0 ;
0 commit comments