ClickHouse/dbms/src/Interpreters/TraceCollector.cpp

62 lines
1.7 KiB
C++
Raw Normal View History

2019-02-03 09:57:12 +00:00
#include "TraceCollector.h"
2019-02-03 21:30:45 +00:00
#include <common/Backtrace.h>
2019-02-03 09:57:12 +00:00
#include <common/logger_useful.h>
#include <IO/ReadHelpers.h>
#include <IO/ReadBufferFromFileDescriptor.h>
#include <Common/Exception.h>
#include <Common/QueryProfiler.h>
#include <Interpreters/TraceLog.h>
2019-02-03 21:30:45 +00:00
2019-02-03 09:57:12 +00:00
2019-02-09 22:40:47 +00:00
namespace DB
{
LazyPipe trace_pipe;
2019-02-03 09:57:12 +00:00
TraceCollector::TraceCollector(TraceLog * trace_log, std::future<void>&& stop_future)
2019-02-03 09:57:12 +00:00
: log(&Logger::get("TraceCollector"))
2019-02-03 21:30:45 +00:00
, trace_log(trace_log)
, stop_future(std::move(stop_future))
2019-02-03 09:57:12 +00:00
{
}
void TraceCollector::run()
{
DB::ReadBufferFromFileDescriptor in(trace_pipe.fds_rw[0]);
2019-02-03 09:57:12 +00:00
while (stop_future.wait_for(std::chrono::milliseconds(1)) == std::future_status::timeout)
2019-02-03 09:57:12 +00:00
{
2019-05-14 22:15:23 +00:00
Backtrace backtrace;
std::string query_id;
TimerType timer_type;
try {
2019-05-14 22:15:23 +00:00
DB::readPODBinary(backtrace, in);
DB::readStringBinary(query_id, in);
DB::readIntBinary(timer_type, in);
2019-02-09 22:40:47 +00:00
}
2019-05-14 22:15:23 +00:00
catch (...)
2019-02-09 22:40:47 +00:00
{
/// Pipe was closed - looks like server is about to shutdown
/// Let us wait for stop_future
continue;
}
2019-02-03 21:30:45 +00:00
2019-02-09 22:40:47 +00:00
if (trace_log != nullptr)
{
2019-05-14 22:15:23 +00:00
const auto size = backtrace.getSize();
const auto& frames = backtrace.getFrames();
2019-02-03 21:30:45 +00:00
std::vector<UInt64> trace;
2019-05-14 22:15:23 +00:00
trace.reserve(size);
for (size_t i = 0; i < size; i++)
trace.push_back(reinterpret_cast<uintptr_t>(frames[i]));
2019-02-03 21:30:45 +00:00
TraceLogElement element{std::time(nullptr), timer_type, query_id, trace};
2019-02-03 21:30:45 +00:00
trace_log->add(element);
}
2019-02-03 09:57:12 +00:00
}
}
}