2525#include < cstring>
2626#include < vector>
2727#include < sstream>
28+ #include < chrono>
2829
2930#include < sys/syscall.h>
3031#include < tbox/base/defines.h>
@@ -173,6 +174,14 @@ void Sink::commitRecord(const char *name, const char *module, uint32_t line, uin
173174
174175void Sink::onBackendRecvData (const void *data, size_t size)
175176{
177+ auto start_ts = std::chrono::steady_clock::now ();
178+
179+ if (!checkAndCreateRecordFile () ||
180+ !checkAndWriteNames () ||
181+ !checkAndWriteModules () ||
182+ !checkAndWriteThreads ())
183+ return ;
184+
176185 buffer_.append (data, size);
177186 std::vector<uint8_t > write_cache;
178187
@@ -200,6 +209,7 @@ void Sink::onBackendRecvData(const void *data, size_t size)
200209 }
201210
202211 if (!write_cache.empty ()) {
212+
203213 auto wsize = ::write (curr_record_fd_, write_cache.data (), write_cache.size ());
204214 if (wsize != static_cast <ssize_t >(write_cache.size ())) {
205215 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)
210220 if (total_write_size_ >= record_file_max_size_)
211221 CHECK_CLOSE_RESET_FD (curr_record_fd_);
212222 }
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 );
213227}
214228
215229void Sink::onBackendRecvRecord (const RecordHeader &record, const char *name, const char *module , std::vector<uint8_t > &write_cache)
216230{
217231 if (!isFilterPassed (module ))
218232 return ;
219233
220- if (!checkAndCreateRecordFile ())
221- return ;
222-
223234 auto thread_index = allocThreadIndex (record.thread_id );
224235 auto name_index = allocNameIndex (name, record.line );
225236 auto module_index = allocModuleIndex (module );
@@ -285,7 +296,7 @@ bool Sink::checkAndCreateRecordFile()
285296 return true ;
286297}
287298
288- Sink::Index Sink::allocNameIndex ( const std::string &name, uint32_t line )
299+ bool Sink::checkAndWriteNames ( )
289300{
290301 // ! 如果文件不存在了,则重写所有的名称列表
291302 if (!util::fs::IsFileExist (name_list_filename_)) {
@@ -297,24 +308,15 @@ Sink::Index Sink::allocNameIndex(const std::string &name, uint32_t line)
297308 for (auto &content: name_vec)
298309 oss << content << ENDLINE;
299310
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_);
301312 }
302313
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 ;
313315}
314316
315- Sink::Index Sink::allocModuleIndex ( const std::string & module )
317+ bool Sink::checkAndWriteModules ( )
316318{
317- // ! 如果文件不存在了,则重写所有的名称列表
319+ // ! 如果文件不存在了,则重写所有的模块列表
318320 if (!util::fs::IsFileExist (module_list_filename_)) {
319321 std::vector<std::string> module_vec (module_to_index_map_.size ());
320322 for (auto &item : module_to_index_map_)
@@ -324,21 +326,13 @@ Sink::Index Sink::allocModuleIndex(const std::string &module)
324326 for (auto &module : module_vec)
325327 oss << module << ENDLINE;
326328
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_);
328330 }
329331
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 ;
339333}
340334
341- Sink::Index Sink::allocThreadIndex ( long thread_id )
335+ bool Sink::checkAndWriteThreads ( )
342336{
343337 // ! 如果文件不存在了,则重写所有的线程列表
344338 if (!util::fs::IsFileExist (thread_list_filename_)) {
@@ -350,9 +344,41 @@ Sink::Index Sink::allocThreadIndex(long thread_id)
350344 for (auto thread_id : thread_vec)
351345 oss << thread_id << ENDLINE;
352346
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_);
354348 }
355349
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+ {
356382 auto iter = thread_to_index_map_.find (thread_id);
357383 if (iter != thread_to_index_map_.end ())
358384 return iter->second ;
0 commit comments