25
25
#include < cstring>
26
26
#include < vector>
27
27
#include < sstream>
28
+ #include < chrono>
28
29
29
30
#include < sys/syscall.h>
30
31
#include < tbox/base/defines.h>
@@ -173,6 +174,14 @@ void Sink::commitRecord(const char *name, const char *module, uint32_t line, uin
173
174
174
175
void Sink::onBackendRecvData (const void *data, size_t size)
175
176
{
177
+ auto start_ts = std::chrono::steady_clock::now ();
178
+
179
+ if (!checkAndCreateRecordFile () ||
180
+ !checkAndWriteNames () ||
181
+ !checkAndWriteModules () ||
182
+ !checkAndWriteThreads ())
183
+ return ;
184
+
176
185
buffer_.append (data, size);
177
186
std::vector<uint8_t > write_cache;
178
187
@@ -200,6 +209,7 @@ void Sink::onBackendRecvData(const void *data, size_t size)
200
209
}
201
210
202
211
if (!write_cache.empty ()) {
212
+
203
213
auto wsize = ::write (curr_record_fd_, write_cache.data (), write_cache.size ());
204
214
if (wsize != static_cast <ssize_t >(write_cache.size ())) {
205
215
LogErrno (errno, " write record file '%s' fail" , curr_record_filename_.c_str ());
@@ -210,16 +220,17 @@ void Sink::onBackendRecvData(const void *data, size_t size)
210
220
if (total_write_size_ >= record_file_max_size_)
211
221
CHECK_CLOSE_RESET_FD (curr_record_fd_);
212
222
}
223
+
224
+ auto time_cost = std::chrono::steady_clock::now () - start_ts;
225
+ if (time_cost > std::chrono::milliseconds (100 ))
226
+ LogNotice (" trace sink cost > 100 ms, %lu us" , time_cost.count () / 1000 );
213
227
}
214
228
215
229
void Sink::onBackendRecvRecord (const RecordHeader &record, const char *name, const char *module , std::vector<uint8_t > &write_cache)
216
230
{
217
231
if (!isFilterPassed (module ))
218
232
return ;
219
233
220
- if (!checkAndCreateRecordFile ())
221
- return ;
222
-
223
234
auto thread_index = allocThreadIndex (record.thread_id );
224
235
auto name_index = allocNameIndex (name, record.line );
225
236
auto module_index = allocModuleIndex (module );
@@ -285,7 +296,7 @@ bool Sink::checkAndCreateRecordFile()
285
296
return true ;
286
297
}
287
298
288
- Sink::Index Sink::allocNameIndex ( const std::string &name, uint32_t line )
299
+ bool Sink::checkAndWriteNames ( )
289
300
{
290
301
// ! 如果文件不存在了,则重写所有的名称列表
291
302
if (!util::fs::IsFileExist (name_list_filename_)) {
@@ -297,24 +308,15 @@ Sink::Index Sink::allocNameIndex(const std::string &name, uint32_t line)
297
308
for (auto &content: name_vec)
298
309
oss << content << ENDLINE;
299
310
300
- util::fs::WriteStringToTextFile (name_list_filename_, oss.str (), is_file_sync_enabled_);
311
+ return util::fs::WriteStringToTextFile (name_list_filename_, oss.str (), is_file_sync_enabled_);
301
312
}
302
313
303
- std::string content = name + " at L" + std::to_string (line);
304
- auto iter = name_to_index_map_.find (content);
305
- if (iter != name_to_index_map_.end ())
306
- return iter->second ;
307
-
308
- auto new_index = next_name_index_++;
309
- name_to_index_map_[content] = new_index;
310
-
311
- util::fs::AppendStringToTextFile (name_list_filename_, content + ENDLINE, is_file_sync_enabled_);
312
- return new_index;
314
+ return true ;
313
315
}
314
316
315
- Sink::Index Sink::allocModuleIndex ( const std::string & module )
317
+ bool Sink::checkAndWriteModules ( )
316
318
{
317
- // ! 如果文件不存在了,则重写所有的名称列表
319
+ // ! 如果文件不存在了,则重写所有的模块列表
318
320
if (!util::fs::IsFileExist (module_list_filename_)) {
319
321
std::vector<std::string> module_vec (module_to_index_map_.size ());
320
322
for (auto &item : module_to_index_map_)
@@ -324,21 +326,13 @@ Sink::Index Sink::allocModuleIndex(const std::string &module)
324
326
for (auto &module : module_vec)
325
327
oss << module << ENDLINE;
326
328
327
- util::fs::WriteStringToTextFile (module_list_filename_, oss.str (), is_file_sync_enabled_);
329
+ return util::fs::WriteStringToTextFile (module_list_filename_, oss.str (), is_file_sync_enabled_);
328
330
}
329
331
330
- auto iter = module_to_index_map_.find (module );
331
- if (iter != module_to_index_map_.end ())
332
- return iter->second ;
333
-
334
- auto new_index = next_module_index_++;
335
- module_to_index_map_[module ] = new_index;
336
-
337
- util::fs::AppendStringToTextFile (module_list_filename_, module + ENDLINE, is_file_sync_enabled_);
338
- return new_index;
332
+ return true ;
339
333
}
340
334
341
- Sink::Index Sink::allocThreadIndex ( long thread_id )
335
+ bool Sink::checkAndWriteThreads ( )
342
336
{
343
337
// ! 如果文件不存在了,则重写所有的线程列表
344
338
if (!util::fs::IsFileExist (thread_list_filename_)) {
@@ -350,9 +344,41 @@ Sink::Index Sink::allocThreadIndex(long thread_id)
350
344
for (auto thread_id : thread_vec)
351
345
oss << thread_id << ENDLINE;
352
346
353
- util::fs::WriteStringToTextFile (thread_list_filename_, oss.str (), is_file_sync_enabled_);
347
+ return util::fs::WriteStringToTextFile (thread_list_filename_, oss.str (), is_file_sync_enabled_);
354
348
}
355
349
350
+ return true ;
351
+ }
352
+
353
+ Sink::Index Sink::allocNameIndex (const std::string &name, uint32_t line)
354
+ {
355
+ std::string content = name + " at L" + std::to_string (line);
356
+ auto iter = name_to_index_map_.find (content);
357
+ if (iter != name_to_index_map_.end ())
358
+ return iter->second ;
359
+
360
+ auto new_index = next_name_index_++;
361
+ name_to_index_map_[content] = new_index;
362
+
363
+ util::fs::AppendStringToTextFile (name_list_filename_, content + ENDLINE, is_file_sync_enabled_);
364
+ return new_index;
365
+ }
366
+
367
+ Sink::Index Sink::allocModuleIndex (const std::string &module )
368
+ {
369
+ auto iter = module_to_index_map_.find (module );
370
+ if (iter != module_to_index_map_.end ())
371
+ return iter->second ;
372
+
373
+ auto new_index = next_module_index_++;
374
+ module_to_index_map_[module ] = new_index;
375
+
376
+ util::fs::AppendStringToTextFile (module_list_filename_, module + ENDLINE, is_file_sync_enabled_);
377
+ return new_index;
378
+ }
379
+
380
+ Sink::Index Sink::allocThreadIndex (long thread_id)
381
+ {
356
382
auto iter = thread_to_index_map_.find (thread_id);
357
383
if (iter != thread_to_index_map_.end ())
358
384
return iter->second ;
0 commit comments