ClickHouse/dbms/src/Interpreters/TraceCollector.cpp

59 lines
1.5 KiB
C++
Raw Normal View History

2019-02-03 09:57:12 +00:00
#include "TraceCollector.h"
2019-05-19 20:22:44 +00:00
#include <common/Sleep.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-09 22:40:47 +00:00
namespace DB
{
2019-02-03 09:57:12 +00:00
2019-05-19 20:22:44 +00:00
TraceCollector::TraceCollector(std::shared_ptr<TraceLog> trace_log)
2019-02-03 09:57:12 +00:00
: log(&Logger::get("TraceCollector"))
2019-02-03 21:30:45 +00:00
, trace_log(trace_log)
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
2019-05-19 20:22:44 +00:00
while (true)
2019-02-03 09:57:12 +00:00
{
2019-05-19 20:22:44 +00:00
SleepForMicroseconds(1);
bool is_last;
DB::readIntBinary(is_last, in);
2019-05-19 20:25:14 +00:00
if (is_last)
2019-05-19 20:22:44 +00:00
break;
std::string query_id;
2019-05-19 20:22:44 +00:00
Backtrace backtrace;
TimerType timer_type;
2019-05-19 20:22:44 +00:00
DB::readStringBinary(query_id, in);
DB::readPODBinary(backtrace, in);
DB::readIntBinary(timer_type, in);
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
}
}
}