2022-01-11 19:30:55 +00:00
|
|
|
#include <Common/TraceSender.h>
|
|
|
|
|
|
|
|
#include <IO/WriteBufferFromFileDescriptorDiscardOnFailure.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Common/StackTrace.h>
|
|
|
|
#include <Common/CurrentThread.h>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
/// Normally query_id is a UUID (string with a fixed length) but user can provide custom query_id.
|
|
|
|
/// Thus upper bound on query_id length should be introduced to avoid buffer overflow in signal handler.
|
|
|
|
///
|
|
|
|
/// And it cannot be large, since otherwise it will not fit into PIPE_BUF.
|
|
|
|
/// The performance test query ids can be surprisingly long like
|
|
|
|
/// `aggregating_merge_tree_simple_aggregate_function_string.query100.profile100`,
|
|
|
|
/// so make some allowance for them as well.
|
2022-11-25 12:59:50 +00:00
|
|
|
constexpr size_t QUERY_ID_MAX_LEN = 100;
|
2022-01-11 19:30:55 +00:00
|
|
|
static_assert(QUERY_ID_MAX_LEN <= std::numeric_limits<uint8_t>::max());
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
LazyPipeFDs TraceSender::pipe;
|
|
|
|
|
2022-11-24 19:54:39 +00:00
|
|
|
void TraceSender::send(TraceType trace_type, const StackTrace & stack_trace, Extras extras)
|
2022-01-11 19:30:55 +00:00
|
|
|
{
|
|
|
|
constexpr size_t buf_size = sizeof(char) /// TraceCollector stop flag
|
|
|
|
+ sizeof(UInt8) /// String size
|
|
|
|
+ QUERY_ID_MAX_LEN /// Maximum query_id length
|
|
|
|
+ sizeof(UInt8) /// Number of stack frames
|
|
|
|
+ sizeof(StackTrace::FramePointers) /// Collected stack trace, maximum capacity
|
|
|
|
+ sizeof(TraceType) /// trace type
|
|
|
|
+ sizeof(UInt64) /// thread_id
|
2022-07-07 11:54:41 +00:00
|
|
|
+ sizeof(Int64) /// size
|
2023-01-16 14:10:31 +00:00
|
|
|
+ sizeof(void *) /// ptr
|
2022-11-24 19:54:39 +00:00
|
|
|
+ sizeof(ProfileEvents::Event) /// event
|
|
|
|
+ sizeof(ProfileEvents::Count); /// increment
|
2022-01-11 19:30:55 +00:00
|
|
|
|
|
|
|
/// Write should be atomic to avoid overlaps
|
|
|
|
/// (since recursive collect() is possible)
|
|
|
|
static_assert(PIPE_BUF >= 512);
|
2022-11-24 19:54:39 +00:00
|
|
|
static_assert(buf_size <= PIPE_BUF, "Only write of PIPE_BUF to pipe is atomic and the minimal known PIPE_BUF across supported platforms is 512");
|
2022-01-11 19:30:55 +00:00
|
|
|
|
|
|
|
char buffer[buf_size];
|
|
|
|
WriteBufferFromFileDescriptorDiscardOnFailure out(pipe.fds_rw[1], buf_size, buffer);
|
|
|
|
|
2022-07-18 17:31:34 +00:00
|
|
|
std::string_view query_id;
|
2022-01-11 19:30:55 +00:00
|
|
|
UInt64 thread_id;
|
|
|
|
|
|
|
|
if (CurrentThread::isInitialized())
|
|
|
|
{
|
|
|
|
query_id = CurrentThread::getQueryId();
|
2022-07-19 16:44:31 +00:00
|
|
|
if (query_id.size() > QUERY_ID_MAX_LEN)
|
|
|
|
query_id.remove_suffix(query_id.size() - QUERY_ID_MAX_LEN);
|
2022-01-11 19:30:55 +00:00
|
|
|
|
|
|
|
thread_id = CurrentThread::get().thread_id;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
thread_id = MainThreadStatus::get()->thread_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeChar(false, out); /// true if requested to stop the collecting thread.
|
|
|
|
|
2022-07-18 17:31:34 +00:00
|
|
|
writeBinary(static_cast<uint8_t>(query_id.size()), out);
|
|
|
|
out.write(query_id.data(), query_id.size());
|
2022-01-11 19:30:55 +00:00
|
|
|
|
|
|
|
size_t stack_trace_size = stack_trace.getSize();
|
|
|
|
size_t stack_trace_offset = stack_trace.getOffset();
|
2022-04-18 08:18:31 +00:00
|
|
|
writeIntBinary(static_cast<UInt8>(stack_trace_size - stack_trace_offset), out);
|
2022-01-11 19:30:55 +00:00
|
|
|
for (size_t i = stack_trace_offset; i < stack_trace_size; ++i)
|
|
|
|
writePODBinary(stack_trace.getFramePointers()[i], out);
|
|
|
|
|
|
|
|
writePODBinary(trace_type, out);
|
|
|
|
writePODBinary(thread_id, out);
|
2022-11-24 19:54:39 +00:00
|
|
|
writePODBinary(extras.size, out);
|
2023-01-16 14:10:31 +00:00
|
|
|
writePODBinary(UInt64(extras.ptr), out);
|
2022-11-24 19:54:39 +00:00
|
|
|
writePODBinary(extras.event, out);
|
|
|
|
writePODBinary(extras.increment, out);
|
2022-01-11 19:30:55 +00:00
|
|
|
|
|
|
|
out.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|