2019-03-04 13:03:32 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-11-24 20:22:23 +00:00
|
|
|
#include <optional>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2019-03-04 13:03:32 +00:00
|
|
|
#include <signal.h>
|
2019-03-04 13:45:37 +00:00
|
|
|
#include <time.h>
|
2019-03-04 13:03:32 +00:00
|
|
|
|
2021-10-27 23:10:39 +00:00
|
|
|
#include <Common/config.h>
|
2020-04-16 12:31:57 +00:00
|
|
|
|
2019-07-25 22:35:47 +00:00
|
|
|
|
2019-03-04 13:03:32 +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
|
|
|
|
*
|
2020-08-08 00:47:03 +00:00
|
|
|
* Destructor tries to unset timer and restore previous signal handler.
|
2021-08-25 00:58:49 +00:00
|
|
|
* Note that signal handler implementation is defined by template parameter. See QueryProfilerReal and QueryProfilerCPU.
|
2019-07-05 13:48:47 +00:00
|
|
|
*/
|
2019-03-04 13:03:32 +00:00
|
|
|
template <typename ProfilerImpl>
|
|
|
|
class QueryProfilerBase
|
|
|
|
{
|
|
|
|
public:
|
2020-02-02 20:01:13 +00:00
|
|
|
QueryProfilerBase(const UInt64 thread_id, const int clock_type, UInt32 period, const int pause_signal_);
|
2019-07-10 20:47:39 +00:00
|
|
|
~QueryProfilerBase();
|
2019-03-04 13:03:32 +00:00
|
|
|
|
|
|
|
private:
|
2019-07-10 20:47:39 +00:00
|
|
|
void tryCleanup();
|
|
|
|
|
2019-03-04 13:03:32 +00:00
|
|
|
Poco::Logger * log;
|
|
|
|
|
2019-08-28 20:49:37 +00:00
|
|
|
#if USE_UNWIND
|
2019-03-04 13:03:32 +00:00
|
|
|
/// Timer id from timer_create(2)
|
2021-11-24 20:22:23 +00:00
|
|
|
std::optional<timer_t> timer_id;
|
2019-08-17 22:53:46 +00:00
|
|
|
#endif
|
2019-03-04 13:03:32 +00:00
|
|
|
|
|
|
|
/// Pause signal to interrupt threads to get traces
|
|
|
|
int pause_signal;
|
|
|
|
};
|
|
|
|
|
2019-07-05 13:48:47 +00:00
|
|
|
/// Query profiler with timer based on real clock
|
2019-03-04 13:03:32 +00:00
|
|
|
class QueryProfilerReal : public QueryProfilerBase<QueryProfilerReal>
|
|
|
|
{
|
|
|
|
public:
|
2020-02-02 20:01:13 +00:00
|
|
|
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-03-04 13:03:32 +00:00
|
|
|
};
|
|
|
|
|
2019-07-05 13:48:47 +00:00
|
|
|
/// Query profiler with timer based on CPU clock
|
2021-08-25 00:58:49 +00:00
|
|
|
class QueryProfilerCPU : public QueryProfilerBase<QueryProfilerCPU>
|
2019-03-04 13:03:32 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-08-25 00:58:49 +00:00
|
|
|
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);
|
2019-03-04 13:03:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|