ClickHouse/dbms/src/Interpreters/MetricLog.cpp

120 lines
3.7 KiB
C++
Raw Normal View History

2019-08-13 14:31:46 +00:00
#include <Interpreters/MetricLog.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDateTime.h>
namespace DB
{
Block MetricLogElement::createBlock()
{
ColumnsWithTypeAndName columns_with_type_and_name;
2019-08-14 13:08:07 +00:00
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeDate>(), "event_date");
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeDateTime>(), "event_time");
2019-08-15 16:09:43 +00:00
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeUInt64>(), "milliseconds");
2019-08-13 14:31:46 +00:00
//ProfileEvents
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
{
std::string name;
name += "ProfileEvent_";
name += ProfileEvents::getName(ProfileEvents::Event(i));
2019-08-14 13:08:07 +00:00
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeUInt64>(), std::move(name));
2019-08-13 14:31:46 +00:00
}
//CurrentMetrics
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
{
std::string name;
name += "CurrentMetric_";
name += CurrentMetrics::getName(ProfileEvents::Event(i));
2019-08-14 13:08:07 +00:00
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeInt64>(), std::move(name));
2019-08-13 14:31:46 +00:00
}
return Block(columns_with_type_and_name);
}
void MetricLogElement::appendToBlock(Block & block) const
{
MutableColumns columns = block.mutateColumns();
size_t iter = 0;
columns[iter++]->insert(DateLUT::instance().toDayNum(event_time));
columns[iter++]->insert(event_time);
2019-08-15 16:09:43 +00:00
columns[iter++]->insert(milliseconds);
2019-08-13 14:31:46 +00:00
//ProfileEvents
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
{
2019-08-14 12:54:41 +00:00
const UInt64 value = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
2019-08-13 14:31:46 +00:00
columns[iter++]->insert(value);
}
//CurrentMetrics
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
{
2019-08-13 16:17:18 +00:00
const UInt64 value = CurrentMetrics::values[i];
2019-08-13 14:31:46 +00:00
columns[iter++]->insert(value);
}
}
2019-08-13 16:47:12 +00:00
void MetricLog::startCollectMetric(size_t collect_interval_milliseconds_)
{
collect_interval_milliseconds = collect_interval_milliseconds_;
is_shutdown_metric_thread = false;
metric_flush_thread = ThreadFromGlobalPool([this] { metricThreadFunction(); });
}
void MetricLog::stopCollectMetric()
{
bool old_val = false;
if (!is_shutdown_metric_thread.compare_exchange_strong(old_val, true))
return;
metric_flush_thread.join();
}
2019-08-15 16:09:43 +00:00
inline UInt64 time_in_milliseconds(std::chrono::time_point<std::chrono::system_clock> timepoint)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(timepoint.time_since_epoch()).count();
}
inline UInt64 time_in_seconds(std::chrono::time_point<std::chrono::system_clock> timepoint)
{
return std::chrono::duration_cast<std::chrono::seconds>(timepoint.time_since_epoch()).count();
}
2019-08-13 16:47:12 +00:00
void MetricLog::metricThreadFunction()
{
2019-08-14 12:54:41 +00:00
auto desired_timepoint = std::chrono::system_clock::now();
2019-08-13 16:47:12 +00:00
while (true)
{
try
{
2019-08-14 12:54:41 +00:00
const auto prev_timepoint = desired_timepoint;
2019-08-13 16:47:12 +00:00
if (is_shutdown_metric_thread)
break;
MetricLogElement elem;
2019-08-14 12:54:41 +00:00
elem.event_time = std::chrono::system_clock::to_time_t(prev_timepoint);
2019-08-15 16:09:43 +00:00
elem.milliseconds = time_in_milliseconds(prev_timepoint) - time_in_seconds(prev_timepoint) * 1000;
2019-08-13 16:47:12 +00:00
this->add(elem);
2019-08-15 16:09:43 +00:00
while (desired_timepoint <= std::chrono::system_clock::now())
desired_timepoint += std::chrono::milliseconds(collect_interval_milliseconds);
2019-08-14 12:54:41 +00:00
std::this_thread::sleep_until(desired_timepoint);
2019-08-13 16:47:12 +00:00
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
}
2019-08-13 14:31:46 +00:00
}