Skip to content

Commit 38d15ec

Browse files
committed
opt(log,trace):1.10.15, 在AsyncSink中添加写文件时长的监控;优化trace::Sink写文件效率
1 parent 0cfc600 commit 38d15ec

File tree

4 files changed

+66
-30
lines changed

4 files changed

+66
-30
lines changed

modules/log/async_sink.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <cstring>
2323
#include <algorithm>
2424
#include <iostream>
25+
#include <chrono>
2526

2627
constexpr uint32_t LOG_MAX_LEN = (100 << 10); //! 限定单条日志最大长度
2728

@@ -59,6 +60,8 @@ void AsyncSink::onLogFrontEnd(const LogContent *content)
5960

6061
void AsyncSink::onLogBackEndReadPipe(const void *data_ptr, size_t data_size)
6162
{
63+
auto start_ts = std::chrono::steady_clock::now();
64+
6265
buffer_.append(data_ptr, data_size);
6366

6467
bool is_need_flush = false;
@@ -81,6 +84,10 @@ void AsyncSink::onLogBackEndReadPipe(const void *data_ptr, size_t data_size)
8184

8285
if (is_need_flush)
8386
flushLog();
87+
88+
auto time_cost = std::chrono::steady_clock::now() - start_ts;
89+
if (time_cost > std::chrono::milliseconds(500))
90+
std::cerr << timestamp_str_ << " NOTICE: log sink cost > 500 ms, " << time_cost.count() / 1000 << " us" << std::endl;
8491
}
8592

8693
void AsyncSink::onLogBackEnd(const LogContent &content)

modules/trace/sink.cpp

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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

174175
void 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

215229
void 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;

modules/trace/sink.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class Sink {
113113
void onBackendRecvRecord(const RecordHeader &record, const char *name, const char *module,
114114
std::vector<uint8_t> &write_cache);
115115

116+
bool checkAndWriteNames();
117+
bool checkAndWriteModules();
118+
bool checkAndWriteThreads();
116119
bool checkAndCreateRecordFile();
117120

118121
bool isFilterPassed(const std::string &module) const;

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 10
24-
TBOX_VERSION_REVISION := 14
24+
TBOX_VERSION_REVISION := 15

0 commit comments

Comments
 (0)