Use StrongTypedef for ProfileEvents

Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
This commit is contained in:
Azat Khuzhin 2023-04-01 11:52:23 +02:00
parent 8d0e516310
commit c64f9e6f07
7 changed files with 15 additions and 14 deletions

View File

@ -87,7 +87,7 @@ void MetricsTransmitter::transmit(std::vector<ProfileEvents::Count> & prev_count
if (send_events)
{
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
for (ProfileEvents::Event i = ProfileEvents::Event(0), end = ProfileEvents::end(); i < end; ++i)
{
const auto counter = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
const auto counter_increment = counter - prev_counters[i];
@ -100,7 +100,7 @@ void MetricsTransmitter::transmit(std::vector<ProfileEvents::Count> & prev_count
if (send_events_cumulative)
{
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
for (ProfileEvents::Event i = ProfileEvents::Event(0), end = ProfileEvents::end(); i < end; ++i)
{
const auto counter = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
std::string key{ProfileEvents::getName(static_cast<ProfileEvents::Event>(i))};

View File

@ -497,10 +497,10 @@ The server successfully detected this situation and will download merged part fr
namespace ProfileEvents
{
#define M(NAME, DOCUMENTATION) extern const Event NAME = __COUNTER__;
#define M(NAME, DOCUMENTATION) extern const Event NAME = Event(__COUNTER__);
APPLY_FOR_EVENTS(M)
#undef M
constexpr Event END = __COUNTER__;
constexpr Event END = Event(__COUNTER__);
/// Global variable, initialized by zeros.
Counter global_counters_array[END] {};
@ -522,7 +522,7 @@ void Counters::resetCounters()
{
if (counters)
{
for (Event i = 0; i < num_counters; ++i)
for (Event i = Event(0); i < num_counters; ++i)
counters[i].store(0, std::memory_order_relaxed);
}
}
@ -540,7 +540,7 @@ Counters::Snapshot::Snapshot()
Counters::Snapshot Counters::getPartiallyAtomicSnapshot() const
{
Snapshot res;
for (Event i = 0; i < num_counters; ++i)
for (Event i = Event(0); i < num_counters; ++i)
res.counters_holder[i] = counters[i].load(std::memory_order_relaxed);
return res;
}
@ -616,7 +616,7 @@ CountersIncrement::CountersIncrement(Counters::Snapshot const & snapshot)
CountersIncrement::CountersIncrement(Counters::Snapshot const & after, Counters::Snapshot const & before)
{
init();
for (Event i = 0; i < Counters::num_counters; ++i)
for (Event i = Event(0); i < Counters::num_counters; ++i)
increment_holder[i] = static_cast<Increment>(after[i]) - static_cast<Increment>(before[i]);
}

View File

@ -1,7 +1,8 @@
#pragma once
#include <Common/VariableContext.h>
#include "base/types.h"
#include <base/types.h>
#include <base/strong_typedef.h>
#include <atomic>
#include <memory>
#include <cstddef>
@ -14,7 +15,7 @@
namespace ProfileEvents
{
/// Event identifier (index in array).
using Event = size_t;
using Event = StrongTypedef<size_t, struct EventTag>;
using Count = size_t;
using Increment = Int64;
using Counter = std::atomic<Count>;

View File

@ -97,7 +97,7 @@ void MetricLog::metricThreadFunction()
elem.milliseconds = timeInMilliseconds(current_time) - timeInSeconds(current_time) * 1000;
elem.profile_events.resize(ProfileEvents::end());
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
for (ProfileEvents::Event i = ProfileEvents::Event(0), end = ProfileEvents::end(); i < end; ++i)
{
const ProfileEvents::Count new_value = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
auto & old_value = prev_profile_events[i];

View File

@ -32,7 +32,7 @@ void dumpToMapColumn(const Counters::Snapshot & counters, DB::IColumn * column,
auto & value_column = tuple_column.getColumn(1);
size_t size = 0;
for (Event event = 0; event < Counters::num_counters; ++event)
for (Event event = Event(0); event < Counters::num_counters; ++event)
{
UInt64 value = counters[event];
@ -54,7 +54,7 @@ static void dumpProfileEvents(ProfileEventsSnapshot const & snapshot, DB::Mutabl
size_t rows = 0;
auto & name_column = columns[NAME_COLUMN_INDEX];
auto & value_column = columns[VALUE_COLUMN_INDEX];
for (Event event = 0; event < Counters::num_counters; ++event)
for (Event event = Event(0); event < Counters::num_counters; ++event)
{
Int64 value = snapshot.counters[event];

View File

@ -59,7 +59,7 @@ void PrometheusMetricsWriter::write(WriteBuffer & wb) const
{
if (send_events)
{
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
for (ProfileEvents::Event i = ProfileEvents::Event(0), end = ProfileEvents::end(); i < end; ++i)
{
const auto counter = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);

View File

@ -18,7 +18,7 @@ NamesAndTypesList StorageSystemEvents::getNamesAndTypes()
void StorageSystemEvents::fillData(MutableColumns & res_columns, ContextPtr context, const SelectQueryInfo &) const
{
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
for (ProfileEvents::Event i = ProfileEvents::Event(0), end = ProfileEvents::end(); i < end; ++i)
{
UInt64 value = ProfileEvents::global_counters[i];