ClickHouse/dbms/Common/QueryProfiler.h

72 lines
1.7 KiB
C++
Raw Normal View History

#pragma once
2019-07-10 20:47:39 +00:00
#include <Core/Types.h>
#include <Common/config.h>
#include <common/config_common.h>
#include <signal.h>
2019-03-04 13:45:37 +00:00
#include <time.h>
2019-07-25 22:35:47 +00:00
namespace Poco
{
class Logger;
}
namespace DB
{
2019-07-05 13:48:47 +00:00
/**
* Query profiler implementation for selected thread.
*
* This class installs timer and signal handler on creation to:
* 1. periodically pause given thread
* 2. collect thread's current stack trace
* 3. write collected stack trace to trace_pipe for TraceCollector
*
* Desctructor tries to unset timer and restore previous signal handler.
* Note that signal handler implementation is defined by template parameter. See QueryProfilerReal and QueryProfilerCpu.
*/
template <typename ProfilerImpl>
class QueryProfilerBase
{
public:
QueryProfilerBase(const UInt64 thread_id, const int clock_type, UInt32 period, const int pause_signal_);
2019-07-10 20:47:39 +00:00
~QueryProfilerBase();
private:
2019-07-10 20:47:39 +00:00
void tryCleanup();
Poco::Logger * log;
#if USE_UNWIND
/// Timer id from timer_create(2)
timer_t timer_id = nullptr;
#endif
/// Pause signal to interrupt threads to get traces
int pause_signal;
/// Previous signal handler to restore after query profiler exits
struct sigaction * previous_handler = nullptr;
};
2019-07-05 13:48:47 +00:00
/// Query profiler with timer based on real clock
class QueryProfilerReal : public QueryProfilerBase<QueryProfilerReal>
{
public:
QueryProfilerReal(const UInt64 thread_id, const UInt32 period);
2019-07-10 20:47:39 +00:00
static void signalHandler(int sig, siginfo_t * info, void * context);
};
2019-07-05 13:48:47 +00:00
/// Query profiler with timer based on CPU clock
class QueryProfilerCpu : public QueryProfilerBase<QueryProfilerCpu>
{
public:
QueryProfilerCpu(const UInt64 thread_id, const UInt32 period);
2019-07-10 20:47:39 +00:00
static void signalHandler(int sig, siginfo_t * info, void * context);
};
}