2016-10-24 04:06:27 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <string>
|
2019-01-14 19:22:09 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2020-04-19 20:49:13 +00:00
|
|
|
#include <Common/MemoryStatisticsOS.h>
|
2016-10-24 04:06:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class Context;
|
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
typedef double AsynchronousMetricValue;
|
|
|
|
typedef std::unordered_map<std::string, AsynchronousMetricValue> AsynchronousMetricValues;
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2020-06-26 00:16:58 +00:00
|
|
|
/** Periodically (by default, each minute, starting at 30 seconds offset)
|
2016-10-24 04:06:27 +00:00
|
|
|
* calculates and updates some metrics,
|
|
|
|
* that are not updated automatically (so, need to be asynchronously calculated).
|
|
|
|
*/
|
|
|
|
class AsynchronousMetrics
|
|
|
|
{
|
|
|
|
public:
|
2020-06-26 00:16:58 +00:00
|
|
|
AsynchronousMetrics(Context & context_, int update_period_seconds)
|
|
|
|
: context(context_),
|
|
|
|
update_period(update_period_seconds),
|
|
|
|
thread([this] { run(); })
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
}
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
~AsynchronousMetrics();
|
2016-10-24 04:06:27 +00:00
|
|
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Returns copy of all values.
|
2020-06-10 19:17:30 +00:00
|
|
|
AsynchronousMetricValues getValues() const;
|
2016-10-24 04:06:27 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Context & context;
|
2020-06-26 00:16:58 +00:00
|
|
|
const std::chrono::seconds update_period;
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
mutable std::mutex mutex;
|
2017-04-01 07:20:54 +00:00
|
|
|
std::condition_variable wait_cond;
|
2020-06-10 19:17:30 +00:00
|
|
|
bool quit {false};
|
|
|
|
AsynchronousMetricValues values;
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2020-04-21 18:49:19 +00:00
|
|
|
#if defined(OS_LINUX)
|
2020-04-19 20:49:13 +00:00
|
|
|
MemoryStatisticsOS memory_stat;
|
2020-04-21 18:49:19 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-14 19:22:09 +00:00
|
|
|
ThreadFromGlobalPool thread;
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void run();
|
|
|
|
void update();
|
2016-10-24 04:06:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|