Fixed async dump_backtrace

This commit is contained in:
gabime 2019-08-25 23:55:35 +03:00
parent 0258c47774
commit f3e379cf78
8 changed files with 87 additions and 16 deletions

View File

@ -38,7 +38,6 @@ void prepare_logdir()
void bench_c_string(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
{
logger->enable_backtrace(64);
const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
"lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, eu consequat sem "
"libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec fringilla dui sed "
@ -53,7 +52,6 @@ void bench_c_string(benchmark::State &state, std::shared_ptr<spdlog::logger> log
void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
{
logger->enable_backtrace(64);
int i = 0;
for (auto _ : state)
{
@ -63,7 +61,6 @@ void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logge
void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
{
logger->enable_backtrace(64);
int i = 0;
benchmark::DoNotOptimize(i); // prevent unused warnings
benchmark::DoNotOptimize(logger); // prevent unused warnings
@ -93,29 +90,50 @@ int main(int argc, char *argv[])
disabled_logger->set_level(spdlog::level::off);
benchmark::RegisterBenchmark("disabled-at-compile-time", bench_disabled_macro, disabled_logger);
benchmark::RegisterBenchmark("disabled-at-runtime", bench_logger, disabled_logger);
// with backtrace of 64
auto tracing_disabled_logger = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
tracing_disabled_logger->enable_backtrace(64);
benchmark::RegisterBenchmark("disabled-at-runtime/backtrace", bench_logger, tracing_disabled_logger);
auto null_logger_st = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_st>());
benchmark::RegisterBenchmark("null_sink_st (500_bytes c_str)", bench_c_string, std::move(null_logger_st));
benchmark::RegisterBenchmark("null_sink_st", bench_logger, null_logger_st);
// with backtrace of 64
auto tracing_null_logger_st = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_st>());
tracing_null_logger_st->enable_backtrace(64);
benchmark::RegisterBenchmark("null_sink_st/backtrace", bench_logger, tracing_null_logger_st);
// basic_st
auto basic_st = spdlog::basic_logger_st("basic_st", "latency_logs/basic_st.log", true);
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();
spdlog::drop("basic_st");
// with backtrace of 64
auto tracing_basic_st = spdlog::basic_logger_st("tracing_basic_st", "latency_logs/tracing_basic_st.log", true);
tracing_basic_st->enable_backtrace(64);
benchmark::RegisterBenchmark("basic_st/backtrace", bench_logger, std::move(tracing_basic_st))->UseRealTime();
spdlog::drop("tracing_basic_st");
// rotating st
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files);
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime();
spdlog::drop("rotating_st");
// with backtrace of 64
auto tracing_rotating_st =
spdlog::rotating_logger_st("tracing_rotating_st", "latency_logs/tracing_rotating_st.log", file_size, rotating_files);
benchmark::RegisterBenchmark("rotating_st/tracing", bench_logger, std::move(tracing_rotating_st))->UseRealTime();
spdlog::drop("tracing_rotating_st");
// daily st
auto daily_st = spdlog::daily_logger_mt("daily_st", "latency_logs/daily_st.log");
benchmark::RegisterBenchmark("daily_st", bench_logger, std::move(daily_st))->UseRealTime();
spdlog::drop("daily_st");
auto tracing_daily_st = spdlog::daily_logger_mt("tracing_daily_st", "latency_logs/daily_st.log");
benchmark::RegisterBenchmark("daily_st/tracing", bench_logger, std::move(tracing_daily_st))->UseRealTime();
spdlog::drop("tracing_daily_st");
// //
// // Multi threaded bench, 10 loggers using same logger concurrently
// //
//
// Multi threaded bench, 10 loggers using same logger concurrently
//
auto null_logger_mt = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
benchmark::RegisterBenchmark("null_sink_mt", bench_logger, null_logger_mt)->Threads(n_threads)->UseRealTime();

View File

@ -4,7 +4,9 @@
#pragma once
#ifndef SPDLOG_HEADER_ONLY
#include "spdlog/async_logger.h"
#endif
#include "spdlog/sinks/sink.h"
@ -73,12 +75,42 @@ SPDLOG_INLINE void spdlog::async_logger::backend_log_(const details::log_msg &in
}
}
SPDLOG_INLINE void spdlog::async_logger::backend_flush_(){SPDLOG_TRY{for (auto &sink : sinks_){sink->flush();
SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
{
SPDLOG_TRY
{
for (auto &sink : sinks_){sink->flush();}
}
SPDLOG_LOGGER_CATCH()
}
SPDLOG_INLINE void spdlog::async_logger::dump_backtrace_()
{
if (auto pool_ptr = thread_pool_.lock())
{
pool_ptr->post_dump_backtrace(shared_from_this(), overflow_policy_);
}
else
{
SPDLOG_THROW(spdlog_ex("async dumptrace: thread pool doesn't exist anymore"));
}
}
SPDLOG_INLINE void spdlog::async_logger::backend_dump_backtrace_()
{
SPDLOG_TRY
{
if (backtrace_sink_) {
auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
tracer->dump_backtrace(name());
}
}
SPDLOG_LOGGER_CATCH()
}
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
{
auto cloned = std::make_shared<spdlog::async_logger>(std::move(new_name), sinks_.begin(), sinks_.end(), thread_pool_, overflow_policy_);

View File

@ -51,11 +51,14 @@ public:
std::shared_ptr<logger> clone(std::string new_name) override;
protected:
void sink_it_(const details::log_msg &msg) override;
void flush_() override;
void dump_backtrace_() override ;
void backend_log_(const details::log_msg &incoming_log_msg);
void backend_flush_();
void backend_dump_backtrace_();
private:
std::weak_ptr<details::thread_pool> thread_pool_;

View File

@ -62,6 +62,11 @@ void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr, async_
post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
}
void SPDLOG_INLINE thread_pool::post_dump_backtrace(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
{
post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::dump_backtrace), overflow_policy);
}
size_t SPDLOG_INLINE thread_pool::overrun_counter()
{
return q_.overrun_counter();
@ -109,6 +114,12 @@ bool SPDLOG_INLINE thread_pool::process_next_msg_()
return true;
}
case async_msg_type ::dump_backtrace:
{
incoming_async_msg.worker_ptr->backend_dump_backtrace_();
return true;
}
case async_msg_type::terminate:
{
return false;

View File

@ -24,7 +24,8 @@ enum class async_msg_type
{
log,
flush,
terminate
terminate,
dump_backtrace
};
#include "spdlog/details/log_msg_buffer.h"
@ -96,6 +97,7 @@ public:
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
void post_dump_backtrace(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
size_t overrun_counter();
private:

View File

@ -158,13 +158,10 @@ SPDLOG_INLINE void logger::disable_backtrace()
SPDLOG_INLINE void logger::dump_backtrace()
{
if (backtrace_sink_)
{
auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
tracer->dump_backtrace(name());
}
dump_backtrace_();
}
// flush functions
SPDLOG_INLINE void logger::flush()
{
@ -239,6 +236,15 @@ SPDLOG_INLINE void logger::flush_()
}
}
SPDLOG_INLINE void logger::dump_backtrace_()
{
if (backtrace_sink_)
{
auto tracer = static_cast<sinks::backtrace_sink_mt *>(backtrace_sink_.get());
tracer->dump_backtrace(name());
}
}
SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
{
auto flush_level = flush_level_.load(std::memory_order_relaxed);

View File

@ -325,7 +325,6 @@ public:
void enable_backtrace(size_t n_messages = 16);
void disable_backtrace();
void dump_backtrace();
// flush functions
@ -355,6 +354,7 @@ protected:
virtual void sink_it_(const details::log_msg &msg);
virtual void flush_();
virtual void dump_backtrace_();
bool should_flush_(const details::log_msg &msg);
// handle errors during logging.

View File

@ -6,7 +6,6 @@
#endif
#include <mutex>
#include <chrono>
#include "spdlog/details/null_mutex.h"
#include "spdlog/async.h"